Skip to content

Commit

Permalink
[WGSL] Add support for matrix subtraction
Browse files Browse the repository at this point in the history
https://bugs.webkit.org/show_bug.cgi?id=264406
rdar://118117794

Reviewed by Mike Wyrzykowski.

The TypeDeclarations file was missing the subtraction overload for matrices. Add
the missing overload and implement constant matrix subtraction as well.

* Source/WebGPU/WGSL/ConstantFunctions.h:
(WGSL::CONSTANT_FUNCTION):
* Source/WebGPU/WGSL/TypeDeclarations.rb:
* Source/WebGPU/WGSL/tests/valid/overload.wgsl:

Canonical link: https://commits.webkit.org/270428@main
  • Loading branch information
tadeuzagallo committed Nov 9, 2023
1 parent e5c11b8 commit 46bbee9
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 0 deletions.
12 changes: 12 additions & 0 deletions Source/WebGPU/WGSL/ConstantFunctions.h
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,18 @@ CONSTANT_FUNCTION(Minus)
return -arg;
});
}

if (auto* left = std::get_if<ConstantMatrix>(&arguments[0])) {
auto& right = std::get<ConstantMatrix>(arguments[1]);
auto* elementType = std::get<Types::Matrix>(*resultType).element;
ASSERT(left->columns == right.columns);
ASSERT(left->rows == right.rows);
ConstantMatrix result(left->columns, left->rows);
for (unsigned i = 0; i < result.elements.size(); ++i)
CALL_MOVE(result.elements[i], Minus, elementType, { left->elements[i], right.elements[i] });
return { { result } };
}

return constantBinaryOperation<Constraints::Number>(arguments, [&](auto left, auto right) {
return left - right;
});
Expand Down
1 change: 1 addition & 0 deletions Source/WebGPU/WGSL/TypeDeclarations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
[T < Number, N].(vec[N][T], T) => vec[N][T],
[T < Number, N].(T, vec[N][T]) => vec[N][T],
[T < Number, N].(vec[N][T], vec[N][T]) => vec[N][T],
[T < Float, C, R].(mat[C,R][T], mat[C,R][T]) => mat[C,R][T],
}

operator :*, {
Expand Down
1 change: 1 addition & 0 deletions Source/WebGPU/WGSL/tests/valid/overload.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ fn testBinaryMinus() {
_ = vec2(1, 1) - 1;
_ = 1 - vec2(1, 1);
_ = vec2(1, 1) - vec2(1, 1);
_ = mat2x2(2, 2, 2, 2) - mat2x2(1, 1, 1, 1);
}

fn testComparison() {
Expand Down

0 comments on commit 46bbee9

Please sign in to comment.