Skip to content

Commit 6a37f81

Browse files
walbourn_cpwalbourn_cp
walbourn_cp
authored and
walbourn_cp
committed
DirectXTex: TEX_COMPRESS_* flags; added 'compress' flags paramter to GPU Compress [breaking change]
1 parent 8d3dbe1 commit 6a37f81

File tree

4 files changed

+51
-25
lines changed

4 files changed

+51
-25
lines changed

DirectXTex/DirectXTex.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,12 @@ namespace DirectX
482482
TEX_COMPRESS_UNIFORM = 0x40000,
483483
// Uniform color weighting for BC1-3 compression; by default uses perceptual weighting
484484

485+
TEX_COMPRESS_SRGB_IN = 0x1000000,
486+
TEX_COMPRESS_SRGB_OUT = 0x2000000,
487+
TEX_COMPRESS_SRGB = ( TEX_COMPRESS_SRGB_IN | TEX_COMPRESS_SRGB_OUT ),
488+
// if the input format type is IsSRGB(), then SRGB_IN is on by default
489+
// if the output format type is IsSRGB(), then SRGB_OUT is on by default
490+
485491
TEX_COMPRESS_PARALLEL = 0x10000000,
486492
// Compress is free to use multithreading to improve performance (by default it does not use multithreading)
487493
};
@@ -492,9 +498,10 @@ namespace DirectX
492498
_In_ DXGI_FORMAT format, _In_ DWORD compress, _In_ float alphaRef, _Out_ ScratchImage& cImages );
493499
// Note that alphaRef is only used by BC1. 0.5f is a typical value to use
494500

495-
HRESULT Compress( _In_ ID3D11Device* pDevice, _In_ const Image& srcImage, _In_ DXGI_FORMAT format, _Out_ ScratchImage& image );
501+
HRESULT Compress( _In_ ID3D11Device* pDevice, _In_ const Image& srcImage, _In_ DXGI_FORMAT format, _In_ DWORD compress,
502+
_Out_ ScratchImage& image );
496503
HRESULT Compress( _In_ ID3D11Device* pDevice, _In_ const Image* srcImages, _In_ size_t nimages, _In_ const TexMetadata& metadata,
497-
_In_ DXGI_FORMAT format, _Out_ ScratchImage& cImages );
504+
_In_ DXGI_FORMAT format, _In_ DWORD compress, _Out_ ScratchImage& cImages );
498505
// DirectCompute-based compression
499506

500507
HRESULT Decompress( _In_ const Image& cImage, _In_ DXGI_FORMAT format, _Out_ ScratchImage& image );

DirectXTex/DirectXTexCompress.cpp

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,14 @@ inline static DWORD _GetBCFlags( _In_ DWORD compress )
3535
return ( compress & (BC_FLAGS_DITHER_RGB|BC_FLAGS_DITHER_A|BC_FLAGS_UNIFORM) );
3636
}
3737

