Jiawei Shao | 5490e9a | 2019-03-27 00:01:33 +0000 | [diff] [blame] | 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" |
Corentin Wallez | 04863c4 | 2019-10-25 11:36:47 +0000 | [diff] [blame] | 18 | #include "utils/WGPUHelpers.h" |
Jiawei Shao | 5490e9a | 2019-03-27 00:01:33 +0000 | [diff] [blame] | 19 | |
| 20 | class ClipSpaceTest : public DawnTest { |
| 21 | protected: |
Corentin Wallez | cab352c | 2019-10-28 13:27:36 +0000 | [diff] [blame] | 22 | wgpu::RenderPipeline CreatePipelineForTest() { |
Brandon Jones | 41c87d9 | 2021-05-21 05:01:38 +0000 | [diff] [blame] | 23 | utils::ComboRenderPipelineDescriptor pipelineDescriptor; |
Jiawei Shao | 5490e9a | 2019-03-27 00:01:33 +0000 | [diff] [blame] | 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 |
Corentin Wallez | 7aec4ae | 2021-03-24 15:55:32 +0000 | [diff] [blame] | 28 | pipelineDescriptor.vertex.module = utils::CreateShaderModule(device, R"( |
Brandon Jones | e87ea2b | 2021-04-14 17:05:07 +0000 | [diff] [blame] | 29 | [[stage(vertex)]] |
| 30 | fn main([[builtin(vertex_index)]] VertexIndex : u32) -> [[builtin(position)]] vec4<f32> { |
Corentin Wallez | b86e45f | 2021-06-17 21:36:11 +0000 | [diff] [blame] | 31 | var pos = array<vec3<f32>, 6>( |
Ben Clayton | 36edf8d | 2021-06-17 11:25:09 +0000 | [diff] [blame] | 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)); |
Brandon Jones | e87ea2b | 2021-04-14 17:05:07 +0000 | [diff] [blame] | 38 | return vec4<f32>(pos[VertexIndex], 1.0); |
Austin Eng | f6958ff | 2020-11-13 17:15:59 +0000 | [diff] [blame] | 39 | })"); |
| 40 | |
Corentin Wallez | 7aec4ae | 2021-03-24 15:55:32 +0000 | [diff] [blame] | 41 | pipelineDescriptor.cFragment.module = utils::CreateShaderModule(device, R"( |
Brandon Jones | e87ea2b | 2021-04-14 17:05:07 +0000 | [diff] [blame] | 42 | [[stage(fragment)]] fn main() -> [[location(0)]] vec4<f32> { |
| 43 | return vec4<f32>(1.0, 0.0, 0.0, 1.0); |
Austin Eng | f6958ff | 2020-11-13 17:15:59 +0000 | [diff] [blame] | 44 | })"); |
Jiawei Shao | 5490e9a | 2019-03-27 00:01:33 +0000 | [diff] [blame] | 45 | |
Brandon Jones | bff9d3a | 2021-03-18 02:54:27 +0000 | [diff] [blame] | 46 | wgpu::DepthStencilState* depthStencil = pipelineDescriptor.EnableDepthStencil(); |
| 47 | depthStencil->depthCompare = wgpu::CompareFunction::LessEqual; |
Jiawei Shao | 5490e9a | 2019-03-27 00:01:33 +0000 | [diff] [blame] | 48 | |
Brandon Jones | 41c87d9 | 2021-05-21 05:01:38 +0000 | [diff] [blame] | 49 | return device.CreateRenderPipeline(&pipelineDescriptor); |
Jiawei Shao | 5490e9a | 2019-03-27 00:01:33 +0000 | [diff] [blame] | 50 | } |
| 51 | |
Corentin Wallez | cab352c | 2019-10-28 13:27:36 +0000 | [diff] [blame] | 52 | wgpu::Texture Create2DTextureForTest(wgpu::TextureFormat format) { |
| 53 | wgpu::TextureDescriptor textureDescriptor; |
| 54 | textureDescriptor.dimension = wgpu::TextureDimension::e2D; |
Jiawei Shao | 5490e9a | 2019-03-27 00:01:33 +0000 | [diff] [blame] | 55 | textureDescriptor.format = format; |
| 56 | textureDescriptor.usage = |
Corentin Wallez | 6b08781 | 2020-10-27 15:35:56 +0000 | [diff] [blame] | 57 | wgpu::TextureUsage::RenderAttachment | wgpu::TextureUsage::CopySrc; |
Jiawei Shao | 5490e9a | 2019-03-27 00:01:33 +0000 | [diff] [blame] | 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. |
| 68 | TEST_P(ClipSpaceTest, ClipSpace) { |
Corentin Wallez | cab352c | 2019-10-28 13:27:36 +0000 | [diff] [blame] | 69 | wgpu::Texture colorTexture = Create2DTextureForTest(wgpu::TextureFormat::RGBA8Unorm); |
| 70 | wgpu::Texture depthStencilTexture = |
| 71 | Create2DTextureForTest(wgpu::TextureFormat::Depth24PlusStencil8); |
Jiawei Shao | 5490e9a | 2019-03-27 00:01:33 +0000 | [diff] [blame] | 72 | |
Kai Ninomiya | 4078ed8 | 2019-08-27 17:56:23 +0000 | [diff] [blame] | 73 | utils::ComboRenderPassDescriptor renderPassDescriptor({colorTexture.CreateView()}, |
| 74 | depthStencilTexture.CreateView()); |
Corentin Wallez | a838c7d | 2019-09-20 22:59:47 +0000 | [diff] [blame] | 75 | renderPassDescriptor.cColorAttachments[0].clearColor = {0.0, 1.0, 0.0, 1.0}; |
Corentin Wallez | cab352c | 2019-10-28 13:27:36 +0000 | [diff] [blame] | 76 | renderPassDescriptor.cColorAttachments[0].loadOp = wgpu::LoadOp::Clear; |
Jiawei Shao | 5490e9a | 2019-03-27 00:01:33 +0000 | [diff] [blame] | 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; |
Corentin Wallez | cab352c | 2019-10-28 13:27:36 +0000 | [diff] [blame] | 81 | renderPassDescriptor.cDepthStencilAttachmentInfo.depthLoadOp = wgpu::LoadOp::Clear; |
Jiawei Shao | 5490e9a | 2019-03-27 00:01:33 +0000 | [diff] [blame] | 82 | |
Corentin Wallez | cab352c | 2019-10-28 13:27:36 +0000 | [diff] [blame] | 83 | wgpu::CommandEncoder commandEncoder = device.CreateCommandEncoder(); |
| 84 | wgpu::RenderPassEncoder renderPass = commandEncoder.BeginRenderPass(&renderPassDescriptor); |
Jiawei Shao | 5490e9a | 2019-03-27 00:01:33 +0000 | [diff] [blame] | 85 | renderPass.SetPipeline(CreatePipelineForTest()); |
Corentin Wallez | 67b1ad7 | 2020-03-31 16:21:35 +0000 | [diff] [blame] | 86 | renderPass.Draw(6); |
Jiawei Shao | 5490e9a | 2019-03-27 00:01:33 +0000 | [diff] [blame] | 87 | renderPass.EndPass(); |
Corentin Wallez | cab352c | 2019-10-28 13:27:36 +0000 | [diff] [blame] | 88 | wgpu::CommandBuffer commandBuffer = commandEncoder.Finish(); |
Jiawei Shao | 5490e9a | 2019-03-27 00:01:33 +0000 | [diff] [blame] | 89 | queue.Submit(1, &commandBuffer); |
| 90 | |
Yunchao He | 0c02f54 | 2019-11-19 17:57:30 +0000 | [diff] [blame] | 91 | EXPECT_PIXEL_RGBA8_EQ(RGBA8::kRed, colorTexture, kSize - 1, kSize - 1); |
| 92 | EXPECT_PIXEL_RGBA8_EQ(RGBA8::kGreen, colorTexture, 0, 0); |
Jiawei Shao | 5490e9a | 2019-03-27 00:01:33 +0000 | [diff] [blame] | 93 | } |
| 94 | |
Kai Ninomiya | 2afea0c | 2020-07-10 20:33:08 +0000 | [diff] [blame] | 95 | DAWN_INSTANTIATE_TEST(ClipSpaceTest, |
| 96 | D3D12Backend(), |
| 97 | MetalBackend(), |
| 98 | OpenGLBackend(), |
Stephen White | ab59a10 | 2020-12-01 18:37:19 +0000 | [diff] [blame] | 99 | OpenGLESBackend(), |
Kai Ninomiya | 2afea0c | 2020-07-10 20:33:08 +0000 | [diff] [blame] | 100 | VulkanBackend()); |