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

Change-Id: Id997f118a8ce9f4f7c42bed306368d3f204b6607
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/34279
Reviewed-by: dan sinclair <dsinclair@chromium.org>
This commit is contained in:
Ben Clayton 2020-11-30 23:30:58 +00:00
parent 1a23756294
commit b062bbdce8
16 changed files with 53 additions and 92 deletions

View File

@ -19,6 +19,7 @@
#include "src/ast/decorated_variable.h" #include "src/ast/decorated_variable.h"
#include "src/ast/stage_decoration.h" #include "src/ast/stage_decoration.h"
#include "src/ast/type/multisampled_texture_type.h" #include "src/ast/type/multisampled_texture_type.h"
#include "src/ast/type/sampled_texture_type.h"
#include "src/ast/type/texture_type.h" #include "src/ast/type/texture_type.h"
#include "src/ast/workgroup_decoration.h" #include "src/ast/workgroup_decoration.h"
@ -318,7 +319,7 @@ Function::ReferencedSampledTextureVariablesImpl(bool multisampled) const {
if ((multisampled && if ((multisampled &&
!unwrapped_type->Is<ast::type::MultisampledTextureType>()) || !unwrapped_type->Is<ast::type::MultisampledTextureType>()) ||
(!multisampled && (!multisampled &&
!unwrapped_type->As<ast::type::TextureType>()->IsSampled())) { !unwrapped_type->Is<ast::type::SampledTextureType>())) {
continue; continue;
} }

View File

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

View File

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

View File

@ -30,10 +30,6 @@ SampledTextureType::SampledTextureType(SampledTextureType&&) = default;
SampledTextureType::~SampledTextureType() = default; SampledTextureType::~SampledTextureType() = default;
bool SampledTextureType::IsSampled() const {
return true;
}
std::string SampledTextureType::type_name() const { std::string SampledTextureType::type_name() const {
std::ostringstream out; std::ostringstream out;
out << "__sampled_texture_" << dim() << type_->type_name(); out << "__sampled_texture_" << dim() << type_->type_name();

View File

@ -34,9 +34,6 @@ class SampledTextureType : public Castable<SampledTextureType, TextureType> {
SampledTextureType(SampledTextureType&&); SampledTextureType(SampledTextureType&&);
~SampledTextureType() override; ~SampledTextureType() override;
/// @returns true if the type is a sampled texture type
bool IsSampled() const override;
/// @returns the subtype of the sampled texture /// @returns the subtype of the sampled texture
Type* type() const { return type_; } Type* type() const { return type_; }

View File

@ -59,7 +59,7 @@ TEST_F(SampledTextureTypeTest, IsTextureType) {
SampledTextureType s(TextureDimension::kCube, &f32); SampledTextureType s(TextureDimension::kCube, &f32);
TextureType* ty = &s; TextureType* ty = &s;
EXPECT_FALSE(ty->Is<DepthTextureType>()); EXPECT_FALSE(ty->Is<DepthTextureType>());
EXPECT_TRUE(ty->IsSampled()); EXPECT_TRUE(ty->Is<SampledTextureType>());
EXPECT_FALSE(ty->Is<StorageTextureType>()); EXPECT_FALSE(ty->Is<StorageTextureType>());
} }

View File

@ -26,6 +26,7 @@
#include "src/ast/type/i32_type.h" #include "src/ast/type/i32_type.h"
#include "src/ast/type/matrix_type.h" #include "src/ast/type/matrix_type.h"
#include "src/ast/type/pointer_type.h" #include "src/ast/type/pointer_type.h"
#include "src/ast/type/sampled_texture_type.h"
#include "src/ast/type/struct_type.h" #include "src/ast/type/struct_type.h"
#include "src/ast/type/u32_type.h" #include "src/ast/type/u32_type.h"
#include "src/ast/type/vector_type.h" #include "src/ast/type/vector_type.h"
@ -62,7 +63,7 @@ TEST_F(StorageTextureTypeTest, IsTextureType) {
ImageFormat::kRgba32Float); ImageFormat::kRgba32Float);
TextureType* ty = &s; TextureType* ty = &s;
EXPECT_FALSE(ty->Is<DepthTextureType>()); EXPECT_FALSE(ty->Is<DepthTextureType>());
EXPECT_FALSE(ty->IsSampled()); EXPECT_FALSE(ty->Is<SampledTextureType>());
EXPECT_TRUE(ty->Is<StorageTextureType>()); EXPECT_TRUE(ty->Is<StorageTextureType>());
} }

