You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

134 lines
3.9KB

  1. //
  2. // Copyright (c) 2009-2013 Mikko Mononen memon@inside.org
  3. //
  4. // This software is provided 'as-is', without any express or implied
  5. // warranty. In no event will the authors be held liable for any damages
  6. // arising from the use of this software.
  7. // Permission is granted to anyone to use this software for any purpose,
  8. // including commercial applications, and to alter it and redistribute it
  9. // freely, subject to the following restrictions:
  10. // 1. The origin of this software must not be misrepresented; you must not
  11. // claim that you wrote the original software. If you use this software
  12. // in a product, an acknowledgment in the product documentation would be
  13. // appreciated but is not required.
  14. // 2. Altered source versions must be plainly marked as such, and must not be
  15. // misrepresented as being the original software.
  16. // 3. This notice may not be removed or altered from any source distribution.
  17. //
  18. #ifndef NANOVG_GL_UTILS_H
  19. #define NANOVG_GL_UTILS_H
  20. struct NVGLUframebuffer {
  21. NVGcontext* ctx;
  22. GLuint fbo;
  23. GLuint rbo;
  24. GLuint texture;
  25. int image;
  26. };
  27. typedef struct NVGLUframebuffer NVGLUframebuffer;
  28. // Helper function to create GL frame buffer to render to.
  29. void nvgluBindFramebuffer(NVGLUframebuffer* fb);
  30. NVGLUframebuffer* nvgluCreateFramebuffer(NVGcontext* ctx, int w, int h, int imageFlags);
  31. void nvgluDeleteFramebuffer(NVGLUframebuffer* fb);
  32. #endif // NANOVG_GL_UTILS_H
  33. #ifdef NANOVG_GL_IMPLEMENTATION
  34. #if defined(NANOVG_GL3) || defined(NANOVG_GLES2) || defined(NANOVG_GLES3)
  35. // FBO is core in OpenGL 3>.
  36. # define NANOVG_FBO_VALID 1
  37. #elif defined(NANOVG_GL2)
  38. // On OS X including glext defines FBO on GL2 too.
  39. # ifdef __APPLE__
  40. # include <OpenGL/glext.h>
  41. # define NANOVG_FBO_VALID 1
  42. # endif
  43. #endif
  44. static GLint defaultFBO = -1;
  45. NVGLUframebuffer* nvgluCreateFramebuffer(NVGcontext* ctx, int w, int h, int imageFlags)
  46. {
  47. #ifdef NANOVG_FBO_VALID
  48. GLint defaultFBO;
  49. GLint defaultRBO;
  50. NVGLUframebuffer* fb = NULL;
  51. glGetIntegerv(GL_FRAMEBUFFER_BINDING, &defaultFBO);
  52. glGetIntegerv(GL_RENDERBUFFER_BINDING, &defaultRBO);
  53. fb = (NVGLUframebuffer*)malloc(sizeof(NVGLUframebuffer));
  54. if (fb == NULL) goto error;
  55. memset(fb, 0, sizeof(NVGLUframebuffer));
  56. fb->image = nvgCreateImageRGBA(ctx, w, h, imageFlags | NVG_IMAGE_FLIPY | NVG_IMAGE_PREMULTIPLIED, NULL);
  57. fb->texture = nvglImageHandle(ctx, fb->image);
  58. fb->ctx = ctx;
  59. // frame buffer object
  60. glGenFramebuffers(1, &fb->fbo);
  61. glBindFramebuffer(GL_FRAMEBUFFER, fb->fbo);
  62. // render buffer object
  63. glGenRenderbuffers(1, &fb->rbo);
  64. glBindRenderbuffer(GL_RENDERBUFFER, fb->rbo);
  65. glRenderbufferStorage(GL_RENDERBUFFER, GL_STENCIL_INDEX8, w, h);
  66. // combine all
  67. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, fb->texture, 0);
  68. glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, fb->rbo);
  69. if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) goto error;
  70. glBindFramebuffer(GL_FRAMEBUFFER, defaultFBO);
  71. glBindRenderbuffer(GL_RENDERBUFFER, defaultRBO);
  72. return fb;
  73. error:
  74. glBindFramebuffer(GL_FRAMEBUFFER, defaultFBO);
  75. glBindRenderbuffer(GL_RENDERBUFFER, defaultRBO);
  76. nvgluDeleteFramebuffer(fb);
  77. return NULL;
  78. #else
  79. NVG_NOTUSED(ctx);
  80. NVG_NOTUSED(w);
  81. NVG_NOTUSED(h);
  82. NVG_NOTUSED(imageFlags);
  83. return NULL;
  84. #endif
  85. }
  86. void nvgluBindFramebuffer(NVGLUframebuffer* fb)
  87. {
  88. #ifdef NANOVG_FBO_VALID
  89. if (defaultFBO == -1) glGetIntegerv(GL_FRAMEBUFFER_BINDING, &defaultFBO);
  90. glBindFramebuffer(GL_FRAMEBUFFER, fb != NULL ? fb->fbo : defaultFBO);
  91. #else
  92. NVG_NOTUSED(fb);
  93. #endif
  94. }
  95. void nvgluDeleteFramebuffer(NVGLUframebuffer* fb)
  96. {
  97. #ifdef NANOVG_FBO_VALID
  98. if (fb == NULL) return;
  99. if (fb->fbo != 0)
  100. glDeleteFramebuffers(1, &fb->fbo);
  101. if (fb->rbo != 0)
  102. glDeleteRenderbuffers(1, &fb->rbo);
  103. if (fb->image >= 0)
  104. nvgDeleteImage(fb->ctx, fb->image);
  105. fb->ctx = NULL;
  106. fb->fbo = 0;
  107. fb->rbo = 0;
  108. fb->texture = 0;
  109. fb->image = -1;
  110. free(fb);
  111. #else
  112. NVG_NOTUSED(fb);
  113. #endif
  114. }
  115. #endif // NANOVG_GL_IMPLEMENTATION