gl-zstencil.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /******************************************************************************
  2. Copyright (C) 2023 by Lain 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, uint32_t height)
  16. {
  17. glGenRenderbuffers(1, &zs->buffer);
  18. if (!gl_success("glGenRenderbuffers"))
  19. return false;
  20. if (!gl_bind_renderbuffer(GL_RENDERBUFFER, zs->buffer))
  21. return false;
  22. glRenderbufferStorage(GL_RENDERBUFFER, zs->format, width, height);
  23. if (!gl_success("glRenderbufferStorage"))
  24. return false;
  25. gl_bind_renderbuffer(GL_RENDERBUFFER, 0);
  26. return true;
  27. }
  28. static inline GLenum get_attachment(enum gs_zstencil_format format)
  29. {
  30. switch (format) {
  31. case GS_Z16:
  32. return GL_DEPTH_ATTACHMENT;
  33. case GS_Z24_S8:
  34. return GL_DEPTH_STENCIL_ATTACHMENT;
  35. case GS_Z32F:
  36. return GL_DEPTH_ATTACHMENT;
  37. case GS_Z32F_S8X24:
  38. return GL_DEPTH_STENCIL_ATTACHMENT;
  39. case GS_ZS_NONE:
  40. return 0;
  41. }
  42. return 0;
  43. }
  44. gs_zstencil_t *device_zstencil_create(gs_device_t *device, uint32_t width, uint32_t height,
  45. enum gs_zstencil_format format)
  46. {
  47. struct gs_zstencil_buffer *zs;
  48. zs = bzalloc(sizeof(struct gs_zstencil_buffer));
  49. zs->format = convert_zstencil_format(format);
  50. zs->attachment = get_attachment(format);
  51. zs->device = device;
  52. if (!gl_init_zsbuffer(zs, width, height)) {
  53. blog(LOG_ERROR, "device_zstencil_create (GL) failed");
  54. gs_zstencil_destroy(zs);
  55. return NULL;
  56. }
  57. return zs;
  58. }
  59. void gs_zstencil_destroy(gs_zstencil_t *zs)
  60. {
  61. if (zs) {
  62. if (zs->buffer) {
  63. glDeleteRenderbuffers(1, &zs->buffer);
  64. gl_success("glDeleteRenderbuffers");
  65. }
  66. bfree(zs);
  67. }
  68. }