Skip to content

RI::writeSamplers

What it does

RI::writeSamplers(device, heap, writes) writes sampler descriptions into the heap's sampler slots. Each RI::SamplerWrite names a slot and a RI::SamplerDesc. There is no sampler object to create or destroy: you write the description where shaders read it, and writes take effect immediately with no separate commit step.

A DescriptorHeap holds two heaps with separate slot numbers: writeDescriptors fills the resource heap (ResourceDescriptorHeap[i] in HLSL), and writeSamplers fills the sampler heap (SamplerDescriptorHeap[i]). Create the heap with samplerCount slots.

SamplerDesc:

Field Meaning Default
minFilter, magFilter, mipFilter eNearest or eLinear eLinear
addressU, addressV, addressW eRepeat, eMirror, eClamp (to the edge), eBorder eRepeat
mipLodBias, minLod, maxLod level-of-detail bias and clamp 0, 0, 1000 (all mips)
maxAnisotropy 1 (off) to 16; when above 1, min, mag, and mip filtering are forced to linear 1
compare comparison function for shadow-map sampling (SamplerComparisonState) none
border eTransparentBlack, eOpaqueBlack, eOpaqueWhite, used when an address mode is eBorder eTransparentBlack
reduction how filtered texels combine: eAverage, or eMin / eMax when device.limits.samplerMinMax is true eAverage

Signature

// <RI/RI.hpp>, namespace RI
void writeSamplers(Device &device, DescriptorHeap &heap, std::span<const SamplerWrite> writes);
void writeSamplers(Device &device, DescriptorHeap &heap, std::initializer_list<SamplerWrite> writes);

From the header: Writes sampler descriptions into sampler-heap slots (P87). Vulkan keeps one sampler per distinct description, until the device is destroyed.

// <RI/RI.hpp>, namespace RI
struct SamplerDesc {
    Filter minFilter = Filter::eLinear;
    Filter magFilter = Filter::eLinear;
    Filter mipFilter = Filter::eLinear;
    AddressMode addressU = AddressMode::eRepeat;
    AddressMode addressV = AddressMode::eRepeat;
    AddressMode addressW = AddressMode::eRepeat;
    float mipLodBias = 0.0f;
    float minLod = 0.0f;
    float maxLod = 1000.0f;           // all mips
    uint32 maxAnisotropy = 1;         // 1: off, up to 16; on, filtering is linear (mips too)
    std::optional<CompareOp> compare; // a comparison sampler (SamplerComparisonState)
    BorderColor border = BorderColor::eTransparentBlack;
    Reduction reduction = Reduction::eAverage;
};
Field Type Default Description
minFilter Filter Filter::eLinear
magFilter Filter Filter::eLinear
mipFilter Filter Filter::eLinear
addressU AddressMode AddressMode::eRepeat
addressV AddressMode AddressMode::eRepeat
addressW AddressMode AddressMode::eRepeat
mipLodBias float 0.0f
minLod float 0.0f
maxLod float 1000.0f all mips
maxAnisotropy uint32 1 1: off, up to 16; on, filtering is linear (mips too)
compare std::optional<CompareOp> a comparison sampler (SamplerComparisonState)
border BorderColor BorderColor::eTransparentBlack
reduction Reduction Reduction::eAverage

Example

RI::DescriptorHeap heap = RI::createDescriptorHeap(device, {.resourceCount = 1024, .samplerCount = 16});
RI::writeSamplers(device, heap, {
    {.slot = 0, .sampler = {}},                                            // linear, repeat
    {.slot = 1, .sampler = {.maxAnisotropy = 16}},                         // anisotropic
    {.slot = 2, .sampler = {.addressU = RI::AddressMode::eClamp,
                            .addressV = RI::AddressMode::eClamp,
                            .compare = RI::CompareOp::eLess}},              // shadow map
});
Texture2D<float4> albedo = ResourceDescriptorHeap[root.albedoSlot];
SamplerState linearRepeat = SamplerDescriptorHeap[0];
float4 color = albedo.Sample(linearRepeat, uv);

How RI does it

  • Vulkan: RI creates one VkSampler per distinct SamplerDesc on first use and caches it on the device until the device is destroyed, so writing the same description again reuses the existing VkSampler. writeSamplers updates the descriptor in the heap's sampler descriptor set (set 1, binding 0). When reduction is eMin or eMax, RI chains VkSamplerReductionModeCreateInfo (requires samplerFilterMinmax).
  • D3D12: RI calls CreateSampler to write each descriptor directly into the shader-visible sampler heap. The D3D12 filter encodes both the filtering mode and the reduction type (comparison, minimum, or maximum). Min/max reduction requires D3D12 tiled resources tier 2 (device.limits.samplerMinMax).

Rules and errors

  • Every slot must be less than the heap's samplerCount (programmer error in validation builds).
  • In validation builds, RI also reports a programmer error if maxAnisotropy is outside 1 to 16, if minLod is greater than maxLod, if compare is set together with Reduction::eMin or Reduction::eMax, or if Reduction::eMin or Reduction::eMax is used when device.limits.samplerMinMax is false.
  • Vulkan drivers cap the number of distinct VkSampler objects per device (maxSamplerAllocationCount, often 4000); creating a new distinct sampler once that limit is reached reports an unsupported error.
  • Never overwrite a slot that GPU work in flight may read; wait for its timeline point first.