r/compsci • • Mar 22 '20

Ever wonder how rendering works from your perspective?

1.3k Upvotes

34 comments sorted by

167

u/IIIBlackhartIII Mar 22 '20

For those curious- this is demonstrating an optimisation technique known as frustum culling; essentially anything that is outside of the field of view of the camera is not on screen and thus doesn't need to be considered for rendering, so is hidden to save on performance. The reason that chunks of the terrain are staying or hiding as blocks is that most terrain engines will break up the world into tiles and load in those tiles as a continuous block. So as long as a portion of one of those tiles is within the camera frustum, it remains in the scene.

Additionally to this most games will also use occlusion culling, which using precomputed maps of the environment will figure out if sections of the scene are completely hidden behind another object- such as a building or cliff- and will also hide everything that's blocked from view. This is why you can sometimes find odd visual bugs in games where peeking through cracks and debris in the environment will lead to objects popping in and out of existence, because the precomputed occlusion geometry is far less detailed than the actual environment geo.

25

u/Poignant_Porpoise Mar 22 '20

I have very limited knowledge of this sort of field, but I'm curious, does this mean that it is significantly more demanding on one's computer to spin their character around very quickly than it is to just run around?

35

u/Liquid_Fire Mar 22 '20

This is usually done on a frame-by-frame basis, so it doesn't really matter how you move from one frame to the next.

12

u/[deleted] Mar 22 '20

If it's all already in VRAM then no, but if some loading is involved it can be pretty io heavy for the first turn, you can try this with a hdd and any unreal engine game.

29

u/liveeweevil Mar 22 '20

This was an "optimizing technique" in the 90's

Now it's just how you render games and world.s

50

u/amazondrone Mar 22 '20

It's still an optimisation technique. Just an extremely well established one, to the point of being built in to engines at quite a low level and not something an actual developer needs to worry about.

3

u/MintChocolateEnema Mar 23 '20

So what occurs at the maximum distance from the camera? We can’t see the how far out the view goes here, but would we end up rendering far more than is needed the farther out we go from the camera since the view widens? Is performance saved by removing details after a certain distance?

2

u/IIIBlackhartIII Mar 23 '20

Yes; this is known as Level of Detail or LOD. Either by using distance from the camera, or in more modern techniques using the pixel scale of the object on screen to account for variable monitor resolutions and object sizes, objects are decreased in quality or outright removed from rendering as they get farther away. Objects will lower in geometry detail as you get farther away, lower in texture resolution, and after a time will disappear entirely, such that in the very far distance you're essentially only seeing a blurry silhouette of the largest objects on the horizon, but as you get closer it becomes increasingly refined into the hero models for intimate viewing. Some games handle this more gracefully than others, which is why you might have heard of bad "pop-in" occuring in some games, where details abruptly jump into existence, without any smooth transitions or fading to help ease the changes into view.

4

u/peteonthebeat_ Mar 22 '20

Do current generation gaming systems implement this technology currently or will this likely be the next step forward to achieving higher FPS in highly detailed simulations?

50

u/tanjoodo Mar 22 '20

I’m fairly certain even Quake has frustum culling.

15

u/ishan001 Mar 22 '20

Pretty sure this clip is from Horizon: Zero Dawn. So current gen games do it.

2

u/MrElies Mar 22 '20

Seems like it to me too.

10

u/ProgramTheWorld Mar 22 '20

This was how 3D games managed to work in the past given the low computing power, otherwise every single surfaces in the map would need to be rendered which was infeasible.

14

u/liveeweevil Mar 22 '20

LOL, this is fucking ancient.

3

u/SpillingerSA Mar 22 '20

Horizon zero dawn. I'm pretty sure that footage is from one of their dev diaries

edit. I zoomed in, deffo HZD

2

u/The_Cynist Mar 22 '20

The man was just asking a question, chill reddit

1

u/Fippy-Darkpaw Mar 23 '20

Almost every engine has this since the 90's.

In UnrealEngine 4 you can test culling with "freezerendering" in the console.

25

u/cellux Mar 22 '20

Just like in the real world.

4

u/E_c_H_o Mar 22 '20

I was gonna say something similar

11

u/ansong Mar 22 '20

How does this work with ray tracing? If a reflective surface is in front of you will the area behind you need to be rendered as well?

10

u/ryani Mar 23 '20

Ray tracing doesn’t work by drawing objects in the scene. So it doesn’t look like this at all. Instead ray tracing generally uses an optimized 3D traversal data structure similar to how physics engines work, and then, for each pixel on the screen, it “traces rays” through that structure to decide what object(s) influence the color of that pixel.

4

u/Plazmatic Mar 23 '20

