Uniform Buffer Data issue

Hello, I used uniform data struct in my c++ files as below

class UniformData
{
public:
	glm::mat4 view;
	glm::vec3 camerapos;
	glm::vec3 lightpos;
	int projmode;
	int fitmode;
	int width;
	int height;
	float zmin;
	float zmax;
	float eyewide;
};

and in my vert shader file, I defined

layout (set = 0, binding = 0) uniform UniformData
{
	mat4 view;
	vec3 camerapos;
	vec3 lightpos;
	int projmode;
	int fitmode;
	int width;
	int height;
	float zmin;
	float zmax;
	float eyewide;
} uniformData;

But on my window, nothing appear (only blank window)

So I changed the line

  • layout (set = 0, binding = 0) uniform UniformData

to either

  • layout (std430, set = 0, binding = 0) uniform UniformData
    or
  • layout (std140, set = 0, binding = 0) uniform UniformData

Still nothing appear

so I changed

glm::vec3 camerapos;
glm::vec3 lightpos;

to

glm::vec4 camerapos;
glm::vec4 lightpos;

(Also changed vert file accordingly)

Then the window shows the objects all right

So why can’t I use vec3 camerapos, vec3 lightpos above?
Could you please explain to me why this happens?

This is an alignment issue. On the CPU side you have per-byte alignment, so your structure is tightly packed. But by default on the GPU using GLSL you have different alignment rules that you can read up on this in the spec..

So your data layout differs between CPU and GPU and your shader gets wrong/different values.

To fix this you need to adjust alignments, e.g. by using different types, aligning on the CPU side or using the VK_EXT_scalar_block_layout extension which adds a C-like layout rule for shaders.

1 Like

Hello, Sascha
I will use VK_EXT_scalar_block_layout extension
Thank you and see you

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.