1
0

gl-helpers.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 3 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. #include "gl-subsystem.h"
  15. bool gl_init_face(GLenum target, GLenum type, uint32_t num_levels,
  16. GLenum format, GLint internal_format, bool compressed,
  17. uint32_t width, uint32_t height, uint32_t size,
  18. const void ***p_data)
  19. {
  20. bool success = true;
  21. const void **data = p_data ? *p_data : NULL;
  22. uint32_t i;
  23. for (i = 0; i < num_levels; i++) {
  24. if (compressed) {
  25. glCompressedTexImage2D(target, i, internal_format,
  26. width, height, 0, size,
  27. data ? *data : NULL);
  28. if (!gl_success("glCompressedTexImage2D"))
  29. success = false;
  30. } else {
  31. glTexImage2D(target, i, internal_format, width, height,
  32. 0, format, type, data ? *data : NULL);
  33. if (!gl_success("glTexImage2D"))
  34. success = false;
  35. }
  36. if (data)
  37. data++;
  38. size /= 4;
  39. width /= 2;
  40. height /= 2;
  41. if (width == 0) width = 1;
  42. if (height == 0) height = 1;
  43. }
  44. if (data)
  45. *p_data = data;
  46. return success;
  47. }
  48. bool gl_copy_texture(struct gs_device *device,
  49. GLuint dst, GLenum dst_target,
  50. GLuint src, GLenum src_target,
  51. uint32_t width, uint32_t height)
  52. {
  53. bool success = false;
  54. if (device->copy_type == COPY_TYPE_ARB) {
  55. glCopyImageSubData(src, src_target, 0, 0, 0, 0,
  56. dst, dst_target, 0, 0, 0, 0,
  57. width, height, 1);
  58. success = gl_success("glCopyImageSubData");
  59. } else if (device->copy_type == COPY_TYPE_NV) {
  60. glCopyImageSubDataNV(src, src_target, 0, 0, 0, 0,
  61. dst, dst_target, 0, 0, 0, 0,
  62. width, height, 1);
  63. success = gl_success("glCopyImageSubDataNV");
  64. } else if (device->copy_type == COPY_TYPE_FBO_BLIT) {
  65. /* TODO (implement FBOs) */
  66. }
  67. return success;
  68. }
  69. bool gl_create_buffer(GLenum target, GLuint *buffer, GLsizeiptr size,
  70. const GLvoid *data, GLenum usage)
  71. {
  72. bool success;
  73. if (!gl_gen_buffers(1, buffer))
  74. return false;
  75. if (!gl_bind_buffer(target, *buffer))
  76. return false;
  77. glBufferData(target, size, data, usage);
  78. success = gl_success("glBufferData");
  79. gl_bind_buffer(target, 0);
  80. return success;
  81. }
  82. bool update_buffer(GLenum target, GLuint buffer, void *data, size_t size)
  83. {
  84. void *ptr;
  85. bool success = true;
  86. if (!gl_bind_buffer(target, buffer))
  87. return false;
  88. ptr = glMapBuffer(target, GL_WRITE_ONLY);
  89. success = gl_success("glMapBuffer");
  90. if (success && ptr) {
  91. memcpy(ptr, data, size);
  92. glUnmapBuffer(target);
  93. }
  94. gl_bind_buffer(target, 0);
  95. return success;
  96. }