blob: 4c935f88501ae2e3fff6d50deb841d623af9269d [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;
Chao Chenb50c02e2018-09-19 11:42:24 -070063 } else if (stage == "rgen") {
Daniel Kochdb32b242020-03-17 20:42:47 -040064 return EShLangRayGen;
Chao Chenb50c02e2018-09-19 11:42:24 -070065 } else if (stage == "rint") {
Daniel Kochdb32b242020-03-17 20:42:47 -040066 return EShLangIntersect;
Chao Chenb50c02e2018-09-19 11:42:24 -070067 } else if (stage == "rahit") {
Daniel Kochdb32b242020-03-17 20:42:47 -040068 return EShLangAnyHit;
Chao Chenb50c02e2018-09-19 11:42:24 -070069 } else if (stage == "rchit") {
Daniel Kochdb32b242020-03-17 20:42:47 -040070 return EShLangClosestHit;
Chao Chenb50c02e2018-09-19 11:42:24 -070071 } else if (stage == "rmiss") {
Daniel Kochdb32b242020-03-17 20:42:47 -040072 return EShLangMiss;
Chao Chenb50c02e2018-09-19 11:42:24 -070073 } else if (stage == "rcall") {
Daniel Kochdb32b242020-03-17 20:42:47 -040074 return EShLangCallable;
Chao Chen3c366992018-09-19 11:41:59 -070075 } else if (stage == "task") {
Pankaj Mistry228c6722021-08-25 13:41:32 -070076 return EShLangTask;
Chao Chen3c366992018-09-19 11:41:59 -070077 } else if (stage == "mesh") {
Pankaj Mistry228c6722021-08-25 13:41:32 -070078 return EShLangMesh;
Lei Zhang414eb602016-03-04 16:22:34 -050079 } else {
80 assert(0 && "Unknown shader stage");
81 return EShLangCount;
82 }
83}
84
Lei Zhangd6f0ed22016-05-16 12:50:30 -040085EShMessages DeriveOptions(Source source, Semantics semantics, Target target)
Lei Zhang414eb602016-03-04 16:22:34 -050086{
John Kessenicha86836e2016-07-09 14:50:57 -060087 EShMessages result = EShMsgCascadingErrors;
Lei Zhang414eb602016-03-04 16:22:34 -050088
Lei Zhangd6f0ed22016-05-16 12:50:30 -040089 switch (source) {
90 case Source::GLSL:
91 break;
92 case Source::HLSL:
John Kessenicha2a5dd42016-10-01 18:07:57 -060093 result = static_cast<EShMessages>(result | EShMsgReadHlsl);
Lei Zhangd6f0ed22016-05-16 12:50:30 -040094 break;
95 }
96
Lei Zhang414eb602016-03-04 16:22:34 -050097 switch (target) {
98 case Target::AST:
Lei Zhangd6f0ed22016-05-16 12:50:30 -040099 result = static_cast<EShMessages>(result | EShMsgAST);
Lei Zhang414eb602016-03-04 16:22:34 -0500100 break;
Lei Zhangd6f0ed22016-05-16 12:50:30 -0400101 case Target::Spv:
102 result = static_cast<EShMessages>(result | EShMsgSpvRules);
John Kessenich906cc212016-12-09 19:22:20 -0700103 result = static_cast<EShMessages>(result | EShMsgKeepUncalled);
Lei Zhangd6f0ed22016-05-16 12:50:30 -0400104 break;
105 case Target::BothASTAndSpv:
106 result = static_cast<EShMessages>(result | EShMsgSpvRules | EShMsgAST);
John Kessenich906cc212016-12-09 19:22:20 -0700107 result = static_cast<EShMessages>(result | EShMsgKeepUncalled);
Lei Zhang414eb602016-03-04 16:22:34 -0500108 break;
109 };
110
111 switch (semantics) {
112 case Semantics::OpenGL:
113 break;
114 case Semantics::Vulkan:
John Kessenichd82c9062016-05-23 16:07:07 -0600115 result = static_cast<EShMessages>(result | EShMsgVulkanRules | EShMsgSpvRules);
Lei Zhang414eb602016-03-04 16:22:34 -0500116 break;
117 }
118
GregFfb03a552018-03-29 11:49:14 -0600119 result = static_cast<EShMessages>(result | EShMsgHlslLegalization);
120
Lei Zhang414eb602016-03-04 16:22:34 -0500121 return result;
122}
123
124std::pair<bool, std::string> ReadFile(const std::string& path)
125{
126 std::ifstream fstream(path, std::ios::in);
127 if (fstream) {
128 std::string contents;
129 fstream.seekg(0, std::ios::end);
John Kessenich7f349c72016-07-08 22:09:10 -0600130 contents.reserve((std::string::size_type)fstream.tellg());
Lei Zhang414eb602016-03-04 16:22:34 -0500131 fstream.seekg(0, std::ios::beg);
132 contents.assign((std::istreambuf_iterator<char>(fstream)),
133 std::istreambuf_iterator<char>());
134 return std::make_pair(true, contents);
135 }
136 return std::make_pair(false, "");
137}
138
steve-lunarga8456412016-08-17 16:18:06 -0600139std::pair<bool, std::vector<std::uint32_t> > ReadSpvBinaryFile(const std::string& path)
140{
141 std::ifstream fstream(path, std::fstream::in | std::fstream::binary);
142
143 if (!fstream)
144 return std::make_pair(false, std::vector<std::uint32_t>());
145
146 std::vector<std::uint32_t> contents;
147
148 // Reserve space (for efficiency, not for correctness)
149 fstream.seekg(0, fstream.end);
150 contents.reserve(size_t(fstream.tellg()) / sizeof(std::uint32_t));
151 fstream.seekg(0, fstream.beg);
152
153 // There is no istream iterator traversing by uint32_t, so we must loop.
154 while (!fstream.eof()) {
155 std::uint32_t inWord;
156 fstream.read((char *)&inWord, sizeof(inWord));
157
158 if (!fstream.eof())
159 contents.push_back(inWord);
160 }
161
162 return std::make_pair(true, contents); // hopefully, c++11 move semantics optimizes the copy away.
163}
164
Lei Zhang414eb602016-03-04 16:22:34 -0500165bool WriteFile(const std::string& path, const std::string& contents)
166{
167 std::ofstream fstream(path, std::ios::out);
168 if (!fstream) return false;
169 fstream << contents;
170 fstream.flush();
171 return true;
172}
173
174std::string GetSuffix(const std::string& name)
175{
176 const size_t pos = name.rfind('.');
177 return (pos == std::string::npos) ? "" : name.substr(name.rfind('.') + 1);
178}
179
180} // namespace glslangtest