PipelineConverter.cs 13 KB

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