Blender V5.3
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
14
15#include "kernel/geom/object.h"
17
19
20#include "util/types_rgbe.h"
21
23
26 ccl_global float *output,
27 ccl_global uint *cache_miss,
28 const int offset)
29{
30 /* Setup shader data. */
31 const KernelShaderEvalInput in = input[offset];
32
33 ShaderData sd;
34 shader_setup_from_displace(kg, &sd, in.object, in.prim, in.u, in.v);
35
36 /* Evaluate displacement shader. */
38 const float3 P = sd.P;
40 float3 D = sd.P - P;
41 if (sd.runtime_flag & SR_CACHE_MISS) {
42 *cache_miss = true;
43 }
45
46#ifdef __KERNEL_DEBUG_NAN__
47 if (!isfinite_safe(D)) {
48 kernel_assert(!"Cycles displacement with non-finite value detected");
49 }
50#endif
51
52 /* Ensure finite displacement, preventing BVH from becoming degenerate and avoiding possible
53 * traversal issues caused by non-finite math. */
54 D = ensure_finite(D);
55
56 /* Write output. */
57 output[offset * 3 + 0] = D.x;
58 output[offset * 3 + 1] = D.y;
59 output[offset * 3 + 2] = D.z;
60}
61
64 ccl_global float *output,
65 ccl_global uint *cache_miss,
66 const int offset)
67{
68 /* Setup ray */
69 const KernelShaderEvalInput in = input[offset];
70 const float3 ray_P = zero_float3();
71 const float3 ray_D = equirectangular_to_direction(in.u, in.v);
72 const float ray_time = 0.5f;
73
74 /* Compute ray differential from resolution passed via object and prim fields. */
75 const float du = 1.0f / in.object;
76 const float dv = 1.0f / in.prim;
77 const float3 ray_D_du = equirectangular_to_direction(in.u + du, in.v);
78 const float3 ray_D_dv = equirectangular_to_direction(in.u, in.v + dv);
79 const float ray_dD = 0.5f * (len(ray_D_du - ray_D) + len(ray_D_dv - ray_D));
80
81 /* Setup shader data. */
82 ShaderData sd;
83 shader_setup_from_background(kg, &sd, ray_P, ray_D, ray_dD, ray_time);
84
85 /* Evaluate shader.
86 * This is being evaluated for all BSDFs, so path flag does not contain a specific type.
87 * However, we want to flag the ray visibility to ignore the sun in the background map. */
89 const uint32_t path_flag = PATH_RAY_EMISSION | PATH_RAY_IMPORTANCE_BAKE;
92 kg, state, &sd, nullptr, PATH_RAY_VISIBILITY_NONE, path_flag);
93 if (sd.runtime_flag & SR_CACHE_MISS) {
94 *cache_miss = true;
95 }
96
98
99#ifdef __KERNEL_DEBUG_NAN__
100 if (!isfinite_safe(color)) {
101 kernel_assert(!"Cycles background with non-finite value detected");
102 }
103#endif
104
105 /* Ensure finite color, avoiding possible numerical instabilities in the path tracing kernels. */
106 color = ensure_finite(color);
107
108 const float3 color_rgb = spectrum_to_rgb(color);
109
110 /* Write output. */
111 output[offset * 3 + 0] = color_rgb.x;
112 output[offset * 3 + 1] = color_rgb.y;
113 output[offset * 3 + 2] = color_rgb.z;
114}
115
117 KernelGlobals kg,
119 ccl_global float *output,
120 ccl_global uint *cache_miss,
121 const int offset)
122{
123#ifdef __HAIR__
124 /* Setup shader data. */
125 const KernelShaderEvalInput in = input[offset];
126
127 ShaderData sd;
128 shader_setup_from_curve(kg, &sd, in.object, in.prim, __float_as_int(in.v), in.u);
129
130 /* Evaluate transparency. */
135
136 if (sd.runtime_flag & SR_CACHE_MISS) {
137 *cache_miss = true;
138 }
139
140 /* Write output. */
142#endif
143}
144
147 ccl_global float *output,
148 ccl_global uint *cache_miss,
149 const int offset)
150{
151#ifdef __VOLUME__
152 if (input[offset * 2 + 1].object == SHADER_NONE) {
153 return;
154 }
155
156 KernelShaderEvalInput in = input[offset * 2];
157
158 /* Setup ray. */
159 Ray ray;
160 ray.P = make_float3(__int_as_float(in.prim), in.u, in.v);
161 ray.D = zero_float3();
162 ray.tmin = 0.0f;
163 /* Motion blur is ignored when computing the extrema of the density, but we also don't expect the
164 * value to change a lot in one frame. */
165 ray.time = 0.5f;
166
167 /* Setup shader data. */
168 ShaderData sd;
169 shader_setup_from_volume(&sd, &ray, in.object);
170 sd.runtime_flag = SR_IS_VOLUME_SHADER_EVAL;
171 sd.shader_flag = 0;
172 /* For stochastic texture sampling. */
173 sd.lcg_state = lcg_state_init(offset, 0, 0, 0x15b4f88d);
174
175 /* Evaluate extinction and emission without allocating closures. */
176 sd.num_closure_left = 0;
177 /* Evaluate density for camera ray because it usually makes the most visual impact. For shaders
178 * that depends on ray types, the extrema are estimated on the fly. */
179 /* TODO(weizhen): Volume invisible to camera ray might appear noisy. We can at least build a
180 * separate octree for shadow ray. */
181 const PathRayVisibility path_visibility = PATH_RAY_VISIBILITY_CAMERA;
182 const uint32_t path_flag = PATH_RAY_FLAG_NONE;
183
184 /* Setup volume stack entry. */
185 in = input[offset * 2 + 1];
186 const int shader = in.object;
187 const VolumeStack entry = {sd.object, shader};
188
189 const float3 voxel_size = make_float3(__int_as_float(in.prim), in.u, in.v);
190 Extrema<float> extrema = {FLT_MAX, -FLT_MAX};
191 /* For heterogeneous volume, we take 16 samples per grid;
192 * for homogeneous volume, only 1 sample is needed. */
193 const int num_samples = volume_is_homogeneous(kg, entry) ? 1 : 16;
194
195 const bool need_transformation = !(kernel_data_fetch(object_flag, sd.object) &
197 const Transform tfm = need_transformation ?
199 Transform();
200 for (int sample = 0; sample < num_samples; sample++) {
201 /* Blue noise indexing. The sequence length is the number of samples. */
202 const uint3 index = make_uint3(sample + offset * num_samples, 0, 0xffffffff);
203
204 /* Sample a random position inside the voxel. */
205 const float3 rand_p = sobol_burley_sample_3D(
207 sd.P = ray.P + rand_p * voxel_size;
208 if (need_transformation) {
209 /* Convert to world space. */
210 sd.P = transform_point(&tfm, sd.P);
211 }
212 sd.closure_transparent_extinction = zero_float3();
213 sd.closure_emission_background = zero_float3();
214
215 /* Evaluate volume coefficients. */
217 volume_shader_eval_entry<false,
219 kg, state, &sd, entry, path_visibility, path_flag);
220
221 if (sd.runtime_flag & SR_CACHE_MISS) {
222 /* Note we keep rendering other samples so we find all cache misses in one go. */
223 *cache_miss = true;
224 }
225
226 const float sigma = reduce_max(sd.closure_transparent_extinction);
227 const float emission = reduce_max(sd.closure_emission_background);
228
229 extrema = merge(extrema, fmaxf(sigma, emission));
230 }
231
232 /* Write output. */
233 const float scale = object_volume_density(kg, sd.object);
234 output[offset * 2 + 0] = extrema.min / scale;
235 output[offset * 2 + 1] = extrema.max / scale;
236#endif
237}
238
#define D
#define output
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
#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 FLT_MAX
#define index(i)
float3x3 scale(float3x3 mat, float2 scale)
float3 transform_point(float3x3 mat, float3 point)
float reduce_max(float2 a)
ccl_device void kernel_volume_density_evaluate(KernelGlobals kg, ccl_global const KernelShaderEvalInput *input, ccl_global float *output, ccl_global uint *cache_miss, const int offset)
ccl_device void kernel_background_evaluate(KernelGlobals kg, const ccl_global KernelShaderEvalInput *input, ccl_global float *output, ccl_global uint *cache_miss, const int offset)
ccl_device void kernel_curve_shadow_transparency_evaluate(KernelGlobals kg, const ccl_global KernelShaderEvalInput *input, ccl_global float *output, ccl_global uint *cache_miss, const int offset)
CCL_NAMESPACE_BEGIN ccl_device void kernel_displace_evaluate(KernelGlobals kg, const ccl_global KernelShaderEvalInput *input, ccl_global float *output, ccl_global uint *cache_miss, const int offset)
const int state
@ 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)
uint32_t PathRayVisibility
@ SR_IS_VOLUME_SHADER_EVAL
@ SR_CACHE_MISS
@ PRNG_BAKE_VOLUME_DENSITY_EVAL
@ PATH_RAY_IMPORTANCE_BAKE
@ PATH_RAY_FLAG_NONE
@ PATH_RAY_EMISSION
@ PATH_RAY_VISIBILITY_CAMERA
@ PATH_RAY_VISIBILITY_SHADOW
@ PATH_RAY_VISIBILITY_NONE
@ 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:370
ccl_device_inline bool isfinite_safe(const float f)
Definition math_base.h:362
ccl_device_inline dual1 len(const ccl_private dual< T > &a)
Definition math_dual.h:263
CCL_NAMESPACE_BEGIN ccl_device_inline float3 zero_float3()
Definition math_float3.h:17
#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_dD, const float ray_time)
#define saturate(a)
Definition smaa.cc:319
ccl_device float3 sobol_burley_sample_3D(uint index, const uint dimension_set, uint seed, const uint shuffled_index_mask)
IntegratorBakeState ConstIntegratorBakeState
Definition state.h:232
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
ccl_device void surface_shader_eval(KernelGlobals kg, ConstIntegratorGenericState state, ccl_private ShaderData *ccl_restrict sd, ccl_global float *ccl_restrict buffer, const PathRayVisibility path_visibility, 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 RGBE rgb_to_rgbe(float3 rgb)
Definition types_rgbe.h:60
float3 Spectrum