From fcbeccdfb16731999e39fe3c772dd11ff2b414c2 Mon Sep 17 00:00:00 2001 From: bjorn Date: Fri, 5 Aug 2022 21:05:02 -0700 Subject: [PATCH] Fix background colors; --- etc/nogame.lua | 2 +- src/api/l_graphics.c | 19 +++++++++++++++++++ src/modules/graphics/graphics.c | 19 +++++++++++++++++++ src/modules/graphics/graphics.h | 3 +++ src/modules/headset/headset_openxr.c | 4 ++++ 5 files changed, 46 insertions(+), 1 deletion(-) diff --git a/etc/nogame.lua b/etc/nogame.lua index a202910d..3871bbcc 100644 --- a/etc/nogame.lua +++ b/etc/nogame.lua @@ -14,7 +14,7 @@ function lovr.load() return end - lovr.graphics.getWindowPass():setClear(0x20232c) + lovr.graphics.setBackground(0x20232c) --[=[ logo = lovr.graphics.newShader([[ diff --git a/src/api/l_graphics.c b/src/api/l_graphics.c index 2397373e..ba3c5988 100644 --- a/src/api/l_graphics.c +++ b/src/api/l_graphics.c @@ -805,6 +805,23 @@ static int l_lovrGraphicsIsFormatSupported(lua_State* L) { return 1; } +static int l_lovrGraphicsGetBackground(lua_State* L) { + float color[4]; + lovrGraphicsGetBackground(color); + lua_pushnumber(L, color[0]); + lua_pushnumber(L, color[1]); + lua_pushnumber(L, color[2]); + lua_pushnumber(L, color[3]); + return 4; +} + +static int l_lovrGraphicsSetBackground(lua_State* L) { + float color[4]; + luax_readcolor(L, 1, color); + lovrGraphicsSetBackground(color); + return 0; +} + static int l_lovrGraphicsGetWindowPass(lua_State* L) { Pass* pass = lovrGraphicsGetWindowPass(); luax_pushtype(L, Pass, pass); @@ -1481,6 +1498,8 @@ static const luaL_Reg lovrGraphics[] = { { "getFeatures", l_lovrGraphicsGetFeatures }, { "getLimits", l_lovrGraphicsGetLimits }, { "isFormatSupported", l_lovrGraphicsIsFormatSupported }, + { "getBackground", l_lovrGraphicsGetBackground }, + { "setBackground", l_lovrGraphicsSetBackground }, { "getWindowPass", l_lovrGraphicsGetWindowPass }, { "getDefaultFont", l_lovrGraphicsGetDefaultFont }, { "getBuffer", l_lovrGraphicsGetBuffer }, diff --git a/src/modules/graphics/graphics.c b/src/modules/graphics/graphics.c index 78c718ab..bbed9d3d 100644 --- a/src/modules/graphics/graphics.c +++ b/src/modules/graphics/graphics.c @@ -362,6 +362,7 @@ static struct { gpu_device_info device; gpu_features features; gpu_limits limits; + float background[4]; Texture* window; Pass* windowPass; Font* defaultFont; @@ -754,6 +755,20 @@ void lovrGraphicsGetShaderCache(void* data, size_t* size) { gpu_pipeline_get_cache(data, size); } +void lovrGraphicsGetBackground(float background[4]) { + background[0] = lovrMathLinearToGamma(state.background[0]); + background[1] = lovrMathLinearToGamma(state.background[1]); + background[2] = lovrMathLinearToGamma(state.background[2]); + background[3] = state.background[3]; +} + +void lovrGraphicsSetBackground(float background[4]) { + state.background[0] = lovrMathGammaToLinear(background[0]); + state.background[1] = lovrMathGammaToLinear(background[1]); + state.background[2] = lovrMathGammaToLinear(background[2]); + state.background[3] = background[3]; +} + void lovrGraphicsSubmit(Pass** passes, uint32_t count) { if (!state.active) { return; @@ -3409,6 +3424,10 @@ void lovrPassReset(Pass* pass) { } else { pass->target.color[i].texture = lovrGraphicsGetWindowTexture()->gpu; } + + // The window pass always uses the global clear color. The intent is that exposing a global + // clear color that applies to the window/headset is more friendly than messing with passes. + lovrGraphicsGetBackground(pass->target.color[0].clear); } if (canvas->mipmap) { diff --git a/src/modules/graphics/graphics.h b/src/modules/graphics/graphics.h index 91c9cfc5..95ddd35f 100644 --- a/src/modules/graphics/graphics.h +++ b/src/modules/graphics/graphics.h @@ -103,6 +103,9 @@ void lovrGraphicsGetLimits(GraphicsLimits* limits); bool lovrGraphicsIsFormatSupported(uint32_t format, uint32_t features); void lovrGraphicsGetShaderCache(void* data, size_t* size); +void lovrGraphicsGetBackground(float background[4]); +void lovrGraphicsSetBackground(float background[4]); + void lovrGraphicsSubmit(Pass** passes, uint32_t count); void lovrGraphicsPresent(void); void lovrGraphicsWait(void); diff --git a/src/modules/headset/headset_openxr.c b/src/modules/headset/headset_openxr.c index 6d94109c..b958d105 100644 --- a/src/modules/headset/headset_openxr.c +++ b/src/modules/headset/headset_openxr.c @@ -1697,6 +1697,10 @@ static Pass* openxr_getPass(void) { return NULL; } + float color[4][4], depth, stencil; + lovrPassGetClear(state.pass, color, &depth, &stencil); + lovrGraphicsGetBackground(color[0]); + lovrPassSetClear(state.pass, color, depth, stencil); lovrPassSetTarget(state.pass, &texture, NULL); lovrPassReset(state.pass);