PipelineState.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  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 NativeArray<PipelineShaderStageCreateInfo> Stages;
  240. public NativeArray<PipelineShaderStageRequiredSubgroupSizeCreateInfoEXT> StageRequiredSubgroupSizes;
  241. public PipelineLayout PipelineLayout;
  242. public SpecData SpecializationData;
  243. public void Initialize()
  244. {
  245. Stages = new NativeArray<PipelineShaderStageCreateInfo>(Constants.MaxShaderStages);
  246. StageRequiredSubgroupSizes = new NativeArray<PipelineShaderStageRequiredSubgroupSizeCreateInfoEXT>(Constants.MaxShaderStages);
  247. for (int index = 0; index < Constants.MaxShaderStages; index++)
  248. {
  249. StageRequiredSubgroupSizes[index] = new PipelineShaderStageRequiredSubgroupSizeCreateInfoEXT()
  250. {
  251. SType = StructureType.PipelineShaderStageRequiredSubgroupSizeCreateInfoExt,
  252. RequiredSubgroupSize = RequiredSubgroupSize
  253. };
  254. }
  255. }
  256. public unsafe Auto<DisposablePipeline> CreateComputePipeline(
  257. VulkanRenderer gd,
  258. Device device,
  259. ShaderCollection program,
  260. PipelineCache cache)
  261. {
  262. if (program.TryGetComputePipeline(ref SpecializationData, out var pipeline))
  263. {
  264. return pipeline;
  265. }
  266. if (gd.Capabilities.SupportsSubgroupSizeControl)
  267. {
  268. UpdateStageRequiredSubgroupSizes(gd, 1);
  269. }
  270. var pipelineCreateInfo = new ComputePipelineCreateInfo()
  271. {
  272. SType = StructureType.ComputePipelineCreateInfo,
  273. Stage = Stages[0],
  274. BasePipelineIndex = -1,
  275. Layout = PipelineLayout
  276. };
  277. Pipeline pipelineHandle = default;
  278. bool hasSpec = program.SpecDescriptions != null;
  279. var desc = hasSpec ? program.SpecDescriptions[0] : SpecDescription.Empty;
  280. if (hasSpec && SpecializationData.Length < (int)desc.Info.DataSize)
  281. {
  282. throw new InvalidOperationException("Specialization data size does not match description");
  283. }
  284. fixed (SpecializationInfo* info = &desc.Info)
  285. fixed (SpecializationMapEntry* map = desc.Map)
  286. fixed (byte* data = SpecializationData.Span)
  287. {
  288. if (hasSpec)
  289. {
  290. info->PMapEntries = map;
  291. info->PData = data;
  292. pipelineCreateInfo.Stage.PSpecializationInfo = info;
  293. }
  294. gd.Api.CreateComputePipelines(device, cache, 1, &pipelineCreateInfo, null, &pipelineHandle).ThrowOnError();
  295. }
  296. pipeline = new Auto<DisposablePipeline>(new DisposablePipeline(gd.Api, device, pipelineHandle));
  297. program.AddComputePipeline(ref SpecializationData, pipeline);
  298. return pipeline;
  299. }
  300. public unsafe Auto<DisposablePipeline> CreateGraphicsPipeline(
  301. VulkanRenderer gd,
  302. Device device,
  303. ShaderCollection program,
  304. PipelineCache cache,
  305. RenderPass renderPass)
  306. {
  307. if (program.TryGetGraphicsPipeline(ref Internal, out var pipeline))
  308. {
  309. return pipeline;
  310. }
  311. Pipeline pipelineHandle = default;
  312. fixed (VertexInputAttributeDescription* pVertexAttributeDescriptions = &Internal.VertexAttributeDescriptions[0])
  313. fixed (VertexInputBindingDescription* pVertexBindingDescriptions = &Internal.VertexBindingDescriptions[0])
  314. fixed (Viewport* pViewports = &Internal.Viewports[0])
  315. fixed (Rect2D* pScissors = &Internal.Scissors[0])
  316. fixed (PipelineColorBlendAttachmentState* pColorBlendAttachmentState = &Internal.ColorBlendAttachmentState[0])
  317. {
  318. var vertexInputState = new PipelineVertexInputStateCreateInfo
  319. {
  320. SType = StructureType.PipelineVertexInputStateCreateInfo,
  321. VertexAttributeDescriptionCount = VertexAttributeDescriptionsCount,
  322. PVertexAttributeDescriptions = pVertexAttributeDescriptions,
  323. VertexBindingDescriptionCount = VertexBindingDescriptionsCount,
  324. PVertexBindingDescriptions = pVertexBindingDescriptions
  325. };
  326. bool primitiveRestartEnable = PrimitiveRestartEnable;
  327. primitiveRestartEnable &= Topology == PrimitiveTopology.LineStrip ||
  328. Topology == PrimitiveTopology.TriangleStrip ||
  329. Topology == PrimitiveTopology.TriangleFan ||
  330. Topology == PrimitiveTopology.LineStripWithAdjacency ||
  331. Topology == PrimitiveTopology.TriangleStripWithAdjacency;
  332. var inputAssemblyState = new PipelineInputAssemblyStateCreateInfo()
  333. {
  334. SType = StructureType.PipelineInputAssemblyStateCreateInfo,
  335. PrimitiveRestartEnable = primitiveRestartEnable,
  336. Topology = Topology
  337. };
  338. var tessellationState = new PipelineTessellationStateCreateInfo()
  339. {
  340. SType = StructureType.PipelineTessellationStateCreateInfo,
  341. PatchControlPoints = PatchControlPoints
  342. };
  343. var rasterizationState = new PipelineRasterizationStateCreateInfo()
  344. {
  345. SType = StructureType.PipelineRasterizationStateCreateInfo,
  346. DepthClampEnable = DepthClampEnable,
  347. RasterizerDiscardEnable = RasterizerDiscardEnable,
  348. PolygonMode = PolygonMode,
  349. LineWidth = LineWidth,
  350. CullMode = CullMode,
  351. FrontFace = FrontFace,
  352. DepthBiasEnable = DepthBiasEnable,
  353. DepthBiasClamp = DepthBiasClamp,
  354. DepthBiasConstantFactor = DepthBiasConstantFactor,
  355. DepthBiasSlopeFactor = DepthBiasSlopeFactor
  356. };
  357. var viewportState = new PipelineViewportStateCreateInfo()
  358. {
  359. SType = StructureType.PipelineViewportStateCreateInfo,
  360. ViewportCount = ViewportsCount,
  361. PViewports = pViewports,
  362. ScissorCount = ScissorsCount,
  363. PScissors = pScissors
  364. };
  365. var multisampleState = new PipelineMultisampleStateCreateInfo
  366. {
  367. SType = StructureType.PipelineMultisampleStateCreateInfo,
  368. SampleShadingEnable = false,
  369. RasterizationSamples = TextureStorage.ConvertToSampleCountFlags(SamplesCount),
  370. MinSampleShading = 1,
  371. AlphaToCoverageEnable = AlphaToCoverageEnable,
  372. AlphaToOneEnable = AlphaToOneEnable
  373. };
  374. var stencilFront = new StencilOpState(
  375. StencilFrontFailOp,
  376. StencilFrontPassOp,
  377. StencilFrontDepthFailOp,
  378. StencilFrontCompareOp,
  379. StencilFrontCompareMask,
  380. StencilFrontWriteMask,
  381. StencilFrontReference);
  382. var stencilBack = new StencilOpState(
  383. StencilBackFailOp,
  384. StencilBackPassOp,
  385. StencilBackDepthFailOp,
  386. StencilBackCompareOp,
  387. StencilBackCompareMask,
  388. StencilBackWriteMask,
  389. StencilBackReference);
  390. var depthStencilState = new PipelineDepthStencilStateCreateInfo()
  391. {
  392. SType = StructureType.PipelineDepthStencilStateCreateInfo,
  393. DepthTestEnable = DepthTestEnable,
  394. DepthWriteEnable = DepthWriteEnable,
  395. DepthCompareOp = DepthCompareOp,
  396. DepthBoundsTestEnable = DepthBoundsTestEnable,
  397. StencilTestEnable = StencilTestEnable,
  398. Front = stencilFront,
  399. Back = stencilBack,
  400. MinDepthBounds = MinDepthBounds,
  401. MaxDepthBounds = MaxDepthBounds
  402. };
  403. var colorBlendState = new PipelineColorBlendStateCreateInfo()
  404. {
  405. SType = StructureType.PipelineColorBlendStateCreateInfo,
  406. LogicOpEnable = LogicOpEnable,
  407. LogicOp = LogicOp,
  408. AttachmentCount = ColorBlendAttachmentStateCount,
  409. PAttachments = pColorBlendAttachmentState
  410. };
  411. bool supportsExtDynamicState = gd.Capabilities.SupportsExtendedDynamicState;
  412. int dynamicStatesCount = supportsExtDynamicState ? 9 : 8;
  413. DynamicState* dynamicStates = stackalloc DynamicState[dynamicStatesCount];
  414. dynamicStates[0] = DynamicState.Viewport;
  415. dynamicStates[1] = DynamicState.Scissor;
  416. dynamicStates[2] = DynamicState.DepthBias;
  417. dynamicStates[3] = DynamicState.DepthBounds;
  418. dynamicStates[4] = DynamicState.StencilCompareMask;
  419. dynamicStates[5] = DynamicState.StencilWriteMask;
  420. dynamicStates[6] = DynamicState.StencilReference;
  421. dynamicStates[7] = DynamicState.BlendConstants;
  422. if (supportsExtDynamicState)
  423. {
  424. dynamicStates[8] = DynamicState.VertexInputBindingStrideExt;
  425. }
  426. var pipelineDynamicStateCreateInfo = new PipelineDynamicStateCreateInfo()
  427. {
  428. SType = StructureType.PipelineDynamicStateCreateInfo,
  429. DynamicStateCount = (uint)dynamicStatesCount,
  430. PDynamicStates = dynamicStates
  431. };
  432. if (gd.Capabilities.SupportsSubgroupSizeControl)
  433. {
  434. UpdateStageRequiredSubgroupSizes(gd, (int)StagesCount);
  435. }
  436. var pipelineCreateInfo = new GraphicsPipelineCreateInfo()
  437. {
  438. SType = StructureType.GraphicsPipelineCreateInfo,
  439. StageCount = StagesCount,
  440. PStages = Stages.Pointer,
  441. PVertexInputState = &vertexInputState,
  442. PInputAssemblyState = &inputAssemblyState,
  443. PTessellationState = &tessellationState,
  444. PViewportState = &viewportState,
  445. PRasterizationState = &rasterizationState,
  446. PMultisampleState = &multisampleState,
  447. PDepthStencilState = &depthStencilState,
  448. PColorBlendState = &colorBlendState,
  449. PDynamicState = &pipelineDynamicStateCreateInfo,
  450. Layout = PipelineLayout,
  451. RenderPass = renderPass,
  452. BasePipelineIndex = -1
  453. };
  454. gd.Api.CreateGraphicsPipelines(device, cache, 1, &pipelineCreateInfo, null, &pipelineHandle).ThrowOnError();
  455. }
  456. pipeline = new Auto<DisposablePipeline>(new DisposablePipeline(gd.Api, device, pipelineHandle));
  457. program.AddGraphicsPipeline(ref Internal, pipeline);
  458. return pipeline;
  459. }
  460. private unsafe void UpdateStageRequiredSubgroupSizes(VulkanRenderer gd, int count)
  461. {
  462. for (int index = 0; index < count; index++)
  463. {
  464. bool canUseExplicitSubgroupSize =
  465. (gd.Capabilities.RequiredSubgroupSizeStages & Stages[index].Stage) != 0 &&
  466. gd.Capabilities.MinSubgroupSize <= RequiredSubgroupSize &&
  467. gd.Capabilities.MaxSubgroupSize >= RequiredSubgroupSize;
  468. Stages[index].PNext = canUseExplicitSubgroupSize ? StageRequiredSubgroupSizes.Pointer + index : null;
  469. }
  470. }
  471. public void Dispose()
  472. {
  473. Stages.Dispose();
  474. StageRequiredSubgroupSizes.Dispose();
  475. }
  476. }
  477. }