diff --git a/modules/mat4.lua b/modules/mat4.lua index d06b394..f664749 100644 --- a/modules/mat4.lua +++ b/modules/mat4.lua @@ -22,6 +22,7 @@ local function new(m) 0, 0, 0, 0, 0, 0, 0, 0 } + m._m = m return setmetatable(m, mat4_mt) end @@ -33,6 +34,16 @@ local function identity(m) return m end +-- Do the check to see if JIT is enabled. If so use the optimized FFI structs. +local status, ffi, the_type +if type(jit) == "table" and jit.status() then + status, ffi = pcall(require, "ffi") + if status then + ffi.cdef "typedef struct { double _m[16]; } cpml_mat4;" + new = ffi.typeof("cpml_mat4") + end +end + -- Statically allocate a temporary variable used in some of our functions. local tmp = new() local t44 = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } @@ -630,7 +641,31 @@ function mat4.to_frustum(a, infinite) return frustum end -mat4_mt.__index = mat4 +--mat4_mt.__index = mat4 +function mat4_mt.__index(t, k) + if type(t) == "cdata" then + if type(k) == "number" then + return t._m[k-1] + end + elseif type(k) == "number" then + return t._m[k] + end + + return rawget(mat4, k) +end + +function mat4_mt.__newindex(t, k, v) + if type(t) == "cdata" then + if type(k) == "number" then + t._m[k-1] = v + end + elseif type(k) == "number" then + t._m[k] = v + end + + rawset(mat4, k, v) +end + mat4_mt.__tostring = mat4.to_string function mat4_mt.__call(_, a) @@ -665,4 +700,8 @@ function mat4_mt.__mul(a, b) return mat4.mul_mat4x1({}, a, b) end +if status then + ffi.metatype(new, mat4_mt) +end + return setmetatable({}, mat4_mt)