123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154 |
- #ifndef NANOVG_GL_UTILS_H
- #define NANOVG_GL_UTILS_H
- struct NVGLUframebuffer {
- NVGcontext* ctx;
- GLuint fbo;
- GLuint rbo;
- GLuint texture;
- int image;
- };
- typedef struct NVGLUframebuffer NVGLUframebuffer;
- void nvgluBindFramebuffer(NVGLUframebuffer* fb);
- NVGLUframebuffer* nvgluCreateFramebuffer(NVGcontext* ctx, int w, int h, int imageFlags);
- void nvgluDeleteFramebuffer(NVGLUframebuffer* fb);
- #endif
- #ifdef NANOVG_GL_IMPLEMENTATION
- #if defined(NANOVG_GL3) || defined(NANOVG_GLES2) || defined(NANOVG_GLES3)
- # define NANOVG_FBO_VALID 1
- #elif defined(NANOVG_GL2)
- # ifdef __APPLE__
- # include <OpenGL/glext.h>
- # define NANOVG_FBO_VALID 1
- # endif
- #endif
- static GLint defaultFBO = -1;
- NVGLUframebuffer* nvgluCreateFramebuffer(NVGcontext* ctx, int w, int h, int imageFlags)
- {
- #ifdef NANOVG_FBO_VALID
- GLint defaultFBO;
- GLint defaultRBO;
- NVGLUframebuffer* fb = NULL;
- glGetIntegerv(GL_FRAMEBUFFER_BINDING, &defaultFBO);
- glGetIntegerv(GL_RENDERBUFFER_BINDING, &defaultRBO);
- fb = (NVGLUframebuffer*)malloc(sizeof(NVGLUframebuffer));
- if (fb == NULL) goto error;
- memset(fb, 0, sizeof(NVGLUframebuffer));
- fb->image = nvgCreateImageRGBA(ctx, w, h, imageFlags | NVG_IMAGE_FLIPY | NVG_IMAGE_PREMULTIPLIED, NULL);
- #if defined NANOVG_GL2
- fb->texture = nvglImageHandleGL2(ctx, fb->image);
- #elif defined NANOVG_GL3
- fb->texture = nvglImageHandleGL3(ctx, fb->image);
- #elif defined NANOVG_GLES2
- fb->texture = nvglImageHandleGLES2(ctx, fb->image);
- #elif defined NANOVG_GLES3
- fb->texture = nvglImageHandleGLES3(ctx, fb->image);
- #endif
- fb->ctx = ctx;
-
- glGenFramebuffers(1, &fb->fbo);
- glBindFramebuffer(GL_FRAMEBUFFER, fb->fbo);
-
- glGenRenderbuffers(1, &fb->rbo);
- glBindRenderbuffer(GL_RENDERBUFFER, fb->rbo);
- glRenderbufferStorage(GL_RENDERBUFFER, GL_STENCIL_INDEX8, w, h);
-
- glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, fb->texture, 0);
- glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, fb->rbo);
- if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
- #ifdef GL_DEPTH24_STENCIL8
-
-
- glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, w, h);
- glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, fb->texture, 0);
- glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, fb->rbo);
- if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
- #endif
- goto error;
- }
- glBindFramebuffer(GL_FRAMEBUFFER, defaultFBO);
- glBindRenderbuffer(GL_RENDERBUFFER, defaultRBO);
- return fb;
- error:
- glBindFramebuffer(GL_FRAMEBUFFER, defaultFBO);
- glBindRenderbuffer(GL_RENDERBUFFER, defaultRBO);
- nvgluDeleteFramebuffer(fb);
- return NULL;
- #else
- NVG_NOTUSED(ctx);
- NVG_NOTUSED(w);
- NVG_NOTUSED(h);
- NVG_NOTUSED(imageFlags);
- return NULL;
- #endif
- }
- void nvgluBindFramebuffer(NVGLUframebuffer* fb)
- {
- #ifdef NANOVG_FBO_VALID
- if (defaultFBO == -1) glGetIntegerv(GL_FRAMEBUFFER_BINDING, &defaultFBO);
- glBindFramebuffer(GL_FRAMEBUFFER, fb != NULL ? fb->fbo : defaultFBO);
- #else
- NVG_NOTUSED(fb);
- #endif
- }
- void nvgluDeleteFramebuffer(NVGLUframebuffer* fb)
- {
- #ifdef NANOVG_FBO_VALID
- if (fb == NULL) return;
- if (fb->fbo != 0)
- glDeleteFramebuffers(1, &fb->fbo);
- if (fb->rbo != 0)
- glDeleteRenderbuffers(1, &fb->rbo);
- if (fb->image >= 0)
- nvgDeleteImage(fb->ctx, fb->image);
- fb->ctx = NULL;
- fb->fbo = 0;
- fb->rbo = 0;
- fb->texture = 0;
- fb->image = -1;
- free(fb);
- #else
- NVG_NOTUSED(fb);
- #endif
- }
- #endif
|