nanovg_bgfx.cpp 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341
  1. /*
  2. * Copyright 2011-2023 Branimir Karadzic. All rights reserved.
  3. * License: https://github.com/bkaradzic/bgfx/blob/master/LICENSE
  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::VertexLayout s_nvgLayout;
  43. enum GLNVGshaderType
  44. {
  45. NSVG_SHADER_FILLGRAD,
  46. NSVG_SHADER_FILLIMG,
  47. NSVG_SHADER_SIMPLE,
  48. NSVG_SHADER_IMG
  49. };
  50. struct GLNVGtexture
  51. {
  52. bgfx::TextureHandle id;
  53. int width, height;
  54. int type;
  55. int flags;
  56. };
  57. struct GLNVGblend
  58. {
  59. uint64_t srcRGB;
  60. uint64_t dstRGB;
  61. uint64_t srcAlpha;
  62. uint64_t dstAlpha;
  63. };
  64. enum GLNVGcallType
  65. {
  66. GLNVG_FILL,
  67. GLNVG_CONVEXFILL,
  68. GLNVG_STROKE,
  69. GLNVG_TRIANGLES,
  70. };
  71. struct GLNVGcall
  72. {
  73. int type;
  74. int image;
  75. int pathOffset;
  76. int pathCount;
  77. int vertexOffset;
  78. int vertexCount;
  79. int uniformOffset;
  80. GLNVGblend blendFunc;
  81. };
  82. struct GLNVGpath
  83. {
  84. int fillOffset;
  85. int fillCount;
  86. int strokeOffset;
  87. int strokeCount;
  88. };
  89. struct GLNVGfragUniforms
  90. {
  91. float scissorMat[12]; // matrices are actually 3 vec4s
  92. float paintMat[12];
  93. NVGcolor innerCol;
  94. NVGcolor outerCol;
  95. // u_scissorExtScale
  96. float scissorExt[2];
  97. float scissorScale[2];
  98. // u_extentRadius
  99. float extent[2];
  100. float radius;
  101. // u_params
  102. float feather;
  103. float strokeMult;
  104. float texType;
  105. float type;
  106. };
  107. struct GLNVGcontext
  108. {
  109. bx::AllocatorI* allocator;
  110. bgfx::ProgramHandle prog;
  111. bgfx::UniformHandle u_scissorMat;
  112. bgfx::UniformHandle u_paintMat;
  113. bgfx::UniformHandle u_innerCol;
  114. bgfx::UniformHandle u_outerCol;
  115. bgfx::UniformHandle u_viewSize;
  116. bgfx::UniformHandle u_scissorExtScale;
  117. bgfx::UniformHandle u_extentRadius;
  118. bgfx::UniformHandle u_params;
  119. bgfx::UniformHandle s_tex;
  120. uint64_t state;
  121. bgfx::TextureHandle th;
  122. bgfx::TextureHandle texMissing;
  123. bgfx::TransientVertexBuffer tvb;
  124. bgfx::ViewId viewId;
  125. struct GLNVGtexture* textures;
  126. float view[2];
  127. int ntextures;
  128. int ctextures;
  129. int textureId;
  130. int vertBuf;
  131. int fragSize;
  132. int edgeAntiAlias;
  133. // Per frame buffers
  134. struct GLNVGcall* calls;
  135. int ccalls;
  136. int ncalls;
  137. struct GLNVGpath* paths;
  138. int cpaths;
  139. int npaths;
  140. struct NVGvertex* verts;
  141. int cverts;
  142. int nverts;
  143. unsigned char* uniforms;
  144. int cuniforms;
  145. int nuniforms;
  146. };
  147. static struct GLNVGtexture* glnvg__allocTexture(struct GLNVGcontext* gl)
  148. {
  149. struct GLNVGtexture* tex = NULL;
  150. int i;
  151. for (i = 0; i < gl->ntextures; i++)
  152. {
  153. if (gl->textures[i].id.idx == bgfx::kInvalidHandle)
  154. {
  155. tex = &gl->textures[i];
  156. break;
  157. }
  158. }
  159. if (tex == NULL)
  160. {
  161. if (gl->ntextures+1 > gl->ctextures)
  162. {
  163. int old = gl->ctextures;
  164. gl->ctextures = (gl->ctextures == 0) ? 2 : gl->ctextures*2;
  165. gl->textures = (struct GLNVGtexture*)bx::realloc(gl->allocator, gl->textures, sizeof(struct GLNVGtexture)*gl->ctextures);
  166. bx::memSet(&gl->textures[old], 0xff, (gl->ctextures-old)*sizeof(struct GLNVGtexture) );
  167. if (gl->textures == NULL)
  168. {
  169. return NULL;
  170. }
  171. }
  172. tex = &gl->textures[gl->ntextures++];
  173. }
  174. bx::memSet(tex, 0, sizeof(*tex) );
  175. return tex;
  176. }
  177. static struct GLNVGtexture* glnvg__findTexture(struct GLNVGcontext* gl, int id)
  178. {
  179. int i;
  180. for (i = 0; i < gl->ntextures; i++)
  181. {
  182. if (gl->textures[i].id.idx == id)
  183. {
  184. return &gl->textures[i];
  185. }
  186. }
  187. return NULL;
  188. }
  189. static int glnvg__deleteTexture(struct GLNVGcontext* gl, int id)
  190. {
  191. for (int ii = 0; ii < gl->ntextures; ii++)
  192. {
  193. if (gl->textures[ii].id.idx == id)
  194. {
  195. if (bgfx::isValid(gl->textures[ii].id)
  196. && (gl->textures[ii].flags & NVG_IMAGE_NODELETE) == 0)
  197. {
  198. bgfx::destroy(gl->textures[ii].id);
  199. }
  200. bx::memSet(&gl->textures[ii], 0, sizeof(gl->textures[ii]) );
  201. gl->textures[ii].id.idx = bgfx::kInvalidHandle;
  202. return 1;
  203. }
  204. }
  205. return 0;
  206. }
  207. static int nvgRenderCreate(void* _userPtr)
  208. {
  209. struct GLNVGcontext* gl = (struct GLNVGcontext*)_userPtr;
  210. bgfx::RendererType::Enum type = bgfx::getRendererType();
  211. gl->prog = bgfx::createProgram(
  212. bgfx::createEmbeddedShader(s_embeddedShaders, type, "vs_nanovg_fill")
  213. , bgfx::createEmbeddedShader(s_embeddedShaders, type, "fs_nanovg_fill")
  214. , true
  215. );
  216. const bgfx::Memory* mem = bgfx::alloc(4*4*4);
  217. uint32_t* bgra8 = (uint32_t*)mem->data;
  218. bx::memSet(bgra8, 0, 4*4*4);
  219. gl->texMissing = bgfx::createTexture2D(4, 4, false, 1, bgfx::TextureFormat::BGRA8, 0, mem);
  220. gl->u_scissorMat = bgfx::createUniform("u_scissorMat", bgfx::UniformType::Mat3);
  221. gl->u_paintMat = bgfx::createUniform("u_paintMat", bgfx::UniformType::Mat3);
  222. gl->u_innerCol = bgfx::createUniform("u_innerCol", bgfx::UniformType::Vec4);
  223. gl->u_outerCol = bgfx::createUniform("u_outerCol", bgfx::UniformType::Vec4);
  224. gl->u_viewSize = bgfx::createUniform("u_viewSize", bgfx::UniformType::Vec4);
  225. gl->u_scissorExtScale = bgfx::createUniform("u_scissorExtScale", bgfx::UniformType::Vec4);
  226. gl->u_extentRadius = bgfx::createUniform("u_extentRadius", bgfx::UniformType::Vec4);
  227. gl->u_params = bgfx::createUniform("u_params", bgfx::UniformType::Vec4);
  228. gl->s_tex = bgfx::createUniform("s_tex", bgfx::UniformType::Sampler);
  229. s_nvgLayout
  230. .begin()
  231. .add(bgfx::Attrib::Position, 2, bgfx::AttribType::Float)
  232. .add(bgfx::Attrib::TexCoord0, 2, bgfx::AttribType::Float)
  233. .end();
  234. int align = 16;
  235. gl->fragSize = sizeof(struct GLNVGfragUniforms) + align - sizeof(struct GLNVGfragUniforms) % align;
  236. return 1;
  237. }
  238. static int nvgRenderCreateTexture(
  239. void* _userPtr
  240. , int _type
  241. , int _width
  242. , int _height
  243. , int _flags
  244. , const unsigned char* _rgba
  245. )
  246. {
  247. struct GLNVGcontext* gl = (struct GLNVGcontext*)_userPtr;
  248. struct GLNVGtexture* tex = glnvg__allocTexture(gl);
  249. if (tex == NULL)
  250. {
  251. return 0;
  252. }
  253. tex->width = _width;
  254. tex->height = _height;
  255. tex->type = _type;
  256. tex->flags = _flags;
  257. uint32_t bytesPerPixel = NVG_TEXTURE_RGBA == tex->type ? 4 : 1;
  258. uint32_t pitch = tex->width * bytesPerPixel;
  259. const bgfx::Memory* mem = NULL;
  260. if (NULL != _rgba)
  261. {
  262. mem = bgfx::copy(_rgba, tex->height * pitch);
  263. }
  264. BX_ASSERT(tex->width >= 0 && tex->width <= bx::max<uint16_t>(), "Invalid tex width %d (max: %u)", tex->width, bx::max<uint16_t>());
  265. BX_ASSERT(tex->height >= 0 && tex->height <= bx::max<uint16_t>(), "Invalid tex height %d (max: %u)", tex->height, bx::max<uint16_t>());
  266. tex->id = bgfx::createTexture2D(
  267. uint16_t(tex->width)
  268. , uint16_t(tex->height)
  269. , false
  270. , 1
  271. , NVG_TEXTURE_RGBA == _type ? bgfx::TextureFormat::RGBA8 : bgfx::TextureFormat::R8
  272. , BGFX_SAMPLER_NONE
  273. );
  274. if (NULL != mem)
  275. {
  276. bgfx::updateTexture2D(
  277. tex->id
  278. , 0
  279. , 0
  280. , 0
  281. , 0
  282. , uint16_t(tex->width)
  283. , uint16_t(tex->height)
  284. , mem
  285. );
  286. }
  287. return bgfx::isValid(tex->id) ? tex->id.idx : 0;
  288. }
  289. static int nvgRenderDeleteTexture(void* _userPtr, int image)
  290. {
  291. struct GLNVGcontext* gl = (struct GLNVGcontext*)_userPtr;
  292. return glnvg__deleteTexture(gl, image);
  293. }
  294. static int nvgRenderUpdateTexture(void* _userPtr, int image, int x, int y, int w, int h, const unsigned char* data)
  295. {
  296. struct GLNVGcontext* gl = (struct GLNVGcontext*)_userPtr;
  297. struct GLNVGtexture* tex = glnvg__findTexture(gl, image);
  298. if (tex == NULL)
  299. {
  300. return 0;
  301. }
  302. uint32_t bytesPerPixel = NVG_TEXTURE_RGBA == tex->type ? 4 : 1;
  303. uint32_t pitch = tex->width * bytesPerPixel;
  304. const bgfx::Memory* mem = bgfx::alloc(w * h * bytesPerPixel);
  305. bx::gather(mem->data, // dst
  306. data + y * pitch + x * bytesPerPixel, // src
  307. pitch, // srcStride
  308. w * bytesPerPixel, // stride
  309. h); // num
  310. BX_ASSERT(x >= 0 && x <= bx::max<uint16_t>(), "Invalid tex x pos %d (max: %u)", x, bx::max<uint16_t>());
  311. BX_ASSERT(y >= 0 && y <= bx::max<uint16_t>(), "Invalid tex y pos %d (max: %u)", y, bx::max<uint16_t>());
  312. BX_ASSERT(w >= 0 && w <= bx::max<uint16_t>(), "Invalid tex width %d (max: %u)", w, bx::max<uint16_t>());
  313. BX_ASSERT(h >= 0 && h <= bx::max<uint16_t>(), "Invalid tex width %d (max: %u)", h, bx::max<uint16_t>());
  314. bgfx::updateTexture2D(
  315. tex->id
  316. , 0
  317. , 0
  318. , uint16_t(x)
  319. , uint16_t(y)
  320. , uint16_t(w)
  321. , uint16_t(h)
  322. , mem
  323. , UINT16_MAX
  324. );
  325. return 1;
  326. }
  327. static int nvgRenderGetTextureSize(void* _userPtr, int image, int* w, int* h)
  328. {
  329. struct GLNVGcontext* gl = (struct GLNVGcontext*)_userPtr;
  330. struct GLNVGtexture* tex = glnvg__findTexture(gl, image);
  331. if (NULL == tex
  332. || !bgfx::isValid(tex->id) )
  333. {
  334. return 0;
  335. }
  336. *w = tex->width;
  337. *h = tex->height;
  338. return 1;
  339. }
  340. static void glnvg__xformToMat3x4(float* m3, float* t)
  341. {
  342. m3[ 0] = t[0];
  343. m3[ 1] = t[1];
  344. m3[ 2] = 0.0f;
  345. m3[ 3] = 0.0f;
  346. m3[ 4] = t[2];
  347. m3[ 5] = t[3];
  348. m3[ 6] = 0.0f;
  349. m3[ 7] = 0.0f;
  350. m3[ 8] = t[4];
  351. m3[ 9] = t[5];
  352. m3[10] = 1.0f;
  353. m3[11] = 0.0f;
  354. }
  355. static NVGcolor glnvg__premulColor(NVGcolor c)
  356. {
  357. c.r *= c.a;
  358. c.g *= c.a;
  359. c.b *= c.a;
  360. return c;
  361. }
  362. static int glnvg__convertPaint(
  363. struct GLNVGcontext* gl
  364. , struct GLNVGfragUniforms* frag
  365. , struct NVGpaint* paint
  366. , struct NVGscissor* scissor
  367. , float width
  368. , float fringe
  369. )
  370. {
  371. struct GLNVGtexture* tex = NULL;
  372. float invxform[6] = {};
  373. bx::memSet(frag, 0, sizeof(*frag) );
  374. frag->innerCol = glnvg__premulColor(paint->innerColor);
  375. frag->outerCol = glnvg__premulColor(paint->outerColor);
  376. if (scissor->extent[0] < -0.5f || scissor->extent[1] < -0.5f)
  377. {
  378. bx::memSet(frag->scissorMat, 0, sizeof(frag->scissorMat) );
  379. frag->scissorExt[0] = 1.0f;
  380. frag->scissorExt[1] = 1.0f;
  381. frag->scissorScale[0] = 1.0f;
  382. frag->scissorScale[1] = 1.0f;
  383. }
  384. else
  385. {
  386. nvgTransformInverse(invxform, scissor->xform);
  387. glnvg__xformToMat3x4(frag->scissorMat, invxform);
  388. frag->scissorExt[0] = scissor->extent[0];
  389. frag->scissorExt[1] = scissor->extent[1];
  390. frag->scissorScale[0] = sqrtf(scissor->xform[0]*scissor->xform[0] + scissor->xform[2]*scissor->xform[2]) / fringe;
  391. frag->scissorScale[1] = sqrtf(scissor->xform[1]*scissor->xform[1] + scissor->xform[3]*scissor->xform[3]) / fringe;
  392. }
  393. bx::memCopy(frag->extent, paint->extent, sizeof(frag->extent) );
  394. frag->strokeMult = (width*0.5f + fringe*0.5f) / fringe;
  395. gl->th = gl->texMissing;
  396. if (paint->image != 0)
  397. {
  398. tex = glnvg__findTexture(gl, paint->image);
  399. if (tex == NULL)
  400. {
  401. return 0;
  402. }
  403. nvgTransformInverse(invxform, paint->xform);
  404. frag->type = NSVG_SHADER_FILLIMG;
  405. if (tex->type == NVG_TEXTURE_RGBA)
  406. {
  407. frag->texType = (tex->flags & NVG_IMAGE_PREMULTIPLIED) ? 0.0f : 1.0f;
  408. }
  409. else
  410. {
  411. frag->texType = 2.0f;
  412. }
  413. gl->th = tex->id;
  414. }
  415. else
  416. {
  417. frag->type = NSVG_SHADER_FILLGRAD;
  418. frag->radius = paint->radius;
  419. frag->feather = paint->feather;
  420. nvgTransformInverse(invxform, paint->xform);
  421. }
  422. glnvg__xformToMat3x4(frag->paintMat, invxform);
  423. return 1;
  424. }
  425. static void glnvg__mat3(float* dst, float* src)
  426. {
  427. dst[0] = src[ 0];
  428. dst[1] = src[ 1];
  429. dst[2] = src[ 2];
  430. dst[3] = src[ 4];
  431. dst[4] = src[ 5];
  432. dst[5] = src[ 6];
  433. dst[6] = src[ 8];
  434. dst[7] = src[ 9];
  435. dst[8] = src[10];
  436. }
  437. static struct GLNVGfragUniforms* nvg__fragUniformPtr(struct GLNVGcontext* gl, int i)
  438. {
  439. return (struct GLNVGfragUniforms*)&gl->uniforms[i];
  440. }
  441. static void nvgRenderSetUniforms(struct GLNVGcontext* gl, int uniformOffset, int image)
  442. {
  443. struct GLNVGfragUniforms* frag = nvg__fragUniformPtr(gl, uniformOffset);
  444. float tmp[9]; // Maybe there's a way to get rid of this...
  445. glnvg__mat3(tmp, frag->scissorMat);
  446. bgfx::setUniform(gl->u_scissorMat, tmp);
  447. glnvg__mat3(tmp, frag->paintMat);
  448. bgfx::setUniform(gl->u_paintMat, tmp);
  449. bgfx::setUniform(gl->u_innerCol, frag->innerCol.rgba);
  450. bgfx::setUniform(gl->u_outerCol, frag->outerCol.rgba);
  451. bgfx::setUniform(gl->u_scissorExtScale, &frag->scissorExt[0]);
  452. bgfx::setUniform(gl->u_extentRadius, &frag->extent[0]);
  453. bgfx::setUniform(gl->u_params, &frag->feather);
  454. bgfx::TextureHandle handle = gl->texMissing;
  455. if (image != 0)
  456. {
  457. struct GLNVGtexture* tex = glnvg__findTexture(gl, image);
  458. if (tex != NULL)
  459. {
  460. handle = tex->id;
  461. }
  462. }
  463. gl->th = handle;
  464. }
  465. static void nvgRenderViewport(void* _userPtr, float width, float height, float devicePixelRatio)
  466. {
  467. struct GLNVGcontext* gl = (struct GLNVGcontext*)_userPtr;
  468. gl->view[0] = width;
  469. gl->view[1] = height;
  470. bgfx::setViewRect(gl->viewId, 0, 0, width * devicePixelRatio, height * devicePixelRatio);
  471. }
  472. static void fan(uint32_t _start, uint32_t _count)
  473. {
  474. uint32_t numTris = _count-2;
  475. bgfx::TransientIndexBuffer tib;
  476. bgfx::allocTransientIndexBuffer(&tib, numTris*3);
  477. uint16_t* data = (uint16_t*)tib.data;
  478. for (uint32_t ii = 0; ii < numTris; ++ii)
  479. {
  480. data[ii*3+0] = uint16_t(_start);
  481. data[ii*3+1] = uint16_t(_start + ii + 1);
  482. data[ii*3+2] = uint16_t(_start + ii + 2);
  483. }
  484. bgfx::setIndexBuffer(&tib);
  485. }
  486. static void glnvg__fill(struct GLNVGcontext* gl, struct GLNVGcall* call)
  487. {
  488. struct GLNVGpath* paths = &gl->paths[call->pathOffset];
  489. int i, npaths = call->pathCount;
  490. // set bindpoint for solid loc
  491. nvgRenderSetUniforms(gl, call->uniformOffset, 0);
  492. for (i = 0; i < npaths; i++)
  493. {
  494. if (2 < paths[i].fillCount)
  495. {
  496. bgfx::setState(0);
  497. bgfx::setStencil(0
  498. | BGFX_STENCIL_TEST_ALWAYS
  499. | BGFX_STENCIL_FUNC_RMASK(0xff)
  500. | BGFX_STENCIL_OP_FAIL_S_KEEP
  501. | BGFX_STENCIL_OP_FAIL_Z_KEEP
  502. | BGFX_STENCIL_OP_PASS_Z_INCR
  503. , 0
  504. | BGFX_STENCIL_TEST_ALWAYS
  505. | BGFX_STENCIL_FUNC_RMASK(0xff)
  506. | BGFX_STENCIL_OP_FAIL_S_KEEP
  507. | BGFX_STENCIL_OP_FAIL_Z_KEEP
  508. | BGFX_STENCIL_OP_PASS_Z_DECR
  509. );
  510. bgfx::setVertexBuffer(0, &gl->tvb);
  511. bgfx::setTexture(0, gl->s_tex, gl->th);
  512. fan(paths[i].fillOffset, paths[i].fillCount);
  513. bgfx::submit(gl->viewId, gl->prog);
  514. }
  515. }
  516. // Draw aliased off-pixels
  517. nvgRenderSetUniforms(gl, call->uniformOffset + gl->fragSize, call->image);
  518. if (gl->edgeAntiAlias)
  519. {
  520. // Draw fringes
  521. for (i = 0; i < npaths; i++)
  522. {
  523. bgfx::setState(gl->state
  524. | BGFX_STATE_PT_TRISTRIP
  525. );
  526. bgfx::setStencil(0
  527. | BGFX_STENCIL_TEST_EQUAL
  528. | BGFX_STENCIL_FUNC_RMASK(0xff)
  529. | BGFX_STENCIL_OP_FAIL_S_KEEP
  530. | BGFX_STENCIL_OP_FAIL_Z_KEEP
  531. | BGFX_STENCIL_OP_PASS_Z_KEEP
  532. );
  533. bgfx::setVertexBuffer(0, &gl->tvb, paths[i].strokeOffset, paths[i].strokeCount);
  534. bgfx::setTexture(0, gl->s_tex, gl->th);
  535. bgfx::submit(gl->viewId, gl->prog);
  536. }
  537. }
  538. // Draw fill
  539. bgfx::setState(gl->state);
  540. bgfx::setVertexBuffer(0, &gl->tvb, call->vertexOffset, call->vertexCount);
  541. bgfx::setTexture(0, gl->s_tex, gl->th);
  542. bgfx::setStencil(0
  543. | BGFX_STENCIL_TEST_NOTEQUAL
  544. | BGFX_STENCIL_FUNC_RMASK(0xff)
  545. | BGFX_STENCIL_OP_FAIL_S_ZERO
  546. | BGFX_STENCIL_OP_FAIL_Z_ZERO
  547. | BGFX_STENCIL_OP_PASS_Z_ZERO
  548. );
  549. bgfx::submit(gl->viewId, gl->prog);
  550. }
  551. static void glnvg__convexFill(struct GLNVGcontext* gl, struct GLNVGcall* call)
  552. {
  553. struct GLNVGpath* paths = &gl->paths[call->pathOffset];
  554. int i, npaths = call->pathCount;
  555. nvgRenderSetUniforms(gl, call->uniformOffset, call->image);
  556. for (i = 0; i < npaths; i++)
  557. {
  558. if (paths[i].fillCount == 0) continue;
  559. bgfx::setState(gl->state);
  560. bgfx::setVertexBuffer(0, &gl->tvb);
  561. bgfx::setTexture(0, gl->s_tex, gl->th);
  562. fan(paths[i].fillOffset, paths[i].fillCount);
  563. bgfx::submit(gl->viewId, gl->prog);
  564. }
  565. if (gl->edgeAntiAlias)
  566. {
  567. // Draw fringes
  568. for (i = 0; i < npaths; i++)
  569. {
  570. bgfx::setState(gl->state
  571. | BGFX_STATE_PT_TRISTRIP
  572. );
  573. bgfx::setVertexBuffer(0, &gl->tvb, paths[i].strokeOffset, paths[i].strokeCount);
  574. bgfx::setTexture(0, gl->s_tex, gl->th);
  575. bgfx::submit(gl->viewId, gl->prog);
  576. }
  577. }
  578. }
  579. static void glnvg__stroke(struct GLNVGcontext* gl, struct GLNVGcall* call)
  580. {
  581. struct GLNVGpath* paths = &gl->paths[call->pathOffset];
  582. int npaths = call->pathCount, i;
  583. nvgRenderSetUniforms(gl, call->uniformOffset, call->image);
  584. // Draw Strokes
  585. for (i = 0; i < npaths; i++)
  586. {
  587. bgfx::setState(gl->state
  588. | BGFX_STATE_PT_TRISTRIP
  589. );
  590. bgfx::setVertexBuffer(0, &gl->tvb, paths[i].strokeOffset, paths[i].strokeCount);
  591. bgfx::setTexture(0, gl->s_tex, gl->th);
  592. bgfx::submit(gl->viewId, gl->prog);
  593. }
  594. }
  595. static void glnvg__triangles(struct GLNVGcontext* gl, struct GLNVGcall* call)
  596. {
  597. if (3 <= call->vertexCount)
  598. {
  599. nvgRenderSetUniforms(gl, call->uniformOffset, call->image);
  600. bgfx::setState(gl->state);
  601. bgfx::setVertexBuffer(0, &gl->tvb, call->vertexOffset, call->vertexCount);
  602. bgfx::setTexture(0, gl->s_tex, gl->th);
  603. bgfx::submit(gl->viewId, gl->prog);
  604. }
  605. }
  606. static const uint64_t s_blend[] =
  607. {
  608. BGFX_STATE_BLEND_ZERO,
  609. BGFX_STATE_BLEND_ONE,
  610. BGFX_STATE_BLEND_SRC_COLOR,
  611. BGFX_STATE_BLEND_INV_SRC_COLOR,
  612. BGFX_STATE_BLEND_DST_COLOR,
  613. BGFX_STATE_BLEND_INV_DST_COLOR,
  614. BGFX_STATE_BLEND_SRC_ALPHA,
  615. BGFX_STATE_BLEND_INV_SRC_ALPHA,
  616. BGFX_STATE_BLEND_DST_ALPHA,
  617. BGFX_STATE_BLEND_INV_DST_ALPHA,
  618. BGFX_STATE_BLEND_SRC_ALPHA_SAT,
  619. };
  620. static uint64_t glnvg_convertBlendFuncFactor(int factor)
  621. {
  622. const uint32_t numtz = bx::uint32_cnttz(factor);
  623. const uint32_t idx = bx::uint32_min(numtz, BX_COUNTOF(s_blend)-1);
  624. return s_blend[idx];
  625. }
  626. static GLNVGblend glnvg__blendCompositeOperation(NVGcompositeOperationState op)
  627. {
  628. GLNVGblend blend;
  629. blend.srcRGB = glnvg_convertBlendFuncFactor(op.srcRGB);
  630. blend.dstRGB = glnvg_convertBlendFuncFactor(op.dstRGB);
  631. blend.srcAlpha = glnvg_convertBlendFuncFactor(op.srcAlpha);
  632. blend.dstAlpha = glnvg_convertBlendFuncFactor(op.dstAlpha);
  633. if (blend.srcRGB == BGFX_STATE_NONE || blend.dstRGB == BGFX_STATE_NONE || blend.srcAlpha == BGFX_STATE_NONE || blend.dstAlpha == BGFX_STATE_NONE)
  634. {
  635. blend.srcRGB = BGFX_STATE_BLEND_ONE;
  636. blend.dstRGB = BGFX_STATE_BLEND_INV_SRC_ALPHA;
  637. blend.srcAlpha = BGFX_STATE_BLEND_ONE;
  638. blend.dstAlpha = BGFX_STATE_BLEND_INV_SRC_ALPHA;
  639. }
  640. return blend;
  641. }
  642. static void nvgRenderFlush(void* _userPtr)
  643. {
  644. struct GLNVGcontext* gl = (struct GLNVGcontext*)_userPtr;
  645. if (gl->ncalls > 0)
  646. {
  647. int avail = bgfx::getAvailTransientVertexBuffer(gl->nverts, s_nvgLayout);
  648. if (avail < gl->nverts)
  649. {
  650. gl->nverts = avail;
  651. BX_WARN(true, "Vertex number truncated due to transient vertex buffer overflow");
  652. if (gl->nverts < 2)
  653. {
  654. goto _cleanup;
  655. }
  656. }
  657. bgfx::allocTransientVertexBuffer(&gl->tvb, gl->nverts, s_nvgLayout);
  658. int allocated = gl->tvb.size/gl->tvb.stride;
  659. if (allocated < gl->nverts)
  660. {
  661. // this branch should never be taken as we've already checked the transient vertex buffer size
  662. gl->nverts = allocated;
  663. BX_WARN(true, "Vertex number truncated due to transient vertex buffer overflow");
  664. }
  665. bx::memCopy(gl->tvb.data, gl->verts, gl->nverts * sizeof(struct NVGvertex) );
  666. bgfx::setUniform(gl->u_viewSize, gl->view);
  667. for (uint32_t ii = 0, num = gl->ncalls; ii < num; ++ii)
  668. {
  669. struct GLNVGcall* call = &gl->calls[ii];
  670. const GLNVGblend* blend = &call->blendFunc;
  671. gl->state = BGFX_STATE_BLEND_FUNC_SEPARATE(blend->srcRGB, blend->dstRGB, blend->srcAlpha, blend->dstAlpha)
  672. | BGFX_STATE_WRITE_RGB
  673. | BGFX_STATE_WRITE_A
  674. ;
  675. switch (call->type)
  676. {
  677. case GLNVG_FILL:
  678. glnvg__fill(gl, call);
  679. break;
  680. case GLNVG_CONVEXFILL:
  681. glnvg__convexFill(gl, call);
  682. break;
  683. case GLNVG_STROKE:
  684. glnvg__stroke(gl, call);
  685. break;
  686. case GLNVG_TRIANGLES:
  687. glnvg__triangles(gl, call);
  688. break;
  689. }
  690. }
  691. }
  692. _cleanup:
  693. // Reset calls
  694. gl->nverts = 0;
  695. gl->npaths = 0;
  696. gl->ncalls = 0;
  697. gl->nuniforms = 0;
  698. }
  699. static int glnvg__maxVertCount(const struct NVGpath* paths, int npaths)
  700. {
  701. int i, count = 0;
  702. for (i = 0; i < npaths; i++)
  703. {
  704. count += paths[i].nfill;
  705. count += paths[i].nstroke;
  706. }
  707. return count;
  708. }
  709. static int glnvg__maxi(int a, int b) { return a > b ? a : b; }
  710. static struct GLNVGcall* glnvg__allocCall(struct GLNVGcontext* gl)
  711. {
  712. struct GLNVGcall* ret = NULL;
  713. if (gl->ncalls+1 > gl->ccalls)
  714. {
  715. gl->ccalls = gl->ccalls == 0 ? 32 : gl->ccalls * 2;
  716. gl->calls = (struct GLNVGcall*)bx::realloc(gl->allocator, gl->calls, sizeof(struct GLNVGcall) * gl->ccalls);
  717. }
  718. ret = &gl->calls[gl->ncalls++];
  719. bx::memSet(ret, 0, sizeof(struct GLNVGcall) );
  720. return ret;
  721. }
  722. static int glnvg__allocPaths(struct GLNVGcontext* gl, int n)
  723. {
  724. int ret = 0;
  725. if (gl->npaths + n > gl->cpaths) {
  726. GLNVGpath* paths;
  727. int cpaths = glnvg__maxi(gl->npaths + n, 128) + gl->cpaths / 2; // 1.5x Overallocate
  728. paths = (GLNVGpath*)bx::realloc(gl->allocator, gl->paths, sizeof(GLNVGpath) * cpaths);
  729. if (paths == NULL) return -1;
  730. gl->paths = paths;
  731. gl->cpaths = cpaths;
  732. }
  733. ret = gl->npaths;
  734. gl->npaths += n;
  735. return ret;
  736. }
  737. static int glnvg__allocVerts(GLNVGcontext* gl, int n)
  738. {
  739. int ret = 0;
  740. if (gl->nverts+n > gl->cverts)
  741. {
  742. NVGvertex* verts;
  743. int cverts = glnvg__maxi(gl->nverts + n, 4096) + gl->cverts/2; // 1.5x Overallocate
  744. verts = (NVGvertex*)bx::realloc(gl->allocator, gl->verts, sizeof(NVGvertex) * cverts);
  745. if (verts == NULL) return -1;
  746. gl->verts = verts;
  747. gl->cverts = cverts;
  748. }
  749. ret = gl->nverts;
  750. gl->nverts += n;
  751. return ret;
  752. }
  753. static int glnvg__allocFragUniforms(struct GLNVGcontext* gl, int n)
  754. {
  755. int ret = 0, structSize = gl->fragSize;
  756. if (gl->nuniforms+n > gl->cuniforms)
  757. {
  758. gl->cuniforms = gl->cuniforms == 0 ? glnvg__maxi(n, 32) : gl->cuniforms * 2;
  759. gl->uniforms = (unsigned char*)bx::realloc(gl->allocator, gl->uniforms, gl->cuniforms * structSize);
  760. }
  761. ret = gl->nuniforms * structSize;
  762. gl->nuniforms += n;
  763. return ret;
  764. }
  765. static void glnvg__vset(struct NVGvertex* vtx, float x, float y, float u, float v)
  766. {
  767. vtx->x = x;
  768. vtx->y = y;
  769. vtx->u = u;
  770. vtx->v = v;
  771. }
  772. static void nvgRenderFill(
  773. void* _userPtr
  774. , NVGpaint* paint
  775. , NVGcompositeOperationState compositeOperation
  776. , NVGscissor* scissor
  777. , float fringe
  778. , const float* bounds
  779. , const NVGpath* paths
  780. , int npaths
  781. )
  782. {
  783. struct GLNVGcontext* gl = (struct GLNVGcontext*)_userPtr;
  784. struct GLNVGcall* call = glnvg__allocCall(gl);
  785. struct NVGvertex* quad;
  786. struct GLNVGfragUniforms* frag;
  787. int i, maxverts, offset;
  788. call->type = GLNVG_FILL;
  789. call->pathOffset = glnvg__allocPaths(gl, npaths);
  790. call->pathCount = npaths;
  791. call->image = paint->image;
  792. call->blendFunc = glnvg__blendCompositeOperation(compositeOperation);
  793. if (npaths == 1 && paths[0].convex)
  794. {
  795. call->type = GLNVG_CONVEXFILL;
  796. }
  797. // Allocate vertices for all the paths.
  798. maxverts = glnvg__maxVertCount(paths, npaths) + 6;
  799. offset = glnvg__allocVerts(gl, maxverts);
  800. for (i = 0; i < npaths; i++)
  801. {
  802. struct GLNVGpath* copy = &gl->paths[call->pathOffset + i];
  803. const struct NVGpath* path = &paths[i];
  804. bx::memSet(copy, 0, sizeof(struct GLNVGpath) );
  805. if (path->nfill > 0)
  806. {
  807. copy->fillOffset = offset;
  808. copy->fillCount = path->nfill;
  809. bx::memCopy(&gl->verts[offset], path->fill, sizeof(struct NVGvertex) * path->nfill);
  810. offset += path->nfill;
  811. }
  812. if (path->nstroke > 0)
  813. {
  814. copy->strokeOffset = offset;
  815. copy->strokeCount = path->nstroke;
  816. bx::memCopy(&gl->verts[offset], path->stroke, sizeof(struct NVGvertex) * path->nstroke);
  817. offset += path->nstroke;
  818. }
  819. }
  820. // Quad
  821. call->vertexOffset = offset;
  822. call->vertexCount = 6;
  823. quad = &gl->verts[call->vertexOffset];
  824. glnvg__vset(&quad[0], bounds[0], bounds[3], 0.5f, 1.0f);
  825. glnvg__vset(&quad[1], bounds[2], bounds[3], 0.5f, 1.0f);
  826. glnvg__vset(&quad[2], bounds[2], bounds[1], 0.5f, 1.0f);
  827. glnvg__vset(&quad[3], bounds[0], bounds[3], 0.5f, 1.0f);
  828. glnvg__vset(&quad[4], bounds[2], bounds[1], 0.5f, 1.0f);
  829. glnvg__vset(&quad[5], bounds[0], bounds[1], 0.5f, 1.0f);
  830. // Setup uniforms for draw calls
  831. if (call->type == GLNVG_FILL)
  832. {
  833. call->uniformOffset = glnvg__allocFragUniforms(gl, 2);
  834. // Simple shader for stencil
  835. frag = nvg__fragUniformPtr(gl, call->uniformOffset);
  836. bx::memSet(frag, 0, sizeof(*frag) );
  837. frag->type = NSVG_SHADER_SIMPLE;
  838. // Fill shader
  839. glnvg__convertPaint(gl, nvg__fragUniformPtr(gl, call->uniformOffset + gl->fragSize), paint, scissor, fringe, fringe);
  840. }
  841. else
  842. {
  843. call->uniformOffset = glnvg__allocFragUniforms(gl, 1);
  844. // Fill shader
  845. glnvg__convertPaint(gl, nvg__fragUniformPtr(gl, call->uniformOffset), paint, scissor, fringe, fringe);
  846. }
  847. }
  848. static void nvgRenderStroke(
  849. void* _userPtr
  850. , struct NVGpaint* paint
  851. , NVGcompositeOperationState compositeOperation
  852. , struct NVGscissor* scissor
  853. , float fringe
  854. , float strokeWidth
  855. , const struct NVGpath* paths
  856. , int npaths
  857. )
  858. {
  859. struct GLNVGcontext* gl = (struct GLNVGcontext*)_userPtr;
  860. struct GLNVGcall* call = glnvg__allocCall(gl);
  861. int i, maxverts, offset;
  862. call->type = GLNVG_STROKE;
  863. call->pathOffset = glnvg__allocPaths(gl, npaths);
  864. call->pathCount = npaths;
  865. call->image = paint->image;
  866. call->blendFunc = glnvg__blendCompositeOperation(compositeOperation);
  867. // Allocate vertices for all the paths.
  868. maxverts = glnvg__maxVertCount(paths, npaths);
  869. offset = glnvg__allocVerts(gl, maxverts);
  870. for (i = 0; i < npaths; i++)
  871. {
  872. struct GLNVGpath* copy = &gl->paths[call->pathOffset + i];
  873. const struct NVGpath* path = &paths[i];
  874. bx::memSet(copy, 0, sizeof(struct GLNVGpath) );
  875. if (path->nstroke)
  876. {
  877. copy->strokeOffset = offset;
  878. copy->strokeCount = path->nstroke;
  879. bx::memCopy(&gl->verts[offset], path->stroke, sizeof(struct NVGvertex) * path->nstroke);
  880. offset += path->nstroke;
  881. }
  882. }
  883. // Fill shader
  884. call->uniformOffset = glnvg__allocFragUniforms(gl, 1);
  885. glnvg__convertPaint(gl, nvg__fragUniformPtr(gl, call->uniformOffset), paint, scissor, strokeWidth, fringe);
  886. }
  887. static void nvgRenderTriangles(void* _userPtr, struct NVGpaint* paint, NVGcompositeOperationState compositeOperation, struct NVGscissor* scissor,
  888. const struct NVGvertex* verts, int nverts)
  889. {
  890. struct GLNVGcontext* gl = (struct GLNVGcontext*)_userPtr;
  891. struct GLNVGcall* call = glnvg__allocCall(gl);
  892. struct GLNVGfragUniforms* frag;
  893. call->type = GLNVG_TRIANGLES;
  894. call->image = paint->image;
  895. call->blendFunc = glnvg__blendCompositeOperation(compositeOperation);
  896. // Allocate vertices for all the paths.
  897. call->vertexOffset = glnvg__allocVerts(gl, nverts);
  898. call->vertexCount = nverts;
  899. bx::memCopy(&gl->verts[call->vertexOffset], verts, sizeof(struct NVGvertex) * nverts);
  900. // Fill shader
  901. call->uniformOffset = glnvg__allocFragUniforms(gl, 1);
  902. frag = nvg__fragUniformPtr(gl, call->uniformOffset);
  903. glnvg__convertPaint(gl, frag, paint, scissor, 1.0f, 1.0f);
  904. frag->type = NSVG_SHADER_IMG;
  905. }
  906. static void nvgRenderDelete(void* _userPtr)
  907. {
  908. struct GLNVGcontext* gl = (struct GLNVGcontext*)_userPtr;
  909. if (gl == NULL)
  910. {
  911. return;
  912. }
  913. bgfx::destroy(gl->prog);
  914. bgfx::destroy(gl->texMissing);
  915. bgfx::destroy(gl->u_scissorMat);
  916. bgfx::destroy(gl->u_paintMat);
  917. bgfx::destroy(gl->u_innerCol);
  918. bgfx::destroy(gl->u_outerCol);
  919. bgfx::destroy(gl->u_viewSize);
  920. bgfx::destroy(gl->u_scissorExtScale);
  921. bgfx::destroy(gl->u_extentRadius);
  922. bgfx::destroy(gl->u_params);
  923. bgfx::destroy(gl->s_tex);
  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. BX_ASSERT(_width >= 0 && _width <= bx::max<uint16_t>(), "Invalid tex width %d (max: %u)", _width, bx::max<uint16_t>());
  1021. BX_ASSERT(_height >= 0 && _height <= bx::max<uint16_t>(), "Invalid tex height %d (max: %u)", _height, bx::max<uint16_t>());
  1022. const uint16_t w = uint16_t(_width);
  1023. const uint16_t h = uint16_t(_height);
  1024. bgfx::TextureHandle textures[] =
  1025. {
  1026. bgfx::createTexture2D(w, h, false, 1, bgfx::TextureFormat::RGBA8, BGFX_TEXTURE_RT),
  1027. bgfx::createTexture2D(w, h, false, 1, bgfx::TextureFormat::D24S8, BGFX_TEXTURE_RT | BGFX_TEXTURE_RT_WRITE_ONLY)
  1028. };
  1029. bgfx::FrameBufferHandle fbh = bgfx::createFrameBuffer(
  1030. BX_COUNTOF(textures)
  1031. , textures
  1032. , true
  1033. );
  1034. if (!bgfx::isValid(fbh) )
  1035. {
  1036. return NULL;
  1037. }
  1038. struct NVGparams* params = nvgInternalParams(_ctx);
  1039. struct GLNVGcontext* gl = (struct GLNVGcontext*)params->userPtr;
  1040. struct GLNVGtexture* tex = glnvg__allocTexture(gl);
  1041. if (NULL == tex)
  1042. {
  1043. bgfx::destroy(fbh);
  1044. return NULL;
  1045. }
  1046. tex->width = _width;
  1047. tex->height = _height;
  1048. tex->type = NVG_TEXTURE_RGBA;
  1049. tex->flags = _imageFlags | NVG_IMAGE_PREMULTIPLIED;
  1050. tex->id = bgfx::getTexture(fbh);
  1051. NVGLUframebuffer* framebuffer = BX_NEW(gl->allocator, NVGLUframebuffer);
  1052. framebuffer->ctx = _ctx;
  1053. framebuffer->image = tex->id.idx;
  1054. framebuffer->handle = fbh;
  1055. return framebuffer;
  1056. }
  1057. NVGLUframebuffer* nvgluCreateFramebuffer(NVGcontext* ctx, int32_t imageFlags, bgfx::ViewId viewId)
  1058. {
  1059. NVGLUframebuffer* framebuffer = nvgluCreateFramebuffer(ctx, imageFlags);
  1060. if (framebuffer != NULL)
  1061. {
  1062. nvgluSetViewFramebuffer(viewId, framebuffer);
  1063. }
  1064. return framebuffer;
  1065. }
  1066. NVGLUframebuffer* nvgluCreateFramebuffer(NVGcontext* _ctx, int32_t _imageFlags)
  1067. {
  1068. BX_UNUSED(_imageFlags);
  1069. bgfx::TextureHandle textures[] =
  1070. {
  1071. bgfx::createTexture2D(bgfx::BackbufferRatio::Equal, false, 1, bgfx::TextureFormat::RGBA8, BGFX_TEXTURE_RT),
  1072. bgfx::createTexture2D(bgfx::BackbufferRatio::Equal, false, 1, bgfx::TextureFormat::D24S8, BGFX_TEXTURE_RT | BGFX_TEXTURE_RT_WRITE_ONLY)
  1073. };
  1074. bgfx::FrameBufferHandle fbh = bgfx::createFrameBuffer(
  1075. BX_COUNTOF(textures)
  1076. , textures
  1077. , true
  1078. );
  1079. if (!bgfx::isValid(fbh) )
  1080. {
  1081. return NULL;
  1082. }
  1083. struct NVGparams* params = nvgInternalParams(_ctx);
  1084. struct GLNVGcontext* gl = (struct GLNVGcontext*)params->userPtr;
  1085. struct GLNVGtexture* tex = glnvg__allocTexture(gl);
  1086. if (NULL == tex)
  1087. {
  1088. bgfx::destroy(fbh);
  1089. return NULL;
  1090. }
  1091. tex->width = 0;
  1092. tex->height = 0;
  1093. tex->type = NVG_TEXTURE_RGBA;
  1094. tex->flags = _imageFlags | NVG_IMAGE_PREMULTIPLIED;
  1095. tex->id = bgfx::getTexture(fbh);
  1096. NVGLUframebuffer* framebuffer = BX_NEW(gl->allocator, NVGLUframebuffer);
  1097. framebuffer->ctx = _ctx;
  1098. framebuffer->image = tex->id.idx;
  1099. framebuffer->handle = fbh;
  1100. return framebuffer;
  1101. }
  1102. void nvgluBindFramebuffer(NVGLUframebuffer* _framebuffer)
  1103. {
  1104. static NVGcontext* s_prevCtx = NULL;
  1105. static bgfx::ViewId s_prevViewId;
  1106. if (_framebuffer != NULL)
  1107. {
  1108. s_prevCtx = _framebuffer->ctx;
  1109. s_prevViewId = nvgGetViewId(_framebuffer->ctx);
  1110. nvgSetViewId(_framebuffer->ctx, _framebuffer->viewId);
  1111. }
  1112. else if (s_prevCtx != NULL)
  1113. {
  1114. nvgSetViewId(s_prevCtx, s_prevViewId);
  1115. }
  1116. }
  1117. void nvgluDeleteFramebuffer(NVGLUframebuffer* _framebuffer)
  1118. {
  1119. if (_framebuffer == NULL)
  1120. {
  1121. return;
  1122. }
  1123. if (bgfx::isValid(_framebuffer->handle))
  1124. {
  1125. bgfx::destroy(_framebuffer->handle);
  1126. }
  1127. struct NVGparams* params = nvgInternalParams(_framebuffer->ctx);
  1128. struct GLNVGcontext* gl = (struct GLNVGcontext*)params->userPtr;
  1129. glnvg__deleteTexture(gl, _framebuffer->image);
  1130. bx::deleteObject(gl->allocator, _framebuffer);
  1131. }
  1132. void nvgluSetViewFramebuffer(bgfx::ViewId _viewId, NVGLUframebuffer* _framebuffer)
  1133. {
  1134. _framebuffer->viewId = _viewId;
  1135. bgfx::setViewFrameBuffer(_viewId, _framebuffer->handle);
  1136. bgfx::setViewMode(_viewId, bgfx::ViewMode::Sequential);
  1137. }
  1138. int nvgCreateBgfxTexture(struct NVGcontext *_ctx,
  1139. bgfx::TextureHandle _id,
  1140. int _width,
  1141. int _height,
  1142. int _flags) {
  1143. struct NVGparams *params = nvgInternalParams(_ctx);
  1144. struct GLNVGcontext *gl = (struct GLNVGcontext *)params->userPtr;
  1145. struct GLNVGtexture *tex = glnvg__allocTexture(gl);
  1146. tex->id = _id;
  1147. tex->width = _width;
  1148. tex->height = _height;
  1149. tex->flags = _flags;
  1150. tex->type = NVG_TEXTURE_RGBA;
  1151. return tex->id.idx;
  1152. }