Blender V5.0
kernel/bake/bake.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/globals.h"
8
13
14#include "kernel/geom/object.h"
16
18
20
23 ccl_global float *output,
24 const int offset)
25{
26 /* Setup shader data. */
27 const KernelShaderEvalInput in = input[offset];
28
29 ShaderData sd;
30 shader_setup_from_displace(kg, &sd, in.object, in.prim, in.u, in.v);
31
32 /* Evaluate displacement shader. */
33 const float3 P = sd.P;
35 float3 D = sd.P - P;
36
38
39#ifdef __KERNEL_DEBUG_NAN__
40 if (!isfinite_safe(D)) {
41 kernel_assert(!"Cycles displacement with non-finite value detected");
42 }
43#endif
44
45 /* Ensure finite displacement, preventing BVH from becoming degenerate and avoiding possible
46 * traversal issues caused by non-finite math. */
47 D = ensure_finite(D);
48
49 /* Write output. */
50 output[offset * 3 + 0] += D.x;
51 output[offset * 3 + 1] += D.y;
52 output[offset * 3 + 2] += D.z;
53}
54
57 ccl_global float *output,
58 const int offset)
59{
60 /* Setup ray */
61 const KernelShaderEvalInput in = input[offset];
62 const float3 ray_P = zero_float3();
63 const float3 ray_D = equirectangular_to_direction(in.u, in.v);
64 const float ray_time = 0.5f;
65
66 /* Setup shader data. */
67 ShaderData sd;
68 shader_setup_from_background(kg, &sd, ray_P, ray_D, ray_time);
69
70 /* Evaluate shader.
71 * This is being evaluated for all BSDFs, so path flag does not contain a specific type.
72 * However, we want to flag the ray visibility to ignore the sun in the background map. */
73 const uint32_t path_flag = PATH_RAY_EMISSION | PATH_RAY_IMPORTANCE_BAKE;
76 kg, INTEGRATOR_STATE_NULL, &sd, nullptr, path_flag);
78
79#ifdef __KERNEL_DEBUG_NAN__
80 if (!isfinite_safe(color)) {
81 kernel_assert(!"Cycles background with non-finite value detected");
82 }
83#endif
84
85 /* Ensure finite color, avoiding possible numerical instabilities in the path tracing kernels. */
86 color = ensure_finite(color);
87
88 const float3 color_rgb = spectrum_to_rgb(color);
89
90 /* Write output. */
91 output[offset * 3 + 0] += color_rgb.x;
92 output[offset * 3 + 1] += color_rgb.y;
93 output[offset * 3 + 2] += color_rgb.z;
94}
95
99 ccl_global float *output,
100 const int offset)
101{
102#ifdef __HAIR__
103 /* Setup shader data. */
104 const KernelShaderEvalInput in = input[offset];
105
106 ShaderData sd;
107 shader_setup_from_curve(kg, &sd, in.object, in.prim, __float_as_int(in.v), in.u);
108
109 /* Evaluate transparency. */
112 kg, INTEGRATOR_STATE_NULL, &sd, nullptr, PATH_RAY_SHADOW);
113
114 /* Write output. */
115 output[offset] = clamp(average(surface_shader_transparency(&sd)), 0.0f, 1.0f);
116#endif
117}
118
121 ccl_global float *output,
122 const int offset)
123{
124#ifdef __VOLUME__
125 if (input[offset * 2 + 1].object == SHADER_NONE) {
126 return;
127 }
128
129 KernelShaderEvalInput in = input[offset * 2];
130
131 /* Setup ray. */
132 Ray ray;
133 ray.P = make_float3(__int_as_float(in.prim), in.u, in.v);
134 ray.D = zero_float3();
135 ray.tmin = 0.0f;
136 /* Motion blur is ignored when computing the extrema of the density, but we also don't expect the
137 * value to change a lot in one frame. */
138 ray.time = 0.5f;
139
140 /* Setup shader data. */
141 ShaderData sd;
142 shader_setup_from_volume(&sd, &ray, in.object);
143 sd.flag = SD_IS_VOLUME_SHADER_EVAL;
144 /* For stochastic texture sampling. */
145 sd.lcg_state = lcg_state_init(offset, 0, 0, 0x15b4f88d);
146
147 /* Evaluate extinction and emission without allocating closures. */
148 sd.num_closure_left = 0;
149 /* Evaluate density for camera ray because it usually makes the most visual impact. For shaders
150 * that depends on ray types, the extrema are estimated on the fly. */
151 /* TODO(weizhen): Volume invisible to camera ray might appear noisy. We can at least build a
152 * separate octree for shadow ray. */
153 const uint32_t path_flag = PATH_RAY_CAMERA;
154
155 /* Setup volume stack entry. */
156 in = input[offset * 2 + 1];
157 const int shader = in.object;
158 const VolumeStack entry = {sd.object, shader};
159
160 const float3 voxel_size = make_float3(__int_as_float(in.prim), in.u, in.v);
161 Extrema<float> extrema = {FLT_MAX, -FLT_MAX};
162 /* For heterogeneous volume, we take 16 samples per grid;
163 * for homogeneous volume, only 1 sample is needed. */
164 const int num_samples = volume_is_homogeneous(kg, entry) ? 1 : 16;
165
166 const bool need_transformation = !(kernel_data_fetch(object_flag, sd.object) &
168 const Transform tfm = need_transformation ?
170 Transform();
171 for (int sample = 0; sample < num_samples; sample++) {
172 /* Blue noise indexing. The sequence length is the number of samples. */
173 const uint3 index = make_uint3(sample + offset * num_samples, 0, 0xffffffff);
174
175 /* Sample a random position inside the voxel. */
176 const float3 rand_p = sobol_burley_sample_3D(
177 index.x, PRNG_BAKE_VOLUME_DENSITY_EVAL, index.y, index.z);
178 sd.P = ray.P + rand_p * voxel_size;
179 if (need_transformation) {
180 /* Convert to world spcace. */
181 sd.P = transform_point(&tfm, sd.P);
182 }
183 sd.closure_transparent_extinction = zero_float3();
184 sd.closure_emission_background = zero_float3();
185
186 /* Evaluate volume coefficients. */
187 volume_shader_eval_entry<false,
189 kg, INTEGRATOR_STATE_NULL, &sd, entry, path_flag);
190
191 const float sigma = reduce_max(sd.closure_transparent_extinction);
192 const float emission = reduce_max(sd.closure_emission_background);
193
194 extrema = merge(extrema, fmaxf(sigma, emission));
195 }
196
197 /* Write output. */
198 const float scale = object_volume_density(kg, sd.object);
199 output[offset * 2 + 0] = extrema.min / scale;
200 output[offset * 2 + 1] = extrema.max / scale;
201#endif
202}
203
#define D
reduce_max(value.rgb)") DEFINE_VALUE("REDUCE(lhs
ccl_device float3 equirectangular_to_direction(const float u, const float v)
#define kernel_assert(cond)
#define kernel_data_fetch(name, index)
#define SHADER_NONE
#define KERNEL_FEATURE_NODE_MASK_SURFACE_SHADOW
const ThreadKernelGlobalsCPU * KernelGlobals
#define KERNEL_FEATURE_NODE_MASK_VOLUME
#define KERNEL_FEATURE_NODE_LIGHT_PATH
#define KERNEL_FEATURE_NODE_MASK_SURFACE_LIGHT
#define ccl_global
#define KERNEL_FEATURE_NODE_RAYTRACE
#define CCL_NAMESPACE_END
ccl_device_forceinline float3 make_float3(const float x, const float y, const float z)
ccl_device_forceinline uint3 make_uint3(const uint x, const uint y, const uint z)
#define __int_as_float(x)
#define __float_as_int(x)
CCL_NAMESPACE_BEGIN ccl_device void displacement_shader_eval(KernelGlobals kg, ConstIntegratorGenericState state, ccl_private ShaderData *sd)
#define input
#define in
#define output
constexpr T clamp(T, U, U) RET
ccl_device void kernel_volume_density_evaluate(KernelGlobals kg, ccl_global const KernelShaderEvalInput *input, ccl_global float *output, const int offset)
ccl_device void kernel_curve_shadow_transparency_evaluate(KernelGlobals kg, const ccl_global KernelShaderEvalInput *input, ccl_global float *output, const int offset)
CCL_NAMESPACE_BEGIN ccl_device void kernel_displace_evaluate(KernelGlobals kg, const ccl_global KernelShaderEvalInput *input, ccl_global float *output, const int offset)
ccl_device void kernel_background_evaluate(KernelGlobals kg, const ccl_global KernelShaderEvalInput *input, ccl_global float *output, const int offset)
@ OBJECT_TRANSFORM
ccl_device_inline float object_volume_density(KernelGlobals kg, const int object)
ccl_device_inline Transform object_fetch_transform(KernelGlobals kg, const int object, enum ObjectTransform type)
ccl_device_inline void object_inverse_dir_transform(KernelGlobals kg, const ccl_private ShaderData *sd, ccl_private float3 *D)
@ SD_IS_VOLUME_SHADER_EVAL
@ PRNG_BAKE_VOLUME_DENSITY_EVAL
@ PATH_RAY_SHADOW
@ PATH_RAY_IMPORTANCE_BAKE
@ PATH_RAY_EMISSION
@ PATH_RAY_CAMERA
@ SD_OBJECT_TRANSFORM_APPLIED
ccl_device_inline float3 spectrum_to_rgb(Spectrum s)
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
OrientationBounds merge(const OrientationBounds &cone_a, const OrientationBounds &cone_b)
ccl_device_inline float ensure_finite(const float v)
Definition math_base.h:356
ccl_device_inline bool isfinite_safe(const float f)
Definition math_base.h:348
CCL_NAMESPACE_BEGIN ccl_device_inline float3 zero_float3()
Definition math_float3.h:17
float average(point a)
Definition node_math.h:144
#define ccl_device
#define fmaxf
ccl_device void shader_setup_from_displace(KernelGlobals kg, ccl_private ShaderData *ccl_restrict sd, const int object, const int prim, const float u, const float v)
ccl_device_inline void shader_setup_from_background(KernelGlobals kg, ccl_private ShaderData *ccl_restrict sd, const float3 ray_P, const float3 ray_D, const float ray_time)
ccl_device float3 sobol_burley_sample_3D(uint index, const uint dimension_set, uint seed, const uint shuffled_index_mask)
#define INTEGRATOR_STATE_NULL
Definition state.h:233
#define FLT_MAX
Definition stdcycles.h:14
float tmin
float3 P
float time
float3 D
float z
Definition sky_math.h:136
float y
Definition sky_math.h:136
float x
Definition sky_math.h:136
uint y
Definition types_uint3.h:13
uint z
Definition types_uint3.h:13
uint x
Definition types_uint3.h:13
ccl_device void surface_shader_eval(KernelGlobals kg, ConstIntegratorGenericState state, ccl_private ShaderData *ccl_restrict sd, ccl_global float *ccl_restrict buffer, const uint32_t path_flag, bool use_caustics_storage=false)
ccl_device Spectrum surface_shader_transparency(const ccl_private ShaderData *sd)
ccl_device Spectrum surface_shader_background(const ccl_private ShaderData *sd)
ccl_device_inline float3 transform_point(const ccl_private Transform *t, const float3 a)
Definition transform.h:56
float3 Spectrum