Browse Source

Implemented LuaJIT FFI versions of love.math.noise, love.math.gammaToLinear, and love.math.linearToGamma.

The new versions of the functions are only used if LuaJIT's JIT compiler is enabled.

Those functions are often combined with ImageData:mapPixel or ImageData:setPixel, which also have LuaJIT FFI versions in 0.10.0 - so without JIT-able implementations of the math functions, the FFI code in the ImageData methods would not be JIT-compiled and would end up taking up to 10 times as long to execute compared to the non-FFI versions in 0.9.2.
Alex Szpakowski 9 years ago
parent
commit
374d1ae0e1

+ 4 - 0
platform/xcode/liblove.xcodeproj/project.pbxproj

@@ -848,6 +848,7 @@
 		FA620A3A1AA305F6005DB4C2 /* types.cpp in Sources */ = {isa = PBXBuildFile; fileRef = FA620A391AA305F6005DB4C2 /* types.cpp */; };
 		FA620A3A1AA305F6005DB4C2 /* types.cpp in Sources */ = {isa = PBXBuildFile; fileRef = FA620A391AA305F6005DB4C2 /* types.cpp */; };
 		FA620A3B1AA305F6005DB4C2 /* types.cpp in Sources */ = {isa = PBXBuildFile; fileRef = FA620A391AA305F6005DB4C2 /* types.cpp */; };
 		FA620A3B1AA305F6005DB4C2 /* types.cpp in Sources */ = {isa = PBXBuildFile; fileRef = FA620A391AA305F6005DB4C2 /* types.cpp */; };
 		FA7550A81AEBE276003E311E /* libluajit.a in Frameworks */ = {isa = PBXBuildFile; fileRef = FA7550A71AEBE276003E311E /* libluajit.a */; };
 		FA7550A81AEBE276003E311E /* libluajit.a in Frameworks */ = {isa = PBXBuildFile; fileRef = FA7550A71AEBE276003E311E /* libluajit.a */; };
+		FA7DA04D1C16874A0056B200 /* wrap_Math.lua in Resources */ = {isa = PBXBuildFile; fileRef = FA7DA04C1C16874A0056B200 /* wrap_Math.lua */; };
 		FA8951A21AA2EDF300EC385A /* wrap_Event.cpp in Sources */ = {isa = PBXBuildFile; fileRef = FA8951A01AA2EDF300EC385A /* wrap_Event.cpp */; };
 		FA8951A21AA2EDF300EC385A /* wrap_Event.cpp in Sources */ = {isa = PBXBuildFile; fileRef = FA8951A01AA2EDF300EC385A /* wrap_Event.cpp */; };
 		FA8951A31AA2EDF300EC385A /* wrap_Event.cpp in Sources */ = {isa = PBXBuildFile; fileRef = FA8951A01AA2EDF300EC385A /* wrap_Event.cpp */; };
 		FA8951A31AA2EDF300EC385A /* wrap_Event.cpp in Sources */ = {isa = PBXBuildFile; fileRef = FA8951A01AA2EDF300EC385A /* wrap_Event.cpp */; };
 		FA8951A41AA2EDF300EC385A /* wrap_Event.h in Headers */ = {isa = PBXBuildFile; fileRef = FA8951A11AA2EDF300EC385A /* wrap_Event.h */; };
 		FA8951A41AA2EDF300EC385A /* wrap_Event.h in Headers */ = {isa = PBXBuildFile; fileRef = FA8951A11AA2EDF300EC385A /* wrap_Event.h */; };
