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:
27 
28  static ShaderGeneratorPtr create() { return std::make_shared<OslShaderGenerator>(); }
29 
30  /// Return a unique identifier for the target this generator is for
31  const string& getTarget() const override { return TARGET; }
32 
33  /// Generate a shader starting from the given element, translating
34  /// the element and all dependencies upstream into shader code.
35  ShaderPtr generate(const string& name, ElementPtr element, GenContext& context) const override;
36 
37  /// Add all function calls for a graph. If a classification mask is given only functions for
38  /// nodes matching this classification will be emitted.
39  void emitFunctionCalls(const ShaderGraph& graph, GenContext& context, ShaderStage& stage, uint32_t classification = 0u) const override;
40 
41  /// Emit code for starting a new function body.
42  void emitFunctionBodyBegin(const ShaderNode& node, GenContext& context, ShaderStage& stage, Syntax::Punctuation punc = Syntax::CURLY_BRACKETS) const override;
43 
44  /// Unique identifier for this generator target
45  static const string TARGET;
46 
47  /// Register metadata that should be exported to the generated shaders.
48  void registerShaderMetadata(const DocumentPtr& doc, GenContext& context) const override;
49 
50  protected:
51  /// Create and initialize a new OSL shader for shader generation.
52  virtual ShaderPtr createShader(const string& name, ElementPtr element, GenContext& context) const;
53 
54  /// Emit include headers needed by the generated shader code.
55  virtual void emitLibraryIncludes(ShaderStage& stage, GenContext& context) const;
56 
57  /// Emit a block of shader inputs.
58  virtual void emitShaderInputs(const VariableBlock& inputs, ShaderStage& stage) const;
59 
60  /// Emit a block of shader outputs.
61  virtual void emitShaderOutputs(const VariableBlock& inputs, ShaderStage& stage) const;
62 
63  /// Emit metadata for a shader parameter.
64  virtual void emitMetadata(const ShaderPort* port, ShaderStage& stage) const;
65 };
66 
67 namespace OSL
68 {
69 
70 /// Identifiers for OSL variable blocks
71 extern MX_GENOSL_API const string UNIFORMS;
72 extern MX_GENOSL_API const string INPUTS;
73 extern MX_GENOSL_API const string OUTPUTS;
74 
75 } // namespace OSL
76 
78 
79 #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
MX_GENOSL_API const string UNIFORMS
Identifiers for OSL variable blocks.
#define MX_GENOSL_API
Definition: Export.h:18
static ShaderGeneratorPtr create()
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:43
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:34
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:38
virtual ShaderPtr generate(const string &, ElementPtr, GenContext &) const