gl-stagesurf.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /******************************************************************************
  2. Copyright (C) 2023 by Lain Bailey <[email protected]>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. ******************************************************************************/
  14. #include "gl-subsystem.h"
  15. static bool create_pixel_pack_buffer(struct gs_stage_surface *surf)
  16. {
  17. GLsizeiptr size;
  18. bool success = true;
  19. if (!gl_gen_buffers(1, &surf->pack_buffer))
  20. return false;
  21. if (!gl_bind_buffer(GL_PIXEL_PACK_BUFFER, surf->pack_buffer))
  22. return false;
  23. size = surf->width * surf->bytes_per_pixel;
  24. size = (size + 3) & 0xFFFFFFFC; /* align width to 4-byte boundary */
  25. size *= surf->height;
  26. glBufferData(GL_PIXEL_PACK_BUFFER, size, 0, GL_DYNAMIC_READ);
  27. if (!gl_success("glBufferData"))
  28. success = false;
  29. if (!gl_bind_buffer(GL_PIXEL_PACK_BUFFER, 0))
  30. success = false;
  31. return success;
  32. }
  33. gs_stagesurf_t *device_stagesurface_create(gs_device_t *device, uint32_t width, uint32_t height,
  34. enum gs_color_format color_format)
  35. {
  36. struct gs_stage_surface *surf;
  37. surf = bzalloc(sizeof(struct gs_stage_surface));
  38. surf->device = device;
  39. surf->format = color_format;
  40. surf->width = width;
  41. surf->height = height;
  42. surf->gl_format = convert_gs_format(color_format);
  43. surf->gl_internal_format = convert_gs_internal_format(color_format);
  44. surf->gl_type = get_gl_format_type(color_format);
  45. surf->bytes_per_pixel = gs_get_format_bpp(color_format) / 8;
  46. if (!create_pixel_pack_buffer(surf)) {
  47. blog(LOG_ERROR, "device_stagesurface_create (GL) failed");
  48. gs_stagesurface_destroy(surf);
  49. return NULL;
  50. }
  51. return surf;
  52. }
  53. void gs_stagesurface_destroy(gs_stagesurf_t *stagesurf)
  54. {
  55. if (stagesurf) {
  56. if (stagesurf->pack_buffer)
  57. gl_delete_buffers(1, &stagesurf->pack_buffer);
  58. bfree(stagesurf);
  59. }
  60. }
  61. static bool can_stage(struct gs_stage_surface *dst, struct gs_texture_2d *src)
  62. {
  63. if (!src) {
  64. blog(LOG_ERROR, "Source texture is NULL");
  65. return false;
  66. }
  67. if (src->base.type != GS_TEXTURE_2D) {
  68. blog(LOG_ERROR, "Source texture must be a 2D texture");
  69. return false;
  70. }
  71. if (!dst) {
  72. blog(LOG_ERROR, "Destination surface is NULL");
  73. return false;
  74. }
  75. if (src->base.format != dst->format) {
  76. blog(LOG_ERROR, "Source and destination formats do not match");
  77. return false;
  78. }
  79. if (src->width != dst->width || src->height != dst->height) {
  80. blog(LOG_ERROR, "Source and destination must have the same "
  81. "dimensions");
  82. return false;
  83. }
  84. return true;
  85. }
  86. #ifdef __APPLE__
  87. /* Apparently for mac, PBOs won't do an asynchronous transfer unless you use
  88. * FBOs along with glReadPixels, which is really dumb. */
  89. void device_stage_texture(gs_device_t *device, gs_stagesurf_t *dst, gs_texture_t *src)
  90. {
  91. struct gs_texture_2d *tex2d = (struct gs_texture_2d *)src;
  92. struct fbo_info *fbo;
  93. GLint last_fbo;
  94. bool success = false;
  95. if (!can_stage(dst, tex2d))
  96. goto failed;
  97. if (!gl_bind_buffer(GL_PIXEL_PACK_BUFFER, dst->pack_buffer))
  98. goto failed;
  99. fbo = get_fbo(src, dst->width, dst->height);
  100. if (!gl_get_integer_v(GL_READ_FRAMEBUFFER_BINDING, &last_fbo))
  101. goto failed_unbind_buffer;
  102. if (!gl_bind_framebuffer(GL_READ_FRAMEBUFFER, fbo->fbo))
  103. goto failed_unbind_buffer;
  104. glFramebufferTexture2D(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + 0, src->gl_target, src->texture, 0);
  105. if (!gl_success("glFrameBufferTexture2D"))
  106. goto failed_unbind_all;
  107. glReadPixels(0, 0, dst->width, dst->height, dst->gl_format, dst->gl_type, 0);
  108. if (!gl_success("glReadPixels"))
  109. goto failed_unbind_all;
  110. success = true;
  111. failed_unbind_all:
  112. gl_bind_framebuffer(GL_READ_FRAMEBUFFER, last_fbo);
  113. failed_unbind_buffer:
  114. gl_bind_buffer(GL_PIXEL_PACK_BUFFER, 0);
  115. failed:
  116. if (!success)
  117. blog(LOG_ERROR, "device_stage_texture (GL) failed");
  118. UNUSED_PARAMETER(device);
  119. }
  120. #else
  121. void device_stage_texture(gs_device_t *device, gs_stagesurf_t *dst, gs_texture_t *src)
  122. {
  123. struct gs_texture_2d *tex2d = (struct gs_texture_2d *)src;
  124. if (!can_stage(dst, tex2d))
  125. goto failed;
  126. if (!gl_bind_buffer(GL_PIXEL_PACK_BUFFER, dst->pack_buffer))
  127. goto failed;
  128. if (!gl_bind_texture(GL_TEXTURE_2D, tex2d->base.texture))
  129. goto failed;
  130. glGetTexImage(GL_TEXTURE_2D, 0, dst->gl_format, dst->gl_type, 0);
  131. if (!gl_success("glGetTexImage"))
  132. goto failed;
  133. gl_bind_texture(GL_TEXTURE_2D, 0);
  134. gl_bind_buffer(GL_PIXEL_PACK_BUFFER, 0);
  135. return;
  136. failed:
  137. gl_bind_buffer(GL_PIXEL_PACK_BUFFER, 0);
  138. gl_bind_texture(GL_TEXTURE_2D, 0);
  139. blog(LOG_ERROR, "device_stage_texture (GL) failed");
  140. UNUSED_PARAMETER(device);
  141. }
  142. #endif
  143. uint32_t gs_stagesurface_get_width(const gs_stagesurf_t *stagesurf)
  144. {
  145. return stagesurf->width;
  146. }
  147. uint32_t gs_stagesurface_get_height(const gs_stagesurf_t *stagesurf)
  148. {
  149. return stagesurf->height;
  150. }
  151. enum gs_color_format gs_stagesurface_get_color_format(const gs_stagesurf_t *stagesurf)
  152. {
  153. return stagesurf->format;
  154. }
  155. bool gs_stagesurface_map(gs_stagesurf_t *stagesurf, uint8_t **data, uint32_t *linesize)
  156. {
  157. if (!gl_bind_buffer(GL_PIXEL_PACK_BUFFER, stagesurf->pack_buffer))
  158. goto fail;
  159. *data = glMapBuffer(GL_PIXEL_PACK_BUFFER, GL_READ_ONLY);
  160. if (!gl_success("glMapBuffer"))
  161. goto fail;
  162. gl_bind_buffer(GL_PIXEL_PACK_BUFFER, 0);
  163. *linesize = stagesurf->bytes_per_pixel * stagesurf->width;
  164. return true;
  165. fail:
  166. blog(LOG_ERROR, "stagesurf_map (GL) failed");
  167. return false;
  168. }
  169. void gs_stagesurface_unmap(gs_stagesurf_t *stagesurf)
  170. {
  171. if (!gl_bind_buffer(GL_PIXEL_PACK_BUFFER, stagesurf->pack_buffer))
  172. return;
  173. glUnmapBuffer(GL_PIXEL_PACK_BUFFER);
  174. gl_success("glUnmapBuffer");
  175. gl_bind_buffer(GL_PIXEL_PACK_BUFFER, 0);
  176. }