Replace TextureType::(Is|As)Storage with Castable

Change-Id: I627304c0b397605f565ae688f2a2b61132c26b9b
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/34278
Reviewed-by: dan sinclair <dsinclair@chromium.org>
This commit is contained in:
Ben Clayton 2020-11-30 23:30:58 +00:00
parent 0441bd1756
commit 1a23756294
14 changed files with 66 additions and 85 deletions

View File

@ -23,6 +23,7 @@
#include "src/ast/type/i32_type.h"
#include "src/ast/type/matrix_type.h"
#include "src/ast/type/pointer_type.h"
#include "src/ast/type/storage_texture_type.h"
#include "src/ast/type/struct_type.h"
#include "src/ast/type/u32_type.h"
#include "src/ast/type/vector_type.h"
@ -54,9 +55,10 @@ TEST_F(DepthTextureTypeTest, Is) {
TEST_F(DepthTextureTypeTest, IsTextureType) {
DepthTextureType d(TextureDimension::kCube);
EXPECT_TRUE(d.Is<DepthTextureType>());
EXPECT_FALSE(d.IsSampled());
EXPECT_FALSE(d.IsStorage());
TextureType*ty = &d;
EXPECT_TRUE(ty->Is<DepthTextureType>());
EXPECT_FALSE(ty->IsSampled());
EXPECT_FALSE(ty->Is<StorageTextureType>());
}
TEST_F(DepthTextureTypeTest, Dim) {

View File

@ -23,6 +23,7 @@
#include "src/ast/type/i32_type.h"
#include "src/ast/type/matrix_type.h"
#include "src/ast/type/pointer_type.h"
#include "src/ast/type/storage_texture_type.h"
#include "src/ast/type/struct_type.h"
#include "src/ast/type/u32_type.h"
#include "src/ast/type/vector_type.h"
@ -60,7 +61,7 @@ TEST_F(MultisampledTextureTypeTest, IsTextureType) {
EXPECT_FALSE(ty->Is<DepthTextureType>());
EXPECT_TRUE(ty->Is<MultisampledTextureType>());
EXPECT_FALSE(ty->IsSampled());
EXPECT_FALSE(ty->IsStorage());
EXPECT_FALSE(ty->Is<StorageTextureType>());
}
TEST_F(MultisampledTextureTypeTest, Dim) {

View File

@ -23,6 +23,7 @@
#include "src/ast/type/i32_type.h"
#include "src/ast/type/matrix_type.h"
#include "src/ast/type/pointer_type.h"
#include "src/ast/type/storage_texture_type.h"
#include "src/ast/type/struct_type.h"
#include "src/ast/type/u32_type.h"
#include "src/ast/type/vector_type.h"
@ -59,7 +60,7 @@ TEST_F(SampledTextureTypeTest, IsTextureType) {
TextureType* ty = &s;
EXPECT_FALSE(ty->Is<DepthTextureType>());
EXPECT_TRUE(ty->IsSampled());
EXPECT_FALSE(ty->IsStorage());
EXPECT_FALSE(ty->Is<StorageTextureType>());
}
TEST_F(SampledTextureTypeTest, Dim) {

View File

@ -169,10 +169,6 @@ StorageTextureType::StorageTextureType(StorageTextureType&&) = default;
StorageTextureType::~StorageTextureType() = default;
bool StorageTextureType::IsStorage() const {
return true;
}
std::string StorageTextureType::type_name() const {
std::ostringstream out;
out << "__storage_texture_" << access_ << "_" << dim() << "_"

View File

@ -80,9 +80,6 @@ class StorageTextureType : public Castable<StorageTextureType, TextureType> {
StorageTextureType(StorageTextureType&&);
~StorageTextureType() override;
/// @returns true if the type is a storage texture type
bool IsStorage() const override;
/// @param type the subtype of the storage texture
void set_type(Type* const type);

View File

@ -63,7 +63,7 @@ TEST_F(StorageTextureTypeTest, IsTextureType) {
TextureType* ty = &s;
EXPECT_FALSE(ty->Is<DepthTextureType>());
EXPECT_FALSE(ty->IsSampled());
EXPECT_TRUE(ty->IsStorage());
EXPECT_TRUE(ty->Is<StorageTextureType>());
}
TEST_F(StorageTextureTypeTest, Dim) {
@ -93,44 +93,45 @@ TEST_F(StorageTextureTypeTest, TypeName) {
TEST_F(StorageTextureTypeTest, F32Type) {
Context ctx;
ast::Module mod;
ast::type::Type* s = mod.create<StorageTextureType>(
TextureDimension::k2dArray, AccessControl::kReadOnly,
Type* s = mod.create<StorageTextureType>(TextureDimension::k2dArray,
AccessControl::kReadOnly,
ImageFormat::kRgba32Float);
TypeDeterminer td(&ctx, &mod);
ASSERT_TRUE(td.Determine()) << td.error();
ASSERT_TRUE(s->Is<TextureType>());
ASSERT_TRUE(s->As<TextureType>()->IsStorage());
EXPECT_TRUE(s->As<TextureType>()->AsStorage()->type()->Is<F32Type>());
ASSERT_TRUE(s->Is<StorageTextureType>());
EXPECT_TRUE(
s->As<TextureType>()->As<StorageTextureType>()->type()->Is<F32Type>());
}
TEST_F(StorageTextureTypeTest, U32Type) {
Context ctx;
ast::Module mod;
ast::type::Type* s = mod.create<StorageTextureType>(
TextureDimension::k2dArray, AccessControl::kReadOnly,
Type* s = mod.create<StorageTextureType>(TextureDimension::k2dArray,
AccessControl::kReadOnly,
ImageFormat::kRg32Uint);
TypeDeterminer td(&ctx, &mod);
ASSERT_TRUE(td.Determine()) << td.error();
ASSERT_TRUE(s->Is<TextureType>());
ASSERT_TRUE(s->As<TextureType>()->IsStorage());
EXPECT_TRUE(
s->As<TextureType>()->AsStorage()->type()->Is<ast::type::U32Type>());
ASSERT_TRUE(s->Is<StorageTextureType>());
EXPECT_TRUE(s->As<StorageTextureType>()->type()->Is<U32Type>());
}
TEST_F(StorageTextureTypeTest, I32Type) {
Context ctx;
ast::Module mod;
ast::type::Type* s = mod.create<StorageTextureType>(
TextureDimension::k2dArray, AccessControl::kReadOnly,
Type* s = mod.create<StorageTextureType>(TextureDimension::k2dArray,
AccessControl::kReadOnly,
ImageFormat::kRgba32Sint);
TypeDeterminer td(&ctx, &mod);
ASSERT_TRUE(td.Determine()) << td.error();
ASSERT_TRUE(s->Is<TextureType>());
ASSERT_TRUE(s->As<TextureType>()->IsStorage());
EXPECT_TRUE(s->As<TextureType>()->AsStorage()->type()->Is<I32Type>());
ASSERT_TRUE(s->Is<StorageTextureType>());
EXPECT_TRUE(
s->As<TextureType>()->As<StorageTextureType>()->type()->Is<I32Type>());
}
TEST_F(StorageTextureTypeTest, MinBufferBindingSize) {

View File

@ -18,7 +18,6 @@
#include "src/ast/type/multisampled_texture_type.h"
#include "src/ast/type/sampled_texture_type.h"
#include "src/ast/type/storage_texture_type.h"
namespace tint {
namespace ast {
@ -60,9 +59,6 @@ TextureType::TextureType(TextureType&&) = default;
TextureType::~TextureType() = default;
bool TextureType::IsStorage() const {
return false;
}
bool TextureType::IsSampled() const {
return false;
}
@ -72,21 +68,11 @@ const SampledTextureType* TextureType::AsSampled() const {
return static_cast<const SampledTextureType*>(this);
}
const StorageTextureType* TextureType::AsStorage() const {
assert(IsStorage());
return static_cast<const StorageTextureType*>(this);
}
SampledTextureType* TextureType::AsSampled() {
assert(IsSampled());
return static_cast<SampledTextureType*>(this);
}
StorageTextureType* TextureType::AsStorage() {
assert(IsStorage());
return static_cast<StorageTextureType*>(this);
}
} // namespace type
} // namespace ast
} // namespace tint

View File

@ -25,7 +25,6 @@ namespace ast {
namespace type {
class SampledTextureType;
class StorageTextureType;
/// The dimensionality of the texture
enum class TextureDimension {
@ -61,20 +60,14 @@ class TextureType : public Castable<TextureType, Type> {
/// @returns the texture dimension
TextureDimension dim() const { return dim_; }
/// @returns true if this is a storage texture
virtual bool IsStorage() const;
/// @returns true if this is a sampled texture
virtual bool IsSampled() const;
/// @returns the texture as a sampled texture
const SampledTextureType* AsSampled() const;
/// @returns the texture as a storage texture
const StorageTextureType* AsStorage() const;
/// @returns the texture as a sampled texture
SampledTextureType* AsSampled();
/// @returns the texture as a storage texture
StorageTextureType* AsStorage();
private:
TextureDimension dim_ = TextureDimension::k1d;

View File

@ -320,10 +320,10 @@ TEST_F(ParserImplTest,
EXPECT_FALSE(t.errored);
ASSERT_NE(t.value, nullptr);
ASSERT_TRUE(t->Is<ast::type::TextureType>());
ASSERT_TRUE(t->As<ast::type::TextureType>()->IsStorage());
EXPECT_EQ(t->As<ast::type::TextureType>()->AsStorage()->image_format(),
ASSERT_TRUE(t->Is<ast::type::StorageTextureType>());
EXPECT_EQ(t->As<ast::type::StorageTextureType>()->image_format(),
ast::type::ImageFormat::kR8Unorm);
EXPECT_EQ(t->As<ast::type::TextureType>()->AsStorage()->access(),
EXPECT_EQ(t->As<ast::type::StorageTextureType>()->access(),
ast::AccessControl::kReadOnly);
EXPECT_EQ(t->As<ast::type::TextureType>()->dim(),
ast::type::TextureDimension::k1d);
@ -338,10 +338,10 @@ TEST_F(ParserImplTest,
EXPECT_FALSE(t.errored);
ASSERT_NE(t.value, nullptr);
ASSERT_TRUE(t->Is<ast::type::TextureType>());
ASSERT_TRUE(t->As<ast::type::TextureType>()->IsStorage());
EXPECT_EQ(t->As<ast::type::TextureType>()->AsStorage()->image_format(),
ASSERT_TRUE(t->Is<ast::type::StorageTextureType>());
EXPECT_EQ(t->As<ast::type::StorageTextureType>()->image_format(),
ast::type::ImageFormat::kR16Float);
EXPECT_EQ(t->As<ast::type::TextureType>()->AsStorage()->access(),
EXPECT_EQ(t->As<ast::type::StorageTextureType>()->access(),
ast::AccessControl::kWriteOnly);
EXPECT_EQ(t->As<ast::type::TextureType>()->dim(),
ast::type::TextureDimension::k2d);
@ -392,10 +392,10 @@ TEST_F(ParserImplTest, TextureSamplerTypes_StorageTexture_Readonly1dR8Unorm) {
EXPECT_FALSE(t.errored);
ASSERT_NE(t.value, nullptr);
ASSERT_TRUE(t->Is<ast::type::TextureType>());
ASSERT_TRUE(t->As<ast::type::TextureType>()->IsStorage());
EXPECT_EQ(t->As<ast::type::TextureType>()->AsStorage()->image_format(),
ASSERT_TRUE(t->Is<ast::type::StorageTextureType>());
EXPECT_EQ(t->As<ast::type::StorageTextureType>()->image_format(),
ast::type::ImageFormat::kR8Unorm);
EXPECT_EQ(t->As<ast::type::TextureType>()->AsStorage()->access(),
EXPECT_EQ(t->As<ast::type::StorageTextureType>()->access(),
ast::AccessControl::kReadOnly);
EXPECT_EQ(t->As<ast::type::TextureType>()->dim(),
ast::type::TextureDimension::k1d);
@ -409,10 +409,10 @@ TEST_F(ParserImplTest, TextureSamplerTypes_StorageTexture_Writeonly2dR16Float) {
EXPECT_FALSE(t.errored);
ASSERT_NE(t.value, nullptr);
ASSERT_TRUE(t->Is<ast::type::TextureType>());
ASSERT_TRUE(t->As<ast::type::TextureType>()->IsStorage());
EXPECT_EQ(t->As<ast::type::TextureType>()->AsStorage()->image_format(),
ASSERT_TRUE(t->Is<ast::type::StorageTextureType>());
EXPECT_EQ(t->As<ast::type::StorageTextureType>()->image_format(),
ast::type::ImageFormat::kR16Float);
EXPECT_EQ(t->As<ast::type::TextureType>()->AsStorage()->access(),
EXPECT_EQ(t->As<ast::type::StorageTextureType>()->access(),
ast::AccessControl::kWriteOnly);
EXPECT_EQ(t->As<ast::type::TextureType>()->dim(),
ast::type::TextureDimension::k2d);

View File

@ -87,11 +87,11 @@ bool TypeDeterminer::Determine() {
for (auto& iter : mod_->types()) {
auto& type = iter.second;
if (!type->Is<ast::type::TextureType>() ||
!type->As<ast::type::TextureType>()->IsStorage()) {
!type->Is<ast::type::StorageTextureType>()) {
continue;
}
if (!DetermineStorageTextureSubtype(
type->As<ast::type::TextureType>()->AsStorage())) {
type->As<ast::type::StorageTextureType>())) {
set_error(Source{}, "unable to determine storage texture subtype for: " +
type->type_name());
return false;
@ -671,7 +671,7 @@ bool TypeDeterminer::DetermineIntrinsic(ast::IdentifierExpression* ident,
return true;
}
if (!texture->IsStorage() &&
if (!texture->Is<ast::type::StorageTextureType>() &&
!(texture->IsSampled() ||
texture->Is<ast::type::MultisampledTextureType>())) {
set_error(expr->source(), "invalid texture for " + ident->name());
@ -679,8 +679,8 @@ bool TypeDeterminer::DetermineIntrinsic(ast::IdentifierExpression* ident,
}
ast::type::Type* type = nullptr;
if (texture->IsStorage()) {
type = texture->AsStorage()->type();
if (texture->Is<ast::type::StorageTextureType>()) {
type = texture->As<ast::type::StorageTextureType>()->type();
} else if (texture->IsSampled()) {
type = texture->AsSampled()->type();
} else if (texture->Is<ast::type::MultisampledTextureType>()) {

View File

@ -47,6 +47,7 @@
#include "src/ast/type/matrix_type.h"
#include "src/ast/type/pointer_type.h"
#include "src/ast/type/sampler_type.h"
#include "src/ast/type/storage_texture_type.h"
#include "src/ast/type/struct_type.h"
#include "src/ast/type/texture_type.h"
#include "src/ast/type/u32_type.h"
@ -2099,7 +2100,7 @@ bool GeneratorImpl::EmitType(std::ostream& out,
out << type->As<ast::type::StructType>()->name();
} else if (type->Is<ast::type::TextureType>()) {
auto* tex = type->As<ast::type::TextureType>();
if (tex->IsStorage()) {
if (tex->Is<ast::type::StorageTextureType>()) {
out << "RW";
}
out << "Texture";

View File

@ -1880,8 +1880,8 @@ bool GeneratorImpl::EmitType(ast::type::Type* type, const std::string& name) {
out_ << "<";
if (tex->Is<ast::type::DepthTextureType>()) {
out_ << "float, access::sample";
} else if (tex->IsStorage()) {
auto* storage = tex->AsStorage();
} else if (tex->Is<ast::type::StorageTextureType>()) {
auto* storage = tex->As<ast::type::StorageTextureType>();
if (!EmitType(storage->type(), "")) {
return false;
}

View File

@ -718,8 +718,8 @@ bool Builder::GenerateGlobalVariable(ast::Variable* var) {
ops.push_back(Operand::Int(init_id));
} else if (type->Is<ast::type::TextureType>()) {
// Decorate storage texture variables with NonRead/Writeable if needed.
if (type->As<ast::type::TextureType>()->IsStorage()) {
switch (type->As<ast::type::TextureType>()->AsStorage()->access()) {
if (type->Is<ast::type::StorageTextureType>()) {
switch (type->As<ast::type::StorageTextureType>()->access()) {
case ast::AccessControl::kWriteOnly:
push_annot(
spv::Op::OpDecorate,
@ -1964,7 +1964,8 @@ void Builder::GenerateTextureIntrinsic(ast::IdentifierExpression* ident,
spirv_operands.reserve(4); // Enough to fit most parameter lists
if (ident->intrinsic() == ast::Intrinsic::kTextureLoad) {
op = texture_type->IsStorage() ? spv::Op::OpImageRead
op = texture_type->Is<ast::type::StorageTextureType>()
? spv::Op::OpImageRead
: spv::Op::OpImageFetch;
spirv_params.emplace_back(gen_param(pidx.texture));
spirv_params.emplace_back(gen_param(pidx.coords));
@ -2503,7 +2504,7 @@ bool Builder::GenerateTextureType(ast::type::TextureType* texture,
if (texture->IsSampled()) {
push_capability(SpvCapabilitySampled1D);
} else {
assert(texture->IsStorage());
assert(texture->Is<ast::type::StorageTextureType>());
push_capability(SpvCapabilityImage1D);
}
}
@ -2546,12 +2547,14 @@ bool Builder::GenerateTextureType(ast::type::TextureType* texture,
} else if (texture->Is<ast::type::MultisampledTextureType>()) {
type_id = GenerateTypeIfNeeded(
texture->As<ast::type::MultisampledTextureType>()->type());
} else if (texture->IsStorage()) {
if (texture->AsStorage()->access() == ast::AccessControl::kWriteOnly) {
} else if (texture->Is<ast::type::StorageTextureType>()) {
if (texture->As<ast::type::StorageTextureType>()->access() ==
ast::AccessControl::kWriteOnly) {
ast::type::VoidType void_type;
type_id = GenerateTypeIfNeeded(&void_type);
} else {
type_id = GenerateTypeIfNeeded(texture->AsStorage()->type());
type_id = GenerateTypeIfNeeded(
texture->As<ast::type::StorageTextureType>()->type());
}
}
if (type_id == 0u) {
@ -2559,9 +2562,9 @@ bool Builder::GenerateTextureType(ast::type::TextureType* texture,
}
uint32_t format_literal = SpvImageFormat_::SpvImageFormatUnknown;
if (texture->IsStorage()) {
format_literal =
convert_image_format_to_spv(texture->AsStorage()->image_format());
if (texture->Is<ast::type::StorageTextureType>()) {
format_literal = convert_image_format_to_spv(
texture->As<ast::type::StorageTextureType>()->image_format());
}
push_type(spv::Op::OpTypeImage,

View File

@ -476,10 +476,10 @@ bool GeneratorImpl::EmitType(ast::type::Type* type) {
/* nothing to emit */
} else if (texture->Is<ast::type::MultisampledTextureType>()) {
out_ << "multisampled_";
} else if (texture->IsStorage()) {
} else if (texture->Is<ast::type::StorageTextureType>()) {
out_ << "storage_";
auto* storage = texture->AsStorage();
auto* storage = texture->As<ast::type::StorageTextureType>();
if (storage->access() == ast::AccessControl::kReadOnly) {
out_ << "ro_";
} else if (storage->access() == ast::AccessControl::kWriteOnly) {
@ -536,8 +536,8 @@ bool GeneratorImpl::EmitType(ast::type::Type* type) {
return false;
}
out_ << ">";
} else if (texture->IsStorage()) {
auto* storage = texture->AsStorage();
} else if (texture->Is<ast::type::StorageTextureType>()) {
auto* storage = texture->As<ast::type::StorageTextureType>();
out_ << "<";
if (!EmitImageFormat(storage->image_format())) {