I’m trying to implement this OpenGL text-rendering tutorial in Vulkan:
https://learnopengl.com/In-Practice/Text-Rendering
and the improvements in this video:
I have managed to get the original version working (position glyph quads in the program) and also the first improvement (transform matrix in the shader).
For the final improvement (sampler arrays and instanced drawing) I have enabled descriptor indexing to index into the glyph texture array in the fragment shader. It all looks fine except I’m running into a weird synchronization issue between the vertex and fragment shader with gl_InstanceIndex.
The fragment shader is only picking up 1 gl_InstanceIndex for every 4 in the vertex shader. This causes a string like “Hello, Vulkan!” to print like “Houn”.
Am I missing some sync setting?
The Vulkan spec says:
The relative execution order of invocations of different shader types is largely undefined. However, when invoking a shader whose inputs are generated from a previous pipeline stage, the shader invocations from the previous stage are guaranteed to have executed far enough to generate input values for all required inputs.
See: https://docs.vulkan.org/spec/latest/chapters/shaders.html#shaders-execution
Done:
- Correctly implemented descriptor indexing.
- Glyph textures look fine (no pixel artefacts).
Vertex shader:
#version 450
layout(binding = 0) uniform UniformBufferObject {
mat4 model;
mat4 view;
mat4 proj;
} ubo;
layout(binding = 1) uniform UniformBufferObject6 {
mat4 transforms[400];
int letterMap[400];
} ubo6;
layout(push_constant, std430) uniform pc {
vec4 data;
vec4 data2;
};
mat4 transform = mat4(1.0);
layout(location = 0) in vec3 inPosition;
layout(location = 1) in vec3 inColor;
layout(location = 2) in vec2 inTexCoord;
layout(location = 0) out vec3 fragColor;
layout(location = 1) out vec2 fragTexCoord;
layout(location = 2) out VS_OUT {
vec2 TexCoords;
flat int index;
} vs_out;
void main() {
// position
transform[3][0] = ubo6.transforms[gl_InstanceIndex][3][0];
transform[3][1] = ubo6.transforms[gl_InstanceIndex][3][1];
// scaling
transform[0][0] = ubo6.transforms[gl_InstanceIndex][0][0];
transform[1][1] = ubo6.transforms[gl_InstanceIndex][1][1];
gl_Position = ubo.proj * transform * ubo.view * ubo.model * vec4(inPosition, 1.0);
vs_out.index = gl_InstanceIndex;
vs_out.TexCoords = inTexCoord.xy;
fragColor = inColor;
fragTexCoord = inTexCoord;
}
Fragment shader:
#version 450
#extension GL_EXT_nonuniform_qualifier : enable
layout (set = 0, binding = 3) uniform texture2D textures[];
layout (set = 0, binding = 1) uniform UniformBufferObject6 {
mat4 transforms[400];
int letterMap[400];
} ubo6;
layout (set = 0, binding = 2) uniform sampler textureSampler;
layout(location = 1) in vec2 fragTexCoord;
layout(location = 2) in VS_OUT {
vec2 TexCoords;
flat int index;
} fs_in;
layout(push_constant, std430) uniform pc {
vec4 data;
vec4 data2;
};
layout(location = 0) out vec4 outColor;
void main() {
vec4 sampled = vec4(1.0, 1.0, 1.0, texture(sampler2D(textures[nonuniformEXT(ubo6.letterMap[nonuniformEXT(fs_in.index)])], textureSampler), fragTexCoord).r);
outColor = data2 * sampled;
}