gl-texture2d.c 7.8 KB

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