getBufferFormat.lua 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. return {
  2. summary = 'Get the format of a buffer in the Shader.',
  3. description = [[
  4. Returns the format of a buffer declared in shader code. The return type matches the same syntax
  5. used by `lovr.graphics.newBuffer` and `Buffer:getFormat`, so it can be used to quickly create a
  6. Buffer that matches a variable from a Shader.
  7. ]],
  8. arguments = {
  9. name = {
  10. type = 'string',
  11. description = 'The name of the buffer variable to return the format of.'
  12. }
  13. },
  14. returns = {
  15. format = {
  16. type = 'table',
  17. description = [[
  18. A list of fields that match the type declaration of the buffer in the shader code. Each
  19. field has `name`, `type`, and `offset` keys. If the field is an array, it will have
  20. `length` and `stride` keys as well. The top-level table also has a `stride` key. Offsets
  21. and strides are in bytes.
  22. ]]
  23. },
  24. length = {
  25. type = 'number',
  26. description = 'The number of items in the buffer (or 1 if the buffer is not an array).'
  27. }
  28. },
  29. variants = {
  30. {
  31. arguments = { 'name' },
  32. returns = { 'format', 'length' }
  33. }
  34. },
  35. notes = [=[
  36. If the buffer only has a single array field, the format will be "unwrapped" to an array instead
  37. of a single-field struct with an array in it. Example:
  38. shader = lovr.graphics.newShader([[
  39. layout(set = 0, binding = 0) buffer Numbers {
  40. uint numbers[64];
  41. };
  42. void lovrmain(){}
  43. ]])
  44. shader:getBufferFormat('Numbers')
  45. -- returns {{ name = 'numbers', type = 'u32' }}, 64
  46. -- not {{ name = 'numbers', type = 'u32', length = 64 }}, 1
  47. Similarly, if the buffer only has a single struct field, the format will be "unwrapped" to the
  48. inner struct. This lets you use a struct for a Buffer's data without having to wrap everything
  49. in an extra namespace. Example:
  50. shader = lovr.graphics.newShader([[
  51. struct HandParams {
  52. vec3 pos;
  53. float grip;
  54. };
  55. layout(set = 0, binding = 0) buffer Hand {
  56. HandParams params;
  57. };
  58. void lovrmain(){}
  59. ]])
  60. shader:getBufferFormat('Hand')
  61. -- returns {{ name = 'pos', type = 'vec3' }, { name = 'grip', type = 'float' }}, 1
  62. -- not {{ name = 'params', type = {...}}}, 1
  63. ]=],
  64. example = [=[
  65. shader = lovr.graphics.newShader([[
  66. layout(set = 2, binding = 0) uniform Transforms {
  67. mat4 transforms[32];
  68. };
  69. vec4 lovrmain() {
  70. return Projection * View * transforms[InstanceIndex] * VertexPosition;
  71. }
  72. ]], 'unlit')
  73. local format, length = shader:getBufferFormat('Transforms')
  74. print(length) --> 32
  75. print(format[1].name) --> 'transforms'
  76. print(format[1].type) --> 'mat4'
  77. -- Can pass the 2 results directly to newBuffer:
  78. transforms = lovr.graphics.newBuffer(shader:getBufferFormat('Transforms'))
  79. -- Or override the length with some initial data:
  80. transforms = lovr.graphics.newBuffer(shader:getBufferFormat('Transforms'), objects)
  81. ]=],
  82. related = {
  83. 'lovr.graphics.newBuffer',
  84. 'Buffer:getFormat'
  85. }
  86. }