1 //
2 // Copyright 2016 Pixar
3 //
4 // Licensed under the Apache License, Version 2.0 (the "Apache License")
5 // with the following modification; you may not use this file except in
6 // compliance with the Apache License and the following modification to it:
7 // Section 6. Trademarks. is deleted and replaced with:
8 //
9 // 6. Trademarks. This License does not grant permission to use the trade
10 //    names, trademarks, service marks, or product names of the Licensor
11 //    and its affiliates, except as required to comply with Section 4(c) of
12 //    the License and to reproduce the content of the NOTICE file.
13 //
14 // You may obtain a copy of the Apache License at
15 //
16 //     http://www.apache.org/licenses/LICENSE-2.0
17 //
18 // Unless required by applicable law or agreed to in writing, software
19 // distributed under the Apache License with the above modification is
20 // distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
21 // KIND, either express or implied. See the Apache License for the specific
22 // language governing permissions and limitations under the Apache License.
23 //
24 #ifndef PXR_IMAGING_HD_ST_CODE_GEN_H
25 #define PXR_IMAGING_HD_ST_CODE_GEN_H
26 
27 #include "pxr/pxr.h"
28 #include "pxr/imaging/hdSt/api.h"
29 #include "pxr/imaging/hd/version.h"
30 #include "pxr/imaging/hdSt/resourceBinder.h"
31 #include "pxr/imaging/hdSt/glslProgram.h"
32 
33 #include <map>
34 #include <vector>
35 #include <sstream>
36 
37 PXR_NAMESPACE_OPEN_SCOPE
38 
39 using HdStShaderCodeSharedPtr = std::shared_ptr<class HdStShaderCode>;
40 using HdStShaderCodeSharedPtrVector = std::vector<HdStShaderCodeSharedPtr>;
41 using HdSt_GeometricShaderPtr = std::shared_ptr<class HdSt_GeometricShader>;
42 
43 /// \class HdSt_CodeGen
44 ///
45 /// A utility class to compose glsl shader sources and compile them
46 /// upon request of HdShaderSpec.
47 ///
48 class HdSt_CodeGen
49 {
50 public:
51     typedef size_t ID;
52 
53     /// Constructor.
54     HDST_API
55     HdSt_CodeGen(HdSt_GeometricShaderPtr const &geometricShader,
56                HdStShaderCodeSharedPtrVector const &shaders,
57                TfToken const &materialTag);
58 
59     /// Constructor for non-geometric use cases.
60     /// Don't call compile when constructed this way.
61     /// Call CompileComputeProgram instead.
62     HDST_API
63     HdSt_CodeGen(HdStShaderCodeSharedPtrVector const &shaders);
64 
65     /// Return the hash value of glsl shader to be generated.
66     HDST_API
67     ID ComputeHash() const;
68 
69     /// Generate shader source and compile it.
70     HDST_API
71     HdStGLSLProgramSharedPtr Compile(
72         HdStResourceRegistry*const registry);
73 
74     /// Generate compute shader source and compile it.
75     /// It uses the compute information in the meta data to determine
76     /// layouts needed for a compute program.
77     /// The caller should have populated the meta data before calling this
78     /// using a method like HdSt_ResourceBinder::ResolveBindings.
79     ///
80     /// The layout and binding information is combined with the compute stage
81     /// shader code from the shader vector to form a resolved shader for
82     /// compilation.
83     ///
84     /// The generated code that is compiled is available for diagnostic
85     /// purposes from GetComputeShaderSource.
86     ///
87     /// \see GetComputeShaderSource
88     /// \see HdSt_ResourceBinder::ResolveBindings
89     HDST_API
90     HdStGLSLProgramSharedPtr CompileComputeProgram(
91         HdStResourceRegistry*const registry);
92 
93     /// Return the generated vertex shader source
GetVertexShaderSource()94     const std::string &GetVertexShaderSource() const { return _vsSource; }
95 
96     /// Return the generated tess control shader source
GetTessControlShaderSource()97     const std::string &GetTessControlShaderSource() const { return _tcsSource; }
98 
99     /// Return the generated tess eval shader source
GetTessEvalShaderSource()100     const std::string &GetTessEvalShaderSource() const { return _tesSource; }
101 
102     /// Return the generated geometry shader source
GetGeometryShaderSource()103     const std::string &GetGeometryShaderSource() const { return _gsSource; }
104 
105     /// Return the generated fragment shader source
GetFragmentShaderSource()106     const std::string &GetFragmentShaderSource() const { return _fsSource; }
107 
108     /// Return the generated compute shader source
GetComputeShaderSource()109     const std::string &GetComputeShaderSource() const { return _csSource; }
110 
111     /// Return the pointer of metadata to be populated by resource binder.
GetMetaData()112     HdSt_ResourceBinder::MetaData *GetMetaData() { return &_metaData; }
113 
114 private:
115     void _GenerateDrawingCoord();
116     void _GenerateConstantPrimvar();
117     void _GenerateInstancePrimvar();
118     void _GenerateElementPrimvar();
119     void _GenerateVertexAndFaceVaryingPrimvar(bool hasGS);
120     void _GenerateShaderParameters();
121     void _GenerateTopologyVisibilityParameters();
122 
123     HdSt_ResourceBinder::MetaData _metaData;
124     HdSt_GeometricShaderPtr _geometricShader;
125     HdStShaderCodeSharedPtrVector _shaders;
126     TfToken _materialTag;
127 
128     // source buckets
129     std::stringstream _genCommon, _genVS, _genTCS, _genTES;
130     std::stringstream _genGS, _genFS, _genCS;
131     std::stringstream _procVS, _procTCS, _procTES, _procGS;
132 
133     // generated sources (for diagnostics)
134     std::string _vsSource;
135     std::string _tcsSource;
136     std::string _tesSource;
137     std::string _gsSource;
138     std::string _fsSource;
139     std::string _csSource;
140 };
141 
142 
143 PXR_NAMESPACE_CLOSE_SCOPE
144 
145 #endif  // PXR_IMAGING_HD_ST_CODE_GEN_H
146