38+
inline static DWORD _GetSRGBFlags( _In_ DWORD compress )
39+
{
40+
static_assert( TEX_COMPRESS_SRGB_IN == TEX_FILTER_SRGB_IN, "TEX_COMPRESS_SRGB* should match TEX_FILTER_SRGB*" );
41+
static_assert( TEX_COMPRESS_SRGB_OUT == TEX_FILTER_SRGB_OUT, "TEX_COMPRESS_SRGB* should match TEX_FILTER_SRGB*" );
42+
static_assert( TEX_COMPRESS_SRGB == TEX_FILTER_SRGB, "TEX_COMPRESS_SRGB* should match TEX_FILTER_SRGB*" );
43+
return ( compress & TEX_COMPRESS_SRGB );
44+
}
45+
3846
inline static bool _DetermineEncoderSettings( _In_ DXGI_FORMAT format, _Out_ BC_ENCODE& pfEncode, _Out_ size_t& blocksize, _Out_ DWORD& cflags )
3947
{
4048
switch(format)
@@ -62,7 +70,7 @@ inline static bool _DetermineEncoderSettings( _In_ DXGI_FORMAT format, _Out_ BC_
6270

6371
//-------------------------------------------------------------------------------------
6472
static HRESULT _CompressBC( _In_ const Image& image, _In_ const Image& result, _In_ DWORD bcflags,
65-
_In_ float alphaRef )
73+
_In_ DWORD srgb, _In_ float alphaRef )
6674
{
6775
if ( !image.pixels || !result.pixels )
6876
return E_POINTER;
@@ -156,7 +164,7 @@ static HRESULT _CompressBC( _In_ const Image& image, _In_ const Image& result, _
156164
}
157165
}
158166

159-
_ConvertScanline( temp, 16, result.format, format, cflags );
167+
_ConvertScanline( temp, 16, result.format, format, cflags | srgb );
160168

161169
if ( pfEncode )
162170
pfEncode( dptr, temp, bcflags );
@@ -178,7 +186,7 @@ static HRESULT _CompressBC( _In_ const Image& image, _In_ const Image& result, _
178186
//-------------------------------------------------------------------------------------
179187
#ifdef _OPENMP
180188
static HRESULT _CompressBC_Parallel( _In_ const Image& image, _In_ const Image& result, _In_ DWORD bcflags,
181-
_In_ float alphaRef )
189+
_In_ DWORD srgb, _In_ float alphaRef )
182190
{
183191
if ( !image.pixels || !result.pixels )
184192
return E_POINTER;
@@ -281,7 +289,7 @@ static HRESULT _CompressBC_Parallel( _In_ const Image& image, _In_ const Image&
281289
}
282290
}
283291

284-
_ConvertScanline( temp, 16, result.format, format, cflags );
292+
_ConvertScanline( temp, 16, result.format, format, cflags | srgb );
285293

286294
if ( pfEncode )
287295
pfEncode( pDest, temp, bcflags );
@@ -574,12 +582,12 @@ HRESULT Compress( const Image& srcImage, DXGI_FORMAT format, DWORD compress, flo
574582
#ifndef _OPENMP
575583
return E_NOTIMPL;
576584
#else
577-
hr = _CompressBC_Parallel( srcImage, *img, _GetBCFlags( compress ), alphaRef );
585+
hr = _CompressBC_Parallel( srcImage, *img, _GetBCFlags( compress ), _GetSRGBFlags( compress ), alphaRef );
578586
#endif // _OPENMP
579587
}
580588
else
581589
{
582-
hr = _CompressBC( srcImage, *img, _GetBCFlags( compress ), alphaRef );
590+
hr = _CompressBC( srcImage, *img, _GetBCFlags( compress ), _GetSRGBFlags( compress ), alphaRef );
583591
}
584592

585593
if ( FAILED(hr) )
@@ -638,7 +646,7 @@ HRESULT Compress( const Image* srcImages, size_t nimages, const TexMetadata& met
638646
#else
639647
if ( compress & TEX_COMPRESS_PARALLEL )
640648
{
641-
hr = _CompressBC_Parallel( src, dest[ index ], _GetBCFlags( compress ), alphaRef );
649+
hr = _CompressBC_Parallel( src, dest[ index ], _GetBCFlags( compress ), _GetSRGBFlags( compress ), alphaRef );
642650
if ( FAILED(hr) )
643651
{
644652
cImages.Release();
@@ -649,7 +657,7 @@ HRESULT Compress( const Image* srcImages, size_t nimages, const TexMetadata& met
649657
}
650658
else
651659
{
652-
hr = _CompressBC( src, dest[ index ], _GetBCFlags( compress ), alphaRef );
660+
hr = _CompressBC( src, dest[ index ], _GetBCFlags( compress ), _GetSRGBFlags( compress ), alphaRef );
653661
if ( FAILED(hr) )
654662
{
655663
cImages.Release();

DirectXTex/DirectXTexCompressGPU.cpp

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,19 @@
2020
namespace DirectX
2121
{
2222

23+
inline static DWORD _GetSRGBFlags( _In_ DWORD compress )
24+
{
25+
static_assert( TEX_COMPRESS_SRGB_IN == TEX_FILTER_SRGB_IN, "TEX_COMPRESS_SRGB* should match TEX_FILTER_SRGB*" );
26+
static_assert( TEX_COMPRESS_SRGB_OUT == TEX_FILTER_SRGB_OUT, "TEX_COMPRESS_SRGB* should match TEX_FILTER_SRGB*" );
27+
static_assert( TEX_COMPRESS_SRGB == TEX_FILTER_SRGB, "TEX_COMPRESS_SRGB* should match TEX_FILTER_SRGB*" );
28+
return ( compress & TEX_COMPRESS_SRGB );
29+
}
30+
31+
2332
//-------------------------------------------------------------------------------------
2433
// Converts to R8G8B8A8_UNORM or R8G8B8A8_UNORM_SRGB doing any conversion logic needed
2534
//-------------------------------------------------------------------------------------
26-
static HRESULT _ConvertToRGBA32( _In_ const Image& srcImage, _In_ ScratchImage& image, bool srgb )
35+
static HRESULT _ConvertToRGBA32( _In_ const Image& srcImage, _In_ ScratchImage& image, bool srgb, _In_ DWORD filter )
2736
{
2837
if ( !srcImage.pixels )
2938
return E_POINTER;
@@ -64,7 +73,7 @@ static HRESULT _ConvertToRGBA32( _In_ const Image& srcImage, _In_ ScratchImage&
6473
return E_FAIL;
6574
}
6675

67-
_ConvertScanline( scanline.get(), srcImage.width, format, srcImage.format, 0 );
76+
_ConvertScanline( scanline.get(), srcImage.width, format, srcImage.format, filter );
6877

6978
if ( !_StoreScanline( pDest, img->rowPitch, format, scanline.get(), srcImage.width ) )
7079
{
@@ -83,7 +92,7 @@ static HRESULT _ConvertToRGBA32( _In_ const Image& srcImage, _In_ ScratchImage&
8392
//-------------------------------------------------------------------------------------
8493
// Converts to DXGI_FORMAT_R32G32B32A32_FLOAT doing any conversion logic needed
8594
//-------------------------------------------------------------------------------------
86-
static HRESULT _ConvertToRGBAF32( const Image& srcImage, ScratchImage& image )
95+
static HRESULT _ConvertToRGBAF32( const Image& srcImage, ScratchImage& image, _In_ DWORD filter )
8796
{
8897
if ( !srcImage.pixels )
8998
return E_POINTER;
@@ -115,7 +124,7 @@ static HRESULT _ConvertToRGBAF32( const Image& srcImage, ScratchImage& image )
115124
return E_FAIL;
116125
}
117126

118-
_ConvertScanline( reinterpret_cast<XMVECTOR*>(pDest), srcImage.width, DXGI_FORMAT_R32G32B32A32_FLOAT, srcImage.format, 0 );
127+
_ConvertScanline( reinterpret_cast<XMVECTOR*>(pDest), srcImage.width, DXGI_FORMAT_R32G32B32A32_FLOAT, srcImage.format, filter );
119128

120129
pSrc += srcImage.rowPitch;
121130
pDest += img->rowPitch;
@@ -128,7 +137,7 @@ static HRESULT _ConvertToRGBAF32( const Image& srcImage, ScratchImage& image )
128137
//-------------------------------------------------------------------------------------
129138
// Compress using GPU, converting to the proper input format for the shader if needed
130139
//-------------------------------------------------------------------------------------
131-
inline static HRESULT _GPUCompress( _In_ GPUCompressBC* gpubc, _In_ const Image& srcImage, _In_ const Image& destImage )
140+
inline static HRESULT _GPUCompress( _In_ GPUCompressBC* gpubc, _In_ const Image& srcImage, _In_ const Image& destImage, _In_ DWORD compress )
132141
{
133142
if ( !gpubc )
134143
return E_POINTER;
@@ -148,18 +157,20 @@ inline static HRESULT _GPUCompress( _In_ GPUCompressBC* gpubc, _In_ const Image&
148157
ScratchImage image;
149158
HRESULT hr;
150159

160+
DWORD srgb = _GetSRGBFlags( compress );
161+
151162
switch( format )
152163
{
153164
case DXGI_FORMAT_R8G8B8A8_UNORM:
154-
hr = _ConvertToRGBA32( srcImage, image, false );
165+
hr = _ConvertToRGBA32( srcImage, image, false, srgb );
155166
break;
156167

157168
case DXGI_FORMAT_R8G8B8A8_UNORM_SRGB:
158-
hr = _ConvertToRGBA32( srcImage, image, true );
169+
hr = _ConvertToRGBA32( srcImage, image, true, srgb );
159170
break;
160171

161172
case DXGI_FORMAT_R32G32B32A32_FLOAT:
162-
hr = _ConvertToRGBAF32( srcImage, image );
173+
hr = _ConvertToRGBAF32( srcImage, image, srgb );
163174
break;
164175

165176
default:
@@ -187,7 +198,7 @@ inline static HRESULT _GPUCompress( _In_ GPUCompressBC* gpubc, _In_ const Image&
187198
// Compression
188199
//-------------------------------------------------------------------------------------
189200
_Use_decl_annotations_
190-
HRESULT Compress( ID3D11Device* pDevice, const Image& srcImage, DXGI_FORMAT format, ScratchImage& image )
201+
HRESULT Compress( ID3D11Device* pDevice, const Image& srcImage, DXGI_FORMAT format, DWORD compress, ScratchImage& image )
191202
{
192203
if ( !pDevice || IsCompressed(srcImage.format) || !IsCompressed(format) || IsTypeless(format) )
193204
return E_INVALIDARG;
@@ -217,7 +228,7 @@ HRESULT Compress( ID3D11Device* pDevice, const Image& srcImage, DXGI_FORMAT form
217228
return E_POINTER;
218229
}
219230

220-
hr = _GPUCompress( gpubc.get(), srcImage, *img );
231+
hr = _GPUCompress( gpubc.get(), srcImage, *img, compress );
221232
if ( FAILED(hr) )
222233
image.Release();
223234

@@ -226,7 +237,7 @@ HRESULT Compress( ID3D11Device* pDevice, const Image& srcImage, DXGI_FORMAT form
226237

227238
_Use_decl_annotations_
228239
HRESULT Compress( ID3D11Device* pDevice, const Image* srcImages, size_t nimages, const TexMetadata& metadata,
229-
DXGI_FORMAT format, ScratchImage& cImages )
240+
DXGI_FORMAT format, DWORD compress, ScratchImage& cImages )
230241
{
231242
if ( !pDevice || !srcImages || !nimages )
232243
return E_INVALIDARG;
@@ -302,7 +313,7 @@ HRESULT Compress( ID3D11Device* pDevice, const Image* srcImages, size_t nimages,
302313
return E_FAIL;
303314
}
304315

305-
hr = _GPUCompress( gpubc.get(), src, dest[ index ] );
316+
hr = _GPUCompress( gpubc.get(), src, dest[ index ], compress );
306317
if ( FAILED(hr) )
307318
{
308319
cImages.Release();
@@ -353,7 +364,7 @@ HRESULT Compress( ID3D11Device* pDevice, const Image* srcImages, size_t nimages,
353364
return E_FAIL;
354365
}
355366

356-
hr = _GPUCompress( gpubc.get(), src, dest[ index ] );
367+
hr = _GPUCompress( gpubc.get(), src, dest[ index ], compress );
357368
if ( FAILED(hr) )
358369
{
359370
cImages.Release();

Texconv/texconv.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1336,11 +1336,11 @@ int __cdecl wmain(_In_ int argc, _In_z_count_(argc) wchar_t* argv[])
13361336

13371337
if ( bc6hbc7 && pDevice )
13381338
{
1339-
hr = Compress( pDevice, img, nimg, info, tformat, *timage );
1339+
hr = Compress( pDevice, img, nimg, info, tformat, dwSRGB, *timage );
13401340
}
13411341
else
13421342
{
1343-
hr = Compress( img, nimg, info, tformat, cflags, 0.5f, *timage );
1343+
hr = Compress( img, nimg, info, tformat, cflags | dwSRGB, 0.5f, *timage );
13441344
}
13451345
if ( FAILED(hr) )
13461346
{

0 commit comments

Comments
 (0)