newReadback.lua 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. return {
  2. tag = 'texture-transfer',
  3. summary = 'Read back the contents of the Texture asynchronously.',
  4. description = [[
  5. Creates and returns a new `Readback` that will download the pixels in the Texture from VRAM.
  6. Once the readback is complete, `Readback:getImage` returns an `Image` with a CPU copy of the
  7. data.
  8. ]],
  9. arguments = {
  10. x = {
  11. type = 'number',
  12. default = '0',
  13. description = 'The x offset of the region to download.'
  14. },
  15. y = {
  16. type = 'number',
  17. default = '0',
  18. description = 'The y offset of the region to download.'
  19. },
  20. layer = {
  21. type = 'number',
  22. default = '1',
  23. description = 'The index of the layer to download.'
  24. },
  25. mipmap = {
  26. type = 'number',
  27. default = '1',
  28. description = 'The index of the mipmap level to download.'
  29. },
  30. width = {
  31. type = 'number',
  32. default = 'nil',
  33. description = [[
  34. The width of the pixel rectangle to download. If nil, the "rest" of the width will be used,
  35. based on the texture width and x offset.
  36. ]]
  37. },
  38. height = {
  39. type = 'number',
  40. default = 'nil',
  41. description = [[
  42. The height of the pixel rectangle to download. If nil, the "rest" of the height will be
  43. used, based on the texture height and y offset.
  44. ]]
  45. }
  46. },
  47. returns = {
  48. readback = {
  49. type = 'Readback',
  50. description = 'A new Readback object.'
  51. }
  52. },
  53. variants = {
  54. {
  55. arguments = { 'x', 'y', 'layer', 'mipmap', 'width', 'height' },
  56. returns = { 'readback' }
  57. }
  58. },
  59. notes = [[
  60. The texture must have been created with the `transfer` usage.
  61. Multisampled textures can not be read back.
  62. It is not currently possible to read back a texture view.
  63. ]],
  64. example = {
  65. description = [[
  66. Take a screenshot when pressing a key. This uses an intermediate texture and render pass, to
  67. work around the fact that the window/headset textures don't support transfers.
  68. ]],
  69. code = [[
  70. local screenshot = false
  71. local readback
  72. local texture
  73. local pass
  74. function lovr.keypressed(key)
  75. if key == 'p' then screenshot = true end
  76. end
  77. function lovr.load()
  78. local width, height = lovr.headset.getDisplayDimensions()
  79. texture = lovr.graphics.newTexture(width, height, {
  80. usage = { 'render', 'transfer', 'sample' }
  81. })
  82. pass = lovr.graphics.newPass(texture)
  83. end
  84. function lovr.update()
  85. pass:reset()
  86. for i = 1, lovr.headset.getViewCount() do
  87. pass:setViewPose(i, lovr.headset.getViewPose(i))
  88. pass:setProjection(i, lovr.headset.getViewAngles(i))
  89. end
  90. pass:text('hellooo', 0, 1.7, -1, .1)
  91. lovr.graphics.submit(pass)
  92. if screenshot and not readback then
  93. readback = texture:newReadback()
  94. screenshot = false
  95. end
  96. if readback and readback:isComplete() then
  97. local filename = 'screenshot.png'
  98. lovr.filesystem.write(filename, readback:getImage():encode())
  99. print('saved screenshot to ' .. filename)
  100. readback = nil
  101. end
  102. end
  103. function lovr.draw(p)
  104. p:fill(texture)
  105. end
  106. ]]
  107. },
  108. related = {
  109. 'Texture:getPixels',
  110. 'Buffer:newReadback'
  111. }
  112. }