Skip to content

CreateShaderResourceView

Chuck Walbourn edited this page Jul 30, 2022 · 10 revisions
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

For Use

  • Universal Windows Platform apps
  • Windows desktop apps
  • Windows 11
  • Windows 10
  • Windows 8.1
  • Windows 7 Service Pack 1
  • Xbox One
  • Xbox Series X|S
  • Windows Subsystem for Linux

For Development

  • Visual Studio 2022
  • Visual Studio 2019 (16.11)
  • clang/LLVM v12 - v18
  • GCC 10.5, 11.4, 12.3
  • MinGW 12.2, 13.2
  • CMake 3.20

Related Projects

DirectXTex Rust bindings

DirectX Tool Kit for DirectX 11

DirectX Tool Kit for DirectX 12

DirectXMesh

DirectXMath

Tools

Test Suite

Content Exporter

DxCapsViewer

See also

DirectX Landing Page

Clone this wiki locally