Having an unbound sampler inside a homogeneous branch
Suppose I have a pixel shader that sometimes needs to be read from one sampler and sometimes needs to be read from two different samples, depending on the homogeneous 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;
}
Whether a second sampler is being used; sampler2
must be bound to a valid texture even if the texture will not be read if useSecondTexture
- false
.
All commands vkCmdDraw
and vkCmdDispatch
have this Valid Usage statement:
The descriptors in each bind 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 handle or you will end up with undefined behavior.
I am guessing that in some implementations it will work as you expect. But drivers / hardware are allowed to require all descriptors that could be used by the pipeline to be valid and requiring them to check the contents of memory buffers to determine if something could be used would be very expensive.