Cleanup: style
[blender.git] / source / blender / draw / engines / workbench / shaders / workbench_cavity_frag.glsl
1 out vec4 fragColor;
2
3 uniform sampler2D depthBuffer;
4 uniform sampler2D colorBuffer;
5 uniform sampler2D normalBuffer;
6 uniform sampler2D positionBuffer;
7
8 uniform vec2 invertedViewportSize;
9 uniform mat4 WinMatrix; /* inverse WinMatrix */
10
11 uniform vec4 viewvecs[3];
12 uniform vec4 ssao_params;
13 uniform vec4 ssao_settings;
14 uniform sampler2D ssao_jitter;
15
16 layout(std140) uniform samples_block {
17         vec4 ssao_samples[500];
18 };
19
20 #define ssao_samples_num        ssao_params.x
21 #define jitter_tilling          ssao_params.yz
22 #define dfdy_sign                       ssao_params.w
23
24 #define ssao_distance           ssao_settings.x
25 #define ssao_factor_cavity      ssao_settings.y
26 #define ssao_factor_edge        ssao_settings.z
27 #define ssao_attenuation        ssao_settings.a
28
29 vec3 get_view_space_from_depth(in vec2 uvcoords, in float depth)
30 {
31         if (WinMatrix[3][3] == 0.0) {
32                 /* Perspective */
33                 float d = 2.0 * depth - 1.0;
34
35                 float zview = -WinMatrix[3][2] / (d + WinMatrix[2][2]);
36
37                 return zview * (viewvecs[0].xyz + vec3(uvcoords, 0.0) * viewvecs[1].xyz);
38         }
39         else {
40                 /* Orthographic */
41                 vec3 offset = vec3(uvcoords, depth);
42
43                 return viewvecs[0].xyz + offset * viewvecs[1].xyz;
44         }
45 }
46
47 /* forward declartion */
48 void ssao_factors(
49         in float depth, in vec3 normal, in vec3 position, in vec2 screenco,
50         out float cavities, out float edges);
51
52
53 void main()
54 {
55         vec2 screenco = vec2(gl_FragCoord.xy) * invertedViewportSize;
56         ivec2 texel = ivec2(gl_FragCoord.xy);
57
58         float depth = texelFetch(depthBuffer, texel, 0).x;
59         vec3 position = get_view_space_from_depth(screenco, depth);
60
61         vec4 diffuse_color = texelFetch(colorBuffer, texel, 0);
62         vec3 normal_viewport = normal_decode(texelFetch(normalBuffer, texel, 0).rg);
63         if (diffuse_color.a == 0.0) {
64                 normal_viewport = -normal_viewport;
65         }
66
67         float cavity = 0.0, edges = 0.0;
68         ssao_factors(depth, normal_viewport, position, screenco, cavity, edges);
69
70         fragColor = vec4(cavity, edges, 0.0, 1.0);
71 }