Shader specialization for new Vulkan required state (fixes remaining alpha test issues, vertex stretching on AMD on Crash Bandicoot, etc)

This commit is contained in:
gdk 2022-04-10 17:28:44 -03:00 committed by riperiperi
parent 4648e37a31
commit 4f086b0ec9
7 changed files with 52 additions and 27 deletions

View file

@ -1,4 +1,5 @@
using Ryujinx.Common.Logging; using Ryujinx.Common.Logging;
using Ryujinx.Common.Memory;
using Ryujinx.Graphics.GAL; using Ryujinx.Graphics.GAL;
using Ryujinx.Graphics.Gpu.Engine.Types; using Ryujinx.Graphics.Gpu.Engine.Types;
using Ryujinx.Graphics.Gpu.Image; using Ryujinx.Graphics.Gpu.Image;
@ -1267,6 +1268,20 @@ namespace Ryujinx.Graphics.Gpu.Engine.Threed
/// <returns>Current GPU channel state</returns> /// <returns>Current GPU channel state</returns>
private GpuChannelGraphicsState GetGraphicsState() private GpuChannelGraphicsState GetGraphicsState()
{ {
ref var vertexAttribState = ref _state.State.VertexAttribState;
Array32<AttributeType> attributeTypes = new Array32<AttributeType>();
for (int location = 0; location < attributeTypes.Length; location++)
{
attributeTypes[location] = vertexAttribState[location].UnpackType() switch
{
3 => AttributeType.Sint,
4 => AttributeType.Uint,
_ => AttributeType.Float
};
}
return new GpuChannelGraphicsState( return new GpuChannelGraphicsState(
_state.State.EarlyZForce, _state.State.EarlyZForce,
_drawState.Topology, _drawState.Topology,
@ -1277,7 +1292,8 @@ namespace Ryujinx.Graphics.Gpu.Engine.Threed
_state.State.PointSize, _state.State.PointSize,
_state.State.AlphaTestEnable, _state.State.AlphaTestEnable,
_state.State.AlphaTestFunc, _state.State.AlphaTestFunc,
_state.State.AlphaTestRef); _state.State.AlphaTestRef,
ref attributeTypes);
} }
private DepthMode GetDepthMode() private DepthMode GetDepthMode()

View file

@ -163,6 +163,8 @@ namespace Ryujinx.Graphics.Gpu.Shader.Cache
_ => PrimitiveTopology.Points _ => PrimitiveTopology.Points
}; };
Array32<AttributeType> attributeTypes = default;
GpuChannelGraphicsState graphicsState = new GpuChannelGraphicsState( GpuChannelGraphicsState graphicsState = new GpuChannelGraphicsState(
accessorHeader.StateFlags.HasFlag(GuestGpuStateFlags.EarlyZForce), accessorHeader.StateFlags.HasFlag(GuestGpuStateFlags.EarlyZForce),
topology, topology,
@ -173,7 +175,8 @@ namespace Ryujinx.Graphics.Gpu.Shader.Cache
1f, 1f,
false, false,
CompareOp.Always, CompareOp.Always,
0f); 0f,
ref attributeTypes);
TransformFeedbackDescriptor[] tfdNew = null; TransformFeedbackDescriptor[] tfdNew = null;

View file

