diff options
Diffstat (limited to '3rdparty/bgfx/examples/common/nanovg/nanovg_bgfx.cpp')
-rw-r--r-- | 3rdparty/bgfx/examples/common/nanovg/nanovg_bgfx.cpp | 99 |
1 files changed, 79 insertions, 20 deletions
diff --git a/3rdparty/bgfx/examples/common/nanovg/nanovg_bgfx.cpp b/3rdparty/bgfx/examples/common/nanovg/nanovg_bgfx.cpp index 41095d853bb..05973f26284 100644 --- a/3rdparty/bgfx/examples/common/nanovg/nanovg_bgfx.cpp +++ b/3rdparty/bgfx/examples/common/nanovg/nanovg_bgfx.cpp @@ -548,6 +548,7 @@ namespace static void nvgRenderViewport(void* _userPtr, int width, int height, float devicePixelRatio) { struct GLNVGcontext* gl = (struct GLNVGcontext*)_userPtr; + gl->state = 0; gl->view[0] = (float)width; gl->view[1] = (float)height; bgfx::setViewRect(gl->m_viewId, 0, 0, width * devicePixelRatio, height * devicePixelRatio); @@ -714,33 +715,24 @@ namespace int allocated = gl->tvb.size/gl->tvb.stride; - if (allocated < gl->nverts) { + if (allocated < gl->nverts) + { gl->nverts = allocated; BX_WARN(true, "Vertex number truncated due to transient vertex buffer overflow"); } - memcpy(gl->tvb.data, gl->verts, gl->nverts * sizeof(struct NVGvertex) ); - gl->state = 0 + if (0 == gl->state) + { + gl->state = BGFX_STATE_BLEND_FUNC(BGFX_STATE_BLEND_SRC_ALPHA, BGFX_STATE_BLEND_INV_SRC_ALPHA); + } + + gl->state |= 0 | BGFX_STATE_RGB_WRITE | BGFX_STATE_ALPHA_WRITE ; -// if (alphaBlend == NVG_PREMULTIPLIED_ALPHA) -// { -// gl->state |= BGFX_STATE_BLEND_FUNC_SEPARATE( -// BGFX_STATE_BLEND_SRC_ALPHA, BGFX_STATE_BLEND_INV_SRC_ALPHA -// , BGFX_STATE_BLEND_ONE, BGFX_STATE_BLEND_INV_SRC_ALPHA -// ); -// } -// else - { - gl->state |= BGFX_STATE_BLEND_FUNC( - BGFX_STATE_BLEND_SRC_ALPHA, BGFX_STATE_BLEND_INV_SRC_ALPHA - ); - } - bgfx::setUniform(gl->u_viewSize, gl->view); for (uint32_t ii = 0, num = gl->ncalls; ii < num; ++ii) @@ -1089,7 +1081,26 @@ error: } NVGcontext* nvgCreate(int edgeaa, unsigned char _viewId) { - return nvgCreate(edgeaa, _viewId, NULL); + return nvgCreate(edgeaa, _viewId, NULL); +} + +void nvgDelete(struct NVGcontext* ctx) +{ + nvgDeleteInternal(ctx); +} + +void nvgState(struct NVGcontext* ctx, uint64_t state) +{ + struct NVGparams* params = nvgInternalParams(ctx); + struct GLNVGcontext* gl = (struct GLNVGcontext*)params->userPtr; + gl->state = state; +} + +uint8_t nvgViewId(struct NVGcontext* ctx) +{ + struct NVGparams* params = nvgInternalParams(ctx); + struct GLNVGcontext* gl = (struct GLNVGcontext*)params->userPtr; + return gl->m_viewId; } void nvgViewId(struct NVGcontext* ctx, unsigned char _viewId) @@ -1099,7 +1110,55 @@ void nvgViewId(struct NVGcontext* ctx, unsigned char _viewId) gl->m_viewId = uint8_t(_viewId); } -void nvgDelete(struct NVGcontext* ctx) +bgfx::TextureHandle nvglImageHandle(NVGcontext* ctx, int image) { - nvgDeleteInternal(ctx); + GLNVGcontext* gl = (GLNVGcontext*)nvgInternalParams(ctx)->userPtr; + GLNVGtexture* tex = glnvg__findTexture(gl, image); + return tex->id; +} + +NVGLUframebuffer* nvgluCreateFramebuffer(NVGcontext* ctx, int width, + int height, int imageFlags) { + NVGLUframebuffer* framebuffer = new NVGLUframebuffer; + framebuffer->ctx = ctx; + framebuffer->image = nvgCreateImageRGBA(ctx, width, height, + imageFlags, NULL); + bgfx::TextureHandle texture = nvglImageHandle(ctx, framebuffer->image); + if (!bgfx::isValid(texture)) { + nvgluDeleteFramebuffer(framebuffer); + return NULL; + } + framebuffer->handle = bgfx::createFrameBuffer(1, &texture, false); + if (!bgfx::isValid(framebuffer->handle)) + { + nvgluDeleteFramebuffer(framebuffer); + return NULL; + } + static uint8_t s_ViewId = 1; // may have a better way to assign new view id + framebuffer->viewId = s_ViewId++; + bgfx::setViewFrameBuffer(framebuffer->viewId, framebuffer->handle); + bgfx::setViewSeq(framebuffer->viewId, true); + return framebuffer; +} + +void nvgluBindFramebuffer(NVGLUframebuffer* framebuffer) { + static NVGcontext* s_prevCtx = NULL; + static uint8_t s_prevViewId; + if (framebuffer != NULL) { + s_prevCtx = framebuffer->ctx; + s_prevViewId = nvgViewId(framebuffer->ctx); + nvgViewId(framebuffer->ctx, framebuffer->viewId); + } else if (s_prevCtx != NULL) { + nvgViewId(s_prevCtx, s_prevViewId); + } +} + +void nvgluDeleteFramebuffer(NVGLUframebuffer* framebuffer) { + if (framebuffer == NULL) + return; + if (framebuffer->image >= 0) + nvgDeleteImage(framebuffer->ctx, framebuffer->image); + if (bgfx::isValid(framebuffer->handle)) + bgfx::destroyFrameBuffer(framebuffer->handle); + delete framebuffer; } |