r/GraphicsProgramming • u/UnalignedAxis111 • Sep 29 '23
Real time PBR on the CPU (software rasterization)
2
u/APUsilicon Sep 29 '23
Wow I have an epic chip and I as curious about CPU scaling
1
u/UnalignedAxis111 Sep 29 '23
Funnily enough, it kind of struggles to fully saturate my 4-core CPU. At 720p it only loads it to ~60%, 1080p gets it closer to 100% but that's only because of the full screen compositing pass, the rasterizer pipeline is very poorly multi-threaded and idles on a lot of CPU power...
2
u/Zealousideal_Wolf624 Sep 29 '23
This is insane. I have done a CPU renderer but I never managed to get that complex of a scene running. Any suggestions on how to implement SIMD? How does that work?
3
u/UnalignedAxis111 Sep 30 '23
I think the key about SIMD is in how you layout data, it's a place where the struct of arrays approach really shines.
Say, for example, you have an array of structs like
Vec3 { float x, y, z, w; }. You can do a few simple operations like multiplying two of them using SIMD, but it doesn't scale well because you're tied to a single item per vector, and also because for more complex things like dot products, you'd have to shuffle the lanes around and then reduce it to a single scalar - it gets complicated and slow very quickly.Instead, you could have SIMD-sized packets like
Vec3 { SimdVec<float> x, y, z, w; }to store chunks of whatever SIMD length you choose. It's a bit awkward at first but it lets you do almost anything you can with (embarassingly parallel) scalar code, but on multiple items at once. It's basically about the same way GPUs work.You can do that for most of the rendering pipeline - vertex shading, triangle assembly and setup, clipping (at least to compute vertex outcodes)), and of course, pixel shading.
The main difference is probably going to be with control-flow, you need to flatten ifs and other statements into conditional selects, essentially turn code like
if (x == 0) r = x; else r = y;intor = select(x == 0, x, y)or equivalent branchless expressions. Non-sequential memory accesses can generally be replaced with gather/scatters, but these tend to be slowish and not well supported (AVX2+ only).Sometimes you'll also need to extract/insert vector lanes, these have dedicated instructions but you can also just copy an entire vector to temp memory and then load each element. It's only really worth when you have lots of computations before you need to fallback to scalar, like when calculating the triangle edges and bounding box before rasterizing.
You can write your own little wrappers with operator overloading to avoid cluttering the code with
_mm_mul_psand stuff everywhere, or use some library like Google's Highway (in C++), though it's probably a good idea to learn the basics first. You can probably find a tutorial nicer than this messy comment, but I find StackOverflow, Intrinsics Guide and Godbolt to be pretty essential. C#, Rust, and a few other langs have decent support for SIMD as well.1
u/Zealousideal_Wolf624 Oct 01 '23
In my rasterizer I load a mesh as a (simplified):
vec3_t* vertices; triangle_t* triangles;Where the triangles are each composed of 3 vertex indexes (v1, v2 and v3) and each vertex is a
Vec3 { x, y, z; }. In other words my program loads the vertices in a way that multiple triangles can share the same vertex data.So I can have something like:
[Vec3 {x1, y1, z1}, Vec3 {x2, y2, z2}, Vec3 {x3, y3, z3}, ...]And my triangle can be something like {1, 3, 2}, meaning that my triangle is composed of vetex 1, 3 and 2.That being said: do you load the vertices as
Vec3 { SimdVec<float> x, y, z; }from the get go? How do you deal with the fact that the same triangle can be spread in the same SIMD packets?For example
[Vec3 { SIMD[x1, x2, x3, ..., xN], SIMD[y1, y2, y3, ..., yN], SIMD[z1, z2, z3, ..., zN] }, Vec3 {...}, Vec3 {...}, ...]In this case the triagle composed of v1, v2, v3 would be on the same packet (the first one) but on different positions. How would my triangle index look like? Do you even have a triangle index array?
1
u/UnalignedAxis111 Oct 01 '23
I have not come up with a good solution for this, my rasterizer also takes a standard interleaved vertex and index buffer as input, and it just reads and shades vertices multiple times for each triangle (in SoA layout) based on the index buffer data. It's quite wasteful since vertices are typically shared a lot of times.
The indices are read on the go into 3 integer vectors of length W, and de-interleaved using a 3xW transpose (this is done using SIMD permutes but it's really not much faster than reading everything using a plain scalar loop), such that the indices for each triangle vertex are moved to a different vector in a parallel layout, like:
input idx: [ 0 1 2 3 4 5 ... ] out v0: [ 0 3 ... ] out v1: [ 1 4 ... ] out v2: [ 2 5 ... ]The shader will then use these indices to read the position and attributes from the vertex buffer (using gather instructions), before doing the matrix projection and copying the result to the output shaded triangle vertex.
I'm thinking about moving to a different approach that supports vertex caching, which will probably involve some kind of LRU or a fixed-size hash table to crudely de-duplicate the input indices, so that the assembled triangles can just refer/point to a shared batch of shaded vertices.
1
u/GasimGasimzada Sep 30 '23
What is
select(x==0,...)? Is this a SIMD specific funcfion?1
u/UnalignedAxis111 Sep 30 '23 edited Sep 30 '23
It's basically a ternary conditional, but in SIMD form (it picks either
aorbdepending oncfor each lane):select(c, a, b) = c ? a : b.The older SSE/AVX comparison instructions output masks with all-bits-one or zeroes, so you'd implement it as
select(c, a, b) = (a & c) | (b & ~c)or using theblendvinstructions.AVX512 has dedicated support for this and the comparison instructions output packed bitmasks (in special registers, but in code they're just
uint16_tor alike depending on the number of lanes), and it also lets you mask the results of almost any instruction. So, for floats, you can doselect(c, a, b) = _mm_mask_mov_ps(b, c, a).
29
u/positivcheg Sep 29 '23
Wow, I wouldn't believe it's a CPU rasterization at all. 25 fps for such scene looks amazing.
9
5
5
u/DerShokus Sep 29 '23
It’s cool. I learn and implement a basic cpu raytracer and have 30 fps with full cpu load and only 3 spheres on the screen :)
1
Sep 29 '23
Is that not three.js?
1
u/UnalignedAxis111 Sep 30 '23
Nope, all of the rendering code was written from scratch in C++, no GPU use for other than for rendering the final frame and ImGui :)
1
3
2
u/swhizzle Sep 29 '23
Well. I was pleased with rendering a Playstation 1 Spyro scene at 20 FPS with my software renderer. Guess there's quite a lot of optimisation for me to do!
EDIT: Maybe I should try multithreading :D.
2
1
46
u/UnalignedAxis111 Sep 29 '23
This is a quick demo of my toy software rasterizer, running at 1080p on a i5-11320 laptop CPU. It is based on the standard half-edge rasterizer, but parallelized with AVX512 SIMD for all of shading and for most of the pipeline, so it churn through 4x4 pixels or 16 vertices at once.
The Sponza scene was rendered with SSAO at 1/2 resolution (with plain nearest upscaling), a 1024x1024 shadow map with poisson disk sampling (with early out at 4 samples, so there's some blockyness around edges), and some basic TAA. The PBR renderer is deferred and uses a basic microfacet BRDF and cubemap IBL (which was mostly copied from the Filament engine).
I'm a bit embarassed about the graphics because all other posts here put it to shame, lol. I just didn't want to spend too much time on a throwaway renderer, and I'm happy enough with the results nonetheless.
The code is quite messy, but if anyone wants to take a look: https://github.com/dubiousconst282/GLimpSW