2 // Copyright (c) 2009-2013 Mikko Mononen memon@inside.org
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.
28 // Flag indicating if geometry based anti-aliasing is used (may not be needed when using MSAA).
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.
37 #if defined NANOVG_GL2_IMPLEMENTATION
39 # define NANOVG_GL_IMPLEMENTATION 1
40 #elif defined NANOVG_GL3_IMPLEMENTATION
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
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
);
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
);
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
);
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
);
101 // These are additional flags on top of NVGimageFlags.
102 enum NVGimageFlagsGL
{
103 NVG_IMAGE_NODELETE
= 1<<16, // Do not delete GL texture handle.
110 #endif /* NANOVG_GL_H */
112 #ifdef NANOVG_GL_IMPLEMENTATION
120 enum GLNVGuniformLoc
{
127 enum GLNVGshaderType
{
128 NSVG_SHADER_FILLGRAD
,
134 #if NANOVG_GL_USE_UNIFORMBUFFER
135 enum GLNVGuniformBindings
{
136 GLNVG_FRAG_BINDING
= 0,
144 GLint loc
[GLNVG_MAX_LOCS
];
146 typedef struct GLNVGshader GLNVGshader
;
148 struct GLNVGtexture
{
154 #if defined NANOVG_GLES2
158 typedef struct GLNVGtexture GLNVGtexture
;
167 typedef struct GLNVGblend GLNVGblend
;
185 GLNVGblend blendFunc
;
187 typedef struct GLNVGcall GLNVGcall
;
195 typedef struct GLNVGpath GLNVGpath
;
197 struct GLNVGfragUniforms
{
198 #if NANOVG_GL_USE_UNIFORMBUFFER
199 float scissorMat
[12]; // matrices are actually 3 vec4s
201 struct NVGcolor innerCol
;
202 struct NVGcolor outerCol
;
204 float scissorScale
[2];
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
218 float scissorMat
[12]; // matrices are actually 3 vec4s
220 struct NVGcolor innerCol
;
221 struct NVGcolor outerCol
;
223 float scissorScale
[2];
232 float uniformArray
[NANOVG_GL_UNIFORMARRAY_SIZE
][4];
236 typedef struct GLNVGfragUniforms GLNVGfragUniforms
;
238 struct GLNVGtextureContext
{ // Textures; shared between shared NanoVG contexts.
240 GLNVGtexture
* textures
;
245 typedef struct GLNVGtextureContext GLNVGtextureContext
;
247 struct GLNVGcontext
{
249 GLNVGtextureContext
* textureContext
;
252 #if defined NANOVG_GL3
255 #if NANOVG_GL_USE_UNIFORMBUFFER
268 struct NVGvertex
* verts
;
271 unsigned char* uniforms
;
276 #if NANOVG_GL_USE_STATE_FILTER
280 GLint stencilFuncRef
;
281 GLuint stencilFuncMask
;
282 GLNVGblend blendFunc
;
287 typedef struct GLNVGcontext GLNVGcontext
;
289 static int glnvg__maxi(int a
, int b
) { return a
> b
? a
: b
; }
292 static unsigned int glnvg__nearestPow2(unsigned int num
)
294 unsigned n
= num
> 0 ? num
- 1 : 0;
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
);
313 glBindTexture(GL_TEXTURE_2D
, tex
);
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
;
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
);
342 glStencilFunc(func
, ref
, mask
);
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
);
357 glBlendFuncSeparate(blend
->srcRGB
, blend
->dstRGB
, blend
->srcAlpha
,blend
->dstAlpha
);
361 static GLNVGtexture
* glnvg__allocTexture(GLNVGcontext
* gl
)
363 GLNVGtexture
* tex
= NULL
;
366 for (i
= 0; i
< gl
->textureContext
->ntextures
; i
++) {
367 if (gl
->textureContext
->textures
[i
].id
== 0) {
368 tex
= &gl
->textureContext
->textures
[i
];
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
;
390 static GLNVGtexture
* glnvg__findTexture(GLNVGcontext
* gl
, int id
)
393 for (i
= 0; i
< gl
->textureContext
->ntextures
; i
++)
394 if (gl
->textureContext
->textures
[i
].id
== id
)
395 return &gl
->textureContext
->textures
[i
];
399 static int glnvg__deleteTexture(GLNVGcontext
* gl
, int id
)
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
);
411 memset(&gl
->textureContext
->textures
[i
], 0, sizeof(gl
->textureContext
->textures
[i
]));
418 static void glnvg__dumpShaderError(GLuint shader
, const char* name
, const char* type
)
422 glGetShaderInfoLog(shader
, 512, &len
, str
);
423 if (len
> 512) len
= 512;
425 printf("Shader %s/%s error:\n%s\n", name
, type
, str
);
428 static void glnvg__dumpProgramError(GLuint prog
, const char* name
)
432 glGetProgramInfoLog(prog
, 512, &len
, str
);
433 if (len
> 512) len
= 512;
435 printf("Program %s error:\n%s\n", name
, str
);
438 static void glnvg__checkError(GLNVGcontext
* gl
, const char* str
)
441 if ((gl
->flags
& NVG_DEBUG
) == 0) return;
443 if (err
!= GL_NO_ERROR
) {
444 printf("Error %08x after %s\n", err
, str
);
449 static int glnvg__createShader(GLNVGshader
* shader
, const char* name
, const char* header
, const char* opts
, const char* vshader
, const char* fshader
)
452 GLuint prog
, vert
, frag
;
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
);
463 glShaderSource(vert
, 3, str
, 0);
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");
474 glCompileShader(frag
);
475 glGetShaderiv(frag
, GL_COMPILE_STATUS
, &status
);
476 if (status
!= GL_TRUE
) {
477 glnvg__dumpShaderError(frag
, name
, "frag");
481 glAttachShader(prog
, vert
);
482 glAttachShader(prog
, frag
);
484 glBindAttribLocation(prog
, 0, "vertex");
485 glBindAttribLocation(prog
, 1, "tcoord");
488 glGetProgramiv(prog
, GL_LINK_STATUS
, &status
);
489 if (status
!= GL_TRUE
) {
490 glnvg__dumpProgramError(prog
, name
);
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");
519 shader
->loc
[GLNVG_LOC_FRAG
] = glGetUniformLocation(shader
->prog
, "frag");
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
;
530 GLNVGcontext
* other
= (GLNVGcontext
*)otherUptr
;
531 gl
->textureContext
= other
->textureContext
;
532 gl
->textureContext
->refCount
++;
534 gl
->textureContext
= (GLNVGtextureContext
*)malloc(sizeof(GLNVGtextureContext
));
535 memset(gl
->textureContext
, 0, sizeof(GLNVGtextureContext
));
536 gl
->textureContext
->refCount
= 1;
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
551 "#define NANOVG_GL2 1\n"
552 #elif defined NANOVG_GLES3
554 "#define NANOVG_GL3 1\n"
557 #if NANOVG_GL_USE_UNIFORMBUFFER
558 "#define USE_UNIFORMBUFFER 1\n"
560 "#define UNIFORMARRAY_SIZE 11\n"
564 static const char* fillVertShader
=
565 "#ifdef NANOVG_GL3\n"
566 " uniform vec2 viewSize;\n"
569 " out vec2 ftcoord;\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"
578 "void main(void) {\n"
579 " ftcoord = tcoord;\n"
581 " gl_Position = vec4(2.0*vertex.x/viewSize.x - 1.0, 1.0 - 2.0*vertex.y/viewSize.y, 0, 1);\n"
584 static const char* fillFragShader
=
586 "#if defined(GL_FRAGMENT_PRECISION_HIGH) || defined(NANOVG_GL3)\n"
587 " precision highp float;\n"
589 " precision mediump float;\n"
592 "#ifdef NANOVG_GL3\n"
593 "#ifdef USE_UNIFORMBUFFER\n"
594 " layout(std140) uniform frag {\n"
595 " mat3 scissorMat;\n"
599 " vec2 scissorExt;\n"
600 " vec2 scissorScale;\n"
604 " float strokeMult;\n"
605 " float strokeThr;\n"
609 "#else\n" // NANOVG_GL3 && !USE_UNIFORMBUFFER
610 " uniform vec4 frag[UNIFORMARRAY_SIZE];\n"
612 " uniform sampler2D tex;\n"
613 " in vec2 ftcoord;\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"
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"
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"
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"
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"
657 "void main(void) {\n"
659 " float scissor = scissorMask(fpos);\n"
661 " float strokeAlpha = strokeMask();\n"
662 " if (strokeAlpha < strokeThr) discard;\n"
664 " float strokeAlpha = 1.0;\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"
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"
680 " vec4 color = texture2D(tex, pt);\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"
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"
695 " vec4 color = texture2D(tex, ftcoord);\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"
702 "#ifdef NANOVG_GL3\n"
703 " outColor = result;\n"
705 " gl_FragColor = result;\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)
715 if (glnvg__createShader(&gl
->shader
, "shader", shaderHeader
, NULL
, fillVertShader
, fillFragShader
) == 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
);
726 glGenBuffers(1, &gl
->vertBuf
);
728 #if NANOVG_GL_USE_UNIFORMBUFFER
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
);
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");
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;
755 // Check for non-power of 2.
756 if (glnvg__nearestPow2(w
) != (unsigned int)w
|| glnvg__nearestPow2(h
) != (unsigned int)h
) {
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
);
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
;
770 glGenTextures(1, &tex
->tex
);
774 tex
->flags
= imageFlags
;
775 glnvg__bindTexture(gl
, tex
->tex
);
777 glPixelStorei(GL_UNPACK_ALIGNMENT
,1);
779 glPixelStorei(GL_UNPACK_ROW_LENGTH
, tex
->width
);
780 glPixelStorei(GL_UNPACK_SKIP_PIXELS
, 0);
781 glPixelStorei(GL_UNPACK_SKIP_ROWS
, 0);
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
);
793 case NVG_TEXTURE_BGR
:
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];
804 glTexImage2D(GL_TEXTURE_2D
, 0, GL_RGB
, w
, h
, 0, GL_RGB
, GL_UNSIGNED_BYTE
, data
);
806 glTexImage2D(GL_TEXTURE_2D
, 0, GL_RGB
, w
, h
, 0, GL_BGR
, GL_UNSIGNED_BYTE
, data
);
809 case NVG_TEXTURE_BGRA
:
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];
821 glTexImage2D(GL_TEXTURE_2D
, 0, GL_RGBA
, w
, h
, 0, GL_BGRA
, GL_UNSIGNED_BYTE
, data
);
823 glTexImage2D(GL_TEXTURE_2D
, 0, GL_RGBA
, w
, h
, 0, GL_BGRA
, GL_UNSIGNED_BYTE
, data
);
826 case NVG_TEXTURE_RGB
:
827 glTexImage2D(GL_TEXTURE_2D
, 0, GL_RGBA
, w
, h
, 0, GL_RGB
, GL_UNSIGNED_BYTE
, data
);
829 case NVG_TEXTURE_RGBA
:
830 glTexImage2D(GL_TEXTURE_2D
, 0, GL_RGBA
, w
, h
, 0, GL_RGBA
, GL_UNSIGNED_BYTE
, data
);
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
);
838 glTexImage2D(GL_TEXTURE_2D
, 0, GL_RED
, w
, h
, 0, GL_RED
, GL_UNSIGNED_BYTE
, data
);
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
);
847 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_LINEAR_MIPMAP_LINEAR
);
850 if (imageFlags
& NVG_IMAGE_NEAREST
) {
851 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
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
);
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
);
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
);
871 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
873 glPixelStorei(GL_UNPACK_ALIGNMENT
, 4);
875 glPixelStorei(GL_UNPACK_ROW_LENGTH
, 0);
876 glPixelStorei(GL_UNPACK_SKIP_PIXELS
, 0);
877 glPixelStorei(GL_UNPACK_SKIP_ROWS
, 0);
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
);
887 glnvg__checkError(gl
, "create tex");
888 glnvg__bindTexture(gl
, 0);
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);
911 glPixelStorei(GL_UNPACK_ROW_LENGTH
, tex
->width
);
912 glPixelStorei(GL_UNPACK_SKIP_PIXELS
, x
);
913 glPixelStorei(GL_UNPACK_SKIP_ROWS
, y
);
915 // No support for all of skip, need to update a whole row at a time.
918 case NVG_TEXTURE_BGR
:
919 data
+= y
*tex
->width
*3;
921 case NVG_TEXTURE_BGRA
:
922 data
+= y
*tex
->width
*4;
924 case NVG_TEXTURE_RGB
:
925 data
+= y
*tex
->width
*3;
927 case NVG_TEXTURE_RGBA
:
928 data
+= y
*tex
->width
*4;
931 data
+= y
*tex
->width
;
940 case NVG_TEXTURE_BGR
:
941 glTexSubImage2D(GL_TEXTURE_2D
, 0, x
,y
, w
,h
, GL_BGR
, GL_UNSIGNED_BYTE
, data
);
943 case NVG_TEXTURE_BGRA
:
944 glTexSubImage2D(GL_TEXTURE_2D
, 0, x
,y
, w
,h
, GL_BGRA
, GL_UNSIGNED_BYTE
, data
);
946 case NVG_TEXTURE_RGB
:
947 glTexSubImage2D(GL_TEXTURE_2D
, 0, x
,y
, w
,h
, GL_RGB
, GL_UNSIGNED_BYTE
, data
);
949 case NVG_TEXTURE_RGBA
:
950 glTexSubImage2D(GL_TEXTURE_2D
, 0, x
,y
, w
,h
, GL_RGBA
, GL_UNSIGNED_BYTE
, data
);
953 #if defined(NANOVG_GLES2) || defined(NANOVG_GL2)
954 glTexSubImage2D(GL_TEXTURE_2D
, 0, x
,y
, w
,h
, GL_LUMINANCE
, GL_UNSIGNED_BYTE
, data
);
956 glTexSubImage2D(GL_TEXTURE_2D
, 0, x
,y
, w
,h
, GL_RED
, GL_UNSIGNED_BYTE
, data
);
961 glPixelStorei(GL_UNPACK_ALIGNMENT
, 4);
963 glPixelStorei(GL_UNPACK_ROW_LENGTH
, 0);
964 glPixelStorei(GL_UNPACK_SKIP_PIXELS
, 0);
965 glPixelStorei(GL_UNPACK_SKIP_ROWS
, 0);
968 glnvg__bindTexture(gl
, 0);
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;
983 static void glnvg__xformToMat3x4(float* m3
, float* t
)
999 static NVGcolor
glnvg__premulColor(NVGcolor 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
;
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
;
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) {
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
);
1050 nvgTransformInverse(invxform
, paint
->xform
);
1052 frag
->type
= NSVG_SHADER_FILLIMG
;
1054 #if NANOVG_GL_USE_UNIFORMBUFFER
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;
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
;
1077 frag
->texType
= 2.0f
;
1081 // printf("frag->texType = %d\n", frag->texType);
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
);
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
));
1102 GLNVGfragUniforms
* frag
= nvg__fragUniformPtr(gl
, uniformOffset
);
1103 glUniform4fv(gl
->shader
.loc
[GLNVG_LOC_FRAG
], NANOVG_GL_UNIFORMARRAY_SIZE
, &(frag
->uniformArray
[0][0]));
1107 tex
= glnvg__findTexture(gl
, image
);
1109 // If no image is set, use empty texture
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
;
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
);
1157 for (i
= 0; i
< npaths
; i
++)
1158 glDrawArrays(GL_TRIANGLE_STRIP
, paths
[i
].strokeOffset
, paths
[i
].strokeCount
);
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
);
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);
1225 glnvg__setUniforms(gl
, call
->uniformOffset
, call
->image
);
1226 glnvg__checkError(gl
, "stroke fill");
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
;
1249 static GLenum
glnvg_convertBlendFuncFactor(int factor
)
1251 if (factor
== NVG_ZERO
)
1253 if (factor
== NVG_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
)
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
;
1293 static void glnvg__renderFlush(void* uptr
)
1295 GLNVGcontext
* gl
= (GLNVGcontext
*)uptr
;
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
);
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
;
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
);
1333 // Upload vertex data
1334 #if defined NANOVG_GL3
1335 glBindVertexArray(gl
->vertArr
);
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
);
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);
1370 glDisable(GL_CULL_FACE
);
1371 glBindBuffer(GL_ARRAY_BUFFER
, 0);
1373 glnvg__bindTexture(gl
, 0);
1383 static int glnvg__maxVertCount(const NVGpath
* paths
, int npaths
)
1386 for (i
= 0; i
< npaths
; i
++) {
1387 count
+= paths
[i
].nfill
;
1388 count
+= paths
[i
].nstroke
;
1393 static GLNVGcall
* glnvg__allocCall(GLNVGcontext
* gl
)
1395 GLNVGcall
* ret
= NULL
;
1396 if (gl
->ncalls
+1 > gl
->ccalls
) {
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
;
1402 gl
->ccalls
= ccalls
;
1404 ret
= &gl
->calls
[gl
->ncalls
++];
1405 memset(ret
, 0, sizeof(GLNVGcall
));
1409 static int glnvg__allocPaths(GLNVGcontext
* gl
, int n
)
1412 if (gl
->npaths
+n
> gl
->cpaths
) {
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;
1418 gl
->cpaths
= cpaths
;
1425 static int glnvg__allocVerts(GLNVGcontext
* gl
, int n
)
1428 if (gl
->nverts
+n
> gl
->cverts
) {
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;
1434 gl
->cverts
= cverts
;
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
;
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
)
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
);
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
) {
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
;
1536 glnvg__convertPaint(gl
, nvg__fragUniformPtr(gl
, call
->uniformOffset
+ gl
->fragSize
), paint
, scissor
, fringe
, fringe
, -1.0f
);
1538 call
->uniformOffset
= glnvg__allocFragUniforms(gl
, 1);
1539 if (call
->uniformOffset
== -1) goto error
;
1541 glnvg__convertPaint(gl
, nvg__fragUniformPtr(gl
, call
->uniformOffset
), paint
, scissor
, fringe
, fringe
, -1.0f
);
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
) {
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
);
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
);
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
);
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
;
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
;
1647 if (gl
== NULL
) return;
1649 glnvg__deleteShader(&gl
->shader
);
1652 #if NANOVG_GL_USE_UNIFORMBUFFER
1653 if (gl
->fragBuf
!= 0)
1654 glDeleteBuffers(1, &gl
->fragBuf
);
1656 if (gl
->vertArr
!= 0)
1657 glDeleteVertexArrays(1, &gl
->vertArr
);
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
);
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
)
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
);
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
)
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(¶ms
, 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;
1736 ctx
= nvgCreateInternal(¶ms
, other
);
1737 if (ctx
== NULL
) goto error
;
1742 // 'gl' is freed by nvgDeleteInternal.
1743 if (ctx
!= NULL
) nvgDeleteInternal(ctx
);
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
)
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
)
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
;
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
)
1794 GLNVGcontext
* gl
= (GLNVGcontext
*)nvgInternalParams(ctx
)->userPtr
;
1795 GLNVGtexture
* tex
= glnvg__findTexture(gl
, image
);
1799 #endif /* NANOVG_GL_IMPLEMENTATION */