This repository was archived by the owner on Jan 15, 2024. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathpathtracer.rgen
More file actions
259 lines (218 loc) · 8.9 KB
/
Copy pathpathtracer.rgen
File metadata and controls
259 lines (218 loc) · 8.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
#version 460
#extension GL_NV_ray_tracing : require
#extension GL_GOOGLE_include_directive : require
#define USE_PCG
//#define USE_BLUE_RAND
#include "rng/pcg.glsl"
#include "rng/bluesobol.glsl"
#include "common/bsdf.glsl"
#include "common/math.glsl"
#include "common/onb.glsl"
#include "rtcommon/traceray.glsl"
#include "rtcommon/raypayload.glsl"
#include "rtcommon/trimeshlayout.glsl"
#include "rtcommon/emitterlayout.glsl"
layout(location = RAY_PRD_LOCATION) rayPayloadNV CommonRayPayload prd;
layout(location = SHADOW_RAY_PRD_LOCATION) rayPayloadNV CommonShadowRayPayload shadow_prd;
layout(set = 0, binding = 0) uniform accelerationStructureNV tlas;
layout(set = 0, binding = 1, rgba32f) uniform image2D image;
layout(set = 0, binding = 2) uniform CameraProperties
{
mat4 m_view;
mat4 m_proj;
mat4 m_inv_view;
mat4 m_inv_proj;
int m_is_moved;
int m_frame_count;
} cam;
layout(set = 0, binding = 3) uniform sampler2D envmap;
// set 1: triangle attributes
DECLARE_TRIMESH_LAYOUT(1)
// set 2: emitter sampling information
DECLARE_EMITTER_LAYOUT(2, envmap)
// set 3: bluenoise sampler
DECLARE_BLUENOISE_LAYOUT(3)
#define FAST_PT_PRIMITIVE 0
#define FAST_PT_NEE_ONLY 1
#define FAST_PT_MIS 2
#define STAGE_NEE 0
#define STAGE_BRDF_SAMPLING 1
float
compute_mi_weight(const float brdf_pdf,
const float emitter_pdf,
const int mode,
const int stage)
{
if (mode == FAST_PT_PRIMITIVE)
{
return stage == STAGE_NEE ? 0.0f : 1.0f;
}
else if (mode == FAST_PT_NEE_ONLY)
{
return stage == STAGE_NEE ? 1.0f : 0.0f;
}
else // mode == MIS
{
if (stage == STAGE_NEE)
{
return emitter_pdf / (emitter_pdf + brdf_pdf);
}
else
{
return brdf_pdf / (emitter_pdf + brdf_pdf);
}
}
}
Ray
generate_primary_ray()
{
const vec2 pixel_center = vec2(gl_LaunchIDNV.xy) + vec2(0.5f);
const vec2 ndc = pixel_center / vec2(gl_LaunchSizeNV.xy);
const vec2 d = ndc * 2 - 1;
const vec3 origin = (cam.m_inv_view * vec4(0, 0, 0, 1)).xyz;
const vec3 lookat = (cam.m_inv_proj * vec4(d.x, d.y, 1, 1)).xyz;
const vec3 direction = (cam.m_inv_view * vec4(normalize(lookat), 0)).xyz;
Ray result = Ray_create(origin, normalize(direction));
return result;
}
void
main()
{
// if camera is not moved reuse accumulated radiance
vec3 accumulated_radiance = vec3(0.0f);
float num_spp = 0.0f;
if (cam.m_is_moved == 0)
{
vec4 accumulated_radiance_and_num_spp = imageLoad(image, ivec2(gl_LaunchIDNV.xy));
accumulated_radiance = accumulated_radiance_and_num_spp.xyz;
num_spp = accumulated_radiance_and_num_spp.w;
}
int num_path_vertices = 3;
// mode
int mode = FAST_PT_MIS;
//int mode = FAST_PT_PRIMITIVE;
// init rng
RngState rng_state = srand(uvec2(gl_LaunchIDNV),
uvec2(gl_LaunchSizeNV),
uint(cam.m_frame_count));
// result
vec3 direct_light_estimate = vec3(0.0f);
vec3 indirect_light_estimate = vec3(0.0f);
vec3 importance = vec3(1.0f);
// attributes from prev vertices
Ray ray;
vec3 position;
vec3 gnormal;
vec3 snormal;
vec2 texcoord;
float triangle_area;
Material material;
Onb onb;
for (int i_bounce = 0; i_bounce < num_path_vertices; i_bounce++)
{
const vec3 local_incoming = Onb_to_local(onb, -ray.m_direction);
// do next event estimation
if (i_bounce != 0 && (mode == FAST_PT_MIS || mode == FAST_PT_NEE_ONLY)) // we do not do nee with camera vertex
{
// sample emitter
const vec2 samples = rand2(rng_state);
float emitter_pdf;
float emitter_jacobian;
int emitter_type;
Ray shadow_ray;
const vec3 emitter_contrib = sample_emitter(emitter_pdf, shadow_ray, emitter_type, emitter_jacobian, position, snormal, samples);
const vec3 to_emitter = shadow_ray.m_direction;
// do visibility test
shadow_prd.m_visibility = 0.0f;
trace_shadow_ray(shadow_ray, tlas, 1);
const float visibility = shadow_prd.m_visibility;
// compute brdf term
float brdf_pdf;
const vec3 local_to_emitter = Onb_to_local(onb, to_emitter);
const vec3 brdf_term = Material_eval(brdf_pdf, material, local_incoming, local_to_emitter);
const float cosine_term = abs(local_to_emitter.y);
// update pdf
brdf_pdf = emitter_jacobian * brdf_pdf;
// estimate nee
vec3 nee_estimate = importance * brdf_term * cosine_term * emitter_contrib * visibility;
// compute weight
const float weight = compute_mi_weight(brdf_pdf, emitter_pdf, mode, STAGE_NEE);
// accumulate weighted estimate
const vec3 weighted_estimate = importance * nee_estimate * weight;
const bool weighted_estimate_nan = any(isnan(weighted_estimate));
if (i_bounce == 1) direct_light_estimate += weighted_estimate_nan ? vec3(0.0f) : weighted_estimate;
if (i_bounce > 1) indirect_light_estimate += weighted_estimate_nan ? vec3(0.0f) : weighted_estimate;
}
// generate ray to next bounce
float brdf_sampling_pdf_w;
if (i_bounce == 0)
{
// from camera generate primary ray
ray = generate_primary_ray();
}
else
{
// sample next direction
const vec2 samples = rand2(rng_state);
vec3 local_outgoing;
importance *= Material_cos_sample(local_outgoing, brdf_sampling_pdf_w, material, local_incoming, samples);
// compute next position and direction
const vec3 next_position = ray.m_origin + ray.m_direction * prd.m_t;
const vec3 next_direction = Onb_to_world(onb, local_outgoing);
// discard sample if sampled direction goes under the surface
if (dot(next_direction, gnormal) * local_outgoing.y <= 0.0f) break;
ray = Ray_create(next_position, next_direction);
}
// trace ray and extract hit information
trace_ray(ray, tlas, 0);
const bool is_intersect_surface = prd.m_t > 0.0f;
if (is_intersect_surface) // intersect surface
{
// extract hit information
extract_hit_info(position, snormal, gnormal, texcoord, triangle_area,
prd.m_instance_id, prd.m_primitive_id, prd.m_barycoord);
// decode material
const uint material_id = material_ids_arrays[prd.m_instance_id].material_ids[prd.m_primitive_id];
material = mat.materials[material_id];
decode_material(material, texcoord);
// correct normals
gnormal = gnormal * sign(dot(gnormal, -ray.m_direction));
snormal = snormal * sign(dot(gnormal, snormal));
// create onb
onb = Onb_create(snormal);
}
// evaluate mis if we hit emissive objects emission
if (is_intersect_surface)
{
const vec3 emission = material.m_emission;
if (i_bounce == 0) { direct_light_estimate += material.m_emission; continue; }
const float emitter_pdf_a = geometry_emitter_pdf(prd.m_instance_id, prd.m_primitive_id, triangle_area);
const float brdf_pdf_a = brdf_sampling_pdf_w * dot(gnormal, -ray.m_direction) / sqr(prd.m_t);
const float weight = compute_mi_weight(brdf_pdf_a, emitter_pdf_a, mode, STAGE_BRDF_SAMPLING);
const vec3 weighted_estimate = importance * weight * emission;
const bool weighted_estimate_nan = any(isnan(weighted_estimate));
if (i_bounce == 1) direct_light_estimate += weighted_estimate_nan ? vec3(0.0f) : weighted_estimate;
if (i_bounce > 1) indirect_light_estimate += weighted_estimate_nan ? vec3(0.0f) : weighted_estimate;
}
else // intersect envmap
{
// fetch emissive value from envmap
const vec2 latlong_texcoord = panorama_from_world(ray.m_direction);
const vec3 emission = texture(envmap, latlong_texcoord).xyz;
if (i_bounce == 0) { break; }
const float brdf_pdf = brdf_sampling_pdf_w;
const float emitter_pdf = envmap_emitter_pdf(latlong_texcoord);
const float weight = compute_mi_weight(brdf_pdf, emitter_pdf, mode, STAGE_BRDF_SAMPLING);
const vec3 weighted_estimate = importance * weight * emission;
const bool weighted_estimate_nan = any(isnan(weighted_estimate));
if (i_bounce == 1) direct_light_estimate += weighted_estimate_nan ? vec3(0.0f) : weighted_estimate;
if (i_bounce > 1) indirect_light_estimate += weighted_estimate_nan ? vec3(0.0f) : weighted_estimate;
break;
}
}
// accumulate the sample
vec3 radiance = direct_light_estimate + indirect_light_estimate;
accumulated_radiance = ((accumulated_radiance * num_spp) + radiance) / (num_spp + 1.0f);
// determine whether we should temrinate or not
imageStore(image, ivec2(gl_LaunchIDNV.xy), vec4(accumulated_radiance, num_spp + 1.0f));
}