nanovg_bgfx.cpp 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267
  1. /*
  2. * Copyright 2011-2018 Branimir Karadzic. All rights reserved.
  3. * License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause
  4. */
  5. //
  6. // Copyright (c) 2009-2013 Mikko Mononen [email protected]
  7. //
  8. // This software is provided 'as-is', without any express or implied
  9. // warranty. In no event will the authors be held liable for any damages
  10. // arising from the use of this software.
  11. // Permission is granted to anyone to use this software for any purpose,
  12. // including commercial applications, and to alter it and redistribute it
  13. // freely, subject to the following restrictions:
  14. // 1. The origin of this software must not be misrepresented; you must not
  15. // claim that you wrote the original software. If you use this software
  16. // in a product, an acknowledgment in the product documentation would be
  17. // appreciated but is not required.
  18. // 2. Altered source versions must be plainly marked as such, and must not be
  19. // misrepresented as being the original software.
  20. // 3. This notice may not be removed or altered from any source distribution.
  21. //
  22. #define NVG_ANTIALIAS 1
  23. #include <stdlib.h>
  24. #include <math.h>
  25. #include "nanovg.h"
  26. #include <bgfx/bgfx.h>
  27. #include <bgfx/embedded_shader.h>
  28. #include <bx/bx.h>
  29. #include <bx/allocator.h>
  30. #include <bx/uint32_t.h>
  31. BX_PRAGMA_DIAGNOSTIC_IGNORED_MSVC(4244); // warning C4244: '=' : conversion from '' to '', possible loss of data
  32. #include "vs_nanovg_fill.bin.h"
  33. #include "fs_nanovg_fill.bin.h"
  34. static const bgfx::EmbeddedShader s_embeddedShaders[] =
  35. {
  36. BGFX_EMBEDDED_SHADER(vs_nanovg_fill),
  37. BGFX_EMBEDDED_SHADER(fs_nanovg_fill),
  38. BGFX_EMBEDDED_SHADER_END()
  39. };
  40. namespace
  41. {
  42. static bgfx::VertexDecl s_nvgDecl;
  43. enum GLNVGshaderType
  44. {
  45. NSVG_SHADER_FILLGRAD,
  46. NSVG_SHADER_FILLIMG,
  47. NSVG_SHADER_SIMPLE,
  48. NSVG_SHADER_IMG
  49. };
  50. // These are additional flags on top of NVGimageFlags.
  51. enum NVGimageFlagsGL {
  52. NVG_IMAGE_NODELETE = 1<<16, // Do not delete GL texture handle.
  53. };
  54. struct GLNVGtexture
  55. {
  56. bgfx::TextureHandle id;
  57. int width, height;
  58. int type;
  59. int flags;
  60. };
  61. struct GLNVGblend
  62. {
  63. uint64_t srcRGB;
  64. uint64_t dstRGB;
  65. uint64_t srcAlpha;
  66. uint64_t dstAlpha;
  67. };
  68. enum GLNVGcallType
  69. {
  70. GLNVG_FILL,
  71. GLNVG_CONVEXFILL,
  72. GLNVG_STROKE,
  73. GLNVG_TRIANGLES,
  74. };
  75. struct GLNVGcall
  76. {
  77. int type;
  78. int image;
  79. int pathOffset;
  80. int pathCount;
  81. int vertexOffset;
  82. int vertexCount;
  83. int uniformOffset;
  84. GLNVGblend blendFunc;
  85. };
  86. struct GLNVGpath
  87. {
  88. int fillOffset;
  89. int fillCount;
  90. int strokeOffset;
  91. int strokeCount;
  92. };
  93. struct GLNVGfragUniforms
  94. {
  95. float scissorMat[12]; // matrices are actually 3 vec4s
  96. float paintMat[12];
  97. NVGcolor innerCol;
  98. NVGcolor outerCol;
  99. // u_scissorExtScale
  100. float scissorExt[2];
  101. float scissorScale[2];
  102. // u_extentRadius
  103. float extent[2];
  104. float radius;
  105. // u_params
  106. float feather;
  107. float strokeMult;
  108. float texType;
  109. float type;
  110. };
  111. struct GLNVGcontext
  112. {
  113. bx::AllocatorI* allocator;
  114. bgfx::ProgramHandle prog;
  115. bgfx::UniformHandle u_scissorMat;
  116. bgfx::UniformHandle u_paintMat;
  117. bgfx::UniformHandle u_innerCol;
  118. bgfx::UniformHandle u_outerCol;
  119. bgfx::UniformHandle u_viewSize;
  120. bgfx::UniformHandle u_scissorExtScale;
  121. bgfx::UniformHandle u_extentRadius;
  122. bgfx::UniformHandle u_params;
  123. bgfx::UniformHandle u_halfTexel;
  124. bgfx::UniformHandle s_tex;
  125. uint64_t state;
  126. bgfx::TextureHandle th;
  127. bgfx::TextureHandle texMissing;
  128. bgfx::TransientVertexBuffer tvb;
  129. bgfx::ViewId viewId;
  130. struct GLNVGtexture* textures;
  131. float view[2];
  132. int ntextures;
  133. int ctextures;
  134. int textureId;
  135. int vertBuf;
  136. int fragSize;
  137. int edgeAntiAlias;
  138. // Per frame buffers
  139. struct GLNVGcall* calls;
  140. int ccalls;
  141. int ncalls;
  142. struct GLNVGpath* paths;
  143. int cpaths;
  144. int npaths;
  145. struct NVGvertex* verts;
  146. int cverts;
  147. int nverts;
  148. unsigned char* uniforms;
  149. int cuniforms;
  150. int nuniforms;
  151. };
  152. static struct GLNVGtexture* glnvg__allocTexture(struct GLNVGcontext* gl)
  153. {
  154. struct GLNVGtexture* tex = NULL;
  155. int i;
  156. for (i = 0; i < gl->ntextures; i++)
  157. {
  158. if (gl->textures[i].id.idx == bgfx::kInvalidHandle)
  159. {
  160. tex = &gl->textures[i];
  161. break;
  162. }
  163. }
  164. if (tex == NULL)
  165. {
  166. if (gl->ntextures+1 > gl->ctextures)
  167. {
  168. int old = gl->ctextures;
  169. gl->ctextures = (gl->ctextures == 0) ? 2 : gl->ctextures*2;
  170. gl->textures = (struct GLNVGtexture*)BX_REALLOC(gl->allocator, gl->textures, sizeof(struct GLNVGtexture)*gl->ctextures);
  171. bx::memSet(&gl->textures[old], 0xff, (gl->ctextures-old)*sizeof(struct GLNVGtexture) );
  172. if (gl->textures == NULL)
  173. {
  174. return NULL;
  175. }
  176. }
  177. tex = &gl->textures[gl->ntextures++];
  178. }
  179. bx::memSet(tex, 0, sizeof(*tex) );
  180. return tex;
  181. }
  182. static struct GLNVGtexture* glnvg__findTexture(struct GLNVGcontext* gl, int id)
  183. {
  184. int i;
  185. for (i = 0; i < gl->ntextures; i++)
  186. {
  187. if (gl->textures[i].id.idx == id)
  188. {
  189. return &gl->textures[i];
  190. }
  191. }
  192. return NULL;
  193. }
  194. static int glnvg__deleteTexture(struct GLNVGcontext* gl, int id)
  195. {
  196. for (int ii = 0; ii < gl->ntextures; ii++)
  197. {
  198. if (gl->textures[ii].id.idx == id)
  199. {
  200. if (bgfx::isValid(gl->textures[ii].id)
  201. && (gl->textures[ii].flags & NVG_IMAGE_NODELETE) == 0)
  202. {
  203. bgfx::destroy(gl->textures[ii].id);
  204. }
  205. bx::memSet(&gl->textures[ii], 0, sizeof(gl->textures[ii]) );
  206. gl->textures[ii].id.idx = bgfx::kInvalidHandle;
  207. return 1;
  208. }
  209. }
  210. return 0;
  211. }
  212. static int nvgRenderCreate(void* _userPtr)
  213. {
  214. struct GLNVGcontext* gl = (struct GLNVGcontext*)_userPtr;
  215. bgfx::RendererType::Enum type = bgfx::getRendererType();
  216. gl->prog = bgfx::createProgram(
  217. bgfx::createEmbeddedShader(s_embeddedShaders, type, "vs_nanovg_fill")
  218. , bgfx::createEmbeddedShader(s_embeddedShaders, type, "fs_nanovg_fill")
  219. , true
  220. );
  221. const bgfx::Memory* mem = bgfx::alloc(4*4*4);
  222. uint32_t* bgra8 = (uint32_t*)mem->data;
  223. bx::memSet(bgra8, 0, 4*4*4);
  224. gl->texMissing = bgfx::createTexture2D(4, 4, false, 1, bgfx::TextureFormat::BGRA8, 0, mem);
  225. gl->u_scissorMat = bgfx::createUniform("u_scissorMat", bgfx::UniformType::Mat3);
  226. gl->u_paintMat = bgfx::createUniform("u_paintMat", bgfx::UniformType::Mat3);
  227. gl->u_innerCol = bgfx::createUniform("u_innerCol", bgfx::UniformType::Vec4);
  228. gl->u_outerCol = bgfx::createUniform("u_outerCol", bgfx::UniformType::Vec4);
  229. gl->u_viewSize = bgfx::createUniform("u_viewSize", bgfx::UniformType::Vec4);
  230. gl->u_scissorExtScale = bgfx::createUniform("u_scissorExtScale", bgfx::UniformType::Vec4);
  231. gl->u_extentRadius = bgfx::createUniform("u_extentRadius", bgfx::UniformType::Vec4);
  232. gl->u_params = bgfx::createUniform("u_params", bgfx::UniformType::Vec4);
  233. gl->s_tex = bgfx::createUniform("s_tex", bgfx::UniformType::Sampler);
  234. if (bgfx::getRendererType() == bgfx::RendererType::Direct3D9)
  235. {
  236. gl->u_halfTexel = bgfx::createUniform("u_halfTexel", bgfx::UniformType::Vec4);
  237. }
  238. else
  239. {
  240. gl->u_halfTexel.idx = bgfx::kInvalidHandle;
  241. }
  242. s_nvgDecl
  243. .begin()
  244. .add(bgfx::Attrib::Position, 2, bgfx::AttribType::Float)
  245. .add(bgfx::Attrib::TexCoord0, 2, bgfx::AttribType::Float)
  246. .end();
  247. int align = 16;
  248. gl->fragSize = sizeof(struct GLNVGfragUniforms) + align - sizeof(struct GLNVGfragUniforms) % align;
  249. return 1;
  250. }
  251. static int nvgRenderCreateTexture(
  252. void* _userPtr
  253. , int _type
  254. , int _width
  255. , int _height
  256. , int _flags
  257. , const unsigned char* _rgba
  258. )
  259. {
  260. struct GLNVGcontext* gl = (struct GLNVGcontext*)_userPtr;
  261. struct GLNVGtexture* tex = glnvg__allocTexture(gl);
  262. if (tex == NULL)
  263. {
  264. return 0;
  265. }
  266. tex->width = _width;
  267. tex->height = _height;
  268. tex->type = _type;
  269. tex->flags = _flags;
  270. uint32_t bytesPerPixel = NVG_TEXTURE_RGBA == tex->type ? 4 : 1;
  271. uint32_t pitch = tex->width * bytesPerPixel;
  272. const bgfx::Memory* mem = NULL;
  273. if (NULL != _rgba)
  274. {
  275. mem = bgfx::copy(_rgba, tex->height * pitch);
  276. }
  277. tex->id = bgfx::createTexture2D(
  278. tex->width
  279. , tex->height
  280. , false
  281. , 1
  282. , NVG_TEXTURE_RGBA == _type ? bgfx::TextureFormat::RGBA8 : bgfx::TextureFormat::R8
  283. , BGFX_SAMPLER_NONE
  284. );
  285. if (NULL != mem)
  286. {
  287. bgfx::updateTexture2D(
  288. tex->id
  289. , 0
  290. , 0
  291. , 0
  292. , 0
  293. , tex->width
  294. , tex->height
  295. , mem
  296. );
  297. }
  298. return bgfx::isValid(tex->id) ? tex->id.idx : 0;
  299. }
  300. static int nvgRenderDeleteTexture(void* _userPtr, int image)
  301. {
  302. struct GLNVGcontext* gl = (struct GLNVGcontext*)_userPtr;
  303. return glnvg__deleteTexture(gl, image);
  304. }
  305. static int nvgRenderUpdateTexture(void* _userPtr, int image, int x, int y, int w, int h, const unsigned char* data)
  306. {
  307. struct GLNVGcontext* gl = (struct GLNVGcontext*)_userPtr;
  308. struct GLNVGtexture* tex = glnvg__findTexture(gl, image);
  309. if (tex == NULL)
  310. {
  311. return 0;
  312. }
  313. uint32_t bytesPerPixel = NVG_TEXTURE_RGBA == tex->type ? 4 : 1;
  314. uint32_t pitch = tex->width * bytesPerPixel;
  315. const bgfx::Memory* mem = bgfx::alloc(w * h * bytesPerPixel);
  316. bx::gather(mem->data, data + y * pitch + x * bytesPerPixel, w * bytesPerPixel, h, pitch);
  317. bgfx::updateTexture2D(
  318. tex->id
  319. , 0
  320. , 0
  321. , x
  322. , y
  323. , w
  324. , h
  325. , mem
  326. , UINT16_MAX
  327. );
  328. return 1;
  329. }
  330. static int nvgRenderGetTextureSize(void* _userPtr, int image, int* w, int* h)
  331. {
  332. struct GLNVGcontext* gl = (struct GLNVGcontext*)_userPtr;
  333. struct GLNVGtexture* tex = glnvg__findTexture(gl, image);
  334. if (NULL == tex
  335. || !bgfx::isValid(tex->id) )
  336. {
  337. return 0;
  338. }
  339. *w = tex->width;
  340. *h = tex->height;
  341. return 1;
  342. }
  343. static void glnvg__xformToMat3x4(float* m3, float* t)
  344. {
  345. m3[ 0] = t[0];
  346. m3[ 1] = t[1];
  347. m3[ 2] = 0.0f;
  348. m3[ 3] = 0.0f;
  349. m3[ 4] = t[2];
  350. m3[ 5] = t[3];
  351. m3[ 6] = 0.0f;
  352. m3[ 7] = 0.0f;
  353. m3[ 8] = t[4];
  354. m3[ 9] = t[5];
  355. m3[10] = 1.0f;
  356. m3[11] = 0.0f;
  357. }
  358. static NVGcolor glnvg__premulColor(NVGcolor c)
  359. {
  360. c.r *= c.a;
  361. c.g *= c.a;
  362. c.b *= c.a;
  363. return c;
  364. }
  365. static int glnvg__convertPaint(
  366. struct GLNVGcontext* gl
  367. , struct GLNVGfragUniforms* frag
  368. , struct NVGpaint* paint
  369. , struct NVGscissor* scissor
  370. , float width
  371. , float fringe
  372. )
  373. {
  374. struct GLNVGtexture* tex = NULL;
  375. float invxform[6] = {};
  376. bx::memSet(frag, 0, sizeof(*frag) );
  377. frag->innerCol = glnvg__premulColor(paint->innerColor);
  378. frag->outerCol = glnvg__premulColor(paint->outerColor);
  379. if (scissor->extent[0] < -0.5f || scissor->extent[1] < -0.5f)
  380. {
  381. bx::memSet(frag->scissorMat, 0, sizeof(frag->scissorMat) );
  382. frag->scissorExt[0] = 1.0f;
  383. frag->scissorExt[1] = 1.0f;
  384. frag->scissorScale[0] = 1.0f;
  385. frag->scissorScale[1] = 1.0f;
  386. }
  387. else
  388. {
  389. nvgTransformInverse(invxform, scissor->xform);
  390. glnvg__xformToMat3x4(frag->scissorMat, invxform);
  391. frag->scissorExt[0] = scissor->extent[0];
  392. frag->scissorExt[1] = scissor->extent[1];
  393. frag->scissorScale[0] = sqrtf(scissor->xform[0]*scissor->xform[0] + scissor->xform[2]*scissor->xform[2]) / fringe;
  394. frag->scissorScale[1] = sqrtf(scissor->xform[1]*scissor->xform[1] + scissor->xform[3]*scissor->xform[3]) / fringe;
  395. }
  396. bx::memCopy(frag->extent, paint->extent, sizeof(frag->extent) );
  397. frag->strokeMult = (width*0.5f + fringe*0.5f) / fringe;
  398. gl->th = gl->texMissing;
  399. if (paint->image != 0)
  400. {
  401. tex = glnvg__findTexture(gl, paint->image);
  402. if (tex == NULL)
  403. {
  404. return 0;
  405. }
  406. nvgTransformInverse(invxform, paint->xform);
  407. frag->type = NSVG_SHADER_FILLIMG;
  408. if (tex->type == NVG_TEXTURE_RGBA)
  409. {
  410. frag->texType = (tex->flags & NVG_IMAGE_PREMULTIPLIED) ? 0.0f : 1.0f;
  411. }
  412. else
  413. {
  414. frag->texType = 2.0f;
  415. }
  416. gl->th = tex->id;
  417. }
  418. else
  419. {
  420. frag->type = NSVG_SHADER_FILLGRAD;
  421. frag->radius = paint->radius;
  422. frag->feather = paint->feather;
  423. nvgTransformInverse(invxform, paint->xform);
  424. }
  425. glnvg__xformToMat3x4(frag->paintMat, invxform);
  426. return 1;
  427. }
  428. static void glnvg__mat3(float* dst, float* src)
  429. {
  430. dst[0] = src[ 0];
  431. dst[1] = src[ 1];
  432. dst[2] = src[ 2];
  433. dst[3] = src[ 4];
  434. dst[4] = src[ 5];
  435. dst[5] = src[ 6];
  436. dst[6] = src[ 8];
  437. dst[7] = src[ 9];
  438. dst[8] = src[10];
  439. }
  440. static struct GLNVGfragUniforms* nvg__fragUniformPtr(struct GLNVGcontext* gl, int i)
  441. {
  442. return (struct GLNVGfragUniforms*)&gl->uniforms[i];
  443. }
  444. static void nvgRenderSetUniforms(struct GLNVGcontext* gl, int uniformOffset, int image)
  445. {
  446. struct GLNVGfragUniforms* frag = nvg__fragUniformPtr(gl, uniformOffset);
  447. float tmp[9]; // Maybe there's a way to get rid of this...
  448. glnvg__mat3(tmp, frag->scissorMat);
  449. bgfx::setUniform(gl->u_scissorMat, tmp);
  450. glnvg__mat3(tmp, frag->paintMat);
  451. bgfx::setUniform(gl->u_paintMat, tmp);
  452. bgfx::setUniform(gl->u_innerCol, frag->innerCol.rgba);
  453. bgfx::setUniform(gl->u_outerCol, frag->outerCol.rgba);
  454. bgfx::setUniform(gl->u_scissorExtScale, &frag->scissorExt[0]);
  455. bgfx::setUniform(gl->u_extentRadius, &frag->extent[0]);
  456. bgfx::setUniform(gl->u_params, &frag->feather);
  457. bgfx::TextureHandle handle = gl->texMissing;
  458. if (image != 0)
  459. {
  460. struct GLNVGtexture* tex = glnvg__findTexture(gl, image);
  461. if (tex != NULL)
  462. {
  463. handle = tex->id;
  464. if (bgfx::isValid(gl->u_halfTexel) )
  465. {
  466. float halfTexel[4] = { 0.5f / tex->width, 0.5f / tex->height };
  467. bgfx::setUniform(gl->u_halfTexel, halfTexel);
  468. }
  469. }
  470. }
  471. gl->th = handle;
  472. }
  473. static void nvgRenderViewport(void* _userPtr, float width, float height, float devicePixelRatio)
  474. {
  475. struct GLNVGcontext* gl = (struct GLNVGcontext*)_userPtr;
  476. gl->view[0] = width;
  477. gl->view[1] = height;
  478. bgfx::setViewRect(gl->viewId, 0, 0, width * devicePixelRatio, height * devicePixelRatio);
  479. }
  480. static void fan(uint32_t _start, uint32_t _count)
  481. {
  482. uint32_t numTris = _count-2;
  483. bgfx::TransientIndexBuffer tib;
  484. bgfx::allocTransientIndexBuffer(&tib, numTris*3);
  485. uint16_t* data = (uint16_t*)tib.data;
  486. for (uint32_t ii = 0; ii < numTris; ++ii)
  487. {
  488. data[ii*3+0] = _start;
  489. data[ii*3+1] = _start + ii + 1;
  490. data[ii*3+2] = _start + ii + 2;
  491. }
  492. bgfx::setIndexBuffer(&tib);
  493. }
  494. static void glnvg__fill(struct GLNVGcontext* gl, struct GLNVGcall* call)
  495. {
  496. struct GLNVGpath* paths = &gl->paths[call->pathOffset];
  497. int i, npaths = call->pathCount;
  498. // set bindpoint for solid loc
  499. nvgRenderSetUniforms(gl, call->uniformOffset, 0);
  500. for (i = 0; i < npaths; i++)
  501. {
  502. if (2 < paths[i].fillCount)
  503. {
  504. bgfx::setState(0);
  505. bgfx::setStencil(0
  506. | BGFX_STENCIL_TEST_ALWAYS
  507. | BGFX_STENCIL_FUNC_RMASK(0xff)
  508. | BGFX_STENCIL_OP_FAIL_S_KEEP
  509. | BGFX_STENCIL_OP_FAIL_Z_KEEP
  510. | BGFX_STENCIL_OP_PASS_Z_INCR
  511. , 0
  512. | BGFX_STENCIL_TEST_ALWAYS
  513. | BGFX_STENCIL_FUNC_RMASK(0xff)
  514. | BGFX_STENCIL_OP_FAIL_S_KEEP
  515. | BGFX_STENCIL_OP_FAIL_Z_KEEP
  516. | BGFX_STENCIL_OP_PASS_Z_DECR
  517. );
  518. bgfx::setVertexBuffer(0, &gl->tvb);
  519. bgfx::setTexture(0, gl->s_tex, gl->th);
  520. fan(paths[i].fillOffset, paths[i].fillCount);
  521. bgfx::submit(gl->viewId, gl->prog);
  522. }
  523. }
  524. // Draw aliased off-pixels
  525. nvgRenderSetUniforms(gl, call->uniformOffset + gl->fragSize, call->image);
  526. if (gl->edgeAntiAlias)
  527. {
  528. // Draw fringes
  529. for (i = 0; i < npaths; i++)
  530. {
  531. bgfx::setState(gl->state
  532. | BGFX_STATE_PT_TRISTRIP
  533. );
  534. bgfx::setStencil(0
  535. | BGFX_STENCIL_TEST_EQUAL
  536. | BGFX_STENCIL_FUNC_RMASK(0xff)
  537. | BGFX_STENCIL_OP_FAIL_S_KEEP
  538. | BGFX_STENCIL_OP_FAIL_Z_KEEP
  539. | BGFX_STENCIL_OP_PASS_Z_KEEP
  540. );
  541. bgfx::setVertexBuffer(0, &gl->tvb, paths[i].strokeOffset, paths[i].strokeCount);
  542. bgfx::setTexture(0, gl->s_tex, gl->th);
  543. bgfx::submit(gl->viewId, gl->prog);
  544. }
  545. }
  546. // Draw fill
  547. bgfx::setState(gl->state);
  548. bgfx::setVertexBuffer(0, &gl->tvb, call->vertexOffset, call->vertexCount);
  549. bgfx::setTexture(0, gl->s_tex, gl->th);
  550. bgfx::setStencil(0
  551. | BGFX_STENCIL_TEST_NOTEQUAL
  552. | BGFX_STENCIL_FUNC_RMASK(0xff)
  553. | BGFX_STENCIL_OP_FAIL_S_ZERO
  554. | BGFX_STENCIL_OP_FAIL_Z_ZERO
  555. | BGFX_STENCIL_OP_PASS_Z_ZERO
  556. );
  557. bgfx::submit(gl->viewId, gl->prog);
  558. }
  559. static void glnvg__convexFill(struct GLNVGcontext* gl, struct GLNVGcall* call)
  560. {
  561. struct GLNVGpath* paths = &gl->paths[call->pathOffset];
  562. int i, npaths = call->pathCount;
  563. nvgRenderSetUniforms(gl, call->uniformOffset, call->image);
  564. for (i = 0; i < npaths; i++)
  565. {
  566. if (paths[i].fillCount == 0) continue;
  567. bgfx::setState(gl->state);
  568. bgfx::setVertexBuffer(0, &gl->tvb);
  569. bgfx::setTexture(0, gl->s_tex, gl->th);
  570. fan(paths[i].fillOffset, paths[i].fillCount);
  571. bgfx::submit(gl->viewId, gl->prog);
  572. }
  573. if (gl->edgeAntiAlias)
  574. {
  575. // Draw fringes
  576. for (i = 0; i < npaths; i++)
  577. {
  578. bgfx::setState(gl->state
  579. | BGFX_STATE_PT_TRISTRIP
  580. );
  581. bgfx::setVertexBuffer(0, &gl->tvb, paths[i].strokeOffset, paths[i].strokeCount);
  582. bgfx::setTexture(0, gl->s_tex, gl->th);
  583. bgfx::submit(gl->viewId, gl->prog);
  584. }
  585. }
  586. }
  587. static void glnvg__stroke(struct GLNVGcontext* gl, struct GLNVGcall* call)
  588. {
  589. struct GLNVGpath* paths = &gl->paths[call->pathOffset];
  590. int npaths = call->pathCount, i;
  591. nvgRenderSetUniforms(gl, call->uniformOffset, call->image);
  592. // Draw Strokes
  593. for (i = 0; i < npaths; i++)
  594. {
  595. bgfx::setState(gl->state
  596. | BGFX_STATE_PT_TRISTRIP
  597. );
  598. bgfx::setVertexBuffer(0, &gl->tvb, paths[i].strokeOffset, paths[i].strokeCount);
  599. bgfx::setTexture(0, gl->s_tex, gl->th);
  600. bgfx::submit(gl->viewId, gl->prog);
  601. }
  602. }
  603. static void glnvg__triangles(struct GLNVGcontext* gl, struct GLNVGcall* call)
  604. {
  605. if (3 <= call->vertexCount)
  606. {
  607. nvgRenderSetUniforms(gl, call->uniformOffset, call->image);
  608. bgfx::setState(gl->state);
  609. bgfx::setVertexBuffer(0, &gl->tvb, call->vertexOffset, call->vertexCount);
  610. bgfx::setTexture(0, gl->s_tex, gl->th);
  611. bgfx::submit(gl->viewId, gl->prog);
  612. }
  613. }
  614. static const uint64_t s_blend[] =
  615. {
  616. BGFX_STATE_BLEND_ZERO,
  617. BGFX_STATE_BLEND_ONE,
  618. BGFX_STATE_BLEND_SRC_COLOR,
  619. BGFX_STATE_BLEND_INV_SRC_COLOR,
  620. BGFX_STATE_BLEND_DST_COLOR,
  621. BGFX_STATE_BLEND_INV_DST_COLOR,
  622. BGFX_STATE_BLEND_SRC_ALPHA,
  623. BGFX_STATE_BLEND_INV_SRC_ALPHA,
  624. BGFX_STATE_BLEND_DST_ALPHA,
  625. BGFX_STATE_BLEND_INV_DST_ALPHA,
  626. BGFX_STATE_BLEND_SRC_ALPHA_SAT,
  627. };
  628. static uint64_t glnvg_convertBlendFuncFactor(int factor)
  629. {
  630. const uint32_t numtz = bx::uint32_cnttz(factor);
  631. const uint32_t idx = bx::uint32_min(numtz, BX_COUNTOF(s_blend)-1);
  632. return s_blend[idx];
  633. }
  634. static GLNVGblend glnvg__blendCompositeOperation(NVGcompositeOperationState op)
  635. {
  636. GLNVGblend blend;
  637. blend.srcRGB = glnvg_convertBlendFuncFactor(op.srcRGB);
  638. blend.dstRGB = glnvg_convertBlendFuncFactor(op.dstRGB);
  639. blend.srcAlpha = glnvg_convertBlendFuncFactor(op.srcAlpha);
  640. blend.dstAlpha = glnvg_convertBlendFuncFactor(op.dstAlpha);
  641. if (blend.srcRGB == BGFX_STATE_NONE || blend.dstRGB == BGFX_STATE_NONE || blend.srcAlpha == BGFX_STATE_NONE || blend.dstAlpha == BGFX_STATE_NONE)
  642. {
  643. blend.srcRGB = BGFX_STATE_BLEND_ONE;
  644. blend.dstRGB = BGFX_STATE_BLEND_INV_SRC_ALPHA;
  645. blend.srcAlpha = BGFX_STATE_BLEND_ONE;
  646. blend.dstAlpha = BGFX_STATE_BLEND_INV_SRC_ALPHA;
  647. }
  648. return blend;
  649. }
  650. static void nvgRenderFlush(void* _userPtr)
  651. {
  652. struct GLNVGcontext* gl = (struct GLNVGcontext*)_userPtr;
  653. if (gl->ncalls > 0)
  654. {
  655. bgfx::allocTransientVertexBuffer(&gl->tvb, gl->nverts, s_nvgDecl);
  656. int allocated = gl->tvb.size/gl->tvb.stride;
  657. if (allocated < gl->nverts)
  658. {
  659. gl->nverts = allocated;
  660. BX_WARN(true, "Vertex number truncated due to transient vertex buffer overflow");
  661. }
  662. bx::memCopy(gl->tvb.data, gl->verts, gl->nverts * sizeof(struct NVGvertex) );
  663. bgfx::setUniform(gl->u_viewSize, gl->view);
  664. for (uint32_t ii = 0, num = gl->ncalls; ii < num; ++ii)
  665. {
  666. struct GLNVGcall* call = &gl->calls[ii];
  667. const GLNVGblend* blend = &call->blendFunc;
  668. gl->state = BGFX_STATE_BLEND_FUNC_SEPARATE(blend->srcRGB, blend->dstRGB, blend->srcAlpha, blend->dstAlpha)
  669. | BGFX_STATE_WRITE_RGB
  670. | BGFX_STATE_WRITE_A
  671. ;
  672. switch (call->type)
  673. {
  674. case GLNVG_FILL:
  675. glnvg__fill(gl, call);
  676. break;
  677. case GLNVG_CONVEXFILL:
  678. glnvg__convexFill(gl, call);
  679. break;
  680. case GLNVG_STROKE:
  681. glnvg__stroke(gl, call);
  682. break;
  683. case GLNVG_TRIANGLES:
  684. glnvg__triangles(gl, call);
  685. break;
  686. }
  687. }
  688. }
  689. // Reset calls
  690. gl->nverts = 0;
  691. gl->npaths = 0;
  692. gl->ncalls = 0;
  693. gl->nuniforms = 0;
  694. }
  695. static int glnvg__maxVertCount(const struct NVGpath* paths, int npaths)
  696. {
  697. int i, count = 0;
  698. for (i = 0; i < npaths; i++)
  699. {
  700. count += paths[i].nfill;
  701. count += paths[i].nstroke;
  702. }
  703. return count;
  704. }
  705. static int glnvg__maxi(int a, int b) { return a > b ? a : b; }
  706. static struct GLNVGcall* glnvg__allocCall(struct GLNVGcontext* gl)
  707. {
  708. struct GLNVGcall* ret = NULL;
  709. if (gl->ncalls+1 > gl->ccalls)
  710. {
  711. gl->ccalls = gl->ccalls == 0 ? 32 : gl->ccalls * 2;
  712. gl->calls = (struct GLNVGcall*)BX_REALLOC(gl->allocator, gl->calls, sizeof(struct GLNVGcall) * gl->ccalls);
  713. }
  714. ret = &gl->calls[gl->ncalls++];
  715. bx::memSet(ret, 0, sizeof(struct GLNVGcall) );
  716. return ret;
  717. }
  718. static int glnvg__allocPaths(struct GLNVGcontext* gl, int n)
  719. {
  720. int ret = 0;
  721. if (gl->npaths + n > gl->cpaths) {
  722. GLNVGpath* paths;
  723. int cpaths = glnvg__maxi(gl->npaths + n, 128) + gl->cpaths / 2; // 1.5x Overallocate
  724. paths = (GLNVGpath*)BX_REALLOC(gl->allocator, gl->paths, sizeof(GLNVGpath) * cpaths);
  725. if (paths == NULL) return -1;
  726. gl->paths = paths;
  727. gl->cpaths = cpaths;
  728. }
  729. ret = gl->npaths;
  730. gl->npaths += n;
  731. return ret;
  732. }
  733. static int glnvg__allocVerts(GLNVGcontext* gl, int n)
  734. {
  735. int ret = 0;
  736. if (gl->nverts+n > gl->cverts)
  737. {
  738. NVGvertex* verts;
  739. int cverts = glnvg__maxi(gl->nverts + n, 4096) + gl->cverts/2; // 1.5x Overallocate
  740. verts = (NVGvertex*)BX_REALLOC(gl->allocator, gl->verts, sizeof(NVGvertex) * cverts);
  741. if (verts == NULL) return -1;
  742. gl->verts = verts;
  743. gl->cverts = cverts;
  744. }
  745. ret = gl->nverts;
  746. gl->nverts += n;
  747. return ret;
  748. }
  749. static int glnvg__allocFragUniforms(struct GLNVGcontext* gl, int n)
  750. {
  751. int ret = 0, structSize = gl->fragSize;
  752. if (gl->nuniforms+n > gl->cuniforms)
  753. {
  754. gl->cuniforms = gl->cuniforms == 0 ? glnvg__maxi(n, 32) : gl->cuniforms * 2;
  755. gl->uniforms = (unsigned char*)BX_REALLOC(gl->allocator, gl->uniforms, gl->cuniforms * structSize);
  756. }
  757. ret = gl->nuniforms * structSize;
  758. gl->nuniforms += n;
  759. return ret;
  760. }
  761. static void glnvg__vset(struct NVGvertex* vtx, float x, float y, float u, float v)
  762. {
  763. vtx->x = x;
  764. vtx->y = y;
  765. vtx->u = u;
  766. vtx->v = v;
  767. }
  768. static void nvgRenderFill(
  769. void* _userPtr
  770. , NVGpaint* paint
  771. , NVGcompositeOperationState compositeOperation
  772. , NVGscissor* scissor
  773. , float fringe
  774. , const float* bounds
  775. , const NVGpath* paths
  776. , int npaths
  777. )
  778. {
  779. struct GLNVGcontext* gl = (struct GLNVGcontext*)_userPtr;
  780. struct GLNVGcall* call = glnvg__allocCall(gl);
  781. struct NVGvertex* quad;
  782. struct GLNVGfragUniforms* frag;
  783. int i, maxverts, offset;
  784. call->type = GLNVG_FILL;
  785. call->pathOffset = glnvg__allocPaths(gl, npaths);
  786. call->pathCount = npaths;
  787. call->image = paint->image;
  788. call->blendFunc = glnvg__blendCompositeOperation(compositeOperation);
  789. if (npaths == 1 && paths[0].convex)
  790. {
  791. call->type = GLNVG_CONVEXFILL;
  792. }
  793. // Allocate vertices for all the paths.
  794. maxverts = glnvg__maxVertCount(paths, npaths) + 6;
  795. offset = glnvg__allocVerts(gl, maxverts);
  796. for (i = 0; i < npaths; i++)
  797. {
  798. struct GLNVGpath* copy = &gl->paths[call->pathOffset + i];
  799. const struct NVGpath* path = &paths[i];
  800. bx::memSet(copy, 0, sizeof(struct GLNVGpath) );
  801. if (path->nfill > 0)
  802. {
  803. copy->fillOffset = offset;
  804. copy->fillCount = path->nfill;
  805. bx::memCopy(&gl->verts[offset], path->fill, sizeof(struct NVGvertex) * path->nfill);
  806. offset += path->nfill;
  807. }
  808. if (path->nstroke > 0)
  809. {
  810. copy->strokeOffset = offset;
  811. copy->strokeCount = path->nstroke;
  812. bx::memCopy(&gl->verts[offset], path->stroke, sizeof(struct NVGvertex) * path->nstroke);
  813. offset += path->nstroke;
  814. }
  815. }
  816. // Quad
  817. call->vertexOffset = offset;
  818. call->vertexCount = 6;
  819. quad = &gl->verts[call->vertexOffset];
  820. glnvg__vset(&quad[0], bounds[0], bounds[3], 0.5f, 1.0f);
  821. glnvg__vset(&quad[1], bounds[2], bounds[3], 0.5f, 1.0f);
  822. glnvg__vset(&quad[2], bounds[2], bounds[1], 0.5f, 1.0f);
  823. glnvg__vset(&quad[3], bounds[0], bounds[3], 0.5f, 1.0f);
  824. glnvg__vset(&quad[4], bounds[2], bounds[1], 0.5f, 1.0f);
  825. glnvg__vset(&quad[5], bounds[0], bounds[1], 0.5f, 1.0f);
  826. // Setup uniforms for draw calls
  827. if (call->type == GLNVG_FILL)
  828. {
  829. call->uniformOffset = glnvg__allocFragUniforms(gl, 2);
  830. // Simple shader for stencil
  831. frag = nvg__fragUniformPtr(gl, call->uniformOffset);
  832. bx::memSet(frag, 0, sizeof(*frag) );
  833. frag->type = NSVG_SHADER_SIMPLE;
  834. // Fill shader
  835. glnvg__convertPaint(gl, nvg__fragUniformPtr(gl, call->uniformOffset + gl->fragSize), paint, scissor, fringe, fringe);
  836. }
  837. else
  838. {
  839. call->uniformOffset = glnvg__allocFragUniforms(gl, 1);
  840. // Fill shader
  841. glnvg__convertPaint(gl, nvg__fragUniformPtr(gl, call->uniformOffset), paint, scissor, fringe, fringe);
  842. }
  843. }
  844. static void nvgRenderStroke(
  845. void* _userPtr
  846. , struct NVGpaint* paint
  847. , NVGcompositeOperationState compositeOperation
  848. , struct NVGscissor* scissor
  849. , float fringe
  850. , float strokeWidth
  851. , const struct NVGpath* paths
  852. , int npaths
  853. )
  854. {
  855. struct GLNVGcontext* gl = (struct GLNVGcontext*)_userPtr;
  856. struct GLNVGcall* call = glnvg__allocCall(gl);
  857. int i, maxverts, offset;
  858. call->type = GLNVG_STROKE;
  859. call->pathOffset = glnvg__allocPaths(gl, npaths);
  860. call->pathCount = npaths;
  861. call->image = paint->image;
  862. call->blendFunc = glnvg__blendCompositeOperation(compositeOperation);
  863. // Allocate vertices for all the paths.
  864. maxverts = glnvg__maxVertCount(paths, npaths);
  865. offset = glnvg__allocVerts(gl, maxverts);
  866. for (i = 0; i < npaths; i++)
  867. {
  868. struct GLNVGpath* copy = &gl->paths[call->pathOffset + i];
  869. const struct NVGpath* path = &paths[i];
  870. bx::memSet(copy, 0, sizeof(struct GLNVGpath) );
  871. if (path->nstroke)
  872. {
  873. copy->strokeOffset = offset;
  874. copy->strokeCount = path->nstroke;
  875. bx::memCopy(&gl->verts[offset], path->stroke, sizeof(struct NVGvertex) * path->nstroke);
  876. offset += path->nstroke;
  877. }
  878. }
  879. // Fill shader
  880. call->uniformOffset = glnvg__allocFragUniforms(gl, 1);
  881. glnvg__convertPaint(gl, nvg__fragUniformPtr(gl, call->uniformOffset), paint, scissor, strokeWidth, fringe);
  882. }
  883. static void nvgRenderTriangles(void* _userPtr, struct NVGpaint* paint, NVGcompositeOperationState compositeOperation, struct NVGscissor* scissor,
  884. const struct NVGvertex* verts, int nverts)
  885. {
  886. struct GLNVGcontext* gl = (struct GLNVGcontext*)_userPtr;
  887. struct GLNVGcall* call = glnvg__allocCall(gl);
  888. struct GLNVGfragUniforms* frag;
  889. call->type = GLNVG_TRIANGLES;
  890. call->image = paint->image;
  891. call->blendFunc = glnvg__blendCompositeOperation(compositeOperation);
  892. // Allocate vertices for all the paths.
  893. call->vertexOffset = glnvg__allocVerts(gl, nverts);
  894. call->vertexCount = nverts;
  895. bx::memCopy(&gl->verts[call->vertexOffset], verts, sizeof(struct NVGvertex) * nverts);
  896. // Fill shader
  897. call->uniformOffset = glnvg__allocFragUniforms(gl, 1);
  898. frag = nvg__fragUniformPtr(gl, call->uniformOffset);
  899. glnvg__convertPaint(gl, frag, paint, scissor, 1.0f, 1.0f);
  900. frag->type = NSVG_SHADER_IMG;
  901. }
  902. static void nvgRenderDelete(void* _userPtr)
  903. {
  904. struct GLNVGcontext* gl = (struct GLNVGcontext*)_userPtr;
  905. if (gl == NULL)
  906. {
  907. return;
  908. }
  909. bgfx::destroy(gl->prog);
  910. bgfx::destroy(gl->texMissing);
  911. bgfx::destroy(gl->u_scissorMat);
  912. bgfx::destroy(gl->u_paintMat);
  913. bgfx::destroy(gl->u_innerCol);
  914. bgfx::destroy(gl->u_outerCol);
  915. bgfx::destroy(gl->u_viewSize);
  916. bgfx::destroy(gl->u_scissorExtScale);
  917. bgfx::destroy(gl->u_extentRadius);
  918. bgfx::destroy(gl->u_params);
  919. bgfx::destroy(gl->s_tex);
  920. if (bgfx::isValid(gl->u_halfTexel) )
  921. {
  922. bgfx::destroy(gl->u_halfTexel);
  923. }
  924. for (uint32_t ii = 0, num = gl->ntextures; ii < num; ++ii)
  925. {
  926. if (bgfx::isValid(gl->textures[ii].id)
  927. && (gl->textures[ii].flags & NVG_IMAGE_NODELETE) == 0)
  928. {
  929. bgfx::destroy(gl->textures[ii].id);
  930. }
  931. }
  932. BX_FREE(gl->allocator, gl->uniforms);
  933. BX_FREE(gl->allocator, gl->verts);
  934. BX_FREE(gl->allocator, gl->paths);
  935. BX_FREE(gl->allocator, gl->calls);
  936. BX_FREE(gl->allocator, gl->textures);
  937. BX_FREE(gl->allocator, gl);
  938. }
  939. } // namespace
  940. NVGcontext* nvgCreate(int32_t _edgeaa, bgfx::ViewId _viewId, bx::AllocatorI* _allocator)
  941. {
  942. if (NULL == _allocator)
  943. {
  944. static bx::DefaultAllocator allocator;
  945. _allocator = &allocator;
  946. }
  947. struct NVGparams params;
  948. struct NVGcontext* ctx = NULL;
  949. struct GLNVGcontext* gl = (struct GLNVGcontext*)BX_ALLOC(_allocator, sizeof(struct GLNVGcontext) );
  950. if (gl == NULL)
  951. {
  952. goto error;
  953. }
  954. bx::memSet(gl, 0, sizeof(struct GLNVGcontext) );
  955. bx::memSet(&params, 0, sizeof(params) );
  956. params.renderCreate = nvgRenderCreate;
  957. params.renderCreateTexture = nvgRenderCreateTexture;
  958. params.renderDeleteTexture = nvgRenderDeleteTexture;
  959. params.renderUpdateTexture = nvgRenderUpdateTexture;
  960. params.renderGetTextureSize = nvgRenderGetTextureSize;
  961. params.renderViewport = nvgRenderViewport;
  962. params.renderFlush = nvgRenderFlush;
  963. params.renderFill = nvgRenderFill;
  964. params.renderStroke = nvgRenderStroke;
  965. params.renderTriangles = nvgRenderTriangles;
  966. params.renderDelete = nvgRenderDelete;
  967. params.userPtr = gl;
  968. params.edgeAntiAlias = _edgeaa;
  969. gl->allocator = _allocator;
  970. gl->edgeAntiAlias = _edgeaa;
  971. gl->viewId = _viewId;
  972. ctx = nvgCreateInternal(&params);
  973. if (ctx == NULL) goto error;
  974. return ctx;
  975. error:
  976. // 'gl' is freed by nvgDeleteInternal.
  977. if (ctx != NULL)
  978. {
  979. nvgDeleteInternal(ctx);
  980. }
  981. return NULL;
  982. }
  983. NVGcontext* nvgCreate(int32_t _edgeaa, bgfx::ViewId _viewId) {
  984. return nvgCreate(_edgeaa, _viewId, NULL);
  985. }
  986. void nvgDelete(NVGcontext* _ctx)
  987. {
  988. nvgDeleteInternal(_ctx);
  989. }
  990. void nvgSetViewId(NVGcontext* _ctx, bgfx::ViewId _viewId)
  991. {
  992. struct NVGparams* params = nvgInternalParams(_ctx);
  993. struct GLNVGcontext* gl = (struct GLNVGcontext*)params->userPtr;
  994. gl->viewId = _viewId;
  995. }
  996. uint16_t nvgGetViewId(struct NVGcontext* _ctx)
  997. {
  998. struct NVGparams* params = nvgInternalParams(_ctx);
  999. struct GLNVGcontext* gl = (struct GLNVGcontext*)params->userPtr;
  1000. return gl->viewId;
  1001. }
  1002. bgfx::TextureHandle nvglImageHandle(NVGcontext* _ctx, int32_t _image)
  1003. {
  1004. GLNVGcontext* gl = (GLNVGcontext*)nvgInternalParams(_ctx)->userPtr;
  1005. GLNVGtexture* tex = glnvg__findTexture(gl, _image);
  1006. return tex->id;
  1007. }
  1008. NVGLUframebuffer* nvgluCreateFramebuffer(NVGcontext* ctx, int32_t width, int32_t height, int32_t imageFlags, bgfx::ViewId viewId)
  1009. {
  1010. NVGLUframebuffer* framebuffer = nvgluCreateFramebuffer(ctx, width, height, imageFlags);
  1011. if (framebuffer != NULL)
  1012. {
  1013. nvgluSetViewFramebuffer(viewId, framebuffer);
  1014. }
  1015. return framebuffer;
  1016. }
  1017. NVGLUframebuffer* nvgluCreateFramebuffer(NVGcontext* _ctx, int32_t _width, int32_t _height, int32_t _imageFlags)
  1018. {
  1019. BX_UNUSED(_imageFlags);
  1020. bgfx::TextureHandle textures[] =
  1021. {
  1022. bgfx::createTexture2D(_width, _height, false, 1, bgfx::TextureFormat::RGBA8, BGFX_TEXTURE_RT),
  1023. bgfx::createTexture2D(_width, _height, false, 1, bgfx::TextureFormat::D24S8, BGFX_TEXTURE_RT | BGFX_TEXTURE_RT_WRITE_ONLY)
  1024. };
  1025. bgfx::FrameBufferHandle fbh = bgfx::createFrameBuffer(
  1026. BX_COUNTOF(textures)
  1027. , textures
  1028. , true
  1029. );
  1030. if (!bgfx::isValid(fbh) )
  1031. {
  1032. return NULL;
  1033. }
  1034. struct NVGparams* params = nvgInternalParams(_ctx);
  1035. struct GLNVGcontext* gl = (struct GLNVGcontext*)params->userPtr;
  1036. struct GLNVGtexture* tex = glnvg__allocTexture(gl);
  1037. if (NULL == tex)
  1038. {
  1039. bgfx::destroy(fbh);
  1040. return NULL;
  1041. }
  1042. tex->width = _width;
  1043. tex->height = _height;
  1044. tex->type = NVG_TEXTURE_RGBA;
  1045. tex->flags = _imageFlags | NVG_IMAGE_PREMULTIPLIED;
  1046. tex->id = bgfx::getTexture(fbh);
  1047. NVGLUframebuffer* framebuffer = BX_NEW(gl->allocator, NVGLUframebuffer);
  1048. framebuffer->ctx = _ctx;
  1049. framebuffer->image = tex->id.idx;
  1050. framebuffer->handle = fbh;
  1051. return framebuffer;
  1052. }
  1053. void nvgluBindFramebuffer(NVGLUframebuffer* _framebuffer)
  1054. {
  1055. static NVGcontext* s_prevCtx = NULL;
  1056. static bgfx::ViewId s_prevViewId;
  1057. if (_framebuffer != NULL)
  1058. {
  1059. s_prevCtx = _framebuffer->ctx;
  1060. s_prevViewId = nvgGetViewId(_framebuffer->ctx);
  1061. nvgSetViewId(_framebuffer->ctx, _framebuffer->viewId);
  1062. }
  1063. else if (s_prevCtx != NULL)
  1064. {
  1065. nvgSetViewId(s_prevCtx, s_prevViewId);
  1066. }
  1067. }
  1068. void nvgluDeleteFramebuffer(NVGLUframebuffer* _framebuffer)
  1069. {
  1070. if (_framebuffer == NULL)
  1071. {
  1072. return;
  1073. }
  1074. if (bgfx::isValid(_framebuffer->handle))
  1075. {
  1076. bgfx::destroy(_framebuffer->handle);
  1077. }
  1078. struct NVGparams* params = nvgInternalParams(_framebuffer->ctx);
  1079. struct GLNVGcontext* gl = (struct GLNVGcontext*)params->userPtr;
  1080. glnvg__deleteTexture(gl, _framebuffer->image);
  1081. BX_DELETE(gl->allocator, _framebuffer);
  1082. }
  1083. void nvgluSetViewFramebuffer(bgfx::ViewId _viewId, NVGLUframebuffer* _framebuffer)
  1084. {
  1085. _framebuffer->viewId = _viewId;
  1086. bgfx::setViewFrameBuffer(_viewId, _framebuffer->handle);
  1087. bgfx::setViewMode(_viewId, bgfx::ViewMode::Sequential);
  1088. }