Blender V5.3
scene/osl.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#include <atomic>
8
9#include "device/device.h"
10
11#include "util/array.h"
12#include "util/map.h"
13#include "util/set.h"
14#include "util/string.h"
15
16#include "scene/shader.h"
17#include "scene/shader_graph.h"
18#include "scene/shader_nodes.h"
19
20#ifdef WITH_OSL
21# include <OSL/llvm_util.h>
22# include <OSL/oslcomp.h>
23# include <OSL/oslexec.h>
24# include <OSL/oslquery.h>
25#endif
26
28
29class Device;
30class DeviceScene;
31class ImageHandle;
32class ImageManager;
33class ImageSingle;
35struct OSLGlobals;
36class Scene;
37class ShaderGraph;
38class ShaderNode;
39class ShaderInput;
40class ShaderOutput;
41
42#ifdef WITH_OSL
43
44/* OSL Shader Info
45 * to auto detect closures in the shader for MIS and transparent shadows */
46
47struct OSLShaderInfo {
48 OSLShaderInfo() = default;
49
50 OSL::OSLQuery query;
51 bool has_surface_emission = false;
52 bool has_surface_transparent = false;
53 bool has_surface_bssrdf = false;
54};
55
56#endif
57
59 public:
60 OSLManager(Device *device);
62
63 static void free_memory();
64
65 void reset(Scene *scene);
66
67 void device_update_pre(Device *device, Scene *scene);
68 void device_update_post(Device *device,
69 Scene *scene,
70 Progress &progress,
71 const bool reload_kernels);
72 void device_free(Device *device, DeviceScene *dscene, Scene *scene);
73
74#ifdef WITH_OSL
75 /* osl compile and query */
76 static bool osl_compile(const string &inputfile, const string &outputfile);
77 static bool osl_query(OSL::OSLQuery &query, const string &filepath);
78
79 /* shader file loading, all functions return pointer to hash string if found */
80 const char *shader_test_loaded(const string &hash);
81 const char *shader_load_bytecode(const string &hash, const string &bytecode);
82 const char *shader_load_filepath(string filepath);
83 OSLShaderInfo *shader_loaded_info(const string &hash);
84
85 void shading_system_init(const string &colorspace_interop_id);
86
87 OSL::ShadingSystem *get_shading_system(Device *sub_device);
88 OSL::TextureSystem *get_texture_system();
89 static void foreach_osl_device(Device *device,
90 const std::function<void(Device *, OSLGlobals *)> &callback);
91#endif
92
93 void tag_update();
94 bool need_update() const;
95
96 private:
97#ifdef WITH_OSL
98 void shading_system_free();
99
100 void foreach_shading_system(const std::function<void(OSL::ShadingSystem *)> &callback);
101 void foreach_render_services(const std::function<void(OSLRenderServices *)> &callback);
102
103 Device *device_;
104 map<string, OSLShaderInfo> loaded_shaders;
105
106 map<DeviceType, std::shared_ptr<OSL::ShadingSystem>> ss_map;
107
108 bool need_update_;
109#endif
110};
111
112#ifdef WITH_OSL
113
114/* Shader Manage */
115
116class OSLShaderManager : public ShaderManager {
117 public:
118 OSLShaderManager() = default;
119 ~OSLShaderManager() override = default;
120
121 bool use_osl() override
122 {
123 return true;
124 }
125
126 uint64_t get_attribute_id(ustring name) override;
127 uint64_t get_attribute_id(AttributeStandard std) override;
128
129 void device_update_specific(Device *device,
130 DeviceScene *dscene,
131 Scene *scene,
132 Progress &progress) override;
133 void device_free(Device *device, DeviceScene *dscene, Scene *scene) override;
134
135 /* create OSL node using OSLQuery */
136 static OSLNode *osl_node(ShaderGraph *graph,
137 Scene *scene,
138 const std::string &filepath,
139 const std::string &bytecode_hash = "",
140 const std::string &bytecode = "");
141
142 /* Get image handles used by OSL services on device. */
143 static void osl_image_handles(Device *device,
144 ImageManager *image_manager,
145 set<const ImageSingle *> &image_handles);
146};
147
148#endif
149
150/* Graph Compiler */
151
153 public:
154#ifdef WITH_OSL
155 OSLCompiler(OSL::ShadingSystem *ss, Scene *scene, Progress &progress, Device *device);
156#endif
157 void compile(Shader *shader);
158
159 void add(ShaderNode *node, const char *name, bool isfilepath = false);
160
161 /* Add a converter between the "main" node OSL implementation and the shader node output socket.
162 *
163 * The converter uses the `converter_name` shader, and its `converter_input_parameter` is
164 * connected to the "main" node's `node_output_parameter`. The shader's output parameter
165 * `shader_output_parameter` is connected to the converter's `converter_output_parameter`. */
167 string_view converter_name,
168 string_view node_output_parameter,
169 string_view converter_input_parameter,
170 string_view converter_output_parameter,
171 string_view shader_output_parameter);
172
173 void parameter(ShaderNode *node, const char *name);
174
175 void parameter(const char *name, const float f);
176 void parameter_color(const char *name, const float3 f);
177 void parameter_vector(const char *name, const float3 f);
178 void parameter_normal(const char *name, const float3 f);
179 void parameter_point(const char *name, const float3 f);
180 void parameter(const char *name, const int f);
181 void parameter(const char *name, const char *s);
182 void parameter(const char *name, ustring str);
183 void parameter(const char *name, const Transform &tfm);
184
185 void parameter_array(const char *name, const float f[], int arraylen);
186 void parameter_color_array(const char *name, const array<packed_float3> &f);
187 void parameter_string_array(const char *name, const array<ustring> &a);
188
189 void parameter_attribute(const char *name, ustring s);
190
191 void parameter_texture(const char *name, const ImageHandle &handle);
192 void parameter_texture_ies(const char *name, const int svm_slot);
193
195 {
196 return current_type;
197 }
198
201 Progress *progress = nullptr;
203
204 private:
205#ifdef WITH_OSL
206 struct LayerParam {
207 string layer;
208 string param;
209
210 friend auto operator<=>(const LayerParam &lhs, const LayerParam &rhs) = default;
211 };
212
213 string id(const ShaderNode *node);
214 string_view get_shader_usage() const;
215 LayerParam get_output_layer_param(string_view layer, string_view param) const;
216 OSL::ShaderGroupRef compile_type(Shader *shader, ShaderGraph *graph, ShaderType type);
217 bool node_skip_input(ShaderNode *node, ShaderInput *input);
218 string compatible_name(ShaderNode *node, ShaderInput *input);
219 string compatible_name(ShaderNode *node, ShaderOutput *output);
220
221 void find_dependencies(ShaderNodeSet &dependencies, ShaderInput *input);
222 void generate_nodes(const ShaderNodeSet &nodes);
223
224 OSLRenderServices *services;
225 OSL::ShadingSystem *ss;
226 OSL::ShaderGroupRef current_group;
227
228 map<LayerParam, LayerParam> remapped_outputs_;
229#endif
230
231 Device *device;
232 ShaderType current_type;
233 Shader *current_shader;
234
235 static std::atomic<int> texture_shared_unique_id;
236};
237
unsigned long long int uint64_t
#define output
void parameter_array(const char *name, const float f[], int arraylen)
Definition osl.cpp:1728
void add_output_converter(const ShaderNode *node, string_view converter_name, string_view node_output_parameter, string_view converter_input_parameter, string_view converter_output_parameter, string_view shader_output_parameter)
Definition osl.cpp:1699
void add(ShaderNode *node, const char *name, bool isfilepath=false)
Definition osl.cpp:1697
ShaderType output_type()
Definition scene/osl.h:194
void parameter_color_array(const char *name, const array< packed_float3 > &f)
Definition osl.cpp:1730
void parameter(ShaderNode *node, const char *name)
Definition osl.cpp:1708
void compile(Shader *shader)
bool background
Definition scene/osl.h:199
void parameter_texture(const char *name, const ImageHandle &handle)
Definition osl.cpp:1738
Progress * progress
Definition scene/osl.h:201
void parameter_texture_ies(const char *name, const int svm_slot)
Definition osl.cpp:1740
void parameter_point(const char *name, const float3 f)
Definition osl.cpp:1716
void parameter_string_array(const char *name, const array< ustring > &a)
Definition osl.cpp:1734
Scene * scene
Definition scene/osl.h:200
void parameter_vector(const char *name, const float3 f)
Definition osl.cpp:1714
ShaderGraph * current_graph
Definition scene/osl.h:202
void parameter_attribute(const char *name, ustring s)
Definition osl.cpp:1736
void parameter_color(const char *name, const float3 f)
Definition osl.cpp:1712
void parameter_normal(const char *name, const float3 f)
Definition osl.cpp:1718
void reset(Scene *scene)
Definition osl.cpp:1680
void device_free(Device *device, DeviceScene *dscene, Scene *scene)
Definition osl.cpp:1689
void device_update_pre(Device *device, Scene *scene)
Definition osl.cpp:1682
static void free_memory()
Definition osl.cpp:1679
void tag_update()
Definition osl.cpp:1691
void device_update_post(Device *device, Scene *scene, Progress &progress, const bool reload_kernels)
Definition osl.cpp:1683
OSLManager(Device *device)
Definition osl.cpp:1676
bool need_update() const
Definition osl.cpp:1692
Definition scene.h:133
#define CCL_NAMESPACE_END
#define str(s)
#define layer
AttributeStandard
#define hash
Definition noise_c.cc:156
set< ShaderNode *, ShaderNodeIDComparator > ShaderNodeSet