From e0f4c19dc2d9bbebeb75b5bf3b15885f61ec9cc3 Mon Sep 17 00:00:00 2001 From: cosmonaut Date: Wed, 28 Feb 2024 20:07:19 -0800 Subject: [PATCH] another refresh2 update --- lib/RefreshCS | 2 +- src/Graphics/CommandBuffer.cs | 102 +++++++++++++++-------- src/Graphics/Font/Font.cs | 5 +- src/Graphics/Font/TextBatch.cs | 8 +- src/Graphics/ImageUtils.cs | 6 +- src/Graphics/RefreshEnums.cs | 8 +- src/Graphics/ResourceUploader.cs | 32 +++---- src/Graphics/Resources/TransferBuffer.cs | 10 +-- src/Video/VideoPlayer.cs | 15 ++-- 9 files changed, 117 insertions(+), 71 deletions(-) diff --git a/lib/RefreshCS b/lib/RefreshCS index 2e346d8..031ad4c 160000 --- a/lib/RefreshCS +++ b/lib/RefreshCS @@ -1 +1 @@ -Subproject commit 2e346d84016fb7b26dbe7c5b6c485109571c30f4 +Subproject commit 031ad4c04781fe4a0d98a177333ebf7764d2fe91 diff --git a/src/Graphics/CommandBuffer.cs b/src/Graphics/CommandBuffer.cs index 2f2924b..6fa41c1 100644 --- a/src/Graphics/CommandBuffer.cs +++ b/src/Graphics/CommandBuffer.cs @@ -1949,7 +1949,8 @@ namespace MoonWorks.Graphics public void UploadToTexture( TransferBuffer transferBuffer, in TextureSlice textureSlice, - in BufferImageCopy copyParams + in BufferImageCopy copyParams, + CopyOptions option ) { #if DEBUG @@ -1963,7 +1964,8 @@ namespace MoonWorks.Graphics Handle, transferBuffer.Handle, textureSlice.ToRefreshTextureSlice(), - copyParams.ToRefresh() + copyParams.ToRefresh(), + (Refresh.CopyOptions) option ); } @@ -1972,12 +1974,14 @@ namespace MoonWorks.Graphics /// public void UploadToTexture( TransferBuffer transferBuffer, - Texture texture + Texture texture, + CopyOptions option ) { UploadToTexture( transferBuffer, new TextureSlice(texture), - new BufferImageCopy(0, 0, 0) + new BufferImageCopy(0, 0, 0), + option ); } @@ -1993,7 +1997,8 @@ namespace MoonWorks.Graphics public void UploadToBuffer( TransferBuffer transferBuffer, GpuBuffer gpuBuffer, - in BufferCopy copyParams + in BufferCopy copyParams, + CopyOptions option ) { #if DEBUG AssertNotSubmitted(); @@ -2007,7 +2012,8 @@ namespace MoonWorks.Graphics Handle, transferBuffer.Handle, gpuBuffer.Handle, - copyParams.ToRefresh() + copyParams.ToRefresh(), + (Refresh.CopyOptions) option ); } @@ -2016,12 +2022,14 @@ namespace MoonWorks.Graphics /// public void UploadToBuffer( TransferBuffer transferBuffer, - GpuBuffer gpuBuffer + GpuBuffer gpuBuffer, + CopyOptions option ) { UploadToBuffer( transferBuffer, gpuBuffer, - new BufferCopy(0, 0, transferBuffer.Size) + new BufferCopy(0, 0, transferBuffer.Size), + option ); } @@ -2033,7 +2041,8 @@ namespace MoonWorks.Graphics GpuBuffer gpuBuffer, uint sourceStartElement, uint destinationStartElement, - uint numElements + uint numElements, + CopyOptions option ) where T : unmanaged { var elementSize = Marshal.SizeOf(); @@ -2048,7 +2057,8 @@ namespace MoonWorks.Graphics srcOffsetInBytes, dstOffsetInBytes, dataLengthInBytes - ) + ), + option ); } @@ -2062,7 +2072,8 @@ namespace MoonWorks.Graphics public void DownloadFromTexture( in TextureSlice textureSlice, TransferBuffer transferBuffer, - in BufferImageCopy copyParams + in BufferImageCopy copyParams, + TransferOptions option ) { #if DEBUG AssertNotSubmitted(); @@ -2075,7 +2086,8 @@ namespace MoonWorks.Graphics Handle, textureSlice.ToRefreshTextureSlice(), transferBuffer.Handle, - copyParams.ToRefresh() + copyParams.ToRefresh(), + (Refresh.TransferOptions) option ); } @@ -2084,12 +2096,14 @@ namespace MoonWorks.Graphics /// public void DownloadFromTexture( Texture texture, - TransferBuffer transferBuffer + TransferBuffer transferBuffer, + TransferOptions option ) { DownloadFromTexture( new TextureSlice(texture), transferBuffer, - new BufferImageCopy(0, 0, 0) + new BufferImageCopy(0, 0, 0), + option ); } @@ -2103,7 +2117,8 @@ namespace MoonWorks.Graphics public void DownloadFromBuffer( GpuBuffer gpuBuffer, TransferBuffer transferBuffer, - in BufferCopy copyParams + in BufferCopy copyParams, + TransferOptions option ) { #if DEBUG AssertNotSubmitted(); @@ -2116,7 +2131,8 @@ namespace MoonWorks.Graphics Handle, gpuBuffer.Handle, transferBuffer.Handle, - copyParams.ToRefresh() + copyParams.ToRefresh(), + (Refresh.TransferOptions) option ); } @@ -2129,12 +2145,14 @@ namespace MoonWorks.Graphics /// public void DownloadFromBuffer( GpuBuffer gpuBuffer, - TransferBuffer transferBuffer + TransferBuffer transferBuffer, + TransferOptions option ) { DownloadFromBuffer( gpuBuffer, transferBuffer, - new BufferCopy(0, 0, gpuBuffer.Size) + new BufferCopy(0, 0, gpuBuffer.Size), + option ); } @@ -2147,7 +2165,8 @@ namespace MoonWorks.Graphics /// public void CopyTextureToTexture( in TextureSlice source, - in TextureSlice destination + in TextureSlice destination, + CopyOptions option ) { #if DEBUG AssertNotSubmitted(); @@ -2159,7 +2178,8 @@ namespace MoonWorks.Graphics Device.Handle, Handle, source.ToRefreshTextureSlice(), - destination.ToRefreshTextureSlice() + destination.ToRefreshTextureSlice(), + (Refresh.CopyOptions) option ); } @@ -2169,11 +2189,13 @@ namespace MoonWorks.Graphics /// public void CopyTextureToTexture( Texture source, - Texture destination + Texture destination, + CopyOptions option ) { CopyTextureToTexture( new TextureSlice(source), - new TextureSlice(destination) + new TextureSlice(destination), + option ); } @@ -2186,7 +2208,8 @@ namespace MoonWorks.Graphics public void CopyTextureToBuffer( in TextureSlice textureSlice, GpuBuffer buffer, - in BufferImageCopy copyParams + in BufferImageCopy copyParams, + CopyOptions option ) { #if DEBUG AssertNotSubmitted(); @@ -2199,7 +2222,8 @@ namespace MoonWorks.Graphics Handle, textureSlice.ToRefreshTextureSlice(), buffer.Handle, - copyParams.ToRefresh() + copyParams.ToRefresh(), + (Refresh.CopyOptions) option ); } @@ -2208,12 +2232,14 @@ namespace MoonWorks.Graphics /// public void CopyTextureToBuffer( Texture texture, - GpuBuffer buffer + GpuBuffer buffer, + CopyOptions option ) { CopyTextureToBuffer( new TextureSlice(texture), buffer, - new BufferImageCopy(0, 0, 0) + new BufferImageCopy(0, 0, 0), + option ); } @@ -2226,7 +2252,8 @@ namespace MoonWorks.Graphics public void CopyBufferToTexture( GpuBuffer gpuBuffer, in TextureSlice textureSlice, - in BufferImageCopy copyParams + in BufferImageCopy copyParams, + CopyOptions option ) { #if DEBUG AssertNotSubmitted(); @@ -2239,7 +2266,8 @@ namespace MoonWorks.Graphics Handle, gpuBuffer.Handle, textureSlice.ToRefreshTextureSlice(), - copyParams.ToRefresh() + copyParams.ToRefresh(), + (Refresh.CopyOptions) option ); } @@ -2248,12 +2276,14 @@ namespace MoonWorks.Graphics /// public void CopyBufferToTexture( GpuBuffer buffer, - Texture texture + Texture texture, + CopyOptions option ) { CopyBufferToTexture( buffer, new TextureSlice(texture), - new BufferImageCopy(0, 0, 0) + new BufferImageCopy(0, 0, 0), + option ); } @@ -2266,7 +2296,8 @@ namespace MoonWorks.Graphics public void CopyBufferToBuffer( GpuBuffer source, GpuBuffer destination, - in BufferCopy copyParams + in BufferCopy copyParams, + CopyOptions option ) { #if DEBUG AssertNotSubmitted(); @@ -2280,7 +2311,8 @@ namespace MoonWorks.Graphics Handle, source.Handle, destination.Handle, - copyParams.ToRefresh() + copyParams.ToRefresh(), + (Refresh.CopyOptions) option ); } @@ -2289,12 +2321,14 @@ namespace MoonWorks.Graphics /// public void CopyBufferToBuffer( GpuBuffer source, - GpuBuffer destination + GpuBuffer destination, + CopyOptions option ) { CopyBufferToBuffer( source, destination, - new BufferCopy(0, 0, source.Size) + new BufferCopy(0, 0, source.Size), + option ); } diff --git a/src/Graphics/Font/Font.cs b/src/Graphics/Font/Font.cs index 4205696..7fc27b5 100644 --- a/src/Graphics/Font/Font.cs +++ b/src/Graphics/Font/Font.cs @@ -56,13 +56,14 @@ namespace MoonWorks.Graphics.Font imagePath, transferBuffer, 0, - SetDataOptions.Overwrite + TransferOptions.Overwrite ); commandBuffer.BeginCopyPass(); commandBuffer.UploadToTexture( transferBuffer, - texture + texture, + CopyOptions.SafeOverwrite ); commandBuffer.EndCopyPass(); diff --git a/src/Graphics/Font/TextBatch.cs b/src/Graphics/Font/TextBatch.cs index caf6962..4598bbe 100644 --- a/src/Graphics/Font/TextBatch.cs +++ b/src/Graphics/Font/TextBatch.cs @@ -124,11 +124,11 @@ namespace MoonWorks.Graphics.Font if (vertexDataLengthInBytes > 0 && indexDataLengthInBytes > 0) { - TransferBuffer.SetData(vertexSpan, SetDataOptions.Discard); - TransferBuffer.SetData(indexSpan, (uint) vertexSpan.Length, SetDataOptions.Overwrite); + TransferBuffer.SetData(vertexSpan, TransferOptions.Discard); + TransferBuffer.SetData(indexSpan, (uint) vertexSpan.Length, TransferOptions.Overwrite); - commandBuffer.UploadToBuffer(TransferBuffer, VertexBuffer, new BufferCopy(0, 0, (uint) vertexSpan.Length)); - commandBuffer.UploadToBuffer(TransferBuffer, IndexBuffer, new BufferCopy((uint) vertexSpan.Length, 0, (uint) indexSpan.Length)); + commandBuffer.UploadToBuffer(TransferBuffer, VertexBuffer, new BufferCopy(0, 0, (uint) vertexSpan.Length), CopyOptions.SafeDiscard); + commandBuffer.UploadToBuffer(TransferBuffer, IndexBuffer, new BufferCopy((uint) vertexSpan.Length, 0, (uint) indexSpan.Length), CopyOptions.SafeDiscard); } PrimitiveCount = vertexCount / 2; diff --git a/src/Graphics/ImageUtils.cs b/src/Graphics/ImageUtils.cs index 8908e34..cd01fc8 100644 --- a/src/Graphics/ImageUtils.cs +++ b/src/Graphics/ImageUtils.cs @@ -152,7 +152,7 @@ namespace MoonWorks.Graphics Span data, TransferBuffer transferBuffer, uint bufferOffsetInBytes, - SetDataOptions option + TransferOptions option ) { var pixelData = GetPixelDataFromBytes(data, out var w, out var h, out var sizeInBytes); var length = transferBuffer.SetData(new Span((void*) pixelData, (int) sizeInBytes), bufferOffsetInBytes, option); @@ -167,7 +167,7 @@ namespace MoonWorks.Graphics Stream stream, TransferBuffer transferBuffer, uint bufferOffsetInBytes, - SetDataOptions option + TransferOptions option ) { var pixelData = GetPixelDataFromStream(stream, out var w, out var h, out var sizeInBytes); var length = transferBuffer.SetData(new Span((void*) pixelData, (int) sizeInBytes), bufferOffsetInBytes, option); @@ -182,7 +182,7 @@ namespace MoonWorks.Graphics string path, TransferBuffer transferBuffer, uint bufferOffsetInBytes, - SetDataOptions option + TransferOptions option ) { var pixelData = GetPixelDataFromFile(path, out var w, out var h, out var sizeInBytes); var length = transferBuffer.SetData(new Span((void*) pixelData, (int) sizeInBytes), bufferOffsetInBytes, option); diff --git a/src/Graphics/RefreshEnums.cs b/src/Graphics/RefreshEnums.cs index e773f26..792b6a2 100644 --- a/src/Graphics/RefreshEnums.cs +++ b/src/Graphics/RefreshEnums.cs @@ -297,12 +297,18 @@ namespace MoonWorks.Graphics IntOpaqueWhite } - public enum SetDataOptions + public enum TransferOptions { Discard, Overwrite } + public enum CopyOptions + { + SafeDiscard, + SafeOverwrite + } + public enum Backend { DontCare, diff --git a/src/Graphics/ResourceUploader.cs b/src/Graphics/ResourceUploader.cs index 938872e..b946380 100644 --- a/src/Graphics/ResourceUploader.cs +++ b/src/Graphics/ResourceUploader.cs @@ -21,8 +21,8 @@ namespace MoonWorks.Graphics uint dataOffset = 0; uint dataSize = 1024; - List<(GpuBuffer, BufferCopy)> BufferUploads = new List<(GpuBuffer, BufferCopy)>(); - List<(TextureSlice, uint)> TextureUploads = new List<(TextureSlice, uint)>(); + List<(GpuBuffer, BufferCopy, CopyOptions)> BufferUploads = new List<(GpuBuffer, BufferCopy, CopyOptions)>(); + List<(TextureSlice, uint, CopyOptions)> TextureUploads = new List<(TextureSlice, uint, CopyOptions)>(); public ResourceUploader(GraphicsDevice device) : base(device) { @@ -39,7 +39,7 @@ namespace MoonWorks.Graphics var lengthInBytes = (uint) (Marshal.SizeOf() * data.Length); var gpuBuffer = new GpuBuffer(Device, usageFlags, lengthInBytes); - SetBufferData(gpuBuffer, 0, data); + SetBufferData(gpuBuffer, 0, data, CopyOptions.SafeOverwrite); return gpuBuffer; } @@ -47,7 +47,7 @@ namespace MoonWorks.Graphics /// /// Prepares upload of data into a GpuBuffer. /// - public void SetBufferData(GpuBuffer buffer, uint bufferOffsetInElements, Span data) where T : unmanaged + public void SetBufferData(GpuBuffer buffer, uint bufferOffsetInElements, Span data, CopyOptions option) where T : unmanaged { uint elementSize = (uint) Marshal.SizeOf(); uint offsetInBytes = elementSize * bufferOffsetInElements; @@ -60,7 +60,7 @@ namespace MoonWorks.Graphics } var bufferCopyParams = new BufferCopy(resourceOffset, offsetInBytes, lengthInBytes); - BufferUploads.Add((buffer, bufferCopyParams)); + BufferUploads.Add((buffer, bufferCopyParams, option)); } // Textures @@ -68,7 +68,7 @@ namespace MoonWorks.Graphics public Texture CreateTexture2D(Span pixelData, uint width, uint height) { var texture = Texture.CreateTexture2D(Device, width, height, TextureFormat.R8G8B8A8, TextureUsageFlags.Sampler); - SetTextureData(texture, pixelData); + SetTextureData(texture, pixelData, CopyOptions.SafeOverwrite); return texture; } @@ -156,7 +156,7 @@ namespace MoonWorks.Graphics Depth = 1 }; - SetTextureData(textureSlice, byteSpan); + SetTextureData(textureSlice, byteSpan, CopyOptions.SafeOverwrite); NativeMemory.Free(byteBuffer); } @@ -179,7 +179,7 @@ namespace MoonWorks.Graphics var pixelData = ImageUtils.GetPixelDataFromBytes(compressedImageData, out var _, out var _, out var sizeInBytes); var pixelSpan = new Span((void*) pixelData, (int) sizeInBytes); - SetTextureData(textureSlice, pixelSpan); + SetTextureData(textureSlice, pixelSpan, CopyOptions.SafeOverwrite); ImageUtils.FreePixelData(pixelData); } @@ -203,7 +203,7 @@ namespace MoonWorks.Graphics /// /// Prepares upload of pixel data into a TextureSlice. /// - public void SetTextureData(TextureSlice textureSlice, Span data) where T : unmanaged + public void SetTextureData(TextureSlice textureSlice, Span data, CopyOptions option) where T : unmanaged { var elementSize = Marshal.SizeOf(); var dataLengthInBytes = (uint) (elementSize * data.Length); @@ -214,7 +214,7 @@ namespace MoonWorks.Graphics resourceOffset = CopyDataAligned(dataPtr, dataLengthInBytes, Texture.TexelSize(textureSlice.Texture.Format)); } - TextureUploads.Add((textureSlice, resourceOffset)); + TextureUploads.Add((textureSlice, resourceOffset, option)); } // Upload @@ -257,23 +257,24 @@ namespace MoonWorks.Graphics } var dataSpan = new Span(data, (int) dataSize); - TransferBuffer.SetData(dataSpan, SetDataOptions.Discard); + TransferBuffer.SetData(dataSpan, TransferOptions.Discard); } private void RecordUploadCommands(CommandBuffer commandBuffer) { commandBuffer.BeginCopyPass(); - foreach (var (gpuBuffer, bufferCopyParams) in BufferUploads) + foreach (var (gpuBuffer, bufferCopyParams, option) in BufferUploads) { commandBuffer.UploadToBuffer( TransferBuffer, gpuBuffer, - bufferCopyParams + bufferCopyParams, + option ); } - foreach (var (textureSlice, offset) in TextureUploads) + foreach (var (textureSlice, offset, option) in TextureUploads) { commandBuffer.UploadToTexture( TransferBuffer, @@ -282,7 +283,8 @@ namespace MoonWorks.Graphics offset, 0, 0 - ) + ), + option ); } diff --git a/src/Graphics/Resources/TransferBuffer.cs b/src/Graphics/Resources/TransferBuffer.cs index bb94aa2..0b87c57 100644 --- a/src/Graphics/Resources/TransferBuffer.cs +++ b/src/Graphics/Resources/TransferBuffer.cs @@ -61,7 +61,7 @@ namespace MoonWorks.Graphics public unsafe uint SetData( Span data, uint bufferOffsetInBytes, - SetDataOptions setDataOption + TransferOptions setDataOption ) where T : unmanaged { var elementSize = Marshal.SizeOf(); @@ -73,7 +73,7 @@ namespace MoonWorks.Graphics fixed (T* dataPtr = data) { - Refresh.Refresh_SetData( + Refresh.Refresh_SetTransferData( Device.Handle, (nint) dataPtr, Handle, @@ -83,7 +83,7 @@ namespace MoonWorks.Graphics dstOffset = bufferOffsetInBytes, size = dataLengthInBytes }, - (Refresh.SetDataOptions) setDataOption + (Refresh.TransferOptions) setDataOption ); } @@ -102,7 +102,7 @@ namespace MoonWorks.Graphics /// public unsafe uint SetData( Span data, - SetDataOptions setDataOption + TransferOptions setDataOption ) where T : unmanaged { return SetData(data, 0, setDataOption); @@ -125,7 +125,7 @@ namespace MoonWorks.Graphics fixed (T* dataPtr = data) { - Refresh.Refresh_GetData( + Refresh.Refresh_GetTransferData( Device.Handle, Handle, (nint) dataPtr, diff --git a/src/Video/VideoPlayer.cs b/src/Video/VideoPlayer.cs index e32c018..eb9edd8 100644 --- a/src/Video/VideoPlayer.cs +++ b/src/Video/VideoPlayer.cs @@ -243,9 +243,9 @@ namespace MoonWorks.Video TransferBuffer?.Dispose(); TransferBuffer = new TransferBuffer(Device, (uint) (ySpan.Length + uSpan.Length + vSpan.Length)); } - TransferBuffer.SetData(ySpan, 0, SetDataOptions.Discard); - TransferBuffer.SetData(uSpan, (uint) ySpan.Length, SetDataOptions.Overwrite); - TransferBuffer.SetData(vSpan, (uint) (ySpan.Length + uSpan.Length), SetDataOptions.Overwrite); + TransferBuffer.SetData(ySpan, 0, TransferOptions.Discard); + TransferBuffer.SetData(uSpan, (uint) ySpan.Length, TransferOptions.Overwrite); + TransferBuffer.SetData(vSpan, (uint) (ySpan.Length + uSpan.Length), TransferOptions.Overwrite); commandBuffer.BeginCopyPass(); @@ -257,7 +257,8 @@ namespace MoonWorks.Video BufferOffset = 0, BufferStride = CurrentStream.yStride, BufferImageHeight = yTexture.Height - } + }, + CopyOptions.SafeDiscard ); commandBuffer.UploadToTexture( @@ -267,7 +268,8 @@ namespace MoonWorks.Video BufferOffset = (uint) ySpan.Length, BufferStride = CurrentStream.uvStride, BufferImageHeight = uTexture.Height - } + }, + CopyOptions.SafeDiscard ); commandBuffer.UploadToTexture( @@ -278,7 +280,8 @@ namespace MoonWorks.Video BufferOffset = (uint) (ySpan.Length + uSpan.Length), BufferStride = CurrentStream.uvStride, BufferImageHeight = vTexture.Height - } + }, + CopyOptions.SafeDiscard ); commandBuffer.EndCopyPass();