Let me show you what do I mean by wrong texture.
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.