GpuChannelGraphicsState.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. using Ryujinx.Common.Memory;
  2. using Ryujinx.Graphics.GAL;
  3. using Ryujinx.Graphics.Gpu.Engine.Threed;
  4. using Ryujinx.Graphics.Shader;
  5. namespace Ryujinx.Graphics.Gpu.Shader
  6. {
  7. /// <summary>
  8. /// State used by the <see cref="GpuAccessor"/>.
  9. /// </summary>
  10. struct GpuChannelGraphicsState
  11. {
  12. // New fields should be added to the end of the struct to keep disk shader cache compatibility.
  13. /// <summary>
  14. /// Early Z force enable.
  15. /// </summary>
  16. public bool EarlyZForce;
  17. /// <summary>
  18. /// Primitive topology of current draw.
  19. /// </summary>
  20. public PrimitiveTopology Topology;
  21. /// <summary>
  22. /// Tessellation mode.
  23. /// </summary>
  24. public TessMode TessellationMode;
  25. /// <summary>
  26. /// Indicates whether alpha-to-coverage is enabled.
  27. /// </summary>
  28. public bool AlphaToCoverageEnable;
  29. /// <summary>
  30. /// Indicates whether alpha-to-coverage dithering is enabled.
  31. /// </summary>
  32. public bool AlphaToCoverageDitherEnable;
  33. /// <summary>
  34. /// Indicates whether the viewport transform is disabled.
  35. /// </summary>
  36. public bool ViewportTransformDisable;
  37. /// <summary>
  38. /// Depth mode zero to one or minus one to one.
  39. /// </summary>
  40. public bool DepthMode;
  41. /// <summary>
  42. /// Indicates if the point size is set on the shader or is fixed.
  43. /// </summary>
  44. public bool ProgramPointSizeEnable;
  45. /// <summary>
  46. /// Point size used if <see cref="ProgramPointSizeEnable" /> is false.
  47. /// </summary>
  48. public float PointSize;
  49. /// <summary>
  50. /// Indicates whether alpha test is enabled.
  51. /// </summary>
  52. public bool AlphaTestEnable;
  53. /// <summary>
  54. /// When alpha test is enabled, indicates the comparison that decides if the fragment should be discarded.
  55. /// </summary>
  56. public CompareOp AlphaTestCompare;
  57. /// <summary>
  58. /// When alpha test is enabled, indicates the value to compare with the fragment output alpha.
  59. /// </summary>
  60. public float AlphaTestReference;
  61. /// <summary>
  62. /// Type of the vertex attributes consumed by the shader.
  63. /// </summary>
  64. public Array32<AttributeType> AttributeTypes;
  65. /// <summary>
  66. /// Indicates that the draw is writing the base vertex, base instance and draw index to Constant Buffer 0.
  67. /// </summary>
  68. public bool HasConstantBufferDrawParameters;
  69. /// <summary>
  70. /// Indicates that any storage buffer use is unaligned.
  71. /// </summary>
  72. public bool HasUnalignedStorageBuffer;
  73. /// <summary>
  74. /// Type of the fragment shader outputs.
  75. /// </summary>
  76. public Array8<AttributeType> FragmentOutputTypes;
  77. /// <summary>
  78. /// Indicates whether dual source blend is enabled.
  79. /// </summary>
  80. public bool DualSourceBlendEnable;
  81. /// <summary>
  82. /// Indicates whether Y negate of the fragment coordinates is enabled.
  83. /// </summary>
  84. public bool YNegateEnabled;
  85. /// <summary>
  86. /// Creates a new graphics state from this state that can be used for shader generation.
  87. /// </summary>
  88. /// <param name="hostSupportsAlphaTest">Indicates if the host API supports alpha test operations</param>
  89. /// <param name="hostSupportsQuads">Indicates if the host API supports quad primitives</param>
  90. /// <param name="hasGeometryShader">Indicates if a geometry shader is used</param>
  91. /// <param name="originUpperLeft">If true, indicates that the fragment origin is the upper left corner of the viewport, otherwise it is the lower left corner</param>
  92. /// <returns>GPU graphics state that can be used for shader translation</returns>
  93. public readonly GpuGraphicsState CreateShaderGraphicsState(bool hostSupportsAlphaTest, bool hostSupportsQuads, bool hasGeometryShader, bool originUpperLeft)
  94. {
  95. AlphaTestOp alphaTestOp;
  96. if (hostSupportsAlphaTest || !AlphaTestEnable)
  97. {
  98. alphaTestOp = AlphaTestOp.Always;
  99. }
  100. else
  101. {
  102. alphaTestOp = AlphaTestCompare switch
  103. {
  104. CompareOp.Never or CompareOp.NeverGl => AlphaTestOp.Never,
  105. CompareOp.Less or CompareOp.LessGl => AlphaTestOp.Less,
  106. CompareOp.Equal or CompareOp.EqualGl => AlphaTestOp.Equal,
  107. CompareOp.LessOrEqual or CompareOp.LessOrEqualGl => AlphaTestOp.LessOrEqual,
  108. CompareOp.Greater or CompareOp.GreaterGl => AlphaTestOp.Greater,
  109. CompareOp.NotEqual or CompareOp.NotEqualGl => AlphaTestOp.NotEqual,
  110. CompareOp.GreaterOrEqual or CompareOp.GreaterOrEqualGl => AlphaTestOp.GreaterOrEqual,
  111. _ => AlphaTestOp.Always,
  112. };
  113. }
  114. bool isQuad = Topology == PrimitiveTopology.Quads || Topology == PrimitiveTopology.QuadStrip;
  115. bool halvePrimitiveId = !hostSupportsQuads && !hasGeometryShader && isQuad;
  116. return new GpuGraphicsState(
  117. EarlyZForce,
  118. ConvertToInputTopology(Topology, TessellationMode),
  119. TessellationMode.UnpackCw(),
  120. TessellationMode.UnpackPatchType(),
  121. TessellationMode.UnpackSpacing(),
  122. AlphaToCoverageEnable,
  123. AlphaToCoverageDitherEnable,
  124. ViewportTransformDisable,
  125. DepthMode,
  126. ProgramPointSizeEnable,
  127. PointSize,
  128. alphaTestOp,
  129. AlphaTestReference,
  130. in AttributeTypes,
  131. HasConstantBufferDrawParameters,
  132. in FragmentOutputTypes,
  133. DualSourceBlendEnable,
  134. YNegateEnabled,
  135. originUpperLeft,
  136. halvePrimitiveId);
  137. }
  138. /// <summary>
  139. /// Converts the Maxwell primitive topology to the shader translator topology.
  140. /// </summary>
  141. /// <param name="topology">Maxwell primitive topology</param>
  142. /// <param name="tessellationMode">Maxwell tessellation mode</param>
  143. /// <returns>Shader translator topology</returns>
  144. private static InputTopology ConvertToInputTopology(PrimitiveTopology topology, TessMode tessellationMode)
  145. {
  146. return topology switch
  147. {
  148. PrimitiveTopology.Points => InputTopology.Points,
  149. PrimitiveTopology.Lines or
  150. PrimitiveTopology.LineLoop or
  151. PrimitiveTopology.LineStrip => InputTopology.Lines,
  152. PrimitiveTopology.LinesAdjacency or
  153. PrimitiveTopology.LineStripAdjacency => InputTopology.LinesAdjacency,
  154. PrimitiveTopology.Triangles or
  155. PrimitiveTopology.TriangleStrip or
  156. PrimitiveTopology.TriangleFan => InputTopology.Triangles,
  157. PrimitiveTopology.TrianglesAdjacency or
  158. PrimitiveTopology.TriangleStripAdjacency => InputTopology.TrianglesAdjacency,
  159. PrimitiveTopology.Patches => tessellationMode.UnpackPatchType() == TessPatchType.Isolines
  160. ? InputTopology.Lines
  161. : InputTopology.Triangles,
  162. _ => InputTopology.Points,
  163. };
  164. }
  165. }
  166. }