1 //
2 // Copyright (C) 2016-2017 Google, Inc.
3 // Copyright (C) 2020 The Khronos Group Inc.
4 //
5 // All rights reserved.
6 //
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions
9 // are met:
10 //
11 //    Redistributions of source code must retain the above copyright
12 //    notice, this list of conditions and the following disclaimer.
13 //
14 //    Redistributions in binary form must reproduce the above
15 //    copyright notice, this list of conditions and the following
16 //    disclaimer in the documentation and/or other materials provided
17 //    with the distribution.
18 //
19 //    Neither the name of 3Dlabs Inc. Ltd. nor the names of its
20 //    contributors may be used to endorse or promote products derived
21 //    from this software without specific prior written permission.
22 //
23 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26 // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27 // COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28 // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
29 // BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
31 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
33 // ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34 // POSSIBILITY OF SUCH DAMAGE.
35 //
36 #include <algorithm>
37 
38 #include <gtest/gtest.h>
39 
40 #include "TestFixture.h"
41 
42 #include "glslang/MachineIndependent/iomapper.h"
43 #include "glslang/MachineIndependent/reflection.h"
44 
45 #ifndef GLSLANG_WEB
46 namespace glslangtest {
47 namespace {
48 
49 struct vkRelaxedData {
50     std::vector<std::string> fileNames;
51     std::vector<std::vector<std::string>> resourceSetBindings;
52 };
53 
54 using VulkanRelaxedTest = GlslangTest <::testing::TestWithParam<vkRelaxedData>>;
55 
56 template<class T>
interfaceName(T symbol)57 std::string interfaceName(T symbol) {
58     return symbol.getType()->getBasicType() == glslang::EbtBlock ? std::string(symbol.getType()->getTypeName().c_str()) : symbol.name;
59 }
60 
verifyIOMapping(std::string & linkingError,glslang::TProgram & program)61 bool verifyIOMapping(std::string& linkingError, glslang::TProgram& program) {
62     bool success = true;
63 
64     // Verify IO Mapping by generating reflection for each stage individually
65     // and comparing layout qualifiers on the results
66 
67 
68     int reflectionOptions = EShReflectionDefault;
69     //reflectionOptions |= EShReflectionStrictArraySuffix;
70     //reflectionOptions |= EShReflectionBasicArraySuffix;
71     reflectionOptions |= EShReflectionIntermediateIO;
72     reflectionOptions |= EShReflectionSeparateBuffers;
73     reflectionOptions |= EShReflectionAllBlockVariables;
74     //reflectionOptions |= EShReflectionUnwrapIOBlocks;
75 
76     success &= program.buildReflection(reflectionOptions);
77 
78     // check that the reflection output from the individual stages all makes sense..
79     std::vector<glslang::TReflection> stageReflections;
80     for (int s = 0; s < EShLangCount; ++s) {
81         if (program.getIntermediate((EShLanguage)s)) {
82             stageReflections.emplace_back((EShReflectionOptions)reflectionOptions, (EShLanguage)s, (EShLanguage)s);
83             success &= stageReflections.back().addStage((EShLanguage)s, *program.getIntermediate((EShLanguage)s));
84         }
85     }
86 
87     // check that input/output locations match between stages
88     auto it = stageReflections.begin();
89     auto nextIt = it + 1;
90     for (; nextIt != stageReflections.end(); it++, nextIt++) {
91         int numOut = it->getNumPipeOutputs();
92         std::map<std::string, const glslang::TObjectReflection*> pipeOut;
93 
94         for (int i = 0; i < numOut; i++) {
95             const glslang::TObjectReflection& out = it->getPipeOutput(i);
96             std::string name = interfaceName(out);
97             pipeOut[name] = &out;
98         }
99 
100         int numIn = nextIt->getNumPipeInputs();
101         for (int i = 0; i < numIn; i++) {
102             auto in = nextIt->getPipeInput(i);
103             std::string name = interfaceName(in);
104             auto out = pipeOut.find(name);
105 
106             if (out != pipeOut.end()) {
107                 auto inQualifier = in.getType()->getQualifier();
108                 auto outQualifier = out->second->getType()->getQualifier();
109                 success &= outQualifier.layoutLocation == inQualifier.layoutLocation;
110             }
111             else {
112                 success &= false;
113             }
114         }
115     }
116 
117     // compare uniforms in each stage to the program
118     {
119         int totalUniforms = program.getNumUniformVariables();
120         std::map<std::string, const glslang::TObjectReflection*> programUniforms;
121         for (int i = 0; i < totalUniforms; i++) {
122             const glslang::TObjectReflection& uniform = program.getUniform(i);
123             std::string name = interfaceName(uniform);
124             programUniforms[name] = &uniform;
125         }
126         it = stageReflections.begin();
127         for (; it != stageReflections.end(); it++) {
128             int numUniform = it->getNumUniforms();
129             std::map<std::string, glslang::TObjectReflection> uniforms;
130 
131             for (int i = 0; i < numUniform; i++) {
132                 glslang::TObjectReflection uniform = it->getUniform(i);
133                 std::string name = interfaceName(uniform);
134                 auto programUniform = programUniforms.find(name);
135 
136                 if (programUniform != programUniforms.end()) {
137                     auto stageQualifier = uniform.getType()->getQualifier();
138                     auto programQualifier = programUniform->second->getType()->getQualifier();
139 
140                     success &= stageQualifier.layoutLocation == programQualifier.layoutLocation;
141                     success &= stageQualifier.layoutBinding == programQualifier.layoutBinding;
142                     success &= stageQualifier.layoutSet == programQualifier.layoutSet;
143                 }
144                 else {
145                     success &= false;
146                 }
147             }
148         }
149     }
150 
151     // compare uniform blocks in each stage to the program table
152     {
153         int totalUniforms = program.getNumUniformBlocks();
154         std::map<std::string, const glslang::TObjectReflection*> programUniforms;
155         for (int i = 0; i < totalUniforms; i++) {
156             const glslang::TObjectReflection& uniform = program.getUniformBlock(i);
157             std::string name = interfaceName(uniform);
158             programUniforms[name] = &uniform;
159         }
160         it = stageReflections.begin();
161         for (; it != stageReflections.end(); it++) {
162             int numUniform = it->getNumUniformBlocks();
163             std::map<std::string, glslang::TObjectReflection> uniforms;
164 
165             for (int i = 0; i < numUniform; i++) {
166                 glslang::TObjectReflection uniform = it->getUniformBlock(i);
167                 std::string name = interfaceName(uniform);
168                 auto programUniform = programUniforms.find(name);
169 
170                 if (programUniform != programUniforms.end()) {
171                     auto stageQualifier = uniform.getType()->getQualifier();
172                     auto programQualifier = programUniform->second->getType()->getQualifier();
173 
174                     success &= stageQualifier.layoutLocation == programQualifier.layoutLocation;
175                     success &= stageQualifier.layoutBinding == programQualifier.layoutBinding;
176                     success &= stageQualifier.layoutSet == programQualifier.layoutSet;
177                 }
178                 else {
179                     success &= false;
180                 }
181             }
182         }
183     }
184 
185     if (!success) {
186         linkingError += "Mismatched cross-stage IO\n";
187     }
188 
189     return success;
190 }
191 
TEST_P(VulkanRelaxedTest,FromFile)192 TEST_P(VulkanRelaxedTest, FromFile)
193 {
194     const auto& fileNames = GetParam().fileNames;
195     const auto& resourceSetBindings = GetParam().resourceSetBindings;
196     Semantics semantics = Semantics::Vulkan;
197     const size_t fileCount = fileNames.size();
198     const EShMessages controls = DeriveOptions(Source::GLSL, semantics, Target::BothASTAndSpv);
199     GlslangResult result;
200 
201     // Compile each input shader file.
202     bool success = true;
203     std::vector<std::unique_ptr<glslang::TShader>> shaders;
204     for (size_t i = 0; i < fileCount; ++i) {
205         std::string contents;
206         tryLoadFile(GlobalTestSettings.testRoot + "/" + fileNames[i],
207             "input", &contents);
208         shaders.emplace_back(
209             new glslang::TShader(GetShaderStage(GetSuffix(fileNames[i]))));
210         auto* shader = shaders.back().get();
211 
212         shader->setAutoMapLocations(true);
213         shader->setAutoMapBindings(true);
214 
215         shader->setEnvInput(glslang::EShSourceGlsl, shader->getStage(), glslang::EShClientVulkan, 100);
216         shader->setEnvClient(glslang::EShClientVulkan, glslang::EShTargetVulkan_1_1);
217         shader->setEnvTarget(glslang::EShTargetSpv, glslang::EShTargetSpv_1_0);
218 
219         // Use vulkan relaxed option
220         shader->setEnvInputVulkanRulesRelaxed();
221 
222         success &= compile(shader, contents, "", controls);
223 
224         result.shaderResults.push_back(
225             { fileNames[i], shader->getInfoLog(), shader->getInfoDebugLog() });
226     }
227 
228     // Link all of them.
229     glslang::TProgram program;
230     for (const auto& shader : shaders) program.addShader(shader.get());
231     success &= program.link(controls);
232     result.linkingOutput = program.getInfoLog();
233     result.linkingError = program.getInfoDebugLog();
234 
235     if (!resourceSetBindings.empty()) {
236         assert(resourceSetBindings.size() == fileNames.size());
237         for (size_t i = 0; i < shaders.size(); i++)
238             shaders[i]->setResourceSetBinding(resourceSetBindings[i]);
239     }
240 
241     unsigned int stage = 0;
242     glslang::TIntermediate* firstIntermediate = nullptr;
243     while (!program.getIntermediate((EShLanguage)stage) && stage < EShLangCount) { stage++; }
244     firstIntermediate = program.getIntermediate((EShLanguage)stage);
245 
246     glslang::TDefaultGlslIoResolver resolver(*firstIntermediate);
247     glslang::TGlslIoMapper ioMapper;
248 
249     if (success) {
250         success &= program.mapIO(&resolver, &ioMapper);
251         result.linkingOutput = program.getInfoLog();
252         result.linkingError = program.getInfoDebugLog();
253     }
254 
255     success &= verifyIOMapping(result.linkingError, program);
256     result.validationResult = success;
257 
258     if (success && (controls & EShMsgSpvRules)) {
259         for (int stage = 0; stage < EShLangCount; ++stage) {
260             if (program.getIntermediate((EShLanguage)stage)) {
261                 spv::SpvBuildLogger logger;
262                 std::vector<uint32_t> spirv_binary;
263                 options().disableOptimizer = false;
264                 glslang::GlslangToSpv(*program.getIntermediate((EShLanguage)stage),
265                     spirv_binary, &logger, &options());
266 
267                 std::ostringstream disassembly_stream;
268                 spv::Parameterize();
269                 spv::Disassemble(disassembly_stream, spirv_binary);
270                 result.spirvWarningsErrors += logger.getAllMessages();
271                 result.spirv += disassembly_stream.str();
272                 result.validationResult &= !options().validate || logger.getAllMessages().empty();
273             }
274         }
275     }
276 
277     std::ostringstream stream;
278     outputResultToStream(&stream, result, controls);
279 
280     // Check with expected results.
281     const std::string expectedOutputFname =
282         GlobalTestSettings.testRoot + "/baseResults/" + fileNames.front() + ".out";
283     std::string expectedOutput;
284     tryLoadFile(expectedOutputFname, "expected output", &expectedOutput);
285 
286     checkEqAndUpdateIfRequested(expectedOutput, stream.str(), expectedOutputFname,
287         result.spirvWarningsErrors);
288 }
289 
290 // clang-format off
291 INSTANTIATE_TEST_SUITE_P(
292     Glsl, VulkanRelaxedTest,
293     ::testing::ValuesIn(std::vector<vkRelaxedData>({
294         {{"vk.relaxed.frag"}},
295         {{"vk.relaxed.link1.frag", "vk.relaxed.link2.frag"}},
296         {{"vk.relaxed.stagelink.vert", "vk.relaxed.stagelink.frag"}},
297         {{"vk.relaxed.errorcheck.vert", "vk.relaxed.errorcheck.frag"}},
298         {{"vk.relaxed.changeSet.vert", "vk.relaxed.changeSet.frag" }, { {"0"}, {"1"} } },
299     }))
300 );
301 // clang-format on
302 
303 }  // anonymous namespace
304 }  // namespace glslangtest
305 #endif
306