gl-helpers.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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 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,
  68. "%s failed, glGetError returned %s(0x%X)",
  69. funcname, gl_error_to_str(errorcode), errorcode);
  70. errorcode = glGetError();
  71. --attempts;
  72. if (attempts == 0) {
  73. blog(LOG_ERROR,
  74. "Too many GL errors, moving on");
  75. break;
  76. }
  77. } while (errorcode != GL_NO_ERROR);
  78. return false;
  79. }
  80. return true;
  81. }
  82. static inline bool gl_gen_textures(GLsizei num_texture, GLuint *textures)
  83. {
  84. glGenTextures(num_texture, textures);
  85. return gl_success("glGenTextures");
  86. }
  87. static inline bool gl_bind_texture(GLenum target, GLuint texture)
  88. {
  89. glBindTexture(target, texture);
  90. return gl_success("glBindTexture");
  91. }
  92. static inline void gl_delete_textures(GLsizei num_buffers, GLuint *buffers)
  93. {
  94. glDeleteTextures(num_buffers, buffers);
  95. gl_success("glDeleteTextures");
  96. }
  97. static inline bool gl_gen_buffers(GLsizei num_buffers, GLuint *buffers)
  98. {
  99. glGenBuffers(num_buffers, buffers);
  100. return gl_success("glGenBuffers");
  101. }
  102. static inline bool gl_bind_buffer(GLenum target, GLuint buffer)
  103. {
  104. glBindBuffer(target, buffer);
  105. return gl_success("glBindBuffer");
  106. }
  107. static inline void gl_delete_buffers(GLsizei num_buffers, GLuint *buffers)
  108. {
  109. glDeleteBuffers(num_buffers, buffers);
  110. gl_success("glDeleteBuffers");
  111. }
  112. static inline bool gl_gen_vertex_arrays(GLsizei num_arrays, GLuint *arrays)
  113. {
  114. glGenVertexArrays(num_arrays, arrays);
  115. return gl_success("glGenVertexArrays");
  116. }
  117. static inline bool gl_bind_vertex_array(GLuint array)
  118. {
  119. glBindVertexArray(array);
  120. return gl_success("glBindVertexArray");
  121. }
  122. static inline void gl_delete_vertex_arrays(GLsizei num_arrays, GLuint *arrays)
  123. {
  124. glDeleteVertexArrays(num_arrays, arrays);
  125. gl_success("glDeleteVertexArrays");
  126. }
  127. static inline bool gl_bind_renderbuffer(GLenum target, GLuint buffer)
  128. {
  129. glBindRenderbuffer(target, buffer);
  130. return gl_success("glBindRendebuffer");
  131. }
  132. static inline bool gl_gen_framebuffers(GLsizei num_arrays, GLuint *arrays)
  133. {
  134. glGenFramebuffers(num_arrays, arrays);
  135. return gl_success("glGenFramebuffers");
  136. }
  137. static inline bool gl_bind_framebuffer(GLenum target, GLuint buffer)
  138. {
  139. glBindFramebuffer(target, buffer);
  140. return gl_success("glBindFramebuffer");
  141. }
  142. static inline void gl_delete_framebuffers(GLsizei num_arrays, GLuint *arrays)
  143. {
  144. glDeleteFramebuffers(num_arrays, arrays);
  145. gl_success("glDeleteFramebuffers");
  146. }
  147. static inline bool gl_tex_param_f(GLenum target, GLenum param, GLfloat val)
  148. {
  149. glTexParameterf(target, param, val);
  150. return gl_success("glTexParameterf");
  151. }
  152. static inline bool gl_tex_param_fv(GLenum target, GLenum param, GLfloat *val)
  153. {
  154. glTexParameterfv(target, param, val);
  155. return gl_success("glTexParameterf");
  156. }
  157. static inline bool gl_tex_param_i(GLenum target, GLenum param, GLint val)
  158. {
  159. glTexParameteri(target, param, val);
  160. return gl_success("glTexParameteri");
  161. }
  162. static inline bool gl_active_texture(GLenum texture_id)
  163. {
  164. glActiveTexture(texture_id);
  165. return gl_success("glActiveTexture");
  166. }
  167. static inline bool gl_enable(GLenum capability)
  168. {
  169. glEnable(capability);
  170. return gl_success("glEnable");
  171. }
  172. static inline bool gl_disable(GLenum capability)
  173. {
  174. glDisable(capability);
  175. return gl_success("glDisable");
  176. }
  177. static inline bool gl_cull_face(GLenum faces)
  178. {
  179. glCullFace(faces);
  180. return gl_success("glCullFace");
  181. }
  182. static inline bool gl_get_integer_v(GLenum pname, GLint *params)
  183. {
  184. glGetIntegerv(pname, params);
  185. return gl_success("glGetIntegerv");
  186. }
  187. extern bool gl_init_face(GLenum target, GLenum type, uint32_t num_levels,
  188. GLenum format, GLint internal_format, bool compressed,
  189. uint32_t width, uint32_t height, uint32_t size,
  190. const uint8_t ***p_data);
  191. extern bool gl_copy_texture(struct gs_device *device, struct gs_texture *dst,
  192. uint32_t dst_x, uint32_t dst_y,
  193. struct gs_texture *src, uint32_t src_x,
  194. uint32_t src_y, uint32_t width, uint32_t height);
  195. extern bool gl_create_buffer(GLenum target, GLuint *buffer, GLsizeiptr size,
  196. const GLvoid *data, GLenum usage);
  197. extern bool update_buffer(GLenum target, GLuint buffer, const void *data,
  198. size_t size);