[SOLVED] Byte4 vertex element not working

I made a custom vertex declaration by copy-pasting the VertexPositionNormalTexture code from the monogame source and changing the position type from Vector3 to Byte4.

When I debug the vertex shader, the texture coordinates and normals have the expected output.However, the position output outputs weird numbers. For example, the position (94, 1, 128) becomes the position (1,32E-43, 1.4E-45, 1.79E-43).

Here are the results I get when I use a Byte4 to hold the position data. Notice that the BLOCKPOS should be equal to the position input but it isn’t for some reason.

It looks like the shader is not converting the Byte4 to a float4 correctly.

I think the type in your shader needs to be uint.

In HLSL:
float: 32-bit
uint: 32-bit
(C#) byte: 8-bit

Huge loss of space in the shader. And maybe a problem with the mantissa ?
How can you only need 255 values for positions ?

Ok changed the position type from float4 to uint4 and everytings works now. Thanks alot!

1 Like

Good to see it solved the problem. I’ve amended the thread title to show it has been solved.

HLSL doesn’t have byte types. You can pack the bytes if you want, but it’ll be more expensive. It doesn’t matter for sending the data, because the conversion to bytes happens on the GPU.

I’ve used bytes for position before in a voxel engine that had chunks of 256x256x256 (or smaller) voxels. By setting uniforms with the starting position of the chunk and the voxel size you can make do with bytes.