Adding ray tracing (back) to my game engine [Voxel Devlog #17]

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
two years ago I began this YouTube channel posting videos of my progress coding a voxal ray marcher the project has evolved significantly since that point I've achieved amazing things from realistic looking voxal physics to jaw-dropping render distances but somewhere along in the process the original Ray marching part got dropped I switched to using rasterization which is the more standard conventional approach that most games take recently however I've been looking to up the visual quality of my engine I wasn't strong enough as a programmer 2 years ago but since then I've studied algorithms and data structures and learned what matters when it comes to writing high performance code so after a lot of deliberation I decided to completely scrap my engine's graphics and write a brand new pure voxal Ray marcher [Music] the choice to switch to Ray marching was mainly driven by one thing how my voxal are colored my past renderer used a very limited material system the way I assigned a color to each voxel in my old renderer was sort of like the way Minecraft blocks work right like in Minecraft you have a set of blocks there's a dirt block there's a stone block there's a grass block and each of those blocks have their own texture to them but you can't dictate the individual ual block colors or anything you just have to choose from the palette that is provided to you and my voxels worked in the same way I had some voxal which were assigned the dirt material other voxal which were assigned the grass material and then I applied a very simple 16x16 texture on top of each of those voxal and the problem with that is everything looks the same when you zoom out or look far away like if you look at these mountains in the distance they're all just one color they're Gray and there's no detail to them another huge drawback of this approach was that it made importing models very difficult because there are a lot of stock voxal models you can find online which make for nice test scenes but if I wanted to import a voxal map into my engine I would have to assign each of the colors in that voxal map to a material and I didn't really have a full material palette filled out yet I only had like a couple of materials to find so it meant that when I imported many models into my engine they really didn't look very good and so it was very hard to come up with nice example scenes to show off it comes down to this having unique peral colors and peral normals is Paramount to having a realistic looking scene with lighting normals are something I haven't really explored before but it turns out that they're incredibly important to making good-looking scenes when someone refers to a surface normal in computer graphics they're basically talking about the direction that a point on a Surface faces and this is important because it dictates how bright that surface appears so if I take like a piece of paper and I hold it up so that it's directly facing the light the paper is very bright right but when I turn it to the side so that it's perpendicular to the light the paper gets dark because not as much light falls on that paper per the amount of surface area that there is you can encode this normal data into every voxal these per voxal normals are I think the secret sauce that help engines like Gabe runlets and John Lind look so good they make it easier to discern the shape of surfaces and allow things to look really smooth and not blocky now it's much easier to support per voxal colors and normals using Ray marching than it is with rasterization in Ray marching the color of each pixel on one screen is determined by this little Ray that you shoot through the world you start at wherever the camera is in the scene and you draw a little line out from the camera whatever that line hits um you just take the color of that object and that becomes the color of your pixel and this is great because you can use the voxal data directly while you're doing your ray marching whereas with rasterization you have to turn the entire scene that you want to draw into triangles if you have unique Vox that means that you have to potentially generate multiple triangles for every single Vox soulle which can get really performance intensive and inefficient so with this in mind I removed the existing Graphics backend and all of the voxal data structures from my engine I removed over 11,000 lines of code and began reimplementing everything with Ray marching [Music] the new Ray marcher has a number of improvements over my initial attempts from 2 years ago this includes things like support for an arbitrary number of non-gd aligned objects and levels of detail but the most profound Improvement and the thing that I'm happiest with is the reduced memory usage one thing I've learned over the past 2 years of working with gpus is that GPU memory is really slow I mean like really slow so you want to minimize the amount of memory reads that you do as much as possible my original Ray marcher used a voxel octree and so every time the Ray took a step moved forward in the scene to check if it had hit an object yet that meant doing eight or more memory reads to Traverse down the octree I tried to cach some of the data but that meant that I was using up a lot of the gpus registers or basically like local memory and that wasn't too good either so I redid the data structure that I use on the GPU in order to do ray marching and vastly reduced the number of memory reads that I need to perform I'm still using a tree like structure but instead of an OCT tree I now use a tree which splits each parent Cube into 64 smaller cubes so each axis is subdivided by four I'm not sure if there's an official name for this kind of a tree I guess you could call it a Tetra hexacon tree THC tree but um for now I'm just going to call it a brick tree the core of the ray marcher works the same way as before the scene is organized into brick trees and when it comes time to cast a ray out into the scene to determine what color a pixel should be the ray moves through the scene and moves through the higher levels of the tree first so if a region of space has no voxal in it that level of the tree is not subdivided and the ray can move through that entire cube without needing to check any voxal inside so it allows for a form of of empty space skipping but there's a secret something that's allowed me to really leverage the architecture of modern processors and that would be bit masks this is a 64-bit number where each bit represents whether the child node in this tree is subdivided or not and so what happens when the rays are moving through the scene is they'll get to a tree they'll read the 64-bit bit mask and then they'll start stepping through each child child node and when they get to a child node they don't do any more memory reads to check if the child is empty or not they use this bit mask which is just one or maybe two registers and they use bitwise operations to select the bit that corresponds to the child node which they are currently in and they check to see if that bit is set or not and if it's not set the ray moves on without performing any memory reads at all this means that my rays can take up to 10 Ray steps without needing to read from memory again which is a huge performance win as a showcase here's a castle scene from a tear down map which is rendering at 7 milliseconds per frame with a primary and a shadow Ray on my GTX 1660ti notice how smooth the Rocks appear now by the way this is the power of per oxal normals I haven't optimized the ray marching code or the ray marching loop at all and I'm not doing any other fancy optimizations like a depth prepass so this performance is all just coming from the empty space skipping and this bit mask acceleration structure so I think I should be able to improve the frame times even more because I have a hunch right now that I am compute bound the end result is that I can now load any voxal model that I want and it looks great from tear down Maps like this one to three 3D scans of real world places like this church if you want to try out the ray marcher for yourself you can use it in browser and um check out the church model I also will put a link in the description to a Google drive folder where I will post all of the other models shown in this video you can download them and then uh import them into the engine and try them out for those who are worried that I'm giving up my goal of supporting integrated G gpus don't be another really neat thing about Ray marching that I probably should have thought about earlier when I initially switched to rasterization is the fact that there's a really easy way to improve the performance of a ray marcher and that is simply to render at a lower resolution like this and so this is a scene running in real time on my integrated graphics card and this is all done using rate tracing yes it's low quality but I think that's a fair tradeoff right the graphics won't be held back um while I try to support low-end gpus but hopefully the game will still be playable just at a lower resolution and honestly I mean I think the lower resolution looks kind of cute everything's made of Cubes anyway so it it has a certain charm to it but maybe I'm just making excuses um anyway the next steps that I want to pursue are I need to add back um destruction and placement of objects and then of course there's even more optimization to be had thank you very much for watching please leave a comment if you had any questions and also let me know what you thought of the new video style I'm trying to take a more unstructured approach to things because in the past creating videos has become like a really big daunting task and I don't want my YouTube channel to be about the videos I want it to be about the voxel because that's what we're all here for at the end end of the day so if you enjoyed the video please leave a like subscribe to see where I take the project in the future and have a lovely [Music] [Music] day
Info
Channel: Douglas
Views: 79,897
Rating: undefined out of 5
Keywords: voxel, game, engine, coding, programming, douglas, doug, douglasvoxel, minecraft, teardown, magicavoxel, 3d graphics, graphics, gpu, cpu, rendering, raytracing, ray tracing, rasterization, computer, computer graphics, ray marching, marching, ray
Id: aY4Zet_C9Zs
Channel Id: undefined
Length: 12min 29sec (749 seconds)
Published: Fri Mar 29 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.