Blender V4.5
subsurface.h
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4
5#pragma once
6
10
16
18
19#ifdef __SUBSURFACE__
20
21ccl_device_inline bool subsurface_entry_bounce(KernelGlobals kg,
23 ccl_private ShaderData *sd,
24 ccl_private RNGState *rng_state,
26{
27 float2 rand_bsdf = path_state_rng_2D(kg, rng_state, PRNG_SUBSURFACE_BSDF);
28
30 /* CLOSURE_BSSRDF_RANDOM_WALK_SKIN_ID has a 50% chance to sample a diffuse entry bounce.
31 * Also, for the refractive entry, it uses a fixed roughness of 1.0. */
32 if (rand_bsdf.x < 0.5f) {
33 rand_bsdf.x *= 2.0f;
34 float pdf;
35 sample_cos_hemisphere(-bssrdf->N, rand_bsdf, wo, &pdf);
36 return true;
37 }
38 rand_bsdf.x = 2.0f * (rand_bsdf.x - 0.5f);
39 }
40
41 const float cos_NI = dot(bssrdf->N, sd->wi);
42 if (cos_NI <= 0.0f) {
43 return false;
44 }
45
46 float3 X;
47 float3 Y;
48 const float3 Z = bssrdf->N;
50
51 const float alpha = bssrdf->alpha;
52 const float neta = 1.0f / bssrdf->ior;
53
54 /* Sample microfacet normal by transforming to/from local coordinates. */
55 const float3 local_I = make_float3(dot(X, sd->wi), dot(Y, sd->wi), cos_NI);
56 const float3 local_H = microfacet_ggx_sample_vndf(local_I, alpha, alpha, rand_bsdf);
57 const float3 H = to_global(local_H, X, Y, Z);
58
59 const float cos_HI = dot(H, sd->wi);
60 const float arg = 1.0f - (sqr(neta) * (1.0f - sqr(cos_HI)));
61 /* We clamp subsurface IOR to be above 1, so there should never be TIR. */
62 kernel_assert(arg >= 0.0f);
63
64 const float dnp = max(sqrtf(arg), 1e-7f);
65 const float nK = (neta * cos_HI) - dnp;
66 *wo = -(neta * sd->wi) + (nK * H);
67 return true;
68 /* NOTE: For a proper refractive GGX interface, we should be computing lambdaI and lambdaO
69 * and multiplying the throughput by BSDF/pdf, which for VNDF sampling works out to
70 * `(1 + lambdaI) / (1 + lambdaI + lambdaO)`.
71 * However, this causes darkening due to the single-scattering approximation, which we'd
72 * then have to correct with a lookup table.
73 * Since we only really care about the directional distribution here, it's much easier to
74 * just skip all that instead. */
75}
76
77ccl_device int subsurface_bounce(KernelGlobals kg,
79 ccl_private ShaderData *sd,
80 const ccl_private ShaderClosure *sc)
81{
82 /* We should never have two consecutive BSSRDF bounces, the second one should
83 * be converted to a diffuse BSDF to avoid this. */
85
86 /* Setup path state for intersect_subsurface kernel. */
87 const ccl_private Bssrdf *bssrdf = (const ccl_private Bssrdf *)sc;
88
89 /* Setup ray into surface. */
90 INTEGRATOR_STATE_WRITE(state, ray, P) = sd->P;
91 INTEGRATOR_STATE_WRITE(state, ray, tmin) = 0.0f;
95
96 /* Advance random number offset for bounce. */
97 INTEGRATOR_STATE_WRITE(state, path, rng_offset) += PRNG_BOUNCE_NUM;
98
99 /* Compute weight, optionally including Fresnel from entry point. */
100 const Spectrum weight = surface_shader_bssrdf_sample_weight(sd, sc);
101 INTEGRATOR_STATE_WRITE(state, path, throughput) *= weight;
102
103 uint32_t path_flag = (INTEGRATOR_STATE(state, path, flag) & ~PATH_RAY_CAMERA);
104 if (sc->type == CLOSURE_BSSRDF_BURLEY_ID) {
105 path_flag |= PATH_RAY_SUBSURFACE_DISK;
106 INTEGRATOR_STATE_WRITE(state, subsurface, N) = sd->Ng;
107 }
108 else {
110
111 /* Sample entry bounce into the material. */
112 RNGState rng_state;
113 path_state_rng_load(state, &rng_state);
114 float3 wo;
115 if (!subsurface_entry_bounce(kg, bssrdf, sd, &rng_state, &wo) || dot(sd->Ng, wo) >= 0.0f) {
116 /* Sampling failed, give up on this bounce. */
117 return LABEL_NONE;
118 }
119 INTEGRATOR_STATE_WRITE(state, ray, D) = wo;
120 INTEGRATOR_STATE_WRITE(state, subsurface, N) = sd->N;
121 }
122
123 if (sd->flag & SD_BACKFACING) {
125 }
126
127 INTEGRATOR_STATE_WRITE(state, path, flag) = path_flag;
128
129 if (kernel_data.kernel_features & KERNEL_FEATURE_LIGHT_PASSES) {
130 if (INTEGRATOR_STATE(state, path, bounce) == 0) {
131 INTEGRATOR_STATE_WRITE(state, path, pass_diffuse_weight) = one_spectrum();
132 INTEGRATOR_STATE_WRITE(state, path, pass_glossy_weight) = zero_spectrum();
133 }
134 }
135
136 /* Pass BSSRDF parameters. */
137 INTEGRATOR_STATE_WRITE(state, subsurface, albedo) = bssrdf->albedo;
138 INTEGRATOR_STATE_WRITE(state, subsurface, radius) = bssrdf->radius;
139 INTEGRATOR_STATE_WRITE(state, subsurface, anisotropy) = bssrdf->anisotropy;
140
141 /* Path guiding. */
142 guiding_record_bssrdf_weight(kg, state, weight, bssrdf->albedo);
143
145}
146
147ccl_device void subsurface_shader_data_setup(KernelGlobals kg,
149 ccl_private ShaderData *sd,
150 const uint32_t path_flag)
151{
152 /* Get bump mapped normal from shader evaluation at exit point. */
153 float3 N = sd->N;
154 if (sd->flag & SD_HAS_BSSRDF_BUMP) {
155 N = surface_shader_bssrdf_normal(sd);
156 }
157
158 /* Setup diffuse BSDF at the exit point. This replaces shader_eval_surface. */
159 sd->flag &= ~SD_CLOSURE_FLAGS;
160 sd->num_closure = 0;
161 sd->num_closure_left = kernel_data.max_closures;
162
163 const Spectrum weight = one_spectrum();
164
166 sd, sizeof(DiffuseBsdf), weight);
167
168 if (bsdf) {
169 bsdf->N = N;
170 sd->flag |= bsdf_diffuse_setup(bsdf);
171 }
172}
173
174ccl_device_inline bool subsurface_scatter(KernelGlobals kg, IntegratorState state)
175{
176 RNGState rng_state;
177 path_state_rng_load(state, &rng_state);
178
181
183 if (!subsurface_random_walk(kg, state, rng_state, ray, ss_isect)) {
184 return false;
185 }
186 }
187 else {
188 if (!subsurface_disk(kg, state, rng_state, ray, ss_isect)) {
189 return false;
190 }
191 }
192
193# ifdef __VOLUME__
194 /* Update volume stack if needed. */
195 if (kernel_data.integrator.use_volumes) {
196 const int object = ss_isect.hits[0].object;
197 const int object_flag = kernel_data_fetch(object_flag, object);
198
199 if (object_flag & SD_OBJECT_INTERSECTS_VOLUME) {
200 const float3 P = INTEGRATOR_STATE(state, ray, P);
201
203 }
204 }
205# endif /* __VOLUME__ */
206
207 /* Pretend ray is coming from the outside towards the exit point. This ensures
208 * correct front/back facing normals.
209 * TODO: find a more elegant solution? */
210 ray.P += ray.D * ray.tmax * 2.0f;
211 ray.D = -ray.D;
212
213 integrator_state_write_isect(state, &ss_isect.hits[0]);
215
216 /* Advance random number offset for bounce. */
217 INTEGRATOR_STATE_WRITE(state, path, rng_offset) += PRNG_BOUNCE_NUM;
218
219 const int shader = intersection_get_shader(kg, &ss_isect.hits[0]);
220 const int shader_flags = kernel_data_fetch(shaders, shader).flags;
221 const int object_flags = intersection_get_object_flags(kg, &ss_isect.hits[0]);
222 const bool use_caustics = kernel_data.integrator.use_caustics &&
223 (object_flags & SD_OBJECT_CAUSTICS);
224 const bool use_raytrace_kernel = (shader_flags & SD_HAS_RAYTRACE);
225
226 if (use_caustics) {
228 state,
231 shader);
232 }
233 else if (use_raytrace_kernel) {
235 state,
238 shader);
239 }
240 else {
242 state,
245 shader);
246 }
247
248 return true;
249}
250
251#endif /* __SUBSURFACE__ */
252
#define D
#define X
#define Z
#define Y
ccl_device_inline ccl_private ShaderClosure * bsdf_alloc(ccl_private ShaderData *sd, const int size, Spectrum weight)
Definition alloc.h:55
ccl_device int bsdf_diffuse_setup(ccl_private DiffuseBsdf *bsdf)
ccl_device_forceinline float3 microfacet_ggx_sample_vndf(const float3 wi, const float alpha_x, const float alpha_y, const float2 rand)
dot(value.rgb, luminance_coefficients)") DEFINE_VALUE("REDUCE(lhs
ccl_device_forceinline int intersection_get_shader(KernelGlobals kg, const ccl_private Intersection *ccl_restrict isect)
ccl_device_forceinline int intersection_get_object_flags(KernelGlobals kg, const ccl_private Intersection *ccl_restrict isect)
ccl_device_inline T to_global(const float2 p, const T X, const T Y)
#define kernel_assert(cond)
#define kernel_data
#define ccl_optional_struct_init
#define kernel_data_fetch(name, index)
#define one_spectrum
#define ccl_device
#define zero_spectrum
#define ccl_private
const ThreadKernelGlobalsCPU * KernelGlobals
#define ccl_device_inline
#define KERNEL_FEATURE_LIGHT_PASSES
#define CCL_NAMESPACE_END
ccl_device_forceinline float3 make_float3(const float x, const float y, const float z)
#define sqrtf(x)
ccl_device_forceinline float differential_make_compact(const float dD)
ccl_device_forceinline float differential_zero_compact()
CCL_NAMESPACE_BEGIN ccl_device void integrator_volume_stack_update_for_subsurface(KernelGlobals kg, IntegratorState state, const float3 from_P, const float3 to_P)
ccl_device_forceinline void guiding_record_bssrdf_weight(KernelGlobals kg, IntegratorState state, const Spectrum weight, const Spectrum albedo)
@ CLOSURE_BSSRDF_BURLEY_ID
@ CLOSURE_BSSRDF_RANDOM_WALK_SKIN_ID
@ SD_CLOSURE_FLAGS
@ SD_BACKFACING
@ SD_HAS_BSSRDF_BUMP
@ SD_HAS_RAYTRACE
@ PRNG_BOUNCE_NUM
@ PRNG_SUBSURFACE_BSDF
@ PATH_RAY_SUBSURFACE_BACKFACING
@ PATH_RAY_SUBSURFACE_DISK
@ PATH_RAY_CAMERA
@ PATH_RAY_DIFFUSE_ANCESTOR
@ PATH_RAY_SUBSURFACE_RANDOM_WALK
@ SD_OBJECT_CAUSTICS
@ SD_OBJECT_INTERSECTS_VOLUME
@ LABEL_NONE
@ LABEL_SUBSURFACE_SCATTER
@ DEVICE_KERNEL_INTEGRATOR_SHADE_SURFACE
@ DEVICE_KERNEL_INTEGRATOR_INTERSECT_SUBSURFACE
@ DEVICE_KERNEL_INTEGRATOR_SHADE_SURFACE_RAYTRACE
@ DEVICE_KERNEL_INTEGRATOR_SHADE_SURFACE_MNEE
ccl_device_inline float sqr(const float a)
Definition math_base.h:600
ccl_device_inline void make_orthonormals(const float3 N, ccl_private float3 *a, ccl_private float3 *b)
static ulong state[N]
#define N
#define H(x, y, z)
ccl_device_inline void path_state_rng_load(ConstIntegratorState state, ccl_private RNGState *rng_state)
Definition path_state.h:314
ccl_device_inline float2 path_state_rng_2D(KernelGlobals kg, const ccl_private RNGState *rng_state, const int dimension)
Definition path_state.h:346
ccl_device_inline void sample_cos_hemisphere(const float3 N, const float2 rand_in, ccl_private float3 *wo, ccl_private float *pdf)
#define INTEGRATOR_STATE_WRITE(state, nested_struct, member)
Definition state.h:236
#define INTEGRATOR_STATE(state, nested_struct, member)
Definition state.h:235
IntegratorStateCPU * IntegratorState
Definition state.h:228
ccl_device_forceinline void integrator_path_next_sorted(KernelGlobals kg, IntegratorState state, const DeviceKernel current_kernel, const DeviceKernel next_kernel, const uint32_t key)
Definition state_flow.h:190
CCL_NAMESPACE_BEGIN ccl_device_forceinline void integrator_state_write_ray(IntegratorState state, const ccl_private Ray *ccl_restrict ray)
Definition state_util.h:17
ccl_device_forceinline void integrator_state_write_isect(IntegratorState state, const ccl_private Intersection *ccl_restrict isect)
Definition state_util.h:149
#define FLT_MAX
Definition stdcycles.h:14
closure color bssrdf(string method, normal N, vector radius, color albedo) BUILTIN
float x
ccl_device_inline Spectrum surface_shader_bssrdf_sample_weight(const ccl_private ShaderData *ccl_restrict sd, const ccl_private ShaderClosure *ccl_restrict bssrdf_sc)
max
Definition text_draw.cc:251
float3 Spectrum
uint8_t flag
Definition wm_window.cc:139