2020-12-08 21:07:24 +00:00
|
|
|
// Copyright 2020 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/transform/first_index_offset.h"
|
|
|
|
|
|
|
|
#include <memory>
|
|
|
|
#include <utility>
|
2021-01-06 12:57:41 +00:00
|
|
|
#include <vector>
|
2020-12-08 21:07:24 +00:00
|
|
|
|
2021-01-06 12:57:41 +00:00
|
|
|
#include "src/transform/test_helper.h"
|
2020-12-08 21:07:24 +00:00
|
|
|
|
|
|
|
namespace tint {
|
|
|
|
namespace transform {
|
|
|
|
namespace {
|
|
|
|
|
2021-01-06 12:57:41 +00:00
|
|
|
using FirstIndexOffsetTest = TransformTest;
|
2020-12-08 21:07:24 +00:00
|
|
|
|
|
|
|
TEST_F(FirstIndexOffsetTest, Error_AlreadyTransformed) {
|
2021-01-06 12:57:41 +00:00
|
|
|
auto* src = R"(
|
2021-01-18 15:51:13 +00:00
|
|
|
[[builtin(vertex_index)]] var<in> vert_idx : u32;
|
2021-01-06 12:57:41 +00:00
|
|
|
|
|
|
|
fn test() -> u32 {
|
|
|
|
return vert_idx;
|
|
|
|
}
|
|
|
|
|
|
|
|
[[stage(vertex)]]
|
|
|
|
fn entry() -> void {
|
|
|
|
test();
|
|
|
|
}
|
|
|
|
)";
|
2020-12-08 21:07:24 +00:00
|
|
|
|
2021-02-24 15:55:24 +00:00
|
|
|
auto* expect =
|
|
|
|
"error: First index offset transform has already been applied.";
|
2020-12-08 21:07:24 +00:00
|
|
|
|
2021-01-06 12:57:41 +00:00
|
|
|
std::vector<std::unique_ptr<transform::Transform>> transforms;
|
|
|
|
transforms.emplace_back(std::make_unique<FirstIndexOffset>(0, 0));
|
|
|
|
transforms.emplace_back(std::make_unique<FirstIndexOffset>(1, 1));
|
2020-12-08 21:07:24 +00:00
|
|
|
|
2021-01-06 12:57:41 +00:00
|
|
|
auto got = Transform(src, std::move(transforms));
|
2020-12-08 21:07:24 +00:00
|
|
|
|
2021-02-24 15:55:24 +00:00
|
|
|
EXPECT_EQ(expect, str(got));
|
|
|
|
|
|
|
|
auto* data = got.data.Get<FirstIndexOffset::Data>();
|
|
|
|
|
|
|
|
ASSERT_NE(data, nullptr);
|
|
|
|
EXPECT_EQ(data->has_vertex_index, true);
|
|
|
|
EXPECT_EQ(data->has_instance_index, false);
|
|
|
|
EXPECT_EQ(data->first_vertex_offset, 0u);
|
2021-02-25 14:11:50 +00:00
|
|
|
EXPECT_EQ(data->first_instance_offset, 0u);
|
2020-12-08 21:07:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(FirstIndexOffsetTest, EmptyModule) {
|
2021-01-06 12:57:41 +00:00
|
|
|
auto* src = "";
|
|
|
|
auto* expect = "";
|
2020-12-08 21:07:24 +00:00
|
|
|
|
2021-01-06 12:57:41 +00:00
|
|
|
auto got = Transform<FirstIndexOffset>(src, 0, 0);
|
2020-12-08 21:07:24 +00:00
|
|
|
|
2021-02-24 15:55:24 +00:00
|
|
|
EXPECT_EQ(expect, str(got));
|
|
|
|
|
|
|
|
auto* data = got.data.Get<FirstIndexOffset::Data>();
|
|
|
|
|
|
|
|
ASSERT_NE(data, nullptr);
|
|
|
|
EXPECT_EQ(data->has_vertex_index, false);
|
|
|
|
EXPECT_EQ(data->has_instance_index, false);
|
|
|
|
EXPECT_EQ(data->first_vertex_offset, 0u);
|
2021-02-25 14:11:50 +00:00
|
|
|
EXPECT_EQ(data->first_instance_offset, 0u);
|
2021-01-06 12:57:41 +00:00
|
|
|
}
|
2020-12-08 21:07:24 +00:00
|
|
|
|
2021-01-06 12:57:41 +00:00
|
|
|
TEST_F(FirstIndexOffsetTest, BasicModuleVertexIndex) {
|
|
|
|
auto* src = R"(
|
2021-01-18 15:51:13 +00:00
|
|
|
[[builtin(vertex_index)]] var<in> vert_idx : u32;
|
2020-12-08 21:07:24 +00:00
|
|
|
|
2021-01-06 12:57:41 +00:00
|
|
|
fn test() -> u32 {
|
|
|
|
return vert_idx;
|
2020-12-08 21:07:24 +00:00
|
|
|
}
|
|
|
|
|
2021-01-06 12:57:41 +00:00
|
|
|
[[stage(vertex)]]
|
|
|
|
fn entry() -> void {
|
|
|
|
test();
|
|
|
|
}
|
|
|
|
)";
|
|
|
|
|
|
|
|
auto* expect = R"(
|
2021-02-10 15:34:37 +00:00
|
|
|
[[builtin(vertex_index)]] var<in> tint_first_index_offset_vert_idx : u32;
|
|
|
|
|
|
|
|
[[binding(1), group(2)]] var<uniform> tint_first_index_data : TintFirstIndexOffsetData;
|
|
|
|
|
2021-01-06 12:57:41 +00:00
|
|
|
[[block]]
|
|
|
|
struct TintFirstIndexOffsetData {
|
|
|
|
tint_first_vertex_index : u32;
|
|
|
|
};
|
|
|
|
|
|
|
|
fn test() -> u32 {
|
|
|
|
const vert_idx : u32 = (tint_first_index_offset_vert_idx + tint_first_index_data.tint_first_vertex_index);
|
|
|
|
return vert_idx;
|
|
|
|
}
|
|
|
|
|
|
|
|
[[stage(vertex)]]
|
|
|
|
fn entry() -> void {
|
|
|
|
test();
|
2020-12-08 21:07:24 +00:00
|
|
|
}
|
2020-12-10 17:47:41 +00:00
|
|
|
)";
|
2021-01-06 12:57:41 +00:00
|
|
|
|
|
|
|
auto got = Transform<FirstIndexOffset>(src, 1, 2);
|
|
|
|
|
2021-02-24 15:55:24 +00:00
|
|
|
EXPECT_EQ(expect, str(got));
|
|
|
|
|
|
|
|
auto* data = got.data.Get<FirstIndexOffset::Data>();
|
|
|
|
|
|
|
|
ASSERT_NE(data, nullptr);
|
|
|
|
EXPECT_EQ(data->has_vertex_index, true);
|
|
|
|
EXPECT_EQ(data->has_instance_index, false);
|
|
|
|
EXPECT_EQ(data->first_vertex_offset, 0u);
|
2021-02-25 14:11:50 +00:00
|
|
|
EXPECT_EQ(data->first_instance_offset, 0u);
|
2020-12-08 21:07:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(FirstIndexOffsetTest, BasicModuleInstanceIndex) {
|
2021-01-06 12:57:41 +00:00
|
|
|
auto* src = R"(
|
2021-01-18 15:51:13 +00:00
|
|
|
[[builtin(instance_index)]] var<in> inst_idx : u32;
|
2021-01-06 12:57:41 +00:00
|
|
|
|
|
|
|
fn test() -> u32 {
|
|
|
|
return inst_idx;
|
|
|
|
}
|
|
|
|
|
|
|
|
[[stage(vertex)]]
|
|
|
|
fn entry() -> void {
|
|
|
|
test();
|
|
|
|
}
|
|
|
|
)";
|
|
|
|
|
|
|
|
auto* expect = R"(
|
2021-02-10 15:34:37 +00:00
|
|
|
[[builtin(instance_index)]] var<in> tint_first_index_offset_inst_idx : u32;
|
|
|
|
|
|
|
|
[[binding(1), group(7)]] var<uniform> tint_first_index_data : TintFirstIndexOffsetData;
|
|
|
|
|
2021-01-06 12:57:41 +00:00
|
|
|
[[block]]
|
|
|
|
struct TintFirstIndexOffsetData {
|
|
|
|
tint_first_instance_index : u32;
|
|
|
|
};
|
|
|
|
|
|
|
|
fn test() -> u32 {
|
|
|
|
const inst_idx : u32 = (tint_first_index_offset_inst_idx + tint_first_index_data.tint_first_instance_index);
|
|
|
|
return inst_idx;
|
|
|
|
}
|
|
|
|
|
|
|
|
[[stage(vertex)]]
|
|
|
|
fn entry() -> void {
|
|
|
|
test();
|
2020-12-08 21:07:24 +00:00
|
|
|
}
|
2020-12-10 17:47:41 +00:00
|
|
|
)";
|
2021-01-06 12:57:41 +00:00
|
|
|
|
|
|
|
auto got = Transform<FirstIndexOffset>(src, 1, 7);
|
|
|
|
|
2021-02-24 15:55:24 +00:00
|
|
|
EXPECT_EQ(expect, str(got));
|
|
|
|
|
|
|
|
auto* data = got.data.Get<FirstIndexOffset::Data>();
|
|
|
|
|
|
|
|
ASSERT_NE(data, nullptr);
|
|
|
|
EXPECT_EQ(data->has_vertex_index, false);
|
|
|
|
EXPECT_EQ(data->has_instance_index, true);
|
|
|
|
EXPECT_EQ(data->first_vertex_offset, 0u);
|
2021-02-25 14:11:50 +00:00
|
|
|
EXPECT_EQ(data->first_instance_offset, 0u);
|
2020-12-08 21:07:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(FirstIndexOffsetTest, BasicModuleBothIndex) {
|
2021-01-06 12:57:41 +00:00
|
|
|
auto* src = R"(
|
2021-01-18 15:51:13 +00:00
|
|
|
[[builtin(instance_index)]] var<in> instance_idx : u32;
|
|
|
|
[[builtin(vertex_index)]] var<in> vert_idx : u32;
|
2021-01-06 12:57:41 +00:00
|
|
|
|
|
|
|
fn test() -> u32 {
|
|
|
|
return instance_idx + vert_idx;
|
|
|
|
}
|
|
|
|
|
|
|
|
[[stage(vertex)]]
|
|
|
|
fn entry() -> void {
|
|
|
|
test();
|
2020-12-08 21:07:24 +00:00
|
|
|
}
|
2020-12-10 17:47:41 +00:00
|
|
|
)";
|
2020-12-08 21:07:24 +00:00
|
|
|
|
2021-01-06 12:57:41 +00:00
|
|
|
auto* expect = R"(
|
2021-02-10 15:34:37 +00:00
|
|
|
[[builtin(instance_index)]] var<in> tint_first_index_offset_instance_idx : u32;
|
|
|
|
|
|
|
|
[[builtin(vertex_index)]] var<in> tint_first_index_offset_vert_idx : u32;
|
|
|
|
|
|
|
|
[[binding(1), group(2)]] var<uniform> tint_first_index_data : TintFirstIndexOffsetData;
|
|
|
|
|
2021-01-06 12:57:41 +00:00
|
|
|
[[block]]
|
|
|
|
struct TintFirstIndexOffsetData {
|
|
|
|
tint_first_vertex_index : u32;
|
|
|
|
tint_first_instance_index : u32;
|
|
|
|
};
|
|
|
|
|
|
|
|
fn test() -> u32 {
|
|
|
|
const instance_idx : u32 = (tint_first_index_offset_instance_idx + tint_first_index_data.tint_first_instance_index);
|
|
|
|
const vert_idx : u32 = (tint_first_index_offset_vert_idx + tint_first_index_data.tint_first_vertex_index);
|
|
|
|
return (instance_idx + vert_idx);
|
|
|
|
}
|
|
|
|
|
|
|
|
[[stage(vertex)]]
|
|
|
|
fn entry() -> void {
|
|
|
|
test();
|
|
|
|
}
|
|
|
|
)";
|
|
|
|
|
|
|
|
auto got = Transform<FirstIndexOffset>(src, 1, 2);
|
|
|
|
|
2021-02-24 15:55:24 +00:00
|
|
|
EXPECT_EQ(expect, str(got));
|
|
|
|
|
|
|
|
auto* data = got.data.Get<FirstIndexOffset::Data>();
|
|
|
|
|
|
|
|
ASSERT_NE(data, nullptr);
|
|
|
|
EXPECT_EQ(data->has_vertex_index, true);
|
|
|
|
EXPECT_EQ(data->has_instance_index, true);
|
|
|
|
EXPECT_EQ(data->first_vertex_offset, 0u);
|
2021-02-25 14:11:50 +00:00
|
|
|
EXPECT_EQ(data->first_instance_offset, 4u);
|
2020-12-08 21:07:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(FirstIndexOffsetTest, NestedCalls) {
|
2021-01-06 12:57:41 +00:00
|
|
|
auto* src = R"(
|
2021-01-18 15:51:13 +00:00
|
|
|
[[builtin(vertex_index)]] var<in> vert_idx : u32;
|
2021-01-06 12:57:41 +00:00
|
|
|
|
|
|
|
fn func1() -> u32 {
|
|
|
|
return vert_idx;
|
|
|
|
}
|
|
|
|
|
|
|
|
fn func2() -> u32 {
|
|
|
|
return func1();
|
|
|
|
}
|
|
|
|
|
|
|
|
[[stage(vertex)]]
|
|
|
|
fn entry() -> void {
|
|
|
|
func2();
|
2020-12-08 21:07:24 +00:00
|
|
|
}
|
2020-12-10 17:47:41 +00:00
|
|
|
)";
|
2021-01-06 12:57:41 +00:00
|
|
|
|
|
|
|
auto* expect = R"(
|
2021-02-10 15:34:37 +00:00
|
|
|
[[builtin(vertex_index)]] var<in> tint_first_index_offset_vert_idx : u32;
|
|
|
|
|
|
|
|
[[binding(1), group(2)]] var<uniform> tint_first_index_data : TintFirstIndexOffsetData;
|
|
|
|
|
2021-01-06 12:57:41 +00:00
|
|
|
[[block]]
|
|
|
|
struct TintFirstIndexOffsetData {
|
|
|
|
tint_first_vertex_index : u32;
|
|
|
|
};
|
|
|
|
|
|
|
|
fn func1() -> u32 {
|
|
|
|
const vert_idx : u32 = (tint_first_index_offset_vert_idx + tint_first_index_data.tint_first_vertex_index);
|
|
|
|
return vert_idx;
|
|
|
|
}
|
|
|
|
|
|
|
|
fn func2() -> u32 {
|
|
|
|
return func1();
|
|
|
|
}
|
|
|
|
|
|
|
|
[[stage(vertex)]]
|
|
|
|
fn entry() -> void {
|
|
|
|
func2();
|
|
|
|
}
|
|
|
|
)";
|
|
|
|
|
|
|
|
auto got = Transform<FirstIndexOffset>(src, 1, 2);
|
|
|
|
|
2021-02-24 15:55:24 +00:00
|
|
|
EXPECT_EQ(expect, str(got));
|
|
|
|
|
|
|
|
auto* data = got.data.Get<FirstIndexOffset::Data>();
|
|
|
|
|
|
|
|
ASSERT_NE(data, nullptr);
|
|
|
|
EXPECT_EQ(data->has_vertex_index, true);
|
|
|
|
EXPECT_EQ(data->has_instance_index, false);
|
|
|
|
EXPECT_EQ(data->first_vertex_offset, 0u);
|
2021-02-25 14:11:50 +00:00
|
|
|
EXPECT_EQ(data->first_instance_offset, 0u);
|
2020-12-08 21:07:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
} // namespace transform
|
|
|
|
} // namespace tint
|