Blender V5.0
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/set.h"
13#include "util/string.h"
14
15#include "scene/shader.h"
16#include "scene/shader_graph.h"
17#include "scene/shader_nodes.h"
18
19#ifdef WITH_OSL
20# include <OSL/llvm_util.h>
21# include <OSL/oslcomp.h>
22# include <OSL/oslexec.h>
23# include <OSL/oslquery.h>
24#endif
25
27
28class Device;
29class DeviceScene;
30class ImageManager;
32struct OSLGlobals;
33class Scene;
34class ShaderGraph;
35class ShaderNode;
36class ShaderInput;
37class ShaderOutput;
38
39#ifdef WITH_OSL
40
41/* OSL Shader Info
42 * to auto detect closures in the shader for MIS and transparent shadows */
43
44struct OSLShaderInfo {
45 OSLShaderInfo() = default;
46
47 OSL::OSLQuery query;
48 bool has_surface_emission = false;
49 bool has_surface_transparent = false;
50 bool has_surface_bssrdf = false;
51};
52
53#endif
54
56 public:
57 OSLManager(Device *device);
59
60 static void free_memory();
61
62 void reset(Scene *scene);
63
64 void device_update_pre(Device *device, Scene *scene);
65 void device_update_post(Device *device,
66 Scene *scene,
67 Progress &progress,
68 const bool reload_kernels);
69 void device_free(Device *device, DeviceScene *dscene, Scene *scene);
70
71#ifdef WITH_OSL
72 /* osl compile and query */
73 static bool osl_compile(const string &inputfile, const string &outputfile);
74 static bool osl_query(OSL::OSLQuery &query, const string &filepath);
75
76 /* shader file loading, all functions return pointer to hash string if found */
77 const char *shader_test_loaded(const string &hash);
78 const char *shader_load_bytecode(const string &hash, const string &bytecode);
79 const char *shader_load_filepath(string filepath);
80 OSLShaderInfo *shader_loaded_info(const string &hash);
81
82 void shading_system_init(ShaderManager::SceneLinearSpace colorspace);
83
84 OSL::ShadingSystem *get_shading_system(Device *sub_device);
85 OSL::TextureSystem *get_texture_system();
86 static void foreach_osl_device(Device *device,
87 const std::function<void(Device *, OSLGlobals *)> &callback);
88#endif
89
90 void tag_update();
91 bool need_update() const;
92
93 private:
94#ifdef WITH_OSL
95 void texture_system_init();
96 void texture_system_free();
97
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 std::shared_ptr<OSL::TextureSystem> ts;
107 map<DeviceType, std::shared_ptr<OSL::ShadingSystem>> ss_map;
108
109 bool need_update_;
110#endif
111};
112
113#ifdef WITH_OSL
114
115/* Shader Manage */
116
117class OSLShaderManager : public ShaderManager {
118 public:
119 OSLShaderManager() = default;
120 ~OSLShaderManager() override = default;
121
122 bool use_osl() override
123 {
124 return true;
125 }
126
127 uint64_t get_attribute_id(ustring name) override;
129
130 void device_update_specific(Device *device,
131 DeviceScene *dscene,
132 Scene *scene,
133 Progress &progress) override;
134 void device_free(Device *device, DeviceScene *dscene, Scene *scene) override;
135
136 /* create OSL node using OSLQuery */
137 static OSLNode *osl_node(ShaderGraph *graph,
138 Scene *scene,
139 const std::string &filepath,
140 const std::string &bytecode_hash = "",
141 const std::string &bytecode = "");
142
143 /* Get image slots used by OSL services on device. */
144 static void osl_image_slots(Device *device, ImageManager *image_manager, set<int> &image_slots);
145};
146
147#endif
148
149/* Graph Compiler */
150
152 public:
153#ifdef WITH_OSL
154 OSLCompiler(OSL::ShadingSystem *ss, Scene *scene, Device *device);
155#endif
156 void compile(Shader *shader);
157
158 void add(ShaderNode *node, const char *name, bool isfilepath = false);
159
160 void parameter(ShaderNode *node, const char *name);
161
162 void parameter(const char *name, const float f);
163 void parameter_color(const char *name, const float3 f);
164 void parameter_vector(const char *name, const float3 f);
165 void parameter_normal(const char *name, const float3 f);
166 void parameter_point(const char *name, const float3 f);
167 void parameter(const char *name, const int f);
168 void parameter(const char *name, const char *s);
169 void parameter(const char *name, ustring str);
170 void parameter(const char *name, const Transform &tfm);
171
172 void parameter_array(const char *name, const float f[], int arraylen);
173 void parameter_color_array(const char *name, const array<float3> &f);
174
175 void parameter_attribute(const char *name, ustring s);
176
177 void parameter_texture(const char *name, ustring filename, ustring colorspace);
178 void parameter_texture(const char *name, const ImageHandle &handle);
179 void parameter_texture_ies(const char *name, const int svm_slot);
180
182 {
183 return current_type;
184 }
185
188
189 private:
190#ifdef WITH_OSL
191 string id(ShaderNode *node);
192 OSL::ShaderGroupRef compile_type(Shader *shader, ShaderGraph *graph, ShaderType type);
193 bool node_skip_input(ShaderNode *node, ShaderInput *input);
194 string compatible_name(ShaderNode *node, ShaderInput *input);
195 string compatible_name(ShaderNode *node, ShaderOutput *output);
196
197 void find_dependencies(ShaderNodeSet &dependencies, ShaderInput *input);
198 void generate_nodes(const ShaderNodeSet &nodes);
199
200 OSLRenderServices *services;
201 OSL::ShadingSystem *ss;
202 OSL::ShaderGroupRef current_group;
203#endif
204
205 Device *device;
206 ShaderType current_type;
207 Shader *current_shader;
208
209 static std::atomic<int> texture_shared_unique_id;
210};
211
struct Scene Scene
unsigned long long int uint64_t
void reset()
clear internal cached data and reset random seed
void parameter_array(const char *name, const float f[], int arraylen)
Definition osl.cpp:1658
void add(ShaderNode *node, const char *name, bool isfilepath=false)
Definition osl.cpp:1636
ShaderType output_type()
Definition scene/osl.h:181
void parameter_texture(const char *name, ustring filename, ustring colorspace)
Definition osl.cpp:1662
void parameter(ShaderNode *node, const char *name)
Definition osl.cpp:1638
void compile(Shader *shader)
bool background
Definition scene/osl.h:186
void parameter_color_array(const char *name, const array< float3 > &f)
Definition osl.cpp:1660
void parameter_texture_ies(const char *name, const int svm_slot)
Definition osl.cpp:1670
void parameter_point(const char *name, const float3 f)
Definition osl.cpp:1646
Scene * scene
Definition scene/osl.h:187
void parameter_vector(const char *name, const float3 f)
Definition osl.cpp:1644
void parameter_attribute(const char *name, ustring s)
void parameter_color(const char *name, const float3 f)
Definition osl.cpp:1642
void parameter_normal(const char *name, const float3 f)
Definition osl.cpp:1648
~OSLManager()
Definition osl.cpp:1616
void device_free(Device *device, DeviceScene *dscene, Scene *scene)
Definition osl.cpp:1628
void device_update_pre(Device *device, Scene *scene)
Definition osl.cpp:1621
static void free_memory()
Definition osl.cpp:1618
void tag_update()
Definition osl.cpp:1630
void device_update_post(Device *device, Scene *scene, Progress &progress, const bool reload_kernels)
Definition osl.cpp:1622
OSLManager(Device *device)
Definition osl.cpp:1615
bool need_update() const
Definition osl.cpp:1631
virtual void device_free(Device *device, DeviceScene *dscene, Scene *scene)=0
virtual uint64_t get_attribute_id(ustring name)
virtual void device_update_specific(Device *device, DeviceScene *dscene, Scene *scene, Progress &progress)=0
virtual bool use_osl()
#define CCL_NAMESPACE_END
#define str(s)
#define input
#define output
AttributeStandard
#define hash
Definition noise_c.cc:154
const char * name
set< ShaderNode *, ShaderNodeIDComparator > ShaderNodeSet