Skip to content

Commit

Permalink
StandardSwizzle3D merged into main function
Browse files Browse the repository at this point in the history
  • Loading branch information
walbourn committed Sep 25, 2024
1 parent 496177f commit 09ff9e8
Show file tree
Hide file tree
Showing 2 changed files with 155 additions and 129 deletions.
10 changes: 7 additions & 3 deletions DirectXTex/DirectXTex.h
Original file line number Diff line number Diff line change
Expand Up @@ -728,9 +728,13 @@ namespace DirectX
_In_reads_(nimages) const Image* srcImages, _In_ size_t nimages, _In_ const TexMetadata& metadata, _In_ size_t item,
_In_ float alphaReference, _Inout_ ScratchImage& mipChain) noexcept;

HRESULT __cdecl StandardSwizzle(_In_ const Image& srcImage, _In_ bool toSwizzle, _Out_ ScratchImage& image) noexcept;
HRESULT __cdecl StandardSwizzle(_In_reads_(nimages) const Image* srcImages, _In_ size_t nimages, _In_ const TexMetadata& metadata, _In_ bool toSwizzle, _Out_ ScratchImage& result) noexcept;
HRESULT __cdecl StandardSwizzle3D(_In_reads_(depth) const Image* srcImages, _In_ size_t depth, _In_ const TexMetadata& metadata, _In_ bool toSwizzle, _Out_ ScratchImage& result) noexcept;
HRESULT __cdecl StandardSwizzle(
_In_ const Image& srcImage, _In_ bool toSwizzle,
_Out_ ScratchImage& image) noexcept;
HRESULT __cdecl StandardSwizzle(
_In_reads_(nimages) const Image* srcImages, _In_ size_t nimages, _In_ const TexMetadata& metadata, _In_ bool toSwizzle,
_Out_ ScratchImage& result) noexcept;
// Performs row-major linear <-> z-order curve swizzling

enum TEX_PMALPHA_FLAGS : unsigned long
{
Expand Down
274 changes: 148 additions & 126 deletions DirectXTex/DirectXTexSwizzle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,10 @@ namespace
#else
constexpr size_t MAX_TEXTURE_SIZE = UINT32_MAX;
#endif
}


//-------------------------------------------------------------------------------------
// 2D z-order curve
//-------------------------------------------------------------------------------------
namespace
{
constexpr uint16_t STANDARD_SWIZZLE_MASK_8 = 0b1010101000001111;
constexpr uint16_t STANDARD_SWIZZLE_MASK_16 = 0b1010101010001111;
constexpr uint16_t STANDARD_SWIZZLE_MASK_32 = 0b1010101010001111;
Expand Down Expand Up @@ -199,24 +195,90 @@ namespace

return S_OK;
}


//-------------------------------------------------------------------------------------
// 3D z-order curve
//-------------------------------------------------------------------------------------
constexpr uint16_t VOLUME_STANDARD_SWIZZLE_X_8 = 0b1001000000001111;
constexpr uint16_t VOLUME_STANDARD_SWIZZLE_X_16 = 0b1001000000001111;
constexpr uint16_t VOLUME_STANDARD_SWIZZLE_X_32 = 0b1001001000001111;
constexpr uint16_t VOLUME_STANDARD_SWIZZLE_X_64 = 0b1001001100001111;
constexpr uint16_t VOLUME_STANDARD_SWIZZLE_X_128 = 0b1001001100001111;

constexpr uint16_t VOLUME_STANDARD_SWIZZLE_Y_8 = 0b0100101000110000;
constexpr uint16_t VOLUME_STANDARD_SWIZZLE_Y_16 = 0b0100101000110001;
constexpr uint16_t VOLUME_STANDARD_SWIZZLE_Y_32 = 0b0100100100110011;
constexpr uint16_t VOLUME_STANDARD_SWIZZLE_Y_64 = 0b0100100000110111;
constexpr uint16_t VOLUME_STANDARD_SWIZZLE_Y_128 = 0b0100100000111111;

