lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Roberto Ierusalimschy wrote:
>> I would like to be able to process lots of small mathematical objects
>> (vector3 and quaternions) in a resource constrained environment
>> (computer game), without having to pay the cost of new-ing and
>> garbage collecting them. I.e. I would just like to reuse a pool of
>> existing objects. 
> 
> Did you meet performance problems or are you just anticipating them?

I implemented such a geometric object system in my application too, and
I met such performance problems. With a few hundred entities in my scene
graph, each recomputing a few matrices in their update method from their
position and orientation, it allocated several tens of megabytes of
temporary data per second.

I then added methods to objects to perform computation in-place and
avoid having to allocate temporary objects for intemediate
sub-expressions, and it gave a significant speed boost.

Here is an example of the optimization:

function model:init()
    self.orbital_orientation = quaternion()
    self.altitude = 0
    self.orientation = quaternion()
    self.position = vector()
end

function model:update_slow()
    self.global_matrix =
        matrixh(self.orbital_orientation) *
        matrixh(vector(0,self.altitude,0)) *
        matrixh(self.orientation) *
        matrixh(self.position)
end

local midentity = matrixh()
local valtitude = vector()
function model:update_fast()
    local global_matrix = self.global_matrix
    geometry.matrixh.copy(global_matrix, midentity)
    global_matrix:rotate(self.orbital_orientation)
    valtitude.y = self.altitude
    global_matrix:translate(valtitude)
    global_matrix:rotate(self.orientation)
    global_matrix:translate(self.position)
end

And even if that optimized form is acceptable for entity update, for
skeletal animation of a couple hundred of entities with 64 bones each it
just takes too much time to run at interactive framerate, so instead I
rewrite these critical bottlenecks in C.

I was confident that I could do most of my 3D/OpenGL stuff in Lua, but
while it's fine for a few  objects to demo some shaders, for a
full-featured virtual world renderer Lua becomes the bottleneck.

On the other hand I didn't try adding new datatypes to the Lua VM. Did
someone try that for such 3D math, and was that successful ?