Sending Uint indexes from mesh shader to fragment Shader

Hello, I’m rendering a mesh that has 4 “sub meshes”, with draw index I would do one drawcall per “submesh” with proper index offset. However I’m using mesh shaders, I’m rendering all meshlets in a single drawcall.

Each of these “submeshes” has their own textures, therefore I have a texture array and per meshlet I’m sending the proper texture(uint) (I know this is not good it adds a huge amount of memory etc…).

Problem is : I can send the texture Id from mesh shader to fragment shader. More Details at the bottom.

Please I’m open to suggestions and other aproachs.

Mesh Shader
layout(location = 0) out VertexOutput
{
	vec2 uv;
	uvec2 index;

} vertexOutput[];

...
vertexOutput[ti].index = uvec2(mesh_id,0.0) ;

Fragment Shader

layout (location = 0) in VertexInput {
    vec2 uv;
    uvec2 index;

} vertexInput;

layout( push_constant ) uniform block
{
    mat4 model;
    uvec2 id;
};

void main()
{
    vec2 uv = vec2(vertexInput.uv.x,1.0 - vertexInput.uv.y);
    color_out = texture(textures[vertexInput.index.x],uv);
}

When I send type uvec2, the input on fragment shader is always zero…
When I try send vec2 and convert the first element to uint and use it as index to access the texturearray I get this result.
Textures seems to be warping and strangly mixing inside the vector.

When I try send vec2 and convert the first element to uint and use it as index to access the texturearray I get this result.

I had a similar issue and this is due to floating point precision, if you convert lets say a float 1, the value may be 0.9999999999 or it could be 1.0000001 etc… and the decimal gets truncated when casting so what I ended up doing is before casting to an int i would add 0.5.

int TextureID = int(InTextureID + 0.5);

and this fixed these types of issues for me.

1 Like

Thanks, calling round(textureId) also seems to work.

1 Like

nice, make sure to mark as solved for others to see.