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