2013-10-05 09:25:12 -07:00
|
|
|
/******************************************************************************
|
|
|
|
Copyright (C) 2013 by Hugh Bailey <obs.jim@gmail.com>
|
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
2013-12-02 21:24:38 -08:00
|
|
|
the Free Software Foundation, either version 2 of the License, or
|
2013-10-05 09:25:12 -07:00
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
******************************************************************************/
|
|
|
|
|
|
|
|
#include "gl-subsystem.h"
|
|
|
|
|
2013-10-16 23:31:18 -07:00
|
|
|
static inline bool upload_texture_cube(struct gs_texture_cube *tex,
|
2014-06-27 21:29:06 -07:00
|
|
|
const uint8_t **data)
|
2013-10-05 09:25:12 -07:00
|
|
|
{
|
2013-10-05 09:40:43 -07:00
|
|
|
uint32_t row_size = tex->size * gs_get_format_bpp(tex->base.format);
|
2013-10-05 09:25:12 -07:00
|
|
|
uint32_t tex_size = tex->size * row_size / 8;
|
2013-10-05 09:40:43 -07:00
|
|
|
uint32_t num_levels = tex->base.levels;
|
|
|
|
bool compressed = gs_is_compressed_format(tex->base.format);
|
2013-10-10 17:39:56 -07:00
|
|
|
GLenum gl_type = get_gl_format_type(tex->base.format);
|
2013-10-05 09:25:12 -07:00
|
|
|
bool success = true;
|
|
|
|
uint32_t i;
|
|
|
|
|
2013-10-05 09:40:43 -07:00
|
|
|
if (!num_levels)
|
2014-08-07 23:42:07 -07:00
|
|
|
num_levels = gs_get_total_levels(tex->size, tex->size);
|
2013-10-05 09:40:43 -07:00
|
|
|
|
2013-10-05 09:25:12 -07:00
|
|
|
for (i = 0; i < 6; i++) {
|
2013-10-10 17:39:56 -07:00
|
|
|
GLenum target = GL_TEXTURE_CUBE_MAP_POSITIVE_X + i;
|
2013-10-05 09:25:12 -07:00
|
|
|
|
2013-10-10 17:39:56 -07:00
|
|
|
if (!gl_bind_texture(target, tex->base.texture))
|
2013-10-05 09:25:12 -07:00
|
|
|
success = false;
|
|
|
|
|
2013-10-10 17:39:56 -07:00
|
|
|
if (!gl_init_face(target, gl_type, num_levels,
|
|
|
|
tex->base.gl_format,
|
2013-10-05 09:25:12 -07:00
|
|
|
tex->base.gl_internal_format,
|
|
|
|
compressed, tex->size, tex->size,
|
|
|
|
tex_size, &data))
|
|
|
|
success = false;
|
|
|
|
|
2013-10-10 17:39:56 -07:00
|
|
|
if (!gl_bind_texture(target, 0))
|
2013-10-05 09:25:12 -07:00
|
|
|
success = false;
|
|
|
|
|
2013-10-10 17:39:56 -07:00
|
|
|
if (data)
|
|
|
|
data++;
|
2013-10-05 09:25:12 -07:00
|
|
|
}
|
|
|
|
|
2013-10-16 23:31:18 -07:00
|
|
|
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAX_LEVEL, num_levels);
|
|
|
|
if (!gl_success("glTexParameteri"))
|
|
|
|
success = false;
|
|
|
|
|
2013-10-05 09:25:12 -07:00
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
2014-09-25 17:44:05 -07:00
|
|
|
gs_texture_t *device_cubetexture_create(gs_device_t *device, uint32_t size,
|
2013-10-05 09:25:12 -07:00
|
|
|
enum gs_color_format color_format, uint32_t levels,
|
2014-06-27 21:29:06 -07:00
|
|
|
const uint8_t **data, uint32_t flags)
|
2013-10-05 09:25:12 -07:00
|
|
|
{
|
2014-02-09 11:34:07 -08:00
|
|
|
struct gs_texture_cube *tex = bzalloc(sizeof(struct gs_texture_cube));
|
2013-10-05 09:25:12 -07:00
|
|
|
tex->base.device = device;
|
2013-10-12 12:35:38 -07:00
|
|
|
tex->base.type = GS_TEXTURE_CUBE;
|
2013-10-05 09:25:12 -07:00
|
|
|
tex->base.format = color_format;
|
2013-10-14 12:37:52 -07:00
|
|
|
tex->base.levels = levels;
|
2013-10-05 09:25:12 -07:00
|
|
|
tex->base.gl_format = convert_gs_format(color_format);
|
|
|
|
tex->base.gl_internal_format = convert_gs_internal_format(color_format);
|
2013-10-12 12:35:38 -07:00
|
|
|
tex->base.gl_target = GL_TEXTURE_CUBE_MAP;
|
2014-08-07 23:42:07 -07:00
|
|
|
tex->base.is_render_target = (flags & GS_RENDER_TARGET) != 0;
|
|
|
|
tex->base.gen_mipmaps = (flags & GS_BUILD_MIPMAPS) != 0;
|
2013-10-05 09:25:12 -07:00
|
|
|
tex->size = size;
|
|
|
|
|
|
|
|
if (!gl_gen_textures(1, &tex->base.texture))
|
|
|
|
goto fail;
|
2013-10-10 17:39:56 -07:00
|
|
|
if (!upload_texture_cube(tex, data))
|
2013-10-05 09:25:12 -07:00
|
|
|
goto fail;
|
|
|
|
|
2014-09-25 17:44:05 -07:00
|
|
|
return (gs_texture_t*)tex;
|
2013-10-05 09:25:12 -07:00
|
|
|
|
|
|
|
fail:
|
2014-09-25 17:44:05 -07:00
|
|
|
gs_cubetexture_destroy((gs_texture_t*)tex);
|
2014-08-07 23:42:07 -07:00
|
|
|
blog(LOG_ERROR, "device_cubetexture_create (GL) failed");
|
2013-10-05 09:25:12 -07:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2014-09-25 17:44:05 -07:00
|
|
|
void gs_cubetexture_destroy(gs_texture_t *tex)
|
2013-10-05 09:25:12 -07:00
|
|
|
{
|
|
|
|
if (!tex)
|
|
|
|
return;
|
|
|
|
|
2013-10-05 09:48:35 -07:00
|
|
|
if (tex->texture) {
|
|
|
|
glDeleteTextures(1, &tex->texture);
|
|
|
|
gl_success("glDeleteTextures");
|
|
|
|
}
|
|
|
|
|
2013-10-05 09:25:12 -07:00
|
|
|
bfree(tex);
|
|
|
|
}
|
|
|
|
|
2014-09-25 17:44:05 -07:00
|
|
|
static inline bool is_texture_cube(gs_texture_t *tex, const char *func)
|
2013-10-05 09:25:12 -07:00
|
|
|
{
|
|
|
|
bool is_texcube = tex->type == GS_TEXTURE_CUBE;
|
|
|
|
if (!is_texcube)
|
|
|
|
blog(LOG_ERROR, "%s (GL) failed: Not a cubemap texture", func);
|
|
|
|
return is_texcube;
|
|
|
|
}
|
|
|
|
|
2014-09-25 17:44:05 -07:00
|
|
|
uint32_t gs_cubetexture_get_size(gs_texture_t *cubetex)
|
2013-10-05 09:25:12 -07:00
|
|
|
{
|
|
|
|
struct gs_texture_cube *cube = (struct gs_texture_cube*)cubetex;
|
2014-08-07 23:42:07 -07:00
|
|
|
if (!is_texture_cube(cubetex, "gs_cubetexture_get_size"))
|
2013-10-05 09:25:12 -07:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
return cube->size;
|
|
|
|
}
|
|
|
|
|
2014-09-25 17:44:05 -07:00
|
|
|
enum gs_color_format gs_cubetexture_get_color_format(gs_texture_t *cubetex)
|
2013-10-05 09:25:12 -07:00
|
|
|
{
|
|
|
|
return cubetex->format;
|
|
|
|
}
|