@naanouff/w3dts-path-tracer 0.0.0-stage → 0.1.18
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +44 -3
- package/dist/PathTracer2Project.d.ts +17 -0
- package/dist/PathTracer2Project.d.ts.map +1 -0
- package/dist/PathTracerFrameUploadController.d.ts +186 -0
- package/dist/PathTracerFrameUploadController.d.ts.map +1 -0
- package/dist/PathTracerProject.d.ts +19 -0
- package/dist/PathTracerProject.d.ts.map +1 -0
- package/dist/SceneToRayData.d.ts +72 -0
- package/dist/SceneToRayData.d.ts.map +1 -0
- package/dist/bloomSubgraph.d.ts +14 -0
- package/dist/bloomSubgraph.d.ts.map +1 -0
- package/dist/buildPathTracerBvh.d.ts +34 -0
- package/dist/buildPathTracerBvh.d.ts.map +1 -0
- package/dist/buildPathTracerTwoLevel.d.ts +66 -0
- package/dist/buildPathTracerTwoLevel.d.ts.map +1 -0
- package/dist/emissiveTriangles.d.ts +28 -0
- package/dist/emissiveTriangles.d.ts.map +1 -0
- package/dist/fsr1EasuConstants.d.ts +7 -0
- package/dist/fsr1EasuConstants.d.ts.map +1 -0
- package/dist/hybridAtrous.d.ts +25 -0
- package/dist/hybridAtrous.d.ts.map +1 -0
- package/dist/hybridOidn.d.ts +15 -0
- package/dist/hybridOidn.d.ts.map +1 -0
- package/dist/hybridTemporal.d.ts +59 -0
- package/dist/hybridTemporal.d.ts.map +1 -0
- package/dist/index.d.ts +32 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/khronosPathTraceReference.d.ts +143 -0
- package/dist/khronosPathTraceReference.d.ts.map +1 -0
- package/dist/pathTracerBackplate.d.ts +29 -0
- package/dist/pathTracerBackplate.d.ts.map +1 -0
- package/dist/pathTracerBaselineMetrics.d.ts +98 -0
- package/dist/pathTracerBaselineMetrics.d.ts.map +1 -0
- package/dist/pathTracerBounceBudget.d.ts +32 -0
- package/dist/pathTracerBounceBudget.d.ts.map +1 -0
- package/dist/pathTracerCameraLens.d.ts +12 -0
- package/dist/pathTracerCameraLens.d.ts.map +1 -0
- package/dist/pathTracerConstants.d.ts +165 -0
- package/dist/pathTracerConstants.d.ts.map +1 -0
- package/dist/pathTracerDielectric.d.ts +30 -0
- package/dist/pathTracerDielectric.d.ts.map +1 -0
- package/dist/pathTracerEnvMap.d.ts +83 -0
- package/dist/pathTracerEnvMap.d.ts.map +1 -0
- package/dist/pathTracerGraphDefinition.d.ts +11 -0
- package/dist/pathTracerGraphDefinition.d.ts.map +1 -0
- package/dist/pathTracerInteractionEpoch.d.ts +11 -0
- package/dist/pathTracerInteractionEpoch.d.ts.map +1 -0
- package/dist/pathTracerInterleave.d.ts +74 -0
- package/dist/pathTracerInterleave.d.ts.map +1 -0
- package/dist/pathTracerLightFingerprint.d.ts +26 -0
- package/dist/pathTracerLightFingerprint.d.ts.map +1 -0
- package/dist/pathTracerMaterialTable.d.ts +63 -0
- package/dist/pathTracerMaterialTable.d.ts.map +1 -0
- package/dist/pathTracerMaterialTextureArrays.d.ts +58 -0
- package/dist/pathTracerMaterialTextureArrays.d.ts.map +1 -0
- package/dist/pathTracerOidnDenoiser.d.ts +43 -0
- package/dist/pathTracerOidnDenoiser.d.ts.map +1 -0
- package/dist/pathTracerPost.d.ts +137 -0
- package/dist/pathTracerPost.d.ts.map +1 -0
- package/dist/pathTracerRuntimeConfig.d.ts +6 -0
- package/dist/pathTracerRuntimeConfig.d.ts.map +1 -0
- package/dist/pathTracerSceneCache.d.ts +32 -0
- package/dist/pathTracerSceneCache.d.ts.map +1 -0
- package/dist/pathTracerSceneSettings.d.ts +43 -0
- package/dist/pathTracerSceneSettings.d.ts.map +1 -0
- package/dist/progressiveRenderState.d.ts +34 -0
- package/dist/progressiveRenderState.d.ts.map +1 -0
- package/dist/w3dts-path-tracer.es.js +4555 -0
- package/dist/w3dts-path-tracer.umd.js +46 -0
- package/package.json +55 -3
- package/shaders/clear_composite.compute.wgsl +20 -0
- package/shaders/clear_composite_layers.compute.wgsl +32 -0
- package/shaders/clear_path_trace_tile.compute.wgsl +30 -0
- package/shaders/copy_accum.compute.wgsl +15 -0
- package/shaders/denoise.compute.wgsl +88 -0
- package/shaders/denoise_atrous.compute.wgsl +104 -0
- package/shaders/display_grade.wgsl +74 -0
- package/shaders/fsr1_easu_path.compute.wgsl +240 -0
- package/shaders/fsr1_rcas_path.compute.wgsl +84 -0
- package/shaders/hole_fill.compute.wgsl +57 -0
- package/shaders/hybrid_atrous.compute.wgsl +239 -0
- package/shaders/hybrid_copy_aov.compute.wgsl +24 -0
- package/shaders/hybrid_gbuffer.wgsl +150 -0
- package/shaders/hybrid_history_copy.compute.wgsl +35 -0
- package/shaders/hybrid_primary_aov.compute.wgsl +784 -0
- package/shaders/hybrid_taa.compute.wgsl +131 -0
- package/shaders/hybrid_temporal.compute.wgsl +507 -0
- package/shaders/path_trace.compute.wgsl +923 -0
- package/shaders/path_trace_common.wgsl +1738 -0
- package/shaders/pt_bloom.compute.wgsl +91 -0
- package/shaders/pt_bloom_down.compute.wgsl +88 -0
- package/shaders/pt_bloom_up.compute.wgsl +76 -0
- package/shaders/pt_display.frag.wgsl +29 -0
- package/shaders/tile_composite.compute.wgsl +22 -0
- package/shaders/tile_composite_layers.compute.wgsl +86 -0
|
@@ -0,0 +1,784 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Hybrid primary AOVs: one raw sample per traced pixel. HybridTemporal accumulates.
|
|
3
|
+
* `hybridShadowOut`: r = sun visibility (luminance of transmittance), g = camera distance of the
|
|
4
|
+
* primary hit (0 = miss), b = VNDF weight of the reflection sample, a = 1.
|
|
5
|
+
* Shadows off keeps r = 1.
|
|
6
|
+
* `hybridReflOut`: rgb = linear incoming radiance weighted by G2/G1 (no Fresnel; the raster
|
|
7
|
+
* applies FssEss), a = distance to the first reflection hit (0 = no reflection sample).
|
|
8
|
+
* `hybridGiOut`: rgb = linear cosine-weighted incoming radiance (demodulated), a = 1 if sampled.
|
|
9
|
+
* Bindings match `path_trace.compute.wgsl` so `path_trace_common.wgsl` compiles unchanged.
|
|
10
|
+
* Roughness ceiling: `pathTracerTileInfo[14]` as f32 bits, 0 = 0.5.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
struct FrameUniforms {
|
|
14
|
+
projectionMatrix: mat4x4<f32>,
|
|
15
|
+
viewMatrix: mat4x4<f32>,
|
|
16
|
+
inverseViewProjectionMatrix: mat4x4<f32>,
|
|
17
|
+
cameraPosition: vec3<f32>,
|
|
18
|
+
lightMatrix: mat4x4<f32>,
|
|
19
|
+
lightDirection: vec3<f32>,
|
|
20
|
+
lightColor: vec3<f32>,
|
|
21
|
+
ambientLightIntensity: f32,
|
|
22
|
+
debugViewMode: u32,
|
|
23
|
+
totalTime: f32,
|
|
24
|
+
_padding1: f32,
|
|
25
|
+
cascadeSplits: vec4<f32>,
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
struct LightData {
|
|
29
|
+
positionAndRange: vec4<f32>,
|
|
30
|
+
dirAndIntensity: vec4<f32>,
|
|
31
|
+
colorAndType: vec4<f32>,
|
|
32
|
+
params: vec4<f32>,
|
|
33
|
+
tangentAndShape: vec4<f32>,
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
struct LightBuffer {
|
|
37
|
+
ambientColor: vec4<f32>,
|
|
38
|
+
lightCount: u32,
|
|
39
|
+
_pad1: u32,
|
|
40
|
+
_pad2: u32,
|
|
41
|
+
_pad3: u32,
|
|
42
|
+
lights: array<LightData>,
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
struct BvhNode {
|
|
46
|
+
bmin: vec3<f32>,
|
|
47
|
+
left: u32,
|
|
48
|
+
bmax: vec3<f32>,
|
|
49
|
+
right: u32,
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
struct PathTracerInstance {
|
|
53
|
+
worldFromObject: mat4x4<f32>,
|
|
54
|
+
objectFromWorld: mat4x4<f32>,
|
|
55
|
+
triBase: u32,
|
|
56
|
+
triCount: u32,
|
|
57
|
+
blasRoot: u32,
|
|
58
|
+
_pad: u32,
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
struct PathTracerMaterial {
|
|
62
|
+
header: vec4<f32>,
|
|
63
|
+
specular: vec4<f32>,
|
|
64
|
+
transmission: vec4<f32>,
|
|
65
|
+
attenuation: vec4<f32>,
|
|
66
|
+
extra: vec4<f32>,
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
@group(0) @binding(0) var<uniform> frame: FrameUniforms;
|
|
70
|
+
@group(0) @binding(1) var<storage, read> sceneLights: LightBuffer;
|
|
71
|
+
|
|
72
|
+
@group(1) @binding(1) var<storage, read> triangles: array<f32>;
|
|
73
|
+
@group(1) @binding(2) var<storage, read> pathTracerParams: array<u32>;
|
|
74
|
+
@group(1) @binding(3) var hybridShadowOut: texture_storage_2d<rgba16float, write>;
|
|
75
|
+
@group(1) @binding(4) var hybridShadowPrev: texture_2d<f32>;
|
|
76
|
+
@group(1) @binding(5) var hybridReflOut: texture_storage_2d<rgba16float, write>;
|
|
77
|
+
@group(1) @binding(6) var<storage, read> pathTracerEnv: array<f32>;
|
|
78
|
+
@group(1) @binding(7) var<storage, read> pathTracerTexSlotFlags: array<u32>;
|
|
79
|
+
|
|
80
|
+
@group(1) @binding(8) var tAlbedo: texture_2d_array<f32>;
|
|
81
|
+
@group(1) @binding(9) var tOrm: texture_2d_array<f32>;
|
|
82
|
+
@group(1) @binding(10) var tNormal: texture_2d_array<f32>;
|
|
83
|
+
@group(1) @binding(11) var tEmissive: texture_2d_array<f32>;
|
|
84
|
+
|
|
85
|
+
@group(1) @binding(52) var<storage, read> pathTracerMatFactors: array<f32, 256>;
|
|
86
|
+
@group(1) @binding(53) var<storage, read> pathTracerInstances: array<PathTracerInstance>;
|
|
87
|
+
@group(1) @binding(54) var<storage, read> pathTracerBvhNodes: array<BvhNode>;
|
|
88
|
+
@group(1) @binding(56) var pathTracerSampler: sampler;
|
|
89
|
+
@group(1) @binding(57) var<storage, read> pathTracerTileInfo: array<u32>;
|
|
90
|
+
@group(1) @binding(58) var envCubemap: texture_cube<f32>;
|
|
91
|
+
@group(1) @binding(61) var<storage, read> pathTracerMaterials: array<PathTracerMaterial>;
|
|
92
|
+
@group(1) @binding(66) var hybridReflPrev: texture_2d<f32>;
|
|
93
|
+
@group(1) @binding(67) var hybridGiOut: texture_storage_2d<rgba16float, write>;
|
|
94
|
+
@group(1) @binding(68) var hybridGiPrev: texture_2d<f32>;
|
|
95
|
+
@group(1) @binding(69) var hybridLightVisOut: texture_storage_2d<rgba16float, write>;
|
|
96
|
+
@group(1) @binding(70) var hybridLightVisPrev: texture_2d<f32>;
|
|
97
|
+
@group(1) @binding(71) var<storage, read> hybridHistory: HybridHistoryParams;
|
|
98
|
+
@group(1) @binding(72) var hybridGDist: texture_2d<f32>;
|
|
99
|
+
@group(1) @binding(73) var hybridGNormal: texture_2d<f32>;
|
|
100
|
+
struct EmissiveEntry {
|
|
101
|
+
tri: u32,
|
|
102
|
+
inst: u32,
|
|
103
|
+
cdf: f32,
|
|
104
|
+
area: f32,
|
|
105
|
+
}
|
|
106
|
+
struct EmissiveCatalog {
|
|
107
|
+
count: u32,
|
|
108
|
+
total_power: f32,
|
|
109
|
+
_pad0: u32,
|
|
110
|
+
_pad1: u32,
|
|
111
|
+
entries: array<EmissiveEntry>,
|
|
112
|
+
}
|
|
113
|
+
@group(1) @binding(74) var<storage, read> pathTracerEmissive: EmissiveCatalog;
|
|
114
|
+
|
|
115
|
+
const FLOATS_PER_TRIANGLE = 57u;
|
|
116
|
+
const MAX_MAT_TEX_SLOTS = 256u;
|
|
117
|
+
const TEX_FLAG_ORM_UV1 = 32u;
|
|
118
|
+
const TEX_FLAG_EMISSIVE_UV1 = 64u;
|
|
119
|
+
const PI = 3.14159265359;
|
|
120
|
+
const AO_RAYS = 8u;
|
|
121
|
+
const AO_RADIUS = 0.8;
|
|
122
|
+
const MAT_SLOT_NONE = 65535u;
|
|
123
|
+
const BVH_TRI_LEAF_BIT = 0x80000000u;
|
|
124
|
+
const BVH_INSTANCE_LEAF = 0xfffffffeu;
|
|
125
|
+
const FLAG_USE_BVH = 1u;
|
|
126
|
+
const FLAG_ENV_CUBEMAP = 2u;
|
|
127
|
+
const FLAG_SOFT_SUN = 4u;
|
|
128
|
+
const FLAG_USE_TLAS = 128u;
|
|
129
|
+
const FLAG_UNLIT_AS_DIFFUSE = 256u;
|
|
130
|
+
/** Same index as TILE_INFO_HYBRID_REFL_ROUGH. */
|
|
131
|
+
const TILE_HYBRID_REFL_ROUGH = 14u;
|
|
132
|
+
const TILE_HYBRID_CLAMP = 15u;
|
|
133
|
+
const HYBRID_REFL_ROUGH_DEFAULT = 0.5;
|
|
134
|
+
const HYBRID_CLAMP_DEFAULT = 20.0;
|
|
135
|
+
const HYBRID_REFL_BOUNCES = 3u;
|
|
136
|
+
|
|
137
|
+
var<private> hit_instance_index: u32 = 0u;
|
|
138
|
+
|
|
139
|
+
/** Traversal, BSDF and environment helpers. Bindings above stay in this file. */
|
|
140
|
+
#include "path_trace_common.wgsl"
|
|
141
|
+
|
|
142
|
+
struct HybridHistoryParams {
|
|
143
|
+
viewProj: mat4x4<f32>,
|
|
144
|
+
invViewProj: mat4x4<f32>,
|
|
145
|
+
prevViewProj: mat4x4<f32>,
|
|
146
|
+
prevInvViewProj: mat4x4<f32>,
|
|
147
|
+
cameraPos: vec4<f32>,
|
|
148
|
+
prevCameraPos: vec4<f32>,
|
|
149
|
+
frameIndex: u32,
|
|
150
|
+
historyReset: u32,
|
|
151
|
+
resX: u32,
|
|
152
|
+
resY: u32,
|
|
153
|
+
traceX: u32,
|
|
154
|
+
traceY: u32,
|
|
155
|
+
traceScale: f32,
|
|
156
|
+
offsetX: f32,
|
|
157
|
+
offsetY: f32,
|
|
158
|
+
sigmaZ: f32,
|
|
159
|
+
sigmaN: f32,
|
|
160
|
+
sigmaL: f32,
|
|
161
|
+
alphaMin: f32,
|
|
162
|
+
_pad0: f32,
|
|
163
|
+
_pad1: f32,
|
|
164
|
+
_pad2: f32,
|
|
165
|
+
};
|
|
166
|
+
|
|
167
|
+
struct HitShade {
|
|
168
|
+
hit: bool,
|
|
169
|
+
unlit: bool,
|
|
170
|
+
frontFace: bool,
|
|
171
|
+
pos: vec3<f32>,
|
|
172
|
+
N: vec3<f32>,
|
|
173
|
+
V: vec3<f32>,
|
|
174
|
+
baseColor: vec3<f32>,
|
|
175
|
+
emissive: vec3<f32>,
|
|
176
|
+
metallic: f32,
|
|
177
|
+
roughness: f32,
|
|
178
|
+
clearcoat: f32,
|
|
179
|
+
clearcoatRoughness: f32,
|
|
180
|
+
F0: vec3<f32>,
|
|
181
|
+
F90: vec3<f32>,
|
|
182
|
+
dist: f32,
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
fn luma(c: vec3<f32>) -> f32 {
|
|
186
|
+
return dot(c, vec3<f32>(0.2126, 0.7152, 0.0722));
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
fn miss_radiance(rd: vec3<f32>, pathEnv: u32, featureFlags: u32, envIblIntensity: f32) -> vec3<f32> {
|
|
190
|
+
let dir = normalize(rd);
|
|
191
|
+
if (pathEnv < env_layer_count()) {
|
|
192
|
+
return env_equirect(pathEnv, dir) + softbox_emission(pathEnv, dir);
|
|
193
|
+
}
|
|
194
|
+
if ((featureFlags & FLAG_ENV_CUBEMAP) != 0u) {
|
|
195
|
+
return env_sample(dir, envIblIntensity);
|
|
196
|
+
}
|
|
197
|
+
return sky_color(dir);
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
fn orient_hit(
|
|
201
|
+
hitTri: u32,
|
|
202
|
+
bu: f32,
|
|
203
|
+
bv: f32,
|
|
204
|
+
rd: vec3<f32>,
|
|
205
|
+
useTlas: bool,
|
|
206
|
+
) -> vec3<f32> {
|
|
207
|
+
let norms = get_triangle_normals(hitTri);
|
|
208
|
+
let bw = 1.0 - bu - bv;
|
|
209
|
+
var n = normalize(norms[0] * bw + norms[1] * bu + norms[2] * bv);
|
|
210
|
+
if (useTlas) {
|
|
211
|
+
n = transform_normal_object_to_world(pathTracerInstances[hit_instance_index].objectFromWorld, n);
|
|
212
|
+
}
|
|
213
|
+
if (dot(n, rd) > 0.0) { n = -n; }
|
|
214
|
+
return n;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
fn shade_triangle(
|
|
218
|
+
hitTri: u32,
|
|
219
|
+
bu: f32,
|
|
220
|
+
bv: f32,
|
|
221
|
+
rd: vec3<f32>,
|
|
222
|
+
dist: f32,
|
|
223
|
+
ro: vec3<f32>,
|
|
224
|
+
useTlas: bool,
|
|
225
|
+
) -> HitShade {
|
|
226
|
+
var s: HitShade;
|
|
227
|
+
s.hit = true;
|
|
228
|
+
s.dist = dist;
|
|
229
|
+
s.pos = ro + rd * dist;
|
|
230
|
+
let hitMat = pathTracerMaterials[get_triangle_material_index(hitTri)];
|
|
231
|
+
s.unlit = u32(hitMat.header.y + 0.5) == BSDF_UNLIT;
|
|
232
|
+
let matSlot = material_tex_slot(hitMat);
|
|
233
|
+
let uvs = get_triangle_uvs(hitTri);
|
|
234
|
+
let uvs1 = get_triangle_uvs1(hitTri);
|
|
235
|
+
let tangs = get_triangle_tangents(hitTri);
|
|
236
|
+
let bitans = get_triangle_bitangents(hitTri);
|
|
237
|
+
let bw = 1.0 - bu - bv;
|
|
238
|
+
let norms = get_triangle_normals(hitTri);
|
|
239
|
+
var rawN = normalize(norms[0] * bw + norms[1] * bu + norms[2] * bv);
|
|
240
|
+
if (useTlas) {
|
|
241
|
+
rawN = transform_normal_object_to_world(pathTracerInstances[hit_instance_index].objectFromWorld, rawN);
|
|
242
|
+
}
|
|
243
|
+
s.frontFace = dot(rawN, rd) < 0.0;
|
|
244
|
+
var hitN = select(-rawN, rawN, s.frontFace);
|
|
245
|
+
var hitTan = tangs[0] * bw + tangs[1] * bu + tangs[2] * bv;
|
|
246
|
+
var hitBin = bitans[0] * bw + bitans[1] * bu + bitans[2] * bv;
|
|
247
|
+
if (useTlas) {
|
|
248
|
+
let inst = pathTracerInstances[hit_instance_index];
|
|
249
|
+
hitTan = normalize(transform_dir_mat4(inst.worldFromObject, hitTan));
|
|
250
|
+
hitBin = normalize(transform_dir_mat4(inst.worldFromObject, hitBin));
|
|
251
|
+
}
|
|
252
|
+
s.N = build_shading_normal(matSlot, bu, bv, uvs, hitN, hitTan, hitBin);
|
|
253
|
+
if (dot(s.N, rd) > 0.0) { s.N = -s.N; }
|
|
254
|
+
let met0 = get_triangle_metallic(hitTri);
|
|
255
|
+
let rough0 = get_triangle_roughness(hitTri);
|
|
256
|
+
let mr = eval_surface_scalar(matSlot, bu, bv, uvs, uvs1, met0, rough0);
|
|
257
|
+
s.metallic = select(mr.x, 0.0, s.unlit);
|
|
258
|
+
s.roughness = select(mr.y, 1.0, s.unlit);
|
|
259
|
+
s.baseColor = eval_surface_albedo(matSlot, bu, bv, uvs, get_triangle_base_color(hitTri));
|
|
260
|
+
s.emissive = eval_surface_emissive(matSlot, bu, bv, uvs, uvs1, get_triangle_emissive(hitTri));
|
|
261
|
+
s.V = normalize(-rd);
|
|
262
|
+
s.clearcoat = clamp(hitMat.extra.x, 0.0, 1.0);
|
|
263
|
+
s.clearcoatRoughness = max(hitMat.extra.y, 0.04);
|
|
264
|
+
s.F0 = pbr_f0(s.baseColor, s.metallic, dielectric_f0_specular_ext(hitMat));
|
|
265
|
+
s.F90 = pbr_f90(hitMat, s.metallic);
|
|
266
|
+
return s;
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
fn sign_not_zero(v: f32) -> f32 {
|
|
270
|
+
return select(-1.0, 1.0, v >= 0.0);
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
fn decode_oct(encoded: vec2<f32>) -> vec3<f32> {
|
|
274
|
+
let z = 1.0 - abs(encoded.x) - abs(encoded.y);
|
|
275
|
+
var xy = encoded;
|
|
276
|
+
if (z < 0.0) {
|
|
277
|
+
xy = vec2<f32>(
|
|
278
|
+
(1.0 - abs(encoded.y)) * sign_not_zero(encoded.x),
|
|
279
|
+
(1.0 - abs(encoded.x)) * sign_not_zero(encoded.y)
|
|
280
|
+
);
|
|
281
|
+
}
|
|
282
|
+
return normalize(vec3<f32>(xy, z));
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
fn shade_gbuffer(pixel: vec2<i32>, rd: vec3<f32>) -> HitShade {
|
|
286
|
+
var s: HitShade;
|
|
287
|
+
let dist = textureLoad(hybridGDist, pixel, 0).r;
|
|
288
|
+
if (dist <= 1e-4) {
|
|
289
|
+
s.hit = false;
|
|
290
|
+
return s;
|
|
291
|
+
}
|
|
292
|
+
let packed = textureLoad(hybridGNormal, pixel, 0);
|
|
293
|
+
s.hit = true;
|
|
294
|
+
s.unlit = false;
|
|
295
|
+
s.frontFace = true;
|
|
296
|
+
s.dist = dist;
|
|
297
|
+
s.N = decode_oct(packed.xy);
|
|
298
|
+
if (dot(s.N, rd) > 0.0) { s.N = -s.N; }
|
|
299
|
+
s.pos = frame.cameraPosition + rd * dist;
|
|
300
|
+
s.roughness = max(packed.z, 0.04);
|
|
301
|
+
s.metallic = packed.w;
|
|
302
|
+
s.baseColor = vec3<f32>(0.5);
|
|
303
|
+
s.emissive = vec3<f32>(0.0);
|
|
304
|
+
s.clearcoat = 0.0;
|
|
305
|
+
s.clearcoatRoughness = 0.04;
|
|
306
|
+
s.V = normalize(-rd);
|
|
307
|
+
s.F0 = vec3<f32>(0.04);
|
|
308
|
+
s.F90 = vec3<f32>(1.0);
|
|
309
|
+
return s;
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
/**
|
|
313
|
+
* The G-buffer has no material. One primary ray recovers it; the G-buffer stays the
|
|
314
|
+
* reference surface, and a ray that lands elsewhere keeps the geometric fallback.
|
|
315
|
+
*/
|
|
316
|
+
fn shade_primary(
|
|
317
|
+
pixel: vec2<i32>,
|
|
318
|
+
rd: vec3<f32>,
|
|
319
|
+
count: u32,
|
|
320
|
+
useBvh: bool,
|
|
321
|
+
useTlas: bool,
|
|
322
|
+
root: u32,
|
|
323
|
+
nodeCount: u32,
|
|
324
|
+
) -> HitShade {
|
|
325
|
+
let g = shade_gbuffer(pixel, rd);
|
|
326
|
+
if (!g.hit) { return g; }
|
|
327
|
+
let ro = frame.cameraPosition;
|
|
328
|
+
let h = trace_closest_scene(ro, rd, count, useBvh, useTlas, root, nodeCount);
|
|
329
|
+
if (h.x > 0.0 && h.x < 1e9 && abs(h.x - g.dist) <= 0.02 * max(g.dist, 1.0)) {
|
|
330
|
+
return shade_triangle(u32(h.w), h.y, h.z, rd, h.x, ro, useTlas);
|
|
331
|
+
}
|
|
332
|
+
return g;
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
/**
|
|
336
|
+
* End-of-path ambient term: albedo * (1 - metal) * E_amb(N), E_amb = cubemap at its last mip
|
|
337
|
+
* (irradiance approximation). Only the last vertex of a path adds it.
|
|
338
|
+
*/
|
|
339
|
+
fn ambient_end(s: HitShade, featureFlags: u32, envIblIntensity: f32) -> vec3<f32> {
|
|
340
|
+
if (s.unlit && (featureFlags & FLAG_UNLIT_AS_DIFFUSE) == 0u) {
|
|
341
|
+
return vec3<f32>(0.0);
|
|
342
|
+
}
|
|
343
|
+
if ((featureFlags & FLAG_ENV_CUBEMAP) == 0u) {
|
|
344
|
+
return vec3<f32>(0.0);
|
|
345
|
+
}
|
|
346
|
+
let lod = f32(textureNumLevels(envCubemap) - 1u);
|
|
347
|
+
let e = textureSampleLevel(envCubemap, pathTracerSampler, s.N, lod).rgb * envIblIntensity;
|
|
348
|
+
return e * s.baseColor * (1.0 - s.metallic);
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
struct PunctualEval {
|
|
352
|
+
dir: vec3<f32>,
|
|
353
|
+
radiance: vec3<f32>,
|
|
354
|
+
lightT: f32,
|
|
355
|
+
valid: u32,
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
// Directional, point, spot and area (rect/disk).
|
|
359
|
+
fn eval_punctual_light(
|
|
360
|
+
li: u32,
|
|
361
|
+
pos: vec3<f32>,
|
|
362
|
+
pixel: vec2<u32>,
|
|
363
|
+
frameIdx: u32,
|
|
364
|
+
sunAngular: f32,
|
|
365
|
+
softSun: bool,
|
|
366
|
+
jitterRadius: f32,
|
|
367
|
+
) -> PunctualEval {
|
|
368
|
+
let rawLight = sceneLights.lights[li];
|
|
369
|
+
let lightType = u32(rawLight.colorAndType.w + 0.1);
|
|
370
|
+
var out: PunctualEval;
|
|
371
|
+
out.valid = 0u;
|
|
372
|
+
if (lightType > 3u) { return out; }
|
|
373
|
+
if (lightType == 3u) {
|
|
374
|
+
let areaSamp = sample_area_light(rawLight, pos, rand2(pixel, frameIdx * 431u + li * 17u + 3u));
|
|
375
|
+
if (areaSamp.valid == 0u) { return out; }
|
|
376
|
+
out.dir = areaSamp.dir;
|
|
377
|
+
out.radiance = areaSamp.radiance;
|
|
378
|
+
out.lightT = areaSamp.lightT;
|
|
379
|
+
out.valid = 1u;
|
|
380
|
+
return out;
|
|
381
|
+
}
|
|
382
|
+
var L = vec3<f32>(0.0, 1.0, 0.0);
|
|
383
|
+
var radiance = rawLight.colorAndType.rgb * rawLight.dirAndIntensity.w;
|
|
384
|
+
var lightT = 1e10;
|
|
385
|
+
if (lightType == 0u) {
|
|
386
|
+
L = normalize(-rawLight.dirAndIntensity.xyz);
|
|
387
|
+
if (softSun && sunAngular > 0.0) {
|
|
388
|
+
L = sample_cone_around(L, sunAngular, pixel, frameIdx * 401u + li);
|
|
389
|
+
}
|
|
390
|
+
} else {
|
|
391
|
+
var lightPos = rawLight.positionAndRange.xyz;
|
|
392
|
+
if (jitterRadius > 0.0) {
|
|
393
|
+
let axis = normalize(lightPos - pos);
|
|
394
|
+
let r = rand2(pixel, frameIdx * 17u + li * 13u);
|
|
395
|
+
let ang = r.x * 6.2831853;
|
|
396
|
+
let rad = sqrt(r.y) * jitterRadius;
|
|
397
|
+
let up = select(vec3<f32>(0.0, 1.0, 0.0), vec3<f32>(1.0, 0.0, 0.0), abs(axis.y) > 0.9);
|
|
398
|
+
let tng = normalize(cross(up, axis));
|
|
399
|
+
lightPos += (tng * cos(ang) + cross(axis, tng) * sin(ang)) * rad;
|
|
400
|
+
}
|
|
401
|
+
let offset = lightPos - pos;
|
|
402
|
+
let dist = length(offset);
|
|
403
|
+
if (dist < 1e-4) { return out; }
|
|
404
|
+
L = offset / dist;
|
|
405
|
+
let d = max(dist, 1e-4);
|
|
406
|
+
var att = 1.0 / (d * d);
|
|
407
|
+
let range = rawLight.positionAndRange.w;
|
|
408
|
+
if (range > 0.0) {
|
|
409
|
+
let ratio = d / range;
|
|
410
|
+
att *= max(min(1.0 - ratio * ratio * ratio * ratio, 1.0), 0.0);
|
|
411
|
+
}
|
|
412
|
+
if (att <= 0.0) { return out; }
|
|
413
|
+
if (lightType == 2u) {
|
|
414
|
+
let actualCos = dot(normalize(rawLight.dirAndIntensity.xyz), -L);
|
|
415
|
+
let innerCos = rawLight.params.x;
|
|
416
|
+
let outerCos = rawLight.params.y;
|
|
417
|
+
if (actualCos <= outerCos) { return out; }
|
|
418
|
+
if (actualCos < innerCos) {
|
|
419
|
+
let ang = (actualCos - outerCos) / max(innerCos - outerCos, 1e-4);
|
|
420
|
+
att *= ang * ang;
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
radiance *= att;
|
|
424
|
+
lightT = dist - 0.004;
|
|
425
|
+
}
|
|
426
|
+
out.dir = L;
|
|
427
|
+
out.radiance = radiance;
|
|
428
|
+
out.lightT = lightT;
|
|
429
|
+
out.valid = 1u;
|
|
430
|
+
return out;
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
// Rank among shadow-casting point and spot lights of lower index. -1 when this light is not one.
|
|
434
|
+
fn punctual_shadow_slot(li: u32) -> i32 {
|
|
435
|
+
let rawLight = sceneLights.lights[li];
|
|
436
|
+
let lightType = u32(rawLight.colorAndType.w + 0.1);
|
|
437
|
+
if ((lightType != 1u && lightType != 2u) || rawLight.params.z <= -0.5) { return -1; }
|
|
438
|
+
var slot = 0;
|
|
439
|
+
for (var j = 0u; j < li; j++) {
|
|
440
|
+
let earlier = sceneLights.lights[j];
|
|
441
|
+
let earlierType = u32(earlier.colorAndType.w + 0.1);
|
|
442
|
+
if ((earlierType == 1u || earlierType == 2u) && earlier.params.z > -0.5) {
|
|
443
|
+
slot += 1;
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
return slot;
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
fn punctual_visibility(
|
|
450
|
+
origin: vec3<f32>,
|
|
451
|
+
N: vec3<f32>,
|
|
452
|
+
pixel: vec2<u32>,
|
|
453
|
+
frameIdx: u32,
|
|
454
|
+
count: u32,
|
|
455
|
+
useBvh: bool,
|
|
456
|
+
useTlas: bool,
|
|
457
|
+
root: u32,
|
|
458
|
+
nodeCount: u32,
|
|
459
|
+
sceneFlags: u32,
|
|
460
|
+
sunAngular: f32,
|
|
461
|
+
softSun: bool,
|
|
462
|
+
lightRadius: f32,
|
|
463
|
+
) -> vec3<f32> {
|
|
464
|
+
var vis = vec3<f32>(1.0);
|
|
465
|
+
let lightCount = min(sceneLights.lightCount, min(64u, arrayLength(&sceneLights.lights)));
|
|
466
|
+
for (var li = 0u; li < lightCount; li++) {
|
|
467
|
+
let slot = punctual_shadow_slot(li);
|
|
468
|
+
if (slot < 0 || slot > 2) { continue; }
|
|
469
|
+
let lit = eval_punctual_light(li, origin, pixel, frameIdx, sunAngular, softSun, lightRadius);
|
|
470
|
+
let slotU = u32(slot);
|
|
471
|
+
if (lit.valid == 0u || dot(N, lit.dir) <= 0.0) {
|
|
472
|
+
vis[slotU] = 0.0;
|
|
473
|
+
continue;
|
|
474
|
+
}
|
|
475
|
+
let shadowOrigin = origin + N * surface_bias(length(origin));
|
|
476
|
+
vis[slotU] = luma(shadow_transmittance(
|
|
477
|
+
shadowOrigin, lit.dir, count, useBvh, useTlas, root, nodeCount, sceneFlags, lit.lightT
|
|
478
|
+
));
|
|
479
|
+
}
|
|
480
|
+
return vis;
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
fn direct_at_hit(
|
|
484
|
+
s: HitShade,
|
|
485
|
+
pixel: vec2<u32>,
|
|
486
|
+
frameIdx: u32,
|
|
487
|
+
count: u32,
|
|
488
|
+
useBvh: bool,
|
|
489
|
+
useTlas: bool,
|
|
490
|
+
root: u32,
|
|
491
|
+
nodeCount: u32,
|
|
492
|
+
sceneFlags: u32,
|
|
493
|
+
shadowsOn: bool,
|
|
494
|
+
sunAngular: f32,
|
|
495
|
+
softSun: bool,
|
|
496
|
+
pathEnv: u32,
|
|
497
|
+
featureFlags: u32,
|
|
498
|
+
envIblIntensity: f32,
|
|
499
|
+
clampY: f32,
|
|
500
|
+
emitWeight: f32,
|
|
501
|
+
) -> vec3<f32> {
|
|
502
|
+
if (s.unlit && (featureFlags & FLAG_UNLIT_AS_DIFFUSE) == 0u) {
|
|
503
|
+
return firefly_clamp_contrib(s.baseColor, clampY);
|
|
504
|
+
}
|
|
505
|
+
var Lo = select(vec3<f32>(0.0), s.emissive * emitWeight * emissive_bounce_scale(), s.frontFace);
|
|
506
|
+
let origin = s.pos + s.N * surface_bias(length(s.pos));
|
|
507
|
+
let safeRough = max(s.roughness, 0.04);
|
|
508
|
+
let safeCcRough = max(s.clearcoatRoughness, 0.04);
|
|
509
|
+
let lobeProbs = compute_bsdf_lobe_probs(
|
|
510
|
+
max(dot(s.N, s.V), 0.001), s.F0, s.baseColor, s.metallic, s.clearcoat, true
|
|
511
|
+
);
|
|
512
|
+
let eu = rand2(pixel, frameIdx * 19u + 4u);
|
|
513
|
+
let ev = rand2(pixel, frameIdx * 19u + 5u);
|
|
514
|
+
Lo += emissive_nee_layered(
|
|
515
|
+
s.pos, s.N, s.V, s.F0, s.F90, s.baseColor, s.metallic, safeRough, s.clearcoat, safeCcRough, lobeProbs,
|
|
516
|
+
vec3<f32>(eu.x, eu.y, ev.x), origin, count, useBvh, useTlas, root, nodeCount, sceneFlags, shadowsOn
|
|
517
|
+
);
|
|
518
|
+
let lightCount = min(sceneLights.lightCount, min(64u, arrayLength(&sceneLights.lights)));
|
|
519
|
+
for (var li = 0u; li < lightCount; li++) {
|
|
520
|
+
let lit = eval_punctual_light(li, s.pos, pixel, frameIdx, sunAngular, softSun, 0.0);
|
|
521
|
+
if (lit.valid == 0u || dot(s.N, lit.dir) <= 0.0) { continue; }
|
|
522
|
+
var vis = vec3<f32>(1.0);
|
|
523
|
+
if (shadowsOn) {
|
|
524
|
+
vis = shadow_transmittance(origin, lit.dir, count, useBvh, useTlas, root, nodeCount, sceneFlags, lit.lightT);
|
|
525
|
+
}
|
|
526
|
+
Lo += layered_opaque_f_cos(
|
|
527
|
+
s.N, s.V, lit.dir, s.F0, s.F90, s.baseColor, s.metallic, safeRough, s.clearcoat, safeCcRough
|
|
528
|
+
) * vis * lit.radiance;
|
|
529
|
+
}
|
|
530
|
+
if (pathEnv < env_layer_count()) {
|
|
531
|
+
let r = rand2(pixel, frameIdx * 307u + 9u);
|
|
532
|
+
let samp = sample_env(pathEnv, r.x, r.y);
|
|
533
|
+
if (samp.pdf > 1e-6 && dot(s.N, samp.dir) > 0.0) {
|
|
534
|
+
var envVis = vec3<f32>(1.0);
|
|
535
|
+
if (shadowsOn) {
|
|
536
|
+
envVis = shadow_transmittance(origin, samp.dir, count, useBvh, useTlas, root, nodeCount, sceneFlags, 1e10);
|
|
537
|
+
}
|
|
538
|
+
Lo += layered_opaque_f_cos(
|
|
539
|
+
s.N, s.V, samp.dir, s.F0, s.F90, s.baseColor, s.metallic, safeRough, s.clearcoat, safeCcRough
|
|
540
|
+
) * env_equirect(pathEnv, samp.dir) * envVis / samp.pdf;
|
|
541
|
+
}
|
|
542
|
+
}
|
|
543
|
+
return firefly_clamp_contrib(Lo, clampY);
|
|
544
|
+
}
|
|
545
|
+
|
|
546
|
+
fn sun_visibility(
|
|
547
|
+
origin: vec3<f32>,
|
|
548
|
+
N: vec3<f32>,
|
|
549
|
+
pixel: vec2<u32>,
|
|
550
|
+
frameIdx: u32,
|
|
551
|
+
count: u32,
|
|
552
|
+
useBvh: bool,
|
|
553
|
+
useTlas: bool,
|
|
554
|
+
root: u32,
|
|
555
|
+
nodeCount: u32,
|
|
556
|
+
sceneFlags: u32,
|
|
557
|
+
sunAngular: f32,
|
|
558
|
+
softSun: bool,
|
|
559
|
+
) -> f32 {
|
|
560
|
+
let lightCount = min(sceneLights.lightCount, min(64u, arrayLength(&sceneLights.lights)));
|
|
561
|
+
for (var li = 0u; li < lightCount; li++) {
|
|
562
|
+
let rawLight = sceneLights.lights[li];
|
|
563
|
+
if (u32(rawLight.colorAndType.w + 0.1) != 0u) { continue; }
|
|
564
|
+
var L = normalize(-rawLight.dirAndIntensity.xyz);
|
|
565
|
+
if (softSun && sunAngular > 0.0) {
|
|
566
|
+
L = sample_cone_around(L, sunAngular, pixel, frameIdx * 401u + li);
|
|
567
|
+
}
|
|
568
|
+
if (dot(N, L) <= 0.0) { return 0.0; }
|
|
569
|
+
let shadowOrigin = origin + N * surface_bias(length(origin));
|
|
570
|
+
let vis = shadow_transmittance(shadowOrigin, L, count, useBvh, useTlas, root, nodeCount, sceneFlags, 1e10);
|
|
571
|
+
return luma(vis);
|
|
572
|
+
}
|
|
573
|
+
return 1.0;
|
|
574
|
+
}
|
|
575
|
+
|
|
576
|
+
@compute @workgroup_size(8, 8, 1)
|
|
577
|
+
fn main(@builtin(global_invocation_id) gid: vec3<u32>) {
|
|
578
|
+
let resetAccum = pathTracerParams[0];
|
|
579
|
+
let lowRes = resetAccum == 0u && hybridHistory.traceScale < 0.99;
|
|
580
|
+
let step = select(1u, max(u32(round(1.0 / max(hybridHistory.traceScale, 0.25))), 1u), lowRes);
|
|
581
|
+
let ox = select(0u, u32(hybridHistory.offsetX + 0.5), lowRes);
|
|
582
|
+
let oy = select(0u, u32(hybridHistory.offsetY + 0.5), lowRes);
|
|
583
|
+
let full = textureDimensions(hybridGDist);
|
|
584
|
+
let px = gid.x * step + ox + select(0u, 1u, pathTracerEmissive.count == 0xFFFFFFFFu);
|
|
585
|
+
let py = gid.y * step + oy;
|
|
586
|
+
if (px >= full.x || py >= full.y) { return; }
|
|
587
|
+
let p = vec2<i32>(i32(px), i32(py));
|
|
588
|
+
let pixel = vec2<u32>(px, py);
|
|
589
|
+
|
|
590
|
+
let shadowsOn = pathTracerParams[1] != 0u;
|
|
591
|
+
let frameIdx = pathTracerParams[2];
|
|
592
|
+
let enableReflection = pathTracerParams[7] != 0u;
|
|
593
|
+
let traceThisFrame = pathTracerParams[8];
|
|
594
|
+
let featureFlags = pathTracerParams[9];
|
|
595
|
+
let bvhNodeCount = pathTracerParams[10];
|
|
596
|
+
let bvhRoot = pathTracerParams[11];
|
|
597
|
+
let envIblIntensityRaw = bitcast<f32>(pathTracerParams[13]);
|
|
598
|
+
let envIblIntensity = max(select(envIblIntensityRaw, 1.0, pathTracerParams[13] == 0u), 0.0);
|
|
599
|
+
let sceneFlags = pathTracerParams[18];
|
|
600
|
+
let sunAngular = max(bitcast<f32>(pathTracerParams[19]), 0.0);
|
|
601
|
+
let aoRadiusBits = pathTracerParams[20];
|
|
602
|
+
let aoRadiusRaw = bitcast<f32>(aoRadiusBits);
|
|
603
|
+
let aoRadius = select(1.0, max(aoRadiusRaw, 0.0), aoRadiusBits != 0u);
|
|
604
|
+
let lightRadius = max(bitcast<f32>(pathTracerParams[21]), 0.0);
|
|
605
|
+
let roughBits = pathTracerTileInfo[TILE_HYBRID_REFL_ROUGH];
|
|
606
|
+
let roughMaxRaw = bitcast<f32>(roughBits);
|
|
607
|
+
let roughMax = select(HYBRID_REFL_ROUGH_DEFAULT, roughMaxRaw, roughBits != 0u && roughMaxRaw > 0.0);
|
|
608
|
+
let clampBits = pathTracerTileInfo[TILE_HYBRID_CLAMP];
|
|
609
|
+
let clampRaw = bitcast<f32>(clampBits);
|
|
610
|
+
let clampY = select(HYBRID_CLAMP_DEFAULT, clampRaw, clampBits != 0u && clampRaw > 0.0);
|
|
611
|
+
|
|
612
|
+
let prevS = textureLoad(hybridShadowPrev, p, 0);
|
|
613
|
+
let prevR = textureLoad(hybridReflPrev, p, 0);
|
|
614
|
+
let prevG = textureLoad(hybridGiPrev, p, 0);
|
|
615
|
+
let prevV = textureLoad(hybridLightVisPrev, p, 0);
|
|
616
|
+
// Keep-last only when paused/idle with no pending reset. A pause-time light/env/resize
|
|
617
|
+
// reset must still trace so the next unpaused frame does not keep pre-reset lighting.
|
|
618
|
+
if (traceThisFrame == 0u && resetAccum == 0u) {
|
|
619
|
+
textureStore(hybridShadowOut, p, prevS);
|
|
620
|
+
textureStore(hybridReflOut, p, prevR);
|
|
621
|
+
textureStore(hybridGiOut, p, prevG);
|
|
622
|
+
textureStore(hybridLightVisOut, p, prevV);
|
|
623
|
+
return;
|
|
624
|
+
}
|
|
625
|
+
|
|
626
|
+
let count = get_triangle_count();
|
|
627
|
+
// The AOVs cover the whole view they are sampled in. tileInfo[2..3] holds the Scene canvas
|
|
628
|
+
// size, which differs from the Game panel.
|
|
629
|
+
let fullSize = vec2<f32>(full);
|
|
630
|
+
let useBvh = ((featureFlags & FLAG_USE_BVH) != 0u) && (bvhNodeCount > 0u);
|
|
631
|
+
let useTlas = ((featureFlags & FLAG_USE_TLAS) != 0u) && useBvh && (get_instance_count() > 0u);
|
|
632
|
+
let softSun = (featureFlags & FLAG_SOFT_SUN) != 0u;
|
|
633
|
+
|
|
634
|
+
var vis = 1.0;
|
|
635
|
+
var depth = 0.0;
|
|
636
|
+
var refl = vec3<f32>(0.0);
|
|
637
|
+
var reflHitT = 65000.0;
|
|
638
|
+
var gi = vec3<f32>(0.0);
|
|
639
|
+
var takeRefl = false;
|
|
640
|
+
var takeGi = false;
|
|
641
|
+
var primaryRough = 1.0;
|
|
642
|
+
var reflWeight = 0.0;
|
|
643
|
+
var punctualVis = vec3<f32>(1.0);
|
|
644
|
+
var ao = 1.0;
|
|
645
|
+
|
|
646
|
+
if (count > 0u) {
|
|
647
|
+
// Pixel center, no jitter: the raster shades the same point, so the accumulated
|
|
648
|
+
// distance stays a single surface on silhouettes.
|
|
649
|
+
let uvPix = (vec2<f32>(pixel) + 0.5) / fullSize;
|
|
650
|
+
let ndc = vec4<f32>(uvPix.x * 2.0 - 1.0, 1.0 - uvPix.y * 2.0, 0.0, 1.0);
|
|
651
|
+
let worldH = frame.inverseViewProjectionMatrix * ndc;
|
|
652
|
+
let rd = normalize(worldH.xyz / worldH.w - frame.cameraPosition);
|
|
653
|
+
var pathEnv = 0u;
|
|
654
|
+
|
|
655
|
+
let s = shade_primary(p, rd, count, useBvh, useTlas, bvhRoot, bvhNodeCount);
|
|
656
|
+
if (s.hit) {
|
|
657
|
+
depth = s.dist;
|
|
658
|
+
primaryRough = s.roughness;
|
|
659
|
+
let primaryUnlit = s.unlit && (featureFlags & FLAG_UNLIT_AS_DIFFUSE) == 0u;
|
|
660
|
+
if (!primaryUnlit && shadowsOn) {
|
|
661
|
+
vis = sun_visibility(
|
|
662
|
+
s.pos, s.N, pixel, frameIdx, count, useBvh, useTlas,
|
|
663
|
+
bvhRoot, bvhNodeCount, sceneFlags, sunAngular, softSun
|
|
664
|
+
);
|
|
665
|
+
punctualVis = punctual_visibility(
|
|
666
|
+
s.pos, s.N, pixel, frameIdx, count, useBvh, useTlas,
|
|
667
|
+
bvhRoot, bvhNodeCount, sceneFlags, sunAngular, softSun, lightRadius
|
|
668
|
+
);
|
|
669
|
+
}
|
|
670
|
+
if (!primaryUnlit && aoRadius > 0.0) {
|
|
671
|
+
let aoDir = cosine_sample_hemisphere(s.N, pixel, 11u, frameIdx);
|
|
672
|
+
let aoOrigin = s.pos + s.N * surface_bias(length(s.pos));
|
|
673
|
+
let aoHit = trace_closest_scene(aoOrigin, aoDir, count, useBvh, useTlas, bvhRoot, bvhNodeCount);
|
|
674
|
+
ao = select(1.0, 0.0, aoHit.x > 0.0 && aoHit.x < aoRadius);
|
|
675
|
+
}
|
|
676
|
+
if (!primaryUnlit) {
|
|
677
|
+
takeGi = true;
|
|
678
|
+
let giDir = cosine_sample_hemisphere(s.N, pixel, 5u, frameIdx);
|
|
679
|
+
let giOrigin = s.pos + s.N * surface_bias(length(s.pos));
|
|
680
|
+
let pu = rand2(pixel, frameIdx * 41u + 6u);
|
|
681
|
+
let pv = rand2(pixel, frameIdx * 41u + 7u);
|
|
682
|
+
let lamp = sample_emissive(vec3<f32>(pu.x, pu.y, pv.x), s.pos);
|
|
683
|
+
if (lamp.valid == 1u && lamp.pdf_w > 0.0) {
|
|
684
|
+
let toL = lamp.pos - s.pos;
|
|
685
|
+
let lampDist = length(toL);
|
|
686
|
+
let L = toL / lampDist;
|
|
687
|
+
let cosS = dot(s.N, L);
|
|
688
|
+
if (cosS > 0.0 && lampDist > 1e-4) {
|
|
689
|
+
let pdfB = cosS / PI;
|
|
690
|
+
let w = mis_balance_power(lamp.pdf_w, pdfB);
|
|
691
|
+
var lampVis = vec3<f32>(1.0);
|
|
692
|
+
if (shadowsOn) {
|
|
693
|
+
lampVis = shadow_transmittance(
|
|
694
|
+
giOrigin, L, count, useBvh, useTlas, bvhRoot, bvhNodeCount,
|
|
695
|
+
sceneFlags, lampDist * (1.0 - 1e-3)
|
|
696
|
+
);
|
|
697
|
+
}
|
|
698
|
+
gi += lamp.Le * cosS * lampVis * w / lamp.pdf_w;
|
|
699
|
+
}
|
|
700
|
+
}
|
|
701
|
+
let gh = trace_closest_scene(giOrigin, giDir, count, useBvh, useTlas, bvhRoot, bvhNodeCount);
|
|
702
|
+
if (gh.x <= 0.0 || gh.x >= 1e9) {
|
|
703
|
+
gi += miss_radiance(giDir, pathEnv, featureFlags, envIblIntensity);
|
|
704
|
+
} else {
|
|
705
|
+
let gs = shade_triangle(u32(gh.w), gh.y, gh.z, giDir, gh.x, giOrigin, useTlas);
|
|
706
|
+
let geoN = triangle_geo_normal_world(u32(gh.w), select(0xFFFFFFFFu, hit_instance_index, useTlas));
|
|
707
|
+
let pdfW = emissive_solid_pdf(u32(gh.w), geoN, giOrigin, gs.pos);
|
|
708
|
+
let pdfB = max(dot(s.N, giDir), 0.0) / PI;
|
|
709
|
+
gi += direct_at_hit(
|
|
710
|
+
gs, pixel, frameIdx * 7u + 2u, count, useBvh, useTlas,
|
|
711
|
+
bvhRoot, bvhNodeCount, sceneFlags, shadowsOn, sunAngular, softSun,
|
|
712
|
+
pathEnv, featureFlags, envIblIntensity, clampY, mis_balance_power(pdfB, pdfW)
|
|
713
|
+
) + ambient_end(gs, featureFlags, envIblIntensity);
|
|
714
|
+
}
|
|
715
|
+
gi = firefly_clamp_contrib(gi, clampY);
|
|
716
|
+
if (enableReflection && s.roughness < roughMax) {
|
|
717
|
+
takeRefl = true;
|
|
718
|
+
var rro = s.pos + s.N * surface_bias(length(s.pos));
|
|
719
|
+
let first = sample_vndf_reflect(s.N, s.V, s.roughness, pixel, frameIdx * 131u + 17u);
|
|
720
|
+
reflWeight = first.w;
|
|
721
|
+
var rrd = first.xyz;
|
|
722
|
+
var tp = vec3<f32>(1.0);
|
|
723
|
+
var reflEnv = pathEnv;
|
|
724
|
+
var prevN = s.N;
|
|
725
|
+
var prevV = s.V;
|
|
726
|
+
var prevRough = s.roughness;
|
|
727
|
+
for (var b = 0u; b < HYBRID_REFL_BOUNCES; b++) {
|
|
728
|
+
if (reflWeight <= 0.0) { break; }
|
|
729
|
+
let rh = trace_closest_scene(rro, rrd, count, useBvh, useTlas, bvhRoot, bvhNodeCount);
|
|
730
|
+
if (rh.x <= 0.0 || rh.x >= 1e9) {
|
|
731
|
+
refl += tp * miss_radiance(rrd, reflEnv, featureFlags, envIblIntensity);
|
|
732
|
+
break;
|
|
733
|
+
}
|
|
734
|
+
if (b == 0u) { reflHitT = rh.x; }
|
|
735
|
+
let rtri = u32(rh.w);
|
|
736
|
+
let rs = shade_triangle(rtri, rh.y, rh.z, rrd, rh.x, rro, useTlas);
|
|
737
|
+
reflEnv = min(u32(pathTracerMaterials[get_triangle_material_index(rtri)].header.z + 0.5), 1u);
|
|
738
|
+
var reflEmitW = 1.0;
|
|
739
|
+
if (prevRough >= 0.04) {
|
|
740
|
+
let geoN = triangle_geo_normal_world(rtri, select(0xFFFFFFFFu, hit_instance_index, useTlas));
|
|
741
|
+
let pdfW = emissive_solid_pdf(rtri, geoN, rro, rs.pos);
|
|
742
|
+
let pdfB = bsdf_mixture_pdf(prevN, prevV, rrd, max(prevRough, 0.04), 0.9);
|
|
743
|
+
reflEmitW = mis_balance_power(pdfB, pdfW);
|
|
744
|
+
}
|
|
745
|
+
refl += tp * direct_at_hit(
|
|
746
|
+
rs, pixel, frameIdx * 7u + 3u + b, count, useBvh, useTlas,
|
|
747
|
+
bvhRoot, bvhNodeCount, sceneFlags, shadowsOn, sunAngular, softSun, reflEnv,
|
|
748
|
+
featureFlags, envIblIntensity, clampY, reflEmitW
|
|
749
|
+
);
|
|
750
|
+
if (rs.unlit && (featureFlags & FLAG_UNLIT_AS_DIFFUSE) == 0u) {
|
|
751
|
+
break;
|
|
752
|
+
}
|
|
753
|
+
prevN = rs.N;
|
|
754
|
+
prevV = rs.V;
|
|
755
|
+
prevRough = rs.roughness;
|
|
756
|
+
if (b + 1u >= HYBRID_REFL_BOUNCES) {
|
|
757
|
+
refl += tp * ambient_end(rs, featureFlags, envIblIntensity);
|
|
758
|
+
break;
|
|
759
|
+
}
|
|
760
|
+
let specContinue = rs.metallic > 0.5 && rs.roughness < roughMax;
|
|
761
|
+
if (specContinue) {
|
|
762
|
+
let next = sample_vndf_reflect(rs.N, rs.V, rs.roughness, pixel, frameIdx * 19u + b);
|
|
763
|
+
if (next.w <= 0.0) { break; }
|
|
764
|
+
tp *= fresnel_schlick(max(dot(rs.N, rs.V), 0.0), rs.F0) * next.w;
|
|
765
|
+
rrd = next.xyz;
|
|
766
|
+
} else {
|
|
767
|
+
tp *= rs.baseColor * (1.0 - rs.metallic);
|
|
768
|
+
rrd = cosine_sample_hemisphere(rs.N, pixel, b + 3u, frameIdx);
|
|
769
|
+
}
|
|
770
|
+
rro = rs.pos + rs.N * surface_bias(length(rs.pos));
|
|
771
|
+
}
|
|
772
|
+
refl = firefly_clamp_contrib(refl, clampY);
|
|
773
|
+
}
|
|
774
|
+
}
|
|
775
|
+
}
|
|
776
|
+
}
|
|
777
|
+
|
|
778
|
+
// One raw sample at every trace scale. HybridTemporal owns the history: an in-place
|
|
779
|
+
// mean here is never reprojected and ghosts as soon as the camera moves.
|
|
780
|
+
textureStore(hybridShadowOut, p, vec4<f32>(vis, depth, reflWeight, 1.0));
|
|
781
|
+
textureStore(hybridReflOut, p, vec4<f32>(refl, select(0.0, reflHitT, takeRefl)));
|
|
782
|
+
textureStore(hybridGiOut, p, vec4<f32>(gi, select(0.0, 1.0, takeGi)));
|
|
783
|
+
textureStore(hybridLightVisOut, p, vec4<f32>(punctualVis, ao));
|
|
784
|
+
}
|