gl-texture2d.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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 void **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_num_total_levels(tex->width, tex->height);
  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, tex->base.gl_internal_format,
  28. compressed, tex->width, tex->height, tex_size, &data);
  29. if (!gl_tex_param_i(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, num_levels-1))
  30. success = false;
  31. if (!gl_bind_texture(GL_TEXTURE_2D, 0))
  32. success = false;
  33. return success;
  34. }
  35. static bool create_pixel_unpack_buffer(struct gs_texture_2d *tex)
  36. {
  37. GLsizeiptr size;
  38. bool success = true;
  39. if (!gl_gen_buffers(1, &tex->unpack_buffer))
  40. return false;
  41. if (!gl_bind_buffer(GL_PIXEL_UNPACK_BUFFER, tex->unpack_buffer))
  42. return false;
  43. size = tex->width * gs_get_format_bpp(tex->base.format);
  44. if (!gs_is_compressed_format(tex->base.format)) {
  45. size /= 8;
  46. size = (size+3) & 0xFFFFFFFC;
  47. size *= tex->height;
  48. } else {
  49. size *= tex->height;
  50. size /= 8;
  51. }
  52. glBufferData(GL_PIXEL_UNPACK_BUFFER, size, 0, GL_DYNAMIC_DRAW);
  53. if (!gl_success("glBufferData"))
  54. success = false;
  55. if (!gl_bind_buffer(GL_PIXEL_UNPACK_BUFFER, 0))
  56. success = false;
  57. return success;
  58. }
  59. texture_t device_create_texture(device_t device, uint32_t width,
  60. uint32_t height, enum gs_color_format color_format,
  61. uint32_t levels, const void **data, uint32_t flags)
  62. {
  63. struct gs_texture_2d *tex = bmalloc(sizeof(struct gs_texture_2d));
  64. memset(tex, 0, sizeof(struct gs_texture_2d));
  65. tex->base.device = device;
  66. tex->base.type = GS_TEXTURE_2D;
  67. tex->base.format = color_format;
  68. tex->base.levels = levels;
  69. tex->base.gl_format = convert_gs_format(color_format);
  70. tex->base.gl_internal_format = convert_gs_internal_format(color_format);
  71. tex->base.gl_type = get_gl_format_type(color_format);
  72. tex->base.gl_target = GL_TEXTURE_2D;
  73. tex->base.is_dynamic = (flags & GS_DYNAMIC) != 0;
  74. tex->base.is_render_target = (flags & GS_RENDERTARGET) != 0;
  75. tex->base.gen_mipmaps = (flags & GS_BUILDMIPMAPS) != 0;
  76. tex->width = width;
  77. tex->height = height;
  78. if (!gl_gen_textures(1, &tex->base.texture))
  79. goto fail;
  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. return (texture_t)tex;
  85. fail:
  86. texture_destroy((texture_t)tex);
  87. blog(LOG_ERROR, "device_create_texture (GL) failed");
  88. return NULL;
  89. }
  90. static inline bool is_texture_2d(texture_t tex, const char *func)
  91. {
  92. bool is_tex2d = tex->type == GS_TEXTURE_2D;
  93. if (!is_tex2d)
  94. blog(LOG_ERROR, "%s (GL) failed: Not a 2D texture", func);
  95. return is_tex2d;
  96. }
  97. void texture_destroy(texture_t tex)
  98. {
  99. struct gs_texture_2d *tex2d = (struct gs_texture_2d*)tex;
  100. if (!tex)
  101. return;
  102. if (!is_texture_2d(tex, "texture_destroy"))
  103. return;
  104. if (tex->cur_sampler)
  105. samplerstate_destroy(tex->cur_sampler);
  106. if (tex->is_dynamic && tex2d->unpack_buffer)
  107. gl_delete_buffers(1, &tex2d->unpack_buffer);
  108. if (tex->texture)
  109. gl_delete_textures(1, &tex->texture);
  110. bfree(tex);
  111. }
  112. uint32_t texture_getwidth(texture_t tex)
  113. {
  114. struct gs_texture_2d *tex2d = (struct gs_texture_2d*)tex;
  115. if (!is_texture_2d(tex, "texture_getwidth"))
  116. return 0;
  117. return tex2d->width;
  118. }
  119. uint32_t texture_getheight(texture_t tex)
  120. {
  121. struct gs_texture_2d *tex2d = (struct gs_texture_2d*)tex;
  122. if (!is_texture_2d(tex, "texture_getheight"))
  123. return 0;
  124. return tex2d->height;
  125. }
  126. enum gs_color_format texture_getcolorformat(texture_t tex)
  127. {
  128. return tex->format;
  129. }
  130. bool texture_map(texture_t tex, void **ptr, uint32_t *row_bytes)
  131. {
  132. struct gs_texture_2d *tex2d = (struct gs_texture_2d*)tex;
  133. if (!is_texture_2d(tex, "texture_map"))
  134. goto fail;
  135. if (!tex2d->base.is_dynamic) {
  136. blog(LOG_ERROR, "Texture is not dynamic");
  137. goto fail;
  138. }
  139. if (!gl_bind_buffer(GL_PIXEL_UNPACK_BUFFER, tex2d->unpack_buffer))
  140. goto fail;
  141. *ptr = glMapBuffer(GL_PIXEL_UNPACK_BUFFER, GL_WRITE_ONLY);
  142. if (!gl_success("glMapBuffer"))
  143. goto fail;
  144. gl_bind_buffer(GL_PIXEL_UNPACK_BUFFER, 0);
  145. *row_bytes = tex2d->width * gs_get_format_bpp(tex->format) / 8;
  146. *row_bytes = (*row_bytes + 3) & 0xFFFFFFFC;
  147. return true;
  148. fail:
  149. blog(LOG_ERROR, "texture_map (GL) failed");
  150. return false;
  151. }
  152. void texture_unmap(texture_t tex)
  153. {
  154. struct gs_texture_2d *tex2d = (struct gs_texture_2d*)tex;
  155. if (!is_texture_2d(tex, "texture_unmap"))
  156. goto failed;
  157. if (!gl_bind_buffer(GL_PIXEL_UNPACK_BUFFER, tex2d->unpack_buffer))
  158. goto failed;
  159. glUnmapBuffer(GL_PIXEL_UNPACK_BUFFER);
  160. if (!gl_success("glUnmapBuffer"))
  161. goto failed;
  162. if (!gl_bind_texture(GL_TEXTURE_2D, tex2d->base.texture))
  163. goto failed;
  164. glTexImage2D(GL_TEXTURE_2D, 0, tex->gl_internal_format,
  165. tex2d->width, tex2d->height, 0,
  166. tex->gl_format, tex->gl_type, 0);
  167. if (!gl_success("glTexImage2D"))
  168. goto failed;
  169. gl_bind_buffer(GL_PIXEL_UNPACK_BUFFER, 0);
  170. gl_bind_texture(GL_TEXTURE_2D, 0);
  171. return;
  172. failed:
  173. gl_bind_buffer(GL_PIXEL_UNPACK_BUFFER, 0);
  174. gl_bind_texture(GL_TEXTURE_2D, 0);
  175. blog(LOG_ERROR, "texture_unmap (GL) failed");
  176. }
  177. bool texture_isrect(texture_t tex)
  178. {
  179. struct gs_texture_2d *tex2d = (struct gs_texture_2d*)tex;
  180. if (!is_texture_2d(tex, "texture_unmap")) {
  181. blog(LOG_ERROR, "texture_isrect (GL) failed");
  182. return false;
  183. }
  184. return tex2d->base.gl_target == GL_TEXTURE_RECTANGLE;
  185. }