blob: a43190e05b7c63ee1560dd4227147e999507560c [file] [log] [blame]
Jiawei Shao5490e9a2019-03-27 00:01:33 +00001// 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"
Corentin Wallez04863c42019-10-25 11:36:47 +000018#include "utils/WGPUHelpers.h"
Jiawei Shao5490e9a2019-03-27 00:01:33 +000019
20class ClipSpaceTest : public DawnTest {
21 protected:
Corentin Wallezcab352c2019-10-28 13:27:36 +000022 wgpu::RenderPipeline CreatePipelineForTest() {
Brandon Jones41c87d92021-05-21 05:01:38 +000023 utils::ComboRenderPipelineDescriptor pipelineDescriptor;
Jiawei Shao5490e9a2019-03-27 00:01:33 +000024
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
Corentin Wallez7aec4ae2021-03-24 15:55:32 +000028 pipelineDescriptor.vertex.module = utils::CreateShaderModule(device, R"(
Brandon Jonese87ea2b2021-04-14 17:05:07 +000029 [[stage(vertex)]]
30 fn main([[builtin(vertex_index)]] VertexIndex : u32) -> [[builtin(position)]] vec4<f32> {
Corentin Wallezb86e45f2021-06-17 21:36:11 +000031 var pos = array<vec3<f32>, 6>(
Ben Clayton36edf8d2021-06-17 11:25:09 +000032 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));
Brandon Jonese87ea2b2021-04-14 17:05:07 +000038 return vec4<f32>(pos[VertexIndex], 1.0);
Austin Engf6958ff2020-11-13 17:15:59 +000039 })");
40
Corentin Wallez7aec4ae2021-03-24 15:55:32 +000041 pipelineDescriptor.cFragment.module = utils::CreateShaderModule(device, R"(
Brandon Jonese87ea2b2021-04-14 17:05:07 +000042 [[stage(fragment)]] fn main() -> [[location(0)]] vec4<f32> {
43 return vec4<f32>(1.0, 0.0, 0.0, 1.0);
Austin Engf6958ff2020-11-13 17:15:59 +000044 })");
Jiawei Shao5490e9a2019-03-27 00:01:33 +000045
Brandon Jonesbff9d3a2021-03-18 02:54:27 +000046 wgpu::DepthStencilState* depthStencil = pipelineDescriptor.EnableDepthStencil();
47 depthStencil->depthCompare = wgpu::CompareFunction::LessEqual;
Jiawei Shao5490e9a2019-03-27 00:01:33 +000048
Brandon Jones41c87d92021-05-21 05:01:38 +000049 return device.CreateRenderPipeline(&pipelineDescriptor);
Jiawei Shao5490e9a2019-03-27 00:01:33 +000050 }
51
Corentin Wallezcab352c2019-10-28 13:27:36 +000052 wgpu::Texture Create2DTextureForTest(wgpu::TextureFormat format) {
53 wgpu::TextureDescriptor textureDescriptor;
54 textureDescriptor.dimension = wgpu::TextureDimension::e2D;
Jiawei Shao5490e9a2019-03-27 00:01:33 +000055 textureDescriptor.format = format;
56 textureDescriptor.usage =
Corentin Wallez6b087812020-10-27 15:35:56 +000057 wgpu::TextureUsage::RenderAttachment | wgpu::TextureUsage::CopySrc;
Jiawei Shao5490e9a2019-03-27 00:01:33 +000058 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.
68TEST_P(ClipSpaceTest, ClipSpace) {
Corentin Wallezcab352c2019-10-28 13:27:36 +000069 wgpu::Texture colorTexture = Create2DTextureForTest(wgpu::TextureFormat::RGBA8Unorm);
70 wgpu::Texture depthStencilTexture =
71 Create2DTextureForTest(wgpu::TextureFormat::Depth24PlusStencil8);
Jiawei Shao5490e9a2019-03-27 00:01:33 +000072
Kai Ninomiya4078ed82019-08-27 17:56:23 +000073 utils::ComboRenderPassDescriptor renderPassDescriptor({colorTexture.CreateView()},
74 depthStencilTexture.CreateView());
Corentin Walleza838c7d2019-09-20 22:59:47 +000075 renderPassDescriptor.cColorAttachments[0].clearColor = {0.0, 1.0, 0.0, 1.0};
Corentin Wallezcab352c2019-10-28 13:27:36 +000076 renderPassDescriptor.cColorAttachments[0].loadOp = wgpu::LoadOp::Clear;
Jiawei Shao5490e9a2019-03-27 00:01:33 +000077
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;
Corentin Wallezcab352c2019-10-28 13:27:36 +000081 renderPassDescriptor.cDepthStencilAttachmentInfo.depthLoadOp = wgpu::LoadOp::Clear;
Jiawei Shao5490e9a2019-03-27 00:01:33 +000082
Corentin Wallezcab352c2019-10-28 13:27:36 +000083 wgpu::CommandEncoder commandEncoder = device.CreateCommandEncoder();
84 wgpu::RenderPassEncoder renderPass = commandEncoder.BeginRenderPass(&renderPassDescriptor);
Jiawei Shao5490e9a2019-03-27 00:01:33 +000085 renderPass.SetPipeline(CreatePipelineForTest());
Corentin Wallez67b1ad72020-03-31 16:21:35 +000086 renderPass.Draw(6);
Jiawei Shao5490e9a2019-03-27 00:01:33 +000087 renderPass.EndPass();
Corentin Wallezcab352c2019-10-28 13:27:36 +000088 wgpu::CommandBuffer commandBuffer = commandEncoder.Finish();
Jiawei Shao5490e9a2019-03-27 00:01:33 +000089 queue.Submit(1, &commandBuffer);
90
Yunchao He0c02f542019-11-19 17:57:30 +000091 EXPECT_PIXEL_RGBA8_EQ(RGBA8::kRed, colorTexture, kSize - 1, kSize - 1);
92 EXPECT_PIXEL_RGBA8_EQ(RGBA8::kGreen, colorTexture, 0, 0);
Jiawei Shao5490e9a2019-03-27 00:01:33 +000093}
94
Kai Ninomiya2afea0c2020-07-10 20:33:08 +000095DAWN_INSTANTIATE_TEST(ClipSpaceTest,
96 D3D12Backend(),
97 MetalBackend(),
98 OpenGLBackend(),
Stephen Whiteab59a102020-12-01 18:37:19 +000099 OpenGLESBackend(),
Kai Ninomiya2afea0c2020-07-10 20:33:08 +0000100 VulkanBackend());