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