gl-stagesurf.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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, 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,
  90. gs_texture_t *src)
  91. {
  92. struct gs_texture_2d *tex2d = (struct gs_texture_2d*)src;
  93. struct fbo_info *fbo;
  94. GLint last_fbo;
  95. bool success = false;
  96. if (!can_stage(dst, tex2d))
  97. goto failed;
  98. if (!gl_bind_buffer(GL_PIXEL_PACK_BUFFER, dst->pack_buffer))
  99. goto failed;
  100. fbo = get_fbo(src, dst->width, dst->height);
  101. if (!gl_get_integer_v(GL_READ_FRAMEBUFFER_BINDING, &last_fbo))
  102. goto failed_unbind_buffer;
  103. if (!gl_bind_framebuffer(GL_READ_FRAMEBUFFER, fbo->fbo))
  104. goto failed_unbind_buffer;
  105. glFramebufferTexture2D(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + 0,
  106. src->gl_target, src->texture, 0);
  107. if (!gl_success("glFrameBufferTexture2D"))
  108. goto failed_unbind_all;
  109. glReadPixels(0, 0, dst->width, dst->height, dst->gl_format,
  110. dst->gl_type, 0);
  111. if (!gl_success("glReadPixels"))
  112. goto failed_unbind_all;
  113. success = true;
  114. failed_unbind_all:
  115. gl_bind_framebuffer(GL_READ_FRAMEBUFFER, last_fbo);
  116. failed_unbind_buffer:
  117. gl_bind_buffer(GL_PIXEL_PACK_BUFFER, 0);
  118. failed:
  119. if (!success)
  120. blog(LOG_ERROR, "device_stage_texture (GL) failed");
  121. UNUSED_PARAMETER(device);
  122. }
  123. #else
  124. void device_stage_texture(gs_device_t *device, gs_stagesurf_t *dst,
  125. gs_texture_t *src)
  126. {
  127. struct gs_texture_2d *tex2d = (struct gs_texture_2d*)src;
  128. if (!can_stage(dst, tex2d))
  129. goto failed;
  130. if (!gl_bind_buffer(GL_PIXEL_PACK_BUFFER, dst->pack_buffer))
  131. goto failed;
  132. if (!gl_bind_texture(GL_TEXTURE_2D, tex2d->base.texture))
  133. goto failed;
  134. glGetTexImage(GL_TEXTURE_2D, 0, dst->gl_format, dst->gl_type, 0);
  135. if (!gl_success("glGetTexImage"))
  136. goto failed;
  137. gl_bind_texture(GL_TEXTURE_2D, 0);
  138. gl_bind_buffer(GL_PIXEL_PACK_BUFFER, 0);
  139. return;
  140. failed:
  141. gl_bind_buffer(GL_PIXEL_PACK_BUFFER, 0);
  142. gl_bind_texture(GL_TEXTURE_2D, 0);
  143. blog(LOG_ERROR, "device_stage_texture (GL) failed");
  144. UNUSED_PARAMETER(device);
  145. }
  146. #endif
  147. uint32_t gs_stagesurface_get_width(const gs_stagesurf_t *stagesurf)
  148. {
  149. return stagesurf->width;
  150. }
  151. uint32_t gs_stagesurface_get_height(const gs_stagesurf_t *stagesurf)
  152. {
  153. return stagesurf->height;
  154. }
  155. enum gs_color_format gs_stagesurface_get_color_format(
  156. const gs_stagesurf_t *stagesurf)
  157. {
  158. return stagesurf->format;
  159. }
  160. bool gs_stagesurface_map(gs_stagesurf_t *stagesurf, uint8_t **data,
  161. uint32_t *linesize)
  162. {
  163. if (!gl_bind_buffer(GL_PIXEL_PACK_BUFFER, stagesurf->pack_buffer))
  164. goto fail;
  165. *data = glMapBuffer(GL_PIXEL_PACK_BUFFER, GL_READ_ONLY);
  166. if (!gl_success("glMapBuffer"))
  167. goto fail;
  168. gl_bind_buffer(GL_PIXEL_PACK_BUFFER, 0);
  169. *linesize = stagesurf->bytes_per_pixel * stagesurf->width;
  170. return true;
  171. fail:
  172. blog(LOG_ERROR, "stagesurf_map (GL) failed");
  173. return false;
  174. }
  175. void gs_stagesurface_unmap(gs_stagesurf_t *stagesurf)
  176. {
  177. if (!gl_bind_buffer(GL_PIXEL_PACK_BUFFER, stagesurf->pack_buffer))
  178. return;
  179. glUnmapBuffer(GL_PIXEL_PACK_BUFFER);
  180. gl_success("glUnmapBuffer");
  181. gl_bind_buffer(GL_PIXEL_PACK_BUFFER, 0);
  182. }