Resources

SM 6.2+ adds templated loads on byte address buffers: gMeshData.Load<float3x4>(offset).

14.1 Resource types

Texture1D/2D/3D, TextureCube, Texture2DArray, TextureCubeArray, Texture2DMS<T, N>

Texture2D<float4> gAlbedo: register(t0); SamplerState, SamplerComparisonState SamplerState gLinear: register(s0); typed read with format conversion Buffer<float4> gColors; StructuredBuffer<Light> gLights; raw 32-bit reads by byte offset RWTexture1D/2D/3D, RWTexture2DArray

RWTexture2D<float4> gOut: register(u0); typed read/write read/write structs, optional hidden counter raw read/write, atomics InterlockedAdd(offset, v, orig)

AppendStructuredBuffer<T> / ConsumeStructuredBuffer<T> stack-like push/pop Append(v), Consume()

RasterizerOrderedTexture2D etc. (ROVs) ordered per-pixel access ConstantBuffer<T> / cbuffer sampler feedback (SM 6.5)

14.2 Declaring and using resources

SM 6.2+ adds templated loads on byte address buffers: gMeshData.Load<float3x4>(offset).

Texture2D<float4>           gAlbedo    : register(t0);
Texture2D<float>            gHeight    : register(t1);
TextureCube                 gSky       : register(t2);   // default element type float4
StructuredBuffer<Light>     gLights    : register(t3);
ByteAddressBuffer           gMeshData  : register(t4);
SamplerState                gLinearWrap : register(s0);
SamplerComparisonState      gShadowCmp : register(s1);
RWTexture2D<float4>         gOutput    : register(u0);
RWStructuredBuffer<uint>    gCounters  : register(u1);

float4 Example(float2 uv, uint2 pixel, uint lightIndex)
{
    float4 albedo = gAlbedo.Sample(gLinearWrap, uv);
    float  h      = gHeight.Load(int3(pixel, 0));          // mip 0
    Light  L      = gLights[lightIndex];
    uint   raw    = gMeshData.Load(lightIndex * 4);        // byte offset must be 4-aligned
    uint4  raw4   = gMeshData.Load4(64);
    float3 n      = asfloat(gMeshData.Load3(128));
    gOutput[pixel] = albedo;                               // UAV write (CS or PS)
    uint count, stride;
    gLights.GetDimensions(count, stride);
    return albedo * h + float4(L.color, 0) + float4(n, 0) * (raw + raw4.x) / count;
}

14.3 Samplers

Samplers are created on the host (D3D12_SAMPLER_DESC / static samplers): filter (point, linear, anisotropic, comparison), address modes (wrap, clamp, mirror, border), LOD bias/clamp, comparison function. In Effects/legacy code they could be declared in HLSL:

In D3D12, prefer static samplers in the root signature.

SamplerState LinearWrap   // inline state: only honored by the Effects framework (FX) / some engines
{
    Filter   = MIN_MAG_MIP_LINEAR;
    AddressU = Wrap;
    AddressV = Wrap;
};

14.4 Views and formats

A resource is accessed through a view: SRV (shader resource view, read), UAV (unordered access view, read/write), CBV, RTV/DSV (render/depth targets). The view's format determines conversion: an R8G8B8A8_UNORM texture returns float4 in [0,1]; _SRGB formats convert sRGB→linear when sampling; R32_UINT should be declared Texture2D<uint>. Typed UAV loads of formats beyond R32_FLOAT/UINT/SINT need hardware support (TypedUAVLoadAdditionalFormats, standard on modern GPUs).