PipelineConverter.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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.BlendConstantA = state.BlendDescriptors[0].BlendConstant.Alpha;
  111. pipeline.BlendConstantB = state.BlendDescriptors[0].BlendConstant.Blue;
  112. pipeline.BlendConstantG = state.BlendDescriptors[0].BlendConstant.Green;
  113. pipeline.BlendConstantR = state.BlendDescriptors[0].BlendConstant.Red;
  114. pipeline.CullMode = state.CullEnable ? state.CullMode.Convert() : CullModeFlags.CullModeNone;
  115. pipeline.DepthBoundsTestEnable = false; // Not implemented.
  116. pipeline.DepthClampEnable = state.DepthClampEnable;
  117. pipeline.DepthTestEnable = state.DepthTest.TestEnable;
  118. pipeline.DepthWriteEnable = state.DepthTest.WriteEnable;
  119. pipeline.DepthCompareOp = state.DepthTest.Func.Convert();
  120. pipeline.FrontFace = state.FrontFace.Convert();
  121. pipeline.HasDepthStencil = state.DepthStencilEnable;
  122. pipeline.LineWidth = state.LineWidth;
  123. pipeline.LogicOpEnable = state.LogicOpEnable;
  124. pipeline.LogicOp = state.LogicOp.Convert();
  125. pipeline.MinDepthBounds = 0f; // Not implemented.
  126. pipeline.MaxDepthBounds = 0f; // Not implemented.
  127. pipeline.PatchControlPoints = state.PatchControlPoints;
  128. pipeline.PolygonMode = Silk.NET.Vulkan.PolygonMode.Fill; // Not implemented.
  129. pipeline.PrimitiveRestartEnable = state.PrimitiveRestartEnable;
  130. pipeline.RasterizerDiscardEnable = state.RasterizerDiscard;
  131. pipeline.SamplesCount = (uint)state.SamplesCount;
  132. if (gd.Capabilities.SupportsMultiView)
  133. {
  134. pipeline.ScissorsCount = Constants.MaxViewports;
  135. pipeline.ViewportsCount = Constants.MaxViewports;
  136. }
  137. else
  138. {
  139. pipeline.ScissorsCount = 1;
  140. pipeline.ViewportsCount = 1;
  141. }
  142. pipeline.DepthBiasEnable = state.BiasEnable != 0;
  143. // Stencil masks and ref are dynamic, so are 0 in the Vulkan pipeline.
  144. pipeline.StencilFrontFailOp = state.StencilTest.FrontSFail.Convert();
  145. pipeline.StencilFrontPassOp = state.StencilTest.FrontDpPass.Convert();
  146. pipeline.StencilFrontDepthFailOp = state.StencilTest.FrontDpFail.Convert();
  147. pipeline.StencilFrontCompareOp = state.StencilTest.FrontFunc.Convert();
  148. pipeline.StencilFrontCompareMask = 0;
  149. pipeline.StencilFrontWriteMask = 0;
  150. pipeline.StencilFrontReference = 0;
  151. pipeline.StencilBackFailOp = state.StencilTest.BackSFail.Convert();
  152. pipeline.StencilBackPassOp = state.StencilTest.BackDpPass.Convert();
  153. pipeline.StencilBackDepthFailOp = state.StencilTest.BackDpFail.Convert();
  154. pipeline.StencilBackCompareOp = state.StencilTest.BackFunc.Convert();
  155. pipeline.StencilBackCompareMask = 0;
  156. pipeline.StencilBackWriteMask = 0;
  157. pipeline.StencilBackReference = 0;
  158. pipeline.StencilTestEnable = state.StencilTest.TestEnable;
  159. pipeline.Topology = gd.TopologyRemap(state.Topology).Convert();
  160. int vaCount = Math.Min(Constants.MaxVertexAttributes, state.VertexAttribCount);
  161. int vbCount = Math.Min(Constants.MaxVertexBuffers, state.VertexBufferCount);
  162. Span<int> vbScalarSizes = stackalloc int[vbCount];
  163. for (int i = 0; i < vaCount; i++)
  164. {
  165. var attribute = state.VertexAttribs[i];
  166. var bufferIndex = attribute.IsZero ? 0 : attribute.BufferIndex + 1;
  167. pipeline.Internal.VertexAttributeDescriptions[i] = new VertexInputAttributeDescription(
  168. (uint)i,
  169. (uint)bufferIndex,
  170. gd.FormatCapabilities.ConvertToVertexVkFormat(attribute.Format),
  171. (uint)attribute.Offset);
  172. if (!attribute.IsZero && bufferIndex < vbCount)
  173. {
  174. vbScalarSizes[bufferIndex - 1] = Math.Max(attribute.Format.GetScalarSize(), vbScalarSizes[bufferIndex - 1]);
  175. }
  176. }
  177. int descriptorIndex = 1;
  178. pipeline.Internal.VertexBindingDescriptions[0] = new VertexInputBindingDescription(0, 0, VertexInputRate.Vertex);
  179. for (int i = 0; i < vbCount; i++)
  180. {
  181. var vertexBuffer = state.VertexBuffers[i];
  182. if (vertexBuffer.Enable)
  183. {
  184. var inputRate = vertexBuffer.Divisor != 0 ? VertexInputRate.Instance : VertexInputRate.Vertex;
  185. int alignedStride = vertexBuffer.Stride;
  186. if (gd.NeedsVertexBufferAlignment(vbScalarSizes[i], out int alignment))
  187. {
  188. alignedStride = (vertexBuffer.Stride + (alignment - 1)) & -alignment;
  189. }
  190. // TODO: Support divisor > 1
  191. pipeline.Internal.VertexBindingDescriptions[descriptorIndex++] = new VertexInputBindingDescription(
  192. (uint)i + 1,
  193. (uint)alignedStride,
  194. inputRate);
  195. }
  196. }
  197. pipeline.VertexBindingDescriptionsCount = (uint)descriptorIndex;
  198. // NOTE: Viewports, Scissors are dynamic.
  199. for (int i = 0; i < 8; i++)
  200. {
  201. var blend = state.BlendDescriptors[i];
  202. if (blend.Enable && state.ColorWriteMask[i] != 0)
  203. {
  204. pipeline.Internal.ColorBlendAttachmentState[i] = new PipelineColorBlendAttachmentState(
  205. blend.Enable,
  206. blend.ColorSrcFactor.Convert(),
  207. blend.ColorDstFactor.Convert(),
  208. blend.ColorOp.Convert(),
  209. blend.AlphaSrcFactor.Convert(),
  210. blend.AlphaDstFactor.Convert(),
  211. blend.AlphaOp.Convert(),
  212. (ColorComponentFlags)state.ColorWriteMask[i]);
  213. }
  214. else
  215. {
  216. pipeline.Internal.ColorBlendAttachmentState[i] = new PipelineColorBlendAttachmentState(
  217. colorWriteMask: (ColorComponentFlags)state.ColorWriteMask[i]);
  218. }
  219. }
  220. int maxAttachmentIndex = 0;
  221. for (int i = 0; i < 8; i++)
  222. {
  223. if (state.AttachmentEnable[i])
  224. {
  225. pipeline.Internal.AttachmentFormats[maxAttachmentIndex++] = gd.FormatCapabilities.ConvertToVkFormat(state.AttachmentFormats[i]);
  226. }
  227. }
  228. if (state.DepthStencilEnable)
  229. {
  230. pipeline.Internal.AttachmentFormats[maxAttachmentIndex++] = gd.FormatCapabilities.ConvertToVkFormat(state.DepthStencilFormat);
  231. }
  232. pipeline.ColorBlendAttachmentStateCount = 8;
  233. pipeline.VertexAttributeDescriptionsCount = (uint)Math.Min(Constants.MaxVertexAttributes, state.VertexAttribCount);
  234. return pipeline;
  235. }
  236. }
  237. }