Lei Zhang | 414eb60 | 2016-03-04 16:22:34 -0500 | [diff] [blame] | 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 | |
| 35 | #include "TestFixture.h" |
| 36 | |
| 37 | namespace glslangtest { |
| 38 | |
Lei Zhang | d6f0ed2 | 2016-05-16 12:50:30 -0400 | [diff] [blame] | 39 | std::string FileNameAsCustomTestSuffix( |
Lei Zhang | 414eb60 | 2016-03-04 16:22:34 -0500 | [diff] [blame] | 40 | const ::testing::TestParamInfo<std::string>& info) |
| 41 | { |
| 42 | std::string name = info.param; |
| 43 | // A valid test case suffix cannot have '.' and '-' inside. |
| 44 | std::replace(name.begin(), name.end(), '.', '_'); |
| 45 | std::replace(name.begin(), name.end(), '-', '_'); |
| 46 | return name; |
| 47 | } |
| 48 | |
Lei Zhang | d6f0ed2 | 2016-05-16 12:50:30 -0400 | [diff] [blame] | 49 | EShLanguage GetShaderStage(const std::string& stage) |
Lei Zhang | 414eb60 | 2016-03-04 16:22:34 -0500 | [diff] [blame] | 50 | { |
| 51 | if (stage == "vert") { |
| 52 | return EShLangVertex; |
| 53 | } else if (stage == "tesc") { |
| 54 | return EShLangTessControl; |
| 55 | } else if (stage == "tese") { |
| 56 | return EShLangTessEvaluation; |
| 57 | } else if (stage == "geom") { |
| 58 | return EShLangGeometry; |
| 59 | } else if (stage == "frag") { |
| 60 | return EShLangFragment; |
| 61 | } else if (stage == "comp") { |
| 62 | return EShLangCompute; |
| 63 | } else { |
| 64 | assert(0 && "Unknown shader stage"); |
| 65 | return EShLangCount; |
| 66 | } |
| 67 | } |
| 68 | |
Lei Zhang | d6f0ed2 | 2016-05-16 12:50:30 -0400 | [diff] [blame] | 69 | EShMessages DeriveOptions(Source source, Semantics semantics, Target target) |
Lei Zhang | 414eb60 | 2016-03-04 16:22:34 -0500 | [diff] [blame] | 70 | { |
John Kessenich | a86836e | 2016-07-09 14:50:57 -0600 | [diff] [blame] | 71 | EShMessages result = EShMsgCascadingErrors; |
Lei Zhang | 414eb60 | 2016-03-04 16:22:34 -0500 | [diff] [blame] | 72 | |
Lei Zhang | d6f0ed2 | 2016-05-16 12:50:30 -0400 | [diff] [blame] | 73 | switch (source) { |
| 74 | case Source::GLSL: |
| 75 | break; |
| 76 | case Source::HLSL: |
John Kessenich | a2a5dd4 | 2016-10-01 18:07:57 -0600 | [diff] [blame] | 77 | result = static_cast<EShMessages>(result | EShMsgReadHlsl); |
Lei Zhang | d6f0ed2 | 2016-05-16 12:50:30 -0400 | [diff] [blame] | 78 | break; |
| 79 | } |
| 80 | |
Lei Zhang | 414eb60 | 2016-03-04 16:22:34 -0500 | [diff] [blame] | 81 | switch (target) { |
| 82 | case Target::AST: |
Lei Zhang | d6f0ed2 | 2016-05-16 12:50:30 -0400 | [diff] [blame] | 83 | result = static_cast<EShMessages>(result | EShMsgAST); |
Lei Zhang | 414eb60 | 2016-03-04 16:22:34 -0500 | [diff] [blame] | 84 | break; |
Lei Zhang | d6f0ed2 | 2016-05-16 12:50:30 -0400 | [diff] [blame] | 85 | case Target::Spv: |
| 86 | result = static_cast<EShMessages>(result | EShMsgSpvRules); |
John Kessenich | 906cc21 | 2016-12-09 19:22:20 -0700 | [diff] [blame] | 87 | result = static_cast<EShMessages>(result | EShMsgKeepUncalled); |
Lei Zhang | d6f0ed2 | 2016-05-16 12:50:30 -0400 | [diff] [blame] | 88 | break; |
| 89 | case Target::BothASTAndSpv: |
| 90 | result = static_cast<EShMessages>(result | EShMsgSpvRules | EShMsgAST); |
John Kessenich | 906cc21 | 2016-12-09 19:22:20 -0700 | [diff] [blame] | 91 | result = static_cast<EShMessages>(result | EShMsgKeepUncalled); |
Lei Zhang | 414eb60 | 2016-03-04 16:22:34 -0500 | [diff] [blame] | 92 | break; |
| 93 | }; |
| 94 | |
| 95 | switch (semantics) { |
| 96 | case Semantics::OpenGL: |
| 97 | break; |
| 98 | case Semantics::Vulkan: |
John Kessenich | d82c906 | 2016-05-23 16:07:07 -0600 | [diff] [blame] | 99 | result = static_cast<EShMessages>(result | EShMsgVulkanRules | EShMsgSpvRules); |
Lei Zhang | 414eb60 | 2016-03-04 16:22:34 -0500 | [diff] [blame] | 100 | break; |
| 101 | } |
| 102 | |
| 103 | return result; |
| 104 | } |
| 105 | |
| 106 | std::pair<bool, std::string> ReadFile(const std::string& path) |
| 107 | { |
| 108 | std::ifstream fstream(path, std::ios::in); |
| 109 | if (fstream) { |
| 110 | std::string contents; |
| 111 | fstream.seekg(0, std::ios::end); |
John Kessenich | 7f349c7 | 2016-07-08 22:09:10 -0600 | [diff] [blame] | 112 | contents.reserve((std::string::size_type)fstream.tellg()); |
Lei Zhang | 414eb60 | 2016-03-04 16:22:34 -0500 | [diff] [blame] | 113 | fstream.seekg(0, std::ios::beg); |
| 114 | contents.assign((std::istreambuf_iterator<char>(fstream)), |
| 115 | std::istreambuf_iterator<char>()); |
| 116 | return std::make_pair(true, contents); |
| 117 | } |
| 118 | return std::make_pair(false, ""); |
| 119 | } |
| 120 | |
steve-lunarg | a845641 | 2016-08-17 16:18:06 -0600 | [diff] [blame] | 121 | std::pair<bool, std::vector<std::uint32_t> > ReadSpvBinaryFile(const std::string& path) |
| 122 | { |
| 123 | std::ifstream fstream(path, std::fstream::in | std::fstream::binary); |
| 124 | |
| 125 | if (!fstream) |
| 126 | return std::make_pair(false, std::vector<std::uint32_t>()); |
| 127 | |
| 128 | std::vector<std::uint32_t> contents; |
| 129 | |
| 130 | // Reserve space (for efficiency, not for correctness) |
| 131 | fstream.seekg(0, fstream.end); |
| 132 | contents.reserve(size_t(fstream.tellg()) / sizeof(std::uint32_t)); |
| 133 | fstream.seekg(0, fstream.beg); |
| 134 | |
| 135 | // There is no istream iterator traversing by uint32_t, so we must loop. |
| 136 | while (!fstream.eof()) { |
| 137 | std::uint32_t inWord; |
| 138 | fstream.read((char *)&inWord, sizeof(inWord)); |
| 139 | |
| 140 | if (!fstream.eof()) |
| 141 | contents.push_back(inWord); |
| 142 | } |
| 143 | |
| 144 | return std::make_pair(true, contents); // hopefully, c++11 move semantics optimizes the copy away. |
| 145 | } |
| 146 | |
Lei Zhang | 414eb60 | 2016-03-04 16:22:34 -0500 | [diff] [blame] | 147 | bool WriteFile(const std::string& path, const std::string& contents) |
| 148 | { |
| 149 | std::ofstream fstream(path, std::ios::out); |
| 150 | if (!fstream) return false; |
| 151 | fstream << contents; |
| 152 | fstream.flush(); |
| 153 | return true; |
| 154 | } |
| 155 | |
| 156 | std::string GetSuffix(const std::string& name) |
| 157 | { |
| 158 | const size_t pos = name.rfind('.'); |
| 159 | return (pos == std::string::npos) ? "" : name.substr(name.rfind('.') + 1); |
| 160 | } |
| 161 | |
| 162 | } // namespace glslangtest |