Skip to content

Commit

Permalink
Merge pull request #12 from riperiperi/perf/helper-shader-staging
Browse files Browse the repository at this point in the history
Perf/helper shader staging
  • Loading branch information
ryzendew authored Jan 24, 2024
2 parents f5e120d + f2b895f commit a84a042
Show file tree
Hide file tree
Showing 7 changed files with 122 additions and 82 deletions.
2 changes: 1 addition & 1 deletion src/Ryujinx.Graphics.Vulkan/BufferHolder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ private unsafe bool TryGetMirror(CommandBufferScoped cbs, ref int offset, int si
var baseData = new Span<byte>((void*)(_map + offset), size);
var modData = _pendingData.AsSpan(offset, size);

StagingBufferReserved? newMirror = _gd.BufferManager.StagingBuffer.TryReserveData(cbs, size, (int)_gd.Capabilities.MinResourceAlignment);
StagingBufferReserved? newMirror = _gd.BufferManager.StagingBuffer.TryReserveData(cbs, size);

if (newMirror != null)
{
Expand Down
54 changes: 53 additions & 1 deletion src/Ryujinx.Graphics.Vulkan/BufferManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,34 @@

namespace Ryujinx.Graphics.Vulkan
{
readonly struct ScopedTemporaryBuffer : IDisposable
{
private readonly BufferManager _bufferManager;
private readonly bool _isReserved;

public readonly BufferRange Range;

public BufferHandle Handle => Range.Handle;
public int Offset => Range.Offset;

public ScopedTemporaryBuffer(BufferManager bufferManager, BufferHandle handle, int offset, int size, bool isReserved)
{
_bufferManager = bufferManager;

Range = new BufferRange(handle, offset, size);

_isReserved = isReserved;
}

public void Dispose()
{
if (!_isReserved)
{
_bufferManager.Delete(Range.Handle);
}
}
}

class BufferManager : IDisposable
{
public const MemoryPropertyFlags DefaultBufferMemoryFlags =
Expand Down Expand Up @@ -238,6 +266,29 @@ public BufferHandle CreateWithHandle(
return Unsafe.As<ulong, BufferHandle>(ref handle64);
}

public ScopedTemporaryBuffer ReserveOrCreate(VulkanRenderer gd, CommandBufferScoped cbs, int size, out BufferHolder holder)
{
StagingBufferReserved? result = StagingBuffer.TryReserveData(cbs, size);

if (result.HasValue)
{
holder = result.Value.Buffer;
return new ScopedTemporaryBuffer(this, StagingBuffer.Handle, result.Value.Offset, result.Value.Size, true);
}
else
{
// Create a temporary buffer.
BufferHandle handle = CreateWithHandle(gd, size, out holder);

return new ScopedTemporaryBuffer(this, handle, 0, size, false);
}
}

public ScopedTemporaryBuffer ReserveOrCreate(VulkanRenderer gd, CommandBufferScoped cbs, int size)
{
return ReserveOrCreate(gd, cbs, size, out _);
}

public unsafe MemoryRequirements GetHostImportedUsageRequirements(VulkanRenderer gd)
{
var usage = HostImportedBufferUsageFlags;
Expand Down Expand Up @@ -635,13 +686,14 @@ protected virtual void Dispose(bool disposing)
{
if (disposing)
{
StagingBuffer.Dispose();

foreach (BufferHolder buffer in _buffers)
{
buffer.Dispose();
}

_buffers.Clear();
StagingBuffer.Dispose();
}
}

Expand Down
19 changes: 7 additions & 12 deletions src/Ryujinx.Graphics.Vulkan/Effects/FsrScalingFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,36 +142,31 @@ public void Run(
};

int rangeSize = dimensionsBuffer.Length * sizeof(float);
var bufferHandle = _renderer.BufferManager.CreateWithHandle(_renderer, rangeSize);
_renderer.BufferManager.SetData(bufferHandle, 0, dimensionsBuffer);
using var buffer = _renderer.BufferManager.ReserveOrCreate(_renderer, cbs, rangeSize);
_renderer.BufferManager.SetData(buffer.Handle, buffer.Offset, dimensionsBuffer);

ReadOnlySpan<float> sharpeningBuffer = stackalloc float[] { 1.5f - (Level * 0.01f * 1.5f) };
var sharpeningBufferHandle = _renderer.BufferManager.CreateWithHandle(_renderer, sizeof(float));
_renderer.BufferManager.SetData(sharpeningBufferHandle, 0, sharpeningBuffer);
ReadOnlySpan<float> sharpeningBufferData = stackalloc float[] { 1.5f - (Level * 0.01f * 1.5f) };
using var sharpeningBuffer = _renderer.BufferManager.ReserveOrCreate(_renderer, cbs, sizeof(float));
_renderer.BufferManager.SetData(sharpeningBuffer.Handle, sharpeningBuffer.Offset, sharpeningBufferData);

int threadGroupWorkRegionDim = 16;
int dispatchX = (width + (threadGroupWorkRegionDim - 1)) / threadGroupWorkRegionDim;
int dispatchY = (height + (threadGroupWorkRegionDim - 1)) / threadGroupWorkRegionDim;

var bufferRanges = new BufferRange(bufferHandle, 0, rangeSize);
_pipeline.SetUniformBuffers(stackalloc[] { new BufferAssignment(2, bufferRanges) });
_pipeline.SetUniformBuffers(stackalloc[] { new BufferAssignment(2, buffer.Range) });
_pipeline.SetImage(0, _intermediaryTexture, FormatTable.ConvertRgba8SrgbToUnorm(view.Info.Format));
_pipeline.DispatchCompute(dispatchX, dispatchY, 1);
_pipeline.ComputeBarrier();

// Sharpening pass
_pipeline.SetProgram(_sharpeningProgram);
_pipeline.SetTextureAndSampler(ShaderStage.Compute, 1, _intermediaryTexture, _sampler);
var sharpeningRange = new BufferRange(sharpeningBufferHandle, 0, sizeof(float));
_pipeline.SetUniformBuffers(stackalloc[] { new BufferAssignment(4, sharpeningRange) });
_pipeline.SetUniformBuffers(stackalloc[] { new BufferAssignment(4, sharpeningBuffer.Range) });
_pipeline.SetImage(0, destinationTexture);
_pipeline.DispatchCompute(dispatchX, dispatchY, 1);
_pipeline.ComputeBarrier();

_pipeline.Finish();

_renderer.BufferManager.Delete(bufferHandle);
_renderer.BufferManager.Delete(sharpeningBufferHandle);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,20 +66,18 @@ public TextureView Run(TextureView view, CommandBufferScoped cbs, int width, int

ReadOnlySpan<float> resolutionBuffer = stackalloc float[] { view.Width, view.Height };
int rangeSize = resolutionBuffer.Length * sizeof(float);
var bufferHandle = _renderer.BufferManager.CreateWithHandle(_renderer, rangeSize);
using var buffer = _renderer.BufferManager.ReserveOrCreate(_renderer, cbs, rangeSize);

_renderer.BufferManager.SetData(bufferHandle, 0, resolutionBuffer);
_renderer.BufferManager.SetData(buffer.Handle, buffer.Offset, resolutionBuffer);

var bufferRanges = new BufferRange(bufferHandle, 0, rangeSize);
_pipeline.SetUniformBuffers(stackalloc[] { new BufferAssignment(2, bufferRanges) });
_pipeline.SetUniformBuffers(stackalloc[] { new BufferAssignment(2, buffer.Range) });

var dispatchX = BitUtils.DivRoundUp(view.Width, IPostProcessingEffect.LocalGroupSize);
var dispatchY = BitUtils.DivRoundUp(view.Height, IPostProcessingEffect.LocalGroupSize);

_pipeline.SetImage(0, _texture, FormatTable.ConvertRgba8SrgbToUnorm(view.Info.Format));
_pipeline.DispatchCompute(dispatchX, dispatchY, 1);

_renderer.BufferManager.Delete(bufferHandle);
_pipeline.ComputeBarrier();

_pipeline.Finish();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,11 +215,10 @@ public TextureView Run(TextureView view, CommandBufferScoped cbs, int width, int

ReadOnlySpan<float> resolutionBuffer = stackalloc float[] { view.Width, view.Height };
int rangeSize = resolutionBuffer.Length * sizeof(float);
var bufferHandle = _renderer.BufferManager.CreateWithHandle(_renderer, rangeSize);
using var buffer = _renderer.BufferManager.ReserveOrCreate(_renderer, cbs, rangeSize);

_renderer.BufferManager.SetData(bufferHandle, 0, resolutionBuffer);
var bufferRanges = new BufferRange(bufferHandle, 0, rangeSize);
_pipeline.SetUniformBuffers(stackalloc[] { new BufferAssignment(2, bufferRanges) });
_renderer.BufferManager.SetData(buffer.Handle, buffer.Offset, resolutionBuffer);
_pipeline.SetUniformBuffers(stackalloc[] { new BufferAssignment(2, buffer.Range) });
_pipeline.SetImage(0, _edgeOutputTexture, FormatTable.ConvertRgba8SrgbToUnorm(view.Info.Format));
_pipeline.DispatchCompute(dispatchX, dispatchY, 1);
_pipeline.ComputeBarrier();
Expand All @@ -245,8 +244,6 @@ public TextureView Run(TextureView view, CommandBufferScoped cbs, int width, int

_pipeline.Finish();

_renderer.BufferManager.Delete(bufferHandle);

return _outputTexture;
}

Expand Down
Loading

0 comments on commit a84a042

Please sign in to comment.