1 // Copyright 2019 The Dawn Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "tests/DawnTest.h"
16
17 #include "utils/ComboRenderPipelineDescriptor.h"
18 #include "utils/WGPUHelpers.h"
19
20 class ClipSpaceTest : public DawnTest {
21 protected:
CreatePipelineForTest()22 wgpu::RenderPipeline CreatePipelineForTest() {
23 utils::ComboRenderPipelineDescriptor pipelineDescriptor;
24
25 // Draw two triangles:
26 // 1. The depth value of the top-left one is >= 0.5
27 // 2. The depth value of the bottom-right one is <= 0.5
28 pipelineDescriptor.vertex.module = utils::CreateShaderModule(device, R"(
29 [[stage(vertex)]]
30 fn main([[builtin(vertex_index)]] VertexIndex : u32) -> [[builtin(position)]] vec4<f32> {
31 var pos = array<vec3<f32>, 6>(
32 vec3<f32>(-1.0, 1.0, 1.0),
33 vec3<f32>(-1.0, -1.0, 0.5),
34 vec3<f32>( 1.0, 1.0, 0.5),
35 vec3<f32>( 1.0, 1.0, 0.5),
36 vec3<f32>(-1.0, -1.0, 0.5),
37 vec3<f32>( 1.0, -1.0, 0.0));
38 return vec4<f32>(pos[VertexIndex], 1.0);
39 })");
40
41 pipelineDescriptor.cFragment.module = utils::CreateShaderModule(device, R"(
42 [[stage(fragment)]] fn main() -> [[location(0)]] vec4<f32> {
43 return vec4<f32>(1.0, 0.0, 0.0, 1.0);
44 })");
45
46 wgpu::DepthStencilState* depthStencil = pipelineDescriptor.EnableDepthStencil();
47 depthStencil->depthCompare = wgpu::CompareFunction::LessEqual;
48
49 return device.CreateRenderPipeline(&pipelineDescriptor);
50 }
51
Create2DTextureForTest(wgpu::TextureFormat format)52 wgpu::Texture Create2DTextureForTest(wgpu::TextureFormat format) {
53 wgpu::TextureDescriptor textureDescriptor;
54 textureDescriptor.dimension = wgpu::TextureDimension::e2D;
55 textureDescriptor.format = format;
56 textureDescriptor.usage =
57 wgpu::TextureUsage::RenderAttachment | wgpu::TextureUsage::CopySrc;
58 textureDescriptor.mipLevelCount = 1;
59 textureDescriptor.sampleCount = 1;
60 textureDescriptor.size = {kSize, kSize, 1};
61 return device.CreateTexture(&textureDescriptor);
62 }
63
64 static constexpr uint32_t kSize = 4;
65 };
66
67 // Test that the clip space is correctly configured.
TEST_P(ClipSpaceTest,ClipSpace)68 TEST_P(ClipSpaceTest, ClipSpace) {
69 wgpu::Texture colorTexture = Create2DTextureForTest(wgpu::TextureFormat::RGBA8Unorm);
70 wgpu::Texture depthStencilTexture =
71 Create2DTextureForTest(wgpu::TextureFormat::Depth24PlusStencil8);
72
73 utils::ComboRenderPassDescriptor renderPassDescriptor({colorTexture.CreateView()},
74 depthStencilTexture.CreateView());
75 renderPassDescriptor.cColorAttachments[0].clearColor = {0.0, 1.0, 0.0, 1.0};
76 renderPassDescriptor.cColorAttachments[0].loadOp = wgpu::LoadOp::Clear;
77
78 // Clear the depth stencil attachment to 0.5f, so only the bottom-right triangle should be
79 // drawn.
80 renderPassDescriptor.cDepthStencilAttachmentInfo.clearDepth = 0.5f;
81 renderPassDescriptor.cDepthStencilAttachmentInfo.depthLoadOp = wgpu::LoadOp::Clear;
82
83 wgpu::CommandEncoder commandEncoder = device.CreateCommandEncoder();
84 wgpu::RenderPassEncoder renderPass = commandEncoder.BeginRenderPass(&renderPassDescriptor);
85 renderPass.SetPipeline(CreatePipelineForTest());
86 renderPass.Draw(6);
87 renderPass.EndPass();
88 wgpu::CommandBuffer commandBuffer = commandEncoder.Finish();
89 queue.Submit(1, &commandBuffer);
90
91 EXPECT_PIXEL_RGBA8_EQ(RGBA8::kRed, colorTexture, kSize - 1, kSize - 1);
92 EXPECT_PIXEL_RGBA8_EQ(RGBA8::kGreen, colorTexture, 0, 0);
93 }
94
95 DAWN_INSTANTIATE_TEST(ClipSpaceTest,
96 D3D12Backend(),
97 MetalBackend(),
98 OpenGLBackend(),
99 OpenGLESBackend(),
100 VulkanBackend());
101