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
21 struct NVGLUframebuffer {
22 NVGcontext* ctx;
23 GLuint fbo;
24 GLuint rbo;
25 GLuint texture;
26 int image;
27 };
28 typedef struct NVGLUframebuffer NVGLUframebuffer;
29
30 // Helper function to create GL frame buffer to render to.
31 void nvgluBindFramebuffer(NVGLUframebuffer* fb);
32 NVGLUframebuffer* nvgluCreateFramebuffer(NVGcontext* ctx, int w, int h, int imageFlags);
33 void nvgluDeleteFramebuffer(NVGcontext* ctx, NVGLUframebuffer* fb);
34
35 #endif // NANOVG_GL_UTILS_H
36
37 #ifdef NANOVG_GL_IMPLEMENTATION
38
39 #if defined(NANOVG_GL3) || defined(NANOVG_GLES2) || defined(NANOVG_GLES3)
40 // FBO is core in OpenGL 3>.
41 # define NANOVG_FBO_VALID 1
42 #elif defined(NANOVG_GL2)
43 // On OS X including glext defines FBO on GL2 too.
44 # ifdef __APPLE__
45 # include <OpenGL/glext.h>
46 # define NANOVG_FBO_VALID 1
47 # endif
48 #endif
49
50 static GLint defaultFBO = -1;
51
nvgluCreateFramebuffer(NVGcontext * ctx,int w,int h,int imageFlags)52 NVGLUframebuffer* nvgluCreateFramebuffer(NVGcontext* ctx, int w, int h, int imageFlags)
53 {
54 #ifdef NANOVG_FBO_VALID
55 GLint defaultFBO;
56 GLint defaultRBO;
57 NVGLUframebuffer* fb = NULL;
58
59 glGetIntegerv(GL_FRAMEBUFFER_BINDING, &defaultFBO);
60 glGetIntegerv(GL_RENDERBUFFER_BINDING, &defaultRBO);
61
62 fb = (NVGLUframebuffer*)malloc(sizeof(NVGLUframebuffer));
63 if (fb == NULL) goto error;
64 memset(fb, 0, sizeof(NVGLUframebuffer));
65
66 fb->image = nvgCreateImageRGBA(ctx, w, h, imageFlags | NVG_IMAGE_FLIPY | NVG_IMAGE_PREMULTIPLIED, NULL);
67 fb->texture = nvglImageHandle(ctx, fb->image);
68
69 // frame buffer object
70 glGenFramebuffers(1, &fb->fbo);
71 glBindFramebuffer(GL_FRAMEBUFFER, fb->fbo);
72
73 // render buffer object
74 glGenRenderbuffers(1, &fb->rbo);
75 glBindRenderbuffer(GL_RENDERBUFFER, fb->rbo);
76 glRenderbufferStorage(GL_RENDERBUFFER, GL_STENCIL_INDEX8, w, h);
77
78 // combine all
79 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, fb->texture, 0);
80 glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, fb->rbo);
81
82 if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) goto error;
83
84 glBindFramebuffer(GL_FRAMEBUFFER, defaultFBO);
85 glBindRenderbuffer(GL_RENDERBUFFER, defaultRBO);
86 return fb;
87 error:
88 glBindFramebuffer(GL_FRAMEBUFFER, defaultFBO);
89 glBindRenderbuffer(GL_RENDERBUFFER, defaultRBO);
90 nvgluDeleteFramebuffer(ctx, fb);
91 return NULL;
92 #else
93 NVG_NOTUSED(ctx);
94 NVG_NOTUSED(w);
95 NVG_NOTUSED(h);
96 NVG_NOTUSED(imageFlags);
97 return NULL;
98 #endif
99 }
100
nvgluBindFramebuffer(NVGLUframebuffer * fb)101 void nvgluBindFramebuffer(NVGLUframebuffer* fb)
102 {
103 #ifdef NANOVG_FBO_VALID
104 if (defaultFBO == -1) glGetIntegerv(GL_FRAMEBUFFER_BINDING, &defaultFBO);
105 glBindFramebuffer(GL_FRAMEBUFFER, fb != NULL ? fb->fbo : defaultFBO);
106 #else
107 NVG_NOTUSED(fb);
108 #endif
109 }
110
nvgluDeleteFramebuffer(NVGcontext * ctx,NVGLUframebuffer * fb)111 void nvgluDeleteFramebuffer(NVGcontext* ctx, NVGLUframebuffer* fb)
112 {
113 #ifdef NANOVG_FBO_VALID
114 if (fb == NULL) return;
115 if (fb->fbo != 0)
116 glDeleteFramebuffers(1, &fb->fbo);
117 if (fb->rbo != 0)
118 glDeleteRenderbuffers(1, &fb->rbo);
119 if (fb->image >= 0)
120 nvgDeleteImage(ctx, fb->image);
121 fb->fbo = 0;
122 fb->rbo = 0;
123 fb->texture = 0;
124 fb->image = -1;
125 free(fb);
126 #else
127 NVG_NOTUSED(ctx);
128 NVG_NOTUSED(fb);
129 #endif
130 }
131
132 #endif // NANOVG_GL_IMPLEMENTATION
133