PipelineConverter.cs 13 KB

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