gl-zstencil.c 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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:
  33. return GL_DEPTH_ATTACHMENT;
  34. case GS_Z24_S8:
  35. return GL_DEPTH_STENCIL_ATTACHMENT;
  36. case GS_Z32F:
  37. return GL_DEPTH_ATTACHMENT;
  38. case GS_Z32F_S8X24:
  39. return GL_DEPTH_STENCIL_ATTACHMENT;
  40. case GS_ZS_NONE:
  41. return 0;
  42. }
  43. return 0;
  44. }
  45. gs_zstencil_t *device_zstencil_create(gs_device_t *device, uint32_t width,
  46. uint32_t height,
  47. enum gs_zstencil_format format)
  48. {
  49. struct gs_zstencil_buffer *zs;
  50. zs = bzalloc(sizeof(struct gs_zstencil_buffer));
  51. zs->format = convert_zstencil_format(format);
  52. zs->attachment = get_attachment(format);
  53. zs->device = device;
  54. if (!gl_init_zsbuffer(zs, width, height)) {
  55. blog(LOG_ERROR, "device_zstencil_create (GL) failed");
  56. gs_zstencil_destroy(zs);
  57. return NULL;
  58. }
  59. return zs;
  60. }
  61. void gs_zstencil_destroy(gs_zstencil_t *zs)
  62. {
  63. if (zs) {
  64. if (zs->buffer) {
  65. glDeleteRenderbuffers(1, &zs->buffer);
  66. gl_success("glDeleteRenderbuffers");
  67. }
  68. bfree(zs);
  69. }
  70. }