Skip to content

CreateShaderResourceView

Chuck Walbourn edited this page Jan 2, 2020 · 10 revisions

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,
    bool forceSRGB,
    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.

  • forceSRGB: This is 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.

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

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
  • Xbox One
  • Xbox Series X|S
  • Windows Subsystem for Linux

Architecture

  • x86
  • x64
  • ARM64

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