Blender  V3.3
scene/shader.h
Go to the documentation of this file.
1 /* SPDX-License-Identifier: Apache-2.0
2  * Copyright 2011-2022 Blender Foundation */
3 
4 #ifndef __SHADER_H__
5 #define __SHADER_H__
6 
7 #ifdef WITH_OSL
8 /* So no context pollution happens from indirectly included windows.h */
9 # include "util/windows.h"
10 # include <OSL/oslexec.h>
11 #endif
12 
13 #include "kernel/types.h"
14 #include "scene/attribute.h"
15 
16 #include "graph/node.h"
17 
18 #include "util/map.h"
19 #include "util/param.h"
20 #include "util/string.h"
21 #include "util/thread.h"
22 #include "util/types.h"
23 
25 
26 class Device;
27 class DeviceScene;
28 class Mesh;
29 class Progress;
30 class Scene;
31 class ShaderGraph;
32 struct float3;
33 
35 
36 /* Keep those in sync with the python-defined enum. */
41 
43 };
44 
48 
50 };
51 
56 
58 };
59 
60 /* Shader describing the appearance of a Mesh, Light or Background.
61  *
62  * While there is only a single shader graph, it has three outputs: surface,
63  * volume and displacement, that the shader manager will compile and execute
64  * separately. */
65 
66 class Shader : public Node {
67  public:
69 
70  /* shader graph */
72 
73  NODE_SOCKET_API(int, pass_id)
74 
75  /* sampling */
76  NODE_SOCKET_API(bool, use_mis)
77  NODE_SOCKET_API(bool, use_transparent_shadow)
78  NODE_SOCKET_API(bool, heterogeneous_volume)
79  NODE_SOCKET_API(VolumeSampling, volume_sampling_method)
80  NODE_SOCKET_API(int, volume_interpolation_method)
81  NODE_SOCKET_API(float, volume_step_rate)
82 
83  /* displacement */
84  NODE_SOCKET_API(DisplacementMethod, displacement_method)
85 
87 
88  /* synchronization */
92 
93  /* If the shader has only volume components, the surface is assumed to
94  * be transparent.
95  * However, graph optimization might remove the volume subgraph, but
96  * since the user connected something to the volume output the surface
97  * should still be transparent.
98  * Therefore, has_volume_connected stores whether some volume sub-tree
99  * was connected before optimization. */
101 
102  /* information about shader after compiling */
110  bool has_bump;
116 
117  /* requested mesh attributes */
119 
120  /* determined before compiling */
121  uint id;
122 
123 #ifdef WITH_OSL
124  /* osl shading state references */
125  OSL::ShaderGroupRef osl_surface_ref;
126  OSL::ShaderGroupRef osl_surface_bump_ref;
127  OSL::ShaderGroupRef osl_volume_ref;
128  OSL::ShaderGroupRef osl_displacement_ref;
129 #endif
130 
131  Shader();
132  ~Shader();
133 
134  /* Checks whether the shader consists of just a emission node with fixed inputs that's connected
135  * directly to the output.
136  * If yes, it sets the content of emission to the constant value (color * strength), which is
137  * then used for speeding up light evaluation. */
138  bool is_constant_emission(float3 *emission);
139 
140  void set_graph(ShaderGraph *graph);
141  void tag_update(Scene *scene);
142  void tag_used(Scene *scene);
143 
144  /* Return true when either of the surface or displacement socket of the output node is linked.
145  * This should be used to ensure that surface attributes are also requested even when only the
146  * displacement socket is linked. */
147  bool has_surface_link() const
148  {
149  return has_surface || has_displacement;
150  }
151 
152  bool need_update_geometry() const;
153 };
154 
155 /* Shader Manager virtual base class
156  *
157  * From this the SVM and OSL shader managers are derived, that do the actual
158  * shader compiling and device updating. */
159 
161  public:
162  enum : uint32_t {
163  SHADER_ADDED = (1 << 0),
164  SHADER_MODIFIED = (1 << 2),
166 
167  /* tag everything in the manager for an update */
168  UPDATE_ALL = ~0u,
169 
171  };
172 
173  static ShaderManager *create(int shadingsystem);
174  virtual ~ShaderManager();
175 
176  virtual void reset(Scene *scene) = 0;
177 
178  virtual bool use_osl()
179  {
180  return false;
181  }
182 
183  /* device update */
184  void device_update(Device *device, DeviceScene *dscene, Scene *scene, Progress &progress);
185  virtual void device_update_specific(Device *device,
186  DeviceScene *dscene,
187  Scene *scene,
188  Progress &progress) = 0;
189  virtual void device_free(Device *device, DeviceScene *dscene, Scene *scene) = 0;
190 
191  void device_update_common(Device *device, DeviceScene *dscene, Scene *scene, Progress &progress);
192  void device_free_common(Device *device, DeviceScene *dscene, Scene *scene);
193 
194  /* get globally unique id for a type of attribute */
195  uint get_attribute_id(ustring name);
197 
198  /* get shader id for mesh faces */
199  int get_shader_id(Shader *shader, bool smooth = false);
200 
201  /* add default shaders to scene, to use as default for things that don't
202  * have any shader assigned explicitly */
203  static void add_default(Scene *scene);
204 
205  /* Selective nodes compilation. */
207 
208  static void free_memory();
209 
210  float linear_rgb_to_gray(float3 c);
212 
214 
215  void tag_update(Scene *scene, uint32_t flag);
216 
217  bool need_update() const;
218 
219  void init_xyz_transforms();
220 
221  protected:
222  ShaderManager();
223 
225 
226  typedef unordered_map<ustring, uint, ustringHash> AttributeIDMap;
228 
231  static bool beckmann_table_ready;
232 
234 
236 
238 
246  bool is_rec709;
247 };
248 
250 
251 #endif /* __SHADER_H__ */
typedef float(TangentPoint)[2]
unsigned int uint
Definition: BLI_sys_types.h:67
string get_cryptomatte_materials(Scene *scene)
void device_update(Device *device, DeviceScene *dscene, Scene *scene, Progress &progress)
void device_update_common(Device *device, DeviceScene *dscene, Scene *scene, Progress &progress)
static bool beckmann_table_ready
Definition: scene/shader.h:231
static void free_memory()
virtual void device_free(Device *device, DeviceScene *dscene, Scene *scene)=0
uint get_attribute_id(ustring name)
AttributeIDMap unique_attribute_id
Definition: scene/shader.h:227
float linear_rgb_to_gray(float3 c)
unordered_map< ustring, uint, ustringHash > AttributeIDMap
Definition: scene/shader.h:226
void init_xyz_transforms()
virtual void device_update_specific(Device *device, DeviceScene *dscene, Scene *scene, Progress &progress)=0
size_t beckmann_table_offset
Definition: scene/shader.h:233
thread_spin_lock attribute_lock_
Definition: scene/shader.h:237
virtual ~ShaderManager()
int get_shader_id(Shader *shader, bool smooth=false)
float3 rec709_to_b
Definition: scene/shader.h:245
bool need_update() const
static ShaderManager * create(int shadingsystem)
void device_free_common(Device *device, DeviceScene *dscene, Scene *scene)
static vector< float > beckmann_table
Definition: scene/shader.h:230
static thread_mutex lookup_table_mutex
Definition: scene/shader.h:229
void tag_update(Scene *scene, uint32_t flag)
uint get_kernel_features(Scene *scene)
float3 rec709_to_g
Definition: scene/shader.h:244
float3 rec709_to_scene_linear(float3 c)
float3 rec709_to_r
Definition: scene/shader.h:243
static void add_default(Scene *scene)
uint32_t update_flags
Definition: scene/shader.h:224
virtual bool use_osl()
Definition: scene/shader.h:178
uint get_graph_kernel_features(ShaderGraph *graph)
virtual void reset(Scene *scene)=0
bool has_surface_spatial_varying
Definition: scene/shader.h:112
bool has_surface_link() const
Definition: scene/shader.h:147
bool has_surface_bssrdf
Definition: scene/shader.h:109
bool need_update_attribute
Definition: scene/shader.h:90
bool has_volume_attribute_dependency
Definition: scene/shader.h:114
void set_graph(ShaderGraph *graph)
bool need_update_geometry() const
bool has_volume
Definition: scene/shader.h:107
bool has_surface
Definition: scene/shader.h:103
bool has_bssrdf_bump
Definition: scene/shader.h:111
NODE_DECLARE ShaderGraph * graph
Definition: scene/shader.h:71
float prev_volume_step_rate
Definition: scene/shader.h:86
bool has_integrator_dependency
Definition: scene/shader.h:115
bool need_update_displacement
Definition: scene/shader.h:91
bool has_surface_emission
Definition: scene/shader.h:104
bool has_surface_raytrace
Definition: scene/shader.h:106
bool need_update_uvs
Definition: scene/shader.h:89
bool has_displacement
Definition: scene/shader.h:108
bool has_bump
Definition: scene/shader.h:110
AttributeRequestSet attributes
Definition: scene/shader.h:118
bool has_surface_transparent
Definition: scene/shader.h:105
void tag_update(Scene *scene)
bool is_constant_emission(float3 *emission)
bool has_volume_connected
Definition: scene/shader.h:100
void tag_used(Scene *scene)
bool has_volume_spatial_varying
Definition: scene/shader.h:113
#define CCL_NAMESPACE_END
Definition: cuda/compat.h:9
Depsgraph * graph
Scene scene
#define NODE_SOCKET_API(type_, name)
Definition: graph/node.h:54
AttributeStandard
Definition: kernel/types.h:612
static unsigned c
Definition: RandGen.cpp:83
#define NODE_DECLARE
Definition: node_type.h:135
smooth(Type::FLOAT, "mask_weight")
VolumeInterpolation
Definition: scene/shader.h:45
@ VOLUME_NUM_INTERPOLATION
Definition: scene/shader.h:49
@ VOLUME_INTERPOLATION_LINEAR
Definition: scene/shader.h:46
@ VOLUME_INTERPOLATION_CUBIC
Definition: scene/shader.h:47
DisplacementMethod
Definition: scene/shader.h:52
@ DISPLACE_NUM_METHODS
Definition: scene/shader.h:57
@ DISPLACE_BUMP
Definition: scene/shader.h:53
@ DISPLACE_TRUE
Definition: scene/shader.h:54
@ DISPLACE_BOTH
Definition: scene/shader.h:55
ShadingSystem
Definition: scene/shader.h:34
@ SHADINGSYSTEM_OSL
Definition: scene/shader.h:34
@ SHADINGSYSTEM_SVM
Definition: scene/shader.h:34
VolumeSampling
Definition: scene/shader.h:37
@ VOLUME_NUM_SAMPLING
Definition: scene/shader.h:42
@ VOLUME_SAMPLING_DISTANCE
Definition: scene/shader.h:38
@ VOLUME_SAMPLING_EQUIANGULAR
Definition: scene/shader.h:39
@ VOLUME_SAMPLING_MULTIPLE_IMPORTANCE
Definition: scene/shader.h:40
unsigned int uint32_t
Definition: stdint.h:80
CCL_NAMESPACE_BEGIN typedef std::mutex thread_mutex
Definition: thread.h:27
tbb::spin_mutex thread_spin_lock
Definition: thread.h:52