mirror of
https://github.com/encounter/dawn-cmake.git
synced 2025-12-12 14:46:08 +00:00
intrinsics: Add degrees() and radians()
Fixed: tint:1329 Change-Id: I5fb927268fc9cb8047a2b365d26e813a8546605a Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/75423 Kokoro: Kokoro <noreply+kokoro@google.com> Reviewed-by: David Neto <dneto@google.com> Commit-Queue: Ben Clayton <bclayton@google.com>
This commit is contained in:
committed by
Tint LUCI CQ
parent
4beeaea9da
commit
5c99ed046a
File diff suppressed because it is too large
Load Diff
@@ -289,6 +289,8 @@ fn cosh<N: num>(vec<N, f32>) -> vec<N, f32>
|
||||
fn countOneBits<T: iu32>(T) -> T
|
||||
fn countOneBits<N: num, T: iu32>(vec<N, T>) -> vec<N, T>
|
||||
fn cross(vec3<f32>, vec3<f32>) -> vec3<f32>
|
||||
fn degrees(f32) -> f32
|
||||
fn degrees<N: num>(vec<N, f32>) -> vec<N, f32>
|
||||
fn determinant<N: num>(mat<N, N, f32>) -> f32
|
||||
fn distance(f32, f32) -> f32
|
||||
fn distance<N: num>(vec<N, f32>, vec<N, f32>) -> f32
|
||||
@@ -360,6 +362,8 @@ fn pack4x8snorm(vec4<f32>) -> u32
|
||||
fn pack4x8unorm(vec4<f32>) -> u32
|
||||
fn pow(f32, f32) -> f32
|
||||
fn pow<N: num>(vec<N, f32>, vec<N, f32>) -> vec<N, f32>
|
||||
fn radians(f32) -> f32
|
||||
fn radians<N: num>(vec<N, f32>) -> vec<N, f32>
|
||||
fn reflect<N: num>(vec<N, f32>, vec<N, f32>) -> vec<N, f32>
|
||||
fn refract<N: num>(vec<N, f32>, vec<N, f32>, f32) -> vec<N, f32>
|
||||
fn reverseBits<T: iu32>(T) -> T
|
||||
|
||||
@@ -312,6 +312,8 @@ std::string GetGlslStd450FuncName(uint32_t ext_opcode) {
|
||||
return "cosh";
|
||||
case GLSLstd450Cross:
|
||||
return "cross";
|
||||
case GLSLstd450Degrees:
|
||||
return "degrees";
|
||||
case GLSLstd450Distance:
|
||||
return "distance";
|
||||
case GLSLstd450Exp:
|
||||
@@ -364,6 +366,8 @@ std::string GetGlslStd450FuncName(uint32_t ext_opcode) {
|
||||
return "pow";
|
||||
case GLSLstd450FSign:
|
||||
return "sign";
|
||||
case GLSLstd450Radians:
|
||||
return "radians";
|
||||
case GLSLstd450Reflect:
|
||||
return "reflect";
|
||||
case GLSLstd450Refract:
|
||||
@@ -404,8 +408,6 @@ std::string GetGlslStd450FuncName(uint32_t ext_opcode) {
|
||||
|
||||
case GLSLstd450SSign:
|
||||
|
||||
case GLSLstd450Radians:
|
||||
case GLSLstd450Degrees:
|
||||
case GLSLstd450Asinh:
|
||||
case GLSLstd450Acosh:
|
||||
case GLSLstd450Atanh:
|
||||
@@ -4114,41 +4116,6 @@ TypedExpression FunctionEmitter::EmitGlslStd450ExtInst(
|
||||
}
|
||||
}
|
||||
|
||||
// Some GLSLStd450 builtins don't have a WGSL equivalent. Polyfill them.
|
||||
switch (ext_opcode) {
|
||||
case GLSLstd450Radians: {
|
||||
auto degrees = MakeOperand(inst, 2);
|
||||
TINT_ASSERT(Reader, degrees.type->IsFloatScalarOrVector());
|
||||
|
||||
constexpr auto kPiOver180 = static_cast<float>(3.141592653589793 / 180.0);
|
||||
auto* factor = builder_.Expr(kPiOver180);
|
||||
if (degrees.type->Is<F32>()) {
|
||||
return {degrees.type, builder_.Mul(degrees.expr, factor)};
|
||||
} else {
|
||||
uint32_t size = degrees.type->As<Vector>()->size;
|
||||
return {degrees.type,
|
||||
builder_.Mul(degrees.expr,
|
||||
builder_.vec(builder_.ty.f32(), size, factor))};
|
||||
}
|
||||
}
|
||||
|
||||
case GLSLstd450Degrees: {
|
||||
auto radians = MakeOperand(inst, 2);
|
||||
TINT_ASSERT(Reader, radians.type->IsFloatScalarOrVector());
|
||||
|
||||
constexpr auto k180OverPi = static_cast<float>(180.0 / 3.141592653589793);
|
||||
auto* factor = builder_.Expr(k180OverPi);
|
||||
if (radians.type->Is<F32>()) {
|
||||
return {radians.type, builder_.Mul(radians.expr, factor)};
|
||||
} else {
|
||||
uint32_t size = radians.type->As<Vector>()->size;
|
||||
return {radians.type,
|
||||
builder_.Mul(radians.expr,
|
||||
builder_.vec(builder_.ty.f32(), size, factor))};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const auto name = GetGlslStd450FuncName(ext_opcode);
|
||||
if (name.empty()) {
|
||||
Fail() << "unhandled GLSL.std.450 instruction " << ext_opcode;
|
||||
|
||||
@@ -421,29 +421,31 @@ INSTANTIATE_TEST_SUITE_P(Samples,
|
||||
INSTANTIATE_TEST_SUITE_P(Samples,
|
||||
SpvParserTest_GlslStd450_Floating_Floating,
|
||||
::testing::ValuesIn(std::vector<GlslStd450Case>{
|
||||
{"Acos", "acos"},
|
||||
{"Asin", "asin"},
|
||||
{"Atan", "atan"},
|
||||
{"Ceil", "ceil"},
|
||||
{"Cos", "cos"},
|
||||
{"Cosh", "cosh"},
|
||||
{"Exp", "exp"},
|
||||
{"Exp2", "exp2"},
|
||||
{"FAbs", "abs"},
|
||||
{"FSign", "sign"},
|
||||
{"Floor", "floor"},
|
||||
{"Fract", "fract"},
|
||||
{"InverseSqrt", "inverseSqrt"},
|
||||
{"Log", "log"},
|
||||
{"Log2", "log2"},
|
||||
{"Round", "round"},
|
||||
{"RoundEven", "round"},
|
||||
{"Sin", "sin"},
|
||||
{"Sinh", "sinh"},
|
||||
{"Sqrt", "sqrt"},
|
||||
{"Tan", "tan"},
|
||||
{"Tanh", "tanh"},
|
||||
{"Trunc", "trunc"},
|
||||
{"Acos", "acos"}, //
|
||||
{"Asin", "asin"}, //
|
||||
{"Atan", "atan"}, //
|
||||
{"Ceil", "ceil"}, //
|
||||
{"Cos", "cos"}, //
|
||||
{"Cosh", "cosh"}, //
|
||||
{"Degrees", "degrees"}, //
|
||||
{"Exp", "exp"}, //
|
||||
{"Exp2", "exp2"}, //
|
||||
{"FAbs", "abs"}, //
|
||||
{"FSign", "sign"}, //
|
||||
{"Floor", "floor"}, //
|
||||
{"Fract", "fract"}, //
|
||||
{"InverseSqrt", "inverseSqrt"}, //
|
||||
{"Log", "log"}, //
|
||||
{"Log2", "log2"}, //
|
||||
{"Radians", "radians"}, //
|
||||
{"Round", "round"}, //
|
||||
{"RoundEven", "round"}, //
|
||||
{"Sin", "sin"}, //
|
||||
{"Sinh", "sinh"}, //
|
||||
{"Sqrt", "sqrt"}, //
|
||||
{"Tan", "tan"}, //
|
||||
{"Tanh", "tanh"}, //
|
||||
{"Trunc", "trunc"}, //
|
||||
}));
|
||||
|
||||
INSTANTIATE_TEST_SUITE_P(Samples,
|
||||
@@ -1134,76 +1136,6 @@ let x_1 : vec2<f32> = reflect(x_98, x_99);
|
||||
EXPECT_THAT(body, HasSubstr(expected)) << body;
|
||||
}
|
||||
|
||||
TEST_F(SpvParserTest, GlslStd450_Degrees_Scalar) {
|
||||
const auto assembly = Preamble() + R"(
|
||||
%1 = OpExtInst %float %glsl Degrees %float_50
|
||||
OpReturn
|
||||
OpFunctionEnd
|
||||
)";
|
||||
auto p = parser(test::Assemble(assembly));
|
||||
ASSERT_TRUE(p->BuildAndParseInternalModuleExceptFunctions());
|
||||
auto fe = p->function_emitter(100);
|
||||
EXPECT_TRUE(fe.EmitBody()) << p->error();
|
||||
auto ast_body = fe.ast_body();
|
||||
const auto body = test::ToString(p->program(), ast_body);
|
||||
const auto* expected = "let x_1 : f32 = (50.0 * 57.295780182);";
|
||||
|
||||
EXPECT_THAT(body, HasSubstr(expected)) << body;
|
||||
}
|
||||
|
||||
TEST_F(SpvParserTest, GlslStd450_Degrees_Vector) {
|
||||
const auto assembly = Preamble() + R"(
|
||||
%1 = OpExtInst %v3float %glsl Degrees %v3float_60_70_50
|
||||
OpReturn
|
||||
OpFunctionEnd
|
||||
)";
|
||||
auto p = parser(test::Assemble(assembly));
|
||||
ASSERT_TRUE(p->BuildAndParseInternalModuleExceptFunctions());
|
||||
auto fe = p->function_emitter(100);
|
||||
EXPECT_TRUE(fe.EmitBody()) << p->error();
|
||||
auto ast_body = fe.ast_body();
|
||||
const auto body = test::ToString(p->program(), ast_body);
|
||||
const auto* expected =
|
||||
R"(let x_1 : vec3<f32> = (vec3<f32>(60.0, 70.0, 50.0) * vec3<f32>(57.295780182));)";
|
||||
|
||||
EXPECT_THAT(body, HasSubstr(expected)) << body;
|
||||
}
|
||||
|
||||
TEST_F(SpvParserTest, GlslStd450_Radians_Scalar) {
|
||||
const auto assembly = Preamble() + R"(
|
||||
%1 = OpExtInst %float %glsl Radians %float_50
|
||||
OpReturn
|
||||
OpFunctionEnd
|
||||
)";
|
||||
auto p = parser(test::Assemble(assembly));
|
||||
ASSERT_TRUE(p->BuildAndParseInternalModuleExceptFunctions());
|
||||
auto fe = p->function_emitter(100);
|
||||
EXPECT_TRUE(fe.EmitBody()) << p->error();
|
||||
auto ast_body = fe.ast_body();
|
||||
const auto body = test::ToString(p->program(), ast_body);
|
||||
const auto* expected = "let x_1 : f32 = (50.0 * 0.017453292);";
|
||||
|
||||
EXPECT_THAT(body, HasSubstr(expected)) << body;
|
||||
}
|
||||
|
||||
TEST_F(SpvParserTest, GlslStd450_Radians_Vector) {
|
||||
const auto assembly = Preamble() + R"(
|
||||
%1 = OpExtInst %v3float %glsl Radians %v3float_60_70_50
|
||||
OpReturn
|
||||
OpFunctionEnd
|
||||
)";
|
||||
auto p = parser(test::Assemble(assembly));
|
||||
ASSERT_TRUE(p->BuildAndParseInternalModuleExceptFunctions());
|
||||
auto fe = p->function_emitter(100);
|
||||
EXPECT_TRUE(fe.EmitBody()) << p->error();
|
||||
auto ast_body = fe.ast_body();
|
||||
const auto body = test::ToString(p->program(), ast_body);
|
||||
const auto* expected =
|
||||
R"(let x_1 : vec3<f32> = (vec3<f32>(60.0, 70.0, 50.0) * vec3<f32>(0.017453292));)";
|
||||
|
||||
EXPECT_THAT(body, HasSubstr(expected)) << body;
|
||||
}
|
||||
|
||||
// For ldexp with signed second argument, see above.
|
||||
TEST_F(SpvParserTest, GlslStd450_Ldexp_Scalar_Float_Uint) {
|
||||
const auto assembly = Preamble() + R"(
|
||||
|
||||
@@ -145,6 +145,12 @@ class Intrinsic : public Castable<Intrinsic, CallTarget> {
|
||||
const bool is_deprecated_;
|
||||
};
|
||||
|
||||
/// Constant value used by the degrees() intrinsic
|
||||
static constexpr double kRadToDeg = 57.295779513082322865;
|
||||
|
||||
/// Constant value used by the radians() intrinsic
|
||||
static constexpr double kDegToRad = 0.017453292519943295474;
|
||||
|
||||
} // namespace sem
|
||||
} // namespace tint
|
||||
|
||||
|
||||
@@ -72,6 +72,9 @@ IntrinsicType ParseIntrinsicType(const std::string& name) {
|
||||
if (name == "cross") {
|
||||
return IntrinsicType::kCross;
|
||||
}
|
||||
if (name == "degrees") {
|
||||
return IntrinsicType::kDegrees;
|
||||
}
|
||||
if (name == "determinant") {
|
||||
return IntrinsicType::kDeterminant;
|
||||
}
|
||||
@@ -192,6 +195,9 @@ IntrinsicType ParseIntrinsicType(const std::string& name) {
|
||||
if (name == "pow") {
|
||||
return IntrinsicType::kPow;
|
||||
}
|
||||
if (name == "radians") {
|
||||
return IntrinsicType::kRadians;
|
||||
}
|
||||
if (name == "reflect") {
|
||||
return IntrinsicType::kReflect;
|
||||
}
|
||||
@@ -368,6 +374,8 @@ const char* str(IntrinsicType i) {
|
||||
return "countOneBits";
|
||||
case IntrinsicType::kCross:
|
||||
return "cross";
|
||||
case IntrinsicType::kDegrees:
|
||||
return "degrees";
|
||||
case IntrinsicType::kDeterminant:
|
||||
return "determinant";
|
||||
case IntrinsicType::kDistance:
|
||||
@@ -448,6 +456,8 @@ const char* str(IntrinsicType i) {
|
||||
return "pack4x8unorm";
|
||||
case IntrinsicType::kPow:
|
||||
return "pow";
|
||||
case IntrinsicType::kRadians:
|
||||
return "radians";
|
||||
case IntrinsicType::kReflect:
|
||||
return "reflect";
|
||||
case IntrinsicType::kRefract:
|
||||
|
||||
@@ -48,6 +48,7 @@ enum class IntrinsicType {
|
||||
kCosh,
|
||||
kCountOneBits,
|
||||
kCross,
|
||||
kDegrees,
|
||||
kDeterminant,
|
||||
kDistance,
|
||||
kDot,
|
||||
@@ -88,6 +89,7 @@ enum class IntrinsicType {
|
||||
kPack4x8snorm,
|
||||
kPack4x8unorm,
|
||||
kPow,
|
||||
kRadians,
|
||||
kReflect,
|
||||
kRefract,
|
||||
kReverseBits,
|
||||
|
||||
@@ -505,6 +505,12 @@ bool GeneratorImpl::EmitIntrinsicCall(std::ostream& out,
|
||||
if (intrinsic->Type() == sem::IntrinsicType::kIsNormal) {
|
||||
return EmitIsNormalCall(out, expr, intrinsic);
|
||||
}
|
||||
if (intrinsic->Type() == sem::IntrinsicType::kDegrees) {
|
||||
return EmitDegreesCall(out, expr, intrinsic);
|
||||
}
|
||||
if (intrinsic->Type() == sem::IntrinsicType::kRadians) {
|
||||
return EmitRadiansCall(out, expr, intrinsic);
|
||||
}
|
||||
if (intrinsic->Type() == sem::IntrinsicType::kIgnore) {
|
||||
return EmitExpression(out, expr->args[0]); // [DEPRECATED]
|
||||
}
|
||||
@@ -953,6 +959,30 @@ bool GeneratorImpl::EmitIsNormalCall(std::ostream& out,
|
||||
});
|
||||
}
|
||||
|
||||
bool GeneratorImpl::EmitDegreesCall(std::ostream& out,
|
||||
const ast::CallExpression* expr,
|
||||
const sem::Intrinsic* intrinsic) {
|
||||
return CallIntrinsicHelper(
|
||||
out, expr, intrinsic,
|
||||
[&](TextBuffer* b, const std::vector<std::string>& params) {
|
||||
line(b) << "return " << params[0] << " * " << std::setprecision(20)
|
||||
<< sem::kRadToDeg << ";";
|
||||
return true;
|
||||
});
|
||||
}
|
||||
|
||||
bool GeneratorImpl::EmitRadiansCall(std::ostream& out,
|
||||
const ast::CallExpression* expr,
|
||||
const sem::Intrinsic* intrinsic) {
|
||||
return CallIntrinsicHelper(
|
||||
out, expr, intrinsic,
|
||||
[&](TextBuffer* b, const std::vector<std::string>& params) {
|
||||
line(b) << "return " << params[0] << " * " << std::setprecision(20)
|
||||
<< sem::kDegToRad << ";";
|
||||
return true;
|
||||
});
|
||||
}
|
||||
|
||||
bool GeneratorImpl::EmitDataPackingCall(std::ostream& out,
|
||||
const ast::CallExpression* expr,
|
||||
const sem::Intrinsic* intrinsic) {
|
||||
|
||||
@@ -208,6 +208,22 @@ class GeneratorImpl : public TextGenerator {
|
||||
bool EmitIsNormalCall(std::ostream& out,
|
||||
const ast::CallExpression* expr,
|
||||
const sem::Intrinsic* intrinsic);
|
||||
/// Handles generating a call to the `degrees()` intrinsic
|
||||
/// @param out the output of the expression stream
|
||||
/// @param expr the call expression
|
||||
/// @param intrinsic the semantic information for the intrinsic
|
||||
/// @returns true if the call expression is emitted
|
||||
bool EmitDegreesCall(std::ostream& out,
|
||||
const ast::CallExpression* expr,
|
||||
const sem::Intrinsic* intrinsic);
|
||||
/// Handles generating a call to the `radians()` intrinsic
|
||||
/// @param out the output of the expression stream
|
||||
/// @param expr the call expression
|
||||
/// @param intrinsic the semantic information for the intrinsic
|
||||
/// @returns true if the call expression is emitted
|
||||
bool EmitRadiansCall(std::ostream& out,
|
||||
const ast::CallExpression* expr,
|
||||
const sem::Intrinsic* intrinsic);
|
||||
/// Handles generating a call to data packing intrinsic
|
||||
/// @param out the output of the expression stream
|
||||
/// @param expr the call expression
|
||||
|
||||
@@ -381,7 +381,129 @@ TEST_F(GlslGeneratorImplTest_Intrinsic, IsNormal_Vector) {
|
||||
(tint_isnormal_clamped == tint_isnormal_exponent);
|
||||
)"));
|
||||
}
|
||||
#endif
|
||||
|
||||
TEST_F(GlslGeneratorImplTest_Intrinsic, Degrees_Scalar) {
|
||||
auto* val = Var("val", ty.f32());
|
||||
auto* call = Call("degrees", val);
|
||||
WrapInFunction(val, call);
|
||||
|
||||
GeneratorImpl& gen = SanitizeAndBuild();
|
||||
|
||||
ASSERT_TRUE(gen.Generate()) << gen.error();
|
||||
EXPECT_EQ(gen.result(), R"(#version 310 es
|
||||
precision mediump float;
|
||||
|
||||
float tint_degrees(float param_0) {
|
||||
return param_0 * 57.295779513082322865;
|
||||
}
|
||||
|
||||
|
||||
layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in;
|
||||
void test_function() {
|
||||
float val = 0.0f;
|
||||
float tint_symbol = tint_degrees(val);
|
||||
return;
|
||||
}
|
||||
void main() {
|
||||
test_function();
|
||||
}
|
||||
|
||||
|
||||
)");
|
||||
}
|
||||
|
||||
TEST_F(GlslGeneratorImplTest_Intrinsic, Degrees_Vector) {
|
||||
auto* val = Var("val", ty.vec3<f32>());
|
||||
auto* call = Call("degrees", val);
|
||||
WrapInFunction(val, call);
|
||||
|
||||
GeneratorImpl& gen = SanitizeAndBuild();
|
||||
|
||||
ASSERT_TRUE(gen.Generate()) << gen.error();
|
||||
EXPECT_EQ(gen.result(), R"(#version 310 es
|
||||
precision mediump float;
|
||||
|
||||
vec3 tint_degrees(vec3 param_0) {
|
||||
return param_0 * 57.295779513082322865;
|
||||
}
|
||||
|
||||
|
||||
layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in;
|
||||
void test_function() {
|
||||
vec3 val = vec3(0.0f, 0.0f, 0.0f);
|
||||
vec3 tint_symbol = tint_degrees(val);
|
||||
return;
|
||||
}
|
||||
void main() {
|
||||
test_function();
|
||||
}
|
||||
|
||||
|
||||
)");
|
||||
}
|
||||
|
||||
TEST_F(GlslGeneratorImplTest_Intrinsic, Radians_Scalar) {
|
||||
auto* val = Var("val", ty.f32());
|
||||
auto* call = Call("radians", val);
|
||||
WrapInFunction(val, call);
|
||||
|
||||
GeneratorImpl& gen = SanitizeAndBuild();
|
||||
|
||||
ASSERT_TRUE(gen.Generate()) << gen.error();
|
||||
EXPECT_EQ(gen.result(), R"(#version 310 es
|
||||
precision mediump float;
|
||||
|
||||
float tint_radians(float param_0) {
|
||||
return param_0 * 0.017453292519943295474;
|
||||
}
|
||||
|
||||
|
||||
layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in;
|
||||
void test_function() {
|
||||
float val = 0.0f;
|
||||
float tint_symbol = tint_radians(val);
|
||||
return;
|
||||
}
|
||||
void main() {
|
||||
test_function();
|
||||
}
|
||||
|
||||
|
||||
)");
|
||||
}
|
||||
|
||||
TEST_F(GlslGeneratorImplTest_Intrinsic, Radians_Vector) {
|
||||
auto* val = Var("val", ty.vec3<f32>());
|
||||
auto* call = Call("radians", val);
|
||||
WrapInFunction(val, call);
|
||||
|
||||
GeneratorImpl& gen = SanitizeAndBuild();
|
||||
|
||||
ASSERT_TRUE(gen.Generate()) << gen.error();
|
||||
EXPECT_EQ(gen.result(), R"(#version 310 es
|
||||
precision mediump float;
|
||||
|
||||
vec3 tint_radians(vec3 param_0) {
|
||||
return param_0 * 0.017453292519943295474;
|
||||
}
|
||||
|
||||
|
||||
layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in;
|
||||
void test_function() {
|
||||
vec3 val = vec3(0.0f, 0.0f, 0.0f);
|
||||
vec3 tint_symbol = tint_radians(val);
|
||||
return;
|
||||
}
|
||||
void main() {
|
||||
test_function();
|
||||
}
|
||||
|
||||
|
||||
)");
|
||||
}
|
||||
|
||||
#if 0
|
||||
TEST_F(GlslGeneratorImplTest_Intrinsic, Pack4x8Snorm) {
|
||||
auto* call = Call("pack4x8snorm", "p1");
|
||||
Global("p1", ty.vec4<f32>(), ast::StorageClass::kPrivate);
|
||||
|
||||
@@ -1016,6 +1016,12 @@ bool GeneratorImpl::EmitIntrinsicCall(std::ostream& out,
|
||||
if (intrinsic->Type() == sem::IntrinsicType::kIsNormal) {
|
||||
return EmitIsNormalCall(out, expr, intrinsic);
|
||||
}
|
||||
if (intrinsic->Type() == sem::IntrinsicType::kDegrees) {
|
||||
return EmitDegreesCall(out, expr, intrinsic);
|
||||
}
|
||||
if (intrinsic->Type() == sem::IntrinsicType::kRadians) {
|
||||
return EmitRadiansCall(out, expr, intrinsic);
|
||||
}
|
||||
if (intrinsic->Type() == sem::IntrinsicType::kIgnore) {
|
||||
return EmitExpression(out, expr->args[0]); // [DEPRECATED]
|
||||
}
|
||||
@@ -1031,7 +1037,6 @@ bool GeneratorImpl::EmitIntrinsicCall(std::ostream& out,
|
||||
if (intrinsic->IsAtomic()) {
|
||||
return EmitWorkgroupAtomicCall(out, expr, intrinsic);
|
||||
}
|
||||
|
||||
auto name = generate_builtin_name(intrinsic);
|
||||
if (name.empty()) {
|
||||
return false;
|
||||
@@ -1927,6 +1932,30 @@ bool GeneratorImpl::EmitIsNormalCall(std::ostream& out,
|
||||
});
|
||||
}
|
||||
|
||||
bool GeneratorImpl::EmitDegreesCall(std::ostream& out,
|
||||
const ast::CallExpression* expr,
|
||||
const sem::Intrinsic* intrinsic) {
|
||||
return CallIntrinsicHelper(
|
||||
out, expr, intrinsic,
|
||||
[&](TextBuffer* b, const std::vector<std::string>& params) {
|
||||
line(b) << "return " << params[0] << " * " << std::setprecision(20)
|
||||
<< sem::kRadToDeg << ";";
|
||||
return true;
|
||||
});
|
||||
}
|
||||
|
||||
bool GeneratorImpl::EmitRadiansCall(std::ostream& out,
|
||||
const ast::CallExpression* expr,
|
||||
const sem::Intrinsic* intrinsic) {
|
||||
return CallIntrinsicHelper(
|
||||
out, expr, intrinsic,
|
||||
[&](TextBuffer* b, const std::vector<std::string>& params) {
|
||||
line(b) << "return " << params[0] << " * " << std::setprecision(20)
|
||||
<< sem::kDegToRad << ";";
|
||||
return true;
|
||||
});
|
||||
}
|
||||
|
||||
bool GeneratorImpl::EmitDataPackingCall(std::ostream& out,
|
||||
const ast::CallExpression* expr,
|
||||
const sem::Intrinsic* intrinsic) {
|
||||
|
||||
@@ -249,6 +249,22 @@ class GeneratorImpl : public TextGenerator {
|
||||
bool EmitIsNormalCall(std::ostream& out,
|
||||
const ast::CallExpression* expr,
|
||||
const sem::Intrinsic* intrinsic);
|
||||
/// Handles generating a call to the `degrees()` intrinsic
|
||||
/// @param out the output of the expression stream
|
||||
/// @param expr the call expression
|
||||
/// @param intrinsic the semantic information for the intrinsic
|
||||
/// @returns true if the call expression is emitted
|
||||
bool EmitDegreesCall(std::ostream& out,
|
||||
const ast::CallExpression* expr,
|
||||
const sem::Intrinsic* intrinsic);
|
||||
/// Handles generating a call to the `radians()` intrinsic
|
||||
/// @param out the output of the expression stream
|
||||
/// @param expr the call expression
|
||||
/// @param intrinsic the semantic information for the intrinsic
|
||||
/// @returns true if the call expression is emitted
|
||||
bool EmitRadiansCall(std::ostream& out,
|
||||
const ast::CallExpression* expr,
|
||||
const sem::Intrinsic* intrinsic);
|
||||
/// Handles generating a call to data packing intrinsic
|
||||
/// @param out the output of the expression stream
|
||||
/// @param expr the call expression
|
||||
|
||||
@@ -447,6 +447,90 @@ void test_function() {
|
||||
)");
|
||||
}
|
||||
|
||||
TEST_F(HlslGeneratorImplTest_Intrinsic, Degrees_Scalar) {
|
||||
auto* val = Var("val", ty.f32());
|
||||
auto* call = Call("degrees", val);
|
||||
WrapInFunction(val, call);
|
||||
|
||||
GeneratorImpl& gen = SanitizeAndBuild();
|
||||
|
||||
ASSERT_TRUE(gen.Generate()) << gen.error();
|
||||
EXPECT_EQ(gen.result(), R"(float tint_degrees(float param_0) {
|
||||
return param_0 * 57.295779513082322865;
|
||||
}
|
||||
|
||||
[numthreads(1, 1, 1)]
|
||||
void test_function() {
|
||||
float val = 0.0f;
|
||||
const float tint_symbol = tint_degrees(val);
|
||||
return;
|
||||
}
|
||||
)");
|
||||
}
|
||||
|
||||
TEST_F(HlslGeneratorImplTest_Intrinsic, Degrees_Vector) {
|
||||
auto* val = Var("val", ty.vec3<f32>());
|
||||
auto* call = Call("degrees", val);
|
||||
WrapInFunction(val, call);
|
||||
|
||||
GeneratorImpl& gen = SanitizeAndBuild();
|
||||
|
||||
ASSERT_TRUE(gen.Generate()) << gen.error();
|
||||
EXPECT_EQ(gen.result(), R"(float3 tint_degrees(float3 param_0) {
|
||||
return param_0 * 57.295779513082322865;
|
||||
}
|
||||
|
||||
[numthreads(1, 1, 1)]
|
||||
void test_function() {
|
||||
float3 val = float3(0.0f, 0.0f, 0.0f);
|
||||
const float3 tint_symbol = tint_degrees(val);
|
||||
return;
|
||||
}
|
||||
)");
|
||||
}
|
||||
|
||||
TEST_F(HlslGeneratorImplTest_Intrinsic, Radians_Scalar) {
|
||||
auto* val = Var("val", ty.f32());
|
||||
auto* call = Call("radians", val);
|
||||
WrapInFunction(val, call);
|
||||
|
||||
GeneratorImpl& gen = SanitizeAndBuild();
|
||||
|
||||
ASSERT_TRUE(gen.Generate()) << gen.error();
|
||||
EXPECT_EQ(gen.result(), R"(float tint_radians(float param_0) {
|
||||
return param_0 * 0.017453292519943295474;
|
||||
}
|
||||
|
||||
[numthreads(1, 1, 1)]
|
||||
void test_function() {
|
||||
float val = 0.0f;
|
||||
const float tint_symbol = tint_radians(val);
|
||||
return;
|
||||
}
|
||||
)");
|
||||
}
|
||||
|
||||
TEST_F(HlslGeneratorImplTest_Intrinsic, Radians_Vector) {
|
||||
auto* val = Var("val", ty.vec3<f32>());
|
||||
auto* call = Call("radians", val);
|
||||
WrapInFunction(val, call);
|
||||
|
||||
GeneratorImpl& gen = SanitizeAndBuild();
|
||||
|
||||
ASSERT_TRUE(gen.Generate()) << gen.error();
|
||||
EXPECT_EQ(gen.result(), R"(float3 tint_radians(float3 param_0) {
|
||||
return param_0 * 0.017453292519943295474;
|
||||
}
|
||||
|
||||
[numthreads(1, 1, 1)]
|
||||
void test_function() {
|
||||
float3 val = float3(0.0f, 0.0f, 0.0f);
|
||||
const float3 tint_symbol = tint_radians(val);
|
||||
return;
|
||||
}
|
||||
)");
|
||||
}
|
||||
|
||||
TEST_F(HlslGeneratorImplTest_Intrinsic, Pack4x8Snorm) {
|
||||
auto* call = Call("pack4x8snorm", "p1");
|
||||
Global("p1", ty.vec4<f32>(), ast::StorageClass::kPrivate);
|
||||
|
||||
@@ -594,6 +594,10 @@ bool GeneratorImpl::EmitIntrinsicCall(std::ostream& out,
|
||||
return EmitModfCall(out, expr, intrinsic);
|
||||
case sem::IntrinsicType::kFrexp:
|
||||
return EmitFrexpCall(out, expr, intrinsic);
|
||||
case sem::IntrinsicType::kDegrees:
|
||||
return EmitDegreesCall(out, expr, intrinsic);
|
||||
case sem::IntrinsicType::kRadians:
|
||||
return EmitRadiansCall(out, expr, intrinsic);
|
||||
|
||||
case sem::IntrinsicType::kPack2x16float:
|
||||
case sem::IntrinsicType::kUnpack2x16float: {
|
||||
@@ -1253,6 +1257,30 @@ bool GeneratorImpl::EmitFrexpCall(std::ostream& out,
|
||||
});
|
||||
}
|
||||
|
||||
bool GeneratorImpl::EmitDegreesCall(std::ostream& out,
|
||||
const ast::CallExpression* expr,
|
||||
const sem::Intrinsic* intrinsic) {
|
||||
return CallIntrinsicHelper(
|
||||
out, expr, intrinsic,
|
||||
[&](TextBuffer* b, const std::vector<std::string>& params) {
|
||||
line(b) << "return " << params[0] << " * " << std::setprecision(20)
|
||||
<< sem::kRadToDeg << ";";
|
||||
return true;
|
||||
});
|
||||
}
|
||||
|
||||
bool GeneratorImpl::EmitRadiansCall(std::ostream& out,
|
||||
const ast::CallExpression* expr,
|
||||
const sem::Intrinsic* intrinsic) {
|
||||
return CallIntrinsicHelper(
|
||||
out, expr, intrinsic,
|
||||
[&](TextBuffer* b, const std::vector<std::string>& params) {
|
||||
line(b) << "return " << params[0] << " * " << std::setprecision(20)
|
||||
<< sem::kDegToRad << ";";
|
||||
return true;
|
||||
});
|
||||
}
|
||||
|
||||
std::string GeneratorImpl::generate_builtin_name(
|
||||
const sem::Intrinsic* intrinsic) {
|
||||
std::string out = "";
|
||||
|
||||
@@ -217,6 +217,22 @@ class GeneratorImpl : public TextGenerator {
|
||||
bool EmitFrexpCall(std::ostream& out,
|
||||
const ast::CallExpression* expr,
|
||||
const sem::Intrinsic* intrinsic);
|
||||
/// Handles generating a call to the `degrees()` intrinsic
|
||||
/// @param out the output of the expression stream
|
||||
/// @param expr the call expression
|
||||
/// @param intrinsic the semantic information for the intrinsic
|
||||
/// @returns true if the call expression is emitted
|
||||
bool EmitDegreesCall(std::ostream& out,
|
||||
const ast::CallExpression* expr,
|
||||
const sem::Intrinsic* intrinsic);
|
||||
/// Handles generating a call to the `radians()` intrinsic
|
||||
/// @param out the output of the expression stream
|
||||
/// @param expr the call expression
|
||||
/// @param intrinsic the semantic information for the intrinsic
|
||||
/// @returns true if the call expression is emitted
|
||||
bool EmitRadiansCall(std::ostream& out,
|
||||
const ast::CallExpression* expr,
|
||||
const sem::Intrinsic* intrinsic);
|
||||
/// Handles a case statement
|
||||
/// @param stmt the statement
|
||||
/// @returns true if the statement was emitted successfully
|
||||
|
||||
@@ -319,6 +319,106 @@ TEST_F(MslGeneratorImplTest, WorkgroupBarrier) {
|
||||
EXPECT_EQ(out.str(), "threadgroup_barrier(mem_flags::mem_threadgroup)");
|
||||
}
|
||||
|
||||
TEST_F(MslGeneratorImplTest, Degrees_Scalar) {
|
||||
auto* val = Var("val", ty.f32());
|
||||
auto* call = Call("degrees", val);
|
||||
WrapInFunction(val, call);
|
||||
|
||||
GeneratorImpl& gen = Build();
|
||||
|
||||
ASSERT_TRUE(gen.Generate()) << gen.error();
|
||||
EXPECT_EQ(gen.result(), R"(#include <metal_stdlib>
|
||||
|
||||
using namespace metal;
|
||||
|
||||
float tint_degrees(float param_0) {
|
||||
return param_0 * 57.295779513082322865;
|
||||
}
|
||||
|
||||
kernel void test_function() {
|
||||
float val = 0.0f;
|
||||
float const tint_symbol = tint_degrees(val);
|
||||
return;
|
||||
}
|
||||
|
||||
)");
|
||||
}
|
||||
|
||||
TEST_F(MslGeneratorImplTest, Degrees_Vector) {
|
||||
auto* val = Var("val", ty.vec3<f32>());
|
||||
auto* call = Call("degrees", val);
|
||||
WrapInFunction(val, call);
|
||||
|
||||
GeneratorImpl& gen = Build();
|
||||
|
||||
ASSERT_TRUE(gen.Generate()) << gen.error();
|
||||
EXPECT_EQ(gen.result(), R"(#include <metal_stdlib>
|
||||
|
||||
using namespace metal;
|
||||
|
||||
float3 tint_degrees(float3 param_0) {
|
||||
return param_0 * 57.295779513082322865;
|
||||
}
|
||||
|
||||
kernel void test_function() {
|
||||
float3 val = 0.0f;
|
||||
float3 const tint_symbol = tint_degrees(val);
|
||||
return;
|
||||
}
|
||||
|
||||
)");
|
||||
}
|
||||
|
||||
TEST_F(MslGeneratorImplTest, Radians_Scalar) {
|
||||
auto* val = Var("val", ty.f32());
|
||||
auto* call = Call("radians", val);
|
||||
WrapInFunction(val, call);
|
||||
|
||||
GeneratorImpl& gen = Build();
|
||||
|
||||
ASSERT_TRUE(gen.Generate()) << gen.error();
|
||||
EXPECT_EQ(gen.result(), R"(#include <metal_stdlib>
|
||||
|
||||
using namespace metal;
|
||||
|
||||
float tint_radians(float param_0) {
|
||||
return param_0 * 0.017453292519943295474;
|
||||
}
|
||||
|
||||
kernel void test_function() {
|
||||
float val = 0.0f;
|
||||
float const tint_symbol = tint_radians(val);
|
||||
return;
|
||||
}
|
||||
|
||||
)");
|
||||
}
|
||||
|
||||
TEST_F(MslGeneratorImplTest, Radians_Vector) {
|
||||
auto* val = Var("val", ty.vec3<f32>());
|
||||
auto* call = Call("radians", val);
|
||||
WrapInFunction(val, call);
|
||||
|
||||
GeneratorImpl& gen = Build();
|
||||
|
||||
ASSERT_TRUE(gen.Generate()) << gen.error();
|
||||
EXPECT_EQ(gen.result(), R"(#include <metal_stdlib>
|
||||
|
||||
using namespace metal;
|
||||
|
||||
float3 tint_radians(float3 param_0) {
|
||||
return param_0 * 0.017453292519943295474;
|
||||
}
|
||||
|
||||
kernel void test_function() {
|
||||
float3 val = 0.0f;
|
||||
float3 const tint_symbol = tint_radians(val);
|
||||
return;
|
||||
}
|
||||
|
||||
)");
|
||||
}
|
||||
|
||||
TEST_F(MslGeneratorImplTest, Pack2x16Float) {
|
||||
auto* call = Call("pack2x16float", "p1");
|
||||
Global("p1", ty.vec2<f32>(), ast::StorageClass::kPrivate);
|
||||
|
||||
@@ -133,6 +133,8 @@ uint32_t intrinsic_to_glsl_method(const sem::Intrinsic* intrinsic) {
|
||||
return GLSLstd450Cosh;
|
||||
case IntrinsicType::kCross:
|
||||
return GLSLstd450Cross;
|
||||
case IntrinsicType::kDegrees:
|
||||
return GLSLstd450Degrees;
|
||||
case IntrinsicType::kDeterminant:
|
||||
return GLSLstd450Determinant;
|
||||
case IntrinsicType::kDistance:
|
||||
@@ -195,6 +197,8 @@ uint32_t intrinsic_to_glsl_method(const sem::Intrinsic* intrinsic) {
|
||||
return GLSLstd450PackHalf2x16;
|
||||
case IntrinsicType::kPow:
|
||||
return GLSLstd450Pow;
|
||||
case IntrinsicType::kRadians:
|
||||
return GLSLstd450Radians;
|
||||
case IntrinsicType::kReflect:
|
||||
return GLSLstd450Reflect;
|
||||
case IntrinsicType::kRefract:
|
||||
|
||||
@@ -829,6 +829,7 @@ INSTANTIATE_TEST_SUITE_P(IntrinsicBuilderTest,
|
||||
IntrinsicData{"ceil", "Ceil"},
|
||||
IntrinsicData{"cos", "Cos"},
|
||||
IntrinsicData{"cosh", "Cosh"},
|
||||
IntrinsicData{"degrees", "Degrees"},
|
||||
IntrinsicData{"exp", "Exp"},
|
||||
IntrinsicData{"exp2", "Exp2"},
|
||||
IntrinsicData{"floor", "Floor"},
|
||||
@@ -837,6 +838,7 @@ INSTANTIATE_TEST_SUITE_P(IntrinsicBuilderTest,
|
||||
"InverseSqrt"},
|
||||
IntrinsicData{"log", "Log"},
|
||||
IntrinsicData{"log2", "Log2"},
|
||||
IntrinsicData{"radians", "Radians"},
|
||||
IntrinsicData{"round", "RoundEven"},
|
||||
IntrinsicData{"sign", "FSign"},
|
||||
IntrinsicData{"sin", "Sin"},
|
||||
|
||||
Reference in New Issue
Block a user