Quake II RTX doxygen  1.0 dev
path_tracer.h
Go to the documentation of this file.
1 /*
2 Copyright (C) 2018 Christoph Schied
3 Copyright (C) 2019, NVIDIA CORPORATION. All rights reserved.
4 
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9 
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14 
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19 
20 /*
21 // ========================================================================== //
22  This file contains declarations used by all path tracer shaders, including
23  rgen and all the hit shaders.
24 
25 General notes about the Q2RTX path tracer.
26 
27 The path tracer is separated into 3 stages for performance reasons:
28 
29  1. `primary_rays.rgen` - responsible for shooting primary rays from the
30  camera. It can work with different projections, see `projection.glsl` for
31  more information. The primary rays stage will find the primary visible
32  surface or test the visibility of a gradient sample. For the visible
33  surface, it will compute motion vectors and texture gradients.
34  This stage will also collect transparent objects such as sprites and
35  particles between the camera and the primary surface, and store them in
36  the transparency channel. Surface information is stored as a visibility
37  buffer, which is enough to reconstruct the position and material parameters
38  of the surface in a later shader stage.
39 
40  The primary rays stage can potentially be replaced with a rasterization
41  pass, but that pass would have to process checkerboarding and per-pixel
42  offsets for temporal AA using programmable sample positions. Also, a
43  rasterization pass will not be able to handle custom projections like
44  the cylindrical projection.
45 
46  2. `direct_lighting.rgen` - takes the primary surface produced by the first
47  stage. If that surface is a special material like water, glass, or chrome,
48  it will trace a single reflection or refraction ray, ignoring subsequent
49  intersections with transparent geometry. For the first opaque surface,
50  this stage computes direct lighting from local polygonal and sphere lights
51  and sun light.
52 
53  This stage also writes out some parameters of the first opaque surface,
54  like world position and normal, into G-buffer channels. Even though the
55  next stage can extract this information using the visibility buffer, it is
56  faster to write them to a G-buffer because accessing the vertex buffers
57  and textures and reconstructing materials from them is relatively
58  inefficient.
59 
60  3. `indirect_lighting.rgen` - takes the opaque surface from the G-buffer,
61  as produced by stage (2) or previous iteration of stage (3). From that
62  surface, it traces a single bounce ray - either diffuse or specular,
63  depending on whether it's the first or second bounce (second bounce
64  doesn't do specular) and depending on material roughness and incident
65  ray direction. For the surface hit by the bounce ray, this stage will
66  compute direct lighting in the same way as stage (2) does, including
67  local lights and sun light.
68 
69  Stage (3) can be invoked multiple times, currently that number is limited
70  to 2. First invocation computes the first lighting bounce and replaces
71  the surface parameters in the G-buffer with the surface hit by the bounce
72  ray. Second invocation computes the second lighting bounce.
73 
74  Second bounce does not include local lights because they are very
75  expensive, yet their contribution from the second bounce is barely
76  noticeable, if at all.
77 
78  If the denoiser is disabled, the last invocation of stage (3) will also
79  perform compositing of all lighting channels into final color.
80 
81 
82 Also note that "local lights" in this path tracer includes skybox triangles in
83 some cases. Generally, if there is an area with a relatively small opening
84 that exposes the sky, that opening will be marked as an analytic local light.
85 Quake 2 geometry includes these skyboxes that are often placed right where
86 we would need a portal light. But in many cases, they are also big and complex
87 meshes that enclose large outdoor areas, and we don't want to create analytic
88 light from those. So specific places in the game where portal lights make sense
89 are marked in the "sky_clusters.txt" file in the game directory. Same conversion
90 is applied for lava geometry in many places in the game, and is guided by
91 the same file.
92 
93 Converting skyboxes to local lights provides two benefits:
94 
95  - Reducing noise that comes from small openings. For example, if there is a
96  room with a window, and that window is visible as a 0.3 steradian opening
97  from a wall on the other side (which corresponds to a roughly 36 degree
98  cone), that window covers only 5% of the hemisphere above the surface,
99  so the probability of a diffuse bounce ray of hitting that window is also
100  about 5%. If we place a portal light, and there are no other lights in the
101  room, that probability becomes 100%.
102 
103  - Adding a free bounce of sky light. Without portal lights, the first bounce
104  stage will compute direct lighting from the sky, and the second bounce
105  stage will compute bounce lighting from the sky. With portal lights, the
106  direct lighting stage will compute direct sky lighting, and the first
107  bounce stage will compute bounce lighting. For this reason, you might
108  notice that some areas lose sky lighting when you disable all bounce passes,
109  and some do not.
110 
111 // ========================================================================== //
112 */
113 
114 #extension GL_NV_ray_tracing : require
115 #extension GL_ARB_separate_shader_objects : enable
116 #extension GL_EXT_nonuniform_qualifier : enable
117 
118 #define INSTANCE_DYNAMIC_FLAG (1u << 31)
119 #define INSTANCE_SKY_FLAG (1u << 30)
120 #define PRIM_ID_MASK (~(INSTANCE_DYNAMIC_FLAG | INSTANCE_SKY_FLAG))
121 
122 #define GLOBAL_UBO_DESC_SET_IDX 1
123 #include "global_ubo.h"
124 
125 
126 layout (push_constant) uniform push_constant_block {
127  int gpu_index;
128  int bounce_index;
130 
131 struct RayPayload {
135  uvec2 transparency; // half4x16
137 };
138 
140  int missed;
141 };
142 
143 // vim: shiftwidth=4 noexpandtab tabstop=4 cindent
layout
layout(push_constant) uniform push_constant_block
Definition: path_tracer.h:126
RayPayloadShadow
Definition: path_tracer.h:139
uint
uint32_t uint
Definition: global_ubo.h:233
RayPayload::max_transparent_distance
float max_transparent_distance
Definition: path_tracer.h:136
RayPayload::instance_prim
uint instance_prim
Definition: path_tracer.h:133
RayPayload::barycentric
vec2 barycentric
Definition: path_tracer.h:132
push_constants
push_constants
Definition: path_tracer.h:129
RayPayload::transparency
uvec2 transparency
Definition: path_tracer.h:135
RayPayload::hit_distance
float hit_distance
Definition: path_tracer.h:134
RayPayload
Definition: path_tracer.h:131
RayPayloadShadow::missed
int missed
Definition: path_tracer.h:140
global_ubo.h