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

effect_dof_setup_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: 178ef46862b8463b76a957b02a55d2eabb383f4e (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

/**
 * Setup pass: CoC and luma aware downsample to half resolution of the input scene color buffer.
 *
 * An addition to the downsample CoC, we output the maximum slight out of focus CoC to be
 * sure we don't miss a pixel.
 */

#pragma BLENDER_REQUIRE(effect_dof_lib.glsl)

/* Full resolution. */
uniform sampler2D colorBuffer;
uniform sampler2D depthBuffer;

uniform float bokehMaxSize;

/* Half resolution. */
layout(location = 0) out vec4 outColor;
layout(location = 1) out vec2 outCoc; /* x: Downsample CoC, y: Max slight focus abs CoC */

void main()
{
  vec2 fullres_texel_size = 1.0 / vec2(textureSize(colorBuffer, 0).xy);
  /* Center uv around the 4 fullres pixels. */
  vec2 quad_center = (floor(gl_FragCoord.xy) * 2.0 + 1.0) * fullres_texel_size;

  vec4 colors[4];
  vec4 depths;
  for (int i = 0; i < 4; i++) {
    vec2 sample_uv = quad_center + quad_offsets[i] * fullres_texel_size;
    colors[i] = safe_color(textureLod(colorBuffer, sample_uv, 0.0));
    depths[i] = textureLod(depthBuffer, sample_uv, 0.0).r;
  }

  vec4 cocs = dof_coc_from_zdepth(depths);

  cocs = clamp(cocs, -bokehMaxSize, bokehMaxSize);

  vec4 weights = dof_downsample_bilateral_coc_weights(cocs);
  weights *= dof_downsample_bilateral_color_weights(colors);
  /* Normalize so that the sum is 1. */
  weights *= safe_rcp(sum(weights));

  outColor = weighted_sum_array(colors, weights);
  outCoc.x = dot(cocs, weights);

  /* Max slight focus abs CoC. */

  /* Clamp to 0.5 if full in defocus to differentiate full focus tiles with coc == 0.0.
   * This enables an optimization in the resolve pass. */
  const vec4 threshold = vec4(layer_threshold + layer_offset);
  cocs = abs(cocs);
  bvec4 defocus = greaterThan(cocs, threshold);
  bvec4 focus = lessThanEqual(cocs, vec4(0.5));
  if (any(defocus) && any(focus)) {
    /* For the same reason as in the flatten pass. This is a case we cannot optimize for. */
    cocs = select(cocs, vec4(DOF_TILE_MIXED), focus);
    cocs = select(cocs, vec4(DOF_TILE_MIXED), defocus);
  }
  else {
    cocs = select(cocs, vec4(DOF_TILE_FOCUS), focus);
    cocs = select(cocs, vec4(DOF_TILE_DEFOCUS), defocus);
  }
  outCoc.y = max_v4(cocs);
}