From 2d86bf4c32fd5da4a4578bbde700631ac3f84d83 Mon Sep 17 00:00:00 2001 From: Ben Clayton Date: Tue, 17 Nov 2020 15:28:48 +0000 Subject: [PATCH] ast::Builder: Add more builder methods Add matrix types and missing expression overloads. Tweak the signatures of the vec constructors so they can take mixed parameter types. Change-Id: I303401e7ba80cbfddd14d9415e586e23eefcdd90 Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/32982 Commit-Queue: Ben Clayton Reviewed-by: dan sinclair --- src/ast/builder.h | 255 ++++++++++++++++----- src/writer/spirv/builder_intrinsic_test.cc | 9 +- 2 files changed, 205 insertions(+), 59 deletions(-) diff --git a/src/ast/builder.h b/src/ast/builder.h index 39ae9d2cdc..364bfd3334 100644 --- a/src/ast/builder.h +++ b/src/ast/builder.h @@ -26,6 +26,7 @@ #include "src/ast/identifier_expression.h" #include "src/ast/scalar_constructor_expression.h" #include "src/ast/sint_literal.h" +#include "src/ast/type/array_type.h" #include "src/ast/type/bool_type.h" #include "src/ast/type/f32_type.h" #include "src/ast/type/i32_type.h" @@ -61,27 +62,94 @@ class TypesBuilder { /// @return the tint AST type for the C type `T`. template - ast::type::Type* Of() const; + ast::type::Type* Of() const { + return CToAST::get(this); + } /// @return the tint AST type for a 2-element vector of the C type `T`. template - ast::type::Type* vec2() const; + ast::type::VectorType* vec2() const { + return tm_->Get(Of(), 2); + } /// @return the tint AST type for a 3-element vector of the C type `T`. template - ast::type::Type* vec3() const; + ast::type::VectorType* vec3() const { + return tm_->Get(Of(), 3); + } /// @return the tint AST type for a 4-element vector of the C type `T`. template - ast::type::Type* vec4() const; + ast::type::Type* vec4() const { + return tm_->Get(Of(), 4); + } + + /// @return the tint AST type for a 2x3 matrix of the C type `T`. + template + ast::type::MatrixType* mat2x2() const { + return tm_->Get(Of(), 2, 2); + } + + /// @return the tint AST type for a 2x3 matrix of the C type `T`. + template + ast::type::MatrixType* mat2x3() const { + return tm_->Get(Of(), 3, 2); + } + + /// @return the tint AST type for a 2x4 matrix of the C type `T`. + template + ast::type::MatrixType* mat2x4() const { + return tm_->Get(Of(), 4, 2); + } + + /// @return the tint AST type for a 3x2 matrix of the C type `T`. + template + ast::type::MatrixType* mat3x2() const { + return tm_->Get(Of(), 2, 3); + } /// @return the tint AST type for a 3x3 matrix of the C type `T`. template - ast::type::Type* mat3x3() const; + ast::type::MatrixType* mat3x3() const { + return tm_->Get(Of(), 3, 3); + } - /// @return the tint AST type for an array of type `T`. + /// @return the tint AST type for a 3x4 matrix of the C type `T`. template - ast::type::Type* arr() const; + ast::type::MatrixType* mat3x4() const { + return tm_->Get(Of(), 4, 3); + } + + /// @return the tint AST type for a 4x2 matrix of the C type `T`. + template + ast::type::MatrixType* mat4x2() const { + return tm_->Get(Of(), 2, 4); + } + + /// @return the tint AST type for a 4x3 matrix of the C type `T`. + template + ast::type::MatrixType* mat4x3() const { + return tm_->Get(Of(), 3, 4); + } + + /// @return the tint AST type for a 4x4 matrix of the C type `T`. + template + ast::type::MatrixType* mat4x4() const { + return tm_->Get(Of(), 4, 4); + } + + /// @param subtype the array element type + /// @param n the array size. 0 represents unbounded + /// @return the tint AST type for a array of size `n` of type `T` + ast::type::ArrayType* array(ast::type::Type* subtype, uint32_t n) const { + return tm_->Get(subtype, n); + } + + /// @return the tint AST type for an array of size `N` of type `T` + template + ast::type::ArrayType* array() const { + return array(Of(), N); + } private: /// CToAST is specialized for various `T` types and each specialization @@ -136,6 +204,12 @@ class Builder { return create(name); } + /// @param value the boolean value + /// @return a Scalar constructor for the given value + ast::ScalarConstructorExpression* Expr(bool value) { + return create(Literal(value)); + } + /// @param value the float value /// @return a Scalar constructor for the given value ast::ScalarConstructorExpression* Expr(f32 value) { @@ -174,6 +248,9 @@ class Builder { Append(list, std::forward(args)...); } + /// @return an empty list of expressions, + ast::ExpressionList ExprList() { return {}; } + /// @param args the list of expressions /// @return the list of expressions converted to `ast::Expression`s using /// `Expr()`, @@ -218,38 +295,132 @@ class Builder { ty.Of(), ExprList(std::forward(args)...)); } - /// @param x the first component of the vector - /// @param y the second component of the vector + /// @param type the type to construct + /// @param args the arguments for the constructor + /// @return an `ast::TypeConstructorExpression` of `type` constructed with the + /// values `args`. + template + ast::TypeConstructorExpression* Construct(ast::type::Type* type, + ARGS&&... args) { + return create( + type, ExprList(std::forward(args)...)); + } + + /// @param args the arguments for the vector constructor /// @return an `ast::TypeConstructorExpression` of a 2-element vector of type - /// `T`, constructed with the values `x` and `y`. - template - ast::TypeConstructorExpression* vec2(T&& x, T&& y) { + /// `T`, constructed with the values `args`. + template + ast::TypeConstructorExpression* vec2(ARGS&&... args) { return create( - ty.vec2(), ExprList(std::move(x), std::move(y))); + ty.vec2(), ExprList(std::forward(args)...)); } - /// @param x the first component of the vector - /// @param y the second component of the vector - /// @param z the third component of the vector + /// @param args the arguments for the vector constructor /// @return an `ast::TypeConstructorExpression` of a 3-element vector of type - /// `T`, constructed with the values `x`, `y` and `z`. - template - ast::TypeConstructorExpression* vec3(T&& x, T&& y, T&& z) { + /// `T`, constructed with the values `args`. + template + ast::TypeConstructorExpression* vec3(ARGS&&... args) { return create( - ty.vec3(), ExprList(std::move(x), std::move(y), std::move(z))); + ty.vec3(), ExprList(std::forward(args)...)); } - /// @param x the first component of the vector - /// @param y the second component of the vector - /// @param z the third component of the vector - /// @param w the fourth component of the vector + /// @param args the arguments for the vector constructor /// @return an `ast::TypeConstructorExpression` of a 4-element vector of type - /// `T`, constructed with the values `x`, `y`, `z` and `w`. - template - ast::TypeConstructorExpression* vec4(T&& x, T&& y, T&& z, T&& w) { + /// `T`, constructed with the values `args`. + template + ast::TypeConstructorExpression* vec4(ARGS&&... args) { return create( - ty.vec4(), - ExprList(std::move(x), std::move(y), std::move(z), std::move(w))); + ty.vec4(), ExprList(std::forward(args)...)); + } + + /// @param args the arguments for the matrix constructor + /// @return an `ast::TypeConstructorExpression` of a 2x2 matrix of type + /// `T`, constructed with the values `args`. + template + ast::TypeConstructorExpression* mat2x2(ARGS&&... args) { + return create( + ty.mat2x2(), ExprList(std::forward(args)...)); + } + + /// @param args the arguments for the matrix constructor + /// @return an `ast::TypeConstructorExpression` of a 2x3 matrix of type + /// `T`, constructed with the values `args`. + template + ast::TypeConstructorExpression* mat2x3(ARGS&&... args) { + return create( + ty.mat2x3(), ExprList(std::forward(args)...)); + } + + /// @param args the arguments for the matrix constructor + /// @return an `ast::TypeConstructorExpression` of a 2x4 matrix of type + /// `T`, constructed with the values `args`. + template + ast::TypeConstructorExpression* mat2x4(ARGS&&... args) { + return create( + ty.mat2x4(), ExprList(std::forward(args)...)); + } + + /// @param args the arguments for the matrix constructor + /// @return an `ast::TypeConstructorExpression` of a 3x2 matrix of type + /// `T`, constructed with the values `args`. + template + ast::TypeConstructorExpression* mat3x2(ARGS&&... args) { + return create( + ty.mat3x2(), ExprList(std::forward(args)...)); + } + + /// @param args the arguments for the matrix constructor + /// @return an `ast::TypeConstructorExpression` of a 3x3 matrix of type + /// `T`, constructed with the values `args`. + template + ast::TypeConstructorExpression* mat3x3(ARGS&&... args) { + return create( + ty.mat3x3(), ExprList(std::forward(args)...)); + } + + /// @param args the arguments for the matrix constructor + /// @return an `ast::TypeConstructorExpression` of a 3x4 matrix of type + /// `T`, constructed with the values `args`. + template + ast::TypeConstructorExpression* mat3x4(ARGS&&... args) { + return create( + ty.mat3x4(), ExprList(std::forward(args)...)); + } + + /// @param args the arguments for the matrix constructor + /// @return an `ast::TypeConstructorExpression` of a 4x2 matrix of type + /// `T`, constructed with the values `args`. + template + ast::TypeConstructorExpression* mat4x2(ARGS&&... args) { + return create( + ty.mat4x2(), ExprList(std::forward(args)...)); + } + + /// @param args the arguments for the matrix constructor + /// @return an `ast::TypeConstructorExpression` of a 4x3 matrix of type + /// `T`, constructed with the values `args`. + template + ast::TypeConstructorExpression* mat4x3(ARGS&&... args) { + return create( + ty.mat4x3(), ExprList(std::forward(args)...)); + } + + /// @param args the arguments for the matrix constructor + /// @return an `ast::TypeConstructorExpression` of a 4x4 matrix of type + /// `T`, constructed with the values `args`. + template + ast::TypeConstructorExpression* mat4x4(ARGS&&... args) { + return create( + ty.mat4x4(), ExprList(std::forward(args)...)); + } + + /// @param args the arguments for the array constructor + /// @return an `ast::TypeConstructorExpression` of a 4x4 matrix of type + /// `T`, constructed with the values `args`. + template + ast::TypeConstructorExpression* array(ARGS&&... args) { + return create( + ty.array(), ExprList(std::forward(args)...)); } /// @param name the variable name @@ -289,32 +460,6 @@ class Builder { virtual void OnVariableBuilt(ast::Variable*) {} }; -template -ast::type::Type* TypesBuilder::Of() const { - return CToAST::get(this); -} - -template -ast::type::Type* TypesBuilder::vec2() const { - return tm_->Get(Of(), 2); -} -template -ast::type::Type* TypesBuilder::vec3() const { - return tm_->Get(Of(), 3); -} -template -ast::type::Type* TypesBuilder::vec4() const { - return tm_->Get(Of(), 4); -} -template -ast::type::Type* TypesBuilder::mat3x3() const { - return tm_->Get(Of(), 3, 3); -} -template -ast::type::Type* TypesBuilder::arr() const { - return tm_->Get(Of()); -} - /// BuilderWithContext is a `Builder` that constructs and owns its `Context`. class BuilderWithContext : public Builder { public: diff --git a/src/writer/spirv/builder_intrinsic_test.cc b/src/writer/spirv/builder_intrinsic_test.cc index 631e604943..de8cecfa56 100644 --- a/src/writer/spirv/builder_intrinsic_test.cc +++ b/src/writer/spirv/builder_intrinsic_test.cc @@ -1680,7 +1680,7 @@ OpFunctionEnd TEST_F(IntrinsicBuilderTest, Call_ArrayLength) { ast::StructMemberDecorationList decos; ast::StructMemberList members; - members.push_back(create("a", ty.arr(), decos)); + members.push_back(create("a", ty.array(), decos)); auto* s = create(members); ast::type::StructType s_type("my_struct", s); @@ -1719,7 +1719,7 @@ TEST_F(IntrinsicBuilderTest, Call_ArrayLength_OtherMembersInStruct) { ast::StructMemberDecorationList decos; ast::StructMemberList members; members.push_back(create("z", ty.f32, decos)); - members.push_back(create("a", ty.arr(), decos)); + members.push_back(create("a", ty.array(), decos)); auto* s = create(members); ast::type::StructType s_type("my_struct", s); @@ -1755,12 +1755,13 @@ TEST_F(IntrinsicBuilderTest, Call_ArrayLength_OtherMembersInStruct) { // TODO(dsinclair): https://bugs.chromium.org/p/tint/issues/detail?id=266 TEST_F(IntrinsicBuilderTest, DISABLED_Call_ArrayLength_Ptr) { - ast::type::PointerType ptr(ty.arr(), ast::StorageClass::kStorageBuffer); + ast::type::PointerType ptr(ty.array(), + ast::StorageClass::kStorageBuffer); ast::StructMemberDecorationList decos; ast::StructMemberList members; members.push_back(create("z", ty.f32, decos)); - members.push_back(create("a", ty.arr(), decos)); + members.push_back(create("a", ty.array(), decos)); auto* s = create(members); ast::type::StructType s_type("my_struct", s);