summaryrefslogtreecommitdiffstatshomepage
path: root/3rdparty/bgfx/examples/common/nanovg/nanovg_bgfx.cpp
diff options
context:
space:
mode:
Diffstat (limited to '3rdparty/bgfx/examples/common/nanovg/nanovg_bgfx.cpp')
-rw-r--r--3rdparty/bgfx/examples/common/nanovg/nanovg_bgfx.cpp98
1 files changed, 68 insertions, 30 deletions
diff --git a/3rdparty/bgfx/examples/common/nanovg/nanovg_bgfx.cpp b/3rdparty/bgfx/examples/common/nanovg/nanovg_bgfx.cpp
index 3fd944178d0..1da34bd790f 100644
--- a/3rdparty/bgfx/examples/common/nanovg/nanovg_bgfx.cpp
+++ b/3rdparty/bgfx/examples/common/nanovg/nanovg_bgfx.cpp
@@ -23,14 +23,13 @@
#define NVG_ANTIALIAS 1
#include <stdlib.h>
-#include <stdio.h>
-#include <string.h>
#include <math.h>
#include "nanovg.h"
#include <bgfx/bgfx.h>
#include <bx/bx.h>
+#include <bx/allocator.h>
BX_PRAGMA_DIAGNOSTIC_IGNORED_MSVC(4244); // warning C4244: '=' : conversion from '' to '', possible loss of data
@@ -113,6 +112,8 @@ namespace
struct GLNVGcontext
{
+ bx::AllocatorI* m_allocator;
+
bgfx::ProgramHandle prog;
bgfx::UniformHandle u_scissorMat;
bgfx::UniformHandle u_paintMat;
@@ -128,9 +129,10 @@ namespace
uint64_t state;
bgfx::TextureHandle th;
+ bgfx::TextureHandle texMissing;
bgfx::TransientVertexBuffer tvb;
- uint8_t viewid;
+ uint8_t m_viewId;
struct GLNVGtexture* textures;
float view[2];
@@ -176,7 +178,7 @@ namespace
{
int old = gl->ctextures;
gl->ctextures = (gl->ctextures == 0) ? 2 : gl->ctextures*2;
- gl->textures = (struct GLNVGtexture*)realloc(gl->textures, sizeof(struct GLNVGtexture)*gl->ctextures);
+ gl->textures = (struct GLNVGtexture*)BX_REALLOC(gl->m_allocator, gl->textures, sizeof(struct GLNVGtexture)*gl->ctextures);
memset(&gl->textures[old], 0xff, (gl->ctextures-old)*sizeof(struct GLNVGtexture) );
if (gl->textures == NULL)
@@ -263,6 +265,11 @@ namespace
, true
);
+ const bgfx::Memory* mem = bgfx::alloc(4*4*4);
+ uint32_t* bgra8 = (uint32_t*)mem->data;
+ memset(bgra8, 0, 4*4*4);
+ gl->texMissing = bgfx::createTexture2D(4, 4, 0, bgfx::TextureFormat::BGRA8, 0, mem);
+
gl->u_scissorMat = bgfx::createUniform("u_scissorMat", bgfx::UniformType::Mat3);
gl->u_paintMat = bgfx::createUniform("u_paintMat", bgfx::UniformType::Mat3);
gl->u_innerCol = bgfx::createUniform("u_innerCol", bgfx::UniformType::Vec4);
@@ -323,9 +330,20 @@ namespace
, 1
, NVG_TEXTURE_RGBA == _type ? bgfx::TextureFormat::RGBA8 : bgfx::TextureFormat::R8
, BGFX_TEXTURE_NONE
- , mem
);
+ if (NULL != mem)
+ {
+ bgfx::updateTexture2D(tex->id
+ , 0
+ , 0
+ , 0
+ , tex->width
+ , tex->height
+ , mem
+ );
+ }
+
return bgfx::isValid(tex->id) ? tex->id.idx : 0;
}
@@ -347,7 +365,12 @@ namespace
uint32_t bytesPerPixel = NVG_TEXTURE_RGBA == tex->type ? 4 : 1;
uint32_t pitch = tex->width * bytesPerPixel;
- bgfx::updateTexture2D(tex->id, 0, x, y, w, h
+ bgfx::updateTexture2D(tex->id
+ , 0
+ , x
+ , y
+ , w
+ , h
, bgfx::copy(data + y*pitch + x*bytesPerPixel, h*pitch)
, pitch
);
@@ -444,8 +467,7 @@ namespace
memcpy(frag->extent, paint->extent, sizeof(frag->extent) );
frag->strokeMult = (width*0.5f + fringe*0.5f) / fringe;
- bgfx::TextureHandle invalid = BGFX_INVALID_HANDLE;
- gl->th = invalid;
+ gl->th = gl->texMissing;
if (paint->image != 0)
{
tex = glnvg__findTexture(gl, paint->image);
@@ -460,7 +482,7 @@ namespace
else
{
frag->type = NSVG_SHADER_FILLGRAD;
- frag->radius = paint->radius;
+ frag->radius = paint->radius;
frag->feather = paint->feather;
}
@@ -502,7 +524,7 @@ namespace
bgfx::setUniform(gl->u_extentRadius, &frag->extent[0]);
bgfx::setUniform(gl->u_params, &frag->feather);
- bgfx::TextureHandle handle = BGFX_INVALID_HANDLE;
+ bgfx::TextureHandle handle = gl->texMissing;
if (image != 0)
{
@@ -527,7 +549,7 @@ namespace
struct GLNVGcontext* gl = (struct GLNVGcontext*)_userPtr;
gl->view[0] = (float)width;
gl->view[1] = (float)height;
- bgfx::setViewRect(gl->viewid, 0, 0, width, height);
+ bgfx::setViewRect(gl->m_viewId, 0, 0, width, height);
}
static void fan(uint32_t _start, uint32_t _count)
@@ -575,7 +597,7 @@ namespace
bgfx::setVertexBuffer(&gl->tvb);
bgfx::setTexture(0, gl->s_tex, gl->th);
fan(paths[i].fillOffset, paths[i].fillCount);
- bgfx::submit(gl->viewid, gl->prog);
+ bgfx::submit(gl->m_viewId, gl->prog);
}
}
@@ -599,7 +621,7 @@ namespace
);
bgfx::setVertexBuffer(&gl->tvb, paths[i].strokeOffset, paths[i].strokeCount);
bgfx::setTexture(0, gl->s_tex, gl->th);
- bgfx::submit(gl->viewid, gl->prog);
+ bgfx::submit(gl->m_viewId, gl->prog);
}
}
@@ -614,7 +636,7 @@ namespace
| BGFX_STENCIL_OP_FAIL_Z_ZERO
| BGFX_STENCIL_OP_PASS_Z_ZERO
);
- bgfx::submit(gl->viewid, gl->prog);
+ bgfx::submit(gl->m_viewId, gl->prog);
}
static void glnvg__convexFill(struct GLNVGcontext* gl, struct GLNVGcall* call)
@@ -631,7 +653,7 @@ namespace
bgfx::setVertexBuffer(&gl->tvb);
bgfx::setTexture(0, gl->s_tex, gl->th);
fan(paths[i].fillOffset, paths[i].fillCount);
- bgfx::submit(gl->viewid, gl->prog);
+ bgfx::submit(gl->m_viewId, gl->prog);
}
if (gl->edgeAntiAlias)
@@ -644,7 +666,7 @@ namespace
);
bgfx::setVertexBuffer(&gl->tvb, paths[i].strokeOffset, paths[i].strokeCount);
bgfx::setTexture(0, gl->s_tex, gl->th);
- bgfx::submit(gl->viewid, gl->prog);
+ bgfx::submit(gl->m_viewId, gl->prog);
}
}
}
@@ -664,7 +686,7 @@ namespace
);
bgfx::setVertexBuffer(&gl->tvb, paths[i].strokeOffset, paths[i].strokeCount);
bgfx::setTexture(0, gl->s_tex, gl->th);
- bgfx::submit(gl->viewid, gl->prog);
+ bgfx::submit(gl->m_viewId, gl->prog);
}
}
@@ -677,7 +699,7 @@ namespace
bgfx::setState(gl->state);
bgfx::setVertexBuffer(&gl->tvb, call->vertexOffset, call->vertexCount);
bgfx::setTexture(0, gl->s_tex, gl->th);
- bgfx::submit(gl->viewid, gl->prog);
+ bgfx::submit(gl->m_viewId, gl->prog);
}
}
@@ -770,7 +792,7 @@ namespace
if (gl->ncalls+1 > gl->ccalls)
{
gl->ccalls = gl->ccalls == 0 ? 32 : gl->ccalls * 2;
- gl->calls = (struct GLNVGcall*)realloc(gl->calls, sizeof(struct GLNVGcall) * gl->ccalls);
+ gl->calls = (struct GLNVGcall*)BX_REALLOC(gl->m_allocator, gl->calls, sizeof(struct GLNVGcall) * gl->ccalls);
}
ret = &gl->calls[gl->ncalls++];
memset(ret, 0, sizeof(struct GLNVGcall) );
@@ -783,7 +805,7 @@ namespace
if (gl->npaths + n > gl->cpaths) {
GLNVGpath* paths;
int cpaths = glnvg__maxi(gl->npaths + n, 128) + gl->cpaths / 2; // 1.5x Overallocate
- paths = (GLNVGpath*)realloc(gl->paths, sizeof(GLNVGpath) * cpaths);
+ paths = (GLNVGpath*)BX_REALLOC(gl->m_allocator, gl->paths, sizeof(GLNVGpath) * cpaths);
if (paths == NULL) return -1;
gl->paths = paths;
gl->cpaths = cpaths;
@@ -800,7 +822,7 @@ namespace
{
NVGvertex* verts;
int cverts = glnvg__maxi(gl->nverts + n, 4096) + gl->cverts/2; // 1.5x Overallocate
- verts = (NVGvertex*)realloc(gl->verts, sizeof(NVGvertex) * cverts);
+ verts = (NVGvertex*)BX_REALLOC(gl->m_allocator, gl->verts, sizeof(NVGvertex) * cverts);
if (verts == NULL) return -1;
gl->verts = verts;
gl->cverts = cverts;
@@ -816,7 +838,7 @@ namespace
if (gl->nuniforms+n > gl->cuniforms)
{
gl->cuniforms = gl->cuniforms == 0 ? glnvg__maxi(n, 32) : gl->cuniforms * 2;
- gl->uniforms = (unsigned char*)realloc(gl->uniforms, gl->cuniforms * structSize);
+ gl->uniforms = (unsigned char*)BX_REALLOC(gl->m_allocator, gl->uniforms, gl->cuniforms * structSize);
}
ret = gl->nuniforms * structSize;
gl->nuniforms += n;
@@ -976,6 +998,7 @@ namespace
}
bgfx::destroyProgram(gl->prog);
+ bgfx::destroyTexture(gl->texMissing);
bgfx::destroyUniform(gl->u_scissorMat);
bgfx::destroyUniform(gl->u_paintMat);
@@ -1001,18 +1024,32 @@ namespace
}
}
- free(gl->textures);
-
- free(gl);
+ BX_FREE(gl->m_allocator, gl->uniforms);
+ BX_FREE(gl->m_allocator, gl->verts);
+ BX_FREE(gl->m_allocator, gl->paths);
+ BX_FREE(gl->m_allocator, gl->calls);
+ BX_FREE(gl->m_allocator, gl->textures);
+ BX_FREE(gl->m_allocator, gl);
}
} // namespace
-NVGcontext* nvgCreate(int edgeaa, unsigned char viewid)
+NVGcontext* nvgCreate(int edgeaa, unsigned char _viewId, bx::AllocatorI* _allocator)
{
+ if (NULL == _allocator)
+ {
+#if BX_CONFIG_ALLOCATOR_CRT
+ static bx::CrtAllocator allocator;
+ _allocator = &allocator;
+#else
+ BX_CHECK(false, "No allocator has been passed to nvgCreate(). Either specify a bx::AllocatorI instance or enable BX_CONFIG_ALLOCATOR_CRT directive.");
+ return NULL;
+#endif // BX_CONFIG_ALLOCATOR_CRT
+ }
+
struct NVGparams params;
struct NVGcontext* ctx = NULL;
- struct GLNVGcontext* gl = (struct GLNVGcontext*)malloc(sizeof(struct GLNVGcontext) );
+ struct GLNVGcontext* gl = (struct GLNVGcontext*)BX_ALLOC(_allocator, sizeof(struct GLNVGcontext) );
if (gl == NULL) goto error;
memset(gl, 0, sizeof(struct GLNVGcontext) );
@@ -1031,8 +1068,9 @@ NVGcontext* nvgCreate(int edgeaa, unsigned char viewid)
params.userPtr = gl;
params.edgeAntiAlias = edgeaa;
+ gl->m_allocator = _allocator;
gl->edgeAntiAlias = edgeaa;
- gl->viewid = uint8_t(viewid);
+ gl->m_viewId = uint8_t(_viewId);
ctx = nvgCreateInternal(&params);
if (ctx == NULL) goto error;
@@ -1049,11 +1087,11 @@ error:
return NULL;
}
-void nvgViewId(struct NVGcontext* ctx, unsigned char viewid)
+void nvgViewId(struct NVGcontext* ctx, unsigned char _viewId)
{
struct NVGparams* params = nvgInternalParams(ctx);
struct GLNVGcontext* gl = (struct GLNVGcontext*)params->userPtr;
- gl->viewid = uint8_t(viewid);
+ gl->m_viewId = uint8_t(_viewId);
}
void nvgDelete(struct NVGcontext* ctx)