hidayat
hidayat

Reputation: 9753

Having a non bound sampler inside an uniform branch

Lets say I have pixel shader that sometimes need to read from one sampler and sometimes needs to read from two different samplers, depending on a uniform variable

layout (set = 0, binding = 0) uniform UBO {
   ....
   bool useSecondTexture;
} ubo;
...
void main() {
   vec3 value0 = texture(sampler1, pos).rgb;
   vec3 value2 = vec3(0,0,0);
   if(ubo.useSecondTexture) {
       value2 = texture(sampler2, pos).rgb;
   }
   value0 += value2;
} 

Does the second sampler; sampler2 need to be bound to a valid texture even though the texture will not be read if useSecondTexture is false.

Upvotes: 3

Views: 106

Answers (1)

Jesse Hall
Jesse Hall

Reputation: 6787

All of the vkCmdDraw and vkCmdDispatch commands have this Valid Usage statement:

Descriptors in each bound descriptor set, specified via vkCmdBindDescriptorSets, must be valid if they are statically used by the currently bound VkPipeline object, specified via vkCmdBindPipeline

Since sampler2 is statically used, you must have a valid descriptor for it or you'll get undefined behavior.

My guess is that on some implementations, it'll work as you expect. But drivers/hardware are allowed to require that all descriptors that might be used by a pipeline are valid, and requiring them to inspect the contents of memory buffers to determine if something might be used would be very expensive.

Upvotes: 2

Related Questions