PipelineConverter.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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.FrontFace = state.FrontFace.Convert();
  136. pipeline.HasDepthStencil = state.DepthStencilEnable;
  137. pipeline.LineWidth = state.LineWidth;
  138. pipeline.LogicOpEnable = state.LogicOpEnable;
  139. pipeline.LogicOp = state.LogicOp.Convert();
  140. pipeline.MinDepthBounds = 0f; // Not implemented.
  141. pipeline.MaxDepthBounds = 0f; // Not implemented.
  142. pipeline.PatchControlPoints = state.PatchControlPoints;
  143. pipeline.PolygonMode = Silk.NET.Vulkan.PolygonMode.Fill; // Not implemented.
  144. pipeline.PrimitiveRestartEnable = state.PrimitiveRestartEnable;
  145. pipeline.RasterizerDiscardEnable = state.RasterizerDiscard;
  146. pipeline.SamplesCount = (uint)state.SamplesCount;
  147. if (gd.Capabilities.SupportsMultiView)
  148. {
  149. pipeline.ScissorsCount = Constants.MaxViewports;
  150. pipeline.ViewportsCount = Constants.MaxViewports;
  151. }
  152. else
  153. {
  154. pipeline.ScissorsCount = 1;
  155. pipeline.ViewportsCount = 1;
  156. }
  157. pipeline.DepthBiasEnable = state.BiasEnable != 0;
  158. // Stencil masks and ref are dynamic, so are 0 in the Vulkan pipeline.
  159. pipeline.StencilFrontFailOp = state.StencilTest.FrontSFail.Convert();
  160. pipeline.StencilFrontPassOp = state.StencilTest.FrontDpPass.Convert();
  161. pipeline.StencilFrontDepthFailOp = state.StencilTest.FrontDpFail.Convert();
  162. pipeline.StencilFrontCompareOp = state.StencilTest.FrontFunc.Convert();
  163. pipeline.StencilFrontCompareMask = 0;
  164. pipeline.StencilFrontWriteMask = 0;
  165. pipeline.StencilFrontReference = 0;
  166. pipeline.StencilBackFailOp = state.StencilTest.BackSFail.Convert();
  167. pipeline.StencilBackPassOp = state.StencilTest.BackDpPass.Convert();
  168. pipeline.StencilBackDepthFailOp = state.StencilTest.BackDpFail.Convert();
  169. pipeline.StencilBackCompareOp = state.StencilTest.BackFunc.Convert();
  170. pipeline.StencilBackCompareMask = 0;
  171. pipeline.StencilBackWriteMask = 0;
  172. pipeline.StencilBackReference = 0;
  173. pipeline.StencilTestEnable = state.StencilTest.TestEnable;
  174. pipeline.Topology = gd.TopologyRemap(state.Topology).Convert();
  175. int vaCount = Math.Min(Constants.MaxVertexAttributes, state.VertexAttribCount);
  176. int vbCount = Math.Min(Constants.MaxVertexBuffers, state.VertexBufferCount);
  177. Span<int> vbScalarSizes = stackalloc int[vbCount];
  178. for (int i = 0; i < vaCount; i++)
  179. {
  180. var attribute = state.VertexAttribs[i];
  181. var bufferIndex = attribute.IsZero ? 0 : attribute.BufferIndex + 1;
  182. pipeline.Internal.VertexAttributeDescriptions[i] = new VertexInputAttributeDescription(
  183. (uint)i,
  184. (uint)bufferIndex,
  185. gd.FormatCapabilities.ConvertToVertexVkFormat(attribute.Format),
  186. (uint)attribute.Offset);
  187. if (!attribute.IsZero && bufferIndex < vbCount)
  188. {
  189. vbScalarSizes[bufferIndex - 1] = Math.Max(attribute.Format.GetScalarSize(), vbScalarSizes[bufferIndex - 1]);
  190. }
  191. }
  192. int descriptorIndex = 1;
  193. pipeline.Internal.VertexBindingDescriptions[0] = new VertexInputBindingDescription(0, 0, VertexInputRate.Vertex);
  194. for (int i = 0; i < vbCount; i++)
  195. {
  196. var vertexBuffer = state.VertexBuffers[i];
  197. if (vertexBuffer.Enable)
  198. {
  199. var inputRate = vertexBuffer.Divisor != 0 ? VertexInputRate.Instance : VertexInputRate.Vertex;
  200. int alignedStride = vertexBuffer.Stride;
  201. if (gd.NeedsVertexBufferAlignment(vbScalarSizes[i], out int alignment))
  202. {
  203. alignedStride = BitUtils.AlignUp(vertexBuffer.Stride, alignment);
  204. }
  205. // TODO: Support divisor > 1
  206. pipeline.Internal.VertexBindingDescriptions[descriptorIndex++] = new VertexInputBindingDescription(
  207. (uint)i + 1,
  208. (uint)alignedStride,
  209. inputRate);
  210. }
  211. }
  212. pipeline.VertexBindingDescriptionsCount = (uint)descriptorIndex;
  213. // NOTE: Viewports, Scissors are dynamic.
  214. for (int i = 0; i < Constants.MaxRenderTargets; i++)
  215. {
  216. var blend = state.BlendDescriptors[i];
  217. if (blend.Enable && state.ColorWriteMask[i] != 0)
  218. {
  219. pipeline.Internal.ColorBlendAttachmentState[i] = new PipelineColorBlendAttachmentState(
  220. blend.Enable,
  221. blend.ColorSrcFactor.Convert(),
  222. blend.ColorDstFactor.Convert(),
  223. blend.ColorOp.Convert(),
  224. blend.AlphaSrcFactor.Convert(),
  225. blend.AlphaDstFactor.Convert(),
  226. blend.AlphaOp.Convert(),
  227. (ColorComponentFlags)state.ColorWriteMask[i]);
  228. }
  229. else
  230. {
  231. pipeline.Internal.ColorBlendAttachmentState[i] = new PipelineColorBlendAttachmentState(
  232. colorWriteMask: (ColorComponentFlags)state.ColorWriteMask[i]);
  233. }
  234. }
  235. int attachmentCount = 0;
  236. int maxColorAttachmentIndex = -1;
  237. for (int i = 0; i < Constants.MaxRenderTargets; i++)
  238. {
  239. if (state.AttachmentEnable[i])
  240. {
  241. pipeline.Internal.AttachmentFormats[attachmentCount++] = gd.FormatCapabilities.ConvertToVkFormat(state.AttachmentFormats[i]);
  242. maxColorAttachmentIndex = i;
  243. }
  244. }
  245. if (state.DepthStencilEnable)
  246. {
  247. pipeline.Internal.AttachmentFormats[attachmentCount++] = gd.FormatCapabilities.ConvertToVkFormat(state.DepthStencilFormat);
  248. }
  249. pipeline.ColorBlendAttachmentStateCount = (uint)(maxColorAttachmentIndex + 1);
  250. pipeline.VertexAttributeDescriptionsCount = (uint)Math.Min(Constants.MaxVertexAttributes, state.VertexAttribCount);
  251. return pipeline;
  252. }
  253. }
  254. }