gl-texturecube.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 inline bool upload_texture_cube(struct gs_texture_cube *tex,
  16. const uint8_t **data)
  17. {
  18. uint32_t row_size = tex->size * gs_get_format_bpp(tex->base.format);
  19. uint32_t tex_size = tex->size * row_size / 8;
  20. uint32_t num_levels = tex->base.levels;
  21. bool compressed = gs_is_compressed_format(tex->base.format);
  22. GLenum gl_type = get_gl_format_type(tex->base.format);
  23. bool success = true;
  24. uint32_t i;
  25. if (!num_levels)
  26. num_levels = gs_get_total_levels(tex->size, tex->size, 1);
  27. for (i = 0; i < 6; i++) {
  28. GLenum target = GL_TEXTURE_CUBE_MAP_POSITIVE_X + i;
  29. if (!gl_bind_texture(target, tex->base.texture))
  30. success = false;
  31. if (!gl_init_face(target, gl_type, num_levels,
  32. tex->base.gl_format,
  33. tex->base.gl_internal_format, compressed,
  34. tex->size, tex->size, tex_size, &data))
  35. success = false;
  36. if (!gl_bind_texture(target, 0))
  37. success = false;
  38. if (data)
  39. data++;
  40. }
  41. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAX_LEVEL, num_levels);
  42. if (!gl_success("glTexParameteri"))
  43. success = false;
  44. return success;
  45. }
  46. gs_texture_t *device_cubetexture_create(gs_device_t *device, uint32_t size,
  47. enum gs_color_format color_format,
  48. uint32_t levels, const uint8_t **data,
  49. uint32_t flags)
  50. {
  51. struct gs_texture_cube *tex = bzalloc(sizeof(struct gs_texture_cube));
  52. tex->base.device = device;
  53. tex->base.type = GS_TEXTURE_CUBE;
  54. tex->base.format = color_format;
  55. tex->base.levels = levels;
  56. tex->base.gl_format = convert_gs_format(color_format);
  57. tex->base.gl_internal_format = convert_gs_internal_format(color_format);
  58. tex->base.gl_target = GL_TEXTURE_CUBE_MAP;
  59. tex->base.is_render_target = (flags & GS_RENDER_TARGET) != 0;
  60. tex->base.gen_mipmaps = (flags & GS_BUILD_MIPMAPS) != 0;
  61. tex->size = size;
  62. if (!gl_gen_textures(1, &tex->base.texture))
  63. goto fail;
  64. if (!upload_texture_cube(tex, data))
  65. goto fail;
  66. return (gs_texture_t *)tex;
  67. fail:
  68. gs_cubetexture_destroy((gs_texture_t *)tex);
  69. blog(LOG_ERROR, "device_cubetexture_create (GL) failed");
  70. return NULL;
  71. }
  72. void gs_cubetexture_destroy(gs_texture_t *tex)
  73. {
  74. if (!tex)
  75. return;
  76. if (tex->texture)
  77. gl_delete_textures(1, &tex->texture);
  78. if (tex->fbo)
  79. fbo_info_destroy(tex->fbo);
  80. bfree(tex);
  81. }
  82. static inline bool is_texture_cube(const gs_texture_t *tex, const char *func)
  83. {
  84. bool is_texcube = tex->type == GS_TEXTURE_CUBE;
  85. if (!is_texcube)
  86. blog(LOG_ERROR, "%s (GL) failed: Not a cubemap texture", func);
  87. return is_texcube;
  88. }
  89. uint32_t gs_cubetexture_get_size(const gs_texture_t *cubetex)
  90. {
  91. const struct gs_texture_cube *cube =
  92. (const struct gs_texture_cube *)cubetex;
  93. if (!is_texture_cube(cubetex, "gs_cubetexture_get_size"))
  94. return 0;
  95. return cube->size;
  96. }
  97. enum gs_color_format
  98. gs_cubetexture_get_color_format(const gs_texture_t *cubetex)
  99. {
  100. return cubetex->format;
  101. }