View File

@ -59,20 +59,6 @@ TextureType::TextureType(TextureType&&) = default;
TextureType::~TextureType() = default; TextureType::~TextureType() = default;
bool TextureType::IsSampled() const {
return false;
}
const SampledTextureType* TextureType::AsSampled() const {
assert(IsSampled());
return static_cast<const SampledTextureType*>(this);
}
SampledTextureType* TextureType::AsSampled() {
assert(IsSampled());
return static_cast<SampledTextureType*>(this);
}
} // namespace type } // namespace type
} // namespace ast } // namespace ast
} // namespace tint } // namespace tint

View File

@ -24,8 +24,6 @@ namespace tint {
namespace ast { namespace ast {
namespace type { namespace type {
class SampledTextureType;
/// The dimensionality of the texture /// The dimensionality of the texture
enum class TextureDimension { enum class TextureDimension {
/// Invalid texture /// Invalid texture
@ -60,15 +58,6 @@ class TextureType : public Castable<TextureType, Type> {
/// @returns the texture dimension /// @returns the texture dimension
TextureDimension dim() const { return dim_; } TextureDimension dim() const { return dim_; }
/// @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 sampled texture
SampledTextureType* AsSampled();
private: private:
TextureDimension dim_ = TextureDimension::k1d; TextureDimension dim_ = TextureDimension::k1d;
}; };

View File

@ -386,7 +386,9 @@ std::vector<ResourceBinding> Inspector::GetSampledTextureResourceBindingsImpl(
->type() ->type()
->UnwrapIfNeeded(); ->UnwrapIfNeeded();
} else { } else {
base_type = texture_type->AsSampled()->type()->UnwrapIfNeeded(); base_type = texture_type->As<ast::type::SampledTextureType>()
->type()
->UnwrapIfNeeded();
} }
if (base_type->Is<ast::type::ArrayType>()) { if (base_type->Is<ast::type::ArrayType>()) {

View File

@ -80,11 +80,9 @@ TEST_F(ParserImplTest, TextureSamplerTypes_SampledTexture_F32_Old) {
EXPECT_FALSE(t.errored); EXPECT_FALSE(t.errored);
ASSERT_NE(t.value, nullptr); ASSERT_NE(t.value, nullptr);
ASSERT_TRUE(t->Is<ast::type::TextureType>()); ASSERT_TRUE(t->Is<ast::type::TextureType>());
ASSERT_TRUE(t->As<ast::type::TextureType>()->IsSampled()); ASSERT_TRUE(t->Is<ast::type::SampledTextureType>());
ASSERT_TRUE(t->As<ast::type::TextureType>() ASSERT_TRUE(
->AsSampled() t->As<ast::type::SampledTextureType>()->type()->Is<ast::type::F32Type>());
->type()
->Is<ast::type::F32Type>());
EXPECT_EQ(t->As<ast::type::TextureType>()->dim(), EXPECT_EQ(t->As<ast::type::TextureType>()->dim(),
ast::type::TextureDimension::k1d); ast::type::TextureDimension::k1d);
} }
@ -97,11 +95,9 @@ TEST_F(ParserImplTest, TextureSamplerTypes_SampledTexture_I32_Old) {
EXPECT_FALSE(t.errored); EXPECT_FALSE(t.errored);
ASSERT_NE(t.value, nullptr); ASSERT_NE(t.value, nullptr);
ASSERT_TRUE(t->Is<ast::type::TextureType>()); ASSERT_TRUE(t->Is<ast::type::TextureType>());
ASSERT_TRUE(t->As<ast::type::TextureType>()->IsSampled()); ASSERT_TRUE(t->Is<ast::type::SampledTextureType>());
ASSERT_TRUE(t->As<ast::type::TextureType>() ASSERT_TRUE(
->AsSampled() t->As<ast::type::SampledTextureType>()->type()->Is<ast::type::I32Type>());
->type()
->Is<ast::type::I32Type>());
EXPECT_EQ(t->As<ast::type::TextureType>()->dim(), EXPECT_EQ(t->As<ast::type::TextureType>()->dim(),
ast::type::TextureDimension::k2d); ast::type::TextureDimension::k2d);
} }
@ -114,11 +110,9 @@ TEST_F(ParserImplTest, TextureSamplerTypes_SampledTexture_U32_Old) {
EXPECT_FALSE(t.errored); EXPECT_FALSE(t.errored);
ASSERT_NE(t.value, nullptr); ASSERT_NE(t.value, nullptr);
ASSERT_TRUE(t->Is<ast::type::TextureType>()); ASSERT_TRUE(t->Is<ast::type::TextureType>());
ASSERT_TRUE(t->As<ast::type::TextureType>()->IsSampled()); ASSERT_TRUE(t->Is<ast::type::SampledTextureType>());
ASSERT_TRUE(t->As<ast::type::TextureType>() ASSERT_TRUE(
->AsSampled() t->As<ast::type::SampledTextureType>()->type()->Is<ast::type::U32Type>());
->type()
->Is<ast::type::U32Type>());
EXPECT_EQ(t->As<ast::type::TextureType>()->dim(), EXPECT_EQ(t->As<ast::type::TextureType>()->dim(),
ast::type::TextureDimension::k3d); ast::type::TextureDimension::k3d);
} }
@ -172,11 +166,9 @@ TEST_F(ParserImplTest, TextureSamplerTypes_SampledTexture_F32) {
EXPECT_FALSE(t.errored); EXPECT_FALSE(t.errored);
ASSERT_NE(t.value, nullptr); ASSERT_NE(t.value, nullptr);
ASSERT_TRUE(t->Is<ast::type::TextureType>()); ASSERT_TRUE(t->Is<ast::type::TextureType>());
ASSERT_TRUE(t->As<ast::type::TextureType>()->IsSampled()); ASSERT_TRUE(t->Is<ast::type::SampledTextureType>());
ASSERT_TRUE(t->As<ast::type::TextureType>() ASSERT_TRUE(
->AsSampled() t->As<ast::type::SampledTextureType>()->type()->Is<ast::type::F32Type>());
->type()
->Is<ast::type::F32Type>());
EXPECT_EQ(t->As<ast::type::TextureType>()->dim(), EXPECT_EQ(t->As<ast::type::TextureType>()->dim(),
ast::type::TextureDimension::k1d); ast::type::TextureDimension::k1d);
} }
@ -189,11 +181,9 @@ TEST_F(ParserImplTest, TextureSamplerTypes_SampledTexture_I32) {
EXPECT_FALSE(t.errored); EXPECT_FALSE(t.errored);
ASSERT_NE(t.value, nullptr); ASSERT_NE(t.value, nullptr);
ASSERT_TRUE(t->Is<ast::type::TextureType>()); ASSERT_TRUE(t->Is<ast::type::TextureType>());
ASSERT_TRUE(t->As<ast::type::TextureType>()->IsSampled()); ASSERT_TRUE(t->Is<ast::type::SampledTextureType>());
ASSERT_TRUE(t->As<ast::type::TextureType>() ASSERT_TRUE(
->AsSampled() t->As<ast::type::SampledTextureType>()->type()->Is<ast::type::I32Type>());
->type()
->Is<ast::type::I32Type>());
EXPECT_EQ(t->As<ast::type::TextureType>()->dim(), EXPECT_EQ(t->As<ast::type::TextureType>()->dim(),
ast::type::TextureDimension::k2d); ast::type::TextureDimension::k2d);
} }
@ -206,11 +196,9 @@ TEST_F(ParserImplTest, TextureSamplerTypes_SampledTexture_U32) {
EXPECT_FALSE(t.errored); EXPECT_FALSE(t.errored);
ASSERT_NE(t.value, nullptr); ASSERT_NE(t.value, nullptr);
ASSERT_TRUE(t->Is<ast::type::TextureType>()); ASSERT_TRUE(t->Is<ast::type::TextureType>());
ASSERT_TRUE(t->As<ast::type::TextureType>()->IsSampled()); ASSERT_TRUE(t->Is<ast::type::SampledTextureType>());
ASSERT_TRUE(t->As<ast::type::TextureType>() ASSERT_TRUE(
->AsSampled() t->As<ast::type::SampledTextureType>()->type()->Is<ast::type::U32Type>());
->type()
->Is<ast::type::U32Type>());
EXPECT_EQ(t->As<ast::type::TextureType>()->dim(), EXPECT_EQ(t->As<ast::type::TextureType>()->dim(),
ast::type::TextureDimension::k3d); ast::type::TextureDimension::k3d);
} }

View File

@ -765,11 +765,9 @@ TEST_F(ParserImplTest, TypeDecl_Texture_Old) {
ASSERT_NE(t.value, nullptr) << p->error(); ASSERT_NE(t.value, nullptr) << p->error();
EXPECT_EQ(t.value, type); EXPECT_EQ(t.value, type);
ASSERT_TRUE(t->Is<ast::type::TextureType>()); ASSERT_TRUE(t->Is<ast::type::TextureType>());
ASSERT_TRUE(t->As<ast::type::TextureType>()->IsSampled()); ASSERT_TRUE(t->Is<ast::type::SampledTextureType>());
ASSERT_TRUE(t->As<ast::type::TextureType>() ASSERT_TRUE(
->AsSampled() t->As<ast::type::SampledTextureType>()->type()->Is<ast::type::F32Type>());
->type()
->Is<ast::type::F32Type>());
} }
TEST_F(ParserImplTest, TypeDecl_Texture) { TEST_F(ParserImplTest, TypeDecl_Texture) {
@ -786,11 +784,9 @@ TEST_F(ParserImplTest, TypeDecl_Texture) {
ASSERT_NE(t.value, nullptr); ASSERT_NE(t.value, nullptr);
EXPECT_EQ(t.value, type); EXPECT_EQ(t.value, type);
ASSERT_TRUE(t->Is<ast::type::TextureType>()); ASSERT_TRUE(t->Is<ast::type::TextureType>());
ASSERT_TRUE(t->As<ast::type::TextureType>()->IsSampled()); ASSERT_TRUE(t->Is<ast::type::SampledTextureType>());
ASSERT_TRUE(t->As<ast::type::TextureType>() ASSERT_TRUE(
->AsSampled() t->As<ast::type::SampledTextureType>()->type()->Is<ast::type::F32Type>());
->type()
->Is<ast::type::F32Type>());
} }
} // namespace } // namespace

