2

In my GLSL Compute Shader, I've declared a struct called ParticleData, and when I read in part of my buffer as a mat3, the size (which I assess by looking at the output) is different than if I use a float[9].

I haven't been able to find a definitive source explaining the mat3 structure. he data I'm writing to my buffer is simply 9 floats, stored in an std::array<float,9>.

What is the difference between a mat3 and a float[9], in terms of size?

Gnemlock
  • 5,263
  • 5
  • 28
  • 58
Matt
  • 165
  • 7

1 Answers1

4

It all depends on the GPU and driver.

Due to the way many GPUs work a mat3 may be padded internally to take 12 floats of space arranged as [x0, y0, z0, pad][x1, y1, z1, pad][x2, y2, z2, pad].

A float[9] will take 9 floats of space but then may effectively be padded (or not) to 12 floats by adding 3 floats of padding at the end [0, 1, 2, 3, 4, 5, 6, 7, 8][pad, pad, pad] for the next data to align properly. Although those don't count as part of its size the next data item may start after the padding.

This is because some GPUs can only access data aligned to 16 bytes (4 floats) or are otherwise very limited in how they can access the data for performance reasons.

Some more info from the official OpenGL site: https://www.khronos.org/opengl/wiki/Interface_Block_(GLSL)#Memory_layout

Stephane Hockenhull
  • 11,962
  • 1
  • 25
  • 44
  • Okay, makes sense. Right now I'm using std430. Is there a recommended way then to write a matrix to a buffer, and read it in the ComputeShader? In my case, I don't actually care about this particular matrix in my compute shader, but have it in my buffer so that I can read it in my vertex shader. At least in my vertex shader I can better define the matrix with glVertexAttribPointer, but I'm asking this question to make sure I won't have the same kind of issues. Thanks – Matt Apr 28 '17 at 20:19
  • 2
    I just use a mat4 – Stephane Hockenhull Apr 28 '17 at 20:31
  • Implemented a mat4 and it works very intuitively. Thanks – Matt Apr 29 '17 at 16:04