Skip to content

Commit

Permalink
Add get_row_pitch helper function
Browse files Browse the repository at this point in the history
Added premake solution to more easily pick up where we left off and modify properties
Fix some warnings and non-standard usages
  • Loading branch information
redorav committed Feb 20, 2023
1 parent c52b7d7 commit 510d812
Show file tree
Hide file tree
Showing 11 changed files with 142 additions and 127 deletions.
15 changes: 15 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# User files
*.suo
*.user
*.sln.docstates

# Build folders

workspace/

# Visual Studio Intermediate files
*.opendb
*.db

# Temporary
*.tmp
3 changes: 3 additions & 0 deletions Visual Studio 2010.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@echo off
start /B /W premake/win/premake5 --file=ddspp.lua vs2010
pause
26 changes: 20 additions & 6 deletions ddspp.h
Original file line number Diff line number Diff line change
Expand Up @@ -362,8 +362,8 @@ namespace ddspp
unsigned int rowPitch; // Row pitch for mip 0
unsigned int depthPitch; // Size of mip 0
unsigned int bitsPerPixelOrBlock; // If compressed bits per block, else bits per pixel
unsigned int blockWidth;
unsigned int blockHeight;
unsigned int blockWidth; // Width of block in pixels (1 if uncompressed)
unsigned int blockHeight;// Height of block in pixels (1 if uncompressed)
bool compressed;
bool srgb;
unsigned int headerSize; // Actual size of header, use this to get to image data
Expand Down Expand Up @@ -599,13 +599,27 @@ namespace ddspp
return;
}

inline ddspp_constexpr unsigned int get_row_pitch(unsigned int width, unsigned int bitsPerPixelOrBlock, unsigned int blockWidth, unsigned int mip)
{
// Shift width by mipmap index, round to next block size and round to next byte (for the rare less than 1 byte per pixel formats)
// E.g. width = 119, mip = 3, BC1 compression
// ((((119 >> 2) + 4 - 1) / 4) * 64) / 8 = 64 bytes
return ((((width >> mip) + blockWidth - 1) / blockWidth) * bitsPerPixelOrBlock + 7) / 8;
}

// Returns number of bytes for each row of a given mip. Valid range is [0, desc.numMips)
inline ddspp_constexpr unsigned int get_row_pitch(const Descriptor& desc, unsigned int mip)
{
return get_row_pitch(desc.width, desc.bitsPerPixelOrBlock, desc.blockWidth, mip);
}