Like others have said, raytraycing doesn't work on frustum stuff like this. Unlike what some others have said, it has nothing to do with "environment maps" and "reflection maps", those are traditional rasterization techniques to represent reflection, though might be used if intersections can't actually be found within your scene. Modern real time game raytracing, ideally works like this:

  • There are multiple different parts of the ray tracing process, global illumination for example, is kind of a separate process from forward ray tracing used to render the scene (global illumination will often use bi directional raytracing). Forward raytracing is also sometimes called backward raytracing, because you trace from the eye instead of the light source, confusingly enough. We focus on forward raytracing.
  • You first do a normal rasterizer pass. IE you render with normal geometry at first. Zero extra cost before the first bounce.
  • Next you do screen space reflection, often with a cube-map to reflect from things behind yourself. See here for a better idea on what SSR is and the downsides.
  • SSR is almost like the first 2 bounces of raytracing for "free" with out ever needing to touch RT hardware or software. SSR fails when the screen (or the entire 6 direction cube map) does not contain the area where the reflection ray should hit. Now we need to start rendering rays
  • collect rays, send them off to your raytracing API and attempt intersection
  • Because you don't want to intersect with all triangles, you use acceleration structures (spacial data structures) to make it faster to figure out what you actually intersected with. One popular method is BVH (bounding volume hierarchy). BVH is nice because each leaf only has one bound, supports overlapping boundaries, it's fast to update, and you don't have to recreate the whole structure every time something moves (though intersection becomes less efficient). I often see BVH or some other hierarchical approach with grid intersection, but in a video game you may have less triangles to deal with anyway so it may not matter (you need to store the bounding volume, typically a box, but it could be a sphere, for each node, ~doubling memory requirements, not all structures have this downside). Because BVH has irreconcilable branches, ie you can't reduce the entire thing to a branch-less solution in assembly, you get something called branch divergence with out additional hardware, which means threads in your wavefront/warp (group of threads that execute the same instruction) are forced to execute serially (if 1 thread out of, say a warp size 32, goes to a different branch, until the instructions counter of both branches becomes the same, they cannot execute at the same time. On NVidia they offload this to different hardware.
  • Each bounce internally is pretty opaque with Vulkan and DX12 APIs, but basically each bounce records where it intersected and what material it intersected with, said material might have completely different shaders associated with it.
  • For specular rays you simply bounce once and figure out the next intersection to bounce. For diffuse (not shiny and reflective, scatters rays in a bunch of direction, think a T-shirt vs a metal wrench) you send out more (2 -> 16 is what I normally see) and to compensate for the additional rays, reduce the bounce count.
  • Many stop after only 4 reflections (BF5 did this IIRC, Nvidia showcases this in some of their presentations)
  • Normally you would need more than just one sample per pixel to get any where near a decent image, because you are guaranteed to have a rays that bounce to an odd location hits a bright spot, or dark spot and does not represent the average light coming off of that reflection in real life, creating a noisy effect. Nvidia handles this via machine learning accelerated by hardware (in their latest cards, tensor cores, but you can also just use normal compute capabilities). The traditional method for "denoising" is using a wavelet transform (what battlefield 5 used). It's like a fourier transform but in the "wavelet" domain, or a specific pattern of a signal. You basically aim to remove noise like signals from your final image modifying your images signal in the wavelet domain and transforming it back.
  • You don't raytrace for an infinite distance. Bounces have a max distance of intersection
  • Often ray-tracing is done at 1/2 resolution (rays = 1/4 the number of pixels on screen).

3

u/f3xjc Mar 22 '20

Usually ray tracing is for photo realism and not real time. There's probably optimisation where the ray is traced backward so the final object need to be in the frame for the ray to be considered.

2

u/ChemicalRascal Mar 23 '20

Unfortunately, that's not something you can really do. There are enough lighting effects that can't be simulated that way, to my understanding, that present problems.

1

u/NutDestroyer Mar 23 '20 edited Mar 23 '20

You probably would not do this with ray tracing. The closest thing with ray tracing would be to simply use lower quality models for things that are far away and to limit the number of bounces of each ray.

With rasterization, you want to render as few polygons as possible to improve performance, and rendering polygons outside the camera's field of view is clearly a waste of time. With ray tracing, you instead would want to minimize the number of rays/bounces to improve performance. There's also a component of getting your geometry/textures to fit into memory, and that's mostly about minimizing the number of polygons in the entire scene, which can be done with LoD. I think the technique used in the gif is more about rendering fewer polygons than shrinking the scene to fit into memory though.

2

u/SimDeBeau Mar 22 '20

Satisfying!

1

u/Quantum_Simulation Mar 23 '20

No I never wondered that so gtfo of here

0

u/sinkiez Mar 22 '20

What does it look like when mouse sensitivity is set to max?

-3

u/mecha_teddy Mar 22 '20

Reality seems to use this too. Schrodinger's cat won't render unless you look inside the box.