1
0

gl-helpers.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. /*
  16. * Okay, so GL error handling is.. unclean to work with. I don't want
  17. * to have to keep typing out the same stuff over and over again do I'll just
  18. * make a bunch of helper functions to make it a bit easier to handle errors
  19. */
  20. static inline bool gl_success(const char *funcname)
  21. {
  22. GLenum errorcode = glGetError();
  23. if (errorcode != GL_NO_ERROR) {
  24. do {
  25. blog(LOG_ERROR, "%s failed, glGetError returned 0x%X",
  26. funcname, errorcode);
  27. errorcode = glGetError();
  28. } while (errorcode != GL_NO_ERROR);
  29. return false;
  30. }
  31. return true;
  32. }
  33. static inline bool gl_gen_textures(GLsizei num_texture, GLuint *textures)
  34. {
  35. glGenTextures(num_texture, textures);
  36. return gl_success("glGenTextures");
  37. }
  38. static inline bool gl_bind_texture(GLenum target, GLuint texture)
  39. {
  40. glBindTexture(target, texture);
  41. return gl_success("glBindTexture");
  42. }
  43. static inline void gl_delete_textures(GLsizei num_buffers, GLuint *buffers)
  44. {
  45. glDeleteTextures(num_buffers, buffers);
  46. gl_success("glDeleteTextures");
  47. }
  48. static inline bool gl_gen_buffers(GLsizei num_buffers, GLuint *buffers)
  49. {
  50. glGenBuffers(num_buffers, buffers);
  51. return gl_success("glGenBuffers");
  52. }
  53. static inline bool gl_bind_buffer(GLenum target, GLuint buffer)
  54. {
  55. glBindBuffer(target, buffer);
  56. return gl_success("glBindBuffer");
  57. }
  58. static inline void gl_delete_buffers(GLsizei num_buffers, GLuint *buffers)
  59. {
  60. glDeleteBuffers(num_buffers, buffers);
  61. gl_success("glDeleteBuffers");
  62. }
  63. static inline bool gl_gen_vertex_arrays(GLsizei num_arrays, GLuint *arrays)
  64. {
  65. glGenVertexArrays(num_arrays, arrays);
  66. return gl_success("glGenVertexArrays");
  67. }
  68. static inline bool gl_bind_vertex_array(GLuint array)
  69. {
  70. glBindVertexArray(array);
  71. return gl_success("glBindVertexArray");
  72. }
  73. static inline void gl_delete_vertex_arrays(GLsizei num_arrays, GLuint *arrays)
  74. {
  75. glDeleteVertexArrays(num_arrays, arrays);
  76. gl_success("glDeleteVertexArrays");
  77. }
  78. static inline bool gl_bind_renderbuffer(GLenum target, GLuint buffer)
  79. {
  80. glBindRenderbuffer(target, buffer);
  81. return gl_success("glBindRendebuffer");
  82. }
  83. static inline bool gl_bind_framebuffer(GLenum target, GLuint buffer)
  84. {
  85. glBindFramebuffer(target, buffer);
  86. return gl_success("glBindFramebuffer");
  87. }
  88. static inline bool gl_tex_param_f(GLenum target, GLenum param, GLfloat val)
  89. {
  90. glTexParameterf(target, param, val);
  91. return gl_success("glTexParameterf");
  92. }
  93. static inline bool gl_tex_param_i(GLenum target, GLenum param, GLint val)
  94. {
  95. glTexParameteri(target, param, val);
  96. return gl_success("glTexParameteri");
  97. }
  98. static inline bool gl_active_texture(GLenum texture_id)
  99. {
  100. glActiveTexture(texture_id);
  101. return gl_success("glActiveTexture");
  102. }
  103. static inline bool gl_enable(GLenum capability)
  104. {
  105. glEnable(capability);
  106. return gl_success("glEnable");
  107. }
  108. static inline bool gl_disable(GLenum capability)
  109. {
  110. glDisable(capability);
  111. return gl_success("glDisable");
  112. }
  113. static inline bool gl_cull_face(GLenum faces)
  114. {
  115. glCullFace(faces);
  116. return gl_success("glCullFace");
  117. }
  118. static inline bool gl_get_integer_v(GLenum pname, GLint *params)
  119. {
  120. glGetIntegerv(pname, params);
  121. return gl_success("glGetIntegerv");
  122. }
  123. extern bool gl_init_face(GLenum target, GLenum type, uint32_t num_levels,
  124. GLenum format, GLint internal_format, bool compressed,
  125. uint32_t width, uint32_t height, uint32_t size,
  126. const uint8_t ***p_data);
  127. extern bool gl_copy_texture(struct gs_device *device, struct gs_texture *dst,
  128. uint32_t dst_x, uint32_t dst_y,
  129. struct gs_texture *src, uint32_t src_x,
  130. uint32_t src_y, uint32_t width, uint32_t height);
  131. extern bool gl_create_buffer(GLenum target, GLuint *buffer, GLsizeiptr size,
  132. const GLvoid *data, GLenum usage);
  133. extern bool update_buffer(GLenum target, GLuint buffer, const void *data,
  134. size_t size);