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 static OSL::TextureSystem *ts_shared;
110 static thread_mutex ts_shared_mutex;
111 static int ts_shared_users;
112
113 static OSL::ErrorHandler errhandler;
114 static map<int, OSL::ShadingSystem *> ss_shared;
115 static thread_mutex ss_shared_mutex;
116 static thread_mutex ss_mutex;
117 static int ss_shared_users;
118};
119
120#endif
121
122/* Graph Compiler */
123
125 public:
126#ifdef WITH_OSL
127 OSLCompiler(OSLShaderManager *manager, OSL::ShadingSystem *shadingsys, Scene *scene);
128#endif
129 void compile(OSLGlobals *og, Shader *shader);
130
131 void add(ShaderNode *node, const char *name, bool isfilepath = false);
132
133 void parameter(ShaderNode *node, const char *name);
134
135 void parameter(const char *name, float f);
136 void parameter_color(const char *name, float3 f);
137 void parameter_vector(const char *name, float3 f);
138 void parameter_normal(const char *name, float3 f);
139 void parameter_point(const char *name, float3 f);
140 void parameter(const char *name, int f);
141 void parameter(const char *name, const char *s);
142 void parameter(const char *name, ustring str);
143 void parameter(const char *name, const Transform &tfm);
144
145 void parameter_array(const char *name, const float f[], int arraylen);
146 void parameter_color_array(const char *name, const array<float3> &f);
147
148 void parameter_attribute(const char *name, ustring s);
149
150 void parameter_texture(const char *name, ustring filename, ustring colorspace);
151 void parameter_texture(const char *name, const ImageHandle &handle);
152 void parameter_texture_ies(const char *name, int svm_slot);
153
155 {
156 return current_type;
157 }
158
161
162 private:
163#ifdef WITH_OSL
164 string id(ShaderNode *node);
165 OSL::ShaderGroupRef compile_type(Shader *shader, ShaderGraph *graph, ShaderType type);
166 bool node_skip_input(ShaderNode *node, ShaderInput *input);
167 string compatible_name(ShaderNode *node, ShaderInput *input);
168 string compatible_name(ShaderNode *node, ShaderOutput *output);
169
170 void find_dependencies(ShaderNodeSet &dependencies, ShaderInput *input);
171 void generate_nodes(const ShaderNodeSet &nodes);
172
173 OSLShaderManager *manager;
174 OSLRenderServices *services;
175 OSL::ShadingSystem *ss;
176#endif
177
178 ShaderType current_type;
179 Shader *current_shader;
180
181 static int texture_shared_unique_id;
182};
183
185
186#endif /* __OSL_H__ */
void reset()
clear internal cached data and reset random seed
void add(ShaderNode *node, const char *name, bool isfilepath=false)
Definition osl.cpp:1351
void parameter_vector(const char *name, float3 f)
Definition osl.cpp:1359
void parameter_array(const char *name, const float f[], int arraylen)
Definition osl.cpp:1373
void parameter_texture_ies(const char *name, int svm_slot)
Definition osl.cpp:1385
ShaderType output_type()
Definition scene/osl.h:154
void parameter_texture(const char *name, ustring filename, ustring colorspace)
Definition osl.cpp:1377
void parameter(ShaderNode *node, const char *name)
Definition osl.cpp:1353
bool background
Definition scene/osl.h:159
void parameter_normal(const char *name, float3 f)
Definition osl.cpp:1363
void parameter_color_array(const char *name, const array< float3 > &f)
Definition osl.cpp:1375
void compile(OSLGlobals *og, Shader *shader)
Scene * scene
Definition scene/osl.h:160
void parameter_point(const char *name, float3 f)
Definition osl.cpp:1361
void parameter_color(const char *name, float3 f)
Definition osl.cpp:1357
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