gl-zstencil.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. #include "gl-subsystem.h"
  15. static bool gl_init_zsbuffer(struct gs_zstencil_buffer *zs, uint32_t width,
  16. uint32_t height)
  17. {
  18. glGenRenderbuffers(1, &zs->buffer);
  19. if (!gl_success("glGenRenderbuffers"))
  20. return false;
  21. if (!gl_bind_renderbuffer(GL_RENDERBUFFER, zs->buffer))
  22. return false;
  23. glRenderbufferStorage(GL_RENDERBUFFER, zs->format, width, height);
  24. if (!gl_success("glRenderbufferStorage"))
  25. return false;
  26. gl_bind_renderbuffer(GL_RENDERBUFFER, 0);
  27. return true;
  28. }
  29. static inline GLenum get_attachment(enum gs_zstencil_format format)
  30. {
  31. switch (format) {
  32. case GS_Z16: return GL_DEPTH_ATTACHMENT;
  33. case GS_Z24_S8: return GL_DEPTH_STENCIL_ATTACHMENT;
  34. case GS_Z32F: return GL_DEPTH_ATTACHMENT;
  35. case GS_Z32F_S8X24: return GL_DEPTH_STENCIL_ATTACHMENT;
  36. case GS_ZS_NONE: return 0;
  37. }
  38. return 0;
  39. }
  40. zstencil_t device_create_zstencil(device_t device, uint32_t width,
  41. uint32_t height, enum gs_zstencil_format format)
  42. {
  43. struct gs_zstencil_buffer *zs;
  44. zs = bmalloc(sizeof(struct gs_zstencil_buffer));
  45. memset(zs, 0, sizeof(struct gs_zstencil_buffer));
  46. zs->format = convert_zstencil_format(format);
  47. zs->attachment = get_attachment(format);
  48. zs->device = device;
  49. if (!gl_init_zsbuffer(zs, width, height)) {
  50. blog(LOG_ERROR, "device_create_zstencil (GL) failed");
  51. zstencil_destroy(zs);
  52. return NULL;
  53. }
  54. return zs;
  55. }
  56. void zstencil_destroy(zstencil_t zs)
  57. {
  58. if (zs) {
  59. if (zs->buffer) {
  60. glDeleteRenderbuffers(1, &zs->buffer);
  61. gl_success("glDeleteRenderbuffers");
  62. }
  63. bfree(zs);
  64. }
  65. }