type::StorageTexture: Remove set_type()

The subtype of a storage texture is statically determinisic from the ImageFormat, and does not need to be late-set by the TypeDeterminer.

Add StorageTexture::SubtypeFor() helper for returning the subtype for a given ImageFormat, and add the subtype as another immutable constructor parameter.

Bug: tint:390
Change-Id: Ibe732293e3142064b60f4e666a7eb39ae8db50e7
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/40064
Commit-Queue: Ben Clayton <bclayton@google.com>
Reviewed-by: David Neto <dneto@google.com>
This commit is contained in:
Ben Clayton
2021-02-03 20:28:26 +00:00
committed by Commit Bot service account
parent b17aea159c
commit 48b384168c
14 changed files with 168 additions and 143 deletions

View File

@@ -141,16 +141,10 @@ std::ostream& operator<<(std::ostream& out, ImageFormat format) {
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(TextureDimension dim,
ImageFormat format,
type::Type* subtype)
: Base(dim), image_format_(format), subtype_(subtype) {}
StorageTexture::StorageTexture(StorageTexture&&) = default;
@@ -163,7 +157,62 @@ std::string StorageTexture::type_name() const {
}
StorageTexture* StorageTexture::Clone(CloneContext* ctx) const {
return ctx->dst->create<StorageTexture>(dim(), image_format_);
return ctx->dst->create<StorageTexture>(dim(), image_format_,
ctx->Clone(subtype_));
}
type::Type* StorageTexture::SubtypeFor(type::ImageFormat format,
ProgramBuilder* builder) {
switch (format) {
case type::ImageFormat::kR8Uint:
case type::ImageFormat::kR16Uint:
case type::ImageFormat::kRg8Uint:
case type::ImageFormat::kR32Uint:
case type::ImageFormat::kRg16Uint:
case type::ImageFormat::kRgba8Uint:
case type::ImageFormat::kRg32Uint:
case type::ImageFormat::kRgba16Uint:
case type::ImageFormat::kRgba32Uint: {
return builder->create<type::U32>();
}
case type::ImageFormat::kR8Sint:
case type::ImageFormat::kR16Sint:
case type::ImageFormat::kRg8Sint:
case type::ImageFormat::kR32Sint:
case type::ImageFormat::kRg16Sint:
case type::ImageFormat::kRgba8Sint:
case type::ImageFormat::kRg32Sint:
case type::ImageFormat::kRgba16Sint:
case type::ImageFormat::kRgba32Sint: {
return builder->create<type::I32>();
}
case type::ImageFormat::kR8Unorm:
case type::ImageFormat::kRg8Unorm:
case type::ImageFormat::kRgba8Unorm:
case type::ImageFormat::kRgba8UnormSrgb:
case type::ImageFormat::kBgra8Unorm:
case type::ImageFormat::kBgra8UnormSrgb:
case type::ImageFormat::kRgb10A2Unorm:
case type::ImageFormat::kR8Snorm:
case type::ImageFormat::kRg8Snorm:
case type::ImageFormat::kRgba8Snorm:
case type::ImageFormat::kR16Float:
case type::ImageFormat::kR32Float:
case type::ImageFormat::kRg16Float:
case type::ImageFormat::kRg11B10Float:
case type::ImageFormat::kRg32Float:
case type::ImageFormat::kRgba16Float:
case type::ImageFormat::kRgba32Float: {
return builder->create<type::F32>();
}
case type::ImageFormat::kNone:
break;
}
return nullptr;
}
} // namespace type

View File

@@ -69,17 +69,15 @@ class StorageTexture : public Castable<StorageTexture, Texture> {
/// Constructor
/// @param dim the dimensionality of the texture
/// @param format the image format of the texture
StorageTexture(TextureDimension dim, ImageFormat format);
/// @param subtype the storage subtype. Use SubtypeFor() to calculate this.
StorageTexture(TextureDimension dim, ImageFormat format, type::Type* subtype);
/// 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 storage subtype
Type* type() const { return subtype_; }
/// @returns the image format
ImageFormat image_format() const { return image_format_; }
@@ -92,10 +90,14 @@ class StorageTexture : public Castable<StorageTexture, Texture> {
/// @return the newly cloned type
StorageTexture* Clone(CloneContext* ctx) const override;
/// @param format the storage texture image format
/// @param builder the ProgramBuilder used to build the returned type
/// @returns the storage texture subtype for the given ImageFormat
static type::Type* SubtypeFor(ImageFormat format, ProgramBuilder* builder);
private:
ImageFormat const image_format_;
Type* type_ = nullptr; // Semantic info
Type* const subtype_;
};
} // namespace type

