Refactor getting number of coordinate dimensions
Change-Id: Ibafffb29bc33c722b8a4da25ed7a9c1986d13a24
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/38162
Commit-Queue: dan sinclair <dsinclair@chromium.org>
Reviewed-by: dan sinclair <dsinclair@chromium.org>
Auto-Submit: David Neto <dneto@google.com>
diff --git a/BUILD.gn b/BUILD.gn
index d93cb69..112e76e 100644
--- a/BUILD.gn
+++ b/BUILD.gn
@@ -817,6 +817,7 @@
"src/ast/type/sampler_type_test.cc",
"src/ast/type/storage_texture_type_test.cc",
"src/ast/type/struct_type_test.cc",
+ "src/ast/type/texture_type_test.cc",
"src/ast/type/u32_type_test.cc",
"src/ast/type/vector_type_test.cc",
"src/ast/type_constructor_expression_test.cc",
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index d23d6bf..8cc4777 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -446,6 +446,7 @@
ast/type/sampler_type_test.cc
ast/type/storage_texture_type_test.cc
ast/type/struct_type_test.cc
+ ast/type/texture_type_test.cc
ast/type/u32_type_test.cc
ast/type/vector_type_test.cc
ast/traits_test.cc
diff --git a/src/ast/type/texture_type.cc b/src/ast/type/texture_type.cc
index 2914fb6..58a0d47 100644
--- a/src/ast/type/texture_type.cc
+++ b/src/ast/type/texture_type.cc
@@ -72,6 +72,24 @@
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;
diff --git a/src/ast/type/texture_type.h b/src/ast/type/texture_type.h
index d44f5e2..495afd9 100644
--- a/src/ast/type/texture_type.h
+++ b/src/ast/type/texture_type.h
@@ -46,6 +46,15 @@
/// @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:
diff --git a/src/ast/type/texture_type_test.cc b/src/ast/type/texture_type_test.cc
new file mode 100644
index 0000000..3ed1921
--- /dev/null
+++ b/src/ast/type/texture_type_test.cc
@@ -0,0 +1,51 @@
+// 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/ast/type/texture_type.h"
+
+#include "src/ast/test_helper.h"
+
+namespace tint {
+namespace ast {
+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 ast
+} // namespace tint
diff --git a/src/reader/spirv/function.cc b/src/reader/spirv/function.cc
index abd6e25..af6e3b5 100644
--- a/src/reader/spirv/function.cc
+++ b/src/reader/spirv/function.cc
@@ -4340,41 +4340,12 @@
ast::type::TextureDimension dim =
unwrapped_type->As<ast::type::Texture>()->dim();
// Number of regular coordinates.
- uint32_t num_axes = 0;
- bool is_arrayed = false;
- switch (dim) {
- case ast::type::TextureDimension::k1d:
- num_axes = 1;
- break;
- case ast::type::TextureDimension::k1dArray:
- num_axes = 1;
- is_arrayed = true;
- break;
- case ast::type::TextureDimension::k2d:
- num_axes = 2;
- break;
- case ast::type::TextureDimension::k2dArray:
- num_axes = 2;
- is_arrayed = true;
- break;
- case ast::type::TextureDimension::k3d:
- num_axes = 3;
- break;
- case ast::type::TextureDimension::kCube:
- // For cubes, 3 coordinates form a direction vector.
- num_axes = 3;
- break;
- case ast::type::TextureDimension::kCubeArray:
- // For cubes, 3 coordinates form a direction vector.
- num_axes = 3;
- is_arrayed = true;
- break;
- default:
- Fail() << "unsupported image dimensionality for " << type->type_name()
- << " prompted by " << inst.PrettyPrint();
- return {};
+ uint32_t num_axes = ast::type::NumCoordinateAxes(dim);
+ bool is_arrayed = ast::type::IsTextureArray(dim);
+ if ((num_axes == 0) || (num_axes > 3)) {
+ Fail() << "unsupported image dimensionality for " << type->type_name()
+ << " prompted by " << inst.PrettyPrint();
}
- assert(num_axes <= 3);
const auto num_coords_required = num_axes + (is_arrayed ? 1 : 0);
uint32_t num_coords_supplied = 0;
auto* component_type = raw_coords.type;