@plasius/gpu-lighting 0.1.15 → 0.1.17
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/CHANGELOG.md +39 -0
- package/README.md +50 -0
- package/dist/index.cjs +105 -0
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +102 -0
- package/dist/index.js.map +1 -1
- package/dist/techniques/techniques/hybrid/prelude.wgsl +117 -0
- package/dist/techniques/techniques/hybrid/reflection-resolve.job.wgsl +235 -2
- package/dist/techniques/techniques/pathtracer/accumulate.job.wgsl +64 -2
- package/dist/techniques/techniques/pathtracer/denoise.job.wgsl +125 -2
- package/dist/techniques/techniques/pathtracer/pathtrace.job.wgsl +429 -2
- package/dist/techniques/techniques/pathtracer/prelude.wgsl +260 -0
- package/package.json +8 -8
- package/src/index.js +114 -0
- package/src/techniques/hybrid/prelude.wgsl +117 -0
- package/src/techniques/hybrid/reflection-resolve.job.wgsl +235 -2
- package/src/techniques/pathtracer/accumulate.job.wgsl +64 -2
- package/src/techniques/pathtracer/denoise.job.wgsl +125 -2
- package/src/techniques/pathtracer/pathtrace.job.wgsl +429 -2
- package/src/techniques/pathtracer/prelude.wgsl +260 -0
|
@@ -1,8 +1,20 @@
|
|
|
1
|
+
const PATH_TRACER_PI: f32 = 3.141592653589793;
|
|
2
|
+
const PATH_TRACER_INV_PI: f32 = 0.3183098861837907;
|
|
3
|
+
const PATH_TRACER_EPSILON: f32 = 0.0005;
|
|
4
|
+
|
|
1
5
|
struct PathTracerParams {
|
|
2
6
|
frame_index: u32,
|
|
3
7
|
max_bounces: u32,
|
|
4
8
|
samples_per_pixel: u32,
|
|
5
9
|
enable_next_event_estimation: u32,
|
|
10
|
+
image_width: u32,
|
|
11
|
+
image_height: u32,
|
|
12
|
+
accumulation_reset: u32,
|
|
13
|
+
environment_mode: u32,
|
|
14
|
+
environment_intensity: f32,
|
|
15
|
+
exposure: f32,
|
|
16
|
+
history_blend: f32,
|
|
17
|
+
denoise_strength: f32,
|
|
6
18
|
};
|
|
7
19
|
|
|
8
20
|
struct PathSample {
|
|
@@ -10,6 +22,254 @@ struct PathSample {
|
|
|
10
22
|
throughput: vec3<f32>,
|
|
11
23
|
};
|
|
12
24
|
|
|
25
|
+
struct PathTracerCamera {
|
|
26
|
+
origin: vec3<f32>,
|
|
27
|
+
aspect_ratio: f32,
|
|
28
|
+
forward: vec3<f32>,
|
|
29
|
+
vertical_fov_radians: f32,
|
|
30
|
+
right: vec3<f32>,
|
|
31
|
+
focus_distance: f32,
|
|
32
|
+
up: vec3<f32>,
|
|
33
|
+
aperture_radius: f32,
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
struct PathTracerSceneMetadata {
|
|
37
|
+
sphere_count: u32,
|
|
38
|
+
triangle_count: u32,
|
|
39
|
+
material_count: u32,
|
|
40
|
+
max_trace_distance: f32,
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
struct PathTracerGroundPlane {
|
|
44
|
+
normal: vec3<f32>,
|
|
45
|
+
height: f32,
|
|
46
|
+
material_index: u32,
|
|
47
|
+
enabled: u32,
|
|
48
|
+
_padding0: vec2<u32>,
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
struct PathTracerMaterial {
|
|
52
|
+
albedo_roughness: vec4<f32>,
|
|
53
|
+
emission_metalness: vec4<f32>,
|
|
54
|
+
transmittance_ior: vec4<f32>,
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
struct PathTracerSphere {
|
|
58
|
+
center_radius: vec4<f32>,
|
|
59
|
+
material_index: u32,
|
|
60
|
+
flags: u32,
|
|
61
|
+
_padding0: vec2<u32>,
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
struct PathTracerTriangle {
|
|
65
|
+
position0: vec4<f32>,
|
|
66
|
+
position1: vec4<f32>,
|
|
67
|
+
position2: vec4<f32>,
|
|
68
|
+
normal0: vec4<f32>,
|
|
69
|
+
normal1: vec4<f32>,
|
|
70
|
+
normal2: vec4<f32>,
|
|
71
|
+
material_index: u32,
|
|
72
|
+
flags: u32,
|
|
73
|
+
_padding0: vec2<u32>,
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
struct Ray {
|
|
77
|
+
origin: vec3<f32>,
|
|
78
|
+
t_min: f32,
|
|
79
|
+
direction: vec3<f32>,
|
|
80
|
+
t_max: f32,
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
struct PathHit {
|
|
84
|
+
hit: u32,
|
|
85
|
+
distance: f32,
|
|
86
|
+
position: vec3<f32>,
|
|
87
|
+
material_index: u32,
|
|
88
|
+
normal: vec3<f32>,
|
|
89
|
+
primitive_kind: u32,
|
|
90
|
+
barycentric: vec3<f32>,
|
|
91
|
+
_padding1: u32,
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
struct MaterialSample {
|
|
95
|
+
albedo: vec3<f32>,
|
|
96
|
+
roughness: f32,
|
|
97
|
+
emission: vec3<f32>,
|
|
98
|
+
metalness: f32,
|
|
99
|
+
transmittance: vec3<f32>,
|
|
100
|
+
refractive_index: f32,
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
struct PathScatter {
|
|
104
|
+
direction: vec3<f32>,
|
|
105
|
+
pdf: f32,
|
|
106
|
+
attenuation: vec3<f32>,
|
|
107
|
+
event_kind: f32,
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
struct PathState {
|
|
111
|
+
throughput_bounce: vec4<f32>,
|
|
112
|
+
origin_active: vec4<f32>,
|
|
113
|
+
direction_pdf: vec4<f32>,
|
|
114
|
+
random_state: u32,
|
|
115
|
+
last_hit_kind: u32,
|
|
116
|
+
_padding0: vec2<u32>,
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
struct PathSamplePixel {
|
|
120
|
+
radiance_opacity: vec4<f32>,
|
|
121
|
+
direction_distance: vec4<f32>,
|
|
122
|
+
normal_roughness: vec4<f32>,
|
|
123
|
+
albedo_sample_count: vec4<f32>,
|
|
124
|
+
};
|
|
125
|
+
|
|
126
|
+
struct PathAccumulationPixel {
|
|
127
|
+
integrated_radiance: vec4<f32>,
|
|
128
|
+
moments: vec4<f32>,
|
|
129
|
+
};
|
|
130
|
+
|
|
131
|
+
struct PathDenoisePixel {
|
|
132
|
+
filtered_radiance: vec4<f32>,
|
|
133
|
+
normal_depth: vec4<f32>,
|
|
134
|
+
albedo_history: vec4<f32>,
|
|
135
|
+
};
|
|
136
|
+
|
|
13
137
|
fn luminance(value: vec3<f32>) -> f32 {
|
|
14
138
|
return dot(value, vec3<f32>(0.2126, 0.7152, 0.0722));
|
|
15
139
|
}
|
|
140
|
+
|
|
141
|
+
fn saturate(value: f32) -> f32 {
|
|
142
|
+
return clamp(value, 0.0, 1.0);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
fn safe_rcp(value: f32) -> f32 {
|
|
146
|
+
if (abs(value) <= PATH_TRACER_EPSILON) {
|
|
147
|
+
return 0.0;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
return 1.0 / value;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
fn safe_normalize(value: vec3<f32>) -> vec3<f32> {
|
|
154
|
+
if (dot(value, value) <= PATH_TRACER_EPSILON) {
|
|
155
|
+
return vec3<f32>(0.0, 1.0, 0.0);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
return normalize(value);
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
fn make_default_material() -> PathTracerMaterial {
|
|
162
|
+
return PathTracerMaterial(
|
|
163
|
+
vec4<f32>(0.72, 0.74, 0.76, 0.5),
|
|
164
|
+
vec4<f32>(0.0, 0.0, 0.0, 0.0),
|
|
165
|
+
vec4<f32>(0.0, 0.0, 0.0, 1.45)
|
|
166
|
+
);
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
fn unpack_material(material: PathTracerMaterial) -> MaterialSample {
|
|
170
|
+
return MaterialSample(
|
|
171
|
+
material.albedo_roughness.xyz,
|
|
172
|
+
clamp(material.albedo_roughness.w, 0.02, 1.0),
|
|
173
|
+
material.emission_metalness.xyz,
|
|
174
|
+
saturate(material.emission_metalness.w),
|
|
175
|
+
material.transmittance_ior.xyz,
|
|
176
|
+
max(material.transmittance_ior.w, 1.0)
|
|
177
|
+
);
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
fn hash_u32(value: u32) -> u32 {
|
|
181
|
+
var x = value + 0x9e3779b9u;
|
|
182
|
+
x = (x ^ (x >> 16u)) * 0x85ebca6bu;
|
|
183
|
+
x = (x ^ (x >> 13u)) * 0xc2b2ae35u;
|
|
184
|
+
return x ^ (x >> 16u);
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
fn random_f32(state: ptr<function, u32>) -> f32 {
|
|
188
|
+
let next = hash_u32((*state) ^ 0x68bc21ebu);
|
|
189
|
+
*state = next;
|
|
190
|
+
return f32(next) * 2.3283064365386963e-10;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
fn sample_unit_disk(state: ptr<function, u32>) -> vec2<f32> {
|
|
194
|
+
let radius = sqrt(random_f32(state));
|
|
195
|
+
let angle = 2.0 * PATH_TRACER_PI * random_f32(state);
|
|
196
|
+
return vec2<f32>(cos(angle), sin(angle)) * radius;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
fn sample_unit_sphere(state: ptr<function, u32>) -> vec3<f32> {
|
|
200
|
+
let z = random_f32(state) * 2.0 - 1.0;
|
|
201
|
+
let angle = 2.0 * PATH_TRACER_PI * random_f32(state);
|
|
202
|
+
let radius = sqrt(max(1.0 - z * z, 0.0));
|
|
203
|
+
return vec3<f32>(radius * cos(angle), radius * sin(angle), z);
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
fn build_tangent(normal: vec3<f32>) -> vec3<f32> {
|
|
207
|
+
let basis = select(
|
|
208
|
+
vec3<f32>(0.0, 1.0, 0.0),
|
|
209
|
+
vec3<f32>(1.0, 0.0, 0.0),
|
|
210
|
+
abs(normal.y) > 0.999
|
|
211
|
+
);
|
|
212
|
+
return safe_normalize(cross(basis, normal));
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
fn build_bitangent(normal: vec3<f32>, tangent: vec3<f32>) -> vec3<f32> {
|
|
216
|
+
return safe_normalize(cross(normal, tangent));
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
fn sample_cosine_hemisphere(normal: vec3<f32>, state: ptr<function, u32>) -> vec3<f32> {
|
|
220
|
+
let disk = sample_unit_disk(state);
|
|
221
|
+
let z = sqrt(max(1.0 - dot(disk, disk), 0.0));
|
|
222
|
+
let tangent = build_tangent(normal);
|
|
223
|
+
let bitangent = build_bitangent(normal, tangent);
|
|
224
|
+
return safe_normalize(tangent * disk.x + bitangent * disk.y + normal * z);
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
fn fresnel_schlick(cos_theta: f32, f0: vec3<f32>) -> vec3<f32> {
|
|
228
|
+
let factor = pow(1.0 - saturate(cos_theta), 5.0);
|
|
229
|
+
return f0 + (vec3<f32>(1.0) - f0) * factor;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
fn environment_radiance(
|
|
233
|
+
direction: vec3<f32>,
|
|
234
|
+
intensity: f32,
|
|
235
|
+
mode: u32
|
|
236
|
+
) -> vec3<f32> {
|
|
237
|
+
let up_factor = saturate(direction.y * 0.5 + 0.5);
|
|
238
|
+
let horizon_color = vec3<f32>(0.65, 0.74, 0.86);
|
|
239
|
+
let zenith_color = select(
|
|
240
|
+
vec3<f32>(0.05, 0.12, 0.24),
|
|
241
|
+
vec3<f32>(0.11, 0.08, 0.18),
|
|
242
|
+
mode == 1u
|
|
243
|
+
);
|
|
244
|
+
let sunset_color = vec3<f32>(1.1, 0.64, 0.32);
|
|
245
|
+
let sun_direction = safe_normalize(vec3<f32>(0.35, 0.92, 0.18));
|
|
246
|
+
let moon_direction = safe_normalize(vec3<f32>(-0.2, 0.98, -0.1));
|
|
247
|
+
let sun_glow = pow(saturate(dot(direction, sun_direction)), 256.0);
|
|
248
|
+
let moon_glow = pow(saturate(dot(direction, moon_direction)), 512.0);
|
|
249
|
+
var sky = horizon_color * (1.0 - up_factor) + zenith_color * up_factor;
|
|
250
|
+
if (mode == 1u) {
|
|
251
|
+
sky = sky * (1.0 - up_factor * 0.4) + sunset_color * sun_glow * 0.25;
|
|
252
|
+
}
|
|
253
|
+
return (
|
|
254
|
+
sky +
|
|
255
|
+
vec3<f32>(8.0, 7.6, 6.8) * sun_glow +
|
|
256
|
+
vec3<f32>(0.7, 0.76, 0.9) * moon_glow * 0.3
|
|
257
|
+
) * max(intensity, 0.0001);
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
fn miss_hit(ray: Ray) -> PathHit {
|
|
261
|
+
return PathHit(
|
|
262
|
+
0u,
|
|
263
|
+
ray.t_max,
|
|
264
|
+
ray.origin + ray.direction * ray.t_max,
|
|
265
|
+
0u,
|
|
266
|
+
vec3<f32>(0.0, 1.0, 0.0),
|
|
267
|
+
0u,
|
|
268
|
+
vec3<f32>(0.0),
|
|
269
|
+
0u
|
|
270
|
+
);
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
fn faceforward_normal(normal: vec3<f32>, direction: vec3<f32>) -> vec3<f32> {
|
|
274
|
+
return select(normal, -normal, dot(normal, direction) > 0.0);
|
|
275
|
+
}
|