gl-helpers.c 4.6 KB

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