inline Result decode_header(unsigned char* sourceData, Descriptor& desc)
{
unsigned int magic = *reinterpret_cast<unsigned int*>(sourceData); // First 4 bytes are the magic DDS number

if (magic != DDS_MAGIC)
{
return Result::Error;
return ddspp::Error;
}

const Header header = *reinterpret_cast<const Header*>(sourceData + sizeof(DDS_MAGIC));
Expand Down Expand Up @@ -859,7 +873,7 @@ namespace ddspp
{
if((header.caps2 & DDS_HEADER_CAPS2_CUBEMAP_ALLFACES) != DDS_HEADER_CAPS2_CUBEMAP_ALLFACES)
{
return Result::Error;
return ddspp::Error;
}

desc.type = Cubemap;
Expand Down Expand Up @@ -909,11 +923,11 @@ namespace ddspp
desc.bitsPerPixelOrBlock = get_bits_per_pixel_or_block(desc.format);
get_block_size(desc.format, desc.blockWidth, desc.blockHeight);

desc.rowPitch = desc.width * desc.bitsPerPixelOrBlock / (8 * desc.blockWidth);
desc.rowPitch = get_row_pitch(desc.width, desc.bitsPerPixelOrBlock, desc.blockWidth, 0);
desc.depthPitch = desc.rowPitch * desc.height / desc.blockHeight;
desc.headerSize = sizeof(DDS_MAGIC) + sizeof(Header) + (dxt10Extension ? sizeof(HeaderDXT10) : 0);

return Result::Success;
return ddspp::Success;
}

inline void encode_header(const DXGIFormat format, const unsigned int width, const unsigned int height, const unsigned int depth,
Expand Down
95 changes: 95 additions & 0 deletions ddspp.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
Workspace = "workspace/".._ACTION

-- Compilers

-- x86/x64
PlatformMSVC64 = "MSVC 64"
PlatformOSX64 = "OSX 64"
PlatformLinux64_GCC = "Linux64_GCC"
PlatformLinux64_Clang = "Linux64_Clang"

isMacBuild = _ACTION == "xcode4"
isLinuxBuild = _ACTION == "gmake2"
isWindowsBuild = not isMacBuild and not isLinuxBuild
supportsARMBuild = _ACTION == "vs2017" or _ACTION == "vs2019" or _ACTION == "vs2022"

workspace("dds++")
configurations { "Debug", "Release" }
location (Workspace)

flags
{
"multiprocessorcompile", -- /MP
}

includedirs
{
includeDir,
}

cppdialect("c++11")

if(isMacBuild) then

platforms { PlatformOSX64 }
toolset("clang")
architecture("x64")
buildoptions { "-std=c++11 -Wno-unused-variable" }
linkoptions { "-stdlib=libc++" }

elseif(isLinuxBuild) then

platforms { PlatformLinux64_GCC, PlatformLinux64_Clang }
architecture("x64")
buildoptions { "-std=c++11 -Wno-unused-variable" }

filter { "platforms:"..PlatformLinux64_GCC }
toolset("gcc")

filter { "platforms:"..PlatformLinux64_Clang }
toolset("clang")

else

platforms
{
PlatformMSVC64
}

local llvmToolset;

if (_ACTION == "vs2015") then
llvmToolset = "msc-llvm-vs2014";
elseif(_ACTION == "vs2017") then
llvmToolset = "msc-llvm";
else
llvmToolset = "msc-clangcl";
end

startproject(UnitTestProject)

filter { "platforms:"..PlatformMSVC64 }
toolset("msc")
architecture("x64")

filter{}
end

filter { "configurations:Debug" }
defines { "DEBUG" }
symbols ("full")
optimize("debug")

filter { "configurations:Release" }
defines { "NDEBUG" }
inlining("auto")
optimize("speed")

project ('dds++')
kind("consoleapp")

files
{
"*.cpp",
"*.h",
}
20 changes: 0 additions & 20 deletions ddspp.sln

This file was deleted.

71 changes: 0 additions & 71 deletions ddspp.vcxproj

This file was deleted.

27 changes: 0 additions & 27 deletions ddspp.vcxproj.filters

This file was deleted.

12 changes: 9 additions & 3 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,17 @@ int main()
fread(data, sizeof(*data), ddsSize, fh);

ddspp::Descriptor desc;
unsigned char* ddsData = ddspp::decode_header(data, desc);
ddspp::Result ddsResult = ddspp::decode_header(data, desc);

ddspp::Header h;
ddspp::Header header;
ddspp::HeaderDXT10 h10;
ddspp::encode_header(desc.format, desc.width, desc.height, desc.depth, desc.type, desc.numMips, desc.arraySize, h, h10);
ddspp::encode_header(desc.format, desc.width, desc.height, desc.depth, desc.type, desc.numMips, desc.arraySize, header, h10);

unsigned int rowPitch512 = ddspp::get_row_pitch(512, 64, 4, 0);

unsigned int rowPitch = ddspp::get_row_pitch(29, 8, 1, 0);

unsigned int rowPitchDesc = ddspp::get_row_pitch(desc, 0);

unsigned int offset = ddspp::get_offset(desc, 4, 0);

Expand Down
Binary file added premake/linux/premake5
Binary file not shown.
Binary file added premake/osx/premake5
Binary file not shown.
Binary file added premake/win/premake5.exe
Binary file not shown.

0 comments on commit 510d812

Please sign in to comment.