HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
OslShaderGenerator.h
Go to the documentation of this file.
1 //
2 // Copyright Contributors to the MaterialX Project
3 // SPDX-License-Identifier: Apache-2.0
4 //
5 
6 #ifndef MATERIALX_OSLSHADERGENERATOR_H
7 #define MATERIALX_OSLSHADERGENERATOR_H
8 
9 /// @file
10 /// OSL shading language generator
11 
12 #include <MaterialXGenOsl/Export.h>
13 
15 
17 
18 using OslShaderGeneratorPtr = shared_ptr<class OslShaderGenerator>;
19 
20 /// @class OslShaderGenerator
21 /// Base class for OSL (Open Shading Language) shader generators.
22 /// A generator for a specific OSL target should be derived from this class.
24 {
25  public:
26  /// Constructor.
28 
29  /// Creator function.
30  /// If a TypeSystem is not provided it will be created internally.
31  /// Optionally pass in an externally created TypeSystem here,
32  /// if you want to keep type descriptions alive after the lifetime
33  /// of the shader generator.
34  static ShaderGeneratorPtr create(TypeSystemPtr typeSystem = nullptr)
35  {
36  return std::make_shared<OslShaderGenerator>(typeSystem ? typeSystem : TypeSystem::create());
37  }
38 
39  /// Return a unique identifier for the target this generator is for
40  const string& getTarget() const override { return TARGET; }
41 
42  /// Generate a shader starting from the given element, translating
43  /// the element and all dependencies upstream into shader code.
44  ShaderPtr generate(const string& name, ElementPtr element, GenContext& context) const override;
45 
46  /// Add all function calls for a node, and all upstream nodes.
47  void emitAllDependentFunctionCalls(const ShaderNode& node, GenContext& context, ShaderStage& stage) const;
48 
49  /// Unique identifier for this generator target
50  static const string TARGET;
51 
52  /// Register metadata that should be exported to the generated shaders.
53  void registerShaderMetadata(const DocumentPtr& doc, GenContext& context) const override;
54 
55  protected:
56  /// Create and initialize a new OSL shader for shader generation.
57  virtual ShaderPtr createShader(const string& name, ElementPtr element, GenContext& context) const;
58 
59  /// Emit include headers needed by the generated shader code.
60  virtual void emitLibraryIncludes(ShaderStage& stage, GenContext& context) const;
61 
62  /// Emit a block of shader inputs.
63  virtual void emitShaderInputs(const VariableBlock& inputs, ShaderStage& stage) const;
64 
65  /// Emit a block of shader outputs.
66  virtual void emitShaderOutputs(const VariableBlock& inputs, ShaderStage& stage) const;
67 
68  /// Emit metadata for a shader parameter.
69  virtual void emitMetadata(const ShaderPort* port, ShaderStage& stage) const;
70 
71  void addSetCiTerminalNode(ShaderGraph& graph, ConstDocumentPtr document, GenContext& context) const;
72 };
73 
74 namespace OSL
75 {
76 
77 /// Identifiers for OSL variable blocks
78 extern MX_GENOSL_API const string UNIFORMS;
79 extern MX_GENOSL_API const string INPUTS;
80 extern MX_GENOSL_API const string OUTPUTS;
81 
82 } // namespace OSL
83 
85 
86 #endif
#define MATERIALX_NAMESPACE_BEGIN
Definition: Generated.h:25
MX_GENOSL_API const string INPUTS
shared_ptr< class OslShaderGenerator > OslShaderGeneratorPtr
virtual void registerShaderMetadata(const DocumentPtr &doc, GenContext &context) const
static ShaderGeneratorPtr create(TypeSystemPtr typeSystem=nullptr)
static TypeSystemPtr create()
Create a new type system.
MX_GENOSL_API const string UNIFORMS
Identifiers for OSL variable blocks.
shared_ptr< class TypeSystem > TypeSystemPtr
Definition: TypeDesc.h:219
#define MX_GENOSL_API
Definition: Export.h:18
shared_ptr< Document > DocumentPtr
A shared pointer to a Document.
Definition: Document.h:22
GLuint const GLchar * name
Definition: glcorearb.h:786
MX_GENOSL_API const string OUTPUTS
static const string TARGET
Unique identifier for this generator target.
MX_RENDER_API ShaderPtr createShader(const string &shaderName, GenContext &context, ElementPtr elem)
Create a shader for a given element.
shared_ptr< Shader > ShaderPtr
Shared pointer to a Shader.
Definition: Library.h:33
shared_ptr< Element > ElementPtr
A shared pointer to an Element.
Definition: Element.h:31
#define MATERIALX_NAMESPACE_END
Definition: Generated.h:26
const string & getTarget() const override
Return a unique identifier for the target this generator is for.
shared_ptr< ShaderGenerator > ShaderGeneratorPtr
Shared pointer to a ShaderGenerator.
Definition: Library.h:37
shared_ptr< const Document > ConstDocumentPtr
A shared pointer to a const Document.
Definition: Document.h:24
virtual ShaderPtr generate(const string &, ElementPtr, GenContext &) const