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;
51 std::vector<std::vector<std::string>> resourceSetBindings;
54 using VulkanRelaxedTest = GlslangTest <::testing::TestWithParam<vkRelaxedData>>;
57 std::string interfaceName(T symbol) {
58 return symbol.getType()->getBasicType() == glslang::EbtBlock ? std::string(symbol.getType()->getTypeName().c_str()) : symbol.name;
61 bool verifyIOMapping(std::string& linkingError, glslang::TProgram& program) {
64 // Verify IO Mapping by generating reflection for each stage individually
65 // and comparing layout qualifiers on the results
68 int reflectionOptions = EShReflectionDefault;
69 //reflectionOptions |= EShReflectionStrictArraySuffix;
70 //reflectionOptions |= EShReflectionBasicArraySuffix;
71 reflectionOptions |= EShReflectionIntermediateIO;
72 reflectionOptions |= EShReflectionSeparateBuffers;
73 reflectionOptions |= EShReflectionAllBlockVariables;
74 //reflectionOptions |= EShReflectionUnwrapIOBlocks;
76 success &= program.buildReflection(reflectionOptions);
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));
87 // check that input/output locations match between stages
88 auto it = stageReflections.begin();
90 for (; nextIt != stageReflections.end(); it++, nextIt++) {
91 int numOut = it->getNumPipeOutputs();
92 std::map<std::string, const glslang::TObjectReflection*> pipeOut;
94 for (int i = 0; i < numOut; i++) {
95 const glslang::TObjectReflection& out = it->getPipeOutput(i);
96 std::string name = interfaceName(out);
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);
106 if (out != pipeOut.end()) {
107 auto inQualifier = in.getType()->getQualifier();
108 auto outQualifier = out->second->getType()->getQualifier();
109 success &= outQualifier.layoutLocation == inQualifier.layoutLocation;
117 // compare uniforms in each stage to the program
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;
126 it = stageReflections.begin();
127 for (; it != stageReflections.end(); it++) {
128 int numUniform = it->getNumUniforms();
129 std::map<std::string, glslang::TObjectReflection> uniforms;
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);
136 if (programUniform != programUniforms.end()) {
137 auto stageQualifier = uniform.getType()->getQualifier();
138 auto programQualifier = programUniform->second->getType()->getQualifier();
140 success &= stageQualifier.layoutLocation == programQualifier.layoutLocation;
141 success &= stageQualifier.layoutBinding == programQualifier.layoutBinding;
142 success &= stageQualifier.layoutSet == programQualifier.layoutSet;
151 // compare uniform blocks in each stage to the program table
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;
160 it = stageReflections.begin();
161 for (; it != stageReflections.end(); it++) {
162 int numUniform = it->getNumUniformBlocks();
163 std::map<std::string, glslang::TObjectReflection> uniforms;
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);
170 if (programUniform != programUniforms.end()) {
171 auto stageQualifier = uniform.getType()->getQualifier();
172 auto programQualifier = programUniform->second->getType()->getQualifier();
174 success &= stageQualifier.layoutLocation == programQualifier.layoutLocation;
175 success &= stageQualifier.layoutBinding == programQualifier.layoutBinding;
176 success &= stageQualifier.layoutSet == programQualifier.layoutSet;
186 linkingError += "Mismatched cross-stage IO\n";
192 TEST_P(VulkanRelaxedTest, FromFile)
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;
201 // Compile each input shader file.
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],
208 shaders.emplace_back(
209 new glslang::TShader(GetShaderStage(GetSuffix(fileNames[i]))));
210 auto* shader = shaders.back().get();
212 shader->setAutoMapLocations(true);
213 shader->setAutoMapBindings(true);
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);
219 // Use vulkan relaxed option
220 shader->setEnvInputVulkanRulesRelaxed();
222 success &= compile(shader, contents, "", controls);
224 result.shaderResults.push_back(
225 { fileNames[i], shader->getInfoLog(), shader->getInfoDebugLog() });
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();
235 if (!resourceSetBindings.empty()) {
236 assert(resourceSetBindings.size() == fileNames.size());
237 for (int i = 0; i < shaders.size(); i++)
238 shaders[i]->setResourceSetBinding(resourceSetBindings[i]);
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);
246 glslang::TDefaultGlslIoResolver resolver(*firstIntermediate);
247 glslang::TGlslIoMapper ioMapper;
250 success &= program.mapIO(&resolver, &ioMapper);
251 result.linkingOutput = program.getInfoLog();
252 result.linkingError = program.getInfoDebugLog();
255 success &= verifyIOMapping(result.linkingError, program);
256 result.validationResult = success;
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());
267 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();
277 std::ostringstream stream;
278 outputResultToStream(&stream, result, controls);
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);
286 checkEqAndUpdateIfRequested(expectedOutput, stream.str(), expectedOutputFname,
287 result.spirvWarningsErrors);
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"} } },
303 } // anonymous namespace
304 } // namespace glslangtest