Blender V5.3
eevee_subsurface.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2021 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include <algorithm>
10
11#include "eevee_instance.hh"
12#include "eevee_subsurface.hh"
13
14namespace blender::eevee {
15
16/* -------------------------------------------------------------------- */
19
21{
22 data_.sample_len = 16;
23
24 if (!(inst_.pipelines.deferred.closure_bits_get() & CLOSURE_SSS)) {
25 return;
26 }
27
28 /* Process direct / indirect radiance separately for correct light-path intensity. */
29 /* Note that we do not branch on clamp.surface_indirect since it is applied on the spherical
30 * harmonics or rays before SSS evaluation. */
31 use_split_radiance_ = inst_.uniform_data.data.clamp.direct_scale != 1.0f ||
32 inst_.uniform_data.data.clamp.indirect_scale != 1.0f ||
33 inst_.scene->eevee.clamp_surface_direct != 0.0f;
34
35 {
36 gpu::Shader *sh = inst_.shaders.static_shader_get(SUBSURFACE_SETUP);
37 PassSimple &pass = setup_ps_;
38 pass.init();
40 pass.specialize_constant(sh, "use_split_radiance", use_split_radiance_);
41 pass.shader_set(sh);
42 pass.bind_resources(inst_.gbuffer);
43 pass.bind_texture("depth_tx", &inst_.render_buffers.depth_tx);
44 pass.bind_image("direct_light_img", &direct_light_tx_);
45 pass.bind_image("indirect_light_img", &indirect_light_tx_);
46 pass.bind_image("object_id_img", &object_id_tx_);
47 pass.bind_image("radiance_img", &radiance_tx_);
48 pass.bind_ssbo("convolve_tile_buf", &convolve_tile_buf_);
49 pass.bind_ssbo("convolve_dispatch_buf", &convolve_dispatch_buf_);
51 pass.dispatch(&setup_dispatch_size_);
52 }
53 {
54 /* Clamping to border color allows to always load ID 0 for out of view samples and discard
55 * their influence. Also disable filtering to avoid light bleeding between different objects
56 * and loading invalid interpolated IDs. */
62
63 gpu::Shader *sh = inst_.shaders.static_shader_get(SUBSURFACE_CONVOLVE);
64
65 PassSimple &pass = convolve_ps_;
66 pass.init();
68 pass.specialize_constant(sh, "use_split_radiance", use_split_radiance_);
69 pass.shader_set(sh);
70 pass.bind_resources(inst_.uniform_data);
71 pass.bind_ubo(SUBSURFACE_BUF_SLOT, data_);
72 pass.bind_resources(inst_.gbuffer);
73 pass.bind_texture("radiance_tx", &radiance_tx_, sampler);
74 pass.bind_texture("depth_tx", &inst_.render_buffers.depth_tx, sampler);
75 pass.bind_texture("object_id_tx", &object_id_tx_, sampler);
76 pass.bind_image("out_direct_light_img", &direct_light_tx_);
77 pass.bind_image("out_indirect_light_img", &indirect_light_tx_);
78 pass.bind_ssbo("tiles_coord_buf", &convolve_tile_buf_);
80 pass.dispatch(convolve_dispatch_buf_);
81 }
82}
83
84void SubsurfaceModule::render(gpu::Texture *direct_diffuse_light_tx,
85 gpu::Texture *indirect_diffuse_light_tx,
86 eClosureBits active_closures,
87 View &view)
88{
89 if (!(active_closures & CLOSURE_SSS)) {
90 return;
91 }
92
93 /* TODO: This only needs to be update once per render sample. */
94 precompute_samples_location();
95 data_.push_update();
96
97 int2 render_extent = inst_.film.render_extent_get();
98 setup_dispatch_size_ = int3(math::divide_ceil(render_extent, int2(SUBSURFACE_GROUP_SIZE)), 1);
99
100 const int convolve_tile_count = setup_dispatch_size_.x * setup_dispatch_size_.y;
101 convolve_tile_buf_.resize(ceil_to_multiple_u(convolve_tile_count, 512));
102
103 direct_light_tx_ = direct_diffuse_light_tx;
104 indirect_light_tx_ = indirect_diffuse_light_tx;
105
107 object_id_tx_.acquire_2d(render_extent, gpu::TextureFormat::SUBSURFACE_OBJECT_ID_FORMAT, usage);
108 radiance_tx_.acquire_2d_array(render_extent,
109 use_split_radiance_ ? 2 : 1,
110 gpu::TextureFormat::SUBSURFACE_RADIANCE_FORMAT,
111 usage);
112
113 convolve_dispatch_buf_.clear_to_zero();
114
115 inst_.manager->submit(setup_ps_, view);
116 inst_.manager->submit(convolve_ps_, view);
117
118 object_id_tx_.release();
119 radiance_tx_.release();
120}
121
122void SubsurfaceModule::precompute_samples_location()
123{
124 /* Precompute sample position with white albedo. */
125 float d = burley_setup(float3(1.0f), float3(1.0f)).x;
126
127 float rand_u = inst_.sampling.rng_get(SAMPLING_SSS_U);
128 float rand_v = inst_.sampling.rng_get(SAMPLING_SSS_V);
129
130 /* Find minimum radius that we can represent because we are only sampling the largest radius. */
131 data_.min_radius = 1.0f;
132
133 double golden_angle = M_PI * (3.0 - sqrt(5.0));
134 for (auto i : IndexRange(data_.sample_len)) {
135 float theta = golden_angle * i + M_PI * 2.0f * rand_u;
136 float x = (rand_v + i) / data_.sample_len;
137 float r = SubsurfaceModule::burley_sample(d, x);
138 data_.min_radius = min_ff(data_.min_radius, r);
139 data_.samples[i].x = cosf(theta) * r;
140 data_.samples[i].y = sinf(theta) * r;
141 data_.samples[i].z = 1.0f / burley_pdf(d, r);
142 }
143 /* Avoid float imprecision. */
144 data_.min_radius = max_ff(data_.min_radius, 1e-4f);
145}
146
148
149/* -------------------------------------------------------------------- */
156
157float SubsurfaceModule::burley_sample(float d, float x_rand)
158{
159 x_rand *= SSS_BURLEY_TRUNCATE_CDF;
160
161 const float tolerance = 1e-6;
162 const int max_iteration_count = 10;
163 /* Do initial guess based on manual curve fitting, this allows us to reduce
164 * number of iterations to maximum 4 across the [0..1] range. We keep maximum
165 * number of iteration higher just to be sure we didn't miss root in some
166 * corner case.
167 */
168 float r;
169 if (x_rand <= 0.9) {
170 r = exp(x_rand * x_rand * 2.4) - 1.0;
171 }
172 else {
173 /* TODO(sergey): Some nicer curve fit is possible here. */
174 r = 15.0;
175 }
176 /* Solve against scaled radius. */
177 for (int i = 0; i < max_iteration_count; i++) {
178 float exp_r_3 = exp(-r / 3.0);
179 float exp_r = exp_r_3 * exp_r_3 * exp_r_3;
180 float f = 1.0 - 0.25 * exp_r - 0.75 * exp_r_3 - x_rand;
181 float f_ = 0.25 * exp_r + 0.25 * exp_r_3;
182
183 if (abs(f) < tolerance || f_ == 0.0) {
184 break;
185 }
186
187 r = r - f / f_;
188 r = std::max<double>(r, 0.0);
189 }
190
191 return r * d;
192}
193
194float SubsurfaceModule::burley_eval(float d, float r)
195{
196 if (r >= SSS_BURLEY_TRUNCATE * d) {
197 return 0.0;
198 }
199 /* Slide 33. */
200 float exp_r_3_d = expf(-r / (3.0f * d));
201 float exp_r_d = exp_r_3_d * exp_r_3_d * exp_r_3_d;
202 return (exp_r_d + exp_r_3_d) / (8.0f * float(M_PI) * d);
203}
204
205float SubsurfaceModule::burley_pdf(float d, float r)
206{
207 return burley_eval(d, r) / SSS_BURLEY_TRUNCATE_CDF;
208}
209
211
212} // namespace blender::eevee
#define M_PI
constexpr IndexRange()=default
void bind_resources(U &resources)
Definition draw_pass.hh:459
void shader_set(gpu::Shader *shader)
void bind_texture(const char *name, gpu::Texture *texture, GPUSamplerState state=sampler_auto)
void specialize_constant(gpu::Shader *shader, const char *name, const float &data)
void bind_image(const char *name, gpu::Texture *image)
void dispatch(int group_len)
void state_set(DRWState state, int clip_plane_count=0)
void barrier(GPUBarrier type)
void bind_ubo(const char *name, gpu::UniformBuf *buffer)
void bind_ssbo(const char *name, gpu::StorageBuf *buffer)
float rng_get(eSamplingDimension dimension) const
#define expf(x)
#define SUBSURFACE_BUF_SLOT
#define sampler(slot)
#define exp
#define abs
detail::Pass< command::DrawCommandBuf > PassSimple
VecBase< T, Size > divide_ceil(const VecBase< T, Size > &a, const VecBase< T, Size > &b)
MINLINE float min_ff(float a, float b)
@ GPU_SAMPLER_EXTEND_MODE_CLAMP_TO_BORDER
@ GPU_SAMPLER_FILTERING_DEFAULT
VecBase< int32_t, 2 > int2
VecBase< int32_t, 3 > int3
@ GPU_TEXTURE_USAGE_SHADER_READ
@ GPU_TEXTURE_USAGE_SHADER_WRITE
sqrt(x)+1/max(0
@ GPU_BARRIER_TEXTURE_FETCH
Definition GPU_state.hh:40
@ GPU_BARRIER_SHADER_STORAGE
Definition GPU_state.hh:51
@ GPU_BARRIER_SHADER_IMAGE_ACCESS
Definition GPU_state.hh:38
@ DRW_STATE_NO_DRAW
Definition draw_state.hh:29
@ GPU_SAMPLER_CUSTOM_COMPARE
MINLINE float max_ff(float a, float b)
@ GPU_SAMPLER_STATE_TYPE_PARAMETERS
MINLINE uint ceil_to_multiple_u(uint a, uint b)
VecBase< float, 3 > float3
ATTR_WARN_UNUSED_RESULT const BMVert const BMEdge * e
#define sinf
#define cosf
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
void render(gpu::Texture *direct_diffuse_light_tx, gpu::Texture *indirect_diffuse_light_tx, eClosureBits active_closures, View &view)