newShaderBlock.lua 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. return {
  2. tag = 'graphicsObjects',
  3. summary = 'Create a new ShaderBlock.',
  4. description = [[
  5. Creates a new ShaderBlock from a table of variable definitions with their names and types.
  6. ]],
  7. arguments = {
  8. {
  9. name = 'type',
  10. type = 'BlockType',
  11. description = 'Whether the block will be used for read-only uniform data or compute shaders.'
  12. },
  13. {
  14. name = 'uniforms',
  15. type = 'table',
  16. description = [[
  17. A table where the keys are uniform names and the values are uniform types. Uniform arrays
  18. can be specified by supplying a table as the uniform's value containing the type and the
  19. array size.
  20. ]]
  21. },
  22. {
  23. name = 'flags',
  24. type = 'table',
  25. default = '{}',
  26. description = 'Optional settings.',
  27. table = {
  28. {
  29. name = 'usage',
  30. type = 'BufferUsage',
  31. default = [['dynamic']],
  32. description = 'How the data in the block will be updated.'
  33. },
  34. {
  35. name = 'readable',
  36. type = 'boolean',
  37. default = 'false',
  38. description = 'Whether the data in the block can be read using `ShaderBlock:read`.'
  39. }
  40. }
  41. }
  42. },
  43. returns = {
  44. {
  45. name = 'shaderBlock',
  46. type = 'ShaderBlock',
  47. description = 'The new ShaderBlock.'
  48. }
  49. },
  50. notes = [[
  51. `compute` blocks can only be true if compute shaders are supported, see
  52. `lovr.graphics.getFeatures`. Compute blocks may be slightly slower than uniform blocks, but
  53. they can also be much, much larger. Uniform blocks are usually limited to around 16 kilobytes
  54. in size, depending on hardware.
  55. ]],
  56. example = {
  57. description = 'Create a ShaderBlock to hold a block of useful shader data.',
  58. code = [=[
  59. function lovr.load()
  60. block = lovr.graphics.newShaderBlock('uniform', {
  61. time = 'float',
  62. lightCount = 'int',
  63. lightPositions = { 'vec3', 16 },
  64. lightColors = { 'vec3', 16 },
  65. objectCount = 'int',
  66. objectTransforms = { 'mat4', 256 }
  67. }, 'uniform')
  68. shader = lovr.graphics.newShader(
  69. block:getShaderCode('Block') .. -- Define the block in the shader
  70. [[
  71. vec4 lovrMain() {
  72. // ...use the object transforms from the block
  73. return lovrProjection * lovrTransform * lovrVertex;
  74. }
  75. ]],
  76. block:getShaderCode('Block') ..
  77. [[
  78. vec4 lovrMain() {
  79. // ...use the lights from the block
  80. return lovrGraphicsColor * texture(lovrDiffuseTexture, lovrTexCoord);
  81. }
  82. ]]
  83. )
  84. -- Bind the block to the shader
  85. shader:sendBlock('Block', block)
  86. end
  87. -- Update the data in the block every frame
  88. function lovr.update(dt)
  89. block:send('time', lovr.timer.getTime())
  90. block:send('lightCount', lightCount)
  91. block:send('lightPositions', { { x, y, z}, { x, y, z } })
  92. -- etc.
  93. end
  94. ]=]
  95. },
  96. related = {
  97. 'lovr.graphics.newShader',
  98. 'lovr.graphics.newComputeShader'
  99. }
  100. }