gl-stagesurf.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /******************************************************************************
  2. Copyright (C) 2013 by Hugh 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,
  34. uint32_t height,
  35. enum gs_color_format color_format)
  36. {
  37. struct gs_stage_surface *surf;
  38. surf = bzalloc(sizeof(struct gs_stage_surface));
  39. surf->device = device;
  40. surf->format = color_format;
  41. surf->width = width;
  42. surf->height = height;
  43. surf->gl_format = convert_gs_format(color_format);
  44. surf->gl_internal_format = convert_gs_internal_format(color_format);
  45. surf->gl_type = get_gl_format_type(color_format);
  46. surf->bytes_per_pixel = gs_get_format_bpp(color_format) / 8;
  47. if (!create_pixel_pack_buffer(surf)) {
  48. blog(LOG_ERROR, "device_stagesurface_create (GL) failed");
  49. gs_stagesurface_destroy(surf);
  50. return NULL;
  51. }
  52. return surf;
  53. }
  54. void gs_stagesurface_destroy(gs_stagesurf_t *stagesurf)
  55. {
  56. if (stagesurf) {
  57. if (stagesurf->pack_buffer)
  58. gl_delete_buffers(1, &stagesurf->pack_buffer);
  59. bfree(stagesurf);
  60. }
  61. }
  62. static bool can_stage(struct gs_stage_surface *dst, struct gs_texture_2d *src)
  63. {
  64. if (!src) {
  65. blog(LOG_ERROR, "Source texture is NULL");
  66. return false;
  67. }
  68. if (src->base.type != GS_TEXTURE_2D) {
  69. blog(LOG_ERROR, "Source texture must be a 2D texture");
  70. return false;
  71. }
  72. if (!dst) {
  73. blog(LOG_ERROR, "Destination surface is NULL");
  74. return false;
  75. }
  76. if (src->base.format != dst->format) {
  77. blog(LOG_ERROR, "Source and destination formats do not match");
  78. return false;
  79. }
  80. if (src->width != dst->width || src->height != dst->height) {
  81. blog(LOG_ERROR, "Source and destination must have the same "
  82. "dimensions");
  83. return false;
  84. }
  85. return true;
  86. }
  87. #ifdef __APPLE__
  88. /* Apparently for mac, PBOs won't do an asynchronous transfer unless you use
  89. * FBOs along with glReadPixels, which is really dumb. */
  90. void device_stage_texture(gs_device_t *device, gs_stagesurf_t *dst,
  91. gs_texture_t *src)
  92. {
  93. struct gs_texture_2d *tex2d = (struct gs_texture_2d *)src;
  94. struct fbo_info *fbo;
  95. GLint last_fbo;
  96. bool success = false;
  97. if (!can_stage(dst, tex2d))
  98. goto failed;
  99. if (!gl_bind_buffer(GL_PIXEL_PACK_BUFFER, dst->pack_buffer))
  100. goto failed;
  101. fbo = get_fbo(src, dst->width, dst->height);
  102. if (!gl_get_integer_v(GL_READ_FRAMEBUFFER_BINDING, &last_fbo))
  103. goto failed_unbind_buffer;
  104. if (!gl_bind_framebuffer(GL_READ_FRAMEBUFFER, fbo->fbo))
  105. goto failed_unbind_buffer;
  106. glFramebufferTexture2D(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + 0,
  107. src->gl_target, src->texture, 0);
  108. if (!gl_success("glFrameBufferTexture2D"))
  109. goto failed_unbind_all;
  110. glReadPixels(0, 0, dst->width, dst->height, dst->gl_format,
  111. dst->gl_type, 0);
  112. if (!gl_success("glReadPixels"))
  113. goto failed_unbind_all;
  114. success = true;
  115. failed_unbind_all:
  116. gl_bind_framebuffer(GL_READ_FRAMEBUFFER, last_fbo);
  117. failed_unbind_buffer:
  118. gl_bind_buffer(GL_PIXEL_PACK_BUFFER, 0);
  119. failed:
  120. if (!success)
  121. blog(LOG_ERROR, "device_stage_texture (GL) failed");
  122. UNUSED_PARAMETER(device);
  123. }
  124. #else
  125. void device_stage_texture(gs_device_t *device, gs_stagesurf_t *dst,
  126. gs_texture_t *src)
  127. {
  128. struct gs_texture_2d *tex2d = (struct gs_texture_2d *)src;
  129. if (!can_stage(dst, tex2d))
  130. goto failed;
  131. if (!gl_bind_buffer(GL_PIXEL_PACK_BUFFER, dst->pack_buffer))
  132. goto failed;
  133. if (!gl_bind_texture(GL_TEXTURE_2D, tex2d->base.texture))
  134. goto failed;
  135. glGetTexImage(GL_TEXTURE_2D, 0, dst->gl_format, dst->gl_type, 0);
  136. if (!gl_success("glGetTexImage"))
  137. goto failed;
  138. gl_bind_texture(GL_TEXTURE_2D, 0);
  139. gl_bind_buffer(GL_PIXEL_PACK_BUFFER, 0);
  140. return;
  141. failed:
  142. gl_bind_buffer(GL_PIXEL_PACK_BUFFER, 0);
  143. gl_bind_texture(GL_TEXTURE_2D, 0);
  144. blog(LOG_ERROR, "device_stage_texture (GL) failed");
  145. UNUSED_PARAMETER(device);
  146. }
  147. #endif
  148. uint32_t gs_stagesurface_get_width(const gs_stagesurf_t *stagesurf)
  149. {
  150. return stagesurf->width;
  151. }
  152. uint32_t gs_stagesurface_get_height(const gs_stagesurf_t *stagesurf)
  153. {
  154. return stagesurf->height;
  155. }
  156. enum gs_color_format
  157. gs_stagesurface_get_color_format(const gs_stagesurf_t *stagesurf)
  158. {
  159. return stagesurf->format;
  160. }
  161. bool gs_stagesurface_map(gs_stagesurf_t *stagesurf, uint8_t **data,
  162. uint32_t *linesize)
  163. {
  164. if (!gl_bind_buffer(GL_PIXEL_PACK_BUFFER, stagesurf->pack_buffer))
  165. goto fail;
  166. *data = glMapBuffer(GL_PIXEL_PACK_BUFFER, GL_READ_ONLY);
  167. if (!gl_success("glMapBuffer"))
  168. goto fail;
  169. gl_bind_buffer(GL_PIXEL_PACK_BUFFER, 0);
  170. *linesize = stagesurf->bytes_per_pixel * stagesurf->width;
  171. return true;
  172. fail:
  173. blog(LOG_ERROR, "stagesurf_map (GL) failed");
  174. return false;
  175. }
  176. void gs_stagesurface_unmap(gs_stagesurf_t *stagesurf)
  177. {
  178. if (!gl_bind_buffer(GL_PIXEL_PACK_BUFFER, stagesurf->pack_buffer))
  179. return;
  180. glUnmapBuffer(GL_PIXEL_PACK_BUFFER);
  181. gl_success("glUnmapBuffer");
  182. gl_bind_buffer(GL_PIXEL_PACK_BUFFER, 0);
  183. }