gl-texture2d.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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 3 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, 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. GLenum gl_type = get_gl_format_type(tex->base.format);
  22. bool success;
  23. if (!num_levels)
  24. num_levels = gs_num_total_levels(tex->width, tex->height);
  25. if (!gl_bind_texture(GL_TEXTURE_2D, tex->base.texture))
  26. return false;
  27. success = gl_init_face(GL_TEXTURE_2D, gl_type, num_levels,
  28. tex->base.gl_format, tex->base.gl_internal_format,
  29. compressed, tex->width, tex->height, tex_size, &data);
  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 * tex->height * gs_get_format_bpp(tex->base.format);
  43. size /= 8;
  44. glBufferData(GL_PIXEL_UNPACK_BUFFER, size, 0, GL_DYNAMIC_DRAW);
  45. if (!gl_success("glBufferData"))
  46. success = false;
  47. if (!gl_bind_buffer(GL_PIXEL_UNPACK_BUFFER, 0))
  48. success = false;
  49. return success;
  50. }
  51. texture_t device_create_texture(device_t device, uint32_t width,
  52. uint32_t height, enum gs_color_format color_format,
  53. uint32_t levels, void **data, uint32_t flags)
  54. {
  55. struct gs_texture_2d *tex = bmalloc(sizeof(struct gs_texture_2d));
  56. memset(tex, 0, sizeof(struct gs_texture_2d));
  57. tex->base.device = device;
  58. tex->base.type = GS_TEXTURE_2D;
  59. tex->base.format = color_format;
  60. tex->base.gl_format = convert_gs_format(color_format);
  61. tex->base.gl_internal_format = convert_gs_internal_format(color_format);
  62. tex->base.is_dynamic = flags & GS_DYNAMIC;
  63. tex->base.is_render_target = flags & GS_RENDERTARGET;
  64. tex->base.gen_mipmaps = flags & GS_BUILDMIPMAPS;
  65. tex->width = width;
  66. tex->height = height;
  67. if (!gl_gen_textures(1, &tex->base.texture))
  68. goto fail;
  69. if (tex->base.is_dynamic && !create_pixel_unpack_buffer(tex))
  70. goto fail;
  71. if (!upload_texture_2d(tex, data))
  72. goto fail;
  73. return (texture_t)tex;
  74. fail:
  75. texture_destroy((texture_t)tex);
  76. blog(LOG_ERROR, "device_create_texture (GL) failed");
  77. return NULL;
  78. }
  79. static inline bool is_texture_2d(texture_t tex, const char *func)
  80. {
  81. bool is_tex2d = tex->type == GS_TEXTURE_2D;
  82. if (!is_tex2d)
  83. blog(LOG_ERROR, "%s (GL) failed: Not a 2D texture", func);
  84. return is_tex2d;
  85. }
  86. void texture_destroy(texture_t tex)
  87. {
  88. struct gs_texture_2d *tex2d = (struct gs_texture_2d*)tex;
  89. if (!tex)
  90. return;
  91. if (!is_texture_2d(tex, "texture_destroy"))
  92. return;
  93. if (tex->is_dynamic && tex2d->unpack_buffer) {
  94. glDeleteBuffers(1, &tex2d->unpack_buffer);
  95. gl_success("glDeleteBuffers");
  96. }
  97. if (tex->texture) {
  98. glDeleteTextures(1, &tex->texture);
  99. gl_success("glDeleteTextures");
  100. }
  101. bfree(tex);
  102. }
  103. uint32_t texture_getwidth(texture_t tex)
  104. {
  105. struct gs_texture_2d *tex2d = (struct gs_texture_2d*)tex;
  106. if (!is_texture_2d(tex, "texture_getwidth"))
  107. return 0;
  108. return tex2d->width;
  109. }
  110. uint32_t texture_getheight(texture_t tex)
  111. {
  112. struct gs_texture_2d *tex2d = (struct gs_texture_2d*)tex;
  113. if (!is_texture_2d(tex, "texture_getheight"))
  114. return 0;
  115. return tex2d->height;
  116. }
  117. enum gs_color_format texture_getcolorformat(texture_t tex)
  118. {
  119. return tex->format;
  120. }
  121. bool texture_map(texture_t tex, void **ptr, uint32_t *byte_width)
  122. {
  123. struct gs_texture_2d *tex2d = (struct gs_texture_2d*)tex;
  124. if (!is_texture_2d(tex, "texture_map"))
  125. goto fail;
  126. if (!tex2d->base.is_dynamic) {
  127. blog(LOG_ERROR, "Texture is not dynamic");
  128. goto fail;
  129. }
  130. if (!gl_bind_buffer(GL_PIXEL_UNPACK_BUFFER, tex2d->unpack_buffer))
  131. goto fail;
  132. *ptr = glMapBuffer(GL_PIXEL_UNPACK_BUFFER, GL_WRITE_ONLY);
  133. if (!gl_success("glMapBuffer"))
  134. goto fail;
  135. gl_bind_buffer(GL_PIXEL_UNPACK_BUFFER, 0);
  136. *byte_width = tex2d->width * gs_get_format_bpp(tex->format) / 8;
  137. return true;
  138. fail:
  139. blog(LOG_ERROR, "texture_map (GL) failed");
  140. return false;
  141. }
  142. void texture_unmap(texture_t tex)
  143. {
  144. struct gs_texture_2d *tex2d = (struct gs_texture_2d*)tex;
  145. if (!is_texture_2d(tex, "texture_unmap"))
  146. return;
  147. if (!gl_bind_buffer(GL_PIXEL_UNPACK_BUFFER, tex2d->unpack_buffer))
  148. return;
  149. glUnmapBuffer(GL_PIXEL_UNPACK_BUFFER);
  150. gl_success("glUnmapBuffer");
  151. gl_bind_buffer(GL_PIXEL_UNPACK_BUFFER, 0);
  152. }