@@ -1506,6 +1507,7 @@
 		FA620A391AA305F6005DB4C2 /* types.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = types.cpp; sourceTree = "<group>"; };
 		FA620A391AA305F6005DB4C2 /* types.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = types.cpp; sourceTree = "<group>"; };
 		FA6AE6041B3335EC00583D5C /* wrap_Graphics.lua */ = {isa = PBXFileReference; lastKnownFileType = text; path = wrap_Graphics.lua; sourceTree = "<group>"; };
 		FA6AE6041B3335EC00583D5C /* wrap_Graphics.lua */ = {isa = PBXFileReference; lastKnownFileType = text; path = wrap_Graphics.lua; sourceTree = "<group>"; };
 		FA7550A71AEBE276003E311E /* libluajit.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; path = libluajit.a; sourceTree = "<group>"; };
 		FA7550A71AEBE276003E311E /* libluajit.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; path = libluajit.a; sourceTree = "<group>"; };
+		FA7DA04C1C16874A0056B200 /* wrap_Math.lua */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = wrap_Math.lua; sourceTree = "<group>"; };
 		FA8951A01AA2EDF300EC385A /* wrap_Event.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = wrap_Event.cpp; sourceTree = "<group>"; };
 		FA8951A01AA2EDF300EC385A /* wrap_Event.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = wrap_Event.cpp; sourceTree = "<group>"; };
 		FA8951A11AA2EDF300EC385A /* wrap_Event.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = wrap_Event.h; sourceTree = "<group>"; };
 		FA8951A11AA2EDF300EC385A /* wrap_Event.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = wrap_Event.h; sourceTree = "<group>"; };
 		FA9B4A0716E1578300074F42 /* SDL2.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = SDL2.framework; path = /Library/Frameworks/SDL2.framework; sourceTree = "<absolute>"; };
 		FA9B4A0716E1578300074F42 /* SDL2.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = SDL2.framework; path = /Library/Frameworks/SDL2.framework; sourceTree = "<absolute>"; };
@@ -2405,6 +2407,7 @@
 				FAB17BEF1ABFB37500F9BA27 /* wrap_CompressedData.h */,
 				FAB17BEF1ABFB37500F9BA27 /* wrap_CompressedData.h */,
 				FA0B7C091A95902C000E1D17 /* wrap_Math.cpp */,
 				FA0B7C091A95902C000E1D17 /* wrap_Math.cpp */,
 				FA0B7C0A1A95902C000E1D17 /* wrap_Math.h */,
 				FA0B7C0A1A95902C000E1D17 /* wrap_Math.h */,
+				FA7DA04C1C16874A0056B200 /* wrap_Math.lua */,
 				FA0B7C0B1A95902C000E1D17 /* wrap_RandomGenerator.cpp */,
 				FA0B7C0B1A95902C000E1D17 /* wrap_RandomGenerator.cpp */,
 				FA0B7C0C1A95902C000E1D17 /* wrap_RandomGenerator.h */,
 				FA0B7C0C1A95902C000E1D17 /* wrap_RandomGenerator.h */,
 			);
 			);
@@ -3197,6 +3200,7 @@
 			isa = PBXResourcesBuildPhase;
 			isa = PBXResourcesBuildPhase;
 			buildActionMask = 2147483647;
 			buildActionMask = 2147483647;
 			files = (
 			files = (
+				FA7DA04D1C16874A0056B200 /* wrap_Math.lua in Resources */,
 			);
 			);
 			runOnlyForDeploymentPostprocessing = 0;
 			runOnlyForDeploymentPostprocessing = 0;
 		};
 		};

+ 52 - 0
src/modules/math/wrap_Math.cpp

@@ -29,6 +29,11 @@
 #include <iostream>
 #include <iostream>
 #include <algorithm>
 #include <algorithm>
 
 
+// Put the Lua code directly into a raw string literal.
+static const char math_lua[] =
+#include "wrap_Math.lua"
+;
+
 namespace love
 namespace love
 {
 {
 namespace math
 namespace math
@@ -414,6 +419,47 @@ int w_decompress(lua_State *L)
 	return 1;
 	return 1;
 }
 }
 
 
+// C functions in a struct, necessary for the FFI versions of math functions.
+struct FFI_Math
+{
+	float (*noise1)(float x);
+	float (*noise2)(float x, float y);
+	float (*noise3)(float x, float y, float z);
+	float (*noise4)(float x, float y, float z, float w);
+
+	float (*gammaToLinear)(float c);
+	float (*linearToGamma)(float c);
+};
+
+static FFI_Math ffifuncs =
+{
+	[](float x) -> float // noise1
+	{
+		return Math::instance.noise(x);
+	},
+	[](float x, float y) -> float // noise2
+	{
+		return Math::instance.noise(x, y);
+	},
+	[](float x, float y, float z) -> float // noise3
+	{
+		return Math::instance.noise(x, y, z);
+	},
+	[](float x, float y, float z, float w) -> float // noise4
+	{
+		return Math::instance.noise(x, y, z, w);
+	},
+
+	[](float c) -> float // gammaToLinear
+	{
+		return Math::instance.gammaToLinear(c);
+	},
+	[](float c) -> float // linearToGamma
+	{
+		return Math::instance.linearToGamma(c);
+	}
+};
+
 // List of functions to wrap.
 // List of functions to wrap.
 static const luaL_Reg functions[] =
 static const luaL_Reg functions[] =
 {
 {
@@ -456,6 +502,12 @@ extern "C" int luaopen_love_math(lua_State *L)
 
 
 	int n = luax_register_module(L, w);
 	int n = luax_register_module(L, w);
 
 
+	// Execute wrap_Event.lua, sending the math table and ffifuncs pointer as args.
+	luaL_loadbuffer(L, math_lua, sizeof(math_lua), "wrap_Math.lua");
+	lua_pushvalue(L, -2);
+	lua_pushlightuserdata(L, &ffifuncs);
+	lua_call(L, 2, 0);
+
 	return n;
 	return n;
 }
 }
 
 

