1 //
2 // Copyright 2020 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 
25 #ifndef PXR_IMAGING_HGIGL_SHADERSECTION_H
26 #define PXR_IMAGING_HGIGL_SHADERSECTION_H
27 
28 #include "pxr/imaging/hgi/shaderFunctionDesc.h"
29 #include "pxr/imaging/hgi/shaderSection.h"
30 #include "pxr/imaging/hgiGL/api.h"
31 
32 #include <string>
33 #include <vector>
34 
35 PXR_NAMESPACE_OPEN_SCOPE
36 
37 /// \class HgiGLShaderSection
38 ///
39 /// Base class for OpenGL code sections. The generator holds these
40 ///
41 class HgiGLShaderSection : public HgiShaderSection
42 {
43 public:
44     HGIGL_API
45     explicit HgiGLShaderSection(
46         const std::string &identifier,
47         const HgiShaderSectionAttributeVector &attributes = {},
48         const std::string &storageQualifier = std::string(),
49         const std::string &defaultValue = std::string());
50 
51     HGIGL_API
52     ~HgiGLShaderSection() override;
53 
54     HGIGL_API
55     void WriteDeclaration(std::ostream &ss) const override;
56     HGIGL_API
57     void WriteParameter(std::ostream &ss) const override;
58 
59     HGIGL_API
60     virtual bool VisitGlobalIncludes(std::ostream &ss);
61     HGIGL_API
62     virtual bool VisitGlobalMacros(std::ostream &ss);
63     HGIGL_API
64     virtual bool VisitGlobalStructs(std::ostream &ss);
65     HGIGL_API
66     virtual bool VisitGlobalMemberDeclarations(std::ostream &ss);
67     HGIGL_API
68     virtual bool VisitGlobalFunctionDefinitions(std::ostream &ss);
69 
70 private:
71     HgiGLShaderSection() = delete;
72     HgiGLShaderSection & operator=(const HgiGLShaderSection&) = delete;
73     HgiGLShaderSection(const HgiGLShaderSection&) = delete;
74 
75     const std::string _storageQualifier;
76 };
77 
78 /// \class HgiGLMacroShaderSection
79 ///
80 /// A ShaderSection for defining macros.
81 /// Accepts raw strings and dumps it to the global scope under includes
82 ///
83 class HgiGLMacroShaderSection final: public HgiGLShaderSection
84 {
85 public:
86     HGIGL_API
87     explicit HgiGLMacroShaderSection(
88         const std::string &macroDeclaration,
89         const std::string &macroComment);
90 
91     HGIGL_API
92     ~HgiGLMacroShaderSection() override;
93 
94     HGIGL_API
95     bool VisitGlobalMacros(std::ostream &ss) override;
96 
97 private:
98     HgiGLMacroShaderSection() = delete;
99     HgiGLMacroShaderSection & operator=(
100         const HgiGLMacroShaderSection&) = delete;
101     HgiGLMacroShaderSection(const HgiGLMacroShaderSection&) = delete;
102 
103     const std::string _macroComment;
104 };
105 
106 /// \class HgiGLMemberShaderSection
107 ///
108 /// Declares a member in global scope, for declaring instances of structs, constant
109 /// params etc - it's quite flexible in it's writing capabilities
110 ///
111 class HgiGLMemberShaderSection final: public HgiGLShaderSection
112 {
113 public:
114     HGIGL_API
115     explicit HgiGLMemberShaderSection(
116         const std::string &identifier,
117         const std::string &typeName,
118         const HgiShaderSectionAttributeVector &attributes,
119         const std::string &storageQualifier = std::string(),
120         const std::string &defaultValue = std::string());
121 
122     HGIGL_API
123     ~HgiGLMemberShaderSection() override;
124 
125     HGIGL_API
126     bool VisitGlobalMemberDeclarations(std::ostream &ss) override;
127 
128     HGIGL_API
129     void WriteType(std::ostream& ss) const override;
130 
131 private:
132     HgiGLMemberShaderSection() = delete;
133     HgiGLMemberShaderSection & operator=(
134         const HgiGLMemberShaderSection&) = delete;
135     HgiGLMemberShaderSection(const HgiGLMemberShaderSection&) = delete;
136 
137     std::string _typeName;
138 };
139 
140 /// \class HgiGLBlockShaderSection
141 ///
142 /// For writing out uniform blocks, defines them in the global member declerations.
143 ///
144 class HgiGLBlockShaderSection final: public HgiGLShaderSection
145 {
146 public:
147     HGIGL_API
148     explicit HgiGLBlockShaderSection(
149             const std::string &identifier,
150             const HgiShaderFunctionParamDescVector &parameters,
151             const uint32_t bindingNo=0);
152 
153     HGIGL_API
154     ~HgiGLBlockShaderSection() override;
155 
156     HGIGL_API
157     bool VisitGlobalMemberDeclarations(std::ostream &ss) override;
158 
159 private:
160     const HgiShaderFunctionParamDescVector _parameters;
161     const uint32_t _bindingNo;
162 };
163 
164 /// \class HgiGLTextureShaderSection
165 ///
166 /// Declares OpenGL textures, and their cross language function
167 ///
168 class HgiGLTextureShaderSection final: public HgiGLShaderSection
169 {
170 public:
171     HGIGL_API
172     explicit HgiGLTextureShaderSection(
173         const std::string &identifier,
174         const uint32_t layoutIndex,
175         const uint32_t dimensions,
176         const HgiFormat format,
177         const HgiShaderSectionAttributeVector &attributes,
178         const std::string &defaultValue = std::string());
179 
180     HGIGL_API
181     ~HgiGLTextureShaderSection() override;
182 
183     HGIGL_API
184     void WriteType(std::ostream &ss) const override;
185 
186     HGIGL_API
187     bool VisitGlobalMemberDeclarations(std::ostream &ss) override;
188     HGIGL_API
189     bool VisitGlobalFunctionDefinitions(std::ostream &ss) override;
190 
191 private:
192     HgiGLTextureShaderSection() = delete;
193     HgiGLTextureShaderSection & operator=(
194         const HgiGLTextureShaderSection&) = delete;
195     HgiGLTextureShaderSection(const HgiGLTextureShaderSection&) = delete;
196 
197     void _WriteSamplerType(std::ostream &ss) const;
198     void _WriteSampledDataType(std::ostream &ss) const;
199 
200     const uint32_t _dimensions;
201     const HgiFormat _format;
202     static const std::string _storageQualifier;
203 };
204 
205 /// \class HgiGLBufferShaderSection
206 ///
207 /// Declares OpenGL buffers, and their cross language function
208 ///
209 class HgiGLBufferShaderSection final: public HgiGLShaderSection
210 {
211 public:
212     HGIGL_API
213     explicit HgiGLBufferShaderSection(
214         const std::string &identifier,
215         const uint32_t layoutIndex,
216         const std::string &type,
217         const HgiShaderSectionAttributeVector &attributes);
218 
219     HGIGL_API
220     ~HgiGLBufferShaderSection() override;
221 
222     HGIGL_API
223     void WriteType(std::ostream &ss) const override;
224 
225     HGIGL_API
226     bool VisitGlobalMemberDeclarations(std::ostream &ss) override;
227 
228 private:
229     HgiGLBufferShaderSection() = delete;
230     HgiGLBufferShaderSection & operator=(
231         const HgiGLBufferShaderSection&) = delete;
232     HgiGLBufferShaderSection(const HgiGLBufferShaderSection&) = delete;
233 
234     const std::string _type;
235 };
236 
237 /// \class HgiGLKeywordShaderSection
238 ///
239 /// Declares reserved OpenGL shader inputs, and their cross language function
240 ///
241 class HgiGLKeywordShaderSection final: public HgiGLShaderSection
242 {
243 public:
244     HGIGL_API
245     explicit HgiGLKeywordShaderSection(
246         const std::string &identifier,
247         const std::string &type,
248         const std::string &glKeyword);
249 
250     HGIGL_API
251     ~HgiGLKeywordShaderSection() override;
252 
253     HGIGL_API
254     void WriteType(std::ostream &ss) const override;
255 
256     HGIGL_API
257     bool VisitGlobalMemberDeclarations(std::ostream &ss) override;
258 
259 private:
260     HgiGLKeywordShaderSection() = delete;
261     HgiGLKeywordShaderSection & operator=(
262         const HgiGLKeywordShaderSection&) = delete;
263     HgiGLKeywordShaderSection(const HgiGLKeywordShaderSection&) = delete;
264 
265     const std::string _type;
266     const std::string _keyword;
267 };
268 
269 PXR_NAMESPACE_CLOSE_SCOPE
270 
271 #endif
272