View File

@@ -1,4 +1,4 @@
// Copyright 2020 The Tint Authors.
// 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.
@@ -39,8 +39,10 @@ namespace {
using StorageTextureTest = TestHelper;
TEST_F(StorageTextureTest, Is) {
StorageTexture s(TextureDimension::k2dArray, ImageFormat::kRgba32Float);
Type* ty = &s;
auto* subtype = StorageTexture::SubtypeFor(ImageFormat::kRgba32Float, this);
auto* s = create<StorageTexture>(TextureDimension::k2dArray,
ImageFormat::kRgba32Float, subtype);
Type* ty = s;
EXPECT_FALSE(ty->Is<AccessControl>());
EXPECT_FALSE(ty->Is<Alias>());
EXPECT_FALSE(ty->Is<Array>());
@@ -57,31 +59,41 @@ TEST_F(StorageTextureTest, Is) {
}
TEST_F(StorageTextureTest, IsTexture) {
StorageTexture s(TextureDimension::k2dArray, ImageFormat::kRgba32Float);
Texture* ty = &s;
auto* subtype = StorageTexture::SubtypeFor(ImageFormat::kRgba32Float, this);
auto* s = create<StorageTexture>(TextureDimension::k2dArray,
ImageFormat::kRgba32Float, subtype);
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);
auto* subtype = StorageTexture::SubtypeFor(ImageFormat::kRgba32Float, this);
auto* s = create<StorageTexture>(TextureDimension::k2dArray,
ImageFormat::kRgba32Float, subtype);
EXPECT_EQ(s->dim(), TextureDimension::k2dArray);
}
TEST_F(StorageTextureTest, Format) {
StorageTexture s(TextureDimension::k2dArray, ImageFormat::kRgba32Float);
EXPECT_EQ(s.image_format(), ImageFormat::kRgba32Float);
auto* subtype = StorageTexture::SubtypeFor(ImageFormat::kRgba32Float, this);
auto* s = create<StorageTexture>(TextureDimension::k2dArray,
ImageFormat::kRgba32Float, subtype);
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");
auto* subtype = StorageTexture::SubtypeFor(ImageFormat::kRgba32Float, this);
auto* s = create<StorageTexture>(TextureDimension::k2dArray,
ImageFormat::kRgba32Float, subtype);
EXPECT_EQ(s->type_name(), "__storage_texture_2d_array_rgba32float");
}
TEST_F(StorageTextureTest, F32) {
auto* subtype =
type::StorageTexture::SubtypeFor(ImageFormat::kRgba32Float, this);
Type* s = create<StorageTexture>(TextureDimension::k2dArray,
ImageFormat::kRgba32Float);
ImageFormat::kRgba32Float, subtype);
TypeDeterminer td(this);
ASSERT_TRUE(td.Determine()) << td.error();
@@ -91,8 +103,10 @@ TEST_F(StorageTextureTest, F32) {
}
TEST_F(StorageTextureTest, U32) {
auto* subtype =
type::StorageTexture::SubtypeFor(ImageFormat::kRg32Uint, this);
Type* s = create<StorageTexture>(TextureDimension::k2dArray,
ImageFormat::kRg32Uint);
ImageFormat::kRg32Uint, subtype);
TypeDeterminer td(this);
ASSERT_TRUE(td.Determine()) << td.error();
@@ -102,8 +116,10 @@ TEST_F(StorageTextureTest, U32) {
}
TEST_F(StorageTextureTest, I32) {
auto* subtype =
type::StorageTexture::SubtypeFor(ImageFormat::kRgba32Sint, this);
Type* s = create<StorageTexture>(TextureDimension::k2dArray,
ImageFormat::kRgba32Sint);
ImageFormat::kRgba32Sint, subtype);
TypeDeterminer td(this);
ASSERT_TRUE(td.Determine()) << td.error();
@@ -113,8 +129,10 @@ TEST_F(StorageTextureTest, I32) {
}
TEST_F(StorageTextureTest, MinBufferBindingSize) {
StorageTexture s(TextureDimension::k2dArray, ImageFormat::kRgba32Sint);
EXPECT_EQ(0u, s.MinBufferBindingSize(MemoryLayout::kUniformBuffer));
auto* subtype = StorageTexture::SubtypeFor(ImageFormat::kRgba32Sint, this);
auto* s = create<StorageTexture>(TextureDimension::k2dArray,
ImageFormat::kRgba32Sint, subtype);
EXPECT_EQ(0u, s->MinBufferBindingSize(MemoryLayout::kUniformBuffer));
}
} // namespace