mirror of
https://github.com/love2d/love.git
synced 2026-08-14 01:20:56 +02:00
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.
This commit is contained in:
@@ -29,6 +29,11 @@
|
||||
#include <iostream>
|
||||
#include <algorithm>
|
||||
|
||||
// Put the Lua code directly into a raw string literal.
|
||||
static const char math_lua[] =
|
||||
#include "wrap_Math.lua"
|
||||
;
|
||||
|
||||
namespace love
|
||||
{
|
||||
namespace math
|
||||
@@ -414,6 +419,47 @@ int w_decompress(lua_State *L)
|
||||
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.
|
||||
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);
|
||||
|
||||
// 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;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user