1
0
mirror of https://github.com/microsoft/DirectXTex synced 2024-11-21 12:00:06 +00:00

DirectXTex: Add 'forceSRGB' option to Ex versions of CreateSRV, DDSTextureLoader, & WICTextureLoader

This commit is contained in:
walbourn_cp 2012-12-11 12:24:03 -08:00
parent cee58c4aed
commit db135329b2
6 changed files with 120 additions and 18 deletions

View File

@ -713,6 +713,39 @@ static DXGI_FORMAT GetDXGIFormat( const DDS_PIXELFORMAT& ddpf )
}
//--------------------------------------------------------------------------------------
static DXGI_FORMAT MakeSRGB( _In_ DXGI_FORMAT format )
{
switch( format )
{
case DXGI_FORMAT_R8G8B8A8_UNORM:
return DXGI_FORMAT_R8G8B8A8_UNORM_SRGB;
case DXGI_FORMAT_BC1_UNORM:
return DXGI_FORMAT_BC1_UNORM_SRGB;
case DXGI_FORMAT_BC2_UNORM:
return DXGI_FORMAT_BC2_UNORM_SRGB;
case DXGI_FORMAT_BC3_UNORM:
return DXGI_FORMAT_BC3_UNORM_SRGB;
case DXGI_FORMAT_B8G8R8A8_UNORM:
return DXGI_FORMAT_B8G8R8A8_UNORM_SRGB;
case DXGI_FORMAT_B8G8R8X8_UNORM:
return DXGI_FORMAT_B8G8R8X8_UNORM_SRGB;
case DXGI_FORMAT_BC7_UNORM:
return DXGI_FORMAT_BC7_UNORM_SRGB;
default:
return format;
}
}
//--------------------------------------------------------------------------------------
static HRESULT FillInitData( _In_ size_t width,
_In_ size_t height,
@ -818,6 +851,7 @@ static HRESULT CreateD3DResources( _In_ ID3D11Device* d3dDevice,
_In_ unsigned int bindFlags,
_In_ unsigned int cpuAccessFlags,
_In_ unsigned int miscFlags,
_In_ bool forceSRGB,
_In_ bool isCubeMap,
_In_count_x_(mipCount*arraySize) D3D11_SUBRESOURCE_DATA* initData,
_Out_opt_ ID3D11Resource** texture,
@ -853,7 +887,12 @@ static HRESULT CreateD3DResources( _In_ ID3D11Device* d3dDevice,
{
D3D11_SHADER_RESOURCE_VIEW_DESC SRVDesc;
memset( &SRVDesc, 0, sizeof( SRVDesc ) );
SRVDesc.Format = format;
if ( forceSRGB )
{
SRVDesc.Format = MakeSRGB( format );
}
else
SRVDesc.Format = format;
if (arraySize > 1)
{
@ -920,7 +959,12 @@ static HRESULT CreateD3DResources( _In_ ID3D11Device* d3dDevice,
{
D3D11_SHADER_RESOURCE_VIEW_DESC SRVDesc;
memset( &SRVDesc, 0, sizeof( SRVDesc ) );
SRVDesc.Format = format;
if ( forceSRGB )
{
SRVDesc.Format = MakeSRGB( format );
}
else
SRVDesc.Format = format;
if (isCubeMap)
{
@ -998,7 +1042,13 @@ static HRESULT CreateD3DResources( _In_ ID3D11Device* d3dDevice,
{
D3D11_SHADER_RESOURCE_VIEW_DESC SRVDesc;
memset( &SRVDesc, 0, sizeof( SRVDesc ) );
SRVDesc.Format = format;
if ( forceSRGB )
{
SRVDesc.Format = MakeSRGB( format );
}
else
SRVDesc.Format = format;
SRVDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE3D;
SRVDesc.Texture3D.MipLevels = desc.MipLevels;
@ -1041,6 +1091,7 @@ static HRESULT CreateTextureFromDDS( _In_ ID3D11Device* d3dDevice,
_In_ unsigned int bindFlags,
_In_ unsigned int cpuAccessFlags,
_In_ unsigned int miscFlags,
_In_ bool forceSRGB,
_Out_opt_ ID3D11Resource** texture,
_Out_opt_ ID3D11ShaderResourceView** textureView )
{
@ -1216,7 +1267,7 @@ static HRESULT CreateTextureFromDDS( _In_ ID3D11Device* d3dDevice,
if ( SUCCEEDED(hr) )
{
hr = CreateD3DResources( d3dDevice, resDim, twidth, theight, tdepth, mipCount - skipMip, arraySize,
format, usage, bindFlags, cpuAccessFlags, miscFlags,
format, usage, bindFlags, cpuAccessFlags, miscFlags, forceSRGB,
isCubeMap, initData.get(), texture, textureView );
if ( FAILED(hr) && !maxsize && (mipCount > 1) )
@ -1256,7 +1307,7 @@ static HRESULT CreateTextureFromDDS( _In_ ID3D11Device* d3dDevice,
if ( SUCCEEDED(hr) )
{
hr = CreateD3DResources( d3dDevice, resDim, twidth, theight, tdepth, mipCount - skipMip, arraySize,
format, usage, bindFlags, cpuAccessFlags, miscFlags,
format, usage, bindFlags, cpuAccessFlags, miscFlags, forceSRGB,
isCubeMap, initData.get(), texture, textureView );
}
}
@ -1274,7 +1325,7 @@ HRESULT DirectX::CreateDDSTextureFromMemory( _In_ ID3D11Device* d3dDevice,
_In_ size_t maxsize )
{
return CreateDDSTextureFromMemoryEx( d3dDevice, ddsData, ddsDataSize, maxsize,
D3D11_USAGE_DEFAULT, D3D11_BIND_SHADER_RESOURCE, 0, 0,
D3D11_USAGE_DEFAULT, D3D11_BIND_SHADER_RESOURCE, 0, 0, false,
texture, textureView );
}
@ -1286,6 +1337,7 @@ HRESULT DirectX::CreateDDSTextureFromMemoryEx( _In_ ID3D11Device* d3dDevice,
_In_ unsigned int bindFlags,
_In_ unsigned int cpuAccessFlags,
_In_ unsigned int miscFlags,
_In_ bool forceSRGB,
_Out_opt_ ID3D11Resource** texture,
_Out_opt_ ID3D11ShaderResourceView** textureView )
{
@ -1335,7 +1387,7 @@ HRESULT DirectX::CreateDDSTextureFromMemoryEx( _In_ ID3D11Device* d3dDevice,
HRESULT hr = CreateTextureFromDDS( d3dDevice, header,
ddsData + offset, ddsDataSize - offset, maxsize,
usage, bindFlags, cpuAccessFlags, miscFlags,
usage, bindFlags, cpuAccessFlags, miscFlags, forceSRGB,
texture, textureView );
if (texture != 0 && *texture != 0)
@ -1359,7 +1411,7 @@ HRESULT DirectX::CreateDDSTextureFromFile( _In_ ID3D11Device* d3dDevice,
_In_ size_t maxsize )
{
return CreateDDSTextureFromFileEx( d3dDevice, fileName, maxsize,
D3D11_USAGE_DEFAULT, D3D11_BIND_SHADER_RESOURCE, 0, 0,
D3D11_USAGE_DEFAULT, D3D11_BIND_SHADER_RESOURCE, 0, 0, false,
texture, textureView );
}
@ -1370,6 +1422,7 @@ HRESULT DirectX::CreateDDSTextureFromFileEx( _In_ ID3D11Device* d3dDevice,
_In_ unsigned int bindFlags,
_In_ unsigned int cpuAccessFlags,
_In_ unsigned int miscFlags,
_In_ bool forceSRGB,
_Out_opt_ ID3D11Resource** texture,
_Out_opt_ ID3D11ShaderResourceView** textureView )
{
@ -1396,7 +1449,7 @@ HRESULT DirectX::CreateDDSTextureFromFileEx( _In_ ID3D11Device* d3dDevice,
hr = CreateTextureFromDDS( d3dDevice, header,
bitData, bitSize, maxsize,
usage, bindFlags, cpuAccessFlags, miscFlags,
usage, bindFlags, cpuAccessFlags, miscFlags, forceSRGB,
texture, textureView );
#if defined(_DEBUG) || defined(PROFILE)

View File

@ -54,6 +54,7 @@ namespace DirectX
_In_ unsigned int bindFlags,
_In_ unsigned int cpuAccessFlags,
_In_ unsigned int miscFlags,
_In_ bool forceSRGB,
_Out_opt_ ID3D11Resource** texture,
_Out_opt_ ID3D11ShaderResourceView** textureView
);
@ -65,6 +66,7 @@ namespace DirectX
_In_ unsigned int bindFlags,
_In_ unsigned int cpuAccessFlags,
_In_ unsigned int miscFlags,
_In_ bool forceSRGB,
_Out_opt_ ID3D11Resource** texture,
_Out_opt_ ID3D11ShaderResourceView** textureView
);

View File

@ -484,7 +484,7 @@ namespace DirectX
_Outptr_ ID3D11Resource** ppResource );
HRESULT CreateShaderResourceViewEx( _In_ ID3D11Device* pDevice, _In_reads_(nimages) const Image* srcImages, _In_ size_t nimages, _In_ const TexMetadata& metadata,
_In_ D3D11_USAGE usage, _In_ unsigned int bindFlags, _In_ unsigned int cpuAccessFlags, _In_ unsigned int miscFlags,
_In_ D3D11_USAGE usage, _In_ unsigned int bindFlags, _In_ unsigned int cpuAccessFlags, _In_ unsigned int miscFlags, _In_ bool forceSRGB,
_Outptr_ ID3D11ShaderResourceView** ppSRV );
HRESULT CaptureTexture( _In_ ID3D11Device* pDevice, _In_ ID3D11DeviceContext* pContext, _In_ ID3D11Resource* pSource, _Out_ ScratchImage& result );

View File

@ -538,13 +538,13 @@ HRESULT CreateShaderResourceView( ID3D11Device* pDevice, const Image* srcImages,
ID3D11ShaderResourceView** ppSRV )
{
return CreateShaderResourceViewEx( pDevice, srcImages, nimages, metadata,
D3D11_USAGE_DEFAULT, D3D11_BIND_SHADER_RESOURCE, 0, 0,
D3D11_USAGE_DEFAULT, D3D11_BIND_SHADER_RESOURCE, 0, 0, false,
ppSRV );
}
_Use_decl_annotations_
HRESULT CreateShaderResourceViewEx( ID3D11Device* pDevice, const Image* srcImages, size_t nimages, const TexMetadata& metadata,
D3D11_USAGE usage, unsigned int bindFlags, unsigned int cpuAccessFlags, unsigned int miscFlags,
D3D11_USAGE usage, unsigned int bindFlags, unsigned int cpuAccessFlags, unsigned int miscFlags, bool forceSRGB,
ID3D11ShaderResourceView** ppSRV )
{
if ( !ppSRV )
@ -561,7 +561,10 @@ HRESULT CreateShaderResourceViewEx( ID3D11Device* pDevice, const Image* srcImage
D3D11_SHADER_RESOURCE_VIEW_DESC SRVDesc;
memset( &SRVDesc, 0, sizeof(SRVDesc) );
SRVDesc.Format = metadata.format;
if ( forceSRGB )
SRVDesc.Format = MakeSRGB( metadata.format );
else
SRVDesc.Format = metadata.format;
switch ( metadata.dimension )
{

View File

@ -315,6 +315,39 @@ static size_t _WICBitsPerPixel( REFGUID targetGuid )
return bpp;
}
//--------------------------------------------------------------------------------------
static DXGI_FORMAT MakeSRGB( _In_ DXGI_FORMAT format )
{
switch( format )
{
case DXGI_FORMAT_R8G8B8A8_UNORM:
return DXGI_FORMAT_R8G8B8A8_UNORM_SRGB;
case DXGI_FORMAT_BC1_UNORM:
return DXGI_FORMAT_BC1_UNORM_SRGB;
case DXGI_FORMAT_BC2_UNORM:
return DXGI_FORMAT_BC2_UNORM_SRGB;
case DXGI_FORMAT_BC3_UNORM:
return DXGI_FORMAT_BC3_UNORM_SRGB;
case DXGI_FORMAT_B8G8R8A8_UNORM:
return DXGI_FORMAT_B8G8R8A8_UNORM_SRGB;
case DXGI_FORMAT_B8G8R8X8_UNORM:
return DXGI_FORMAT_B8G8R8X8_UNORM_SRGB;
case DXGI_FORMAT_BC7_UNORM:
return DXGI_FORMAT_BC7_UNORM_SRGB;
default:
return format;
}
}
//---------------------------------------------------------------------------------
static HRESULT CreateTextureFromWIC( _In_ ID3D11Device* d3dDevice,
_In_opt_ ID3D11DeviceContext* d3dContext,
@ -324,6 +357,7 @@ static HRESULT CreateTextureFromWIC( _In_ ID3D11Device* d3dDevice,
_In_ unsigned int bindFlags,
_In_ unsigned int cpuAccessFlags,
_In_ unsigned int miscFlags,
_In_ bool forceSRGB,
_Out_opt_ ID3D11Resource** texture,
_Out_opt_ ID3D11ShaderResourceView** textureView )
{
@ -599,7 +633,13 @@ static HRESULT CreateTextureFromWIC( _In_ ID3D11Device* d3dDevice,
{
D3D11_SHADER_RESOURCE_VIEW_DESC SRVDesc;
memset( &SRVDesc, 0, sizeof( SRVDesc ) );
SRVDesc.Format = format;
if ( forceSRGB )
{
SRVDesc.Format = MakeSRGB( format );
}
else
SRVDesc.Format = format;
SRVDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
SRVDesc.Texture2D.MipLevels = (autogen) ? -1 : 1;
@ -642,7 +682,7 @@ HRESULT DirectX::CreateWICTextureFromMemory( _In_ ID3D11Device* d3dDevice,
_In_ size_t maxsize )
{
return CreateWICTextureFromMemoryEx( d3dDevice, d3dContext, wicData, wicDataSize, maxsize,
D3D11_USAGE_DEFAULT, D3D11_BIND_SHADER_RESOURCE, 0, 0,
D3D11_USAGE_DEFAULT, D3D11_BIND_SHADER_RESOURCE, 0, 0, false,
texture, textureView );
}
@ -655,6 +695,7 @@ HRESULT DirectX::CreateWICTextureFromMemoryEx( _In_ ID3D11Device* d3dDevice,
_In_ unsigned int bindFlags,
_In_ unsigned int cpuAccessFlags,
_In_ unsigned int miscFlags,
_In_ bool forceSRGB,
_Out_opt_ ID3D11Resource** texture,
_Out_opt_ ID3D11ShaderResourceView** textureView )
{
@ -699,7 +740,7 @@ HRESULT DirectX::CreateWICTextureFromMemoryEx( _In_ ID3D11Device* d3dDevice,
return hr;
hr = CreateTextureFromWIC( d3dDevice, d3dContext, frame.Get(), maxsize,
usage, bindFlags, cpuAccessFlags, miscFlags,
usage, bindFlags, cpuAccessFlags, miscFlags, forceSRGB,
texture, textureView );
if ( FAILED(hr))
return hr;
@ -726,7 +767,7 @@ HRESULT DirectX::CreateWICTextureFromFile( _In_ ID3D11Device* d3dDevice,
_In_ size_t maxsize )
{
return CreateWICTextureFromFileEx( d3dDevice, d3dContext, fileName, maxsize,
D3D11_USAGE_DEFAULT, D3D11_BIND_SHADER_RESOURCE, 0, 0,
D3D11_USAGE_DEFAULT, D3D11_BIND_SHADER_RESOURCE, 0, 0, false,
texture, textureView );
}
@ -738,6 +779,7 @@ HRESULT DirectX::CreateWICTextureFromFileEx( _In_ ID3D11Device* d3dDevice,
_In_ unsigned int bindFlags,
_In_ unsigned int cpuAccessFlags,
_In_ unsigned int miscFlags,
_In_ bool forceSRGB,
_Out_opt_ ID3D11Resource** texture,
_Out_opt_ ID3D11ShaderResourceView** textureView )
{
@ -762,7 +804,7 @@ HRESULT DirectX::CreateWICTextureFromFileEx( _In_ ID3D11Device* d3dDevice,
return hr;
hr = CreateTextureFromWIC( d3dDevice, d3dContext, frame.Get(), maxsize,
usage, bindFlags, cpuAccessFlags, miscFlags,
usage, bindFlags, cpuAccessFlags, miscFlags, forceSRGB,
texture, textureView );
if ( FAILED(hr))
return hr;

View File

@ -68,6 +68,7 @@ namespace DirectX
_In_ unsigned int bindFlags,
_In_ unsigned int cpuAccessFlags,
_In_ unsigned int miscFlags,
_In_ bool forceSRGB,
_Out_opt_ ID3D11Resource** texture,
_Out_opt_ ID3D11ShaderResourceView** textureView
);
@ -80,6 +81,7 @@ namespace DirectX
_In_ unsigned int bindFlags,
_In_ unsigned int cpuAccessFlags,
_In_ unsigned int miscFlags,
_In_ bool forceSRGB,
_Out_opt_ ID3D11Resource** texture,
_Out_opt_ ID3D11ShaderResourceView** textureView
);