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

Change-Id: I2f1785cec1880f728fbbf0c75762bc957e34ca0a
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/34276
Reviewed-by: dan sinclair <dsinclair@chromium.org>
This commit is contained in:
Ben Clayton 2020-11-30 23:30:58 +00:00
parent 16ec1bb626
commit 09b8829d8e
15 changed files with 39 additions and 52 deletions

View File

@ -41,10 +41,6 @@ DepthTextureType::DepthTextureType(DepthTextureType&&) = default;
DepthTextureType::~DepthTextureType() = default;
bool DepthTextureType::IsDepth() const {
return true;
}
std::string DepthTextureType::type_name() const {
std::ostringstream out;
out << "__depth_texture_" << dim();

View File

@ -33,9 +33,6 @@ class DepthTextureType : public Castable<DepthTextureType, TextureType> {
DepthTextureType(DepthTextureType&&);
~DepthTextureType() override;
/// @returns true if the type is a depth texture type
bool IsDepth() const override;
/// @returns the name for this type
std::string type_name() const override;
};

View File

@ -54,7 +54,7 @@ TEST_F(DepthTextureTypeTest, Is) {
TEST_F(DepthTextureTypeTest, IsTextureType) {
DepthTextureType d(TextureDimension::kCube);
EXPECT_TRUE(d.IsDepth());
EXPECT_TRUE(d.Is<DepthTextureType>());
EXPECT_FALSE(d.IsSampled());
EXPECT_FALSE(d.IsStorage());
}

View File

@ -18,6 +18,7 @@
#include "src/ast/type/access_control_type.h"
#include "src/ast/type/array_type.h"
#include "src/ast/type/bool_type.h"
#include "src/ast/type/depth_texture_type.h"
#include "src/ast/type/f32_type.h"
#include "src/ast/type/i32_type.h"
#include "src/ast/type/matrix_type.h"
@ -55,10 +56,11 @@ TEST_F(MultisampledTextureTypeTest, Is) {
TEST_F(MultisampledTextureTypeTest, IsTextureType) {
F32Type f32;
MultisampledTextureType s(TextureDimension::kCube, &f32);
EXPECT_FALSE(s.IsDepth());
EXPECT_TRUE(s.IsMultisampled());
EXPECT_FALSE(s.IsSampled());
EXPECT_FALSE(s.IsStorage());
TextureType* ty = &s;
EXPECT_FALSE(ty->Is<DepthTextureType>());
EXPECT_TRUE(ty->IsMultisampled());
EXPECT_FALSE(ty->IsSampled());
EXPECT_FALSE(ty->IsStorage());
}
TEST_F(MultisampledTextureTypeTest, Dim) {

View File

@ -18,6 +18,7 @@
#include "src/ast/type/access_control_type.h"
#include "src/ast/type/array_type.h"
#include "src/ast/type/bool_type.h"
#include "src/ast/type/depth_texture_type.h"
#include "src/ast/type/f32_type.h"
#include "src/ast/type/i32_type.h"
#include "src/ast/type/matrix_type.h"
@ -55,9 +56,10 @@ TEST_F(SampledTextureTypeTest, Is) {
TEST_F(SampledTextureTypeTest, IsTextureType) {
F32Type f32;
SampledTextureType s(TextureDimension::kCube, &f32);
EXPECT_FALSE(s.IsDepth());
EXPECT_TRUE(s.IsSampled());
EXPECT_FALSE(s.IsStorage());
TextureType* ty = &s;
EXPECT_FALSE(ty->Is<DepthTextureType>());
EXPECT_TRUE(ty->IsSampled());
EXPECT_FALSE(ty->IsStorage());
}
TEST_F(SampledTextureTypeTest, Dim) {

View File

@ -21,6 +21,7 @@
#include "src/ast/type/access_control_type.h"
#include "src/ast/type/array_type.h"
#include "src/ast/type/bool_type.h"
#include "src/ast/type/depth_texture_type.h"
#include "src/ast/type/f32_type.h"
#include "src/ast/type/i32_type.h"
#include "src/ast/type/matrix_type.h"
@ -59,9 +60,10 @@ TEST_F(StorageTextureTypeTest, Is) {
TEST_F(StorageTextureTypeTest, IsTextureType) {
StorageTextureType s(TextureDimension::k2dArray, AccessControl::kReadOnly,
ImageFormat::kRgba32Float);
EXPECT_FALSE(s.IsDepth());
EXPECT_FALSE(s.IsSampled());
EXPECT_TRUE(s.IsStorage());
TextureType* ty = &s;
EXPECT_FALSE(ty->Is<DepthTextureType>());
EXPECT_FALSE(ty->IsSampled());
EXPECT_TRUE(ty->IsStorage());
}
TEST_F(StorageTextureTypeTest, Dim) {

View File

@ -16,7 +16,6 @@
#include <cassert>
#include "src/ast/type/depth_texture_type.h"
#include "src/ast/type/multisampled_texture_type.h"
#include "src/ast/type/sampled_texture_type.h"
#include "src/ast/type/storage_texture_type.h"
@ -61,9 +60,6 @@ TextureType::TextureType(TextureType&&) = default;
TextureType::~TextureType() = default;
bool TextureType::IsDepth() const {
return false;
}
bool TextureType::IsMultisampled() const {
return false;
}
@ -74,11 +70,6 @@ bool TextureType::IsSampled() const {
return false;
}
const DepthTextureType* TextureType::AsDepth() const {
assert(IsDepth());
return static_cast<const DepthTextureType*>(this);
}
const MultisampledTextureType* TextureType::AsMultisampled() const {
assert(IsMultisampled());
return static_cast<const MultisampledTextureType*>(this);
@ -94,11 +85,6 @@ const StorageTextureType* TextureType::AsStorage() const {
return static_cast<const StorageTextureType*>(this);
}
DepthTextureType* TextureType::AsDepth() {
assert(IsDepth());
return static_cast<DepthTextureType*>(this);
}
MultisampledTextureType* TextureType::AsMultisampled() {
assert(IsMultisampled());
return static_cast<MultisampledTextureType*>(this);

View File

@ -63,8 +63,6 @@ class TextureType : public Castable<TextureType, Type> {
/// @returns the texture dimension
TextureDimension dim() const { return dim_; }
/// @returns true if this is a depth texture
virtual bool IsDepth() const;
/// @returns ture if this is a multisampled texture
virtual bool IsMultisampled() const;
/// @returns true if this is a storage texture
@ -72,8 +70,6 @@ class TextureType : public Castable<TextureType, Type> {
/// @returns true if this is a sampled texture
virtual bool IsSampled() const;
/// @returns the texture as a depth texture
const DepthTextureType* AsDepth() const;
/// @returns the texture as a multisampled texture
const MultisampledTextureType* AsMultisampled() const;
/// @returns the texture as a sampled texture
@ -81,8 +77,6 @@ class TextureType : public Castable<TextureType, Type> {
/// @returns the texture as a storage texture
const StorageTextureType* AsStorage() const;
/// @returns the texture as a depth texture
DepthTextureType* AsDepth();
/// @returns the texture as a multisampled texture
MultisampledTextureType* AsMultisampled();
/// @returns the texture as a sampled texture

View File

@ -53,6 +53,7 @@
#include "src/ast/storage_class.h"
#include "src/ast/switch_statement.h"
#include "src/ast/type/bool_type.h"
#include "src/ast/type/depth_texture_type.h"
#include "src/ast/type/f32_type.h"
#include "src/ast/type/pointer_type.h"
#include "src/ast/type/texture_type.h"
@ -3714,7 +3715,7 @@ bool FunctionEmitter::EmitSampledImageAccess(
parser_impl_.GetTypeForHandleVar(*image)) {
if (ast::type::TextureType* texture_type =
type->type()->As<ast::type::TextureType>()) {
if (texture_type->IsDepth()) {
if (texture_type->Is<ast::type::DepthTextureType>()) {
// Convert it to an unsigned integer type.
lod_operand = ast_module_.create<ast::TypeConstructorExpression>(
ast_module_.create<ast::type::U32Type>(),

View File

@ -37,7 +37,8 @@ TEST_F(ParserImplTest, DepthTextureType_2d) {
EXPECT_FALSE(t.errored);
ASSERT_NE(t.value, nullptr);
ASSERT_TRUE(t->Is<ast::type::TextureType>());
ASSERT_TRUE(t->As<ast::type::TextureType>()->IsDepth());
ASSERT_TRUE(
t->As<ast::type::TextureType>()->Is<ast::type::DepthTextureType>());
EXPECT_EQ(t->As<ast::type::TextureType>()->dim(),
ast::type::TextureDimension::k2d);
EXPECT_FALSE(p->has_error());
@ -50,7 +51,8 @@ TEST_F(ParserImplTest, DepthTextureType_2dArray) {
EXPECT_FALSE(t.errored);
ASSERT_NE(t.value, nullptr);
ASSERT_TRUE(t->Is<ast::type::TextureType>());
ASSERT_TRUE(t->As<ast::type::TextureType>()->IsDepth());
ASSERT_TRUE(
t->As<ast::type::TextureType>()->Is<ast::type::DepthTextureType>());
EXPECT_EQ(t->As<ast::type::TextureType>()->dim(),
ast::type::TextureDimension::k2dArray);
EXPECT_FALSE(p->has_error());
@ -63,7 +65,8 @@ TEST_F(ParserImplTest, DepthTextureType_Cube) {
EXPECT_FALSE(t.errored);
ASSERT_NE(t.value, nullptr);
ASSERT_TRUE(t->Is<ast::type::TextureType>());
ASSERT_TRUE(t->As<ast::type::TextureType>()->IsDepth());
ASSERT_TRUE(
t->As<ast::type::TextureType>()->Is<ast::type::DepthTextureType>());
EXPECT_EQ(t->As<ast::type::TextureType>()->dim(),
ast::type::TextureDimension::kCube);
EXPECT_FALSE(p->has_error());
@ -76,7 +79,8 @@ TEST_F(ParserImplTest, DepthTextureType_CubeArray) {
EXPECT_FALSE(t.errored);
ASSERT_NE(t.value, nullptr);
ASSERT_TRUE(t->Is<ast::type::TextureType>());
ASSERT_TRUE(t->As<ast::type::TextureType>()->IsDepth());
ASSERT_TRUE(
t->As<ast::type::TextureType>()->Is<ast::type::DepthTextureType>());
EXPECT_EQ(t->As<ast::type::TextureType>()->dim(),
ast::type::TextureDimension::kCubeArray);
EXPECT_FALSE(p->has_error());

View File

@ -13,6 +13,7 @@
// limitations under the License.
#include "gtest/gtest.h"
#include "src/ast/type/depth_texture_type.h"
#include "src/ast/type/f32_type.h"
#include "src/ast/type/i32_type.h"
#include "src/ast/type/multisampled_texture_type.h"
@ -66,7 +67,7 @@ TEST_F(ParserImplTest, TextureSamplerTypes_DepthTexture) {
EXPECT_FALSE(t.errored);
ASSERT_NE(t.value, nullptr);
ASSERT_TRUE(t->Is<ast::type::TextureType>());
ASSERT_TRUE(t->As<ast::type::TextureType>()->IsDepth());
ASSERT_TRUE(t->Is<ast::type::DepthTextureType>());
EXPECT_EQ(t->As<ast::type::TextureType>()->dim(),
ast::type::TextureDimension::k2d);
}

View File

@ -38,6 +38,7 @@
#include "src/ast/switch_statement.h"
#include "src/ast/type/array_type.h"
#include "src/ast/type/bool_type.h"
#include "src/ast/type/depth_texture_type.h"
#include "src/ast/type/f32_type.h"
#include "src/ast/type/i32_type.h"
#include "src/ast/type/matrix_type.h"
@ -665,7 +666,7 @@ bool TypeDeterminer::DetermineIntrinsic(ast::IdentifierExpression* ident,
ident->set_intrinsic_signature(
std::make_unique<ast::intrinsic::TextureSignature>(param));
if (texture->IsDepth()) {
if (texture->Is<ast::type::DepthTextureType>()) {
expr->func()->set_result_type(mod_->create<ast::type::F32Type>());
return true;
}

View File

@ -1842,7 +1842,7 @@ bool GeneratorImpl::EmitType(ast::type::Type* type, const std::string& name) {
} else if (type->Is<ast::type::TextureType>()) {
auto* tex = type->As<ast::type::TextureType>();
if (tex->IsDepth()) {
if (tex->Is<ast::type::DepthTextureType>()) {
out_ << "depth";
} else {
out_ << "texture";
@ -1878,7 +1878,7 @@ bool GeneratorImpl::EmitType(ast::type::Type* type, const std::string& name) {
out_ << "_ms";
}
out_ << "<";
if (tex->IsDepth()) {
if (tex->Is<ast::type::DepthTextureType>()) {
out_ << "float, access::sample";
} else if (tex->IsStorage()) {
auto* storage = tex->AsStorage();

View File

@ -2521,23 +2521,24 @@ bool Builder::GenerateTextureType(ast::type::TextureType* texture,
}
uint32_t depth_literal = 0u;
if (texture->IsDepth()) {
if (texture->Is<ast::type::DepthTextureType>()) {
depth_literal = 1u;
}
uint32_t sampled_literal = 2u;
if (texture->IsMultisampled() || texture->IsSampled() || texture->IsDepth()) {
if (texture->IsMultisampled() || texture->IsSampled() ||
texture->Is<ast::type::DepthTextureType>()) {
sampled_literal = 1u;
}
if (dim == ast::type::TextureDimension::kCubeArray) {
if (texture->IsSampled() || texture->IsDepth()) {
if (texture->IsSampled() || texture->Is<ast::type::DepthTextureType>()) {
push_capability(SpvCapabilitySampledCubeArray);
}
}
uint32_t type_id = 0u;
if (texture->IsDepth()) {
if (texture->Is<ast::type::DepthTextureType>()) {
ast::type::F32Type f32;
type_id = GenerateTypeIfNeeded(&f32);
} else if (texture->IsSampled()) {

View File

@ -470,7 +470,7 @@ bool GeneratorImpl::EmitType(ast::type::Type* type) {
auto* texture = type->As<ast::type::TextureType>();
out_ << "texture_";
if (texture->IsDepth()) {
if (texture->Is<ast::type::DepthTextureType>()) {
out_ << "depth_";
} else if (texture->IsSampled()) {
/* nothing to emit */