r/GraphicsProgramming • • Oct 13 '26

Source Code A little software renderer written in C that I was working on over the weekend

https://github.com/nimrag-b/Nim-Software-Renderer/

Hey guys! This is a little project I made over the weekend, its a simple software renderer that renders to an abstract grid of pixels. Basically, anything that can provide a pointer to a pixel array is a valid target.

Currently uses a simple fixed function rendering pipeline, but it was designed to take a function pointer for both the Vertex and Fragment shader, which will become accessible to the public API when ive cleaned them up a bit.

It requires no external code other than a c runtime, and my tests show it runs pretty fast, despite processing each triangle single-threaded. It supports per-vertex colours, backface culling and a depth buffer. It also makes zero allocations other than creating the pipeline object at the start of the program, and ive tried to lay things out in a cache friendly way.

I dont like feeling like I have to say this, but zero AI was involved at any point during making this, this was all coded by hand.

Anyway, lemme know what you guys think, let me know about any bugs/ missing features, and feel free to contribute to the repo!

29 Upvotes

14 comments sorted by

3

u/corysama Oct 13 '26

Have you run it through a profiler yet?

Having SR_DrawPoint in your inner loop is not helping your performance. Move the checks it's doing out of the max_y, max_x loops and your frame rate will thank you.

2

u/nimrag_is_coming Oct 13 '26

Ah good catch! Ive tried to avoid doing thinks like that in the inner loop, but i somehow managed to miss this glaringly obvious one haha. Although its only really checking bounds which shouldnt be too expensive, but when youre running it 2000000 a frame it does add up.

Haven't run it though a profiler yet, but rendering the things ive tested so far completes in the scale of nanoseconds, so didnt feel it nessacery yet haha

2

u/nimrag_is_coming Oct 13 '26

updated repo fixing this :)

2

u/rAngus3 Oct 13 '26

It'd be great to know which algos you have implemented for the several features and/or papers/articles/books you have followed.

6

u/nimrag_is_coming Oct 13 '26

A lot of it comes from just me and my knowledge of how rendering works. I followed a formula for generating barycentric coordinates for triangles from Wikipedia, as well as the bresenham line drawing algorithm. Other than that, I referenced the perspective matrices from Opengl and DirectX (which are slightly different, apparently), as well as the documentation for triangle rasterisation rules from DirectX.

Other than that, it's a lot of internet research and scrolling through stackoverflow posts for things that I didn't understand, and a lot of just working through things and making it up.

3

u/rAngus3 Oct 13 '26

thanks! imho people that wants to implement a software rasterizer would appreciate your reply :D

1

u/nimrag_is_coming Oct 13 '26

Haha I hope so. If there's one thing I have learned from this project is that you can make a renderer without matrix multiplication, but wow it does make your life a while lot easier with it.

1

u/Wittyname_McDingus Oct 14 '26

Nice project! Can you post a picture or two here or in the repo? They go a long way :)

1

u/nimrag_is_coming Oct 14 '26

sure, gimme a sec and ill post something :)

1

u/ACRM64 Oct 14 '26

I'm guessing this is Windows only? Any chance of a Linux version?!

1

u/nimrag_is_coming Oct 14 '26

Completely platform agnostic, can run on anything that can provide a pointer to a grid of pixels :)

1

u/nimrag_is_coming Oct 14 '26

The windows platform code is just some helper functions for easy interop with a Windows window, it doesn't do anything special, and is non-essential. Should have probably made that more clear haha

1

u/ACRM64 Oct 14 '26

Excellent!

3

u/tastygames_official Oct 13 '26

you didn't need to say you didn't use AI. People who use AI either just drop a link or have AI write a long-ass text with emoticons and em dashes everywhere.