TestFixture.h 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647
  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. // Tries to load the contents from the file at the given |path|. On success,
  100. // writes the contents into |contents|. On failure, errors out.
  101. void tryLoadFile(const std::string& path, const std::string& tag,
  102. std::string* contents)
  103. {
  104. bool fileReadOk;
  105. std::tie(fileReadOk, *contents) = ReadFile(path);
  106. ASSERT_TRUE(fileReadOk) << "Cannot open " << tag << " file: " << path;
  107. }
  108. // Tries to load the contents from the file at the given |path|. On success,
  109. // writes the contents into |contents|. On failure, errors out.
  110. void tryLoadSpvFile(const std::string& path, const std::string& tag,
  111. std::vector<uint32_t>& contents)
  112. {
  113. bool fileReadOk;
  114. std::tie(fileReadOk, contents) = ReadSpvBinaryFile(path);
  115. ASSERT_TRUE(fileReadOk) << "Cannot open " << tag << " file: " << path;
  116. }
  117. // Checks the equality of |expected| and |real|. If they are not equal,
  118. // write |real| to the given file named as |fname| if update mode is on.
  119. void checkEqAndUpdateIfRequested(const std::string& expected,
  120. const std::string& real,
  121. const std::string& fname)
  122. {
  123. // In order to output the message we want under proper circumstances,
  124. // we need the following operator<< stuff.
  125. EXPECT_EQ(expected, real)
  126. << (GlobalTestSettings.updateMode
  127. ? ("Mismatch found and update mode turned on - "
  128. "flushing expected result output.")
  129. : "");
  130. // Update the expected output file if requested.
  131. // It looks weird to duplicate the comparison between expected_output
  132. // and stream.str(). However, if creating a variable for the comparison
  133. // result, we cannot have pretty print of the string diff in the above.
  134. if (GlobalTestSettings.updateMode && expected != real) {
  135. EXPECT_TRUE(WriteFile(fname, real)) << "Flushing failed";
  136. }
  137. }
  138. struct ShaderResult {
  139. std::string shaderName;
  140. std::string output;
  141. std::string error;
  142. };
  143. // A struct for holding all the information returned by glslang compilation
  144. // and linking.
  145. struct GlslangResult {
  146. std::vector<ShaderResult> shaderResults;
  147. std::string linkingOutput;
  148. std::string linkingError;
  149. std::string spirvWarningsErrors;
  150. std::string spirv; // Optional SPIR-V disassembly text.
  151. };
  152. // Compiles and the given source |code| of the given shader |stage| into
  153. // the target under the semantics conveyed via |controls|. Returns true
  154. // and modifies |shader| on success.
  155. bool compile(glslang::TShader* shader, const std::string& code,
  156. const std::string& entryPointName, EShMessages controls,
  157. const TBuiltInResource* resources=nullptr)
  158. {
  159. const char* shaderStrings = code.data();
  160. const int shaderLengths = static_cast<int>(code.size());
  161. shader->setStringsWithLengths(&shaderStrings, &shaderLengths, 1);
  162. if (!entryPointName.empty()) shader->setEntryPoint(entryPointName.c_str());
  163. return shader->parse(
  164. (resources ? resources : &glslang::DefaultTBuiltInResource),
  165. defaultVersion, isForwardCompatible, controls);
  166. }
  167. // Compiles and links the given source |code| of the given shader
  168. // |stage| into the target under the semantics specified via |controls|.
  169. // Returns a GlslangResult instance containing all the information generated
  170. // during the process. If the target includes SPIR-V, also disassembles
  171. // the result and returns disassembly text.
  172. GlslangResult compileAndLink(
  173. const std::string shaderName, const std::string& code,
  174. const std::string& entryPointName, EShMessages controls,
  175. glslang::EShTargetClientVersion clientTargetVersion,
  176. bool flattenUniformArrays = false,
  177. EShTextureSamplerTransformMode texSampTransMode = EShTexSampTransKeep,
  178. bool enableOptimizer = false,
  179. bool automap = true)
  180. {
  181. const EShLanguage stage = GetShaderStage(GetSuffix(shaderName));
  182. glslang::TShader shader(stage);
  183. if (automap) {
  184. shader.setAutoMapLocations(true);
  185. shader.setAutoMapBindings(true);
  186. }
  187. shader.setTextureSamplerTransformMode(texSampTransMode);
  188. shader.setFlattenUniformArrays(flattenUniformArrays);
  189. if (controls & EShMsgSpvRules) {
  190. if (controls & EShMsgVulkanRules) {
  191. shader.setEnvInput((controls & EShMsgReadHlsl) ? glslang::EShSourceHlsl
  192. : glslang::EShSourceGlsl,
  193. stage, glslang::EShClientVulkan, 100);
  194. shader.setEnvClient(glslang::EShClientVulkan, clientTargetVersion);
  195. shader.setEnvTarget(glslang::EShTargetSpv,
  196. clientTargetVersion == glslang::EShTargetVulkan_1_1 ? glslang::EShTargetSpv_1_3
  197. : glslang::EShTargetSpv_1_0);
  198. } else {
  199. shader.setEnvInput((controls & EShMsgReadHlsl) ? glslang::EShSourceHlsl
  200. : glslang::EShSourceGlsl,
  201. stage, glslang::EShClientOpenGL, 100);
  202. shader.setEnvClient(glslang::EShClientOpenGL, clientTargetVersion);
  203. shader.setEnvTarget(glslang::EshTargetSpv, glslang::EShTargetSpv_1_0);
  204. }
  205. }
  206. bool success = compile(&shader, code, entryPointName, controls);
  207. glslang::TProgram program;
  208. program.addShader(&shader);
  209. success &= program.link(controls);
  210. spv::SpvBuildLogger logger;
  211. if (success && (controls & EShMsgSpvRules)) {
  212. std::vector<uint32_t> spirv_binary;
  213. glslang::SpvOptions options;
  214. options.disableOptimizer = !enableOptimizer;
  215. glslang::GlslangToSpv(*program.getIntermediate(stage),
  216. spirv_binary, &logger, &options);
  217. std::ostringstream disassembly_stream;
  218. spv::Parameterize();
  219. spv::Disassemble(disassembly_stream, spirv_binary);
  220. return {{{shaderName, shader.getInfoLog(), shader.getInfoDebugLog()},},
  221. program.getInfoLog(), program.getInfoDebugLog(),
  222. logger.getAllMessages(), disassembly_stream.str()};
  223. } else {
  224. return {{{shaderName, shader.getInfoLog(), shader.getInfoDebugLog()},},
  225. program.getInfoLog(), program.getInfoDebugLog(), "", ""};
  226. }
  227. }
  228. // Compiles and links the given source |code| of the given shader
  229. // |stage| into the target under the semantics specified via |controls|.
  230. // Returns a GlslangResult instance containing all the information generated
  231. // during the process. If the target includes SPIR-V, also disassembles
  232. // the result and returns disassembly text.
  233. GlslangResult compileLinkIoMap(
  234. const std::string shaderName, const std::string& code,
  235. const std::string& entryPointName, EShMessages controls,
  236. int baseSamplerBinding,
  237. int baseTextureBinding,
  238. int baseImageBinding,
  239. int baseUboBinding,
  240. int baseSsboBinding,
  241. bool autoMapBindings,
  242. bool flattenUniformArrays)
  243. {
  244. const EShLanguage stage = GetShaderStage(GetSuffix(shaderName));
  245. glslang::TShader shader(stage);
  246. shader.setShiftSamplerBinding(baseSamplerBinding);
  247. shader.setShiftTextureBinding(baseTextureBinding);
  248. shader.setShiftImageBinding(baseImageBinding);
  249. shader.setShiftUboBinding(baseUboBinding);
  250. shader.setShiftSsboBinding(baseSsboBinding);
  251. shader.setAutoMapBindings(autoMapBindings);
  252. shader.setAutoMapLocations(true);
  253. shader.setFlattenUniformArrays(flattenUniformArrays);
  254. bool success = compile(&shader, code, entryPointName, controls);
  255. glslang::TProgram program;
  256. program.addShader(&shader);
  257. success &= program.link(controls);
  258. success &= program.mapIO();
  259. spv::SpvBuildLogger logger;
  260. if (success && (controls & EShMsgSpvRules)) {
  261. std::vector<uint32_t> spirv_binary;
  262. glslang::GlslangToSpv(*program.getIntermediate(stage),
  263. spirv_binary, &logger);
  264. std::ostringstream disassembly_stream;
  265. spv::Parameterize();
  266. spv::Disassemble(disassembly_stream, spirv_binary);
  267. return {{{shaderName, shader.getInfoLog(), shader.getInfoDebugLog()},},
  268. program.getInfoLog(), program.getInfoDebugLog(),
  269. logger.getAllMessages(), disassembly_stream.str()};
  270. } else {
  271. return {{{shaderName, shader.getInfoLog(), shader.getInfoDebugLog()},},
  272. program.getInfoLog(), program.getInfoDebugLog(), "", ""};
  273. }
  274. }
  275. // This is like compileAndLink but with remapping of the SPV binary
  276. // through spirvbin_t::remap(). While technically this could be merged
  277. // with compileAndLink() above (with the remap step optionally being a no-op)
  278. // it is given separately here for ease of future extraction.
  279. GlslangResult compileLinkRemap(
  280. const std::string shaderName, const std::string& code,
  281. const std::string& entryPointName, EShMessages controls,
  282. const unsigned int remapOptions = spv::spirvbin_t::NONE)
  283. {
  284. const EShLanguage stage = GetShaderStage(GetSuffix(shaderName));
  285. glslang::TShader shader(stage);
  286. shader.setAutoMapBindings(true);
  287. shader.setAutoMapLocations(true);
  288. bool success = compile(&shader, code, entryPointName, controls);
  289. glslang::TProgram program;
  290. program.addShader(&shader);
  291. success &= program.link(controls);
  292. spv::SpvBuildLogger logger;
  293. if (success && (controls & EShMsgSpvRules)) {
  294. std::vector<uint32_t> spirv_binary;
  295. glslang::GlslangToSpv(*program.getIntermediate(stage),
  296. spirv_binary, &logger);
  297. spv::spirvbin_t(0 /*verbosity*/).remap(spirv_binary, remapOptions);
  298. std::ostringstream disassembly_stream;
  299. spv::Parameterize();
  300. spv::Disassemble(disassembly_stream, spirv_binary);
  301. return {{{shaderName, shader.getInfoLog(), shader.getInfoDebugLog()},},
  302. program.getInfoLog(), program.getInfoDebugLog(),
  303. logger.getAllMessages(), disassembly_stream.str()};
  304. } else {
  305. return {{{shaderName, shader.getInfoLog(), shader.getInfoDebugLog()},},
  306. program.getInfoLog(), program.getInfoDebugLog(), "", ""};
  307. }
  308. }
  309. // remap the binary in 'code' with the options in remapOptions
  310. GlslangResult remap(
  311. const std::string shaderName, const std::vector<uint32_t>& code,
  312. EShMessages controls,
  313. const unsigned int remapOptions = spv::spirvbin_t::NONE)
  314. {
  315. if ((controls & EShMsgSpvRules)) {
  316. std::vector<uint32_t> spirv_binary(code); // scratch copy
  317. spv::spirvbin_t(0 /*verbosity*/).remap(spirv_binary, remapOptions);
  318. std::ostringstream disassembly_stream;
  319. spv::Parameterize();
  320. spv::Disassemble(disassembly_stream, spirv_binary);
  321. return {{{shaderName, "", ""},},
  322. "", "",
  323. "", disassembly_stream.str()};
  324. } else {
  325. return {{{shaderName, "", ""},}, "", "", "", ""};
  326. }
  327. }
  328. void outputResultToStream(std::ostringstream* stream,
  329. const GlslangResult& result,
  330. EShMessages controls)
  331. {
  332. const auto outputIfNotEmpty = [&stream](const std::string& str) {
  333. if (!str.empty()) *stream << str << "\n";
  334. };
  335. for (const auto& shaderResult : result.shaderResults) {
  336. *stream << shaderResult.shaderName << "\n";
  337. outputIfNotEmpty(shaderResult.output);
  338. outputIfNotEmpty(shaderResult.error);
  339. }
  340. outputIfNotEmpty(result.linkingOutput);
  341. outputIfNotEmpty(result.linkingError);
  342. *stream << result.spirvWarningsErrors;
  343. if (controls & EShMsgSpvRules) {
  344. *stream
  345. << (result.spirv.empty()
  346. ? "SPIR-V is not generated for failed compile or link\n"
  347. : result.spirv);
  348. }
  349. }
  350. void loadFileCompileAndCheck(const std::string& testDir,
  351. const std::string& testName,
  352. Source source,
  353. Semantics semantics,
  354. glslang::EShTargetClientVersion clientTargetVersion,
  355. Target target,
  356. bool automap = true,
  357. const std::string& entryPointName="",
  358. const std::string& baseDir="/baseResults/",
  359. const bool enableOptimizer = false)
  360. {
  361. const std::string inputFname = testDir + "/" + testName;
  362. const std::string expectedOutputFname =
  363. testDir + baseDir + testName + ".out";
  364. std::string input, expectedOutput;
  365. tryLoadFile(inputFname, "input", &input);
  366. tryLoadFile(expectedOutputFname, "expected output", &expectedOutput);
  367. EShMessages controls = DeriveOptions(source, semantics, target);
  368. if (enableOptimizer)
  369. controls = static_cast<EShMessages>(controls & ~EShMsgHlslLegalization);
  370. GlslangResult result = compileAndLink(testName, input, entryPointName, controls, clientTargetVersion, false,
  371. EShTexSampTransKeep, enableOptimizer, automap);
  372. // Generate the hybrid output in the way of glslangValidator.
  373. std::ostringstream stream;
  374. outputResultToStream(&stream, result, controls);
  375. checkEqAndUpdateIfRequested(expectedOutput, stream.str(),
  376. expectedOutputFname);
  377. }
  378. void loadFileCompileFlattenUniformsAndCheck(const std::string& testDir,
  379. const std::string& testName,
  380. Source source,
  381. Semantics semantics,
  382. Target target,
  383. const std::string& entryPointName="")
  384. {
  385. const std::string inputFname = testDir + "/" + testName;
  386. const std::string expectedOutputFname =
  387. testDir + "/baseResults/" + testName + ".out";
  388. std::string input, expectedOutput;
  389. tryLoadFile(inputFname, "input", &input);
  390. tryLoadFile(expectedOutputFname, "expected output", &expectedOutput);
  391. const EShMessages controls = DeriveOptions(source, semantics, target);
  392. GlslangResult result = compileAndLink(testName, input, entryPointName, controls,
  393. glslang::EShTargetVulkan_1_0, true);
  394. // Generate the hybrid output in the way of glslangValidator.
  395. std::ostringstream stream;
  396. outputResultToStream(&stream, result, controls);
  397. checkEqAndUpdateIfRequested(expectedOutput, stream.str(),
  398. expectedOutputFname);
  399. }
  400. void loadFileCompileIoMapAndCheck(const std::string& testDir,
  401. const std::string& testName,
  402. Source source,
  403. Semantics semantics,
  404. Target target,
  405. const std::string& entryPointName,
  406. int baseSamplerBinding,
  407. int baseTextureBinding,
  408. int baseImageBinding,
  409. int baseUboBinding,
  410. int baseSsboBinding,
  411. bool autoMapBindings,
  412. bool flattenUniformArrays)
  413. {
  414. const std::string inputFname = testDir + "/" + testName;
  415. const std::string expectedOutputFname =
  416. testDir + "/baseResults/" + testName + ".out";
  417. std::string input, expectedOutput;
  418. tryLoadFile(inputFname, "input", &input);
  419. tryLoadFile(expectedOutputFname, "expected output", &expectedOutput);
  420. const EShMessages controls = DeriveOptions(source, semantics, target);
  421. GlslangResult result = compileLinkIoMap(testName, input, entryPointName, controls,
  422. baseSamplerBinding, baseTextureBinding, baseImageBinding,
  423. baseUboBinding, baseSsboBinding,
  424. autoMapBindings,
  425. flattenUniformArrays);
  426. // Generate the hybrid output in the way of glslangValidator.
  427. std::ostringstream stream;
  428. outputResultToStream(&stream, result, controls);
  429. checkEqAndUpdateIfRequested(expectedOutput, stream.str(),
  430. expectedOutputFname);
  431. }
  432. void loadFileCompileRemapAndCheck(const std::string& testDir,
  433. const std::string& testName,
  434. Source source,
  435. Semantics semantics,
  436. Target target,
  437. const std::string& entryPointName="",
  438. const unsigned int remapOptions = spv::spirvbin_t::NONE)
  439. {
  440. const std::string inputFname = testDir + "/" + testName;
  441. const std::string expectedOutputFname =
  442. testDir + "/baseResults/" + testName + ".out";
  443. std::string input, expectedOutput;
  444. tryLoadFile(inputFname, "input", &input);
  445. tryLoadFile(expectedOutputFname, "expected output", &expectedOutput);
  446. const EShMessages controls = DeriveOptions(source, semantics, target);
  447. GlslangResult result = compileLinkRemap(testName, input, entryPointName, controls, remapOptions);
  448. // Generate the hybrid output in the way of glslangValidator.
  449. std::ostringstream stream;
  450. outputResultToStream(&stream, result, controls);
  451. checkEqAndUpdateIfRequested(expectedOutput, stream.str(),
  452. expectedOutputFname);
  453. }
  454. void loadFileRemapAndCheck(const std::string& testDir,
  455. const std::string& testName,
  456. Source source,
  457. Semantics semantics,
  458. Target target,
  459. const unsigned int remapOptions = spv::spirvbin_t::NONE)
  460. {
  461. const std::string inputFname = testDir + "/" + testName;
  462. const std::string expectedOutputFname =
  463. testDir + "/baseResults/" + testName + ".out";
  464. std::vector<std::uint32_t> input;
  465. std::string expectedOutput;
  466. tryLoadSpvFile(inputFname, "input", input);
  467. tryLoadFile(expectedOutputFname, "expected output", &expectedOutput);
  468. const EShMessages controls = DeriveOptions(source, semantics, target);
  469. GlslangResult result = remap(testName, input, controls, remapOptions);
  470. // Generate the hybrid output in the way of glslangValidator.
  471. std::ostringstream stream;
  472. outputResultToStream(&stream, result, controls);
  473. checkEqAndUpdateIfRequested(expectedOutput, stream.str(),
  474. expectedOutputFname);
  475. }
  476. // Preprocesses the given |source| code. On success, returns true, the
  477. // preprocessed shader, and warning messages. Otherwise, returns false, an
  478. // empty string, and error messages.
  479. std::tuple<bool, std::string, std::string> preprocess(
  480. const std::string& source)
  481. {
  482. const char* shaderStrings = source.data();
  483. const int shaderLengths = static_cast<int>(source.size());
  484. glslang::TShader shader(EShLangVertex);
  485. shader.setStringsWithLengths(&shaderStrings, &shaderLengths, 1);
  486. std::string ppShader;
  487. glslang::TShader::ForbidIncluder includer;
  488. const bool success = shader.preprocess(
  489. &glslang::DefaultTBuiltInResource, defaultVersion, defaultProfile,
  490. forceVersionProfile, isForwardCompatible, (EShMessages)(EShMsgOnlyPreprocessor | EShMsgCascadingErrors),
  491. &ppShader, includer);
  492. std::string log = shader.getInfoLog();
  493. log += shader.getInfoDebugLog();
  494. if (success) {
  495. return std::make_tuple(true, ppShader, log);
  496. } else {
  497. return std::make_tuple(false, "", log);
  498. }
  499. }
  500. void loadFilePreprocessAndCheck(const std::string& testDir,
  501. const std::string& testName)
  502. {
  503. const std::string inputFname = testDir + "/" + testName;
  504. const std::string expectedOutputFname =
  505. testDir + "/baseResults/" + testName + ".out";
  506. const std::string expectedErrorFname =
  507. testDir + "/baseResults/" + testName + ".err";
  508. std::string input, expectedOutput, expectedError;
  509. tryLoadFile(inputFname, "input", &input);
  510. tryLoadFile(expectedOutputFname, "expected output", &expectedOutput);
  511. tryLoadFile(expectedErrorFname, "expected error", &expectedError);
  512. bool ppOk;
  513. std::string output, error;
  514. std::tie(ppOk, output, error) = preprocess(input);
  515. if (!output.empty()) output += '\n';
  516. if (!error.empty()) error += '\n';
  517. checkEqAndUpdateIfRequested(expectedOutput, output,
  518. expectedOutputFname);
  519. checkEqAndUpdateIfRequested(expectedError, error,
  520. expectedErrorFname);
  521. }
  522. void loadCompileUpgradeTextureToSampledTextureAndDropSamplersAndCheck(const std::string& testDir,
  523. const std::string& testName,
  524. Source source,
  525. Semantics semantics,
  526. Target target,
  527. const std::string& entryPointName = "")
  528. {
  529. const std::string inputFname = testDir + "/" + testName;
  530. const std::string expectedOutputFname = testDir + "/baseResults/" + testName + ".out";
  531. std::string input, expectedOutput;
  532. tryLoadFile(inputFname, "input", &input);
  533. tryLoadFile(expectedOutputFname, "expected output", &expectedOutput);
  534. const EShMessages controls = DeriveOptions(source, semantics, target);
  535. GlslangResult result = compileAndLink(testName, input, entryPointName, controls,
  536. glslang::EShTargetVulkan_1_0, false,
  537. EShTexSampTransUpgradeTextureRemoveSampler);
  538. // Generate the hybrid output in the way of glslangValidator.
  539. std::ostringstream stream;
  540. outputResultToStream(&stream, result, controls);
  541. checkEqAndUpdateIfRequested(expectedOutput, stream.str(),
  542. expectedOutputFname);
  543. }
  544. private:
  545. const int defaultVersion;
  546. const EProfile defaultProfile;
  547. const bool forceVersionProfile;
  548. const bool isForwardCompatible;
  549. };
  550. } // namespace glslangtest
  551. #endif // GLSLANG_GTESTS_TEST_FIXTURE_H