newBuffer.lua 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. return {
  2. tag = 'graphics-objects',
  3. summary = 'Create a new Buffer.',
  4. description = 'Creates a Buffer.',
  5. arguments = {
  6. size = {
  7. type = 'number',
  8. description = 'The size of the Buffer, in bytes.'
  9. },
  10. length = {
  11. type = 'number',
  12. default = '1',
  13. description = 'The length of the Buffer.'
  14. },
  15. data = {
  16. type = 'table',
  17. description = [[
  18. The initial data to put into the Buffer. The length of the Buffer will be determined by the
  19. contents of the table. The contents can be a mix of tables, numbers, and vectors, but the
  20. length calculation requires each field to consistently use one type of data.
  21. ]]
  22. },
  23. blob = {
  24. type = 'Blob',
  25. description = [[
  26. A Blob with the initial contents of the Buffer. The size of the Blob will be used to
  27. determine the length of the Buffer.
  28. ]]
  29. },
  30. type = {
  31. type = 'DataType',
  32. description = 'The type of each item in the Buffer.'
  33. },
  34. format = {
  35. type = 'table',
  36. description = 'A list of fields in the Buffer.',
  37. table = {
  38. {
  39. name = 'layout',
  40. type = 'DataLayout',
  41. default = 'packed',
  42. description = 'How to lay out the Buffer fields in memory.'
  43. },
  44. {
  45. name = 'stride',
  46. type = 'number',
  47. description = [[
  48. The stride of the Buffer, in bytes. When `nil`, the stride will be automatically
  49. computed based on the fields. The stride can not be zero or smaller than the max byte
  50. occupied by one of the fields. The layout of the Buffer may adjust the stride.
  51. ]]
  52. }
  53. }
  54. }
  55. },
  56. returns = {
  57. buffer = {
  58. type = 'Buffer',
  59. description = 'The new Buffer.'
  60. }
  61. },
  62. variants = {
  63. {
  64. arguments = { 'size' },
  65. returns = { 'buffer' }
  66. },
  67. {
  68. arguments = { 'blob' },
  69. returns = { 'buffer' }
  70. },
  71. {
  72. arguments = { 'format', 'length' },
  73. returns = { 'buffer' }
  74. },
  75. {
  76. arguments = { 'format', 'data' },
  77. returns = { 'buffer' }
  78. },
  79. {
  80. arguments = { 'format', 'blob' },
  81. returns = { 'buffer' }
  82. },
  83. {
  84. arguments = { 'type', 'length' },
  85. returns = { 'buffer' }
  86. },
  87. {
  88. arguments = { 'type', 'data' },
  89. returns = { 'buffer' }
  90. },
  91. {
  92. arguments = { 'type', 'blob' },
  93. returns = { 'buffer' }
  94. },
  95. {
  96. deprecated = true,
  97. arguments = { 'length', 'type' },
  98. returns = { 'buffer' }
  99. },
  100. {
  101. deprecated = true,
  102. arguments = { 'data', 'type' },
  103. returns = { 'buffer' }
  104. },
  105. {
  106. deprecated = true,
  107. arguments = { 'length', 'format' },
  108. returns = { 'buffer' }
  109. },
  110. {
  111. deprecated = true,
  112. arguments = { 'data', 'format' },
  113. returns = { 'buffer' }
  114. },
  115. {
  116. deprecated = true,
  117. arguments = { 'blob', 'type' },
  118. returns = { 'buffer' }
  119. },
  120. {
  121. deprecated = true,
  122. arguments = { 'blob', 'format' },
  123. returns = { 'buffer' }
  124. }
  125. },
  126. notes = [[
  127. The format table can contain a list of `DataType`s or a list of tables to provide extra
  128. information about each field. Each inner table has the following keys:
  129. - `type` is the `DataType` of the field and is required.
  130. - `name` is the name of the field, used to match table keys and vertex attribute names.
  131. - `offset` is the byte offset of the field. Any fields with a `nil` offset will be placed next
  132. to each other sequentially in memory, subject to any padding required by the Buffer's layout.
  133. In practice this means that you probably want to provide an `offset` for either all of the
  134. fields or none of them.
  135. - `length` is the array size of the field.
  136. As a shorthand, the name, type, and optionally the length of a field can be provided as a list
  137. instead of using keys.
  138. If no table or Blob is used to define the initial Buffer contents, its data will be undefined.
  139. ]],
  140. example = {
  141. description = 'Examples of different buffer formats.',
  142. code = [[
  143. -- 2 matrices
  144. lovr.graphics.newBuffer('mat4', 2)
  145. -- 3 integers, with initial data
  146. lovr.graphics.newBuffer('int', { 1, 2, 3 })
  147. -- a simple mesh:
  148. lovr.graphics.newBuffer({
  149. { name = 'VertexPosition', type = 'vec3' },
  150. { name = 'VertexColor', type = 'color' }
  151. }, 4)
  152. -- a uniform buffer with vec3's, using the std140 packing
  153. lovr.graphics.newBuffer({ 'vec3', layout = 'std140' }, data)
  154. -- a uniform buffer with key-value fields
  155. lovr.graphics.newBuffer({
  156. { 'AmbientColor', 'vec3' },
  157. { 'LightPosition', 'vec3' },
  158. { 'LightType', 'u32' },
  159. { 'LightColor', 'vec4' },
  160. layout = 'std140'
  161. })
  162. -- a buffer with nested structure and array types
  163. lovr.graphics.newBuffer({
  164. { 'globals', {
  165. { 'ObjectCount', 'int' },
  166. { 'WorldSize', 'vec2' },
  167. { 'Scale', 'float' }
  168. }},
  169. { 'materials', {
  170. { 'Color', 'vec4' },
  171. { 'Glow', 'vec3' },
  172. { 'Roughness', 'float' }
  173. }, length = 32 },
  174. layout = 'std430'
  175. })
  176. -- a buffer using a variable from a shader:
  177. lovr.graphics.newBuffer(shader:getBufferFormat('transforms'))
  178. ]]
  179. },
  180. related = {
  181. 'Shader:getBufferFormat'
  182. }
  183. }