constexpr uint16_t VOLUME_STANDARD_SWIZZLE_Z_8 = 0b0010010111000000;
constexpr uint16_t VOLUME_STANDARD_SWIZZLE_Z_16 = 0b0010010111000001;
constexpr uint16_t VOLUME_STANDARD_SWIZZLE_Z_32 = 0b0010010011000011;
constexpr uint16_t VOLUME_STANDARD_SWIZZLE_Z_64 = 0b0010010011000111;
constexpr uint16_t VOLUME_STANDARD_SWIZZLE_Z_128 = 0b0010010011001111;

inline int GetSwizzleMask3D_X(size_t bytesPerPixel) noexcept
{
switch(bytesPerPixel)
{
case 1: return VOLUME_STANDARD_SWIZZLE_X_8;
case 2: return VOLUME_STANDARD_SWIZZLE_X_16;
case 8: return VOLUME_STANDARD_SWIZZLE_X_64;
case 16: return VOLUME_STANDARD_SWIZZLE_X_128;
default: return VOLUME_STANDARD_SWIZZLE_X_32;
}
}

inline int GetSwizzleMask3D_Y(size_t bytesPerPixel) noexcept
{
switch(bytesPerPixel)
{
case 1: return VOLUME_STANDARD_SWIZZLE_Y_8;
case 2: return VOLUME_STANDARD_SWIZZLE_Y_16;
case 8: return VOLUME_STANDARD_SWIZZLE_Y_64;
case 16: return VOLUME_STANDARD_SWIZZLE_Y_128;
default: return VOLUME_STANDARD_SWIZZLE_Y_32;
}
}

inline int GetSwizzleMask3D_Z(size_t bytesPerPixel) noexcept
{
switch(bytesPerPixel)
{
case 1: return VOLUME_STANDARD_SWIZZLE_Z_8;
case 2: return VOLUME_STANDARD_SWIZZLE_Z_16;
case 8: return VOLUME_STANDARD_SWIZZLE_Z_64;
case 16: return VOLUME_STANDARD_SWIZZLE_Z_128;
default: return VOLUME_STANDARD_SWIZZLE_Z_32;
}
}
}


//=====================================================================================
// Entry points
//=====================================================================================

