r/GraphicsProgramming • u/protec_loli_not_lewd • May 01 '24
Question How to have ray tracing as an optional button
I recently started to learn ray tracing, and from my understanding, the entire pipeline for ray tracing is very different And if i have to use ray tracing in an interactive scene, I need to generate a texture and then display the texture using openGL 2 triangles.
However, the rasterizer pipeline is entirely using many shaders. So do games just write 2 complete pipelines and switch between them based on if ray tracing is enabled?
5
u/fgennari May 01 '24
Did you start learning ray tracing on the CPU or GPU? I'm guessing CPU, since it's easier. And sadly OpenGL doesn't support hardware/GPU ray tracing in the API, though you can implement it yourself with compute shaders.
Most of the games that I'm aware of that support ray tracing don't use it for the entire game, but only for specific features such as reflections, shadows, global illumination, etc. These steps are generally done as postprocessing with a very different pipeline. When ray tracing is disabled (or not supported), the games will either revert to an alternative such as a compute shader, or disable the effect entirely. This generally produces a lower quality resulting image. For example, they may switch to screen space postprocessing effects, which is approximate and limited to what's visible on the screen.
1
u/Ok-Sherbert-6569 May 01 '24
You can certainly combine the two pipeline. For instead you can trace secondary rays from the G buffer that’s created but yes that would require running that in a compute shader ( in case of software lumen and some other game ) or most likely using the raytracing API that exist in the API used.
1
u/moschles May 01 '24
WHat you describe is entirely possible. Ray tracing can be used natively in a completely different pipeline from rasterizers. That is done by shooting rays from the camera's position.
do games just write 2 complete pipelines and switch between them
Absolutely not. Numerous games use strange techniques, which are hybrid rasterizer/ray tracing combinations. All of them are in-house solutions. For example, the game Cyberpunk2077 has (at least) two different kinds of hybrid ray tracing, selectable in the options. The differences between the various hybrid renderers mostly has something to do with shadows.
1
u/Solrax May 01 '24
I can't answer your question but I know Nvidia has a lot of information on using ray tracing with their RTX cards on their website. You will possibly find your answers by digging deeper there. I've been meaning to dig into this myself.
1
u/wm_lex_dev May 01 '24
Look at Unreal's Materials. The shader for a 3D surface is written in a fairly abstract way, where they don't have to know exactly what the output format looks like or what sort of shader they're running within.
Regular rendering will invoke the materials with a Forward, Deferred, etc. output.
Ray-tracing will invoke the materials to apply the triangle's surface color to the ray that hits it, and to generate some number of bounce rays.
Additionally, as others said, game ray-tracing is done on the GPU (usually with the help of hardware like RTX) and often only for specific things like shadows or reflections. OpenGL doesn't support ray-tracing cards, so you'd have to do it all yourself in compute shaders or switch to DX12/Vulkan.
2
u/Economy_Bedroom3902 May 01 '24
OpenGL supports the cards, it just can't use the raytracing features provided by those cards. Most of those cards are still extremely powerful cards for the traditional rasterized rendering pipeline as well.
1
u/wm_lex_dev May 01 '24
Sorry I meant to say that it doesn't support driver-level RT and certainly not hardware RT
4
u/waramped May 01 '24
Games that have a raytracing option don't do a CPU raytracer and upload it as a texture.
Games these days that support raytracing use the raytracing acceleration in modern GPUs. Simplified: The raytracing instructions are called from within the existing shaders and are just used to enhance lighting or effects that are otherwise very difficult to do with rasterization alone.