Repository navigation
Why use multiple samplers? #672
|
in The Normal Maps section, the example code describes setting up both a normal texture & a sampler meant exclusively for the normal map (alongside the existing diffuse texture & sampler): @group(0) @binding(0)
var t_diffuse: texture_2d<f32>;
@group(0)@binding(1)
var s_diffuse: sampler;
@group(0)@binding(2)
var t_normal: texture_2d<f32>;
@group(0) @binding(3)
var s_normal: sampler;This stands out to me a bit - i've done some experimenting with a Principled BSDF shader that reads 8 textures, and i was able to get away with using a shared sampler for all of them: // textures
@group(2) @binding(0)
var linear_sampler: sampler;
@group(2) @binding(1)
var base_color_tex: texture_2d<f32>;
@group(2) @binding(2)
var normal_map_tex: texture_2d<f32>;
@group(2) @binding(3)
var roughness_metallic_tex: texture_2d<f32>;
@group(2) @binding(4)
var specular_tex: texture_2d<f32>;
@group(2) @binding(5)
var sheen_tex: texture_2d<f32>;
@group(2) @binding(6)
var clearcoat_tex: texture_2d<f32>;
@group(2) @binding(7)
var anisotropic_tex: texture_2d<f32>;
@group(2) @binding(8)
var subsurface_tex: texture_2d<f32>;I wanted to push the texture usage to an extreme here, so there's not really much concern for the practicality of 8 textures. but even with a smaller amount, a shared sampler seems like the more natural design choice to me. sharing a sampler makes the code a bit simpler, and using less bindings seems like it'd be a nice optimization. But there's probably some restrictions that come with it that I haven't considered. I'm not sure how the sampler would handle textures with different dimensions, for instance, and im curious if there are design benefits or considerations that I'm not taking into account, or if i've got some wrong assumptions about optimization. |
Replies: 1 comment
|
A sampler does not know or care about the texture's dimensions. It only describes how sampling should behave: filtering, address modes, mipmap filtering, comparison mode, anisotropy, and similar state. So your shared-sampler approach is valid when all those textures should be sampled the same way. For a typical material, it is common to use one linear/repeat sampler for several maps if their sampling rules match. The tutorial likely keeps one sampler next to each texture for clarity and flexibility. Splitting samplers becomes useful when the behavior differs, for example:
Normal/roughness/metallic maps often need non-sRGB texture formats, but that is a texture format choice, not a sampler choice. If all eight textures use the same filtering and address modes, sharing one sampler is fine and saves bindings. Please mark this answered if it clears up the sampler vs texture distinction. |
A sampler does not know or care about the texture's dimensions. It only describes how sampling should behave: filtering, address modes, mipmap filtering, comparison mode, anisotropy, and similar state.
So your shared-sampler approach is valid when all those textures should be sampled the same way. For a typical material, it is common to use one linear/repeat sampler for several maps if their sampling rules match.
The tutorial likely keeps one sampler next to each texture for clarity and flexibility. Splitting samplers becomes useful when the behavior differs, for example:
nearestinstead oflinear;