Fast Mesh Vertex Setting

Showcase your libraries, tools and other projects that help your fellow love users.
Post Reply
User avatar
Tanner
Party member
Posts: 166
Joined: Tue Apr 10, 2012 1:51 am

Fast Mesh Vertex Setting

Post by Tanner » Mon Jan 23, 2017 2:02 pm

Are you using meshes with thousands or tens of thousands of vertices? Are you updating those vertices every frame? Do you long for speed, blazing speed above all else, including memory safety and your game targets platforms where you have access to LuaJIT's FFI?

Image

It doesn't seem to be on the wiki anywhere but this little gem does appear in the changelog for Love version 0.10.0: "Added the ability for love.graphics.newMesh and Mesh:setVertices to accept a Data object." Under the hood, what this means, is that instead of iterating over a Lua table and pulling vertex data from it, `memcpy` can be used on a piece of contiguous memory to buffer your vertex data. What this means is you're faster than this. Don't think you are, know you are. Come on. Stop trying to hit me and hit me.

Example implementation: https://gist.github.com/TannerRogalsky/ ... 9ff571cff2

Essentially, we 'abuse' `love.image.newImageData` to allocate us a block of memory, cast that pointer into something useful using the FFI and then gain a 10 to 20 times speed increase when setting vertices.
==BENCHMARKS ==

# Lua Table
0.01018577840228970278479980748898
0.01030243035248539114345778244797
0.01036473468815287020394766415166
0.01040154481597710400819778442383
0.01147316898359864410783526267323

# Data Object
0.00056909972287068875691079528067
0.00057624589825555996326633678706
0.00060287722488671811535421163342
0.00062500485667250872331979216767
0.00064352708351280958967710610352
Benchmarks performed on a MacBook Pro (Retina, 13-inch, Early 2015).

Post Reply

Who is online

Users browsing this forum: Baidu [Spider], Bing [Bot], Google [Bot] and 5 guests