PipelineConverter.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. using Ryujinx.Graphics.GAL;
  2. using Silk.NET.Vulkan;
  3. using System;
  4. namespace Ryujinx.Graphics.Vulkan
  5. {
  6. static class PipelineConverter
  7. {
  8. private const AccessFlags SubpassSrcAccessMask = AccessFlags.MemoryReadBit | AccessFlags.MemoryWriteBit | AccessFlags.ColorAttachmentWriteBit;
  9. private const AccessFlags SubpassDstAccessMask = AccessFlags.MemoryReadBit | AccessFlags.MemoryWriteBit | AccessFlags.ShaderReadBit;
  10. public static unsafe DisposableRenderPass ToRenderPass(this ProgramPipelineState state, VulkanRenderer gd, Device device)
  11. {
  12. const int MaxAttachments = Constants.MaxRenderTargets + 1;
  13. AttachmentDescription[] attachmentDescs = null;
  14. var subpass = new SubpassDescription()
  15. {
  16. PipelineBindPoint = PipelineBindPoint.Graphics
  17. };
  18. AttachmentReference* attachmentReferences = stackalloc AttachmentReference[MaxAttachments];
  19. Span<int> attachmentIndices = stackalloc int[MaxAttachments];
  20. Span<Silk.NET.Vulkan.Format> attachmentFormats = stackalloc Silk.NET.Vulkan.Format[MaxAttachments];
  21. int attachmentCount = 0;
  22. int colorCount = 0;
  23. int maxColorAttachmentIndex = -1;
  24. for (int i = 0; i < state.AttachmentEnable.Length; i++)
  25. {
  26. if (state.AttachmentEnable[i])
  27. {
  28. attachmentFormats[attachmentCount] = gd.FormatCapabilities.ConvertToVkFormat(state.AttachmentFormats[i]);
  29. attachmentIndices[attachmentCount++] = i;
  30. colorCount++;
  31. maxColorAttachmentIndex = i;
  32. }
  33. }
  34. if (state.DepthStencilEnable)
  35. {
  36. attachmentFormats[attachmentCount++] = gd.FormatCapabilities.ConvertToVkFormat(state.DepthStencilFormat);
  37. }
  38. if (attachmentCount != 0)
  39. {
  40. attachmentDescs = new AttachmentDescription[attachmentCount];
  41. for (int i = 0; i < attachmentCount; i++)
  42. {
  43. int bindIndex = attachmentIndices[i];
  44. attachmentDescs[i] = new AttachmentDescription(
  45. 0,
  46. attachmentFormats[i],
  47. TextureStorage.ConvertToSampleCountFlags(gd.Capabilities.SupportedSampleCounts, (uint)state.SamplesCount),
  48. AttachmentLoadOp.Load,
  49. AttachmentStoreOp.Store,
  50. AttachmentLoadOp.Load,
  51. AttachmentStoreOp.Store,
  52. ImageLayout.General,
  53. ImageLayout.General);
  54. }
  55. int colorAttachmentsCount = colorCount;
  56. if (colorAttachmentsCount > MaxAttachments - 1)
  57. {
  58. colorAttachmentsCount = MaxAttachments - 1;
  59. }
  60. if (colorAttachmentsCount != 0)
  61. {
  62. subpass.ColorAttachmentCount = (uint)maxColorAttachmentIndex + 1;
  63. subpass.PColorAttachments = &attachmentReferences[0];
  64. // Fill with VK_ATTACHMENT_UNUSED to cover any gaps.
  65. for (int i = 0; i <= maxColorAttachmentIndex; i++)
  66. {
  67. subpass.PColorAttachments[i] = new AttachmentReference(Vk.AttachmentUnused, ImageLayout.Undefined);
  68. }
  69. for (int i = 0; i < colorAttachmentsCount; i++)
  70. {
  71. int bindIndex = attachmentIndices[i];
  72. subpass.PColorAttachments[bindIndex] = new AttachmentReference((uint)i, ImageLayout.General);
  73. }
  74. }
  75. if (state.DepthStencilEnable)
  76. {
  77. uint dsIndex = (uint)attachmentCount - 1;
  78. subpass.PDepthStencilAttachment = &attachmentReferences[MaxAttachments - 1];
  79. *subpass.PDepthStencilAttachment = new AttachmentReference(dsIndex, ImageLayout.General);
  80. }
  81. }
  82. var subpassDependency = CreateSubpassDependency();
  83. fixed (AttachmentDescription* pAttachmentDescs = attachmentDescs)
  84. {
  85. var renderPassCreateInfo = new RenderPassCreateInfo()
  86. {
  87. SType = StructureType.RenderPassCreateInfo,
  88. PAttachments = pAttachmentDescs,
  89. AttachmentCount = attachmentDescs != null ? (uint)attachmentDescs.Length : 0,
  90. PSubpasses = &subpass,
  91. SubpassCount = 1,
  92. PDependencies = &subpassDependency,
  93. DependencyCount = 1
  94. };
  95. gd.Api.CreateRenderPass(device, renderPassCreateInfo, null, out var renderPass).ThrowOnError();
  96. return new DisposableRenderPass(gd.Api, device, renderPass);
  97. }
  98. }
  99. public static SubpassDependency CreateSubpassDependency()
  100. {
  101. return new SubpassDependency(
  102. 0,
  103. 0,
  104. PipelineStageFlags.AllGraphicsBit,
  105. PipelineStageFlags.AllGraphicsBit,
  106. SubpassSrcAccessMask,
  107. SubpassDstAccessMask,
  108. 0);
  109. }
  110. public unsafe static SubpassDependency2 CreateSubpassDependency2()
  111. {
  112. return new SubpassDependency2(
  113. StructureType.SubpassDependency2,
  114. null,
  115. 0,
  116. 0,
  117. PipelineStageFlags.AllGraphicsBit,
  118. PipelineStageFlags.AllGraphicsBit,
  119. SubpassSrcAccessMask,
  120. SubpassDstAccessMask,
  121. 0);
  122. }
  123. public static PipelineState ToVulkanPipelineState(this ProgramPipelineState state, VulkanRenderer gd)
  124. {
  125. PipelineState pipeline = new PipelineState();
  126. pipeline.Initialize();
  127. // It is assumed that Dynamic State is enabled when this conversion is used.
  128. pipeline.CullMode = state.CullEnable ? state.CullMode.Convert() : CullModeFlags.None;
  129. pipeline.DepthBoundsTestEnable = false; // Not implemented.
  130. pipeline.DepthClampEnable = state.DepthClampEnable;
  131. pipeline.DepthTestEnable = state.DepthTest.TestEnable;
  132. pipeline.DepthWriteEnable = state.DepthTest.WriteEnable;
  133. pipeline.DepthCompareOp = state.DepthTest.Func.Convert();
  134. pipeline.FrontFace = state.FrontFace.Convert();
  135. pipeline.HasDepthStencil = state.DepthStencilEnable;
  136. pipeline.LineWidth = state.LineWidth;
  137. pipeline.LogicOpEnable = state.LogicOpEnable;
  138. pipeline.LogicOp = state.LogicOp.Convert();
  139. pipeline.MinDepthBounds = 0f; // Not implemented.
  140. pipeline.MaxDepthBounds = 0f; // Not implemented.
  141. pipeline.PatchControlPoints = state.PatchControlPoints;
  142. pipeline.PolygonMode = Silk.NET.Vulkan.PolygonMode.Fill; // Not implemented.
  143. pipeline.PrimitiveRestartEnable = state.PrimitiveRestartEnable;
  144. pipeline.RasterizerDiscardEnable = state.RasterizerDiscard;
  145. pipeline.SamplesCount = (uint)state.SamplesCount;
  146. if (gd.Capabilities.SupportsMultiView)
  147. {
  148. pipeline.ScissorsCount = Constants.MaxViewports;
  149. pipeline.ViewportsCount = Constants.MaxViewports;
  150. }
  151. else
  152. {
  153. pipeline.ScissorsCount = 1;
  154. pipeline.ViewportsCount = 1;
  155. }
  156. pipeline.DepthBiasEnable = state.BiasEnable != 0;
  157. // Stencil masks and ref are dynamic, so are 0 in the Vulkan pipeline.
  158. pipeline.StencilFrontFailOp = state.StencilTest.FrontSFail.Convert();
  159. pipeline.StencilFrontPassOp = state.StencilTest.FrontDpPass.Convert();
  160. pipeline.StencilFrontDepthFailOp = state.StencilTest.FrontDpFail.Convert();
  161. pipeline.StencilFrontCompareOp = state.StencilTest.FrontFunc.Convert();
  162. pipeline.StencilFrontCompareMask = 0;
  163. pipeline.StencilFrontWriteMask = 0;
  164. pipeline.StencilFrontReference = 0;
  165. pipeline.StencilBackFailOp = state.StencilTest.BackSFail.Convert();
  166. pipeline.StencilBackPassOp = state.StencilTest.BackDpPass.Convert();
  167. pipeline.StencilBackDepthFailOp = state.StencilTest.BackDpFail.Convert();
  168. pipeline.StencilBackCompareOp = state.StencilTest.BackFunc.Convert();
  169. pipeline.StencilBackCompareMask = 0;
  170. pipeline.StencilBackWriteMask = 0;
  171. pipeline.StencilBackReference = 0;
  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. var attribute = state.VertexAttribs[i];
  180. var 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. var vertexBuffer = state.VertexBuffers[i];
  196. if (vertexBuffer.Enable)
  197. {
  198. var 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 = (vertexBuffer.Stride + (alignment - 1)) & -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. var 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. for (int i = 0; i < Constants.MaxRenderTargets; i++)
  237. {
  238. if (state.AttachmentEnable[i])
  239. {
  240. pipeline.Internal.AttachmentFormats[attachmentCount++] = gd.FormatCapabilities.ConvertToVkFormat(state.AttachmentFormats[i]);
  241. maxColorAttachmentIndex = i;
  242. }
  243. }
  244. if (state.DepthStencilEnable)
  245. {
  246. pipeline.Internal.AttachmentFormats[attachmentCount++] = gd.FormatCapabilities.ConvertToVkFormat(state.DepthStencilFormat);
  247. }
  248. pipeline.ColorBlendAttachmentStateCount = (uint)(maxColorAttachmentIndex + 1);
  249. pipeline.VertexAttributeDescriptionsCount = (uint)Math.Min(Constants.MaxVertexAttributes, state.VertexAttribCount);
  250. return pipeline;
  251. }
  252. }
  253. }