add UploadAndWait method to ResourceInitializer

what_if_no_video_threads
cosmonaut 2024-02-23 14:23:43 -08:00
parent a207f404b9
commit 848b1c706c
1 changed files with 27 additions and 3 deletions

View File

@ -181,6 +181,30 @@ namespace MoonWorks.Graphics
/// Uploads all the data corresponding to the created resources. /// Uploads all the data corresponding to the created resources.
/// </summary> /// </summary>
public void Upload() public void Upload()
{
CopyToTransferBuffer();
var commandBuffer = Device.AcquireCommandBuffer();
RecordUploadCommands(commandBuffer);
Device.Submit(commandBuffer);
}
/// <summary>
/// Uploads and then blocks until the upload is finished.
/// This is useful for keeping memory usage down during threaded upload.
/// </summary>
public void UploadAndWait()
{
CopyToTransferBuffer();
var commandBuffer = Device.AcquireCommandBuffer();
RecordUploadCommands(commandBuffer);
var fence = Device.SubmitAndAcquireFence(commandBuffer);
Device.WaitForFences(fence);
Device.ReleaseFence(fence);
}
private void CopyToTransferBuffer()
{ {
if (TransferBuffer == null || TransferBuffer.Size < dataSize) if (TransferBuffer == null || TransferBuffer.Size < dataSize)
{ {
@ -190,9 +214,10 @@ namespace MoonWorks.Graphics
var dataSpan = new Span<byte>(data, (int) dataSize); var dataSpan = new Span<byte>(data, (int) dataSize);
TransferBuffer.SetData(dataSpan, SetDataOptions.Discard); TransferBuffer.SetData(dataSpan, SetDataOptions.Discard);
}
var commandBuffer = Device.AcquireCommandBuffer(); private void RecordUploadCommands(CommandBuffer commandBuffer)
{
commandBuffer.BeginCopyPass(); commandBuffer.BeginCopyPass();
foreach (var (gpuBuffer, offset, size) in BufferUploads) foreach (var (gpuBuffer, offset, size) in BufferUploads)
@ -222,7 +247,6 @@ namespace MoonWorks.Graphics
} }
commandBuffer.EndCopyPass(); commandBuffer.EndCopyPass();
Device.Submit(commandBuffer);
BufferUploads.Clear(); BufferUploads.Clear();
TextureUploads.Clear(); TextureUploads.Clear();