webgpu-pt

monte carlo path tracer

viewport.wgsl

1.9 kB
 1#include utils.wgsl
 2
 3@group(0) @binding(0) var output_buffer: texture_2d<f32>;
 4
 5struct Interpolator {
 6    @builtin(position) position: vec4<f32>,
 7    @location(0) tex_coord: vec2<f32>,
 8}
 9
10
11fn uncharted2_tonemap_base(x : vec3f) -> vec3f
12{
13	let a = 0.15;
14    let b = 0.50;
15    let c = 0.10;
16    let d = 0.20;
17    let e = 0.02;
18    let f = 0.30;
19	return ((x*(a*x+c*b)+d*e)/(x*(a*x+b)+d*f))-e/f;
20}
21
22fn uncharted2_tonemap(color: vec3f) -> vec3f {
23    let exposure = 2.0; // adjustable
24    let white_point = uncharted2_tonemap_base(vec3f(11.2));
25    let mapped = uncharted2_tonemap_base(color * exposure);
26    return mapped / white_point;
27}
28
29fn gamma_correct(color: vec3<f32>) -> vec3<f32> {
30    return pow(color, vec3<f32>(1.0 / 2.2));
31}
32
33// fn reinhard_tonemap(color: vec3<f32>) -> vec3<f32> {
34//     return color / (color + vec3f(1.0));
35// }
36
37
38@vertex
39fn vert_main(@builtin(vertex_index) vertex_index: u32) -> Interpolator {
40    var positions = array<vec2<f32>, 6>(
41        vec2<f32>(1.0, 1.0),
42        vec2<f32>(1.0, -1.0),
43        vec2<f32>(-1.0, -1.0),
44        vec2<f32>(1.0, 1.0),
45        vec2<f32>(-1.0, -1.0),
46        vec2<f32>(-1.0, 1.0)
47    );
48    var tex_coords = array<vec2<f32>, 6>(
49        vec2<f32>(1.0, 1.0),
50        vec2<f32>(1.0, 0.0),
51        vec2<f32>(0.0, 0.0),
52        vec2<f32>(1.0, 1.0),
53        vec2<f32>(0.0, 0.0),
54        vec2<f32>(0.0, 1.0)
55    );
56    var output: Interpolator;
57    output.position = vec4<f32>(positions[vertex_index], 0.0, 1.0);
58    output.tex_coord = tex_coords[vertex_index];
59    return output;
60}
61
62@fragment
63fn frag_main(@location(0) tex_coord: vec2<f32>) -> @location(0) vec4<f32> {
64    let dims = textureDimensions(output_buffer);
65    let uv = vec2<u32>(tex_coord * vec2<f32>(dims));
66    let linear_color = textureLoad(output_buffer, uv, 0).rgb;
67    return vec4f(gamma_correct(uncharted2_tonemap(linear_color)), 1.0);
68}