From 00adec189cc919654f5fa9ac74690a809cdf29bc Mon Sep 17 00:00:00 2001 From: cosmonaut Date: Fri, 1 Mar 2024 15:03:14 -0800 Subject: [PATCH] update bindings and WriteOption API --- lib/RefreshCS | 2 +- src/Graphics/Bindings/BufferBinding.cs | 24 +- src/Graphics/Bindings/ComputeBufferBinding.cs | 35 ++ .../Bindings/ComputeTextureBinding.cs | 35 ++ .../Bindings/TextureSamplerBinding.cs | 22 +- src/Graphics/CommandBuffer.cs | 447 +++++++----------- src/Graphics/Font/Font.cs | 2 +- src/Graphics/Font/TextBatch.cs | 4 +- src/Graphics/RefreshEnums.cs | 2 +- src/Graphics/RefreshStructs.cs | 180 +++++-- src/Graphics/ResourceUploader.cs | 18 +- src/Video/VideoPlayer.cs | 8 +- 12 files changed, 445 insertions(+), 334 deletions(-) create mode 100644 src/Graphics/Bindings/ComputeBufferBinding.cs create mode 100644 src/Graphics/Bindings/ComputeTextureBinding.cs diff --git a/lib/RefreshCS b/lib/RefreshCS index 020e767..4268db4 160000 --- a/lib/RefreshCS +++ b/lib/RefreshCS @@ -1 +1 @@ -Subproject commit 020e76782a2aba7861a8e356d1d212a2bffb2d2e +Subproject commit 4268db46161ec5ff924c8006a66aa59635d3ca50 diff --git a/src/Graphics/Bindings/BufferBinding.cs b/src/Graphics/Bindings/BufferBinding.cs index c64f767..72584f6 100644 --- a/src/Graphics/Bindings/BufferBinding.cs +++ b/src/Graphics/Bindings/BufferBinding.cs @@ -1,17 +1,21 @@ -namespace MoonWorks.Graphics +using RefreshCS; + +namespace MoonWorks.Graphics { /// - /// A buffer-offset pair to be used when binding vertex buffers. + /// A buffer-offset pair to be used when binding vertex or index buffers. /// - public struct BufferBinding - { - public GpuBuffer Buffer; - public ulong Offset; - - public BufferBinding(GpuBuffer buffer, ulong offset) + public readonly record struct BufferBinding( + GpuBuffer Buffer, + uint Offset + ) { + public Refresh.BufferBinding ToRefresh() { - Buffer = buffer; - Offset = offset; + return new Refresh.BufferBinding + { + gpuBuffer = Buffer.Handle, + offset = Offset + }; } } } diff --git a/src/Graphics/Bindings/ComputeBufferBinding.cs b/src/Graphics/Bindings/ComputeBufferBinding.cs new file mode 100644 index 0000000..f166972 --- /dev/null +++ b/src/Graphics/Bindings/ComputeBufferBinding.cs @@ -0,0 +1,35 @@ +using RefreshCS; + +namespace MoonWorks.Graphics +{ + /// + /// Binding specification to be used when binding buffers for compute shaders. + /// + /// The GpuBuffer to bind. + /// + /// Specifies data dependency behavior when this buffer is written to in the shader.
+ /// + /// SafeDiscard: + /// If this buffer has been used in commands that have not finished, + /// this option will prevent a dependency on those commands + /// at the cost of increased memory usage. + /// You may NOT assume that any of the previous data is retained. + /// Otherwise this option is equivalent to SafeOverwrite.
+ /// + /// SafeOverwrite: + /// Overwrites the data safely using a GPU memory barrier. + /// + public readonly record struct ComputeBufferBinding( + GpuBuffer GpuBuffer, + WriteOptions WriteOption + ) { + public Refresh.ComputeBufferBinding ToRefresh() + { + return new Refresh.ComputeBufferBinding + { + gpuBuffer = GpuBuffer.Handle, + writeOption = (Refresh.WriteOptions) WriteOption + }; + } + } +} diff --git a/src/Graphics/Bindings/ComputeTextureBinding.cs b/src/Graphics/Bindings/ComputeTextureBinding.cs new file mode 100644 index 0000000..99219bf --- /dev/null +++ b/src/Graphics/Bindings/ComputeTextureBinding.cs @@ -0,0 +1,35 @@ +using RefreshCS; + +namespace MoonWorks.Graphics +{ + /// + /// Binding specification used for binding texture slices for compute shaders. + /// + /// The TextureSlice to bind. + /// + /// Specifies data dependency behavior when this texture is written to in the shader.
+ /// + /// SafeDiscard: + /// If this texture slice has been used in commands that have not finished, + /// this option will prevent a dependency on those commands + /// at the cost of increased memory usage. + /// You may NOT assume that any of the previous texture (not slice!) data is retained. + /// Otherwise this option is equivalent to SafeOverwrite.
+ /// + /// SafeOverwrite: + /// Overwrites the data safely using a GPU memory barrier. + /// + public readonly record struct ComputeTextureBinding( + TextureSlice TextureSlice, + WriteOptions WriteOption + ) { + public Refresh.ComputeTextureBinding ToRefresh() + { + return new Refresh.ComputeTextureBinding + { + textureSlice = TextureSlice.ToRefreshTextureSlice(), + writeOption = (Refresh.WriteOptions) WriteOption + }; + } + } +} diff --git a/src/Graphics/Bindings/TextureSamplerBinding.cs b/src/Graphics/Bindings/TextureSamplerBinding.cs index ec2bf3d..e7c09db 100644 --- a/src/Graphics/Bindings/TextureSamplerBinding.cs +++ b/src/Graphics/Bindings/TextureSamplerBinding.cs @@ -1,17 +1,21 @@ -namespace MoonWorks.Graphics +using RefreshCS; + +namespace MoonWorks.Graphics { /// /// A texture-sampler pair to be used when binding samplers. /// - public struct TextureSamplerBinding - { - public Texture Texture; - public Sampler Sampler; - - public TextureSamplerBinding(Texture texture, Sampler sampler) + public readonly record struct TextureSamplerBinding( + Texture Texture, + Sampler Sampler + ) { + public Refresh.TextureSamplerBinding ToRefresh() { - Texture = texture; - Sampler = sampler; + return new Refresh.TextureSamplerBinding + { + texture = Texture.Handle, + sampler = Sampler.Handle + }; } } } diff --git a/src/Graphics/CommandBuffer.cs b/src/Graphics/CommandBuffer.cs index f9c6e79..b1f4d43 100644 --- a/src/Graphics/CommandBuffer.cs +++ b/src/Graphics/CommandBuffer.cs @@ -670,19 +670,15 @@ namespace MoonWorks.Graphics AssertGraphicsPipelineBound(); #endif - var bufferPtrs = stackalloc IntPtr[1]; - var offsets = stackalloc ulong[1]; - - bufferPtrs[0] = bufferBinding.Buffer.Handle; - offsets[0] = bufferBinding.Offset; + var bindingArray = stackalloc Refresh.BufferBinding[1]; + bindingArray[0] = bufferBinding.ToRefresh(); Refresh.Refresh_BindVertexBuffers( Device.Handle, Handle, firstBinding, 1, - (IntPtr) bufferPtrs, - (IntPtr) offsets + bindingArray ); } @@ -702,22 +698,16 @@ namespace MoonWorks.Graphics AssertGraphicsPipelineBound(); #endif - var bufferPtrs = stackalloc IntPtr[2]; - var offsets = stackalloc ulong[2]; - - bufferPtrs[0] = bufferBindingOne.Buffer.Handle; - bufferPtrs[1] = bufferBindingTwo.Buffer.Handle; - - offsets[0] = bufferBindingOne.Offset; - offsets[1] = bufferBindingTwo.Offset; + var bindingArray = stackalloc Refresh.BufferBinding[2]; + bindingArray[0] = bufferBindingOne.ToRefresh(); + bindingArray[1] = bufferBindingTwo.ToRefresh(); Refresh.Refresh_BindVertexBuffers( Device.Handle, Handle, firstBinding, 2, - (IntPtr) bufferPtrs, - (IntPtr) offsets + bindingArray ); } @@ -739,24 +729,17 @@ namespace MoonWorks.Graphics AssertGraphicsPipelineBound(); #endif - var bufferPtrs = stackalloc IntPtr[3]; - var offsets = stackalloc ulong[3]; - - bufferPtrs[0] = bufferBindingOne.Buffer.Handle; - bufferPtrs[1] = bufferBindingTwo.Buffer.Handle; - bufferPtrs[2] = bufferBindingThree.Buffer.Handle; - - offsets[0] = bufferBindingOne.Offset; - offsets[1] = bufferBindingTwo.Offset; - offsets[2] = bufferBindingThree.Offset; + var bindingArray = stackalloc Refresh.BufferBinding[3]; + bindingArray[0] = bufferBindingOne.ToRefresh(); + bindingArray[1] = bufferBindingTwo.ToRefresh(); + bindingArray[2] = bufferBindingThree.ToRefresh(); Refresh.Refresh_BindVertexBuffers( Device.Handle, Handle, firstBinding, 3, - (IntPtr) bufferPtrs, - (IntPtr) offsets + bindingArray ); } @@ -780,26 +763,18 @@ namespace MoonWorks.Graphics AssertGraphicsPipelineBound(); #endif - var bufferPtrs = stackalloc IntPtr[4]; - var offsets = stackalloc ulong[4]; - - bufferPtrs[0] = bufferBindingOne.Buffer.Handle; - bufferPtrs[1] = bufferBindingTwo.Buffer.Handle; - bufferPtrs[2] = bufferBindingThree.Buffer.Handle; - bufferPtrs[3] = bufferBindingFour.Buffer.Handle; - - offsets[0] = bufferBindingOne.Offset; - offsets[1] = bufferBindingTwo.Offset; - offsets[2] = bufferBindingThree.Offset; - offsets[3] = bufferBindingFour.Offset; + var bindingArray = stackalloc Refresh.BufferBinding[4]; + bindingArray[0] = bufferBindingOne.ToRefresh(); + bindingArray[1] = bufferBindingTwo.ToRefresh(); + bindingArray[2] = bufferBindingThree.ToRefresh(); + bindingArray[3] = bufferBindingFour.ToRefresh(); Refresh.Refresh_BindVertexBuffers( Device.Handle, Handle, firstBinding, 4, - (IntPtr) bufferPtrs, - (IntPtr) offsets + bindingArray ); } @@ -817,13 +792,11 @@ namespace MoonWorks.Graphics AssertGraphicsPipelineBound(); #endif - var bufferPtrs = stackalloc IntPtr[bufferBindings.Length]; - var offsets = stackalloc ulong[bufferBindings.Length]; + Refresh.BufferBinding* bufferBindingsArray = (Refresh.BufferBinding*) NativeMemory.Alloc((nuint) (Marshal.SizeOf() * bufferBindings.Length)); for (var i = 0; i < bufferBindings.Length; i += 1) { - bufferPtrs[i] = bufferBindings[i].Buffer.Handle; - offsets[i] = bufferBindings[i].Offset; + bufferBindingsArray[i] = bufferBindings[i].ToRefresh(); } Refresh.Refresh_BindVertexBuffers( @@ -831,9 +804,10 @@ namespace MoonWorks.Graphics Handle, firstBinding, (uint) bufferBindings.Length, - (IntPtr) bufferPtrs, - (IntPtr) offsets + bufferBindingsArray ); + + NativeMemory.Free(bufferBindingsArray); } /// @@ -843,9 +817,8 @@ namespace MoonWorks.Graphics /// The size in bytes of the index buffer elements. /// The offset index for the buffer. public void BindIndexBuffer( - GpuBuffer indexBuffer, - IndexElementSize indexElementSize, - uint offset = 0 + BufferBinding bufferBinding, + IndexElementSize indexElementSize ) { #if DEBUG @@ -855,8 +828,7 @@ namespace MoonWorks.Graphics Refresh.Refresh_BindIndexBuffer( Device.Handle, Handle, - indexBuffer.Handle, - offset, + bufferBinding.ToRefresh(), (Refresh.IndexElementSize) indexElementSize ); } @@ -876,17 +848,13 @@ namespace MoonWorks.Graphics AssertTextureBindingUsageFlags(textureSamplerBinding.Texture); #endif - var texturePtrs = stackalloc IntPtr[1]; - var samplerPtrs = stackalloc IntPtr[1]; - - texturePtrs[0] = textureSamplerBinding.Texture.Handle; - samplerPtrs[0] = textureSamplerBinding.Sampler.Handle; + var bindingArray = stackalloc Refresh.TextureSamplerBinding[1]; + bindingArray[0] = textureSamplerBinding.ToRefresh(); Refresh.Refresh_BindVertexSamplers( Device.Handle, Handle, - (IntPtr) texturePtrs, - (IntPtr) samplerPtrs + bindingArray ); } @@ -909,20 +877,14 @@ namespace MoonWorks.Graphics AssertTextureBindingUsageFlags(textureSamplerBindingTwo.Texture); #endif - var texturePtrs = stackalloc IntPtr[2]; - var samplerPtrs = stackalloc IntPtr[2]; - - texturePtrs[0] = textureSamplerBindingOne.Texture.Handle; - texturePtrs[1] = textureSamplerBindingTwo.Texture.Handle; - - samplerPtrs[0] = textureSamplerBindingOne.Sampler.Handle; - samplerPtrs[1] = textureSamplerBindingTwo.Sampler.Handle; + var bindingArray = stackalloc Refresh.TextureSamplerBinding[2]; + bindingArray[0] = textureSamplerBindingOne.ToRefresh(); + bindingArray[1] = textureSamplerBindingTwo.ToRefresh(); Refresh.Refresh_BindVertexSamplers( Device.Handle, Handle, - (IntPtr) texturePtrs, - (IntPtr) samplerPtrs + bindingArray ); } @@ -949,22 +911,15 @@ namespace MoonWorks.Graphics AssertTextureBindingUsageFlags(textureSamplerBindingThree.Texture); #endif - var texturePtrs = stackalloc IntPtr[3]; - var samplerPtrs = stackalloc IntPtr[3]; - - texturePtrs[0] = textureSamplerBindingOne.Texture.Handle; - texturePtrs[1] = textureSamplerBindingTwo.Texture.Handle; - texturePtrs[2] = textureSamplerBindingThree.Texture.Handle; - - samplerPtrs[0] = textureSamplerBindingOne.Sampler.Handle; - samplerPtrs[1] = textureSamplerBindingTwo.Sampler.Handle; - samplerPtrs[2] = textureSamplerBindingThree.Sampler.Handle; + var bindingArray = stackalloc Refresh.TextureSamplerBinding[3]; + bindingArray[0] = textureSamplerBindingOne.ToRefresh(); + bindingArray[1] = textureSamplerBindingTwo.ToRefresh(); + bindingArray[2] = textureSamplerBindingThree.ToRefresh(); Refresh.Refresh_BindVertexSamplers( Device.Handle, Handle, - (IntPtr) texturePtrs, - (IntPtr) samplerPtrs + bindingArray ); } @@ -995,24 +950,16 @@ namespace MoonWorks.Graphics AssertTextureBindingUsageFlags(textureSamplerBindingFour.Texture); #endif - var texturePtrs = stackalloc IntPtr[4]; - var samplerPtrs = stackalloc IntPtr[4]; - - texturePtrs[0] = textureSamplerBindingOne.Texture.Handle; - texturePtrs[1] = textureSamplerBindingTwo.Texture.Handle; - texturePtrs[2] = textureSamplerBindingThree.Texture.Handle; - texturePtrs[3] = textureSamplerBindingFour.Texture.Handle; - - samplerPtrs[0] = textureSamplerBindingOne.Sampler.Handle; - samplerPtrs[1] = textureSamplerBindingTwo.Sampler.Handle; - samplerPtrs[2] = textureSamplerBindingThree.Sampler.Handle; - samplerPtrs[3] = textureSamplerBindingFour.Sampler.Handle; + var bindingArray = stackalloc Refresh.TextureSamplerBinding[4]; + bindingArray[0] = textureSamplerBindingOne.ToRefresh(); + bindingArray[1] = textureSamplerBindingTwo.ToRefresh(); + bindingArray[2] = textureSamplerBindingThree.ToRefresh(); + bindingArray[3] = textureSamplerBindingFour.ToRefresh(); Refresh.Refresh_BindVertexSamplers( Device.Handle, Handle, - (IntPtr) texturePtrs, - (IntPtr) samplerPtrs + bindingArray ); } @@ -1029,8 +976,7 @@ namespace MoonWorks.Graphics AssertVertexSamplerCount(textureSamplerBindings.Length); #endif - var texturePtrs = stackalloc IntPtr[textureSamplerBindings.Length]; - var samplerPtrs = stackalloc IntPtr[textureSamplerBindings.Length]; + Refresh.TextureSamplerBinding* bindingsArray = (Refresh.TextureSamplerBinding*) NativeMemory.Alloc((nuint) (Marshal.SizeOf() * textureSamplerBindings.Length)); for (var i = 0; i < textureSamplerBindings.Length; i += 1) { @@ -1039,16 +985,16 @@ namespace MoonWorks.Graphics AssertTextureBindingUsageFlags(textureSamplerBindings[i].Texture); #endif - texturePtrs[i] = textureSamplerBindings[i].Texture.Handle; - samplerPtrs[i] = textureSamplerBindings[i].Sampler.Handle; + bindingsArray[i] = textureSamplerBindings[i].ToRefresh(); } Refresh.Refresh_BindVertexSamplers( Device.Handle, Handle, - (IntPtr) texturePtrs, - (IntPtr) samplerPtrs + bindingsArray ); + + NativeMemory.Free(bindingsArray); } /// @@ -1066,17 +1012,13 @@ namespace MoonWorks.Graphics AssertTextureBindingUsageFlags(textureSamplerBinding.Texture); #endif - var texturePtrs = stackalloc IntPtr[1]; - var samplerPtrs = stackalloc IntPtr[1]; - - texturePtrs[0] = textureSamplerBinding.Texture.Handle; - samplerPtrs[0] = textureSamplerBinding.Sampler.Handle; + var bindingArray = stackalloc Refresh.TextureSamplerBinding[1]; + bindingArray[0] = textureSamplerBinding.ToRefresh(); Refresh.Refresh_BindFragmentSamplers( Device.Handle, Handle, - (IntPtr) texturePtrs, - (IntPtr) samplerPtrs + bindingArray ); } @@ -1099,20 +1041,14 @@ namespace MoonWorks.Graphics AssertTextureBindingUsageFlags(textureSamplerBindingTwo.Texture); #endif - var texturePtrs = stackalloc IntPtr[2]; - var samplerPtrs = stackalloc IntPtr[2]; - - texturePtrs[0] = textureSamplerBindingOne.Texture.Handle; - texturePtrs[1] = textureSamplerBindingTwo.Texture.Handle; - - samplerPtrs[0] = textureSamplerBindingOne.Sampler.Handle; - samplerPtrs[1] = textureSamplerBindingTwo.Sampler.Handle; + var bindingArray = stackalloc Refresh.TextureSamplerBinding[2]; + bindingArray[0] = textureSamplerBindingOne.ToRefresh(); + bindingArray[1] = textureSamplerBindingTwo.ToRefresh(); Refresh.Refresh_BindFragmentSamplers( Device.Handle, Handle, - (IntPtr) texturePtrs, - (IntPtr) samplerPtrs + bindingArray ); } @@ -1139,22 +1075,15 @@ namespace MoonWorks.Graphics AssertTextureBindingUsageFlags(textureSamplerBindingThree.Texture); #endif - var texturePtrs = stackalloc IntPtr[3]; - var samplerPtrs = stackalloc IntPtr[3]; - - texturePtrs[0] = textureSamplerBindingOne.Texture.Handle; - texturePtrs[1] = textureSamplerBindingTwo.Texture.Handle; - texturePtrs[2] = textureSamplerBindingThree.Texture.Handle; - - samplerPtrs[0] = textureSamplerBindingOne.Sampler.Handle; - samplerPtrs[1] = textureSamplerBindingTwo.Sampler.Handle; - samplerPtrs[2] = textureSamplerBindingThree.Sampler.Handle; + var bindingArray = stackalloc Refresh.TextureSamplerBinding[3]; + bindingArray[0] = textureSamplerBindingOne.ToRefresh(); + bindingArray[1] = textureSamplerBindingTwo.ToRefresh(); + bindingArray[2] = textureSamplerBindingThree.ToRefresh(); Refresh.Refresh_BindFragmentSamplers( Device.Handle, Handle, - (IntPtr) texturePtrs, - (IntPtr) samplerPtrs + bindingArray ); } @@ -1185,24 +1114,16 @@ namespace MoonWorks.Graphics AssertTextureBindingUsageFlags(textureSamplerBindingFour.Texture); #endif - var texturePtrs = stackalloc IntPtr[4]; - var samplerPtrs = stackalloc IntPtr[4]; - - texturePtrs[0] = textureSamplerBindingOne.Texture.Handle; - texturePtrs[1] = textureSamplerBindingTwo.Texture.Handle; - texturePtrs[2] = textureSamplerBindingThree.Texture.Handle; - texturePtrs[3] = textureSamplerBindingFour.Texture.Handle; - - samplerPtrs[0] = textureSamplerBindingOne.Sampler.Handle; - samplerPtrs[1] = textureSamplerBindingTwo.Sampler.Handle; - samplerPtrs[2] = textureSamplerBindingThree.Sampler.Handle; - samplerPtrs[3] = textureSamplerBindingFour.Sampler.Handle; + var bindingArray = stackalloc Refresh.TextureSamplerBinding[4]; + bindingArray[0] = textureSamplerBindingOne.ToRefresh(); + bindingArray[1] = textureSamplerBindingTwo.ToRefresh(); + bindingArray[2] = textureSamplerBindingThree.ToRefresh(); + bindingArray[3] = textureSamplerBindingFour.ToRefresh(); Refresh.Refresh_BindFragmentSamplers( Device.Handle, Handle, - (IntPtr) texturePtrs, - (IntPtr) samplerPtrs + bindingArray ); } @@ -1219,8 +1140,7 @@ namespace MoonWorks.Graphics AssertFragmentSamplerCount(textureSamplerBindings.Length); #endif - var texturePtrs = stackalloc IntPtr[textureSamplerBindings.Length]; - var samplerPtrs = stackalloc IntPtr[textureSamplerBindings.Length]; + Refresh.TextureSamplerBinding* bindingArray = (Refresh.TextureSamplerBinding*) NativeMemory.Alloc((nuint) (Marshal.SizeOf() * textureSamplerBindings.Length)); for (var i = 0; i < textureSamplerBindings.Length; i += 1) { @@ -1229,16 +1149,16 @@ namespace MoonWorks.Graphics AssertTextureBindingUsageFlags(textureSamplerBindings[i].Texture); #endif - texturePtrs[i] = textureSamplerBindings[i].Texture.Handle; - samplerPtrs[i] = textureSamplerBindings[i].Sampler.Handle; + bindingArray[i] = textureSamplerBindings[i].ToRefresh(); } Refresh.Refresh_BindFragmentSamplers( Device.Handle, Handle, - (IntPtr) texturePtrs, - (IntPtr) samplerPtrs + bindingArray ); + + NativeMemory.Free(bindingArray); } /// @@ -1466,16 +1386,20 @@ namespace MoonWorks.Graphics /// /// This operation cannot be performed inside any pass. /// + /// Specifies data dependency behavior. public void Blit( - Texture source, - Texture destination, - Filter filter + TextureSlice source, + TextureSlice destination, + Filter filter, + WriteOptions writeOption ) { var sampler = filter == Filter.Linear ? Device.LinearSampler : Device.PointSampler; - BeginRenderPass(new ColorAttachmentInfo(destination)); + // FIXME: this will break with non-2D textures + // FIXME: this should take a TextureRegion + BeginRenderPass(new ColorAttachmentInfo(destination, writeOption)); BindGraphicsPipeline(Device.BlitPipeline); - BindFragmentSamplers(new TextureSamplerBinding(source, sampler)); + BindFragmentSamplers(new TextureSamplerBinding(source.Texture, sampler)); DrawPrimitives(0, 2); EndRenderPass(); } @@ -1520,9 +1444,8 @@ namespace MoonWorks.Graphics /// /// Binds a buffer to be used in the compute shader. /// - /// A buffer to bind. public unsafe void BindComputeBuffers( - GpuBuffer buffer + ComputeBufferBinding binding ) { #if DEBUG AssertNotSubmitted(); @@ -1531,24 +1454,22 @@ namespace MoonWorks.Graphics AssertComputeBufferCount(1); #endif - var bufferPtrs = stackalloc IntPtr[1]; - bufferPtrs[0] = buffer.Handle; + var bindingArray = stackalloc Refresh.ComputeBufferBinding[1]; + bindingArray[0] = binding.ToRefresh(); Refresh.Refresh_BindComputeBuffers( Device.Handle, Handle, - (IntPtr) bufferPtrs + bindingArray ); } /// /// Binds buffers to be used in the compute shader. /// - /// A buffer to bind. - /// A buffer to bind. public unsafe void BindComputeBuffers( - GpuBuffer bufferOne, - GpuBuffer bufferTwo + ComputeBufferBinding bindingOne, + ComputeBufferBinding bindingTwo ) { #if DEBUG AssertNotSubmitted(); @@ -1557,27 +1478,24 @@ namespace MoonWorks.Graphics AssertComputeBufferCount(2); #endif - var bufferPtrs = stackalloc IntPtr[2]; - bufferPtrs[0] = bufferOne.Handle; - bufferPtrs[1] = bufferTwo.Handle; + var bindingArray = stackalloc Refresh.ComputeBufferBinding[2]; + bindingArray[0] = bindingOne.ToRefresh(); + bindingArray[1] = bindingTwo.ToRefresh(); Refresh.Refresh_BindComputeBuffers( Device.Handle, Handle, - (IntPtr) bufferPtrs + bindingArray ); } /// /// Binds buffers to be used in the compute shader. /// - /// A buffer to bind. - /// A buffer to bind. - /// A buffer to bind. public unsafe void BindComputeBuffers( - GpuBuffer bufferOne, - GpuBuffer bufferTwo, - GpuBuffer bufferThree + ComputeBufferBinding bindingOne, + ComputeBufferBinding bindingTwo, + ComputeBufferBinding bindingThree ) { #if DEBUG AssertNotSubmitted(); @@ -1586,30 +1504,26 @@ namespace MoonWorks.Graphics AssertComputeBufferCount(3); #endif - var bufferPtrs = stackalloc IntPtr[3]; - bufferPtrs[0] = bufferOne.Handle; - bufferPtrs[1] = bufferTwo.Handle; - bufferPtrs[2] = bufferThree.Handle; + var bindingArray = stackalloc Refresh.ComputeBufferBinding[3]; + bindingArray[0] = bindingOne.ToRefresh(); + bindingArray[1] = bindingTwo.ToRefresh(); + bindingArray[2] = bindingThree.ToRefresh(); Refresh.Refresh_BindComputeBuffers( Device.Handle, Handle, - (IntPtr) bufferPtrs + bindingArray ); } /// /// Binds buffers to be used in the compute shader. /// - /// A buffer to bind. - /// A buffer to bind. - /// A buffer to bind. - /// A buffer to bind. public unsafe void BindComputeBuffers( - GpuBuffer bufferOne, - GpuBuffer bufferTwo, - GpuBuffer bufferThree, - GpuBuffer bufferFour + ComputeBufferBinding bindingOne, + ComputeBufferBinding bindingTwo, + ComputeBufferBinding bindingThree, + ComputeBufferBinding bindingFour ) { #if DEBUG AssertNotSubmitted(); @@ -1618,16 +1532,16 @@ namespace MoonWorks.Graphics AssertComputeBufferCount(4); #endif - var bufferPtrs = stackalloc IntPtr[4]; - bufferPtrs[0] = bufferOne.Handle; - bufferPtrs[1] = bufferTwo.Handle; - bufferPtrs[2] = bufferThree.Handle; - bufferPtrs[3] = bufferFour.Handle; + var bindingArray = stackalloc Refresh.ComputeBufferBinding[4]; + bindingArray[0] = bindingOne.ToRefresh(); + bindingArray[1] = bindingTwo.ToRefresh(); + bindingArray[2] = bindingThree.ToRefresh(); + bindingArray[3] = bindingFour.ToRefresh(); Refresh.Refresh_BindComputeBuffers( Device.Handle, Handle, - (IntPtr) bufferPtrs + bindingArray ); } @@ -1636,35 +1550,38 @@ namespace MoonWorks.Graphics /// /// A Span of buffers to bind. public unsafe void BindComputeBuffers( - in Span buffers + in Span bindings ) { #if DEBUG AssertNotSubmitted(); AssertInComputePass("Cannot bind compute buffers outside of compute pass!"); AssertComputePipelineBound(); - AssertComputeBufferCount(buffers.Length); + AssertComputeBufferCount(bindings.Length); #endif - var bufferPtrs = stackalloc IntPtr[buffers.Length]; + Refresh.ComputeBufferBinding* bindingArray = (Refresh.ComputeBufferBinding*) NativeMemory.Alloc( + (nuint) (Marshal.SizeOf() * bindings.Length) + ); - for (var i = 0; i < buffers.Length; i += 1) + for (var i = 0; i < bindings.Length; i += 1) { - bufferPtrs[i] = buffers[i].Handle; + bindingArray[i] = bindings[i].ToRefresh(); } Refresh.Refresh_BindComputeBuffers( Device.Handle, Handle, - (IntPtr) bufferPtrs + bindingArray ); + + NativeMemory.Free(bindingArray); } /// - /// Binds a texture to be used in the compute shader. + /// Binds a texture slice to be used in the compute shader. /// - /// A texture slice to bind. public unsafe void BindComputeTextures( - TextureSlice slice + ComputeTextureBinding binding ) { #if DEBUG AssertNotSubmitted(); @@ -1673,24 +1590,22 @@ namespace MoonWorks.Graphics AssertComputeTextureCount(1); #endif - var textureSlicePtrs = stackalloc Refresh.TextureSlice[1]; - textureSlicePtrs[0] = slice.ToRefreshTextureSlice(); + var bindingArray = stackalloc Refresh.ComputeTextureBinding[1]; + bindingArray[0] = binding.ToRefresh(); Refresh.Refresh_BindComputeTextures( Device.Handle, Handle, - (IntPtr) textureSlicePtrs + bindingArray ); } /// /// Binds textures to be used in the compute shader. /// - /// A texture-level pair to bind. - /// A texture-level pair to bind. public unsafe void BindComputeTextures( - TextureSlice sliceOne, - TextureSlice sliceTwo + ComputeTextureBinding bindingOne, + ComputeTextureBinding bindingTwo ) { #if DEBUG AssertNotSubmitted(); @@ -1699,27 +1614,24 @@ namespace MoonWorks.Graphics AssertComputeTextureCount(2); #endif - var textureSlicePtrs = stackalloc Refresh.TextureSlice[2]; - textureSlicePtrs[0] = sliceOne.ToRefreshTextureSlice(); - textureSlicePtrs[1] = sliceTwo.ToRefreshTextureSlice(); + var bindingArray = stackalloc Refresh.ComputeTextureBinding[2]; + bindingArray[0] = bindingOne.ToRefresh(); + bindingArray[1] = bindingTwo.ToRefresh(); Refresh.Refresh_BindComputeTextures( Device.Handle, Handle, - (IntPtr) textureSlicePtrs + bindingArray ); } /// /// Binds textures to be used in the compute shader. /// - /// A texture-level pair to bind. - /// A texture-level pair to bind. - /// A texture-level pair to bind. public unsafe void BindComputeTextures( - TextureSlice sliceOne, - TextureSlice sliceTwo, - TextureSlice sliceThree + ComputeTextureBinding bindingOne, + ComputeTextureBinding bindingTwo, + ComputeTextureBinding bindingThree ) { #if DEBUG AssertNotSubmitted(); @@ -1728,30 +1640,26 @@ namespace MoonWorks.Graphics AssertComputeTextureCount(3); #endif - var textureSlicePtrs = stackalloc Refresh.TextureSlice[3]; - textureSlicePtrs[0] = sliceOne.ToRefreshTextureSlice(); - textureSlicePtrs[1] = sliceTwo.ToRefreshTextureSlice(); - textureSlicePtrs[2] = sliceThree.ToRefreshTextureSlice(); + var bindingArray = stackalloc Refresh.ComputeTextureBinding[3]; + bindingArray[0] = bindingOne.ToRefresh(); + bindingArray[1] = bindingTwo.ToRefresh(); + bindingArray[2] = bindingThree.ToRefresh(); Refresh.Refresh_BindComputeTextures( Device.Handle, Handle, - (IntPtr) textureSlicePtrs + bindingArray ); } /// /// Binds textures to be used in the compute shader. /// - /// A texture-level pair to bind. - /// A texture-level pair to bind. - /// A texture-level pair to bind. - /// A texture-level pair to bind. public unsafe void BindComputeTextures( - TextureSlice sliceOne, - TextureSlice sliceTwo, - TextureSlice sliceThree, - TextureSlice sliceFour + ComputeTextureBinding bindingOne, + ComputeTextureBinding bindingTwo, + ComputeTextureBinding bindingThree, + ComputeTextureBinding bindingFour ) { #if DEBUG AssertNotSubmitted(); @@ -1760,49 +1668,48 @@ namespace MoonWorks.Graphics AssertComputeTextureCount(4); #endif - var textureSlicePtrs = stackalloc Refresh.TextureSlice[4]; - textureSlicePtrs[0] = sliceOne.ToRefreshTextureSlice(); - textureSlicePtrs[1] = sliceTwo.ToRefreshTextureSlice(); - textureSlicePtrs[2] = sliceThree.ToRefreshTextureSlice(); - textureSlicePtrs[3] = sliceFour.ToRefreshTextureSlice(); + var textureSlicePtrs = stackalloc Refresh.ComputeTextureBinding[4]; + textureSlicePtrs[0] = bindingOne.ToRefresh(); + textureSlicePtrs[1] = bindingTwo.ToRefresh(); + textureSlicePtrs[2] = bindingThree.ToRefresh(); + textureSlicePtrs[3] = bindingFour.ToRefresh(); Refresh.Refresh_BindComputeTextures( Device.Handle, Handle, - (IntPtr) textureSlicePtrs + textureSlicePtrs ); } /// /// Binds textures to be used in the compute shader. /// - /// A set of texture-level pairs to bind. public unsafe void BindComputeTextures( - in Span slices + in Span bindings ) { #if DEBUG AssertNotSubmitted(); AssertInComputePass("Cannot bind compute textures outside of compute pass!"); AssertComputePipelineBound(); - AssertComputeTextureCount(slices.Length); + AssertComputeTextureCount(bindings.Length); #endif - Refresh.TextureSlice* textureSlicePtrs = (Refresh.TextureSlice*) NativeMemory.Alloc( - (nuint) (Marshal.SizeOf() * slices.Length) + Refresh.ComputeTextureBinding* bindingArray = (Refresh.ComputeTextureBinding*) NativeMemory.Alloc( + (nuint) (Marshal.SizeOf() * bindings.Length) ); - for (var i = 0; i < slices.Length; i += 1) + for (var i = 0; i < bindings.Length; i += 1) { - textureSlicePtrs[i] = slices[i].ToRefreshTextureSlice(); + bindingArray[i] = bindings[i].ToRefresh(); } Refresh.Refresh_BindComputeTextures( Device.Handle, Handle, - (IntPtr) textureSlicePtrs + bindingArray ); - NativeMemory.Free(textureSlicePtrs); + NativeMemory.Free(bindingArray); } /// @@ -1916,6 +1823,7 @@ namespace MoonWorks.Graphics ); } + /// /// Uploads data from a TransferBuffer to a TextureSlice. /// This copy occurs on the GPU timeline. @@ -1925,11 +1833,12 @@ namespace MoonWorks.Graphics /// /// You MAY assume that the copy has finished for subsequent commands. /// + /// Specifies data dependency behavior. public void UploadToTexture( TransferBuffer transferBuffer, in TextureRegion textureRegion, in BufferImageCopy copyParams, - CopyOptions option + WriteOptions writeOption ) { #if DEBUG @@ -1944,7 +1853,7 @@ namespace MoonWorks.Graphics transferBuffer.Handle, textureRegion.ToRefreshTextureRegion(), copyParams.ToRefresh(), - (Refresh.CopyOptions) option + (Refresh.WriteOptions) writeOption ); } @@ -1954,13 +1863,13 @@ namespace MoonWorks.Graphics public void UploadToTexture( TransferBuffer transferBuffer, Texture texture, - CopyOptions option + WriteOptions writeOption ) { UploadToTexture( transferBuffer, new TextureRegion(texture), new BufferImageCopy(0, 0, 0), - option + writeOption ); } @@ -1977,7 +1886,7 @@ namespace MoonWorks.Graphics TransferBuffer transferBuffer, GpuBuffer gpuBuffer, in BufferCopy copyParams, - CopyOptions option + WriteOptions option ) { #if DEBUG AssertNotSubmitted(); @@ -1992,7 +1901,7 @@ namespace MoonWorks.Graphics transferBuffer.Handle, gpuBuffer.Handle, copyParams.ToRefresh(), - (Refresh.CopyOptions) option + (Refresh.WriteOptions) option ); } @@ -2002,7 +1911,7 @@ namespace MoonWorks.Graphics public void UploadToBuffer( TransferBuffer transferBuffer, GpuBuffer gpuBuffer, - CopyOptions option + WriteOptions option ) { UploadToBuffer( transferBuffer, @@ -2021,7 +1930,7 @@ namespace MoonWorks.Graphics uint sourceStartElement, uint destinationStartElement, uint numElements, - CopyOptions option + WriteOptions option ) where T : unmanaged { var elementSize = Marshal.SizeOf(); @@ -2145,7 +2054,7 @@ namespace MoonWorks.Graphics public void CopyTextureToTexture( in TextureRegion source, in TextureRegion destination, - CopyOptions option + WriteOptions option ) { #if DEBUG AssertNotSubmitted(); @@ -2158,7 +2067,7 @@ namespace MoonWorks.Graphics Handle, source.ToRefreshTextureRegion(), destination.ToRefreshTextureRegion(), - (Refresh.CopyOptions) option + (Refresh.WriteOptions) option ); } @@ -2169,7 +2078,7 @@ namespace MoonWorks.Graphics public void CopyTextureToTexture( Texture source, Texture destination, - CopyOptions option + WriteOptions option ) { CopyTextureToTexture( new TextureRegion(source), @@ -2188,7 +2097,7 @@ namespace MoonWorks.Graphics in TextureRegion textureRegion, GpuBuffer buffer, in BufferImageCopy copyParams, - CopyOptions option + WriteOptions option ) { #if DEBUG AssertNotSubmitted(); @@ -2202,7 +2111,7 @@ namespace MoonWorks.Graphics textureRegion.ToRefreshTextureRegion(), buffer.Handle, copyParams.ToRefresh(), - (Refresh.CopyOptions) option + (Refresh.WriteOptions) option ); } @@ -2212,7 +2121,7 @@ namespace MoonWorks.Graphics public void CopyTextureToBuffer( Texture texture, GpuBuffer buffer, - CopyOptions option + WriteOptions option ) { CopyTextureToBuffer( new TextureRegion(texture), @@ -2232,7 +2141,7 @@ namespace MoonWorks.Graphics GpuBuffer gpuBuffer, in TextureRegion textureRegion, in BufferImageCopy copyParams, - CopyOptions option + WriteOptions option ) { #if DEBUG AssertNotSubmitted(); @@ -2246,7 +2155,7 @@ namespace MoonWorks.Graphics gpuBuffer.Handle, textureRegion.ToRefreshTextureRegion(), copyParams.ToRefresh(), - (Refresh.CopyOptions) option + (Refresh.WriteOptions) option ); } @@ -2256,7 +2165,7 @@ namespace MoonWorks.Graphics public void CopyBufferToTexture( GpuBuffer buffer, Texture texture, - CopyOptions option + WriteOptions option ) { CopyBufferToTexture( buffer, @@ -2276,7 +2185,7 @@ namespace MoonWorks.Graphics GpuBuffer source, GpuBuffer destination, in BufferCopy copyParams, - CopyOptions option + WriteOptions option ) { #if DEBUG AssertNotSubmitted(); @@ -2291,7 +2200,7 @@ namespace MoonWorks.Graphics source.Handle, destination.Handle, copyParams.ToRefresh(), - (Refresh.CopyOptions) option + (Refresh.WriteOptions) option ); } @@ -2301,7 +2210,7 @@ namespace MoonWorks.Graphics public void CopyBufferToBuffer( GpuBuffer source, GpuBuffer destination, - CopyOptions option + WriteOptions option ) { CopyBufferToBuffer( source, diff --git a/src/Graphics/Font/Font.cs b/src/Graphics/Font/Font.cs index 7fc27b5..1c0c9a6 100644 --- a/src/Graphics/Font/Font.cs +++ b/src/Graphics/Font/Font.cs @@ -63,7 +63,7 @@ namespace MoonWorks.Graphics.Font commandBuffer.UploadToTexture( transferBuffer, texture, - CopyOptions.SafeOverwrite + WriteOptions.SafeOverwrite ); commandBuffer.EndCopyPass(); diff --git a/src/Graphics/Font/TextBatch.cs b/src/Graphics/Font/TextBatch.cs index 4598bbe..b73d9a3 100644 --- a/src/Graphics/Font/TextBatch.cs +++ b/src/Graphics/Font/TextBatch.cs @@ -127,8 +127,8 @@ namespace MoonWorks.Graphics.Font TransferBuffer.SetData(vertexSpan, TransferOptions.Discard); TransferBuffer.SetData(indexSpan, (uint) vertexSpan.Length, TransferOptions.Overwrite); - 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); + commandBuffer.UploadToBuffer(TransferBuffer, VertexBuffer, new BufferCopy(0, 0, (uint) vertexSpan.Length), WriteOptions.SafeDiscard); + commandBuffer.UploadToBuffer(TransferBuffer, IndexBuffer, new BufferCopy((uint) vertexSpan.Length, 0, (uint) indexSpan.Length), WriteOptions.SafeDiscard); } PrimitiveCount = vertexCount / 2; diff --git a/src/Graphics/RefreshEnums.cs b/src/Graphics/RefreshEnums.cs index 792b6a2..918bdb1 100644 --- a/src/Graphics/RefreshEnums.cs +++ b/src/Graphics/RefreshEnums.cs @@ -303,7 +303,7 @@ namespace MoonWorks.Graphics Overwrite } - public enum CopyOptions + public enum WriteOptions { SafeDiscard, SafeOverwrite diff --git a/src/Graphics/RefreshStructs.cs b/src/Graphics/RefreshStructs.cs index 376b49d..1e63f4d 100644 --- a/src/Graphics/RefreshStructs.cs +++ b/src/Graphics/RefreshStructs.cs @@ -174,29 +174,78 @@ namespace MoonWorks.Graphics } } + /// + /// Determines how a color texture will be read/written in a render pass. + /// public struct ColorAttachmentInfo { public TextureSlice TextureSlice; + + /// + /// If LoadOp is set to Clear, the texture slice will be cleared to this color. + /// public Color ClearColor; + + /// + /// Determines what is done with the texture slice memory + /// at the beginning of the render pass.
+ /// + /// Load: + /// Loads the data currently in the texture slice.
+ /// + /// Clear: + /// Clears the texture slice to a single color.
+ /// + /// DontCare: + /// The driver will do whatever it wants with the texture slice data. + /// This is a good option if you know that every single pixel will be written in the render pass. + ///
public LoadOp LoadOp; + + /// + /// Determines what is done with the texture slice memory + /// at the end of the render pass.
+ /// + /// Store: + /// Stores the results of the render pass in the texture slice memory.
+ /// + /// DontCare: + /// The driver will do whatever it wants with the texture slice memory. + ///
public StoreOp StoreOp; - public bool SafeDiscard; + + /// + /// Specifies data dependency behavior. This option is ignored if LoadOp is Load.
+ /// + /// SafeDiscard: + /// If this texture slice has been used in commands that have not completed, + /// this option will prevent a dependency on those commands + /// at the cost of increased memory usage. + /// You may NOT assume that any of the previous texture (not slice!) data is retained. + /// If the texture slice was not in use, this is equivalent to SafeOverwrite. + /// This is a good option to prevent stalls when frequently reusing a texture slice in rendering.
+ /// + /// SafeOverwrite: + /// Overwrites the data safely using a GPU memory barrier. + ///
+ public WriteOptions WriteOption; public ColorAttachmentInfo( TextureSlice textureSlice, + WriteOptions writeOption, Color clearColor, - bool safeDiscard = true, StoreOp storeOp = StoreOp.Store ) { TextureSlice = textureSlice; ClearColor = clearColor; LoadOp = LoadOp.Clear; StoreOp = storeOp; - SafeDiscard = safeDiscard; + WriteOption = writeOption; } public ColorAttachmentInfo( TextureSlice textureSlice, + WriteOptions writeOption, LoadOp loadOp = LoadOp.DontCare, StoreOp storeOp = StoreOp.Store ) { @@ -204,6 +253,7 @@ namespace MoonWorks.Graphics ClearColor = Color.White; LoadOp = loadOp; StoreOp = storeOp; + WriteOption = writeOption; } public Refresh.ColorAttachmentInfo ToRefresh() @@ -220,27 +270,104 @@ namespace MoonWorks.Graphics }, loadOp = (Refresh.LoadOp) LoadOp, storeOp = (Refresh.StoreOp) StoreOp, - safeDiscard = Conversions.BoolToByte(SafeDiscard) + writeOption = (Refresh.WriteOptions) WriteOption }; } } + /// + /// Determines how a depth/stencil texture will be read/written in a render pass. + /// public struct DepthStencilAttachmentInfo { public TextureSlice TextureSlice; + + /// + /// If LoadOp is set to Clear, the texture slice depth will be cleared to this depth value.
+ /// If StencilLoadOp is set to Clear, the texture slice stencil value will be cleared to this stencil value. + ///
public DepthStencilValue DepthStencilClearValue; + + /// + /// Determines what is done with the texture slice depth values + /// at the beginning of the render pass.
+ /// + /// Load: + /// Loads the data currently in the texture slice.
+ /// + /// Clear: + /// Clears the texture slice to a single depth value.
+ /// + /// DontCare: + /// The driver will do whatever it wants with the texture slice data. + /// This is a good option if you know that every single pixel will be written in the render pass. + ///
public LoadOp LoadOp; + + /// + /// Determines what is done with the texture slice depth values + /// at the end of the render pass.
+ /// + /// Store: + /// Stores the results of the render pass in the texture slice memory.
+ /// + /// DontCare: + /// The driver will do whatever it wants with the texture slice memory. + /// This is usually a good option for depth textures that don't need to be reused. + ///
public StoreOp StoreOp; + + /// + /// Determines what is done with the texture slice stencil values + /// at the beginning of the render pass.
+ /// + /// Load: + /// Loads the data currently in the texture slice.
+ /// + /// Clear: + /// Clears the texture slice to a single stencil value.
+ /// + /// DontCare: + /// The driver will do whatever it wants with the texture slice data. + /// This is a good option if you know that every single pixel will be written in the render pass. + ///
public LoadOp StencilLoadOp; + + /// + /// Determines what is done with the texture slice stencil values + /// at the end of the render pass.
+ /// + /// Store: + /// Stores the results of the render pass in the texture slice memory.
+ /// + /// DontCare: + /// The driver will do whatever it wants with the texture slice memory. + /// This is usually a good option for stencil textures that don't need to be reused. + ///
public StoreOp StencilStoreOp; - public bool SafeDiscard; + + /// + /// Specifies data dependency behavior. This option is ignored if LoadOp or StencilLoadOp is Load.
+ /// + /// SafeDiscard: + /// If this texture slice has been used in commands that have not completed, + /// this option will prevent a dependency on those commands + /// at the cost of increased memory usage. + /// You may NOT assume that any of the previous texture (not slice!) data is retained. + /// If the texture slice was not in use, this is equivalent to SafeOverwrite. + /// This is a good option to prevent stalls when frequently reusing a texture slice in rendering.
+ /// + /// SafeOverwrite: + /// Overwrites the data safely using a GPU memory barrier. + ///
+ public WriteOptions WriteOption; public DepthStencilAttachmentInfo( TextureSlice textureSlice, + WriteOptions writeOption, DepthStencilValue clearValue, - bool safeDiscard = true, - StoreOp depthStoreOp = StoreOp.Store, - StoreOp stencilStoreOp = StoreOp.Store + StoreOp depthStoreOp = StoreOp.DontCare, + StoreOp stencilStoreOp = StoreOp.DontCare ){ TextureSlice = textureSlice; DepthStencilClearValue = clearValue; @@ -248,15 +375,16 @@ namespace MoonWorks.Graphics StoreOp = depthStoreOp; StencilLoadOp = LoadOp.Clear; StencilStoreOp = stencilStoreOp; - SafeDiscard = safeDiscard; + WriteOption = writeOption; } public DepthStencilAttachmentInfo( TextureSlice textureSlice, + WriteOptions writeOption, LoadOp loadOp = LoadOp.DontCare, - StoreOp storeOp = StoreOp.Store, + StoreOp storeOp = StoreOp.DontCare, LoadOp stencilLoadOp = LoadOp.DontCare, - StoreOp stencilStoreOp = StoreOp.Store + StoreOp stencilStoreOp = StoreOp.DontCare ) { TextureSlice = textureSlice; DepthStencilClearValue = new DepthStencilValue(); @@ -264,6 +392,7 @@ namespace MoonWorks.Graphics StoreOp = storeOp; StencilLoadOp = stencilLoadOp; StencilStoreOp = stencilStoreOp; + WriteOption = writeOption; } public Refresh.DepthStencilAttachmentInfo ToRefresh() @@ -276,7 +405,7 @@ namespace MoonWorks.Graphics storeOp = (Refresh.StoreOp) StoreOp, stencilLoadOp = (Refresh.LoadOp) StencilLoadOp, stencilStoreOp = (Refresh.StoreOp) StencilStoreOp, - safeDiscard = Conversions.BoolToByte(SafeDiscard) + writeOption = (Refresh.WriteOptions) WriteOption }; } } @@ -345,23 +474,18 @@ namespace MoonWorks.Graphics } } + /// + /// Parameters for a copy between buffer and image. + /// + /// The offset into the buffer. + /// If 0, image data is assumed tightly packed. + /// If 0, image data is assumed tightly packed. [StructLayout(LayoutKind.Sequential)] - public struct BufferImageCopy - { - public uint BufferOffset; - public uint BufferStride; // if 0, image assumed to be tightly packed - public uint BufferImageHeight; // if 0, image assumed to be tightly packed - - public BufferImageCopy( - uint bufferOffset, - uint bufferStride, - uint bufferImageHeight - ) { - BufferOffset = bufferOffset; - BufferStride = bufferStride; - BufferImageHeight = bufferImageHeight; - } - + public readonly record struct BufferImageCopy( + uint BufferOffset, + uint BufferStride, + uint BufferImageHeight + ) { public Refresh.BufferImageCopy ToRefresh() { return new Refresh.BufferImageCopy diff --git a/src/Graphics/ResourceUploader.cs b/src/Graphics/ResourceUploader.cs index 50662bf..e7f0f3f 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, CopyOptions)> BufferUploads = new List<(GpuBuffer, BufferCopy, CopyOptions)>(); - List<(TextureRegion, uint, CopyOptions)> TextureUploads = new List<(TextureRegion, uint, CopyOptions)>(); + List<(GpuBuffer, BufferCopy, WriteOptions)> BufferUploads = new List<(GpuBuffer, BufferCopy, WriteOptions)>(); + List<(TextureRegion, uint, WriteOptions)> TextureUploads = new List<(TextureRegion, uint, WriteOptions)>(); 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, CopyOptions.SafeOverwrite); + SetBufferData(gpuBuffer, 0, data, WriteOptions.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, CopyOptions option) where T : unmanaged + public void SetBufferData(GpuBuffer buffer, uint bufferOffsetInElements, Span data, WriteOptions option) where T : unmanaged { uint elementSize = (uint) Marshal.SizeOf(); uint offsetInBytes = elementSize * bufferOffsetInElements; @@ -65,10 +65,10 @@ namespace MoonWorks.Graphics // Textures - public Texture CreateTexture2D(Span pixelData, uint width, uint height) + public Texture CreateTexture2D(Span pixelData, uint width, uint height) where T : unmanaged { var texture = Texture.CreateTexture2D(Device, width, height, TextureFormat.R8G8B8A8, TextureUsageFlags.Sampler); - SetTextureData(texture, pixelData, CopyOptions.SafeOverwrite); + SetTextureData(texture, pixelData, WriteOptions.SafeOverwrite); return texture; } @@ -158,7 +158,7 @@ namespace MoonWorks.Graphics Depth = 1 }; - SetTextureData(textureRegion, byteSpan, CopyOptions.SafeOverwrite); + SetTextureData(textureRegion, byteSpan, WriteOptions.SafeOverwrite); NativeMemory.Free(byteBuffer); } @@ -181,7 +181,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(textureRegion, pixelSpan, CopyOptions.SafeOverwrite); + SetTextureData(textureRegion, pixelSpan, WriteOptions.SafeOverwrite); ImageUtils.FreePixelData(pixelData); } @@ -205,7 +205,7 @@ namespace MoonWorks.Graphics /// /// Prepares upload of pixel data into a TextureSlice. /// - public void SetTextureData(TextureRegion textureRegion, Span data, CopyOptions option) where T : unmanaged + public void SetTextureData(TextureRegion textureRegion, Span data, WriteOptions option) where T : unmanaged { var elementSize = Marshal.SizeOf(); var dataLengthInBytes = (uint) (elementSize * data.Length); diff --git a/src/Video/VideoPlayer.cs b/src/Video/VideoPlayer.cs index b6c5125..749f571 100644 --- a/src/Video/VideoPlayer.cs +++ b/src/Video/VideoPlayer.cs @@ -258,7 +258,7 @@ namespace MoonWorks.Video BufferStride = CurrentStream.yStride, BufferImageHeight = yTexture.Height }, - CopyOptions.SafeDiscard + WriteOptions.SafeDiscard ); commandBuffer.UploadToTexture( @@ -269,7 +269,7 @@ namespace MoonWorks.Video BufferStride = CurrentStream.uvStride, BufferImageHeight = uTexture.Height }, - CopyOptions.SafeDiscard + WriteOptions.SafeDiscard ); commandBuffer.UploadToTexture( @@ -281,13 +281,13 @@ namespace MoonWorks.Video BufferStride = CurrentStream.uvStride, BufferImageHeight = vTexture.Height }, - CopyOptions.SafeDiscard + WriteOptions.SafeDiscard ); commandBuffer.EndCopyPass(); commandBuffer.BeginRenderPass( - new ColorAttachmentInfo(RenderTexture, Color.Black, true) + new ColorAttachmentInfo(RenderTexture, WriteOptions.SafeDiscard, Color.Black) ); commandBuffer.BindGraphicsPipeline(Device.VideoPipeline);