1
0

gl-helpers.c 3.2 KB

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