gl-helpers.h 5.4 KB

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