xref: /aosp_15_r20/external/angle/third_party/glslang/src/gtests/VkRelaxed.FromFile.cpp (revision 8975f5c5ed3d1c378011245431ada316dfb6f244)
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 namespace glslangtest {
46 namespace {
47 
48 struct vkRelaxedData {
49     std::vector<std::string> fileNames;
50     std::vector<std::vector<std::string>> resourceSetBindings;
51 };
52 
53 using VulkanRelaxedTest = GlslangTest <::testing::TestWithParam<vkRelaxedData>>;
54 
55 template<class T>
interfaceName(T symbol)56 std::string interfaceName(T symbol) {
57     return symbol.getType()->getBasicType() == glslang::EbtBlock ? std::string(symbol.getType()->getTypeName().c_str()) : symbol.name;
58 }
59 
verifyIOMapping(std::string & linkingError,glslang::TProgram & program)60 bool verifyIOMapping(std::string& linkingError, glslang::TProgram& program) {
61     bool success = true;
62 
63     // Verify IO Mapping by generating reflection for each stage individually
64     // and comparing layout qualifiers on the results
65 
66 
67     int reflectionOptions = EShReflectionDefault;
68     //reflectionOptions |= EShReflectionStrictArraySuffix;
69     //reflectionOptions |= EShReflectionBasicArraySuffix;
70     reflectionOptions |= EShReflectionIntermediateIO;
71     reflectionOptions |= EShReflectionSeparateBuffers;
72     reflectionOptions |= EShReflectionAllBlockVariables;
73     //reflectionOptions |= EShReflectionUnwrapIOBlocks;
74 
75     success &= program.buildReflection(reflectionOptions);
76 
77     // check that the reflection output from the individual stages all makes sense..
78     std::vector<glslang::TReflection> stageReflections;
79     for (int s = 0; s < EShLangCount; ++s) {
80         if (program.getIntermediate((EShLanguage)s)) {
81             stageReflections.emplace_back((EShReflectionOptions)reflectionOptions, (EShLanguage)s, (EShLanguage)s);
82             success &= stageReflections.back().addStage((EShLanguage)s, *program.getIntermediate((EShLanguage)s));
83         }
84     }
85 
86     // check that input/output locations match between stages
87     auto it = stageReflections.begin();
88     auto nextIt = it + 1;
89     for (; nextIt != stageReflections.end(); it++, nextIt++) {
90         int numOut = it->getNumPipeOutputs();
91         std::map<std::string, const glslang::TObjectReflection*> pipeOut;
92 
93         for (int i = 0; i < numOut; i++) {
94             const glslang::TObjectReflection& out = it->getPipeOutput(i);
95             std::string name = interfaceName(out);
96             pipeOut[name] = &out;
97         }
98 
99         int numIn = nextIt->getNumPipeInputs();
100         for (int i = 0; i < numIn; i++) {
101             auto in = nextIt->getPipeInput(i);
102             std::string name = interfaceName(in);
103             auto out = pipeOut.find(name);
104 
105             if (out != pipeOut.end()) {
106                 auto inQualifier = in.getType()->getQualifier();
107                 auto outQualifier = out->second->getType()->getQualifier();
108                 success &= outQualifier.layoutLocation == inQualifier.layoutLocation;
109             // These are not part of a matched interface. Other cases still need to be added.
110             } else if (name != "gl_FrontFacing" && name != "gl_FragCoord") {
111                 success &= false;
112             }
113         }
114     }
115 
116     // compare uniforms in each stage to the program
117     {
118         int totalUniforms = program.getNumUniformVariables();
119         std::map<std::string, const glslang::TObjectReflection*> programUniforms;
120         for (int i = 0; i < totalUniforms; i++) {
121             const glslang::TObjectReflection& uniform = program.getUniform(i);
122             std::string name = interfaceName(uniform);
123             programUniforms[name] = &uniform;
124         }
125         it = stageReflections.begin();
126         for (; it != stageReflections.end(); it++) {
127             int numUniform = it->getNumUniforms();
128             std::map<std::string, glslang::TObjectReflection> uniforms;
129 
130             for (int i = 0; i < numUniform; i++) {
131                 glslang::TObjectReflection uniform = it->getUniform(i);
132                 std::string name = interfaceName(uniform);
133                 auto programUniform = programUniforms.find(name);
134 
135                 if (programUniform != programUniforms.end()) {
136                     auto stageQualifier = uniform.getType()->getQualifier();
137                     auto programQualifier = programUniform->second->getType()->getQualifier();
138 
139                     success &= stageQualifier.layoutLocation == programQualifier.layoutLocation;
140                     success &= stageQualifier.layoutBinding == programQualifier.layoutBinding;
141                     success &= stageQualifier.layoutSet == programQualifier.layoutSet;
142                 }
143                 else {
144                     success &= false;
145                 }
146             }
147         }
148     }
149 
150     // compare uniform blocks in each stage to the program table
151     {
152         int totalUniforms = program.getNumUniformBlocks();
153         std::map<std::string, const glslang::TObjectReflection*> programUniforms;
154         for (int i = 0; i < totalUniforms; i++) {
155             const glslang::TObjectReflection& uniform = program.getUniformBlock(i);
156             std::string name = interfaceName(uniform);
157             programUniforms[name] = &uniform;
158         }
159         it = stageReflections.begin();
160         for (; it != stageReflections.end(); it++) {
161             int numUniform = it->getNumUniformBlocks();
162             std::map<std::string, glslang::TObjectReflection> uniforms;
163 
164             for (int i = 0; i < numUniform; i++) {
165                 glslang::TObjectReflection uniform = it->getUniformBlock(i);
166                 std::string name = interfaceName(uniform);
167                 auto programUniform = programUniforms.find(name);
168 
169                 if (programUniform != programUniforms.end()) {
170                     auto stageQualifier = uniform.getType()->getQualifier();
171                     auto programQualifier = programUniform->second->getType()->getQualifier();
172 
173                     success &= stageQualifier.layoutLocation == programQualifier.layoutLocation;
174                     success &= stageQualifier.layoutBinding == programQualifier.layoutBinding;
175                     success &= stageQualifier.layoutSet == programQualifier.layoutSet;
176                 }
177                 else {
178                     success &= false;
179                 }
180             }
181         }
182     }
183 
184     if (!success) {
185         linkingError += "Mismatched cross-stage IO\n";
186     }
187 
188     return success;
189 }
190 
TEST_P(VulkanRelaxedTest,FromFile)191 TEST_P(VulkanRelaxedTest, FromFile)
192 {
193     const auto& fileNames = GetParam().fileNames;
194     const auto& resourceSetBindings = GetParam().resourceSetBindings;
195     Semantics semantics = Semantics::Vulkan;
196     const size_t fileCount = fileNames.size();
197     const EShMessages controls = DeriveOptions(Source::GLSL, semantics, Target::BothASTAndSpv);
198     GlslangResult result;
199 
200     // Compile each input shader file.
201     bool success = true;
202     std::vector<std::unique_ptr<glslang::TShader>> shaders;
203     for (size_t i = 0; i < fileCount; ++i) {
204         std::string contents;
205         tryLoadFile(GlobalTestSettings.testRoot + "/" + fileNames[i],
206             "input", &contents);
207         shaders.emplace_back(
208             new glslang::TShader(GetShaderStage(GetSuffix(fileNames[i]))));
209         auto* shader = shaders.back().get();
210 
211         shader->setAutoMapLocations(true);
212         shader->setAutoMapBindings(true);
213 
214         shader->setEnvInput(glslang::EShSourceGlsl, shader->getStage(), glslang::EShClientVulkan, 100);
215         shader->setEnvClient(glslang::EShClientVulkan, glslang::EShTargetVulkan_1_1);
216         shader->setEnvTarget(glslang::EShTargetSpv, glslang::EShTargetSpv_1_0);
217 
218         // Use vulkan relaxed option
219         shader->setEnvInputVulkanRulesRelaxed();
220 
221         success &= compile(shader, contents, "", controls);
222 
223         result.shaderResults.push_back(
224             { fileNames[i], shader->getInfoLog(), shader->getInfoDebugLog() });
225     }
226 
227     // Link all of them.
228     glslang::TProgram program;
229     for (const auto& shader : shaders) program.addShader(shader.get());
230     success &= program.link(controls);
231     result.linkingOutput = program.getInfoLog();
232     result.linkingError = program.getInfoDebugLog();
233 
234     if (!resourceSetBindings.empty()) {
235         assert(resourceSetBindings.size() == fileNames.size());
236         for (size_t i = 0; i < shaders.size(); i++)
237             shaders[i]->setResourceSetBinding(resourceSetBindings[i]);
238     }
239 
240     glslang::TIoMapResolver *resolver;
241     for (unsigned stage = 0; stage < EShLangCount; stage++) {
242         resolver = program.getGlslIoResolver((EShLanguage)stage);
243         if (resolver)
244             break;
245     }
246     glslang::TIoMapper *ioMapper = glslang::GetGlslIoMapper();
247 
248     if (success) {
249         success &= program.mapIO(resolver, ioMapper);
250         result.linkingOutput = program.getInfoLog();
251         result.linkingError = program.getInfoDebugLog();
252     }
253     delete ioMapper;
254     delete resolver;
255 
256     success &= verifyIOMapping(result.linkingError, program);
257     result.validationResult = success;
258 
259     if (success && (controls & EShMsgSpvRules)) {
260         for (int stage = 0; stage < EShLangCount; ++stage) {
261             if (program.getIntermediate((EShLanguage)stage)) {
262                 spv::SpvBuildLogger logger;
263                 std::vector<uint32_t> spirv_binary;
264                 options().disableOptimizer = false;
265                 glslang::GlslangToSpv(*program.getIntermediate((EShLanguage)stage),
266                     spirv_binary, &logger, &options());
267 
268                 std::ostringstream disassembly_stream;
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.0.0.vert", "vk.relaxed.stagelink.0.1.vert", "vk.relaxed.stagelink.0.2.vert", "vk.relaxed.stagelink.0.0.frag", "vk.relaxed.stagelink.0.1.frag", "vk.relaxed.stagelink.0.2.frag"}, {}},
297         {{"vk.relaxed.stagelink.vert", "vk.relaxed.stagelink.frag"}, {}},
298         {{"vk.relaxed.errorcheck.vert", "vk.relaxed.errorcheck.frag"}, {}},
299         {{"vk.relaxed.changeSet.vert", "vk.relaxed.changeSet.frag" }, { {"0"}, {"1"} } },
300     }))
301 );
302 // clang-format on
303 
304 }  // anonymous namespace
305 }  // namespace glslangtest
306