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.

133 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(NVGcontext* ctx, 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. // frame buffer object
  59. glGenFramebuffers(1, &fb->fbo);
  60. glBindFramebuffer(GL_FRAMEBUFFER, fb->fbo);
  61. // render buffer object
  62. glGenRenderbuffers(1, &fb->rbo);
  63. glBindRenderbuffer(GL_RENDERBUFFER, fb->rbo);
  64. glRenderbufferStorage(GL_RENDERBUFFER, GL_STENCIL_INDEX8, w, h);
  65. // combine all
  66. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, fb->texture, 0);
  67. glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, fb->rbo);
  68. if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) goto error;
  69. glBindFramebuffer(GL_FRAMEBUFFER, defaultFBO);
  70. glBindRenderbuffer(GL_RENDERBUFFER, defaultRBO);
  71. return fb;
  72. error:
  73. glBindFramebuffer(GL_FRAMEBUFFER, defaultFBO);
  74. glBindRenderbuffer(GL_RENDERBUFFER, defaultRBO);
  75. nvgluDeleteFramebuffer(ctx, fb);
  76. return NULL;
  77. #else
  78. NVG_NOTUSED(ctx);
  79. NVG_NOTUSED(w);
  80. NVG_NOTUSED(h);
  81. NVG_NOTUSED(imageFlags);
  82. return NULL;
  83. #endif
  84. }
  85. void nvgluBindFramebuffer(NVGLUframebuffer* fb)
  86. {
  87. #ifdef NANOVG_FBO_VALID
  88. if (defaultFBO == -1) glGetIntegerv(GL_FRAMEBUFFER_BINDING, &defaultFBO);
  89. glBindFramebuffer(GL_FRAMEBUFFER, fb != NULL ? fb->fbo : defaultFBO);
  90. #else
  91. NVG_NOTUSED(fb);
  92. #endif
  93. }
  94. void nvgluDeleteFramebuffer(NVGcontext* ctx, NVGLUframebuffer* fb)
  95. {
  96. #ifdef NANOVG_FBO_VALID
  97. if (fb == NULL) return;
  98. if (fb->fbo != 0)
  99. glDeleteFramebuffers(1, &fb->fbo);
  100. if (fb->rbo != 0)
  101. glDeleteRenderbuffers(1, &fb->rbo);
  102. if (fb->image >= 0)
  103. nvgDeleteImage(ctx, fb->image);
  104. fb->fbo = 0;
  105. fb->rbo = 0;
  106. fb->texture = 0;
  107. fb->image = -1;
  108. free(fb);
  109. #else
  110. NVG_NOTUSED(ctx);
  111. NVG_NOTUSED(fb);
  112. #endif
  113. }
  114. #endif // NANOVG_GL_IMPLEMENTATION