gl-helpers.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. blog(LOG_ERROR, "%s failed, glGetError returned 0x%X",
  25. funcname, errorcode);
  26. return false;
  27. }
  28. return true;
  29. }
  30. static inline bool gl_gen_textures(GLsizei num_texture, GLuint *textures)
  31. {
  32. glGenTextures(num_texture, textures);
  33. return gl_success("glGenTextures");
  34. }
  35. static inline bool gl_bind_texture(GLenum target, GLuint texture)
  36. {
  37. glBindTexture(target, texture);
  38. return gl_success("glBindTexture");
  39. }
  40. static inline void gl_delete_textures(GLsizei num_buffers, GLuint *buffers)
  41. {
  42. glDeleteTextures(num_buffers, buffers);
  43. gl_success("glDeleteTextures");
  44. }
  45. static inline bool gl_gen_buffers(GLsizei num_buffers, GLuint *buffers)
  46. {
  47. glGenBuffers(num_buffers, buffers);
  48. return gl_success("glGenBuffers");
  49. }
  50. static inline bool gl_bind_buffer(GLenum target, GLuint buffer)
  51. {
  52. glBindBuffer(target, buffer);
  53. return gl_success("glBindBuffer");
  54. }
  55. static inline void gl_delete_buffers(GLsizei num_buffers, GLuint *buffers)
  56. {
  57. glDeleteBuffers(num_buffers, buffers);
  58. gl_success("glDeleteBuffers");
  59. }
  60. static inline bool gl_gen_vertex_arrays(GLsizei num_arrays, GLuint *arrays)
  61. {
  62. glGenVertexArrays(num_arrays, arrays);
  63. return gl_success("glGenVertexArrays");
  64. }
  65. static inline bool gl_bind_vertex_array(GLuint array)
  66. {
  67. glBindVertexArray(array);
  68. return gl_success("glBindVertexArray");
  69. }
  70. static inline void gl_delete_vertex_arrays(GLsizei num_arrays, GLuint *arrays)
  71. {
  72. glDeleteVertexArrays(num_arrays, arrays);
  73. gl_success("glDeleteVertexArrays");
  74. }
  75. static inline bool gl_bind_renderbuffer(GLenum target, GLuint buffer)
  76. {
  77. glBindRenderbuffer(target, buffer);
  78. return gl_success("glBindRendebuffer");
  79. }
  80. static inline bool gl_bind_framebuffer(GLenum target, GLuint buffer)
  81. {
  82. glBindFramebuffer(target, buffer);
  83. return gl_success("glBindFramebuffer");
  84. }
  85. static inline bool gl_tex_param_f(GLenum target, GLenum param, GLfloat val)
  86. {
  87. glTexParameterf(target, param, val);
  88. return gl_success("glTexParameterf");
  89. }
  90. static inline bool gl_tex_param_i(GLenum target, GLenum param, GLint val)
  91. {
  92. glTexParameteri(target, param, val);
  93. return gl_success("glTexParameteri");
  94. }
  95. static inline bool gl_active_texture(GLenum texture_id)
  96. {
  97. glActiveTexture(texture_id);
  98. return gl_success("glActiveTexture");
  99. }
  100. static inline bool gl_enable(GLenum capability)
  101. {
  102. glEnable(capability);
  103. return gl_success("glEnable");
  104. }
  105. static inline bool gl_disable(GLenum capability)
  106. {
  107. glDisable(capability);
  108. return gl_success("glDisable");
  109. }
  110. static inline bool gl_cull_face(GLenum faces)
  111. {
  112. glCullFace(faces);
  113. return gl_success("glCullFace");
  114. }
  115. extern bool gl_init_face(GLenum target, GLenum type, uint32_t num_levels,
  116. GLenum format, GLint internal_format, bool compressed,
  117. uint32_t width, uint32_t height, uint32_t size,
  118. const void ***p_data);
  119. extern bool gl_copy_texture(struct gs_device *device,
  120. GLuint dst, GLenum dst_target,
  121. GLuint src, GLenum src_target,
  122. uint32_t width, uint32_t height);
  123. extern bool gl_create_buffer(GLenum target, GLuint *buffer, GLsizeiptr size,
  124. const GLvoid *data, GLenum usage);
  125. extern bool update_buffer(GLenum target, GLuint buffer, void *data,
  126. size_t size);