From 57f7bc750af295c0ef10fb89715c51754bf310a9 Mon Sep 17 00:00:00 2001 From: Corentin Wallez Date: Wed, 14 Feb 2018 15:23:32 -0500 Subject: [PATCH] Add an end2end test for SetScissorRect --- src/backend/d3d12/CommandBufferD3D12.cpp | 4 +- src/tests/CMakeLists.txt | 1 + src/tests/end2end/BlendStateTests.cpp | 2 - src/tests/end2end/DepthStencilStateTests.cpp | 1 - src/tests/end2end/ScissorTests.cpp | 183 ++++++++++++++++++ .../end2end/ViewportOrientationTests.cpp | 3 +- 6 files changed, 187 insertions(+), 7 deletions(-) create mode 100644 src/tests/end2end/ScissorTests.cpp diff --git a/src/backend/d3d12/CommandBufferD3D12.cpp b/src/backend/d3d12/CommandBufferD3D12.cpp index 195e748074..fd0d4c07c8 100644 --- a/src/backend/d3d12/CommandBufferD3D12.cpp +++ b/src/backend/d3d12/CommandBufferD3D12.cpp @@ -532,9 +532,9 @@ namespace backend { namespace d3d12 { SetScissorRectCmd* cmd = mCommands.NextCommand(); D3D12_RECT rect; rect.left = cmd->x; - rect.bottom = cmd->y; + rect.top = cmd->y; rect.right = cmd->x + cmd->width; - rect.top = cmd->y + cmd->height; + rect.bottom = cmd->y + cmd->height; commandList->RSSetScissorRects(1, &rect); } break; diff --git a/src/tests/CMakeLists.txt b/src/tests/CMakeLists.txt index c44439b5b8..93569d4da5 100644 --- a/src/tests/CMakeLists.txt +++ b/src/tests/CMakeLists.txt @@ -81,6 +81,7 @@ add_executable(nxt_end2end_tests ${END2END_TESTS_DIR}/PrimitiveTopologyTests.cpp ${END2END_TESTS_DIR}/PushConstantTests.cpp ${END2END_TESTS_DIR}/RenderPassLoadOpTests.cpp + ${END2END_TESTS_DIR}/ScissorTests.cpp ${END2END_TESTS_DIR}/ViewportOrientationTests.cpp ${TESTS_DIR}/End2EndTestsMain.cpp ${TESTS_DIR}/NXTTest.cpp diff --git a/src/tests/end2end/BlendStateTests.cpp b/src/tests/end2end/BlendStateTests.cpp index 29e0a6390d..dc65297d1d 100644 --- a/src/tests/end2end/BlendStateTests.cpp +++ b/src/tests/end2end/BlendStateTests.cpp @@ -1,4 +1,3 @@ - // Copyright 2017 The NXT Authors // // Licensed under the Apache License, Version 2.0 (the "License"); @@ -13,7 +12,6 @@ // See the License for the specific language governing permissions and // limitations under the License. - #include #include diff --git a/src/tests/end2end/DepthStencilStateTests.cpp b/src/tests/end2end/DepthStencilStateTests.cpp index a556dd8e0d..92a57564f1 100644 --- a/src/tests/end2end/DepthStencilStateTests.cpp +++ b/src/tests/end2end/DepthStencilStateTests.cpp @@ -1,4 +1,3 @@ - // Copyright 2017 The NXT Authors // // Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/src/tests/end2end/ScissorTests.cpp b/src/tests/end2end/ScissorTests.cpp new file mode 100644 index 0000000000..bddf41f249 --- /dev/null +++ b/src/tests/end2end/ScissorTests.cpp @@ -0,0 +1,183 @@ +// Copyright 2018 The NXT Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "tests/NXTTest.h" + +#include "utils/NXTHelpers.h" + +class ScissorTest: public NXTTest { + protected: + nxt::RenderPipeline CreateQuadPipeline(const nxt::RenderPass& renderPass) { + nxt::ShaderModule vsModule = utils::CreateShaderModule(device, nxt::ShaderStage::Vertex, R"( + #version 450 + const vec2 pos[6] = vec2[6]( + vec2(-1.0f, -1.0f), vec2(-1.0f, 1.0f), vec2(1.0f, -1.0f), + vec2(1.0f, 1.0f), vec2(-1.0f, 1.0f), vec2(1.0f, -1.0f) + ); + void main() { + gl_Position = vec4(pos[gl_VertexIndex], 0.5, 1.0); + })"); + + nxt::ShaderModule fsModule = utils::CreateShaderModule(device, nxt::ShaderStage::Fragment, R"( + #version 450 + layout(location = 0) out vec4 fragColor; + void main() { + fragColor = vec4(0.0f, 1.0f, 0.0f, 1.0f); + })"); + + nxt::RenderPipeline pipeline = device.CreateRenderPipelineBuilder() + .SetSubpass(renderPass, 0) + .SetStage(nxt::ShaderStage::Vertex, vsModule, "main") + .SetStage(nxt::ShaderStage::Fragment, fsModule, "main") + .GetResult(); + + return pipeline; + } +}; + +// Test that by default the scissor test is disabled and the whole attachment can be drawn to. +TEST_P(ScissorTest, DefaultsToWholeRenderTarget) { + utils::BasicFramebuffer fb = utils::CreateBasicFramebuffer(device, 100, 100); + nxt::RenderPipeline pipeline = CreateQuadPipeline(fb.renderPass); + + nxt::CommandBuffer commands = device.CreateCommandBufferBuilder() + .BeginRenderPass(fb.renderPass, fb.framebuffer) + .BeginRenderSubpass() + .SetRenderPipeline(pipeline) + .DrawArrays(6, 1, 0, 0) + .EndRenderSubpass() + .EndRenderPass() + .GetResult(); + + queue.Submit(1, &commands); + + EXPECT_PIXEL_RGBA8_EQ(RGBA8(0, 255, 0, 255), fb.color, 0, 0); + EXPECT_PIXEL_RGBA8_EQ(RGBA8(0, 255, 0, 255), fb.color, 0, 99); + EXPECT_PIXEL_RGBA8_EQ(RGBA8(0, 255, 0, 255), fb.color, 99, 0); + EXPECT_PIXEL_RGBA8_EQ(RGBA8(0, 255, 0, 255), fb.color, 99, 99); +} + +// Test setting the scissor to something larger than the attachments. +TEST_P(ScissorTest, LargerThanAttachment) { + utils::BasicFramebuffer fb = utils::CreateBasicFramebuffer(device, 100, 100); + nxt::RenderPipeline pipeline = CreateQuadPipeline(fb.renderPass); + + nxt::CommandBuffer commands = device.CreateCommandBufferBuilder() + .BeginRenderPass(fb.renderPass, fb.framebuffer) + .BeginRenderSubpass() + .SetRenderPipeline(pipeline) + .SetScissorRect(0, 0, 200, 200) + .DrawArrays(6, 1, 0, 0) + .EndRenderSubpass() + .EndRenderPass() + .GetResult(); + + queue.Submit(1, &commands); + + EXPECT_PIXEL_RGBA8_EQ(RGBA8(0, 255, 0, 255), fb.color, 0, 0); + EXPECT_PIXEL_RGBA8_EQ(RGBA8(0, 255, 0, 255), fb.color, 0, 99); + EXPECT_PIXEL_RGBA8_EQ(RGBA8(0, 255, 0, 255), fb.color, 99, 0); + EXPECT_PIXEL_RGBA8_EQ(RGBA8(0, 255, 0, 255), fb.color, 99, 99); +} + +// Test setting an empty scissor rect +TEST_P(ScissorTest, EmptyRect) { + if (IsMetal()) { + std::cout << "Test skipped on Metal" << std::endl; + return; + } + + utils::BasicFramebuffer fb = utils::CreateBasicFramebuffer(device, 2, 2); + nxt::RenderPipeline pipeline = CreateQuadPipeline(fb.renderPass); + + nxt::CommandBuffer commands = device.CreateCommandBufferBuilder() + .BeginRenderPass(fb.renderPass, fb.framebuffer) + .BeginRenderSubpass() + .SetRenderPipeline(pipeline) + .SetScissorRect(0, 0, 0, 0) + .DrawArrays(6, 1, 0, 0) + .EndRenderSubpass() + .EndRenderPass() + .GetResult(); + + queue.Submit(1, &commands); + + EXPECT_PIXEL_RGBA8_EQ(RGBA8(0, 0, 0, 0), fb.color, 0, 0); + EXPECT_PIXEL_RGBA8_EQ(RGBA8(0, 0, 0, 0), fb.color, 0, 1); + EXPECT_PIXEL_RGBA8_EQ(RGBA8(0, 0, 0, 0), fb.color, 1, 0); + EXPECT_PIXEL_RGBA8_EQ(RGBA8(0, 0, 0, 0), fb.color, 1, 1); +} + +// Test setting a partial scissor (not empty, not full attachment) +TEST_P(ScissorTest, PartialRect) { + utils::BasicFramebuffer fb = utils::CreateBasicFramebuffer(device, 100, 100); + nxt::RenderPipeline pipeline = CreateQuadPipeline(fb.renderPass); + + constexpr uint32_t kX = 3; + constexpr uint32_t kY = 7; + constexpr uint32_t kW = 5; + constexpr uint32_t kH = 13; + + + nxt::CommandBuffer commands = device.CreateCommandBufferBuilder() + .BeginRenderPass(fb.renderPass, fb.framebuffer) + .BeginRenderSubpass() + .SetRenderPipeline(pipeline) + .SetScissorRect(kX, kY, kW, kH) + .DrawArrays(6, 1, 0, 0) + .EndRenderSubpass() + .EndRenderPass() + .GetResult(); + + queue.Submit(1, &commands); + + // Test the two opposite corners of the scissor box. With one pixel inside and on outside + EXPECT_PIXEL_RGBA8_EQ(RGBA8(0, 0, 0, 0), fb.color, kX - 1, kY - 1); + EXPECT_PIXEL_RGBA8_EQ(RGBA8(0, 255, 0, 255), fb.color, kX, kY); + + EXPECT_PIXEL_RGBA8_EQ(RGBA8(0, 0, 0, 0), fb.color, kX + kW, kY + kH); + EXPECT_PIXEL_RGBA8_EQ(RGBA8(0, 255, 0, 255), fb.color, kX + kW - 1, kY + kH - 1); +} + +// Test that the scissor setting doesn't get inherited between renderpasses +// TODO(cwallez@chromium.org): do the same between subpasses? +TEST_P(ScissorTest, NoInheritanceBetweenRenderPass) { + utils::BasicFramebuffer fb = utils::CreateBasicFramebuffer(device, 100, 100); + nxt::RenderPipeline pipeline = CreateQuadPipeline(fb.renderPass); + + nxt::CommandBuffer commands = device.CreateCommandBufferBuilder() + // RenderPass 1 set the scissor + .BeginRenderPass(fb.renderPass, fb.framebuffer) + .BeginRenderSubpass() + .SetScissorRect(0, 0, 0, 0) + .EndRenderSubpass() + .EndRenderPass() + // RenderPass 2 draw a full quad, it shouldn't be scissored + .BeginRenderPass(fb.renderPass, fb.framebuffer) + .BeginRenderSubpass() + .SetRenderPipeline(pipeline) + .DrawArrays(6, 1, 0, 0) + .EndRenderSubpass() + .EndRenderPass() + .GetResult(); + + queue.Submit(1, &commands); + + EXPECT_PIXEL_RGBA8_EQ(RGBA8(0, 255, 0, 255), fb.color, 0, 0); + EXPECT_PIXEL_RGBA8_EQ(RGBA8(0, 255, 0, 255), fb.color, 0, 99); + EXPECT_PIXEL_RGBA8_EQ(RGBA8(0, 255, 0, 255), fb.color, 99, 0); + EXPECT_PIXEL_RGBA8_EQ(RGBA8(0, 255, 0, 255), fb.color, 99, 99); +} + +NXT_INSTANTIATE_TEST(ScissorTest, D3D12Backend, MetalBackend, OpenGLBackend, VulkanBackend) diff --git a/src/tests/end2end/ViewportOrientationTests.cpp b/src/tests/end2end/ViewportOrientationTests.cpp index a8d330e142..88d097f664 100644 --- a/src/tests/end2end/ViewportOrientationTests.cpp +++ b/src/tests/end2end/ViewportOrientationTests.cpp @@ -26,8 +26,7 @@ TEST_P(ViewportOrientationTests, OriginAt0x0) { #version 450 void main() { gl_Position = vec4(-0.5f, -0.5f, 0.0f, 1.0f); - })" - ); + })"); nxt::ShaderModule fsModule = utils::CreateShaderModule(device, nxt::ShaderStage::Fragment, R"( #version 450