PipelineState.cs 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597
  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 SpecData SpecializationData;
  263. public void Initialize()
  264. {
  265. Stages = new NativeArray<PipelineShaderStageCreateInfo>(Constants.MaxShaderStages);
  266. StageRequiredSubgroupSizes = new NativeArray<PipelineShaderStageRequiredSubgroupSizeCreateInfoEXT>(Constants.MaxShaderStages);
  267. for (int index = 0; index < Constants.MaxShaderStages; index++)
  268. {
  269. StageRequiredSubgroupSizes[index] = new PipelineShaderStageRequiredSubgroupSizeCreateInfoEXT()
  270. {
  271. SType = StructureType.PipelineShaderStageRequiredSubgroupSizeCreateInfoExt,
  272. RequiredSubgroupSize = RequiredSubgroupSize
  273. };
  274. }
  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. primitiveRestartEnable &= Topology == PrimitiveTopology.LineStrip ||
  348. Topology == PrimitiveTopology.TriangleStrip ||
  349. Topology == PrimitiveTopology.TriangleFan ||
  350. Topology == PrimitiveTopology.LineStripWithAdjacency ||
  351. Topology == PrimitiveTopology.TriangleStripWithAdjacency;
  352. var inputAssemblyState = new PipelineInputAssemblyStateCreateInfo()
  353. {
  354. SType = StructureType.PipelineInputAssemblyStateCreateInfo,
  355. PrimitiveRestartEnable = primitiveRestartEnable,
  356. Topology = Topology
  357. };
  358. var tessellationState = new PipelineTessellationStateCreateInfo()
  359. {
  360. SType = StructureType.PipelineTessellationStateCreateInfo,
  361. PatchControlPoints = PatchControlPoints
  362. };
  363. var rasterizationState = new PipelineRasterizationStateCreateInfo()
  364. {
  365. SType = StructureType.PipelineRasterizationStateCreateInfo,
  366. DepthClampEnable = DepthClampEnable,
  367. RasterizerDiscardEnable = RasterizerDiscardEnable,
  368. PolygonMode = PolygonMode,
  369. LineWidth = LineWidth,
  370. CullMode = CullMode,
  371. FrontFace = FrontFace,
  372. DepthBiasEnable = DepthBiasEnable,
  373. DepthBiasClamp = DepthBiasClamp,
  374. DepthBiasConstantFactor = DepthBiasConstantFactor,
  375. DepthBiasSlopeFactor = DepthBiasSlopeFactor
  376. };
  377. var viewportState = new PipelineViewportStateCreateInfo()
  378. {
  379. SType = StructureType.PipelineViewportStateCreateInfo,
  380. ViewportCount = ViewportsCount,
  381. PViewports = pViewports,
  382. ScissorCount = ScissorsCount,
  383. PScissors = pScissors
  384. };
  385. var multisampleState = new PipelineMultisampleStateCreateInfo
  386. {
  387. SType = StructureType.PipelineMultisampleStateCreateInfo,
  388. SampleShadingEnable = false,
  389. RasterizationSamples = TextureStorage.ConvertToSampleCountFlags(SamplesCount),
  390. MinSampleShading = 1,
  391. AlphaToCoverageEnable = AlphaToCoverageEnable,
  392. AlphaToOneEnable = AlphaToOneEnable
  393. };
  394. var stencilFront = new StencilOpState(
  395. StencilFrontFailOp,
  396. StencilFrontPassOp,
  397. StencilFrontDepthFailOp,
  398. StencilFrontCompareOp,
  399. StencilFrontCompareMask,
  400. StencilFrontWriteMask,
  401. StencilFrontReference);
  402. var stencilBack = new StencilOpState(
  403. StencilBackFailOp,
  404. StencilBackPassOp,
  405. StencilBackDepthFailOp,
  406. StencilBackCompareOp,
  407. StencilBackCompareMask,
  408. StencilBackWriteMask,
  409. StencilBackReference);
  410. var depthStencilState = new PipelineDepthStencilStateCreateInfo()
  411. {
  412. SType = StructureType.PipelineDepthStencilStateCreateInfo,
  413. DepthTestEnable = DepthTestEnable,
  414. DepthWriteEnable = DepthWriteEnable,
  415. DepthCompareOp = DepthCompareOp,
  416. DepthBoundsTestEnable = DepthBoundsTestEnable,
  417. StencilTestEnable = StencilTestEnable,
  418. Front = stencilFront,
  419. Back = stencilBack,
  420. MinDepthBounds = MinDepthBounds,
  421. MaxDepthBounds = MaxDepthBounds
  422. };
  423. var colorBlendState = new PipelineColorBlendStateCreateInfo()
  424. {
  425. SType = StructureType.PipelineColorBlendStateCreateInfo,
  426. LogicOpEnable = LogicOpEnable,
  427. LogicOp = LogicOp,
  428. AttachmentCount = ColorBlendAttachmentStateCount,
  429. PAttachments = pColorBlendAttachmentState
  430. };
  431. colorBlendState.BlendConstants[0] = BlendConstantR;
  432. colorBlendState.BlendConstants[1] = BlendConstantG;
  433. colorBlendState.BlendConstants[2] = BlendConstantB;
  434. colorBlendState.BlendConstants[3] = BlendConstantA;
  435. bool supportsExtDynamicState = gd.Capabilities.SupportsExtendedDynamicState;
  436. int dynamicStatesCount = supportsExtDynamicState ? 9 : 8;
  437. DynamicState* dynamicStates = stackalloc DynamicState[dynamicStatesCount];
  438. dynamicStates[0] = DynamicState.Viewport;
  439. dynamicStates[1] = DynamicState.Scissor;
  440. dynamicStates[2] = DynamicState.DepthBias;
  441. dynamicStates[3] = DynamicState.DepthBounds;
  442. dynamicStates[4] = DynamicState.StencilCompareMask;
  443. dynamicStates[5] = DynamicState.StencilWriteMask;
  444. dynamicStates[6] = DynamicState.StencilReference;
  445. dynamicStates[7] = DynamicState.BlendConstants;
  446. if (supportsExtDynamicState)
  447. {
  448. dynamicStates[8] = DynamicState.VertexInputBindingStrideExt;
  449. }
  450. var pipelineDynamicStateCreateInfo = new PipelineDynamicStateCreateInfo()
  451. {
  452. SType = StructureType.PipelineDynamicStateCreateInfo,
  453. DynamicStateCount = (uint)dynamicStatesCount,
  454. PDynamicStates = dynamicStates
  455. };
  456. if (gd.Capabilities.SupportsSubgroupSizeControl)
  457. {
  458. UpdateStageRequiredSubgroupSizes(gd, (int)StagesCount);
  459. }
  460. var pipelineCreateInfo = new GraphicsPipelineCreateInfo()
  461. {
  462. SType = StructureType.GraphicsPipelineCreateInfo,
  463. StageCount = StagesCount,
  464. PStages = Stages.Pointer,
  465. PVertexInputState = &vertexInputState,
  466. PInputAssemblyState = &inputAssemblyState,
  467. PTessellationState = &tessellationState,
  468. PViewportState = &viewportState,
  469. PRasterizationState = &rasterizationState,
  470. PMultisampleState = &multisampleState,
  471. PDepthStencilState = &depthStencilState,
  472. PColorBlendState = &colorBlendState,
  473. PDynamicState = &pipelineDynamicStateCreateInfo,
  474. Layout = PipelineLayout,
  475. RenderPass = renderPass,
  476. BasePipelineIndex = -1
  477. };
  478. gd.Api.CreateGraphicsPipelines(device, cache, 1, &pipelineCreateInfo, null, &pipelineHandle).ThrowOnError();
  479. }
  480. pipeline = new Auto<DisposablePipeline>(new DisposablePipeline(gd.Api, device, pipelineHandle));
  481. program.AddGraphicsPipeline(ref Internal, pipeline);
  482. return pipeline;
  483. }
  484. private unsafe void UpdateStageRequiredSubgroupSizes(VulkanRenderer gd, int count)
  485. {
  486. for (int index = 0; index < count; index++)
  487. {
  488. bool canUseExplicitSubgroupSize =
  489. (gd.Capabilities.RequiredSubgroupSizeStages & Stages[index].Stage) != 0 &&
  490. gd.Capabilities.MinSubgroupSize <= RequiredSubgroupSize &&
  491. gd.Capabilities.MaxSubgroupSize >= RequiredSubgroupSize;
  492. Stages[index].PNext = canUseExplicitSubgroupSize ? StageRequiredSubgroupSizes.Pointer + index : null;
  493. }
  494. }
  495. public void Dispose()
  496. {
  497. Stages.Dispose();
  498. StageRequiredSubgroupSizes.Dispose();
  499. }
  500. }
  501. }