Log In  

Cart #rezerododa-0 | 2019-09-25 | Code ▽ | Embed ▽ | License: CC4-BY-NC-SA

An experiment to see if I could get a raytracer running in PICO-8. It's not very speedy, but it's as optimised as I could be bothered to do. If you find further optimisations I'd love to hear about them!

P#68141 2019-09-25 18:24


optimization hints in no order:

  • try shl() to replace any power of 2 multiply
  • inline functions as much as possible
  • favor k,v in pairs(array) instead of for i=1,#array when ordering is not important
  • switch to 64x64 resolution (see pokes from wiki) and use pset instead of rectfill(x/x+1)

over and out :)

P#68146 2019-09-25 21:38 ( Edited 2019-09-25 21:38)
:: dw817

Outstanding ! Reminds me of some of the early demos for Commodore Amiga. Starred.

P#68147 2019-09-26 01:04
:: dw817

I was just thinking of earlier QBasic code doing this. One thing that made it run fast was that SIN and COS calculations were copied to an array and then the raytracer used that.

There is no need to calculate every SIN and COS each machine cycle for a rotating image, right ?

P#68214 2019-09-28 05:00
:: cravo

Yeah, but that’s not the slow part :-). The ray tracing isn’t using any sin/cos stuff. I tried the suggestions from Freds and they didn’t make a significant difference. The best speed up I’ve found so far is to switch both uses of sqrt to x^0.5 but even that didn’t make a huge difference. Thanks for the suggestions so far folks!

P#68216 2019-09-28 08:06

You are using 25 pico cpu!!
There is indeed very litle code optimization that would matter :/

P#68219 2019-09-28 12:14
:: cravo

It was over 30 before I optimised 😀

P#68224 2019-09-28 13:57
:: dw817

Then again we COULD just have ZEP release all the stops in PICO-8 and actually use up the computer's CPU, I would definitely not be against that - and all programs would still run smoothly with _DRAW(), FLIP(), and YIELD().

Running this I see this Firefox tab uses 3% of my CPU. Heaven knows how fast it could be bumped up to and to run even faster in immediate mode or compiled.

P#68226 2019-09-28 14:52 ( Edited 2019-09-28 14:54)
:: cravo

For fun I ported the code to javascript and increased the res to 256x256 and the number of bounces to 3 : http://www.matthewcraven.co.uk/raytrace/

P#68358 2019-10-02 07:27

[Please log in to post a comment]

Follow Lexaloffle:        
Generated 2020-05-31 10:57 | 0.022s | 2097k | Q:47