Blender V5.3
eevee_transparency.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
18
19#pragma once
20
22#include "gpu_shader_compat.hh"
23
25
29float alpha_threshold(float hash_scale, float hash_offset, float3 P)
30{
31 /* Find the discretized derivatives of our coordinates. */
32 float max_deriv = max(length(gpu_dfdx(P)), length(gpu_dfdy(P)));
33 float pix_scale = 1.0f / (hash_scale * max_deriv);
34 /* Find two nearest log-discretized noise scales. */
35 float pix_scale_log = log2(pix_scale);
36 float2 pix_scales;
37 pix_scales.x = exp2(floor(pix_scale_log));
38 pix_scales.y = exp2(ceil(pix_scale_log));
39 /* Compute alpha thresholds at our two noise scales. */
40 float2 alpha;
41 alpha.x = random::pcg(floor(pix_scales.x * P));
42 alpha.y = random::pcg(floor(pix_scales.y * P));
43 /* Factor to interpolate lerp with. */
44 float fac = fract(log2(pix_scale));
45 /* Interpolate alpha threshold from noise at two scales. */
46 float x = mix(alpha.x, alpha.y, fac);
47 /* Pass into CDF to compute uniformly distributed threshold. */
48 float a = min(fac, 1.0f - fac);
49 float one_a = 1.0f - a;
50 float denom = 1.0f / (2 * a * one_a);
51 float one_x = (1 - x);
52 float3 cases = float3((x * x) * denom, (x - 0.5f * a) / one_a, 1.0f - (one_x * one_x * denom));
53 /* Find our final, uniformly distributed alpha threshold. */
54 float threshold = (x < one_a) ? ((x < a) ? cases.x : cases.y) : cases.z;
55 /* Jitter the threshold for TAA accumulation. */
56 threshold = fract(threshold + hash_offset);
57 /* Avoids threshold == 0. */
58 threshold = clamp(threshold, 1.0e-6f, 1.0f);
59 return threshold;
60}
61
62} // namespace eevee::hashed_transparency
btScalar max
#define exp2
T fract(T) RET
T gpu_dfdx(T) RET
#define log2
#define floor
#define ceil
T gpu_dfdy(T) RET
constexpr T clamp(T, U, U) RET
float length(VecOp< float, D >) RET
float alpha_threshold(float hash_scale, float hash_offset, float3 P)
float pcg(float v)
#define mix
#define min(a, b)
Definition sort.cc:38
float x
float y
float z
Definition sky_math.h:136
float y
Definition sky_math.h:136
float x
Definition sky_math.h:136