gl-texture2d.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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 upload_texture_2d(struct gs_texture_2d *tex, const uint8_t **data)
  16. {
  17. uint32_t row_size = tex->width * gs_get_format_bpp(tex->base.format);
  18. uint32_t tex_size = tex->height * row_size / 8;
  19. uint32_t num_levels = tex->base.levels;
  20. bool compressed = gs_is_compressed_format(tex->base.format);
  21. bool success;
  22. if (!num_levels)
  23. num_levels = gs_get_total_levels(tex->width, tex->height, 1);
  24. if (!gl_bind_texture(GL_TEXTURE_2D, tex->base.texture))
  25. return false;
  26. success = gl_init_face(GL_TEXTURE_2D, tex->base.gl_type, num_levels, tex->base.gl_format,
  27. tex->base.gl_internal_format, compressed, tex->width, tex->height, tex_size, &data);
  28. if (!gl_tex_param_i(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, num_levels - 1))
  29. success = false;
  30. if (!gl_bind_texture(GL_TEXTURE_2D, 0))
  31. success = false;
  32. return success;
  33. }
  34. static bool create_pixel_unpack_buffer(struct gs_texture_2d *tex)
  35. {
  36. GLsizeiptr size;
  37. bool success = true;
  38. if (!gl_gen_buffers(1, &tex->unpack_buffer))
  39. return false;
  40. if (!gl_bind_buffer(GL_PIXEL_UNPACK_BUFFER, tex->unpack_buffer))
  41. return false;
  42. size = tex->width * gs_get_format_bpp(tex->base.format);
  43. if (!gs_is_compressed_format(tex->base.format)) {
  44. size /= 8;
  45. size = (size + 3) & 0xFFFFFFFC;
  46. size *= tex->height;
  47. } else {
  48. size *= tex->height;
  49. size /= 8;
  50. }
  51. glBufferData(GL_PIXEL_UNPACK_BUFFER, size, 0, GL_DYNAMIC_DRAW);
  52. if (!gl_success("glBufferData"))
  53. success = false;
  54. if (!gl_bind_buffer(GL_PIXEL_UNPACK_BUFFER, 0))
  55. success = false;
  56. return success;
  57. }
  58. gs_texture_t *device_texture_create(gs_device_t *device, uint32_t width, uint32_t height,
  59. enum gs_color_format color_format, uint32_t levels, const uint8_t **data,
  60. uint32_t flags)
  61. {
  62. struct gs_texture_2d *tex = bzalloc(sizeof(struct gs_texture_2d));
  63. tex->base.device = device;
  64. tex->base.type = GS_TEXTURE_2D;
  65. tex->base.format = color_format;
  66. tex->base.levels = levels;
  67. tex->base.gl_format = convert_gs_format(color_format);
  68. tex->base.gl_internal_format = convert_gs_internal_format(color_format);
  69. tex->base.gl_type = get_gl_format_type(color_format);
  70. tex->base.gl_target = GL_TEXTURE_2D;
  71. tex->base.is_dynamic = (flags & GS_DYNAMIC) != 0;
  72. tex->base.is_render_target = (flags & GS_RENDER_TARGET) != 0;
  73. tex->base.is_dummy = (flags & GS_GL_DUMMYTEX) != 0;
  74. tex->base.gen_mipmaps = (flags & GS_BUILD_MIPMAPS) != 0;
  75. tex->width = width;
  76. tex->height = height;
  77. if (!gl_gen_textures(1, &tex->base.texture))
  78. goto fail;
  79. if (!tex->base.is_dummy) {
  80. if (tex->base.is_dynamic && !create_pixel_unpack_buffer(tex))
  81. goto fail;
  82. if (!upload_texture_2d(tex, data))
  83. goto fail;
  84. } else {
  85. if (!gl_bind_texture(GL_TEXTURE_2D, tex->base.texture))
  86. goto fail;
  87. bool did_init = gl_tex_param_i(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 0);
  88. bool did_unbind = gl_bind_texture(GL_TEXTURE_2D, 0);
  89. if (!did_init || !did_unbind)
  90. goto fail;
  91. }
  92. return (gs_texture_t *)tex;
  93. fail:
  94. gs_texture_destroy((gs_texture_t *)tex);
  95. blog(LOG_ERROR, "device_texture_create (GL) failed");
  96. return NULL;
  97. }
  98. static inline bool is_texture_2d(const gs_texture_t *tex, const char *func)
  99. {
  100. bool is_tex2d = tex->type == GS_TEXTURE_2D;
  101. if (!is_tex2d)
  102. blog(LOG_ERROR, "%s (GL) failed: Not a 2D texture", func);
  103. return is_tex2d;
  104. }
  105. void gs_texture_destroy(gs_texture_t *tex)
  106. {
  107. if (!tex)
  108. return;
  109. if (tex->cur_sampler)
  110. gs_samplerstate_destroy(tex->cur_sampler);
  111. if (!tex->is_dummy && tex->is_dynamic) {
  112. if (tex->type == GS_TEXTURE_2D) {
  113. struct gs_texture_2d *tex2d = (struct gs_texture_2d *)tex;
  114. if (tex2d->unpack_buffer)
  115. gl_delete_buffers(1, &tex2d->unpack_buffer);
  116. } else if (tex->type == GS_TEXTURE_3D) {
  117. struct gs_texture_3d *tex3d = (struct gs_texture_3d *)tex;
  118. if (tex3d->unpack_buffer)
  119. gl_delete_buffers(1, &tex3d->unpack_buffer);
  120. }
  121. }
  122. if (tex->texture)
  123. gl_delete_textures(1, &tex->texture);
  124. if (tex->fbo)
  125. fbo_info_destroy(tex->fbo);
  126. bfree(tex);
  127. }
  128. uint32_t gs_texture_get_width(const gs_texture_t *tex)
  129. {
  130. const struct gs_texture_2d *tex2d = (const struct gs_texture_2d *)tex;
  131. if (!is_texture_2d(tex, "gs_texture_get_width"))
  132. return 0;
  133. return tex2d->width;
  134. }
  135. uint32_t gs_texture_get_height(const gs_texture_t *tex)
  136. {
  137. const struct gs_texture_2d *tex2d = (const struct gs_texture_2d *)tex;
  138. if (!is_texture_2d(tex, "gs_texture_get_height"))
  139. return 0;
  140. return tex2d->height;
  141. }
  142. enum gs_color_format gs_texture_get_color_format(const gs_texture_t *tex)
  143. {
  144. return tex->format;
  145. }
  146. bool gs_texture_map(gs_texture_t *tex, uint8_t **ptr, uint32_t *linesize)
  147. {
  148. struct gs_texture_2d *tex2d = (struct gs_texture_2d *)tex;
  149. if (!is_texture_2d(tex, "gs_texture_map"))
  150. goto fail;
  151. if (!tex2d->base.is_dynamic) {
  152. blog(LOG_ERROR, "Texture is not dynamic");
  153. goto fail;
  154. }
  155. if (!gl_bind_buffer(GL_PIXEL_UNPACK_BUFFER, tex2d->unpack_buffer))
  156. goto fail;
  157. *ptr = glMapBuffer(GL_PIXEL_UNPACK_BUFFER, GL_WRITE_ONLY);
  158. if (!gl_success("glMapBuffer"))
  159. goto fail;
  160. gl_bind_buffer(GL_PIXEL_UNPACK_BUFFER, 0);
  161. *linesize = tex2d->width * gs_get_format_bpp(tex->format) / 8;
  162. *linesize = (*linesize + 3) & 0xFFFFFFFC;
  163. return true;
  164. fail:
  165. blog(LOG_ERROR, "gs_texture_map (GL) failed");
  166. return false;
  167. }
  168. void gs_texture_unmap(gs_texture_t *tex)
  169. {
  170. struct gs_texture_2d *tex2d = (struct gs_texture_2d *)tex;
  171. if (!is_texture_2d(tex, "gs_texture_unmap"))
  172. goto failed;
  173. if (!gl_bind_buffer(GL_PIXEL_UNPACK_BUFFER, tex2d->unpack_buffer))
  174. goto failed;
  175. glUnmapBuffer(GL_PIXEL_UNPACK_BUFFER);
  176. if (!gl_success("glUnmapBuffer"))
  177. goto failed;
  178. if (!gl_bind_texture(GL_TEXTURE_2D, tex2d->base.texture))
  179. goto failed;
  180. glTexImage2D(GL_TEXTURE_2D, 0, tex->gl_internal_format, tex2d->width, tex2d->height, 0, tex->gl_format,
  181. tex->gl_type, 0);
  182. if (!gl_success("glTexImage2D"))
  183. goto failed;
  184. gl_bind_buffer(GL_PIXEL_UNPACK_BUFFER, 0);
  185. gl_bind_texture(GL_TEXTURE_2D, 0);
  186. return;
  187. failed:
  188. gl_bind_buffer(GL_PIXEL_UNPACK_BUFFER, 0);
  189. gl_bind_texture(GL_TEXTURE_2D, 0);
  190. blog(LOG_ERROR, "gs_texture_unmap (GL) failed");
  191. }
  192. bool gs_texture_is_rect(const gs_texture_t *tex)
  193. {
  194. if (tex->type == GS_TEXTURE_3D)
  195. return false;
  196. if (!is_texture_2d(tex, "gs_texture_is_rect")) {
  197. blog(LOG_ERROR, "gs_texture_is_rect (GL) failed");
  198. return false;
  199. }
  200. const struct gs_texture_2d *tex2d = (const struct gs_texture_2d *)tex;
  201. return tex2d->base.gl_target == GL_TEXTURE_RECTANGLE;
  202. }
  203. void *gs_texture_get_obj(gs_texture_t *tex)
  204. {
  205. struct gs_texture_2d *tex2d = (struct gs_texture_2d *)tex;
  206. if (!is_texture_2d(tex, "gs_texture_get_obj")) {
  207. blog(LOG_ERROR, "gs_texture_get_obj (GL) failed");
  208. return NULL;
  209. }
  210. return &tex2d->base.texture;
  211. }