Then, to write to the UAV in the same shader, the application calls the following function to pack shader data into a uint that the application can write to the UAV: UINT D3DX_FLOAT4_to_R10G10B10A2_UNORM(hlsl_precise XMFLOAT4 unpackedInput) After the application reads a uint from the UAV into the shader, it must call the following function to unpack the texture format: XMFLOAT4 D3DX_R10G10B10A2_UNORM_to_FLOAT4(UINT packedInput) For example, suppose the DXGI_FORMAT_R32_UINT UAV of the texture really holds DXGI_FORMAT_R10G10B10A2_UNORM-formatted data. In the compute shader or pixel shader, use the appropriate inline format pack and unpack functions that are provided in the D3DX_DXGIFormatConvert.inl file. The Direct3D 11 API typically does not allow casting between different format "families." However, the Direct3D 11 API makes an exception with the DXGI_FORMAT_R32_UINT format. Formats that descend from DXGI_FORMAT_R32_TYPELESS, such as DXGI_FORMAT_R32_FLOAT, are trivially supported without requiring any of the format conversion help that is described in the following sequence.Ĭreate a texture with the appropriate TYPELESS-dependent format that is specified in the previous scenario together with the required bind flags, such as D3D11_BIND_UNORDERED_ACCESS | D3D11_BIND_SHADER_RESOURCE.įor in-place image editing, create a UAV with the DXGI_FORMAT_R32_UINT format. Therefore, DXGI_FORMAT_R10G10B10A2_UNORM supports the usage pattern that is described in the following sequence. Suppose you want to make a texture on which you can use a pixel shader or compute shader to perform in-place editing, and you want the texture data to be stored in a format that is a descendant of one of the following TYPELESS formats:įor example, the DXGI_FORMAT_R10G10B10A2_UNORM format is a descendant of the DXGI_FORMAT_R10G10B10A2_TYPELESS format. The following scenario requires that an application take the following sequence of actions to perform in-place image editing. Therefore, the hardware can perform the typical automatic format unpack and pack, can perform texture filtering, and so on where there are no hardware limitations. The benefit of casting the texture to the DXGI_FORMAT_R32_UINT texture format is that later on you can use the appropriate format (for example, DXGI_FORMAT_R16G16_FLOAT) with other views on the same texture, such as Render Target Views (RTVs) or SRVs.
By casting the texture to the DXGI_FORMAT_R32_UINT texture format when you create a UAV, you can then simultaneous perform reads and writes to the texture as long as the shader performs manual format unpacking on read and packing on write.
Most 32 bit per element formats support casting to DXGI_FORMAT_R32_UINT. However, you can still simultaneously both read from and write to such other formats by casting the texture to the DXGI_FORMAT_R32_UINT texture format when you create a UAV, as long as the original format of the resource supports casting to DXGI_FORMAT_R32_UINT. Format conversion hardware is not available to simultaneously both read from and write to such other formats. You can use only a UAV to random access write to such other formats, or you can use only a Shader Resource View (SRV) to random access read from such other formats. For example, Direct3D 11 does not support simultaneously both reading from and writing to other, more useful formats, such as DXGI_FORMAT_R8G8B8A8_UNORM. However, Direct3D 11 supports simultaneously both reading from and writing to only the DXGI_FORMAT_R32_UINT texture format. It is also included in the 3DX NuGet package.ĭirect3D 11's Unordered Access View (UAV) of a Texture1D, Texture2D, or Texture3D resource supports random access reads and writes to memory from a compute shader or pixel shader. The D3DX_DXGIFormatConvert.inl header ships in the legacy DirectX SDK. To use these inline format conversion functions, include the D3DX_DXGIFormatConvert.inl file in your application. That is, you can perform in-place image editing. You can use these functions in your application to simultaneously both read from and write to a texture. The D3DX_DXGIFormatConvert.inl file contains inline format conversion functions that you can use in the compute shader or pixel shader on Direct3D 11 hardware.