Blender V5.3
bevel.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
7#include "kernel/bvh/bvh.h"
8
12
14
16#include "kernel/svm/util.h"
17
19
20#ifdef __SHADER_RAYTRACE__
21
22/* Planar Cubic BSSRDF falloff, reused for bevel.
23 *
24 * This is basically (Rm - x)^3, with some factors to normalize it. For sampling
25 * we integrate 2*pi*x * (Rm - x)^3, which gives us a quintic equation that as
26 * far as I can tell has no closed form solution. So we get an iterative solution
27 * instead with newton-raphson. */
28
29ccl_device float svm_bevel_cubic_eval(const float radius, const float r)
30{
31 const float Rm = radius;
32
33 if (r >= Rm) {
34 return 0.0f;
35 }
36
37 /* integrate (2*pi*r * 10*(R - r)^3)/(pi * R^5) from 0 to R = 1 */
38 const float Rm5 = (Rm * Rm) * (Rm * Rm) * Rm;
39 const float f = Rm - r;
40 const float num = f * f * f;
41
42 return (10.0f * num) / (Rm5 * M_PI_F);
43}
44
45ccl_device float svm_bevel_cubic_pdf(const float radius, const float r)
46{
47 return svm_bevel_cubic_eval(radius, r);
48}
49
50/* solve 10x^2 - 20x^3 + 15x^4 - 4x^5 - xi == 0 */
51ccl_device_forceinline float svm_bevel_cubic_quintic_root_find(const float xi)
52{
53 /* newton-raphson iteration, usually succeeds in 2-4 iterations, except
54 * outside 0.02 ... 0.98 where it can go up to 10, so overall performance
55 * should not be too bad */
56 const float tolerance = 1e-6f;
57 const int max_iteration_count = 10;
58 float x = 0.25f;
59 int i;
60
61 for (i = 0; i < max_iteration_count; i++) {
62 const float x2 = x * x;
63 const float x3 = x2 * x;
64 const float nx = (1.0f - x);
65
66 const float f = 10.0f * x2 - 20.0f * x3 + 15.0f * x2 * x2 - 4.0f * x2 * x3 - xi;
67 const float f_ = 20.0f * (x * nx) * (nx * nx);
68
69 if (fabsf(f) < tolerance || f_ == 0.0f) {
70 break;
71 }
72
73 x = saturatef(x - f / f_);
74 }
75
76 return x;
77}
78
79ccl_device void svm_bevel_cubic_sample(const float radius,
80 const float xi,
81 ccl_private float *r,
82 ccl_private float *h)
83{
84 const float Rm = radius;
85 float r_ = svm_bevel_cubic_quintic_root_find(xi);
86
87 r_ *= Rm;
88 *r = r_;
89
90 /* h^2 + r^2 = Rm^2 */
91 *h = safe_sqrtf(Rm * Rm - r_ * r_);
92}
93
94/* Bevel shader averaging normals from nearby surfaces.
95 *
96 * Sampling strategy from: BSSRDF Importance Sampling, SIGGRAPH 2013
97 * http://library.imageworks.com/pdfs/imageworks-library-BSSRDF-sampling.pdf
98 */
99
100# ifdef __KERNEL_OPTIX__
101extern "C" __device__ float3 __direct_callable__svm_node_bevel(
102# else
103ccl_device float3 svm_bevel(
104# endif
105 KernelGlobals kg,
107 ccl_private ShaderData *sd,
108 const float radius,
109 const int num_samples)
110{
111 /* Early out if no sampling needed. */
112 if (radius <= 0.0f || num_samples < 1 || sd->object == OBJECT_NONE) {
113 return sd->N;
114 }
115
116 /* Can't ray-trace from shaders like displacement, before BVH exists. */
117 if (kernel_data.bvh.bvh_layout == BVH_LAYOUT_NONE) {
118 return sd->N;
119 }
120
121 /* Don't bevel for blurry indirect rays. */
122 if (INTEGRATOR_STATE(state, path, min_ray_pdf) < 8.0f) {
123 return sd->N;
124 }
125
126 /* Setup for multi intersection. */
127 LocalIntersection isect;
128 uint lcg_state = lcg_state_init(INTEGRATOR_STATE(state, path, rng_pixel),
129 INTEGRATOR_STATE(state, path, rng_offset),
131 0x64c6a40e);
132
133 /* Sample normals from surrounding points on surface. */
134 float3 sum_N = make_float3(0.0f, 0.0f, 0.0f);
135
136 /* TODO: support ray-tracing in shadow shader evaluation? */
137 RNGState rng_state;
138 path_state_rng_load(state, &rng_state);
139
140 for (int sample = 0; sample < num_samples; sample++) {
141 float2 rand_disk = path_branched_rng_2D(
142 kg, &rng_state, sample, num_samples, PRNG_SURFACE_BEVEL);
143
144 /* Pick random axis in local frame and point on disk. */
145 float3 disk_N;
146 float3 disk_T;
147 float3 disk_B;
148 float pick_pdf_N;
149 float pick_pdf_T;
150 float pick_pdf_B;
151
152 disk_N = sd->Ng;
153 make_orthonormals(disk_N, &disk_T, &disk_B);
154
155 const float axisu = rand_disk.x;
156
157 if (axisu < 0.5f) {
158 pick_pdf_N = 0.5f;
159 pick_pdf_T = 0.25f;
160 pick_pdf_B = 0.25f;
161 rand_disk.x *= 2.0f;
162 }
163 else if (axisu < 0.75f) {
164 const float3 tmp = disk_N;
165 disk_N = disk_T;
166 disk_T = tmp;
167 pick_pdf_N = 0.25f;
168 pick_pdf_T = 0.5f;
169 pick_pdf_B = 0.25f;
170 rand_disk.x = (rand_disk.x - 0.5f) * 4.0f;
171 }
172 else {
173 const float3 tmp = disk_N;
174 disk_N = disk_B;
175 disk_B = tmp;
176 pick_pdf_N = 0.25f;
177 pick_pdf_T = 0.25f;
178 pick_pdf_B = 0.5f;
179 rand_disk.x = (rand_disk.x - 0.75f) * 4.0f;
180 }
181
182 /* Sample point on disk. */
183 const float phi = M_2PI_F * rand_disk.x;
184 float disk_r = rand_disk.y;
185 float disk_height;
186
187 /* Perhaps find something better than Cubic BSSRDF, but happens to work well. */
188 svm_bevel_cubic_sample(radius, disk_r, &disk_r, &disk_height);
189
190 const float3 disk_P = to_global(polar_to_cartesian(disk_r, phi), disk_T, disk_B);
191
192 /* Create ray. */
194 ray.P = sd->P + disk_N * disk_height + disk_P;
195 ray.D = -disk_N;
196 ray.tmin = 0.0f;
197 ray.tmax = 2.0f * disk_height;
198 ray.dP = differential_zero_compact();
199 ray.dD = differential_zero_compact();
200 ray.time = sd->time;
201 ray.self.object = OBJECT_NONE;
202 ray.self.prim = PRIM_NONE;
203 ray.self.light_object = OBJECT_NONE;
204 ray.self.light_prim = PRIM_NONE;
205
206 /* Intersect with the same object. if multiple intersections are found it
207 * will use at most LOCAL_MAX_HITS hits, a random subset of all hits. */
208 scene_intersect_local(kg, &ray, &isect, sd->object, &lcg_state, LOCAL_MAX_HITS);
209
210 const int num_eval_hits = min(isect.num_hits, LOCAL_MAX_HITS);
211
212 for (int hit = 0; hit < num_eval_hits; hit++) {
213 /* Quickly retrieve P and Ng without setting up ShaderData. */
214 float3 hit_P;
215 if (sd->type == PRIMITIVE_TRIANGLE) {
217 kg, sd, isect.hits[hit].prim, isect.hits[hit].u, isect.hits[hit].v);
218 }
219# ifdef __OBJECT_MOTION__
220 else if (sd->type == PRIMITIVE_MOTION_TRIANGLE) {
221 float3 verts[3];
222 motion_triangle_vertices(kg, sd->object, isect.hits[hit].prim, sd->time, verts);
224 kg, sd, isect.hits[hit].u, isect.hits[hit].v, verts);
225 }
226# endif /* __OBJECT_MOTION__ */
227
228 /* Get geometric normal. */
229 float3 hit_Ng = isect.Ng[hit];
230 const int object = isect.hits[hit].object;
231 const uint object_flag = kernel_data_fetch(object_flag, object);
232 if (object_negative_scale_applied(object_flag)) {
233 hit_Ng = -hit_Ng;
234 }
235
236 /* Compute smooth normal. */
237 float3 N = hit_Ng;
238 const int prim = isect.hits[hit].prim;
239 const int shader = kernel_data_fetch(tri_shader, prim);
240
241 if (shader & SHADER_SMOOTH_NORMAL) {
242 const float u = isect.hits[hit].u;
243 const float v = isect.hits[hit].v;
244
245 if (sd->type == PRIMITIVE_TRIANGLE) {
246 N = triangle_smooth_normal(kg, N, object, object_flag, prim, u, v);
247 }
248# ifdef __OBJECT_MOTION__
249 else if (sd->type == PRIMITIVE_MOTION_TRIANGLE) {
250 N = motion_triangle_smooth_normal(kg, N, object, prim, u, v, sd->time);
251 }
252# endif /* __OBJECT_MOTION__ */
253 }
254
255 /* Transform normals to world space. */
256 if (!(object_flag & SD_OBJECT_TRANSFORM_APPLIED)) {
257 object_normal_transform(kg, sd, &N);
258 object_normal_transform(kg, sd, &hit_Ng);
259 }
260
261 /* Probability densities for local frame axes. */
262 const float pdf_N = pick_pdf_N * fabsf(dot(disk_N, hit_Ng));
263 const float pdf_T = pick_pdf_T * fabsf(dot(disk_T, hit_Ng));
264 const float pdf_B = pick_pdf_B * fabsf(dot(disk_B, hit_Ng));
265
266 /* Multiple importance sample between 3 axes, power heuristic
267 * found to be slightly better than balance heuristic. pdf_N
268 * in the MIS weight and denominator canceled out. */
269 float w = pdf_N / (sqr(pdf_N) + sqr(pdf_T) + sqr(pdf_B));
270 if (isect.num_hits > LOCAL_MAX_HITS) {
271 w *= isect.num_hits / (float)LOCAL_MAX_HITS;
272 }
273
274 /* Real distance to sampled point. */
275 const float r = len(hit_P - sd->P);
276
277 /* Compute weight. */
278 const float pdf = svm_bevel_cubic_pdf(radius, r);
279 const float disk_pdf = svm_bevel_cubic_pdf(radius, disk_r);
280
281 w *= pdf / disk_pdf;
282
283 /* Sum normal and weight. */
284 sum_N += w * N;
285 }
286 }
287
288 /* Normalize. */
289 const float3 N = safe_normalize(sum_N);
290 return is_zero(N) ? sd->N : (sd->runtime_flag & SR_BACKFACING) ? -N : N;
291}
292
293template<uint64_t node_feature_mask, typename ConstIntegratorGenericState>
294# if defined(__KERNEL_OPTIX__)
296# else
298# endif
299 void svm_node_bevel(KernelGlobals kg,
300 ConstIntegratorGenericState state,
301 ccl_private ShaderData *sd,
302 ccl_private float *ccl_restrict stack,
304{
305 float3 bevel_N = sd->N;
306
307 IF_KERNEL_NODES_FEATURE (RAYTRACE) {
308 float radius = stack_load(stack, node.radius);
309
310# ifdef __KERNEL_OPTIX__
311 bevel_N = optixDirectCall<float3>(1, kg, state, sd, radius, node.num_samples);
312# else
313 bevel_N = svm_bevel(kg, state, sd, radius, node.num_samples);
314# endif
315
316 if (stack_valid(node.normal_offset)) {
317 /* Preserve input normal. */
318 const float3 ref_N = stack_load_float3(stack, node.normal_offset);
319 bevel_N = normalize(ref_N + (bevel_N - sd->N));
320 }
321 }
322
323 stack_store_float3(stack, node.out_offset, bevel_N);
324}
325
326#endif /* __SHADER_RAYTRACE__ */
327
SIMD_FORCE_INLINE const btScalar & w() const
Return the w value.
Definition btQuadWord.h:119
ccl_device_inline float3 stack_load_float3(const ccl_private float *stack, const uint a)
ccl_device_inline void stack_store_float3(ccl_private float *stack, const uint a, const float3 f)
ccl_device_inline bool stack_valid(const uint a)
ccl_device_inline T stack_load(const ccl_private float *stack, const uint a)
ccl_device_inline float2 polar_to_cartesian(const float r, const float phi)
#define kernel_data
#define ccl_restrict
#define ccl_device_forceinline
#define IF_KERNEL_NODES_FEATURE(feature)
#define ccl_optional_struct_init
#define kernel_data_fetch(name, index)
#define PRIM_NONE
#define OBJECT_NONE
#define LOCAL_MAX_HITS
#define ccl_private
#define ccl_device_inline
#define ccl_global
#define ccl_device_noinline
#define CCL_NAMESPACE_END
#define saturatef(x)
ccl_device_forceinline float3 make_float3(const float x, const float y, const float z)
#define __device__
ccl_device_forceinline float differential_zero_compact()
ccl_device_inline float3 triangle_smooth_normal(KernelGlobals kg, float3 Ng, int object, int object_flag, int prim, float u, float v)
VecBase< float, D > normalize(VecOp< float, D >) RET
float dot(VecOp< float, D >, VecOp< float, D >) RET
bool is_zero(float3x3 a)
VecT safe_normalize(VecT vector)
const int state
ccl_device_inline bool object_negative_scale_applied(const uint object_flag)
ccl_device_inline void object_normal_transform(KernelGlobals kg, const ccl_private ShaderData *sd, ccl_private T *N)
@ SR_BACKFACING
@ PRIMITIVE_MOTION_TRIANGLE
@ PRIMITIVE_TRIANGLE
@ PRNG_SURFACE_BEVEL
@ SD_OBJECT_TRANSFORM_APPLIED
@ SHADER_SMOOTH_NORMAL
@ BVH_LAYOUT_NONE
ccl_device_inline uint lcg_state_init(const uint rng_hash, const uint rng_offset, const uint sample, const uint scramble)
Definition lcg.h:45
ccl_device_inline float safe_sqrtf(const float f)
Definition math_base.h:571
ccl_device_inline dual1 len(const ccl_private dual< T > &a)
Definition math_dual.h:263
ccl_device_inline void make_orthonormals(const float3 N, ccl_private float3 *a, ccl_private float3 *b)
#define N
ccl_device_inline void motion_triangle_vertices(KernelGlobals kg, const int object, const uint3 tri_vindex, const int numsteps, const int numverts, const int step, const float t, float3 verts[3])
ccl_device_inline float3 motion_triangle_smooth_normal(KernelGlobals kg, const float3 Ng, const int object, const int prim, const uint3 tri_vindex, const int numsteps, const int step, const float t, const float u, const float v)
unsigned int uint
Definition BKE_text.h:55
ATTR_WARN_UNUSED_RESULT const BMVert * v
ATTR_WARN_UNUSED_RESULT const size_t num
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_branched_rng_2D(KernelGlobals kg, const ccl_private RNGState *rng_state, const int branch, const int num_branches, const int dimension)
Definition path_state.h:397
#define sqr
#define fabsf
#define ccl_device
#define M_2PI_F
#define M_PI_F
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
#define min(a, b)
Definition sort.cc:38
#define INTEGRATOR_STATE(state, nested_struct, member)
Definition state.h:234
const IntegratorStateCPU * ConstIntegratorState
Definition state.h:228
Intersection hits[LOCAL_MAX_HITS]
float3 Ng[LOCAL_MAX_HITS]
float x
float y
CCL_NAMESPACE_BEGIN ccl_device_inline T to_global(const float2 p, const T X, const T Y)
Definition transform.h:21
ccl_device_inline float3 triangle_point_from_uv(KernelGlobals kg, ccl_private ShaderData *sd, const int isect_prim, const float u, const float v)
ccl_device_inline float3 triangle_point_from_uv_and_verts(KernelGlobals kg, ccl_private ShaderData *sd, const float u, const float v, const float3 verts[3])