From 798dada35a92bc3d5efdaa2b2b887eef67029543 Mon Sep 17 00:00:00 2001 From: cosmonaut Date: Thu, 14 Jan 2021 19:14:56 -0800 Subject: [PATCH] general fixes --- lib/RefreshCS | 2 +- src/CommandBuffer.cs | 62 +++++++++++++++++++++++++++-------- src/GraphicsPipeline.cs | 12 +++---- src/RefreshDevice.cs | 15 +++++---- src/State/ColorBlendState.cs | 1 - src/State/VertexInputState.cs | 8 ++--- src/State/ViewportState.cs | 4 +-- src/Texture.cs | 4 ++- src/VertexAttribute.cs | 12 ------- src/VertexBinding.cs | 11 ------- src/Viewport.cs | 12 ------- 11 files changed, 72 insertions(+), 71 deletions(-) delete mode 100644 src/VertexAttribute.cs delete mode 100644 src/VertexBinding.cs delete mode 100644 src/Viewport.cs diff --git a/lib/RefreshCS b/lib/RefreshCS index 377d53f..3379b95 160000 --- a/lib/RefreshCS +++ b/lib/RefreshCS @@ -1 +1 @@ -Subproject commit 377d53f7a9645758a5702d86fa9059c4d8bd1835 +Subproject commit 3379b95e449904b30d66d8bc17e4da83fc60c1be diff --git a/src/CommandBuffer.cs b/src/CommandBuffer.cs index fca7f61..609a75e 100644 --- a/src/CommandBuffer.cs +++ b/src/CommandBuffer.cs @@ -39,6 +39,29 @@ namespace Campari clearColorHandle.Free(); } + public void BeginRenderPass( + RenderPass renderPass, + Framebuffer framebuffer, + ref Refresh.Rect renderArea, + Refresh.Color[] clearColors + ) { + var clearColorHandle = GCHandle.Alloc(clearColors, GCHandleType.Pinned); + + Refresh.Refresh_BeginRenderPass( + Device.Handle, + Handle, + renderPass.Handle, + framebuffer.Handle, + ref renderArea, + clearColorHandle.AddrOfPinnedObject(), + (uint) clearColors.Length, + IntPtr.Zero + ); + + clearColorHandle.Free(); + + } + public void BindGraphicsPipeline( GraphicsPipeline graphicsPipeline ) { @@ -50,7 +73,7 @@ namespace Campari } public unsafe uint PushVertexShaderParams( - T[] uniforms + params T[] uniforms ) where T : unmanaged { fixed (T* ptr = &uniforms[0]) @@ -65,7 +88,7 @@ namespace Campari } public unsafe uint PushFragmentShaderParams( - T[] uniforms + params T[] uniforms ) where T : unmanaged { fixed (T* ptr = &uniforms[0]) @@ -79,13 +102,18 @@ namespace Campari } } - public void BindVertexBuffers( + public unsafe void BindVertexBuffers( uint firstBinding, uint bindingCount, Buffer[] buffers, UInt64[] offsets ) { - var bufferHandle = GCHandle.Alloc(buffers, GCHandleType.Pinned); + var bufferPtrs = stackalloc IntPtr[buffers.Length]; + + for (var i = 0; i < buffers.Length; i += 1) + { + bufferPtrs[i] = buffers[i].Handle; + } var offsetHandle = GCHandle.Alloc(offsets, GCHandleType.Pinned); Refresh.Refresh_BindVertexBuffers( @@ -93,11 +121,10 @@ namespace Campari Handle, firstBinding, bindingCount, - bufferHandle.AddrOfPinnedObject(), + (IntPtr) bufferPtrs, offsetHandle.AddrOfPinnedObject() ); - bufferHandle.Free(); offsetHandle.Free(); } @@ -115,22 +142,29 @@ namespace Campari ); } - public void BindFragmentSamplers( + public unsafe void BindFragmentSamplers( Texture[] textures, Sampler[] samplers ) { - var textureHandle = GCHandle.Alloc(textures, GCHandleType.Pinned); - var samplerHandle = GCHandle.Alloc(samplers, GCHandleType.Pinned); + var texturePtrs = stackalloc IntPtr[textures.Length]; + var samplerPtrs = stackalloc IntPtr[samplers.Length]; + + for (var i = 0; i < textures.Length; i += 1) + { + texturePtrs[i] = textures[i].Handle; + } + + for (var i = 0; i < samplers.Length; i += 1) + { + samplerPtrs[i] = samplers[i].Handle; + } Refresh.Refresh_BindFragmentSamplers( Device.Handle, Handle, - textureHandle.AddrOfPinnedObject(), - samplerHandle.AddrOfPinnedObject() + (IntPtr) texturePtrs, + (IntPtr) samplerPtrs ); - - textureHandle.Free(); - samplerHandle.Free(); } public void DrawPrimitives( diff --git a/src/GraphicsPipeline.cs b/src/GraphicsPipeline.cs index 27300af..169ce67 100644 --- a/src/GraphicsPipeline.cs +++ b/src/GraphicsPipeline.cs @@ -12,8 +12,8 @@ namespace Campari RefreshDevice device, ColorBlendState colorBlendState, DepthStencilState depthStencilState, - ShaderStageState fragmentShaderState, ShaderStageState vertexShaderState, + ShaderStageState fragmentShaderState, MultisampleState multisampleState, GraphicsPipelineLayoutCreateInfo pipelineLayoutCreateInfo, RasterizerState rasterizerState, @@ -34,7 +34,7 @@ namespace Campari graphicsPipelineCreateInfo.colorBlendState.logicOpEnable = Conversions.BoolToByte(colorBlendState.LogicOpEnable); graphicsPipelineCreateInfo.colorBlendState.logicOp = colorBlendState.LogicOp; graphicsPipelineCreateInfo.colorBlendState.blendStates = blendStateHandle.AddrOfPinnedObject(); - graphicsPipelineCreateInfo.colorBlendState.blendStateCount = colorBlendState.BlendStateCount; + graphicsPipelineCreateInfo.colorBlendState.blendStateCount = (uint) colorBlendState.ColorTargetBlendStates.Length; graphicsPipelineCreateInfo.colorBlendState.blendConstants[0] = colorBlendState.BlendConstants.R; graphicsPipelineCreateInfo.colorBlendState.blendConstants[1] = colorBlendState.BlendConstants.G; graphicsPipelineCreateInfo.colorBlendState.blendConstants[2] = colorBlendState.BlendConstants.B; @@ -75,14 +75,14 @@ namespace Campari graphicsPipelineCreateInfo.rasterizerState.lineWidth = rasterizerState.LineWidth; graphicsPipelineCreateInfo.vertexInputState.vertexAttributes = vertexAttributesHandle.AddrOfPinnedObject(); - graphicsPipelineCreateInfo.vertexInputState.vertexAttributeCount = vertexInputState.VertexAttributeCount; + graphicsPipelineCreateInfo.vertexInputState.vertexAttributeCount = (uint) vertexInputState.VertexAttributes.Length; graphicsPipelineCreateInfo.vertexInputState.vertexBindings = vertexBindingsHandle.AddrOfPinnedObject(); - graphicsPipelineCreateInfo.vertexInputState.vertexBindingCount = vertexInputState.VertexBindingCount; + graphicsPipelineCreateInfo.vertexInputState.vertexBindingCount = (uint) vertexInputState.VertexBindings.Length; graphicsPipelineCreateInfo.viewportState.viewports = viewportHandle.AddrOfPinnedObject(); - graphicsPipelineCreateInfo.viewportState.viewportCount = viewportState.ViewportCount; + graphicsPipelineCreateInfo.viewportState.viewportCount = (uint) viewportState.Viewports.Length; graphicsPipelineCreateInfo.viewportState.scissors = scissorHandle.AddrOfPinnedObject(); - graphicsPipelineCreateInfo.viewportState.scissorCount = viewportState.ScissorCount; + graphicsPipelineCreateInfo.viewportState.scissorCount = (uint) viewportState.Scissors.Length; graphicsPipelineCreateInfo.primitiveType = primitiveType; graphicsPipelineCreateInfo.renderPass = renderPass.Handle; diff --git a/src/RefreshDevice.cs b/src/RefreshDevice.cs index 0ca8baf..828aa3c 100644 --- a/src/RefreshDevice.cs +++ b/src/RefreshDevice.cs @@ -18,7 +18,7 @@ namespace Campari ) { Handle = Refresh.Refresh_CreateDevice( ref presentationParameters, - (byte) (debugMode ? 1 : 0) + Conversions.BoolToByte(debugMode) ); } @@ -29,17 +29,20 @@ namespace Campari return new CommandBuffer(this, commandBufferHandle); } - public void Submit(CommandBuffer[] commandBuffers) + public unsafe void Submit(params CommandBuffer[] commandBuffers) { - var commandBufferHandle = GCHandle.Alloc(commandBuffers, GCHandleType.Pinned); + var commandBufferPtrs = stackalloc IntPtr[commandBuffers.Length]; + + for (var i = 0; i < commandBuffers.Length; i += 1) + { + commandBufferPtrs[i] = commandBuffers[i].Handle; + } Refresh.Refresh_Submit( Handle, (uint) commandBuffers.Length, - commandBufferHandle.AddrOfPinnedObject() + (IntPtr) commandBufferPtrs ); - - commandBufferHandle.Free(); } protected virtual void Dispose(bool disposing) diff --git a/src/State/ColorBlendState.cs b/src/State/ColorBlendState.cs index 4322836..ee460b8 100644 --- a/src/State/ColorBlendState.cs +++ b/src/State/ColorBlendState.cs @@ -7,7 +7,6 @@ namespace Campari public bool LogicOpEnable; public Refresh.LogicOp LogicOp; public BlendConstants BlendConstants; - public uint BlendStateCount; public Refresh.ColorTargetBlendState[] ColorTargetBlendStates; } } diff --git a/src/State/VertexInputState.cs b/src/State/VertexInputState.cs index 3e34887..81027bf 100644 --- a/src/State/VertexInputState.cs +++ b/src/State/VertexInputState.cs @@ -1,10 +1,10 @@ +using RefreshCS; + namespace Campari { public struct VertexInputState { - public VertexBinding[] VertexBindings; - public uint VertexBindingCount; - public VertexAttribute[] VertexAttributes; - public uint VertexAttributeCount; + public Refresh.VertexBinding[] VertexBindings; + public Refresh.VertexAttribute[] VertexAttributes; } } diff --git a/src/State/ViewportState.cs b/src/State/ViewportState.cs index b66cb38..8d05681 100644 --- a/src/State/ViewportState.cs +++ b/src/State/ViewportState.cs @@ -4,9 +4,7 @@ namespace Campari { public struct ViewportState { - public Viewport[] Viewports; - public uint ViewportCount; + public Refresh.Viewport[] Viewports; public Refresh.Rect[] Scissors; - public uint ScissorCount; } } diff --git a/src/Texture.cs b/src/Texture.cs index 6b524a5..140641f 100644 --- a/src/Texture.cs +++ b/src/Texture.cs @@ -6,8 +6,8 @@ namespace Campari { public class Texture : GraphicsResource { - public uint Height { get; } public uint Width { get; } + public uint Height { get; } public Refresh.ColorFormat Format { get; } protected override Action QueueDestroyFunction => Refresh.Refresh_QueueDestroyTexture; @@ -47,6 +47,8 @@ namespace Campari ); Format = textureCreateInfo.format; + Width = textureCreateInfo.width; + Height = textureCreateInfo.height; } public void SetData(IntPtr data, uint dataLengthInBytes) diff --git a/src/VertexAttribute.cs b/src/VertexAttribute.cs deleted file mode 100644 index 5bb51bc..0000000 --- a/src/VertexAttribute.cs +++ /dev/null @@ -1,12 +0,0 @@ -using RefreshCS; - -namespace Campari -{ - public struct VertexAttribute - { - public uint Binding; - public uint Location; - public Refresh.VertexElementFormat Format; - public uint Offset; - } -} diff --git a/src/VertexBinding.cs b/src/VertexBinding.cs deleted file mode 100644 index 6b330e2..0000000 --- a/src/VertexBinding.cs +++ /dev/null @@ -1,11 +0,0 @@ -using RefreshCS; - -namespace Campari -{ - public struct VertexBinding - { - public uint Binding; - public Refresh.VertexInputRate VertexInputRate; - public uint Stride; - } -} diff --git a/src/Viewport.cs b/src/Viewport.cs deleted file mode 100644 index db5dda1..0000000 --- a/src/Viewport.cs +++ /dev/null @@ -1,12 +0,0 @@ -namespace Campari -{ - public struct Viewport - { - public float X; - public float Y; - public float W; - public float H; - public float MinDepth; - public float MaxDepth; - } -}