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

overlay_outline_detect_frag.glsl « shaders « overlay « engines « draw « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 18914b0276fcc2a2bf9fb2e97385bf189de73cd8 (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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360

#pragma BLENDER_REQUIRE(common_view_lib.glsl)

#define XPOS (1 << 0)
#define XNEG (1 << 1)
#define YPOS (1 << 2)
#define YNEG (1 << 3)

#define ALL (XPOS | XNEG | YPOS | YNEG)
#define NONE 0

#define DIAG_XNEG_YPOS (XNEG | YPOS)
#define DIAG_XPOS_YPOS (XPOS | YPOS)
#define DIAG_XPOS_YNEG (XPOS | YNEG)
#define DIAG_XNEG_YNEG (XNEG | YNEG)

#define APEX_XPOS (ALL & (~XPOS))
#define APEX_XNEG (ALL & (~XNEG))
#define APEX_YPOS (ALL & (~YPOS))
#define APEX_YNEG (ALL & (~YNEG))

bool has_edge(uint id, vec2 uv, uint ref, inout uint ref_col, inout vec2 depth_uv)
{
  if (ref_col == 0u) {
    /* Make outline bleed on the background. */
    ref_col = id;
    depth_uv = uv;
  }
  return (id != ref);
}

/* A gather4 + check against ref. */
bvec4 gather_edges(vec2 uv, uint ref)
{
  uvec4 ids;
#ifdef GPU_ARB_texture_gather
  ids = textureGather(outlineId, uv);
#else
  vec3 ofs = vec3(0.5, 0.5, -0.5) * sizeViewportInv.xyy;
  ids.x = textureLod(outlineId, uv - ofs.xz, 0.0).r;
  ids.y = textureLod(outlineId, uv + ofs.xy, 0.0).r;
  ids.z = textureLod(outlineId, uv + ofs.xz, 0.0).r;
  ids.w = textureLod(outlineId, uv - ofs.xy, 0.0).r;
#endif

  return notEqual(ids, uvec4(ref));
}

/* Clockwise */
vec2 rotate_90(vec2 v)
{
  return vec2(v.y, -v.x);
}
vec2 rotate_180(vec2 v)
{
  return vec2(-v.x, -v.y);
}
vec2 rotate_270(vec2 v)
{
  return vec2(-v.y, v.x);
}

/* Counter-Clockwise */
bvec4 rotate_90(bvec4 v)
{
  return v.yzwx;
}
bvec4 rotate_180(bvec4 v)
{
  return v.zwxy;
}
bvec4 rotate_270(bvec4 v)
{
  return v.wxyz;
}

/* Apply offset to line endpoint based on surrounding edges infos. */
bool line_offset(bvec2 edges, vec2 ofs, inout vec2 line_point)
{
  if (all(edges.xy)) {
    line_point.y -= ofs.y;
  }
  else if (!edges.x) {
    line_point.y += ofs.y;
  }
  else /* !edges.y */ {
    line_point.x += ofs.x;
    return true;
  }
  return false;
}

/* Changes Antialiasing pattern and makes line thicker. 0.0 is thin. */
#define PROXIMITY_OFS -0.35

/* Use surrounding edges to approximate the outline direction to create smooth lines. */
void straight_line_dir(bvec4 edges1, bvec4 edges2, out vec2 line_start, out vec2 line_end)
{
  /* Y_POS as reference. Other cases are rotated to match reference. */
  line_end = vec2(1.5, 0.5 + PROXIMITY_OFS);
  line_start = vec2(-line_end.x, line_end.y);

  vec2 line_ofs = vec2(1.0, 0.5);
  if (line_offset(edges1.xw, line_ofs, line_end)) {
    line_offset(edges1.yz, line_ofs, line_end);
  }
  line_ofs = vec2(-line_ofs.x, line_ofs.y);
  if (line_offset(edges2.yz, line_ofs, line_start)) {
    line_offset(edges2.xw, line_ofs, line_start);
  }
}

vec2 diag_offset(bvec4 edges)
{
  /* X_NEG | Y_POS as reference. Other cases are rotated to match reference.
   * So the line is coming from bottom left. */
  if (all(edges.wz)) {
    /* Horizontal line. */
    return vec2(2.5, 0.5);
  }
  else if (all(not(edges.xw))) {
    /* Vertical line. */
    return vec2(0.5, 2.5);
  }
  else if (edges.w) {
    /* Less horizontal Line. */
    return vec2(2.5, 0.5);
  }
  else {
    /* Less vertical Line. */
    return vec2(0.5, 2.5);
  }
}

/* Compute line direction vector from the bottom left corner. */
void diag_dir(bvec4 edges1, bvec4 edges2, out vec2 line_start, out vec2 line_end)
{
  /* Negate instead of rotating back the result of diag_offset. */
  edges2 = not(edges2);
  edges2 = rotate_180(edges2);
  line_end = diag_offset(edges1);
  line_end += diag_offset(edges2);

  if (line_end.x == line_end.y) {
    /* Perfect diagonal line. Push line start towards edge. */
    line_start = vec2(-1.0, 1.0) * PROXIMITY_OFS * 0.4;
  }
  else if (line_end.x > line_end.y) {
    /* Horizontal Line. Lower line start. */
    line_start = vec2(0.0, PROXIMITY_OFS);
  }
  else {
    /* Vertical Line. Push line start to the right. */
    line_start = -vec2(PROXIMITY_OFS, 0.0);
  }
  line_end += line_start;
}

void main()
{
  uint ref = textureLod(outlineId, uvcoordsvar.xy, 0.0).r;
  uint ref_col = ref;

  vec2 uvs = gl_FragCoord.xy * sizeViewportInv;
  vec3 ofs = vec3(sizeViewportInv.xy, 0.0);

  vec2 depth_uv = uvs;

  uvec4 ids;
#ifdef GPU_ARB_texture_gather
  /* Reminder: Samples order is CW starting from top left. */
  uvec2 tmp1, tmp2, tmp3, tmp4;
  if (doThickOutlines) {
    tmp1 = textureGather(outlineId, uvs + ofs.xy * vec2(1.5, -0.5)).xy;
    tmp2 = textureGather(outlineId, uvs + ofs.xy * vec2(-1.5, -0.5)).yx;
    tmp3 = textureGather(outlineId, uvs + ofs.xy * vec2(0.5, 1.5)).wx;
    tmp4 = textureGather(outlineId, uvs + ofs.xy * vec2(0.5, -1.5)).xw;
    ids.x = tmp1.x;
    ids.y = tmp2.x;
    ids.z = tmp3.x;
    ids.w = tmp4.x;
  }
  else {
    ids.xz = textureGather(outlineId, uvs + ofs.xy * 0.5).zx;
    ids.yw = textureGather(outlineId, uvs - ofs.xy * 0.5).xz;
  }
#else
  ids.x = textureLod(outlineId, uvs + ofs.xz, 0.0).r;
  ids.y = textureLod(outlineId, uvs - ofs.xz, 0.0).r;
  ids.z = textureLod(outlineId, uvs + ofs.zy, 0.0).r;
  ids.w = textureLod(outlineId, uvs - ofs.zy, 0.0).r;
#endif

  bool has_edge_pos_x = has_edge(ids.x, uvs + ofs.xz, ref, ref_col, depth_uv);
  bool has_edge_neg_x = has_edge(ids.y, uvs - ofs.xz, ref, ref_col, depth_uv);
  bool has_edge_pos_y = has_edge(ids.z, uvs + ofs.zy, ref, ref_col, depth_uv);
  bool has_edge_neg_y = has_edge(ids.w, uvs - ofs.zy, ref, ref_col, depth_uv);

  if (doThickOutlines) {
    if (!any(bvec4(has_edge_pos_x, has_edge_neg_x, has_edge_pos_y, has_edge_neg_y))) {
#ifdef GPU_ARB_texture_gather
      ids.x = tmp1.y;
      ids.y = tmp2.y;
      ids.z = tmp3.y;
      ids.w = tmp4.y;
#else
      ids.x = textureLod(outlineId, uvs + 2.0 * ofs.xz, 0.0).r;
      ids.y = textureLod(outlineId, uvs - 2.0 * ofs.xz, 0.0).r;
      ids.z = textureLod(outlineId, uvs + 2.0 * ofs.zy, 0.0).r;
      ids.w = textureLod(outlineId, uvs - 2.0 * ofs.zy, 0.0).r;
#endif

      has_edge_pos_x = has_edge(ids.x, uvs + 2.0 * ofs.xz, ref, ref_col, depth_uv);
      has_edge_neg_x = has_edge(ids.y, uvs - 2.0 * ofs.xz, ref, ref_col, depth_uv);
      has_edge_pos_y = has_edge(ids.z, uvs + 2.0 * ofs.zy, ref, ref_col, depth_uv);
      has_edge_neg_y = has_edge(ids.w, uvs - 2.0 * ofs.zy, ref, ref_col, depth_uv);
    }
  }

  if (isXrayWires) {
    /* Don't inflate the wire outlines too much. */
    has_edge_neg_x = has_edge_neg_y = false;
  }

  /* WATCH: Keep in sync with outlineId of the prepass. */
  uint color_id = ref_col >> 14u;
  if (ref_col == 0u) {
    fragColor = vec4(0.0);
  }
  else if (color_id == 1u) {
    fragColor = colorSelect;
  }
  else if (color_id == 3u) {
    fragColor = colorActive;
  }
  else {
    fragColor = colorTransform;
  }

  float ref_depth = textureLod(outlineDepth, depth_uv, 0.0).r;
  float scene_depth = textureLod(sceneDepth, depth_uv, 0.0).r;

  /* Avoid bad cases of zfighting for occlusion only. */
  const float epsilon = 3.0 / 8388608.0;
  bool occluded = (ref_depth > scene_depth + epsilon);

  /* NOTE: We never set alpha to 1.0 to avoid Antialiasing destroying the line. */
  fragColor *= (occluded ? alphaOcclu : 1.0) * (254.0 / 255.0);

  int edge_case = 0;
  edge_case += int(has_edge_pos_x) * XPOS;
  edge_case += int(has_edge_neg_x) * XNEG;
  edge_case += int(has_edge_pos_y) * YPOS;
  edge_case += int(has_edge_neg_y) * YNEG;

  if (edge_case == ALL || edge_case == NONE) {
    discard;
  }

  if (!doAntiAliasing) {
    lineOutput = vec4(0.0);
    return;
  }

  vec2 line_start, line_end;
  vec2 line_ofs;
  bvec4 extra_edges, extra_edges2;
  /* TODO: simplify this branching hell. */
  switch (edge_case) {
      /* Straight lines. */
    case YPOS:
      extra_edges = gather_edges(uvs + sizeViewportInv * vec2(2.5, 0.5), ref);
      extra_edges2 = gather_edges(uvs + sizeViewportInv * vec2(-2.5, 0.5), ref);
      straight_line_dir(extra_edges, extra_edges2, line_start, line_end);
      break;
    case YNEG:
      extra_edges = gather_edges(uvs + sizeViewportInv * vec2(-2.5, -0.5), ref);
      extra_edges2 = gather_edges(uvs + sizeViewportInv * vec2(2.5, -0.5), ref);
      extra_edges = rotate_180(extra_edges);
      extra_edges2 = rotate_180(extra_edges2);
      straight_line_dir(extra_edges, extra_edges2, line_start, line_end);
      line_start = rotate_180(line_start);
      line_end = rotate_180(line_end);
      break;
    case XPOS:
      extra_edges = gather_edges(uvs + sizeViewportInv * vec2(0.5, 2.5), ref);
      extra_edges2 = gather_edges(uvs + sizeViewportInv * vec2(0.5, -2.5), ref);
      extra_edges = rotate_90(extra_edges);
      extra_edges2 = rotate_90(extra_edges2);
      straight_line_dir(extra_edges, extra_edges2, line_start, line_end);
      line_start = rotate_90(line_start);
      line_end = rotate_90(line_end);
      break;
    case XNEG:
      extra_edges = gather_edges(uvs + sizeViewportInv * vec2(-0.5, 2.5), ref);
      extra_edges2 = gather_edges(uvs + sizeViewportInv * vec2(-0.5, -2.5), ref);
      extra_edges = rotate_270(extra_edges);
      extra_edges2 = rotate_270(extra_edges2);
      straight_line_dir(extra_edges, extra_edges2, line_start, line_end);
      line_start = rotate_270(line_start);
      line_end = rotate_270(line_end);
      break;

      /* Diagonal */
    case DIAG_XNEG_YPOS:
      extra_edges = gather_edges(uvs + ofs.xy * vec2(1.5), ref);
      extra_edges2 = gather_edges(uvs + ofs.xy * vec2(-1.5), ref);
      diag_dir(extra_edges, extra_edges2, line_start, line_end);
      break;
    case DIAG_XPOS_YNEG:
      extra_edges = gather_edges(uvs - ofs.xy * vec2(1.5), ref);
      extra_edges2 = gather_edges(uvs - ofs.xy * vec2(-1.5), ref);
      extra_edges = rotate_180(extra_edges);
      extra_edges2 = rotate_180(extra_edges2);
      diag_dir(extra_edges, extra_edges2, line_start, line_end);
      line_start = rotate_180(line_start);
      line_end = rotate_180(line_end);
      break;
    case DIAG_XPOS_YPOS:
      extra_edges = gather_edges(uvs + ofs.xy * vec2(1.5, -1.5), ref);
      extra_edges2 = gather_edges(uvs - ofs.xy * vec2(1.5, -1.5), ref);
      extra_edges = rotate_90(extra_edges);
      extra_edges2 = rotate_90(extra_edges2);
      diag_dir(extra_edges, extra_edges2, line_start, line_end);
      line_start = rotate_90(line_start);
      line_end = rotate_90(line_end);
      break;
    case DIAG_XNEG_YNEG:
      extra_edges = gather_edges(uvs - ofs.xy * vec2(1.5, -1.5), ref);
      extra_edges2 = gather_edges(uvs + ofs.xy * vec2(1.5, -1.5), ref);
      extra_edges = rotate_270(extra_edges);
      extra_edges2 = rotate_270(extra_edges2);
      diag_dir(extra_edges, extra_edges2, line_start, line_end);
      line_start = rotate_270(line_start);
      line_end = rotate_270(line_end);
      break;

      /* Apex */
    case APEX_XPOS:
    case APEX_XNEG:
      line_start = vec2(-0.5, 0.0);
      line_end = vec2(0.5, 0.0);
      break;
    case APEX_YPOS:
    case APEX_YNEG:
      line_start = vec2(0.0, -0.5);
      line_end = vec2(0.0, 0.5);
      break;
    default:
      /* Ensure values are assigned to, avoids undefined behavior for
       * divergent control-flow. This can occur if discard is called
       * as discard is not treated as a return in Metal 2.2. So
       * side-effects can still cause problems. */
      line_start = vec2(0.0);
      line_end = vec2(0.0);
      break;
  }

  lineOutput = pack_line_data(vec2(0.0), line_start, line_end);
}