Cleanup
[carla.git] / source / modules / dgl / src / nanovg / nanovg_gl.h
blob4342c25ffe90f8842ca6faad46a28e09a5117a81
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.
18 #ifndef NANOVG_GL_H
19 #define NANOVG_GL_H
21 #ifdef __cplusplus
22 extern "C" {
23 #endif
25 // Create flags
27 enum NVGcreateFlags {
28 // Flag indicating if geometry based anti-aliasing is used (may not be needed when using MSAA).
29 NVG_ANTIALIAS = 1<<0,
30 // Flag indicating if strokes should be drawn using stencil buffer. The rendering will be a little
31 // slower, but path overlaps (i.e. self-intersecting or sharp turns) will be drawn just once.
32 NVG_STENCIL_STROKES = 1<<1,
33 // Flag indicating that additional debug checks are done.
34 NVG_DEBUG = 1<<2,
37 #if defined NANOVG_GL2_IMPLEMENTATION
38 # define NANOVG_GL2 1
39 # define NANOVG_GL_IMPLEMENTATION 1
40 #elif defined NANOVG_GL3_IMPLEMENTATION
41 # define NANOVG_GL3 1
42 # define NANOVG_GL_IMPLEMENTATION 1
43 # define NANOVG_GL_USE_UNIFORMBUFFER 1
44 #elif defined NANOVG_GLES2_IMPLEMENTATION
45 # define NANOVG_GLES2 1
46 # define NANOVG_GL_IMPLEMENTATION 1
47 #elif defined NANOVG_GLES3_IMPLEMENTATION
48 # define NANOVG_GLES3 1
49 # define NANOVG_GL_IMPLEMENTATION 1
50 #endif
52 #define NANOVG_GL_USE_STATE_FILTER (1)
54 // Creates NanoVG contexts for different OpenGL (ES) versions.
55 // Flags should be combination of the create flags above.
57 #if defined NANOVG_GL2
59 NVGcontext* nvgCreateGL2(int flags);
60 NVGcontext* nvgCreateSharedGL2(NVGcontext* other, int flags);
61 void nvgDeleteGL2(NVGcontext* ctx);
63 int nvglCreateImageFromHandleGL2(NVGcontext* ctx, GLuint textureId, int w, int h, int flags);
64 GLuint nvglImageHandleGL2(NVGcontext* ctx, int image);
66 #endif
68 #if defined NANOVG_GL3
70 NVGcontext* nvgCreateGL3(int flags);
71 NVGcontext* nvgCreateSharedGL3(NVGcontext* other, int flags);
72 void nvgDeleteGL3(NVGcontext* ctx);
74 int nvglCreateImageFromHandleGL3(NVGcontext* ctx, GLuint textureId, int w, int h, int flags);
75 GLuint nvglImageHandleGL3(NVGcontext* ctx, int image);
77 #endif
79 #if defined NANOVG_GLES2
81 NVGcontext* nvgCreateGLES2(int flags);
82 NVGcontext* nvgCreateSharedGLES2(NVGcontext* other, int flags);
83 void nvgDeleteGLES2(NVGcontext* ctx);
85 int nvglCreateImageFromHandleGLES2(NVGcontext* ctx, GLuint textureId, int w, int h, int flags);
86 GLuint nvglImageHandleGLES2(NVGcontext* ctx, int image);
88 #endif
90 #if defined NANOVG_GLES3
92 NVGcontext* nvgCreateGLES3(int flags);
93 NVGcontext* nvgCreateSharedGLES3(NVGcontext* other, int flags);
94 void nvgDeleteGLES3(NVGcontext* ctx);
96 int nvglCreateImageFromHandleGLES3(NVGcontext* ctx, GLuint textureId, int w, int h, int flags);
97 GLuint nvglImageHandleGLES3(NVGcontext* ctx, int image);
99 #endif
101 // These are additional flags on top of NVGimageFlags.
102 enum NVGimageFlagsGL {
103 NVG_IMAGE_NODELETE = 1<<16, // Do not delete GL texture handle.
106 #ifdef __cplusplus
108 #endif
110 #endif /* NANOVG_GL_H */
112 #ifdef NANOVG_GL_IMPLEMENTATION
114 #include <stdlib.h>
115 #include <stdio.h>
116 #include <string.h>
117 #include <math.h>
118 #include "nanovg.h"
120 enum GLNVGuniformLoc {
121 GLNVG_LOC_VIEWSIZE,
122 GLNVG_LOC_TEX,
123 GLNVG_LOC_FRAG,
124 GLNVG_MAX_LOCS
127 enum GLNVGshaderType {
128 NSVG_SHADER_FILLGRAD,
129 NSVG_SHADER_FILLIMG,
130 NSVG_SHADER_SIMPLE,
131 NSVG_SHADER_IMG
134 #if NANOVG_GL_USE_UNIFORMBUFFER
135 enum GLNVGuniformBindings {
136 GLNVG_FRAG_BINDING = 0,
138 #endif
140 struct GLNVGshader {
141 GLuint prog;
142 GLuint frag;
143 GLuint vert;
144 GLint loc[GLNVG_MAX_LOCS];
146 typedef struct GLNVGshader GLNVGshader;
148 struct GLNVGtexture {
149 int id;
150 GLuint tex;
151 int width, height;
152 int type;
153 int flags;
154 #if defined NANOVG_GLES2
155 unsigned char* data;
156 #endif
158 typedef struct GLNVGtexture GLNVGtexture;
160 struct GLNVGblend
162 GLenum srcRGB;
163 GLenum dstRGB;
164 GLenum srcAlpha;
165 GLenum dstAlpha;
167 typedef struct GLNVGblend GLNVGblend;
169 enum GLNVGcallType {
170 GLNVG_NONE = 0,
171 GLNVG_FILL,
172 GLNVG_CONVEXFILL,
173 GLNVG_STROKE,
174 GLNVG_TRIANGLES,
177 struct GLNVGcall {
178 int type;
179 int image;
180 int pathOffset;
181 int pathCount;
182 int triangleOffset;
183 int triangleCount;
184 int uniformOffset;
185 GLNVGblend blendFunc;
187 typedef struct GLNVGcall GLNVGcall;
189 struct GLNVGpath {
190 int fillOffset;
191 int fillCount;
192 int strokeOffset;
193 int strokeCount;
195 typedef struct GLNVGpath GLNVGpath;
197 struct GLNVGfragUniforms {
198 #if NANOVG_GL_USE_UNIFORMBUFFER
199 float scissorMat[12]; // matrices are actually 3 vec4s
200 float paintMat[12];
201 struct NVGcolor innerCol;
202 struct NVGcolor outerCol;
203 float scissorExt[2];
204 float scissorScale[2];
205 float extent[2];
206 float radius;
207 float feather;
208 float strokeMult;
209 float strokeThr;
210 int texType;
211 int type;
212 #else
213 // note: after modifying layout or size of uniform array,
214 // don't forget to also update the fragment shader source!
215 #define NANOVG_GL_UNIFORMARRAY_SIZE 11
216 union {
217 struct {
218 float scissorMat[12]; // matrices are actually 3 vec4s
219 float paintMat[12];
220 struct NVGcolor innerCol;
221 struct NVGcolor outerCol;
222 float scissorExt[2];
223 float scissorScale[2];
224 float extent[2];
225 float radius;
226 float feather;
227 float strokeMult;
228 float strokeThr;
229 float texType;
230 float type;
232 float uniformArray[NANOVG_GL_UNIFORMARRAY_SIZE][4];
234 #endif
236 typedef struct GLNVGfragUniforms GLNVGfragUniforms;
238 struct GLNVGtextureContext { // Textures; shared between shared NanoVG contexts.
239 int refCount;
240 GLNVGtexture* textures;
241 int ntextures;
242 int ctextures;
243 int textureId;
245 typedef struct GLNVGtextureContext GLNVGtextureContext;
247 struct GLNVGcontext {
248 GLNVGshader shader;
249 GLNVGtextureContext* textureContext;
250 float view[2];
251 GLuint vertBuf;
252 #if defined NANOVG_GL3
253 GLuint vertArr;
254 #endif
255 #if NANOVG_GL_USE_UNIFORMBUFFER
256 GLuint fragBuf;
257 #endif
258 int fragSize;
259 int flags;
261 // Per frame buffers
262 GLNVGcall* calls;
263 int ccalls;
264 int ncalls;
265 GLNVGpath* paths;
266 int cpaths;
267 int npaths;
268 struct NVGvertex* verts;
269 int cverts;
270 int nverts;
271 unsigned char* uniforms;
272 int cuniforms;
273 int nuniforms;
275 // cached state
276 #if NANOVG_GL_USE_STATE_FILTER
277 GLuint boundTexture;
278 GLuint stencilMask;
279 GLenum stencilFunc;
280 GLint stencilFuncRef;
281 GLuint stencilFuncMask;
282 GLNVGblend blendFunc;
283 #endif
285 int dummyTex;
287 typedef struct GLNVGcontext GLNVGcontext;
289 static int glnvg__maxi(int a, int b) { return a > b ? a : b; }
291 #ifdef NANOVG_GLES2
292 static unsigned int glnvg__nearestPow2(unsigned int num)
294 unsigned n = num > 0 ? num - 1 : 0;
295 n |= n >> 1;
296 n |= n >> 2;
297 n |= n >> 4;
298 n |= n >> 8;
299 n |= n >> 16;
300 n++;
301 return n;
303 #endif
305 static void glnvg__bindTexture(GLNVGcontext* gl, GLuint tex)
307 #if NANOVG_GL_USE_STATE_FILTER
308 if (gl->boundTexture != tex) {
309 gl->boundTexture = tex;
310 glBindTexture(GL_TEXTURE_2D, tex);
312 #else
313 glBindTexture(GL_TEXTURE_2D, tex);
314 #endif
317 static void glnvg__stencilMask(GLNVGcontext* gl, GLuint mask)
319 #if NANOVG_GL_USE_STATE_FILTER
320 if (gl->stencilMask != mask) {
321 gl->stencilMask = mask;
322 glStencilMask(mask);
324 #else
325 glStencilMask(mask);
326 #endif
329 static void glnvg__stencilFunc(GLNVGcontext* gl, GLenum func, GLint ref, GLuint mask)
331 #if NANOVG_GL_USE_STATE_FILTER
332 if ((gl->stencilFunc != func) ||
333 (gl->stencilFuncRef != ref) ||
334 (gl->stencilFuncMask != mask)) {
336 gl->stencilFunc = func;
337 gl->stencilFuncRef = ref;
338 gl->stencilFuncMask = mask;
339 glStencilFunc(func, ref, mask);
341 #else
342 glStencilFunc(func, ref, mask);
343 #endif
345 static void glnvg__blendFuncSeparate(GLNVGcontext* gl, const GLNVGblend* blend)
347 #if NANOVG_GL_USE_STATE_FILTER
348 if ((gl->blendFunc.srcRGB != blend->srcRGB) ||
349 (gl->blendFunc.dstRGB != blend->dstRGB) ||
350 (gl->blendFunc.srcAlpha != blend->srcAlpha) ||
351 (gl->blendFunc.dstAlpha != blend->dstAlpha)) {
353 gl->blendFunc = *blend;
354 glBlendFuncSeparate(blend->srcRGB, blend->dstRGB, blend->srcAlpha,blend->dstAlpha);
356 #else
357 glBlendFuncSeparate(blend->srcRGB, blend->dstRGB, blend->srcAlpha,blend->dstAlpha);
358 #endif
361 static GLNVGtexture* glnvg__allocTexture(GLNVGcontext* gl)
363 GLNVGtexture* tex = NULL;
364 int i;
366 for (i = 0; i < gl->textureContext->ntextures; i++) {
367 if (gl->textureContext->textures[i].id == 0) {
368 tex = &gl->textureContext->textures[i];
369 break;
372 if (tex == NULL) {
373 if (gl->textureContext->ntextures+1 > gl->textureContext->ctextures) {
374 GLNVGtexture* textures;
375 int ctextures = glnvg__maxi(gl->textureContext->ntextures+1, 4) + gl->textureContext->ctextures/2; // 1.5x Overallocate
376 textures = (GLNVGtexture*)realloc(gl->textureContext->textures, sizeof(GLNVGtexture)*ctextures);
377 if (textures == NULL) return NULL;
378 gl->textureContext->textures = textures;
379 gl->textureContext->ctextures = ctextures;
381 tex = &gl->textureContext->textures[gl->textureContext->ntextures++];
384 memset(tex, 0, sizeof(*tex));
385 tex->id = ++gl->textureContext->textureId;
387 return tex;
390 static GLNVGtexture* glnvg__findTexture(GLNVGcontext* gl, int id)
392 int i;
393 for (i = 0; i < gl->textureContext->ntextures; i++)
394 if (gl->textureContext->textures[i].id == id)
395 return &gl->textureContext->textures[i];
396 return NULL;
399 static int glnvg__deleteTexture(GLNVGcontext* gl, int id)
401 int i;
402 for (i = 0; i < gl->textureContext->ntextures; i++) {
403 if (gl->textureContext->textures[i].id == id) {
404 if (gl->textureContext->textures[i].tex != 0 && (gl->textureContext->textures[i].flags & NVG_IMAGE_NODELETE) == 0)
406 glDeleteTextures(1, &gl->textureContext->textures[i].tex);
407 #if defined NANOVG_GLES2
408 free(gl->textureContext->textures[i].data);
409 #endif
411 memset(&gl->textureContext->textures[i], 0, sizeof(gl->textureContext->textures[i]));
412 return 1;
415 return 0;
418 static void glnvg__dumpShaderError(GLuint shader, const char* name, const char* type)
420 GLchar str[512+1];
421 GLsizei len = 0;
422 glGetShaderInfoLog(shader, 512, &len, str);
423 if (len > 512) len = 512;
424 str[len] = '\0';
425 printf("Shader %s/%s error:\n%s\n", name, type, str);
428 static void glnvg__dumpProgramError(GLuint prog, const char* name)
430 GLchar str[512+1];
431 GLsizei len = 0;
432 glGetProgramInfoLog(prog, 512, &len, str);
433 if (len > 512) len = 512;
434 str[len] = '\0';
435 printf("Program %s error:\n%s\n", name, str);
438 static void glnvg__checkError(GLNVGcontext* gl, const char* str)
440 GLenum err;
441 if ((gl->flags & NVG_DEBUG) == 0) return;
442 err = glGetError();
443 if (err != GL_NO_ERROR) {
444 printf("Error %08x after %s\n", err, str);
445 return;
449 static int glnvg__createShader(GLNVGshader* shader, const char* name, const char* header, const char* opts, const char* vshader, const char* fshader)
451 GLint status;
452 GLuint prog, vert, frag;
453 const char* str[3];
454 str[0] = header;
455 str[1] = opts != NULL ? opts : "";
457 memset(shader, 0, sizeof(*shader));
459 prog = glCreateProgram();
460 vert = glCreateShader(GL_VERTEX_SHADER);
461 frag = glCreateShader(GL_FRAGMENT_SHADER);
462 str[2] = vshader;
463 glShaderSource(vert, 3, str, 0);
464 str[2] = fshader;
465 glShaderSource(frag, 3, str, 0);
467 glCompileShader(vert);
468 glGetShaderiv(vert, GL_COMPILE_STATUS, &status);
469 if (status != GL_TRUE) {
470 glnvg__dumpShaderError(vert, name, "vert");
471 return 0;
474 glCompileShader(frag);
475 glGetShaderiv(frag, GL_COMPILE_STATUS, &status);
476 if (status != GL_TRUE) {
477 glnvg__dumpShaderError(frag, name, "frag");
478 return 0;
481 glAttachShader(prog, vert);
482 glAttachShader(prog, frag);
484 glBindAttribLocation(prog, 0, "vertex");
485 glBindAttribLocation(prog, 1, "tcoord");
487 glLinkProgram(prog);
488 glGetProgramiv(prog, GL_LINK_STATUS, &status);
489 if (status != GL_TRUE) {
490 glnvg__dumpProgramError(prog, name);
491 return 0;
494 shader->prog = prog;
495 shader->vert = vert;
496 shader->frag = frag;
498 return 1;
501 static void glnvg__deleteShader(GLNVGshader* shader)
503 if (shader->prog != 0)
504 glDeleteProgram(shader->prog);
505 if (shader->vert != 0)
506 glDeleteShader(shader->vert);
507 if (shader->frag != 0)
508 glDeleteShader(shader->frag);
511 static void glnvg__getUniforms(GLNVGshader* shader)
513 shader->loc[GLNVG_LOC_VIEWSIZE] = glGetUniformLocation(shader->prog, "viewSize");
514 shader->loc[GLNVG_LOC_TEX] = glGetUniformLocation(shader->prog, "tex");
516 #if NANOVG_GL_USE_UNIFORMBUFFER
517 shader->loc[GLNVG_LOC_FRAG] = glGetUniformBlockIndex(shader->prog, "frag");
518 #else
519 shader->loc[GLNVG_LOC_FRAG] = glGetUniformLocation(shader->prog, "frag");
520 #endif
523 static int glnvg__renderCreateTexture(void* uptr, int type, int w, int h, int imageFlags, const unsigned char* data);
525 static int glnvg__renderCreate(void* uptr, void* otherUptr) // Share the textures of GLNVGcontext 'otherUptr' if it's non-NULL.
527 GLNVGcontext* gl = (GLNVGcontext*)uptr;
529 if (otherUptr) {
530 GLNVGcontext* other = (GLNVGcontext*)otherUptr;
531 gl->textureContext = other->textureContext;
532 gl->textureContext->refCount++;
533 } else {
534 gl->textureContext = (GLNVGtextureContext*)malloc(sizeof(GLNVGtextureContext));
535 memset(gl->textureContext, 0, sizeof(GLNVGtextureContext));
536 gl->textureContext->refCount = 1;
539 int align = 4;
541 // TODO: mediump float may not be enough for GLES2 in iOS.
542 // see the following discussion: https://github.com/memononen/nanovg/issues/46
543 static const char* shaderHeader =
544 #if defined NANOVG_GL2
545 "#define NANOVG_GL2 1\n"
546 #elif defined NANOVG_GL3
547 "#version 150 core\n"
548 "#define NANOVG_GL3 1\n"
549 #elif defined NANOVG_GLES2
550 "#version 100\n"
551 "#define NANOVG_GL2 1\n"
552 #elif defined NANOVG_GLES3
553 "#version 300 es\n"
554 "#define NANOVG_GL3 1\n"
555 #endif
557 #if NANOVG_GL_USE_UNIFORMBUFFER
558 "#define USE_UNIFORMBUFFER 1\n"
559 #else
560 "#define UNIFORMARRAY_SIZE 11\n"
561 #endif
562 "\n";
564 static const char* fillVertShader =
565 "#ifdef NANOVG_GL3\n"
566 " uniform vec2 viewSize;\n"
567 " in vec2 vertex;\n"
568 " in vec2 tcoord;\n"
569 " out vec2 ftcoord;\n"
570 " out vec2 fpos;\n"
571 "#else\n"
572 " uniform vec2 viewSize;\n"
573 " attribute vec2 vertex;\n"
574 " attribute vec2 tcoord;\n"
575 " varying vec2 ftcoord;\n"
576 " varying vec2 fpos;\n"
577 "#endif\n"
578 "void main(void) {\n"
579 " ftcoord = tcoord;\n"
580 " fpos = vertex;\n"
581 " gl_Position = vec4(2.0*vertex.x/viewSize.x - 1.0, 1.0 - 2.0*vertex.y/viewSize.y, 0, 1);\n"
582 "}\n";
584 static const char* fillFragShader =
585 "#ifdef GL_ES\n"
586 "#if defined(GL_FRAGMENT_PRECISION_HIGH) || defined(NANOVG_GL3)\n"
587 " precision highp float;\n"
588 "#else\n"
589 " precision mediump float;\n"
590 "#endif\n"
591 "#endif\n"
592 "#ifdef NANOVG_GL3\n"
593 "#ifdef USE_UNIFORMBUFFER\n"
594 " layout(std140) uniform frag {\n"
595 " mat3 scissorMat;\n"
596 " mat3 paintMat;\n"
597 " vec4 innerCol;\n"
598 " vec4 outerCol;\n"
599 " vec2 scissorExt;\n"
600 " vec2 scissorScale;\n"
601 " vec2 extent;\n"
602 " float radius;\n"
603 " float feather;\n"
604 " float strokeMult;\n"
605 " float strokeThr;\n"
606 " int texType;\n"
607 " int type;\n"
608 " };\n"
609 "#else\n" // NANOVG_GL3 && !USE_UNIFORMBUFFER
610 " uniform vec4 frag[UNIFORMARRAY_SIZE];\n"
611 "#endif\n"
612 " uniform sampler2D tex;\n"
613 " in vec2 ftcoord;\n"
614 " in vec2 fpos;\n"
615 " out vec4 outColor;\n"
616 "#else\n" // !NANOVG_GL3
617 " uniform vec4 frag[UNIFORMARRAY_SIZE];\n"
618 " uniform sampler2D tex;\n"
619 " varying vec2 ftcoord;\n"
620 " varying vec2 fpos;\n"
621 "#endif\n"
622 "#ifndef USE_UNIFORMBUFFER\n"
623 " #define scissorMat mat3(frag[0].xyz, frag[1].xyz, frag[2].xyz)\n"
624 " #define paintMat mat3(frag[3].xyz, frag[4].xyz, frag[5].xyz)\n"
625 " #define innerCol frag[6]\n"
626 " #define outerCol frag[7]\n"
627 " #define scissorExt frag[8].xy\n"
628 " #define scissorScale frag[8].zw\n"
629 " #define extent frag[9].xy\n"
630 " #define radius frag[9].z\n"
631 " #define feather frag[9].w\n"
632 " #define strokeMult frag[10].x\n"
633 " #define strokeThr frag[10].y\n"
634 " #define texType int(frag[10].z)\n"
635 " #define type int(frag[10].w)\n"
636 "#endif\n"
637 "\n"
638 "float sdroundrect(vec2 pt, vec2 ext, float rad) {\n"
639 " vec2 ext2 = ext - vec2(rad,rad);\n"
640 " vec2 d = abs(pt) - ext2;\n"
641 " return min(max(d.x,d.y),0.0) + length(max(d,0.0)) - rad;\n"
642 "}\n"
643 "\n"
644 "// Scissoring\n"
645 "float scissorMask(vec2 p) {\n"
646 " vec2 sc = (abs((scissorMat * vec3(p,1.0)).xy) - scissorExt);\n"
647 " sc = vec2(0.5,0.5) - sc * scissorScale;\n"
648 " return clamp(sc.x,0.0,1.0) * clamp(sc.y,0.0,1.0);\n"
649 "}\n"
650 "#ifdef EDGE_AA\n"
651 "// Stroke - from [0..1] to clipped pyramid, where the slope is 1px.\n"
652 "float strokeMask() {\n"
653 " return min(1.0, (1.0-abs(ftcoord.x*2.0-1.0))*strokeMult) * min(1.0, ftcoord.y);\n"
654 "}\n"
655 "#endif\n"
656 "\n"
657 "void main(void) {\n"
658 " vec4 result;\n"
659 " float scissor = scissorMask(fpos);\n"
660 "#ifdef EDGE_AA\n"
661 " float strokeAlpha = strokeMask();\n"
662 " if (strokeAlpha < strokeThr) discard;\n"
663 "#else\n"
664 " float strokeAlpha = 1.0;\n"
665 "#endif\n"
666 " if (type == 0) { // Gradient\n"
667 " // Calculate gradient color using box gradient\n"
668 " vec2 pt = (paintMat * vec3(fpos,1.0)).xy;\n"
669 " float d = clamp((sdroundrect(pt, extent, radius) + feather*0.5) / feather, 0.0, 1.0);\n"
670 " vec4 color = mix(innerCol,outerCol,d);\n"
671 " // Combine alpha\n"
672 " color *= strokeAlpha * scissor;\n"
673 " result = color;\n"
674 " } else if (type == 1) { // Image\n"
675 " // Calculate color fron texture\n"
676 " vec2 pt = (paintMat * vec3(fpos,1.0)).xy / extent;\n"
677 "#ifdef NANOVG_GL3\n"
678 " vec4 color = texture(tex, pt);\n"
679 "#else\n"
680 " vec4 color = texture2D(tex, pt);\n"
681 "#endif\n"
682 " if (texType == 1) color = vec4(color.xyz*color.w,color.w);"
683 " if (texType == 2) color = vec4(color.x);"
684 " // Apply color tint and alpha.\n"
685 " color *= innerCol;\n"
686 " // Combine alpha\n"
687 " color *= strokeAlpha * scissor;\n"
688 " result = color;\n"
689 " } else if (type == 2) { // Stencil fill\n"
690 " result = vec4(1,1,1,1);\n"
691 " } else if (type == 3) { // Textured tris\n"
692 "#ifdef NANOVG_GL3\n"
693 " vec4 color = texture(tex, ftcoord);\n"
694 "#else\n"
695 " vec4 color = texture2D(tex, ftcoord);\n"
696 "#endif\n"
697 " if (texType == 1) color = vec4(color.xyz*color.w,color.w);"
698 " if (texType == 2) color = vec4(color.x);"
699 " color *= scissor;\n"
700 " result = color * innerCol;\n"
701 " }\n"
702 "#ifdef NANOVG_GL3\n"
703 " outColor = result;\n"
704 "#else\n"
705 " gl_FragColor = result;\n"
706 "#endif\n"
707 "}\n";
709 glnvg__checkError(gl, "init");
711 if (gl->flags & NVG_ANTIALIAS) {
712 if (glnvg__createShader(&gl->shader, "shader", shaderHeader, "#define EDGE_AA 1\n", fillVertShader, fillFragShader) == 0)
713 return 0;
714 } else {
715 if (glnvg__createShader(&gl->shader, "shader", shaderHeader, NULL, fillVertShader, fillFragShader) == 0)
716 return 0;
719 glnvg__checkError(gl, "uniform locations");
720 glnvg__getUniforms(&gl->shader);
722 // Create dynamic vertex array
723 #if defined NANOVG_GL3
724 glGenVertexArrays(1, &gl->vertArr);
725 #endif
726 glGenBuffers(1, &gl->vertBuf);
728 #if NANOVG_GL_USE_UNIFORMBUFFER
729 // Create UBOs
730 glUniformBlockBinding(gl->shader.prog, gl->shader.loc[GLNVG_LOC_FRAG], GLNVG_FRAG_BINDING);
731 glGenBuffers(1, &gl->fragBuf);
732 glGetIntegerv(GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT, &align);
733 #endif
734 gl->fragSize = sizeof(GLNVGfragUniforms) + align - sizeof(GLNVGfragUniforms) % align;
736 // Some platforms does not allow to have samples to unset textures.
737 // Create empty one which is bound when there's no texture specified.
738 gl->dummyTex = glnvg__renderCreateTexture(gl, NVG_TEXTURE_ALPHA, 1, 1, 0, NULL);
740 glnvg__checkError(gl, "create done");
742 glFinish();
744 return 1;
747 static int glnvg__renderCreateTexture(void* uptr, int type, int w, int h, int imageFlags, const unsigned char* data)
749 GLNVGcontext* gl = (GLNVGcontext*)uptr;
750 GLNVGtexture* tex = glnvg__allocTexture(gl);
752 if (tex == NULL) return 0;
754 #ifdef NANOVG_GLES2
755 // Check for non-power of 2.
756 if (glnvg__nearestPow2(w) != (unsigned int)w || glnvg__nearestPow2(h) != (unsigned int)h) {
757 // No repeat
758 if ((imageFlags & NVG_IMAGE_REPEATX) != 0 || (imageFlags & NVG_IMAGE_REPEATY) != 0) {
759 printf("Repeat X/Y is not supported for non power-of-two textures (%d x %d)\n", w, h);
760 imageFlags &= ~(NVG_IMAGE_REPEATX | NVG_IMAGE_REPEATY);
762 // No mips.
763 if (imageFlags & NVG_IMAGE_GENERATE_MIPMAPS) {
764 printf("Mip-maps is not supported for non power-of-two textures (%d x %d)\n", w, h);
765 imageFlags &= ~NVG_IMAGE_GENERATE_MIPMAPS;
768 #endif
770 glGenTextures(1, &tex->tex);
771 tex->width = w;
772 tex->height = h;
773 tex->type = type;
774 tex->flags = imageFlags;
775 glnvg__bindTexture(gl, tex->tex);
777 glPixelStorei(GL_UNPACK_ALIGNMENT,1);
778 #ifndef NANOVG_GLES2
779 glPixelStorei(GL_UNPACK_ROW_LENGTH, tex->width);
780 glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
781 glPixelStorei(GL_UNPACK_SKIP_ROWS, 0);
782 #endif
784 #if defined (NANOVG_GL2)
785 // GL 1.4 and later has support for generating mipmaps using a tex parameter.
786 if (imageFlags & NVG_IMAGE_GENERATE_MIPMAPS) {
787 glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE);
789 #endif
791 switch (type)
793 case NVG_TEXTURE_BGR:
794 #if NANOVG_GLES2
795 // GLES2 cannot handle GL_BGR, do local conversion to GL_RGB
796 tex->data = (uint8_t*)malloc(sizeof(uint8_t) * 3 * w * h);
797 for (uint32_t i=0; i<w*h; ++i)
799 tex->data[i*3+0] = data[i*3+2];
800 tex->data[i*3+1] = data[i*3+1];
801 tex->data[i*3+2] = data[i*3+0];
803 data = tex->data;
804 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
805 #else
806 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0, GL_BGR, GL_UNSIGNED_BYTE, data);
807 #endif
808 break;
809 case NVG_TEXTURE_BGRA:
810 #if NANOVG_GLES2
811 // GLES2 cannot handle GL_BGRA, do local conversion to GL_RGBA
812 tex->data = (uint8_t*)malloc(sizeof(uint8_t) * 4 * w * h);
813 for (uint32_t i=0; i<w*h; ++i)
815 tex->data[i*3+0] = data[i*3+3];
816 tex->data[i*3+1] = data[i*3+2];
817 tex->data[i*3+2] = data[i*3+1];
818 tex->data[i*3+3] = data[i*3+0];
820 data = tex->data;
821 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_BGRA, GL_UNSIGNED_BYTE, data);
822 #else
823 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_BGRA, GL_UNSIGNED_BYTE, data);
824 #endif
825 break;
826 case NVG_TEXTURE_RGB:
827 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
828 break;
829 case NVG_TEXTURE_RGBA:
830 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
831 break;
832 default:
833 #if defined(NANOVG_GLES2) || defined (NANOVG_GL2)
834 glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, w, h, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, data);
835 #elif defined(NANOVG_GLES3)
836 glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, w, h, 0, GL_RED, GL_UNSIGNED_BYTE, data);
837 #else
838 glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, w, h, 0, GL_RED, GL_UNSIGNED_BYTE, data);
839 #endif
840 break;
843 if (imageFlags & NVG_IMAGE_GENERATE_MIPMAPS) {
844 if (imageFlags & NVG_IMAGE_NEAREST) {
845 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST);
846 } else {
847 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
849 } else {
850 if (imageFlags & NVG_IMAGE_NEAREST) {
851 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
852 } else {
853 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
857 if (imageFlags & NVG_IMAGE_NEAREST) {
858 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
859 } else {
860 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
863 if (imageFlags & NVG_IMAGE_REPEATX)
864 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
865 else
866 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
868 if (imageFlags & NVG_IMAGE_REPEATY)
869 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
870 else
871 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
873 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
874 #ifndef NANOVG_GLES2
875 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
876 glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
877 glPixelStorei(GL_UNPACK_SKIP_ROWS, 0);
878 #endif
880 // The new way to build mipmaps on GLES and GL3
881 #if !defined(NANOVG_GL2)
882 if (imageFlags & NVG_IMAGE_GENERATE_MIPMAPS) {
883 glGenerateMipmap(GL_TEXTURE_2D);
885 #endif
887 glnvg__checkError(gl, "create tex");
888 glnvg__bindTexture(gl, 0);
890 return tex->id;
894 static int glnvg__renderDeleteTexture(void* uptr, int image)
896 GLNVGcontext* gl = (GLNVGcontext*)uptr;
897 return glnvg__deleteTexture(gl, image);
900 static int glnvg__renderUpdateTexture(void* uptr, int image, int x, int y, int w, int h, const unsigned char* data)
902 GLNVGcontext* gl = (GLNVGcontext*)uptr;
903 GLNVGtexture* tex = glnvg__findTexture(gl, image);
905 if (tex == NULL) return 0;
906 glnvg__bindTexture(gl, tex->tex);
908 glPixelStorei(GL_UNPACK_ALIGNMENT,1);
910 #ifndef NANOVG_GLES2
911 glPixelStorei(GL_UNPACK_ROW_LENGTH, tex->width);
912 glPixelStorei(GL_UNPACK_SKIP_PIXELS, x);
913 glPixelStorei(GL_UNPACK_SKIP_ROWS, y);
914 #else
915 // No support for all of skip, need to update a whole row at a time.
916 switch (tex->type)
918 case NVG_TEXTURE_BGR:
919 data += y*tex->width*3;
920 break;
921 case NVG_TEXTURE_BGRA:
922 data += y*tex->width*4;
923 break;
924 case NVG_TEXTURE_RGB:
925 data += y*tex->width*3;
926 break;
927 case NVG_TEXTURE_RGBA:
928 data += y*tex->width*4;
929 break;
930 default:
931 data += y*tex->width;
932 break;
934 x = 0;
935 w = tex->width;
936 #endif
938 switch (tex->type)
940 case NVG_TEXTURE_BGR:
941 glTexSubImage2D(GL_TEXTURE_2D, 0, x,y, w,h, GL_BGR, GL_UNSIGNED_BYTE, data);
942 break;
943 case NVG_TEXTURE_BGRA:
944 glTexSubImage2D(GL_TEXTURE_2D, 0, x,y, w,h, GL_BGRA, GL_UNSIGNED_BYTE, data);
945 break;
946 case NVG_TEXTURE_RGB:
947 glTexSubImage2D(GL_TEXTURE_2D, 0, x,y, w,h, GL_RGB, GL_UNSIGNED_BYTE, data);
948 break;
949 case NVG_TEXTURE_RGBA:
950 glTexSubImage2D(GL_TEXTURE_2D, 0, x,y, w,h, GL_RGBA, GL_UNSIGNED_BYTE, data);
951 break;
952 default:
953 #if defined(NANOVG_GLES2) || defined(NANOVG_GL2)
954 glTexSubImage2D(GL_TEXTURE_2D, 0, x,y, w,h, GL_LUMINANCE, GL_UNSIGNED_BYTE, data);
955 #else
956 glTexSubImage2D(GL_TEXTURE_2D, 0, x,y, w,h, GL_RED, GL_UNSIGNED_BYTE, data);
957 #endif
958 break;
961 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
962 #ifndef NANOVG_GLES2
963 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
964 glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
965 glPixelStorei(GL_UNPACK_SKIP_ROWS, 0);
966 #endif
968 glnvg__bindTexture(gl, 0);
970 return 1;
973 static int glnvg__renderGetTextureSize(void* uptr, int image, int* w, int* h)
975 GLNVGcontext* gl = (GLNVGcontext*)uptr;
976 GLNVGtexture* tex = glnvg__findTexture(gl, image);
977 if (tex == NULL) return 0;
978 *w = tex->width;
979 *h = tex->height;
980 return 1;
983 static void glnvg__xformToMat3x4(float* m3, float* t)
985 m3[0] = t[0];
986 m3[1] = t[1];
987 m3[2] = 0.0f;
988 m3[3] = 0.0f;
989 m3[4] = t[2];
990 m3[5] = t[3];
991 m3[6] = 0.0f;
992 m3[7] = 0.0f;
993 m3[8] = t[4];
994 m3[9] = t[5];
995 m3[10] = 1.0f;
996 m3[11] = 0.0f;
999 static NVGcolor glnvg__premulColor(NVGcolor c)
1001 c.r *= c.a;
1002 c.g *= c.a;
1003 c.b *= c.a;
1004 return c;
1007 static int glnvg__convertPaint(GLNVGcontext* gl, GLNVGfragUniforms* frag, NVGpaint* paint,
1008 NVGscissor* scissor, float width, float fringe, float strokeThr)
1010 GLNVGtexture* tex = NULL;
1011 float invxform[6];
1013 memset(frag, 0, sizeof(*frag));
1015 frag->innerCol = glnvg__premulColor(paint->innerColor);
1016 frag->outerCol = glnvg__premulColor(paint->outerColor);
1018 if (scissor->extent[0] < -0.5f || scissor->extent[1] < -0.5f) {
1019 memset(frag->scissorMat, 0, sizeof(frag->scissorMat));
1020 frag->scissorExt[0] = 1.0f;
1021 frag->scissorExt[1] = 1.0f;
1022 frag->scissorScale[0] = 1.0f;
1023 frag->scissorScale[1] = 1.0f;
1024 } else {
1025 nvgTransformInverse(invxform, scissor->xform);
1026 glnvg__xformToMat3x4(frag->scissorMat, invxform);
1027 frag->scissorExt[0] = scissor->extent[0];
1028 frag->scissorExt[1] = scissor->extent[1];
1029 frag->scissorScale[0] = sqrtf(scissor->xform[0]*scissor->xform[0] + scissor->xform[2]*scissor->xform[2]) / fringe;
1030 frag->scissorScale[1] = sqrtf(scissor->xform[1]*scissor->xform[1] + scissor->xform[3]*scissor->xform[3]) / fringe;
1033 memcpy(frag->extent, paint->extent, sizeof(frag->extent));
1034 frag->strokeMult = (width*0.5f + fringe*0.5f) / fringe;
1035 frag->strokeThr = strokeThr;
1037 if (paint->image != 0) {
1038 tex = glnvg__findTexture(gl, paint->image);
1039 if (tex == NULL) return 0;
1040 if ((tex->flags & NVG_IMAGE_FLIPY) != 0) {
1041 float m1[6], m2[6];
1042 nvgTransformTranslate(m1, 0.0f, frag->extent[1] * 0.5f);
1043 nvgTransformMultiply(m1, paint->xform);
1044 nvgTransformScale(m2, 1.0f, -1.0f);
1045 nvgTransformMultiply(m2, m1);
1046 nvgTransformTranslate(m1, 0.0f, -frag->extent[1] * 0.5f);
1047 nvgTransformMultiply(m1, m2);
1048 nvgTransformInverse(invxform, m1);
1049 } else {
1050 nvgTransformInverse(invxform, paint->xform);
1052 frag->type = NSVG_SHADER_FILLIMG;
1054 #if NANOVG_GL_USE_UNIFORMBUFFER
1055 switch (tex->type)
1057 case NVG_TEXTURE_BGR:
1058 case NVG_TEXTURE_BGRA:
1059 case NVG_TEXTURE_RGB:
1060 case NVG_TEXTURE_RGBA:
1061 frag->texType = (tex->flags & NVG_IMAGE_PREMULTIPLIED) ? 0 : 1;
1062 break;
1063 default:
1064 frag->texType = 2;
1065 break;
1067 #else
1068 switch (tex->type)
1070 case NVG_TEXTURE_BGR:
1071 case NVG_TEXTURE_BGRA:
1072 case NVG_TEXTURE_RGB:
1073 case NVG_TEXTURE_RGBA:
1074 frag->texType = (tex->flags & NVG_IMAGE_PREMULTIPLIED) ? 0.0f : 1.0f;
1075 break;
1076 default:
1077 frag->texType = 2.0f;
1078 break;
1080 #endif
1081 // printf("frag->texType = %d\n", frag->texType);
1082 } else {
1083 frag->type = NSVG_SHADER_FILLGRAD;
1084 frag->radius = paint->radius;
1085 frag->feather = paint->feather;
1086 nvgTransformInverse(invxform, paint->xform);
1089 glnvg__xformToMat3x4(frag->paintMat, invxform);
1091 return 1;
1094 static GLNVGfragUniforms* nvg__fragUniformPtr(GLNVGcontext* gl, int i);
1096 static void glnvg__setUniforms(GLNVGcontext* gl, int uniformOffset, int image)
1098 GLNVGtexture* tex = NULL;
1099 #if NANOVG_GL_USE_UNIFORMBUFFER
1100 glBindBufferRange(GL_UNIFORM_BUFFER, GLNVG_FRAG_BINDING, gl->fragBuf, uniformOffset, sizeof(GLNVGfragUniforms));
1101 #else
1102 GLNVGfragUniforms* frag = nvg__fragUniformPtr(gl, uniformOffset);
1103 glUniform4fv(gl->shader.loc[GLNVG_LOC_FRAG], NANOVG_GL_UNIFORMARRAY_SIZE, &(frag->uniformArray[0][0]));
1104 #endif
1106 if (image != 0) {
1107 tex = glnvg__findTexture(gl, image);
1109 // If no image is set, use empty texture
1110 if (tex == NULL) {
1111 tex = glnvg__findTexture(gl, gl->dummyTex);
1113 glnvg__bindTexture(gl, tex != NULL ? tex->tex : 0);
1114 glnvg__checkError(gl, "tex paint tex");
1117 static void glnvg__renderViewport(void* uptr, float width, float height, float devicePixelRatio)
1119 NVG_NOTUSED(devicePixelRatio);
1120 GLNVGcontext* gl = (GLNVGcontext*)uptr;
1121 gl->view[0] = width;
1122 gl->view[1] = height;
1125 static void glnvg__fill(GLNVGcontext* gl, GLNVGcall* call)
1127 GLNVGpath* paths = &gl->paths[call->pathOffset];
1128 int i, npaths = call->pathCount;
1130 // Draw shapes
1131 glEnable(GL_STENCIL_TEST);
1132 glnvg__stencilMask(gl, 0xff);
1133 glnvg__stencilFunc(gl, GL_ALWAYS, 0, 0xff);
1134 glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
1136 // set bindpoint for solid loc
1137 glnvg__setUniforms(gl, call->uniformOffset, 0);
1138 glnvg__checkError(gl, "fill simple");
1140 glStencilOpSeparate(GL_FRONT, GL_KEEP, GL_KEEP, GL_INCR_WRAP);
1141 glStencilOpSeparate(GL_BACK, GL_KEEP, GL_KEEP, GL_DECR_WRAP);
1142 glDisable(GL_CULL_FACE);
1143 for (i = 0; i < npaths; i++)
1144 glDrawArrays(GL_TRIANGLE_FAN, paths[i].fillOffset, paths[i].fillCount);
1145 glEnable(GL_CULL_FACE);
1147 // Draw anti-aliased pixels
1148 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
1150 glnvg__setUniforms(gl, call->uniformOffset + gl->fragSize, call->image);
1151 glnvg__checkError(gl, "fill fill");
1153 if (gl->flags & NVG_ANTIALIAS) {
1154 glnvg__stencilFunc(gl, GL_EQUAL, 0x00, 0xff);
1155 glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
1156 // Draw fringes
1157 for (i = 0; i < npaths; i++)
1158 glDrawArrays(GL_TRIANGLE_STRIP, paths[i].strokeOffset, paths[i].strokeCount);
1161 // Draw fill
1162 glnvg__stencilFunc(gl, GL_NOTEQUAL, 0x0, 0xff);
1163 glStencilOp(GL_ZERO, GL_ZERO, GL_ZERO);
1164 glDrawArrays(GL_TRIANGLE_STRIP, call->triangleOffset, call->triangleCount);
1166 glDisable(GL_STENCIL_TEST);
1169 static void glnvg__convexFill(GLNVGcontext* gl, GLNVGcall* call)
1171 GLNVGpath* paths = &gl->paths[call->pathOffset];
1172 int i, npaths = call->pathCount;
1174 glnvg__setUniforms(gl, call->uniformOffset, call->image);
1175 glnvg__checkError(gl, "convex fill");
1177 for (i = 0; i < npaths; i++) {
1178 glDrawArrays(GL_TRIANGLE_FAN, paths[i].fillOffset, paths[i].fillCount);
1179 // Draw fringes
1180 if (paths[i].strokeCount > 0) {
1181 glDrawArrays(GL_TRIANGLE_STRIP, paths[i].strokeOffset, paths[i].strokeCount);
1186 static void glnvg__stroke(GLNVGcontext* gl, GLNVGcall* call)
1188 GLNVGpath* paths = &gl->paths[call->pathOffset];
1189 int npaths = call->pathCount, i;
1191 if (gl->flags & NVG_STENCIL_STROKES) {
1193 glEnable(GL_STENCIL_TEST);
1194 glnvg__stencilMask(gl, 0xff);
1196 // Fill the stroke base without overlap
1197 glnvg__stencilFunc(gl, GL_EQUAL, 0x0, 0xff);
1198 glStencilOp(GL_KEEP, GL_KEEP, GL_INCR);
1199 glnvg__setUniforms(gl, call->uniformOffset + gl->fragSize, call->image);
1200 glnvg__checkError(gl, "stroke fill 0");
1201 for (i = 0; i < npaths; i++)
1202 glDrawArrays(GL_TRIANGLE_STRIP, paths[i].strokeOffset, paths[i].strokeCount);
1204 // Draw anti-aliased pixels.
1205 glnvg__setUniforms(gl, call->uniformOffset, call->image);
1206 glnvg__stencilFunc(gl, GL_EQUAL, 0x00, 0xff);
1207 glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
1208 for (i = 0; i < npaths; i++)
1209 glDrawArrays(GL_TRIANGLE_STRIP, paths[i].strokeOffset, paths[i].strokeCount);
1211 // Clear stencil buffer.
1212 glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
1213 glnvg__stencilFunc(gl, GL_ALWAYS, 0x0, 0xff);
1214 glStencilOp(GL_ZERO, GL_ZERO, GL_ZERO);
1215 glnvg__checkError(gl, "stroke fill 1");
1216 for (i = 0; i < npaths; i++)
1217 glDrawArrays(GL_TRIANGLE_STRIP, paths[i].strokeOffset, paths[i].strokeCount);
1218 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
1220 glDisable(GL_STENCIL_TEST);
1222 // glnvg__convertPaint(gl, nvg__fragUniformPtr(gl, call->uniformOffset + gl->fragSize), paint, scissor, strokeWidth, fringe, 1.0f - 0.5f/255.0f);
1224 } else {
1225 glnvg__setUniforms(gl, call->uniformOffset, call->image);
1226 glnvg__checkError(gl, "stroke fill");
1227 // Draw Strokes
1228 for (i = 0; i < npaths; i++)
1229 glDrawArrays(GL_TRIANGLE_STRIP, paths[i].strokeOffset, paths[i].strokeCount);
1233 static void glnvg__triangles(GLNVGcontext* gl, GLNVGcall* call)
1235 glnvg__setUniforms(gl, call->uniformOffset, call->image);
1236 glnvg__checkError(gl, "triangles fill");
1238 glDrawArrays(GL_TRIANGLES, call->triangleOffset, call->triangleCount);
1241 static void glnvg__renderCancel(void* uptr) {
1242 GLNVGcontext* gl = (GLNVGcontext*)uptr;
1243 gl->nverts = 0;
1244 gl->npaths = 0;
1245 gl->ncalls = 0;
1246 gl->nuniforms = 0;
1249 static GLenum glnvg_convertBlendFuncFactor(int factor)
1251 if (factor == NVG_ZERO)
1252 return GL_ZERO;
1253 if (factor == NVG_ONE)
1254 return GL_ONE;
1255 if (factor == NVG_SRC_COLOR)
1256 return GL_SRC_COLOR;
1257 if (factor == NVG_ONE_MINUS_SRC_COLOR)
1258 return GL_ONE_MINUS_SRC_COLOR;
1259 if (factor == NVG_DST_COLOR)
1260 return GL_DST_COLOR;
1261 if (factor == NVG_ONE_MINUS_DST_COLOR)
1262 return GL_ONE_MINUS_DST_COLOR;
1263 if (factor == NVG_SRC_ALPHA)
1264 return GL_SRC_ALPHA;
1265 if (factor == NVG_ONE_MINUS_SRC_ALPHA)
1266 return GL_ONE_MINUS_SRC_ALPHA;
1267 if (factor == NVG_DST_ALPHA)
1268 return GL_DST_ALPHA;
1269 if (factor == NVG_ONE_MINUS_DST_ALPHA)
1270 return GL_ONE_MINUS_DST_ALPHA;
1271 if (factor == NVG_SRC_ALPHA_SATURATE)
1272 return GL_SRC_ALPHA_SATURATE;
1273 return GL_INVALID_ENUM;
1276 static GLNVGblend glnvg__blendCompositeOperation(NVGcompositeOperationState op)
1278 GLNVGblend blend;
1279 blend.srcRGB = glnvg_convertBlendFuncFactor(op.srcRGB);
1280 blend.dstRGB = glnvg_convertBlendFuncFactor(op.dstRGB);
1281 blend.srcAlpha = glnvg_convertBlendFuncFactor(op.srcAlpha);
1282 blend.dstAlpha = glnvg_convertBlendFuncFactor(op.dstAlpha);
1283 if (blend.srcRGB == GL_INVALID_ENUM || blend.dstRGB == GL_INVALID_ENUM || blend.srcAlpha == GL_INVALID_ENUM || blend.dstAlpha == GL_INVALID_ENUM)
1285 blend.srcRGB = GL_ONE;
1286 blend.dstRGB = GL_ONE_MINUS_SRC_ALPHA;
1287 blend.srcAlpha = GL_ONE;
1288 blend.dstAlpha = GL_ONE_MINUS_SRC_ALPHA;
1290 return blend;
1293 static void glnvg__renderFlush(void* uptr)
1295 GLNVGcontext* gl = (GLNVGcontext*)uptr;
1296 int i;
1298 if (gl->ncalls > 0) {
1300 // Setup require GL state.
1301 glUseProgram(gl->shader.prog);
1303 glEnable(GL_CULL_FACE);
1304 glCullFace(GL_BACK);
1305 glFrontFace(GL_CCW);
1306 glEnable(GL_BLEND);
1307 glDisable(GL_DEPTH_TEST);
1308 glDisable(GL_SCISSOR_TEST);
1309 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
1310 glStencilMask(0xffffffff);
1311 glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
1312 glStencilFunc(GL_ALWAYS, 0, 0xffffffff);
1313 glActiveTexture(GL_TEXTURE0);
1314 glBindTexture(GL_TEXTURE_2D, 0);
1315 #if NANOVG_GL_USE_STATE_FILTER
1316 gl->boundTexture = 0;
1317 gl->stencilMask = 0xffffffff;
1318 gl->stencilFunc = GL_ALWAYS;
1319 gl->stencilFuncRef = 0;
1320 gl->stencilFuncMask = 0xffffffff;
1321 gl->blendFunc.srcRGB = GL_INVALID_ENUM;
1322 gl->blendFunc.srcAlpha = GL_INVALID_ENUM;
1323 gl->blendFunc.dstRGB = GL_INVALID_ENUM;
1324 gl->blendFunc.dstAlpha = GL_INVALID_ENUM;
1325 #endif
1327 #if NANOVG_GL_USE_UNIFORMBUFFER
1328 // Upload ubo for frag shaders
1329 glBindBuffer(GL_UNIFORM_BUFFER, gl->fragBuf);
1330 glBufferData(GL_UNIFORM_BUFFER, gl->nuniforms * gl->fragSize, gl->uniforms, GL_STREAM_DRAW);
1331 #endif
1333 // Upload vertex data
1334 #if defined NANOVG_GL3
1335 glBindVertexArray(gl->vertArr);
1336 #endif
1337 glBindBuffer(GL_ARRAY_BUFFER, gl->vertBuf);
1338 glBufferData(GL_ARRAY_BUFFER, gl->nverts * sizeof(NVGvertex), gl->verts, GL_STREAM_DRAW);
1339 glEnableVertexAttribArray(0);
1340 glEnableVertexAttribArray(1);
1341 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(NVGvertex), (const GLvoid*)(size_t)0);
1342 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(NVGvertex), (const GLvoid*)(0 + 2*sizeof(float)));
1344 // Set view and texture just once per frame.
1345 glUniform1i(gl->shader.loc[GLNVG_LOC_TEX], 0);
1346 glUniform2fv(gl->shader.loc[GLNVG_LOC_VIEWSIZE], 1, gl->view);
1348 #if NANOVG_GL_USE_UNIFORMBUFFER
1349 glBindBuffer(GL_UNIFORM_BUFFER, gl->fragBuf);
1350 #endif
1352 for (i = 0; i < gl->ncalls; i++) {
1353 GLNVGcall* call = &gl->calls[i];
1354 glnvg__blendFuncSeparate(gl,&call->blendFunc);
1355 if (call->type == GLNVG_FILL)
1356 glnvg__fill(gl, call);
1357 else if (call->type == GLNVG_CONVEXFILL)
1358 glnvg__convexFill(gl, call);
1359 else if (call->type == GLNVG_STROKE)
1360 glnvg__stroke(gl, call);
1361 else if (call->type == GLNVG_TRIANGLES)
1362 glnvg__triangles(gl, call);
1365 glDisableVertexAttribArray(0);
1366 glDisableVertexAttribArray(1);
1367 #if defined NANOVG_GL3
1368 glBindVertexArray(0);
1369 #endif
1370 glDisable(GL_CULL_FACE);
1371 glBindBuffer(GL_ARRAY_BUFFER, 0);
1372 glUseProgram(0);
1373 glnvg__bindTexture(gl, 0);
1376 // Reset calls
1377 gl->nverts = 0;
1378 gl->npaths = 0;
1379 gl->ncalls = 0;
1380 gl->nuniforms = 0;
1383 static int glnvg__maxVertCount(const NVGpath* paths, int npaths)
1385 int i, count = 0;
1386 for (i = 0; i < npaths; i++) {
1387 count += paths[i].nfill;
1388 count += paths[i].nstroke;
1390 return count;
1393 static GLNVGcall* glnvg__allocCall(GLNVGcontext* gl)
1395 GLNVGcall* ret = NULL;
1396 if (gl->ncalls+1 > gl->ccalls) {
1397 GLNVGcall* calls;
1398 int ccalls = glnvg__maxi(gl->ncalls+1, 128) + gl->ccalls/2; // 1.5x Overallocate
1399 calls = (GLNVGcall*)realloc(gl->calls, sizeof(GLNVGcall) * ccalls);
1400 if (calls == NULL) return NULL;
1401 gl->calls = calls;
1402 gl->ccalls = ccalls;
1404 ret = &gl->calls[gl->ncalls++];
1405 memset(ret, 0, sizeof(GLNVGcall));
1406 return ret;
1409 static int glnvg__allocPaths(GLNVGcontext* gl, int n)
1411 int ret = 0;
1412 if (gl->npaths+n > gl->cpaths) {
1413 GLNVGpath* paths;
1414 int cpaths = glnvg__maxi(gl->npaths + n, 128) + gl->cpaths/2; // 1.5x Overallocate
1415 paths = (GLNVGpath*)realloc(gl->paths, sizeof(GLNVGpath) * cpaths);
1416 if (paths == NULL) return -1;
1417 gl->paths = paths;
1418 gl->cpaths = cpaths;
1420 ret = gl->npaths;
1421 gl->npaths += n;
1422 return ret;
1425 static int glnvg__allocVerts(GLNVGcontext* gl, int n)
1427 int ret = 0;
1428 if (gl->nverts+n > gl->cverts) {
1429 NVGvertex* verts;
1430 int cverts = glnvg__maxi(gl->nverts + n, 4096) + gl->cverts/2; // 1.5x Overallocate
1431 verts = (NVGvertex*)realloc(gl->verts, sizeof(NVGvertex) * cverts);
1432 if (verts == NULL) return -1;
1433 gl->verts = verts;
1434 gl->cverts = cverts;
1436 ret = gl->nverts;
1437 gl->nverts += n;
1438 return ret;
1441 static int glnvg__allocFragUniforms(GLNVGcontext* gl, int n)
1443 int ret = 0, structSize = gl->fragSize;
1444 if (gl->nuniforms+n > gl->cuniforms) {
1445 unsigned char* uniforms;
1446 int cuniforms = glnvg__maxi(gl->nuniforms+n, 128) + gl->cuniforms/2; // 1.5x Overallocate
1447 uniforms = (unsigned char*)realloc(gl->uniforms, structSize * cuniforms);
1448 if (uniforms == NULL) return -1;
1449 gl->uniforms = uniforms;
1450 gl->cuniforms = cuniforms;
1452 ret = gl->nuniforms * structSize;
1453 gl->nuniforms += n;
1454 return ret;
1457 static GLNVGfragUniforms* nvg__fragUniformPtr(GLNVGcontext* gl, int i)
1459 return (GLNVGfragUniforms*)&gl->uniforms[i];
1462 static void glnvg__vset(NVGvertex* vtx, float x, float y, float u, float v)
1464 vtx->x = x;
1465 vtx->y = y;
1466 vtx->u = u;
1467 vtx->v = v;
1470 static void glnvg__renderFill(void* uptr, NVGpaint* paint, NVGcompositeOperationState compositeOperation, NVGscissor* scissor, float fringe,
1471 const float* bounds, const NVGpath* paths, int npaths)
1473 GLNVGcontext* gl = (GLNVGcontext*)uptr;
1474 GLNVGcall* call = glnvg__allocCall(gl);
1475 NVGvertex* quad;
1476 GLNVGfragUniforms* frag;
1477 int i, maxverts, offset;
1479 if (call == NULL) return;
1481 call->type = GLNVG_FILL;
1482 call->triangleCount = 4;
1483 call->pathOffset = glnvg__allocPaths(gl, npaths);
1484 if (call->pathOffset == -1) goto error;
1485 call->pathCount = npaths;
1486 call->image = paint->image;
1487 call->blendFunc = glnvg__blendCompositeOperation(compositeOperation);
1489 if (npaths == 1 && paths[0].convex)
1491 call->type = GLNVG_CONVEXFILL;
1492 call->triangleCount = 0; // Bounding box fill quad not needed for convex fill
1495 // Allocate vertices for all the paths.
1496 maxverts = glnvg__maxVertCount(paths, npaths) + call->triangleCount;
1497 offset = glnvg__allocVerts(gl, maxverts);
1498 if (offset == -1) goto error;
1500 for (i = 0; i < npaths; i++) {
1501 GLNVGpath* copy = &gl->paths[call->pathOffset + i];
1502 const NVGpath* path = &paths[i];
1503 memset(copy, 0, sizeof(GLNVGpath));
1504 if (path->nfill > 0) {
1505 copy->fillOffset = offset;
1506 copy->fillCount = path->nfill;
1507 memcpy(&gl->verts[offset], path->fill, sizeof(NVGvertex) * path->nfill);
1508 offset += path->nfill;
1510 if (path->nstroke > 0) {
1511 copy->strokeOffset = offset;
1512 copy->strokeCount = path->nstroke;
1513 memcpy(&gl->verts[offset], path->stroke, sizeof(NVGvertex) * path->nstroke);
1514 offset += path->nstroke;
1518 // Setup uniforms for draw calls
1519 if (call->type == GLNVG_FILL) {
1520 // Quad
1521 call->triangleOffset = offset;
1522 quad = &gl->verts[call->triangleOffset];
1523 glnvg__vset(&quad[0], bounds[2], bounds[3], 0.5f, 1.0f);
1524 glnvg__vset(&quad[1], bounds[2], bounds[1], 0.5f, 1.0f);
1525 glnvg__vset(&quad[2], bounds[0], bounds[3], 0.5f, 1.0f);
1526 glnvg__vset(&quad[3], bounds[0], bounds[1], 0.5f, 1.0f);
1528 call->uniformOffset = glnvg__allocFragUniforms(gl, 2);
1529 if (call->uniformOffset == -1) goto error;
1530 // Simple shader for stencil
1531 frag = nvg__fragUniformPtr(gl, call->uniformOffset);
1532 memset(frag, 0, sizeof(*frag));
1533 frag->strokeThr = -1.0f;
1534 frag->type = NSVG_SHADER_SIMPLE;
1535 // Fill shader
1536 glnvg__convertPaint(gl, nvg__fragUniformPtr(gl, call->uniformOffset + gl->fragSize), paint, scissor, fringe, fringe, -1.0f);
1537 } else {
1538 call->uniformOffset = glnvg__allocFragUniforms(gl, 1);
1539 if (call->uniformOffset == -1) goto error;
1540 // Fill shader
1541 glnvg__convertPaint(gl, nvg__fragUniformPtr(gl, call->uniformOffset), paint, scissor, fringe, fringe, -1.0f);
1544 return;
1546 error:
1547 // We get here if call alloc was ok, but something else is not.
1548 // Roll back the last call to prevent drawing it.
1549 if (gl->ncalls > 0) gl->ncalls--;
1552 static void glnvg__renderStroke(void* uptr, NVGpaint* paint, NVGcompositeOperationState compositeOperation, NVGscissor* scissor, float fringe,
1553 float strokeWidth, const NVGpath* paths, int npaths)
1555 GLNVGcontext* gl = (GLNVGcontext*)uptr;
1556 GLNVGcall* call = glnvg__allocCall(gl);
1557 int i, maxverts, offset;
1559 if (call == NULL) return;
1561 call->type = GLNVG_STROKE;
1562 call->pathOffset = glnvg__allocPaths(gl, npaths);
1563 if (call->pathOffset == -1) goto error;
1564 call->pathCount = npaths;
1565 call->image = paint->image;
1566 call->blendFunc = glnvg__blendCompositeOperation(compositeOperation);
1568 // Allocate vertices for all the paths.
1569 maxverts = glnvg__maxVertCount(paths, npaths);
1570 offset = glnvg__allocVerts(gl, maxverts);
1571 if (offset == -1) goto error;
1573 for (i = 0; i < npaths; i++) {
1574 GLNVGpath* copy = &gl->paths[call->pathOffset + i];
1575 const NVGpath* path = &paths[i];
1576 memset(copy, 0, sizeof(GLNVGpath));
1577 if (path->nstroke) {
1578 copy->strokeOffset = offset;
1579 copy->strokeCount = path->nstroke;
1580 memcpy(&gl->verts[offset], path->stroke, sizeof(NVGvertex) * path->nstroke);
1581 offset += path->nstroke;
1585 if (gl->flags & NVG_STENCIL_STROKES) {
1586 // Fill shader
1587 call->uniformOffset = glnvg__allocFragUniforms(gl, 2);
1588 if (call->uniformOffset == -1) goto error;
1590 glnvg__convertPaint(gl, nvg__fragUniformPtr(gl, call->uniformOffset), paint, scissor, strokeWidth, fringe, -1.0f);
1591 glnvg__convertPaint(gl, nvg__fragUniformPtr(gl, call->uniformOffset + gl->fragSize), paint, scissor, strokeWidth, fringe, 1.0f - 0.5f/255.0f);
1593 } else {
1594 // Fill shader
1595 call->uniformOffset = glnvg__allocFragUniforms(gl, 1);
1596 if (call->uniformOffset == -1) goto error;
1597 glnvg__convertPaint(gl, nvg__fragUniformPtr(gl, call->uniformOffset), paint, scissor, strokeWidth, fringe, -1.0f);
1600 return;
1602 error:
1603 // We get here if call alloc was ok, but something else is not.
1604 // Roll back the last call to prevent drawing it.
1605 if (gl->ncalls > 0) gl->ncalls--;
1608 static void glnvg__renderTriangles(void* uptr, NVGpaint* paint, NVGcompositeOperationState compositeOperation, NVGscissor* scissor,
1609 const NVGvertex* verts, int nverts, float fringe)
1611 GLNVGcontext* gl = (GLNVGcontext*)uptr;
1612 GLNVGcall* call = glnvg__allocCall(gl);
1613 GLNVGfragUniforms* frag;
1615 if (call == NULL) return;
1617 call->type = GLNVG_TRIANGLES;
1618 call->image = paint->image;
1619 call->blendFunc = glnvg__blendCompositeOperation(compositeOperation);
1621 // Allocate vertices for all the paths.
1622 call->triangleOffset = glnvg__allocVerts(gl, nverts);
1623 if (call->triangleOffset == -1) goto error;
1624 call->triangleCount = nverts;
1626 memcpy(&gl->verts[call->triangleOffset], verts, sizeof(NVGvertex) * nverts);
1628 // Fill shader
1629 call->uniformOffset = glnvg__allocFragUniforms(gl, 1);
1630 if (call->uniformOffset == -1) goto error;
1631 frag = nvg__fragUniformPtr(gl, call->uniformOffset);
1632 glnvg__convertPaint(gl, frag, paint, scissor, 1.0f, fringe, -1.0f);
1633 frag->type = NSVG_SHADER_IMG;
1635 return;
1637 error:
1638 // We get here if call alloc was ok, but something else is not.
1639 // Roll back the last call to prevent drawing it.
1640 if (gl->ncalls > 0) gl->ncalls--;
1643 static void glnvg__renderDelete(void* uptr)
1645 GLNVGcontext* gl = (GLNVGcontext*)uptr;
1646 int i;
1647 if (gl == NULL) return;
1649 glnvg__deleteShader(&gl->shader);
1651 #if NANOVG_GL3
1652 #if NANOVG_GL_USE_UNIFORMBUFFER
1653 if (gl->fragBuf != 0)
1654 glDeleteBuffers(1, &gl->fragBuf);
1655 #endif
1656 if (gl->vertArr != 0)
1657 glDeleteVertexArrays(1, &gl->vertArr);
1658 #endif
1659 if (gl->vertBuf != 0)
1660 glDeleteBuffers(1, &gl->vertBuf);
1662 if (gl->textureContext != NULL && --gl->textureContext->refCount == 0) {
1663 for (i = 0; i < gl->textureContext->ntextures; i++) {
1664 if (gl->textureContext->textures[i].tex != 0 && (gl->textureContext->textures[i].flags & NVG_IMAGE_NODELETE) == 0)
1665 glDeleteTextures(1, &gl->textureContext->textures[i].tex);
1667 free(gl->textureContext->textures);
1668 free(gl->textureContext);
1671 free(gl->paths);
1672 free(gl->verts);
1673 free(gl->uniforms);
1674 free(gl->calls);
1676 free(gl);
1680 #if defined NANOVG_GL2
1681 NVGcontext* nvgCreateGL2(int flags)
1682 #elif defined NANOVG_GL3
1683 NVGcontext* nvgCreateGL3(int flags)
1684 #elif defined NANOVG_GLES2
1685 NVGcontext* nvgCreateGLES2(int flags)
1686 #elif defined NANOVG_GLES3
1687 NVGcontext* nvgCreateGLES3(int flags)
1688 #endif
1690 #if defined NANOVG_GL2
1691 return nvgCreateSharedGL2(NULL, flags);
1692 #elif defined NANOVG_GL3
1693 return nvgCreateSharedGL3(NULL, flags);
1694 #elif defined NANOVG_GLES2
1695 return nvgCreateSharedGLES2(NULL, flags);
1696 #elif defined NANOVG_GLES3
1697 return nvgCreateSharedGLES3(NULL, flags);
1698 #endif
1701 // Share the fonts and textures of 'other' if it's non-NULL.
1702 #if defined NANOVG_GL2
1703 NVGcontext* nvgCreateSharedGL2(NVGcontext* other, int flags)
1704 #elif defined NANOVG_GL3
1705 NVGcontext* nvgCreateSharedGL3(NVGcontext* other, int flags)
1706 #elif defined NANOVG_GLES2
1707 NVGcontext* nvgCreateSharedGLES2(NVGcontext* other, int flags)
1708 #elif defined NANOVG_GLES3
1709 NVGcontext* nvgCreateSharedGLES3(NVGcontext* other, int flags)
1710 #endif
1712 NVGparams params;
1713 NVGcontext* ctx = NULL;
1714 GLNVGcontext* gl = (GLNVGcontext*)malloc(sizeof(GLNVGcontext));
1715 if (gl == NULL) goto error;
1716 memset(gl, 0, sizeof(GLNVGcontext));
1718 memset(&params, 0, sizeof(params));
1719 params.renderCreate = glnvg__renderCreate;
1720 params.renderCreateTexture = glnvg__renderCreateTexture;
1721 params.renderDeleteTexture = glnvg__renderDeleteTexture;
1722 params.renderUpdateTexture = glnvg__renderUpdateTexture;
1723 params.renderGetTextureSize = glnvg__renderGetTextureSize;
1724 params.renderViewport = glnvg__renderViewport;
1725 params.renderCancel = glnvg__renderCancel;
1726 params.renderFlush = glnvg__renderFlush;
1727 params.renderFill = glnvg__renderFill;
1728 params.renderStroke = glnvg__renderStroke;
1729 params.renderTriangles = glnvg__renderTriangles;
1730 params.renderDelete = glnvg__renderDelete;
1731 params.userPtr = gl;
1732 params.edgeAntiAlias = flags & NVG_ANTIALIAS ? 1 : 0;
1734 gl->flags = flags;
1736 ctx = nvgCreateInternal(&params, other);
1737 if (ctx == NULL) goto error;
1739 return ctx;
1741 error:
1742 // 'gl' is freed by nvgDeleteInternal.
1743 if (ctx != NULL) nvgDeleteInternal(ctx);
1744 return NULL;
1747 #if defined NANOVG_GL2
1748 void nvgDeleteGL2(NVGcontext* ctx)
1749 #elif defined NANOVG_GL3
1750 void nvgDeleteGL3(NVGcontext* ctx)
1751 #elif defined NANOVG_GLES2
1752 void nvgDeleteGLES2(NVGcontext* ctx)
1753 #elif defined NANOVG_GLES3
1754 void nvgDeleteGLES3(NVGcontext* ctx)
1755 #endif
1757 nvgDeleteInternal(ctx);
1760 #if defined NANOVG_GL2
1761 int nvglCreateImageFromHandleGL2(NVGcontext* ctx, GLuint textureId, int w, int h, int imageFlags)
1762 #elif defined NANOVG_GL3
1763 int nvglCreateImageFromHandleGL3(NVGcontext* ctx, GLuint textureId, int w, int h, int imageFlags)
1764 #elif defined NANOVG_GLES2
1765 int nvglCreateImageFromHandleGLES2(NVGcontext* ctx, GLuint textureId, int w, int h, int imageFlags)
1766 #elif defined NANOVG_GLES3
1767 int nvglCreateImageFromHandleGLES3(NVGcontext* ctx, GLuint textureId, int w, int h, int imageFlags)
1768 #endif
1770 GLNVGcontext* gl = (GLNVGcontext*)nvgInternalParams(ctx)->userPtr;
1771 GLNVGtexture* tex = glnvg__allocTexture(gl);
1773 if (tex == NULL) return 0;
1775 tex->type = NVG_TEXTURE_RGBA;
1776 tex->tex = textureId;
1777 tex->flags = imageFlags;
1778 tex->width = w;
1779 tex->height = h;
1781 return tex->id;
1784 #if defined NANOVG_GL2
1785 GLuint nvglImageHandleGL2(NVGcontext* ctx, int image)
1786 #elif defined NANOVG_GL3
1787 GLuint nvglImageHandleGL3(NVGcontext* ctx, int image)
1788 #elif defined NANOVG_GLES2
1789 GLuint nvglImageHandleGLES2(NVGcontext* ctx, int image)
1790 #elif defined NANOVG_GLES3
1791 GLuint nvglImageHandleGLES3(NVGcontext* ctx, int image)
1792 #endif
1794 GLNVGcontext* gl = (GLNVGcontext*)nvgInternalParams(ctx)->userPtr;
1795 GLNVGtexture* tex = glnvg__findTexture(gl, image);
1796 return tex->tex;
1799 #endif /* NANOVG_GL_IMPLEMENTATION */