gl-stagesurf.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. static bool gl_init_stage_surface(struct gs_stage_surface *surf)
  34. {
  35. bool success = true;
  36. if (!gl_gen_textures(1, &surf->texture))
  37. return false;
  38. if (!gl_bind_texture(GL_TEXTURE_2D, surf->texture))
  39. return false;
  40. if (!gl_init_face(GL_TEXTURE_2D, surf->gl_type, 1, surf->gl_format,
  41. surf->gl_internal_format, false,
  42. surf->width, surf->height, 0, NULL))
  43. success = false;
  44. if (!gl_bind_texture(GL_TEXTURE_2D, 0))
  45. success = false;
  46. if (success)
  47. success = create_pixel_pack_buffer(surf);
  48. return success;
  49. }
  50. stagesurf_t device_create_stagesurface(device_t device, uint32_t width,
  51. uint32_t height, enum gs_color_format color_format)
  52. {
  53. struct gs_stage_surface *surf;
  54. surf = bmalloc(sizeof(struct gs_stage_surface));
  55. memset(surf, 0, sizeof(struct gs_stage_surface));
  56. surf->format = color_format;
  57. surf->width = width;
  58. surf->height = height;
  59. surf->gl_format = convert_gs_format(color_format);
  60. surf->gl_internal_format = convert_gs_internal_format(color_format);
  61. surf->gl_type = get_gl_format_type(color_format);
  62. surf->bytes_per_pixel = gs_get_format_bpp(color_format)/8;
  63. if (!gl_init_stage_surface(surf)) {
  64. blog(LOG_ERROR, "device_create_stagesurface (GL) failed");
  65. stagesurface_destroy(surf);
  66. return NULL;
  67. }
  68. return surf;
  69. }
  70. void stagesurface_destroy(stagesurf_t stagesurf)
  71. {
  72. if (stagesurf) {
  73. if (stagesurf->pack_buffer)
  74. gl_delete_buffers(1, &stagesurf->pack_buffer);
  75. if (stagesurf->texture)
  76. gl_delete_textures(1, &stagesurf->texture);
  77. bfree(stagesurf);
  78. }
  79. }
  80. static bool can_stage(struct gs_stage_surface *dst, struct gs_texture_2d *src)
  81. {
  82. if (!src) {
  83. blog(LOG_ERROR, "Source texture is NULL");
  84. return false;
  85. }
  86. if (src->base.type != GS_TEXTURE_2D) {
  87. blog(LOG_ERROR, "Source texture must be a 2D texture");
  88. return false;
  89. }
  90. if (!dst) {
  91. blog(LOG_ERROR, "Destination surface is NULL");
  92. return false;
  93. }
  94. if (src->base.format != dst->format) {
  95. blog(LOG_ERROR, "Source and destination formats do not match");
  96. return false;
  97. }
  98. if (src->width != dst->width || src->height != dst->height) {
  99. blog(LOG_ERROR, "Source and destination must have the same "
  100. "dimensions");
  101. return false;
  102. }
  103. return true;
  104. }
  105. void device_stage_texture(device_t device, stagesurf_t dst, texture_t src)
  106. {
  107. struct gs_texture_2d *tex2d = (struct gs_texture_2d*)src;
  108. if (!can_stage(dst, tex2d))
  109. goto failed;
  110. if (!gl_copy_texture(device, dst->texture, GL_TEXTURE_2D,
  111. tex2d->base.texture, GL_TEXTURE_2D,
  112. dst->width, dst->height))
  113. goto failed;
  114. if (!gl_bind_buffer(GL_TEXTURE_2D, dst->texture))
  115. goto failed;
  116. if (!gl_bind_buffer(GL_PIXEL_PACK_BUFFER, dst->pack_buffer))
  117. goto failed;
  118. glGetTexImage(GL_TEXTURE_2D, 0, dst->gl_format, dst->gl_type, 0);
  119. if (!gl_success("glGetTexImage"))
  120. goto failed;
  121. gl_bind_buffer(GL_PIXEL_PACK_BUFFER, 0);
  122. gl_bind_texture(GL_TEXTURE_2D, 0);
  123. return;
  124. failed:
  125. gl_bind_buffer(GL_PIXEL_PACK_BUFFER, 0);
  126. gl_bind_texture(GL_TEXTURE_2D, 0);
  127. blog(LOG_ERROR, "device_stage_texture (GL) failed");
  128. }
  129. uint32_t stagesurface_getwidth(stagesurf_t stagesurf)
  130. {
  131. return stagesurf->width;
  132. }
  133. uint32_t stagesurface_getheight(stagesurf_t stagesurf)
  134. {
  135. return stagesurf->height;
  136. }
  137. enum gs_color_format stagesurface_getcolorformat(stagesurf_t stagesurf)
  138. {
  139. return stagesurf->format;
  140. }
  141. bool stagesurface_map(stagesurf_t stagesurf, const void **data,
  142. uint32_t *row_bytes)
  143. {
  144. if (!gl_bind_buffer(GL_PIXEL_PACK_BUFFER, stagesurf->pack_buffer))
  145. goto fail;
  146. *data = glMapBuffer(GL_PIXEL_PACK_BUFFER, GL_READ_ONLY);
  147. if (!gl_success("glMapBuffer"))
  148. goto fail;
  149. gl_bind_buffer(GL_PIXEL_PACK_BUFFER, 0);
  150. *row_bytes = stagesurf->bytes_per_pixel * stagesurf->width;
  151. return true;
  152. fail:
  153. blog(LOG_ERROR, "stagesurf_map (GL) failed");
  154. return false;
  155. }
  156. void stagesurface_unmap(stagesurf_t stagesurf)
  157. {
  158. if (!gl_bind_buffer(GL_PIXEL_PACK_BUFFER, stagesurf->pack_buffer))
  159. return;
  160. glUnmapBuffer(GL_PIXEL_PACK_BUFFER);
  161. gl_success("glUnmapBuffer");
  162. gl_bind_buffer(GL_PIXEL_PACK_BUFFER, 0);
  163. }