@ -15,7 +15,6 @@ namespace Ryujinx.Graphics.Gpu.Shader
{ {
private readonly GpuChannel _channel; private readonly GpuChannel _channel;
private readonly GpuAccessorState _state; private readonly GpuAccessorState _state;
private readonly AttributeType[] _attributeTypes;
private readonly int _stageIndex; private readonly int _stageIndex;
private readonly bool _compute; private readonly bool _compute;
private readonly bool _isVulkan; private readonly bool _isVulkan;
@ -26,19 +25,16 @@ namespace Ryujinx.Graphics.Gpu.Shader
/// <param name="context">GPU context</param> /// <param name="context">GPU context</param>
/// <param name="channel">GPU channel</param> /// <param name="channel">GPU channel</param>
/// <param name="state">Current GPU state</param> /// <param name="state">Current GPU state</param>
/// <param name="attributeTypes">Type of the vertex attributes consumed by the shader</param>
/// <param name="stageIndex">Graphics shader stage index (0 = Vertex, 4 = Fragment)</param> /// <param name="stageIndex">Graphics shader stage index (0 = Vertex, 4 = Fragment)</param>
public GpuAccessor( public GpuAccessor(
GpuContext context, GpuContext context,
GpuChannel channel, GpuChannel channel,
GpuAccessorState state, GpuAccessorState state,
AttributeType[] attributeTypes,
int stageIndex) : base(context, state.ResourceCounts, stageIndex) int stageIndex) : base(context, state.ResourceCounts, stageIndex)
{ {
_isVulkan = context.Capabilities.Api == TargetApi.Vulkan; _isVulkan = context.Capabilities.Api == TargetApi.Vulkan;
_channel = channel; _channel = channel;
_state = state; _state = state;
_attributeTypes = attributeTypes;
_stageIndex = stageIndex; _stageIndex = stageIndex;
} }
@ -108,12 +104,7 @@ namespace Ryujinx.Graphics.Gpu.Shader
/// <inheritdoc/> /// <inheritdoc/>
public AttributeType QueryAttributeType(int location) public AttributeType QueryAttributeType(int location)
{ {
if (_attributeTypes != null) return _state.GraphicsState.AttributeTypes[location];
{
return _attributeTypes[location];
}
return AttributeType.Float;
} }
/// <inheritdoc/> /// <inheritdoc/>

View file

@ -1,5 +1,7 @@
using Ryujinx.Common.Memory;
using Ryujinx.Graphics.GAL; using Ryujinx.Graphics.GAL;
using Ryujinx.Graphics.Gpu.Engine.Threed; using Ryujinx.Graphics.Gpu.Engine.Threed;
using Ryujinx.Graphics.Shader;
namespace Ryujinx.Graphics.Gpu.Shader namespace Ryujinx.Graphics.Gpu.Shader
{ {
@ -60,6 +62,11 @@ namespace Ryujinx.Graphics.Gpu.Shader
/// </summary> /// </summary>
public readonly float AlphaTestReference; public readonly float AlphaTestReference;
/// <summary>
/// Type of the vertex attributes consumed by the shader.
/// </summary>
public Array32<AttributeType> AttributeTypes;
/// <summary> /// <summary>
/// Creates a new GPU graphics state. /// Creates a new GPU graphics state.
/// </summary> /// </summary>
@ -73,6 +80,7 @@ namespace Ryujinx.Graphics.Gpu.Shader
/// <param name="alphaTestEnable">Indicates whenever alpha test is enabled</param> /// <param name="alphaTestEnable">Indicates whenever alpha test is enabled</param>
/// <param name="alphaTestCompare">When alpha test is enabled, indicates the comparison that decides if the fragment is discarded</param> /// <param name="alphaTestCompare">When alpha test is enabled, indicates the comparison that decides if the fragment is discarded</param>
/// <param name="alphaTestReference">When alpha test is enabled, indicates the value to compare with the fragment output alpha</param> /// <param name="alphaTestReference">When alpha test is enabled, indicates the value to compare with the fragment output alpha</param>
/// <param name="attributeTypes">Type of the vertex attributes consumed by the shader</param>
public GpuChannelGraphicsState( public GpuChannelGraphicsState(
bool earlyZForce, bool earlyZForce,
PrimitiveTopology topology, PrimitiveTopology topology,
@ -83,7 +91,8 @@ namespace Ryujinx.Graphics.Gpu.Shader
float pointSize, float pointSize,
bool alphaTestEnable, bool alphaTestEnable,
CompareOp alphaTestCompare, CompareOp alphaTestCompare,
float alphaTestReference) float alphaTestReference,
ref Array32<AttributeType> attributeTypes)
{ {
EarlyZForce = earlyZForce; EarlyZForce = earlyZForce;
Topology = topology; Topology = topology;
@ -95,6 +104,7 @@ namespace Ryujinx.Graphics.Gpu.Shader
AlphaTestEnable = alphaTestEnable; AlphaTestEnable = alphaTestEnable;
AlphaTestCompare = alphaTestCompare; AlphaTestCompare = alphaTestCompare;
AlphaTestReference = alphaTestReference; AlphaTestReference = alphaTestReference;
AttributeTypes = attributeTypes;
} }
} }
} }

View file

@ -305,18 +305,6 @@ namespace Ryujinx.Graphics.Gpu.Shader
return gpShaders; return gpShaders;
} }
AttributeType[] attributeTypes = new AttributeType[32];
for (int location = 0; location < attributeTypes.Length; location++)
{
attributeTypes[location] = state.VertexAttribState[location].UnpackType() switch
{
3 => AttributeType.Sint,
4 => AttributeType.Uint,
_ => AttributeType.Float
};
}
TransformFeedbackDescriptor[] transformFeedbackDescriptors = GetTransformFeedbackDescriptors(ref state); TransformFeedbackDescriptor[] transformFeedbackDescriptors = GetTransformFeedbackDescriptors(ref state);
ShaderSpecializationState specState = new ShaderSpecializationState(graphicsState, transformFeedbackDescriptors); ShaderSpecializationState specState = new ShaderSpecializationState(graphicsState, transformFeedbackDescriptors);
@ -335,7 +323,7 @@ namespace Ryujinx.Graphics.Gpu.Shader
if (gpuVa != 0) if (gpuVa != 0)
{ {
GpuAccessor gpuAccessor = new GpuAccessor(_context, channel, gpuAccessorState, attributeTypes, stageIndex); GpuAccessor gpuAccessor = new GpuAccessor(_context, channel, gpuAccessorState, stageIndex);
TranslatorContext currentStage = DecodeGraphicsShader(gpuAccessor, api, DefaultFlags, gpuVa); TranslatorContext currentStage = DecodeGraphicsShader(gpuAccessor, api, DefaultFlags, gpuVa);
if (nextStage != null) if (nextStage != null)

View file

@ -455,6 +455,23 @@ namespace Ryujinx.Graphics.Gpu.Shader
return false; return false;
} }
if (graphicsState.DepthMode != GraphicsState.DepthMode)
{
return false;
}
if (graphicsState.AlphaTestEnable != GraphicsState.AlphaTestEnable ||
graphicsState.AlphaTestCompare != GraphicsState.AlphaTestCompare ||
graphicsState.AlphaTestReference != GraphicsState.AlphaTestReference)
{
return false;
}
if (!graphicsState.AttributeTypes.ToSpan().SequenceEqual(GraphicsState.AttributeTypes.ToSpan()))
{
return false;
}
return Matches(channel, poolState, checkTextures, isCompute: false); return Matches(channel, poolState, checkTextures, isCompute: false);
} }

View file

@ -2,7 +2,7 @@ using System;
namespace Ryujinx.Graphics.Shader namespace Ryujinx.Graphics.Shader
{ {
public enum AttributeType public enum AttributeType : byte
{ {
Float, Float,
Sint, Sint,