diff --git a/src/Graphics/CommandBuffer.cs b/src/Graphics/CommandBuffer.cs index baa2899..dd2e61a 100644 --- a/src/Graphics/CommandBuffer.cs +++ b/src/Graphics/CommandBuffer.cs @@ -12,21 +12,35 @@ namespace MoonWorks.Graphics public GraphicsDevice Device { get; } public IntPtr Handle { get; } - // some state for debug validation +#if DEBUG GraphicsPipeline currentGraphicsPipeline; ComputePipeline currentComputePipeline; bool renderPassActive; SampleCount currentSampleCount; + TextureFormat colorFormatOne; + TextureFormat colorFormatTwo; + TextureFormat colorFormatThree; + TextureFormat colorFormatFour; + TextureFormat depthStencilFormat; +#endif // called from RefreshDevice internal CommandBuffer(GraphicsDevice device, IntPtr handle) { Device = device; Handle = handle; + +#if DEBUG currentGraphicsPipeline = null; currentComputePipeline = null; renderPassActive = false; currentSampleCount = SampleCount.One; + colorFormatOne = TextureFormat.R8G8B8A8; + colorFormatTwo = TextureFormat.R8G8B8A8; + colorFormatThree = TextureFormat.R8G8B8A8; + colorFormatFour = TextureFormat.R8G8B8A8; + depthStencilFormat = TextureFormat.D16; +#endif } /// @@ -54,7 +68,11 @@ namespace MoonWorks.Graphics IntPtr.Zero ); +#if DEBUG renderPassActive = true; + currentSampleCount = colorAttachmentInfo.SampleCount; + colorFormatOne = colorAttachmentInfo.Texture.Format; +#endif } /// @@ -90,7 +108,12 @@ namespace MoonWorks.Graphics IntPtr.Zero ); +#if DEBUG renderPassActive = true; + currentSampleCount = colorAttachmentInfoOne.SampleCount; + colorFormatOne = colorAttachmentInfoOne.Texture.Format; + colorFormatTwo = colorAttachmentInfoTwo.Texture.Format; +#endif } /// @@ -133,7 +156,13 @@ namespace MoonWorks.Graphics IntPtr.Zero ); +#if DEBUG renderPassActive = true; + currentSampleCount = colorAttachmentInfoOne.SampleCount; + colorFormatOne = colorAttachmentInfoOne.Texture.Format; + colorFormatTwo = colorAttachmentInfoTwo.Texture.Format; + colorFormatThree = colorAttachmentInfoThree.Texture.Format; +#endif } /// @@ -183,7 +212,14 @@ namespace MoonWorks.Graphics IntPtr.Zero ); +#if DEBUG renderPassActive = true; + currentSampleCount = colorAttachmentInfoOne.SampleCount; + colorFormatOne = colorAttachmentInfoOne.Texture.Format; + colorFormatTwo = colorAttachmentInfoTwo.Texture.Format; + colorFormatThree = colorAttachmentInfoThree.Texture.Format; + colorFormatFour = colorAttachmentInfoFour.Texture.Format; +#endif } /// @@ -209,7 +245,10 @@ namespace MoonWorks.Graphics &refreshDepthStencilAttachmentInfo ); +#if DEBUG renderPassActive = true; + depthStencilFormat = depthStencilAttachmentInfo.Texture.Format; +#endif } /// @@ -243,7 +282,12 @@ namespace MoonWorks.Graphics &refreshDepthStencilAttachmentInfo ); +#if DEBUG renderPassActive = true; + currentSampleCount = colorAttachmentInfo.SampleCount; + colorFormatOne = colorAttachmentInfo.Texture.Format; + depthStencilFormat = depthStencilAttachmentInfo.Texture.Format; +#endif } /// @@ -285,7 +329,13 @@ namespace MoonWorks.Graphics &refreshDepthStencilAttachmentInfo ); +#if DEBUG renderPassActive = true; + currentSampleCount = colorAttachmentInfoOne.SampleCount; + colorFormatOne = colorAttachmentInfoOne.Texture.Format; + colorFormatTwo = colorAttachmentInfoTwo.Texture.Format; + depthStencilFormat = depthStencilAttachmentInfo.Texture.Format; +#endif } /// @@ -334,7 +384,14 @@ namespace MoonWorks.Graphics &refreshDepthStencilAttachmentInfo ); +#if DEBUG renderPassActive = true; + currentSampleCount = colorAttachmentInfoOne.SampleCount; + colorFormatOne = colorAttachmentInfoOne.Texture.Format; + colorFormatTwo = colorAttachmentInfoTwo.Texture.Format; + colorFormatThree = colorAttachmentInfoThree.Texture.Format; + depthStencilFormat = depthStencilAttachmentInfo.Texture.Format; +#endif } /// @@ -390,7 +447,15 @@ namespace MoonWorks.Graphics &refreshDepthStencilAttachmentInfo ); +#if DEBUG renderPassActive = true; + currentSampleCount = colorAttachmentInfoOne.SampleCount; + colorFormatOne = colorAttachmentInfoOne.Texture.Format; + colorFormatTwo = colorAttachmentInfoTwo.Texture.Format; + colorFormatThree = colorAttachmentInfoThree.Texture.Format; + colorFormatFour = colorAttachmentInfoFour.Texture.Format; + depthStencilFormat = depthStencilAttachmentInfo.Texture.Format; +#endif } /// @@ -406,7 +471,9 @@ namespace MoonWorks.Graphics computePipeline.Handle ); +#if DEBUG currentComputePipeline = computePipeline; +#endif } /// @@ -714,6 +781,7 @@ namespace MoonWorks.Graphics ) { #if DEBUG AssertRenderPassActive(); + AssertRenderPassPipelineFormatMatch(graphicsPipeline); if (graphicsPipeline.SampleCount != currentSampleCount) { @@ -727,7 +795,9 @@ namespace MoonWorks.Graphics graphicsPipeline.Handle ); +#if DEBUG currentGraphicsPipeline = graphicsPipeline; +#endif } /// @@ -1530,8 +1600,10 @@ namespace MoonWorks.Graphics Handle ); +#if DEBUG currentGraphicsPipeline = null; renderPassActive = false; +#endif } /// @@ -1845,6 +1917,42 @@ namespace MoonWorks.Graphics } } + private void AssertRenderPassPipelineFormatMatch(GraphicsPipeline graphicsPipeline) + { + for (var i = 0; i < graphicsPipeline.AttachmentInfo.ColorAttachmentDescriptions.Length; i += 1) + { + TextureFormat format; + if (i == 0) + { + format = colorFormatOne; + } + else if (i == 1) + { + format = colorFormatTwo; + } + else if (i == 2) + { + format = colorFormatThree; + } + else + { + format = colorFormatFour; + } + + var pipelineFormat = graphicsPipeline.AttachmentInfo.ColorAttachmentDescriptions[i].Format; + if (pipelineFormat != format) + { + throw new System.InvalidOperationException($"Color texture format mismatch! Pipeline expects {pipelineFormat}, render pass attachment is {format}"); + } + } + + var pipelineDepthFormat = graphicsPipeline.AttachmentInfo.DepthStencilFormat; + if (pipelineDepthFormat != depthStencilFormat) + { + throw new System.InvalidOperationException($"Depth texture format mismatch! Pipeline expects {pipelineDepthFormat}, render pass attachment is {depthStencilFormat}"); + } + } + private void AssertVertexSamplerCount(int count) { if (currentGraphicsPipeline.VertexShaderInfo.SamplerBindingCount != count) diff --git a/src/Graphics/Resources/GraphicsPipeline.cs b/src/Graphics/Resources/GraphicsPipeline.cs index ccf88cd..eb992ae 100644 --- a/src/Graphics/Resources/GraphicsPipeline.cs +++ b/src/Graphics/Resources/GraphicsPipeline.cs @@ -14,7 +14,11 @@ namespace MoonWorks.Graphics public GraphicsShaderInfo VertexShaderInfo { get; } public GraphicsShaderInfo FragmentShaderInfo { get; } - internal SampleCount SampleCount { get; } + public SampleCount SampleCount { get; } + +#if DEBUG + internal GraphicsPipelineAttachmentInfo AttachmentInfo { get; } +#endif public unsafe GraphicsPipeline( GraphicsDevice device, @@ -113,6 +117,10 @@ namespace MoonWorks.Graphics VertexShaderInfo = vertexShaderInfo; FragmentShaderInfo = fragmentShaderInfo; SampleCount = multisampleState.MultisampleCount; + +#if DEBUG + AttachmentInfo = attachmentInfo; +#endif } } }