Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unmap persistent buffers before uploading #5800

Merged
merged 3 commits into from
May 20, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ internal class PersistentStagingBuffer<T> : IStagingBuffer<T>
{
private readonly VeldridRenderer renderer;
private readonly DeviceBuffer stagingBuffer;
private readonly MappedResource stagingBufferMap;
private MappedResource? stagingBufferMap;

public PersistentStagingBuffer(VeldridRenderer renderer, uint count)
{
Expand All @@ -25,7 +25,6 @@ public PersistentStagingBuffer(VeldridRenderer renderer, uint count)
SizeInBytes = (uint)(Unsafe.SizeOf<T>() * count);

stagingBuffer = renderer.Factory.CreateBuffer(new BufferDescription(SizeInBytes, BufferUsage.Staging));
stagingBufferMap = renderer.Device.Map(stagingBuffer, MapMode.ReadWrite);

Data.Clear();
}
Expand All @@ -40,15 +39,20 @@ public Span<T> Data
{
get
{
if (stagingBufferMap is not MappedResource map)
stagingBufferMap = map = renderer.Device.Map(stagingBuffer, MapMode.ReadWrite);

unsafe
{
return new Span<T>(stagingBufferMap.Data.ToPointer(), (int)Count);
return new Span<T>(map.Data.ToPointer(), (int)Count);
}
}
}

public void CopyTo(DeviceBuffer buffer, uint srcOffset, uint dstOffset, uint size)
{
unmap();

renderer.BufferUpdateCommands.CopyBuffer(
stagingBuffer,
(uint)(srcOffset * Unsafe.SizeOf<T>()),
Expand All @@ -57,9 +61,18 @@ public void CopyTo(DeviceBuffer buffer, uint srcOffset, uint dstOffset, uint siz
(uint)(size * Unsafe.SizeOf<T>()));
}

public void Dispose()
private void unmap()
{
if (stagingBufferMap == null)
return;

renderer.Device.Unmap(stagingBuffer);
stagingBufferMap = null;
}

public void Dispose()
{
unmap();
stagingBuffer.Dispose();
}
}
Expand Down