blob: 7008ac7f55d18c2ad3e49df73e51297a6ed25f65 [file] [log] [blame]
// Copyright 2024 The Dawn & Tint Authors
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this
// list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// 3. Neither the name of the copyright holder nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "src/tint/lang/hlsl/writer/helper_test.h"
using namespace tint::core::fluent_types; // NOLINT
using namespace tint::core::number_suffixes; // NOLINT
namespace tint::hlsl::writer {
namespace {
TEST_F(HlslWriterTest, ConstructF32Var) {
auto* f = b.Function("a", ty.f32());
b.Append(f->Block(), [&] {
auto* v = b.Var("v", 2_f);
b.Return(f, b.Construct(ty.f32(), v));
});
ASSERT_TRUE(Generate()) << err_ << output_.hlsl;
EXPECT_EQ(output_.hlsl, R"(
float a() {
float v = 2.0f;
return float(v);
}
[numthreads(1, 1, 1)]
void unused_entry_point() {
}
)");
}
TEST_F(HlslWriterTest, ConstructF16Var) {
auto* f = b.Function("a", ty.f16());
b.Append(f->Block(), [&] {
auto* v = b.Var("v", 2_h);
b.Return(f, b.Construct(ty.f16(), v));
});
ASSERT_TRUE(Generate()) << err_ << output_.hlsl;
EXPECT_EQ(output_.hlsl, R"(
float16_t a() {
float16_t v = float16_t(2.0h);
return float16_t(v);
}
[numthreads(1, 1, 1)]
void unused_entry_point() {
}
)");
}
TEST_F(HlslWriterTest, ConstructBoolVar) {
auto* f = b.Function("a", ty.bool_());
b.Append(f->Block(), [&] {
auto* v = b.Var("v", false);
b.Return(f, b.Construct(ty.bool_(), v));
});
ASSERT_TRUE(Generate()) << err_ << output_.hlsl;
EXPECT_EQ(output_.hlsl, R"(
bool a() {
bool v = false;
return bool(v);
}
[numthreads(1, 1, 1)]
void unused_entry_point() {
}
)");
}
TEST_F(HlslWriterTest, ConstructI32Var) {
auto* f = b.Function("a", ty.i32());
b.Append(f->Block(), [&] {
auto* v = b.Var("v", 2_i);
b.Return(f, b.Construct(ty.i32(), v));
});
ASSERT_TRUE(Generate()) << err_ << output_.hlsl;
EXPECT_EQ(output_.hlsl, R"(
int a() {
int v = 2;
return int(v);
}
[numthreads(1, 1, 1)]
void unused_entry_point() {
}
)");
}
TEST_F(HlslWriterTest, ConstructU32) {
auto* f = b.Function("a", ty.u32());
b.Append(f->Block(), [&] {
auto* v = b.Var("v", 2_u);
b.Return(f, b.Construct(ty.u32(), v));
});
ASSERT_TRUE(Generate()) << err_ << output_.hlsl;
EXPECT_EQ(output_.hlsl, R"(
uint a() {
uint v = 2u;
return uint(v);
}
[numthreads(1, 1, 1)]
void unused_entry_point() {
}
)");
}
TEST_F(HlslWriterTest, ConstructMatrix) {
auto* f = b.Function("a", ty.mat2x2<f32>());
b.Append(f->Block(), [&] {
auto* v = b.Var("v", 2_f);
b.Return(f, b.Construct(ty.mat2x2<f32>(), v, v, v, v));
});
ASSERT_TRUE(Generate()) << err_ << output_.hlsl;
EXPECT_EQ(output_.hlsl, R"(
float2x2 a() {
float v = 2.0f;
return float2x2(v, v, v, v);
}
[numthreads(1, 1, 1)]
void unused_entry_point() {
}
)");
}
TEST_F(HlslWriterTest, ConstructVecSingleScalarF32Var) {
auto* f = b.Function("a", ty.vec3<f32>());
b.Append(f->Block(), [&] {
auto* v = b.Var("v", 2_f);
b.Return(f, b.Construct(ty.vec3<f32>(), v));
});
ASSERT_TRUE(Generate()) << err_ << output_.hlsl;
EXPECT_EQ(output_.hlsl, R"(
float3 a() {
float v = 2.0f;
return float3((v).xxx);
}
[numthreads(1, 1, 1)]
void unused_entry_point() {
}
)");
}
TEST_F(HlslWriterTest, ConstructVecSingleScalarF16Var) {
auto* f = b.Function("a", ty.vec3<f16>());
b.Append(f->Block(), [&] {
auto* v = b.Var("v", 2_h);
b.Return(f, b.Construct(ty.vec3<f16>(), v));
});
ASSERT_TRUE(Generate()) << err_ << output_.hlsl;
EXPECT_EQ(output_.hlsl, R"(
vector<float16_t, 3> a() {
float16_t v = float16_t(2.0h);
return vector<float16_t, 3>((v).xxx);
}
[numthreads(1, 1, 1)]
void unused_entry_point() {
}
)");
}
TEST_F(HlslWriterTest, ConstructVecSingleScalarBoolVar) {
auto* f = b.Function("a", ty.vec3<bool>());
b.Append(f->Block(), [&] {
auto* v = b.Var("v", true);
b.Return(f, b.Construct(ty.vec3<bool>(), v));
});
ASSERT_TRUE(Generate()) << err_ << output_.hlsl;
EXPECT_EQ(output_.hlsl, R"(
bool3 a() {
bool v = true;
return bool3((v).xxx);
}
[numthreads(1, 1, 1)]
void unused_entry_point() {
}
)");
}
TEST_F(HlslWriterTest, ConstructArray) {
auto* f = b.Function("a", ty.void_(), core::ir::Function::PipelineStage::kCompute);
f->SetWorkgroupSize(1, 1, 1);
b.Append(f->Block(), [&] {
b.Var("v", b.Construct(ty.array<vec3<f32>, 3>(), b.Composite(ty.vec3<f32>(), 1_f, 2_f, 3_f),
b.Composite(ty.vec3<f32>(), 4_f, 5_f, 6_f),
b.Composite(ty.vec3<f32>(), 7_f, 8_f, 9_f)));
b.Return(f);
});
ASSERT_TRUE(Generate()) << err_ << output_.hlsl;
EXPECT_EQ(output_.hlsl, R"(
[numthreads(1, 1, 1)]
void a() {
float3 v[3] = {float3(1.0f, 2.0f, 3.0f), float3(4.0f, 5.0f, 6.0f), float3(7.0f, 8.0f, 9.0f)};
}
)");
}
TEST_F(HlslWriterTest, ConstructStruct) {
Vector members{
ty.Get<core::type::StructMember>(b.ir.symbols.New("a"), ty.i32(), 0u, 0u, 4u, 4u,
core::type::StructMemberAttributes{}),
ty.Get<core::type::StructMember>(b.ir.symbols.New("b"), ty.f32(), 1u, 4u, 4u, 4u,
core::type::StructMemberAttributes{}),
ty.Get<core::type::StructMember>(b.ir.symbols.New("c"), ty.vec3<i32>(), 2u, 8u, 16u, 16u,
core::type::StructMemberAttributes{}),
};
auto* strct = ty.Struct(b.ir.symbols.New("S"), std::move(members));
auto* f = b.Function("a", strct);
b.Append(f->Block(), [&] {
b.Return(f, b.Construct(strct, 1_i, 2_f, b.Composite(ty.vec3<i32>(), 3_i, 4_i, 5_i)));
});
ASSERT_TRUE(Generate()) << err_ << output_.hlsl;
EXPECT_EQ(output_.hlsl, R"(struct S {
int a;
float b;
int3 c;
};
S a() {
S v = {1, 2.0f, int3(3, 4, 5)};
return v;
}
[numthreads(1, 1, 1)]
void unused_entry_point() {
}
)");
}
} // namespace
} // namespace tint::hlsl::writer