Blender  V2.93
shader.h
Go to the documentation of this file.
1 /*
2  * Copyright 2011-2013 Blender Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef __SHADER_H__
18 #define __SHADER_H__
19 
20 #ifdef WITH_OSL
21 /* So no context pollution happens from indirectly included windows.h */
22 # include "util/util_windows.h"
23 # include <OSL/oslexec.h>
24 #endif
25 
26 #include "kernel/kernel_types.h"
27 #include "render/attribute.h"
28 
29 #include "graph/node.h"
30 
31 #include "util/util_map.h"
32 #include "util/util_param.h"
33 #include "util/util_string.h"
34 #include "util/util_thread.h"
35 #include "util/util_types.h"
36 
38 
39 class Device;
40 class DeviceScene;
42 class Mesh;
43 class Progress;
44 class Scene;
45 class ShaderGraph;
46 struct float3;
47 
49 
50 /* Keep those in sync with the python-defined enum. */
55 
57 };
58 
62 
64 };
65 
70 
72 };
73 
74 /* Shader describing the appearance of a Mesh, Light or Background.
75  *
76  * While there is only a single shader graph, it has three outputs: surface,
77  * volume and displacement, that the shader manager will compile and execute
78  * separately. */
79 
80 class Shader : public Node {
81  public:
83 
84  /* shader graph */
86 
87  NODE_SOCKET_API(int, pass_id)
88 
89  /* sampling */
90  NODE_SOCKET_API(bool, use_mis)
91  NODE_SOCKET_API(bool, use_transparent_shadow)
92  NODE_SOCKET_API(bool, heterogeneous_volume)
93  NODE_SOCKET_API(VolumeSampling, volume_sampling_method)
94  NODE_SOCKET_API(int, volume_interpolation_method)
95  NODE_SOCKET_API(float, volume_step_rate)
96 
97  /* displacement */
98  NODE_SOCKET_API(DisplacementMethod, displacement_method)
99 
101 
102  /* synchronization */
106 
107  /* If the shader has only volume components, the surface is assumed to
108  * be transparent.
109  * However, graph optimization might remove the volume subgraph, but
110  * since the user connected something to the volume output the surface
111  * should still be transparent.
112  * Therefore, has_volume_connected stores whether some volume sub-tree
113  * was connected before optimization. */
115 
116  /* information about shader after compiling */
123  bool has_bump;
129 
130  /* requested mesh attributes */
132 
133  /* determined before compiling */
134  uint id;
135  bool used;
136 
137 #ifdef WITH_OSL
138  /* osl shading state references */
139  OSL::ShaderGroupRef osl_surface_ref;
140  OSL::ShaderGroupRef osl_surface_bump_ref;
141  OSL::ShaderGroupRef osl_volume_ref;
142  OSL::ShaderGroupRef osl_displacement_ref;
143 #endif
144 
145  Shader();
146  ~Shader();
147 
148  /* Checks whether the shader consists of just a emission node with fixed inputs that's connected
149  * directly to the output.
150  * If yes, it sets the content of emission to the constant value (color * strength), which is
151  * then used for speeding up light evaluation. */
152  bool is_constant_emission(float3 *emission);
153 
154  void set_graph(ShaderGraph *graph);
155  void tag_update(Scene *scene);
156  void tag_used(Scene *scene);
157 
158  bool need_update_geometry() const;
159 };
160 
161 /* Shader Manager virtual base class
162  *
163  * From this the SVM and OSL shader managers are derived, that do the actual
164  * shader compiling and device updating. */
165 
167  public:
168  enum : uint32_t {
169  SHADER_ADDED = (1 << 0),
170  SHADER_MODIFIED = (1 << 2),
172 
173  /* tag everything in the manager for an update */
174  UPDATE_ALL = ~0u,
175 
177  };
178 
179  static ShaderManager *create(int shadingsystem);
180  virtual ~ShaderManager();
181 
182  virtual void reset(Scene *scene) = 0;
183 
184  virtual bool use_osl()
185  {
186  return false;
187  }
188 
189  /* device update */
190  virtual void device_update(Device *device,
191  DeviceScene *dscene,
192  Scene *scene,
193  Progress &progress) = 0;
194  virtual void device_free(Device *device, DeviceScene *dscene, Scene *scene) = 0;
195 
196  void device_update_common(Device *device, DeviceScene *dscene, Scene *scene, Progress &progress);
197  void device_free_common(Device *device, DeviceScene *dscene, Scene *scene);
198 
199  /* get globally unique id for a type of attribute */
200  uint get_attribute_id(ustring name);
202 
203  /* get shader id for mesh faces */
204  int get_shader_id(Shader *shader, bool smooth = false);
205 
206  /* add default shaders to scene, to use as default for things that don't
207  * have any shader assigned explicitly */
208  static void add_default(Scene *scene);
209 
210  /* Selective nodes compilation. */
212  void get_requested_features(Scene *scene, DeviceRequestedFeatures *requested_features);
213 
214  static void free_memory();
215 
216  float linear_rgb_to_gray(float3 c);
217 
219 
220  void tag_update(Scene *scene, uint32_t flag);
221 
222  bool need_update() const;
223 
224  void init_xyz_transforms();
225 
226  protected:
227  ShaderManager();
228 
230 
231  typedef unordered_map<ustring, uint, ustringHash> AttributeIDMap;
233 
236  static bool beckmann_table_ready;
237 
239 
241  DeviceRequestedFeatures *requested_features);
242 
244 
249 };
250 
252 
253 #endif /* __SHADER_H__ */
typedef float(TangentPoint)[2]
unsigned int uint
Definition: BLI_sys_types.h:83
Definition: device.h:293
string get_cryptomatte_materials(Scene *scene)
Definition: shader.cpp:774
float3 xyz_to_b
Definition: shader.h:247
float3 xyz_to_r
Definition: shader.h:245
void device_update_common(Device *device, DeviceScene *dscene, Scene *scene, Progress &progress)
Definition: shader.cpp:512
static bool beckmann_table_ready
Definition: shader.h:236
static void free_memory()
Definition: shader.cpp:758
virtual void device_free(Device *device, DeviceScene *dscene, Scene *scene)=0
uint get_attribute_id(ustring name)
Definition: shader.cpp:429
AttributeIDMap unique_attribute_id
Definition: shader.h:232
float linear_rgb_to_gray(float3 c)
Definition: shader.cpp:769
unordered_map< ustring, uint, ustringHash > AttributeIDMap
Definition: shader.h:231
void init_xyz_transforms()
Definition: shader.cpp:832
virtual void device_update(Device *device, DeviceScene *dscene, Scene *scene, Progress &progress)=0
size_t beckmann_table_offset
Definition: shader.h:238
thread_spin_lock attribute_lock_
Definition: shader.h:243
void get_requested_features(Scene *scene, DeviceRequestedFeatures *requested_features)
Definition: shader.cpp:729
float3 xyz_to_g
Definition: shader.h:246
virtual ~ShaderManager()
Definition: shader.cpp:406
int get_shader_id(Shader *shader, bool smooth=false)
Definition: shader.cpp:449
void get_requested_graph_features(ShaderGraph *graph, DeviceRequestedFeatures *requested_features)
Definition: shader.cpp:701
bool need_update() const
Definition: shader.cpp:796
static ShaderManager * create(int shadingsystem)
Definition: shader.cpp:410
void device_free_common(Device *device, DeviceScene *dscene, Scene *scene)
Definition: shader.cpp:618
static vector< float > beckmann_table
Definition: shader.h:235
void update_shaders_used(Scene *scene)
Definition: shader.cpp:464
static thread_mutex lookup_table_mutex
Definition: shader.h:234
float3 rgb_to_y
Definition: shader.h:248
void tag_update(Scene *scene, uint32_t flag)
Definition: shader.cpp:790
@ SHADER_ADDED
Definition: shader.h:169
@ INTEGRATOR_MODIFIED
Definition: shader.h:171
@ SHADER_MODIFIED
Definition: shader.h:170
static void add_default(Scene *scene)
Definition: shader.cpp:625
uint32_t update_flags
Definition: shader.h:229
virtual bool use_osl()
Definition: shader.h:184
virtual void reset(Scene *scene)=0
Definition: shader.h:80
bool has_surface_spatial_varying
Definition: shader.h:125
bool has_surface_bssrdf
Definition: shader.h:122
bool need_update_attribute
Definition: shader.h:104
Shader()
Definition: shader.cpp:197
bool has_volume_attribute_dependency
Definition: shader.h:127
void set_graph(ShaderGraph *graph)
Definition: shader.cpp:279
bool need_update_geometry() const
Definition: shader.cpp:391
bool has_volume
Definition: shader.h:120
bool used
Definition: shader.h:135
bool has_surface
Definition: shader.h:117
bool has_bssrdf_bump
Definition: shader.h:124
NODE_DECLARE ShaderGraph * graph
Definition: shader.h:85
float prev_volume_step_rate
Definition: shader.h:100
bool has_integrator_dependency
Definition: shader.h:128
bool need_update_displacement
Definition: shader.h:105
bool has_surface_emission
Definition: shader.h:118
bool need_update_uvs
Definition: shader.h:103
bool has_displacement
Definition: shader.h:121
bool has_bump
Definition: shader.h:123
AttributeRequestSet attributes
Definition: shader.h:131
bool has_surface_transparent
Definition: shader.h:119
void tag_update(Scene *scene)
Definition: shader.cpp:311
bool is_constant_emission(float3 *emission)
Definition: shader.cpp:233
bool has_volume_connected
Definition: shader.h:114
void tag_used(Scene *scene)
Definition: shader.cpp:381
bool has_volume_spatial_varying
Definition: shader.h:126
~Shader()
Definition: shader.cpp:228
Depsgraph * graph
Scene scene
#define CCL_NAMESPACE_END
void KERNEL_FUNCTION_FULL_NAME() shader(KernelGlobals *kg, uint4 *input, float4 *output, int type, int filter, int i, int offset, int sample)
AttributeStandard
Definition: kernel_types.h:744
static unsigned c
Definition: RandGen.cpp:97
#define NODE_SOCKET_API(type_, name)
Definition: node.h:63
#define NODE_DECLARE
Definition: node_type.h:148
VolumeInterpolation
Definition: shader.h:59
@ VOLUME_NUM_INTERPOLATION
Definition: shader.h:63
@ VOLUME_INTERPOLATION_LINEAR
Definition: shader.h:60
@ VOLUME_INTERPOLATION_CUBIC
Definition: shader.h:61
DisplacementMethod
Definition: shader.h:66
@ DISPLACE_NUM_METHODS
Definition: shader.h:71
@ DISPLACE_BUMP
Definition: shader.h:67
@ DISPLACE_TRUE
Definition: shader.h:68
@ DISPLACE_BOTH
Definition: shader.h:69
ShadingSystem
Definition: shader.h:48
@ SHADINGSYSTEM_OSL
Definition: shader.h:48
@ SHADINGSYSTEM_SVM
Definition: shader.h:48
VolumeSampling
Definition: shader.h:51
@ VOLUME_NUM_SAMPLING
Definition: shader.h:56
@ VOLUME_SAMPLING_DISTANCE
Definition: shader.h:52
@ VOLUME_SAMPLING_EQUIANGULAR
Definition: shader.h:53
@ VOLUME_SAMPLING_MULTIPLE_IMPORTANCE
Definition: shader.h:54
unsigned int uint32_t
Definition: stdint.h:83
Definition: node.h:98
CCL_NAMESPACE_BEGIN typedef std::mutex thread_mutex
Definition: util_thread.h:40
tbb::spin_mutex thread_spin_lock
Definition: util_thread.h:68