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/localintermediate.h"
43 #include "glslang/MachineIndependent/iomapper.h"
44 #include "glslang/MachineIndependent/reflection.h"
45
46 namespace glslangtest {
47 namespace {
48
49 struct IoMapData {
50 std::vector<std::string> fileNames;
51 Semantics semantics;
52 };
53
54 using GlslMapIOTest = GlslangTest <::testing::TestWithParam<IoMapData>>;
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 if (!in.getType()->isStruct()) {
113 bool found = false;
114 for (auto outIt : pipeOut) {
115 if (outIt.second->getType()->isStruct()) {
116 unsigned int baseLoc = outIt.second->getType()->getQualifier().hasLocation() ?
117 outIt.second->getType()->getQualifier().layoutLocation :
118 std::numeric_limits<unsigned int>::max();
119 for (size_t j = 0; j < outIt.second->getType()->getStruct()->size(); j++) {
120 baseLoc = (*outIt.second->getType()->getStruct())[j].type->getQualifier().hasLocation() ?
121 (*outIt.second->getType()->getStruct())[j].type->getQualifier().layoutLocation : baseLoc;
122 if (baseLoc != std::numeric_limits<unsigned int>::max()) {
123 if (baseLoc == in.getType()->getQualifier().layoutLocation) {
124 found = true;
125 break;
126 }
127 baseLoc += glslang::TIntermediate::computeTypeLocationSize(*(*outIt.second->getType()->getStruct())[j].type, EShLangVertex);
128 }
129 }
130 if (found) {
131 break;
132 }
133 }
134 }
135 success &= found;
136 }
137 else {
138 unsigned int baseLoc = in.getType()->getQualifier().hasLocation() ? in.getType()->getQualifier().layoutLocation : -1;
139 for (size_t j = 0; j < in.getType()->getStruct()->size(); j++) {
140 baseLoc = (*in.getType()->getStruct())[j].type->getQualifier().hasLocation() ?
141 (*in.getType()->getStruct())[j].type->getQualifier().layoutLocation : baseLoc;
142 if (baseLoc != std::numeric_limits<unsigned int>::max()) {
143 bool isMemberFound = false;
144 for (auto outIt : pipeOut) {
145 if (baseLoc == outIt.second->getType()->getQualifier().layoutLocation) {
146 isMemberFound = true;
147 break;
148 }
149 }
150 if (!isMemberFound) {
151 success &= false;
152 break;
153 }
154 baseLoc += glslang::TIntermediate::computeTypeLocationSize(*(*in.getType()->getStruct())[j].type, EShLangVertex);
155 }
156 }
157 }
158 }
159 }
160 }
161
162 // compare uniforms in each stage to the program
163 {
164 int totalUniforms = program.getNumUniformVariables();
165 std::map<std::string, const glslang::TObjectReflection*> programUniforms;
166 for (int i = 0; i < totalUniforms; i++) {
167 const glslang::TObjectReflection& uniform = program.getUniform(i);
168 std::string name = interfaceName(uniform);
169 programUniforms[name] = &uniform;
170 }
171 it = stageReflections.begin();
172 for (; it != stageReflections.end(); it++) {
173 int numUniform = it->getNumUniforms();
174 std::map<std::string, glslang::TObjectReflection> uniforms;
175
176 for (int i = 0; i < numUniform; i++) {
177 glslang::TObjectReflection uniform = it->getUniform(i);
178 std::string name = interfaceName(uniform);
179 auto programUniform = programUniforms.find(name);
180
181 if (programUniform != programUniforms.end()) {
182 auto stageQualifier = uniform.getType()->getQualifier();
183 auto programQualifier = programUniform->second->getType()->getQualifier();
184
185 success &= stageQualifier.layoutLocation == programQualifier.layoutLocation;
186 success &= stageQualifier.layoutBinding == programQualifier.layoutBinding;
187 success &= stageQualifier.layoutSet == programQualifier.layoutSet;
188 }
189 else {
190 success &= false;
191 }
192 }
193 }
194 }
195
196 // compare uniform blocks in each stage to the program table
197 {
198 int totalUniforms = program.getNumUniformBlocks();
199 std::map<std::string, const glslang::TObjectReflection*> programUniforms;
200 for (int i = 0; i < totalUniforms; i++) {
201 const glslang::TObjectReflection& uniform = program.getUniformBlock(i);
202 std::string name = interfaceName(uniform);
203 programUniforms[name] = &uniform;
204 }
205 it = stageReflections.begin();
206 for (; it != stageReflections.end(); it++) {
207 int numUniform = it->getNumUniformBlocks();
208 std::map<std::string, glslang::TObjectReflection> uniforms;
209
210 for (int i = 0; i < numUniform; i++) {
211 glslang::TObjectReflection uniform = it->getUniformBlock(i);
212 std::string name = interfaceName(uniform);
213 auto programUniform = programUniforms.find(name);
214
215 if (programUniform != programUniforms.end()) {
216 auto stageQualifier = uniform.getType()->getQualifier();
217 auto programQualifier = programUniform->second->getType()->getQualifier();
218
219 success &= stageQualifier.layoutLocation == programQualifier.layoutLocation;
220 success &= stageQualifier.layoutBinding == programQualifier.layoutBinding;
221 success &= stageQualifier.layoutSet == programQualifier.layoutSet;
222 }
223 else {
224 success &= false;
225 }
226 }
227 }
228 }
229
230 if (!success) {
231 linkingError += "Mismatched cross-stage IO\n";
232 }
233
234 return success;
235 }
236
TEST_P(GlslMapIOTest,FromFile)237 TEST_P(GlslMapIOTest, FromFile)
238 {
239 const auto& fileNames = GetParam().fileNames;
240 Semantics semantics = GetParam().semantics;
241 const size_t fileCount = fileNames.size();
242 const EShMessages controls = DeriveOptions(Source::GLSL, semantics, Target::BothASTAndSpv);
243 GlslangResult result;
244
245 // Compile each input shader file.
246 bool success = true;
247 std::vector<std::unique_ptr<glslang::TShader>> shaders;
248 for (size_t i = 0; i < fileCount; ++i) {
249 std::string contents;
250 tryLoadFile(GlobalTestSettings.testRoot + "/" + fileNames[i],
251 "input", &contents);
252 shaders.emplace_back(
253 new glslang::TShader(GetShaderStage(GetSuffix(fileNames[i]))));
254 auto* shader = shaders.back().get();
255
256 shader->setAutoMapLocations(true);
257 shader->setAutoMapBindings(true);
258
259 if (controls & EShMsgSpvRules) {
260 if (controls & EShMsgVulkanRules) {
261 shader->setEnvInput((controls & EShMsgReadHlsl) ? glslang::EShSourceHlsl
262 : glslang::EShSourceGlsl,
263 shader->getStage(), glslang::EShClientVulkan, 100);
264 shader->setEnvClient(glslang::EShClientVulkan, glslang::EShTargetVulkan_1_1);
265 shader->setEnvTarget(glslang::EShTargetSpv, glslang::EShTargetSpv_1_0);
266 } else {
267 shader->setEnvInput((controls & EShMsgReadHlsl) ? glslang::EShSourceHlsl
268 : glslang::EShSourceGlsl,
269 shader->getStage(), glslang::EShClientOpenGL, 100);
270 shader->setEnvClient(glslang::EShClientOpenGL, glslang::EShTargetOpenGL_450);
271 shader->setEnvTarget(glslang::EshTargetSpv, glslang::EShTargetSpv_1_0);
272 }
273 }
274
275 success &= compile(shader, contents, "", controls);
276
277 result.shaderResults.push_back(
278 { fileNames[i], shader->getInfoLog(), shader->getInfoDebugLog() });
279 }
280
281 // Link all of them.
282 glslang::TProgram program;
283 for (const auto& shader : shaders) program.addShader(shader.get());
284 success &= program.link(controls);
285 result.linkingOutput = program.getInfoLog();
286 result.linkingError = program.getInfoDebugLog();
287
288 glslang::TIoMapResolver *resolver;
289 for (unsigned stage = 0; stage < EShLangCount; stage++) {
290 resolver = program.getGlslIoResolver((EShLanguage)stage);
291 if (resolver)
292 break;
293 }
294 glslang::TIoMapper *ioMapper = glslang::GetGlslIoMapper();
295
296 if (success) {
297 success &= program.mapIO(resolver, ioMapper);
298 result.linkingOutput = program.getInfoLog();
299 result.linkingError = program.getInfoDebugLog();
300 }
301 delete ioMapper;
302 delete resolver;
303
304 success &= verifyIOMapping(result.linkingError, program);
305 result.validationResult = success;
306
307 if (success && (controls & EShMsgSpvRules)) {
308 for (int stage = 0; stage < EShLangCount; ++stage) {
309 if (program.getIntermediate((EShLanguage)stage)) {
310 spv::SpvBuildLogger logger;
311 std::vector<uint32_t> spirv_binary;
312 options().disableOptimizer = false;
313 glslang::GlslangToSpv(*program.getIntermediate((EShLanguage)stage),
314 spirv_binary, &logger, &options());
315
316 std::ostringstream disassembly_stream;
317 spv::Disassemble(disassembly_stream, spirv_binary);
318 result.spirvWarningsErrors += logger.getAllMessages();
319 result.spirv += disassembly_stream.str();
320 result.validationResult &= !options().validate || logger.getAllMessages().empty();
321 }
322 }
323 }
324
325 std::ostringstream stream;
326 outputResultToStream(&stream, result, controls);
327
328 // Check with expected results.
329 const std::string expectedOutputFname =
330 GlobalTestSettings.testRoot + "/baseResults/" + fileNames.front() + ".out";
331 std::string expectedOutput;
332 tryLoadFile(expectedOutputFname, "expected output", &expectedOutput);
333
334 checkEqAndUpdateIfRequested(expectedOutput, stream.str(), expectedOutputFname,
335 result.spirvWarningsErrors);
336 }
337
338 // clang-format off
339 INSTANTIATE_TEST_SUITE_P(
340 Glsl, GlslMapIOTest,
341 ::testing::ValuesIn(std::vector<IoMapData>({
342 {{"iomap.crossStage.vert", "iomap.crossStage.frag" }, Semantics::OpenGL},
343 {{"iomap.crossStage.2.vert", "iomap.crossStage.2.geom", "iomap.crossStage.2.frag" }, Semantics::OpenGL},
344 {{"iomap.blockOutVariableIn.vert", "iomap.blockOutVariableIn.frag"}, Semantics::OpenGL},
345 {{"iomap.variableOutBlockIn.vert", "iomap.variableOutBlockIn.frag"}, Semantics::OpenGL},
346 {{"iomap.blockOutVariableIn.2.vert", "iomap.blockOutVariableIn.geom"}, Semantics::OpenGL},
347 {{"iomap.variableOutBlockIn.2.vert", "iomap.variableOutBlockIn.geom"}, Semantics::OpenGL},
348 {{"iomap.mismatchedBufferTypes.vert", "iomap.mismatchedBufferTypes.frag"}, Semantics::OpenGL},
349 // vulkan semantics
350 {{"iomap.crossStage.vk.vert", "iomap.crossStage.vk.geom", "iomap.crossStage.vk.frag" }, Semantics::Vulkan},
351 {{"iomap.crossStage.vk.2.vert", "iomap.crossStage.vk.2.geom", "iomap.crossStage.vk.2.frag" }, Semantics::Vulkan},
352 }))
353 );
354 // clang-format on
355
356 } // anonymous namespace
357 } // namespace glslangtest
358