gl-texture3d.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. static bool gl_init_volume(GLenum type, uint32_t num_levels, GLenum format, GLint internal_format, bool compressed,
  16. uint32_t width, uint32_t height, uint32_t depth, const uint8_t *const **p_data)
  17. {
  18. bool success = true;
  19. const uint8_t *const *data = p_data ? *p_data : NULL;
  20. uint32_t i;
  21. const uint32_t bpp = gs_get_format_bpp(format);
  22. for (i = 0; i < num_levels; i++) {
  23. if (compressed) {
  24. uint32_t mip_size = width * height * depth * bpp / 8;
  25. glCompressedTexImage3D(GL_TEXTURE_3D, i, internal_format, width, height, depth, 0, mip_size,
  26. data ? *data : NULL);
  27. if (!gl_success("glCompressedTexImage3D"))
  28. success = false;
  29. } else {
  30. glTexImage3D(GL_TEXTURE_3D, i, internal_format, width, height, depth, 0, format, type,
  31. data ? *data : NULL);
  32. if (!gl_success("glTexImage3D"))
  33. success = false;
  34. }
  35. if (data)
  36. data++;
  37. if (width > 1)
  38. width /= 2;
  39. if (height > 1)
  40. height /= 2;
  41. if (depth > 1)
  42. depth /= 2;
  43. }
  44. if (data)
  45. *p_data = data;
  46. return success;
  47. }
  48. static bool upload_texture_3d(struct gs_texture_3d *tex, const uint8_t *const *data)
  49. {
  50. uint32_t num_levels = tex->base.levels;
  51. bool compressed = gs_is_compressed_format(tex->base.format);
  52. bool success;
  53. if (!num_levels)
  54. num_levels = gs_get_total_levels(tex->width, tex->height, tex->depth);
  55. if (!gl_bind_texture(GL_TEXTURE_3D, tex->base.texture))
  56. return false;
  57. success = gl_init_volume(tex->base.gl_type, num_levels, tex->base.gl_format, tex->base.gl_internal_format,
  58. compressed, tex->width, tex->height, tex->depth, &data);
  59. if (!gl_tex_param_i(GL_TEXTURE_3D, GL_TEXTURE_MAX_LEVEL, num_levels - 1))
  60. success = false;
  61. if (!gl_bind_texture(GL_TEXTURE_3D, 0))
  62. success = false;
  63. return success;
  64. }
  65. static bool create_pixel_unpack_buffer(struct gs_texture_3d *tex)
  66. {
  67. GLsizeiptr size;
  68. bool success = true;
  69. if (!gl_gen_buffers(1, &tex->unpack_buffer))
  70. return false;
  71. if (!gl_bind_buffer(GL_PIXEL_UNPACK_BUFFER, tex->unpack_buffer))
  72. return false;
  73. size = tex->width * gs_get_format_bpp(tex->base.format);
  74. if (!gs_is_compressed_format(tex->base.format)) {
  75. size /= 8;
  76. size = (size + 3) & 0xFFFFFFFC;
  77. size *= tex->height;
  78. size *= tex->depth;
  79. } else {
  80. size *= tex->height;
  81. size *= tex->depth;
  82. size /= 8;
  83. }
  84. glBufferData(GL_PIXEL_UNPACK_BUFFER, size, 0, GL_DYNAMIC_DRAW);
  85. if (!gl_success("glBufferData"))
  86. success = false;
  87. if (!gl_bind_buffer(GL_PIXEL_UNPACK_BUFFER, 0))
  88. success = false;
  89. return success;
  90. }
  91. gs_texture_t *device_voltexture_create(gs_device_t *device, uint32_t width, uint32_t height, uint32_t depth,
  92. enum gs_color_format color_format, uint32_t levels, const uint8_t *const *data,
  93. uint32_t flags)
  94. {
  95. struct gs_texture_3d *tex = bzalloc(sizeof(struct gs_texture_3d));
  96. tex->base.device = device;
  97. tex->base.type = GS_TEXTURE_3D;
  98. tex->base.format = color_format;
  99. tex->base.levels = levels;
  100. tex->base.gl_format = convert_gs_format(color_format);
  101. tex->base.gl_internal_format = convert_gs_internal_format(color_format);
  102. tex->base.gl_type = get_gl_format_type(color_format);
  103. tex->base.gl_target = GL_TEXTURE_3D;
  104. tex->base.is_dynamic = (flags & GS_DYNAMIC) != 0;
  105. tex->base.is_render_target = false;
  106. tex->base.is_dummy = (flags & GS_GL_DUMMYTEX) != 0;
  107. tex->base.gen_mipmaps = (flags & GS_BUILD_MIPMAPS) != 0;
  108. tex->width = width;
  109. tex->height = height;
  110. tex->depth = depth;
  111. if (!gl_gen_textures(1, &tex->base.texture))
  112. goto fail;
  113. if (!tex->base.is_dummy) {
  114. if (tex->base.is_dynamic && !create_pixel_unpack_buffer(tex))
  115. goto fail;
  116. if (!upload_texture_3d(tex, data))
  117. goto fail;
  118. } else {
  119. if (!gl_bind_texture(GL_TEXTURE_3D, tex->base.texture))
  120. goto fail;
  121. bool compressed = gs_is_compressed_format(tex->base.format);
  122. bool did_init = gl_init_volume(tex->base.gl_type, 1, tex->base.gl_format, tex->base.gl_internal_format,
  123. compressed, tex->width, tex->height, tex->depth, NULL);
  124. did_init = gl_tex_param_i(GL_TEXTURE_3D, GL_TEXTURE_MAX_LEVEL, 0);
  125. bool did_unbind = gl_bind_texture(GL_TEXTURE_3D, 0);
  126. if (!did_init || !did_unbind)
  127. goto fail;
  128. }
  129. return (gs_texture_t *)tex;
  130. fail:
  131. gs_texture_destroy((gs_texture_t *)tex);
  132. blog(LOG_ERROR, "device_voltexture_create (GL) failed");
  133. return NULL;
  134. }
  135. void gs_voltexture_destroy(gs_texture_t *voltex)
  136. {
  137. gs_texture_destroy(voltex);
  138. }