Support shared color mask, implement more shader instructions

Support shared color masks (used by Nouveau and maybe the NVIDIA
driver).
Support draw buffers (also required by OpenGL).
Support viewport transform disable (disabled for now as it breaks some
games).
Fix instanced rendering draw being ignored for multi draw.
Fix IADD and IADD3 immediate shader encodings, that was not matching
some ops.
Implement FFMA32I shader instruction.
Implement IMAD shader instruction.
This commit is contained in:
gdkchan 2019-12-06 19:37:00 -03:00 committed by Thog
parent e25b7c9848
commit cb171f6ebf
32 changed files with 313 additions and 92 deletions

View file

@ -7,7 +7,7 @@ namespace Ryujinx.Graphics.Gpu.Engine
{
private void Clear(GpuState state, int argument)
{
UpdateRenderTargetStateIfNeeded(state);
UpdateRenderTargetState(state, useControl: false);
_textureManager.CommitGraphicsBindings();
@ -49,6 +49,8 @@ namespace Ryujinx.Graphics.Gpu.Engine
stencilValue,
stencilMask);
}
UpdateRenderTargetState(state, useControl: true);
}
}
}

View file

@ -10,7 +10,7 @@ namespace Ryujinx.Graphics.Gpu.Engine
private int _firstIndex;
private int _indexCount;
private bool _instancedHasState;
private bool _instancedDrawPending;
private bool _instancedIndexed;
private int _instancedFirstIndex;
@ -32,9 +32,9 @@ namespace Ryujinx.Graphics.Gpu.Engine
if (instanced)
{
if (!_instancedHasState)
if (!_instancedDrawPending)
{
_instancedHasState = true;
_instancedDrawPending = true;
_instancedIndexed = _drawIndexed;
@ -82,20 +82,22 @@ namespace Ryujinx.Graphics.Gpu.Engine
private void DrawBegin(GpuState state, int argument)
{
PrimitiveType type = (PrimitiveType)(argument & 0xffff);
_context.Renderer.Pipeline.SetPrimitiveTopology(type.Convert());
PrimitiveType = type;
if ((argument & (1 << 26)) != 0)
{
_instanceIndex++;
}
else if ((argument & (1 << 27)) == 0)
{
PerformDeferredDraws();
_instanceIndex = 0;
}
PrimitiveType type = (PrimitiveType)(argument & 0xffff);
_context.Renderer.Pipeline.SetPrimitiveTopology(type.Convert());
PrimitiveType = type;
}
private void SetIndexBufferCount(GpuState state, int argument)
@ -106,9 +108,9 @@ namespace Ryujinx.Graphics.Gpu.Engine
public void PerformDeferredDraws()
{
// Perform any pending instanced draw.
if (_instancedHasState)
if (_instancedDrawPending)
{
_instancedHasState = false;
_instancedDrawPending = false;
if (_instancedIndexed)
{

View file

@ -1,4 +1,3 @@
using Ryujinx.Graphics.Gpu.Memory;
using Ryujinx.Graphics.Gpu.State;
namespace Ryujinx.Graphics.Gpu.Engine
@ -9,11 +8,6 @@ namespace Ryujinx.Graphics.Gpu.Engine
{
var uniformBuffer = state.Get<UniformBufferState>(MethodOffset.UniformBufferState);
if (_context.MemoryManager.Translate(uniformBuffer.Address.Pack() + (uint)uniformBuffer.Offset) == MemoryManager.BadAddress)
{
return;
}
_context.MemoryAccessor.Write(uniformBuffer.Address.Pack() + (uint)uniformBuffer.Offset, argument);
state.SetUniformBufferOffset(uniformBuffer.Offset + 4);

View file

@ -86,7 +86,14 @@ namespace Ryujinx.Graphics.Gpu.Engine
UpdateShaderState(state);
}
UpdateRenderTargetStateIfNeeded(state);
if (state.QueryModified(MethodOffset.RtColorState,
MethodOffset.RtDepthStencilState,
MethodOffset.RtControl,
MethodOffset.RtDepthStencilSize,
MethodOffset.RtDepthStencilEnable))
{
UpdateRenderTargetState(state, useControl: true);
}
if (state.QueryModified(MethodOffset.DepthTestEnable,
MethodOffset.DepthWriteEnable,
@ -155,7 +162,7 @@ namespace Ryujinx.Graphics.Gpu.Engine
UpdateFaceState(state);
}
if (state.QueryModified(MethodOffset.RtColorMask))
if (state.QueryModified(MethodOffset.RtColorMaskShared, MethodOffset.RtColorMask))
{
UpdateRtColorMask(state);
}
@ -210,19 +217,12 @@ namespace Ryujinx.Graphics.Gpu.Engine
}
}
private void UpdateRenderTargetStateIfNeeded(GpuState state)
private void UpdateRenderTargetState(GpuState state, bool useControl)
{
if (state.QueryModified(MethodOffset.RtColorState,
MethodOffset.RtDepthStencilState,
MethodOffset.RtDepthStencilSize,
MethodOffset.RtDepthStencilEnable))
{
UpdateRenderTargetState(state);
}
}
var rtControl = state.Get<RtControl>(MethodOffset.RtControl);
int count = useControl ? rtControl.UnpackCount() : Constants.TotalRenderTargets;
private void UpdateRenderTargetState(GpuState state)
{
var msaaMode = state.Get<TextureMsaaMode>(MethodOffset.RtMsaaMode);
int samplesInX = msaaMode.SamplesInX();
@ -230,9 +230,11 @@ namespace Ryujinx.Graphics.Gpu.Engine
for (int index = 0; index < Constants.TotalRenderTargets; index++)
{
var colorState = state.Get<RtColorState>(MethodOffset.RtColorState, index);
int rtIndex = useControl ? rtControl.UnpackPermutationIndex(index) : index;
if (!IsRtEnabled(colorState))
var colorState = state.Get<RtColorState>(MethodOffset.RtColorState, rtIndex);
if (index >= count || !IsRtEnabled(colorState))
{
_textureManager.SetRenderTargetColor(index, null);
@ -292,6 +294,8 @@ namespace Ryujinx.Graphics.Gpu.Engine
private void UpdateViewportTransform(GpuState state)
{
bool transformEnable = GetViewportTransformEnable(state);
bool flipY = (state.Get<int>(MethodOffset.YControl) & 1) != 0;
float yFlip = flipY ? -1 : 1;
@ -303,13 +307,35 @@ namespace Ryujinx.Graphics.Gpu.Engine
var transform = state.Get<ViewportTransform>(MethodOffset.ViewportTransform, index);
var extents = state.Get<ViewportExtents> (MethodOffset.ViewportExtents, index);
float x = transform.TranslateX - MathF.Abs(transform.ScaleX);
float y = transform.TranslateY - MathF.Abs(transform.ScaleY);
RectangleF region;
float width = transform.ScaleX * 2;
float height = transform.ScaleY * 2 * yFlip;
if (transformEnable)
{
float x = transform.TranslateX - MathF.Abs(transform.ScaleX);
float y = transform.TranslateY - MathF.Abs(transform.ScaleY);
RectangleF region = new RectangleF(x, y, width, height);
float width = transform.ScaleX * 2;
float height = transform.ScaleY * 2 * yFlip;
region = new RectangleF(x, y, width, height);
}
else
{
// It's not possible to fully disable viewport transform, at least with the most
// common graphics APIs, but we can effectively disable it with a dummy transform.
// The transform is defined as: xw = (width / 2) * xndc + x + (width / 2)
// By setting x to -(width / 2), we effectively remove the translation.
// By setting the width to 2, we remove the scale since 2 / 2 = 1.
// Now, the only problem is the viewport clipping, that we also can't disable.
// To prevent the values from being clipped, we multiply (-1, -1, 2, 2) by
// the maximum supported viewport dimensions.
// This must be compensated on the shader, by dividing the vertex position
// by the maximum viewport dimensions.
float maxSize = (float)_context.Capabilities.MaximumViewportDimensions;
float halfMaxSize = (float)_context.Capabilities.MaximumViewportDimensions * 0.5f;
region = new RectangleF(-halfMaxSize, -halfMaxSize, maxSize, maxSize * yFlip);
}
viewports[index] = new Viewport(
region,
@ -537,11 +563,13 @@ namespace Ryujinx.Graphics.Gpu.Engine
private void UpdateRtColorMask(GpuState state)
{
bool rtColorMaskShared = state.Get<Boolean32>(MethodOffset.RtColorMaskShared);
uint[] componentMasks = new uint[Constants.TotalRenderTargets];
for (int index = 0; index < Constants.TotalRenderTargets; index++)
{
var colorMask = state.Get<RtColorMask>(MethodOffset.RtColorMask, index);
var colorMask = state.Get<RtColorMask>(MethodOffset.RtColorMask, rtColorMaskShared ? 0 : index);
uint componentMask = 0;
@ -634,7 +662,9 @@ namespace Ryujinx.Graphics.Gpu.Engine
addressesArray[index] = baseAddress + shader.Offset;
}
GraphicsShader gs = _shaderCache.GetGraphicsShader(addresses);
bool viewportTransformEnable = GetViewportTransformEnable(state);
GraphicsShader gs = _shaderCache.GetGraphicsShader(addresses, !viewportTransformEnable);
_vsUsesInstanceId = gs.Shader[0].Program.Info.UsesInstanceId;
@ -695,6 +725,14 @@ namespace Ryujinx.Graphics.Gpu.Engine
_context.Renderer.Pipeline.BindProgram(gs.HostProgram);
}
private bool GetViewportTransformEnable(GpuState state)
{
// FIXME: We should read ViewportTransformEnable, but it seems that some games writes 0 there?
// return state.Get<Boolean32>(MethodOffset.ViewportTransformEnable) != 0;
return true;
}
private static Target GetTarget(SamplerType type)
{
type &= ~(SamplerType.Indexed | SamplerType.Shadow);

View file

@ -105,6 +105,8 @@ namespace Ryujinx.Graphics.Gpu
{
case NvGpuFifoMeth.WaitForIdle:
{
_context.Methods.PerformDeferredDraws();
_context.Renderer.FlushPipelines();
break;

View file

@ -69,7 +69,7 @@ namespace Ryujinx.Graphics.Gpu.Shader
return cpShader;
}
public GraphicsShader GetGraphicsShader(ShaderAddresses addresses)
public GraphicsShader GetGraphicsShader(ShaderAddresses addresses, bool dividePosXY)
{
bool isCached = _gpPrograms.TryGetValue(addresses, out List<GraphicsShader> list);
@ -86,19 +86,28 @@ namespace Ryujinx.Graphics.Gpu.Shader
GraphicsShader gpShaders = new GraphicsShader();
TranslationFlags flags =
TranslationFlags.DebugMode |
TranslationFlags.Unspecialized;
if (dividePosXY)
{
flags |= TranslationFlags.DividePosXY;
}
if (addresses.VertexA != 0)
{
gpShaders.Shader[0] = TranslateGraphicsShader(addresses.Vertex, addresses.VertexA);
gpShaders.Shader[0] = TranslateGraphicsShader(flags, addresses.Vertex, addresses.VertexA);
}
else
{
gpShaders.Shader[0] = TranslateGraphicsShader(addresses.Vertex);
gpShaders.Shader[0] = TranslateGraphicsShader(flags, addresses.Vertex);
}
gpShaders.Shader[1] = TranslateGraphicsShader(addresses.TessControl);
gpShaders.Shader[2] = TranslateGraphicsShader(addresses.TessEvaluation);
gpShaders.Shader[3] = TranslateGraphicsShader(addresses.Geometry);
gpShaders.Shader[4] = TranslateGraphicsShader(addresses.Fragment);
gpShaders.Shader[1] = TranslateGraphicsShader(flags, addresses.TessControl);
gpShaders.Shader[2] = TranslateGraphicsShader(flags, addresses.TessEvaluation);
gpShaders.Shader[3] = TranslateGraphicsShader(flags, addresses.Geometry);
gpShaders.Shader[4] = TranslateGraphicsShader(flags, addresses.Fragment);
BackpropQualifiers(gpShaders);
@ -218,7 +227,7 @@ namespace Ryujinx.Graphics.Gpu.Shader
return new CachedShader(program, codeCached);
}
private CachedShader TranslateGraphicsShader(ulong gpuVa, ulong gpuVaA = 0)
private CachedShader TranslateGraphicsShader(TranslationFlags flags, ulong gpuVa, ulong gpuVaA = 0)
{
if (gpuVa == 0)
{
@ -227,10 +236,6 @@ namespace Ryujinx.Graphics.Gpu.Shader
ShaderProgram program;
const TranslationFlags flags =
TranslationFlags.DebugMode |
TranslationFlags.Unspecialized;
int[] codeCached = null;
if (gpuVaA != 0)
@ -345,7 +350,9 @@ namespace Ryujinx.Graphics.Gpu.Shader
private ShaderCapabilities GetShaderCapabilities()
{
return new ShaderCapabilities(_context.Capabilities.StorageBufferOffsetAlignment);
return new ShaderCapabilities(
_context.Capabilities.MaximumViewportDimensions,
_context.Capabilities.StorageBufferOffsetAlignment);
}
}
}

View file

@ -94,11 +94,17 @@ namespace Ryujinx.Graphics.Gpu.State
_backingMemory[(int)MethodOffset.ViewportExtents + index * 4 + 3] = 0x3F800000;
}
// Viewport transform enable.
_backingMemory[(int)MethodOffset.ViewportTransformEnable] = 1;
// Default front stencil mask.
_backingMemory[0x4e7] = 0xff;
// Default color mask.
_backingMemory[(int)MethodOffset.RtColorMask] = 0x1111;
for (int index = 0; index < Constants.TotalRenderTargets; index++)
{
_backingMemory[(int)MethodOffset.RtColorMask + index] = 0x1111;
}
}
public void RegisterCallback(MethodOffset offset, int count, MethodCallback callback)

View file

@ -29,8 +29,10 @@ namespace Ryujinx.Graphics.Gpu.State
StencilBackMasks = 0x3d5,
InvalidateTextures = 0x3dd,
TextureBarrierTiled = 0x3df,
RtColorMaskShared = 0x3e4,
RtDepthStencilState = 0x3f8,
VertexAttribState = 0x458,
RtControl = 0x487,
RtDepthStencilSize = 0x48a,
SamplerIndex = 0x48d,
DepthTestEnable = 0x4b3,
@ -62,6 +64,7 @@ namespace Ryujinx.Graphics.Gpu.State
DepthBiasClamp = 0x61f,
VertexBufferInstanced = 0x620,
FaceState = 0x646,
ViewportTransformEnable = 0x64b,
Clear = 0x674,
RtColorMask = 0x680,
ReportState = 0x6c0,

View file

@ -0,0 +1,17 @@
namespace Ryujinx.Graphics.Gpu.State
{
struct RtControl
{
public uint Packed;
public int UnpackCount()
{
return (int)(Packed & 0xf);
}
public int UnpackPermutationIndex(int index)
{
return (int)((Packed >> (4 + index * 3)) & 7);
}
}
}