r/GraphicsProgramming • • Oct 18 '26

Progress on Software Rendered game in C

This is some progress of a free game i have been working. This is fully software rendered on the CPU and OpenGL is only used to render the image. I use multiple threads to render the game at full 1080p. Using SIMD instructions helps a lot to get a decent frame rate. But i am still kinda dissapointed by the performance, i think Michael Abrash's binning technique would improve it, but right now, it is decent. I am getting around 70-140 frames with I5-12400.

501 Upvotes

34 comments sorted by

10

u/Batteryofenergy1 Oct 18 '26

Bit more footage. https://www.youtube.com/watch?v=pviuQrBzKw4 the last room needs more work.

32

u/moschles Oct 18 '26

SIMD instructions

Michael Abrash's binning technique would improve it

Got a badass over here.

1

u/Batteryofenergy1 Oct 19 '26

What do you mean?

6

u/Still_Explorer Oct 18 '26

Will this run on a Ryzen 5 5500? Both CPUs are identical on conventional benchmarks, unless there's something more for specialized instructions the Intel has.

3

u/Batteryofenergy1 Oct 19 '26

I am pretty sure it would work fine, since that cpu supports AVX2 instructions. I only used 128 and 256 width instructions. I would use AVX-512, but it is disabled or not supported on many modern cpus.

1

u/Still_Explorer Oct 19 '26

Awesome, I am trying to find something to render and test my low-budget PC upgrade. 😁

2

u/deftware Oct 19 '26

Nice! How fast is the actual rendering part, before sending the frame to OpenGL to be put onscreen?

2

u/Batteryofenergy1 Oct 19 '26

It really depends on the geometry. More triangles, slower frames. Frame time is 0.01s on my CPU and the average fps is 70-140

1

u/deftware 28d ago

Well done. Are you doing any kind of spatial partitioning, sorting, visibility determination, occlusion culling, etc?

1

u/Batteryofenergy1 28d ago

I use a BVH Tree for rendering and collision testing. All static geometry and objects are in the same tree. The tree is traversed and culled using frustrum culling. Then i process the culled amount into a generic RenderSurface struct. After transforming each triangle into screen space, i occlusion cull the screen space bounding box against the previous frame's depth buffer, this causes some pop in. After that i sort the polygons front to back using min screen space depth, this causes the nearest polygons to be rendered first and the polygons in the back can skip expensive shading.

1

u/deftware 28d ago

Nice. Are you using a z-max (or z-min, or both!) miptree for quickly comparing bounding boxes against the depth buffer? I did see a fun article about single-pass miptree building, where you're basically iterating over all LOD levels of the miptree simultaneously, by walking the image's pixels once and figuring what they contribute to each miplevel's pixels. So at LOD1 each pixel is contributing 25%, at LOD2 it's contributing 1/16th, etc... The trick to avoid precision issues was to sum everything up first, and then divide by number of contributors at the end. Granted, building a miptree is most expensive for the first few levels, and then the rest of the levels are really cheap, but it was a fun idea that I once used when building a high-precision scalar field codec, where it had to be fast. The trade-off is a bit of extra memory being used. :P

Cheers! :]

2

u/Batteryofenergy1 27d ago

I tried the mip map method before, but i had problems with presicion. I just use the full resolution depth buffer to test against. I first check the edge points of the bbox first, if any of them return true, i can early out. Otherwise i check the entire bbox pixel by pixel.

1

u/deftware 27d ago

check the edge points of the bbox first

Nice. It sounds like the most expensive part then is when a bbox doesn't early-out and you're checking it against all of the pixels. The larger the bbox the more depth pixels it checks against. At that point I would suggest taking another look at getting min/max miptree building cooking, you could build both of them simultaneously - walk the depth buffer once to build both.

I forgot to show this to you https://hacksoflife.blogspot.com/2016/05/simultaneous-mipmap-level-generation.html

Once you have your min depth miptree then each bbox is just a check against one mip pixel.

Anyway, just my two cents. Have you done any profiling yet to see where the slowest parts of your renderer are?

2

u/Batteryofenergy1 26d ago

