Welcome to mirror list, hosted at ThFree Co, Russian Federation.

renderpass_postprocess_frag.glsl « shaders « eevee « engines « draw « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: eb6ca4b9de89f1c68d380c3444e2db8c31a145bc (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

#pragma BLENDER_REQUIRE(common_view_lib.glsl)
#pragma BLENDER_REQUIRE(common_math_geom_lib.glsl)

#define PASS_POST_UNDEFINED 0
#define PASS_POST_ACCUMULATED_COLOR 1
#define PASS_POST_ACCUMULATED_COLOR_ALPHA 2
#define PASS_POST_ACCUMULATED_LIGHT 3
#define PASS_POST_ACCUMULATED_VALUE 4
#define PASS_POST_DEPTH 5
#define PASS_POST_AO 6
#define PASS_POST_NORMAL 7
#define PASS_POST_TWO_LIGHT_BUFFERS 8

uniform int postProcessType;
uniform int currentSample;

uniform sampler2D depthBuffer;
uniform sampler2D inputBuffer;
uniform sampler2D inputSecondLightBuffer;
uniform sampler2D inputColorBuffer;

out vec4 fragColor;

vec3 safe_divide_even_color(vec3 a, vec3 b)
{
  vec3 result = vec3((b.r != 0.0) ? a.r / b.r : 0.0,
                     (b.g != 0.0) ? a.g / b.g : 0.0,
                     (b.b != 0.0) ? a.b / b.b : 0.0);
  /* try to get gray even if b is zero */
  if (b.r == 0.0) {
    if (b.g == 0.0) {
      result = result.bbb;
    }
    else if (b.b == 0.0) {
      result = result.ggg;
    }
    else {
      result.r = 0.5 * (result.g + result.b);
    }
  }
  else if (b.g == 0.0) {
    if (b.b == 0.0) {
      result = result.rrr;
    }
    else {
      result.g = 0.5 * (result.r + result.b);
    }
  }
  else if (b.b == 0.0) {
    result.b = 0.5 * (result.r + result.g);
  }

  return result;
}

void main()
{
  vec4 color = vec4(0.0, 0.0, 0.0, 1.0);
  ivec2 texel = ivec2(gl_FragCoord.xy);

  if (postProcessType == PASS_POST_DEPTH) {
    float depth = texelFetch(depthBuffer, texel, 0).r;
    if (depth == 1.0f) {
      depth = 1e10;
    }
    else {
      depth = -get_view_z_from_depth(depth);
    }
    color.rgb = vec3(depth);
  }
  else if (postProcessType == PASS_POST_AO) {
    float ao_accum = texelFetch(inputBuffer, texel, 0).r;
    color.rgb = vec3(min(1.0, ao_accum / currentSample));
  }
  else if (postProcessType == PASS_POST_NORMAL) {
    float depth = texelFetch(depthBuffer, texel, 0).r;
    vec2 encoded_normal = texelFetch(inputBuffer, texel, 0).rg;
    /* decode the normals only when they are valid. otherwise the result buffer will be filled
     * with NaN's */
    if (depth != 1.0 && any(notEqual(encoded_normal, vec2(0.0)))) {
      vec3 decoded_normal = normal_decode(texelFetch(inputBuffer, texel, 0).rg, vec3(0.0));
      vec3 world_normal = mat3(ViewMatrixInverse) * decoded_normal;
      color.rgb = world_normal;
    }
    else {
      color.rgb = vec3(0.0);
    }
  }
  else if (postProcessType == PASS_POST_ACCUMULATED_VALUE) {
    float accumulated_value = texelFetch(inputBuffer, texel, 0).r;
    color.rgb = vec3(accumulated_value / currentSample);
  }
  else if (postProcessType == PASS_POST_ACCUMULATED_COLOR) {
    vec3 accumulated_color = texelFetch(inputBuffer, texel, 0).rgb;
    color.rgb = (accumulated_color / currentSample);
  }
  else if (postProcessType == PASS_POST_ACCUMULATED_COLOR_ALPHA) {
    vec4 accumulated_color = texelFetch(inputBuffer, texel, 0);
    color = (accumulated_color / currentSample);
  }
  else if (postProcessType == PASS_POST_ACCUMULATED_LIGHT) {
    vec3 accumulated_light = texelFetch(inputBuffer, texel, 0).rgb;
    vec3 accumulated_color = texelFetch(inputColorBuffer, texel, 0).rgb;
    color.rgb = safe_divide_even_color(accumulated_light, accumulated_color);
  }
  else if (postProcessType == PASS_POST_TWO_LIGHT_BUFFERS) {
    vec3 accumulated_light = texelFetch(inputBuffer, texel, 0).rgb +
                             texelFetch(inputSecondLightBuffer, texel, 0).rgb;
    vec3 accumulated_color = texelFetch(inputColorBuffer, texel, 0).rgb;
    color.rgb = safe_divide_even_color(accumulated_light, accumulated_color);
  }
  else {
    /* Output error color: Unknown how to post process this pass. */
    color.rgb = vec3(1.0, 0.0, 1.0);
  }

  fragColor = color;
}