gl-stagesurf.c 5.4 KB

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