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>
This commit is contained in:
David Neto 2021-01-19 03:29:35 +00:00 committed by Commit Bot service account
parent 71012dcc2f
commit ed14524b1e
6 changed files with 85 additions and 34 deletions

View File

@ -817,6 +817,7 @@ source_set("tint_unittests_core_src") {
"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",

View File

@ -446,6 +446,7 @@ if(${TINT_BUILD_TESTS})
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

View File

@ -72,6 +72,24 @@ bool IsTextureArray(TextureDimension dim) {
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;

View File

@ -46,6 +46,15 @@ std::ostream& operator<<(std::ostream& out, TextureDimension dim);
/// @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:

View File

@ -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

View File

@ -4340,41 +4340,12 @@ ast::ExpressionList FunctionEmitter::MakeCoordinateOperandsForImageAccess(
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;