• 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 constexpr uint32_t kRTSize = 4;
21 
22 class DrawTest : public DawnTest {
23   protected:
SetUp()24     void SetUp() override {
25         DawnTest::SetUp();
26 
27         renderPass = utils::CreateBasicRenderPass(device, kRTSize, kRTSize);
28 
29         wgpu::ShaderModule vsModule = utils::CreateShaderModule(device, R"(
30             [[stage(vertex)]]
31             fn main([[location(0)]] pos : vec4<f32>) -> [[builtin(position)]] vec4<f32> {
32                 return pos;
33             })");
34 
35         wgpu::ShaderModule fsModule = utils::CreateShaderModule(device, R"(
36             [[stage(fragment)]] fn main() -> [[location(0)]] vec4<f32> {
37                 return vec4<f32>(0.0, 1.0, 0.0, 1.0);
38             })");
39 
40         utils::ComboRenderPipelineDescriptor descriptor;
41         descriptor.vertex.module = vsModule;
42         descriptor.cFragment.module = fsModule;
43         descriptor.primitive.topology = wgpu::PrimitiveTopology::TriangleList;
44         descriptor.vertex.bufferCount = 1;
45         descriptor.cBuffers[0].arrayStride = 4 * sizeof(float);
46         descriptor.cBuffers[0].attributeCount = 1;
47         descriptor.cAttributes[0].format = wgpu::VertexFormat::Float32x4;
48         descriptor.cTargets[0].format = renderPass.colorFormat;
49 
50         pipeline = device.CreateRenderPipeline(&descriptor);
51 
52         vertexBuffer = utils::CreateBufferFromData<float>(
53             device, wgpu::BufferUsage::Vertex,
54             {// The bottom left triangle
55              -1.0f, 1.0f, 0.0f, 1.0f, 1.0f, -1.0f, 0.0f, 1.0f, -1.0f, -1.0f, 0.0f, 1.0f,
56 
57              // The top right triangle
58              -1.0f, 1.0f, 0.0f, 1.0f, 1.0f, -1.0f, 0.0f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f});
59     }
60 
61     utils::BasicRenderPass renderPass;
62     wgpu::RenderPipeline pipeline;
63     wgpu::Buffer vertexBuffer;
64 
Test(uint32_t vertexCount,uint32_t instanceCount,uint32_t firstIndex,uint32_t firstInstance,RGBA8 bottomLeftExpected,RGBA8 topRightExpected)65     void Test(uint32_t vertexCount,
66               uint32_t instanceCount,
67               uint32_t firstIndex,
68               uint32_t firstInstance,
69               RGBA8 bottomLeftExpected,
70               RGBA8 topRightExpected) {
71         wgpu::CommandEncoder encoder = device.CreateCommandEncoder();
72         {
73             wgpu::RenderPassEncoder pass = encoder.BeginRenderPass(&renderPass.renderPassInfo);
74             pass.SetPipeline(pipeline);
75             pass.SetVertexBuffer(0, vertexBuffer);
76             pass.Draw(vertexCount, instanceCount, firstIndex, firstInstance);
77             pass.EndPass();
78         }
79 
80         wgpu::CommandBuffer commands = encoder.Finish();
81         queue.Submit(1, &commands);
82 
83         EXPECT_PIXEL_RGBA8_EQ(bottomLeftExpected, renderPass.color, 1, 3);
84         EXPECT_PIXEL_RGBA8_EQ(topRightExpected, renderPass.color, 3, 1);
85     }
86 };
87 
88 // The basic triangle draw.
TEST_P(DrawTest,Uint32)89 TEST_P(DrawTest, Uint32) {
90     RGBA8 filled(0, 255, 0, 255);
91     RGBA8 notFilled(0, 0, 0, 0);
92 
93     // Test a draw with no indices.
94     Test(0, 0, 0, 0, notFilled, notFilled);
95     // Test a draw with only the first 3 indices (bottom left triangle)
96     Test(3, 1, 0, 0, filled, notFilled);
97     // Test a draw with only the last 3 indices (top right triangle)
98     Test(3, 1, 3, 0, notFilled, filled);
99     // Test a draw with all 6 indices (both triangles).
100     Test(6, 1, 0, 0, filled, filled);
101 }
102 
103 DAWN_INSTANTIATE_TEST(DrawTest,
104                       D3D12Backend(),
105                       MetalBackend(),
106                       OpenGLBackend(),
107                       OpenGLESBackend(),
108                       VulkanBackend());
109