diff --git a/TextureConverter/TextureConverter.cpp b/TextureConverter/TextureConverter.cpp new file mode 100644 index 0000000..dbac97f --- /dev/null +++ b/TextureConverter/TextureConverter.cpp @@ -0,0 +1,126 @@ +#include +#include +#include +#include +#include + + + +int Morton(int t, int sx, int sy) { + int num1; + int num2 = num1 = 1; + int num3 = t; + int num4 = sx; + int num5 = sy; + int num6 = 0; + int num7 = 0; + + while (num4 > 1 || num5 > 1) { + if (num4 > 1) { + num6 += num2 * (num3 & 1); + num3 >>= 1; + num2 *= 2; + num4 >>= 1; + } + if (num5 > 1) { + num7 += num1 * (num3 & 1); + num3 >>= 1; + num1 *= 2; + num5 >>= 1; + } + } + + return num7 * sx + num6; +} + +std::vector DoSwizzle(std::vector data, int width, int height, int blockSize, bool unswizzle) { + auto processed = std::vector(); + processed.resize(data.size()); + + auto heightTexels = height / 4; + auto heightTexelsAligned = (heightTexels + 7) / 8; + int widthTexels = width / 4; + auto widthTexelsAligned = (widthTexels + 7) / 8; + auto dataIndex = 0; + + for (int y = 0; y < heightTexelsAligned; ++y) { + for (int x = 0; x < widthTexelsAligned; ++x) { + for (int t = 0; t < 64; ++t) { + int pixelIndex = Morton(t, 8, 8); + int num8 = pixelIndex / 8; + int num9 = pixelIndex % 8; + auto yOffset = (y * 8) + num8; + auto xOffset = (x * 8) + num9; + + if (xOffset < widthTexels && yOffset < heightTexels) { + auto destPixelIndex = yOffset * widthTexels + xOffset; + int destIndex = blockSize * destPixelIndex; + + if (unswizzle) + memcpy(processed.data() + destIndex, data.data() + dataIndex, blockSize); + else + memcpy(processed.data() + dataIndex, data.data() + destIndex, blockSize); + } + + dataIndex += blockSize; + } + } + } + + return processed; +} + + + + +std::vector Swizzle(std::vector data, int width, int height, int blockSize) { + return DoSwizzle(data, width, height, blockSize, false); +} + +std::vector UnSwizzle(std::vector data, int width, int height, int blockSize) { + return DoSwizzle(data, width, height, blockSize, true); +} + + +int main() +{ + + + DirectX::ScratchImage image; + auto initResult = image.Initialize2D(DXGI_FORMAT::DXGI_FORMAT_BC4_UNORM, 1024, 1024, 1, 1); + auto img = image.GetImage(0, 0, 0); + + auto cnt = image.GetImageCount(); + + auto images = image.GetImages(); + for (int i = 0; i < cnt; ++i) { + auto img = images + i; + std::cout << "ID: " << i << " W: " << img->width << " H: " << img->height << " Row pitch: " << img->rowPitch << " Slice pitch: " << img->slicePitch << std::endl; + + } + + + + + //2097152 + //"" + auto readSize = img->slicePitch; + auto inFile = std::ifstream("D:/TextureExport/blt-armor-a-new-spec-mask.tga.1024_1024_unswizzled_page0_file103.dds", std::ios::binary); + + inFile.read((char*)img->pixels, readSize); + inFile.close(); + + /* std::vector mem; + mem.resize(img->slicePitch); + memcpy(mem.data(), img->pixels, img->slicePitch); + + auto unswizzled = UnSwizzle(mem, 1024, 1024, 8); + + memcpy(img->pixels, unswizzled.data(), img->slicePitch);*/ + + DirectX::SaveToDDSFile(*img, DirectX::DDS_FLAGS::DDS_FLAGS_NONE, L"F:/out.dds"); + + //img->pixels + + std::cout << "Hello World!\n"; +} diff --git a/TextureConverter/TextureConverter.vcxproj b/TextureConverter/TextureConverter.vcxproj new file mode 100644 index 0000000..44581b4 --- /dev/null +++ b/TextureConverter/TextureConverter.vcxproj @@ -0,0 +1,150 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 16.0 + Win32Proj + {41215da9-c90e-4fe0-acab-34d6673f52d2} + TextureConverter + 10.0 + + + + Application + true + v142 + Unicode + + + Application + false + v142 + true + Unicode + + + Application + true + v142 + Unicode + + + Application + false + v142 + true + Unicode + + + + + + + + + + + + + + + + + + + + + true + + + false + + + true + + + false + + + + Level3 + true + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + Level3 + true + _DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + F:\DirectXTex\DirectXTex + + + Console + true + F:\DirectXTex\DirectXTex\Bin\Desktop_2019_Win10\x64\Debug + DirectXTex.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies) + + + + + Level3 + true + true + true + NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + + + + + \ No newline at end of file