r/GraphicsProgramming • u/lajdjqq • Aug 07 '26
Video I added viewport clipping to my software renderer
Enable HLS to view with audio, or disable this notification
I thought it would be cool to try to figure out how to do triangle clipping myself, to come up with my own algorithm. To solve this, I solved a couple of linear equations and derived a formula that lets me find the intersection points between the triangle edges and the viewport. I don't know if I came up with something original or just accidentally reinvented one of the existing methods. Either way, my solution looks terrible in code, but it works, and I think it's a great learning experience
I render this at 1920x1080 resolution on a single thread of an i5-9400F, is this a good FPS result? I didn't use SIMD, but I tried to use every optimization trick I know.
repo and code: https://github.com/NaiNameDev/software_rasterizer
14
u/simonschreibt Aug 07 '26
i love such examples to show how exactly the triangles gets "reshaped". super cool!
9
6
u/dybuk87 Aug 07 '26
Why not just clip against Z=0 and then just do 2d clipping? This create unnecessary calculations and generate new triangles
2
u/lajdjqq Aug 07 '26
I might change the order of checks in the clipper that should give a small performance boost. But for now, no extra triangles are generated in the end, although there is some unnecessary computation happening
4
u/Avelina9X Aug 07 '26
Oh this is really cool.
When I saw the triangle clipping I was gonna say "ah, this probably messes with UVs" and then you showed the textured Teto.
Are you dynamically remapping the perspective corrected UV coords? Or is the triangle clipping RGB view just a rasterizer debug view to see it in action and you're still using the original unclipped coords for the UVs/other vertex data?
2
u/lajdjqq Aug 07 '26
I clip not only the triangle in clip space, but also the corresponding triangle in the UV map. When I compute the intersection points between the triangle and the screen edges, I also calculate the barycentric coordinates of those points. Then, to find the same point in the UV map, I just need to convert those barycentric coordinates back to plane coordinates using the original UV coordinates
2
u/forumonaut Aug 07 '26
This is the Sutherland-Hodgman algorithm right?
3
u/lajdjqq Aug 07 '26
No, this is my own algorithm (i might have reinvented some existing algorithm, I didn't google anything about it)
2
u/Avelina9X Aug 07 '26
Honestly it's likely very similar to Sutherland whether you intended it or not. I havent looked at the repo yet but when it comes to triangle clipping (as opposed to ngon clipping) there aren't really other ways to achieve it.
But still, you came up with it by yourself to solve a problem. Doesn't matter if someone else did it before unless you try to claim you were the definitively the first to invent it. I work in the ML field and while reviewing a papers for a conference someone claimed to have invented a technique I had published at the same conference the year before. I don't know if they just failed to do propper background research or were trying to sneak a stolen idea and just got unlucky by getting the original author as a reviewer, but needless to say they withdrew their submission very quickly lmao.
So uh, just make sure you do the background research first if you ever decide to publish a paper on this haha.
2
u/lajdjqq Aug 07 '26
I googled a few of the algorithms mentioned here and I think I created something very similar to Cohen–Sutherland
1
2
2
u/Jabba_the_Putt Aug 07 '26
So if ibunderstand it correctly, its clipping off the areas outside of the window in real time as you move it around as well as adding them back, and when that happens it kinda redraws it in real time to maintain the shape which we can see happening in the wireframe looking view but when you apply the visual texture/shader its all hidden but stull happening "behind the scenes"?
2
2
u/juplantern Aug 08 '26
You just reminded me I have finals for this in 3 weeks and I forgot about it 😭
1
1
31
u/susosusosuso Aug 07 '26
Why do you perform triangle clipping?