Browse Source

Add t.graphics.lowpower = true/false love.conf option (defaults to false).

Add love.graphics.isLowPowerPreferred.

It's currently hooked up in the vulkan backend.
#2127
Sasha Szpakowski 8 months ago
parent
commit
da365e2ab9

+ 11 - 0
src/modules/graphics/Graphics.cpp

@@ -44,6 +44,7 @@ namespace graphics
 {
 
 static bool gammaCorrect = false;
+static bool lowPowerPreferred = false;
 static bool debugMode = false;
 static bool debugModeQueried = false;
 
@@ -149,6 +150,16 @@ void setRenderers(const std::vector<Renderer> &renderers)
 	_renderers = renderers;
 }
 
+void setLowPowerPreferred(bool preferred)
+{
+	lowPowerPreferred = preferred;
+}
+
+bool isLowPowerPreferred()
+{
+	return lowPowerPreferred;
+}
+
 Graphics *Graphics::createInstance()
 {
 	Graphics *instance = Module::getInstance<Graphics>(M_GRAPHICS);

+ 3 - 0
src/modules/graphics/Graphics.h

@@ -109,6 +109,9 @@ const std::vector<Renderer> &getDefaultRenderers();
 const std::vector<Renderer> &getRenderers();
 void setRenderers(const std::vector<Renderer> &renderers);
 
+void setLowPowerPreferred(bool preferred);
+bool isLowPowerPreferred();
+
 class Graphics : public Module
 {
 public:

+ 2 - 2
src/modules/graphics/vulkan/Graphics.cpp

@@ -1605,9 +1605,9 @@ int Graphics::rateDeviceSuitability(VkPhysicalDevice device)
 	// optional
 
 	if (deviceProperties.deviceType == VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU)
-		score += 1000;
+		score += isLowPowerPreferred() ? 100 : 1000;
 	if (deviceProperties.deviceType == VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU)
-		score += 100;
+		score += isLowPowerPreferred() ? 1000 : 100;
 	if (deviceProperties.deviceType == VK_PHYSICAL_DEVICE_TYPE_VIRTUAL_GPU)
 		score += 10;
 

+ 7 - 0
src/modules/graphics/wrap_Graphics.cpp

@@ -198,6 +198,12 @@ int w_isGammaCorrect(lua_State *L)
 	return 1;
 }
 
+int w_isLowPowerPreferred(lua_State *L)
+{
+	luax_pushboolean(L, graphics::isLowPowerPreferred());
+	return 1;
+}
+
 int w_getWidth(lua_State *L)
 {
 	lua_pushinteger(L, instance()->getWidth());
@@ -4112,6 +4118,7 @@ static const luaL_Reg functions[] =
 	{ "isCreated", w_isCreated },
 	{ "isActive", w_isActive },
 	{ "isGammaCorrect", w_isGammaCorrect },
+	{ "isLowPowerPreferred", w_isLowPowerPreferred },
 	{ "getWidth", w_getWidth },
 	{ "getHeight", w_getHeight },
 	{ "getDimensions", w_getDimensions },

+ 5 - 0
src/modules/love/boot.lua

@@ -183,6 +183,7 @@ function love.init()
 		},
 		graphics = {
 			gammacorrect = false,
+			lowpower = false,
 			renderers = nil,
 			excluderenderers = nil,
 		},
@@ -259,6 +260,10 @@ function love.init()
 		love._setGammaCorrect(gammacorrect)
 	end
 
+	if love._setLowPowerPreferred and type(c.graphics) == "table" then
+		love._setLowPowerPreferred(c.graphics.lowpower)
+	end
+
 	if love._setRenderers then
 		local renderers = love._getDefaultRenderers()
 		if type(c.renderers) == "table" then

+ 11 - 0
src/modules/love/love.cpp

@@ -423,6 +423,14 @@ static int w__setRenderers(lua_State *L)
 	return 0;
 }
 
+static int w__setLowPowerPreferred(lua_State *L)
+{
+#ifdef LOVE_ENABLE_GRAPHICS
+	love::graphics::setLowPowerPreferred(love::luax_checkboolean(L, 1));
+#endif
+	return 0;
+}
+
 static int w__setHighDPIAllowed(lua_State *L)
 {
 #ifdef LOVE_ENABLE_WINDOW
@@ -556,6 +564,9 @@ int luaopen_love(lua_State *L)
 	lua_pushcfunction(L, w__setRenderers);
 	lua_setfield(L, -2, "_setRenderers");
 
+	lua_pushcfunction(L, w__setLowPowerPreferred);
+	lua_setfield(L, -2, "_setLowPowerPreferred");
+
 	lua_pushcfunction(L, w__setHighDPIAllowed);
 	lua_setfield(L, -2, "_setHighDPIAllowed");