TestFixture.h 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701
  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. #ifndef GLSLANG_GTESTS_TEST_FIXTURE_H
  35. #define GLSLANG_GTESTS_TEST_FIXTURE_H
  36. #include <cstdint>
  37. #include <fstream>
  38. #include <sstream>
  39. #include <streambuf>
  40. #include <tuple>
  41. #include <string>
  42. #include <gtest/gtest.h>
  43. #include "SPIRV/GlslangToSpv.h"
  44. #include "SPIRV/disassemble.h"
  45. #include "SPIRV/doc.h"
  46. #include "SPIRV/SPVRemapper.h"
  47. #include "StandAlone/ResourceLimits.h"
  48. #include "glslang/Public/ShaderLang.h"
  49. #include "Initializer.h"
  50. #include "Settings.h"
  51. namespace glslangtest {
  52. // This function is used to provide custom test name suffixes based on the
  53. // shader source file names. Otherwise, the test name suffixes will just be
  54. // numbers, which are not quite obvious.
  55. std::string FileNameAsCustomTestSuffix(
  56. const ::testing::TestParamInfo<std::string>& info);
  57. enum class Source {
  58. GLSL,
  59. HLSL,
  60. };
  61. // Enum for shader compilation semantics.
  62. enum class Semantics {
  63. OpenGL,
  64. Vulkan
  65. };
  66. // Enum for compilation target.
  67. enum class Target {
  68. AST,
  69. Spv,
  70. BothASTAndSpv,
  71. };
  72. EShLanguage GetShaderStage(const std::string& stage);
  73. EShMessages DeriveOptions(Source, Semantics, Target);
  74. // Reads the content of the file at the given |path|. On success, returns true
  75. // and the contents; otherwise, returns false and an empty string.
  76. std::pair<bool, std::string> ReadFile(const std::string& path);
  77. std::pair<bool, std::vector<std::uint32_t> > ReadSpvBinaryFile(const std::string& path);
  78. // Writes the given |contents| into the file at the given |path|. Returns true
  79. // on successful output.
  80. bool WriteFile(const std::string& path, const std::string& contents);
  81. // Returns the suffix of the given |name|.
  82. std::string GetSuffix(const std::string& name);
  83. // Base class for glslang integration tests. It contains many handy utility-like
  84. // methods such as reading shader source files, compiling into AST/SPIR-V, and
  85. // comparing with expected outputs.
  86. //
  87. // To write value-Parameterized tests:
  88. // using ValueParamTest = GlslangTest<::testing::TestWithParam<std::string>>;
  89. // To use as normal fixture:
  90. // using FixtureTest = GlslangTest<::testing::Test>;
  91. template <typename GT>
  92. class GlslangTest : public GT {
  93. public:
  94. GlslangTest()
  95. : defaultVersion(100),
  96. defaultProfile(ENoProfile),
  97. forceVersionProfile(false),
  98. isForwardCompatible(false) {
  99. // Perform validation by default.
  100. validatorOptions.validate = true;
  101. }
  102. // Tries to load the contents from the file at the given |path|. On success,
  103. // writes the contents into |contents|. On failure, errors out.
  104. void tryLoadFile(const std::string& path, const std::string& tag,
  105. std::string* contents)
  106. {
  107. bool fileReadOk;
  108. std::tie(fileReadOk, *contents) = ReadFile(path);
  109. ASSERT_TRUE(fileReadOk) << "Cannot open " << tag << " file: " << path;
  110. }
  111. // Tries to load the contents from the file at the given |path|. On success,
  112. // writes the contents into |contents|. On failure, errors out.
  113. void tryLoadSpvFile(const std::string& path, const std::string& tag,
  114. std::vector<uint32_t>& contents)
  115. {
  116. bool fileReadOk;
  117. std::tie(fileReadOk, contents) = ReadSpvBinaryFile(path);
  118. ASSERT_TRUE(fileReadOk) << "Cannot open " << tag << " file: " << path;
  119. }
  120. // Checks the equality of |expected| and |real|. If they are not equal,
  121. // write |real| to the given file named as |fname| if update mode is on.
  122. void checkEqAndUpdateIfRequested(const std::string& expected,
  123. const std::string& real,
  124. const std::string& fname,
  125. const std::string& errorsAndWarnings = "")
  126. {
  127. // In order to output the message we want under proper circumstances,
  128. // we need the following operator<< stuff.
  129. EXPECT_EQ(expected, real)
  130. << (GlobalTestSettings.updateMode
  131. ? ("Mismatch found and update mode turned on - "
  132. "flushing expected result output.\n")
  133. : "")
  134. << "The following warnings/errors occurred:\n"
  135. << errorsAndWarnings;
  136. // Update the expected output file if requested.
  137. // It looks weird to duplicate the comparison between expected_output
  138. // and stream.str(). However, if creating a variable for the comparison
  139. // result, we cannot have pretty print of the string diff in the above.
  140. if (GlobalTestSettings.updateMode && expected != real) {
  141. EXPECT_TRUE(WriteFile(fname, real)) << "Flushing failed";
  142. }
  143. }
  144. struct ShaderResult {
  145. std::string shaderName;
  146. std::string output;
  147. std::string error;
  148. };
  149. // A struct for holding all the information returned by glslang compilation
  150. // and linking.
  151. struct GlslangResult {
  152. std::vector<ShaderResult> shaderResults;
  153. std::string linkingOutput;
  154. std::string linkingError;
  155. bool validationResult;
  156. std::string spirvWarningsErrors;
  157. std::string spirv; // Optional SPIR-V disassembly text.
  158. };
  159. // Compiles and the given source |code| of the given shader |stage| into
  160. // the target under the semantics conveyed via |controls|. Returns true
  161. // and modifies |shader| on success.
  162. bool compile(glslang::TShader* shader, const std::string& code,
  163. const std::string& entryPointName, EShMessages controls,
  164. const TBuiltInResource* resources=nullptr,
  165. const std::string* shaderName=nullptr)
  166. {
  167. const char* shaderStrings = code.data();
  168. const int shaderLengths = static_cast<int>(code.size());
  169. const char* shaderNames = nullptr;
  170. if ((controls & EShMsgDebugInfo) && shaderName != nullptr) {
  171. shaderNames = shaderName->data();
  172. shader->setStringsWithLengthsAndNames(
  173. &shaderStrings, &shaderLengths, &shaderNames, 1);
  174. } else
  175. shader->setStringsWithLengths(&shaderStrings, &shaderLengths, 1);
  176. if (!entryPointName.empty()) shader->setEntryPoint(entryPointName.c_str());
  177. return shader->parse(
  178. (resources ? resources : &glslang::DefaultTBuiltInResource),
  179. defaultVersion, isForwardCompatible, controls);
  180. }
  181. // Compiles and links the given source |code| of the given shader
  182. // |stage| into the target under the semantics specified via |controls|.
  183. // Returns a GlslangResult instance containing all the information generated
  184. // during the process. If the target includes SPIR-V, also disassembles
  185. // the result and returns disassembly text.
  186. GlslangResult compileAndLink(
  187. const std::string& shaderName, const std::string& code,
  188. const std::string& entryPointName, EShMessages controls,
  189. glslang::EShTargetClientVersion clientTargetVersion,
  190. bool flattenUniformArrays = false,
  191. EShTextureSamplerTransformMode texSampTransMode = EShTexSampTransKeep,
  192. bool enableOptimizer = false,
  193. bool enableDebug = false,
  194. bool automap = true)
  195. {
  196. const EShLanguage stage = GetShaderStage(GetSuffix(shaderName));
  197. glslang::TShader shader(stage);
  198. if (automap) {
  199. shader.setAutoMapLocations(true);
  200. shader.setAutoMapBindings(true);
  201. }
  202. shader.setTextureSamplerTransformMode(texSampTransMode);
  203. shader.setFlattenUniformArrays(flattenUniformArrays);
  204. if (controls & EShMsgSpvRules) {
  205. if (controls & EShMsgVulkanRules) {
  206. shader.setEnvInput((controls & EShMsgReadHlsl) ? glslang::EShSourceHlsl
  207. : glslang::EShSourceGlsl,
  208. stage, glslang::EShClientVulkan, 100);
  209. shader.setEnvClient(glslang::EShClientVulkan, clientTargetVersion);
  210. shader.setEnvTarget(glslang::EShTargetSpv,
  211. clientTargetVersion == glslang::EShTargetVulkan_1_1 ? glslang::EShTargetSpv_1_3
  212. : glslang::EShTargetSpv_1_0);
  213. } else {
  214. shader.setEnvInput((controls & EShMsgReadHlsl) ? glslang::EShSourceHlsl
  215. : glslang::EShSourceGlsl,
  216. stage, glslang::EShClientOpenGL, 100);
  217. shader.setEnvClient(glslang::EShClientOpenGL, clientTargetVersion);
  218. shader.setEnvTarget(glslang::EshTargetSpv, glslang::EShTargetSpv_1_0);
  219. }
  220. }
  221. bool success = compile(
  222. &shader, code, entryPointName, controls, nullptr, &shaderName);
  223. glslang::TProgram program;
  224. program.addShader(&shader);
  225. success &= program.link(controls);
  226. spv::SpvBuildLogger logger;
  227. if (success && (controls & EShMsgSpvRules)) {
  228. std::vector<uint32_t> spirv_binary;
  229. options().disableOptimizer = !enableOptimizer;
  230. options().generateDebugInfo = enableDebug;
  231. glslang::GlslangToSpv(*program.getIntermediate(stage),
  232. spirv_binary, &logger, &options());
  233. std::ostringstream disassembly_stream;
  234. spv::Parameterize();
  235. spv::Disassemble(disassembly_stream, spirv_binary);
  236. bool validation_result = !options().validate || logger.getAllMessages().empty();
  237. return {{{shaderName, shader.getInfoLog(), shader.getInfoDebugLog()},},
  238. program.getInfoLog(), program.getInfoDebugLog(),
  239. validation_result, logger.getAllMessages(), disassembly_stream.str()};
  240. } else {
  241. return {{{shaderName, shader.getInfoLog(), shader.getInfoDebugLog()},},
  242. program.getInfoLog(), program.getInfoDebugLog(), true, "", ""};
  243. }
  244. }
  245. // Compiles and links the given source |code| of the given shader
  246. // |stage| into the target under the semantics specified via |controls|.
  247. // Returns a GlslangResult instance containing all the information generated
  248. // during the process. If the target includes SPIR-V, also disassembles
  249. // the result and returns disassembly text.
  250. GlslangResult compileLinkIoMap(
  251. const std::string shaderName, const std::string& code,
  252. const std::string& entryPointName, EShMessages controls,
  253. int baseSamplerBinding,
  254. int baseTextureBinding,
  255. int baseImageBinding,
  256. int baseUboBinding,
  257. int baseSsboBinding,
  258. bool autoMapBindings,
  259. bool flattenUniformArrays)
  260. {
  261. const EShLanguage stage = GetShaderStage(GetSuffix(shaderName));
  262. glslang::TShader shader(stage);
  263. shader.setShiftSamplerBinding(baseSamplerBinding);
  264. shader.setShiftTextureBinding(baseTextureBinding);
  265. shader.setShiftImageBinding(baseImageBinding);
  266. shader.setShiftUboBinding(baseUboBinding);
  267. shader.setShiftSsboBinding(baseSsboBinding);
  268. shader.setAutoMapBindings(autoMapBindings);
  269. shader.setAutoMapLocations(true);
  270. shader.setFlattenUniformArrays(flattenUniformArrays);
  271. bool success = compile(&shader, code, entryPointName, controls);
  272. glslang::TProgram program;
  273. program.addShader(&shader);
  274. success &= program.link(controls);
  275. success &= program.mapIO();
  276. spv::SpvBuildLogger logger;
  277. if (success && (controls & EShMsgSpvRules)) {
  278. std::vector<uint32_t> spirv_binary;
  279. glslang::GlslangToSpv(*program.getIntermediate(stage),
  280. spirv_binary, &logger, &options());
  281. std::ostringstream disassembly_stream;
  282. spv::Parameterize();
  283. spv::Disassemble(disassembly_stream, spirv_binary);
  284. bool validation_result = !options().validate || logger.getAllMessages().empty();
  285. return {{{shaderName, shader.getInfoLog(), shader.getInfoDebugLog()},},
  286. program.getInfoLog(), program.getInfoDebugLog(),
  287. validation_result, logger.getAllMessages(), disassembly_stream.str()};
  288. } else {
  289. return {{{shaderName, shader.getInfoLog(), shader.getInfoDebugLog()},},
  290. program.getInfoLog(), program.getInfoDebugLog(), true, "", ""};
  291. }
  292. }
  293. // This is like compileAndLink but with remapping of the SPV binary
  294. // through spirvbin_t::remap(). While technically this could be merged
  295. // with compileAndLink() above (with the remap step optionally being a no-op)
  296. // it is given separately here for ease of future extraction.
  297. GlslangResult compileLinkRemap(
  298. const std::string shaderName, const std::string& code,
  299. const std::string& entryPointName, EShMessages controls,
  300. const unsigned int remapOptions = spv::spirvbin_t::NONE)
  301. {
  302. const EShLanguage stage = GetShaderStage(GetSuffix(shaderName));
  303. glslang::TShader shader(stage);
  304. shader.setAutoMapBindings(true);
  305. shader.setAutoMapLocations(true);
  306. bool success = compile(&shader, code, entryPointName, controls);
  307. glslang::TProgram program;
  308. program.addShader(&shader);
  309. success &= program.link(controls);
  310. spv::SpvBuildLogger logger;
  311. if (success && (controls & EShMsgSpvRules)) {
  312. std::vector<uint32_t> spirv_binary;
  313. glslang::GlslangToSpv(*program.getIntermediate(stage),
  314. spirv_binary, &logger, &options());
  315. spv::spirvbin_t(0 /*verbosity*/).remap(spirv_binary, remapOptions);
  316. std::ostringstream disassembly_stream;
  317. spv::Parameterize();
  318. spv::Disassemble(disassembly_stream, spirv_binary);
  319. bool validation_result = !options().validate || logger.getAllMessages().empty();
  320. return {{{shaderName, shader.getInfoLog(), shader.getInfoDebugLog()},},
  321. program.getInfoLog(), program.getInfoDebugLog(),
  322. validation_result, logger.getAllMessages(), disassembly_stream.str()};
  323. } else {
  324. return {{{shaderName, shader.getInfoLog(), shader.getInfoDebugLog()},},
  325. program.getInfoLog(), program.getInfoDebugLog(), true, "", ""};
  326. }
  327. }
  328. // remap the binary in 'code' with the options in remapOptions
  329. GlslangResult remap(
  330. const std::string shaderName, const std::vector<uint32_t>& code,
  331. EShMessages controls,
  332. const unsigned int remapOptions = spv::spirvbin_t::NONE)
  333. {
  334. if ((controls & EShMsgSpvRules)) {
  335. std::vector<uint32_t> spirv_binary(code); // scratch copy
  336. spv::spirvbin_t(0 /*verbosity*/).remap(spirv_binary, remapOptions);
  337. std::ostringstream disassembly_stream;
  338. spv::Parameterize();
  339. spv::Disassemble(disassembly_stream, spirv_binary);
  340. return {{{shaderName, "", ""},},
  341. "", "",
  342. true, "", disassembly_stream.str()};
  343. } else {
  344. return {{{shaderName, "", ""},}, "", "", true, "", ""};
  345. }
  346. }
  347. void outputResultToStream(std::ostringstream* stream,
  348. const GlslangResult& result,
  349. EShMessages controls)
  350. {
  351. const auto outputIfNotEmpty = [&stream](const std::string& str) {
  352. if (!str.empty()) *stream << str << "\n";
  353. };
  354. for (const auto& shaderResult : result.shaderResults) {
  355. *stream << shaderResult.shaderName << "\n";
  356. outputIfNotEmpty(shaderResult.output);
  357. outputIfNotEmpty(shaderResult.error);
  358. }
  359. outputIfNotEmpty(result.linkingOutput);
  360. outputIfNotEmpty(result.linkingError);
  361. if (!result.validationResult) {
  362. *stream << "Validation failed\n";
  363. }
  364. if (controls & EShMsgSpvRules) {
  365. *stream
  366. << (result.spirv.empty()
  367. ? "SPIR-V is not generated for failed compile or link\n"
  368. : result.spirv);
  369. }
  370. }
  371. void loadFileCompileAndCheck(const std::string& testDir,
  372. const std::string& testName,
  373. Source source,
  374. Semantics semantics,
  375. glslang::EShTargetClientVersion clientTargetVersion,
  376. Target target,
  377. bool automap = true,
  378. const std::string& entryPointName="",
  379. const std::string& baseDir="/baseResults/",
  380. const bool enableOptimizer = false,
  381. const bool enableDebug = false)
  382. {
  383. const std::string inputFname = testDir + "/" + testName;
  384. const std::string expectedOutputFname =
  385. testDir + baseDir + testName + ".out";
  386. std::string input, expectedOutput;
  387. tryLoadFile(inputFname, "input", &input);
  388. tryLoadFile(expectedOutputFname, "expected output", &expectedOutput);
  389. EShMessages controls = DeriveOptions(source, semantics, target);
  390. if (enableOptimizer)
  391. controls = static_cast<EShMessages>(controls & ~EShMsgHlslLegalization);
  392. if (enableDebug)
  393. controls = static_cast<EShMessages>(controls | EShMsgDebugInfo);
  394. GlslangResult result = compileAndLink(testName, input, entryPointName, controls, clientTargetVersion, false,
  395. EShTexSampTransKeep, enableOptimizer, enableDebug, automap);
  396. // Generate the hybrid output in the way of glslangValidator.
  397. std::ostringstream stream;
  398. outputResultToStream(&stream, result, controls);
  399. checkEqAndUpdateIfRequested(expectedOutput, stream.str(),
  400. expectedOutputFname, result.spirvWarningsErrors);
  401. }
  402. void loadFileCompileAndCheckWithOptions(const std::string &testDir,
  403. const std::string &testName,
  404. Source source,
  405. Semantics semantics,
  406. glslang::EShTargetClientVersion clientTargetVersion,
  407. Target target, bool automap = true, const std::string &entryPointName = "",
  408. const std::string &baseDir = "/baseResults/",
  409. const EShMessages additionalOptions = EShMessages::EShMsgDefault)
  410. {
  411. const std::string inputFname = testDir + "/" + testName;
  412. const std::string expectedOutputFname = testDir + baseDir + testName + ".out";
  413. std::string input, expectedOutput;
  414. tryLoadFile(inputFname, "input", &input);
  415. tryLoadFile(expectedOutputFname, "expected output", &expectedOutput);
  416. EShMessages controls = DeriveOptions(source, semantics, target);
  417. controls = static_cast<EShMessages>(controls | additionalOptions);
  418. GlslangResult result = compileAndLink(testName, input, entryPointName, controls, clientTargetVersion, false,
  419. EShTexSampTransKeep, false, automap);
  420. // Generate the hybrid output in the way of glslangValidator.
  421. std::ostringstream stream;
  422. outputResultToStream(&stream, result, controls);
  423. checkEqAndUpdateIfRequested(expectedOutput, stream.str(), expectedOutputFname);
  424. }
  425. void loadFileCompileFlattenUniformsAndCheck(const std::string& testDir,
  426. const std::string& testName,
  427. Source source,
  428. Semantics semantics,
  429. Target target,
  430. const std::string& entryPointName="")
  431. {
  432. const std::string inputFname = testDir + "/" + testName;
  433. const std::string expectedOutputFname =
  434. testDir + "/baseResults/" + testName + ".out";
  435. std::string input, expectedOutput;
  436. tryLoadFile(inputFname, "input", &input);
  437. tryLoadFile(expectedOutputFname, "expected output", &expectedOutput);
  438. const EShMessages controls = DeriveOptions(source, semantics, target);
  439. GlslangResult result = compileAndLink(testName, input, entryPointName, controls,
  440. glslang::EShTargetVulkan_1_0, true);
  441. // Generate the hybrid output in the way of glslangValidator.
  442. std::ostringstream stream;
  443. outputResultToStream(&stream, result, controls);
  444. checkEqAndUpdateIfRequested(expectedOutput, stream.str(),
  445. expectedOutputFname, result.spirvWarningsErrors);
  446. }
  447. void loadFileCompileIoMapAndCheck(const std::string& testDir,
  448. const std::string& testName,
  449. Source source,
  450. Semantics semantics,
  451. Target target,
  452. const std::string& entryPointName,
  453. int baseSamplerBinding,
  454. int baseTextureBinding,
  455. int baseImageBinding,
  456. int baseUboBinding,
  457. int baseSsboBinding,
  458. bool autoMapBindings,
  459. bool flattenUniformArrays)
  460. {
  461. const std::string inputFname = testDir + "/" + testName;
  462. const std::string expectedOutputFname =
  463. testDir + "/baseResults/" + testName + ".out";
  464. std::string input, expectedOutput;
  465. tryLoadFile(inputFname, "input", &input);
  466. tryLoadFile(expectedOutputFname, "expected output", &expectedOutput);
  467. const EShMessages controls = DeriveOptions(source, semantics, target);
  468. GlslangResult result = compileLinkIoMap(testName, input, entryPointName, controls,
  469. baseSamplerBinding, baseTextureBinding, baseImageBinding,
  470. baseUboBinding, baseSsboBinding,
  471. autoMapBindings,
  472. flattenUniformArrays);
  473. // Generate the hybrid output in the way of glslangValidator.
  474. std::ostringstream stream;
  475. outputResultToStream(&stream, result, controls);
  476. checkEqAndUpdateIfRequested(expectedOutput, stream.str(),
  477. expectedOutputFname, result.spirvWarningsErrors);
  478. }
  479. void loadFileCompileRemapAndCheck(const std::string& testDir,
  480. const std::string& testName,
  481. Source source,
  482. Semantics semantics,
  483. Target target,
  484. const std::string& entryPointName="",
  485. const unsigned int remapOptions = spv::spirvbin_t::NONE)
  486. {
  487. const std::string inputFname = testDir + "/" + testName;
  488. const std::string expectedOutputFname =
  489. testDir + "/baseResults/" + testName + ".out";
  490. std::string input, expectedOutput;
  491. tryLoadFile(inputFname, "input", &input);
  492. tryLoadFile(expectedOutputFname, "expected output", &expectedOutput);
  493. const EShMessages controls = DeriveOptions(source, semantics, target);
  494. GlslangResult result = compileLinkRemap(testName, input, entryPointName, controls, remapOptions);
  495. // Generate the hybrid output in the way of glslangValidator.
  496. std::ostringstream stream;
  497. outputResultToStream(&stream, result, controls);
  498. checkEqAndUpdateIfRequested(expectedOutput, stream.str(),
  499. expectedOutputFname, result.spirvWarningsErrors);
  500. }
  501. void loadFileRemapAndCheck(const std::string& testDir,
  502. const std::string& testName,
  503. Source source,
  504. Semantics semantics,
  505. Target target,
  506. const unsigned int remapOptions = spv::spirvbin_t::NONE)
  507. {
  508. const std::string inputFname = testDir + "/" + testName;
  509. const std::string expectedOutputFname =
  510. testDir + "/baseResults/" + testName + ".out";
  511. std::vector<std::uint32_t> input;
  512. std::string expectedOutput;
  513. tryLoadSpvFile(inputFname, "input", input);
  514. tryLoadFile(expectedOutputFname, "expected output", &expectedOutput);
  515. const EShMessages controls = DeriveOptions(source, semantics, target);
  516. GlslangResult result = remap(testName, input, controls, remapOptions);
  517. // Generate the hybrid output in the way of glslangValidator.
  518. std::ostringstream stream;
  519. outputResultToStream(&stream, result, controls);
  520. checkEqAndUpdateIfRequested(expectedOutput, stream.str(),
  521. expectedOutputFname, result.spirvWarningsErrors);
  522. }
  523. // Preprocesses the given |source| code. On success, returns true, the
  524. // preprocessed shader, and warning messages. Otherwise, returns false, an
  525. // empty string, and error messages.
  526. std::tuple<bool, std::string, std::string> preprocess(
  527. const std::string& source)
  528. {
  529. const char* shaderStrings = source.data();
  530. const int shaderLengths = static_cast<int>(source.size());
  531. glslang::TShader shader(EShLangVertex);
  532. shader.setStringsWithLengths(&shaderStrings, &shaderLengths, 1);
  533. std::string ppShader;
  534. glslang::TShader::ForbidIncluder includer;
  535. const bool success = shader.preprocess(
  536. &glslang::DefaultTBuiltInResource, defaultVersion, defaultProfile,
  537. forceVersionProfile, isForwardCompatible, (EShMessages)(EShMsgOnlyPreprocessor | EShMsgCascadingErrors),
  538. &ppShader, includer);
  539. std::string log = shader.getInfoLog();
  540. log += shader.getInfoDebugLog();
  541. if (success) {
  542. return std::make_tuple(true, ppShader, log);
  543. } else {
  544. return std::make_tuple(false, "", log);
  545. }
  546. }
  547. void loadFilePreprocessAndCheck(const std::string& testDir,
  548. const std::string& testName)
  549. {
  550. const std::string inputFname = testDir + "/" + testName;
  551. const std::string expectedOutputFname =
  552. testDir + "/baseResults/" + testName + ".out";
  553. const std::string expectedErrorFname =
  554. testDir + "/baseResults/" + testName + ".err";
  555. std::string input, expectedOutput, expectedError;
  556. tryLoadFile(inputFname, "input", &input);
  557. tryLoadFile(expectedOutputFname, "expected output", &expectedOutput);
  558. tryLoadFile(expectedErrorFname, "expected error", &expectedError);
  559. bool ppOk;
  560. std::string output, error;
  561. std::tie(ppOk, output, error) = preprocess(input);
  562. if (!output.empty()) output += '\n';
  563. if (!error.empty()) error += '\n';
  564. checkEqAndUpdateIfRequested(expectedOutput, output,
  565. expectedOutputFname);
  566. checkEqAndUpdateIfRequested(expectedError, error,
  567. expectedErrorFname);
  568. }
  569. void loadCompileUpgradeTextureToSampledTextureAndDropSamplersAndCheck(const std::string& testDir,
  570. const std::string& testName,
  571. Source source,
  572. Semantics semantics,
  573. Target target,
  574. const std::string& entryPointName = "")
  575. {
  576. const std::string inputFname = testDir + "/" + testName;
  577. const std::string expectedOutputFname = testDir + "/baseResults/" + testName + ".out";
  578. std::string input, expectedOutput;
  579. tryLoadFile(inputFname, "input", &input);
  580. tryLoadFile(expectedOutputFname, "expected output", &expectedOutput);
  581. const EShMessages controls = DeriveOptions(source, semantics, target);
  582. GlslangResult result = compileAndLink(testName, input, entryPointName, controls,
  583. glslang::EShTargetVulkan_1_0, false,
  584. EShTexSampTransUpgradeTextureRemoveSampler);
  585. // Generate the hybrid output in the way of glslangValidator.
  586. std::ostringstream stream;
  587. outputResultToStream(&stream, result, controls);
  588. checkEqAndUpdateIfRequested(expectedOutput, stream.str(),
  589. expectedOutputFname, result.spirvWarningsErrors);
  590. }
  591. glslang::SpvOptions& options() { return validatorOptions; }
  592. private:
  593. const int defaultVersion;
  594. const EProfile defaultProfile;
  595. const bool forceVersionProfile;
  596. const bool isForwardCompatible;
  597. glslang::SpvOptions validatorOptions;
  598. };
  599. } // namespace glslangtest
  600. #endif // GLSLANG_GTESTS_TEST_FIXTURE_H