PipelineState.cs 23 KB

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