gl-texturecube.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 inline bool upload_texture_cube(struct gs_texture_cube *tex, void **data)
  16. {
  17. uint32_t row_size = tex->size * gs_get_format_bpp(tex->base.format);
  18. uint32_t tex_size = tex->size * row_size / 8;
  19. uint32_t num_levels = tex->base.levels;
  20. bool compressed = gs_is_compressed_format(tex->base.format);
  21. bool success = true;
  22. uint32_t i;
  23. if (!num_levels)
  24. num_levels = gs_num_total_levels(tex->size, tex->size);
  25. for (i = 0; i < 6; i++) {
  26. GLenum type = GL_TEXTURE_CUBE_MAP_POSITIVE_X + i;
  27. if (!gl_bind_texture(type, tex->base.texture))
  28. success = false;
  29. if (!upload_face(type, num_levels, tex->base.gl_format,
  30. tex->base.gl_internal_format,
  31. compressed, tex->size, tex->size,
  32. tex_size, &data))
  33. success = false;
  34. if (!gl_bind_texture(type, 0))
  35. success = false;
  36. data++;
  37. }
  38. return success;
  39. }
  40. texture_t device_create_cubetexture(device_t device, uint32_t size,
  41. enum gs_color_format color_format, uint32_t levels,
  42. void **data, uint32_t flags)
  43. {
  44. struct gs_texture_cube *tex = bmalloc(sizeof(struct gs_texture_cube));
  45. memset(tex, 0, sizeof(struct gs_texture_2d));
  46. tex->base.device = device;
  47. tex->base.type = GS_TEXTURE_2D;
  48. tex->base.format = color_format;
  49. tex->base.gl_format = convert_gs_format(color_format);
  50. tex->base.gl_internal_format = convert_gs_internal_format(color_format);
  51. tex->base.is_render_target = flags & GS_RENDERTARGET;
  52. tex->base.gen_mipmaps = flags & GS_BUILDMIPMAPS;
  53. tex->size = size;
  54. if (!gl_gen_textures(1, &tex->base.texture))
  55. goto fail;
  56. if (data && !upload_texture_cube(tex, data))
  57. goto fail;
  58. return (texture_t)tex;
  59. fail:
  60. cubetexture_destroy((texture_t)tex);
  61. blog(LOG_ERROR, "device_create_cubetexture (GL) failed");
  62. return NULL;
  63. }
  64. void cubetexture_destroy(texture_t tex)
  65. {
  66. if (!tex)
  67. return;
  68. if (tex->texture) {
  69. glDeleteTextures(1, &tex->texture);
  70. gl_success("glDeleteTextures");
  71. }
  72. bfree(tex);
  73. }
  74. static inline bool is_texture_cube(texture_t tex, const char *func)
  75. {
  76. bool is_texcube = tex->type == GS_TEXTURE_CUBE;
  77. if (!is_texcube)
  78. blog(LOG_ERROR, "%s (GL) failed: Not a cubemap texture", func);
  79. return is_texcube;
  80. }
  81. uint32_t cubetexture_getsize(texture_t cubetex)
  82. {
  83. struct gs_texture_cube *cube = (struct gs_texture_cube*)cubetex;
  84. if (!is_texture_cube(cubetex, "cubetexture_getsize"))
  85. return 0;
  86. return cube->size;
  87. }
  88. enum gs_color_format cubetexture_getcolorformat(texture_t cubetex)
  89. {
  90. return cubetex->format;
  91. }