PipelineConverter.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. using Ryujinx.Common;
  2. using Ryujinx.Graphics.GAL;
  3. using Silk.NET.Vulkan;
  4. using System;
  5. using Format = Silk.NET.Vulkan.Format;
  6. using PolygonMode = Silk.NET.Vulkan.PolygonMode;
  7. namespace Ryujinx.Graphics.Vulkan
  8. {
  9. static class PipelineConverter
  10. {
  11. public static unsafe DisposableRenderPass ToRenderPass(this ProgramPipelineState state, VulkanRenderer gd, Device device)
  12. {
  13. const int MaxAttachments = Constants.MaxRenderTargets + 1;
  14. AttachmentDescription[] attachmentDescs = null;
  15. SubpassDescription subpass = new()
  16. {
  17. PipelineBindPoint = PipelineBindPoint.Graphics,
  18. };
  19. AttachmentReference* attachmentReferences = stackalloc AttachmentReference[MaxAttachments];
  20. Span<int> attachmentIndices = stackalloc int[MaxAttachments];
  21. Span<Format> attachmentFormats = stackalloc Format[MaxAttachments];
  22. int attachmentCount = 0;
  23. int colorCount = 0;
  24. int maxColorAttachmentIndex = -1;
  25. bool isNotMsOrSupportsStorage = gd.Capabilities.SupportsShaderStorageImageMultisample ||
  26. !state.DepthStencilFormat.IsImageCompatible();
  27. for (int i = 0; i < state.AttachmentEnable.Length; i++)
  28. {
  29. if (state.AttachmentEnable[i])
  30. {
  31. bool isNotMsOrSupportsStorageAttachments = gd.Capabilities.SupportsShaderStorageImageMultisample ||
  32. !state.AttachmentFormats[i].IsImageCompatible();
  33. attachmentFormats[attachmentCount] = gd.FormatCapabilities.ConvertToVkFormat(state.AttachmentFormats[i], isNotMsOrSupportsStorageAttachments);
  34. attachmentIndices[attachmentCount++] = i;
  35. colorCount++;
  36. maxColorAttachmentIndex = i;
  37. }
  38. }
  39. if (state.DepthStencilEnable)
  40. {
  41. attachmentFormats[attachmentCount++] = gd.FormatCapabilities.ConvertToVkFormat(state.DepthStencilFormat, isNotMsOrSupportsStorage);
  42. }
  43. if (attachmentCount != 0)
  44. {
  45. attachmentDescs = new AttachmentDescription[attachmentCount];
  46. for (int i = 0; i < attachmentCount; i++)
  47. {
  48. int bindIndex = attachmentIndices[i];
  49. attachmentDescs[i] = new AttachmentDescription(
  50. 0,
  51. attachmentFormats[i],
  52. TextureStorage.ConvertToSampleCountFlags(gd.Capabilities.SupportedSampleCounts, (uint)state.SamplesCount),
  53. AttachmentLoadOp.Load,
  54. AttachmentStoreOp.Store,
  55. AttachmentLoadOp.Load,
  56. AttachmentStoreOp.Store,
  57. ImageLayout.General,
  58. ImageLayout.General);
  59. }
  60. int colorAttachmentsCount = colorCount;
  61. if (colorAttachmentsCount > MaxAttachments - 1)
  62. {
  63. colorAttachmentsCount = MaxAttachments - 1;
  64. }
  65. if (colorAttachmentsCount != 0)
  66. {
  67. subpass.ColorAttachmentCount = (uint)maxColorAttachmentIndex + 1;
  68. subpass.PColorAttachments = &attachmentReferences[0];
  69. // Fill with VK_ATTACHMENT_UNUSED to cover any gaps.
  70. for (int i = 0; i <= maxColorAttachmentIndex; i++)
  71. {
  72. subpass.PColorAttachments[i] = new AttachmentReference(Vk.AttachmentUnused, ImageLayout.Undefined);
  73. }
  74. for (int i = 0; i < colorAttachmentsCount; i++)
  75. {
  76. int bindIndex = attachmentIndices[i];
  77. subpass.PColorAttachments[bindIndex] = new AttachmentReference((uint)i, ImageLayout.General);
  78. }
  79. }
  80. if (state.DepthStencilEnable)
  81. {
  82. uint dsIndex = (uint)attachmentCount - 1;
  83. subpass.PDepthStencilAttachment = &attachmentReferences[MaxAttachments - 1];
  84. *subpass.PDepthStencilAttachment = new AttachmentReference(dsIndex, ImageLayout.General);
  85. }
  86. }
  87. SubpassDependency subpassDependency = CreateSubpassDependency(gd);
  88. fixed (AttachmentDescription* pAttachmentDescs = attachmentDescs)
  89. {
  90. RenderPassCreateInfo renderPassCreateInfo = new()
  91. {
  92. SType = StructureType.RenderPassCreateInfo,
  93. PAttachments = pAttachmentDescs,
  94. AttachmentCount = attachmentDescs != null ? (uint)attachmentDescs.Length : 0,
  95. PSubpasses = &subpass,
  96. SubpassCount = 1,
  97. PDependencies = &subpassDependency,
  98. DependencyCount = 1,
  99. };
  100. gd.Api.CreateRenderPass(device, in renderPassCreateInfo, null, out RenderPass renderPass).ThrowOnError();
  101. return new DisposableRenderPass(gd.Api, device, renderPass);
  102. }
  103. }
  104. public static SubpassDependency CreateSubpassDependency(VulkanRenderer gd)
  105. {
  106. (AccessFlags access, PipelineStageFlags stages) = BarrierBatch.GetSubpassAccessSuperset(gd);
  107. return new SubpassDependency(
  108. 0,
  109. 0,
  110. stages,
  111. stages,
  112. access,
  113. access,
  114. 0);
  115. }
  116. public unsafe static SubpassDependency2 CreateSubpassDependency2(VulkanRenderer gd)
  117. {
  118. (AccessFlags access, PipelineStageFlags stages) = BarrierBatch.GetSubpassAccessSuperset(gd);
  119. return new SubpassDependency2(
  120. StructureType.SubpassDependency2,
  121. null,
  122. 0,
  123. 0,
  124. stages,
  125. stages,
  126. access,
  127. access,
  128. 0);
  129. }
  130. public static PipelineState ToVulkanPipelineState(this ProgramPipelineState state, VulkanRenderer gd)
  131. {
  132. PipelineState pipeline = new();
  133. pipeline.Initialize();
  134. // It is assumed that Dynamic State is enabled when this conversion is used.
  135. pipeline.CullMode = state.CullEnable ? state.CullMode.Convert() : CullModeFlags.None;
  136. pipeline.DepthBoundsTestEnable = false; // Not implemented.
  137. pipeline.DepthClampEnable = state.DepthClampEnable;
  138. pipeline.DepthTestEnable = state.DepthTest.TestEnable;
  139. pipeline.DepthWriteEnable = state.DepthTest.WriteEnable;
  140. pipeline.DepthCompareOp = state.DepthTest.Func.Convert();
  141. pipeline.DepthMode = state.DepthMode == DepthMode.MinusOneToOne;
  142. pipeline.FrontFace = state.FrontFace.Convert();
  143. pipeline.HasDepthStencil = state.DepthStencilEnable;
  144. pipeline.LineWidth = state.LineWidth;
  145. pipeline.LogicOpEnable = state.LogicOpEnable;
  146. pipeline.LogicOp = state.LogicOp.Convert();
  147. pipeline.PatchControlPoints = state.PatchControlPoints;
  148. pipeline.PolygonMode = PolygonMode.Fill; // Not implemented.
  149. pipeline.PrimitiveRestartEnable = state.PrimitiveRestartEnable;
  150. pipeline.RasterizerDiscardEnable = state.RasterizerDiscard;
  151. pipeline.SamplesCount = (uint)state.SamplesCount;
  152. if (gd.Capabilities.SupportsMultiView)
  153. {
  154. pipeline.ScissorsCount = Constants.MaxViewports;
  155. pipeline.ViewportsCount = Constants.MaxViewports;
  156. }
  157. else
  158. {
  159. pipeline.ScissorsCount = 1;
  160. pipeline.ViewportsCount = 1;
  161. }
  162. pipeline.DepthBiasEnable = state.BiasEnable != 0;
  163. // Stencil masks and ref are dynamic, so are 0 in the Vulkan pipeline.
  164. pipeline.StencilFrontFailOp = state.StencilTest.FrontSFail.Convert();
  165. pipeline.StencilFrontPassOp = state.StencilTest.FrontDpPass.Convert();
  166. pipeline.StencilFrontDepthFailOp = state.StencilTest.FrontDpFail.Convert();
  167. pipeline.StencilFrontCompareOp = state.StencilTest.FrontFunc.Convert();
  168. pipeline.StencilBackFailOp = state.StencilTest.BackSFail.Convert();
  169. pipeline.StencilBackPassOp = state.StencilTest.BackDpPass.Convert();
  170. pipeline.StencilBackDepthFailOp = state.StencilTest.BackDpFail.Convert();
  171. pipeline.StencilBackCompareOp = state.StencilTest.BackFunc.Convert();
  172. pipeline.StencilTestEnable = state.StencilTest.TestEnable;
  173. pipeline.Topology = gd.TopologyRemap(state.Topology).Convert();
  174. int vaCount = Math.Min(Constants.MaxVertexAttributes, state.VertexAttribCount);
  175. int vbCount = Math.Min(Constants.MaxVertexBuffers, state.VertexBufferCount);
  176. Span<int> vbScalarSizes = stackalloc int[vbCount];
  177. for (int i = 0; i < vaCount; i++)
  178. {
  179. VertexAttribDescriptor attribute = state.VertexAttribs[i];
  180. int bufferIndex = attribute.IsZero ? 0 : attribute.BufferIndex + 1;
  181. pipeline.Internal.VertexAttributeDescriptions[i] = new VertexInputAttributeDescription(
  182. (uint)i,
  183. (uint)bufferIndex,
  184. gd.FormatCapabilities.ConvertToVertexVkFormat(attribute.Format),
  185. (uint)attribute.Offset);
  186. if (!attribute.IsZero && bufferIndex < vbCount)
  187. {
  188. vbScalarSizes[bufferIndex - 1] = Math.Max(attribute.Format.GetScalarSize(), vbScalarSizes[bufferIndex - 1]);
  189. }
  190. }
  191. int descriptorIndex = 1;
  192. pipeline.Internal.VertexBindingDescriptions[0] = new VertexInputBindingDescription(0, 0, VertexInputRate.Vertex);
  193. for (int i = 0; i < vbCount; i++)
  194. {
  195. BufferPipelineDescriptor vertexBuffer = state.VertexBuffers[i];
  196. if (vertexBuffer.Enable)
  197. {
  198. VertexInputRate inputRate = vertexBuffer.Divisor != 0 ? VertexInputRate.Instance : VertexInputRate.Vertex;
  199. int alignedStride = vertexBuffer.Stride;
  200. if (gd.NeedsVertexBufferAlignment(vbScalarSizes[i], out int alignment))
  201. {
  202. alignedStride = BitUtils.AlignUp(vertexBuffer.Stride, alignment);
  203. }
  204. // TODO: Support divisor > 1
  205. pipeline.Internal.VertexBindingDescriptions[descriptorIndex++] = new VertexInputBindingDescription(
  206. (uint)i + 1,
  207. (uint)alignedStride,
  208. inputRate);
  209. }
  210. }
  211. pipeline.VertexBindingDescriptionsCount = (uint)descriptorIndex;
  212. // NOTE: Viewports, Scissors are dynamic.
  213. for (int i = 0; i < Constants.MaxRenderTargets; i++)
  214. {
  215. BlendDescriptor blend = state.BlendDescriptors[i];
  216. if (blend.Enable && state.ColorWriteMask[i] != 0)
  217. {
  218. pipeline.Internal.ColorBlendAttachmentState[i] = new PipelineColorBlendAttachmentState(
  219. blend.Enable,
  220. blend.ColorSrcFactor.Convert(),
  221. blend.ColorDstFactor.Convert(),
  222. blend.ColorOp.Convert(),
  223. blend.AlphaSrcFactor.Convert(),
  224. blend.AlphaDstFactor.Convert(),
  225. blend.AlphaOp.Convert(),
  226. (ColorComponentFlags)state.ColorWriteMask[i]);
  227. }
  228. else
  229. {
  230. pipeline.Internal.ColorBlendAttachmentState[i] = new PipelineColorBlendAttachmentState(
  231. colorWriteMask: (ColorComponentFlags)state.ColorWriteMask[i]);
  232. }
  233. }
  234. int attachmentCount = 0;
  235. int maxColorAttachmentIndex = -1;
  236. uint attachmentIntegerFormatMask = 0;
  237. bool allFormatsFloatOrSrgb = true;
  238. for (int i = 0; i < Constants.MaxRenderTargets; i++)
  239. {
  240. if (state.AttachmentEnable[i])
  241. {
  242. bool isNotMsOrSupportsStorage = gd.Capabilities.SupportsShaderStorageImageMultisample ||
  243. !state.AttachmentFormats[i].IsImageCompatible();
  244. pipeline.Internal.AttachmentFormats[attachmentCount++] = gd.FormatCapabilities.ConvertToVkFormat(state.AttachmentFormats[i], isNotMsOrSupportsStorage);
  245. maxColorAttachmentIndex = i;
  246. if (state.AttachmentFormats[i].IsInteger())
  247. {
  248. attachmentIntegerFormatMask |= 1u << i;
  249. }
  250. allFormatsFloatOrSrgb &= state.AttachmentFormats[i].IsFloatOrSrgb();
  251. }
  252. }
  253. if (state.DepthStencilEnable)
  254. {
  255. bool isNotMsOrSupportsStorage = !state.DepthStencilFormat.IsImageCompatible() ||
  256. gd.Capabilities.SupportsShaderStorageImageMultisample;
  257. pipeline.Internal.AttachmentFormats[attachmentCount++] = gd.FormatCapabilities.ConvertToVkFormat(state.DepthStencilFormat, isNotMsOrSupportsStorage);
  258. }
  259. pipeline.ColorBlendAttachmentStateCount = (uint)(maxColorAttachmentIndex + 1);
  260. pipeline.VertexAttributeDescriptionsCount = (uint)Math.Min(Constants.MaxVertexAttributes, state.VertexAttribCount);
  261. pipeline.Internal.AttachmentIntegerFormatMask = attachmentIntegerFormatMask;
  262. pipeline.Internal.LogicOpsAllowed = attachmentCount == 0 || !allFormatsFloatOrSrgb;
  263. return pipeline;
  264. }
  265. }
  266. }