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 graph. If a classification mask is given only functions for
47  /// nodes matching this classification will be emitted.
48  void emitFunctionCalls(const ShaderGraph& graph, GenContext& context, ShaderStage& stage, uint32_t classification = 0u) const override;
49 
50  /// Emit code for starting a new function body.
51  void emitFunctionBodyBegin(const ShaderNode& node, GenContext& context, ShaderStage& stage, Syntax::Punctuation punc = Syntax::CURLY_BRACKETS) const override;
52 
53  /// Unique identifier for this generator target
54  static const string TARGET;
55 
56  /// Register metadata that should be exported to the generated shaders.
57  void registerShaderMetadata(const DocumentPtr& doc, GenContext& context) const override;
58 
59  protected:
60  /// Create and initialize a new OSL shader for shader generation.
61  virtual ShaderPtr createShader(const string& name, ElementPtr element, GenContext& context) const;
62 
63  /// Emit include headers needed by the generated shader code.
64  virtual void emitLibraryIncludes(ShaderStage& stage, GenContext& context) const;
65 
66  /// Emit a block of shader inputs.
67  virtual void emitShaderInputs(const VariableBlock& inputs, ShaderStage& stage) const;
68 
69  /// Emit a block of shader outputs.
70  virtual void emitShaderOutputs(const VariableBlock& inputs, ShaderStage& stage) const;
71 
72  /// Emit metadata for a shader parameter.
73  virtual void emitMetadata(const ShaderPort* port, ShaderStage& stage) const;
74 };
75 
76 namespace OSL
77 {
78 
79 /// Identifiers for OSL variable blocks
80 extern MX_GENOSL_API const string UNIFORMS;
81 extern MX_GENOSL_API const string INPUTS;
82 extern MX_GENOSL_API const string OUTPUTS;
83 
84 } // namespace OSL
85 
87 
88 #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
virtual void emitFunctionBodyBegin(const ShaderNode &node, GenContext &context, ShaderStage &stage, Syntax::Punctuation punc=Syntax::CURLY_BRACKETS) const
Emit code for starting a new function body.
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.
Punctuation
Punctuation types.
Definition: Syntax.h:47
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.
virtual void emitFunctionCalls(const ShaderGraph &graph, GenContext &context, ShaderStage &stage, uint32_t classification=0u) const
shared_ptr< ShaderGenerator > ShaderGeneratorPtr
Shared pointer to a ShaderGenerator.
Definition: Library.h:37
virtual ShaderPtr generate(const string &, ElementPtr, GenContext &) const