gl-stagesurf.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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 boundry */
  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. stagesurf_t device_create_stagesurface(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_create_stagesurface (GL) failed");
  48. stagesurface_destroy(surf);
  49. return NULL;
  50. }
  51. return surf;
  52. }
  53. void stagesurface_destroy(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 aong with glReadPixels, which is really dumb. */
  89. void device_stage_texture(device_t device, stagesurf_t dst, 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(device, dst->width, dst->height, dst->format);
  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,
  105. src->gl_target, src->texture, 0);
  106. if (!gl_success("glFrameBufferTexture2D"))
  107. goto failed_unbind_all;
  108. glReadPixels(0, 0, dst->width, dst->height, dst->gl_format,
  109. dst->gl_type, 0);
  110. if (!gl_success("glReadPixels"))
  111. goto failed_unbind_all;
  112. success = true;
  113. failed_unbind_all:
  114. gl_bind_framebuffer(GL_READ_FRAMEBUFFER, last_fbo);
  115. failed_unbind_buffer:
  116. gl_bind_buffer(GL_PIXEL_PACK_BUFFER, 0);
  117. failed:
  118. if (!success)
  119. blog(LOG_ERROR, "device_stage_texture (GL) failed");
  120. }
  121. #else
  122. void device_stage_texture(device_t device, stagesurf_t dst, texture_t src)
  123. {
  124. struct gs_texture_2d *tex2d = (struct gs_texture_2d*)src;
  125. if (!can_stage(dst, tex2d))
  126. goto failed;
  127. if (!gl_bind_buffer(GL_PIXEL_PACK_BUFFER, dst->pack_buffer))
  128. goto failed;
  129. if (!gl_bind_texture(GL_TEXTURE_2D, tex2d->base.texture))
  130. goto failed;
  131. glGetTexImage(GL_TEXTURE_2D, 0, dst->gl_format, dst->gl_type, 0);
  132. if (!gl_success("glGetTexImage"))
  133. goto failed;
  134. gl_bind_texture(GL_TEXTURE_2D, 0);
  135. gl_bind_buffer(GL_PIXEL_PACK_BUFFER, 0);
  136. return;
  137. failed:
  138. gl_bind_buffer(GL_PIXEL_PACK_BUFFER, 0);
  139. gl_bind_texture(GL_TEXTURE_2D, 0);
  140. blog(LOG_ERROR, "device_stage_texture (GL) failed");
  141. }
  142. #endif
  143. uint32_t stagesurface_getwidth(stagesurf_t stagesurf)
  144. {
  145. return stagesurf->width;
  146. }
  147. uint32_t stagesurface_getheight(stagesurf_t stagesurf)
  148. {
  149. return stagesurf->height;
  150. }
  151. enum gs_color_format stagesurface_getcolorformat(stagesurf_t stagesurf)
  152. {
  153. return stagesurf->format;
  154. }
  155. bool stagesurface_map(stagesurf_t stagesurf, const uint8_t **data,
  156. uint32_t *linesize)
  157. {
  158. if (!gl_bind_buffer(GL_PIXEL_PACK_BUFFER, stagesurf->pack_buffer))
  159. goto fail;
  160. *data = glMapBuffer(GL_PIXEL_PACK_BUFFER, GL_READ_ONLY);
  161. if (!gl_success("glMapBuffer"))
  162. goto fail;
  163. gl_bind_buffer(GL_PIXEL_PACK_BUFFER, 0);
  164. *linesize = stagesurf->bytes_per_pixel * stagesurf->width;
  165. return true;
  166. fail:
  167. blog(LOG_ERROR, "stagesurf_map (GL) failed");
  168. return false;
  169. }
  170. void stagesurface_unmap(stagesurf_t stagesurf)
  171. {
  172. if (!gl_bind_buffer(GL_PIXEL_PACK_BUFFER, stagesurf->pack_buffer))
  173. return;
  174. glUnmapBuffer(GL_PIXEL_PACK_BUFFER);
  175. gl_success("glUnmapBuffer");
  176. gl_bind_buffer(GL_PIXEL_PACK_BUFFER, 0);
  177. }