TestFixture.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. //
  2. // Copyright (C) 2016 Google, Inc.
  3. //
  4. // All rights reserved.
  5. //
  6. // Redistribution and use in source and binary forms, with or without
  7. // modification, are permitted provided that the following conditions
  8. // are met:
  9. //
  10. // Redistributions of source code must retain the above copyright
  11. // notice, this list of conditions and the following disclaimer.
  12. //
  13. // Redistributions in binary form must reproduce the above
  14. // copyright notice, this list of conditions and the following
  15. // disclaimer in the documentation and/or other materials provided
  16. // with the distribution.
  17. //
  18. // Neither the name of Google Inc. nor the names of its
  19. // contributors may be used to endorse or promote products derived
  20. // from this software without specific prior written permission.
  21. //
  22. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  23. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  24. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  25. // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  26. // COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  27. // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  28. // BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  29. // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  30. // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  31. // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  32. // ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  33. // POSSIBILITY OF SUCH DAMAGE.
  34. #include "TestFixture.h"
  35. namespace glslangtest {
  36. std::string FileNameAsCustomTestSuffix(
  37. const ::testing::TestParamInfo<std::string>& info)
  38. {
  39. std::string name = info.param;
  40. // A valid test case suffix cannot have '.' and '-' inside.
  41. std::replace(name.begin(), name.end(), '.', '_');
  42. std::replace(name.begin(), name.end(), '-', '_');
  43. return name;
  44. }
  45. EShLanguage GetShaderStage(const std::string& stage)
  46. {
  47. if (stage == "vert") {
  48. return EShLangVertex;
  49. } else if (stage == "tesc") {
  50. return EShLangTessControl;
  51. } else if (stage == "tese") {
  52. return EShLangTessEvaluation;
  53. } else if (stage == "geom") {
  54. return EShLangGeometry;
  55. } else if (stage == "frag") {
  56. return EShLangFragment;
  57. } else if (stage == "comp") {
  58. return EShLangCompute;
  59. } else {
  60. assert(0 && "Unknown shader stage");
  61. return EShLangCount;
  62. }
  63. }
  64. EShMessages DeriveOptions(Source source, Semantics semantics, Target target)
  65. {
  66. EShMessages result = EShMsgCascadingErrors;
  67. switch (source) {
  68. case Source::GLSL:
  69. break;
  70. case Source::HLSL:
  71. result = static_cast<EShMessages>(result | EShMsgReadHlsl);
  72. break;
  73. }
  74. switch (target) {
  75. case Target::AST:
  76. result = static_cast<EShMessages>(result | EShMsgAST);
  77. break;
  78. case Target::Spv:
  79. result = static_cast<EShMessages>(result | EShMsgSpvRules);
  80. result = static_cast<EShMessages>(result | EShMsgKeepUncalled);
  81. break;
  82. case Target::BothASTAndSpv:
  83. result = static_cast<EShMessages>(result | EShMsgSpvRules | EShMsgAST);
  84. result = static_cast<EShMessages>(result | EShMsgKeepUncalled);
  85. break;
  86. };
  87. switch (semantics) {
  88. case Semantics::OpenGL:
  89. break;
  90. case Semantics::Vulkan:
  91. result = static_cast<EShMessages>(result | EShMsgVulkanRules | EShMsgSpvRules);
  92. break;
  93. }
  94. result = static_cast<EShMessages>(result | EShMsgHlslLegalization);
  95. return result;
  96. }
  97. std::pair<bool, std::string> ReadFile(const std::string& path)
  98. {
  99. std::ifstream fstream(path, std::ios::in);
  100. if (fstream) {
  101. std::string contents;
  102. fstream.seekg(0, std::ios::end);
  103. contents.reserve((std::string::size_type)fstream.tellg());
  104. fstream.seekg(0, std::ios::beg);
  105. contents.assign((std::istreambuf_iterator<char>(fstream)),
  106. std::istreambuf_iterator<char>());
  107. return std::make_pair(true, contents);
  108. }
  109. return std::make_pair(false, "");
  110. }
  111. std::pair<bool, std::vector<std::uint32_t> > ReadSpvBinaryFile(const std::string& path)
  112. {
  113. std::ifstream fstream(path, std::fstream::in | std::fstream::binary);
  114. if (!fstream)
  115. return std::make_pair(false, std::vector<std::uint32_t>());
  116. std::vector<std::uint32_t> contents;
  117. // Reserve space (for efficiency, not for correctness)
  118. fstream.seekg(0, fstream.end);
  119. contents.reserve(size_t(fstream.tellg()) / sizeof(std::uint32_t));
  120. fstream.seekg(0, fstream.beg);
  121. // There is no istream iterator traversing by uint32_t, so we must loop.
  122. while (!fstream.eof()) {
  123. std::uint32_t inWord;
  124. fstream.read((char *)&inWord, sizeof(inWord));
  125. if (!fstream.eof())
  126. contents.push_back(inWord);
  127. }
  128. return std::make_pair(true, contents); // hopefully, c++11 move semantics optimizes the copy away.
  129. }
  130. bool WriteFile(const std::string& path, const std::string& contents)
  131. {
  132. std::ofstream fstream(path, std::ios::out);
  133. if (!fstream) return false;
  134. fstream << contents;
  135. fstream.flush();
  136. return true;
  137. }
  138. std::string GetSuffix(const std::string& name)
  139. {
  140. const size_t pos = name.rfind('.');
  141. return (pos == std::string::npos) ? "" : name.substr(name.rfind('.') + 1);
  142. }
  143. } // namespace glslangtest