Move tint::ast::type to tint::type

Despite `tint::ast::type::Type` being in the AST namespace, these classes are clearly not AST nodes:
* They don't derive from ast::Node
* They're deduplicated by the type manager
* None of the types have an Source - they have no lexical declaration point
* The fact we have `ast::Struct` and `ast::type::Struct` clearly demonstrates what is an AST node, and what is a type.
* We have code scattered in the codebase (TypeDeterminer, writers, etc) that create new types after parsing - so clearly not part of the original syntax tree.

Types in tint are closer to being semantic info, but due to the parse-time generation of types, and tight dependency of ast::Nodes to types, I'd be reluctant to class these as semantic info. Instead, put these into a separate root level `tint::type` namespace and `src/tint` directory.

The fact that types exist in the ast::Module has already caused bugs (https://dawn-review.googlesource.com/c/tint/+/37261). This is a first step in separating out types from the ast::Module.

Bug: tint:390
Change-Id: I8349bbbd1b19597b8e6d51d5cda0890de46ecaec
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/38002
Commit-Queue: Ben Clayton <bclayton@google.com>
Reviewed-by: dan sinclair <dsinclair@chromium.org>
This commit is contained in:
Ben Clayton
2021-01-21 15:42:10 +00:00
committed by Commit Bot service account
parent 587f387fd9
commit 207b5e2de1
227 changed files with 3647 additions and 3869 deletions

View File

@@ -0,0 +1,66 @@
// Copyright 2020 The Tint 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 "src/type/access_control_type.h"
#include <assert.h>
#include "src/ast/clone_context.h"
#include "src/ast/module.h"
TINT_INSTANTIATE_CLASS_ID(tint::type::AccessControl);
namespace tint {
namespace type {
AccessControl::AccessControl(ast::AccessControl access, Type* subtype)
: access_(access), subtype_(subtype) {
assert(subtype_);
assert(!subtype_->Is<AccessControl>());
}
AccessControl::AccessControl(AccessControl&&) = default;
AccessControl::~AccessControl() = default;
std::string AccessControl::type_name() const {
std::string name = "__access_control_";
switch (access_) {
case ast::AccessControl::kReadOnly:
name += "read_only";
break;
case ast::AccessControl::kWriteOnly:
name += "write_only";
break;
case ast::AccessControl::kReadWrite:
name += "read_write";
break;
}
return name + subtype_->type_name();
}
uint64_t AccessControl::MinBufferBindingSize(MemoryLayout mem_layout) const {
return subtype_->MinBufferBindingSize(mem_layout);
}
uint64_t AccessControl::BaseAlignment(MemoryLayout mem_layout) const {
return subtype_->BaseAlignment(mem_layout);
}
AccessControl* AccessControl::Clone(CloneContext* ctx) const {
return ctx->mod->create<AccessControl>(access_, ctx->Clone(subtype_));
}
} // namespace type
} // namespace tint

View File

@@ -0,0 +1,75 @@
// Copyright 2020 The Tint 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.
#ifndef SRC_TYPE_ACCESS_CONTROL_TYPE_H_
#define SRC_TYPE_ACCESS_CONTROL_TYPE_H_
#include <string>
#include "src/ast/access_control.h"
#include "src/type/type.h"
namespace tint {
namespace type {
/// An access control type. Holds an access setting and pointer to another type.
class AccessControl : public Castable<AccessControl, Type> {
public:
/// Constructor
/// @param access the access control setting
/// @param subtype the access controlled type
AccessControl(ast::AccessControl access, Type* subtype);
/// Move constructor
AccessControl(AccessControl&&);
~AccessControl() override;
/// @returns true if the access control is read only
bool IsReadOnly() const { return access_ == ast::AccessControl::kReadOnly; }
/// @returns true if the access control is write only
bool IsWriteOnly() const { return access_ == ast::AccessControl::kWriteOnly; }
/// @returns true if the access control is read/write
bool IsReadWrite() const { return access_ == ast::AccessControl::kReadWrite; }
/// @returns the access control value
ast::AccessControl access_control() const { return access_; }
/// @returns the subtype type
Type* type() const { return subtype_; }
/// @returns the name for this type
std::string type_name() const override;
/// @param mem_layout type of memory layout to use in calculation.
/// @returns minimum size required for this type, in bytes.
/// 0 for non-host shareable types.
uint64_t MinBufferBindingSize(MemoryLayout mem_layout) const override;
/// @param mem_layout type of memory layout to use in calculation.
/// @returns base alignment for the type, in bytes.
/// 0 for non-host shareable types.
uint64_t BaseAlignment(MemoryLayout mem_layout) const override;
/// Clones this type and all transitive types using the `CloneContext` `ctx`.
/// @param ctx the clone context
/// @return the newly cloned type
AccessControl* Clone(CloneContext* ctx) const override;
private:
ast::AccessControl const access_;
Type* const subtype_;
};
} // namespace type
} // namespace tint
#endif // SRC_TYPE_ACCESS_CONTROL_TYPE_H_

View File

@@ -0,0 +1,170 @@
// Copyright 2020 The Tint 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 "src/type/access_control_type.h"
#include <memory>
#include <utility>
#include "src/ast/storage_class.h"
#include "src/ast/stride_decoration.h"
#include "src/ast/struct_member.h"
#include "src/ast/struct_member_decoration.h"
#include "src/ast/struct_member_offset_decoration.h"
#include "src/type/array_type.h"
#include "src/type/bool_type.h"
#include "src/type/f32_type.h"
#include "src/type/i32_type.h"
#include "src/type/matrix_type.h"
#include "src/type/pointer_type.h"
#include "src/type/struct_type.h"
#include "src/type/test_helper.h"
#include "src/type/texture_type.h"
#include "src/type/u32_type.h"
#include "src/type/vector_type.h"
namespace tint {
namespace type {
namespace {
using AccessControlTest = TestHelper;
TEST_F(AccessControlTest, Create) {
U32 u32;
AccessControl a{ast::AccessControl::kReadWrite, &u32};
EXPECT_TRUE(a.IsReadWrite());
EXPECT_EQ(a.type(), &u32);
}
TEST_F(AccessControlTest, Is) {
I32 i32;
AccessControl at{ast::AccessControl::kReadOnly, &i32};
Type* ty = &at;
EXPECT_TRUE(ty->Is<AccessControl>());
EXPECT_FALSE(ty->Is<Alias>());
EXPECT_FALSE(ty->Is<Array>());
EXPECT_FALSE(ty->Is<Bool>());
EXPECT_FALSE(ty->Is<F32>());
EXPECT_FALSE(ty->Is<I32>());
EXPECT_FALSE(ty->Is<Matrix>());
EXPECT_FALSE(ty->Is<Pointer>());
EXPECT_FALSE(ty->Is<Sampler>());
EXPECT_FALSE(ty->Is<Struct>());
EXPECT_FALSE(ty->Is<Texture>());
EXPECT_FALSE(ty->Is<U32>());
EXPECT_FALSE(ty->Is<Vector>());
}
TEST_F(AccessControlTest, AccessRead) {
I32 i32;
AccessControl at{ast::AccessControl::kReadOnly, &i32};
EXPECT_TRUE(at.IsReadOnly());
EXPECT_FALSE(at.IsWriteOnly());
EXPECT_FALSE(at.IsReadWrite());
EXPECT_EQ(at.type_name(), "__access_control_read_only__i32");
}
TEST_F(AccessControlTest, AccessWrite) {
I32 i32;
AccessControl at{ast::AccessControl::kWriteOnly, &i32};
EXPECT_FALSE(at.IsReadOnly());
EXPECT_TRUE(at.IsWriteOnly());
EXPECT_FALSE(at.IsReadWrite());
EXPECT_EQ(at.type_name(), "__access_control_write_only__i32");
}
TEST_F(AccessControlTest, AccessReadWrite) {
I32 i32;
AccessControl at{ast::AccessControl::kReadWrite, &i32};
EXPECT_FALSE(at.IsReadOnly());
EXPECT_FALSE(at.IsWriteOnly());
EXPECT_TRUE(at.IsReadWrite());
EXPECT_EQ(at.type_name(), "__access_control_read_write__i32");
}
TEST_F(AccessControlTest, MinBufferBindingSizeU32) {
U32 u32;
AccessControl at{ast::AccessControl::kReadOnly, &u32};
EXPECT_EQ(4u, at.MinBufferBindingSize(MemoryLayout::kUniformBuffer));
}
TEST_F(AccessControlTest, MinBufferBindingSizeArray) {
U32 u32;
Array array(&u32, 4,
ast::ArrayDecorationList{create<ast::StrideDecoration>(4)});
AccessControl at{ast::AccessControl::kReadOnly, &array};
EXPECT_EQ(16u, at.MinBufferBindingSize(MemoryLayout::kUniformBuffer));
}
TEST_F(AccessControlTest, MinBufferBindingSizeRuntimeArray) {
U32 u32;
Array array(&u32, 0,
ast::ArrayDecorationList{create<ast::StrideDecoration>(4)});
AccessControl at{ast::AccessControl::kReadOnly, &array};
EXPECT_EQ(4u, at.MinBufferBindingSize(MemoryLayout::kUniformBuffer));
}
TEST_F(AccessControlTest, MinBufferBindingSizeStruct) {
auto* str = create<ast::Struct>(
ast::StructMemberList{Member("foo", ty.u32, {MemberOffset(0)}),
Member("bar", ty.u32, {MemberOffset(4)})},
ast::StructDecorationList{});
auto* struct_type = ty.struct_("struct_type", str);
AccessControl at{ast::AccessControl::kReadOnly, struct_type};
EXPECT_EQ(16u, at.MinBufferBindingSize(MemoryLayout::kUniformBuffer));
EXPECT_EQ(8u, at.MinBufferBindingSize(MemoryLayout::kStorageBuffer));
}
TEST_F(AccessControlTest, BaseAlignmentU32) {
U32 u32;
AccessControl at{ast::AccessControl::kReadOnly, &u32};
EXPECT_EQ(4u, at.BaseAlignment(MemoryLayout::kUniformBuffer));
}
TEST_F(AccessControlTest, BaseAlignmentArray) {
U32 u32;
Array array(&u32, 4,
ast::ArrayDecorationList{create<ast::StrideDecoration>(4)});
AccessControl at{ast::AccessControl::kReadOnly, &array};
EXPECT_EQ(16u, at.BaseAlignment(MemoryLayout::kUniformBuffer));
}
TEST_F(AccessControlTest, BaseAlignmentRuntimeArray) {
U32 u32;
Array array(&u32, 0,
ast::ArrayDecorationList{create<ast::StrideDecoration>(4)});
AccessControl at{ast::AccessControl::kReadOnly, &array};
EXPECT_EQ(16u, at.BaseAlignment(MemoryLayout::kUniformBuffer));
}
TEST_F(AccessControlTest, BaseAlignmentStruct) {
auto* str = create<ast::Struct>(
ast::StructMemberList{Member("foo", ty.u32, {MemberOffset(0)}),
Member("bar", ty.u32, {MemberOffset(4)})},
ast::StructDecorationList{});
auto* struct_type = ty.struct_("struct_type", str);
AccessControl at{ast::AccessControl::kReadOnly, struct_type};
EXPECT_EQ(16u, at.BaseAlignment(MemoryLayout::kUniformBuffer));
EXPECT_EQ(4u, at.BaseAlignment(MemoryLayout::kStorageBuffer));
}
} // namespace
} // namespace type
} // namespace tint

53
src/type/alias_type.cc Normal file
View File

@@ -0,0 +1,53 @@
// Copyright 2020 The Tint 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 "src/type/alias_type.h"
#include <assert.h>
#include "src/ast/clone_context.h"
#include "src/ast/module.h"
TINT_INSTANTIATE_CLASS_ID(tint::type::Alias);
namespace tint {
namespace type {
Alias::Alias(const Symbol& sym, Type* subtype)
: symbol_(sym), subtype_(subtype) {
assert(subtype_);
}
Alias::Alias(Alias&&) = default;
Alias::~Alias() = default;
std::string Alias::type_name() const {
return "__alias_" + symbol_.to_str() + subtype_->type_name();
}
uint64_t Alias::MinBufferBindingSize(MemoryLayout mem_layout) const {
return subtype_->MinBufferBindingSize(mem_layout);
}
uint64_t Alias::BaseAlignment(MemoryLayout mem_layout) const {
return subtype_->BaseAlignment(mem_layout);
}
Alias* Alias::Clone(CloneContext* ctx) const {
return ctx->mod->create<Alias>(ctx->Clone(symbol()), ctx->Clone(subtype_));
}
} // namespace type
} // namespace tint

69
src/type/alias_type.h Normal file
View File

@@ -0,0 +1,69 @@
// Copyright 2020 The Tint 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.
#ifndef SRC_TYPE_ALIAS_TYPE_H_
#define SRC_TYPE_ALIAS_TYPE_H_
#include <string>
#include "src/symbol.h"
#include "src/type/type.h"
namespace tint {
namespace type {
/// A type alias type. Holds a name and pointer to another type.
class Alias : public Castable<Alias, Type> {
public:
/// Constructor
/// @param sym the symbol for the alias
/// @param subtype the alias'd type
Alias(const Symbol& sym, Type* subtype);
/// Move constructor
Alias(Alias&&);
/// Destructor
~Alias() override;
/// @returns the alias symbol
Symbol symbol() const { return symbol_; }
/// @returns the alias type
Type* type() const { return subtype_; }
/// @returns the type_name for this type
std::string type_name() const override;
/// @param mem_layout type of memory layout to use in calculation.
/// @returns minimum size required for this type, in bytes.
/// 0 for non-host shareable types.
uint64_t MinBufferBindingSize(MemoryLayout mem_layout) const override;
/// @param mem_layout type of memory layout to use in calculation.
/// @returns base alignment for the type, in bytes.
/// 0 for non-host shareable types.
uint64_t BaseAlignment(MemoryLayout mem_layout) const override;
/// Clones this type and all transitive types using the `CloneContext` `ctx`.
/// @param ctx the clone context
/// @return the newly cloned type
Alias* Clone(CloneContext* ctx) const override;
private:
Symbol const symbol_;
Type* const subtype_;
};
} // namespace type
} // namespace tint
#endif // SRC_TYPE_ALIAS_TYPE_H_

225
src/type/alias_type_test.cc Normal file
View File

@@ -0,0 +1,225 @@
// Copyright 2020 The Tint 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 "src/type/alias_type.h"
#include <memory>
#include <utility>
#include "src/ast/storage_class.h"
#include "src/ast/stride_decoration.h"
#include "src/ast/struct_member.h"
#include "src/ast/struct_member_decoration.h"
#include "src/ast/struct_member_offset_decoration.h"
#include "src/type/access_control_type.h"
#include "src/type/array_type.h"
#include "src/type/bool_type.h"
#include "src/type/f32_type.h"
#include "src/type/i32_type.h"
#include "src/type/matrix_type.h"
#include "src/type/pointer_type.h"
#include "src/type/struct_type.h"
#include "src/type/test_helper.h"
#include "src/type/texture_type.h"
#include "src/type/vector_type.h"
namespace tint {
namespace type {
namespace {
using AliasTest = TestHelper;
TEST_F(AliasTest, Create) {
auto* a = ty.alias("a_type", ty.u32);
EXPECT_EQ(a->symbol(), Symbol(1));
EXPECT_EQ(a->type(), ty.u32);
}
TEST_F(AliasTest, Is) {
auto* at = ty.alias("a", ty.i32);
type::Type* ty = at;
EXPECT_FALSE(ty->Is<AccessControl>());
EXPECT_TRUE(ty->Is<Alias>());
EXPECT_FALSE(ty->Is<Array>());
EXPECT_FALSE(ty->Is<Bool>());
EXPECT_FALSE(ty->Is<F32>());
EXPECT_FALSE(ty->Is<I32>());
EXPECT_FALSE(ty->Is<Matrix>());
EXPECT_FALSE(ty->Is<Pointer>());
EXPECT_FALSE(ty->Is<Sampler>());
EXPECT_FALSE(ty->Is<Struct>());
EXPECT_FALSE(ty->Is<Texture>());
EXPECT_FALSE(ty->Is<U32>());
EXPECT_FALSE(ty->Is<Vector>());
}
TEST_F(AliasTest, TypeName) {
auto* at = ty.alias("Particle", ty.i32);
EXPECT_EQ(at->type_name(), "__alias_tint_symbol_1__i32");
}
TEST_F(AliasTest, UnwrapIfNeeded_Alias) {
auto* a = ty.alias("a_type", ty.u32);
EXPECT_EQ(a->symbol(), Symbol(1));
EXPECT_EQ(a->type(), ty.u32);
EXPECT_EQ(a->UnwrapIfNeeded(), ty.u32);
EXPECT_EQ(ty.u32->UnwrapIfNeeded(), ty.u32);
}
TEST_F(AliasTest, UnwrapIfNeeded_AccessControl) {
AccessControl a{ast::AccessControl::kReadOnly, ty.u32};
EXPECT_EQ(a.type(), ty.u32);
EXPECT_EQ(a.UnwrapIfNeeded(), ty.u32);
}
TEST_F(AliasTest, UnwrapIfNeeded_MultiLevel) {
auto* a = ty.alias("a_type", ty.u32);
auto* aa = ty.alias("aa_type", a);
EXPECT_EQ(aa->symbol(), Symbol(2));
EXPECT_EQ(aa->type(), a);
EXPECT_EQ(aa->UnwrapIfNeeded(), ty.u32);
}
TEST_F(AliasTest, UnwrapIfNeeded_MultiLevel_AliasAccessControl) {
auto* a = ty.alias("a_type", ty.u32);
AccessControl aa{ast::AccessControl::kReadWrite, a};
EXPECT_EQ(aa.type(), a);
EXPECT_EQ(aa.UnwrapIfNeeded(), ty.u32);
}
TEST_F(AliasTest, UnwrapAll_TwiceAliasPointerTwiceAlias) {
auto* a = ty.alias("a_type", ty.u32);
auto* aa = ty.alias("aa_type", a);
Pointer paa{aa, ast::StorageClass::kUniform};
auto* apaa = ty.alias("paa_type", &paa);
auto* aapaa = ty.alias("aapaa_type", apaa);
EXPECT_EQ(aapaa->symbol(), Symbol(4));
EXPECT_EQ(aapaa->type(), apaa);
EXPECT_EQ(aapaa->UnwrapAll(), ty.u32);
}
TEST_F(AliasTest, UnwrapAll_SecondConsecutivePointerBlocksUnrapping) {
auto* a = ty.alias("a_type", ty.u32);
auto* aa = ty.alias("aa_type", a);
Pointer paa{aa, ast::StorageClass::kUniform};
Pointer ppaa{&paa, ast::StorageClass::kUniform};
auto* appaa = ty.alias("appaa_type", &ppaa);
EXPECT_EQ(appaa->UnwrapAll(), &paa);
}
TEST_F(AliasTest, UnwrapAll_SecondNonConsecutivePointerBlocksUnrapping) {
auto* a = ty.alias("a_type", ty.u32);
auto* aa = ty.alias("aa_type", a);
Pointer paa{aa, ast::StorageClass::kUniform};
auto* apaa = ty.alias("apaa_type", &paa);
auto* aapaa = ty.alias("aapaa_type", apaa);
Pointer paapaa{aapaa, ast::StorageClass::kUniform};
auto* apaapaa = ty.alias("apaapaa_type", &paapaa);
EXPECT_EQ(apaapaa->UnwrapAll(), &paa);
}
TEST_F(AliasTest, UnwrapAll_AccessControlPointer) {
AccessControl a{ast::AccessControl::kReadOnly, ty.u32};
Pointer pa{&a, ast::StorageClass::kUniform};
EXPECT_EQ(pa.type(), &a);
EXPECT_EQ(pa.UnwrapAll(), ty.u32);
}
TEST_F(AliasTest, UnwrapAll_PointerAccessControl) {
Pointer p{ty.u32, ast::StorageClass::kUniform};
AccessControl a{ast::AccessControl::kReadOnly, &p};
EXPECT_EQ(a.type(), &p);
EXPECT_EQ(a.UnwrapAll(), ty.u32);
}
TEST_F(AliasTest, MinBufferBindingSizeU32) {
auto* alias = ty.alias("alias", ty.u32);
EXPECT_EQ(4u, alias->MinBufferBindingSize(MemoryLayout::kUniformBuffer));
}
TEST_F(AliasTest, MinBufferBindingSizeArray) {
Array array(ty.u32, 4,
ast::ArrayDecorationList{
create<ast::StrideDecoration>(4),
});
auto* alias = ty.alias("alias", &array);
EXPECT_EQ(16u, alias->MinBufferBindingSize(MemoryLayout::kUniformBuffer));
}
TEST_F(AliasTest, MinBufferBindingSizeRuntimeArray) {
Array array(ty.u32, 0,
ast::ArrayDecorationList{
create<ast::StrideDecoration>(4),
});
auto* alias = ty.alias("alias", &array);
EXPECT_EQ(4u, alias->MinBufferBindingSize(MemoryLayout::kUniformBuffer));
}
TEST_F(AliasTest, MinBufferBindingSizeStruct) {
auto* str = create<ast::Struct>(
ast::StructMemberList{Member("foo", ty.u32, {MemberOffset(0)}),
Member("bar", ty.u32, {MemberOffset(4)})},
ast::StructDecorationList{});
auto* struct_type = ty.struct_("struct_type", str);
auto* alias = ty.alias("alias", struct_type);
EXPECT_EQ(16u, alias->MinBufferBindingSize(MemoryLayout::kUniformBuffer));
EXPECT_EQ(8u, alias->MinBufferBindingSize(MemoryLayout::kStorageBuffer));
}
TEST_F(AliasTest, BaseAlignmentU32) {
auto* alias = ty.alias("alias", ty.u32);
EXPECT_EQ(4u, alias->BaseAlignment(MemoryLayout::kUniformBuffer));
}
TEST_F(AliasTest, BaseAlignmentArray) {
Array array(ty.u32, 4,
ast::ArrayDecorationList{
create<ast::StrideDecoration>(4),
});
auto* alias = ty.alias("alias", &array);
EXPECT_EQ(16u, alias->BaseAlignment(MemoryLayout::kUniformBuffer));
}
TEST_F(AliasTest, BaseAlignmentRuntimeArray) {
Array array(ty.u32, 0,
ast::ArrayDecorationList{
create<ast::StrideDecoration>(4),
});
auto* alias = ty.alias("alias", &array);
EXPECT_EQ(16u, alias->BaseAlignment(MemoryLayout::kUniformBuffer));
}
TEST_F(AliasTest, BaseAlignmentStruct) {
auto* str = create<ast::Struct>(
ast::StructMemberList{Member("foo", ty.u32, {MemberOffset(0)}),
Member("bar", ty.u32, {MemberOffset(4)})},
ast::StructDecorationList{});
auto* struct_type = ty.struct_("struct_type", str);
auto* alias = ty.alias("alias", struct_type);
EXPECT_EQ(16u, alias->BaseAlignment(MemoryLayout::kUniformBuffer));
EXPECT_EQ(4u, alias->BaseAlignment(MemoryLayout::kStorageBuffer));
}
} // namespace
} // namespace type
} // namespace tint

104
src/type/array_type.cc Normal file
View File

@@ -0,0 +1,104 @@
// Copyright 2020 The Tint 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 "src/type/array_type.h"
#include <cmath>
#include <memory>
#include "src/ast/clone_context.h"
#include "src/ast/module.h"
#include "src/ast/stride_decoration.h"
#include "src/type/vector_type.h"
TINT_INSTANTIATE_CLASS_ID(tint::type::Array);
namespace tint {
namespace type {
Array::Array(Type* subtype, uint32_t size, ast::ArrayDecorationList decorations)
: subtype_(subtype), size_(size), decos_(decorations) {}
Array::Array(Array&&) = default;
Array::~Array() = default;
uint64_t Array::MinBufferBindingSize(MemoryLayout mem_layout) const {
if (!has_array_stride()) {
// Arrays in buffers are required to have a stride.
return 0;
}
if (IsRuntimeArray()) {
// WebGPU spec 10.1.2:
// If the last field of the corresponding structure defined in the shader
// has an unbounded array type, then the value of minBufferBindingSize must
// be greater than or equal to the byte offset of that field plus the stride
// of the unbounded array
return array_stride();
} else {
// Not including the padding for the last element
return (size_ - 1) * array_stride() +
subtype_->MinBufferBindingSize(mem_layout);
}
}
uint64_t Array::BaseAlignment(MemoryLayout mem_layout) const {
if (mem_layout == MemoryLayout::kUniformBuffer) {
float aligment = 16; // for a vec4
float unaligned = static_cast<float>(subtype_->BaseAlignment(mem_layout));
return static_cast<uint64_t>(aligment * std::ceil(unaligned / aligment));
} else if (mem_layout == MemoryLayout::kStorageBuffer) {
return subtype_->BaseAlignment(mem_layout);
}
return 0;
}
uint32_t Array::array_stride() const {
for (auto* deco : decos_) {
if (auto* stride = deco->As<ast::StrideDecoration>()) {
return stride->stride();
}
}
return 0;
}
bool Array::has_array_stride() const {
for (auto* deco : decos_) {
if (deco->Is<ast::StrideDecoration>()) {
return true;
}
}
return false;
}
std::string Array::type_name() const {
assert(subtype_);
std::string type_name = "__array" + subtype_->type_name();
if (!IsRuntimeArray())
type_name += "_" + std::to_string(size_);
if (has_array_stride())
type_name += "_stride_" + std::to_string(array_stride());
return type_name;
}
Array* Array::Clone(CloneContext* ctx) const {
return ctx->mod->create<Array>(ctx->Clone(subtype_), size_,
ctx->Clone(decorations()));
}
} // namespace type
} // namespace tint

86
src/type/array_type.h Normal file
View File

@@ -0,0 +1,86 @@
// Copyright 2020 The Tint 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.
#ifndef SRC_TYPE_ARRAY_TYPE_H_
#define SRC_TYPE_ARRAY_TYPE_H_
#include <assert.h>
#include <string>
#include <utility>
#include "src/ast/array_decoration.h"
#include "src/type/type.h"
namespace tint {
namespace type {
/// An array type. If size is zero then it is a runtime array.
class Array : public Castable<Array, Type> {
public:
/// Constructor
/// @param subtype the type of the array elements
/// @param size the number of elements in the array. `0` represents a
/// runtime-sized array.
/// @param decorations the array decorations
Array(Type* subtype, uint32_t size, ast::ArrayDecorationList decorations);
/// Move constructor
Array(Array&&);
~Array() override;
/// @returns true if this is a runtime array.
/// i.e. the size is determined at runtime
bool IsRuntimeArray() const { return size_ == 0; }
/// @param mem_layout type of memory layout to use in calculation.
/// @returns minimum size required for this type, in bytes.
/// 0 for non-host shareable types.
uint64_t MinBufferBindingSize(MemoryLayout mem_layout) const override;
/// @param mem_layout type of memory layout to use in calculation.
/// @returns base alignment for the type, in bytes.
/// 0 for non-host shareable types.
uint64_t BaseAlignment(MemoryLayout mem_layout) const override;
/// @returns the array decorations
const ast::ArrayDecorationList& decorations() const { return decos_; }
/// @returns the array stride or 0 if none set.
uint32_t array_stride() const;
/// @returns true if the array has a stride set
bool has_array_stride() const;
/// @returns the array type
Type* type() const { return subtype_; }
/// @returns the array size. Size is 0 for a runtime array
uint32_t size() const { return size_; }
/// @returns the name for the type
std::string type_name() const override;
/// Clones this type and all transitive types using the `CloneContext` `ctx`.
/// @param ctx the clone context
/// @return the newly cloned type
Array* Clone(CloneContext* ctx) const override;
private:
Type* const subtype_;
uint32_t const size_;
ast::ArrayDecorationList const decos_;
};
} // namespace type
} // namespace tint
#endif // SRC_TYPE_ARRAY_TYPE_H_

134
src/type/array_type_test.cc Normal file
View File

@@ -0,0 +1,134 @@
// Copyright 2020 The Tint 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 "src/type/array_type.h"
#include <memory>
#include <utility>
#include "src/ast/stride_decoration.h"
#include "src/type/access_control_type.h"
#include "src/type/bool_type.h"
#include "src/type/f32_type.h"
#include "src/type/i32_type.h"
#include "src/type/matrix_type.h"
#include "src/type/pointer_type.h"
#include "src/type/struct_type.h"
#include "src/type/test_helper.h"
#include "src/type/texture_type.h"
#include "src/type/u32_type.h"
#include "src/type/vector_type.h"
namespace tint {
namespace type {
namespace {
using ArrayTest = TestHelper;
TEST_F(ArrayTest, CreateSizedArray) {
U32 u32;
Array arr{&u32, 3, ast::ArrayDecorationList{}};
EXPECT_EQ(arr.type(), &u32);
EXPECT_EQ(arr.size(), 3u);
EXPECT_TRUE(arr.Is<Array>());
EXPECT_FALSE(arr.IsRuntimeArray());
}
TEST_F(ArrayTest, CreateRuntimeArray) {
U32 u32;
Array arr{&u32, 0, ast::ArrayDecorationList{}};
EXPECT_EQ(arr.type(), &u32);
EXPECT_EQ(arr.size(), 0u);
EXPECT_TRUE(arr.Is<Array>());
EXPECT_TRUE(arr.IsRuntimeArray());
}
TEST_F(ArrayTest, Is) {
I32 i32;
Array arr{&i32, 3, ast::ArrayDecorationList{}};
Type* ty = &arr;
EXPECT_FALSE(ty->Is<AccessControl>());
EXPECT_FALSE(ty->Is<Alias>());
EXPECT_TRUE(ty->Is<Array>());
EXPECT_FALSE(ty->Is<Bool>());
EXPECT_FALSE(ty->Is<F32>());
EXPECT_FALSE(ty->Is<I32>());
EXPECT_FALSE(ty->Is<Matrix>());
EXPECT_FALSE(ty->Is<Pointer>());
EXPECT_FALSE(ty->Is<Sampler>());
EXPECT_FALSE(ty->Is<Struct>());
EXPECT_FALSE(ty->Is<Texture>());
EXPECT_FALSE(ty->Is<U32>());
EXPECT_FALSE(ty->Is<Vector>());
}
TEST_F(ArrayTest, TypeName) {
I32 i32;
Array arr{&i32, 0, ast::ArrayDecorationList{}};
EXPECT_EQ(arr.type_name(), "__array__i32");
}
TEST_F(ArrayTest, TypeName_RuntimeArray) {
I32 i32;
Array arr{&i32, 3, ast::ArrayDecorationList{}};
EXPECT_EQ(arr.type_name(), "__array__i32_3");
}
TEST_F(ArrayTest, TypeName_WithStride) {
I32 i32;
Array arr{&i32, 3,
ast::ArrayDecorationList{create<ast::StrideDecoration>(16)}};
EXPECT_EQ(arr.type_name(), "__array__i32_3_stride_16");
}
TEST_F(ArrayTest, MinBufferBindingSizeNoStride) {
U32 u32;
Array arr(&u32, 4, ast::ArrayDecorationList{});
EXPECT_EQ(0u, arr.MinBufferBindingSize(MemoryLayout::kUniformBuffer));
}
TEST_F(ArrayTest, MinBufferBindingSizeArray) {
U32 u32;
Array arr(&u32, 4,
ast::ArrayDecorationList{create<ast::StrideDecoration>(4)});
EXPECT_EQ(16u, arr.MinBufferBindingSize(MemoryLayout::kUniformBuffer));
}
TEST_F(ArrayTest, MinBufferBindingSizeRuntimeArray) {
U32 u32;
Array arr(&u32, 0,
ast::ArrayDecorationList{create<ast::StrideDecoration>(4)});
EXPECT_EQ(4u, arr.MinBufferBindingSize(MemoryLayout::kUniformBuffer));
}
TEST_F(ArrayTest, BaseAlignmentArray) {
U32 u32;
Array arr(&u32, 4,
ast::ArrayDecorationList{create<ast::StrideDecoration>(4)});
EXPECT_EQ(16u, arr.BaseAlignment(MemoryLayout::kUniformBuffer));
EXPECT_EQ(4u, arr.BaseAlignment(MemoryLayout::kStorageBuffer));
}
TEST_F(ArrayTest, BaseAlignmentRuntimeArray) {
U32 u32;
Array arr(&u32, 0,
ast::ArrayDecorationList{create<ast::StrideDecoration>(4)});
EXPECT_EQ(16u, arr.BaseAlignment(MemoryLayout::kUniformBuffer));
EXPECT_EQ(4u, arr.BaseAlignment(MemoryLayout::kStorageBuffer));
}
} // namespace
} // namespace type
} // namespace tint

40
src/type/bool_type.cc Normal file
View File

@@ -0,0 +1,40 @@
// Copyright 2020 The Tint 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 "src/type/bool_type.h"
#include "src/ast/clone_context.h"
#include "src/ast/module.h"
TINT_INSTANTIATE_CLASS_ID(tint::type::Bool);
namespace tint {
namespace type {
Bool::Bool() = default;
Bool::Bool(Bool&&) = default;
Bool::~Bool() = default;
std::string Bool::type_name() const {
return "__bool";
}
Bool* Bool::Clone(CloneContext* ctx) const {
return ctx->mod->create<Bool>();
}
} // namespace type
} // namespace tint

46
src/type/bool_type.h Normal file
View File

@@ -0,0 +1,46 @@
// Copyright 2020 The Tint 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.
#ifndef SRC_TYPE_BOOL_TYPE_H_
#define SRC_TYPE_BOOL_TYPE_H_
#include <string>
#include "src/type/type.h"
namespace tint {
namespace type {
/// A boolean type
class Bool : public Castable<Bool, Type> {
public:
/// Constructor
Bool();
/// Move constructor
Bool(Bool&&);
~Bool() override;
/// @returns the name for this type
std::string type_name() const override;
/// Clones this type and all transitive types using the `CloneContext` `ctx`.
/// @param ctx the clone context
/// @return the newly cloned type
Bool* Clone(CloneContext* ctx) const override;
};
} // namespace type
} // namespace tint
#endif // SRC_TYPE_BOOL_TYPE_H_

View File

@@ -0,0 +1,65 @@
// Copyright 2020 The Tint 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 "src/type/bool_type.h"
#include "src/type/access_control_type.h"
#include "src/type/array_type.h"
#include "src/type/f32_type.h"
#include "src/type/i32_type.h"
#include "src/type/matrix_type.h"
#include "src/type/pointer_type.h"
#include "src/type/struct_type.h"
#include "src/type/test_helper.h"
#include "src/type/texture_type.h"
#include "src/type/u32_type.h"
#include "src/type/vector_type.h"
namespace tint {
namespace type {
namespace {
using BoolTest = TestHelper;
TEST_F(BoolTest, Is) {
Bool b;
Type* ty = &b;
EXPECT_FALSE(ty->Is<AccessControl>());
EXPECT_FALSE(ty->Is<Alias>());
EXPECT_FALSE(ty->Is<Array>());
EXPECT_TRUE(ty->Is<Bool>());
EXPECT_FALSE(ty->Is<F32>());
EXPECT_FALSE(ty->Is<I32>());
EXPECT_FALSE(ty->Is<Matrix>());
EXPECT_FALSE(ty->Is<Pointer>());
EXPECT_FALSE(ty->Is<Sampler>());
EXPECT_FALSE(ty->Is<Struct>());
EXPECT_FALSE(ty->Is<Texture>());
EXPECT_FALSE(ty->Is<U32>());
EXPECT_FALSE(ty->Is<Vector>());
}
TEST_F(BoolTest, TypeName) {
Bool b;
EXPECT_EQ(b.type_name(), "__bool");
}
TEST_F(BoolTest, MinBufferBindingSize) {
Bool b;
EXPECT_EQ(0u, b.MinBufferBindingSize(MemoryLayout::kUniformBuffer));
}
} // namespace
} // namespace type
} // namespace tint

View File

@@ -0,0 +1,59 @@
// Copyright 2020 The Tint 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 "src/type/depth_texture_type.h"
#include <cassert>
#include <sstream>
#include "src/ast/clone_context.h"
#include "src/ast/module.h"
TINT_INSTANTIATE_CLASS_ID(tint::type::DepthTexture);
namespace tint {
namespace type {
namespace {
#ifndef NDEBUG
bool IsValidDepthDimension(TextureDimension dim) {
return dim == TextureDimension::k2d || dim == TextureDimension::k2dArray ||
dim == TextureDimension::kCube || dim == TextureDimension::kCubeArray;
}
#endif // NDEBUG
} // namespace
DepthTexture::DepthTexture(TextureDimension dim) : Base(dim) {
assert(IsValidDepthDimension(dim));
}
DepthTexture::DepthTexture(DepthTexture&&) = default;
DepthTexture::~DepthTexture() = default;
std::string DepthTexture::type_name() const {
std::ostringstream out;
out << "__depth_texture_" << dim();
return out.str();
}
DepthTexture* DepthTexture::Clone(CloneContext* ctx) const {
return ctx->mod->create<DepthTexture>(dim());
}
} // namespace type
} // namespace tint

View File

@@ -0,0 +1,47 @@
// Copyright 2020 The Tint 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.
#ifndef SRC_TYPE_DEPTH_TEXTURE_TYPE_H_
#define SRC_TYPE_DEPTH_TEXTURE_TYPE_H_
#include <string>
#include "src/type/texture_type.h"
namespace tint {
namespace type {
/// A depth texture type.
class DepthTexture : public Castable<DepthTexture, Texture> {
public:
/// Constructor
/// @param dim the dimensionality of the texture
explicit DepthTexture(TextureDimension dim);
/// Move constructor
DepthTexture(DepthTexture&&);
~DepthTexture() override;
/// @returns the name for this type
std::string type_name() const override;
/// Clones this type and all transitive types using the `CloneContext` `ctx`.
/// @param ctx the clone context
/// @return the newly cloned type
DepthTexture* Clone(CloneContext* ctx) const override;
};
} // namespace type
} // namespace tint
#endif // SRC_TYPE_DEPTH_TEXTURE_TYPE_H_

View File

@@ -0,0 +1,81 @@
// Copyright 2020 The Tint 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 "src/type/depth_texture_type.h"
#include "src/type/test_helper.h"
#include "src/type/access_control_type.h"
#include "src/type/array_type.h"
#include "src/type/bool_type.h"
#include "src/type/f32_type.h"
#include "src/type/i32_type.h"
#include "src/type/matrix_type.h"
#include "src/type/pointer_type.h"
#include "src/type/sampled_texture_type.h"
#include "src/type/storage_texture_type.h"
#include "src/type/struct_type.h"
#include "src/type/u32_type.h"
#include "src/type/vector_type.h"
namespace tint {
namespace type {
namespace {
using DepthTextureTest = TestHelper;
TEST_F(DepthTextureTest, Is) {
DepthTexture d(TextureDimension::kCube);
Type* ty = &d;
EXPECT_FALSE(ty->Is<AccessControl>());
EXPECT_FALSE(ty->Is<Alias>());
EXPECT_FALSE(ty->Is<Array>());
EXPECT_FALSE(ty->Is<Bool>());
EXPECT_FALSE(ty->Is<F32>());
EXPECT_FALSE(ty->Is<I32>());
EXPECT_FALSE(ty->Is<Matrix>());
EXPECT_FALSE(ty->Is<Pointer>());
EXPECT_FALSE(ty->Is<Sampler>());
EXPECT_FALSE(ty->Is<Struct>());
EXPECT_TRUE(ty->Is<Texture>());
EXPECT_FALSE(ty->Is<U32>());
EXPECT_FALSE(ty->Is<Vector>());
}
TEST_F(DepthTextureTest, IsTexture) {
DepthTexture d(TextureDimension::kCube);
Texture* ty = &d;
EXPECT_TRUE(ty->Is<DepthTexture>());
EXPECT_FALSE(ty->Is<SampledTexture>());
EXPECT_FALSE(ty->Is<StorageTexture>());
}
TEST_F(DepthTextureTest, Dim) {
DepthTexture d(TextureDimension::kCube);
EXPECT_EQ(d.dim(), TextureDimension::kCube);
}
TEST_F(DepthTextureTest, TypeName) {
DepthTexture d(TextureDimension::kCube);
EXPECT_EQ(d.type_name(), "__depth_texture_cube");
}
TEST_F(DepthTextureTest, MinBufferBindingSize) {
DepthTexture d(TextureDimension::kCube);
EXPECT_EQ(0u, d.MinBufferBindingSize(MemoryLayout::kUniformBuffer));
}
} // namespace
} // namespace type
} // namespace tint

48
src/type/f32_type.cc Normal file
View File

@@ -0,0 +1,48 @@
// Copyright 2020 The Tint 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 "src/type/f32_type.h"
#include "src/ast/clone_context.h"
#include "src/ast/module.h"
TINT_INSTANTIATE_CLASS_ID(tint::type::F32);
namespace tint {
namespace type {
F32::F32() = default;
F32::F32(F32&&) = default;
F32::~F32() = default;
std::string F32::type_name() const {
return "__f32";
}
uint64_t F32::MinBufferBindingSize(MemoryLayout) const {
return 4;
}
uint64_t F32::BaseAlignment(MemoryLayout) const {
return 4;
}
F32* F32::Clone(CloneContext* ctx) const {
return ctx->mod->create<F32>();
}
} // namespace type
} // namespace tint

56
src/type/f32_type.h Normal file
View File

@@ -0,0 +1,56 @@
// Copyright 2020 The Tint 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.
#ifndef SRC_TYPE_F32_TYPE_H_
#define SRC_TYPE_F32_TYPE_H_
#include <string>
#include "src/type/type.h"
namespace tint {
namespace type {
/// A float 32 type
class F32 : public Castable<F32, Type> {
public:
/// Constructor
F32();
/// Move constructor
F32(F32&&);
~F32() override;
/// @returns the name for this type
std::string type_name() const override;
/// @param mem_layout type of memory layout to use in calculation.
/// @returns minimum size required for this type, in bytes.
/// 0 for non-host shareable types.
uint64_t MinBufferBindingSize(MemoryLayout mem_layout) const override;
/// @param mem_layout type of memory layout to use in calculation.
/// @returns base alignment for the type, in bytes.
/// 0 for non-host shareable types.
uint64_t BaseAlignment(MemoryLayout mem_layout) const override;
/// Clones this type and all transitive types using the `CloneContext` `ctx`.
/// @param ctx the clone context
/// @return the newly cloned type
F32* Clone(CloneContext* ctx) const override;
};
} // namespace type
} // namespace tint
#endif // SRC_TYPE_F32_TYPE_H_

70
src/type/f32_type_test.cc Normal file
View File

@@ -0,0 +1,70 @@
// Copyright 2020 The Tint 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 "src/type/f32_type.h"
#include "src/type/access_control_type.h"
#include "src/type/array_type.h"
#include "src/type/bool_type.h"
#include "src/type/i32_type.h"
#include "src/type/matrix_type.h"
#include "src/type/pointer_type.h"
#include "src/type/struct_type.h"
#include "src/type/test_helper.h"
#include "src/type/texture_type.h"
#include "src/type/u32_type.h"
#include "src/type/vector_type.h"
namespace tint {
namespace type {
namespace {
using F32Test = TestHelper;
TEST_F(F32Test, Is) {
F32 f;
Type* ty = &f;
EXPECT_FALSE(ty->Is<AccessControl>());
EXPECT_FALSE(ty->Is<Alias>());
EXPECT_FALSE(ty->Is<Array>());
EXPECT_FALSE(ty->Is<Bool>());
EXPECT_TRUE(ty->Is<F32>());
EXPECT_FALSE(ty->Is<I32>());
EXPECT_FALSE(ty->Is<Matrix>());
EXPECT_FALSE(ty->Is<Pointer>());
EXPECT_FALSE(ty->Is<Sampler>());
EXPECT_FALSE(ty->Is<Struct>());
EXPECT_FALSE(ty->Is<Texture>());
EXPECT_FALSE(ty->Is<U32>());
EXPECT_FALSE(ty->Is<Vector>());
}
TEST_F(F32Test, TypeName) {
F32 f;
EXPECT_EQ(f.type_name(), "__f32");
}
TEST_F(F32Test, MinBufferBindingSize) {
F32 f;
EXPECT_EQ(4u, f.MinBufferBindingSize(MemoryLayout::kUniformBuffer));
}
TEST_F(F32Test, BaseAlignment) {
F32 f;
EXPECT_EQ(4u, f.BaseAlignment(MemoryLayout::kUniformBuffer));
}
} // namespace
} // namespace type
} // namespace tint

48
src/type/i32_type.cc Normal file
View File

@@ -0,0 +1,48 @@
// Copyright 2020 The Tint 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 "src/type/i32_type.h"
#include "src/ast/clone_context.h"
#include "src/ast/module.h"
TINT_INSTANTIATE_CLASS_ID(tint::type::I32);
namespace tint {
namespace type {
I32::I32() = default;
I32::I32(I32&&) = default;
I32::~I32() = default;
std::string I32::type_name() const {
return "__i32";
}
uint64_t I32::MinBufferBindingSize(MemoryLayout) const {
return 4;
}
uint64_t I32::BaseAlignment(MemoryLayout) const {
return 4;
}
I32* I32::Clone(CloneContext* ctx) const {
return ctx->mod->create<I32>();
}
} // namespace type
} // namespace tint

56
src/type/i32_type.h Normal file
View File

@@ -0,0 +1,56 @@
// Copyright 2020 The Tint 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.
#ifndef SRC_TYPE_I32_TYPE_H_
#define SRC_TYPE_I32_TYPE_H_
#include <string>
#include "src/type/type.h"
namespace tint {
namespace type {
/// A signed int 32 type.
class I32 : public Castable<I32, Type> {
public:
/// Constructor
I32();
/// Move constructor
I32(I32&&);
~I32() override;
/// @returns the name for this type
std::string type_name() const override;
/// @param mem_layout type of memory layout to use in calculation.
/// @returns minimum size required for this type, in bytes.
/// 0 for non-host shareable types.
uint64_t MinBufferBindingSize(MemoryLayout mem_layout) const override;
/// @param mem_layout type of memory layout to use in calculation.
/// @returns base alignment for the type, in bytes.
/// 0 for non-host shareable types.
uint64_t BaseAlignment(MemoryLayout mem_layout) const override;
/// Clones this type and all transitive types using the `CloneContext` `ctx`.
/// @param ctx the clone context
/// @return the newly cloned type
I32* Clone(CloneContext* ctx) const override;
};
} // namespace type
} // namespace tint
#endif // SRC_TYPE_I32_TYPE_H_

70
src/type/i32_type_test.cc Normal file
View File

@@ -0,0 +1,70 @@
// Copyright 2020 The Tint 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 "src/type/i32_type.h"
#include "src/type/access_control_type.h"
#include "src/type/array_type.h"
#include "src/type/bool_type.h"
#include "src/type/f32_type.h"
#include "src/type/matrix_type.h"
#include "src/type/pointer_type.h"
#include "src/type/struct_type.h"
#include "src/type/test_helper.h"
#include "src/type/texture_type.h"
#include "src/type/u32_type.h"
#include "src/type/vector_type.h"
namespace tint {
namespace type {
namespace {
using I32Test = TestHelper;
TEST_F(I32Test, Is) {
I32 i;
Type* ty = &i;
EXPECT_FALSE(ty->Is<AccessControl>());
EXPECT_FALSE(ty->Is<Alias>());
EXPECT_FALSE(ty->Is<Array>());
EXPECT_FALSE(ty->Is<Bool>());
EXPECT_FALSE(ty->Is<F32>());
EXPECT_TRUE(ty->Is<I32>());
EXPECT_FALSE(ty->Is<Matrix>());
EXPECT_FALSE(ty->Is<Pointer>());
EXPECT_FALSE(ty->Is<Sampler>());
EXPECT_FALSE(ty->Is<Struct>());
EXPECT_FALSE(ty->Is<Texture>());
EXPECT_FALSE(ty->Is<U32>());
EXPECT_FALSE(ty->Is<Vector>());
}
TEST_F(I32Test, TypeName) {
I32 i;
EXPECT_EQ(i.type_name(), "__i32");
}
TEST_F(I32Test, MinBufferBindingSize) {
I32 i;
EXPECT_EQ(4u, i.MinBufferBindingSize(MemoryLayout::kUniformBuffer));
}
TEST_F(I32Test, BaseAlignment) {
I32 i;
EXPECT_EQ(4u, i.BaseAlignment(MemoryLayout::kUniformBuffer));
}
} // namespace
} // namespace type
} // namespace tint

63
src/type/matrix_type.cc Normal file
View File

@@ -0,0 +1,63 @@
// Copyright 2020 The Tint 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 "src/type/matrix_type.h"
#include <assert.h>
#include "src/ast/clone_context.h"
#include "src/ast/module.h"
#include "src/type/array_type.h"
#include "src/type/vector_type.h"
TINT_INSTANTIATE_CLASS_ID(tint::type::Matrix);
namespace tint {
namespace type {
Matrix::Matrix(Type* subtype, uint32_t rows, uint32_t columns)
: subtype_(subtype), rows_(rows), columns_(columns) {
assert(rows > 1);
assert(rows < 5);
assert(columns > 1);
assert(columns < 5);
}
Matrix::Matrix(Matrix&&) = default;
Matrix::~Matrix() = default;
std::string Matrix::type_name() const {
return "__mat_" + std::to_string(rows_) + "_" + std::to_string(columns_) +
subtype_->type_name();
}
uint64_t Matrix::MinBufferBindingSize(MemoryLayout mem_layout) const {
Vector vec(subtype_, rows_);
return (columns_ - 1) * vec.BaseAlignment(mem_layout) +
vec.MinBufferBindingSize(mem_layout);
}
uint64_t Matrix::BaseAlignment(MemoryLayout mem_layout) const {
Vector vec(subtype_, rows_);
Array arr(&vec, columns_, ast::ArrayDecorationList{});
return arr.BaseAlignment(mem_layout);
}
Matrix* Matrix::Clone(CloneContext* ctx) const {
return ctx->mod->create<Matrix>(ctx->Clone(subtype_), rows_, columns_);
}
} // namespace type
} // namespace tint

71
src/type/matrix_type.h Normal file
View File

@@ -0,0 +1,71 @@
// Copyright 2020 The Tint 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.
#ifndef SRC_TYPE_MATRIX_TYPE_H_
#define SRC_TYPE_MATRIX_TYPE_H_
#include <string>
#include "src/type/type.h"
namespace tint {
namespace type {
/// A matrix type
class Matrix : public Castable<Matrix, Type> {
public:
/// Constructor
/// @param subtype type matrix type
/// @param rows the number of rows in the matrix
/// @param columns the number of columns in the matrix
Matrix(Type* subtype, uint32_t rows, uint32_t columns);
/// Move constructor
Matrix(Matrix&&);
~Matrix() override;
/// @returns the type of the matrix
Type* type() const { return subtype_; }
/// @returns the number of rows in the matrix
uint32_t rows() const { return rows_; }
/// @returns the number of columns in the matrix
uint32_t columns() const { return columns_; }
/// @returns the name for this type
std::string type_name() const override;
/// @param mem_layout type of memory layout to use in calculation.
/// @returns minimum size required for this type, in bytes.
/// 0 for non-host shareable types.
uint64_t MinBufferBindingSize(MemoryLayout mem_layout) const override;
/// @param mem_layout type of memory layout to use in calculation.
/// @returns base alignment for the type, in bytes.
/// 0 for non-host shareable types.
uint64_t BaseAlignment(MemoryLayout mem_layout) const override;
/// Clones this type and all transitive types using the `CloneContext` `ctx`.
/// @param ctx the clone context
/// @return the newly cloned type
Matrix* Clone(CloneContext* ctx) const override;
private:
Type* const subtype_;
uint32_t const rows_;
uint32_t const columns_;
};
} // namespace type
} // namespace tint
#endif // SRC_TYPE_MATRIX_TYPE_H_

View File

@@ -0,0 +1,126 @@
// Copyright 2020 The Tint 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 "src/type/matrix_type.h"
#include "src/type/access_control_type.h"
#include "src/type/array_type.h"
#include "src/type/bool_type.h"
#include "src/type/f32_type.h"
#include "src/type/i32_type.h"
#include "src/type/pointer_type.h"
#include "src/type/struct_type.h"
#include "src/type/test_helper.h"
#include "src/type/texture_type.h"
#include "src/type/u32_type.h"
#include "src/type/vector_type.h"
namespace tint {
namespace type {
namespace {
using MatrixTest = TestHelper;
TEST_F(MatrixTest, Creation) {
I32 i32;
Matrix m{&i32, 2, 4};
EXPECT_EQ(m.type(), &i32);
EXPECT_EQ(m.rows(), 2u);
EXPECT_EQ(m.columns(), 4u);
}
TEST_F(MatrixTest, Is) {
I32 i32;
Matrix m{&i32, 2, 3};
Type* ty = &m;
EXPECT_FALSE(ty->Is<AccessControl>());
EXPECT_FALSE(ty->Is<Alias>());
EXPECT_FALSE(ty->Is<Array>());
EXPECT_FALSE(ty->Is<Bool>());
EXPECT_FALSE(ty->Is<F32>());
EXPECT_FALSE(ty->Is<I32>());
EXPECT_TRUE(ty->Is<Matrix>());
EXPECT_FALSE(ty->Is<Pointer>());
EXPECT_FALSE(ty->Is<Sampler>());
EXPECT_FALSE(ty->Is<Struct>());
EXPECT_FALSE(ty->Is<Texture>());
EXPECT_FALSE(ty->Is<U32>());
EXPECT_FALSE(ty->Is<Vector>());
}
TEST_F(MatrixTest, TypeName) {
I32 i32;
Matrix m{&i32, 2, 3};
EXPECT_EQ(m.type_name(), "__mat_2_3__i32");
}
TEST_F(MatrixTest, MinBufferBindingSize4x2) {
I32 i32;
Matrix m{&i32, 4, 2};
EXPECT_EQ(32u, m.MinBufferBindingSize(MemoryLayout::kUniformBuffer));
EXPECT_EQ(32u, m.MinBufferBindingSize(MemoryLayout::kStorageBuffer));
}
TEST_F(MatrixTest, MinBufferBindingSize3x2) {
I32 i32;
Matrix m{&i32, 3, 2};
EXPECT_EQ(28u, m.MinBufferBindingSize(MemoryLayout::kUniformBuffer));
EXPECT_EQ(28u, m.MinBufferBindingSize(MemoryLayout::kStorageBuffer));
}
TEST_F(MatrixTest, MinBufferBindingSize2x3) {
I32 i32;
Matrix m{&i32, 2, 3};
EXPECT_EQ(24u, m.MinBufferBindingSize(MemoryLayout::kUniformBuffer));
EXPECT_EQ(24u, m.MinBufferBindingSize(MemoryLayout::kStorageBuffer));
}
TEST_F(MatrixTest, MinBufferBindingSize2x2) {
I32 i32;
Matrix m{&i32, 2, 2};
EXPECT_EQ(16u, m.MinBufferBindingSize(MemoryLayout::kUniformBuffer));
EXPECT_EQ(16u, m.MinBufferBindingSize(MemoryLayout::kStorageBuffer));
}
TEST_F(MatrixTest, BaseAlignment4x2) {
I32 i32;
Matrix m{&i32, 4, 2};
EXPECT_EQ(16u, m.BaseAlignment(MemoryLayout::kUniformBuffer));
EXPECT_EQ(16u, m.BaseAlignment(MemoryLayout::kStorageBuffer));
}
TEST_F(MatrixTest, BaseAlignment3x2) {
I32 i32;
Matrix m{&i32, 3, 2};
EXPECT_EQ(16u, m.BaseAlignment(MemoryLayout::kUniformBuffer));
EXPECT_EQ(16u, m.BaseAlignment(MemoryLayout::kStorageBuffer));
}
TEST_F(MatrixTest, BaseAlignment2x3) {
I32 i32;
Matrix m{&i32, 2, 3};
EXPECT_EQ(16u, m.BaseAlignment(MemoryLayout::kUniformBuffer));
EXPECT_EQ(8u, m.BaseAlignment(MemoryLayout::kStorageBuffer));
}
TEST_F(MatrixTest, BaseAlignment2x2) {
I32 i32;
Matrix m{&i32, 2, 2};
EXPECT_EQ(16u, m.BaseAlignment(MemoryLayout::kUniformBuffer));
EXPECT_EQ(8u, m.BaseAlignment(MemoryLayout::kStorageBuffer));
}
} // namespace
} // namespace type
} // namespace tint

View File

@@ -0,0 +1,48 @@
// Copyright 2020 The Tint 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 "src/type/multisampled_texture_type.h"
#include <cassert>
#include <sstream>
#include "src/ast/clone_context.h"
#include "src/ast/module.h"
TINT_INSTANTIATE_CLASS_ID(tint::type::MultisampledTexture);
namespace tint {
namespace type {
MultisampledTexture::MultisampledTexture(TextureDimension dim, Type* type)
: Base(dim), type_(type) {
assert(type_);
}
MultisampledTexture::MultisampledTexture(MultisampledTexture&&) = default;
MultisampledTexture::~MultisampledTexture() = default;
std::string MultisampledTexture::type_name() const {
std::ostringstream out;
out << "__multisampled_texture_" << dim() << type_->type_name();
return out.str();
}
MultisampledTexture* MultisampledTexture::Clone(CloneContext* ctx) const {
return ctx->mod->create<MultisampledTexture>(dim(), ctx->Clone(type_));
}
} // namespace type
} // namespace tint

View File

@@ -0,0 +1,54 @@
// Copyright 2020 The Tint 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.
#ifndef SRC_TYPE_MULTISAMPLED_TEXTURE_TYPE_H_
#define SRC_TYPE_MULTISAMPLED_TEXTURE_TYPE_H_
#include <string>
#include "src/type/texture_type.h"
namespace tint {
namespace type {
/// A multisampled texture type.
class MultisampledTexture : public Castable<MultisampledTexture, Texture> {
public:
/// Constructor
/// @param dim the dimensionality of the texture
/// @param type the data type of the multisampled texture
MultisampledTexture(TextureDimension dim, Type* type);
/// Move constructor
MultisampledTexture(MultisampledTexture&&);
~MultisampledTexture() override;
/// @returns the subtype of the sampled texture
Type* type() const { return type_; }
/// @returns the name for this type
std::string type_name() const override;
/// Clones this type and all transitive types using the `CloneContext` `ctx`.
/// @param ctx the clone context
/// @return the newly cloned type
MultisampledTexture* Clone(CloneContext* ctx) const override;
private:
Type* const type_;
};
} // namespace type
} // namespace tint
#endif // SRC_TYPE_MULTISAMPLED_TEXTURE_TYPE_H_

View File

@@ -0,0 +1,93 @@
// Copyright 2020 The Tint 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 "src/type/multisampled_texture_type.h"
#include "src/type/access_control_type.h"
#include "src/type/array_type.h"
#include "src/type/bool_type.h"
#include "src/type/depth_texture_type.h"
#include "src/type/f32_type.h"
#include "src/type/i32_type.h"
#include "src/type/matrix_type.h"
#include "src/type/pointer_type.h"
#include "src/type/sampled_texture_type.h"
#include "src/type/storage_texture_type.h"
#include "src/type/struct_type.h"
#include "src/type/test_helper.h"
#include "src/type/u32_type.h"
#include "src/type/vector_type.h"
namespace tint {
namespace type {
namespace {
using MultisampledTextureTest = TestHelper;
TEST_F(MultisampledTextureTest, Is) {
F32 f32;
MultisampledTexture s(TextureDimension::kCube, &f32);
Type* ty = &s;
EXPECT_FALSE(ty->Is<AccessControl>());
EXPECT_FALSE(ty->Is<Alias>());
EXPECT_FALSE(ty->Is<Array>());
EXPECT_FALSE(ty->Is<Bool>());
EXPECT_FALSE(ty->Is<F32>());
EXPECT_FALSE(ty->Is<I32>());
EXPECT_FALSE(ty->Is<Matrix>());
EXPECT_FALSE(ty->Is<Pointer>());
EXPECT_FALSE(ty->Is<Sampler>());
EXPECT_FALSE(ty->Is<Struct>());
EXPECT_TRUE(ty->Is<Texture>());
EXPECT_FALSE(ty->Is<U32>());
EXPECT_FALSE(ty->Is<Vector>());
}
TEST_F(MultisampledTextureTest, IsTexture) {
F32 f32;
MultisampledTexture s(TextureDimension::kCube, &f32);
Texture* ty = &s;
EXPECT_FALSE(ty->Is<DepthTexture>());
EXPECT_TRUE(ty->Is<MultisampledTexture>());
EXPECT_FALSE(ty->Is<SampledTexture>());
EXPECT_FALSE(ty->Is<StorageTexture>());
}
TEST_F(MultisampledTextureTest, Dim) {
F32 f32;
MultisampledTexture s(TextureDimension::k3d, &f32);
EXPECT_EQ(s.dim(), TextureDimension::k3d);
}
TEST_F(MultisampledTextureTest, Type) {
F32 f32;
MultisampledTexture s(TextureDimension::k3d, &f32);
EXPECT_EQ(s.type(), &f32);
}
TEST_F(MultisampledTextureTest, TypeName) {
F32 f32;
MultisampledTexture s(TextureDimension::k3d, &f32);
EXPECT_EQ(s.type_name(), "__multisampled_texture_3d__f32");
}
TEST_F(MultisampledTextureTest, MinBufferBindingSize) {
F32 f32;
MultisampledTexture s(TextureDimension::k3d, &f32);
EXPECT_EQ(0u, s.MinBufferBindingSize(MemoryLayout::kUniformBuffer));
}
} // namespace
} // namespace type
} // namespace tint

43
src/type/pointer_type.cc Normal file
View File

@@ -0,0 +1,43 @@
// Copyright 2020 The Tint 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 "src/type/pointer_type.h"
#include "src/ast/clone_context.h"
#include "src/ast/module.h"
TINT_INSTANTIATE_CLASS_ID(tint::type::Pointer);
namespace tint {
namespace type {
Pointer::Pointer(Type* subtype, ast::StorageClass storage_class)
: subtype_(subtype), storage_class_(storage_class) {}
std::string Pointer::type_name() const {
std::ostringstream out;
out << "__ptr_" << storage_class_ << subtype_->type_name();
return out.str();
}
Pointer::Pointer(Pointer&&) = default;
Pointer::~Pointer() = default;
Pointer* Pointer::Clone(CloneContext* ctx) const {
return ctx->mod->create<Pointer>(ctx->Clone(subtype_), storage_class_);
}
} // namespace type
} // namespace tint

59
src/type/pointer_type.h Normal file
View File

@@ -0,0 +1,59 @@
// Copyright 2020 The Tint 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.
#ifndef SRC_TYPE_POINTER_TYPE_H_
#define SRC_TYPE_POINTER_TYPE_H_
#include <sstream>
#include <string>
#include "src/ast/storage_class.h"
#include "src/type/type.h"
namespace tint {
namespace type {
/// A pointer type.
class Pointer : public Castable<Pointer, Type> {
public:
/// Construtor
/// @param subtype the pointee type
/// @param storage_class the storage class of the pointer
explicit Pointer(Type* subtype, ast::StorageClass storage_class);
/// Move constructor
Pointer(Pointer&&);
~Pointer() override;
/// @returns the pointee type
Type* type() const { return subtype_; }
/// @returns the storage class of the pointer
ast::StorageClass storage_class() const { return storage_class_; }
/// @returns the name for this type
std::string type_name() const override;
/// Clones this type and all transitive types using the `CloneContext` `ctx`.
/// @param ctx the clone context
/// @return the newly cloned type
Pointer* Clone(CloneContext* ctx) const override;
private:
Type* const subtype_;
ast::StorageClass const storage_class_;
};
} // namespace type
} // namespace tint
#endif // SRC_TYPE_POINTER_TYPE_H_

View File

@@ -0,0 +1,69 @@
// Copyright 2020 The Tint 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 "src/type/pointer_type.h"
#include "src/type/access_control_type.h"
#include "src/type/array_type.h"
#include "src/type/bool_type.h"
#include "src/type/f32_type.h"
#include "src/type/i32_type.h"
#include "src/type/matrix_type.h"
#include "src/type/struct_type.h"
#include "src/type/test_helper.h"
#include "src/type/texture_type.h"
#include "src/type/u32_type.h"
#include "src/type/vector_type.h"
namespace tint {
namespace type {
namespace {
using PointerTest = TestHelper;
TEST_F(PointerTest, Creation) {
I32 i32;
Pointer p{&i32, ast::StorageClass::kStorage};
EXPECT_EQ(p.type(), &i32);
EXPECT_EQ(p.storage_class(), ast::StorageClass::kStorage);
}
TEST_F(PointerTest, Is) {
I32 i32;
Pointer p{&i32, ast::StorageClass::kFunction};
Type* ty = &p;
EXPECT_FALSE(ty->Is<AccessControl>());
EXPECT_FALSE(ty->Is<Alias>());
EXPECT_FALSE(ty->Is<Array>());
EXPECT_FALSE(ty->Is<Bool>());
EXPECT_FALSE(ty->Is<F32>());
EXPECT_FALSE(ty->Is<I32>());
EXPECT_FALSE(ty->Is<Matrix>());
EXPECT_TRUE(ty->Is<Pointer>());
EXPECT_FALSE(ty->Is<Sampler>());
EXPECT_FALSE(ty->Is<Struct>());
EXPECT_FALSE(ty->Is<Texture>());
EXPECT_FALSE(ty->Is<U32>());
EXPECT_FALSE(ty->Is<Vector>());
}
TEST_F(PointerTest, TypeName) {
I32 i32;
Pointer p{&i32, ast::StorageClass::kWorkgroup};
EXPECT_EQ(p.type_name(), "__ptr_workgroup__i32");
}
} // namespace
} // namespace type
} // namespace tint

View File

@@ -0,0 +1,48 @@
// Copyright 2020 The Tint 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 "src/type/sampled_texture_type.h"
#include <cassert>
#include <sstream>
#include "src/ast/clone_context.h"
#include "src/ast/module.h"
TINT_INSTANTIATE_CLASS_ID(tint::type::SampledTexture);
namespace tint {
namespace type {
SampledTexture::SampledTexture(TextureDimension dim, Type* type)
: Base(dim), type_(type) {
assert(type_);
}
SampledTexture::SampledTexture(SampledTexture&&) = default;
SampledTexture::~SampledTexture() = default;
std::string SampledTexture::type_name() const {
std::ostringstream out;
out << "__sampled_texture_" << dim() << type_->type_name();
return out.str();
}
SampledTexture* SampledTexture::Clone(CloneContext* ctx) const {
return ctx->mod->create<SampledTexture>(dim(), ctx->Clone(type_));
}
} // namespace type
} // namespace tint

View File

@@ -0,0 +1,54 @@
// Copyright 2020 The Tint 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.
#ifndef SRC_TYPE_SAMPLED_TEXTURE_TYPE_H_
#define SRC_TYPE_SAMPLED_TEXTURE_TYPE_H_
#include <string>
#include "src/type/texture_type.h"
namespace tint {
namespace type {
/// A sampled texture type.
class SampledTexture : public Castable<SampledTexture, Texture> {
public:
/// Constructor
/// @param dim the dimensionality of the texture
/// @param type the data type of the sampled texture
SampledTexture(TextureDimension dim, Type* type);
/// Move constructor
SampledTexture(SampledTexture&&);
~SampledTexture() override;
/// @returns the subtype of the sampled texture
Type* type() const { return type_; }
/// @returns the name for this type
std::string type_name() const override;
/// Clones this type and all transitive types using the `CloneContext` `ctx`.
/// @param ctx the clone context
/// @return the newly cloned type
SampledTexture* Clone(CloneContext* ctx) const override;
private:
Type* const type_;
};
} // namespace type
} // namespace tint
#endif // SRC_TYPE_SAMPLED_TEXTURE_TYPE_H_

View File

@@ -0,0 +1,91 @@
// Copyright 2020 The Tint 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 "src/type/sampled_texture_type.h"
#include "src/type/access_control_type.h"
#include "src/type/array_type.h"
#include "src/type/bool_type.h"
#include "src/type/depth_texture_type.h"
#include "src/type/f32_type.h"
#include "src/type/i32_type.h"
#include "src/type/matrix_type.h"
#include "src/type/pointer_type.h"
#include "src/type/storage_texture_type.h"
#include "src/type/struct_type.h"
#include "src/type/test_helper.h"
#include "src/type/u32_type.h"
#include "src/type/vector_type.h"
namespace tint {
namespace type {
namespace {
using SampledTextureTest = TestHelper;
TEST_F(SampledTextureTest, Is) {
F32 f32;
SampledTexture s(TextureDimension::kCube, &f32);
Type* ty = &s;
EXPECT_FALSE(ty->Is<AccessControl>());
EXPECT_FALSE(ty->Is<Alias>());
EXPECT_FALSE(ty->Is<Array>());
EXPECT_FALSE(ty->Is<Bool>());
EXPECT_FALSE(ty->Is<F32>());
EXPECT_FALSE(ty->Is<I32>());
EXPECT_FALSE(ty->Is<Matrix>());
EXPECT_FALSE(ty->Is<Pointer>());
EXPECT_FALSE(ty->Is<Sampler>());
EXPECT_FALSE(ty->Is<Struct>());
EXPECT_TRUE(ty->Is<Texture>());
EXPECT_FALSE(ty->Is<U32>());
EXPECT_FALSE(ty->Is<Vector>());
}
TEST_F(SampledTextureTest, IsTexture) {
F32 f32;
SampledTexture s(TextureDimension::kCube, &f32);
Texture* ty = &s;
EXPECT_FALSE(ty->Is<DepthTexture>());
EXPECT_TRUE(ty->Is<SampledTexture>());
EXPECT_FALSE(ty->Is<StorageTexture>());
}
TEST_F(SampledTextureTest, Dim) {
F32 f32;
SampledTexture s(TextureDimension::k3d, &f32);
EXPECT_EQ(s.dim(), TextureDimension::k3d);
}
TEST_F(SampledTextureTest, Type) {
F32 f32;
SampledTexture s(TextureDimension::k3d, &f32);
EXPECT_EQ(s.type(), &f32);
}
TEST_F(SampledTextureTest, TypeName) {
F32 f32;
SampledTexture s(TextureDimension::k3d, &f32);
EXPECT_EQ(s.type_name(), "__sampled_texture_3d__f32");
}
TEST_F(SampledTextureTest, MinBufferBindingSize) {
F32 f32;
SampledTexture s(TextureDimension::kCube, &f32);
EXPECT_EQ(0u, s.MinBufferBindingSize(MemoryLayout::kUniformBuffer));
}
} // namespace
} // namespace type
} // namespace tint

53
src/type/sampler_type.cc Normal file
View File

@@ -0,0 +1,53 @@
// Copyright 2020 The Tint 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 "src/type/sampler_type.h"
#include "src/ast/clone_context.h"
#include "src/ast/module.h"
TINT_INSTANTIATE_CLASS_ID(tint::type::Sampler);
namespace tint {
namespace type {
std::ostream& operator<<(std::ostream& out, SamplerKind kind) {
switch (kind) {
case SamplerKind::kSampler:
out << "sampler";
break;
case SamplerKind::kComparisonSampler:
out << "comparison_sampler";
break;
}
return out;
}
Sampler::Sampler(SamplerKind kind) : kind_(kind) {}
Sampler::Sampler(Sampler&&) = default;
Sampler::~Sampler() = default;
std::string Sampler::type_name() const {
return std::string("__sampler_") +
(kind_ == SamplerKind::kSampler ? "sampler" : "comparison");
}
Sampler* Sampler::Clone(CloneContext* ctx) const {
return ctx->mod->create<Sampler>(kind_);
}
} // namespace type
} // namespace tint

66
src/type/sampler_type.h Normal file
View File

@@ -0,0 +1,66 @@
// Copyright 2020 The Tint 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.
#ifndef SRC_TYPE_SAMPLER_TYPE_H_
#define SRC_TYPE_SAMPLER_TYPE_H_
#include <ostream>
#include <string>
#include "src/type/type.h"
namespace tint {
namespace type {
/// The different kinds of samplers
enum class SamplerKind {
/// A regular sampler
kSampler,
/// A comparison sampler
kComparisonSampler
};
std::ostream& operator<<(std::ostream& out, SamplerKind kind);
/// A sampler type.
class Sampler : public Castable<Sampler, Type> {
public:
/// Constructor
/// @param kind the kind of sampler
explicit Sampler(SamplerKind kind);
/// Move constructor
Sampler(Sampler&&);
~Sampler() override;
/// @returns the sampler type
SamplerKind kind() const { return kind_; }
/// @returns true if this is a comparison sampler
bool IsComparison() const { return kind_ == SamplerKind::kComparisonSampler; }
/// @returns the name for this type
std::string type_name() const override;
/// Clones this type and all transitive types using the `CloneContext` `ctx`.
/// @param ctx the clone context
/// @return the newly cloned type
Sampler* Clone(CloneContext* ctx) const override;
private:
SamplerKind const kind_;
};
} // namespace type
} // namespace tint
#endif // SRC_TYPE_SAMPLER_TYPE_H_

View File

@@ -0,0 +1,82 @@
// Copyright 2020 The Tint 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 "src/type/sampler_type.h"
#include "src/type/access_control_type.h"
#include "src/type/array_type.h"
#include "src/type/bool_type.h"
#include "src/type/f32_type.h"
#include "src/type/i32_type.h"
#include "src/type/matrix_type.h"
#include "src/type/pointer_type.h"
#include "src/type/struct_type.h"
#include "src/type/test_helper.h"
#include "src/type/texture_type.h"
#include "src/type/u32_type.h"
#include "src/type/vector_type.h"
namespace tint {
namespace type {
namespace {
using SamplerTest = TestHelper;
TEST_F(SamplerTest, Creation) {
Sampler s{SamplerKind::kSampler};
EXPECT_EQ(s.kind(), SamplerKind::kSampler);
}
TEST_F(SamplerTest, Creation_ComparisonSampler) {
Sampler s{SamplerKind::kComparisonSampler};
EXPECT_EQ(s.kind(), SamplerKind::kComparisonSampler);
EXPECT_TRUE(s.IsComparison());
}
TEST_F(SamplerTest, Is) {
Sampler s{SamplerKind::kSampler};
Type* ty = &s;
EXPECT_FALSE(ty->Is<AccessControl>());
EXPECT_FALSE(ty->Is<Alias>());
EXPECT_FALSE(ty->Is<Array>());
EXPECT_FALSE(ty->Is<Bool>());
EXPECT_FALSE(ty->Is<F32>());
EXPECT_FALSE(ty->Is<I32>());
EXPECT_FALSE(ty->Is<Matrix>());
EXPECT_FALSE(ty->Is<Pointer>());
EXPECT_TRUE(ty->Is<Sampler>());
EXPECT_FALSE(ty->Is<Struct>());
EXPECT_FALSE(ty->Is<Texture>());
EXPECT_FALSE(ty->Is<U32>());
EXPECT_FALSE(ty->Is<Vector>());
}
TEST_F(SamplerTest, TypeName_Sampler) {
Sampler s{SamplerKind::kSampler};
EXPECT_EQ(s.type_name(), "__sampler_sampler");
}
TEST_F(SamplerTest, TypeName_Comparison) {
Sampler s{SamplerKind::kComparisonSampler};
EXPECT_EQ(s.type_name(), "__sampler_comparison");
}
TEST_F(SamplerTest, MinBufferBindingSize) {
Sampler s{SamplerKind::kSampler};
EXPECT_EQ(0u, s.MinBufferBindingSize(MemoryLayout::kUniformBuffer));
}
} // namespace
} // namespace type
} // namespace tint

View File

@@ -0,0 +1,170 @@
// Copyright 2020 The Tint 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 "src/type/storage_texture_type.h"
#include <cassert>
#include <sstream>
#include "src/ast/clone_context.h"
#include "src/ast/module.h"
TINT_INSTANTIATE_CLASS_ID(tint::type::StorageTexture);
namespace tint {
namespace type {
// Note, these names match the names in the WGSL spec. This behaviour is used
// in the WGSL writer to emit the texture format names.
std::ostream& operator<<(std::ostream& out, ImageFormat format) {
switch (format) {
case ImageFormat::kNone:
out << "none";
break;
case ImageFormat::kR8Unorm:
out << "r8unorm";
break;
case ImageFormat::kR8Snorm:
out << "r8snorm";
break;
case ImageFormat::kR8Uint:
out << "r8uint";
break;
case ImageFormat::kR8Sint:
out << "r8sint";
break;
case ImageFormat::kR16Uint:
out << "r16uint";
break;
case ImageFormat::kR16Sint:
out << "r16sint";
break;
case ImageFormat::kR16Float:
out << "r16float";
break;
case ImageFormat::kRg8Unorm:
out << "rg8unorm";
break;
case ImageFormat::kRg8Snorm:
out << "rg8snorm";
break;
case ImageFormat::kRg8Uint:
out << "rg8uint";
break;
case ImageFormat::kRg8Sint:
out << "rg8sint";
break;
case ImageFormat::kR32Uint:
out << "r32uint";
break;
case ImageFormat::kR32Sint:
out << "r32sint";
break;
case ImageFormat::kR32Float:
out << "r32float";
break;
case ImageFormat::kRg16Uint:
out << "rg16uint";
break;
case ImageFormat::kRg16Sint:
out << "rg16sint";
break;
case ImageFormat::kRg16Float:
out << "rg16float";
break;
case ImageFormat::kRgba8Unorm:
out << "rgba8unorm";
break;
case ImageFormat::kRgba8UnormSrgb:
out << "rgba8unorm_srgb";
break;
case ImageFormat::kRgba8Snorm:
out << "rgba8snorm";
break;
case ImageFormat::kRgba8Uint:
out << "rgba8uint";
break;
case ImageFormat::kRgba8Sint:
out << "rgba8sint";
break;
case ImageFormat::kBgra8Unorm:
out << "bgra8unorm";
break;
case ImageFormat::kBgra8UnormSrgb:
out << "bgra8unorm_srgb";
break;
case ImageFormat::kRgb10A2Unorm:
out << "rgb10a2unorm";
break;
case ImageFormat::kRg11B10Float:
out << "rg11b10float";
break;
case ImageFormat::kRg32Uint:
out << "rg32uint";
break;
case ImageFormat::kRg32Sint:
out << "rg32sint";
break;
case ImageFormat::kRg32Float:
out << "rg32float";
break;
case ImageFormat::kRgba16Uint:
out << "rgba16uint";
break;
case ImageFormat::kRgba16Sint:
out << "rgba16sint";
break;
case ImageFormat::kRgba16Float:
out << "rgba16float";
break;
case ImageFormat::kRgba32Uint:
out << "rgba32uint";
break;
case ImageFormat::kRgba32Sint:
out << "rgba32sint";
break;
case ImageFormat::kRgba32Float:
out << "rgba32float";
break;
}
return out;
}
StorageTexture::StorageTexture(TextureDimension dim, ImageFormat format)
: Base(dim), image_format_(format) {}
void StorageTexture::set_type(Type* const type) {
type_ = type;
}
Type* StorageTexture::type() const {
return type_;
}
StorageTexture::StorageTexture(StorageTexture&&) = default;
StorageTexture::~StorageTexture() = default;
std::string StorageTexture::type_name() const {
std::ostringstream out;
out << "__storage_texture_" << dim() << "_" << image_format_;
return out.str();
}
StorageTexture* StorageTexture::Clone(CloneContext* ctx) const {
return ctx->mod->create<StorageTexture>(dim(), image_format_);
}
} // namespace type
} // namespace tint

View File

@@ -0,0 +1,104 @@
// Copyright 2020 The Tint 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.
#ifndef SRC_TYPE_STORAGE_TEXTURE_TYPE_H_
#define SRC_TYPE_STORAGE_TEXTURE_TYPE_H_
#include <string>
#include "src/type/texture_type.h"
namespace tint {
namespace type {
/// The image format in the storage texture
enum class ImageFormat {
kNone = -1,
kR8Unorm,
kR8Snorm,
kR8Uint,
kR8Sint,
kR16Uint,
kR16Sint,
kR16Float,
kRg8Unorm,
kRg8Snorm,
kRg8Uint,
kRg8Sint,
kR32Uint,
kR32Sint,
kR32Float,
kRg16Uint,
kRg16Sint,
kRg16Float,
kRgba8Unorm,
kRgba8UnormSrgb,
kRgba8Snorm,
kRgba8Uint,
kRgba8Sint,
kBgra8Unorm,
kBgra8UnormSrgb,
kRgb10A2Unorm,
kRg11B10Float,
kRg32Uint,
kRg32Sint,
kRg32Float,
kRgba16Uint,
kRgba16Sint,
kRgba16Float,
kRgba32Uint,
kRgba32Sint,
kRgba32Float,
};
std::ostream& operator<<(std::ostream& out, ImageFormat dim);
/// A storage texture type.
class StorageTexture : public Castable<StorageTexture, Texture> {
public:
/// Constructor
/// @param dim the dimensionality of the texture
/// @param format the image format of the texture
StorageTexture(TextureDimension dim, ImageFormat format);
/// Move constructor
StorageTexture(StorageTexture&&);
~StorageTexture() override;
/// @param type the subtype of the storage texture
void set_type(Type* const type);
/// @returns the subtype of the storage texture set with set_type
Type* type() const;
/// @returns the image format
ImageFormat image_format() const { return image_format_; }
/// @returns the name for this type
std::string type_name() const override;
/// Clones this type and all transitive types using the `CloneContext` `ctx`.
/// @param ctx the clone context
/// @return the newly cloned type
StorageTexture* Clone(CloneContext* ctx) const override;
private:
ImageFormat const image_format_;
Type* type_ = nullptr; // Semantic info
};
} // namespace type
} // namespace tint
#endif // SRC_TYPE_STORAGE_TEXTURE_TYPE_H_

View File

@@ -0,0 +1,122 @@
// Copyright 2020 The Tint 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 "src/type/storage_texture_type.h"
#include <memory>
#include "src/ast/identifier_expression.h"
#include "src/type/access_control_type.h"
#include "src/type/array_type.h"
#include "src/type/bool_type.h"
#include "src/type/depth_texture_type.h"
#include "src/type/f32_type.h"
#include "src/type/i32_type.h"
#include "src/type/matrix_type.h"
#include "src/type/pointer_type.h"
#include "src/type/sampled_texture_type.h"
#include "src/type/struct_type.h"
#include "src/type/test_helper.h"
#include "src/type/u32_type.h"
#include "src/type/vector_type.h"
#include "src/type_determiner.h"
namespace tint {
namespace type {
namespace {
using StorageTextureTest = TestHelper;
TEST_F(StorageTextureTest, Is) {
StorageTexture s(TextureDimension::k2dArray, ImageFormat::kRgba32Float);
Type* ty = &s;
EXPECT_FALSE(ty->Is<AccessControl>());
EXPECT_FALSE(ty->Is<Alias>());
EXPECT_FALSE(ty->Is<Array>());
EXPECT_FALSE(ty->Is<Bool>());
EXPECT_FALSE(ty->Is<F32>());
EXPECT_FALSE(ty->Is<I32>());
EXPECT_FALSE(ty->Is<Matrix>());
EXPECT_FALSE(ty->Is<Pointer>());
EXPECT_FALSE(ty->Is<Sampler>());
EXPECT_FALSE(ty->Is<Struct>());
EXPECT_TRUE(ty->Is<Texture>());
EXPECT_FALSE(ty->Is<U32>());
EXPECT_FALSE(ty->Is<Vector>());
}
TEST_F(StorageTextureTest, IsTexture) {
StorageTexture s(TextureDimension::k2dArray, ImageFormat::kRgba32Float);
Texture* ty = &s;
EXPECT_FALSE(ty->Is<DepthTexture>());
EXPECT_FALSE(ty->Is<SampledTexture>());
EXPECT_TRUE(ty->Is<StorageTexture>());
}
TEST_F(StorageTextureTest, Dim) {
StorageTexture s(TextureDimension::k2dArray, ImageFormat::kRgba32Float);
EXPECT_EQ(s.dim(), TextureDimension::k2dArray);
}
TEST_F(StorageTextureTest, Format) {
StorageTexture s(TextureDimension::k2dArray, ImageFormat::kRgba32Float);
EXPECT_EQ(s.image_format(), ImageFormat::kRgba32Float);
}
TEST_F(StorageTextureTest, TypeName) {
StorageTexture s(TextureDimension::k2dArray, ImageFormat::kRgba32Float);
EXPECT_EQ(s.type_name(), "__storage_texture_2d_array_rgba32float");
}
TEST_F(StorageTextureTest, F32) {
Type* s = mod->create<StorageTexture>(TextureDimension::k2dArray,
ImageFormat::kRgba32Float);
TypeDeterminer td(mod);
ASSERT_TRUE(td.Determine()) << td.error();
ASSERT_TRUE(s->Is<Texture>());
ASSERT_TRUE(s->Is<StorageTexture>());
EXPECT_TRUE(s->As<StorageTexture>()->type()->Is<F32>());
}
TEST_F(StorageTextureTest, U32) {
Type* s = mod->create<StorageTexture>(TextureDimension::k2dArray,
ImageFormat::kRg32Uint);
TypeDeterminer td(mod);
ASSERT_TRUE(td.Determine()) << td.error();
ASSERT_TRUE(s->Is<Texture>());
ASSERT_TRUE(s->Is<StorageTexture>());
EXPECT_TRUE(s->As<StorageTexture>()->type()->Is<U32>());
}
TEST_F(StorageTextureTest, I32) {
Type* s = mod->create<StorageTexture>(TextureDimension::k2dArray,
ImageFormat::kRgba32Sint);
TypeDeterminer td(mod);
ASSERT_TRUE(td.Determine()) << td.error();
ASSERT_TRUE(s->Is<Texture>());
ASSERT_TRUE(s->Is<StorageTexture>());
EXPECT_TRUE(s->As<StorageTexture>()->type()->Is<I32>());
}
TEST_F(StorageTextureTest, MinBufferBindingSize) {
StorageTexture s(TextureDimension::k2dArray, ImageFormat::kRgba32Sint);
EXPECT_EQ(0u, s.MinBufferBindingSize(MemoryLayout::kUniformBuffer));
}
} // namespace
} // namespace type
} // namespace tint

90
src/type/struct_type.cc Normal file
View File

@@ -0,0 +1,90 @@
// Copyright 2020 The Tint 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 "src/type/struct_type.h"
#include <cmath>
#include <utility>
#include "src/ast/clone_context.h"
#include "src/ast/module.h"
#include "src/type/alias_type.h"
#include "src/type/array_type.h"
#include "src/type/matrix_type.h"
#include "src/type/vector_type.h"
TINT_INSTANTIATE_CLASS_ID(tint::type::Struct);
namespace tint {
namespace type {
Struct::Struct(const Symbol& sym, ast::Struct* impl)
: symbol_(sym), struct_(impl) {}
Struct::Struct(Struct&&) = default;
Struct::~Struct() = default;
std::string Struct::type_name() const {
return "__struct_" + symbol_.to_str();
}
uint64_t Struct::MinBufferBindingSize(MemoryLayout mem_layout) const {
if (!struct_->members().size()) {
return 0;
}
auto* last_member = struct_->members().back();
// If there is no offset, then this is not a host-shareable struct, returning
// 0 indicates this to the caller.
if (!last_member->has_offset_decoration()) {
return 0;
}
uint64_t size = last_member->type()->MinBufferBindingSize(mem_layout);
if (!size) {
return 0;
}
float unaligned = static_cast<float>(last_member->offset() + size);
float alignment = static_cast<float>(BaseAlignment(mem_layout));
return static_cast<uint64_t>(alignment * std::ceil(unaligned / alignment));
}
uint64_t Struct::BaseAlignment(MemoryLayout mem_layout) const {
uint64_t max = 0;
for (auto* member : struct_->members()) {
if (member->type()->BaseAlignment(mem_layout) > max) {
max = member->type()->BaseAlignment(mem_layout);
}
}
if (mem_layout == MemoryLayout::kUniformBuffer) {
// Round up to a vec4.
return static_cast<uint64_t>(16 *
std::ceil(static_cast<float>(max) / 16.0f));
} else if (mem_layout == MemoryLayout::kStorageBuffer) {
return max;
}
return 0;
}
Struct* Struct::Clone(CloneContext* ctx) const {
return ctx->mod->create<Struct>(ctx->Clone(symbol()), ctx->Clone(struct_));
}
} // namespace type
} // namespace tint

76
src/type/struct_type.h Normal file
View File

@@ -0,0 +1,76 @@
// Copyright 2020 The Tint 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.
#ifndef SRC_TYPE_STRUCT_TYPE_H_
#define SRC_TYPE_STRUCT_TYPE_H_
#include <memory>
#include <string>
#include "src/ast/struct.h"
#include "src/symbol.h"
#include "src/type/type.h"
namespace tint {
namespace type {
/// A structure type
class Struct : public Castable<Struct, Type> {
public:
/// Constructor
/// @param sym the symbol representing the struct
/// @param impl the struct data
Struct(const Symbol& sym, ast::Struct* impl);
/// Move constructor
Struct(Struct&&);
~Struct() override;
/// @returns the struct symbol
const Symbol& symbol() const { return symbol_; }
/// @returns true if the struct has a block decoration
bool IsBlockDecorated() const { return struct_->IsBlockDecorated(); }
/// @returns the struct
ast::Struct* impl() const { return struct_; }
/// @returns the name for the type
std::string type_name() const override;
/// @param mem_layout type of memory layout to use in calculation.
/// @returns minimum size required for this type, in bytes.
/// 0 for non-host shareable types.
uint64_t MinBufferBindingSize(MemoryLayout mem_layout) const override;
/// @param mem_layout type of memory layout to use in calculation.
/// @returns base alignment for the type, in bytes.
/// 0 for non-host shareable types.
uint64_t BaseAlignment(MemoryLayout mem_layout) const override;
/// Clones this type and all transitive types using the `CloneContext` `ctx`.
/// @param ctx the clone context
/// @return the newly cloned type
Struct* Clone(CloneContext* ctx) const override;
private:
Symbol const symbol_;
ast::Struct* const struct_;
uint64_t LargestMemberBaseAlignment(MemoryLayout mem_layout) const;
};
} // namespace type
} // namespace tint
#endif // SRC_TYPE_STRUCT_TYPE_H_

View File

@@ -0,0 +1,216 @@
// Copyright 2020 The Tint 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 "src/type/struct_type.h"
#include <utility>
#include "src/ast/stride_decoration.h"
#include "src/ast/struct_member.h"
#include "src/ast/struct_member_decoration.h"
#include "src/ast/struct_member_offset_decoration.h"
#include "src/type/access_control_type.h"
#include "src/type/array_type.h"
#include "src/type/bool_type.h"
#include "src/type/f32_type.h"
#include "src/type/i32_type.h"
#include "src/type/matrix_type.h"
#include "src/type/pointer_type.h"
#include "src/type/test_helper.h"
#include "src/type/texture_type.h"
#include "src/type/u32_type.h"
#include "src/type/vector_type.h"
namespace tint {
namespace type {
namespace {
using StructTypeTest = TestHelper;
TEST_F(StructTypeTest, Creation) {
auto* impl =
create<ast::Struct>(ast::StructMemberList{}, ast::StructDecorationList{});
auto* ptr = impl;
auto* s = ty.struct_("S", impl);
EXPECT_EQ(s->impl(), ptr);
}
TEST_F(StructTypeTest, Is) {
auto* impl =
create<ast::Struct>(ast::StructMemberList{}, ast::StructDecorationList{});
auto* s = ty.struct_("S", impl);
type::Type* ty = s;
EXPECT_FALSE(ty->Is<AccessControl>());
EXPECT_FALSE(ty->Is<Alias>());
EXPECT_FALSE(ty->Is<Array>());
EXPECT_FALSE(ty->Is<Bool>());
EXPECT_FALSE(ty->Is<F32>());
EXPECT_FALSE(ty->Is<I32>());
EXPECT_FALSE(ty->Is<Matrix>());
EXPECT_FALSE(ty->Is<Pointer>());
EXPECT_FALSE(ty->Is<Sampler>());
EXPECT_TRUE(ty->Is<Struct>());
EXPECT_FALSE(ty->Is<Texture>());
EXPECT_FALSE(ty->Is<U32>());
EXPECT_FALSE(ty->Is<Vector>());
}
TEST_F(StructTypeTest, TypeName) {
auto* impl =
create<ast::Struct>(ast::StructMemberList{}, ast::StructDecorationList{});
auto* s = ty.struct_("my_struct", impl);
EXPECT_EQ(s->type_name(), "__struct_tint_symbol_1");
}
TEST_F(StructTypeTest, MinBufferBindingSize) {
auto* str = create<ast::Struct>(
ast::StructMemberList{Member("foo", ty.u32, {MemberOffset(0)}),
Member("bar", ty.u32, {MemberOffset(4)})},
ast::StructDecorationList{});
auto* s_ty = ty.struct_("s_ty", str);
EXPECT_EQ(16u, s_ty->MinBufferBindingSize(MemoryLayout::kUniformBuffer));
EXPECT_EQ(8u, s_ty->MinBufferBindingSize(MemoryLayout::kStorageBuffer));
}
TEST_F(StructTypeTest, MinBufferBindingSizeArray) {
Array arr(ty.u32, 4,
ast::ArrayDecorationList{create<ast::StrideDecoration>(4)});
auto* str = create<ast::Struct>(
ast::StructMemberList{Member("foo", ty.u32, {MemberOffset(0)}),
Member("bar", ty.u32, {MemberOffset(4)}),
Member("bar", &arr, {MemberOffset(8)})},
ast::StructDecorationList{});
auto* s_ty = ty.struct_("s_ty", str);
EXPECT_EQ(32u, s_ty->MinBufferBindingSize(MemoryLayout::kUniformBuffer));
EXPECT_EQ(24u, s_ty->MinBufferBindingSize(MemoryLayout::kStorageBuffer));
}
TEST_F(StructTypeTest, MinBufferBindingSizeRuntimeArray) {
Array arr(ty.u32, 0,
ast::ArrayDecorationList{create<ast::StrideDecoration>(4)});
auto* str = create<ast::Struct>(
ast::StructMemberList{Member("foo", ty.u32, {MemberOffset(0)}),
Member("bar", ty.u32, {MemberOffset(4)}),
Member("bar", ty.u32, {MemberOffset(8)})},
ast::StructDecorationList{});
auto* s_ty = ty.struct_("s_ty", str);
EXPECT_EQ(12u, s_ty->MinBufferBindingSize(MemoryLayout::kStorageBuffer));
}
TEST_F(StructTypeTest, MinBufferBindingSizeVec2) {
auto* str = create<ast::Struct>(
ast::StructMemberList{Member("foo", ty.vec2<u32>(), {MemberOffset(0)})},
ast::StructDecorationList{});
auto* s_ty = ty.struct_("s_ty", str);
EXPECT_EQ(16u, s_ty->MinBufferBindingSize(MemoryLayout::kUniformBuffer));
EXPECT_EQ(8u, s_ty->MinBufferBindingSize(MemoryLayout::kStorageBuffer));
}
TEST_F(StructTypeTest, MinBufferBindingSizeVec3) {
auto* str = create<ast::Struct>(
ast::StructMemberList{Member("foo", ty.vec3<u32>(), {MemberOffset(0)})},
ast::StructDecorationList{});
auto* s_ty = ty.struct_("s_ty", str);
EXPECT_EQ(16u, s_ty->MinBufferBindingSize(MemoryLayout::kUniformBuffer));
EXPECT_EQ(16u, s_ty->MinBufferBindingSize(MemoryLayout::kStorageBuffer));
}
TEST_F(StructTypeTest, MinBufferBindingSizeVec4) {
auto* str = create<ast::Struct>(
ast::StructMemberList{Member("foo", ty.vec4<u32>(), {MemberOffset(0)})},
ast::StructDecorationList{});
auto* s_ty = ty.struct_("s_ty", str);
EXPECT_EQ(16u, s_ty->MinBufferBindingSize(MemoryLayout::kUniformBuffer));
EXPECT_EQ(16u, s_ty->MinBufferBindingSize(MemoryLayout::kStorageBuffer));
}
TEST_F(StructTypeTest, BaseAlignment) {
auto* str = create<ast::Struct>(
ast::StructMemberList{Member("foo", ty.u32, {MemberOffset(0)}),
Member("bar", ty.u32, {MemberOffset(8)})},
ast::StructDecorationList{});
auto* s_ty = ty.struct_("s_ty", str);
EXPECT_EQ(16u, s_ty->BaseAlignment(MemoryLayout::kUniformBuffer));
EXPECT_EQ(4u, s_ty->BaseAlignment(MemoryLayout::kStorageBuffer));
}
TEST_F(StructTypeTest, BaseAlignmentArray) {
Array arr(ty.u32, 4,
ast::ArrayDecorationList{create<ast::StrideDecoration>(4)});
auto* str = create<ast::Struct>(
ast::StructMemberList{Member("foo", ty.u32, {MemberOffset(0)}),
Member("bar", ty.u32, {MemberOffset(4)}),
Member("bar", &arr, {MemberOffset(8)})},
ast::StructDecorationList{});
auto* s_ty = ty.struct_("s_ty", str);
EXPECT_EQ(16u, s_ty->BaseAlignment(MemoryLayout::kUniformBuffer));
EXPECT_EQ(4u, s_ty->BaseAlignment(MemoryLayout::kStorageBuffer));
}
TEST_F(StructTypeTest, BaseAlignmentRuntimeArray) {
Array arr(ty.u32, 0,
ast::ArrayDecorationList{create<ast::StrideDecoration>(4)});
auto* str = create<ast::Struct>(
ast::StructMemberList{Member("foo", ty.u32, {MemberOffset(0)}),
Member("bar", ty.u32, {MemberOffset(4)}),
Member("bar", ty.u32, {MemberOffset(8)})},
ast::StructDecorationList{});
auto* s_ty = ty.struct_("s_ty", str);
EXPECT_EQ(4u, s_ty->BaseAlignment(MemoryLayout::kStorageBuffer));
}
TEST_F(StructTypeTest, BaseAlignmentVec2) {
auto* str = create<ast::Struct>(
ast::StructMemberList{Member("foo", ty.vec2<u32>(), {MemberOffset(0)})},
ast::StructDecorationList{});
auto* s_ty = ty.struct_("s_ty", str);
EXPECT_EQ(16u, s_ty->BaseAlignment(MemoryLayout::kUniformBuffer));
EXPECT_EQ(8u, s_ty->BaseAlignment(MemoryLayout::kStorageBuffer));
}
TEST_F(StructTypeTest, BaseAlignmentVec3) {
auto* str = create<ast::Struct>(
ast::StructMemberList{Member("foo", ty.vec3<u32>(), {MemberOffset(0)})},
ast::StructDecorationList{});
auto* s_ty = ty.struct_("s_ty", str);
EXPECT_EQ(16u, s_ty->BaseAlignment(MemoryLayout::kUniformBuffer));
EXPECT_EQ(16u, s_ty->BaseAlignment(MemoryLayout::kStorageBuffer));
}
TEST_F(StructTypeTest, BaseAlignmentVec4) {
auto* str = create<ast::Struct>(
ast::StructMemberList{Member("foo", ty.vec4<u32>(), {MemberOffset(0)})},
ast::StructDecorationList{});
auto* s_ty = ty.struct_("s_ty", str);
EXPECT_EQ(16u, s_ty->BaseAlignment(MemoryLayout::kUniformBuffer));
EXPECT_EQ(16u, s_ty->BaseAlignment(MemoryLayout::kStorageBuffer));
}
} // namespace
} // namespace type
} // namespace tint

52
src/type/test_helper.h Normal file
View File

@@ -0,0 +1,52 @@
// Copyright 2020 The Tint 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.
#ifndef SRC_TYPE_TEST_HELPER_H_
#define SRC_TYPE_TEST_HELPER_H_
#include <memory>
#include <string>
#include <utility>
#include "gtest/gtest.h"
#include "src/ast/builder.h"
#include "src/ast/module.h"
#include "src/demangler.h"
namespace tint {
namespace type {
/// Helper class for testing
template <typename BASE>
class TestHelperBase : public BASE, public ast::BuilderWithModule {
public:
/// Demangles the given string
/// @param s the string to demangle
/// @returns the demangled string
std::string demangle(const std::string& s) {
return demanger.Demangle(*mod, s);
}
/// A demangler
Demangler demanger;
};
using TestHelper = TestHelperBase<testing::Test>;
template <typename T>
using TestParamHelper = TestHelperBase<testing::TestWithParam<T>>;
} // namespace type
} // namespace tint
#endif // SRC_TYPE_TEST_HELPER_H_

99
src/type/texture_type.cc Normal file
View File

@@ -0,0 +1,99 @@
// Copyright 2020 The Tint 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 "src/type/texture_type.h"
#include <cassert>
#include <ostream>
#include "src/type/multisampled_texture_type.h"
#include "src/type/sampled_texture_type.h"
TINT_INSTANTIATE_CLASS_ID(tint::type::Texture);
namespace tint {
namespace type {
std::ostream& operator<<(std::ostream& out, TextureDimension dim) {
switch (dim) {
case TextureDimension::kNone:
out << "None";
break;
case TextureDimension::k1d:
out << "1d";
break;
case TextureDimension::k1dArray:
out << "1d_array";
break;
case TextureDimension::k2d:
out << "2d";
break;
case TextureDimension::k2dArray:
out << "2d_array";
break;
case TextureDimension::k3d:
out << "3d";
break;
case TextureDimension::kCube:
out << "cube";
break;
case TextureDimension::kCubeArray:
out << "cube_array";
break;
}
return out;
}
bool IsTextureArray(TextureDimension dim) {
switch (dim) {
case TextureDimension::k1dArray:
case TextureDimension::k2dArray:
case TextureDimension::kCubeArray:
return true;
case TextureDimension::k2d:
case TextureDimension::kNone:
case TextureDimension::k1d:
case TextureDimension::k3d:
case TextureDimension::kCube:
return false;
}
return false;
}
int NumCoordinateAxes(TextureDimension dim) {
switch (dim) {
case TextureDimension::kNone:
return 0;
case TextureDimension::k1d:
case TextureDimension::k1dArray:
return 1;
case TextureDimension::k2d:
case TextureDimension::k2dArray:
return 2;
case TextureDimension::k3d:
case TextureDimension::kCube:
case TextureDimension::kCubeArray:
return 3;
}
return 0;
}
Texture::Texture(TextureDimension dim) : dim_(dim) {}
Texture::Texture(Texture&&) = default;
Texture::~Texture() = default;
} // namespace type
} // namespace tint

77
src/type/texture_type.h Normal file
View File

@@ -0,0 +1,77 @@
// Copyright 2020 The Tint 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.
#ifndef SRC_TYPE_TEXTURE_TYPE_H_
#define SRC_TYPE_TEXTURE_TYPE_H_
#include "src/type/type.h"
namespace tint {
namespace type {
/// The dimensionality of the texture
enum class TextureDimension {
/// Invalid texture
kNone = -1,
/// 1 dimensional texture
k1d,
/// 1 dimenstional array texture
k1dArray,
/// 2 dimensional texture
k2d,
/// 2 dimensional array texture
k2dArray,
/// 3 dimensional texture
k3d,
/// cube texture
kCube,
/// cube array texture
kCubeArray,
};
std::ostream& operator<<(std::ostream& out, TextureDimension dim);
/// @param dim the TextureDimension to query
/// @return true if the given TextureDimension is an array texture
bool IsTextureArray(TextureDimension dim);
/// Returns the number of axes in the coordinate for a dimensionality.
/// None -> 0
/// 1D, 1DArray -> 1
/// 2D, 2DArray -> 2
/// 3D, Cube, CubeArray -> 3
/// @param dim the TextureDimension to query
/// @return number of dimensions in a coordinate for the dimensionality
int NumCoordinateAxes(TextureDimension dim);
/// A texture type.
class Texture : public Castable<Texture, Type> {
public:
/// Constructor
/// @param dim the dimensionality of the texture
explicit Texture(TextureDimension dim);
/// Move constructor
Texture(Texture&&);
~Texture() override;
/// @returns the texture dimension
TextureDimension dim() const { return dim_; }
private:
TextureDimension const dim_;
};
} // namespace type
} // namespace tint
#endif // SRC_TYPE_TEXTURE_TYPE_H_

View File

@@ -0,0 +1,49 @@
// Copyright 2021 The Tint 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 "src/type/texture_type.h"
#include "src/type/test_helper.h"
namespace tint {
namespace type {
namespace {
using TextureTypeTest = TestHelper;
TEST_F(TextureTypeTest, IsTextureArray) {
EXPECT_EQ(false, IsTextureArray(TextureDimension::kNone));
EXPECT_EQ(false, IsTextureArray(TextureDimension::k1d));
EXPECT_EQ(true, IsTextureArray(TextureDimension::k1dArray));
EXPECT_EQ(false, IsTextureArray(TextureDimension::k2d));
EXPECT_EQ(true, IsTextureArray(TextureDimension::k2dArray));
EXPECT_EQ(false, IsTextureArray(TextureDimension::k3d));
EXPECT_EQ(false, IsTextureArray(TextureDimension::kCube));
EXPECT_EQ(true, IsTextureArray(TextureDimension::kCubeArray));
}
TEST_F(TextureTypeTest, NumCoordinateAxes) {
EXPECT_EQ(0, NumCoordinateAxes(TextureDimension::kNone));
EXPECT_EQ(1, NumCoordinateAxes(TextureDimension::k1d));
EXPECT_EQ(1, NumCoordinateAxes(TextureDimension::k1dArray));
EXPECT_EQ(2, NumCoordinateAxes(TextureDimension::k2d));
EXPECT_EQ(2, NumCoordinateAxes(TextureDimension::k2dArray));
EXPECT_EQ(3, NumCoordinateAxes(TextureDimension::k3d));
EXPECT_EQ(3, NumCoordinateAxes(TextureDimension::kCube));
EXPECT_EQ(3, NumCoordinateAxes(TextureDimension::kCubeArray));
}
} // namespace
} // namespace type
} // namespace tint

123
src/type/type.cc Normal file
View File

@@ -0,0 +1,123 @@
// Copyright 2020 The Tint 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 "src/type/type.h"
#include <assert.h>
#include "src/type/access_control_type.h"
#include "src/type/alias_type.h"
#include "src/type/array_type.h"
#include "src/type/bool_type.h"
#include "src/type/f32_type.h"
#include "src/type/i32_type.h"
#include "src/type/matrix_type.h"
#include "src/type/pointer_type.h"
#include "src/type/sampler_type.h"
#include "src/type/struct_type.h"
#include "src/type/texture_type.h"
#include "src/type/u32_type.h"
#include "src/type/vector_type.h"
#include "src/type/void_type.h"
TINT_INSTANTIATE_CLASS_ID(tint::type::Type);
namespace tint {
namespace type {
Type::Type() = default;
Type::Type(Type&&) = default;
Type::~Type() = default;
Type* Type::UnwrapPtrIfNeeded() {
if (auto* ptr = As<type::Pointer>()) {
return ptr->type();
}
return this;
}
Type* Type::UnwrapIfNeeded() {
auto* where = this;
while (true) {
if (auto* alias = where->As<type::Alias>()) {
where = alias->type();
} else if (auto* access = where->As<type::AccessControl>()) {
where = access->type();
} else {
break;
}
}
return where;
}
Type* Type::UnwrapAll() {
return UnwrapIfNeeded()->UnwrapPtrIfNeeded()->UnwrapIfNeeded();
}
uint64_t Type::MinBufferBindingSize(MemoryLayout) const {
return 0;
}
uint64_t Type::BaseAlignment(MemoryLayout) const {
return 0;
}
bool Type::is_scalar() {
return is_float_scalar() || is_integer_scalar() || Is<Bool>();
}
bool Type::is_float_scalar() {
return Is<F32>();
}
bool Type::is_float_matrix() {
return Is<Matrix>() && As<Matrix>()->type()->is_float_scalar();
}
bool Type::is_float_vector() {
return Is<Vector>() && As<Vector>()->type()->is_float_scalar();
}
bool Type::is_float_scalar_or_vector() {
return is_float_scalar() || is_float_vector();
}
bool Type::is_integer_scalar() {
return Is<U32>() || Is<I32>();
}
bool Type::is_unsigned_integer_vector() {
return Is<Vector>() && As<Vector>()->type()->Is<U32>();
}
bool Type::is_signed_integer_vector() {
return Is<Vector>() && As<Vector>()->type()->Is<I32>();
}
bool Type::is_unsigned_scalar_or_vector() {
return Is<U32>() || (Is<Vector>() && As<Vector>()->type()->Is<U32>());
}
bool Type::is_signed_scalar_or_vector() {
return Is<I32>() || (Is<Vector>() && As<Vector>()->type()->Is<I32>());
}
bool Type::is_integer_scalar_or_vector() {
return is_unsigned_scalar_or_vector() || is_signed_scalar_or_vector();
}
} // namespace type
} // namespace tint

119
src/type/type.h Normal file
View File

@@ -0,0 +1,119 @@
// Copyright 2020 The Tint 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.
#ifndef SRC_TYPE_TYPE_H_
#define SRC_TYPE_TYPE_H_
#include <string>
#include "src/castable.h"
namespace tint {
namespace ast {
class Module;
class CloneContext;
} // namespace ast
namespace type {
using CloneContext = ast::CloneContext; // TEMP
/// Supported memory layouts for calculating sizes
enum class MemoryLayout { kUniformBuffer, kStorageBuffer };
/// Base class for a type in the system
class Type : public Castable<Type> {
public:
/// Move constructor
Type(Type&&);
~Type() override;
/// Clones this type and all transitive types using the `CloneContext` `ctx`.
/// @param ctx the clone context
/// @return the newly cloned type
virtual Type* Clone(ast::CloneContext* ctx) const = 0;
/// @returns the name for this type. The type name is unique over all types.
virtual std::string type_name() const = 0;
/// @param mem_layout type of memory layout to use in calculation.
/// @returns minimum size required for this type, in bytes.
/// 0 for non-host shareable types.
virtual uint64_t MinBufferBindingSize(MemoryLayout mem_layout) const;
/// @param mem_layout type of memory layout to use in calculation.
/// @returns base alignment for the type, in bytes.
/// 0 for non-host shareable types.
virtual uint64_t BaseAlignment(MemoryLayout mem_layout) const;
/// @returns the pointee type if this is a pointer, `this` otherwise
Type* UnwrapPtrIfNeeded();
/// Removes all levels of aliasing and access control.
/// This is just enough to assist with WGSL translation
/// in that you want see through one level of pointer to get from an
/// identifier-like expression as an l-value to its corresponding r-value,
/// plus see through the wrappers on either side.
/// @returns the completely unaliased type.
Type* UnwrapIfNeeded();
/// Returns the type found after:
/// - removing all layers of aliasing and access control if they exist, then
/// - removing the pointer, if it exists, then
/// - removing all further layers of aliasing or access control, if they exist
/// @returns the unwrapped type
Type* UnwrapAll();
/// @returns true if this type is a scalar
bool is_scalar();
/// @returns true if this type is a float scalar
bool is_float_scalar();
/// @returns true if this type is a float matrix
bool is_float_matrix();
/// @returns true if this type is a float vector
bool is_float_vector();
/// @returns true if this type is a float scalar or vector
bool is_float_scalar_or_vector();
/// @returns ture if this type is an integer scalar
bool is_integer_scalar();
/// @returns true if this type is a signed integer vector
bool is_signed_integer_vector();
/// @returns true if this type is an unsigned vector
bool is_unsigned_integer_vector();
/// @returns true if this type is an unsigned scalar or vector
bool is_unsigned_scalar_or_vector();
/// @returns true if this type is a signed scalar or vector
bool is_signed_scalar_or_vector();
/// @returns true if this type is an integer scalar or vector
bool is_integer_scalar_or_vector();
protected:
Type();
/// A helper method for cloning the `Type` `t` if it is not null.
/// If `t` is null, then `Clone()` returns null.
/// @param m the module to clone `n` into
/// @param t the `Type` to clone (if not null)
/// @return the cloned type
template <typename T>
static T* Clone(ast::Module* m, const T* t) {
return (t != nullptr) ? static_cast<T*>(t->Clone(m)) : nullptr;
}
};
} // namespace type
} // namespace tint
#endif // SRC_TYPE_TYPE_H_

41
src/type/type_manager.cc Normal file
View File

@@ -0,0 +1,41 @@
// Copyright 2020 The Tint 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 "src/type/type_manager.h"
#include <utility>
namespace tint {
namespace type {
Manager::Manager() = default;
Manager::Manager(Manager&&) = default;
Manager& Manager::operator=(Manager&& rhs) = default;
Manager::~Manager() = default;
void Manager::Reset() {
types_.clear();
}
type::Type* Manager::Get(std::unique_ptr<type::Type> type) {
auto name = type->type_name();
if (types_.find(name) == types_.end()) {
types_[name] = std::move(type);
}
return types_.find(name)->second.get();
}
} // namespace type
} // namespace tint

75
src/type/type_manager.h Normal file
View File

@@ -0,0 +1,75 @@
// Copyright 2020 The Tint 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.
#ifndef SRC_TYPE_TYPE_MANAGER_H_
#define SRC_TYPE_TYPE_MANAGER_H_
#include <memory>
#include <string>
#include <unordered_map>
#include <utility>
#include "src/type/type.h"
namespace tint {
namespace type {
/// The type manager holds all the pointers to the known types.
class Manager {
public:
/// Constructor
Manager();
/// Move constructor
Manager(Manager&&);
/// Move assignment operator
/// @param rhs the Manager to move
/// @return this Manager
Manager& operator=(Manager&& rhs);
/// Destructor
~Manager();
/// Clears all registered types.
void Reset();
/// Get the given type from the type manager
/// @param type The type to register
/// @return the pointer to the registered type
type::Type* Get(std::unique_ptr<type::Type> type);
/// Get the given type `T` from the type manager
/// @param args the arguments to pass to the type constructor
/// @return the pointer to the registered type
template <typename T, typename... ARGS>
T* Get(ARGS&&... args) {
auto ty = Get(std::make_unique<T>(std::forward<ARGS>(args)...));
return static_cast<T*>(ty);
}
/// Returns the type map
/// @returns the mapping from name string to type.
const std::unordered_map<std::string, std::unique_ptr<type::Type>>& types() {
return types_;
}
private:
std::unordered_map<std::string, std::unique_ptr<type::Type>> types_;
};
} // namespace type
} // namespace tint
#endif // SRC_TYPE_TYPE_MANAGER_H_

View File

@@ -0,0 +1,71 @@
// Copyright 2020 The Tint 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 "src/type/type_manager.h"
#include "gtest/gtest.h"
#include "src/type/i32_type.h"
#include "src/type/u32_type.h"
namespace tint {
namespace type {
namespace {
using TypeManagerTest = testing::Test;
TEST_F(TypeManagerTest, GetUnregistered) {
Manager tm;
auto* t = tm.Get(std::make_unique<I32>());
ASSERT_NE(t, nullptr);
EXPECT_TRUE(t->Is<I32>());
}
TEST_F(TypeManagerTest, GetSameTypeReturnsSamePtr) {
Manager tm;
auto* t = tm.Get(std::make_unique<I32>());
ASSERT_NE(t, nullptr);
EXPECT_TRUE(t->Is<I32>());
auto* t2 = tm.Get(std::make_unique<I32>());
EXPECT_EQ(t, t2);
}
TEST_F(TypeManagerTest, GetDifferentTypeReturnsDifferentPtr) {
Manager tm;
auto* t = tm.Get(std::make_unique<I32>());
ASSERT_NE(t, nullptr);
EXPECT_TRUE(t->Is<I32>());
auto* t2 = tm.Get(std::make_unique<U32>());
ASSERT_NE(t2, nullptr);
EXPECT_NE(t, t2);
EXPECT_TRUE(t2->Is<U32>());
}
TEST_F(TypeManagerTest, ResetClearsPreviousData) {
Manager tm;
auto* t = tm.Get(std::make_unique<I32>());
ASSERT_NE(t, nullptr);
EXPECT_FALSE(tm.types().empty());
tm.Reset();
EXPECT_TRUE(tm.types().empty());
auto* t2 = tm.Get(std::make_unique<I32>());
ASSERT_NE(t2, nullptr);
}
} // namespace
} // namespace type
} // namespace tint

48
src/type/u32_type.cc Normal file
View File

@@ -0,0 +1,48 @@
// Copyright 2020 The Tint 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 "src/type/u32_type.h"
#include "src/ast/clone_context.h"
#include "src/ast/module.h"
TINT_INSTANTIATE_CLASS_ID(tint::type::U32);
namespace tint {
namespace type {
U32::U32() = default;
U32::~U32() = default;
U32::U32(U32&&) = default;
std::string U32::type_name() const {
return "__u32";
}
uint64_t U32::MinBufferBindingSize(MemoryLayout) const {
return 4;
}
uint64_t U32::BaseAlignment(MemoryLayout) const {
return 4;
}
U32* U32::Clone(CloneContext* ctx) const {
return ctx->mod->create<U32>();
}
} // namespace type
} // namespace tint

56
src/type/u32_type.h Normal file
View File

@@ -0,0 +1,56 @@
// Copyright 2020 The Tint 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.
#ifndef SRC_TYPE_U32_TYPE_H_
#define SRC_TYPE_U32_TYPE_H_
#include <string>
#include "src/type/type.h"
namespace tint {
namespace type {
/// A unsigned int 32 type.
class U32 : public Castable<U32, Type> {
public:
/// Constructor
U32();
/// Move constructor
U32(U32&&);
~U32() override;
/// @returns the name for th type
std::string type_name() const override;
/// @param mem_layout type of memory layout to use in calculation.
/// @returns minimum size required for this type, in bytes.
/// 0 for non-host shareable types.
uint64_t MinBufferBindingSize(MemoryLayout mem_layout) const override;
/// @param mem_layout type of memory layout to use in calculation.
/// @returns base alignment for the type, in bytes.
/// 0 for non-host shareable types.
uint64_t BaseAlignment(MemoryLayout mem_layout) const override;
/// Clones this type and all transitive types using the `CloneContext` `ctx`.
/// @param ctx the clone context
/// @return the newly cloned type
U32* Clone(CloneContext* ctx) const override;
};
} // namespace type
} // namespace tint
#endif // SRC_TYPE_U32_TYPE_H_

71
src/type/u32_type_test.cc Normal file
View File

@@ -0,0 +1,71 @@
// Copyright 2020 The Tint 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 "src/type/u32_type.h"
#include "src/type/access_control_type.h"
#include "src/type/array_type.h"
#include "src/type/bool_type.h"
#include "src/type/f32_type.h"
#include "src/type/i32_type.h"
#include "src/type/matrix_type.h"
#include "src/type/pointer_type.h"
#include "src/type/sampler_type.h"
#include "src/type/struct_type.h"
#include "src/type/test_helper.h"
#include "src/type/texture_type.h"
#include "src/type/vector_type.h"
namespace tint {
namespace type {
namespace {
using U32Test = TestHelper;
TEST_F(U32Test, Is) {
U32 u;
Type* ty = &u;
EXPECT_FALSE(ty->Is<AccessControl>());
EXPECT_FALSE(ty->Is<Alias>());
EXPECT_FALSE(ty->Is<Array>());
EXPECT_FALSE(ty->Is<Bool>());
EXPECT_FALSE(ty->Is<F32>());
EXPECT_FALSE(ty->Is<I32>());
EXPECT_FALSE(ty->Is<Matrix>());
EXPECT_FALSE(ty->Is<Pointer>());
EXPECT_FALSE(ty->Is<Sampler>());
EXPECT_FALSE(ty->Is<Struct>());
EXPECT_FALSE(ty->Is<Texture>());
EXPECT_TRUE(ty->Is<U32>());
EXPECT_FALSE(ty->Is<Vector>());
}
TEST_F(U32Test, TypeName) {
U32 u;
EXPECT_EQ(u.type_name(), "__u32");
}
TEST_F(U32Test, MinBufferBindingSize) {
U32 u;
EXPECT_EQ(4u, u.MinBufferBindingSize(MemoryLayout::kUniformBuffer));
}
TEST_F(U32Test, BaseAlignment) {
U32 u;
EXPECT_EQ(4u, u.BaseAlignment(MemoryLayout::kUniformBuffer));
}
} // namespace
} // namespace type
} // namespace tint

60
src/type/vector_type.cc Normal file
View File

@@ -0,0 +1,60 @@
// Copyright 2020 The Tint 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 "src/type/vector_type.h"
#include <assert.h>
#include <cmath>
#include "src/ast/clone_context.h"
#include "src/ast/module.h"
TINT_INSTANTIATE_CLASS_ID(tint::type::Vector);
namespace tint {
namespace type {
Vector::Vector(Type* subtype, uint32_t size) : subtype_(subtype), size_(size) {
assert(size_ > 1);
assert(size_ < 5);
}
Vector::Vector(Vector&&) = default;
Vector::~Vector() = default;
std::string Vector::type_name() const {
return "__vec_" + std::to_string(size_) + subtype_->type_name();
}
uint64_t Vector::MinBufferBindingSize(MemoryLayout mem_layout) const {
return size_ * subtype_->MinBufferBindingSize(mem_layout);
}
uint64_t Vector::BaseAlignment(MemoryLayout mem_layout) const {
if (size_ == 2) {
return 2 * subtype_->BaseAlignment(mem_layout);
} else if (size_ == 3 || size_ == 4) {
return 4 * subtype_->BaseAlignment(mem_layout);
}
return 0; // vectors are only supposed to have 2, 3, or 4 elements.
}
Vector* Vector::Clone(CloneContext* ctx) const {
return ctx->mod->create<Vector>(ctx->Clone(subtype_), size_);
}
} // namespace type
} // namespace tint

67
src/type/vector_type.h Normal file
View File

@@ -0,0 +1,67 @@
// Copyright 2020 The Tint 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.
#ifndef SRC_TYPE_VECTOR_TYPE_H_
#define SRC_TYPE_VECTOR_TYPE_H_
#include <string>
#include "src/type/type.h"
namespace tint {
namespace type {
/// A vector type.
class Vector : public Castable<Vector, Type> {
public:
/// Constructor
/// @param subtype the vector element type
/// @param size the number of elements in the vector
Vector(Type* subtype, uint32_t size);
/// Move constructor
Vector(Vector&&);
~Vector() override;
/// @returns the type of the vector elements
Type* type() const { return subtype_; }
/// @returns the size of the vector
uint32_t size() const { return size_; }
/// @returns the name for th type
std::string type_name() const override;
/// @param mem_layout type of memory layout to use in calculation.
/// @returns minimum size required for this type, in bytes.
/// 0 for non-host shareable types.
uint64_t MinBufferBindingSize(MemoryLayout mem_layout) const override;
/// @param mem_layout type of memory layout to use in calculation.
/// @returns base alignment for the type, in bytes.
/// 0 for non-host shareable types.
uint64_t BaseAlignment(MemoryLayout mem_layout) const override;
/// Clones this type and all transitive types using the `CloneContext` `ctx`.
/// @param ctx the clone context
/// @return the newly cloned type
Vector* Clone(CloneContext* ctx) const override;
private:
Type* const subtype_;
uint32_t const size_;
};
} // namespace type
} // namespace tint
#endif // SRC_TYPE_VECTOR_TYPE_H_

View File

@@ -0,0 +1,105 @@
// Copyright 2020 The Tint 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 "src/type/vector_type.h"
#include "src/type/access_control_type.h"
#include "src/type/array_type.h"
#include "src/type/bool_type.h"
#include "src/type/f32_type.h"
#include "src/type/i32_type.h"
#include "src/type/matrix_type.h"
#include "src/type/pointer_type.h"
#include "src/type/struct_type.h"
#include "src/type/test_helper.h"
#include "src/type/texture_type.h"
#include "src/type/u32_type.h"
namespace tint {
namespace type {
namespace {
using VectorTest = TestHelper;
TEST_F(VectorTest, Creation) {
I32 i32;
Vector v{&i32, 2};
EXPECT_EQ(v.type(), &i32);
EXPECT_EQ(v.size(), 2u);
}
TEST_F(VectorTest, Is) {
I32 i32;
Vector v{&i32, 4};
Type* ty = &v;
EXPECT_FALSE(ty->Is<AccessControl>());
EXPECT_FALSE(ty->Is<Alias>());
EXPECT_FALSE(ty->Is<Array>());
EXPECT_FALSE(ty->Is<Bool>());
EXPECT_FALSE(ty->Is<F32>());
EXPECT_FALSE(ty->Is<I32>());
EXPECT_FALSE(ty->Is<Matrix>());
EXPECT_FALSE(ty->Is<Pointer>());
EXPECT_FALSE(ty->Is<Sampler>());
EXPECT_FALSE(ty->Is<Struct>());
EXPECT_FALSE(ty->Is<Texture>());
EXPECT_FALSE(ty->Is<U32>());
EXPECT_TRUE(ty->Is<Vector>());
}
TEST_F(VectorTest, TypeName) {
I32 i32;
Vector v{&i32, 3};
EXPECT_EQ(v.type_name(), "__vec_3__i32");
}
TEST_F(VectorTest, MinBufferBindingSizeVec2) {
I32 i32;
Vector v{&i32, 2};
EXPECT_EQ(8u, v.MinBufferBindingSize(MemoryLayout::kUniformBuffer));
}
TEST_F(VectorTest, MinBufferBindingSizeVec3) {
I32 i32;
Vector v{&i32, 3};
EXPECT_EQ(12u, v.MinBufferBindingSize(MemoryLayout::kUniformBuffer));
}
TEST_F(VectorTest, MinBufferBindingSizeVec4) {
I32 i32;
Vector v{&i32, 4};
EXPECT_EQ(16u, v.MinBufferBindingSize(MemoryLayout::kUniformBuffer));
}
TEST_F(VectorTest, BaseAlignmentVec2) {
I32 i32;
Vector v{&i32, 2};
EXPECT_EQ(8u, v.BaseAlignment(MemoryLayout::kUniformBuffer));
}
TEST_F(VectorTest, BaseAlignmentVec3) {
I32 i32;
Vector v{&i32, 3};
EXPECT_EQ(16u, v.BaseAlignment(MemoryLayout::kUniformBuffer));
}
TEST_F(VectorTest, BaseAlignmentVec4) {
I32 i32;
Vector v{&i32, 4};
EXPECT_EQ(16u, v.BaseAlignment(MemoryLayout::kUniformBuffer));
}
} // namespace
} // namespace type
} // namespace tint

40
src/type/void_type.cc Normal file
View File

@@ -0,0 +1,40 @@
// Copyright 2020 The Tint 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 "src/type/void_type.h"
#include "src/ast/clone_context.h"
#include "src/ast/module.h"
TINT_INSTANTIATE_CLASS_ID(tint::type::Void);
namespace tint {
namespace type {
Void::Void() = default;
Void::Void(Void&&) = default;
Void::~Void() = default;
std::string Void::type_name() const {
return "__void";
}
Void* Void::Clone(CloneContext* ctx) const {
return ctx->mod->create<Void>();
}
} // namespace type
} // namespace tint

46
src/type/void_type.h Normal file
View File

@@ -0,0 +1,46 @@
// Copyright 2020 The Tint 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.
#ifndef SRC_TYPE_VOID_TYPE_H_
#define SRC_TYPE_VOID_TYPE_H_
#include <string>
#include "src/type/type.h"
namespace tint {
namespace type {
/// A void type
class Void : public Castable<Void, Type> {
public:
/// Constructor
Void();
/// Move constructor
Void(Void&&);
~Void() override;
/// @returns the name for this type
std::string type_name() const override;
/// Clones this type and all transitive types using the `CloneContext` `ctx`.
/// @param ctx the clone context
/// @return the newly cloned type
Void* Clone(CloneContext* ctx) const override;
};
} // namespace type
} // namespace tint
#endif // SRC_TYPE_VOID_TYPE_H_