gl-helpers.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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 uint8_t ***p_data)
  19. {
  20. bool success = true;
  21. const uint8_t **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)
  42. width = 1;
  43. if (height == 0)
  44. height = 1;
  45. }
  46. if (data)
  47. *p_data = data;
  48. return success;
  49. }
  50. static bool gl_copy_fbo(struct gs_texture *dst, uint32_t dst_x, uint32_t dst_y,
  51. struct gs_texture *src, uint32_t src_x, uint32_t src_y,
  52. uint32_t width, uint32_t height)
  53. {
  54. struct fbo_info *fbo = get_fbo(src, width, height);
  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->gl_target, dst->texture))
  64. goto fail;
  65. glFramebufferTexture2D(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + 0,
  66. src->gl_target, src->texture, 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->gl_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->gl_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, struct gs_texture *dst,
  85. uint32_t dst_x, uint32_t dst_y, struct gs_texture *src,
  86. uint32_t src_x, uint32_t src_y, uint32_t width,
  87. uint32_t height)
  88. {
  89. bool success = false;
  90. if (device->copy_type == COPY_TYPE_ARB) {
  91. glCopyImageSubData(src->texture, src->gl_target, 0, src_x,
  92. src_y, 0, dst->texture, dst->gl_target, 0,
  93. dst_x, dst_y, 0, width, height, 1);
  94. success = gl_success("glCopyImageSubData");
  95. } else if (device->copy_type == COPY_TYPE_NV) {
  96. glCopyImageSubDataNV(src->texture, src->gl_target, 0, src_x,
  97. src_y, 0, dst->texture, dst->gl_target, 0,
  98. dst_x, dst_y, 0, width, height, 1);
  99. success = gl_success("glCopyImageSubDataNV");
  100. } else if (device->copy_type == COPY_TYPE_FBO_BLIT) {
  101. success = gl_copy_fbo(dst, dst_x, dst_y, src, src_x, src_y,
  102. width, height);
  103. if (!success)
  104. blog(LOG_ERROR, "gl_copy_texture failed");
  105. }
  106. return success;
  107. }
  108. bool gl_create_buffer(GLenum target, GLuint *buffer, GLsizeiptr size,
  109. const GLvoid *data, GLenum usage)
  110. {
  111. bool success;
  112. if (!gl_gen_buffers(1, buffer))
  113. return false;
  114. if (!gl_bind_buffer(target, *buffer))
  115. return false;
  116. glBufferData(target, size, data, usage);
  117. success = gl_success("glBufferData");
  118. gl_bind_buffer(target, 0);
  119. return success;
  120. }
  121. bool update_buffer(GLenum target, GLuint buffer, const void *data, size_t size)
  122. {
  123. void *ptr;
  124. bool success = true;
  125. if (!gl_bind_buffer(target, buffer))
  126. return false;
  127. /* glMapBufferRange with these flags will actually give far better
  128. * performance than a plain glMapBuffer call */
  129. ptr = glMapBufferRange(target, 0, size,
  130. GL_MAP_WRITE_BIT | GL_MAP_INVALIDATE_BUFFER_BIT);
  131. success = gl_success("glMapBufferRange");
  132. if (success && ptr) {
  133. memcpy(ptr, data, size);
  134. glUnmapBuffer(target);
  135. }
  136. gl_bind_buffer(target, 0);
  137. return success;
  138. }