2

I have to send a 3x3 matrix to my Vertex Shader in a Dynamic Uniform Buffer Object, but I got stuck when creating the VkDescriptorBufferInfo:

VkDescriptorBufferInfo bufferInfo = {};
bufferInfo.range = ...?

The problem is, my 'limits.minUniformBufferOffsetAlignment' is 32, but a 3x3 matrix is 36, and apart of the dynamic offsets that I know they must be a multiple of that value (so in this case a 3x3 matrix every 0, 64, 128, ...), what about that 'range' field?

I have to write 36 or 64 in there? Thanks.

ShockCoding
  • 216
  • 2
  • 12
  • 1
    "*send a 3x3 matrix to my Vertex Shader*" [Please don't do that.](https://stackoverflow.com/questions/38172696/should-i-ever-use-a-vec3-inside-of-a-uniform-buffer-or-shader-storage-buffer-o) "*a 3x3 matrix is 36*" No, it isn't. It's actually 48 bytes. – Nicol Bolas Feb 15 '19 at 14:47
  • Interesting. Are you telling me that I should go with a 4x4 matrix instead of a 3x3, even if I don't actually need it, then just do a mat3() of the matrix inside the Vertex Shader? And, in general, to not use vec3/mat3 as uniforms, but their 4-version? – ShockCoding Feb 15 '19 at 15:56
  • "Note that, for Vulkan, the SDK's GLSL compiler gets this right, so you don't need to be worried about it for that" I think here you were referring to vec3 type. Does that mean that a vec3 is safe, but not a mat3? – ShockCoding Feb 15 '19 at 16:12
  • @NicolBolas Wait it isn't safe to use non power two aligned types in uniforms still?? I thought in that answer that because glslangvalidator packs it tight it doesn't matter? – Krupip Feb 18 '19 at 15:19
  • @opa: Protection from driver bugs is only half the issue. You still have to correctly *use them*; half of my answer is devoted to explaining that C++ and GLSL do not have a direct, 1:1 correspondence for layout when 3-element vector are involved. If you avoid 3-element vectors, then your C++ types will correspond to GLSL; if you use them, then it may not (depending on the specific struct), so you have to compensate for it. Which requires remembering GLSLs rules and always following them. Or... you can just *do what works* and not think about it. – Nicol Bolas Feb 18 '19 at 15:30
  • @opa Okay, I have more questions, but I'll ask on your answer over in that other question instead since it doesn't directly deal with this question. – Krupip Feb 18 '19 at 15:35

1 Answers1

4

Vulkan does not require that the descriptor's range be aligned. But you may as well align it; it's not like you can do anything else with those bytes.

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
  • Sorry, but I don't understand. Does this mean I should go with the multiple of 'limits.minUniformBufferOffsetAlignment', or with the actual size of the object? – ShockCoding Feb 15 '19 at 15:57
  • 1
    @ShockCoding: It means that Vulkan doesn't require it, so you can do as you see fit. – Nicol Bolas Feb 15 '19 at 16:01