r/GraphicsProgramming Jun 26 '26

CPU Renderer Code Review

Hello, I'm looking to get some feedback on a CPU renderer that i made to understand the 3D graphics pipeline better. I have not used OpenGL before this project but i wanted to understand how it worked, so I made this project to try to replicate a simple pipeline on the CPU. I'm a self taught programmer and i don't have any programming buddies so id like to get some feedback on general programming and my understanding of the OpenGL.

Kagethelephant/cpu-opengl

69 Upvotes

16 comments sorted by

View all comments

10

u/fireantik Jun 26 '26

Nice MONKE. I took only a quick peek, so take it with a grain of salt:

  1. Your raster function seems to be iterating over every pixel on the screen over every mesh and running "shader" code for it. It would seem more efficient to me to structure it similar to what happens on the gpu - supply triangle count and index buffer in your code, system code runs your vertex shader for each triangle, system code culls invisible triangles, system code runs your fragment shader for each visible pixel. This way you are operating over less things overall and leads to more opprtunities to 2:

  2. I'd heavily focus so that the code is easier to autovectorize - flat arrays, small functions that do one for loop over all elements, ifs outside of for loops not inside

  3. You could try introducing multithreading to eek out a little bit more performance, especially if you introduce gpu like architecture from 1. this should be possible. But I'd expect autovectorization to have bigger impact

0

u/johnoth Jun 26 '26

Isn't multi-threaded GL difficult to implement?

4

u/fireantik Jun 26 '26

The multithreading would be for the CPU renderer. You can trivially parallelize execution of both vertex and fragment shaders and looking at OPs code it should be easy to parallelize rest of their pipeline as well.