// Copyright 2018 The Dawn Authors // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. #include "tests/DawnTest.h" #include "utils/ComboRenderPipelineDescriptor.h" #include "utils/WGPUHelpers.h" constexpr uint32_t kRTSize = 4; class DrawIndexedIndirectTest : public DawnTest { protected: void TestSetUp() override { DawnTest::TestSetUp(); renderPass = utils::CreateBasicRenderPass(device, kRTSize, kRTSize); wgpu::ShaderModule vsModule = utils::CreateShaderModule(device, utils::SingleShaderStage::Vertex, R"( #version 450 layout(location = 0) in vec4 pos; void main() { gl_Position = pos; })"); wgpu::ShaderModule fsModule = utils::CreateShaderModule(device, utils::SingleShaderStage::Fragment, R"( #version 450 layout(location = 0) out vec4 fragColor; void main() { fragColor = vec4(0.0, 1.0, 0.0, 1.0); })"); utils::ComboRenderPipelineDescriptor descriptor(device); descriptor.vertexStage.module = vsModule; descriptor.cFragmentStage.module = fsModule; descriptor.primitiveTopology = wgpu::PrimitiveTopology::TriangleStrip; descriptor.cVertexInput.bufferCount = 1; descriptor.cVertexInput.cBuffers[0].stride = 4 * sizeof(float); descriptor.cVertexInput.cBuffers[0].attributeCount = 1; descriptor.cVertexInput.cAttributes[0].format = wgpu::VertexFormat::Float4; descriptor.cColorStates[0].format = renderPass.colorFormat; pipeline = device.CreateRenderPipeline(&descriptor); vertexBuffer = utils::CreateBufferFromData( device, wgpu::BufferUsage::Vertex, {// First quad: the first 3 vertices represent the bottom left triangle -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, 0.0f, 1.0f, // Second quad: the first 3 vertices represent the top right triangle -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, 0.0f, 1.0f}); indexBuffer = utils::CreateBufferFromData( device, wgpu::BufferUsage::Index, {0, 1, 2, 0, 3, 1, // The indices below are added to test negatve baseVertex 0 + 4, 1 + 4, 2 + 4, 0 + 4, 3 + 4, 1 + 4}); } utils::BasicRenderPass renderPass; wgpu::RenderPipeline pipeline; wgpu::Buffer vertexBuffer; wgpu::Buffer indexBuffer; void Test(std::initializer_list bufferList, uint64_t indexOffset, uint64_t indirectOffset, RGBA8 bottomLeftExpected, RGBA8 topRightExpected) { wgpu::Buffer indirectBuffer = utils::CreateBufferFromData(device, wgpu::BufferUsage::Indirect, bufferList); wgpu::CommandEncoder encoder = device.CreateCommandEncoder(); { wgpu::RenderPassEncoder pass = encoder.BeginRenderPass(&renderPass.renderPassInfo); pass.SetPipeline(pipeline); pass.SetVertexBuffer(0, vertexBuffer); pass.SetIndexBuffer(indexBuffer, indexOffset); pass.DrawIndexedIndirect(indirectBuffer, indirectOffset); pass.EndPass(); } wgpu::CommandBuffer commands = encoder.Finish(); queue.Submit(1, &commands); EXPECT_PIXEL_RGBA8_EQ(bottomLeftExpected, renderPass.color, 1, 3); EXPECT_PIXEL_RGBA8_EQ(topRightExpected, renderPass.color, 3, 1); } }; // The most basic DrawIndexed triangle draw. TEST_P(DrawIndexedIndirectTest, Uint32) { RGBA8 filled(0, 255, 0, 255); RGBA8 notFilled(0, 0, 0, 0); // Test a draw with no indices. Test({0, 0, 0, 0, 0}, 0, 0, notFilled, notFilled); // Test a draw with only the first 3 indices of the first quad (bottom left triangle) Test({3, 1, 0, 0, 0}, 0, 0, filled, notFilled); // Test a draw with only the last 3 indices of the first quad (top right triangle) Test({3, 1, 3, 0, 0}, 0, 0, notFilled, filled); // Test a draw with all 6 indices (both triangles). Test({6, 1, 0, 0, 0}, 0, 0, filled, filled); } // Test the parameter 'baseVertex' of DrawIndexed() works. TEST_P(DrawIndexedIndirectTest, BaseVertex) { // TODO(crbug.com/dawn/161): add workaround for OpenGL index buffer offset (could be compute // shader that adds it to the draw calls) DAWN_SKIP_TEST_IF(IsOpenGL()); RGBA8 filled(0, 255, 0, 255); RGBA8 notFilled(0, 0, 0, 0); // Test a draw with only the first 3 indices of the second quad (top right triangle) Test({3, 1, 0, 4, 0}, 0, 0, notFilled, filled); // Test a draw with only the last 3 indices of the second quad (bottom left triangle) Test({3, 1, 3, 4, 0}, 0, 0, filled, notFilled); const int negFour = -4; uint32_t unsignedNegFour; std::memcpy(&unsignedNegFour, &negFour, sizeof(int)); // Test negative baseVertex // Test a draw with only the first 3 indices of the first quad (bottom left triangle) Test({3, 1, 0, unsignedNegFour, 0}, 6 * sizeof(uint32_t), 0, filled, notFilled); // Test a draw with only the last 3 indices of the first quad (top right triangle) Test({3, 1, 3, unsignedNegFour, 0}, 6 * sizeof(uint32_t), 0, notFilled, filled); } TEST_P(DrawIndexedIndirectTest, IndirectOffset) { RGBA8 filled(0, 255, 0, 255); RGBA8 notFilled(0, 0, 0, 0); // Test an offset draw call, with indirect buffer containing 2 calls: // 1) first 3 indices of the second quad (top right triangle) // 2) last 3 indices of the second quad // Test #1 (no offset) Test({3, 1, 0, 4, 0, 3, 1, 3, 4, 0}, 0, 0, notFilled, filled); // Offset to draw #2 Test({3, 1, 0, 4, 0, 3, 1, 3, 4, 0}, 0, 5 * sizeof(uint32_t), filled, notFilled); } DAWN_INSTANTIATE_TEST(DrawIndexedIndirectTest, D3D12Backend, MetalBackend, OpenGLBackend, VulkanBackend);