0
votes

Let me show you what do I mean by wrong texture.

enter image description here

Red square is drawn first, then green and then blue. All three are drawn using a single command vkCmdDrawIndirect. I have one uniform buffer which is exposed in vertex shader. It contains an array of structs (one per instance drawn). I grab texture index from struct for given instance. I pass it to fragment shader using layout(location = 2) to avoid having another uniform buffer for fragment shader:

layout(binding = 0) uniform UniformBufferObject {
    camera cam;
    transform t[10000];
} ubo;

layout(location = 2) out flat int textureIndex;
...
// in main
textureIndex = ubo.t[gl_InstanceIndex].textureInstance;

In fragment shader I have this:

layout(location = 2) in flat int textureIndex;
layout(binding = 2) uniform texture2D sampledImage[40]; // descriptor with max 40 textures
...
// in main
outColor = texture(sampler2D(sampledImage[textureIndex],texSampler), fragTexCoord);

Failed pixels seem to grab index from the previous instance. Also, they exist only in the first triangle and more frequently at the top and they change depending on where on screen I draw the squares.

1

1 Answers

1
votes

gl_InstanceIndex is not guaranteed to be dynamically uniform. If your draw call only draws one instance, then its value will be dynamically uniform. But if your draw call includes multiple instances, then it won't be.

You cannot index an array of textures with a value which is not dynamically uniform. If you need to sample based on the index, then use an array texture (sampler2DArray) and pass textureIndex as the array index for it.