Blender V4.5
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(bool, heterogeneous_volume)
85 NODE_SOCKET_API(VolumeSampling, volume_sampling_method)
86 NODE_SOCKET_API(int, volume_interpolation_method)
87 NODE_SOCKET_API(float, volume_step_rate)
88
89 /* displacement */
90 NODE_SOCKET_API(DisplacementMethod, displacement_method)
91
93
94 /* synchronization */
98
99 /* If the shader has only volume components, the surface is assumed to
100 * be transparent.
101 * However, graph optimization might remove the volume subgraph, but
102 * since the user connected something to the volume output the surface
103 * should still be transparent.
104 * Therefore, has_volume_connected stores whether some volume sub-tree
105 * was connected before optimization. */
107
108 /* information about shader after compiling */
120
124
125 /* requested mesh attributes */
127
128 /* determined before compiling */
130
131#ifdef WITH_OSL
132 /* osl shading state references */
133 OSL::ShaderGroupRef osl_surface_ref;
134 OSL::ShaderGroupRef osl_surface_bump_ref;
135 OSL::ShaderGroupRef osl_volume_ref;
136 OSL::ShaderGroupRef osl_displacement_ref;
137#endif
138
139 Shader();
140
141 /* Estimate emission of this shader based on the shader graph. This works only in very simple
142 * cases. But it helps improve light importance sampling in common cases.
143 *
144 * If the emission is fully constant, returns true, so that shader evaluation can be skipped
145 * entirely for a light. */
146 void estimate_emission();
147
149 void tag_update(Scene *scene);
150 void tag_used(Scene *scene);
151
152 /* Return true when either of the surface or displacement socket of the output node is linked.
153 * This should be used to ensure that surface attributes are also requested even when only the
154 * displacement socket is linked. */
155 bool has_surface_link() const
156 {
158 }
159
160 bool need_update_geometry() const;
161};
162
163/* Shader Manager virtual base class
164 *
165 * From this the SVM and OSL shader managers are derived, that do the actual
166 * shader compiling and device updating. */
167
169 public:
170 enum : uint32_t {
171 SHADER_ADDED = (1 << 0),
172 SHADER_MODIFIED = (1 << 2),
173
174 /* tag everything in the manager for an update */
176
178 };
179
180 static unique_ptr<ShaderManager> create(const int shadingsystem);
181 virtual ~ShaderManager();
182
183 virtual bool use_osl()
184 {
185 return false;
186 }
187
188 /* device update */
189 void device_update_pre(Device *device, DeviceScene *dscene, Scene *scene, Progress &progress);
190 void device_update_post(Device *device, DeviceScene *dscene, Scene *scene, Progress &progress);
191 virtual void device_free(Device *device, DeviceScene *dscene, Scene *scene) = 0;
192
193 /* get globally unique id for a type of attribute */
194 virtual uint64_t get_attribute_id(ustring name);
196
197 /* get shader id for mesh faces */
198 int get_shader_id(Shader *shader, bool smooth = false);
199
200 /* add default shaders to scene, to use as default for things that don't
201 * have any shader assigned explicitly */
202 static void add_default(Scene *scene);
203
204 /* Selective nodes compilation. */
206
207 float linear_rgb_to_gray(const float3 c);
209
210 string get_cryptomatte_materials(Scene *scene);
211
212 void tag_update(Scene *scene, const uint32_t flag);
213
214 bool need_update() const;
215
216 void init_xyz_transforms();
217
218 protected:
220
221 uint32_t update_flags;
222
223 using AttributeIDMap = unordered_map<ustring, uint64_t>;
225
227
228 unordered_map<const float *, size_t> bsdf_tables;
229
231
241
242 template<std::size_t n>
243 size_t ensure_bsdf_table(DeviceScene *dscene, Scene *scene, const float (&table)[n])
244 {
245 return ensure_bsdf_table_impl(dscene, scene, table, n);
246 }
247 size_t ensure_bsdf_table_impl(DeviceScene *dscene,
248 Scene *scene,
249 const float *table,
250 const size_t n);
251
253
254 virtual void device_update_specific(Device *device,
255 DeviceScene *dscene,
256 Scene *scene,
257 Progress &progress) = 0;
258
259 void device_update_common(Device *device, DeviceScene *dscene, Scene *scene, Progress &progress);
260 void device_free_common(Device *device, DeviceScene *dscene, Scene *scene);
261};
262
unsigned int uint
float progress
Definition WM_types.hh:1019
unsigned long long int uint64_t
unordered_map< ustring, uint64_t > AttributeIDMap
string get_cryptomatte_materials(Scene *scene)
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_
virtual ~ShaderManager()
int get_shader_id(Shader *shader, bool smooth=false)
unordered_map< const float *, size_t > bsdf_tables
void device_update_post(Device *device, DeviceScene *dscene, Scene *scene, Progress &progress)
bool need_update() const
void tag_update(Scene *scene, const uint32_t flag)
void device_free_common(Device *device, DeviceScene *dscene, Scene *scene)
static thread_mutex lookup_table_mutex
uint get_kernel_features(Scene *scene)
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()
uint get_graph_kernel_features(ShaderGraph *graph)
bool has_surface_spatial_varying
bool has_surface_link() const
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_bssrdf_bump
float prev_volume_step_rate
bool need_update_displacement
EmissionSampling emission_sampling
bool has_surface_raytrace
bool need_update_uvs
bool has_displacement
bool has_bump
AttributeRequestSet attributes
bool has_surface_transparent
void tag_update(Scene *scene)
NODE_DECLARE unique_ptr< ShaderGraph > graph
bool has_volume_connected
void tag_used(Scene *scene)
bool has_volume_spatial_varying
#define CCL_NAMESPACE_END
#define NODE_SOCKET_API(type_, name)
Definition graph/node.h:55
AttributeStandard
EmissionSampling
#define NODE_DECLARE
Definition node_type.h:142
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
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
uint8_t flag
Definition wm_window.cc:139