Implement GL_EXT_vulkan_glsl_relaxed option
[platform/upstream/glslang.git] / gtests / VkRelaxed.FromFile.cpp
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 };
52
53 using VulkanRelaxedTest = GlslangTest <::testing::TestWithParam<vkRelaxedData>>;
54
55 template<class T>
56 std::string interfaceName(T symbol) {
57     return symbol.getType()->getBasicType() == glslang::EbtBlock ? std::string(symbol.getType()->getTypeName().c_str()) : symbol.name;
58 }
59
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             }
110             else {
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
191 TEST_P(VulkanRelaxedTest, FromFile)
192 {
193     const auto& fileNames = GetParam().fileNames;
194     Semantics semantics = Semantics::Vulkan;
195     const size_t fileCount = fileNames.size();
196     const EShMessages controls = DeriveOptions(Source::GLSL, semantics, Target::BothASTAndSpv);
197     GlslangResult result;
198
199     // Compile each input shader file.
200     bool success = true;
201     std::vector<std::unique_ptr<glslang::TShader>> shaders;
202     for (size_t i = 0; i < fileCount; ++i) {
203         std::string contents;
204         tryLoadFile(GlobalTestSettings.testRoot + "/" + fileNames[i],
205             "input", &contents);
206         shaders.emplace_back(
207             new glslang::TShader(GetShaderStage(GetSuffix(fileNames[i]))));
208         auto* shader = shaders.back().get();
209         
210         shader->setAutoMapLocations(true);
211         shader->setAutoMapBindings(true);
212
213         shader->setEnvInput(glslang::EShSourceGlsl, shader->getStage(), glslang::EShClientVulkan, 100);
214         shader->setEnvClient(glslang::EShClientVulkan, glslang::EShTargetVulkan_1_1);
215         shader->setEnvTarget(glslang::EShTargetSpv, glslang::EShTargetSpv_1_0);
216
217         // Use vulkan relaxed option
218         shader->setEnvInputVulkanRulesRelaxed();
219
220         success &= compile(shader, contents, "", controls);
221         
222         result.shaderResults.push_back(
223             { fileNames[i], shader->getInfoLog(), shader->getInfoDebugLog() });
224     }
225
226     // Link all of them.
227     glslang::TProgram program;
228     for (const auto& shader : shaders) program.addShader(shader.get());
229     success &= program.link(controls);
230     result.linkingOutput = program.getInfoLog();
231     result.linkingError = program.getInfoDebugLog();
232
233     unsigned int stage = 0;
234     glslang::TIntermediate* firstIntermediate = nullptr;
235     while (!program.getIntermediate((EShLanguage)stage) && stage < EShLangCount) { stage++; }
236     firstIntermediate = program.getIntermediate((EShLanguage)stage);
237
238     glslang::TDefaultGlslIoResolver resolver(*firstIntermediate);
239     glslang::TGlslIoMapper ioMapper;
240
241     if (success) {
242         success &= program.mapIO(&resolver, &ioMapper);
243         result.linkingOutput = program.getInfoLog();
244         result.linkingError = program.getInfoDebugLog();
245     }
246
247     success &= verifyIOMapping(result.linkingError, program);
248     result.validationResult = success;
249
250     if (success && (controls & EShMsgSpvRules)) {
251         for (int stage = 0; stage < EShLangCount; ++stage) {
252             if (program.getIntermediate((EShLanguage)stage)) {
253                 spv::SpvBuildLogger logger;
254                 std::vector<uint32_t> spirv_binary;
255                 options().disableOptimizer = false;
256                 glslang::GlslangToSpv(*program.getIntermediate((EShLanguage)stage),
257                     spirv_binary, &logger, &options());
258
259                 std::ostringstream disassembly_stream;
260                 spv::Parameterize();
261                 spv::Disassemble(disassembly_stream, spirv_binary);
262                 result.spirvWarningsErrors += logger.getAllMessages();
263                 result.spirv += disassembly_stream.str();
264                 result.validationResult &= !options().validate || logger.getAllMessages().empty();
265             }
266         }
267     }
268
269     std::ostringstream stream;
270     outputResultToStream(&stream, result, controls);
271
272     // Check with expected results.
273     const std::string expectedOutputFname =
274         GlobalTestSettings.testRoot + "/baseResults/" + fileNames.front() + ".out";
275     std::string expectedOutput;
276     tryLoadFile(expectedOutputFname, "expected output", &expectedOutput);
277
278     checkEqAndUpdateIfRequested(expectedOutput, stream.str(), expectedOutputFname,
279         result.spirvWarningsErrors);
280 }
281
282 // clang-format off
283 INSTANTIATE_TEST_SUITE_P(
284     Glsl, VulkanRelaxedTest,
285     ::testing::ValuesIn(std::vector<vkRelaxedData>({
286         {{"vk.relaxed.frag"}},
287         {{"vk.relaxed.link1.frag", "vk.relaxed.link2.frag"}},
288         {{"vk.relaxed.stagelink.vert", "vk.relaxed.stagelink.frag"}},
289         {{"vk.relaxed.errorcheck.vert", "vk.relaxed.errorcheck.frag"}},
290     }))
291 );
292 // clang-format on
293
294 }  // anonymous namespace
295 }  // namespace glslangtest
296 #endif