r/GraphicsProgramming • • 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?

0 Upvotes

11 comments sorted by

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.

2

u/protec_loli_not_lewd May 01 '24

Can you explain in a bit more detail? So the normal rendering is still using a rasterizer. And in the fragment shader they run raytracer code if enabled?

Are there any resources to read on how to accomplish this. I am not sure how will I get the entire scene information in the shader for ray tracer

1

u/waramped May 01 '24

So the normal rendering is still using a rasterizer. And in the fragment shader they run raytracer code if enabled?

That's one way to do it, yes. Say you wanted ray-traced shadows instead of shadow maps. You'd have a set of shaders that would use the RT instructions instead of your typical shadow sampling code. Or you wanted to add ray-traced reflections, you could just have your SSR pass shoot rays if the screen-space trace failed. If you wanted to do something like Global Illumination, then that would be more complicated than just integrating it into an existing lighting solution.

Are there any resources to read on how to accomplish this.

Loads, just google DX12 Raytracing or Vulkan Raytracing. (Those are the APIs you will need to use to have access to it)

I am not sure how will I get the entire scene information in the shader for ray tracer

Yea, it's complicated. You need to create a BVH (Bounding Volume Hierarchy) of your scene. The API will do this for you, but it's up to you to manage what's in the BVH. That's what allows the hardware to shoot rays against your geometry. But that's only geometry. Once you have a hit against a triangle, how you resolve the color/material data for that location is up to you.

1

u/Economy_Bedroom3902 May 01 '24

You can impliment raytracing on the GPU with no rasterization, since each camera ray can be traced individually. Most actual rendering systems do still use the rasterization because that gives them a backwards compatibility fallback (where the RT can just be turned off), and there is a small performance benefit to rasterizing the first step in a raytracing pipeline (there is a downside though, the raster pipeline requires quite a bit of memory storage space that could be saved if you did not rasterize at all).

It is possible to implement a raytracing system entirely in the fragment shader, but it's extremely ugly and it's not possible to make that system fast. Normally raytracing uses a separate shader pipeline. https://developer.nvidia.com/blog/ray-tracing-essentials-part-4-the-ray-tracing-pipeline/

OpenGL does not have a raytracing pipeline built into it. People who raytrace with OpenGL generally use OpenCL to build the steps in the raytracing pipeline and load the raytraced data into a buffer, and then pull that data out of the buffer during the fragment shader pass on the OpenGL side. Other rendering pipelines like DirectX, Vulkan, and Optix have the raytracing pipeline built into them directly.

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.

https://developer.nvidia.com/rtx/ray-tracing

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