R2 Core Shaders

R2BilateralBlur.h
Go to the documentation of this file.
1 #ifndef R2_BILATERAL_BLUR_H
2 #define R2_BILATERAL_BLUR_H
3
4 /// \file R2BilateralBlur.h
5 /// \brief Functions for performing bilateral blurs.
6
7 #include "R2LogDepth.h"
8
9 /// Parameters for the blur effect
10
11 struct R2_bilateral_blur_depth_aware_t {
12 /// The depth coefficient that was used to produce the scene's depth values
13 float depth_coefficient;
14 /// The blur radius in texels (typically between 3 and 7)
15 float radius;
16 /// The blur sharpness value (typically 16)
17 float sharpness;
18 /// The blur falloff value
19 float falloff;
20 /// The inverse of the output image size: `(1 / width, 1 / height)`
21 vec2 output_image_size_inverse;
22 };
23
24 vec4
25 R2_bilateralBlurDepthAwareSample4f(
26 const sampler2D t,
27 const sampler2D d,
28 const R2_bilateral_blur_depth_aware_t blur,
29 const vec2 uv,
30 const float radius,
31 const vec4 center_val,
32 const float center_depth,
33 inout float weight_total)
34 {
35 vec4 next_val =
36 textureLod (t, uv, 0.0);
37 float next_depth =
38 R2_logDepthDecode (textureLod (d, uv, 0.0).x, blur.depth_coefficient);
39
40 float falloff =
41 -radius * radius * blur.falloff;
42 float depth_diff =
43 next_depth - center_depth;
44 float sharpness =
45 depth_diff * depth_diff * blur.sharpness;
46
47 float weight = exp (falloff - sharpness);
48 weight_total += weight;
49
50 return weight * next_val;
51 }
52
53 ///
54 /// Perform a single depth-aware horizontal blur sample for texel \a uv in \a t.
55 ///
56 /// The texture `d` is assumed to hold logarithmicaly-encoded depth samples,
57 /// and the values contained within will be used to determine how much each
58 /// sample contributes to the blur effect.
59 ///
60 /// @param t A texture
61 /// @param uv The coordinates of the current texel
62 ///
63
64 vec4
65 R2_bilateralBlurDepthAwareHorizontal4f(
66 const sampler2D t,
67 const sampler2D d,
68 const R2_bilateral_blur_depth_aware_t blur,
69 const vec2 uv)
70 {
71 float weight_total = 0.0;
72
73 vec4 center_val =
74 textureLod (t, uv, 0.0);
75 float center_depth =
76 R2_logDepthDecode (textureLod (d, uv, 0.0).x, blur.depth_coefficient);
77
78 vec4 sum = vec4 (0.0);
79 for (float r = -blur.radius; r <= blur.radius; ++r)
80 {
81 vec2 uv_off =
82 uv + vec2 (r * blur.output_image_size_inverse.x, 0);
83 sum += R2_bilateralBlurDepthAwareSample4f(
84 t, d, blur, uv_off, r, center_val, center_depth, weight_total);
85 }
86
87 return sum / weight_total;
88 }
89
90 ///
91 /// Perform a single depth-aware vertical blur sample for texel \a uv in \a t.
92 ///
93 /// The texture `d` is assumed to hold logarithmicaly-encoded depth samples,
94 /// and the values contained within will be used to determine how much each
95 /// sample contributes to the blur effect.
96 ///
97 /// @param t A texture
98 /// @param uv The coordinates of the current texel
99 ///
100
101 vec4
102 R2_bilateralBlurDepthAwareVertical4f(
103 const sampler2D t,
104 const sampler2D d,
105 const R2_bilateral_blur_depth_aware_t blur,
106 const vec2 uv)
107 {
108 float weight_total = 0.0;
109
110 vec4 center_val =
111 textureLod (t, uv, 0.0);
112 float center_depth =
113 R2_logDepthDecode (textureLod (d, uv, 0.0).x, blur.depth_coefficient);
114
115 vec4 sum = vec4 (0.0);
116 for (float r = -blur.radius; r <= blur.radius; ++r)
117 {
118 vec2 uv_off =
119 uv + vec2 (0, r * blur.output_image_size_inverse.y);
120 sum += R2_bilateralBlurDepthAwareSample4f(
121 t, d, blur, uv_off, r, center_val, center_depth, weight_total);
122 }
123
124 return sum / weight_total;
125 }
126
127 #endif // R2_BILATERAL_BLUR_H
R2_bilateral_blur_depth_aware_t::output_image_size_inverse
vec2 output_image_size_inverse
The inverse of the output image size: (1 / width, 1 / height)
Definition: R2BilateralBlur.h:21
R2_bilateral_blur_depth_aware_t::radius
float radius
The blur radius in texels (typically between 3 and 7)
Definition: R2BilateralBlur.h:15
R2_bilateralBlurDepthAwareHorizontal4f
vec4 R2_bilateralBlurDepthAwareHorizontal4f(const sampler2D t, const sampler2D d, const R2_bilateral_blur_depth_aware_t blur, const vec2 uv)
Definition: R2BilateralBlur.h:65
R2_bilateral_blur_depth_aware_t::depth_coefficient
float depth_coefficient
The depth coefficient that was used to produce the scene&#39;s depth values.
Definition: R2BilateralBlur.h:13
R2_bilateral_blur_depth_aware_t::sharpness
float sharpness
The blur sharpness value (typically 16)
Definition: R2BilateralBlur.h:17
R2_bilateral_blur_depth_aware_t::falloff
float falloff
The blur falloff value.
Definition: R2BilateralBlur.h:19
R2_bilateral_blur_depth_aware_t
Parameters for the blur effect.
Definition: R2BilateralBlur.h:11
R2_logDepthDecode
float R2_logDepthDecode(const float z, const float depth_coefficient)
Definition: R2LogDepth.h:72
R2_bilateralBlurDepthAwareVertical4f
vec4 R2_bilateralBlurDepthAwareVertical4f(const sampler2D t, const sampler2D d, const R2_bilateral_blur_depth_aware_t blur, const vec2 uv)
Definition: R2BilateralBlur.h:102
R2LogDepth.h
Logarithmic depth functions.