From bfeb3d2d51e419bc4429915e24bce181ffec134a Mon Sep 17 00:00:00 2001 From: bjorn Date: Fri, 26 Oct 2018 09:14:57 -0700 Subject: [PATCH] lovr.graphics.discard; --- src/api/graphics.c | 10 ++++++++++ src/graphics/graphics.c | 6 ++++++ src/graphics/graphics.h | 2 ++ src/graphics/opengl.c | 26 ++++++++++++++++++++++++++ 4 files changed, 44 insertions(+) diff --git a/src/api/graphics.c b/src/api/graphics.c index 18dda691..24c6c991 100644 --- a/src/api/graphics.c +++ b/src/api/graphics.c @@ -635,6 +635,15 @@ static int l_lovrGraphicsClear(lua_State* L) { return 0; } +static int l_lovrGraphicsDiscard(lua_State* L) { + int top = lua_gettop(L); + bool color = top >= 1 ? lua_toboolean(L, 1) : true; + bool depth = top >= 2 ? lua_toboolean(L, 2) : true; + bool stencil = top >= 3 ? lua_toboolean(L, 3) : true; + lovrGraphicsDiscard(color, depth, stencil); + return 0; +} + static int l_lovrGraphicsPoints(lua_State* L) { uint32_t count = luax_readvertices(L, 1); lovrGraphicsPoints(count); @@ -1313,6 +1322,7 @@ static const luaL_Reg lovrGraphics[] = { // Rendering { "clear", l_lovrGraphicsClear }, + { "discard", l_lovrGraphicsDiscard }, { "points", l_lovrGraphicsPoints }, { "line", l_lovrGraphicsLine }, { "triangle", l_lovrGraphicsTriangle }, diff --git a/src/graphics/graphics.c b/src/graphics/graphics.c index 9a8a9081..b4db4a98 100644 --- a/src/graphics/graphics.c +++ b/src/graphics/graphics.c @@ -398,6 +398,12 @@ void lovrGraphicsClear(Color* color, float* depth, int* stencil) { lovrGpuClear(canvas, color, depth, stencil); } +void lovrGraphicsDiscard(bool color, bool depth, bool stencil) { + Pipeline* pipeline = &state.pipelines[state.pipeline]; + Canvas* canvas = pipeline->canvas ? pipeline->canvas : state.camera.canvas; + lovrGpuDiscard(canvas, color, depth, stencil); +} + void lovrGraphicsDraw(DrawCommand* draw) { Mesh* mesh = draw->mesh; if (!mesh) { diff --git a/src/graphics/graphics.h b/src/graphics/graphics.h index b3149a0a..6d5d7d5a 100644 --- a/src/graphics/graphics.h +++ b/src/graphics/graphics.h @@ -211,6 +211,7 @@ void lovrGraphicsSetProjection(mat4 projection); // Rendering VertexPointer lovrGraphicsGetVertexPointer(uint32_t capacity); void lovrGraphicsClear(Color* color, float* depth, int* stencil); +void lovrGraphicsDiscard(bool color, bool depth, bool stencil); void lovrGraphicsDraw(DrawCommand* draw); void lovrGraphicsPoints(uint32_t count); void lovrGraphicsLine(uint32_t count); @@ -237,6 +238,7 @@ void lovrGpuDestroy(); void lovrGpuBindPipeline(Pipeline* pipeline); void lovrGpuSetViewports(float* viewports, int count); void lovrGpuClear(Canvas* canvas, Color* color, float* depth, int* stencil); +void lovrGpuDiscard(Canvas* canvas, bool color, bool depth, bool stencil); void lovrGpuStencil(StencilAction action, int replaceValue, StencilCallback callback, void* userdata); void lovrGpuCompute(Shader* shader, int x, int y, int z); void lovrGpuPresent(); diff --git a/src/graphics/opengl.c b/src/graphics/opengl.c index 17e15892..690000f5 100644 --- a/src/graphics/opengl.c +++ b/src/graphics/opengl.c @@ -859,6 +859,32 @@ void lovrGpuClear(Canvas* canvas, Color* color, float* depth, int* stencil) { } } +void lovrGpuDiscard(Canvas* canvas, bool color, bool depth, bool stencil) { +#if defined(EMSCRIPTEN) || defined(__ANDROID__) + lovrCanvasBind(canvas, false); + + GLenum attachments[MAX_CANVAS_ATTACHMENTS + 1] = { 0 }; + int count = 0; + + if (color) { + int n = canvas ? canvas->attachmentCount : 1; + for (int i = 0; i < n; i++) { + attachments[count++] = GL_COLOR_ATTACHMENT0 + i; + } + } + + if (depth) { + attachments[count++] = GL_DEPTH_ATTACHMENT; + } + + if (stencil) { + attachments[count++] = GL_STENCIL_ATTACHMENT; + } + + glInvalidateFramebuffer(GL_FRAMEBUFFER, count, attachments); +#endif +} + void lovrGpuStencil(StencilAction action, int replaceValue, StencilCallback callback, void* userdata) { state.depthWrite = false; glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);