blob: 1dff65aca07373d23fe321d8e1d2fa4162bdacb3 [file] [log] [blame]
Lei Zhang414eb602016-03-04 16:22:34 -05001//
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
37namespace glslangtest {
38
Lei Zhangd6f0ed22016-05-16 12:50:30 -040039std::string FileNameAsCustomTestSuffix(
Lei Zhang414eb602016-03-04 16:22:34 -050040 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 Zhangd6f0ed22016-05-16 12:50:30 -040049EShLanguage GetShaderStage(const std::string& stage)
Lei Zhang414eb602016-03-04 16:22:34 -050050{
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 Zhangd6f0ed22016-05-16 12:50:30 -040069EShMessages DeriveOptions(Source source, Semantics semantics, Target target)
Lei Zhang414eb602016-03-04 16:22:34 -050070{
John Kessenicha86836e2016-07-09 14:50:57 -060071 EShMessages result = EShMsgCascadingErrors;
Lei Zhang414eb602016-03-04 16:22:34 -050072
Lei Zhangd6f0ed22016-05-16 12:50:30 -040073 switch (source) {
74 case Source::GLSL:
75 break;
76 case Source::HLSL:
77 result = EShMsgReadHlsl;
78 break;
79 }
80
Lei Zhang414eb602016-03-04 16:22:34 -050081 switch (target) {
82 case Target::AST:
Lei Zhangd6f0ed22016-05-16 12:50:30 -040083 result = static_cast<EShMessages>(result | EShMsgAST);
Lei Zhang414eb602016-03-04 16:22:34 -050084 break;
Lei Zhangd6f0ed22016-05-16 12:50:30 -040085 case Target::Spv:
86 result = static_cast<EShMessages>(result | EShMsgSpvRules);
87 break;
88 case Target::BothASTAndSpv:
89 result = static_cast<EShMessages>(result | EShMsgSpvRules | EShMsgAST);
Lei Zhang414eb602016-03-04 16:22:34 -050090 break;
91 };
92
93 switch (semantics) {
94 case Semantics::OpenGL:
95 break;
96 case Semantics::Vulkan:
John Kessenichd82c9062016-05-23 16:07:07 -060097 result = static_cast<EShMessages>(result | EShMsgVulkanRules | EShMsgSpvRules);
Lei Zhang414eb602016-03-04 16:22:34 -050098 break;
99 }
100
101 return result;
102}
103
104std::pair<bool, std::string> ReadFile(const std::string& path)
105{
106 std::ifstream fstream(path, std::ios::in);
107 if (fstream) {
108 std::string contents;
109 fstream.seekg(0, std::ios::end);
John Kessenich7f349c72016-07-08 22:09:10 -0600110 contents.reserve((std::string::size_type)fstream.tellg());
Lei Zhang414eb602016-03-04 16:22:34 -0500111 fstream.seekg(0, std::ios::beg);
112 contents.assign((std::istreambuf_iterator<char>(fstream)),
113 std::istreambuf_iterator<char>());
114 return std::make_pair(true, contents);
115 }
116 return std::make_pair(false, "");
117}
118
steve-lunarga8456412016-08-17 16:18:06 -0600119std::pair<bool, std::vector<std::uint32_t> > ReadSpvBinaryFile(const std::string& path)
120{
121 std::ifstream fstream(path, std::fstream::in | std::fstream::binary);
122
123 if (!fstream)
124 return std::make_pair(false, std::vector<std::uint32_t>());
125
126 std::vector<std::uint32_t> contents;
127
128 // Reserve space (for efficiency, not for correctness)
129 fstream.seekg(0, fstream.end);
130 contents.reserve(size_t(fstream.tellg()) / sizeof(std::uint32_t));
131 fstream.seekg(0, fstream.beg);
132
133 // There is no istream iterator traversing by uint32_t, so we must loop.
134 while (!fstream.eof()) {
135 std::uint32_t inWord;
136 fstream.read((char *)&inWord, sizeof(inWord));
137
138 if (!fstream.eof())
139 contents.push_back(inWord);
140 }
141
142 return std::make_pair(true, contents); // hopefully, c++11 move semantics optimizes the copy away.
143}
144
Lei Zhang414eb602016-03-04 16:22:34 -0500145bool WriteFile(const std::string& path, const std::string& contents)
146{
147 std::ofstream fstream(path, std::ios::out);
148 if (!fstream) return false;
149 fstream << contents;
150 fstream.flush();
151 return true;
152}
153
154std::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
160} // namespace glslangtest