Skip to content

Commit

Permalink
Support VK_EXT_extended_dynamic_state and VK_EXT_extended_dynamic_state2
Browse files Browse the repository at this point in the history
  • Loading branch information
sunshineinabox committed Nov 22, 2024
1 parent 1d42c29 commit 48f9ada
Show file tree
Hide file tree
Showing 26 changed files with 1,386 additions and 539 deletions.
12 changes: 12 additions & 0 deletions src/Ryujinx.Graphics.GAL/Capabilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ public readonly struct Capabilities
public readonly bool SupportsViewportSwizzle;
public readonly bool SupportsIndirectParameters;
public readonly bool SupportsDepthClipControl;
public readonly bool SupportsExtendedDynamicState;
public readonly bool SupportsExtendedDynamicState2;
public readonly bool SupportsLogicOpDynamicState;
public readonly bool SupportsPatchControlPointsDynamicState;

public readonly int UniformBufferSetIndex;
public readonly int StorageBufferSetIndex;
Expand Down Expand Up @@ -118,6 +122,10 @@ public Capabilities(
bool supportsViewportSwizzle,
bool supportsIndirectParameters,
bool supportsDepthClipControl,
bool supportsExtendedDynamicState,
bool supportsExtendedDynamicState2,
bool supportsLogicOpDynamicState,
bool supportsPatchControlPointsDynamicState,
int uniformBufferSetIndex,
int storageBufferSetIndex,
int textureSetIndex,
Expand Down Expand Up @@ -180,6 +188,10 @@ public Capabilities(
SupportsViewportSwizzle = supportsViewportSwizzle;
SupportsIndirectParameters = supportsIndirectParameters;
SupportsDepthClipControl = supportsDepthClipControl;
SupportsExtendedDynamicState = supportsExtendedDynamicState;
SupportsExtendedDynamicState2 = supportsExtendedDynamicState2;
SupportsLogicOpDynamicState = supportsLogicOpDynamicState;
SupportsPatchControlPointsDynamicState = supportsPatchControlPointsDynamicState;
UniformBufferSetIndex = uniformBufferSetIndex;
StorageBufferSetIndex = storageBufferSetIndex;
TextureSetIndex = textureSetIndex;
Expand Down
1 change: 1 addition & 0 deletions src/Ryujinx.Graphics.GAL/Face.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ namespace Ryujinx.Graphics.GAL
{
public enum Face
{
None = 0,
Front = 0x404,
Back = 0x405,
FrontAndBack = 0x408,
Expand Down
14 changes: 7 additions & 7 deletions src/Ryujinx.Graphics.GAL/IPipeline.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ void DrawIndexed(
void SetDepthBias(PolygonModeMask enables, float factor, float units, float clamp);
void SetDepthClamp(bool clamp);
void SetDepthMode(DepthMode mode);
void SetDepthTest(DepthTestDescriptor depthTest);
void SetDepthTest(DepthTestDescriptor depthTest, bool signalChange = true);

void SetFaceCulling(bool enable, Face face);
void SetFaceCulling(Face face);

void SetFrontFace(FrontFace frontFace);

Expand All @@ -75,16 +75,16 @@ void DrawIndexed(

void SetPrimitiveRestart(bool enable, int index);

void SetPrimitiveTopology(PrimitiveTopology topology);
void SetPrimitiveTopology(PrimitiveTopology topology, bool signalChange = true);

void SetProgram(IProgram program);
void SetProgram(IProgram program, bool signalChange = true);

void SetRasterizerDiscard(bool discard);

void SetRenderTargetColorMasks(ReadOnlySpan<uint> componentMask);
void SetRenderTargetColorMasks(ReadOnlySpan<uint> componentMask, bool signalChange = true);
void SetRenderTargets(ITexture[] colors, ITexture depthStencil);

void SetScissors(ReadOnlySpan<Rectangle<int>> regions);
void SetScissors(ReadOnlySpan<Rectangle<int>> regions, bool signalChange = true);

void SetStencilTest(StencilTestDescriptor stencilTest);

Expand All @@ -102,7 +102,7 @@ void DrawIndexed(
void SetVertexAttribs(ReadOnlySpan<VertexAttribDescriptor> vertexAttribs);
void SetVertexBuffers(ReadOnlySpan<VertexBufferDescriptor> vertexBuffers);

void SetViewports(ReadOnlySpan<Viewport> viewports);
void SetViewports(ReadOnlySpan<Viewport> viewports, bool signalChange = true);

void TextureBarrier();
void TextureBarrierTiled();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,16 @@ namespace Ryujinx.Graphics.GAL.Multithreading.Commands
struct SetFaceCullingCommand : IGALCommand, IGALCommand<SetFaceCullingCommand>
{
public readonly CommandType CommandType => CommandType.SetFaceCulling;
private bool _enable;
private Face _face;

public void Set(bool enable, Face face)
public void Set(Face face)
{
_enable = enable;
_face = face;
}

public static void Run(ref SetFaceCullingCommand command, ThreadedRenderer threaded, IRenderer renderer)
{
renderer.Pipeline.SetFaceCulling(command._enable, command._face);
renderer.Pipeline.SetFaceCulling(command._face);
}
}
}
16 changes: 8 additions & 8 deletions src/Ryujinx.Graphics.GAL/Multithreading/ThreadedPipeline.cs
Original file line number Diff line number Diff line change
Expand Up @@ -159,15 +159,15 @@ public void SetDepthMode(DepthMode mode)
_renderer.QueueCommand();
}

public void SetDepthTest(DepthTestDescriptor depthTest)
public void SetDepthTest(DepthTestDescriptor depthTest, bool signalChange = true)
{
_renderer.New<SetDepthTestCommand>().Set(depthTest);
_renderer.QueueCommand();
}

public void SetFaceCulling(bool enable, Face face)
public void SetFaceCulling(Face face)
{
_renderer.New<SetFaceCullingCommand>().Set(enable, face);
_renderer.New<SetFaceCullingCommand>().Set(face);
_renderer.QueueCommand();
}

Expand Down Expand Up @@ -243,13 +243,13 @@ public void SetPrimitiveRestart(bool enable, int index)
_renderer.QueueCommand();
}

public void SetPrimitiveTopology(PrimitiveTopology topology)
public void SetPrimitiveTopology(PrimitiveTopology topology, bool signalChange = true)
{
_renderer.New<SetPrimitiveTopologyCommand>().Set(topology);
_renderer.QueueCommand();
}

public void SetProgram(IProgram program)
public void SetProgram(IProgram program, bool signalChange = true)
{
_renderer.New<SetProgramCommand>().Set(Ref(program));
_renderer.QueueCommand();
Expand All @@ -261,7 +261,7 @@ public void SetRasterizerDiscard(bool discard)
_renderer.QueueCommand();
}

public void SetRenderTargetColorMasks(ReadOnlySpan<uint> componentMask)
public void SetRenderTargetColorMasks(ReadOnlySpan<uint> componentMask, bool signalChange = true)
{
_renderer.New<SetRenderTargetColorMasksCommand>().Set(_renderer.CopySpan(componentMask));
_renderer.QueueCommand();
Expand All @@ -273,7 +273,7 @@ public void SetRenderTargets(ITexture[] colors, ITexture depthStencil)
_renderer.QueueCommand();
}

public void SetScissors(ReadOnlySpan<Rectangle<int>> scissors)
public void SetScissors(ReadOnlySpan<Rectangle<int>> scissors, bool signalChange = true)
{
_renderer.New<SetScissorsCommand>().Set(_renderer.CopySpan(scissors));
_renderer.QueueCommand();
Expand Down Expand Up @@ -339,7 +339,7 @@ public void SetVertexBuffers(ReadOnlySpan<VertexBufferDescriptor> vertexBuffers)
_renderer.QueueCommand();
}

public void SetViewports(ReadOnlySpan<Viewport> viewports)
public void SetViewports(ReadOnlySpan<Viewport> viewports, bool signalChange = true)
{
_renderer.New<SetViewportsCommand>().Set(_renderer.CopySpan(viewports));
_renderer.QueueCommand();
Expand Down
8 changes: 6 additions & 2 deletions src/Ryujinx.Graphics.GAL/ProgramPipelineState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,22 @@ public struct ProgramPipelineState
public StencilTestDescriptor StencilTest;
public FrontFace FrontFace;
public Face CullMode;
public bool CullEnable;

public PolygonModeMask BiasEnable;

public float LineWidth;
public bool AlphaToCoverageEnable;
public bool AlphaToOneEnable;

// TODO: Polygon mode.
public bool DepthClampEnable;
public bool RasterizerDiscard;
public PrimitiveTopology Topology;
public bool PrimitiveRestartEnable;
public uint PatchControlPoints;

public float DepthBiasUnits;
public float DepthBiasFactor;

public DepthMode DepthMode;

public void SetVertexAttribs(ReadOnlySpan<VertexAttribDescriptor> vertexAttribs)
Expand Down
19 changes: 15 additions & 4 deletions src/Ryujinx.Graphics.Gpu/Engine/Threed/StateUpdater.cs
Original file line number Diff line number Diff line change
Expand Up @@ -854,6 +854,9 @@ private void UpdateDepthBiasState()
enables |= (depthBias.FillEnable ? PolygonModeMask.Fill : 0);

_pipeline.BiasEnable = enables;
_pipeline.DepthBiasUnits = units / 2f;
_pipeline.DepthBiasFactor = factor;

_context.Renderer.Pipeline.SetDepthBias(enables, factor, units / 2f, clamp);
}

Expand Down Expand Up @@ -1026,7 +1029,6 @@ private void UpdateLineState()
float width = _state.State.LineWidthSmooth;
bool smooth = _state.State.LineSmoothEnable;

_pipeline.LineWidth = width;
_context.Renderer.Pipeline.SetLineParameters(width, smooth);
}

Expand Down Expand Up @@ -1196,9 +1198,16 @@ private void UpdateFaceState()
var yControl = _state.State.YControl;
var face = _state.State.FaceState;

_pipeline.CullEnable = face.CullEnable;
_pipeline.CullMode = face.CullFace;
_context.Renderer.Pipeline.SetFaceCulling(face.CullEnable, face.CullFace);
if (face.CullEnable)
{
_pipeline.CullMode = face.CullFace;
_context.Renderer.Pipeline.SetFaceCulling(face.CullFace);
}
else
{
_pipeline.CullMode = Face.None;
_context.Renderer.Pipeline.SetFaceCulling(Face.None);
}

UpdateFrontFace(yControl, face.FrontFace);
}
Expand Down Expand Up @@ -1388,6 +1397,8 @@ private void UpdateMultisampleState()
bool alphaToCoverageEnable = (_state.State.MultisampleControl & 1) != 0;
bool alphaToOneEnable = (_state.State.MultisampleControl & 0x10) != 0;

_pipeline.AlphaToCoverageEnable = alphaToCoverageEnable;
_pipeline.AlphaToOneEnable = alphaToOneEnable;
_context.Renderer.Pipeline.SetMultisampleState(new MultisampleDescriptor(
alphaToCoverageEnable,
_state.State.AlphaToCoverageDitherEnable,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class DiskCacheHostStorage
private const ushort FileFormatVersionMajor = 1;
private const ushort FileFormatVersionMinor = 2;
private const uint FileFormatVersionPacked = ((uint)FileFormatVersionMajor << 16) | FileFormatVersionMinor;
private const uint CodeGenVersion = 7353;
private const uint CodeGenVersion = 6877;

private const string SharedTocFileName = "shared.toc";
private const string SharedDataFileName = "shared.data";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ class ParallelDiskCacheLoader
private readonly CancellationToken _cancellationToken;
private readonly Action<ShaderCacheState, int, int> _stateChangeCallback;

private readonly HashSet<ProgramPipelineState> _pipelineStateSet = new();

/// <summary>
/// Indicates if the cache should be loaded.
/// </summary>
Expand Down Expand Up @@ -232,10 +234,7 @@ public void LoadShaders()

for (int index = 0; index < ThreadCount; index++)
{
workThreads[index] = new Thread(ProcessAsyncQueue)
{
Name = $"GPU.AsyncTranslationThread.{index}",
};
workThreads[index] = new Thread(ProcessAsyncQueue) { Name = $"GPU.AsyncTranslationThread.{index}", };
}

int programCount = _hostStorage.GetProgramCount();
Expand Down Expand Up @@ -305,6 +304,8 @@ public void LoadShaders()
using var streams = _hostStorage.GetOutputStreams(_context);

int packagedShaders = 0;
ProgramPipelineState currentPipelineState = default;

foreach (var kv in _programList)
{
if (!Active)
Expand All @@ -314,12 +315,53 @@ public void LoadShaders()

(CachedShaderProgram program, byte[] binaryCode) = kv.Value;

_hostStorage.AddShader(_context, program, binaryCode, streams);
if (program.SpecializationState.PipelineState.HasValue && _context.Capabilities.SupportsExtendedDynamicState)
{
currentPipelineState = program.SpecializationState.PipelineState.Value;

if (_context.Capabilities.SupportsExtendedDynamicState)
{
currentPipelineState.StencilTest = default;

currentPipelineState.CullMode = 0;
currentPipelineState.FrontFace = 0;
currentPipelineState.DepthTest = default;
currentPipelineState.Topology = ConvertToClass(currentPipelineState.Topology);
}

if (_context.Capabilities.SupportsExtendedDynamicState2)
{
currentPipelineState.PrimitiveRestartEnable = false;
currentPipelineState.BiasEnable = 0;
currentPipelineState.RasterizerDiscard = false;
}

if (_context.Capabilities.SupportsLogicOpDynamicState)
{
currentPipelineState.LogicOp = 0;
}

if (_context.Capabilities.SupportsPatchControlPointsDynamicState)
{
currentPipelineState.PatchControlPoints = 0;
}
}

_stateChangeCallback(ShaderCacheState.Packaging, ++packagedShaders, _programList.Count);
}
if (!_pipelineStateSet.Contains(currentPipelineState) ||
!_context.Capabilities.SupportsExtendedDynamicState ||
!program.SpecializationState.PipelineState.HasValue)
{
_hostStorage.AddShader(_context, program, binaryCode, streams);

Logger.Info?.Print(LogClass.Gpu, $"Rebuilt {_programList.Count} shaders successfully.");
_stateChangeCallback(ShaderCacheState.Packaging, ++packagedShaders, _programList.Count);

if (_context.Capabilities.SupportsExtendedDynamicState)
{
_pipelineStateSet.Add(currentPipelineState);
}
}
}
Logger.Info?.Print(LogClass.Gpu, $"Rebuilt {packagedShaders} shaders successfully.");
}
else
{
Expand All @@ -343,6 +385,29 @@ public void LoadShaders()
_stateChangeCallback(ShaderCacheState.Loaded, programCount, programCount);
}

private PrimitiveTopology ConvertToClass(PrimitiveTopology topology)
{
return topology switch
{
PrimitiveTopology.Points => PrimitiveTopology.Points,
PrimitiveTopology.Lines or
PrimitiveTopology.LineStrip or
PrimitiveTopology.LinesAdjacency or
PrimitiveTopology.LineStripAdjacency => PrimitiveTopology.Lines,
PrimitiveTopology.Triangles or
PrimitiveTopology.TriangleStrip or
PrimitiveTopology.TriangleFan or
PrimitiveTopology.TrianglesAdjacency or
PrimitiveTopology.TriangleStripAdjacency or
PrimitiveTopology.Polygon => PrimitiveTopology.TriangleStrip,
PrimitiveTopology.Patches => PrimitiveTopology.Patches,
PrimitiveTopology.Quads => PrimitiveTopology.Quads,
PrimitiveTopology.QuadStrip => PrimitiveTopology.QuadStrip,
PrimitiveTopology.LineLoop => PrimitiveTopology.LineLoop,
_ => PrimitiveTopology.TriangleStrip,
};
}

/// <summary>
/// Enqueues a host program for compilation.
/// </summary>
Expand Down
4 changes: 4 additions & 0 deletions src/Ryujinx.Graphics.OpenGL/OpenGLRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,10 @@ public Capabilities GetCapabilities()
supportsViewportSwizzle: HwCapabilities.SupportsViewportSwizzle,
supportsIndirectParameters: HwCapabilities.SupportsIndirectParameters,
supportsDepthClipControl: true,
supportsExtendedDynamicState: false,
supportsExtendedDynamicState2: false,
supportsLogicOpDynamicState: false,
supportsPatchControlPointsDynamicState: false,
uniformBufferSetIndex: 0,
storageBufferSetIndex: 1,
textureSetIndex: 2,
Expand Down
Loading

0 comments on commit 48f9ada

Please sign in to comment.