gl-helpers.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. bool gl_init_face(GLenum target, GLenum type, uint32_t num_levels,
  16. GLenum format, GLint internal_format, bool compressed,
  17. uint32_t width, uint32_t height, uint32_t size,
  18. const void ***p_data)
  19. {
  20. bool success = true;
  21. const void **data = p_data ? *p_data : NULL;
  22. uint32_t i;
  23. for (i = 0; i < num_levels; i++) {
  24. if (compressed) {
  25. glCompressedTexImage2D(target, i, internal_format,
  26. width, height, 0, size,
  27. data ? *data : NULL);
  28. if (!gl_success("glCompressedTexImage2D"))
  29. success = false;
  30. } else {
  31. glTexImage2D(target, i, internal_format, width, height,
  32. 0, format, type, data ? *data : NULL);
  33. if (!gl_success("glTexImage2D"))
  34. success = false;
  35. }
  36. if (data)
  37. data++;
  38. size /= 4;
  39. width /= 2;
  40. height /= 2;
  41. if (width == 0) width = 1;
  42. if (height == 0) height = 1;
  43. }
  44. if (data)
  45. *p_data = data;
  46. return success;
  47. }
  48. static bool gl_copy_fbo(struct gs_device *device,
  49. GLuint dst, GLenum dst_target, uint32_t dst_x, uint32_t dst_y,
  50. GLuint src, GLenum src_target, uint32_t src_x, uint32_t src_y,
  51. uint32_t width, uint32_t height,
  52. enum gs_color_format format)
  53. {
  54. struct fbo_info *fbo = get_fbo(device, width, height, format);
  55. GLint last_fbo;
  56. bool success = false;
  57. if (!fbo)
  58. return false;
  59. if (!gl_get_integer_v(GL_READ_FRAMEBUFFER_BINDING, &last_fbo))
  60. return false;
  61. if (!gl_bind_framebuffer(GL_READ_FRAMEBUFFER, fbo->fbo))
  62. return false;
  63. if (!gl_bind_texture(dst_target, dst))
  64. goto fail;
  65. glFramebufferTexture2D(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + 0,
  66. src_target, src, 0);
  67. if (!gl_success("glFrameBufferTexture2D"))
  68. goto fail;
  69. glReadBuffer(GL_COLOR_ATTACHMENT0 + 0);
  70. if (!gl_success("glReadBuffer"))
  71. goto fail;
  72. glCopyTexSubImage2D(dst_target, 0, dst_x, dst_y, src_x, src_y,
  73. width, height);
  74. if (!gl_success("glCopyTexSubImage2D"))
  75. goto fail;
  76. success = true;
  77. fail:
  78. if (!gl_bind_texture(dst_target, 0))
  79. success = false;
  80. if (!gl_bind_framebuffer(GL_READ_FRAMEBUFFER, last_fbo))
  81. success = false;
  82. return success;
  83. }
  84. bool gl_copy_texture(struct gs_device *device,
  85. GLuint dst, GLenum dst_target, uint32_t dst_x, uint32_t dst_y,
  86. GLuint src, GLenum src_target, uint32_t src_x, uint32_t src_y,
  87. uint32_t width, uint32_t height, enum gs_color_format format)
  88. {
  89. bool success = false;
  90. if (device->copy_type == COPY_TYPE_ARB) {
  91. glCopyImageSubData(src, src_target, 0, src_x, src_y, 0,
  92. dst, dst_target, 0, dst_x, dst_y, 0,
  93. width, height, 1);
  94. success = gl_success("glCopyImageSubData");
  95. } else if (device->copy_type == COPY_TYPE_NV) {
  96. glCopyImageSubDataNV(src, src_target, 0, src_x, src_y, 0,
  97. dst, dst_target, 0, dst_x, dst_y, 0,
  98. width, height, 1);
  99. success = gl_success("glCopyImageSubDataNV");
  100. } else if (device->copy_type == COPY_TYPE_FBO_BLIT) {
  101. success = gl_copy_fbo(device, dst, dst_target, dst_x, dst_y,
  102. src, src_target, src_x, src_y,
  103. width, height, format);
  104. if (!success)
  105. blog(LOG_ERROR, "gl_copy_texture failed");
  106. }
  107. return success;
  108. }
  109. bool gl_create_buffer(GLenum target, GLuint *buffer, GLsizeiptr size,
  110. const GLvoid *data, GLenum usage)
  111. {
  112. bool success;
  113. if (!gl_gen_buffers(1, buffer))
  114. return false;
  115. if (!gl_bind_buffer(target, *buffer))
  116. return false;
  117. glBufferData(target, size, data, usage);
  118. success = gl_success("glBufferData");
  119. gl_bind_buffer(target, 0);
  120. return success;
  121. }
  122. bool update_buffer(GLenum target, GLuint buffer, void *data, size_t size)
  123. {
  124. void *ptr;
  125. bool success = true;
  126. if (!gl_bind_buffer(target, buffer))
  127. return false;
  128. /* glMapBufferRange with these flags will actually give far better
  129. * performance than a plain glMapBuffer call */
  130. ptr = glMapBufferRange(target, 0, size,
  131. GL_MAP_WRITE_BIT |
  132. GL_MAP_INVALIDATE_BUFFER_BIT);
  133. success = gl_success("glMapBufferRange");
  134. if (success && ptr) {
  135. memcpy(ptr, data, size);
  136. glUnmapBuffer(target);
  137. }
  138. gl_bind_buffer(target, 0);
  139. return success;
  140. }