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-10 17:39:56 -07:00
|
|
|
bool gl_init_face(GLenum target, GLenum type, uint32_t num_levels,
|
2019-06-22 22:13:45 -07:00
|
|
|
GLenum format, GLint internal_format, bool compressed,
|
|
|
|
uint32_t width, uint32_t height, uint32_t size,
|
|
|
|
const uint8_t ***p_data)
|
2013-10-05 09:25:12 -07:00
|
|
|
{
|
|
|
|
bool success = true;
|
2014-06-27 21:29:06 -07:00
|
|
|
const uint8_t **data = p_data ? *p_data : NULL;
|
2013-10-05 09:25:12 -07:00
|
|
|
uint32_t i;
|
|
|
|
|
|
|
|
for (i = 0; i < num_levels; i++) {
|
|
|
|
if (compressed) {
|
2013-10-10 17:39:56 -07:00
|
|
|
glCompressedTexImage2D(target, i, internal_format,
|
2019-06-22 22:13:45 -07:00
|
|
|
width, height, 0, size,
|
|
|
|
data ? *data : NULL);
|
2013-10-05 09:25:12 -07:00
|
|
|
if (!gl_success("glCompressedTexImage2D"))
|
|
|
|
success = false;
|
|
|
|
|
|
|
|
} else {
|
2013-10-10 17:39:56 -07:00
|
|
|
glTexImage2D(target, i, internal_format, width, height,
|
2019-06-22 22:13:45 -07:00
|
|
|
0, format, type, data ? *data : NULL);
|
2013-10-05 09:25:12 -07:00
|
|
|
if (!gl_success("glTexImage2D"))
|
|
|
|
success = false;
|
|
|
|
}
|
|
|
|
|
2013-10-10 17:39:56 -07:00
|
|
|
if (data)
|
|
|
|
data++;
|
2014-04-12 11:21:47 -07:00
|
|
|
|
2019-06-22 22:13:45 -07:00
|
|
|
size /= 4;
|
2019-12-15 11:36:24 -08:00
|
|
|
if (width > 1)
|
|
|
|
width /= 2;
|
|
|
|
if (height > 1)
|
|
|
|
height /= 2;
|
2013-10-05 09:25:12 -07:00
|
|
|
}
|
|
|
|
|
2013-10-14 12:37:52 -07:00
|
|
|
if (data)
|
|
|
|
*p_data = data;
|
2013-10-05 09:25:12 -07:00
|
|
|
return success;
|
|
|
|
}
|
2013-10-10 17:39:56 -07:00
|
|
|
|
2018-08-31 14:07:00 -07:00
|
|
|
static bool gl_copy_fbo(struct gs_texture *dst, uint32_t dst_x, uint32_t dst_y,
|
2019-06-22 22:13:45 -07:00
|
|
|
struct gs_texture *src, uint32_t src_x, uint32_t src_y,
|
|
|
|
uint32_t width, uint32_t height)
|
2014-02-09 10:37:22 -08:00
|
|
|
{
|
2018-08-31 14:07:00 -07:00
|
|
|
struct fbo_info *fbo = get_fbo(src, width, height);
|
2014-02-09 10:37:22 -08:00
|
|
|
GLint last_fbo;
|
|
|
|
bool success = false;
|
|
|
|
|
|
|
|
if (!fbo)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!gl_get_integer_v(GL_READ_FRAMEBUFFER_BINDING, &last_fbo))
|
|
|
|
return false;
|
|
|
|
if (!gl_bind_framebuffer(GL_READ_FRAMEBUFFER, fbo->fbo))
|
|
|
|
return false;
|
2018-08-31 14:07:00 -07:00
|
|
|
if (!gl_bind_texture(dst->gl_target, dst->texture))
|
2014-02-09 10:37:22 -08:00
|
|
|
goto fail;
|
|
|
|
|
|
|
|
glFramebufferTexture2D(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + 0,
|
2019-06-22 22:13:45 -07:00
|
|
|
src->gl_target, src->texture, 0);
|
2014-02-09 10:37:22 -08:00
|
|
|
if (!gl_success("glFrameBufferTexture2D"))
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
glReadBuffer(GL_COLOR_ATTACHMENT0 + 0);
|
|
|
|
if (!gl_success("glReadBuffer"))
|
|
|
|
goto fail;
|
|
|
|
|
2018-08-31 14:07:00 -07:00
|
|
|
glCopyTexSubImage2D(dst->gl_target, 0, dst_x, dst_y, src_x, src_y,
|
2019-06-22 22:13:45 -07:00
|
|
|
width, height);
|
2014-02-09 10:37:22 -08:00
|
|
|
if (!gl_success("glCopyTexSubImage2D"))
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
success = true;
|
|
|
|
|
|
|
|
fail:
|
2018-08-31 14:07:00 -07:00
|
|
|
if (!gl_bind_texture(dst->gl_target, 0))
|
2014-02-09 10:37:22 -08:00
|
|
|
success = false;
|
|
|
|
if (!gl_bind_framebuffer(GL_READ_FRAMEBUFFER, last_fbo))
|
|
|
|
success = false;
|
|
|
|
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
2018-08-31 14:07:00 -07:00
|
|
|
bool gl_copy_texture(struct gs_device *device, struct gs_texture *dst,
|
2019-06-22 22:13:45 -07:00
|
|
|
uint32_t dst_x, uint32_t dst_y, struct gs_texture *src,
|
|
|
|
uint32_t src_x, uint32_t src_y, uint32_t width,
|
|
|
|
uint32_t height)
|
2013-10-10 17:39:56 -07:00
|
|
|
{
|
|
|
|
bool success = false;
|
|
|
|
|
|
|
|
if (device->copy_type == COPY_TYPE_ARB) {
|
2018-08-31 14:07:00 -07:00
|
|
|
glCopyImageSubData(src->texture, src->gl_target, 0, src_x,
|
2019-06-22 22:13:45 -07:00
|
|
|
src_y, 0, dst->texture, dst->gl_target, 0,
|
|
|
|
dst_x, dst_y, 0, width, height, 1);
|
2013-10-10 17:39:56 -07:00
|
|
|
success = gl_success("glCopyImageSubData");
|
|
|
|
|
|
|
|
} else if (device->copy_type == COPY_TYPE_NV) {
|
2018-08-31 14:07:00 -07:00
|
|
|
glCopyImageSubDataNV(src->texture, src->gl_target, 0, src_x,
|
2019-06-22 22:13:45 -07:00
|
|
|
src_y, 0, dst->texture, dst->gl_target, 0,
|
|
|
|
dst_x, dst_y, 0, width, height, 1);
|
2013-10-10 17:39:56 -07:00
|
|
|
success = gl_success("glCopyImageSubDataNV");
|
|
|
|
|
|
|
|
} else if (device->copy_type == COPY_TYPE_FBO_BLIT) {
|
2018-08-31 14:07:00 -07:00
|
|
|
success = gl_copy_fbo(dst, dst_x, dst_y, src, src_x, src_y,
|
2019-06-22 22:13:45 -07:00
|
|
|
width, height);
|
2014-04-12 11:21:47 -07:00
|
|
|
if (!success)
|
2014-02-09 10:37:22 -08:00
|
|
|
blog(LOG_ERROR, "gl_copy_texture failed");
|
2013-10-10 17:39:56 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return success;
|
|
|
|
}
|
2013-10-11 11:41:36 -07:00
|
|
|
|
2013-10-11 13:00:39 -07:00
|
|
|
bool gl_create_buffer(GLenum target, GLuint *buffer, GLsizeiptr size,
|
2019-06-22 22:13:45 -07:00
|
|
|
const GLvoid *data, GLenum usage)
|
2013-10-11 11:41:36 -07:00
|
|
|
{
|
|
|
|
bool success;
|
|
|
|
if (!gl_gen_buffers(1, buffer))
|
|
|
|
return false;
|
2013-10-11 13:00:39 -07:00
|
|
|
if (!gl_bind_buffer(target, *buffer))
|
2013-10-11 11:41:36 -07:00
|
|
|
return false;
|
|
|
|
|
2013-10-11 13:00:39 -07:00
|
|
|
glBufferData(target, size, data, usage);
|
2013-10-11 11:41:36 -07:00
|
|
|
success = gl_success("glBufferData");
|
|
|
|
|
2013-10-11 13:00:39 -07:00
|
|
|
gl_bind_buffer(target, 0);
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
2017-11-27 02:19:02 -08:00
|
|
|
bool update_buffer(GLenum target, GLuint buffer, const void *data, size_t size)
|
2013-10-11 13:00:39 -07:00
|
|
|
{
|
|
|
|
void *ptr;
|
|
|
|
bool success = true;
|
|
|
|
|
|
|
|
if (!gl_bind_buffer(target, buffer))
|
|
|
|
return false;
|
|
|
|
|
2014-02-16 18:28:21 -08:00
|
|
|
/* glMapBufferRange with these flags will actually give far better
|
|
|
|
* performance than a plain glMapBuffer call */
|
|
|
|
ptr = glMapBufferRange(target, 0, size,
|
2019-06-22 22:13:45 -07:00
|
|
|
GL_MAP_WRITE_BIT | GL_MAP_INVALIDATE_BUFFER_BIT);
|
2014-02-16 18:28:21 -08:00
|
|
|
success = gl_success("glMapBufferRange");
|
2013-10-11 13:00:39 -07:00
|
|
|
if (success && ptr) {
|
|
|
|
memcpy(ptr, data, size);
|
|
|
|
glUnmapBuffer(target);
|
|
|
|
}
|
|
|
|
|
|
|
|
gl_bind_buffer(target, 0);
|
2013-10-11 11:41:36 -07:00
|
|
|
return success;
|
|
|
|
}
|