2 // Copyright (C) 2016-2017 Google, Inc.
3 // Copyright (C) 2020 The Khronos Group Inc.
5 // All rights reserved.
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions
11 // Redistributions of source code must retain the above copyright
12 // notice, this list of conditions and the following disclaimer.
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.
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.
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.
38 #include <gtest/gtest.h>
40 #include "TestFixture.h"
42 #include "glslang/MachineIndependent/iomapper.h"
43 #include "glslang/MachineIndependent/reflection.h"
46 namespace glslangtest {
49 struct vkRelaxedData {
50 std::vector<std::string> fileNames;
53 using VulkanRelaxedTest = GlslangTest <::testing::TestWithParam<vkRelaxedData>>;
56 std::string interfaceName(T symbol) {
57 return symbol.getType()->getBasicType() == glslang::EbtBlock ? std::string(symbol.getType()->getTypeName().c_str()) : symbol.name;
60 bool verifyIOMapping(std::string& linkingError, glslang::TProgram& program) {
63 // Verify IO Mapping by generating reflection for each stage individually
64 // and comparing layout qualifiers on the results
67 int reflectionOptions = EShReflectionDefault;
68 //reflectionOptions |= EShReflectionStrictArraySuffix;
69 //reflectionOptions |= EShReflectionBasicArraySuffix;
70 reflectionOptions |= EShReflectionIntermediateIO;
71 reflectionOptions |= EShReflectionSeparateBuffers;
72 reflectionOptions |= EShReflectionAllBlockVariables;
73 //reflectionOptions |= EShReflectionUnwrapIOBlocks;
75 success &= program.buildReflection(reflectionOptions);
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));
86 // check that input/output locations match between stages
87 auto it = stageReflections.begin();
89 for (; nextIt != stageReflections.end(); it++, nextIt++) {
90 int numOut = it->getNumPipeOutputs();
91 std::map<std::string, const glslang::TObjectReflection*> pipeOut;
93 for (int i = 0; i < numOut; i++) {
94 const glslang::TObjectReflection& out = it->getPipeOutput(i);
95 std::string name = interfaceName(out);
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);
105 if (out != pipeOut.end()) {
106 auto inQualifier = in.getType()->getQualifier();
107 auto outQualifier = out->second->getType()->getQualifier();
108 success &= outQualifier.layoutLocation == inQualifier.layoutLocation;
116 // compare uniforms in each stage to the program
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;
125 it = stageReflections.begin();
126 for (; it != stageReflections.end(); it++) {
127 int numUniform = it->getNumUniforms();
128 std::map<std::string, glslang::TObjectReflection> uniforms;
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);
135 if (programUniform != programUniforms.end()) {
136 auto stageQualifier = uniform.getType()->getQualifier();
137 auto programQualifier = programUniform->second->getType()->getQualifier();
139 success &= stageQualifier.layoutLocation == programQualifier.layoutLocation;
140 success &= stageQualifier.layoutBinding == programQualifier.layoutBinding;
141 success &= stageQualifier.layoutSet == programQualifier.layoutSet;
150 // compare uniform blocks in each stage to the program table
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;
159 it = stageReflections.begin();
160 for (; it != stageReflections.end(); it++) {
161 int numUniform = it->getNumUniformBlocks();
162 std::map<std::string, glslang::TObjectReflection> uniforms;
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);
169 if (programUniform != programUniforms.end()) {
170 auto stageQualifier = uniform.getType()->getQualifier();
171 auto programQualifier = programUniform->second->getType()->getQualifier();
173 success &= stageQualifier.layoutLocation == programQualifier.layoutLocation;
174 success &= stageQualifier.layoutBinding == programQualifier.layoutBinding;
175 success &= stageQualifier.layoutSet == programQualifier.layoutSet;
185 linkingError += "Mismatched cross-stage IO\n";
191 TEST_P(VulkanRelaxedTest, FromFile)
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;
199 // Compile each input shader file.
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],
206 shaders.emplace_back(
207 new glslang::TShader(GetShaderStage(GetSuffix(fileNames[i]))));
208 auto* shader = shaders.back().get();
210 shader->setAutoMapLocations(true);
211 shader->setAutoMapBindings(true);
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);
217 // Use vulkan relaxed option
218 shader->setEnvInputVulkanRulesRelaxed();
220 success &= compile(shader, contents, "", controls);
222 result.shaderResults.push_back(
223 { fileNames[i], shader->getInfoLog(), shader->getInfoDebugLog() });
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();
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);
238 glslang::TDefaultGlslIoResolver resolver(*firstIntermediate);
239 glslang::TGlslIoMapper ioMapper;
242 success &= program.mapIO(&resolver, &ioMapper);
243 result.linkingOutput = program.getInfoLog();
244 result.linkingError = program.getInfoDebugLog();
247 success &= verifyIOMapping(result.linkingError, program);
248 result.validationResult = success;
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());
259 std::ostringstream disassembly_stream;
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();
269 std::ostringstream stream;
270 outputResultToStream(&stream, result, controls);
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);
278 checkEqAndUpdateIfRequested(expectedOutput, stream.str(), expectedOutputFname,
279 result.spirvWarningsErrors);
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"}},
294 } // anonymous namespace
295 } // namespace glslangtest