LiveTraverser.FromFile.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // Copyright (c) 2025 The Khronos Group Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include <gtest/gtest.h>
  15. #include "TestFixture.h"
  16. #include "glslang/MachineIndependent/LiveTraverser.h"
  17. namespace glslangtest {
  18. namespace {
  19. struct LiveTraverserTestParams {
  20. std::string fileName;
  21. std::vector<std::string> liveVars;
  22. };
  23. using LiveTraverserTest = GlslangTest<::testing::TestWithParam<LiveTraverserTestParams>>;
  24. TEST_P(LiveTraverserTest, FromFile)
  25. {
  26. const auto& fileName = GetParam().fileName;
  27. const auto& expectedLiveVars = GetParam().liveVars;
  28. const EShMessages controls = DeriveOptions(Source::GLSL, Semantics::Vulkan, Target::AST);
  29. GlslangResult result;
  30. result.validationResult = true;
  31. std::string contents;
  32. tryLoadFile(GlobalTestSettings.testRoot + "/" + fileName, "input", &contents);
  33. std::unique_ptr<glslang::TShader> shader = std::make_unique<glslang::TShader>(GetShaderStage(GetSuffix(fileName)));
  34. bool success = compile(shader.get(), contents, "", controls);
  35. result.shaderResults.push_back({fileName, shader->getInfoLog(), shader->getInfoDebugLog()});
  36. std::ostringstream stream;
  37. outputResultToStream(&stream, result, controls);
  38. class TLiveSymbolTraverser : public glslang::TLiveTraverser {
  39. public:
  40. TLiveSymbolTraverser(const glslang::TIntermediate& i, std::vector<std::string>& liveVars)
  41. : glslang::TLiveTraverser(i), liveVars(liveVars)
  42. {
  43. }
  44. virtual void visitSymbol(glslang::TIntermSymbol* symbol)
  45. {
  46. if (symbol->getAsSymbolNode()->getAccessName().compare(0, 3, "gl_") == 0)
  47. return;
  48. if (symbol->getQualifier().storage == glslang::TStorageQualifier::EvqVaryingIn ||
  49. symbol->getQualifier().storage == glslang::TStorageQualifier::EvqVaryingOut ||
  50. symbol->getQualifier().storage == glslang::TStorageQualifier::EvqUniform ||
  51. symbol->getQualifier().storage == glslang::TStorageQualifier::EvqBuffer) {
  52. liveVars.push_back(symbol->getAccessName().c_str());
  53. }
  54. }
  55. private:
  56. std::vector<std::string>& liveVars;
  57. };
  58. if (success) {
  59. std::vector<std::string> actualLiveVars;
  60. TLiveSymbolTraverser liveTraverser(*shader->getIntermediate(), actualLiveVars);
  61. liveTraverser.pushFunction(shader->getIntermediate()->getEntryPointMangledName().c_str());
  62. while (!liveTraverser.destinations.empty()) {
  63. TIntermNode* destination = liveTraverser.destinations.back();
  64. liveTraverser.destinations.pop_back();
  65. destination->traverse(&liveTraverser);
  66. }
  67. for (const auto& expectedVar : expectedLiveVars) {
  68. auto it = std::find(actualLiveVars.begin(), actualLiveVars.end(), expectedVar);
  69. EXPECT_NE(it, actualLiveVars.end());
  70. if (it != actualLiveVars.end())
  71. actualLiveVars.erase(it);
  72. }
  73. EXPECT_TRUE(actualLiveVars.empty());
  74. }
  75. // Check with expected results.
  76. const std::string expectedOutputFname = GlobalTestSettings.testRoot + "/baseResults/" + fileName + ".out";
  77. std::string expectedOutput;
  78. tryLoadFile(expectedOutputFname, "expected output", &expectedOutput);
  79. checkEqAndUpdateIfRequested(expectedOutput, stream.str(), expectedOutputFname, result.spirvWarningsErrors);
  80. }
  81. // clang-format off
  82. INSTANTIATE_TEST_SUITE_P(
  83. Glsl, LiveTraverserTest,
  84. ::testing::ValuesIn(std::vector<LiveTraverserTestParams>({
  85. {"liveTraverser.switch.vert", {"a0", "a1", "a2", "a3", "a4", "a5", "a6", "a7", "a8", "a9", "a10"}},
  86. // TODO: implement test for if statements
  87. }))
  88. );
  89. // clang-format on
  90. } // anonymous namespace
  91. } // namespace glslangtest