Sampler questions

Started by
2 comments, last by MJP 9 years, 9 months ago

I got a few questions regarding samplers objects in directx 11.

I have the following kind of textures that needs to be sampled:

  • 3D model texture2d (diffuse, normal textures)
  • GBuffer texture2d (position, diffuse, normals)
  • Depth texture2d
  • Depth textureCube

This is how I create my (so far only) sampler:


D3D11_SAMPLER_DESC samplerDesc;
ZeroMemory(&samplerDesc, sizeof(D3D11_SAMPLER_DESC));
samplerDesc.Filter = D3D11_FILTER_ANISOTROPIC;
samplerDesc.AddressU = D3D11_TEXTURE_ADDRESS_WRAP;
samplerDesc.AddressV = D3D11_TEXTURE_ADDRESS_WRAP;
samplerDesc.AddressW = D3D11_TEXTURE_ADDRESS_WRAP;
samplerDesc.MaxAnisotropy = anisotropic;			// typically 16
samplerDesc.ComparisonFunc = D3D11_COMPARISON_ALWAYS;
samplerDesc.MaxLOD = D3D11_FLOAT32_MAX;
DXCALL(mDevice->CreateSamplerState(&samplerDesc, &mTextureSampler));

1. Up untill now, I have sampled the 3d model textures AND GBuffer textures using the same sampler, even though the 3d model textures are mipmapped and the gbuffers are not - and it works, not even any dxgi warnings. How can this be? Shouldnt the gbuffer textures be wrong if they are not mipmapped, yet sampled by a mipmap sampler?

2. Which leads me to the following question, if the first two can be sampled using the same sampler, can I also use it for depth 2d and cubemap textures?

Advertisement
  1. It is completely valid for a texture to only have 1 mip level, and still get sampled with mipmapping enabled. It may not be as efficient as it could if you used a different sampler state, but it's still valid.
  2. Sure, you can re-use the same sampler for those cases if you just want to sample them with filtering.

For things like your G-Buffer, you typically don't want to use a sampler at all. Usually you don't want any filtering at all, and you want to sample using a pixel coordinate instead of [0, 1] texture coordinates. This can be easily done using the Load() function, or by using the array operator []. Here's an example:


Texture2D GBufferAlbedo;
 
float4 PSMain(in float4 ScreenPos : SV_Position) : SV_Target0
{
    float3 albedo = GBufferAlbedo[uint2(ScreenPos.xy)].xyz;
 
    // ...do lighting stuff
}

Interesting. Is there any performance penalty to sample for example a textureCube (for shadowmapping) with no mipmaps with the sampler I posted? Does a sampler with more mipmap filtering incur any extra operations no-mipmap textures?

ALSO - you cast the screenPos.xy into uint2. Is this because of precision issues, or why is it?


Interesting. Is there any performance penalty to sample for example a textureCube (for shadowmapping) with no mipmaps with the sampler I posted? Does a sampler with more mipmap filtering incur any extra operations no-mipmap textures?

It depends on the hardware and driver, so I can't say for sure. It's also not something I've tested personally. However using anisotropic filtering is definitely going to add a performance cost.

Since it's a shadow map, you should consider making a new sampler that uses D3D11_FILTER_COMPARISON_MIN_MAG_MIP_LINEAR or one of the similar variants. If you do this, then you can use SampleCmp in your shader to get hardware PCF.


ALSO - you cast the screenPos.xy into uint2. Is this because of precision issues, or why is it?


The Texture2D array operator takes a uint2 as a parameter, so I use an explicit cast to prevent warnings.

This topic is closed to new replies.

Advertisement