init.lua 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. return {
  2. summary = 'A big ol\' block of data that can be sent to a Shader.',
  3. description = [[
  4. ShaderBlocks are objects that can hold large amounts of data and can be sent to Shaders. It is
  5. common to use "uniform" variables to send data to shaders, but uniforms are usually limited to
  6. a few kilobytes in size. ShaderBlocks are useful for a few reasons:
  7. - They can hold a lot more data.
  8. - Shaders can modify the data in them, which is really useful for compute shaders.
  9. - Setting the data in a ShaderBlock updates the data for all Shaders using the block, so you
  10. don't need to go around setting the same uniforms in lots of different shaders.
  11. On systems that support compute shaders, ShaderBlocks can optionally be "writable". A writable
  12. ShaderBlock is a little bit slower than a non-writable one, but shaders can modify its contents
  13. and it can be much, much larger than a non-writable ShaderBlock.
  14. ]],
  15. constructor = 'lovr.graphics.newShaderBlock',
  16. notes = [[
  17. - A Shader can use up to 8 ShaderBlocks.
  18. - ShaderBlocks can not contain textures.
  19. - Some systems have bugs with `vec3` variables in ShaderBlocks. If you run into strange bugs,
  20. try switching to a `vec4` for the variable.
  21. ]],
  22. example = [=[
  23. function lovr.load()
  24. -- Create a ShaderBlock to store positions for 1000 models
  25. block = lovr.graphics.newShaderBlock('uniform', {
  26. modelPositions = { 'mat4', 1000 }
  27. }, { usage = 'static' })
  28. -- Write some random transforms to the block
  29. local transforms = {}
  30. for i = 1, 1000 do
  31. transforms[i] = lovr.math.mat4()
  32. local random, randomNormal = lovr.math.random, lovr.math.randomNormal
  33. transforms[i]:translate(randomNormal(8), randomNormal(8), randomNormal(8))
  34. transforms[i]:rotate(random(2 * math.pi), random(), random(), random())
  35. end
  36. block:send('modelPositions', transforms)
  37. -- Create the shader, injecting the shader code for the block
  38. shader = lovr.graphics.newShader(
  39. block:getShaderCode('ModelBlock') .. [[
  40. vec4 lovrMain() {
  41. return lovrProjection * lovrTransform * modelPositions[gl_InstanceID] * lovrVertex;
  42. }
  43. ]])
  44. -- Bind the block to the shader
  45. shader:sendBlock('ModelBlock', block)
  46. model = lovr.graphics.newModel('monkey.obj')
  47. end
  48. -- Draw the model 1000 times, using positions from the shader block
  49. function lovr.draw()
  50. lovr.graphics.setShader(shader)
  51. model:draw(lovr.math.mat4(), 1000)
  52. lovr.graphics.setShader()
  53. end
  54. ]=]
  55. }