Skip to content

Commit

Permalink
Use temporary buffer for ConvertIndexBufferIndirect
Browse files Browse the repository at this point in the history
  • Loading branch information
riperiperi committed Jan 23, 2024
1 parent bd83221 commit 1b17ecc
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 11 deletions.
10 changes: 8 additions & 2 deletions src/Ryujinx.Graphics.Vulkan/BufferManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -266,23 +266,29 @@ public BufferHandle CreateWithHandle(
return Unsafe.As<ulong, BufferHandle>(ref handle64);
}

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

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);
BufferHandle handle = CreateWithHandle(gd, size, out holder);

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

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

public unsafe MemoryRequirements GetHostImportedUsageRequirements(VulkanRenderer gd)
{
var usage = HostImportedBufferUsageFlags;
Expand Down
17 changes: 8 additions & 9 deletions src/Ryujinx.Graphics.Vulkan/HelperShader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1600,10 +1600,10 @@ public void ConvertIndexBufferIndirect(

pattern.OffsetIndex.CopyTo(shaderParams[..pattern.OffsetIndex.Length]);

var patternBufferHandle = gd.BufferManager.CreateWithHandle(gd, ParamsBufferSize, out var patternBuffer);
using var patternScoped = gd.BufferManager.ReserveOrCreate(gd, cbs, ParamsBufferSize, out var patternBuffer);
var patternBufferAuto = patternBuffer.GetBuffer();

gd.BufferManager.SetData<int>(patternBufferHandle, 0, shaderParams);
gd.BufferManager.SetData<int>(patternScoped.Handle, patternScoped.Offset, shaderParams);

_pipeline.SetCommandBuffer(cbs);

Expand All @@ -1619,20 +1619,21 @@ public void ConvertIndexBufferIndirect(
indirectDataSize);

_pipeline.SetUniformBuffers(stackalloc[] { new BufferAssignment(0, drawCountBufferAligned) });
_pipeline.SetStorageBuffers(1, new[] { srcIndirectBuffer.GetBuffer(), dstIndirectBuffer.GetBuffer(), patternBuffer.GetBuffer() });
_pipeline.SetStorageBuffers(1, new[] { srcIndirectBuffer.GetBuffer(), dstIndirectBuffer.GetBuffer() });
_pipeline.SetStorageBuffers(stackalloc[] { new BufferAssignment(3, patternScoped.Range) });

_pipeline.SetProgram(_programConvertIndirectData);
_pipeline.DispatchCompute(1, 1, 1);

BufferHolder.InsertBufferBarrier(
gd,
cbs.CommandBuffer,
patternBufferAuto.Get(cbs, ParamsIndirectDispatchOffset, ParamsIndirectDispatchSize).Value,
patternBufferAuto.Get(cbs, patternScoped.Offset + ParamsIndirectDispatchOffset, ParamsIndirectDispatchSize).Value,
AccessFlags.ShaderWriteBit,
AccessFlags.IndirectCommandReadBit,
PipelineStageFlags.ComputeShaderBit,
PipelineStageFlags.DrawIndirectBit,
ParamsIndirectDispatchOffset,
patternScoped.Offset + ParamsIndirectDispatchOffset,
ParamsIndirectDispatchSize);

BufferHolder.InsertBufferBarrier(
Expand All @@ -1646,11 +1647,11 @@ public void ConvertIndexBufferIndirect(
0,
convertedCount * outputIndexSize);

_pipeline.SetUniformBuffers(stackalloc[] { new BufferAssignment(0, new BufferRange(patternBufferHandle, 0, ParamsBufferSize)) });
_pipeline.SetUniformBuffers(stackalloc[] { new BufferAssignment(0, new BufferRange(patternScoped.Handle, patternScoped.Offset, ParamsBufferSize)) });
_pipeline.SetStorageBuffers(1, new[] { srcIndexBuffer.GetBuffer(), dstIndexBuffer.GetBuffer() });

_pipeline.SetProgram(_programConvertIndexBuffer);
_pipeline.DispatchComputeIndirect(patternBufferAuto, ParamsIndirectDispatchOffset);
_pipeline.DispatchComputeIndirect(patternBufferAuto, patternScoped.Offset + ParamsIndirectDispatchOffset);

BufferHolder.InsertBufferBarrier(
gd,
Expand All @@ -1663,8 +1664,6 @@ public void ConvertIndexBufferIndirect(
0,
convertedCount * outputIndexSize);

gd.BufferManager.Delete(patternBufferHandle);

_pipeline.Finish(gd, cbs);
}

Expand Down

0 comments on commit 1b17ecc

Please sign in to comment.