Search code examples
winapihlsl

How to sample from a TextureCubeArray


So I was surprisingly unable to find literally any information about sampling from a texture cube array. In the mdsn docs, it states that the uv input for TextureCubeArray.Sample() is a vector4. This makes no sense to me. In 2d the uv goes from [0, 1] in 2 dimensions (x, y) and that is straightforward. In 3d, it's the direction which goes from [-1, 1] in three dimensions (x, y, z).

So what I'm straight up guessing, is that the w coordinate of a 4d uv also goes from -1 to 1. If this is true, then the w coord of the uv given a cubemap[i] from an array of size L is uv.w = (i / L) * 2 - 1 (+ eps?). Is at all correct? (Btw, I'm thinking you have to add the epsilon to account for the possible floating point error, or is the better way of doing all this?)


Solution

  • A TextureCubeArray requires a vector3 for the sampling just like a TextureCube, but since it's an array of cubes, you need the index for the array.

    For a TextureCube, you'd Sample it as:

    sampler Sampler : register(s0);
    TextureCube<float4> cubeMap : register(t0);
    
    ...
    
    float3 envcoord = reflect(-eyeVector, worldNormal);
    float4 color = cubeMap.Sample(Sampler, envcoord);
    

    You have to use a float4 for the TextureCubeArray where the .w is the cube index.

    sampler Sampler : register(s0);
    TextureCubeArray<float4> cubeMap : register(t0);
    
    ...
    
    float3 envcoord = reflect(-eyeVector, worldNormal);
    float4 color = cubeMap.Sample(Sampler, float4(envcoord, cubeIndex));