summaryrefslogtreecommitdiff
path: root/servers/visual/rasterizer_rd/shaders/bokeh_dof.glsl
blob: 83e77a19fcf34236f286e7b656b939cd5ce00b41 (plain)
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
/* clang-format off */
[compute]
/* clang-format on */
#version 450

VERSION_DEFINES

#define BLOCK_SIZE 8

layout(local_size_x = BLOCK_SIZE, local_size_y = BLOCK_SIZE, local_size_z = 1) in;


#ifdef MODE_GEN_BLUR_SIZE
layout(rgba16f, set = 0, binding = 0) uniform restrict image2D color_image;
layout(set = 1, binding = 0) uniform sampler2D source_depth;
#endif

#ifdef MODE_GEN_BOKEH
layout(set = 2, binding = 0) uniform sampler2D color_texture;
layout(set = 1, binding = 0) uniform sampler2D source_depth;
layout(rgba16f, set = 0, binding = 0) uniform restrict writeonly image2D bokeh_image;
#endif

#ifdef MODE_COMPOSITE_BOKEH
layout(rgba16f, set = 0, binding = 0) uniform restrict image2D color_image;
layout(set = 1, binding = 0) uniform sampler2D source_bokeh;
#endif




layout(push_constant, binding = 1, std430) uniform Params {
	ivec2 size;
	float z_far;
	float z_near;

	bool orthogonal;
	float blur_size;
	float blur_scale;
	uint pad;

	bool blur_near_active;
	float blur_near_begin;
	float blur_near_end;
	bool blur_far_active;
	float blur_far_begin;
	float blur_far_end;
	uint pad2[2];

} params;

#ifndef MODE_COMPOSITE_BOKEH

float get_depth_at_pos(vec2 uv) {
	float depth = textureLod(source_depth,uv,0.0).x;
	if (params.orthogonal) {
		depth = ((depth + (params.z_far + params.z_near) / (params.z_far - params.z_near)) * (params.z_far - params.z_near)) / 2.0;
	} else {
		depth = 2.0 * params.z_near * params.z_far / (params.z_far + params.z_near - depth * (params.z_far - params.z_near));
	}
	return depth;
}

float get_blur_size(float depth) {

	if (params.blur_near_active && depth < params.blur_near_begin) {
		return (1.0 - smoothstep(params.blur_near_end,params.blur_near_begin,depth) ) * params.blur_size;
	}

	if (params.blur_far_active && depth > params.blur_far_begin) {
		return smoothstep(params.blur_far_begin,params.blur_far_end,depth) * params.blur_size;
	}

	return 0.0;
}

#endif

const float GOLDEN_ANGLE = 2.39996323;

void main() {

	ivec2 pos = ivec2(gl_GlobalInvocationID.xy);

	if (any(greaterThan(pos,params.size))) { //too large, do nothing
		return;
	}

	vec2 pixel_size = 1.0/vec2(params.size);
	vec2 uv = vec2(pos) / vec2(params.size);

#ifdef MODE_GEN_BLUR_SIZE
	uv+=pixel_size * 0.5;
	//precompute size in alpha channel
	float depth = get_depth_at_pos(uv);
	float size = get_blur_size(depth);

	vec4 color = imageLoad(color_image,pos);
	color.a = size;
	imageStore(color_image,pos,color);
#endif

#ifdef MODE_GEN_BOKEH

	pixel_size*=0.5; //resolution is doubled

	uv+=pixel_size * 0.5; //half pixel to read centers

	float depth = get_depth_at_pos(uv);
	float size = get_blur_size(depth);
	vec4 color = texture(color_texture,uv);
	float accum = 1.0;
	float radius = params.blur_scale;

	for (float ang = 0.0; radius < params.blur_size; ang += GOLDEN_ANGLE) {

		vec2 suv = uv + vec2(cos(ang), sin(ang)) * pixel_size * radius;
		vec4 sample_color = texture(color_texture, suv);
		float sample_depth = get_depth_at_pos(suv);
		if (sample_depth > depth) {
			sample_color.a = clamp(sample_color.a, 0.0, size*2.0);
		}

		float m = smoothstep(radius-0.5, radius+0.5, sample_color.a);
		color += mix(color/accum, sample_color, m);
		accum += 1.0;
		radius += params.blur_scale/radius;
	}

	color /= accum;

	imageStore(bokeh_image,pos,color);
#endif

#ifdef MODE_COMPOSITE_BOKEH

	uv+=pixel_size * 0.5;
	vec4 color = imageLoad(color_image,pos);
	vec4 bokeh = texture(source_bokeh,uv);

	color.rgb = mix(color.rgb,bokeh.rgb,min(1.0,max(color.a,bokeh.a))); //blend between hires and lowres

	color.a=0; //reset alpha
	imageStore(color_image,pos,color);
#endif

}