blob: 5c6044a1b36c804ffe2355361cc64025d6736efe [file]
// Copyright 2026 The Dawn & Tint Authors
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this
// list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// 3. Neither the name of the copyright holder nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <array>
#include "src/dawn/tests/DawnTest.h"
#include "src/dawn/utils/WGPUHelpers.h"
namespace dawn {
namespace {
class ShaderMatrixLoweringTest : public DawnTest {};
// Verifies matrix multiplication of two transposed matrices, including non-square operands
// (crbug.com/591811902).
TEST_P(ShaderMatrixLoweringTest, TransposeTimesTranspose) {
const char* shader = R"(
/* A = (1 4) B = (10 30)
(2 5) (20 40)
(3 6)
Transpose(B) = (10 20) Transpose(A) = (1 2 3)
(30 40) (4 5 6)
Transpose(B)*Transpose(A) = ( 10*1+20*4 10*2+20*5 10*3+20*6 )
( 30*1+40*4 30*2+40*5 30*3+40*6 )
*/
@group(0) @binding(0) var<storage> A: mat2x3f;
@group(0) @binding(1) var<storage> B: mat2x2f;
@group(0) @binding(2) var<storage, read_write> Result: mat3x2f;
@compute @workgroup_size(1)
fn main() {
Result = transpose(B) * transpose(A);
}
)";
wgpu::ShaderModule csModule = utils::CreateShaderModule(device, shader);
wgpu::ComputePipelineDescriptor pipelineDesc = {};
pipelineDesc.compute.module = csModule;
pipelineDesc.compute.entryPoint = "main";
wgpu::ComputePipeline pipeline = device.CreateComputePipeline(&pipelineDesc);
// A = (1 4)
// (2 5)
// (3 6)
// mat2x3f is stored in column-major order with 16-byte (4-float) column stride.
const std::array<float, 8> kMatrixA = {
1.0f, 2.0f, 3.0f, 0.0f, // column 0 (+ padding)
4.0f, 5.0f, 6.0f, 0.0f, // column 1 (+ padding)
};
wgpu::Buffer bufferA = utils::CreateBufferFromData(device, kMatrixA.data(), sizeof(kMatrixA),
wgpu::BufferUsage::Storage);
// B = (10 30)
// (20 40)
// mat2x2f is stored in column-major order with 8-byte (2-float) column stride.
const std::array<float, 4> kMatrixB = {
10.0f,
20.0f, // column 0
30.0f,
40.0f, // column 1
};
wgpu::Buffer bufferB = utils::CreateBufferFromData(device, kMatrixB.data(), sizeof(kMatrixB),
wgpu::BufferUsage::Storage);
const std::array<float, 6> kZeroResult = {};
wgpu::Buffer bufferResult =
utils::CreateBufferFromData(device, kZeroResult.data(), sizeof(kZeroResult),
wgpu::BufferUsage::Storage | wgpu::BufferUsage::CopySrc);
wgpu::BindGroup bindGroup = utils::MakeBindGroup(
device, pipeline.GetBindGroupLayout(0), {{0, bufferA}, {1, bufferB}, {2, bufferResult}});
wgpu::CommandEncoder encoder = device.CreateCommandEncoder();
wgpu::ComputePassEncoder pass = encoder.BeginComputePass();
pass.SetPipeline(pipeline);
pass.SetBindGroup(0, bindGroup);
pass.DispatchWorkgroups(1);
pass.End();
wgpu::CommandBuffer commands = encoder.Finish();
queue.Submit(1, &commands);
// Result is mat3x2f (3 columns of vec2f, 8-byte column stride):
// Transpose(B)*Transpose(A) = ( 10*1+20*4 10*2+20*5 10*3+20*6 )
// ( 30*1+40*4 30*2+40*5 30*3+40*6 )
const std::array<float, 6> kExpected = {
// Column 0: (10*1+20*4, 30*1+40*4) = (90, 190)
10.0f * 1.0f + 20.0f * 4.0f,
30.0f * 1.0f + 40.0f * 4.0f,
// Column 1: (10*2+20*5, 30*2+40*5) = (120, 260)
10.0f * 2.0f + 20.0f * 5.0f,
30.0f * 2.0f + 40.0f * 5.0f,
// Column 2: (10*3+20*6, 30*3+40*6) = (150, 330)
10.0f * 3.0f + 20.0f * 6.0f,
30.0f * 3.0f + 40.0f * 6.0f,
};
EXPECT_BUFFER_FLOAT_RANGE_EQ(kExpected.data(), bufferResult, 0, kExpected.size());
}
DAWN_INSTANTIATE_TEST(ShaderMatrixLoweringTest,
D3D11Backend(),
D3D12Backend(),
MetalBackend(),
OpenGLBackend(),
OpenGLESBackend(),
VulkanBackend(),
VulkanBackend({"vulkan_disguise_transpose_matrix_multiply"}),
WebGPUBackend());
} // anonymous namespace
} // namespace dawn