+ 100 - 0
src/modules/math/wrap_Math.lua

@@ -0,0 +1,100 @@
+R"luastring"--(
+-- DO NOT REMOVE THE ABOVE LINE. It is used to load this file as a C++ string.
+-- There is a matching delimiter at the bottom of the file.
+
+--[[
+Copyright (c) 2006-2015 LOVE Development Team
+
+This software is provided 'as-is', without any express or implied
+warranty.  In no event will the authors be held liable for any damages
+arising from the use of this software.
+
+Permission is granted to anyone to use this software for any purpose,
+including commercial applications, and to alter it and redistribute it
+freely, subject to the following restrictions:
+
+1. The origin of this software must not be misrepresented; you must not
+claim that you wrote the original software. If you use this software
+in a product, an acknowledgment in the product documentation would be
+appreciated but is not required.
+2. Altered source versions must be plainly marked as such, and must not be
+misrepresented as being the original software.
+3. This notice may not be removed or altered from any source distribution.
+--]]
+
+local math, ffifuncspointer = ...
+
+if type(jit) ~= "table" or not jit.status() then
+	-- LuaJIT's FFI is *much* slower than LOVE's regular methods when the JIT
+	-- compiler is disabled.
+	return
+end
+
+local status, ffi = pcall(require, "ffi")
+if not status then return end
+
+local type, tonumber = type, tonumber
+
+-- Matches the struct declaration in wrap_Math.cpp.
+pcall(ffi.cdef, [[
+typedef struct FFI_Math
+{
+	float (*noise1)(float x);
+	float (*noise2)(float x, float y);
+	float (*noise3)(float x, float y, float z);
+	float (*noise4)(float x, float y, float z, float w);
+
+	float (*gammaToLinear)(float c);
+	float (*linearToGamma)(float c);
+} FFI_Math;
+]])
+
+local ffifuncs = ffi.cast("FFI_Math *", ffifuncspointer)
+
+
+-- Overwrite some regular love.math functions with FFI implementations.
+
+function math.noise(x, y, z, w)
+	if w ~= nil then
+		return tonumber(ffifuncs.noise4(x, y, z, w))
+	elseif z ~= nil then
+		return tonumber(ffifuncs.noise3(x, y, z))
+	elseif y ~= nil then
+		return tonumber(ffifuncs.noise2(x, y))
+	elseif x ~= nil then
+		return tonumber(ffifuncs.noise1(x))
+	end
+end
+
+local function gammaToLinear(c)
+	if c ~= nil then
+		return tonumber(ffifuncs.gammaToLinear(c / 255)) * 255
+	end
+	return c
+end
+
+function math.gammaToLinear(r, g, b, a)
+	if type(r) == "table" then
+		local t = r
+		return gammaToLinear(t[1]), gammaToLinear(t[2]), gammaToLinear(t[3]), a
+	end
+	return gammaToLinear(r), gammaToLinear(g), gammaToLinear(b), a
+end
+
+local function linearToGamma(c)
+	if c ~= nil then
+		return tonumber(ffifuncs.linearToGamma(c / 255)) * 255
+	end
+	return c
+end
+
+function math.linearToGamma(r, g, b, a)
+	if type(r) == "table" then
+		local t = r
+		return linearToGamma(t[1]), linearToGamma(t[2]), linearToGamma(t[3]), a
+	end
+	return linearToGamma(r), linearToGamma(g), linearToGamma(b), a
+end
+
+-- DO NOT REMOVE THE NEXT LINE. It is used to load this file as a C++ string.
+--)luastring"--"