gl-texture3d.c 5.3 KB

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