Blender V4.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#ifndef __SHADER_H__
6#define __SHADER_H__
7
8#ifdef WITH_OSL
9/* So no context pollution happens from indirectly included windows.h */
10# include "util/windows.h"
11# include <OSL/oslexec.h>
12#endif
13
14#include "kernel/types.h"
15#include "scene/attribute.h"
16
17#include "graph/node.h"
18
19#include "util/map.h"
20#include "util/param.h"
21#include "util/string.h"
22#include "util/thread.h"
23#include "util/types.h"
24
26
27class Device;
28class DeviceScene;
29class Mesh;
30class Progress;
31class Scene;
32class ShaderGraph;
33struct float3;
34
36
37/* Keep those in sync with the python-defined enum. */
38
46
53
61
62/* Shader describing the appearance of a Mesh, Light or Background.
63 *
64 * While there is only a single shader graph, it has three outputs: surface,
65 * volume and displacement, that the shader manager will compile and execute
66 * separately. */
67
68class Shader : public Node {
69 public:
71
72 /* shader graph */
74
75 NODE_SOCKET_API(int, pass_id)
76
77 /* sampling */
78 NODE_SOCKET_API(EmissionSampling, emission_sampling_method)
79 NODE_SOCKET_API(bool, use_transparent_shadow)
80 NODE_SOCKET_API(bool, use_bump_map_correction)
81 NODE_SOCKET_API(bool, heterogeneous_volume)
82 NODE_SOCKET_API(VolumeSampling, volume_sampling_method)
83 NODE_SOCKET_API(int, volume_interpolation_method)
84 NODE_SOCKET_API(float, volume_step_rate)
85
86 /* displacement */
87 NODE_SOCKET_API(DisplacementMethod, displacement_method)
88
90
91 /* synchronization */
95
96 /* If the shader has only volume components, the surface is assumed to
97 * be transparent.
98 * However, graph optimization might remove the volume subgraph, but
99 * since the user connected something to the volume output the surface
100 * should still be transparent.
101 * Therefore, has_volume_connected stores whether some volume sub-tree
102 * was connected before optimization. */
104
105 /* information about shader after compiling */
117
121
122 /* requested mesh attributes */
124
125 /* determined before compiling */
127
128#ifdef WITH_OSL
129 /* osl shading state references */
130 OSL::ShaderGroupRef osl_surface_ref;
131 OSL::ShaderGroupRef osl_surface_bump_ref;
132 OSL::ShaderGroupRef osl_volume_ref;
133 OSL::ShaderGroupRef osl_displacement_ref;
134#endif
135
136 Shader();
137 ~Shader();
138
139 /* Estimate emission of this shader based on the shader graph. This works only in very simple
140 * cases. But it helps improve light importance sampling in common cases.
141 *
142 * If the emission is fully constant, returns true, so that shader evaluation can be skipped
143 * entirely for a light. */
144 void estimate_emission();
145
146 void set_graph(ShaderGraph *graph);
147 void tag_update(Scene *scene);
148 void tag_used(Scene *scene);
149
150 /* Return true when either of the surface or displacement socket of the output node is linked.
151 * This should be used to ensure that surface attributes are also requested even when only the
152 * displacement socket is linked. */
153 bool has_surface_link() const
154 {
156 }
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),
171
172 /* tag everything in the manager for an update */
174
176 };
177
178 static ShaderManager *create(int shadingsystem, Device *device);
179 virtual ~ShaderManager();
180
181 virtual void reset(Scene *scene) = 0;
182
183 virtual bool use_osl()
184 {
185 return false;
186 }
187
188 /* device update */
189 void device_update(Device *device, DeviceScene *dscene, Scene *scene, Progress &progress);
190 virtual void device_update_specific(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 virtual uint64_t 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
213 static void free_memory();
214
215 float linear_rgb_to_gray(float3 c);
217
218 string get_cryptomatte_materials(Scene *scene);
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:
228
230
231 typedef unordered_map<ustring, uint64_t, ustringHash> AttributeIDMap;
233
235
236 unordered_map<const float *, size_t> bsdf_tables;
237
238 template<std::size_t n>
239 size_t ensure_bsdf_table(DeviceScene *dscene, Scene *scene, const float (&table)[n])
240 {
241 return ensure_bsdf_table_impl(dscene, scene, table, n);
242 }
243 size_t ensure_bsdf_table_impl(DeviceScene *dscene, Scene *scene, const float *table, size_t n);
244
246
248
258};
259
261
262#endif /* __SHADER_H__ */
unsigned int uint
string get_cryptomatte_materials(Scene *scene)
static ShaderManager * create(int shadingsystem, Device *device)
void device_update(Device *device, DeviceScene *dscene, Scene *scene, Progress &progress)
void device_update_common(Device *device, DeviceScene *dscene, Scene *scene, Progress &progress)
static void free_memory()
virtual void device_free(Device *device, DeviceScene *dscene, Scene *scene)=0
AttributeIDMap unique_attribute_id
float linear_rgb_to_gray(float3 c)
unordered_map< ustring, uint64_t, ustringHash > AttributeIDMap
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
bool need_update() const
void device_free_common(Device *device, DeviceScene *dscene, Scene *scene)
static thread_mutex lookup_table_mutex
void tag_update(Scene *scene, uint32_t flag)
uint get_kernel_features(Scene *scene)
size_t ensure_bsdf_table_impl(DeviceScene *dscene, Scene *scene, const float *table, size_t n)
float3 rec709_to_scene_linear(float3 c)
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)
virtual void reset(Scene *scene)=0
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
void set_graph(ShaderGraph *graph)
bool need_update_geometry() const
bool has_volume
bool emission_is_constant
float3 emission_estimate
bool has_surface
bool has_bssrdf_bump
NODE_DECLARE ShaderGraph * graph
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
bool has_surface_transparent
void tag_update(Scene *scene)
bool has_volume_connected
void tag_used(Scene *scene)
bool has_volume_spatial_varying
#define CCL_NAMESPACE_END
draw_view in_light_buf[] float
#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
unsigned int uint32_t
Definition stdint.h:80
unsigned __int64 uint64_t
Definition stdint.h:90
CCL_NAMESPACE_BEGIN typedef std::mutex thread_mutex
Definition thread.h:29
tbb::spin_mutex thread_spin_lock
Definition thread.h:55
uint8_t flag
Definition wm_window.cc:138