gl-stagesurf.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. void device_stage_texture(gs_device_t *device, gs_stagesurf_t *dst,
  88. gs_texture_t *src)
  89. {
  90. struct gs_texture_2d *tex2d = (struct gs_texture_2d *)src;
  91. if (!can_stage(dst, tex2d))
  92. goto failed;
  93. if (!gl_bind_buffer(GL_PIXEL_PACK_BUFFER, dst->pack_buffer))
  94. goto failed;
  95. if (!gl_bind_texture(GL_TEXTURE_2D, tex2d->base.texture))
  96. goto failed;
  97. glGetTexImage(GL_TEXTURE_2D, 0, dst->gl_format, dst->gl_type, 0);
  98. if (!gl_success("glGetTexImage"))
  99. goto failed;
  100. gl_bind_texture(GL_TEXTURE_2D, 0);
  101. gl_bind_buffer(GL_PIXEL_PACK_BUFFER, 0);
  102. return;
  103. failed:
  104. gl_bind_buffer(GL_PIXEL_PACK_BUFFER, 0);
  105. gl_bind_texture(GL_TEXTURE_2D, 0);
  106. blog(LOG_ERROR, "device_stage_texture (GL) failed");
  107. UNUSED_PARAMETER(device);
  108. }
  109. uint32_t gs_stagesurface_get_width(const gs_stagesurf_t *stagesurf)
  110. {
  111. return stagesurf->width;
  112. }
  113. uint32_t gs_stagesurface_get_height(const gs_stagesurf_t *stagesurf)
  114. {
  115. return stagesurf->height;
  116. }
  117. enum gs_color_format
  118. gs_stagesurface_get_color_format(const gs_stagesurf_t *stagesurf)
  119. {
  120. return stagesurf->format;
  121. }
  122. bool gs_stagesurface_map(gs_stagesurf_t *stagesurf, uint8_t **data,
  123. uint32_t *linesize)
  124. {
  125. if (!gl_bind_buffer(GL_PIXEL_PACK_BUFFER, stagesurf->pack_buffer))
  126. goto fail;
  127. *data = glMapBuffer(GL_PIXEL_PACK_BUFFER, GL_READ_ONLY);
  128. if (!gl_success("glMapBuffer"))
  129. goto fail;
  130. gl_bind_buffer(GL_PIXEL_PACK_BUFFER, 0);
  131. *linesize = stagesurf->bytes_per_pixel * stagesurf->width;
  132. return true;
  133. fail:
  134. blog(LOG_ERROR, "stagesurf_map (GL) failed");
  135. return false;
  136. }
  137. void gs_stagesurface_unmap(gs_stagesurf_t *stagesurf)
  138. {
  139. if (!gl_bind_buffer(GL_PIXEL_PACK_BUFFER, stagesurf->pack_buffer))
  140. return;
  141. glUnmapBuffer(GL_PIXEL_PACK_BUFFER);
  142. gl_success("glUnmapBuffer");
  143. gl_bind_buffer(GL_PIXEL_PACK_BUFFER, 0);
  144. }