-
Notifications
You must be signed in to change notification settings - Fork 12
/
post_process.wgsl
85 lines (73 loc) · 2.98 KB
/
post_process.wgsl
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
struct VertexOutput {
@location(0) texture_coordinates: vec2<f32>,
@builtin(position) member: vec4<f32>,
}
@vertex
fn fullscreen_main(@builtin(vertex_index) vertex_index: u32) -> VertexOutput {
var texture_coordinates: vec2<f32>;
texture_coordinates.x = select(0.0, 2.0, (vertex_index == u32(2)));
texture_coordinates.y = select(0.0, 2.0, (vertex_index == u32(1)));
var position: vec4<f32> = vec4<f32>(((texture_coordinates * vec2<f32>(2.0, -(2.0))) + vec2<f32>(-(1.0), 1.0)), 1.0, 1.0);
return VertexOutput(texture_coordinates, position);
}
struct Uniform {
texture_size: vec2<f32>,
}
struct FragmentOutput {
@location(0) color: vec4<f32>,
}
@group(0) @binding(0)
var texture_sampler: sampler;
@group(0) @binding(1)
var input_texture: texture_2d<f32>;
@group(0) @binding(2)
var<uniform> global: Uniform;
var<private> color: vec4<f32>;
fn blur(horizontal: bool, texture_coordinates: vec2<f32>) {
color = vec4<f32>(0.0, 0.0, 0.0, 1.0);
for (var i: i32 = -4; i <= 4; i = i + 1) {
var coordinates: vec2<f32> = texture_coordinates;
if (horizontal) {
coordinates.x = (coordinates.x + (f32(i) / (global.texture_size.x - 1.0)));
} else {
coordinates.y = (coordinates.y + (f32(i) / (global.texture_size.y - 1.0)));
}
let tex_sample: vec4<f32> = textureSample(input_texture, texture_sampler, coordinates);
color = vec4<f32>(color.rgb + tex_sample.rgb, color.a);
}
color = vec4<f32>((color.rgb / vec3<f32>(9.0)).rgb, color.a);
}
@fragment
fn blur_horizontal_main(@location(0) texture_coordinates: vec2<f32>) -> FragmentOutput {
blur(true, texture_coordinates);
return FragmentOutput(color);
}
@fragment
fn blur_vertical_main(@location(0) texture_coordinates: vec2<f32>) -> FragmentOutput {
blur(false, texture_coordinates);
return FragmentOutput(color);
}
@fragment
fn copy_glowing_main(@location(0) texture_coordinates: vec2<f32>) -> FragmentOutput {
color = textureSample(input_texture, texture_sampler, texture_coordinates);
color = mix(vec4<f32>(0.0, 0.0, 0.0, 1.0), color, vec4<f32>(step(color.a, 0.0)));
return FragmentOutput(color);
}
@group(0) @binding(1)
var input_texture1: texture_2d<f32>;
@group(0) @binding(2)
var input_texture2: texture_2d<f32>;
@fragment
fn final_pass_main(@location(0) texture_coordinates: vec2<f32>) -> FragmentOutput {
color = textureSample(input_texture1, texture_sampler, texture_coordinates);
color += vec4<f32>(textureSample(input_texture2, texture_sampler, texture_coordinates).rgb, 0.0);
var c = color;
c.r = textureSample(input_texture1, texture_sampler, texture_coordinates + vec2<f32>(0.01, -0.01)).r;
c.g = textureSample(input_texture1, texture_sampler, texture_coordinates + vec2<f32>(-0.01, 0.0)).g;
c.b = textureSample(input_texture1, texture_sampler, texture_coordinates + vec2<f32>(0.0, 0.01)).b;
if (color.a > 0.0 && color.a < 1.0) {
color = c;
}
color.a = 1.0;
return FragmentOutput(color);
}