_Use_decl_annotations_
HRESULT DirectX::StandardSwizzle(
const Image& srcImage,
bool toSwizzle,
ScratchImage& result) noexcept
{
if (srcImage.height == 1
if ((srcImage.height == 1)
|| (srcImage.width > MAX_TEXTURE_DIMENSION) || (srcImage.height > MAX_TEXTURE_DIMENSION))
{
// Standard Swizzle is not defined for 1D textures or textures larger than 16k
return E_INVALIDARG;
return HRESULT_E_NOT_SUPPORTED;
}

if (IsPlanar(srcImage.format) || IsPalettized(srcImage.format) || (srcImage.format == DXGI_FORMAT_R1_UNORM))
return HRESULT_E_NOT_SUPPORTED;

if (!srcImage.pixels)
return E_POINTER;

HRESULT hr = result.Initialize2D(srcImage.format, srcImage.width, srcImage.height, 1, 1);
if (FAILED(hr))
return hr;
Expand Down Expand Up @@ -296,11 +358,16 @@ HRESULT DirectX::StandardSwizzle(
bool toSwizzle,
ScratchImage& result) noexcept
{
if (!srcImages || !nimages
|| (metadata.dimension != TEX_DIMENSION_TEXTURE2D)
|| (metadata.width > MAX_TEXTURE_DIMENSION) || (metadata.height > MAX_TEXTURE_DIMENSION))
if (!srcImages || !nimages)
return E_INVALIDARG;

if (((metadata.dimension != TEX_DIMENSION_TEXTURE2D) && (metadata.dimension != TEX_DIMENSION_TEXTURE3D))
|| (metadata.width > MAX_TEXTURE_DIMENSION) || (metadata.height > MAX_TEXTURE_DIMENSION))
{
// Standard Swizzle is not defined for 1D textures or textures larger than 16k
return HRESULT_E_NOT_SUPPORTED;
}

if (IsPlanar(metadata.format) || IsPalettized(metadata.format) || (metadata.format == DXGI_FORMAT_R1_UNORM))
return HRESULT_E_NOT_SUPPORTED;

Expand Down Expand Up @@ -329,141 +396,95 @@ HRESULT DirectX::StandardSwizzle(
return E_POINTER;
}

for (size_t index = 0; index < nimages; ++index)
if (metadata.dimension == TEX_DIMENSION_TEXTURE3D)
{
// TODO -
return E_NOTIMPL;
}
else
{
const Image& src = srcImages[index];
if (src.format != metadata.format)
// Handle the 2D case for TEX_DIMENSION_TEXTURE2D
for (size_t index = 0; index < nimages; ++index)
{
result.Release();
return E_FAIL;
}
const Image& src = srcImages[index];
if (src.format != metadata.format)
{
result.Release();
return E_FAIL;
}

if ((src.width > MAX_TEXTURE_DIMENSION) || (src.height > MAX_TEXTURE_DIMENSION))
return E_FAIL;
if ((src.width > MAX_TEXTURE_DIMENSION) || (src.height > MAX_TEXTURE_DIMENSION))
{
result.Release();
return E_FAIL;
}

const Image& dst = dest[index];
assert(dst.format == metadata.format);
const Image& dst = dest[index];
assert(dst.format == metadata.format);

if (src.width != dst.width || src.height != dst.height)
{
result.Release();
return E_FAIL;
}
if (src.width != dst.width || src.height != dst.height)
{
result.Release();
return E_FAIL;
}

if (toSwizzle)
{
switch(bytesPerPixel)
if (toSwizzle)
{
case 1:
hr = LinearToStandardSwizzle2D<STANDARD_SWIZZLE_MASK_8, 1>(src, dst, false);
break;
case 2:
hr = LinearToStandardSwizzle2D<STANDARD_SWIZZLE_MASK_16, 2>(src, dst, false);
break;
case 8:
hr = LinearToStandardSwizzle2D<STANDARD_SWIZZLE_MASK_64, 8>(src, dst, isCompressed);
break;
case 16:
hr = LinearToStandardSwizzle2D<STANDARD_SWIZZLE_MASK_128, 16>(src, dst, isCompressed);
break;
default:
hr = LinearToStandardSwizzle2D<STANDARD_SWIZZLE_MASK_32, 4>(src, dst, false);
break;
switch(bytesPerPixel)
{
case 1:
hr = LinearToStandardSwizzle2D<STANDARD_SWIZZLE_MASK_8, 1>(src, dst, false);
break;
case 2:
hr = LinearToStandardSwizzle2D<STANDARD_SWIZZLE_MASK_16, 2>(src, dst, false);
break;
case 8:
hr = LinearToStandardSwizzle2D<STANDARD_SWIZZLE_MASK_64, 8>(src, dst, isCompressed);
break;
case 16:
hr = LinearToStandardSwizzle2D<STANDARD_SWIZZLE_MASK_128, 16>(src, dst, isCompressed);
break;
default:
hr = LinearToStandardSwizzle2D<STANDARD_SWIZZLE_MASK_32, 4>(src, dst, false);
break;
}
}
}
else
{
switch(bytesPerPixel)
else
{
case 1:
hr = StandardSwizzleToLinear2D<STANDARD_SWIZZLE_MASK_8, 1>(src, dst, false);
break;
case 2:
hr = StandardSwizzleToLinear2D<STANDARD_SWIZZLE_MASK_16, 2>(src, dst, false);
break;
case 8:
hr = StandardSwizzleToLinear2D<STANDARD_SWIZZLE_MASK_64, 8>(src, dst, isCompressed);
break;
case 16:
hr = StandardSwizzleToLinear2D<STANDARD_SWIZZLE_MASK_128, 16>(src, dst, isCompressed);
break;
default:
hr = StandardSwizzleToLinear2D<STANDARD_SWIZZLE_MASK_32, 4>(src, dst, false);
break;
switch(bytesPerPixel)
{
case 1:
hr = StandardSwizzleToLinear2D<STANDARD_SWIZZLE_MASK_8, 1>(src, dst, false);
break;
case 2:
hr = StandardSwizzleToLinear2D<STANDARD_SWIZZLE_MASK_16, 2>(src, dst, false);
break;
case 8:
hr = StandardSwizzleToLinear2D<STANDARD_SWIZZLE_MASK_64, 8>(src, dst, isCompressed);
break;
case 16:
hr = StandardSwizzleToLinear2D<STANDARD_SWIZZLE_MASK_128, 16>(src, dst, isCompressed);
break;
default:
hr = StandardSwizzleToLinear2D<STANDARD_SWIZZLE_MASK_32, 4>(src, dst, false);
break;
}
}
}

if (FAILED(hr))
{
result.Release();
return hr;
if (FAILED(hr))
{
result.Release();
return hr;
}
}
}

return S_OK;
}


//-------------------------------------------------------------------------------------
// 3D z-order curve
//-------------------------------------------------------------------------------------
namespace
{
constexpr uint16_t VOLUME_STANDARD_SWIZZLE_X_8 = 0b1001000000001111;
constexpr uint16_t VOLUME_STANDARD_SWIZZLE_X_16 = 0b1001000000001111;
constexpr uint16_t VOLUME_STANDARD_SWIZZLE_X_32 = 0b1001001000001111;
constexpr uint16_t VOLUME_STANDARD_SWIZZLE_X_64 = 0b1001001100001111;
constexpr uint16_t VOLUME_STANDARD_SWIZZLE_X_128 = 0b1001001100001111;

constexpr uint16_t VOLUME_STANDARD_SWIZZLE_Y_8 = 0b0100101000110000;
constexpr uint16_t VOLUME_STANDARD_SWIZZLE_Y_16 = 0b0100101000110001;
constexpr uint16_t VOLUME_STANDARD_SWIZZLE_Y_32 = 0b0100100100110011;
constexpr uint16_t VOLUME_STANDARD_SWIZZLE_Y_64 = 0b0100100000110111;
constexpr uint16_t VOLUME_STANDARD_SWIZZLE_Y_128 = 0b0100100000111111;

constexpr uint16_t VOLUME_STANDARD_SWIZZLE_Z_8 = 0b0010010111000000;
constexpr uint16_t VOLUME_STANDARD_SWIZZLE_Z_16 = 0b0010010111000001;
constexpr uint16_t VOLUME_STANDARD_SWIZZLE_Z_32 = 0b0010010011000011;
constexpr uint16_t VOLUME_STANDARD_SWIZZLE_Z_64 = 0b0010010011000111;
constexpr uint16_t VOLUME_STANDARD_SWIZZLE_Z_128 = 0b0010010011001111;

inline int GetSwizzleMask3D_X(size_t bytesPerPixel) noexcept
{
switch(bytesPerPixel)
{
case 1: return VOLUME_STANDARD_SWIZZLE_X_8;
case 2: return VOLUME_STANDARD_SWIZZLE_X_16;
case 8: return VOLUME_STANDARD_SWIZZLE_X_64;
case 16: return VOLUME_STANDARD_SWIZZLE_X_128;
default: return VOLUME_STANDARD_SWIZZLE_X_32;
}
}

inline int GetSwizzleMask3D_Y(size_t bytesPerPixel) noexcept
{
switch(bytesPerPixel)
{
case 1: return VOLUME_STANDARD_SWIZZLE_Y_8;
case 2: return VOLUME_STANDARD_SWIZZLE_Y_16;
case 8: return VOLUME_STANDARD_SWIZZLE_Y_64;
case 16: return VOLUME_STANDARD_SWIZZLE_Y_128;
default: return VOLUME_STANDARD_SWIZZLE_Y_32;
}
}

inline int GetSwizzleMask3D_Z(size_t bytesPerPixel) noexcept
{
switch(bytesPerPixel)
{
case 1: return VOLUME_STANDARD_SWIZZLE_Z_8;
case 2: return VOLUME_STANDARD_SWIZZLE_Z_16;
case 8: return VOLUME_STANDARD_SWIZZLE_Z_64;
case 16: return VOLUME_STANDARD_SWIZZLE_Z_128;
default: return VOLUME_STANDARD_SWIZZLE_Z_32;
}
}
}

#if 0
// TODO: merging into main based on metadata
_Use_decl_annotations_
HRESULT DirectX::StandardSwizzle3D(
const Image* srcImages,
Expand Down Expand Up @@ -566,3 +587,4 @@ HRESULT DirectX::StandardSwizzle3D(

return S_OK;
}
#endif

0 comments on commit 09ff9e8

Please sign in to comment.