Blender V5.3
scene/shader.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#ifdef WITH_OSL
8# include <cstdint> /* Needed before `sdlexec.h` for `int32_t` with GCC 15.1. */
9/* So no context pollution happens from indirectly included windows.h */
10# ifdef _WIN32
11# include "util/windows.h"
12# endif
13# include <OSL/oslexec.h>
14#endif
15
16#include "kernel/types.h"
17#include "scene/attribute.h"
18
19#include "graph/node.h"
20
21#include "util/map.h"
22#include "util/param.h"
23#include "util/string.h"
24#include "util/thread.h"
25#include "util/types.h"
26#include "util/unique_ptr.h"
27
29
30class Device;
31class DeviceScene;
32class Mesh;
33class Progress;
34class Scene;
35class ShaderGraph;
36struct float3;
37
39
40/* Keep those in sync with the python-defined enum. */
41
49
56
64
65/* Shader describing the appearance of a Mesh, Light or Background.
66 *
67 * While there is only a single shader graph, it has three outputs: surface,
68 * volume and displacement, that the shader manager will compile and execute
69 * separately. */
70
71class Shader : public Node {
72 public:
74
75 /* shader graph */
77
78 NODE_SOCKET_API(int, pass_id)
79
80 /* sampling */
81 NODE_SOCKET_API(EmissionSampling, emission_sampling_method)
82 NODE_SOCKET_API(bool, use_transparent_shadow)
83 NODE_SOCKET_API(bool, use_bump_map_correction)
84 NODE_SOCKET_API(VolumeSampling, volume_sampling_method)
85 NODE_SOCKET_API(int, volume_interpolation_method)
86 NODE_SOCKET_API(float, volume_step_rate)
87
88 /* displacement */
89 NODE_SOCKET_API(DisplacementMethod, displacement_method)
90
93
94 /* synchronization */
100
101 /* If the shader has only volume components, the surface is assumed to
102 * be transparent.
103 * However, graph optimization might remove the volume subgraph, but
104 * since the user connected something to the volume output the surface
105 * should still be transparent.
106 * Therefore, has_volume_connected stores whether some volume sub-tree
107 * was connected before optimization. */
109
110 /* information about shader after compiling */
127
131
132 /* requested mesh attributes */
135
136 /* determined before compiling */
137 uint id;
138
139#ifdef WITH_OSL
140 /* Compiled osl shading state references. */
141 struct OSLCache {
142 OSL::ShaderGroupRef surface;
143 OSL::ShaderGroupRef bump;
144 OSL::ShaderGroupRef displacement;
145 OSL::ShaderGroupRef volume;
146 };
147 map<Device *, OSLCache> osl_cache;
148#endif
149
150 Shader();
151
152 /* Estimate emission of this shader based on the shader graph. This works only in very simple
153 * cases. But it helps improve light importance sampling in common cases.
154 *
155 * If the emission is fully constant, returns true, so that shader evaluation can be skipped
156 * entirely for a light. */
157 void estimate_emission();
158
160 void tag_update(Scene *scene);
161 void tag_used(Scene *scene);
162
163 /* Return true when either of the surface or displacement socket of the output node is linked.
164 * This should be used to ensure that surface attributes are also requested even when only the
165 * displacement socket is linked. */
166 bool has_surface_link() const
167 {
169 }
170
171 bool need_update_geometry() const;
172
174};
175
176/* Shader Manager virtual base class
177 *
178 * From this the SVM and OSL shader managers are derived, that do the actual
179 * shader compiling and device updating. */
180
182 public:
183 enum : uint32_t {
184 SHADER_ADDED = (1 << 0),
185 SHADER_MODIFIED = (1 << 2),
186
187 /* tag everything in the manager for an update */
189
191 };
192
193 static unique_ptr<ShaderManager> create(const int shadingsystem);
194 virtual ~ShaderManager();
195
196 virtual bool use_osl()
197 {
198 return false;
199 }
200
201 /* device update */
202 void device_update_pre(Device *device, DeviceScene *dscene, Scene *scene, Progress &progress);
203 void device_update_post(Device *device, DeviceScene *dscene, Scene *scene, Progress &progress);
204 virtual void device_free(Device *device, DeviceScene *dscene, Scene *scene) = 0;
205
206 /* get globally unique id for a type of attribute */
207 virtual uint64_t get_attribute_id(ustring name);
209
210 /* get shader id for mesh faces */
211 int get_shader_id(const Shader *shader, bool smooth = false);
212
213 /* add default shaders to scene, to use as default for things that don't
214 * have any shader assigned explicitly */
215 static void add_default(Scene *scene);
216
217 /* Selective nodes compilation. */
219
220 float linear_rgb_to_gray(const float3 c);
222
223 string get_cryptomatte_materials(Scene *scene);
224
225 void tag_update(Scene *scene, const uint32_t flag);
226
227 bool need_update() const;
228
229 void init_xyz_transforms();
230
232 {
234 }
235
236 protected:
238
239 uint32_t update_flags;
240
241 using AttributeIDMap = unordered_map<ustring, uint64_t>;
243
245
246 unordered_map<const float *, size_t> bsdf_tables;
248
250
261
262 template<std::size_t n>
263 size_t ensure_bsdf_table(DeviceScene *dscene, Scene *scene, const float (&table)[n])
264 {
265 return ensure_bsdf_table_impl(dscene, scene, table, n);
266 }
267 size_t ensure_bsdf_table_impl(DeviceScene *dscene,
268 Scene *scene,
269 const float *table,
270 const size_t n);
271
273
275
276 virtual void device_update_specific(Device *device,
277 DeviceScene *dscene,
278 Scene *scene,
279 Progress &progress) = 0;
280
281 void device_update_common(Device *device, DeviceScene *dscene, Scene *scene, Progress &progress);
282 void device_free_common(Device *device, DeviceScene *dscene, Scene *scene);
283};
284
unsigned long long int uint64_t
Definition scene.h:133
unordered_map< ustring, uint64_t > AttributeIDMap
string get_cryptomatte_materials(Scene *scene)
size_t thin_film_table_offset_
float3 rec709_to_scene_linear(const float3 c)
static unique_ptr< ShaderManager > create(const int shadingsystem)
void device_update_common(Device *device, DeviceScene *dscene, Scene *scene, Progress &progress)
virtual void device_free(Device *device, DeviceScene *dscene, Scene *scene)=0
size_t ensure_bsdf_table_impl(DeviceScene *dscene, Scene *scene, const float *table, const size_t n)
void device_update_pre(Device *device, DeviceScene *dscene, Scene *scene, Progress &progress)
AttributeIDMap unique_attribute_id
float linear_rgb_to_gray(const float3 c)
virtual uint64_t get_attribute_id(ustring name)
void init_xyz_transforms()
virtual void device_update_specific(Device *device, DeviceScene *dscene, Scene *scene, Progress &progress)=0
thread_spin_lock attribute_lock_
const string & get_scene_linear_interop_id()
virtual ~ShaderManager()
void compute_thin_film_table(const Transform &xyz_to_rgb)
unordered_map< const float *, size_t > bsdf_tables
void device_update_post(Device *device, DeviceScene *dscene, Scene *scene, Progress &progress)
int get_shader_id(const Shader *shader, bool smooth=false)
bool need_update() const
void tag_update(Scene *scene, const uint32_t flag)
void device_free_common(Device *device, DeviceScene *dscene, Scene *scene)
string scene_linear_interop_id
static thread_mutex lookup_table_mutex
uint64_t get_kernel_features(Scene *scene)
uint64_t get_graph_kernel_features(ShaderGraph *graph)
size_t ensure_bsdf_table(DeviceScene *dscene, Scene *scene, const float(&table)[n])
static void add_default(Scene *scene)
uint32_t update_flags
virtual bool use_osl()
vector< float > thin_film_table
bool has_dispersion
AttributeRequestSet global_attributes
bool has_surface_spatial_varying
bool has_surface_link() const
bool has_bump_from_surface
bool has_surface_bssrdf
void estimate_emission()
bool need_update_attribute
bool has_volume_attribute_dependency
bool need_update_geometry() const
void set_graph(unique_ptr< ShaderGraph > &&graph)
bool has_volume
bool emission_is_constant
float3 emission_estimate
bool has_surface
bool has_light_path_node
bool has_time_dependency
bool has_bssrdf_bump
float prev_volume_step_rate
bool has_bump_from_displacement
bool need_update_displacement
EmissionSampling emission_sampling
bool shadow_transparency_needs_realloc
bool has_surface_raytrace
bool need_update_uvs
bool has_displacement
AttributeRequestSet attributes
bool has_surface_transparent
void tag_update(Scene *scene)
NODE_DECLARE unique_ptr< ShaderGraph > graph
bool need_update_shadow_transparency
bool prev_has_surface_shadow_transparency
bool has_aov_output_node
bool has_surface_shadow_transparency() const
bool has_volume_connected
void tag_used(Scene *scene)
bool has_volume_spatial_varying
#define CCL_NAMESPACE_END
#define smooth
#define NODE_SOCKET_API(type_, name)
Definition graph/node.h:56
EmissionSampling
AttributeStandard
color xyz_to_rgb(float x, float y, float z)
Definition node_color.h:73
#define NODE_DECLARE
Definition node_type.h:150
VolumeInterpolation
@ VOLUME_NUM_INTERPOLATION
@ VOLUME_INTERPOLATION_LINEAR
@ VOLUME_INTERPOLATION_CUBIC
DisplacementMethod
@ DISPLACE_NUM_METHODS
@ DISPLACE_BUMP
@ DISPLACE_TRUE
@ DISPLACE_BOTH
ShadingSystem
@ SHADINGSYSTEM_OSL
@ SHADINGSYSTEM_SVM
VolumeSampling
@ VOLUME_NUM_SAMPLING
@ VOLUME_SAMPLING_DISTANCE
@ VOLUME_SAMPLING_EQUIANGULAR
@ VOLUME_SAMPLING_MULTIPLE_IMPORTANCE
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
Node(const NodeType *type, ustring name=ustring())
std::mutex thread_mutex
Definition thread.h:27
tbb::spin_mutex thread_spin_lock
Definition thread.h:53