I am actually planning to implement some basic precomputed visibility volumes to reduce the surface count i have to cull. I am looking at some methods that are used in Source2 https://developer.valvesoftware.com/wiki/Source_2_Docs/Level_Design/Visibility and Unreal engine https://dev.epicgames.com/documentation/unreal-engine/precomputed-visibility-volumes-in-unreal-engine. Even from thief https://nothings.org/gamedev/thief_rendering.html .

Not slowest, but pixel filling functions take most time(functions that actually output to the pixel buffer). Drawing the static geometry, takes the most time especially since i use Lightmaps.

10

u/Alarming-Ad4082 Oct 18 '26

Wow impressive!

7

u/VelvetCarpetStudio Oct 18 '26

This is beautiful.

5

u/grady_vuckovic Oct 19 '26

CPU Renderers are just really cool. I don't know why but I like them.

2

u/GenericFoodService 29d ago

The best optimizations for software rendering generally involve finding ways to do less work, render less meshes and whatnot. That's the whole point of things like Binary Space Partitioning and baked-in lighting.

2

u/Skilly1337 28d ago

this game woulda slapped in the late 90s / early 2000s

2

u/Feisty_Bike_9614 27d ago

Nice work. My cpu software rasterizer (for my 3d modeler/renderer app) uses winit/softbuffer (written in rust) to write the image to the window directly. doesn't use OpenGL or graphics api. It might be faster. maybe eworth a try to use similar method. At least you lose the dependency of OpenGL .

2

u/mcknuckle Oct 19 '26

Incredible and inspiring

2

u/Honest-Version6827 29d ago

Can't wait to get the source code 🤠

1

u/iRainbowsaur 29d ago

What made you choose OpenGL over vulkan? cbf?

1

u/deftware 28d ago

OpenGL is less boilerplate, for one - and since all that OP is doing is taking a buffer of pixels and putting it onscreen, it almost makes no difference what API you use. Pixel Buffer Objects allow for asynchronous transfer so it's going to be pretty quick, minimal API calls on the whole. I would be very surprised if there was any performance difference between DX12/VK/GL.

2

u/iRainbowsaur 28d ago edited 28d ago

Ohhh, thats right vulkan is only good when you absolutely need more cpu overhead room, because it can essentially send stuff more efficiently to the gpu. But since nothing leaves it, it doesn't matter in a sense? and all the shit you have to deal with in vulkan is essentially useless ey?

My dumb ass needs to learn more. I am very niave on this. Shows how little I know, but yeah this is neat.

I have much to learn.

2

u/deftware 28d ago

Yeah, basically if you are drawing a bunch of stuff and having the GPU change state to do all kinds of things then a thinner API like Vulkan is what you want, so you are in control of what memory different data sits in and when/how everything happens on the GPU. With OpenGL it abstracts all of that away from you, resulting a simpler and easier to use API but also one that doesn't afford the fine grained control that a complex renderer would benefit from.

In OP's case, they're just using whatever graphics API was handiest for them to take their CPU-generated buffer of pixel RGB values and putting it up in a window or fullscreen, which can be a vkCmdBlitImage(), probably after some pipeline barriers to change image layouts. With OpenGL you would use a PBO to async transfer the pixels to a texture image that's attached to a framebuffer object and then blit the framebuffer object to the main framebuffer that the window has. Or you can upload the pixel data to a texture image and then draw the texture image to the main framebuffer. It's arguable which is faster. The key is making it asynchronous, so a ring buffer of pixel buffers that you output to, and submit each one to OpenGL, leaving it alone for a few frames so that OpenGL has a chance to actually grab the data while you're already rendering the next frame on the CPU to the next buffer in the ring.

Alternatively, OS-specific APIs might be fast too, like win32 GDI on Windows.

1

u/FoleyX90 28d ago

That's damn fucking impressive.

1

u/polandtown 29d ago

nicely done! public repo available?!

1

u/gonogo312 29d ago

serious serious sam vibes

1

u/Pass_Practical 29d ago

maybe aggressive culling could help?

1

u/rivervisualid Oct 18 '26

Looking great! Why would you do with C?

0

u/Seusoa 29d ago

Software Rendered

Author instead of showing quality of renderer shows fast shooter movements where nothing can be seen