VkRelaxed.FromFile.cpp 12 KB

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