1
0
mirror of https://github.com/microsoft/DirectXTex synced 2024-11-08 14:00:05 +00:00
10 CreateShaderResourceView
Chuck Walbourn edited this page 2022-07-29 18:03:53 -07:00
DirectXTex

Creates a Direct3D 11 resource and shader resource view from a set of images.

This function is intended for use with tools or editor programs. For runtime/engine use, we strongly recommend using DDSTextureLoader, and/or WICTextureLoader instead of DirectXTex.

HRESULT CreateShaderResourceView(
    ID3D11Device* pDevice,
    const Image* srcImages, size_t nimages, const TexMetadata& metadata,
    ID3D11ShaderResourceView** ppSRV );

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,
    CREATETEX_FLAGS flags,
    ID3D11ShaderResourceView** ppSRV );

Parameters

  • usage: The non-Ex version of this function assumes this is D3D11_USAGE_DEFAULT.

  • bindFlags: The non-Ex version of this function assumes this is D3D11_BIND_SHADER_RESOURCE.

  • cpuAccessFlags: The non-Ex version of this function assumes this is 0.

  • miscFlags: The non-Ex version of this function assumes this is 0.

  • loadFlags: Values here are CREATETEX_DEFAULT, CREATETEX_FORCE_SRGB, and CREATETEX_IGNORE_SRGB.

Example

wchar_t ext[_MAX_EXT] = {};
_wsplitpath_s( filename, nullptr, 0, nullptr, 0, nullptr, 0, ext, _MAX_EXT );

ScratchImage image;
HRESULT hr;
if ( _wcsicmp( ext, L".dds" ) == 0 )
{
    hr = LoadFromDDSFile( filename, DDS_FLAGS_NONE, nullptr, image );
}
else if ( _wcsicmp( ext, L".tga" ) == 0 )
{
    hr = LoadFromTGAFile( filename, nullptr, image );
}
else if ( _wcsicmp( ext, L".hdr" ) == 0 )
{
    hr = LoadFromHDRFile( filename, nullptr, image );
}
else
{
    hr = LoadFromWICFile( filename, WIC_FLAGS_NONE, nullptr, image );
}
if ( SUCCEEDED(hr) )
{
    ID3D11ShaderResourceView* pSRV = nullptr;
    hr = CreateShaderResourceView( device,
        image.GetImages(), image.GetImageCount(),
        image.GetMetadata(), &pSRV );
    if ( FAILED(hr) )
    {
        ...

Remark

The CREATETEX_SRGB flag provides an option for working around gamma issues with content that is in the sRGB or similar color space but is not encoded explicitly as an SRGB format. This will force the resource format be one of the of DXGI_FORMAT_*_SRGB formats if it exist. Note that no pixel data conversion takes place. The CREATETEX_IGNORE_SRGB flag does the opposite; it will force the resource format to not have the _*_SRGB version.

This function does not provide support for auto-gen mipmaps (the runtime/engine loaders can support this) because the assumption is if you need mipmaps with DirectTex you will call GenerateMipMaps or GenerateMipMaps3D

See DirectX Tool Kit for DirectX 12's DirectXHelpers header for a CreateShaderResourceView descriptor helper