gl-helpers.c 4.7 KB

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