PipelineConverter.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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. public static unsafe DisposableRenderPass ToRenderPass(this ProgramPipelineState state, VulkanRenderer gd, Device device)
  9. {
  10. const int MaxAttachments = Constants.MaxRenderTargets + 1;
  11. AttachmentDescription[] attachmentDescs = null;
  12. var subpass = new SubpassDescription()
  13. {
  14. PipelineBindPoint = PipelineBindPoint.Graphics
  15. };
  16. AttachmentReference* attachmentReferences = stackalloc AttachmentReference[MaxAttachments];
  17. Span<int> attachmentIndices = stackalloc int[MaxAttachments];
  18. Span<Silk.NET.Vulkan.Format> attachmentFormats = stackalloc Silk.NET.Vulkan.Format[MaxAttachments];
  19. int attachmentCount = 0;
  20. int colorCount = 0;
  21. int maxColorAttachmentIndex = 0;
  22. for (int i = 0; i < state.AttachmentEnable.Length; i++)
  23. {
  24. if (state.AttachmentEnable[i])
  25. {
  26. maxColorAttachmentIndex = i;
  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((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 = new SubpassDependency(
  82. 0,
  83. 0,
  84. PipelineStageFlags.PipelineStageAllGraphicsBit,
  85. PipelineStageFlags.PipelineStageAllGraphicsBit,
  86. AccessFlags.AccessMemoryReadBit | AccessFlags.AccessMemoryWriteBit,
  87. AccessFlags.AccessMemoryReadBit | AccessFlags.AccessMemoryWriteBit,
  88. 0);
  89. fixed (AttachmentDescription* pAttachmentDescs = attachmentDescs)
  90. {
  91. var renderPassCreateInfo = new RenderPassCreateInfo()
  92. {
  93. SType = StructureType.RenderPassCreateInfo,
  94. PAttachments = pAttachmentDescs,
  95. AttachmentCount = attachmentDescs != null ? (uint)attachmentDescs.Length : 0,
  96. PSubpasses = &subpass,
  97. SubpassCount = 1,
  98. PDependencies = &subpassDependency,
  99. DependencyCount = 1
  100. };
  101. gd.Api.CreateRenderPass(device, renderPassCreateInfo, null, out var renderPass).ThrowOnError();
  102. return new DisposableRenderPass(gd.Api, device, renderPass);
  103. }
  104. }
  105. public static PipelineState ToVulkanPipelineState(this ProgramPipelineState state, VulkanRenderer gd)
  106. {
  107. PipelineState pipeline = new PipelineState();
  108. pipeline.Initialize();
  109. // It is assumed that Dynamic State is enabled when this conversion is used.
  110. pipeline.CullMode = state.CullEnable ? state.CullMode.Convert() : CullModeFlags.CullModeNone;
  111. pipeline.DepthBoundsTestEnable = false; // Not implemented.
  112. pipeline.DepthClampEnable = state.DepthClampEnable;
  113. pipeline.DepthTestEnable = state.DepthTest.TestEnable;
  114. pipeline.DepthWriteEnable = state.DepthTest.WriteEnable;
  115. pipeline.DepthCompareOp = state.DepthTest.Func.Convert();
  116. pipeline.FrontFace = state.FrontFace.Convert();
  117. pipeline.HasDepthStencil = state.DepthStencilEnable;
  118. pipeline.LineWidth = state.LineWidth;
  119. pipeline.LogicOpEnable = state.LogicOpEnable;
  120. pipeline.LogicOp = state.LogicOp.Convert();
  121. pipeline.MinDepthBounds = 0f; // Not implemented.
  122. pipeline.MaxDepthBounds = 0f; // Not implemented.
  123. pipeline.PatchControlPoints = state.PatchControlPoints;
  124. pipeline.PolygonMode = Silk.NET.Vulkan.PolygonMode.Fill; // Not implemented.
  125. pipeline.PrimitiveRestartEnable = state.PrimitiveRestartEnable;
  126. pipeline.RasterizerDiscardEnable = state.RasterizerDiscard;
  127. pipeline.SamplesCount = (uint)state.SamplesCount;
  128. if (gd.Capabilities.SupportsMultiView)
  129. {
  130. pipeline.ScissorsCount = Constants.MaxViewports;
  131. pipeline.ViewportsCount = Constants.MaxViewports;
  132. }
  133. else
  134. {
  135. pipeline.ScissorsCount = 1;
  136. pipeline.ViewportsCount = 1;
  137. }
  138. pipeline.DepthBiasEnable = state.BiasEnable != 0;
  139. // Stencil masks and ref are dynamic, so are 0 in the Vulkan pipeline.
  140. pipeline.StencilFrontFailOp = state.StencilTest.FrontSFail.Convert();
  141. pipeline.StencilFrontPassOp = state.StencilTest.FrontDpPass.Convert();
  142. pipeline.StencilFrontDepthFailOp = state.StencilTest.FrontDpFail.Convert();
  143. pipeline.StencilFrontCompareOp = state.StencilTest.FrontFunc.Convert();
  144. pipeline.StencilFrontCompareMask = 0;
  145. pipeline.StencilFrontWriteMask = 0;
  146. pipeline.StencilFrontReference = 0;
  147. pipeline.StencilBackFailOp = state.StencilTest.BackSFail.Convert();
  148. pipeline.StencilBackPassOp = state.StencilTest.BackDpPass.Convert();
  149. pipeline.StencilBackDepthFailOp = state.StencilTest.BackDpFail.Convert();
  150. pipeline.StencilBackCompareOp = state.StencilTest.BackFunc.Convert();
  151. pipeline.StencilBackCompareMask = 0;
  152. pipeline.StencilBackWriteMask = 0;
  153. pipeline.StencilBackReference = 0;
  154. pipeline.StencilTestEnable = state.StencilTest.TestEnable;
  155. pipeline.Topology = gd.TopologyRemap(state.Topology).Convert();
  156. int vaCount = Math.Min(Constants.MaxVertexAttributes, state.VertexAttribCount);
  157. int vbCount = Math.Min(Constants.MaxVertexBuffers, state.VertexBufferCount);
  158. Span<int> vbScalarSizes = stackalloc int[vbCount];
  159. for (int i = 0; i < vaCount; i++)
  160. {
  161. var attribute = state.VertexAttribs[i];
  162. var bufferIndex = attribute.IsZero ? 0 : attribute.BufferIndex + 1;
  163. pipeline.Internal.VertexAttributeDescriptions[i] = new VertexInputAttributeDescription(
  164. (uint)i,
  165. (uint)bufferIndex,
  166. gd.FormatCapabilities.ConvertToVertexVkFormat(attribute.Format),
  167. (uint)attribute.Offset);
  168. if (!attribute.IsZero && bufferIndex < vbCount)
  169. {
  170. vbScalarSizes[bufferIndex - 1] = Math.Max(attribute.Format.GetScalarSize(), vbScalarSizes[bufferIndex - 1]);
  171. }
  172. }
  173. int descriptorIndex = 1;
  174. pipeline.Internal.VertexBindingDescriptions[0] = new VertexInputBindingDescription(0, 0, VertexInputRate.Vertex);
  175. for (int i = 0; i < vbCount; i++)
  176. {
  177. var vertexBuffer = state.VertexBuffers[i];
  178. if (vertexBuffer.Enable)
  179. {
  180. var inputRate = vertexBuffer.Divisor != 0 ? VertexInputRate.Instance : VertexInputRate.Vertex;
  181. int alignedStride = vertexBuffer.Stride;
  182. if (gd.NeedsVertexBufferAlignment(vbScalarSizes[i], out int alignment))
  183. {
  184. alignedStride = (vertexBuffer.Stride + (alignment - 1)) & -alignment;
  185. }
  186. // TODO: Support divisor > 1
  187. pipeline.Internal.VertexBindingDescriptions[descriptorIndex++] = new VertexInputBindingDescription(
  188. (uint)i + 1,
  189. (uint)alignedStride,
  190. inputRate);
  191. }
  192. }
  193. pipeline.VertexBindingDescriptionsCount = (uint)descriptorIndex;
  194. // NOTE: Viewports, Scissors are dynamic.
  195. for (int i = 0; i < 8; i++)
  196. {
  197. var blend = state.BlendDescriptors[i];
  198. if (blend.Enable && state.ColorWriteMask[i] != 0)
  199. {
  200. pipeline.Internal.ColorBlendAttachmentState[i] = new PipelineColorBlendAttachmentState(
  201. blend.Enable,
  202. blend.ColorSrcFactor.Convert(),
  203. blend.ColorDstFactor.Convert(),
  204. blend.ColorOp.Convert(),
  205. blend.AlphaSrcFactor.Convert(),
  206. blend.AlphaDstFactor.Convert(),
  207. blend.AlphaOp.Convert(),
  208. (ColorComponentFlags)state.ColorWriteMask[i]);
  209. }
  210. else
  211. {
  212. pipeline.Internal.ColorBlendAttachmentState[i] = new PipelineColorBlendAttachmentState(
  213. colorWriteMask: (ColorComponentFlags)state.ColorWriteMask[i]);
  214. }
  215. }
  216. int maxAttachmentIndex = 0;
  217. for (int i = 0; i < 8; i++)
  218. {
  219. if (state.AttachmentEnable[i])
  220. {
  221. pipeline.Internal.AttachmentFormats[maxAttachmentIndex++] = gd.FormatCapabilities.ConvertToVkFormat(state.AttachmentFormats[i]);
  222. }
  223. }
  224. if (state.DepthStencilEnable)
  225. {
  226. pipeline.Internal.AttachmentFormats[maxAttachmentIndex++] = gd.FormatCapabilities.ConvertToVkFormat(state.DepthStencilFormat);
  227. }
  228. pipeline.ColorBlendAttachmentStateCount = 8;
  229. pipeline.VertexAttributeDescriptionsCount = (uint)Math.Min(Constants.MaxVertexAttributes, state.VertexAttribCount);
  230. return pipeline;
  231. }
  232. }
  233. }