gl-helpers.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /******************************************************************************
  2. Copyright (C) 2023 by Lain 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. #pragma once
  15. static const char *gl_error_to_str(GLenum errorcode)
  16. {
  17. static const struct {
  18. GLenum error;
  19. const char *str;
  20. } err_to_str[] = {
  21. {
  22. GL_INVALID_ENUM,
  23. "GL_INVALID_ENUM",
  24. },
  25. {
  26. GL_INVALID_VALUE,
  27. "GL_INVALID_VALUE",
  28. },
  29. {
  30. GL_INVALID_OPERATION,
  31. "GL_INVALID_OPERATION",
  32. },
  33. {
  34. GL_INVALID_FRAMEBUFFER_OPERATION,
  35. "GL_INVALID_FRAMEBUFFER_OPERATION",
  36. },
  37. {
  38. GL_OUT_OF_MEMORY,
  39. "GL_OUT_OF_MEMORY",
  40. },
  41. {
  42. GL_STACK_UNDERFLOW,
  43. "GL_STACK_UNDERFLOW",
  44. },
  45. {
  46. GL_STACK_OVERFLOW,
  47. "GL_STACK_OVERFLOW",
  48. },
  49. };
  50. for (size_t i = 0; i < sizeof(err_to_str) / sizeof(*err_to_str); i++) {
  51. if (err_to_str[i].error == errorcode)
  52. return err_to_str[i].str;
  53. }
  54. return "Unknown";
  55. }
  56. /*
  57. * Okay, so GL error handling is.. unclean to work with. I don't want
  58. * to have to keep typing out the same stuff over and over again do I'll just
  59. * make a bunch of helper functions to make it a bit easier to handle errors
  60. */
  61. static inline bool gl_success(const char *funcname)
  62. {
  63. GLenum errorcode = glGetError();
  64. if (errorcode != GL_NO_ERROR) {
  65. int attempts = 8;
  66. do {
  67. blog(LOG_ERROR, "%s failed, glGetError returned %s(0x%X)", funcname, gl_error_to_str(errorcode),
  68. errorcode);
  69. errorcode = glGetError();
  70. --attempts;
  71. if (attempts == 0) {
  72. blog(LOG_ERROR, "Too many GL errors, moving on");
  73. break;
  74. }
  75. } while (errorcode != GL_NO_ERROR);
  76. return false;
  77. }
  78. return true;
  79. }
  80. static inline bool gl_gen_textures(GLsizei num_texture, GLuint *textures)
  81. {
  82. glGenTextures(num_texture, textures);
  83. return gl_success("glGenTextures");
  84. }
  85. static inline bool gl_bind_texture(GLenum target, GLuint texture)
  86. {
  87. glBindTexture(target, texture);
  88. return gl_success("glBindTexture");
  89. }
  90. static inline void gl_delete_textures(GLsizei num_buffers, GLuint *buffers)
  91. {
  92. glDeleteTextures(num_buffers, buffers);
  93. gl_success("glDeleteTextures");
  94. }
  95. static inline bool gl_gen_buffers(GLsizei num_buffers, GLuint *buffers)
  96. {
  97. glGenBuffers(num_buffers, buffers);
  98. return gl_success("glGenBuffers");
  99. }
  100. static inline bool gl_bind_buffer(GLenum target, GLuint buffer)
  101. {
  102. glBindBuffer(target, buffer);
  103. return gl_success("glBindBuffer");
  104. }
  105. static inline void gl_delete_buffers(GLsizei num_buffers, GLuint *buffers)
  106. {
  107. glDeleteBuffers(num_buffers, buffers);
  108. gl_success("glDeleteBuffers");
  109. }
  110. static inline bool gl_gen_vertex_arrays(GLsizei num_arrays, GLuint *arrays)
  111. {
  112. glGenVertexArrays(num_arrays, arrays);
  113. return gl_success("glGenVertexArrays");
  114. }
  115. static inline bool gl_bind_vertex_array(GLuint array)
  116. {
  117. glBindVertexArray(array);
  118. return gl_success("glBindVertexArray");
  119. }
  120. static inline void gl_delete_vertex_arrays(GLsizei num_arrays, GLuint *arrays)
  121. {
  122. glDeleteVertexArrays(num_arrays, arrays);
  123. gl_success("glDeleteVertexArrays");
  124. }
  125. static inline bool gl_bind_renderbuffer(GLenum target, GLuint buffer)
  126. {
  127. glBindRenderbuffer(target, buffer);
  128. return gl_success("glBindRendebuffer");
  129. }
  130. static inline bool gl_gen_framebuffers(GLsizei num_arrays, GLuint *arrays)
  131. {
  132. glGenFramebuffers(num_arrays, arrays);
  133. return gl_success("glGenFramebuffers");
  134. }
  135. static inline bool gl_bind_framebuffer(GLenum target, GLuint buffer)
  136. {
  137. glBindFramebuffer(target, buffer);
  138. return gl_success("glBindFramebuffer");
  139. }
  140. static inline void gl_delete_framebuffers(GLsizei num_arrays, GLuint *arrays)
  141. {
  142. glDeleteFramebuffers(num_arrays, arrays);
  143. gl_success("glDeleteFramebuffers");
  144. }
  145. static inline bool gl_tex_param_f(GLenum target, GLenum param, GLfloat val)
  146. {
  147. glTexParameterf(target, param, val);
  148. return gl_success("glTexParameterf");
  149. }
  150. static inline bool gl_tex_param_fv(GLenum target, GLenum param, GLfloat *val)
  151. {
  152. glTexParameterfv(target, param, val);
  153. return gl_success("glTexParameterf");
  154. }
  155. static inline bool gl_tex_param_i(GLenum target, GLenum param, GLint val)
  156. {
  157. glTexParameteri(target, param, val);
  158. return gl_success("glTexParameteri");
  159. }
  160. static inline bool gl_active_texture(GLenum texture_id)
  161. {
  162. glActiveTexture(texture_id);
  163. return gl_success("glActiveTexture");
  164. }
  165. static inline bool gl_enable(GLenum capability)
  166. {
  167. glEnable(capability);
  168. return gl_success("glEnable");
  169. }
  170. static inline bool gl_disable(GLenum capability)
  171. {
  172. glDisable(capability);
  173. return gl_success("glDisable");
  174. }
  175. static inline bool gl_cull_face(GLenum faces)
  176. {
  177. glCullFace(faces);
  178. return gl_success("glCullFace");
  179. }
  180. static inline bool gl_get_integer_v(GLenum pname, GLint *params)
  181. {
  182. glGetIntegerv(pname, params);
  183. return gl_success("glGetIntegerv");
  184. }
  185. extern bool gl_init_face(GLenum target, GLenum type, uint32_t num_levels, GLenum format, GLint internal_format,
  186. bool compressed, uint32_t width, uint32_t height, uint32_t size, const uint8_t ***p_data);
  187. extern bool gl_copy_texture(struct gs_device *device, struct gs_texture *dst, uint32_t dst_x, uint32_t dst_y,
  188. struct gs_texture *src, uint32_t src_x, uint32_t src_y, uint32_t width, uint32_t height);
  189. extern bool gl_create_buffer(GLenum target, GLuint *buffer, GLsizeiptr size, const GLvoid *data, GLenum usage);
  190. extern bool update_buffer(GLenum target, GLuint buffer, const void *data, size_t size);