Blender V5.3
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 const PathRayVisibility path_visibility = (INTEGRATOR_STATE(state, path, visibility) &
102 uint32_t path_flag = INTEGRATOR_STATE(state, path, flag);
103 if (sc->type == CLOSURE_BSSRDF_BURLEY_ID) {
104 /* We should never have two consecutive BSSRDF bounces, the second one should
105 * be converted to a diffuse BSDF to avoid this. */
107
108 path_flag |= PATH_RAY_SUBSURFACE_DISK;
109 INTEGRATOR_STATE_WRITE(state, subsurface, N) = sd->Ng;
110 }
111 else {
113
114 /* Sample entry bounce into the material. */
115 RNGState rng_state;
116 path_state_rng_load(state, &rng_state);
117 float3 wo;
118 if (!subsurface_entry_bounce(kg, bssrdf, sd, &rng_state, &wo) || dot(sd->Ng, wo) >= 0.0f) {
119 /* Sampling failed, give up on this bounce. */
120 return LABEL_NONE;
121 }
122 INTEGRATOR_STATE_WRITE(state, ray, D) = wo;
123 INTEGRATOR_STATE_WRITE(state, subsurface, N) = sd->N;
124 }
125
126 if (sd->runtime_flag & SR_BACKFACING) {
128 }
129
130 INTEGRATOR_STATE_WRITE(state, path, visibility) = path_visibility;
131 INTEGRATOR_STATE_WRITE(state, path, flag) = path_flag;
132
133 if (kernel_data.kernel_features & KERNEL_FEATURE_LIGHT_PASSES) {
134 if (INTEGRATOR_STATE(state, path, bounce) == 0) {
136 INTEGRATOR_STATE_WRITE(state, path, pass_glossy_weight) = zero_spectrum();
137 }
138 }
139
140 /* Pass BSSRDF parameters. */
141 INTEGRATOR_STATE_WRITE(state, subsurface, albedo) = bssrdf->albedo;
142 INTEGRATOR_STATE_WRITE(state, subsurface, radius) = bssrdf->radius;
143 /* Encode the bssrdf type in anisotropy. */
144 INTEGRATOR_STATE_WRITE(state, subsurface, anisotropy) = (bssrdf->type ==
146 bssrdf->anisotropy :
147 bssrdf->anisotropy + 2.0f;
148
149 /* Path guiding. */
150 guiding_record_bssrdf_weight(kg, state, weight, bssrdf->albedo);
151
153}
154
155ccl_device void subsurface_shader_data_setup(KernelGlobals kg, ccl_private ShaderData *sd)
156{
157 /* Get bump mapped normal from shader evaluation at exit point. */
158 float3 N = sd->N;
159 if (sd->shader_flag & SD_HAS_BSSRDF_BUMP) {
160 N = surface_shader_bssrdf_normal(sd);
161 }
162
163 /* Setup diffuse BSDF at the exit point. This replaces shader_eval_surface. */
164 sd->runtime_flag &= ~SR_CLOSURE_FLAG;
165 sd->num_closure = 0;
166 sd->num_closure_left = kernel_data.max_closures;
167
168 const Spectrum weight = one_spectrum();
169 bsdf_diffuse_setup(sd, N, weight);
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 uint 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 uint 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_RECEIVER);
222 const bool use_raytrace_kernel = (shader_flags & SD_HAS_RAYTRACE);
223
224 if (use_caustics) {
228 }
229 else if (use_raytrace_kernel) {
231 state,
234 shader);
235 }
236 else {
238 state,
241 shader);
242 }
243
244 return true;
245}
246
247#endif /* __SUBSURFACE__ */
248
#define D
#define Z
#define Y
ccl_device void bsdf_diffuse_setup(ccl_private ShaderData *sd, const float3 N, const Spectrum weight)
ccl_device_forceinline float3 microfacet_ggx_sample_vndf(const float3 wi, const float alpha_x, const float alpha_y, const float2 rand)
btScalar max
ccl_device_forceinline uint intersection_get_object_flags(KernelGlobals kg, const ccl_private Intersection *ccl_restrict isect)
ccl_device_forceinline int intersection_get_shader(KernelGlobals kg, const ccl_private Intersection *ccl_restrict isect)
#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
#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()
float3 sample_cos_hemisphere(const float2 rand)
#define FLT_MAX
float dot(VecOp< float, D >, VecOp< float, D >) RET
CCL_NAMESPACE_BEGIN ccl_device void integrator_volume_stack_update_for_subsurface(KernelGlobals kg, IntegratorState state, const float3 from_P, const float3 to_P)
const int state
ccl_device_forceinline void guiding_record_bssrdf_weight(ccl_attr_maybe_unused KernelGlobals kg, ccl_attr_maybe_unused IntegratorState state, ccl_attr_maybe_unused const Spectrum weight, ccl_attr_maybe_unused const Spectrum albedo)
@ CLOSURE_BSSRDF_BURLEY_ID
@ CLOSURE_BSSRDF_RANDOM_WALK_ID
@ CLOSURE_BSSRDF_RANDOM_WALK_SKIN_ID
uint32_t PathRayVisibility
@ SR_CLOSURE_FLAG
@ SR_BACKFACING
@ SD_HAS_BSSRDF_BUMP
@ SD_HAS_RAYTRACE
@ PRNG_BOUNCE_NUM
@ PRNG_SUBSURFACE_BSDF
@ PATH_RAY_SUBSURFACE_BACKFACING
@ PATH_RAY_SUBSURFACE_DISK
@ PATH_RAY_DIFFUSE_ANCESTOR
@ PATH_RAY_SUBSURFACE_RANDOM_WALK
@ PATH_RAY_VISIBILITY_CAMERA
@ SD_OBJECT_INTERSECTS_VOLUME
@ SD_OBJECT_CAUSTICS_RECEIVER
@ LABEL_NONE
@ LABEL_SUBSURFACE_SCATTER
@ DEVICE_KERNEL_INTEGRATOR_SHADE_SURFACE
@ DEVICE_KERNEL_INTEGRATOR_INTERSECT_MNEE
@ DEVICE_KERNEL_INTEGRATOR_INTERSECT_SUBSURFACE
@ DEVICE_KERNEL_INTEGRATOR_SHADE_SURFACE_RAYTRACE
ccl_device_inline void make_orthonormals(const float3 N, ccl_private float3 *a, ccl_private float3 *b)
#define N
#define H(x, y, z)
unsigned int uint
Definition BKE_text.h:55
unsigned int uint32_t
Definition BKE_text.h:81
#define X(TYPE)
ccl_device_inline void path_state_rng_load(ConstIntegratorState state, ccl_private RNGState *rng_state)
Definition path_state.h:337
ccl_device_inline float2 path_state_rng_2D(KernelGlobals kg, const ccl_private RNGState *rng_state, const int dimension)
Definition path_state.h:369
#define sqr
#define sqrtf
#define ccl_device
pass_diffuse_weight
#define INTEGRATOR_STATE_WRITE(state, nested_struct, member)
Definition state.h:235
#define INTEGRATOR_STATE(state, nested_struct, member)
Definition state.h:234
IntegratorStateCPU * IntegratorState
Definition state.h:227
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:250
ccl_device_forceinline void integrator_path_next(IntegratorState state, const DeviceKernel current_kernel, const DeviceKernel next_kernel)
Definition state_flow.h:219
CCL_NAMESPACE_BEGIN ccl_device_forceinline void integrator_state_write_ray(IntegratorState state, const ccl_private Ray *ccl_restrict ray)
Definition state_util.h:25
ccl_device_forceinline void integrator_state_write_isect(IntegratorState state, const ccl_private Intersection *ccl_restrict isect)
Definition state_util.h:149
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)
CCL_NAMESPACE_BEGIN ccl_device_inline T to_global(const float2 p, const T X, const T Y)
Definition transform.h:21
float3 Spectrum