2013-10-04 12:13:59 -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-04 12:13:59 -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/>.
|
|
|
|
******************************************************************************/
|
|
|
|
|
2013-10-14 04:21:15 -07:00
|
|
|
#pragma once
|
2013-10-04 08:55:33 -07:00
|
|
|
|
2019-08-12 08:05:38 -07:00
|
|
|
static const char *gl_error_to_str(GLenum errorcode)
|
2019-07-08 19:22:55 -07:00
|
|
|
{
|
2019-08-12 08:05:38 -07:00
|
|
|
static const struct {
|
|
|
|
GLenum error;
|
|
|
|
const char *str;
|
|
|
|
} err_to_str[] = {
|
2019-07-08 19:22:55 -07:00
|
|
|
{
|
|
|
|
GL_INVALID_ENUM,
|
|
|
|
"GL_INVALID_ENUM",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
GL_INVALID_VALUE,
|
|
|
|
"GL_INVALID_VALUE",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
GL_INVALID_OPERATION,
|
|
|
|
"GL_INVALID_OPERATION",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
GL_INVALID_FRAMEBUFFER_OPERATION,
|
|
|
|
"GL_INVALID_FRAMEBUFFER_OPERATION",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
GL_OUT_OF_MEMORY,
|
|
|
|
"GL_OUT_OF_MEMORY",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
GL_STACK_UNDERFLOW,
|
|
|
|
"GL_STACK_UNDERFLOW",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
GL_STACK_OVERFLOW,
|
|
|
|
"GL_STACK_OVERFLOW",
|
|
|
|
},
|
|
|
|
};
|
2019-08-12 08:05:38 -07:00
|
|
|
for (size_t i = 0; i < sizeof(err_to_str) / sizeof(*err_to_str); i++) {
|
|
|
|
if (err_to_str[i].error == errorcode)
|
|
|
|
return err_to_str[i].str;
|
2019-07-08 19:22:55 -07:00
|
|
|
}
|
2019-08-12 08:05:38 -07:00
|
|
|
return "Unknown";
|
2019-07-08 19:22:55 -07:00
|
|
|
}
|
|
|
|
|
2013-10-04 08:55:33 -07:00
|
|
|
/*
|
|
|
|
* Okay, so GL error handling is.. unclean to work with. I don't want
|
|
|
|
* to have to keep typing out the same stuff over and over again do I'll just
|
|
|
|
* make a bunch of helper functions to make it a bit easier to handle errors
|
|
|
|
*/
|
|
|
|
|
2013-10-05 00:34:43 -07:00
|
|
|
static inline bool gl_success(const char *funcname)
|
2013-10-04 08:55:33 -07:00
|
|
|
{
|
|
|
|
GLenum errorcode = glGetError();
|
|
|
|
if (errorcode != GL_NO_ERROR) {
|
2019-07-15 09:13:14 -07:00
|
|
|
int attempts = 8;
|
2019-06-24 21:13:04 -07:00
|
|
|
do {
|
2019-07-08 19:22:55 -07:00
|
|
|
blog(LOG_ERROR,
|
|
|
|
"%s failed, glGetError returned %s(0x%X)",
|
|
|
|
funcname, gl_error_to_str(errorcode), errorcode);
|
2019-06-24 21:13:04 -07:00
|
|
|
errorcode = glGetError();
|
2019-07-15 09:13:14 -07:00
|
|
|
|
|
|
|
--attempts;
|
|
|
|
if (attempts == 0) {
|
2019-08-17 11:02:03 -07:00
|
|
|
blog(LOG_ERROR,
|
|
|
|
"Too many GL errors, moving on");
|
2019-07-15 09:13:14 -07:00
|
|
|
break;
|
|
|
|
}
|
2019-06-24 21:13:04 -07:00
|
|
|
} while (errorcode != GL_NO_ERROR);
|
2013-10-04 08:55:33 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool gl_gen_textures(GLsizei num_texture, GLuint *textures)
|
|
|
|
{
|
|
|
|
glGenTextures(num_texture, textures);
|
2013-10-05 00:34:43 -07:00
|
|
|
return gl_success("glGenTextures");
|
2013-10-04 08:55:33 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool gl_bind_texture(GLenum target, GLuint texture)
|
|
|
|
{
|
|
|
|
glBindTexture(target, texture);
|
2013-10-05 00:34:43 -07:00
|
|
|
return gl_success("glBindTexture");
|
2013-10-04 08:55:33 -07:00
|
|
|
}
|
|
|
|
|
2013-10-11 11:41:36 -07:00
|
|
|
static inline void gl_delete_textures(GLsizei num_buffers, GLuint *buffers)
|
|
|
|
{
|
|
|
|
glDeleteTextures(num_buffers, buffers);
|
|
|
|
gl_success("glDeleteTextures");
|
|
|
|
}
|
|
|
|
|
2013-10-05 09:25:12 -07:00
|
|
|
static inline bool gl_gen_buffers(GLsizei num_buffers, GLuint *buffers)
|
|
|
|
{
|
|
|
|
glGenBuffers(num_buffers, buffers);
|
|
|
|
return gl_success("glGenBuffers");
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool gl_bind_buffer(GLenum target, GLuint buffer)
|
|
|
|
{
|
|
|
|
glBindBuffer(target, buffer);
|
|
|
|
return gl_success("glBindBuffer");
|
|
|
|
}
|
|
|
|
|
2013-10-11 11:41:36 -07:00
|
|
|
static inline void gl_delete_buffers(GLsizei num_buffers, GLuint *buffers)
|
|
|
|
{
|
|
|
|
glDeleteBuffers(num_buffers, buffers);
|
|
|
|
gl_success("glDeleteBuffers");
|
|
|
|
}
|
|
|
|
|
2013-10-16 23:31:18 -07:00
|
|
|
static inline bool gl_gen_vertex_arrays(GLsizei num_arrays, GLuint *arrays)
|
|
|
|
{
|
|
|
|
glGenVertexArrays(num_arrays, arrays);
|
|
|
|
return gl_success("glGenVertexArrays");
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool gl_bind_vertex_array(GLuint array)
|
|
|
|
{
|
|
|
|
glBindVertexArray(array);
|
|
|
|
return gl_success("glBindVertexArray");
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void gl_delete_vertex_arrays(GLsizei num_arrays, GLuint *arrays)
|
|
|
|
{
|
|
|
|
glDeleteVertexArrays(num_arrays, arrays);
|
|
|
|
gl_success("glDeleteVertexArrays");
|
|
|
|
}
|
|
|
|
|
2013-10-10 12:37:03 -07:00
|
|
|
static inline bool gl_bind_renderbuffer(GLenum target, GLuint buffer)
|
|
|
|
{
|
|
|
|
glBindRenderbuffer(target, buffer);
|
|
|
|
return gl_success("glBindRendebuffer");
|
|
|
|
}
|
|
|
|
|
libobs-opengl: OpenGL thread-safety on Mac
Attempt to fix threading issues that cause OBS to force-crash when
compiled with latest Xcode. There are two places where the new SDK
introduces a force-crash because operations are not happening on the
main thread: when we modify the context view to switch swap chains, and
when we resize a swap chain.
Instead of using just one context for all rendering, we create an
additional context for each swap chain, set each view once on
initialization, and switch contexts only in present to blit the final
framebuffer. This is an extra copy, but it's pretty hairy to optimize
away, and it's not worth potential regressions just to speed up Mac.
For resizing, we schedule the update code to run on the main thread from
the render thread. Ideally, we wouldn't have to round trip the logic
from main thread to graphics thread and back, but I don't think we want
to hack up the interface for this, especially since OpenGL will give way
to Metal soon enough.
2019-12-25 22:25:38 -08:00
|
|
|
static inline bool gl_gen_framebuffers(GLsizei num_arrays, GLuint *arrays)
|
|
|
|
{
|
|
|
|
glGenFramebuffers(num_arrays, arrays);
|
|
|
|
return gl_success("glGenFramebuffers");
|
|
|
|
}
|
|
|
|
|
2013-10-12 16:28:10 -07:00
|
|
|
static inline bool gl_bind_framebuffer(GLenum target, GLuint buffer)
|
|
|
|
{
|
|
|
|
glBindFramebuffer(target, buffer);
|
|
|
|
return gl_success("glBindFramebuffer");
|
|
|
|
}
|
|
|
|
|
libobs-opengl: OpenGL thread-safety on Mac
Attempt to fix threading issues that cause OBS to force-crash when
compiled with latest Xcode. There are two places where the new SDK
introduces a force-crash because operations are not happening on the
main thread: when we modify the context view to switch swap chains, and
when we resize a swap chain.
Instead of using just one context for all rendering, we create an
additional context for each swap chain, set each view once on
initialization, and switch contexts only in present to blit the final
framebuffer. This is an extra copy, but it's pretty hairy to optimize
away, and it's not worth potential regressions just to speed up Mac.
For resizing, we schedule the update code to run on the main thread from
the render thread. Ideally, we wouldn't have to round trip the logic
from main thread to graphics thread and back, but I don't think we want
to hack up the interface for this, especially since OpenGL will give way
to Metal soon enough.
2019-12-25 22:25:38 -08:00
|
|
|
static inline void gl_delete_framebuffers(GLsizei num_arrays, GLuint *arrays)
|
|
|
|
{
|
|
|
|
glDeleteFramebuffers(num_arrays, arrays);
|
|
|
|
gl_success("glDeleteFramebuffers");
|
|
|
|
}
|
|
|
|
|
2013-10-12 12:35:38 -07:00
|
|
|
static inline bool gl_tex_param_f(GLenum target, GLenum param, GLfloat val)
|
|
|
|
{
|
|
|
|
glTexParameterf(target, param, val);
|
|
|
|
return gl_success("glTexParameterf");
|
|
|
|
}
|
|
|
|
|
2022-01-12 06:34:00 -08:00
|
|
|
static inline bool gl_tex_param_fv(GLenum target, GLenum param, GLfloat *val)
|
|
|
|
{
|
|
|
|
glTexParameterfv(target, param, val);
|
|
|
|
return gl_success("glTexParameterf");
|
|
|
|
}
|
|
|
|
|
2013-10-12 12:35:38 -07:00
|
|
|
static inline bool gl_tex_param_i(GLenum target, GLenum param, GLint val)
|
|
|
|
{
|
|
|
|
glTexParameteri(target, param, val);
|
|
|
|
return gl_success("glTexParameteri");
|
|
|
|
}
|
|
|
|
|
2013-10-12 20:18:05 -07:00
|
|
|
static inline bool gl_active_texture(GLenum texture_id)
|
2013-10-12 12:35:38 -07:00
|
|
|
{
|
2013-10-12 20:18:05 -07:00
|
|
|
glActiveTexture(texture_id);
|
2013-10-12 12:35:38 -07:00
|
|
|
return gl_success("glActiveTexture");
|
|
|
|
}
|
|
|
|
|
2013-10-12 20:18:05 -07:00
|
|
|
static inline bool gl_enable(GLenum capability)
|
|
|
|
{
|
|
|
|
glEnable(capability);
|
|
|
|
return gl_success("glEnable");
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool gl_disable(GLenum capability)
|
|
|
|
{
|
|
|
|
glDisable(capability);
|
|
|
|
return gl_success("glDisable");
|
|
|
|
}
|
|
|
|
|
2013-10-16 23:31:18 -07:00
|
|
|
static inline bool gl_cull_face(GLenum faces)
|
|
|
|
{
|
|
|
|
glCullFace(faces);
|
|
|
|
return gl_success("glCullFace");
|
|
|
|
}
|
|
|
|
|
2014-02-09 10:37:22 -08:00
|
|
|
static inline bool gl_get_integer_v(GLenum pname, GLint *params)
|
|
|
|
{
|
|
|
|
glGetIntegerv(pname, params);
|
|
|
|
return gl_success("glGetIntegerv");
|
|
|
|
}
|
|
|
|
|
2013-10-10 17:39:56 -07:00
|
|
|
extern 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
|
|
|
|
2018-08-31 14:07:00 -07:00
|
|
|
extern 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
|
|
|
|
2013-10-11 13:00:39 -07:00
|
|
|
extern 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
|
|
|
|
2017-11-27 02:19:02 -08:00
|
|
|
extern bool update_buffer(GLenum target, GLuint buffer, const void *data,
|
2019-06-22 22:13:45 -07:00
|
|
|
size_t size);
|