Blender V5.0
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
83 /* Setup path state for intersect_subsurface kernel. */
84 const ccl_private Bssrdf *bssrdf = (const ccl_private Bssrdf *)sc;
85
86 /* Setup ray into surface. */
87 INTEGRATOR_STATE_WRITE(state, ray, P) = sd->P;
88 INTEGRATOR_STATE_WRITE(state, ray, tmin) = 0.0f;
92
93 /* Advance random number offset for bounce. */
94 INTEGRATOR_STATE_WRITE(state, path, rng_offset) += PRNG_BOUNCE_NUM;
95
96 /* Compute weight, optionally including Fresnel from entry point. */
97 const Spectrum weight = surface_shader_bssrdf_sample_weight(sd, sc);
98 INTEGRATOR_STATE_WRITE(state, path, throughput) *= weight;
99
100 uint32_t path_flag = (INTEGRATOR_STATE(state, path, flag) & ~PATH_RAY_CAMERA);
101 if (sc->type == CLOSURE_BSSRDF_BURLEY_ID) {
102 /* We should never have two consecutive BSSRDF bounces, the second one should
103 * be converted to a diffuse BSDF to avoid this. */
105
106 path_flag |= PATH_RAY_SUBSURFACE_DISK;
107 INTEGRATOR_STATE_WRITE(state, subsurface, N) = sd->Ng;
108 }
109 else {
111
112 /* Sample entry bounce into the material. */
113 RNGState rng_state;
114 path_state_rng_load(state, &rng_state);
115 float3 wo;
116 if (!subsurface_entry_bounce(kg, bssrdf, sd, &rng_state, &wo) || dot(sd->Ng, wo) >= 0.0f) {
117 /* Sampling failed, give up on this bounce. */
118 return LABEL_NONE;
119 }
120 INTEGRATOR_STATE_WRITE(state, ray, D) = wo;
121 INTEGRATOR_STATE_WRITE(state, subsurface, N) = sd->N;
122 }
123
124 if (sd->flag & SD_BACKFACING) {
126 }
127
128 INTEGRATOR_STATE_WRITE(state, path, flag) = path_flag;
129
130 if (kernel_data.kernel_features & KERNEL_FEATURE_LIGHT_PASSES) {
131 if (INTEGRATOR_STATE(state, path, bounce) == 0) {
132 INTEGRATOR_STATE_WRITE(state, path, pass_diffuse_weight) = one_spectrum();
133 INTEGRATOR_STATE_WRITE(state, path, pass_glossy_weight) = zero_spectrum();
134 }
135 }
136
137 /* Pass BSSRDF parameters. */
138 INTEGRATOR_STATE_WRITE(state, subsurface, albedo) = bssrdf->albedo;
139 INTEGRATOR_STATE_WRITE(state, subsurface, radius) = bssrdf->radius;
140 INTEGRATOR_STATE_WRITE(state, subsurface, anisotropy) = bssrdf->anisotropy;
141
142 /* Path guiding. */
143 guiding_record_bssrdf_weight(kg, state, weight, bssrdf->albedo);
144
146}
147
148ccl_device void subsurface_shader_data_setup(KernelGlobals kg, ccl_private ShaderData *sd)
149{
150 /* Get bump mapped normal from shader evaluation at exit point. */
151 float3 N = sd->N;
152 if (sd->flag & SD_HAS_BSSRDF_BUMP) {
153 N = surface_shader_bssrdf_normal(sd);
154 }
155
156 /* Setup diffuse BSDF at the exit point. This replaces shader_eval_surface. */
157 sd->flag &= ~SD_CLOSURE_FLAGS;
158 sd->num_closure = 0;
159 sd->num_closure_left = kernel_data.max_closures;
160
161 const Spectrum weight = one_spectrum();
162
164 sd, sizeof(DiffuseBsdf), weight);
165
166 if (bsdf) {
167 bsdf->N = N;
168 sd->flag |= bsdf_diffuse_setup(bsdf);
169 }
170}
171
172ccl_device_inline bool subsurface_scatter(KernelGlobals kg, IntegratorState state)
173{
174 RNGState rng_state;
175 path_state_rng_load(state, &rng_state);
176
179
181 if (!subsurface_random_walk(kg, state, rng_state, ray, ss_isect)) {
182 return false;
183 }
184 }
185 else {
186 if (!subsurface_disk(kg, state, rng_state, ray, ss_isect)) {
187 return false;
188 }
189 }
190
191# ifdef __VOLUME__
192 /* Update volume stack if needed. */
193 if (kernel_data.integrator.use_volumes) {
194 const int object = ss_isect.hits[0].object;
195 const int object_flag = kernel_data_fetch(object_flag, object);
196
197 if (object_flag & SD_OBJECT_INTERSECTS_VOLUME) {
198 const float3 P = INTEGRATOR_STATE(state, ray, P);
199
201 }
202 }
203# endif /* __VOLUME__ */
204
205 /* Pretend ray is coming from the outside towards the exit point. This ensures
206 * correct front/back facing normals.
207 * TODO: find a more elegant solution? */
208 ray.P += ray.D * ray.tmax * 2.0f;
209 ray.D = -ray.D;
210
211 integrator_state_write_isect(state, &ss_isect.hits[0]);
213
214 /* Advance random number offset for bounce. */
215 INTEGRATOR_STATE_WRITE(state, path, rng_offset) += PRNG_BOUNCE_NUM;
216
217 const int shader = intersection_get_shader(kg, &ss_isect.hits[0]);
218 const int shader_flags = kernel_data_fetch(shaders, shader).flags;
219 const int object_flags = intersection_get_object_flags(kg, &ss_isect.hits[0]);
220 const bool use_caustics = kernel_data.integrator.use_caustics &&
221 (object_flags & SD_OBJECT_CAUSTICS);
222 const bool use_raytrace_kernel = (shader_flags & SD_HAS_RAYTRACE);
223
224 if (use_caustics) {
226 state,
229 shader);
230 }
231 else if (use_raytrace_kernel) {
233 state,
236 shader);
237 }
238 else {
240 state,
243 shader);
244 }
245
246 return true;
247}
248
249#endif /* __SUBSURFACE__ */
250
#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 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)
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 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:329
ccl_device_inline float2 path_state_rng_2D(KernelGlobals kg, const ccl_private RNGState *rng_state, const int dimension)
Definition path_state.h:361
#define sqr
#define sqrtf
#define ccl_device
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, IntegratorState state, const DeviceKernel current_kernel, const DeviceKernel next_kernel, const uint32_t key)
Definition state_flow.h:214
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:145