TestFixture.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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 if (stage == "rgen") {
  60. return EShLangRayGen;
  61. } else if (stage == "rint") {
  62. return EShLangIntersect;
  63. } else if (stage == "rahit") {
  64. return EShLangAnyHit;
  65. } else if (stage == "rchit") {
  66. return EShLangClosestHit;
  67. } else if (stage == "rmiss") {
  68. return EShLangMiss;
  69. } else if (stage == "rcall") {
  70. return EShLangCallable;
  71. } else if (stage == "task") {
  72. return EShLangTaskNV;
  73. } else if (stage == "mesh") {
  74. return EShLangMeshNV;
  75. } else {
  76. assert(0 && "Unknown shader stage");
  77. return EShLangCount;
  78. }
  79. }
  80. EShMessages DeriveOptions(Source source, Semantics semantics, Target target)
  81. {
  82. EShMessages result = EShMsgCascadingErrors;
  83. switch (source) {
  84. case Source::GLSL:
  85. break;
  86. case Source::HLSL:
  87. result = static_cast<EShMessages>(result | EShMsgReadHlsl);
  88. break;
  89. }
  90. switch (target) {
  91. case Target::AST:
  92. result = static_cast<EShMessages>(result | EShMsgAST);
  93. break;
  94. case Target::Spv:
  95. result = static_cast<EShMessages>(result | EShMsgSpvRules);
  96. result = static_cast<EShMessages>(result | EShMsgKeepUncalled);
  97. break;
  98. case Target::BothASTAndSpv:
  99. result = static_cast<EShMessages>(result | EShMsgSpvRules | EShMsgAST);
  100. result = static_cast<EShMessages>(result | EShMsgKeepUncalled);
  101. break;
  102. };
  103. switch (semantics) {
  104. case Semantics::OpenGL:
  105. break;
  106. case Semantics::Vulkan:
  107. result = static_cast<EShMessages>(result | EShMsgVulkanRules | EShMsgSpvRules);
  108. break;
  109. }
  110. result = static_cast<EShMessages>(result | EShMsgHlslLegalization);
  111. return result;
  112. }
  113. std::pair<bool, std::string> ReadFile(const std::string& path)
  114. {
  115. std::ifstream fstream(path, std::ios::in);
  116. if (fstream) {
  117. std::string contents;
  118. fstream.seekg(0, std::ios::end);
  119. contents.reserve((std::string::size_type)fstream.tellg());
  120. fstream.seekg(0, std::ios::beg);
  121. contents.assign((std::istreambuf_iterator<char>(fstream)),
  122. std::istreambuf_iterator<char>());
  123. return std::make_pair(true, contents);
  124. }
  125. return std::make_pair(false, "");
  126. }
  127. std::pair<bool, std::vector<std::uint32_t> > ReadSpvBinaryFile(const std::string& path)
  128. {
  129. std::ifstream fstream(path, std::fstream::in | std::fstream::binary);
  130. if (!fstream)
  131. return std::make_pair(false, std::vector<std::uint32_t>());
  132. std::vector<std::uint32_t> contents;
  133. // Reserve space (for efficiency, not for correctness)
  134. fstream.seekg(0, fstream.end);
  135. contents.reserve(size_t(fstream.tellg()) / sizeof(std::uint32_t));
  136. fstream.seekg(0, fstream.beg);
  137. // There is no istream iterator traversing by uint32_t, so we must loop.
  138. while (!fstream.eof()) {
  139. std::uint32_t inWord;
  140. fstream.read((char *)&inWord, sizeof(inWord));
  141. if (!fstream.eof())
  142. contents.push_back(inWord);
  143. }
  144. return std::make_pair(true, contents); // hopefully, c++11 move semantics optimizes the copy away.
  145. }
  146. bool WriteFile(const std::string& path, const std::string& contents)
  147. {
  148. std::ofstream fstream(path, std::ios::out);
  149. if (!fstream) return false;
  150. fstream << contents;
  151. fstream.flush();
  152. return true;
  153. }
  154. std::string GetSuffix(const std::string& name)
  155. {
  156. const size_t pos = name.rfind('.');
  157. return (pos == std::string::npos) ? "" : name.substr(name.rfind('.') + 1);
  158. }
  159. } // namespace glslangtest