add SetBufferData to ResourceUploader
parent
d83501437d
commit
50b8cb11c9
|
@ -21,7 +21,7 @@ namespace MoonWorks.Graphics
|
||||||
uint dataOffset = 0;
|
uint dataOffset = 0;
|
||||||
uint dataSize = 1024;
|
uint dataSize = 1024;
|
||||||
|
|
||||||
List<(GpuBuffer, uint, uint)> BufferUploads = new List<(GpuBuffer, uint, uint)>();
|
List<(GpuBuffer, BufferCopy)> BufferUploads = new List<(GpuBuffer, BufferCopy)>();
|
||||||
List<(TextureSlice, uint)> TextureUploads = new List<(TextureSlice, uint)>();
|
List<(TextureSlice, uint)> TextureUploads = new List<(TextureSlice, uint)>();
|
||||||
|
|
||||||
public ResourceUploader(GraphicsDevice device) : base(device)
|
public ResourceUploader(GraphicsDevice device) : base(device)
|
||||||
|
@ -29,6 +29,8 @@ namespace MoonWorks.Graphics
|
||||||
data = (byte*) NativeMemory.Alloc(dataSize);
|
data = (byte*) NativeMemory.Alloc(dataSize);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Buffers
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Creates a GpuBuffer with data to be uploaded.
|
/// Creates a GpuBuffer with data to be uploaded.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -37,17 +39,30 @@ namespace MoonWorks.Graphics
|
||||||
var lengthInBytes = (uint) (Marshal.SizeOf<T>() * data.Length);
|
var lengthInBytes = (uint) (Marshal.SizeOf<T>() * data.Length);
|
||||||
var gpuBuffer = new GpuBuffer(Device, usageFlags, lengthInBytes);
|
var gpuBuffer = new GpuBuffer(Device, usageFlags, lengthInBytes);
|
||||||
|
|
||||||
|
SetBufferData(gpuBuffer, 0, data);
|
||||||
|
|
||||||
|
return gpuBuffer;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Prepares upload of data into a GpuBuffer.
|
||||||
|
/// </summary>
|
||||||
|
public void SetBufferData<T>(GpuBuffer buffer, uint bufferOffset, Span<T> data) where T : unmanaged
|
||||||
|
{
|
||||||
|
var lengthInBytes = (uint) (Marshal.SizeOf<T>() * data.Length);
|
||||||
|
|
||||||
uint resourceOffset;
|
uint resourceOffset;
|
||||||
fixed (void* spanPtr = data)
|
fixed (void* spanPtr = data)
|
||||||
{
|
{
|
||||||
resourceOffset = CopyData(spanPtr, lengthInBytes);
|
resourceOffset = CopyData(spanPtr, lengthInBytes);
|
||||||
}
|
}
|
||||||
|
|
||||||
BufferUploads.Add((gpuBuffer, resourceOffset, lengthInBytes));
|
var bufferCopyParams = new BufferCopy(resourceOffset, bufferOffset, lengthInBytes);
|
||||||
|
BufferUploads.Add((buffer, bufferCopyParams));
|
||||||
return gpuBuffer;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Textures
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Creates a 2D Texture from compressed image data to be uploaded.
|
/// Creates a 2D Texture from compressed image data to be uploaded.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -193,6 +208,8 @@ namespace MoonWorks.Graphics
|
||||||
TextureUploads.Add((textureSlice, resourceOffset));
|
TextureUploads.Add((textureSlice, resourceOffset));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Upload
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Uploads all the data corresponding to the created resources.
|
/// Uploads all the data corresponding to the created resources.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -220,6 +237,8 @@ namespace MoonWorks.Graphics
|
||||||
Device.ReleaseFence(fence);
|
Device.ReleaseFence(fence);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Helper methods
|
||||||
|
|
||||||
private void CopyToTransferBuffer()
|
private void CopyToTransferBuffer()
|
||||||
{
|
{
|
||||||
if (TransferBuffer == null || TransferBuffer.Size < dataSize)
|
if (TransferBuffer == null || TransferBuffer.Size < dataSize)
|
||||||
|
@ -236,16 +255,12 @@ namespace MoonWorks.Graphics
|
||||||
{
|
{
|
||||||
commandBuffer.BeginCopyPass();
|
commandBuffer.BeginCopyPass();
|
||||||
|
|
||||||
foreach (var (gpuBuffer, offset, size) in BufferUploads)
|
foreach (var (gpuBuffer, bufferCopyParams) in BufferUploads)
|
||||||
{
|
{
|
||||||
commandBuffer.UploadToBuffer(
|
commandBuffer.UploadToBuffer(
|
||||||
TransferBuffer,
|
TransferBuffer,
|
||||||
gpuBuffer,
|
gpuBuffer,
|
||||||
new BufferCopy(
|
bufferCopyParams
|
||||||
offset,
|
|
||||||
0,
|
|
||||||
size
|
|
||||||
)
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -296,6 +311,11 @@ namespace MoonWorks.Graphics
|
||||||
return alignment * ((value + alignment - 1) / alignment);
|
return alignment * ((value + alignment - 1) / alignment);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Dispose
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// It is valid to immediately call Dispose after calling Upload.
|
||||||
|
/// </summary>
|
||||||
protected override void Dispose(bool disposing)
|
protected override void Dispose(bool disposing)
|
||||||
{
|
{
|
||||||
if (!IsDisposed)
|
if (!IsDisposed)
|
||||||
|
|
Loading…
Reference in New Issue