View File

@ -672,7 +672,7 @@ bool TypeDeterminer::DetermineIntrinsic(ast::IdentifierExpression* ident,
} }
if (!texture->Is<ast::type::StorageTextureType>() && if (!texture->Is<ast::type::StorageTextureType>() &&
!(texture->IsSampled() || !(texture->Is<ast::type::SampledTextureType>() ||
texture->Is<ast::type::MultisampledTextureType>())) { texture->Is<ast::type::MultisampledTextureType>())) {
set_error(expr->source(), "invalid texture for " + ident->name()); set_error(expr->source(), "invalid texture for " + ident->name());
return false; return false;
@ -681,8 +681,8 @@ bool TypeDeterminer::DetermineIntrinsic(ast::IdentifierExpression* ident,
ast::type::Type* type = nullptr; ast::type::Type* type = nullptr;
if (texture->Is<ast::type::StorageTextureType>()) { if (texture->Is<ast::type::StorageTextureType>()) {
type = texture->As<ast::type::StorageTextureType>()->type(); type = texture->As<ast::type::StorageTextureType>()->type();
} else if (texture->IsSampled()) { } else if (texture->Is<ast::type::SampledTextureType>()) {
type = texture->AsSampled()->type(); type = texture->As<ast::type::SampledTextureType>()->type();
} else if (texture->Is<ast::type::MultisampledTextureType>()) { } else if (texture->Is<ast::type::MultisampledTextureType>()) {
type = texture->As<ast::type::MultisampledTextureType>()->type(); type = texture->As<ast::type::MultisampledTextureType>()->type();
} else { } else {

View File

@ -1900,8 +1900,8 @@ bool GeneratorImpl::EmitType(ast::type::Type* type, const std::string& name) {
return false; return false;
} }
out_ << ", access::sample"; out_ << ", access::sample";
} else if (tex->IsSampled()) { } else if (tex->Is<ast::type::SampledTextureType>()) {
if (!EmitType(tex->AsSampled()->type(), "")) { if (!EmitType(tex->As<ast::type::SampledTextureType>()->type(), "")) {
return false; return false;
} }
out_ << ", access::sample"; out_ << ", access::sample";

View File

@ -2501,7 +2501,7 @@ bool Builder::GenerateTextureType(ast::type::TextureType* texture,
if (dim == ast::type::TextureDimension::k1dArray || if (dim == ast::type::TextureDimension::k1dArray ||
dim == ast::type::TextureDimension::k1d) { dim == ast::type::TextureDimension::k1d) {
dim_literal = SpvDim1D; dim_literal = SpvDim1D;
if (texture->IsSampled()) { if (texture->Is<ast::type::SampledTextureType>()) {
push_capability(SpvCapabilitySampled1D); push_capability(SpvCapabilitySampled1D);
} else { } else {
assert(texture->Is<ast::type::StorageTextureType>()); assert(texture->Is<ast::type::StorageTextureType>());
@ -2528,12 +2528,14 @@ bool Builder::GenerateTextureType(ast::type::TextureType* texture,
uint32_t sampled_literal = 2u; uint32_t sampled_literal = 2u;
if (texture->Is<ast::type::MultisampledTextureType>() || if (texture->Is<ast::type::MultisampledTextureType>() ||
texture->IsSampled() || texture->Is<ast::type::DepthTextureType>()) { texture->Is<ast::type::SampledTextureType>() ||
texture->Is<ast::type::DepthTextureType>()) {
sampled_literal = 1u; sampled_literal = 1u;
} }
if (dim == ast::type::TextureDimension::kCubeArray) { if (dim == ast::type::TextureDimension::kCubeArray) {
if (texture->IsSampled() || texture->Is<ast::type::DepthTextureType>()) { if (texture->Is<ast::type::SampledTextureType>() ||
texture->Is<ast::type::DepthTextureType>()) {
push_capability(SpvCapabilitySampledCubeArray); push_capability(SpvCapabilitySampledCubeArray);
} }
} }
@ -2542,8 +2544,9 @@ bool Builder::GenerateTextureType(ast::type::TextureType* texture,
if (texture->Is<ast::type::DepthTextureType>()) { if (texture->Is<ast::type::DepthTextureType>()) {
ast::type::F32Type f32; ast::type::F32Type f32;
type_id = GenerateTypeIfNeeded(&f32); type_id = GenerateTypeIfNeeded(&f32);
} else if (texture->IsSampled()) { } else if (texture->Is<ast::type::SampledTextureType>()) {
type_id = GenerateTypeIfNeeded(texture->AsSampled()->type()); type_id = GenerateTypeIfNeeded(
texture->As<ast::type::SampledTextureType>()->type());
} else if (texture->Is<ast::type::MultisampledTextureType>()) { } else if (texture->Is<ast::type::MultisampledTextureType>()) {
type_id = GenerateTypeIfNeeded( type_id = GenerateTypeIfNeeded(
texture->As<ast::type::MultisampledTextureType>()->type()); texture->As<ast::type::MultisampledTextureType>()->type());

View File

@ -472,7 +472,7 @@ bool GeneratorImpl::EmitType(ast::type::Type* type) {
out_ << "texture_"; out_ << "texture_";
if (texture->Is<ast::type::DepthTextureType>()) { if (texture->Is<ast::type::DepthTextureType>()) {
out_ << "depth_"; out_ << "depth_";
} else if (texture->IsSampled()) { } else if (texture->Is<ast::type::SampledTextureType>()) {
/* nothing to emit */ /* nothing to emit */
} else if (texture->Is<ast::type::MultisampledTextureType>()) { } else if (texture->Is<ast::type::MultisampledTextureType>()) {
out_ << "multisampled_"; out_ << "multisampled_";
@ -520,8 +520,8 @@ bool GeneratorImpl::EmitType(ast::type::Type* type) {
return false; return false;
} }
if (texture->IsSampled()) { if (texture->Is<ast::type::SampledTextureType>()) {
auto* sampled = texture->AsSampled(); auto* sampled = texture->As<ast::type::SampledTextureType>();
out_ << "<"; out_ << "<";
if (!EmitType(sampled->type())) { if (!EmitType(sampled->type())) {