Spv.FromFile.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  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. #include <algorithm>
  35. #include <gtest/gtest.h>
  36. #include "TestFixture.h"
  37. namespace glslangtest {
  38. namespace {
  39. struct IoMapData {
  40. const char* fileName;
  41. const char* entryPoint;
  42. int baseSamplerBinding;
  43. int baseTextureBinding;
  44. int baseImageBinding;
  45. int baseUboBinding;
  46. int baseSsboBinding;
  47. bool autoMapBindings;
  48. bool flattenUniforms;
  49. };
  50. std::string FileNameAsCustomTestSuffixIoMap(
  51. const ::testing::TestParamInfo<IoMapData>& info) {
  52. std::string name = info.param.fileName;
  53. // A valid test case suffix cannot have '.' and '-' inside.
  54. std::replace(name.begin(), name.end(), '.', '_');
  55. std::replace(name.begin(), name.end(), '-', '_');
  56. return name;
  57. }
  58. using CompileVulkanToSpirvTest = GlslangTest<::testing::TestWithParam<std::string>>;
  59. using CompileOpenGLToSpirvTest = GlslangTest<::testing::TestWithParam<std::string>>;
  60. using VulkanSemantics = GlslangTest<::testing::TestWithParam<std::string>>;
  61. using OpenGLSemantics = GlslangTest<::testing::TestWithParam<std::string>>;
  62. using VulkanAstSemantics = GlslangTest<::testing::TestWithParam<std::string>>;
  63. using HlslIoMap = GlslangTest<::testing::TestWithParam<IoMapData>>;
  64. using GlslIoMap = GlslangTest<::testing::TestWithParam<IoMapData>>;
  65. #ifdef AMD_EXTENSIONS
  66. using CompileVulkanToSpirvTestAMD = GlslangTest<::testing::TestWithParam<std::string>>;
  67. #endif
  68. #ifdef NV_EXTENSIONS
  69. using CompileVulkanToSpirvTestNV = GlslangTest<::testing::TestWithParam<std::string>>;
  70. #endif
  71. using CompileUpgradeTextureToSampledTextureAndDropSamplersTest = GlslangTest<::testing::TestWithParam<std::string>>;
  72. // Compiling GLSL to SPIR-V under Vulkan semantics. Expected to successfully
  73. // generate SPIR-V.
  74. TEST_P(CompileVulkanToSpirvTest, FromFile)
  75. {
  76. loadFileCompileAndCheck(GlobalTestSettings.testRoot, GetParam(),
  77. Source::GLSL, Semantics::Vulkan,
  78. Target::Spv);
  79. }
  80. // Compiling GLSL to SPIR-V under OpenGL semantics. Expected to successfully
  81. // generate SPIR-V.
  82. TEST_P(CompileOpenGLToSpirvTest, FromFile)
  83. {
  84. loadFileCompileAndCheck(GlobalTestSettings.testRoot, GetParam(),
  85. Source::GLSL, Semantics::OpenGL,
  86. Target::Spv);
  87. }
  88. // GLSL-level Vulkan semantics test. Expected to error out before generating
  89. // SPIR-V.
  90. TEST_P(VulkanSemantics, FromFile)
  91. {
  92. loadFileCompileAndCheck(GlobalTestSettings.testRoot, GetParam(),
  93. Source::GLSL, Semantics::Vulkan,
  94. Target::Spv);
  95. }
  96. // GLSL-level Vulkan semantics test. Expected to error out before generating
  97. // SPIR-V.
  98. TEST_P(OpenGLSemantics, FromFile)
  99. {
  100. loadFileCompileAndCheck(GlobalTestSettings.testRoot, GetParam(),
  101. Source::GLSL, Semantics::OpenGL,
  102. Target::Spv);
  103. }
  104. // GLSL-level Vulkan semantics test that need to see the AST for validation.
  105. TEST_P(VulkanAstSemantics, FromFile)
  106. {
  107. loadFileCompileAndCheck(GlobalTestSettings.testRoot, GetParam(),
  108. Source::GLSL, Semantics::Vulkan,
  109. Target::AST);
  110. }
  111. // HLSL-level Vulkan semantics tests.
  112. TEST_P(HlslIoMap, FromFile)
  113. {
  114. loadFileCompileIoMapAndCheck(GlobalTestSettings.testRoot, GetParam().fileName,
  115. Source::HLSL, Semantics::Vulkan,
  116. Target::Spv, GetParam().entryPoint,
  117. GetParam().baseSamplerBinding,
  118. GetParam().baseTextureBinding,
  119. GetParam().baseImageBinding,
  120. GetParam().baseUboBinding,
  121. GetParam().baseSsboBinding,
  122. GetParam().autoMapBindings,
  123. GetParam().flattenUniforms);
  124. }
  125. // GLSL-level Vulkan semantics tests.
  126. TEST_P(GlslIoMap, FromFile)
  127. {
  128. loadFileCompileIoMapAndCheck(GlobalTestSettings.testRoot, GetParam().fileName,
  129. Source::GLSL, Semantics::Vulkan,
  130. Target::Spv, GetParam().entryPoint,
  131. GetParam().baseSamplerBinding,
  132. GetParam().baseTextureBinding,
  133. GetParam().baseImageBinding,
  134. GetParam().baseUboBinding,
  135. GetParam().baseSsboBinding,
  136. GetParam().autoMapBindings,
  137. GetParam().flattenUniforms);
  138. }
  139. #ifdef AMD_EXTENSIONS
  140. // Compiling GLSL to SPIR-V under Vulkan semantics (AMD extensions enabled).
  141. // Expected to successfully generate SPIR-V.
  142. TEST_P(CompileVulkanToSpirvTestAMD, FromFile)
  143. {
  144. loadFileCompileAndCheck(GlobalTestSettings.testRoot, GetParam(),
  145. Source::GLSL, Semantics::Vulkan,
  146. Target::Spv);
  147. }
  148. #endif
  149. #ifdef NV_EXTENSIONS
  150. // Compiling GLSL to SPIR-V under Vulkan semantics (AMD extensions enabled).
  151. // Expected to successfully generate SPIR-V.
  152. TEST_P(CompileVulkanToSpirvTestNV, FromFile)
  153. {
  154. loadFileCompileAndCheck(GlobalTestSettings.testRoot, GetParam(),
  155. Source::GLSL, Semantics::Vulkan,
  156. Target::Spv);
  157. }
  158. #endif
  159. TEST_P(CompileUpgradeTextureToSampledTextureAndDropSamplersTest, FromFile)
  160. {
  161. loadCompileUpgradeTextureToSampledTextureAndDropSamplersAndCheck(GlobalTestSettings.testRoot,
  162. GetParam(),
  163. Source::GLSL,
  164. Semantics::Vulkan,
  165. Target::Spv);
  166. }
  167. // clang-format off
  168. INSTANTIATE_TEST_CASE_P(
  169. Glsl, CompileVulkanToSpirvTest,
  170. ::testing::ValuesIn(std::vector<std::string>({
  171. // Test looping constructs.
  172. // No tests yet for making sure break and continue from a nested loop
  173. // goes to the innermost target.
  174. "spv.barrier.vert",
  175. "spv.do-simple.vert",
  176. "spv.do-while-continue-break.vert",
  177. "spv.for-complex-condition.vert",
  178. "spv.for-continue-break.vert",
  179. "spv.for-simple.vert",
  180. "spv.for-notest.vert",
  181. "spv.for-nobody.vert",
  182. "spv.while-continue-break.vert",
  183. "spv.while-simple.vert",
  184. // vulkan-specific tests
  185. "spv.set.vert",
  186. "spv.double.comp",
  187. "spv.100ops.frag",
  188. "spv.130.frag",
  189. "spv.140.frag",
  190. "spv.150.geom",
  191. "spv.150.vert",
  192. "spv.300BuiltIns.vert",
  193. "spv.300layout.frag",
  194. "spv.300layout.vert",
  195. "spv.300layoutp.vert",
  196. "spv.310.comp",
  197. "spv.310.bitcast.frag",
  198. "spv.330.geom",
  199. "spv.400.frag",
  200. "spv.400.tesc",
  201. "spv.400.tese",
  202. "spv.420.geom",
  203. "spv.430.frag",
  204. "spv.430.vert",
  205. "spv.450.tesc",
  206. "spv.450.geom",
  207. "spv.450.noRedecl.tesc",
  208. "spv.accessChain.frag",
  209. "spv.aggOps.frag",
  210. "spv.always-discard.frag",
  211. "spv.always-discard2.frag",
  212. "spv.arbPostDepthCoverage.frag",
  213. "spv.arbPostDepthCoverage_Error.frag",
  214. "spv.bitCast.frag",
  215. "spv.bool.vert",
  216. "spv.boolInBlock.frag",
  217. "spv.branch-return.vert",
  218. "spv.builtInXFB.vert",
  219. "spv.conditionalDiscard.frag",
  220. "spv.controlFlowAttributes.frag",
  221. "spv.conversion.frag",
  222. "spv.dataOut.frag",
  223. "spv.dataOutIndirect.frag",
  224. "spv.dataOutIndirect.vert",
  225. "spv.deepRvalue.frag",
  226. "spv.depthOut.frag",
  227. "spv.deviceGroup.frag",
  228. "spv.discard-dce.frag",
  229. "spv.drawParams.vert",
  230. "spv.doWhileLoop.frag",
  231. "spv.earlyReturnDiscard.frag",
  232. "spv.extPostDepthCoverage.frag",
  233. "spv.extPostDepthCoverage_Error.frag",
  234. "spv.flowControl.frag",
  235. "spv.forLoop.frag",
  236. "spv.forwardFun.frag",
  237. "spv.fullyCovered.frag",
  238. "spv.functionCall.frag",
  239. "spv.functionNestedOpaque.vert",
  240. "spv.functionSemantics.frag",
  241. "spv.GeometryShaderPassthrough.geom",
  242. "spv.interpOps.frag",
  243. "spv.int64.frag",
  244. "spv.intOps.vert",
  245. "spv.layoutNested.vert",
  246. "spv.length.frag",
  247. "spv.localAggregates.frag",
  248. "spv.loops.frag",
  249. "spv.loopsArtificial.frag",
  250. "spv.matFun.vert",
  251. "spv.matrix.frag",
  252. "spv.matrix2.frag",
  253. "spv.memoryQualifier.frag",
  254. "spv.merge-unreachable.frag",
  255. "spv.multiStruct.comp",
  256. "spv.multiStructFuncall.frag",
  257. "spv.multiView.frag",
  258. "spv.newTexture.frag",
  259. "spv.noDeadDecorations.vert",
  260. "spv.nonSquare.vert",
  261. "spv.noWorkgroup.comp",
  262. "spv.offsets.frag",
  263. "spv.Operations.frag",
  264. "spv.paramMemory.frag",
  265. "spv.precision.frag",
  266. "spv.precisionNonESSamp.frag",
  267. "spv.prepost.frag",
  268. "spv.qualifiers.vert",
  269. "spv.sample.frag",
  270. "spv.sampleId.frag",
  271. "spv.samplePosition.frag",
  272. "spv.sampleMaskOverrideCoverage.frag",
  273. "spv.shaderBallot.comp",
  274. "spv.shaderDrawParams.vert",
  275. "spv.shaderGroupVote.comp",
  276. "spv.shaderStencilExport.frag",
  277. "spv.shiftOps.frag",
  278. "spv.simpleFunctionCall.frag",
  279. "spv.simpleMat.vert",
  280. "spv.sparseTexture.frag",
  281. "spv.sparseTextureClamp.frag",
  282. "spv.structAssignment.frag",
  283. "spv.structDeref.frag",
  284. "spv.structure.frag",
  285. "spv.switch.frag",
  286. "spv.swizzle.frag",
  287. "spv.swizzleInversion.frag",
  288. "spv.test.frag",
  289. "spv.test.vert",
  290. "spv.texture.frag",
  291. "spv.texture.vert",
  292. "spv.textureBuffer.vert",
  293. "spv.image.frag",
  294. "spv.types.frag",
  295. "spv.uint.frag",
  296. "spv.uniformArray.frag",
  297. "spv.variableArrayIndex.frag",
  298. "spv.varyingArray.frag",
  299. "spv.varyingArrayIndirect.frag",
  300. "spv.voidFunction.frag",
  301. "spv.whileLoop.frag",
  302. "spv.AofA.frag",
  303. "spv.queryL.frag",
  304. "spv.separate.frag",
  305. "spv.shortCircuit.frag",
  306. "spv.pushConstant.vert",
  307. "spv.pushConstantAnon.vert",
  308. "spv.subpass.frag",
  309. "spv.specConstant.vert",
  310. "spv.specConstant.comp",
  311. "spv.specConstantComposite.vert",
  312. "spv.specConstantOperations.vert",
  313. "spv.storageBuffer.vert",
  314. "spv.precise.tese",
  315. "spv.precise.tesc",
  316. "spv.xfb.vert",
  317. "spv.xfb2.vert",
  318. "spv.xfb3.vert",
  319. })),
  320. FileNameAsCustomTestSuffix
  321. );
  322. // clang-format off
  323. INSTANTIATE_TEST_CASE_P(
  324. Hlsl, HlslIoMap,
  325. ::testing::ValuesIn(std::vector<IoMapData>{
  326. { "spv.register.autoassign.frag", "main_ep", 5, 10, 0, 20, 30, true, false },
  327. { "spv.register.noautoassign.frag", "main_ep", 5, 10, 0, 15, 30, false, false },
  328. { "spv.register.autoassign-2.frag", "main", 5, 10, 0, 15, 30, true, true },
  329. { "spv.register.subpass.frag", "main", 0, 20, 0, 0, 0, true, true },
  330. { "spv.buffer.autoassign.frag", "main", 5, 10, 0, 15, 30, true, true },
  331. { "spv.ssbo.autoassign.frag", "main", 5, 10, 0, 15, 30, true, true },
  332. { "spv.ssboAlias.frag", "main", 0, 0, 0, 0, 83, true, false },
  333. { "spv.rw.autoassign.frag", "main", 5, 10, 20, 15, 30, true, true },
  334. { "spv.register.autoassign.rangetest.frag", "main",
  335. glslang::TQualifier::layoutBindingEnd-2,
  336. glslang::TQualifier::layoutBindingEnd+5,
  337. 20, 30, true, false },
  338. }),
  339. FileNameAsCustomTestSuffixIoMap
  340. );
  341. // clang-format off
  342. INSTANTIATE_TEST_CASE_P(
  343. Hlsl, GlslIoMap,
  344. ::testing::ValuesIn(std::vector<IoMapData>{
  345. { "spv.glsl.register.autoassign.frag", "main", 5, 10, 0, 20, 30, true, false },
  346. { "spv.glsl.register.noautoassign.frag", "main", 5, 10, 0, 15, 30, false, false },
  347. }),
  348. FileNameAsCustomTestSuffixIoMap
  349. );
  350. // clang-format off
  351. INSTANTIATE_TEST_CASE_P(
  352. Glsl, CompileOpenGLToSpirvTest,
  353. ::testing::ValuesIn(std::vector<std::string>({
  354. "spv.460.frag",
  355. "spv.460.vert",
  356. "spv.460.comp",
  357. "spv.atomic.comp",
  358. "spv.glFragColor.frag",
  359. "spv.specConst.vert",
  360. "spv.OVR_multiview.vert",
  361. })),
  362. FileNameAsCustomTestSuffix
  363. );
  364. INSTANTIATE_TEST_CASE_P(
  365. Glsl, VulkanSemantics,
  366. ::testing::ValuesIn(std::vector<std::string>({
  367. "vulkan.frag",
  368. "vulkan.vert",
  369. "vulkan.comp",
  370. })),
  371. FileNameAsCustomTestSuffix
  372. );
  373. INSTANTIATE_TEST_CASE_P(
  374. Glsl, OpenGLSemantics,
  375. ::testing::ValuesIn(std::vector<std::string>({
  376. "glspv.esversion.vert",
  377. "glspv.version.frag",
  378. "glspv.version.vert",
  379. "glspv.frag",
  380. "glspv.vert",
  381. })),
  382. FileNameAsCustomTestSuffix
  383. );
  384. INSTANTIATE_TEST_CASE_P(
  385. Glsl, VulkanAstSemantics,
  386. ::testing::ValuesIn(std::vector<std::string>({
  387. "vulkan.ast.vert",
  388. })),
  389. FileNameAsCustomTestSuffix
  390. );
  391. #ifdef AMD_EXTENSIONS
  392. INSTANTIATE_TEST_CASE_P(
  393. Glsl, CompileVulkanToSpirvTestAMD,
  394. ::testing::ValuesIn(std::vector<std::string>({
  395. "spv.float16.frag",
  396. "spv.imageLoadStoreLod.frag",
  397. "spv.int16.frag",
  398. "spv.shaderBallotAMD.comp",
  399. "spv.shaderFragMaskAMD.frag",
  400. "spv.textureGatherBiasLod.frag",
  401. })),
  402. FileNameAsCustomTestSuffix
  403. );
  404. #endif
  405. #ifdef NV_EXTENSIONS
  406. INSTANTIATE_TEST_CASE_P(
  407. Glsl, CompileVulkanToSpirvTestNV,
  408. ::testing::ValuesIn(std::vector<std::string>({
  409. "spv.sampleMaskOverrideCoverage.frag",
  410. "spv.GeometryShaderPassthrough.geom",
  411. "spv.viewportArray2.vert",
  412. "spv.viewportArray2.tesc",
  413. "spv.stereoViewRendering.vert",
  414. "spv.stereoViewRendering.tesc",
  415. "spv.multiviewPerViewAttributes.vert",
  416. "spv.multiviewPerViewAttributes.tesc",
  417. "spv.atomicInt64.comp",
  418. })),
  419. FileNameAsCustomTestSuffix
  420. );
  421. #endif
  422. INSTANTIATE_TEST_CASE_P(
  423. Glsl, CompileUpgradeTextureToSampledTextureAndDropSamplersTest,
  424. ::testing::ValuesIn(std::vector<std::string>({
  425. "spv.texture.sampler.transform.frag",
  426. })),
  427. FileNameAsCustomTestSuffix
  428. );
  429. // clang-format on
  430. } // anonymous namespace
  431. } // namespace glslangtest