setData.lua 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. return {
  2. tag = 'buffer-transfer',
  3. summary = 'Change the data in the Buffer.',
  4. description = 'Copies data to the Buffer from either a table, `Blob`, or `Buffer`.',
  5. arguments = {
  6. table = {
  7. type = 'table',
  8. description = 'A flat or nested table of items to copy to the Buffer (see notes for format).'
  9. },
  10. destinationIndex = {
  11. type = 'number',
  12. default = '1',
  13. description = 'The index of the first value in the Buffer to update.'
  14. },
  15. sourceIndex = {
  16. type = 'number',
  17. default = '1',
  18. description = 'The index in the table to copy from.'
  19. },
  20. count = {
  21. type = 'number',
  22. default = 'nil',
  23. description = [[
  24. The number of items to copy. `nil` will copy as many items as possible, based on the
  25. lengths of the source and destination.
  26. ]]
  27. },
  28. ['...numbers'] = {
  29. type = 'number',
  30. description = 'Numerical components to copy to the buffer.'
  31. },
  32. vector = {
  33. type = '*',
  34. description = 'Vector objects to copy to the buffer.'
  35. },
  36. blob = {
  37. type = 'Blob',
  38. description = 'The Blob to copy data from.'
  39. },
  40. buffer = {
  41. type = 'Buffer',
  42. description = 'The Buffer to copy data from.'
  43. },
  44. sourceOffset = {
  45. type = 'number',
  46. default = '0',
  47. description = 'The byte offset to copy from.'
  48. },
  49. destinationOffset = {
  50. type = 'number',
  51. default = '0',
  52. description = 'The byte offset to copy to.'
  53. },
  54. size = {
  55. type = 'number',
  56. default = 'nil',
  57. description = 'The number of bytes to copy. If nil, copies as many bytes as possible.'
  58. }
  59. },
  60. returns = {},
  61. variants = {
  62. {
  63. arguments = { 'table', 'destinationIndex', 'sourceIndex', 'count' },
  64. returns = {}
  65. },
  66. {
  67. description = 'Copies a single field to a buffer with numbers (buffer length must be 1).',
  68. arguments = { '...numbers' },
  69. returns = {}
  70. },
  71. {
  72. description = 'Copies a single vector to a buffer (buffer length must be 1).',
  73. arguments = { 'vector' },
  74. returns = {}
  75. },
  76. {
  77. arguments = { 'blob', 'destinationOffset', 'sourceOffset', 'size' },
  78. returns = {}
  79. },
  80. {
  81. arguments = { 'buffer', 'destinationOffset', 'sourceOffset', 'size' },
  82. returns = {}
  83. }
  84. },
  85. notes = [[
  86. One gotcha is that unspecified fields will be set to zero. Here's an example:
  87. buffer = lovr.graphics.newBuffer({{ 'x', 'int' }, { 'y', 'int' }})
  88. buffer:setData({ x = 1, y = 1 }) -- set the data
  89. buffer:setData({ x = 1 }) -- set the data, partially
  90. -- buffer data is now {x=1, y=0}!
  91. This doesn't apply to separate items in the buffer. For example, if the Buffer's length is 2
  92. and only the 1st item is set, the second item will remain undisturbed:
  93. buffer = lovr.graphics.newBuffer({{ 'x', 'int' }, { 'y', 'int' }}, 2)
  94. buffer:setData({{ x = 1, y = 1 }, { x = 2, y = 2 }}) -- set the data
  95. buffer:setData({{ x = 1 }}) -- set one item, partially
  96. -- buffer data is now {{x=1, y=0}, {x=2, y=2}}
  97. ]],
  98. example = {
  99. description = 'Various examples of copying different formats.',
  100. code = [[
  101. function lovr.load()
  102. buffer = lovr.graphics.newBuffer('int', 3)
  103. buffer:setData({ 1, 2, 3 })
  104. buffer = lovr.graphics.newBuffer('vec3', 2)
  105. buffer:setData({ 1,2,3, 4,5,6 })
  106. buffer:setData({ { 1, 2, 3 }, { 4, 5, 6 } })
  107. buffer:setData({ vec3(1, 2, 3), vec3(4, 5, 6) })
  108. -- When the Buffer's length is 1, wrapping in table is optional:
  109. buffer = lovr.graphics.newBuffer('vec3')
  110. buffer:setData(1, 2, 3)
  111. buffer:setData(vec3(1, 2, 3))
  112. -- Same for key-value structs
  113. buffer = lovr.graphics.newBuffer({
  114. { 'x', 'float' },
  115. { 'y', 'float' }
  116. })
  117. buffer:setData({ x = 1, y = 2 })
  118. -- Key/value formats
  119. buffer = lovr.graphics.newBuffer({
  120. { 'x', 'float' },
  121. { 'y', 'float' }
  122. }, 3)
  123. buffer:setData({
  124. { x = 1, y = 2 },
  125. { x = 3, y = 4 },
  126. { x = 5, y = 6 }
  127. })
  128. buffer:setData({ 1, 2, 3, 4, 5, 6 })
  129. buffer:setData({ { 1, 2 }, { 3, 4 }, { 5, 6 } })
  130. -- Nested formats
  131. buffer = lovr.graphics.newBuffer({
  132. { 'a', {
  133. {'b', {
  134. 'c', 'float'
  135. }}
  136. }}
  137. })
  138. buffer:setData({ a = { b = { c = 42 } } })
  139. -- Inner arrays
  140. buffer = lovr.graphics.newBuffer({
  141. { 'positions', 'vec3', 10 },
  142. { 'sizes', 'float', 10 },
  143. layout = 'std140'
  144. })
  145. local data = { positions = {}, sizes = {} }
  146. for i = 1, buffer:getLength() do
  147. data.positions[i] = vec3(i, i, i)
  148. data.sizes[i] = i
  149. end
  150. buffer:setData(data)
  151. end
  152. ]]
  153. }
  154. }