Add GLSL distance support.

This CL adds support for the GLSL distance command.

Bug: tint:5
Change-Id: I0934d461ca6d4fca379849d8f41a33fb202c67ff
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/20060
Reviewed-by: David Neto <dneto@google.com>
This commit is contained in:
dan sinclair 2020-04-21 13:04:15 +00:00
parent 2ee4a7e0c9
commit 54444382a6
2 changed files with 193 additions and 22 deletions

View File

@ -595,7 +595,8 @@ ast::type::Type* TypeDeterminer::GetImportData(
name == "acos" || name == "atan" || name == "sinh" || name == "cosh" ||
name == "tanh" || name == "asinh" || name == "acosh" || name == "atanh" ||
name == "exp" || name == "log" || name == "exp2" || name == "log2" ||
name == "sqrt" || name == "inversesqrt" || name == "normalize") {
name == "sqrt" || name == "inversesqrt" || name == "normalize" ||
name == "length") {
if (params.size() != 1) {
error_ = "incorrect number of parameters for " + name +
". Expected 1 got " + std::to_string(params.size());
@ -607,6 +608,7 @@ ast::type::Type* TypeDeterminer::GetImportData(
return nullptr;
}
auto* result_type = params[0]->result_type();
if (name == "round") {
*id = GLSLstd450Round;
} else if (name == "roundeven") {
@ -665,30 +667,18 @@ ast::type::Type* TypeDeterminer::GetImportData(
*id = GLSLstd450InverseSqrt;
} else if (name == "normalize") {
*id = GLSLstd450Normalize;
}
return params[0]->result_type();
} else if (name == "length") {
if (params.size() != 1) {
error_ = "incorrect number of parameters for " + name +
". Expected 1 got " + std::to_string(params.size());
return nullptr;
}
if (!params[0]->result_type()->is_float_scalar_or_vector()) {
error_ = "incorrect type for " + name +
". Requires a float scalar or a float vector";
return nullptr;
}
*id = GLSLstd450Length;
auto* result_type = params[0]->result_type();
// Length returns a scalar of the same type as the parameter.
return result_type->is_float_scalar() ? result_type
: result_type->AsVector()->type();
}
return result_type;
} else if (name == "atan2" || name == "pow" || name == "fmin" ||
name == "fmax" || name == "step" || name == "reflect" ||
name == "nmin" || name == "nmax") {
name == "nmin" || name == "nmax" || name == "distance") {
if (params.size() != 2) {
error_ = "incorrect number of parameters for " + name +
". Expected 2 got " + std::to_string(params.size());
@ -705,6 +695,7 @@ ast::type::Type* TypeDeterminer::GetImportData(
return nullptr;
}
auto* result_type = params[0]->result_type();
if (name == "atan2") {
*id = GLSLstd450Atan2;
} else if (name == "pow") {
@ -721,9 +712,15 @@ ast::type::Type* TypeDeterminer::GetImportData(
*id = GLSLstd450NMin;
} else if (name == "nmax") {
*id = GLSLstd450NMax;
} else if (name == "distance") {
*id = GLSLstd450Distance;
// Distance returns a scalar of the same type as the parameter.
return result_type->is_float_scalar() ? result_type
: result_type->AsVector()->type();
}
return params[0]->result_type();
return result_type;
}
return nullptr;

View File

@ -1910,5 +1910,179 @@ INSTANTIATE_TEST_SUITE_P(TypeDeterminerTest,
GLSLData{"nmin", GLSLstd450NMin},
GLSLData{"nmax", GLSLstd450NMax}));
TEST_F(TypeDeterminerTest, ImportData_Distance_Scalar) {
ast::type::F32Type f32;
ast::ExpressionList params;
params.push_back(std::make_unique<ast::ScalarConstructorExpression>(
std::make_unique<ast::FloatLiteral>(&f32, 1.f)));
params.push_back(std::make_unique<ast::ScalarConstructorExpression>(
std::make_unique<ast::FloatLiteral>(&f32, 1.f)));
ASSERT_TRUE(td()->DetermineResultType(params)) << td()->error();
uint32_t id = 0;
auto* type = td()->GetImportData("GLSL.std.450", "distance", params, &id);
ASSERT_NE(type, nullptr);
EXPECT_TRUE(type->is_float_scalar());
EXPECT_EQ(id, GLSLstd450Distance);
}
TEST_F(TypeDeterminerTest, ImportData_Distance_Vector) {
ast::type::F32Type f32;
ast::type::VectorType vec(&f32, 3);
ast::ExpressionList vals_1;
vals_1.push_back(std::make_unique<ast::ScalarConstructorExpression>(
std::make_unique<ast::FloatLiteral>(&f32, 1.0f)));
vals_1.push_back(std::make_unique<ast::ScalarConstructorExpression>(
std::make_unique<ast::FloatLiteral>(&f32, 1.0f)));
vals_1.push_back(std::make_unique<ast::ScalarConstructorExpression>(
std::make_unique<ast::FloatLiteral>(&f32, 3.0f)));
ast::ExpressionList vals_2;
vals_2.push_back(std::make_unique<ast::ScalarConstructorExpression>(
std::make_unique<ast::FloatLiteral>(&f32, 1.0f)));
vals_2.push_back(std::make_unique<ast::ScalarConstructorExpression>(
std::make_unique<ast::FloatLiteral>(&f32, 1.0f)));
vals_2.push_back(std::make_unique<ast::ScalarConstructorExpression>(
std::make_unique<ast::FloatLiteral>(&f32, 3.0f)));
ast::ExpressionList params;
params.push_back(std::make_unique<ast::TypeConstructorExpression>(
&vec, std::move(vals_1)));
params.push_back(std::make_unique<ast::TypeConstructorExpression>(
&vec, std::move(vals_2)));
ASSERT_TRUE(td()->DetermineResultType(params)) << td()->error();
uint32_t id = 0;
auto* type = td()->GetImportData("GLSL.std.450", "distance", params, &id);
ASSERT_NE(type, nullptr);
EXPECT_TRUE(type->IsF32());
EXPECT_EQ(id, GLSLstd450Distance);
}
TEST_F(TypeDeterminerTest, ImportData_Distance_Error_Integer) {
ast::type::I32Type i32;
ast::ExpressionList params;
params.push_back(std::make_unique<ast::ScalarConstructorExpression>(
std::make_unique<ast::IntLiteral>(&i32, 1)));
params.push_back(std::make_unique<ast::ScalarConstructorExpression>(
std::make_unique<ast::IntLiteral>(&i32, 2)));
ASSERT_TRUE(td()->DetermineResultType(params)) << td()->error();
uint32_t id = 0;
auto* type = td()->GetImportData("GLSL.std.450", "distance", params, &id);
ASSERT_EQ(type, nullptr);
EXPECT_EQ(td()->error(),
"incorrect type for distance. Requires float scalar or a float "
"vector values");
}
TEST_F(TypeDeterminerTest, ImportData_Distance_Error_NoParams) {
ast::ExpressionList params;
uint32_t id = 0;
auto* type = td()->GetImportData("GLSL.std.450", "distance", params, &id);
ASSERT_EQ(type, nullptr);
EXPECT_EQ(td()->error(),
"incorrect number of parameters for distance. Expected 2 got 0");
}
TEST_F(TypeDeterminerTest, ImportData_Distance_Error_OneParam) {
ast::type::F32Type f32;
ast::ExpressionList params;
params.push_back(std::make_unique<ast::ScalarConstructorExpression>(
std::make_unique<ast::FloatLiteral>(&f32, 1.f)));
ASSERT_TRUE(td()->DetermineResultType(params)) << td()->error();
uint32_t id = 0;
auto* type = td()->GetImportData("GLSL.std.450", "distance", params, &id);
ASSERT_EQ(type, nullptr);
EXPECT_EQ(td()->error(),
"incorrect number of parameters for distance. Expected 2 got 1");
}
TEST_F(TypeDeterminerTest, ImportData_Distance_Error_MismatchedParamCount) {
ast::type::F32Type f32;
ast::type::VectorType vec2(&f32, 2);
ast::type::VectorType vec3(&f32, 3);
ast::ExpressionList vals_1;
vals_1.push_back(std::make_unique<ast::ScalarConstructorExpression>(
std::make_unique<ast::FloatLiteral>(&f32, 1.0f)));
vals_1.push_back(std::make_unique<ast::ScalarConstructorExpression>(
std::make_unique<ast::FloatLiteral>(&f32, 1.0f)));
ast::ExpressionList vals_2;
vals_2.push_back(std::make_unique<ast::ScalarConstructorExpression>(
std::make_unique<ast::FloatLiteral>(&f32, 1.0f)));
vals_2.push_back(std::make_unique<ast::ScalarConstructorExpression>(
std::make_unique<ast::FloatLiteral>(&f32, 1.0f)));
vals_2.push_back(std::make_unique<ast::ScalarConstructorExpression>(
std::make_unique<ast::FloatLiteral>(&f32, 3.0f)));
ast::ExpressionList params;
params.push_back(std::make_unique<ast::TypeConstructorExpression>(
&vec2, std::move(vals_1)));
params.push_back(std::make_unique<ast::TypeConstructorExpression>(
&vec3, std::move(vals_2)));
ASSERT_TRUE(td()->DetermineResultType(params)) << td()->error();
uint32_t id = 0;
auto* type = td()->GetImportData("GLSL.std.450", "distance", params, &id);
ASSERT_EQ(type, nullptr);
EXPECT_EQ(td()->error(), "mismatched parameter types for distance");
}
TEST_F(TypeDeterminerTest, ImportData_Distance_Error_MismatchedParamType) {
ast::type::F32Type f32;
ast::type::VectorType vec(&f32, 3);
ast::ExpressionList vals;
vals.push_back(std::make_unique<ast::ScalarConstructorExpression>(
std::make_unique<ast::FloatLiteral>(&f32, 1.0f)));
vals.push_back(std::make_unique<ast::ScalarConstructorExpression>(
std::make_unique<ast::FloatLiteral>(&f32, 1.0f)));
vals.push_back(std::make_unique<ast::ScalarConstructorExpression>(
std::make_unique<ast::FloatLiteral>(&f32, 3.0f)));
ast::ExpressionList params;
params.push_back(std::make_unique<ast::ScalarConstructorExpression>(
std::make_unique<ast::FloatLiteral>(&f32, 1.0f)));
params.push_back(
std::make_unique<ast::TypeConstructorExpression>(&vec, std::move(vals)));
ASSERT_TRUE(td()->DetermineResultType(params)) << td()->error();
uint32_t id = 0;
auto* type = td()->GetImportData("GLSL.std.450", "distance", params, &id);
ASSERT_EQ(type, nullptr);
EXPECT_EQ(td()->error(), "mismatched parameter types for distance");
}
TEST_F(TypeDeterminerTest, ImportData_Distance_Error_TooManyParams) {
ast::type::F32Type f32;
ast::ExpressionList params;
params.push_back(std::make_unique<ast::ScalarConstructorExpression>(
std::make_unique<ast::FloatLiteral>(&f32, 1.f)));
params.push_back(std::make_unique<ast::ScalarConstructorExpression>(
std::make_unique<ast::FloatLiteral>(&f32, 1.f)));
params.push_back(std::make_unique<ast::ScalarConstructorExpression>(
std::make_unique<ast::FloatLiteral>(&f32, 1.f)));
ASSERT_TRUE(td()->DetermineResultType(params)) << td()->error();
uint32_t id = 0;
auto* type = td()->GetImportData("GLSL.std.450", "distance", params, &id);
ASSERT_EQ(type, nullptr);
EXPECT_EQ(td()->error(),
"incorrect number of parameters for distance. Expected 2 got 3");
}
} // namespace
} // namespace tint