• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 CullingTest : public DawnTest {
21   protected:
CreatePipelineForTest(wgpu::FrontFace frontFace,wgpu::CullMode cullMode)22     wgpu::RenderPipeline CreatePipelineForTest(wgpu::FrontFace frontFace, wgpu::CullMode cullMode) {
23         utils::ComboRenderPipelineDescriptor pipelineDescriptor;
24 
25         // Draw two triangles with different winding orders:
26         // 1. The top-left one is counterclockwise (CCW)
27         // 2. The bottom-right one is clockwise (CW)
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<vec2<f32>, 6>(
32                     vec2<f32>(-1.0,  1.0),
33                     vec2<f32>(-1.0,  0.0),
34                     vec2<f32>( 0.0,  1.0),
35                     vec2<f32>( 0.0, -1.0),
36                     vec2<f32>( 1.0,  0.0),
37                     vec2<f32>( 1.0, -1.0));
38                 return vec4<f32>(pos[VertexIndex], 0.0, 1.0);
39             })");
40 
41         // FragCoord of pixel(x, y) in framebuffer coordinate is (x + 0.5, y + 0.5). And we use
42         // RGBA8 format for the back buffer. So (FragCoord.xy - vec2(0.5)) / 255 in shader code
43         // will make the pixel's R and G channels exactly equal to the pixel's x and y coordinates.
44         pipelineDescriptor.cFragment.module = utils::CreateShaderModule(device, R"(
45             [[stage(fragment)]]
46             fn main([[builtin(position)]] FragCoord : vec4<f32>) -> [[location(0)]] vec4<f32> {
47                 return vec4<f32>(
48                     (FragCoord.xy - vec2<f32>(0.5, 0.5)) / vec2<f32>(255.0, 255.0),
49                     0.0, 1.0);
50             })");
51 
52         // Set culling mode and front face according to the parameters
53         pipelineDescriptor.primitive.frontFace = frontFace;
54         pipelineDescriptor.primitive.cullMode = cullMode;
55 
56         return device.CreateRenderPipeline(&pipelineDescriptor);
57     }
58 
Create2DTextureForTest(wgpu::TextureFormat format)59     wgpu::Texture Create2DTextureForTest(wgpu::TextureFormat format) {
60         wgpu::TextureDescriptor textureDescriptor;
61         textureDescriptor.dimension = wgpu::TextureDimension::e2D;
62         textureDescriptor.format = format;
63         textureDescriptor.usage =
64             wgpu::TextureUsage::RenderAttachment | wgpu::TextureUsage::CopySrc;
65         textureDescriptor.mipLevelCount = 1;
66         textureDescriptor.sampleCount = 1;
67         textureDescriptor.size = {kSize, kSize, 1};
68         return device.CreateTexture(&textureDescriptor);
69     }
70 
DoTest(wgpu::FrontFace frontFace,wgpu::CullMode cullMode,bool isCCWTriangleCulled,bool isCWTriangleCulled)71     void DoTest(wgpu::FrontFace frontFace,
72                 wgpu::CullMode cullMode,
73                 bool isCCWTriangleCulled,
74                 bool isCWTriangleCulled) {
75         wgpu::Texture colorTexture = Create2DTextureForTest(wgpu::TextureFormat::RGBA8Unorm);
76 
77         utils::ComboRenderPassDescriptor renderPassDescriptor({colorTexture.CreateView()});
78         renderPassDescriptor.cColorAttachments[0].clearColor = {0.0, 0.0, 1.0, 1.0};
79         renderPassDescriptor.cColorAttachments[0].loadOp = wgpu::LoadOp::Clear;
80 
81         wgpu::CommandEncoder commandEncoder = device.CreateCommandEncoder();
82         wgpu::RenderPassEncoder renderPass = commandEncoder.BeginRenderPass(&renderPassDescriptor);
83         renderPass.SetPipeline(CreatePipelineForTest(frontFace, cullMode));
84         renderPass.Draw(6);
85         renderPass.EndPass();
86         wgpu::CommandBuffer commandBuffer = commandEncoder.Finish();
87         queue.Submit(1, &commandBuffer);
88 
89         const RGBA8 kBackgroundColor = RGBA8::kBlue;
90         const RGBA8 kTopLeftColor = RGBA8::kBlack;
91         constexpr RGBA8 kBottomRightColor = RGBA8(3, 3, 0, 255);
92 
93         RGBA8 kCCWTriangleTopLeftColor = isCCWTriangleCulled ? kBackgroundColor : kTopLeftColor;
94         EXPECT_PIXEL_RGBA8_EQ(kCCWTriangleTopLeftColor, colorTexture, 0, 0);
95 
96         RGBA8 kCWTriangleBottomRightColor =
97             isCWTriangleCulled ? kBackgroundColor : kBottomRightColor;
98         EXPECT_PIXEL_RGBA8_EQ(kCWTriangleBottomRightColor, colorTexture, kSize - 1, kSize - 1);
99     }
100 
101     static constexpr uint32_t kSize = 4;
102 };
103 
TEST_P(CullingTest,CullNoneWhenCCWIsFrontFace)104 TEST_P(CullingTest, CullNoneWhenCCWIsFrontFace) {
105     DoTest(wgpu::FrontFace::CCW, wgpu::CullMode::None, false, false);
106 }
107 
TEST_P(CullingTest,CullFrontFaceWhenCCWIsFrontFace)108 TEST_P(CullingTest, CullFrontFaceWhenCCWIsFrontFace) {
109     DoTest(wgpu::FrontFace::CCW, wgpu::CullMode::Front, true, false);
110 }
111 
TEST_P(CullingTest,CullBackFaceWhenCCWIsFrontFace)112 TEST_P(CullingTest, CullBackFaceWhenCCWIsFrontFace) {
113     DoTest(wgpu::FrontFace::CCW, wgpu::CullMode::Back, false, true);
114 }
115 
TEST_P(CullingTest,CullNoneWhenCWIsFrontFace)116 TEST_P(CullingTest, CullNoneWhenCWIsFrontFace) {
117     DoTest(wgpu::FrontFace::CW, wgpu::CullMode::None, false, false);
118 }
119 
TEST_P(CullingTest,CullFrontFaceWhenCWIsFrontFace)120 TEST_P(CullingTest, CullFrontFaceWhenCWIsFrontFace) {
121     DoTest(wgpu::FrontFace::CW, wgpu::CullMode::Front, false, true);
122 }
123 
TEST_P(CullingTest,CullBackFaceWhenCWIsFrontFace)124 TEST_P(CullingTest, CullBackFaceWhenCWIsFrontFace) {
125     DoTest(wgpu::FrontFace::CW, wgpu::CullMode::Back, true, false);
126 }
127 
128 DAWN_INSTANTIATE_TEST(CullingTest,
129                       D3D12Backend(),
130                       MetalBackend(),
131                       OpenGLBackend(),
132                       OpenGLESBackend(),
133                       VulkanBackend());
134