|
| 1 | +// Copyright (c) .NET Foundation and Contributors (https://dotnetfoundation.org/ & https://stride3d.net) and Silicon Studio Corp. (https://www.siliconstudio.co.jp) |
| 2 | +// Distributed under the MIT license. See the LICENSE.md file in the project root for more information. |
| 3 | + |
| 4 | +using System; |
| 5 | + |
| 6 | +using Xunit; |
| 7 | + |
| 8 | +using Stride.Core.Mathematics; |
| 9 | +using Stride.Rendering; |
| 10 | + |
| 11 | +namespace Stride.Graphics.Tests |
| 12 | +{ |
| 13 | + public class TestFramebufferAttachmentGuard : GraphicTestGameBase |
| 14 | + { |
| 15 | + private struct Vertex |
| 16 | + { |
| 17 | + public Vector3 Position; |
| 18 | + public Vector2 TexCoords; |
| 19 | + } |
| 20 | + |
| 21 | + /// <summary> |
| 22 | + /// The Vulkan backend's debug guard turns a framebuffer/render-pass attachment-count mismatch into a clear |
| 23 | + /// exception instead of a device loss. Verify it fires when more render targets are bound than the active |
| 24 | + /// pipeline's render pass declares. |
| 25 | + /// </summary> |
| 26 | + [SkippableFact] |
| 27 | + public void ThrowsWhenBoundTargetsExceedPipelineRenderPass() |
| 28 | + { |
| 29 | + PerformTest(game => |
| 30 | + { |
| 31 | + // The guard lives in the Vulkan backend (and needs the debug device, which GraphicTestGameBase sets). |
| 32 | + Skip.IfNot(GraphicsDevice.Platform == GraphicsPlatform.Vulkan, "Attachment guard is Vulkan-only."); |
| 33 | + |
| 34 | + var device = game.GraphicsDevice; |
| 35 | + var commandList = game.GraphicsContext.CommandList; |
| 36 | + |
| 37 | + var backBuffer = device.Presenter.BackBuffer; |
| 38 | + var depth = device.Presenter.DepthStencilBuffer; |
| 39 | + var extraTarget = Texture.New2D(device, backBuffer.Width, backBuffer.Height, backBuffer.Format, TextureFlags.RenderTarget); |
| 40 | + |
| 41 | + var declaration = new VertexDeclaration(VertexElement.Position<Vector3>(), VertexElement.TextureCoordinate<Vector2>()); |
| 42 | + var vertexBuffer = Buffer.Vertex.New(device, new Vertex[3], GraphicsResourceUsage.Default); |
| 43 | + var sampledTexture = Texture.New2D(device, 4, 4, PixelFormat.R8G8B8A8_UNorm, TextureFlags.ShaderResource); |
| 44 | + |
| 45 | + var effect = new EffectInstance(new Effect(device, SpriteEffect.Bytecode)); |
| 46 | + effect.Parameters.Set(TexturingKeys.Texture0, sampledTexture); |
| 47 | + effect.Parameters.Set(TexturingKeys.Sampler, device.SamplerStates.LinearClamp); |
| 48 | + effect.UpdateEffect(device); |
| 49 | + |
| 50 | + var pipelineState = new MutablePipelineState(device); |
| 51 | + pipelineState.State.SetDefaults(); |
| 52 | + pipelineState.State.RootSignature = effect.RootSignature; |
| 53 | + pipelineState.State.EffectBytecode = effect.Effect.Bytecode; |
| 54 | + pipelineState.State.InputElements = declaration.CreateInputElements(); |
| 55 | + pipelineState.State.PrimitiveType = PrimitiveType.TriangleList; |
| 56 | + |
| 57 | + // Capture the pipeline Output with a single color + depth bound: its render pass declares 2 attachments. |
| 58 | + commandList.SetRenderTargetAndViewport(depth, backBuffer); |
| 59 | + pipelineState.State.Output.CaptureState(commandList); |
| 60 | + pipelineState.Update(); |
| 61 | + |
| 62 | + // Now bind two color targets + depth: the framebuffer would have 3 attachments, mismatching the pass. |
| 63 | + commandList.SetRenderTargets(depth, backBuffer, extraTarget); |
| 64 | + commandList.SetPipelineState(pipelineState.CurrentState); |
| 65 | + commandList.SetVertexBuffer(0, vertexBuffer, 0, declaration.VertexStride); |
| 66 | + effect.Apply(game.GraphicsContext); |
| 67 | + |
| 68 | + var exception = Assert.Throws<InvalidOperationException>(() => commandList.Draw(3)); |
| 69 | + Assert.Contains("render pass", exception.Message); |
| 70 | + |
| 71 | + sampledTexture.Dispose(); |
| 72 | + extraTarget.Dispose(); |
| 73 | + }); |
| 74 | + } |
| 75 | + } |
| 76 | +} |
0 commit comments