Blender V5.3
workbench_cavity.bsl.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2017-2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#pragma once
6
7#include "draw_view.bsl.hh"
8
10
11/* From The Alchemy screen-space ambient obscurance algorithm
12 * http://graphics.cs.williams.edu/papers/AlchemyHPG11/VV11AlchemyAO.pdf */
13
14namespace workbench {
15
16struct Cavity {
18 [[uniform(5)]] const float4 (&cavity_samples)[512];
19};
20
22 [[resource_table]] const workbench::World &world,
23 ViewMatrices view,
24 sampler2DDepth depth_tx,
25 sampler2D normal_tx,
26 float2 screenco,
27 float &cavities,
28 float &edges)
29{
30 cavities = edges = 0.0f;
31
32 float depth = texture(depth_tx, screenco).x;
33
34 /* Early out if background and in front. */
35 if (depth == 1.0f || depth == 0.0f) {
36 return;
37 }
38
39 const WorldData &world_data = world.world_data;
40
42 float3 normal = workbench::normal_decode(texture(normal_tx, screenco));
43
44 float2 jitter_co = (screenco * world_data.viewport_size.xy) * world_data.cavity_jitter_scale;
45 float3 noise = texture(cavity.jitter_tx, jitter_co).rgb;
46
47 /* find the offset in screen space by multiplying a point
48 * in camera space at the depth of the point by the projection matrix. */
49 float2 offset;
50 float homcoord = view.winmat[2][3] * position.z + view.winmat[3][3];
51 offset.x = view.winmat[0][0] * world_data.cavity_distance / homcoord;
52 offset.y = view.winmat[1][1] * world_data.cavity_distance / homcoord;
53 /* convert from -1...1 range to 0..1 for easy use with texture coordinates */
54 offset *= 0.5f;
55
56 /* NOTE: Putting noise usage here to put some ALU after texture fetch. */
57 float2 rotX = noise.rg;
58 float2 rotY = float2(-rotX.y, rotX.x);
59
60 int sample_start = world_data.cavity_sample_start;
61 int sample_end = world_data.cavity_sample_end;
62 for (int i = sample_start; i < sample_end && i < 512; i++) {
63 /* sample_coord.xy is sample direction (normalized).
64 * sample_coord.z is sample distance from disk center. */
65 float3 sample_coord = cavity.cavity_samples[i].xyz;
66 /* Rotate with random direction to get jittered result. */
67 float2 dir_jittered = float2(dot(sample_coord.xy, rotX), dot(sample_coord.xy, rotY));
68 dir_jittered.xy *= sample_coord.z + noise.b;
69
70 float2 uvcoords = screenco + dir_jittered * offset;
71 /* Out of screen case. */
72 if (any(greaterThan(abs(uvcoords - 0.5f), float2(0.5f)))) {
73 continue;
74 }
75 /* Sample depth. */
76 float s_depth = texture(depth_tx, uvcoords).r;
77 /* Handle Background case */
78 bool is_background = (s_depth == 1.0f);
79 /* This trick provide good edge effect even if no neighbor is found. */
80 s_depth = (is_background) ? depth : s_depth;
81 float3 s_pos = view.point_screen_to_view(float3(uvcoords, s_depth));
82
83 if (is_background) {
84 s_pos.z -= world_data.cavity_distance;
85 }
86
87 float3 dir = s_pos - position;
88 float len = length(dir);
89 float f_cavities = dot(dir, normal);
90 float f_edge = -f_cavities;
91 float f_bias = 0.05f * len + 0.0001f;
92
93 float attenuation = 1.0f / (len * (1.0f + len * len * world_data.cavity_attenuation));
94
95 /* use minor bias here to avoid self shadowing */
96 if (f_cavities > -f_bias) {
97 cavities += f_cavities * attenuation;
98 }
99
100 if (f_edge > f_bias) {
101 edges += f_edge * attenuation;
102 }
103 }
104 cavities *= world_data.cavity_sample_count_inv;
105 edges *= world_data.cavity_sample_count_inv;
106
107 /* don't let cavity wash out the surface appearance */
108 cavities = clamp(cavities * world_data.cavity_valley_factor, 0.0f, 1.0f);
109 edges = edges * world_data.cavity_ridge_factor;
110}
111
112} // namespace workbench
#define uniform(slot)
#define position
#define resource_table
#define sampler(slot)
bool any(VecOp< bool, D >) RET
VecBase< bool, D > greaterThan(VecOp< T, D >, VecOp< T, D >) RET
float dot(VecOp< float, D >, VecOp< float, D >) RET
#define abs
constexpr T clamp(T, U, U) RET
float length(VecOp< float, D >) RET
TEX_TEMPLATE DataVec texture(T, FltCoord, float=0.0f) RET
SamplerBase< float, 2, false, false, false, true > sampler2DDepth
SamplerBase< float, 2 > sampler2D
float noise(float p)
ccl_device_inline dual1 len(const ccl_private dual< T > &a)
Definition math_dual.h:263
float3 normal_decode(float4 enc)
void cavity_compute(const workbench::Cavity &cavity, const workbench::World &world, ViewMatrices view, sampler2DDepth depth_tx, sampler2D normal_tx, float2 screenco, float &cavities, float &edges)
float3 point_screen_to_view(float3 ssP) const
float x
float y
float z
Definition sky_math.h:136
const float4(& cavity_samples)[512]