Blender V4.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#ifndef __OSL_H__
6#define __OSL_H__
7
8#include "util/array.h"
9#include "util/set.h"
10#include "util/string.h"
11#include "util/thread.h"
12
13#include "scene/shader.h"
14#include "scene/shader_graph.h"
15#include "scene/shader_nodes.h"
16
17#ifdef WITH_OSL
18# include <OSL/llvm_util.h>
19# include <OSL/oslcomp.h>
20# include <OSL/oslexec.h>
21# include <OSL/oslquery.h>
22#endif
23
25
26class Device;
27class DeviceScene;
28class ImageManager;
30struct OSLGlobals;
31class Scene;
32class ShaderGraph;
33class ShaderNode;
34class ShaderInput;
35class ShaderOutput;
36
37#ifdef WITH_OSL
38
39/* OSL Shader Info
40 * to auto detect closures in the shader for MIS and transparent shadows */
41
42struct OSLShaderInfo {
43 OSLShaderInfo()
44 : has_surface_emission(false), has_surface_transparent(false), has_surface_bssrdf(false)
45 {
46 }
47
48 OSL::OSLQuery query;
49 bool has_surface_emission;
50 bool has_surface_transparent;
51 bool has_surface_bssrdf;
52};
53
54/* Shader Manage */
55
56class OSLShaderManager : public ShaderManager {
57 public:
58 OSLShaderManager(Device *device);
59 ~OSLShaderManager();
60
61 static void free_memory();
62
63 void reset(Scene *scene) override;
64
65 bool use_osl() override
66 {
67 return true;
68 }
69
70 uint64_t get_attribute_id(ustring name) override;
72
73 void device_update_specific(Device *device,
74 DeviceScene *dscene,
75 Scene *scene,
76 Progress &progress) override;
77 void device_free(Device *device, DeviceScene *dscene, Scene *scene) override;
78
79 /* osl compile and query */
80 static bool osl_compile(const string &inputfile, const string &outputfile);
81 static bool osl_query(OSL::OSLQuery &query, const string &filepath);
82
83 /* shader file loading, all functions return pointer to hash string if found */
84 const char *shader_test_loaded(const string &hash);
85 const char *shader_load_bytecode(const string &hash, const string &bytecode);
86 const char *shader_load_filepath(string filepath);
87 OSLShaderInfo *shader_loaded_info(const string &hash);
88
89 /* create OSL node using OSLQuery */
90 static OSLNode *osl_node(ShaderGraph *graph,
91 ShaderManager *manager,
92 const std::string &filepath,
93 const std::string &bytecode_hash = "",
94 const std::string &bytecode = "");
95
96 /* Get image slots used by OSL services on device. */
97 static void osl_image_slots(Device *device, ImageManager *image_manager, set<int> &image_slots);
98
99 private:
100 void texture_system_init();
101 void texture_system_free();
102
103 void shading_system_init();
104 void shading_system_free();
105
106 Device *device_;
107 map<string, OSLShaderInfo> loaded_shaders;
108
109# if OIIO_VERSION_MAJOR >= 3
110 static std::shared_ptr<OSL::TextureSystem> ts_shared;
111# else
112 static OSL::TextureSystem *ts_shared;
113# endif
114 static thread_mutex ts_shared_mutex;
115 static int ts_shared_users;
116
117 static OSL::ErrorHandler errhandler;
118 static map<int, OSL::ShadingSystem *> ss_shared;
119 static thread_mutex ss_shared_mutex;
120 static thread_mutex ss_mutex;
121 static int ss_shared_users;
122};
123
124#endif
125
126/* Graph Compiler */
127
129 public:
130#ifdef WITH_OSL
131 OSLCompiler(OSLShaderManager *manager, OSL::ShadingSystem *shadingsys, Scene *scene);
132#endif
133 void compile(OSLGlobals *og, Shader *shader);
134
135 void add(ShaderNode *node, const char *name, bool isfilepath = false);
136
137 void parameter(ShaderNode *node, const char *name);
138
139 void parameter(const char *name, float f);
140 void parameter_color(const char *name, float3 f);
141 void parameter_vector(const char *name, float3 f);
142 void parameter_normal(const char *name, float3 f);
143 void parameter_point(const char *name, float3 f);
144 void parameter(const char *name, int f);
145 void parameter(const char *name, const char *s);
146 void parameter(const char *name, ustring str);
147 void parameter(const char *name, const Transform &tfm);
148
149 void parameter_array(const char *name, const float f[], int arraylen);
150 void parameter_color_array(const char *name, const array<float3> &f);
151
152 void parameter_attribute(const char *name, ustring s);
153
154 void parameter_texture(const char *name, ustring filename, ustring colorspace);
155 void parameter_texture(const char *name, const ImageHandle &handle);
156 void parameter_texture_ies(const char *name, int svm_slot);
157
159 {
160 return current_type;
161 }
162
165
166 private:
167#ifdef WITH_OSL
168 string id(ShaderNode *node);
169 OSL::ShaderGroupRef compile_type(Shader *shader, ShaderGraph *graph, ShaderType type);
170 bool node_skip_input(ShaderNode *node, ShaderInput *input);
171 string compatible_name(ShaderNode *node, ShaderInput *input);
172 string compatible_name(ShaderNode *node, ShaderOutput *output);
173
174 void find_dependencies(ShaderNodeSet &dependencies, ShaderInput *input);
175 void generate_nodes(const ShaderNodeSet &nodes);
176
177 OSLShaderManager *manager;
178 OSLRenderServices *services;
179 OSL::ShadingSystem *ss;
180#endif
181
182 ShaderType current_type;
183 Shader *current_shader;
184
185 static int texture_shared_unique_id;
186};
187
189
190#endif /* __OSL_H__ */
struct Scene Scene
#define output
void reset()
clear internal cached data and reset random seed
void add(ShaderNode *node, const char *name, bool isfilepath=false)
Definition osl.cpp:1378
void parameter_vector(const char *name, float3 f)
Definition osl.cpp:1386
void parameter_array(const char *name, const float f[], int arraylen)
Definition osl.cpp:1400
void parameter_texture_ies(const char *name, int svm_slot)
Definition osl.cpp:1412
ShaderType output_type()
Definition scene/osl.h:158
void parameter_texture(const char *name, ustring filename, ustring colorspace)
Definition osl.cpp:1404
void parameter(ShaderNode *node, const char *name)
Definition osl.cpp:1380
bool background
Definition scene/osl.h:163
void parameter_normal(const char *name, float3 f)
Definition osl.cpp:1390
void parameter_color_array(const char *name, const array< float3 > &f)
Definition osl.cpp:1402
void compile(OSLGlobals *og, Shader *shader)
Scene * scene
Definition scene/osl.h:164
void parameter_point(const char *name, float3 f)
Definition osl.cpp:1388
void parameter_color(const char *name, float3 f)
Definition osl.cpp:1384
void parameter_attribute(const char *name, ustring s)
static void free_memory()
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)
AttributeStandard
#define hash
Definition noise.c:154
set< ShaderNode *, ShaderNodeIDComparator > ShaderNodeSet
unsigned __int64 uint64_t
Definition stdint.h:90
CCL_NAMESPACE_BEGIN typedef std::mutex thread_mutex
Definition thread.h:29