PipelineState.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621
  1. using Silk.NET.Vulkan;
  2. using System;
  3. namespace Ryujinx.Graphics.Vulkan
  4. {
  5. struct PipelineState : IDisposable
  6. {
  7. private const int RequiredSubgroupSize = 32;
  8. public PipelineUid Internal;
  9. public float LineWidth
  10. {
  11. get => BitConverter.Int32BitsToSingle((int)((Internal.Id0 >> 0) & 0xFFFFFFFF));
  12. set => Internal.Id0 = (Internal.Id0 & 0xFFFFFFFF00000000) | ((ulong)(uint)BitConverter.SingleToInt32Bits(value) << 0);
  13. }
  14. public float DepthBiasClamp
  15. {
  16. get => BitConverter.Int32BitsToSingle((int)((Internal.Id0 >> 32) & 0xFFFFFFFF));
  17. set => Internal.Id0 = (Internal.Id0 & 0xFFFFFFFF) | ((ulong)(uint)BitConverter.SingleToInt32Bits(value) << 32);
  18. }
  19. public float DepthBiasConstantFactor
  20. {
  21. get => BitConverter.Int32BitsToSingle((int)((Internal.Id1 >> 0) & 0xFFFFFFFF));
  22. set => Internal.Id1 = (Internal.Id1 & 0xFFFFFFFF00000000) | ((ulong)(uint)BitConverter.SingleToInt32Bits(value) << 0);
  23. }
  24. public float DepthBiasSlopeFactor
  25. {
  26. get => BitConverter.Int32BitsToSingle((int)((Internal.Id1 >> 32) & 0xFFFFFFFF));
  27. set => Internal.Id1 = (Internal.Id1 & 0xFFFFFFFF) | ((ulong)(uint)BitConverter.SingleToInt32Bits(value) << 32);
  28. }
  29. public uint StencilFrontCompareMask
  30. {
  31. get => (uint)((Internal.Id2 >> 0) & 0xFFFFFFFF);
  32. set => Internal.Id2 = (Internal.Id2 & 0xFFFFFFFF00000000) | ((ulong)value << 0);
  33. }
  34. public uint StencilFrontWriteMask
  35. {
  36. get => (uint)((Internal.Id2 >> 32) & 0xFFFFFFFF);
  37. set => Internal.Id2 = (Internal.Id2 & 0xFFFFFFFF) | ((ulong)value << 32);
  38. }
  39. public uint StencilFrontReference
  40. {
  41. get => (uint)((Internal.Id3 >> 0) & 0xFFFFFFFF);
  42. set => Internal.Id3 = (Internal.Id3 & 0xFFFFFFFF00000000) | ((ulong)value << 0);
  43. }
  44. public uint StencilBackCompareMask
  45. {
  46. get => (uint)((Internal.Id3 >> 32) & 0xFFFFFFFF);
  47. set => Internal.Id3 = (Internal.Id3 & 0xFFFFFFFF) | ((ulong)value << 32);
  48. }
  49. public uint StencilBackWriteMask
  50. {
  51. get => (uint)((Internal.Id4 >> 0) & 0xFFFFFFFF);
  52. set => Internal.Id4 = (Internal.Id4 & 0xFFFFFFFF00000000) | ((ulong)value << 0);
  53. }
  54. public uint StencilBackReference
  55. {
  56. get => (uint)((Internal.Id4 >> 32) & 0xFFFFFFFF);
  57. set => Internal.Id4 = (Internal.Id4 & 0xFFFFFFFF) | ((ulong)value << 32);
  58. }
  59. public float MinDepthBounds
  60. {
  61. get => BitConverter.Int32BitsToSingle((int)((Internal.Id5 >> 0) & 0xFFFFFFFF));
  62. set => Internal.Id5 = (Internal.Id5 & 0xFFFFFFFF00000000) | ((ulong)(uint)BitConverter.SingleToInt32Bits(value) << 0);
  63. }
  64. public float MaxDepthBounds
  65. {
  66. get => BitConverter.Int32BitsToSingle((int)((Internal.Id5 >> 32) & 0xFFFFFFFF));
  67. set => Internal.Id5 = (Internal.Id5 & 0xFFFFFFFF) | ((ulong)(uint)BitConverter.SingleToInt32Bits(value) << 32);
  68. }
  69. public PolygonMode PolygonMode
  70. {
  71. get => (PolygonMode)((Internal.Id6 >> 0) & 0x3FFFFFFF);
  72. set => Internal.Id6 = (Internal.Id6 & 0xFFFFFFFFC0000000) | ((ulong)value << 0);
  73. }
  74. public uint StagesCount
  75. {
  76. get => (byte)((Internal.Id6 >> 30) & 0xFF);
  77. set => Internal.Id6 = (Internal.Id6 & 0xFFFFFFC03FFFFFFF) | ((ulong)value << 30);
  78. }
  79. public uint VertexAttributeDescriptionsCount
  80. {
  81. get => (byte)((Internal.Id6 >> 38) & 0xFF);
  82. set => Internal.Id6 = (Internal.Id6 & 0xFFFFC03FFFFFFFFF) | ((ulong)value << 38);
  83. }
  84. public uint VertexBindingDescriptionsCount
  85. {
  86. get => (byte)((Internal.Id6 >> 46) & 0xFF);
  87. set => Internal.Id6 = (Internal.Id6 & 0xFFC03FFFFFFFFFFF) | ((ulong)value << 46);
  88. }
  89. public uint ViewportsCount
  90. {
  91. get => (byte)((Internal.Id6 >> 54) & 0xFF);
  92. set => Internal.Id6 = (Internal.Id6 & 0xC03FFFFFFFFFFFFF) | ((ulong)value << 54);
  93. }
  94. public uint ScissorsCount
  95. {
  96. get => (byte)((Internal.Id7 >> 0) & 0xFF);
  97. set => Internal.Id7 = (Internal.Id7 & 0xFFFFFFFFFFFFFF00) | ((ulong)value << 0);
  98. }
  99. public uint ColorBlendAttachmentStateCount
  100. {
  101. get => (byte)((Internal.Id7 >> 8) & 0xFF);
  102. set => Internal.Id7 = (Internal.Id7 & 0xFFFFFFFFFFFF00FF) | ((ulong)value << 8);
  103. }
  104. public PrimitiveTopology Topology
  105. {
  106. get => (PrimitiveTopology)((Internal.Id7 >> 16) & 0xF);
  107. set => Internal.Id7 = (Internal.Id7 & 0xFFFFFFFFFFF0FFFF) | ((ulong)value << 16);
  108. }
  109. public LogicOp LogicOp
  110. {
  111. get => (LogicOp)((Internal.Id7 >> 20) & 0xF);
  112. set => Internal.Id7 = (Internal.Id7 & 0xFFFFFFFFFF0FFFFF) | ((ulong)value << 20);
  113. }
  114. public CompareOp DepthCompareOp
  115. {
  116. get => (CompareOp)((Internal.Id7 >> 24) & 0x7);
  117. set => Internal.Id7 = (Internal.Id7 & 0xFFFFFFFFF8FFFFFF) | ((ulong)value << 24);
  118. }
  119. public StencilOp StencilFrontFailOp
  120. {
  121. get => (StencilOp)((Internal.Id7 >> 27) & 0x7);
  122. set => Internal.Id7 = (Internal.Id7 & 0xFFFFFFFFC7FFFFFF) | ((ulong)value << 27);
  123. }
  124. public StencilOp StencilFrontPassOp
  125. {
  126. get => (StencilOp)((Internal.Id7 >> 30) & 0x7);
  127. set => Internal.Id7 = (Internal.Id7 & 0xFFFFFFFE3FFFFFFF) | ((ulong)value << 30);
  128. }
  129. public StencilOp StencilFrontDepthFailOp
  130. {
  131. get => (StencilOp)((Internal.Id7 >> 33) & 0x7);
  132. set => Internal.Id7 = (Internal.Id7 & 0xFFFFFFF1FFFFFFFF) | ((ulong)value << 33);
  133. }
  134. public CompareOp StencilFrontCompareOp
  135. {
  136. get => (CompareOp)((Internal.Id7 >> 36) & 0x7);
  137. set => Internal.Id7 = (Internal.Id7 & 0xFFFFFF8FFFFFFFFF) | ((ulong)value << 36);
  138. }
  139. public StencilOp StencilBackFailOp
  140. {
  141. get => (StencilOp)((Internal.Id7 >> 39) & 0x7);
  142. set => Internal.Id7 = (Internal.Id7 & 0xFFFFFC7FFFFFFFFF) | ((ulong)value << 39);
  143. }
  144. public StencilOp StencilBackPassOp
  145. {
  146. get => (StencilOp)((Internal.Id7 >> 42) & 0x7);
  147. set => Internal.Id7 = (Internal.Id7 & 0xFFFFE3FFFFFFFFFF) | ((ulong)value << 42);
  148. }
  149. public StencilOp StencilBackDepthFailOp
  150. {
  151. get => (StencilOp)((Internal.Id7 >> 45) & 0x7);
  152. set => Internal.Id7 = (Internal.Id7 & 0xFFFF1FFFFFFFFFFF) | ((ulong)value << 45);
  153. }
  154. public CompareOp StencilBackCompareOp
  155. {
  156. get => (CompareOp)((Internal.Id7 >> 48) & 0x7);
  157. set => Internal.Id7 = (Internal.Id7 & 0xFFF8FFFFFFFFFFFF) | ((ulong)value << 48);
  158. }
  159. public CullModeFlags CullMode
  160. {
  161. get => (CullModeFlags)((Internal.Id7 >> 51) & 0x3);
  162. set => Internal.Id7 = (Internal.Id7 & 0xFFE7FFFFFFFFFFFF) | ((ulong)value << 51);
  163. }
  164. public bool PrimitiveRestartEnable
  165. {
  166. get => ((Internal.Id7 >> 53) & 0x1) != 0UL;
  167. set => Internal.Id7 = (Internal.Id7 & 0xFFDFFFFFFFFFFFFF) | ((value ? 1UL : 0UL) << 53);
  168. }
  169. public bool DepthClampEnable
  170. {
  171. get => ((Internal.Id7 >> 54) & 0x1) != 0UL;
  172. set => Internal.Id7 = (Internal.Id7 & 0xFFBFFFFFFFFFFFFF) | ((value ? 1UL : 0UL) << 54);
  173. }
  174. public bool RasterizerDiscardEnable
  175. {
  176. get => ((Internal.Id7 >> 55) & 0x1) != 0UL;
  177. set => Internal.Id7 = (Internal.Id7 & 0xFF7FFFFFFFFFFFFF) | ((value ? 1UL : 0UL) << 55);
  178. }
  179. public FrontFace FrontFace
  180. {
  181. get => (FrontFace)((Internal.Id7 >> 56) & 0x1);
  182. set => Internal.Id7 = (Internal.Id7 & 0xFEFFFFFFFFFFFFFF) | ((ulong)value << 56);
  183. }
  184. public bool DepthBiasEnable
  185. {
  186. get => ((Internal.Id7 >> 57) & 0x1) != 0UL;
  187. set => Internal.Id7 = (Internal.Id7 & 0xFDFFFFFFFFFFFFFF) | ((value ? 1UL : 0UL) << 57);
  188. }
  189. public bool DepthTestEnable
  190. {
  191. get => ((Internal.Id7 >> 58) & 0x1) != 0UL;
  192. set => Internal.Id7 = (Internal.Id7 & 0xFBFFFFFFFFFFFFFF) | ((value ? 1UL : 0UL) << 58);
  193. }
  194. public bool DepthWriteEnable
  195. {
  196. get => ((Internal.Id7 >> 59) & 0x1) != 0UL;
  197. set => Internal.Id7 = (Internal.Id7 & 0xF7FFFFFFFFFFFFFF) | ((value ? 1UL : 0UL) << 59);
  198. }
  199. public bool DepthBoundsTestEnable
  200. {
  201. get => ((Internal.Id7 >> 60) & 0x1) != 0UL;
  202. set => Internal.Id7 = (Internal.Id7 & 0xEFFFFFFFFFFFFFFF) | ((value ? 1UL : 0UL) << 60);
  203. }
  204. public bool StencilTestEnable
  205. {
  206. get => ((Internal.Id7 >> 61) & 0x1) != 0UL;
  207. set => Internal.Id7 = (Internal.Id7 & 0xDFFFFFFFFFFFFFFF) | ((value ? 1UL : 0UL) << 61);
  208. }
  209. public bool LogicOpEnable
  210. {
  211. get => ((Internal.Id7 >> 62) & 0x1) != 0UL;
  212. set => Internal.Id7 = (Internal.Id7 & 0xBFFFFFFFFFFFFFFF) | ((value ? 1UL : 0UL) << 62);
  213. }
  214. public bool HasDepthStencil
  215. {
  216. get => ((Internal.Id7 >> 63) & 0x1) != 0UL;
  217. set => Internal.Id7 = (Internal.Id7 & 0x7FFFFFFFFFFFFFFF) | ((value ? 1UL : 0UL) << 63);
  218. }
  219. public uint PatchControlPoints
  220. {
  221. get => (uint)((Internal.Id8 >> 0) & 0xFFFFFFFF);
  222. set => Internal.Id8 = (Internal.Id8 & 0xFFFFFFFF00000000) | ((ulong)value << 0);
  223. }
  224. public uint SamplesCount
  225. {
  226. get => (uint)((Internal.Id8 >> 32) & 0xFFFFFFFF);
  227. set => Internal.Id8 = (Internal.Id8 & 0xFFFFFFFF) | ((ulong)value << 32);
  228. }
  229. public bool AlphaToCoverageEnable
  230. {
  231. get => ((Internal.Id9 >> 0) & 0x1) != 0UL;
  232. set => Internal.Id9 = (Internal.Id9 & 0xFFFFFFFFFFFFFFFE) | ((value ? 1UL : 0UL) << 0);
  233. }
  234. public bool AlphaToOneEnable
  235. {
  236. get => ((Internal.Id9 >> 1) & 0x1) != 0UL;
  237. set => Internal.Id9 = (Internal.Id9 & 0xFFFFFFFFFFFFFFFD) | ((value ? 1UL : 0UL) << 1);
  238. }
  239. public bool AdvancedBlendSrcPreMultiplied
  240. {
  241. get => ((Internal.Id9 >> 2) & 0x1) != 0UL;
  242. set => Internal.Id9 = (Internal.Id9 & 0xFFFFFFFFFFFFFFFB) | ((value ? 1UL : 0UL) << 2);
  243. }
  244. public bool AdvancedBlendDstPreMultiplied
  245. {
  246. get => ((Internal.Id9 >> 3) & 0x1) != 0UL;
  247. set => Internal.Id9 = (Internal.Id9 & 0xFFFFFFFFFFFFFFF7) | ((value ? 1UL : 0UL) << 3);
  248. }
  249. public BlendOverlapEXT AdvancedBlendOverlap
  250. {
  251. get => (BlendOverlapEXT)((Internal.Id9 >> 4) & 0x3);
  252. set => Internal.Id9 = (Internal.Id9 & 0xFFFFFFFFFFFFFFCF) | ((ulong)value << 4);
  253. }
  254. public NativeArray<PipelineShaderStageCreateInfo> Stages;
  255. public NativeArray<PipelineShaderStageRequiredSubgroupSizeCreateInfoEXT> StageRequiredSubgroupSizes;
  256. public PipelineLayout PipelineLayout;
  257. public SpecData SpecializationData;
  258. public void Initialize()
  259. {
  260. Stages = new NativeArray<PipelineShaderStageCreateInfo>(Constants.MaxShaderStages);
  261. StageRequiredSubgroupSizes = new NativeArray<PipelineShaderStageRequiredSubgroupSizeCreateInfoEXT>(Constants.MaxShaderStages);
  262. for (int index = 0; index < Constants.MaxShaderStages; index++)
  263. {
  264. StageRequiredSubgroupSizes[index] = new PipelineShaderStageRequiredSubgroupSizeCreateInfoEXT()
  265. {
  266. SType = StructureType.PipelineShaderStageRequiredSubgroupSizeCreateInfoExt,
  267. RequiredSubgroupSize = RequiredSubgroupSize
  268. };
  269. }
  270. AdvancedBlendSrcPreMultiplied = true;
  271. AdvancedBlendDstPreMultiplied = true;
  272. AdvancedBlendOverlap = BlendOverlapEXT.UncorrelatedExt;
  273. LineWidth = 1f;
  274. SamplesCount = 1;
  275. }
  276. public unsafe Auto<DisposablePipeline> CreateComputePipeline(
  277. VulkanRenderer gd,
  278. Device device,
  279. ShaderCollection program,
  280. PipelineCache cache)
  281. {
  282. if (program.TryGetComputePipeline(ref SpecializationData, out var pipeline))
  283. {
  284. return pipeline;
  285. }
  286. if (gd.Capabilities.SupportsSubgroupSizeControl)
  287. {
  288. UpdateStageRequiredSubgroupSizes(gd, 1);
  289. }
  290. var pipelineCreateInfo = new ComputePipelineCreateInfo()
  291. {
  292. SType = StructureType.ComputePipelineCreateInfo,
  293. Stage = Stages[0],
  294. BasePipelineIndex = -1,
  295. Layout = PipelineLayout
  296. };
  297. Pipeline pipelineHandle = default;
  298. bool hasSpec = program.SpecDescriptions != null;
  299. var desc = hasSpec ? program.SpecDescriptions[0] : SpecDescription.Empty;
  300. if (hasSpec && SpecializationData.Length < (int)desc.Info.DataSize)
  301. {
  302. throw new InvalidOperationException("Specialization data size does not match description");
  303. }
  304. fixed (SpecializationInfo* info = &desc.Info)
  305. fixed (SpecializationMapEntry* map = desc.Map)
  306. fixed (byte* data = SpecializationData.Span)
  307. {
  308. if (hasSpec)
  309. {
  310. info->PMapEntries = map;
  311. info->PData = data;
  312. pipelineCreateInfo.Stage.PSpecializationInfo = info;
  313. }
  314. gd.Api.CreateComputePipelines(device, cache, 1, &pipelineCreateInfo, null, &pipelineHandle).ThrowOnError();
  315. }
  316. pipeline = new Auto<DisposablePipeline>(new DisposablePipeline(gd.Api, device, pipelineHandle));
  317. program.AddComputePipeline(ref SpecializationData, pipeline);
  318. return pipeline;
  319. }
  320. public unsafe Auto<DisposablePipeline> CreateGraphicsPipeline(
  321. VulkanRenderer gd,
  322. Device device,
  323. ShaderCollection program,
  324. PipelineCache cache,
  325. RenderPass renderPass)
  326. {
  327. if (program.TryGetGraphicsPipeline(ref Internal, out var pipeline))
  328. {
  329. return pipeline;
  330. }
  331. Pipeline pipelineHandle = default;
  332. fixed (VertexInputAttributeDescription* pVertexAttributeDescriptions = &Internal.VertexAttributeDescriptions[0])
  333. fixed (VertexInputBindingDescription* pVertexBindingDescriptions = &Internal.VertexBindingDescriptions[0])
  334. fixed (Viewport* pViewports = &Internal.Viewports[0])
  335. fixed (Rect2D* pScissors = &Internal.Scissors[0])
  336. fixed (PipelineColorBlendAttachmentState* pColorBlendAttachmentState = &Internal.ColorBlendAttachmentState[0])
  337. {
  338. var vertexInputState = new PipelineVertexInputStateCreateInfo
  339. {
  340. SType = StructureType.PipelineVertexInputStateCreateInfo,
  341. VertexAttributeDescriptionCount = VertexAttributeDescriptionsCount,
  342. PVertexAttributeDescriptions = pVertexAttributeDescriptions,
  343. VertexBindingDescriptionCount = VertexBindingDescriptionsCount,
  344. PVertexBindingDescriptions = pVertexBindingDescriptions
  345. };
  346. bool primitiveRestartEnable = PrimitiveRestartEnable;
  347. bool topologySupportsRestart;
  348. if (gd.Capabilities.SupportsPrimitiveTopologyListRestart)
  349. {
  350. topologySupportsRestart = gd.Capabilities.SupportsPrimitiveTopologyPatchListRestart || Topology != PrimitiveTopology.PatchList;
  351. }
  352. else
  353. {
  354. topologySupportsRestart = Topology == PrimitiveTopology.LineStrip ||
  355. Topology == PrimitiveTopology.TriangleStrip ||
  356. Topology == PrimitiveTopology.TriangleFan ||
  357. Topology == PrimitiveTopology.LineStripWithAdjacency ||
  358. Topology == PrimitiveTopology.TriangleStripWithAdjacency;
  359. }
  360. primitiveRestartEnable &= topologySupportsRestart;
  361. var inputAssemblyState = new PipelineInputAssemblyStateCreateInfo()
  362. {
  363. SType = StructureType.PipelineInputAssemblyStateCreateInfo,
  364. PrimitiveRestartEnable = primitiveRestartEnable,
  365. Topology = Topology
  366. };
  367. var tessellationState = new PipelineTessellationStateCreateInfo()
  368. {
  369. SType = StructureType.PipelineTessellationStateCreateInfo,
  370. PatchControlPoints = PatchControlPoints
  371. };
  372. var rasterizationState = new PipelineRasterizationStateCreateInfo()
  373. {
  374. SType = StructureType.PipelineRasterizationStateCreateInfo,
  375. DepthClampEnable = DepthClampEnable,
  376. RasterizerDiscardEnable = RasterizerDiscardEnable,
  377. PolygonMode = PolygonMode,
  378. LineWidth = LineWidth,
  379. CullMode = CullMode,
  380. FrontFace = FrontFace,
  381. DepthBiasEnable = DepthBiasEnable,
  382. DepthBiasClamp = DepthBiasClamp,
  383. DepthBiasConstantFactor = DepthBiasConstantFactor,
  384. DepthBiasSlopeFactor = DepthBiasSlopeFactor
  385. };
  386. var viewportState = new PipelineViewportStateCreateInfo()
  387. {
  388. SType = StructureType.PipelineViewportStateCreateInfo,
  389. ViewportCount = ViewportsCount,
  390. PViewports = pViewports,
  391. ScissorCount = ScissorsCount,
  392. PScissors = pScissors
  393. };
  394. var multisampleState = new PipelineMultisampleStateCreateInfo
  395. {
  396. SType = StructureType.PipelineMultisampleStateCreateInfo,
  397. SampleShadingEnable = false,
  398. RasterizationSamples = TextureStorage.ConvertToSampleCountFlags(gd.Capabilities.SupportedSampleCounts, SamplesCount),
  399. MinSampleShading = 1,
  400. AlphaToCoverageEnable = AlphaToCoverageEnable,
  401. AlphaToOneEnable = AlphaToOneEnable
  402. };
  403. var stencilFront = new StencilOpState(
  404. StencilFrontFailOp,
  405. StencilFrontPassOp,
  406. StencilFrontDepthFailOp,
  407. StencilFrontCompareOp,
  408. StencilFrontCompareMask,
  409. StencilFrontWriteMask,
  410. StencilFrontReference);
  411. var stencilBack = new StencilOpState(
  412. StencilBackFailOp,
  413. StencilBackPassOp,
  414. StencilBackDepthFailOp,
  415. StencilBackCompareOp,
  416. StencilBackCompareMask,
  417. StencilBackWriteMask,
  418. StencilBackReference);
  419. var depthStencilState = new PipelineDepthStencilStateCreateInfo()
  420. {
  421. SType = StructureType.PipelineDepthStencilStateCreateInfo,
  422. DepthTestEnable = DepthTestEnable,
  423. DepthWriteEnable = DepthWriteEnable,
  424. DepthCompareOp = DepthCompareOp,
  425. DepthBoundsTestEnable = DepthBoundsTestEnable,
  426. StencilTestEnable = StencilTestEnable,
  427. Front = stencilFront,
  428. Back = stencilBack,
  429. MinDepthBounds = MinDepthBounds,
  430. MaxDepthBounds = MaxDepthBounds
  431. };
  432. var colorBlendState = new PipelineColorBlendStateCreateInfo()
  433. {
  434. SType = StructureType.PipelineColorBlendStateCreateInfo,
  435. LogicOpEnable = LogicOpEnable,
  436. LogicOp = LogicOp,
  437. AttachmentCount = ColorBlendAttachmentStateCount,
  438. PAttachments = pColorBlendAttachmentState
  439. };
  440. PipelineColorBlendAdvancedStateCreateInfoEXT colorBlendAdvancedState;
  441. if (!AdvancedBlendSrcPreMultiplied ||
  442. !AdvancedBlendDstPreMultiplied ||
  443. AdvancedBlendOverlap != BlendOverlapEXT.UncorrelatedExt)
  444. {
  445. colorBlendAdvancedState = new PipelineColorBlendAdvancedStateCreateInfoEXT()
  446. {
  447. SType = StructureType.PipelineColorBlendAdvancedStateCreateInfoExt,
  448. SrcPremultiplied = AdvancedBlendSrcPreMultiplied,
  449. DstPremultiplied = AdvancedBlendDstPreMultiplied,
  450. BlendOverlap = AdvancedBlendOverlap
  451. };
  452. colorBlendState.PNext = &colorBlendAdvancedState;
  453. }
  454. bool supportsExtDynamicState = gd.Capabilities.SupportsExtendedDynamicState;
  455. int dynamicStatesCount = supportsExtDynamicState ? 9 : 8;
  456. DynamicState* dynamicStates = stackalloc DynamicState[dynamicStatesCount];
  457. dynamicStates[0] = DynamicState.Viewport;
  458. dynamicStates[1] = DynamicState.Scissor;
  459. dynamicStates[2] = DynamicState.DepthBias;
  460. dynamicStates[3] = DynamicState.DepthBounds;
  461. dynamicStates[4] = DynamicState.StencilCompareMask;
  462. dynamicStates[5] = DynamicState.StencilWriteMask;
  463. dynamicStates[6] = DynamicState.StencilReference;
  464. dynamicStates[7] = DynamicState.BlendConstants;
  465. if (supportsExtDynamicState)
  466. {
  467. dynamicStates[8] = DynamicState.VertexInputBindingStrideExt;
  468. }
  469. var pipelineDynamicStateCreateInfo = new PipelineDynamicStateCreateInfo()
  470. {
  471. SType = StructureType.PipelineDynamicStateCreateInfo,
  472. DynamicStateCount = (uint)dynamicStatesCount,
  473. PDynamicStates = dynamicStates
  474. };
  475. if (gd.Capabilities.SupportsSubgroupSizeControl)
  476. {
  477. UpdateStageRequiredSubgroupSizes(gd, (int)StagesCount);
  478. }
  479. var pipelineCreateInfo = new GraphicsPipelineCreateInfo()
  480. {
  481. SType = StructureType.GraphicsPipelineCreateInfo,
  482. StageCount = StagesCount,
  483. PStages = Stages.Pointer,
  484. PVertexInputState = &vertexInputState,
  485. PInputAssemblyState = &inputAssemblyState,
  486. PTessellationState = &tessellationState,
  487. PViewportState = &viewportState,
  488. PRasterizationState = &rasterizationState,
  489. PMultisampleState = &multisampleState,
  490. PDepthStencilState = &depthStencilState,
  491. PColorBlendState = &colorBlendState,
  492. PDynamicState = &pipelineDynamicStateCreateInfo,
  493. Layout = PipelineLayout,
  494. RenderPass = renderPass,
  495. BasePipelineIndex = -1
  496. };
  497. gd.Api.CreateGraphicsPipelines(device, cache, 1, &pipelineCreateInfo, null, &pipelineHandle).ThrowOnError();
  498. }
  499. pipeline = new Auto<DisposablePipeline>(new DisposablePipeline(gd.Api, device, pipelineHandle));
  500. program.AddGraphicsPipeline(ref Internal, pipeline);
  501. return pipeline;
  502. }
  503. private unsafe void UpdateStageRequiredSubgroupSizes(VulkanRenderer gd, int count)
  504. {
  505. for (int index = 0; index < count; index++)
  506. {
  507. bool canUseExplicitSubgroupSize =
  508. (gd.Capabilities.RequiredSubgroupSizeStages & Stages[index].Stage) != 0 &&
  509. gd.Capabilities.MinSubgroupSize <= RequiredSubgroupSize &&
  510. gd.Capabilities.MaxSubgroupSize >= RequiredSubgroupSize;
  511. Stages[index].PNext = canUseExplicitSubgroupSize ? StageRequiredSubgroupSizes.Pointer + index : null;
  512. }
  513. }
  514. public void Dispose()
  515. {
  516. Stages.Dispose();
  517. StageRequiredSubgroupSizes.Dispose();
  518. }
  519. }
  520. }