Blender  V3.3
osl.h
Go to the documentation of this file.
1 /* SPDX-License-Identifier: Apache-2.0
2  * Copyright 2011-2022 Blender Foundation */
3 
4 #ifndef __OSL_H__
5 #define __OSL_H__
6 
7 #include "util/array.h"
8 #include "util/set.h"
9 #include "util/string.h"
10 #include "util/thread.h"
11 
12 #include "scene/shader.h"
13 #include "scene/shader_graph.h"
14 #include "scene/shader_nodes.h"
15 
16 #ifdef WITH_OSL
17 # include <OSL/llvm_util.h>
18 # include <OSL/oslcomp.h>
19 # include <OSL/oslexec.h>
20 # include <OSL/oslquery.h>
21 #endif
22 
24 
25 class Device;
26 class DeviceScene;
27 class ImageManager;
28 class OSLRenderServices;
29 struct OSLGlobals;
30 class Scene;
31 class ShaderGraph;
32 class ShaderNode;
33 class ShaderInput;
34 class ShaderOutput;
35 
36 #ifdef WITH_OSL
37 
38 /* OSL Shader Info
39  * to auto detect closures in the shader for MIS and transparent shadows */
40 
41 struct OSLShaderInfo {
42  OSLShaderInfo()
43  : has_surface_emission(false), has_surface_transparent(false), has_surface_bssrdf(false)
44  {
45  }
46 
47  OSL::OSLQuery query;
48  bool has_surface_emission;
49  bool has_surface_transparent;
50  bool has_surface_bssrdf;
51 };
52 
53 /* Shader Manage */
54 
55 class OSLShaderManager : public ShaderManager {
56  public:
57  OSLShaderManager();
58  ~OSLShaderManager();
59 
60  static void free_memory();
61 
62  void reset(Scene *scene) override;
63 
64  bool use_osl() override
65  {
66  return true;
67  }
68 
69  void device_update_specific(Device *device,
70  DeviceScene *dscene,
71  Scene *scene,
72  Progress &progress) override;
73  void device_free(Device *device, DeviceScene *dscene, Scene *scene) override;
74 
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  /* create OSL node using OSLQuery */
86  static OSLNode *osl_node(ShaderGraph *graph,
87  ShaderManager *manager,
88  const std::string &filepath,
89  const std::string &bytecode_hash = "",
90  const std::string &bytecode = "");
91 
92  protected:
93  void texture_system_init();
94  void texture_system_free();
95 
96  void shading_system_init();
97  void shading_system_free();
98 
100  OSL::TextureSystem *ts;
101  OSLRenderServices *services;
102  OSL::ErrorHandler errhandler;
103  map<string, OSLShaderInfo> loaded_shaders;
104 
105  static OSL::TextureSystem *ts_shared;
106  static thread_mutex ts_shared_mutex;
107  static int ts_shared_users;
108 
109  static OSL::ShadingSystem *ss_shared;
110  static OSLRenderServices *services_shared;
111  static thread_mutex ss_shared_mutex;
112  static thread_mutex ss_mutex;
113  static int ss_shared_users;
114 };
115 
116 #endif
117 
118 /* Graph Compiler */
119 
120 class OSLCompiler {
121  public:
122 #ifdef WITH_OSL
123  OSLCompiler(OSLShaderManager *manager,
124  OSLRenderServices *services,
125  OSL::ShadingSystem *shadingsys,
126  Scene *scene);
127 #endif
128  void compile(OSLGlobals *og, Shader *shader);
129 
130  void add(ShaderNode *node, const char *name, bool isfilepath = false);
131 
132  void parameter(ShaderNode *node, const char *name);
133 
134  void parameter(const char *name, float f);
135  void parameter_color(const char *name, float3 f);
136  void parameter_vector(const char *name, float3 f);
137  void parameter_normal(const char *name, float3 f);
138  void parameter_point(const char *name, float3 f);
139  void parameter(const char *name, int f);
140  void parameter(const char *name, const char *s);
141  void parameter(const char *name, ustring str);
142  void parameter(const char *name, const Transform &tfm);
143 
144  void parameter_array(const char *name, const float f[], int arraylen);
145  void parameter_color_array(const char *name, const array<float3> &f);
146 
147  void parameter_attribute(const char *name, ustring s);
148 
149  void parameter_texture(const char *name, ustring filename, ustring colorspace);
150  void parameter_texture(const char *name, const ImageHandle &handle);
151  void parameter_texture_ies(const char *name, int svm_slot);
152 
154  {
155  return current_type;
156  }
157 
160 
161  private:
162 #ifdef WITH_OSL
163  string id(ShaderNode *node);
164  OSL::ShaderGroupRef compile_type(Shader *shader, ShaderGraph *graph, ShaderType type);
165  bool node_skip_input(ShaderNode *node, ShaderInput *input);
166  string compatible_name(ShaderNode *node, ShaderInput *input);
167  string compatible_name(ShaderNode *node, ShaderOutput *output);
168 
169  void find_dependencies(ShaderNodeSet &dependencies, ShaderInput *input);
170  void generate_nodes(const ShaderNodeSet &nodes);
171 
172  OSLShaderManager *manager;
173  OSLRenderServices *services;
174  OSL::ShadingSystem *ss;
175 #endif
176 
177  ShaderType current_type;
178  Shader *current_shader;
179 
180  static int texture_shared_unique_id;
181 };
182 
184 
185 #endif /* __OSL_H__ */
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum const void *lists _GL_VOID_RET _GL_VOID const GLdouble *equation _GL_VOID_RET _GL_VOID GLdouble GLdouble blue _GL_VOID_RET _GL_VOID GLfloat GLfloat blue _GL_VOID_RET _GL_VOID GLint GLint blue _GL_VOID_RET _GL_VOID GLshort GLshort blue _GL_VOID_RET _GL_VOID GLubyte GLubyte blue _GL_VOID_RET _GL_VOID GLuint GLuint blue _GL_VOID_RET _GL_VOID GLushort GLushort blue _GL_VOID_RET _GL_VOID GLbyte GLbyte GLbyte alpha _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble alpha _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat alpha _GL_VOID_RET _GL_VOID GLint GLint GLint alpha _GL_VOID_RET _GL_VOID GLshort GLshort GLshort alpha _GL_VOID_RET _GL_VOID GLubyte GLubyte GLubyte alpha _GL_VOID_RET _GL_VOID GLuint GLuint GLuint alpha _GL_VOID_RET _GL_VOID GLushort GLushort GLushort alpha _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLint GLsizei GLsizei GLenum type _GL_VOID_RET _GL_VOID GLsizei GLenum GLenum const void *pixels _GL_VOID_RET _GL_VOID const void *pointer _GL_VOID_RET _GL_VOID GLdouble v _GL_VOID_RET _GL_VOID GLfloat v _GL_VOID_RET _GL_VOID GLint GLint i2 _GL_VOID_RET _GL_VOID GLint j _GL_VOID_RET _GL_VOID GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble GLdouble GLdouble zFar _GL_VOID_RET _GL_UINT GLdouble *equation _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLenum query
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum type
void add(ShaderNode *node, const char *name, bool isfilepath=false)
Definition: osl.cpp:1236
void parameter_vector(const char *name, float3 f)
Definition: osl.cpp:1252
void parameter_array(const char *name, const float f[], int arraylen)
Definition: osl.cpp:1280
void parameter_texture_ies(const char *name, int svm_slot)
Definition: osl.cpp:1298
ShaderType output_type()
Definition: osl.h:153
void parameter_texture(const char *name, ustring filename, ustring colorspace)
Definition: osl.cpp:1288
void parameter(ShaderNode *node, const char *name)
Definition: osl.cpp:1240
bool background
Definition: osl.h:158
void parameter_normal(const char *name, float3 f)
Definition: osl.cpp:1260
void parameter_color_array(const char *name, const array< float3 > &f)
Definition: osl.cpp:1284
void compile(OSLGlobals *og, Shader *shader)
Scene * scene
Definition: osl.h:159
void parameter_point(const char *name, float3 f)
Definition: osl.cpp:1256
void parameter_color(const char *name, float3 f)
Definition: osl.cpp:1248
void parameter_attribute(const char *name, ustring s)
static void free_memory()
virtual void device_free(Device *device, DeviceScene *dscene, Scene *scene)=0
virtual void device_update_specific(Device *device, DeviceScene *dscene, Scene *scene, Progress &progress)=0
virtual bool use_osl()
Definition: scene/shader.h:178
virtual void reset(Scene *scene)=0
#define CCL_NAMESPACE_END
Definition: cuda/compat.h:9
OperationNode * node
Depsgraph * graph
Scene scene
#define str(s)
ccl_global KernelShaderEvalInput ccl_global float * output
ccl_global KernelShaderEvalInput * input
ShaderType
#define hash
Definition: noise.c:153
ShadingSystem
Definition: scene/shader.h:34
set< ShaderNode *, ShaderNodeIDComparator > ShaderNodeSet
Definition: shader_graph.h:289
CCL_NAMESPACE_BEGIN typedef std::mutex thread_mutex
Definition: thread.h:27