Raytracer optimization help needed

Hi all!!!
Alr so before i continue - Yes its stupid to create a real time raytracer on a platform that doesn’t support shader coding and compute shaders, not even GPU access, but whatever Ig im looking for something more challenging.

So its been going well so far. Before i continue i must mention a few settings / how it works.

  1. Pixel step - aka if the pixel step is set to 2, it will run the simulation at half the resolution/ half the rays.
  2. Ray step- aka how many frames it takes to create 1 render. (If i have to shoot 10k raycasts each frame, if the ray step is set to 2, it will shoot 5k raycasts but twice/per 2 frames)

Hope i made it clear enough

And yeah thats pretty much it, now i wont share code cuz its just a full on mess tbh, but at a Pixel step of 4(aka quarter resolution) i get around 150 fps, which is actually pretty decent, but the quality is meh. Im looking to achieve the same fps but for half the resolution (or 2 times the current one).

This can be done by just setting the PixelStep from 4 to 2, but the fps drops to like 30 so you can see the problem yourself

Anyway, i am looking for suggestions on how i can optimize it while still keeping the visuals at a decent level. AND maybe if POSSIBLE a way to use multithreading

i made a multithreaded ascii renderer that works at a decent fps on my computer
but it doesnt have much of a performance gain, maybe it was implemented wrong
at a high number of actors the performance is actually much worse

it would probably work better if each thread was responsible for its own part of the image and updated asynchronously

if youre using frames for pixels then it might also be faster to use editableimage to minimize instances

asciirenderer.rbxm (9.9 KB)

1 Like

Yeah i already use editable images, I tried your example and im quite suprised, because i thought that Actors can only handle server scripts and not local scripts, but is it really multithreading? Since from what i know lua is single-thread language, but i am unsure if that applies only for coroutines

Parallel Luau is true multithreading, it runs code in separate Luau VMs. Coroutines don’t actually multithread, it pauses the execution of one bit of code to run another bit of code.

Parallel Luau is quite complicated, in order to run code in parallel you first need to put scripts under an Actor, then write your code to desynchronize (either by calling task.desynchronize or using RBXScriptSignal:ConnectParallel()). Not all code can be run in parallel, operations on the DataModel are typically not safe to run in parallel due to race conditions, which results in undefined behavior. As code runs in a separate VM, you also need to use SharedTables/Actor messaging/Events to share data.

Currently the client is limited to 3 threads, so you might not benefit a lot from using it.

If you’d like to learn Parallel Luau I suggest reading the creator documentation on it.
https://create.roblox.com/docs/scripting/multithreading