Blender V5.3
kernel/osl/osl.h
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2009-2010 Sony Pictures Imageworks Inc., et al. All Rights Reserved.
2 * SPDX-FileCopyrightText: 2011-2022 Blender Foundation
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 *
6 * Adapted code from Open Shading Language. */
7
8#pragma once
9
10/* OSL Shader Engine
11 *
12 * Holds all variables to execute and use OSL shaders from the kernel.
13 */
14
15#ifdef __KERNEL_OPTIX__
18#endif
19
21#include "kernel/osl/types.h"
22
24
26
28 const PathRayVisibility path_visibility,
29 const uint32_t path_flag,
31{
32 const differential3 dP = differential_from_compact(sd->Ng, sd->dP);
33 const differential3 dI = differential_from_compact(sd->wi, sd->dI);
34
35 /* copy from shader data to shader globals */
36 globals->P = sd->P;
37 globals->dPdx = dP.dx;
38 globals->dPdy = dP.dy;
39 globals->I = sd->wi;
40 globals->dIdx = dI.dx;
41 globals->dIdy = dI.dy;
42 globals->N = sd->N;
43 globals->Ng = sd->Ng;
44 globals->u = sd->u;
45 globals->dudx = sd->du.dx;
46 globals->dudy = sd->du.dy;
47 globals->v = sd->v;
48 globals->dvdx = sd->dv.dx;
49 globals->dvdy = sd->dv.dy;
50 globals->dPdu = sd->dPdu;
51 globals->dPdv = sd->dPdv;
52 globals->time = sd->time;
53 globals->dtime = 1.0f;
54 globals->surfacearea = 1.0f;
55 globals->raytype = OSL_RAYTYPE_PACK(path_visibility, path_flag);
56 globals->flipHandedness = 0;
57 globals->backfacing = (sd->runtime_flag & SR_BACKFACING);
58
59 /* shader data to be used in services callbacks */
60 globals->sd = sd;
61 globals->shadingStateUniform = nullptr;
62 globals->thread_index = 0;
63 globals->shade_index = 0;
64
65 /* hacky, we leave it to services to fetch actual object matrix */
66 globals->shader2common = sd;
67 globals->object2common = sd;
68
69 /* must be set to nullptr before execute */
70 globals->Ci = nullptr;
71}
72
74 ccl_private ShaderData *sd,
75 const PathRayVisibility ray_visibility,
76 const uint32_t path_flag,
77 const ccl_private OSLClosure *closure)
78{
79 int stack_size = 0;
80 float3 weight = one_float3();
81 float3 weight_stack[16];
82 const ccl_private OSLClosure *closure_stack[16];
83 int layer_stack_level = -1;
84 float3 layer_albedo = zero_float3();
85
86 while (true) {
87 switch (closure->id) {
88 case OSL_CLOSURE_MUL_ID: {
89 const ccl_private OSLClosureMul *mul = static_cast<const ccl_private OSLClosureMul *>(
90 closure);
91 weight *= mul->weight;
92 closure = mul->closure;
93 continue;
94 }
95 case OSL_CLOSURE_ADD_ID: {
96 if (stack_size >= 16) {
97 kernel_assert(!"Exhausted OSL closure stack");
98 break;
99 }
100 const ccl_private OSLClosureAdd *add = static_cast<const ccl_private OSLClosureAdd *>(
101 closure);
102 closure = add->closureA;
103 weight_stack[stack_size] = weight;
104 closure_stack[stack_size++] = add->closureB;
105 continue;
106 }
107 case OSL_CLOSURE_LAYER_ID: {
108 const ccl_private OSLClosureComponent *comp =
109 static_cast<const ccl_private OSLClosureComponent *>(closure);
110 const ccl_private LayerClosure *layer = reinterpret_cast<const ccl_private LayerClosure *>(
111 comp + 1);
112
113 /* Layer closures may not appear in the top layer subtree of another layer closure. */
114 kernel_assert(layer_stack_level == -1);
115
116 if (layer->top != nullptr) {
117 /* Push base layer onto the stack, will be handled after the top layers */
118 weight_stack[stack_size] = weight;
119 closure_stack[stack_size] = layer->base;
120 /* Start accumulating albedo of the top layers */
121 layer_stack_level = stack_size++;
122 layer_albedo = zero_float3();
123 /* Continue with the top layers */
124 closure = layer->top;
125 }
126 else {
127 /* No top layer, just continue with base. */
128 closure = layer->base;
129 }
130 continue;
131 }
132#define OSL_CLOSURE_STRUCT_BEGIN(Upper, lower) \
133 case OSL_CLOSURE_##Upper##_ID: { \
134 ccl_private const OSLClosureComponent *comp = \
135 static_cast<ccl_private const OSLClosureComponent *>(closure); \
136 float3 albedo = one_float3(); \
137 osl_closure_##lower##_setup(kg, \
138 sd, \
139 ray_visibility, \
140 path_flag, \
141 weight * comp->weight, \
142 reinterpret_cast<ccl_private const Upper##Closure *>(comp + 1), \
143 (layer_stack_level >= 0) ? &albedo : nullptr); \
144 if (layer_stack_level >= 0) { \
145 layer_albedo += albedo; \
146 } \
147 break; \
148 }
149#include "closures_template.h"
150
151 default:
152 break;
153 }
154
155 /* Pop the next closure from the stack (or return if we're done). */
156 do {
157 if (stack_size == 0) {
158 return;
159 }
160
161 weight = weight_stack[--stack_size];
162 closure = closure_stack[stack_size];
163 if (stack_size == layer_stack_level) {
164 /* We just finished processing the top layers of a Layer closure, so adjust the weight to
165 * account for the layering. */
166 weight = closure_layering_weight(layer_albedo, weight);
167 layer_stack_level = -1;
168 /* If it's fully occluded, skip the base layer we just popped from the stack and grab
169 * the next entry instead. */
170 if (is_zero(weight)) {
171 continue;
172 }
173 }
174 } while (closure == nullptr);
175 }
176}
177
178#ifndef __KERNEL_GPU__
179
180template<ShaderType type, typename ConstIntegratorGenericState>
182 ConstIntegratorGenericState state,
183 ShaderData *sd,
184 PathRayVisibility path_visibility,
185 uint32_t path_flag);
186
187#else
188
189template<ShaderType type, typename ConstIntegratorGenericState>
191 ConstIntegratorGenericState state,
192 ccl_private ShaderData *sd,
193 const PathRayVisibility path_visibility,
194 const uint32_t path_flag)
195{
196 ShaderGlobals globals;
197 shaderdata_to_shaderglobals(sd, path_visibility, path_flag, &globals);
198
199 const int shader = sd->shader & SHADER_MASK;
200
201# ifdef __KERNEL_OPTIX__
202 uint8_t closure_pool[1024];
203 globals.closure_pool = closure_pool;
204 globals.closure_pool_end = closure_pool + sizeof(closure_pool);
205 if constexpr (std::is_same_v<ConstIntegratorGenericState, ConstIntegratorBakeState>) {
206 globals.shade_index = 0;
207 }
208 else if constexpr (std::is_same_v<ConstIntegratorGenericState, ConstIntegratorShadowState>) {
209 globals.shade_index = -state - 1;
210 }
211 else {
212 globals.shade_index = state + 1;
213 }
214
215 /* For surface shaders, we might have an automatic bump shader that needs to be executed before
216 * the main shader to update globals.N. */
217 if constexpr (type == SHADER_TYPE_SURFACE) {
218 if (sd->shader_flag & SD_HAS_BUMP_FROM_DISPLACEMENT) {
219 /* Save state. */
220 const float3 P = sd->P;
221 const float dP = sd->dP;
222 const packed_float3 dPdx = globals.dPdx;
223 const packed_float3 dPdy = globals.dPdy;
224
225 /* Set position state as if undisplaced. */
226 if (sd->shader_flag & SD_HAS_DISPLACEMENT) {
229
231
233
234 sd->P = P.val;
236
237 globals.P = sd->P;
238 globals.dPdx = P.dx;
239 globals.dPdy = P.dy;
240
241 /* Set normal as if undisplaced. */
243 globals.N = sd->N;
244 }
245
246 /* Execute bump shader. */
247 unsigned int optix_dc_index = 2 /* NUM_CALLABLE_PROGRAM_GROUPS */ + 1 /* camera program */ +
248 (shader + SHADER_TYPE_BUMP * kernel_data.max_shaders);
249 optixDirectCall<void>(optix_dc_index,
250 /* shaderglobals_ptr = */ &globals,
251 /* groupdata_ptr = */ (void *)nullptr,
252 /* userdata_base_ptr = */ (void *)nullptr,
253 /* output_base_ptr = */ (void *)nullptr,
254 /* shadeindex = */ 0,
255 /* interactive_params_ptr */ (void *)nullptr);
256
257 /* Reset state. */
258 sd->P = P;
259 sd->dP = dP;
260
261 /* Apply bump output to sd->N since it's used for shadow terminator logic, for example. */
262 sd->N = globals.N;
263
264 globals.P = P;
265 globals.dPdx = dPdx;
266 globals.dPdy = dPdy;
267 }
268 }
269
270 unsigned int optix_dc_index = 2 /* NUM_CALLABLE_PROGRAM_GROUPS */ + 1 /* camera program */ +
271 (shader + type * kernel_data.max_shaders);
272 optixDirectCall<void>(optix_dc_index,
273 /* shaderglobals_ptr = */ &globals,
274 /* groupdata_ptr = */ (void *)nullptr,
275 /* userdata_base_ptr = */ (void *)nullptr,
276 /* output_base_ptr = */ (void *)nullptr,
277 /* shadeindex = */ 0,
278 /* interactive_params_ptr */ (void *)nullptr);
279# endif
280
281 if constexpr (type == SHADER_TYPE_DISPLACEMENT) {
282 sd->P = globals.P;
283 }
284 else if (globals.Ci) {
285 flatten_closure_tree(kg, sd, path_visibility, path_flag, globals.Ci);
286 }
287}
288
289#endif
290
ccl_device_inline Spectrum closure_layering_weight(const Spectrum layer_albedo, const Spectrum weight)
Definition bsdf_util.h:440
static void mul(btAlignedObjectArray< T > &items, const Q &value)
static void add(btAlignedObjectArray< T > &items, const Q &value)
ccl_device_forceinline float3 dPdx(const ccl_private ShaderData *sd)
ccl_device_forceinline float3 dPdy(const ccl_private ShaderData *sd)
#define kernel_assert(cond)
#define kernel_data
#define ccl_private
#define ccl_device_inline
#define OSL_RAYTYPE_PACK(visibility, path_flag)
#define CCL_NAMESPACE_END
ccl_device_forceinline float differential_make_compact(const float dD)
ccl_device_forceinline differential3 differential_from_compact(const float3 D, const float dD)
#define layer
bool is_zero(float3x3 a)
const int state
CCL_NAMESPACE_BEGIN ccl_device_forceinline bool is_attribute_found(const ccl_private AttributeDescriptor &desc)
ccl_device_inline AttributeDescriptor find_attribute(const ccl_global AttributeMap *attributes_map, uint attr_offset, const int prim, const uint64_t id)
ccl_device_inline void object_position_transform(KernelGlobals kg, const ccl_private ShaderData *sd, ccl_private T *P)
CCL_NAMESPACE_BEGIN ccl_device_inline void shaderdata_to_shaderglobals(ccl_private ShaderData *sd, const PathRayVisibility path_visibility, const uint32_t path_flag, ccl_private ShaderGlobals *globals)
void osl_eval_nodes(const ThreadKernelGlobalsCPU *kg, ConstIntegratorGenericState state, ShaderData *sd, PathRayVisibility path_visibility, uint32_t path_flag)
ccl_device void flatten_closure_tree(KernelGlobals kg, ccl_private ShaderData *sd, const PathRayVisibility ray_visibility, const uint32_t path_flag, const ccl_private OSLClosure *closure)
@ OSL_CLOSURE_MUL_ID
@ OSL_CLOSURE_ADD_ID
@ SHADER_TYPE_BUMP
@ SHADER_TYPE_SURFACE
@ SHADER_TYPE_DISPLACEMENT
uint32_t PathRayVisibility
@ SR_BACKFACING
@ SD_HAS_DISPLACEMENT
@ SD_HAS_BUMP_FROM_DISPLACEMENT
@ SHADER_MASK
@ ATTR_STD_POSITION_UNDISPLACED
ccl_device_inline float3 one_float3()
Definition math_float3.h:26
CCL_NAMESPACE_BEGIN ccl_device_inline float3 zero_float3()
Definition math_float3.h:17
ccl_device void primitive_normal_set_undisplaced(KernelGlobals kg, ccl_private ShaderData *sd, const int position_undisplaced_offset)
Definition primitive.h:63
CCL_NAMESPACE_BEGIN ccl_device_forceinline T primitive_surface_attribute(KernelGlobals kg, const ccl_private ShaderData *sd, const AttributeDescriptor desc)
Definition primitive.h:32
#define ccl_device
ccl_private OSLClosure * Ci
packed_float3 dPdx
packed_float3 N
packed_float3 dPdy
packed_float3 P
dual< float3 > dual3
Definition types_dual.h:106