*/}}
1
0

nanovg_gl.h 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660
  1. //
  2. // Copyright (c) 2009-2013 Mikko Mononen memon@inside.org
  3. //
  4. // This software is provided 'as-is', without any express or implied
  5. // warranty. In no event will the authors be held liable for any damages
  6. // arising from the use of this software.
  7. // Permission is granted to anyone to use this software for any purpose,
  8. // including commercial applications, and to alter it and redistribute it
  9. // freely, subject to the following restrictions:
  10. // 1. The origin of this software must not be misrepresented; you must not
  11. // claim that you wrote the original software. If you use this software
  12. // in a product, an acknowledgment in the product documentation would be
  13. // appreciated but is not required.
  14. // 2. Altered source versions must be plainly marked as such, and must not be
  15. // misrepresented as being the original software.
  16. // 3. This notice may not be removed or altered from any source distribution.
  17. //
  18. #ifndef NANOVG_GL_H
  19. #define NANOVG_GL_H
  20. #ifdef __cplusplus
  21. extern "C" {
  22. #endif
  23. // Create flags
  24. enum NVGcreateFlags {
  25. // Flag indicating if geometry based anti-aliasing is used (may not be needed when using MSAA).
  26. NVG_ANTIALIAS = 1<<0,
  27. // Flag indicating if strokes should be drawn using stencil buffer. The rendering will be a little
  28. // slower, but path overlaps (i.e. self-intersecting or sharp turns) will be drawn just once.
  29. NVG_STENCIL_STROKES = 1<<1,
  30. // Flag indicating that additional debug checks are done.
  31. NVG_DEBUG = 1<<2,
  32. };
  33. #if defined NANOVG_GL2_IMPLEMENTATION
  34. # define NANOVG_GL2 1
  35. # define NANOVG_GL_IMPLEMENTATION 1
  36. #elif defined NANOVG_GL3_IMPLEMENTATION
  37. # define NANOVG_GL3 1
  38. # define NANOVG_GL_IMPLEMENTATION 1
  39. # define NANOVG_GL_USE_UNIFORMBUFFER 1
  40. #elif defined NANOVG_GLES2_IMPLEMENTATION
  41. # define NANOVG_GLES2 1
  42. # define NANOVG_GL_IMPLEMENTATION 1
  43. #elif defined NANOVG_GLES3_IMPLEMENTATION
  44. # define NANOVG_GLES3 1
  45. # define NANOVG_GL_IMPLEMENTATION 1
  46. #endif
  47. #define NANOVG_GL_USE_STATE_FILTER (1)
  48. // Creates NanoVG contexts for different OpenGL (ES) versions.
  49. // Flags should be combination of the create flags above.
  50. #if defined NANOVG_GL2
  51. NVGcontext* nvgCreateGL2(int flags);
  52. void nvgDeleteGL2(NVGcontext* ctx);
  53. int nvglCreateImageFromHandleGL2(NVGcontext* ctx, GLuint textureId, int w, int h, int flags);
  54. GLuint nvglImageHandleGL2(NVGcontext* ctx, int image);
  55. #endif
  56. #if defined NANOVG_GL3
  57. NVGcontext* nvgCreateGL3(int flags);
  58. void nvgDeleteGL3(NVGcontext* ctx);
  59. int nvglCreateImageFromHandleGL3(NVGcontext* ctx, GLuint textureId, int w, int h, int flags);
  60. GLuint nvglImageHandleGL3(NVGcontext* ctx, int image);
  61. #endif
  62. #if defined NANOVG_GLES2
  63. NVGcontext* nvgCreateGLES2(int flags);
  64. void nvgDeleteGLES2(NVGcontext* ctx);
  65. int nvglCreateImageFromHandleGLES2(NVGcontext* ctx, GLuint textureId, int w, int h, int flags);
  66. GLuint nvglImageHandleGLES2(NVGcontext* ctx, int image);
  67. #endif
  68. #if defined NANOVG_GLES3
  69. NVGcontext* nvgCreateGLES3(int flags);
  70. void nvgDeleteGLES3(NVGcontext* ctx);
  71. int nvglCreateImageFromHandleGLES3(NVGcontext* ctx, GLuint textureId, int w, int h, int flags);
  72. GLuint nvglImageHandleGLES3(NVGcontext* ctx, int image);
  73. #endif
  74. // These are additional flags on top of NVGimageFlags.
  75. enum NVGimageFlagsGL {
  76. NVG_IMAGE_NODELETE = 1<<16, // Do not delete GL texture handle.
  77. };
  78. #ifdef __cplusplus
  79. }
  80. #endif
  81. #endif /* NANOVG_GL_H */
  82. #ifdef NANOVG_GL_IMPLEMENTATION
  83. #include <stdlib.h>
  84. #include <stdio.h>
  85. #include <string.h>
  86. #include <math.h>
  87. #include "nanovg.h"
  88. enum GLNVGuniformLoc {
  89. GLNVG_LOC_VIEWSIZE,
  90. GLNVG_LOC_TEX,
  91. GLNVG_LOC_FRAG,
  92. GLNVG_MAX_LOCS
  93. };
  94. enum GLNVGshaderType {
  95. NSVG_SHADER_FILLGRAD,
  96. NSVG_SHADER_FILLIMG,
  97. NSVG_SHADER_SIMPLE,
  98. NSVG_SHADER_IMG
  99. };
  100. #if NANOVG_GL_USE_UNIFORMBUFFER
  101. enum GLNVGuniformBindings {
  102. GLNVG_FRAG_BINDING = 0,
  103. };
  104. #endif
  105. struct GLNVGshader {
  106. GLuint prog;
  107. GLuint frag;
  108. GLuint vert;
  109. GLint loc[GLNVG_MAX_LOCS];
  110. };
  111. typedef struct GLNVGshader GLNVGshader;
  112. struct GLNVGtexture {
  113. int id;
  114. GLuint tex;
  115. int width, height;
  116. int type;
  117. int flags;
  118. };
  119. typedef struct GLNVGtexture GLNVGtexture;
  120. struct GLNVGblend
  121. {
  122. GLenum srcRGB;
  123. GLenum dstRGB;
  124. GLenum srcAlpha;
  125. GLenum dstAlpha;
  126. };
  127. typedef struct GLNVGblend GLNVGblend;
  128. enum GLNVGcallType {
  129. GLNVG_NONE = 0,
  130. GLNVG_FILL,
  131. GLNVG_CONVEXFILL,
  132. GLNVG_STROKE,
  133. GLNVG_TRIANGLES,
  134. };
  135. struct GLNVGcall {
  136. int type;
  137. int image;
  138. int pathOffset;
  139. int pathCount;
  140. int triangleOffset;
  141. int triangleCount;
  142. int uniformOffset;
  143. GLNVGblend blendFunc;
  144. };
  145. typedef struct GLNVGcall GLNVGcall;
  146. struct GLNVGpath {
  147. int fillOffset;
  148. int fillCount;
  149. int strokeOffset;
  150. int strokeCount;
  151. };
  152. typedef struct GLNVGpath GLNVGpath;
  153. struct GLNVGfragUniforms {
  154. #if NANOVG_GL_USE_UNIFORMBUFFER
  155. float scissorMat[12]; // matrices are actually 3 vec4s
  156. float paintMat[12];
  157. struct NVGcolor innerCol;
  158. struct NVGcolor outerCol;
  159. float scissorExt[2];
  160. float scissorScale[2];
  161. float extent[2];
  162. float radius;
  163. float feather;
  164. float strokeMult;
  165. float strokeThr;
  166. int texType;
  167. int type;
  168. #else
  169. // note: after modifying layout or size of uniform array,
  170. // don't forget to also update the fragment shader source!
  171. #define NANOVG_GL_UNIFORMARRAY_SIZE 11
  172. union {
  173. struct {
  174. float scissorMat[12]; // matrices are actually 3 vec4s
  175. float paintMat[12];
  176. struct NVGcolor innerCol;
  177. struct NVGcolor outerCol;
  178. float scissorExt[2];
  179. float scissorScale[2];
  180. float extent[2];
  181. float radius;
  182. float feather;
  183. float strokeMult;
  184. float strokeThr;
  185. float texType;
  186. float type;
  187. };
  188. float uniformArray[NANOVG_GL_UNIFORMARRAY_SIZE][4];
  189. };
  190. #endif
  191. };
  192. typedef struct GLNVGfragUniforms GLNVGfragUniforms;
  193. struct GLNVGcontext {
  194. GLNVGshader shader;
  195. GLNVGtexture* textures;
  196. float view[2];
  197. int ntextures;
  198. int ctextures;
  199. int textureId;
  200. GLuint vertBuf;
  201. #if defined NANOVG_GL3
  202. GLuint vertArr;
  203. #endif
  204. #if NANOVG_GL_USE_UNIFORMBUFFER
  205. GLuint fragBuf;
  206. #endif
  207. int fragSize;
  208. int flags;
  209. // Per frame buffers
  210. GLNVGcall* calls;
  211. int ccalls;
  212. int ncalls;
  213. GLNVGpath* paths;
  214. int cpaths;
  215. int npaths;
  216. struct NVGvertex* verts;
  217. int cverts;
  218. int nverts;
  219. unsigned char* uniforms;
  220. int cuniforms;
  221. int nuniforms;
  222. // cached state
  223. #if NANOVG_GL_USE_STATE_FILTER
  224. GLuint boundTexture;
  225. GLuint stencilMask;
  226. GLenum stencilFunc;
  227. GLint stencilFuncRef;
  228. GLuint stencilFuncMask;
  229. GLNVGblend blendFunc;
  230. #endif
  231. int dummyTex;
  232. };
  233. typedef struct GLNVGcontext GLNVGcontext;
  234. static int glnvg__maxi(int a, int b) { return a > b ? a : b; }
  235. #ifdef NANOVG_GLES2
  236. static unsigned int glnvg__nearestPow2(unsigned int num)
  237. {
  238. unsigned n = num > 0 ? num - 1 : 0;
  239. n |= n >> 1;
  240. n |= n >> 2;
  241. n |= n >> 4;
  242. n |= n >> 8;
  243. n |= n >> 16;
  244. n++;
  245. return n;
  246. }
  247. #endif
  248. static void glnvg__bindTexture(GLNVGcontext* gl, GLuint tex)
  249. {
  250. #if NANOVG_GL_USE_STATE_FILTER
  251. if (gl->boundTexture != tex) {
  252. gl->boundTexture = tex;
  253. glBindTexture(GL_TEXTURE_2D, tex);
  254. }
  255. #else
  256. glBindTexture(GL_TEXTURE_2D, tex);
  257. #endif
  258. }
  259. static void glnvg__stencilMask(GLNVGcontext* gl, GLuint mask)
  260. {
  261. #if NANOVG_GL_USE_STATE_FILTER
  262. if (gl->stencilMask != mask) {
  263. gl->stencilMask = mask;
  264. glStencilMask(mask);
  265. }
  266. #else
  267. glStencilMask(mask);
  268. #endif
  269. }
  270. static void glnvg__stencilFunc(GLNVGcontext* gl, GLenum func, GLint ref, GLuint mask)
  271. {
  272. #if NANOVG_GL_USE_STATE_FILTER
  273. if ((gl->stencilFunc != func) ||
  274. (gl->stencilFuncRef != ref) ||
  275. (gl->stencilFuncMask != mask)) {
  276. gl->stencilFunc = func;
  277. gl->stencilFuncRef = ref;
  278. gl->stencilFuncMask = mask;
  279. glStencilFunc(func, ref, mask);
  280. }
  281. #else
  282. glStencilFunc(func, ref, mask);
  283. #endif
  284. }
  285. static void glnvg__blendFuncSeparate(GLNVGcontext* gl, const GLNVGblend* blend)
  286. {
  287. #if NANOVG_GL_USE_STATE_FILTER
  288. if ((gl->blendFunc.srcRGB != blend->srcRGB) ||
  289. (gl->blendFunc.dstRGB != blend->dstRGB) ||
  290. (gl->blendFunc.srcAlpha != blend->srcAlpha) ||
  291. (gl->blendFunc.dstAlpha != blend->dstAlpha)) {
  292. gl->blendFunc = *blend;
  293. glBlendFuncSeparate(blend->srcRGB, blend->dstRGB, blend->srcAlpha,blend->dstAlpha);
  294. }
  295. #else
  296. glBlendFuncSeparate(blend->srcRGB, blend->dstRGB, blend->srcAlpha,blend->dstAlpha);
  297. #endif
  298. }
  299. static GLNVGtexture* glnvg__allocTexture(GLNVGcontext* gl)
  300. {
  301. GLNVGtexture* tex = NULL;
  302. int i;
  303. for (i = 0; i < gl->ntextures; i++) {
  304. if (gl->textures[i].id == 0) {
  305. tex = &gl->textures[i];
  306. break;
  307. }
  308. }
  309. if (tex == NULL) {
  310. if (gl->ntextures+1 > gl->ctextures) {
  311. GLNVGtexture* textures;
  312. int ctextures = glnvg__maxi(gl->ntextures+1, 4) + gl->ctextures/2; // 1.5x Overallocate
  313. textures = (GLNVGtexture*)realloc(gl->textures, sizeof(GLNVGtexture)*ctextures);
  314. if (textures == NULL) return NULL;
  315. gl->textures = textures;
  316. gl->ctextures = ctextures;
  317. }
  318. tex = &gl->textures[gl->ntextures++];
  319. }
  320. memset(tex, 0, sizeof(*tex));
  321. tex->id = ++gl->textureId;
  322. return tex;
  323. }
  324. static GLNVGtexture* glnvg__findTexture(GLNVGcontext* gl, int id)
  325. {
  326. int i;
  327. for (i = 0; i < gl->ntextures; i++)
  328. if (gl->textures[i].id == id)
  329. return &gl->textures[i];
  330. return NULL;
  331. }
  332. static int glnvg__deleteTexture(GLNVGcontext* gl, int id)
  333. {
  334. int i;
  335. for (i = 0; i < gl->ntextures; i++) {
  336. if (gl->textures[i].id == id) {
  337. if (gl->textures[i].tex != 0 && (gl->textures[i].flags & NVG_IMAGE_NODELETE) == 0)
  338. glDeleteTextures(1, &gl->textures[i].tex);
  339. memset(&gl->textures[i], 0, sizeof(gl->textures[i]));
  340. return 1;
  341. }
  342. }
  343. return 0;
  344. }
  345. static void glnvg__dumpShaderError(GLuint shader, const char* name, const char* type)
  346. {
  347. GLchar str[512+1];
  348. GLsizei len = 0;
  349. glGetShaderInfoLog(shader, 512, &len, str);
  350. if (len > 512) len = 512;
  351. str[len] = '\0';
  352. printf("Shader %s/%s error:\n%s\n", name, type, str);
  353. }
  354. static void glnvg__dumpProgramError(GLuint prog, const char* name)
  355. {
  356. GLchar str[512+1];
  357. GLsizei len = 0;
  358. glGetProgramInfoLog(prog, 512, &len, str);
  359. if (len > 512) len = 512;
  360. str[len] = '\0';
  361. printf("Program %s error:\n%s\n", name, str);
  362. }
  363. static void glnvg__checkError(GLNVGcontext* gl, const char* str)
  364. {
  365. GLenum err;
  366. if ((gl->flags & NVG_DEBUG) == 0) return;
  367. err = glGetError();
  368. if (err != GL_NO_ERROR) {
  369. printf("Error %08x after %s\n", err, str);
  370. return;
  371. }
  372. }
  373. static int glnvg__createShader(GLNVGshader* shader, const char* name, const char* header, const char* opts, const char* vshader, const char* fshader)
  374. {
  375. GLint status;
  376. GLuint prog, vert, frag;
  377. const char* str[3];
  378. str[0] = header;
  379. str[1] = opts != NULL ? opts : "";
  380. memset(shader, 0, sizeof(*shader));
  381. prog = glCreateProgram();
  382. vert = glCreateShader(GL_VERTEX_SHADER);
  383. frag = glCreateShader(GL_FRAGMENT_SHADER);
  384. str[2] = vshader;
  385. glShaderSource(vert, 3, str, 0);
  386. str[2] = fshader;
  387. glShaderSource(frag, 3, str, 0);
  388. glCompileShader(vert);
  389. glGetShaderiv(vert, GL_COMPILE_STATUS, &status);
  390. if (status != GL_TRUE) {
  391. glnvg__dumpShaderError(vert, name, "vert");
  392. return 0;
  393. }
  394. glCompileShader(frag);
  395. glGetShaderiv(frag, GL_COMPILE_STATUS, &status);
  396. if (status != GL_TRUE) {
  397. glnvg__dumpShaderError(frag, name, "frag");
  398. return 0;
  399. }
  400. glAttachShader(prog, vert);
  401. glAttachShader(prog, frag);
  402. glBindAttribLocation(prog, 0, "vertex");
  403. glBindAttribLocation(prog, 1, "tcoord");
  404. glLinkProgram(prog);
  405. glGetProgramiv(prog, GL_LINK_STATUS, &status);
  406. if (status != GL_TRUE) {
  407. glnvg__dumpProgramError(prog, name);
  408. return 0;
  409. }
  410. shader->prog = prog;
  411. shader->vert = vert;
  412. shader->frag = frag;
  413. return 1;
  414. }
  415. static void glnvg__deleteShader(GLNVGshader* shader)
  416. {
  417. if (shader->prog != 0)
  418. glDeleteProgram(shader->prog);
  419. if (shader->vert != 0)
  420. glDeleteShader(shader->vert);
  421. if (shader->frag != 0)
  422. glDeleteShader(shader->frag);
  423. }
  424. static void glnvg__getUniforms(GLNVGshader* shader)
  425. {
  426. shader->loc[GLNVG_LOC_VIEWSIZE] = glGetUniformLocation(shader->prog, "viewSize");
  427. shader->loc[GLNVG_LOC_TEX] = glGetUniformLocation(shader->prog, "tex");
  428. #if NANOVG_GL_USE_UNIFORMBUFFER
  429. shader->loc[GLNVG_LOC_FRAG] = glGetUniformBlockIndex(shader->prog, "frag");
  430. #else
  431. shader->loc[GLNVG_LOC_FRAG] = glGetUniformLocation(shader->prog, "frag");
  432. #endif
  433. }
  434. static int glnvg__renderCreateTexture(void* uptr, int type, int w, int h, int imageFlags, const unsigned char* data);
  435. static int glnvg__renderCreate(void* uptr)
  436. {
  437. GLNVGcontext* gl = (GLNVGcontext*)uptr;
  438. int align = 4;
  439. // TODO: mediump float may not be enough for GLES2 in iOS.
  440. // see the following discussion: https://github.com/memononen/nanovg/issues/46
  441. static const char* shaderHeader =
  442. #if defined NANOVG_GL2
  443. "#define NANOVG_GL2 1\n"
  444. #elif defined NANOVG_GL3
  445. "#version 150 core\n"
  446. "#define NANOVG_GL3 1\n"
  447. #elif defined NANOVG_GLES2
  448. "#version 100\n"
  449. "#define NANOVG_GL2 1\n"
  450. #elif defined NANOVG_GLES3
  451. "#version 300 es\n"
  452. "#define NANOVG_GL3 1\n"
  453. #endif
  454. #if NANOVG_GL_USE_UNIFORMBUFFER
  455. "#define USE_UNIFORMBUFFER 1\n"
  456. #else
  457. "#define UNIFORMARRAY_SIZE 11\n"
  458. #endif
  459. "\n";
  460. static const char* fillVertShader =
  461. "#ifdef NANOVG_GL3\n"
  462. " uniform vec2 viewSize;\n"
  463. " in vec2 vertex;\n"
  464. " in vec2 tcoord;\n"
  465. " out vec2 ftcoord;\n"
  466. " out vec2 fpos;\n"
  467. "#else\n"
  468. " uniform vec2 viewSize;\n"
  469. " attribute vec2 vertex;\n"
  470. " attribute vec2 tcoord;\n"
  471. " varying vec2 ftcoord;\n"
  472. " varying vec2 fpos;\n"
  473. "#endif\n"
  474. "void main(void) {\n"
  475. " ftcoord = tcoord;\n"
  476. " fpos = vertex;\n"
  477. " gl_Position = vec4(2.0*vertex.x/viewSize.x - 1.0, 1.0 - 2.0*vertex.y/viewSize.y, 0, 1);\n"
  478. "}\n";
  479. static const char* fillFragShader =
  480. "#ifdef GL_ES\n"
  481. "#if defined(GL_FRAGMENT_PRECISION_HIGH) || defined(NANOVG_GL3)\n"
  482. " precision highp float;\n"
  483. "#else\n"
  484. " precision mediump float;\n"
  485. "#endif\n"
  486. "#endif\n"
  487. "#ifdef NANOVG_GL3\n"
  488. "#ifdef USE_UNIFORMBUFFER\n"
  489. " layout(std140) uniform frag {\n"
  490. " mat3 scissorMat;\n"
  491. " mat3 paintMat;\n"
  492. " vec4 innerCol;\n"
  493. " vec4 outerCol;\n"
  494. " vec2 scissorExt;\n"
  495. " vec2 scissorScale;\n"
  496. " vec2 extent;\n"
  497. " float radius;\n"
  498. " float feather;\n"
  499. " float strokeMult;\n"
  500. " float strokeThr;\n"
  501. " int texType;\n"
  502. " int type;\n"
  503. " };\n"
  504. "#else\n" // NANOVG_GL3 && !USE_UNIFORMBUFFER
  505. " uniform vec4 frag[UNIFORMARRAY_SIZE];\n"
  506. "#endif\n"
  507. " uniform sampler2D tex;\n"
  508. " in vec2 ftcoord;\n"
  509. " in vec2 fpos;\n"
  510. " out vec4 outColor;\n"
  511. "#else\n" // !NANOVG_GL3
  512. " uniform vec4 frag[UNIFORMARRAY_SIZE];\n"
  513. " uniform sampler2D tex;\n"
  514. " varying vec2 ftcoord;\n"
  515. " varying vec2 fpos;\n"
  516. "#endif\n"
  517. "#ifndef USE_UNIFORMBUFFER\n"
  518. " #define scissorMat mat3(frag[0].xyz, frag[1].xyz, frag[2].xyz)\n"
  519. " #define paintMat mat3(frag[3].xyz, frag[4].xyz, frag[5].xyz)\n"
  520. " #define innerCol frag[6]\n"
  521. " #define outerCol frag[7]\n"
  522. " #define scissorExt frag[8].xy\n"
  523. " #define scissorScale frag[8].zw\n"
  524. " #define extent frag[9].xy\n"
  525. " #define radius frag[9].z\n"
  526. " #define feather frag[9].w\n"
  527. " #define strokeMult frag[10].x\n"
  528. " #define strokeThr frag[10].y\n"
  529. " #define texType int(frag[10].z)\n"
  530. " #define type int(frag[10].w)\n"
  531. "#endif\n"
  532. "\n"
  533. "float sdroundrect(vec2 pt, vec2 ext, float rad) {\n"
  534. " vec2 ext2 = ext - vec2(rad,rad);\n"
  535. " vec2 d = abs(pt) - ext2;\n"
  536. " return min(max(d.x,d.y),0.0) + length(max(d,0.0)) - rad;\n"
  537. "}\n"
  538. "\n"
  539. "// Scissoring\n"
  540. "float scissorMask(vec2 p) {\n"
  541. " vec2 sc = (abs((scissorMat * vec3(p,1.0)).xy) - scissorExt);\n"
  542. " sc = vec2(0.5,0.5) - sc * scissorScale;\n"
  543. " return clamp(sc.x,0.0,1.0) * clamp(sc.y,0.0,1.0);\n"
  544. "}\n"
  545. "#ifdef EDGE_AA\n"
  546. "// Stroke - from [0..1] to clipped pyramid, where the slope is 1px.\n"
  547. "float strokeMask() {\n"
  548. " return min(1.0, (1.0-abs(ftcoord.x*2.0-1.0))*strokeMult) * min(1.0, ftcoord.y);\n"
  549. "}\n"
  550. "#endif\n"
  551. "\n"
  552. "void main(void) {\n"
  553. " vec4 result;\n"
  554. " float scissor = scissorMask(fpos);\n"
  555. "#ifdef EDGE_AA\n"
  556. " float strokeAlpha = strokeMask();\n"
  557. " if (strokeAlpha < strokeThr) discard;\n"
  558. "#else\n"
  559. " float strokeAlpha = 1.0;\n"
  560. "#endif\n"
  561. " if (type == 0) { // Gradient\n"
  562. " // Calculate gradient color using box gradient\n"
  563. " vec2 pt = (paintMat * vec3(fpos,1.0)).xy;\n"
  564. " float d = clamp((sdroundrect(pt, extent, radius) + feather*0.5) / feather, 0.0, 1.0);\n"
  565. " vec4 color = mix(innerCol,outerCol,d);\n"
  566. " // Combine alpha\n"
  567. " color *= strokeAlpha * scissor;\n"
  568. " result = color;\n"
  569. " } else if (type == 1) { // Image\n"
  570. " // Calculate color fron texture\n"
  571. " vec2 pt = (paintMat * vec3(fpos,1.0)).xy / extent;\n"
  572. "#ifdef NANOVG_GL3\n"
  573. " vec4 color = texture(tex, pt);\n"
  574. "#else\n"
  575. " vec4 color = texture2D(tex, pt);\n"
  576. "#endif\n"
  577. " if (texType == 1) color = vec4(color.xyz*color.w,color.w);"
  578. " if (texType == 2) color = vec4(color.x);"
  579. " // Apply color tint and alpha.\n"
  580. " color *= innerCol;\n"
  581. " // Combine alpha\n"
  582. " color *= strokeAlpha * scissor;\n"
  583. " result = color;\n"
  584. " } else if (type == 2) { // Stencil fill\n"
  585. " result = vec4(1,1,1,1);\n"
  586. " } else if (type == 3) { // Textured tris\n"
  587. "#ifdef NANOVG_GL3\n"
  588. " vec4 color = texture(tex, ftcoord);\n"
  589. "#else\n"
  590. " vec4 color = texture2D(tex, ftcoord);\n"
  591. "#endif\n"
  592. " if (texType == 1) color = vec4(color.xyz*color.w,color.w);"
  593. " if (texType == 2) color = vec4(color.x);"
  594. " color *= scissor;\n"
  595. " result = color * innerCol;\n"
  596. " }\n"
  597. "#ifdef NANOVG_GL3\n"
  598. " outColor = result;\n"
  599. "#else\n"
  600. " gl_FragColor = result;\n"
  601. "#endif\n"
  602. "}\n";
  603. glnvg__checkError(gl, "init");
  604. if (gl->flags & NVG_ANTIALIAS) {
  605. if (glnvg__createShader(&gl->shader, "shader", shaderHeader, "#define EDGE_AA 1\n", fillVertShader, fillFragShader) == 0)
  606. return 0;
  607. } else {
  608. if (glnvg__createShader(&gl->shader, "shader", shaderHeader, NULL, fillVertShader, fillFragShader) == 0)
  609. return 0;
  610. }
  611. glnvg__checkError(gl, "uniform locations");
  612. glnvg__getUniforms(&gl->shader);
  613. // Create dynamic vertex array
  614. #if defined NANOVG_GL3
  615. glGenVertexArrays(1, &gl->vertArr);
  616. #endif
  617. glGenBuffers(1, &gl->vertBuf);
  618. #if NANOVG_GL_USE_UNIFORMBUFFER
  619. // Create UBOs
  620. glUniformBlockBinding(gl->shader.prog, gl->shader.loc[GLNVG_LOC_FRAG], GLNVG_FRAG_BINDING);
  621. glGenBuffers(1, &gl->fragBuf);
  622. glGetIntegerv(GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT, &align);
  623. #endif
  624. gl->fragSize = sizeof(GLNVGfragUniforms) + align - sizeof(GLNVGfragUniforms) % align;
  625. // Some platforms does not allow to have samples to unset textures.
  626. // Create empty one which is bound when there's no texture specified.
  627. gl->dummyTex = glnvg__renderCreateTexture(gl, NVG_TEXTURE_ALPHA, 1, 1, 0, NULL);
  628. glnvg__checkError(gl, "create done");
  629. glFinish();
  630. return 1;
  631. }
  632. static int glnvg__renderCreateTexture(void* uptr, int type, int w, int h, int imageFlags, const unsigned char* data)
  633. {
  634. GLNVGcontext* gl = (GLNVGcontext*)uptr;
  635. GLNVGtexture* tex = glnvg__allocTexture(gl);
  636. if (tex == NULL) return 0;
  637. #ifdef NANOVG_GLES2
  638. // Check for non-power of 2.
  639. if (glnvg__nearestPow2(w) != (unsigned int)w || glnvg__nearestPow2(h) != (unsigned int)h) {
  640. // No repeat
  641. if ((imageFlags & NVG_IMAGE_REPEATX) != 0 || (imageFlags & NVG_IMAGE_REPEATY) != 0) {
  642. printf("Repeat X/Y is not supported for non power-of-two textures (%d x %d)\n", w, h);
  643. imageFlags &= ~(NVG_IMAGE_REPEATX | NVG_IMAGE_REPEATY);
  644. }
  645. // No mips.
  646. if (imageFlags & NVG_IMAGE_GENERATE_MIPMAPS) {
  647. printf("Mip-maps is not support for non power-of-two textures (%d x %d)\n", w, h);
  648. imageFlags &= ~NVG_IMAGE_GENERATE_MIPMAPS;
  649. }
  650. }
  651. #endif
  652. glGenTextures(1, &tex->tex);
  653. tex->width = w;
  654. tex->height = h;
  655. tex->type = type;
  656. tex->flags = imageFlags;
  657. glnvg__bindTexture(gl, tex->tex);
  658. glPixelStorei(GL_UNPACK_ALIGNMENT,1);
  659. #ifndef NANOVG_GLES2
  660. glPixelStorei(GL_UNPACK_ROW_LENGTH, tex->width);
  661. glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
  662. glPixelStorei(GL_UNPACK_SKIP_ROWS, 0);
  663. #endif
  664. #if defined (NANOVG_GL2)
  665. // GL 1.4 and later has support for generating mipmaps using a tex parameter.
  666. if (imageFlags & NVG_IMAGE_GENERATE_MIPMAPS) {
  667. glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE);
  668. }
  669. #endif
  670. if (type == NVG_TEXTURE_RGBA)
  671. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
  672. else
  673. #if defined(NANOVG_GLES2) || defined (NANOVG_GL2)
  674. glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, w, h, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, data);
  675. #elif defined(NANOVG_GLES3)
  676. glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, w, h, 0, GL_RED, GL_UNSIGNED_BYTE, data);
  677. #else
  678. glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, w, h, 0, GL_RED, GL_UNSIGNED_BYTE, data);
  679. #endif
  680. if (imageFlags & NVG_IMAGE_GENERATE_MIPMAPS) {
  681. if (imageFlags & NVG_IMAGE_NEAREST) {
  682. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST);
  683. } else {
  684. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
  685. }
  686. } else {
  687. if (imageFlags & NVG_IMAGE_NEAREST) {
  688. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  689. } else {
  690. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  691. }
  692. }
  693. if (imageFlags & NVG_IMAGE_NEAREST) {
  694. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  695. } else {
  696. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  697. }
  698. if (imageFlags & NVG_IMAGE_REPEATX)
  699. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  700. else
  701. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  702. if (imageFlags & NVG_IMAGE_REPEATY)
  703. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  704. else
  705. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  706. glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
  707. #ifndef NANOVG_GLES2
  708. glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
  709. glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
  710. glPixelStorei(GL_UNPACK_SKIP_ROWS, 0);
  711. #endif
  712. // The new way to build mipmaps on GLES and GL3
  713. #if !defined(NANOVG_GL2)
  714. if (imageFlags & NVG_IMAGE_GENERATE_MIPMAPS) {
  715. glGenerateMipmap(GL_TEXTURE_2D);
  716. }
  717. #endif
  718. glnvg__checkError(gl, "create tex");
  719. glnvg__bindTexture(gl, 0);
  720. return tex->id;
  721. }
  722. static int glnvg__renderDeleteTexture(void* uptr, int image)
  723. {
  724. GLNVGcontext* gl = (GLNVGcontext*)uptr;
  725. return glnvg__deleteTexture(gl, image);
  726. }
  727. static int glnvg__renderUpdateTexture(void* uptr, int image, int x, int y, int w, int h, const unsigned char* data)
  728. {
  729. GLNVGcontext* gl = (GLNVGcontext*)uptr;
  730. GLNVGtexture* tex = glnvg__findTexture(gl, image);
  731. if (tex == NULL) return 0;
  732. glnvg__bindTexture(gl, tex->tex);
  733. glPixelStorei(GL_UNPACK_ALIGNMENT,1);
  734. #ifndef NANOVG_GLES2
  735. glPixelStorei(GL_UNPACK_ROW_LENGTH, tex->width);
  736. glPixelStorei(GL_UNPACK_SKIP_PIXELS, x);
  737. glPixelStorei(GL_UNPACK_SKIP_ROWS, y);
  738. #else
  739. // No support for all of skip, need to update a whole row at a time.
  740. if (tex->type == NVG_TEXTURE_RGBA)
  741. data += y*tex->width*4;
  742. else
  743. data += y*tex->width;
  744. x = 0;
  745. w = tex->width;
  746. #endif
  747. if (tex->type == NVG_TEXTURE_RGBA)
  748. glTexSubImage2D(GL_TEXTURE_2D, 0, x,y, w,h, GL_RGBA, GL_UNSIGNED_BYTE, data);
  749. else
  750. #if defined(NANOVG_GLES2) || defined(NANOVG_GL2)
  751. glTexSubImage2D(GL_TEXTURE_2D, 0, x,y, w,h, GL_LUMINANCE, GL_UNSIGNED_BYTE, data);
  752. #else
  753. glTexSubImage2D(GL_TEXTURE_2D, 0, x,y, w,h, GL_RED, GL_UNSIGNED_BYTE, data);
  754. #endif
  755. glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
  756. #ifndef NANOVG_GLES2
  757. glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
  758. glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
  759. glPixelStorei(GL_UNPACK_SKIP_ROWS, 0);
  760. #endif
  761. glnvg__bindTexture(gl, 0);
  762. return 1;
  763. }
  764. static int glnvg__renderGetTextureSize(void* uptr, int image, int* w, int* h)
  765. {
  766. GLNVGcontext* gl = (GLNVGcontext*)uptr;
  767. GLNVGtexture* tex = glnvg__findTexture(gl, image);
  768. if (tex == NULL) return 0;
  769. *w = tex->width;
  770. *h = tex->height;
  771. return 1;
  772. }
  773. static void glnvg__xformToMat3x4(float* m3, float* t)
  774. {
  775. m3[0] = t[0];
  776. m3[1] = t[1];
  777. m3[2] = 0.0f;
  778. m3[3] = 0.0f;
  779. m3[4] = t[2];
  780. m3[5] = t[3];
  781. m3[6] = 0.0f;
  782. m3[7] = 0.0f;
  783. m3[8] = t[4];
  784. m3[9] = t[5];
  785. m3[10] = 1.0f;
  786. m3[11] = 0.0f;
  787. }
  788. static NVGcolor glnvg__premulColor(NVGcolor c)
  789. {
  790. c.r *= c.a;
  791. c.g *= c.a;
  792. c.b *= c.a;
  793. return c;
  794. }
  795. static int glnvg__convertPaint(GLNVGcontext* gl, GLNVGfragUniforms* frag, NVGpaint* paint,
  796. NVGscissor* scissor, float width, float fringe, float strokeThr)
  797. {
  798. GLNVGtexture* tex = NULL;
  799. float invxform[6];
  800. memset(frag, 0, sizeof(*frag));
  801. frag->innerCol = glnvg__premulColor(paint->innerColor);
  802. frag->outerCol = glnvg__premulColor(paint->outerColor);
  803. if (scissor->extent[0] < -0.5f || scissor->extent[1] < -0.5f) {
  804. memset(frag->scissorMat, 0, sizeof(frag->scissorMat));
  805. frag->scissorExt[0] = 1.0f;
  806. frag->scissorExt[1] = 1.0f;
  807. frag->scissorScale[0] = 1.0f;
  808. frag->scissorScale[1] = 1.0f;
  809. } else {
  810. nvgTransformInverse(invxform, scissor->xform);
  811. glnvg__xformToMat3x4(frag->scissorMat, invxform);
  812. frag->scissorExt[0] = scissor->extent[0];
  813. frag->scissorExt[1] = scissor->extent[1];
  814. frag->scissorScale[0] = sqrtf(scissor->xform[0]*scissor->xform[0] + scissor->xform[2]*scissor->xform[2]) / fringe;
  815. frag->scissorScale[1] = sqrtf(scissor->xform[1]*scissor->xform[1] + scissor->xform[3]*scissor->xform[3]) / fringe;
  816. }
  817. memcpy(frag->extent, paint->extent, sizeof(frag->extent));
  818. frag->strokeMult = (width*0.5f + fringe*0.5f) / fringe;
  819. frag->strokeThr = strokeThr;
  820. if (paint->image != 0) {
  821. tex = glnvg__findTexture(gl, paint->image);
  822. if (tex == NULL) return 0;
  823. if ((tex->flags & NVG_IMAGE_FLIPY) != 0) {
  824. float m1[6], m2[6];
  825. nvgTransformTranslate(m1, 0.0f, frag->extent[1] * 0.5f);
  826. nvgTransformMultiply(m1, paint->xform);
  827. nvgTransformScale(m2, 1.0f, -1.0f);
  828. nvgTransformMultiply(m2, m1);
  829. nvgTransformTranslate(m1, 0.0f, -frag->extent[1] * 0.5f);
  830. nvgTransformMultiply(m1, m2);
  831. nvgTransformInverse(invxform, m1);
  832. } else {
  833. nvgTransformInverse(invxform, paint->xform);
  834. }
  835. frag->type = NSVG_SHADER_FILLIMG;
  836. #if NANOVG_GL_USE_UNIFORMBUFFER
  837. if (tex->type == NVG_TEXTURE_RGBA)
  838. frag->texType = (tex->flags & NVG_IMAGE_PREMULTIPLIED) ? 0 : 1;
  839. else
  840. frag->texType = 2;
  841. #else
  842. if (tex->type == NVG_TEXTURE_RGBA)
  843. frag->texType = (tex->flags & NVG_IMAGE_PREMULTIPLIED) ? 0.0f : 1.0f;
  844. else
  845. frag->texType = 2.0f;
  846. #endif
  847. // printf("frag->texType = %d\n", frag->texType);
  848. } else {
  849. frag->type = NSVG_SHADER_FILLGRAD;
  850. frag->radius = paint->radius;
  851. frag->feather = paint->feather;
  852. nvgTransformInverse(invxform, paint->xform);
  853. }
  854. glnvg__xformToMat3x4(frag->paintMat, invxform);
  855. return 1;
  856. }
  857. static GLNVGfragUniforms* nvg__fragUniformPtr(GLNVGcontext* gl, int i);
  858. static void glnvg__setUniforms(GLNVGcontext* gl, int uniformOffset, int image)
  859. {
  860. GLNVGtexture* tex = NULL;
  861. #if NANOVG_GL_USE_UNIFORMBUFFER
  862. glBindBufferRange(GL_UNIFORM_BUFFER, GLNVG_FRAG_BINDING, gl->fragBuf, uniformOffset, sizeof(GLNVGfragUniforms));
  863. #else
  864. GLNVGfragUniforms* frag = nvg__fragUniformPtr(gl, uniformOffset);
  865. glUniform4fv(gl->shader.loc[GLNVG_LOC_FRAG], NANOVG_GL_UNIFORMARRAY_SIZE, &(frag->uniformArray[0][0]));
  866. #endif
  867. if (image != 0) {
  868. tex = glnvg__findTexture(gl, image);
  869. }
  870. // If no image is set, use empty texture
  871. if (tex == NULL) {
  872. tex = glnvg__findTexture(gl, gl->dummyTex);
  873. }
  874. glnvg__bindTexture(gl, tex != NULL ? tex->tex : 0);
  875. glnvg__checkError(gl, "tex paint tex");
  876. }
  877. static void glnvg__renderViewport(void* uptr, float width, float height, float devicePixelRatio)
  878. {
  879. NVG_NOTUSED(devicePixelRatio);
  880. GLNVGcontext* gl = (GLNVGcontext*)uptr;
  881. gl->view[0] = width;
  882. gl->view[1] = height;
  883. }
  884. static void glnvg__fill(GLNVGcontext* gl, GLNVGcall* call)
  885. {
  886. GLNVGpath* paths = &gl->paths[call->pathOffset];
  887. int i, npaths = call->pathCount;
  888. // Draw shapes
  889. glEnable(GL_STENCIL_TEST);
  890. glnvg__stencilMask(gl, 0xff);
  891. glnvg__stencilFunc(gl, GL_ALWAYS, 0, 0xff);
  892. glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
  893. // set bindpoint for solid loc
  894. glnvg__setUniforms(gl, call->uniformOffset, 0);
  895. glnvg__checkError(gl, "fill simple");
  896. glStencilOpSeparate(GL_FRONT, GL_KEEP, GL_KEEP, GL_INCR_WRAP);
  897. glStencilOpSeparate(GL_BACK, GL_KEEP, GL_KEEP, GL_DECR_WRAP);
  898. glDisable(GL_CULL_FACE);
  899. for (i = 0; i < npaths; i++)
  900. glDrawArrays(GL_TRIANGLE_FAN, paths[i].fillOffset, paths[i].fillCount);
  901. glEnable(GL_CULL_FACE);
  902. // Draw anti-aliased pixels
  903. glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
  904. glnvg__setUniforms(gl, call->uniformOffset + gl->fragSize, call->image);
  905. glnvg__checkError(gl, "fill fill");
  906. if (gl->flags & NVG_ANTIALIAS) {
  907. glnvg__stencilFunc(gl, GL_EQUAL, 0x00, 0xff);
  908. glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
  909. // Draw fringes
  910. for (i = 0; i < npaths; i++)
  911. glDrawArrays(GL_TRIANGLE_STRIP, paths[i].strokeOffset, paths[i].strokeCount);
  912. }
  913. // Draw fill
  914. glnvg__stencilFunc(gl, GL_NOTEQUAL, 0x0, 0xff);
  915. glStencilOp(GL_ZERO, GL_ZERO, GL_ZERO);
  916. glDrawArrays(GL_TRIANGLE_STRIP, call->triangleOffset, call->triangleCount);
  917. glDisable(GL_STENCIL_TEST);
  918. }
  919. static void glnvg__convexFill(GLNVGcontext* gl, GLNVGcall* call)
  920. {
  921. GLNVGpath* paths = &gl->paths[call->pathOffset];
  922. int i, npaths = call->pathCount;
  923. glnvg__setUniforms(gl, call->uniformOffset, call->image);
  924. glnvg__checkError(gl, "convex fill");
  925. for (i = 0; i < npaths; i++) {
  926. glDrawArrays(GL_TRIANGLE_FAN, paths[i].fillOffset, paths[i].fillCount);
  927. // Draw fringes
  928. if (paths[i].strokeCount > 0) {
  929. glDrawArrays(GL_TRIANGLE_STRIP, paths[i].strokeOffset, paths[i].strokeCount);
  930. }
  931. }
  932. }
  933. static void glnvg__stroke(GLNVGcontext* gl, GLNVGcall* call)
  934. {
  935. GLNVGpath* paths = &gl->paths[call->pathOffset];
  936. int npaths = call->pathCount, i;
  937. if (gl->flags & NVG_STENCIL_STROKES) {
  938. glEnable(GL_STENCIL_TEST);
  939. glnvg__stencilMask(gl, 0xff);
  940. // Fill the stroke base without overlap
  941. glnvg__stencilFunc(gl, GL_EQUAL, 0x0, 0xff);
  942. glStencilOp(GL_KEEP, GL_KEEP, GL_INCR);
  943. glnvg__setUniforms(gl, call->uniformOffset + gl->fragSize, call->image);
  944. glnvg__checkError(gl, "stroke fill 0");
  945. for (i = 0; i < npaths; i++)
  946. glDrawArrays(GL_TRIANGLE_STRIP, paths[i].strokeOffset, paths[i].strokeCount);
  947. // Draw anti-aliased pixels.
  948. glnvg__setUniforms(gl, call->uniformOffset, call->image);
  949. glnvg__stencilFunc(gl, GL_EQUAL, 0x00, 0xff);
  950. glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
  951. for (i = 0; i < npaths; i++)
  952. glDrawArrays(GL_TRIANGLE_STRIP, paths[i].strokeOffset, paths[i].strokeCount);
  953. // Clear stencil buffer.
  954. glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
  955. glnvg__stencilFunc(gl, GL_ALWAYS, 0x0, 0xff);
  956. glStencilOp(GL_ZERO, GL_ZERO, GL_ZERO);
  957. glnvg__checkError(gl, "stroke fill 1");
  958. for (i = 0; i < npaths; i++)
  959. glDrawArrays(GL_TRIANGLE_STRIP, paths[i].strokeOffset, paths[i].strokeCount);
  960. glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
  961. glDisable(GL_STENCIL_TEST);
  962. // glnvg__convertPaint(gl, nvg__fragUniformPtr(gl, call->uniformOffset + gl->fragSize), paint, scissor, strokeWidth, fringe, 1.0f - 0.5f/255.0f);
  963. } else {
  964. glnvg__setUniforms(gl, call->uniformOffset, call->image);
  965. glnvg__checkError(gl, "stroke fill");
  966. // Draw Strokes
  967. for (i = 0; i < npaths; i++)
  968. glDrawArrays(GL_TRIANGLE_STRIP, paths[i].strokeOffset, paths[i].strokeCount);
  969. }
  970. }
  971. static void glnvg__triangles(GLNVGcontext* gl, GLNVGcall* call)
  972. {
  973. glnvg__setUniforms(gl, call->uniformOffset, call->image);
  974. glnvg__checkError(gl, "triangles fill");
  975. glDrawArrays(GL_TRIANGLES, call->triangleOffset, call->triangleCount);
  976. }
  977. static void glnvg__renderCancel(void* uptr) {
  978. GLNVGcontext* gl = (GLNVGcontext*)uptr;
  979. gl->nverts = 0;
  980. gl->npaths = 0;
  981. gl->ncalls = 0;
  982. gl->nuniforms = 0;
  983. }
  984. static GLenum glnvg_convertBlendFuncFactor(int factor)
  985. {
  986. if (factor == NVG_ZERO)
  987. return GL_ZERO;
  988. if (factor == NVG_ONE)
  989. return GL_ONE;
  990. if (factor == NVG_SRC_COLOR)
  991. return GL_SRC_COLOR;
  992. if (factor == NVG_ONE_MINUS_SRC_COLOR)
  993. return GL_ONE_MINUS_SRC_COLOR;
  994. if (factor == NVG_DST_COLOR)
  995. return GL_DST_COLOR;
  996. if (factor == NVG_ONE_MINUS_DST_COLOR)
  997. return GL_ONE_MINUS_DST_COLOR;
  998. if (factor == NVG_SRC_ALPHA)
  999. return GL_SRC_ALPHA;
  1000. if (factor == NVG_ONE_MINUS_SRC_ALPHA)
  1001. return GL_ONE_MINUS_SRC_ALPHA;
  1002. if (factor == NVG_DST_ALPHA)
  1003. return GL_DST_ALPHA;
  1004. if (factor == NVG_ONE_MINUS_DST_ALPHA)
  1005. return GL_ONE_MINUS_DST_ALPHA;
  1006. if (factor == NVG_SRC_ALPHA_SATURATE)
  1007. return GL_SRC_ALPHA_SATURATE;
  1008. return GL_INVALID_ENUM;
  1009. }
  1010. static GLNVGblend glnvg__blendCompositeOperation(NVGcompositeOperationState op)
  1011. {
  1012. GLNVGblend blend;
  1013. blend.srcRGB = glnvg_convertBlendFuncFactor(op.srcRGB);
  1014. blend.dstRGB = glnvg_convertBlendFuncFactor(op.dstRGB);
  1015. blend.srcAlpha = glnvg_convertBlendFuncFactor(op.srcAlpha);
  1016. blend.dstAlpha = glnvg_convertBlendFuncFactor(op.dstAlpha);
  1017. if (blend.srcRGB == GL_INVALID_ENUM || blend.dstRGB == GL_INVALID_ENUM || blend.srcAlpha == GL_INVALID_ENUM || blend.dstAlpha == GL_INVALID_ENUM)
  1018. {
  1019. blend.srcRGB = GL_ONE;
  1020. blend.dstRGB = GL_ONE_MINUS_SRC_ALPHA;
  1021. blend.srcAlpha = GL_ONE;
  1022. blend.dstAlpha = GL_ONE_MINUS_SRC_ALPHA;
  1023. }
  1024. return blend;
  1025. }
  1026. static void glnvg__renderFlush(void* uptr)
  1027. {
  1028. GLNVGcontext* gl = (GLNVGcontext*)uptr;
  1029. int i;
  1030. if (gl->ncalls > 0) {
  1031. // Setup require GL state.
  1032. glUseProgram(gl->shader.prog);
  1033. glEnable(GL_CULL_FACE);
  1034. glCullFace(GL_BACK);
  1035. glFrontFace(GL_CCW);
  1036. glEnable(GL_BLEND);
  1037. glDisable(GL_DEPTH_TEST);
  1038. glDisable(GL_SCISSOR_TEST);
  1039. glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
  1040. glStencilMask(0xffffffff);
  1041. glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
  1042. glStencilFunc(GL_ALWAYS, 0, 0xffffffff);
  1043. glActiveTexture(GL_TEXTURE0);
  1044. glBindTexture(GL_TEXTURE_2D, 0);
  1045. #if NANOVG_GL_USE_STATE_FILTER
  1046. gl->boundTexture = 0;
  1047. gl->stencilMask = 0xffffffff;
  1048. gl->stencilFunc = GL_ALWAYS;
  1049. gl->stencilFuncRef = 0;
  1050. gl->stencilFuncMask = 0xffffffff;
  1051. gl->blendFunc.srcRGB = GL_INVALID_ENUM;
  1052. gl->blendFunc.srcAlpha = GL_INVALID_ENUM;
  1053. gl->blendFunc.dstRGB = GL_INVALID_ENUM;
  1054. gl->blendFunc.dstAlpha = GL_INVALID_ENUM;
  1055. #endif
  1056. #if NANOVG_GL_USE_UNIFORMBUFFER
  1057. // Upload ubo for frag shaders
  1058. glBindBuffer(GL_UNIFORM_BUFFER, gl->fragBuf);
  1059. glBufferData(GL_UNIFORM_BUFFER, gl->nuniforms * gl->fragSize, gl->uniforms, GL_STREAM_DRAW);
  1060. #endif
  1061. // Upload vertex data
  1062. #if defined NANOVG_GL3
  1063. glBindVertexArray(gl->vertArr);
  1064. #endif
  1065. glBindBuffer(GL_ARRAY_BUFFER, gl->vertBuf);
  1066. glBufferData(GL_ARRAY_BUFFER, gl->nverts * sizeof(NVGvertex), gl->verts, GL_STREAM_DRAW);
  1067. glEnableVertexAttribArray(0);
  1068. glEnableVertexAttribArray(1);
  1069. glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(NVGvertex), (const GLvoid*)(size_t)0);
  1070. glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(NVGvertex), (const GLvoid*)(0 + 2*sizeof(float)));
  1071. // Set view and texture just once per frame.
  1072. glUniform1i(gl->shader.loc[GLNVG_LOC_TEX], 0);
  1073. glUniform2fv(gl->shader.loc[GLNVG_LOC_VIEWSIZE], 1, gl->view);
  1074. #if NANOVG_GL_USE_UNIFORMBUFFER
  1075. glBindBuffer(GL_UNIFORM_BUFFER, gl->fragBuf);
  1076. #endif
  1077. for (i = 0; i < gl->ncalls; i++) {
  1078. GLNVGcall* call = &gl->calls[i];
  1079. glnvg__blendFuncSeparate(gl,&call->blendFunc);
  1080. if (call->type == GLNVG_FILL)
  1081. glnvg__fill(gl, call);
  1082. else if (call->type == GLNVG_CONVEXFILL)
  1083. glnvg__convexFill(gl, call);
  1084. else if (call->type == GLNVG_STROKE)
  1085. glnvg__stroke(gl, call);
  1086. else if (call->type == GLNVG_TRIANGLES)
  1087. glnvg__triangles(gl, call);
  1088. }
  1089. glDisableVertexAttribArray(0);
  1090. glDisableVertexAttribArray(1);
  1091. #if defined NANOVG_GL3
  1092. glBindVertexArray(0);
  1093. #endif
  1094. glDisable(GL_CULL_FACE);
  1095. glBindBuffer(GL_ARRAY_BUFFER, 0);
  1096. glUseProgram(0);
  1097. glnvg__bindTexture(gl, 0);
  1098. }
  1099. // Reset calls
  1100. gl->nverts = 0;
  1101. gl->npaths = 0;
  1102. gl->ncalls = 0;
  1103. gl->nuniforms = 0;
  1104. }
  1105. static int glnvg__maxVertCount(const NVGpath* paths, int npaths)
  1106. {
  1107. int i, count = 0;
  1108. for (i = 0; i < npaths; i++) {
  1109. count += paths[i].nfill;
  1110. count += paths[i].nstroke;
  1111. }
  1112. return count;
  1113. }
  1114. static GLNVGcall* glnvg__allocCall(GLNVGcontext* gl)
  1115. {
  1116. GLNVGcall* ret = NULL;
  1117. if (gl->ncalls+1 > gl->ccalls) {
  1118. GLNVGcall* calls;
  1119. int ccalls = glnvg__maxi(gl->ncalls+1, 128) + gl->ccalls/2; // 1.5x Overallocate
  1120. calls = (GLNVGcall*)realloc(gl->calls, sizeof(GLNVGcall) * ccalls);
  1121. if (calls == NULL) return NULL;
  1122. gl->calls = calls;
  1123. gl->ccalls = ccalls;
  1124. }
  1125. ret = &gl->calls[gl->ncalls++];
  1126. memset(ret, 0, sizeof(GLNVGcall));
  1127. return ret;
  1128. }
  1129. static int glnvg__allocPaths(GLNVGcontext* gl, int n)
  1130. {
  1131. int ret = 0;
  1132. if (gl->npaths+n > gl->cpaths) {
  1133. GLNVGpath* paths;
  1134. int cpaths = glnvg__maxi(gl->npaths + n, 128) + gl->cpaths/2; // 1.5x Overallocate
  1135. paths = (GLNVGpath*)realloc(gl->paths, sizeof(GLNVGpath) * cpaths);
  1136. if (paths == NULL) return -1;
  1137. gl->paths = paths;
  1138. gl->cpaths = cpaths;
  1139. }
  1140. ret = gl->npaths;
  1141. gl->npaths += n;
  1142. return ret;
  1143. }
  1144. static int glnvg__allocVerts(GLNVGcontext* gl, int n)
  1145. {
  1146. int ret = 0;
  1147. if (gl->nverts+n > gl->cverts) {
  1148. NVGvertex* verts;
  1149. int cverts = glnvg__maxi(gl->nverts + n, 4096) + gl->cverts/2; // 1.5x Overallocate
  1150. verts = (NVGvertex*)realloc(gl->verts, sizeof(NVGvertex) * cverts);
  1151. if (verts == NULL) return -1;
  1152. gl->verts = verts;
  1153. gl->cverts = cverts;
  1154. }
  1155. ret = gl->nverts;
  1156. gl->nverts += n;
  1157. return ret;
  1158. }
  1159. static int glnvg__allocFragUniforms(GLNVGcontext* gl, int n)
  1160. {
  1161. int ret = 0, structSize = gl->fragSize;
  1162. if (gl->nuniforms+n > gl->cuniforms) {
  1163. unsigned char* uniforms;
  1164. int cuniforms = glnvg__maxi(gl->nuniforms+n, 128) + gl->cuniforms/2; // 1.5x Overallocate
  1165. uniforms = (unsigned char*)realloc(gl->uniforms, structSize * cuniforms);
  1166. if (uniforms == NULL) return -1;
  1167. gl->uniforms = uniforms;
  1168. gl->cuniforms = cuniforms;
  1169. }
  1170. ret = gl->nuniforms * structSize;
  1171. gl->nuniforms += n;
  1172. return ret;
  1173. }
  1174. static GLNVGfragUniforms* nvg__fragUniformPtr(GLNVGcontext* gl, int i)
  1175. {
  1176. return (GLNVGfragUniforms*)&gl->uniforms[i];
  1177. }
  1178. static void glnvg__vset(NVGvertex* vtx, float x, float y, float u, float v)
  1179. {
  1180. vtx->x = x;
  1181. vtx->y = y;
  1182. vtx->u = u;
  1183. vtx->v = v;
  1184. }
  1185. static void glnvg__renderFill(void* uptr, NVGpaint* paint, NVGcompositeOperationState compositeOperation, NVGscissor* scissor, float fringe,
  1186. const float* bounds, const NVGpath* paths, int npaths)
  1187. {
  1188. GLNVGcontext* gl = (GLNVGcontext*)uptr;
  1189. GLNVGcall* call = glnvg__allocCall(gl);
  1190. NVGvertex* quad;
  1191. GLNVGfragUniforms* frag;
  1192. int i, maxverts, offset;
  1193. if (call == NULL) return;
  1194. call->type = GLNVG_FILL;
  1195. call->triangleCount = 4;
  1196. call->pathOffset = glnvg__allocPaths(gl, npaths);
  1197. if (call->pathOffset == -1) goto error;
  1198. call->pathCount = npaths;
  1199. call->image = paint->image;
  1200. call->blendFunc = glnvg__blendCompositeOperation(compositeOperation);
  1201. if (npaths == 1 && paths[0].convex)
  1202. {
  1203. call->type = GLNVG_CONVEXFILL;
  1204. call->triangleCount = 0; // Bounding box fill quad not needed for convex fill
  1205. }
  1206. // Allocate vertices for all the paths.
  1207. maxverts = glnvg__maxVertCount(paths, npaths) + call->triangleCount;
  1208. offset = glnvg__allocVerts(gl, maxverts);
  1209. if (offset == -1) goto error;
  1210. for (i = 0; i < npaths; i++) {
  1211. GLNVGpath* copy = &gl->paths[call->pathOffset + i];
  1212. const NVGpath* path = &paths[i];
  1213. memset(copy, 0, sizeof(GLNVGpath));
  1214. if (path->nfill > 0) {
  1215. copy->fillOffset = offset;
  1216. copy->fillCount = path->nfill;
  1217. memcpy(&gl->verts[offset], path->fill, sizeof(NVGvertex) * path->nfill);
  1218. offset += path->nfill;
  1219. }
  1220. if (path->nstroke > 0) {
  1221. copy->strokeOffset = offset;
  1222. copy->strokeCount = path->nstroke;
  1223. memcpy(&gl->verts[offset], path->stroke, sizeof(NVGvertex) * path->nstroke);
  1224. offset += path->nstroke;
  1225. }
  1226. }
  1227. // Setup uniforms for draw calls
  1228. if (call->type == GLNVG_FILL) {
  1229. // Quad
  1230. call->triangleOffset = offset;
  1231. quad = &gl->verts[call->triangleOffset];
  1232. glnvg__vset(&quad[0], bounds[2], bounds[3], 0.5f, 1.0f);
  1233. glnvg__vset(&quad[1], bounds[2], bounds[1], 0.5f, 1.0f);
  1234. glnvg__vset(&quad[2], bounds[0], bounds[3], 0.5f, 1.0f);
  1235. glnvg__vset(&quad[3], bounds[0], bounds[1], 0.5f, 1.0f);
  1236. call->uniformOffset = glnvg__allocFragUniforms(gl, 2);
  1237. if (call->uniformOffset == -1) goto error;
  1238. // Simple shader for stencil
  1239. frag = nvg__fragUniformPtr(gl, call->uniformOffset);
  1240. memset(frag, 0, sizeof(*frag));
  1241. frag->strokeThr = -1.0f;
  1242. frag->type = NSVG_SHADER_SIMPLE;
  1243. // Fill shader
  1244. glnvg__convertPaint(gl, nvg__fragUniformPtr(gl, call->uniformOffset + gl->fragSize), paint, scissor, fringe, fringe, -1.0f);
  1245. } else {
  1246. call->uniformOffset = glnvg__allocFragUniforms(gl, 1);
  1247. if (call->uniformOffset == -1) goto error;
  1248. // Fill shader
  1249. glnvg__convertPaint(gl, nvg__fragUniformPtr(gl, call->uniformOffset), paint, scissor, fringe, fringe, -1.0f);
  1250. }
  1251. return;
  1252. error:
  1253. // We get here if call alloc was ok, but something else is not.
  1254. // Roll back the last call to prevent drawing it.
  1255. if (gl->ncalls > 0) gl->ncalls--;
  1256. }
  1257. static void glnvg__renderStroke(void* uptr, NVGpaint* paint, NVGcompositeOperationState compositeOperation, NVGscissor* scissor, float fringe,
  1258. float strokeWidth, const NVGpath* paths, int npaths)
  1259. {
  1260. GLNVGcontext* gl = (GLNVGcontext*)uptr;
  1261. GLNVGcall* call = glnvg__allocCall(gl);
  1262. int i, maxverts, offset;
  1263. if (call == NULL) return;
  1264. call->type = GLNVG_STROKE;
  1265. call->pathOffset = glnvg__allocPaths(gl, npaths);
  1266. if (call->pathOffset == -1) goto error;
  1267. call->pathCount = npaths;
  1268. call->image = paint->image;
  1269. call->blendFunc = glnvg__blendCompositeOperation(compositeOperation);
  1270. // Allocate vertices for all the paths.
  1271. maxverts = glnvg__maxVertCount(paths, npaths);
  1272. offset = glnvg__allocVerts(gl, maxverts);
  1273. if (offset == -1) goto error;
  1274. for (i = 0; i < npaths; i++) {
  1275. GLNVGpath* copy = &gl->paths[call->pathOffset + i];
  1276. const NVGpath* path = &paths[i];
  1277. memset(copy, 0, sizeof(GLNVGpath));
  1278. if (path->nstroke) {
  1279. copy->strokeOffset = offset;
  1280. copy->strokeCount = path->nstroke;
  1281. memcpy(&gl->verts[offset], path->stroke, sizeof(NVGvertex) * path->nstroke);
  1282. offset += path->nstroke;
  1283. }
  1284. }
  1285. if (gl->flags & NVG_STENCIL_STROKES) {
  1286. // Fill shader
  1287. call->uniformOffset = glnvg__allocFragUniforms(gl, 2);
  1288. if (call->uniformOffset == -1) goto error;
  1289. glnvg__convertPaint(gl, nvg__fragUniformPtr(gl, call->uniformOffset), paint, scissor, strokeWidth, fringe, -1.0f);
  1290. glnvg__convertPaint(gl, nvg__fragUniformPtr(gl, call->uniformOffset + gl->fragSize), paint, scissor, strokeWidth, fringe, 1.0f - 0.5f/255.0f);
  1291. } else {
  1292. // Fill shader
  1293. call->uniformOffset = glnvg__allocFragUniforms(gl, 1);
  1294. if (call->uniformOffset == -1) goto error;
  1295. glnvg__convertPaint(gl, nvg__fragUniformPtr(gl, call->uniformOffset), paint, scissor, strokeWidth, fringe, -1.0f);
  1296. }
  1297. return;
  1298. error:
  1299. // We get here if call alloc was ok, but something else is not.
  1300. // Roll back the last call to prevent drawing it.
  1301. if (gl->ncalls > 0) gl->ncalls--;
  1302. }
  1303. static void glnvg__renderTriangles(void* uptr, NVGpaint* paint, NVGcompositeOperationState compositeOperation, NVGscissor* scissor,
  1304. const NVGvertex* verts, int nverts, float fringe)
  1305. {
  1306. GLNVGcontext* gl = (GLNVGcontext*)uptr;
  1307. GLNVGcall* call = glnvg__allocCall(gl);
  1308. GLNVGfragUniforms* frag;
  1309. if (call == NULL) return;
  1310. call->type = GLNVG_TRIANGLES;
  1311. call->image = paint->image;
  1312. call->blendFunc = glnvg__blendCompositeOperation(compositeOperation);
  1313. // Allocate vertices for all the paths.
  1314. call->triangleOffset = glnvg__allocVerts(gl, nverts);
  1315. if (call->triangleOffset == -1) goto error;
  1316. call->triangleCount = nverts;
  1317. memcpy(&gl->verts[call->triangleOffset], verts, sizeof(NVGvertex) * nverts);
  1318. // Fill shader
  1319. call->uniformOffset = glnvg__allocFragUniforms(gl, 1);
  1320. if (call->uniformOffset == -1) goto error;
  1321. frag = nvg__fragUniformPtr(gl, call->uniformOffset);
  1322. glnvg__convertPaint(gl, frag, paint, scissor, 1.0f, fringe, -1.0f);
  1323. frag->type = NSVG_SHADER_IMG;
  1324. return;
  1325. error:
  1326. // We get here if call alloc was ok, but something else is not.
  1327. // Roll back the last call to prevent drawing it.
  1328. if (gl->ncalls > 0) gl->ncalls--;
  1329. }
  1330. static void glnvg__renderDelete(void* uptr)
  1331. {
  1332. GLNVGcontext* gl = (GLNVGcontext*)uptr;
  1333. int i;
  1334. if (gl == NULL) return;
  1335. glnvg__deleteShader(&gl->shader);
  1336. #if NANOVG_GL3
  1337. #if NANOVG_GL_USE_UNIFORMBUFFER
  1338. if (gl->fragBuf != 0)
  1339. glDeleteBuffers(1, &gl->fragBuf);
  1340. #endif
  1341. if (gl->vertArr != 0)
  1342. glDeleteVertexArrays(1, &gl->vertArr);
  1343. #endif
  1344. if (gl->vertBuf != 0)
  1345. glDeleteBuffers(1, &gl->vertBuf);
  1346. for (i = 0; i < gl->ntextures; i++) {
  1347. if (gl->textures[i].tex != 0 && (gl->textures[i].flags & NVG_IMAGE_NODELETE) == 0)
  1348. glDeleteTextures(1, &gl->textures[i].tex);
  1349. }
  1350. free(gl->textures);
  1351. free(gl->paths);
  1352. free(gl->verts);
  1353. free(gl->uniforms);
  1354. free(gl->calls);
  1355. free(gl);
  1356. }
  1357. #if defined NANOVG_GL2
  1358. NVGcontext* nvgCreateGL2(int flags)
  1359. #elif defined NANOVG_GL3
  1360. NVGcontext* nvgCreateGL3(int flags)
  1361. #elif defined NANOVG_GLES2
  1362. NVGcontext* nvgCreateGLES2(int flags)
  1363. #elif defined NANOVG_GLES3
  1364. NVGcontext* nvgCreateGLES3(int flags)
  1365. #endif
  1366. {
  1367. NVGparams params;
  1368. NVGcontext* ctx = NULL;
  1369. GLNVGcontext* gl = (GLNVGcontext*)malloc(sizeof(GLNVGcontext));
  1370. if (gl == NULL) goto error;
  1371. memset(gl, 0, sizeof(GLNVGcontext));
  1372. memset(&params, 0, sizeof(params));
  1373. params.renderCreate = glnvg__renderCreate;
  1374. params.renderCreateTexture = glnvg__renderCreateTexture;
  1375. params.renderDeleteTexture = glnvg__renderDeleteTexture;
  1376. params.renderUpdateTexture = glnvg__renderUpdateTexture;
  1377. params.renderGetTextureSize = glnvg__renderGetTextureSize;
  1378. params.renderViewport = glnvg__renderViewport;
  1379. params.renderCancel = glnvg__renderCancel;
  1380. params.renderFlush = glnvg__renderFlush;
  1381. params.renderFill = glnvg__renderFill;
  1382. params.renderStroke = glnvg__renderStroke;
  1383. params.renderTriangles = glnvg__renderTriangles;
  1384. params.renderDelete = glnvg__renderDelete;
  1385. params.userPtr = gl;
  1386. params.edgeAntiAlias = flags & NVG_ANTIALIAS ? 1 : 0;
  1387. gl->flags = flags;
  1388. ctx = nvgCreateInternal(&params);
  1389. if (ctx == NULL) goto error;
  1390. return ctx;
  1391. error:
  1392. // 'gl' is freed by nvgDeleteInternal.
  1393. if (ctx != NULL) nvgDeleteInternal(ctx);
  1394. return NULL;
  1395. }
  1396. #if defined NANOVG_GL2
  1397. void nvgDeleteGL2(NVGcontext* ctx)
  1398. #elif defined NANOVG_GL3
  1399. void nvgDeleteGL3(NVGcontext* ctx)
  1400. #elif defined NANOVG_GLES2
  1401. void nvgDeleteGLES2(NVGcontext* ctx)
  1402. #elif defined NANOVG_GLES3
  1403. void nvgDeleteGLES3(NVGcontext* ctx)
  1404. #endif
  1405. {
  1406. nvgDeleteInternal(ctx);
  1407. }
  1408. #if defined NANOVG_GL2
  1409. int nvglCreateImageFromHandleGL2(NVGcontext* ctx, GLuint textureId, int w, int h, int imageFlags)
  1410. #elif defined NANOVG_GL3
  1411. int nvglCreateImageFromHandleGL3(NVGcontext* ctx, GLuint textureId, int w, int h, int imageFlags)
  1412. #elif defined NANOVG_GLES2
  1413. int nvglCreateImageFromHandleGLES2(NVGcontext* ctx, GLuint textureId, int w, int h, int imageFlags)
  1414. #elif defined NANOVG_GLES3
  1415. int nvglCreateImageFromHandleGLES3(NVGcontext* ctx, GLuint textureId, int w, int h, int imageFlags)
  1416. #endif
  1417. {
  1418. GLNVGcontext* gl = (GLNVGcontext*)nvgInternalParams(ctx)->userPtr;
  1419. GLNVGtexture* tex = glnvg__allocTexture(gl);
  1420. if (tex == NULL) return 0;
  1421. tex->type = NVG_TEXTURE_RGBA;
  1422. tex->tex = textureId;
  1423. tex->flags = imageFlags;
  1424. tex->width = w;
  1425. tex->height = h;
  1426. return tex->id;
  1427. }
  1428. #if defined NANOVG_GL2
  1429. GLuint nvglImageHandleGL2(NVGcontext* ctx, int image)
  1430. #elif defined NANOVG_GL3
  1431. GLuint nvglImageHandleGL3(NVGcontext* ctx, int image)
  1432. #elif defined NANOVG_GLES2
  1433. GLuint nvglImageHandleGLES2(NVGcontext* ctx, int image)
  1434. #elif defined NANOVG_GLES3
  1435. GLuint nvglImageHandleGLES3(NVGcontext* ctx, int image)
  1436. #endif
  1437. {
  1438. GLNVGcontext* gl = (GLNVGcontext*)nvgInternalParams(ctx)->userPtr;
  1439. GLNVGtexture* tex = glnvg__findTexture(gl, image);
  1440. return tex->tex;
  1441. }
  1442. #endif /* NANOVG_GL_IMPLEMENTATION */