Blender V4.5
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,
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 OSL::ShadingSystem *get_shading_system(Device *sub_device);
83 OSL::TextureSystem *get_texture_system();
84 static void foreach_osl_device(Device *device,
85 const std::function<void(Device *, OSLGlobals *)> &callback);
86#endif
87
88 void tag_update();
89 bool need_update() const;
90
91 private:
92#ifdef WITH_OSL
93 void texture_system_init();
94 void texture_system_free();
95
96 void shading_system_init();
97 void shading_system_free();
98
99 void foreach_shading_system(const std::function<void(OSL::ShadingSystem *)> &callback);
100 void foreach_render_services(const std::function<void(OSLRenderServices *)> &callback);
101
102 Device *device_;
103 map<string, OSLShaderInfo> loaded_shaders;
104
105 std::shared_ptr<OSL::TextureSystem> ts;
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;
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 slots used by OSL services on device. */
143 static void osl_image_slots(Device *device, ImageManager *image_manager, set<int> &image_slots);
144};
145
146#endif
147
148/* Graph Compiler */
149
151 public:
152#ifdef WITH_OSL
153 OSLCompiler(OSL::ShadingSystem *ss, Scene *scene);
154#endif
155 void compile(Shader *shader);
156
157 void add(ShaderNode *node, const char *name, bool isfilepath = false);
158
159 void parameter(ShaderNode *node, const char *name);
160
161 void parameter(const char *name, const float f);
162 void parameter_color(const char *name, const float3 f);
163 void parameter_vector(const char *name, const float3 f);
164 void parameter_normal(const char *name, const float3 f);
165 void parameter_point(const char *name, const float3 f);
166 void parameter(const char *name, const int f);
167 void parameter(const char *name, const char *s);
168 void parameter(const char *name, ustring str);
169 void parameter(const char *name, const Transform &tfm);
170
171 void parameter_array(const char *name, const float f[], int arraylen);
172 void parameter_color_array(const char *name, const array<float3> &f);
173
174 void parameter_attribute(const char *name, ustring s);
175
176 void parameter_texture(const char *name, ustring filename, ustring colorspace);
177 void parameter_texture(const char *name, const ImageHandle &handle);
178 void parameter_texture_ies(const char *name, const int svm_slot);
179
181 {
182 return current_type;
183 }
184
187
188 private:
189#ifdef WITH_OSL
190 string id(ShaderNode *node);
191 OSL::ShaderGroupRef compile_type(Shader *shader, ShaderGraph *graph, ShaderType type);
192 bool node_skip_input(ShaderNode *node, ShaderInput *input);
193 string compatible_name(ShaderNode *node, ShaderInput *input);
194 string compatible_name(ShaderNode *node, ShaderOutput *output);
195
196 void find_dependencies(ShaderNodeSet &dependencies, ShaderInput *input);
197 void generate_nodes(const ShaderNodeSet &nodes);
198
199 OSLRenderServices *services;
200 OSL::ShadingSystem *ss;
201 OSL::ShaderGroupRef current_group;
202#endif
203
204 ShaderType current_type;
205 Shader *current_shader;
206
207 static std::atomic<int> texture_shared_unique_id;
208};
209
struct Scene Scene
float progress
Definition WM_types.hh:1019
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:1667
void add(ShaderNode *node, const char *name, bool isfilepath=false)
Definition osl.cpp:1645
ShaderType output_type()
Definition scene/osl.h:180
void parameter_texture(const char *name, ustring filename, ustring colorspace)
Definition osl.cpp:1671
void parameter(ShaderNode *node, const char *name)
Definition osl.cpp:1647
void compile(Shader *shader)
bool background
Definition scene/osl.h:185
void parameter_color_array(const char *name, const array< float3 > &f)
Definition osl.cpp:1669
void parameter_texture_ies(const char *name, const int svm_slot)
Definition osl.cpp:1679
void parameter_point(const char *name, const float3 f)
Definition osl.cpp:1655
Scene * scene
Definition scene/osl.h:186
void parameter_vector(const char *name, const float3 f)
Definition osl.cpp:1653
void parameter_attribute(const char *name, ustring s)
void parameter_color(const char *name, const float3 f)
Definition osl.cpp:1651
void parameter_normal(const char *name, const float3 f)
Definition osl.cpp:1657
~OSLManager()
Definition osl.cpp:1625
void device_free(Device *device, DeviceScene *dscene, Scene *scene)
Definition osl.cpp:1637
void device_update_pre(Device *device, Scene *scene)
Definition osl.cpp:1630
static void free_memory()
Definition osl.cpp:1627
void tag_update()
Definition osl.cpp:1639
void device_update_post(Device *device, Scene *scene, Progress &progress, const bool reload_kernels)
Definition osl.cpp:1631
OSLManager(Device *device)
Definition osl.cpp:1624
bool need_update() const
Definition osl.cpp:1640
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
set< ShaderNode *, ShaderNodeIDComparator > ShaderNodeSet