hn.today

I wrote a ray tracer in Brainfuck

epestr.com39 points12 comments
Screenshot of I wrote a ray tracer in Brainfuck

This recounts building a full ray tracer in Brainfuck as a constrained engineering exercise. Motivated by a claim that exotic build-language tools exist, the project implements a classic ray tracer using Brainfuck’s eight instructions and a one-sided byte tape. Floating-point values are encoded in signed Q16.16 fixed-point across four cells; multiplication is implemented as convolution of cell products with low cells discarded, division as manual long division with repeated subtraction per cell, and square root computed via integer square-root on scaled integers (isqrt) to preserve Q16.16 precision. A tiny linear congruential generator A = (5*A + 1) % 256 provides randomness for supersampling. The toolchain converts higher-level code into an SSA-like DSL (abs, add, mul, sqrt, if, while, etc.); an LLM was used only to linearize recursion to iteration. Primitives include move/copy routines, two’s-complement negation, boolean-as-low-bit, and comparison by paired decrements.

The resulting Brainfuck program is large (23 MB) and extremely slow: about 100 ray calculations per minute (roughly one pixel per minute), implying a full 400×225 image would take on the order of months; a partial run produced 1,229 of 90,000 pixels with only ten differing by one from the C reference. The write-up documents implementation trade-offs (precision vs. speed, skipping normalization) and identifies possible optimizations. Source code is published under the repository mTvare6/rayfuck.

Read on epestr.com12 comments on Hacker News

Summary generated by AI from the linked article. hn.today is not affiliated with Hacker News or Y Combinator.

More in Programming

The daily digest

Today's best Hacker News stories, summarized and screenshotted, one email a day.