Fast EditableImage Triangle Rasterizer (FASTER THAN ROBLOXS OFFICIAL ONE?!?!)

I’ve written a very fast triangle rasterizer for EditableImage. I’m going to go over how I wrote it and why it’s so damn fast.
If you want to read the source code for it now, click here!

Why make this?

Roblox has a built-in function for drawing triangles on an EditableImage (also called DrawTriangle). But it isn’t publicly accessible as of the time this thread was created. I attempted to create one myself, but previous iterations of the function had issues. Now, I am confident that what I’ve created is FAST and doesn’t have bugs.

How did you make this?

EditableImage functions like a software renderer in a way. Most of the calculations that you perform for it will run on the CPU and can’t be offloaded to the GPU. This presents problems where we need to draw triangles at a fast speed but are limited by the CPU.

I decided to look at the specification of OpenGL 1.1 to see how they did rasterization. It uses a process called scanline rasterization. Basically, a triangle would be drawn one line at a time until every line has been drawn. This would be totally fast, because it turns something like a few thousand draw calls into something like a few hundred.

The initial results

My original iteration of the function actually performed slightly slower than Roblox’s own internal triangle drawing function. It took 1.0434729000007792 seconds to complete the benchmark when Roblox’s own took 0.9514149999995425 seconds. I thought I was defeated and this was about as good as I could get, when I realized something insane.

Native codegen

Adding the @native attribute to my function was something I’d considered. I studied the Luau source code a few days before to understand how much power you could get out of native. After realizing everything, I applied types to my codebase to make it compatible with strict mode. Then, I added the native attribute to the scanline utility function and the main function itself.

The results?

TWICE AS FAST AS ROBLOX’S OWN FUNCTION

As I stated in the repository:

This version of DrawTriangle took 0.4369358000003558 seconds to complete the benchmark, taking around 145.64526666678526 microseconds per triangle.

The Roblox version of DrawTriangle took 0.9570504000002984 seconds to complete the benchmark, taking around 319.01680000009947 microseconds per triangle.

What does this mean?

We have genuinely achieved better performance than Roblox’s own methods! This means that this function is more viable than the internal methods Roblox is using. I mean, the competition has been destroyed basically.

On top of that…

This function supports ImageCombineType, so now it has parity with the official Draw functions of EditableImage

TLDR: This is the fastest triangle draw function for EditableImage

If you got to the end of this, you might not have known the link for this on GitHub was at the top.
So here, I’ll give you a chance to click it again.

9 Likes

quoting two singular benches is not sufficient, so i dont really trust the post

Hi,
When I try drawing a triangle using this code a line appears at the bottom of the triangle:
DrawTriangle(editableimage, Vector2.new(50, 50), Vector2.new(50, 200), Vector2.new(200, 200),Color3.new(0,0,0),0,Enum.ImageCombineType.BlendSourceOver)

Is there any chance you could fix this? It seems to happen for all triangles with a base that is parallel to the ground. Editable image size is 256

Hey there! Sorry I didn’t get to your reply soon enough (I don’t use the devforum too much)

I believe that I fixed the bug. The bug was in the scanline renderer which I didn’t catch during testing. I retested your exact parameters you provided and the issue does not seem to occur anymore

These changes have been pushed to the repository if you want to try them for yourself

1 Like