1 // Copyright 2018 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/DawnHelpers.h"
19
20 constexpr uint32_t kRTSize = 4;
21
22 class DrawIndexedIndirectTest : public DawnTest {
23 protected:
SetUp()24 void SetUp() override {
25 DawnTest::SetUp();
26
27 renderPass = utils::CreateBasicRenderPass(device, kRTSize, kRTSize);
28
29 dawn::ShaderModule vsModule =
30 utils::CreateShaderModule(device, utils::ShaderStage::Vertex, R"(
31 #version 450
32 layout(location = 0) in vec4 pos;
33 void main() {
34 gl_Position = pos;
35 })");
36
37 dawn::ShaderModule fsModule =
38 utils::CreateShaderModule(device, utils::ShaderStage::Fragment, R"(
39 #version 450
40 layout(location = 0) out vec4 fragColor;
41 void main() {
42 fragColor = vec4(0.0, 1.0, 0.0, 1.0);
43 })");
44
45 utils::ComboRenderPipelineDescriptor descriptor(device);
46 descriptor.cVertexStage.module = vsModule;
47 descriptor.cFragmentStage.module = fsModule;
48 descriptor.primitiveTopology = dawn::PrimitiveTopology::TriangleStrip;
49 descriptor.cVertexInput.bufferCount = 1;
50 descriptor.cVertexInput.cBuffers[0].stride = 4 * sizeof(float);
51 descriptor.cVertexInput.cBuffers[0].attributeCount = 1;
52 descriptor.cVertexInput.cAttributes[0].format = dawn::VertexFormat::Float4;
53 descriptor.cColorStates[0]->format = renderPass.colorFormat;
54
55 pipeline = device.CreateRenderPipeline(&descriptor);
56
57 vertexBuffer = utils::CreateBufferFromData<float>(
58 device, dawn::BufferUsageBit::Vertex,
59 {// First quad: the first 3 vertices represent the bottom left triangle
60 -1.0f, -1.0f, 0.0f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f, -1.0f, 1.0f, 0.0f, 1.0f, 1.0f, -1.0f,
61 0.0f, 1.0f,
62
63 // Second quad: the first 3 vertices represent the top right triangle
64 -1.0f, -1.0f, 0.0f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f, 1.0f, -1.0f, 0.0f, 1.0f, -1.0f, 1.0f,
65 0.0f, 1.0f});
66 indexBuffer = utils::CreateBufferFromData<uint32_t>(
67 device, dawn::BufferUsageBit::Index,
68 {0, 1, 2, 0, 3, 1,
69 // The indices below are added to test negatve baseVertex
70 0 + 4, 1 + 4, 2 + 4, 0 + 4, 3 + 4, 1 + 4});
71 }
72
73 utils::BasicRenderPass renderPass;
74 dawn::RenderPipeline pipeline;
75 dawn::Buffer vertexBuffer;
76 dawn::Buffer indexBuffer;
77
Test(std::initializer_list<uint32_t> bufferList,uint64_t indexOffset,uint64_t indirectOffset,RGBA8 bottomLeftExpected,RGBA8 topRightExpected)78 void Test(std::initializer_list<uint32_t> bufferList,
79 uint64_t indexOffset,
80 uint64_t indirectOffset,
81 RGBA8 bottomLeftExpected,
82 RGBA8 topRightExpected) {
83 dawn::Buffer indirectBuffer = utils::CreateBufferFromData<uint32_t>(
84 device, dawn::BufferUsageBit::Indirect, bufferList);
85
86 uint64_t zeroOffset = 0;
87 dawn::CommandEncoder encoder = device.CreateCommandEncoder();
88 {
89 dawn::RenderPassEncoder pass = encoder.BeginRenderPass(&renderPass.renderPassInfo);
90 pass.SetPipeline(pipeline);
91 pass.SetVertexBuffers(0, 1, &vertexBuffer, &zeroOffset);
92 pass.SetIndexBuffer(indexBuffer, indexOffset);
93 pass.DrawIndexedIndirect(indirectBuffer, indirectOffset);
94 pass.EndPass();
95 }
96
97 dawn::CommandBuffer commands = encoder.Finish();
98 queue.Submit(1, &commands);
99
100 EXPECT_PIXEL_RGBA8_EQ(bottomLeftExpected, renderPass.color, 1, 3);
101 EXPECT_PIXEL_RGBA8_EQ(topRightExpected, renderPass.color, 3, 1);
102 }
103 };
104
105 // The most basic DrawIndexed triangle draw.
TEST_P(DrawIndexedIndirectTest,Uint32)106 TEST_P(DrawIndexedIndirectTest, Uint32) {
107 RGBA8 filled(0, 255, 0, 255);
108 RGBA8 notFilled(0, 0, 0, 0);
109
110 // Test a draw with no indices.
111 Test({0, 0, 0, 0, 0}, 0, 0, notFilled, notFilled);
112
113 // Test a draw with only the first 3 indices of the first quad (bottom left triangle)
114 Test({3, 1, 0, 0, 0}, 0, 0, filled, notFilled);
115
116 // Test a draw with only the last 3 indices of the first quad (top right triangle)
117 Test({3, 1, 3, 0, 0}, 0, 0, notFilled, filled);
118
119 // Test a draw with all 6 indices (both triangles).
120 Test({6, 1, 0, 0, 0}, 0, 0, filled, filled);
121 }
122
123 // Test the parameter 'baseVertex' of DrawIndexed() works.
TEST_P(DrawIndexedIndirectTest,BaseVertex)124 TEST_P(DrawIndexedIndirectTest, BaseVertex) {
125 // TODO(crbug.com/dawn/161): add workaround for OpenGL index buffer offset (could be compute
126 // shader that adds it to the draw calls)
127 DAWN_SKIP_TEST_IF(IsOpenGL());
128
129 RGBA8 filled(0, 255, 0, 255);
130 RGBA8 notFilled(0, 0, 0, 0);
131
132 // Test a draw with only the first 3 indices of the second quad (top right triangle)
133 Test({3, 1, 0, 4, 0}, 0, 0, notFilled, filled);
134
135 // Test a draw with only the last 3 indices of the second quad (bottom left triangle)
136 Test({3, 1, 3, 4, 0}, 0, 0, filled, notFilled);
137
138 const int negFour = -4;
139 uint32_t unsignedNegFour;
140 std::memcpy(&unsignedNegFour, &negFour, sizeof(int));
141
142 // Test negative baseVertex
143 // Test a draw with only the first 3 indices of the first quad (bottom left triangle)
144 Test({3, 1, 0, unsignedNegFour, 0}, 6 * sizeof(uint32_t), 0, filled, notFilled);
145
146 // Test a draw with only the last 3 indices of the first quad (top right triangle)
147 Test({3, 1, 3, unsignedNegFour, 0}, 6 * sizeof(uint32_t), 0, notFilled, filled);
148 }
149
TEST_P(DrawIndexedIndirectTest,IndirectOffset)150 TEST_P(DrawIndexedIndirectTest, IndirectOffset) {
151 RGBA8 filled(0, 255, 0, 255);
152 RGBA8 notFilled(0, 0, 0, 0);
153
154 // Test an offset draw call, with indirect buffer containing 2 calls:
155 // 1) first 3 indices of the second quad (top right triangle)
156 // 2) last 3 indices of the second quad
157
158 // Test #1 (no offset)
159 Test({3, 1, 0, 4, 0, 3, 1, 3, 4, 0}, 0, 0, notFilled, filled);
160
161 // Offset to draw #2
162 Test({3, 1, 0, 4, 0, 3, 1, 3, 4, 0}, 0, 5 * sizeof(uint32_t), filled, notFilled);
163 }
164
165 DAWN_INSTANTIATE_TEST(DrawIndexedIndirectTest,
166 D3D12Backend,
167 MetalBackend,
168 OpenGLBackend,
169 VulkanBackend);
170