mirror of
https://github.com/encounter/dawn-cmake.git
synced 2025-06-12 17:43:45 +00:00
SymbolTable::New() used to build and return a symbol without a registered name. When you asked for the name of the symbol it would return tint_symbol_N, where N is the numerical identifier for the symbol. This approach was a major tripping hazzard for transforms that liked to fetch the source program name, and register it in the new program (in this situation, you should always use `CloneContext::Clone(Symbol)`). Without special casing for unnamed symbols, you could end up promoting the unnamed symbol to a named symbol, and then colliding against a new unnamed symbol. This is exactly what happened in tint:711. Instead, with this change: * The concept of unnamed symbols has been removed. All symbols now have a name. * The signature of `SymbolTable::New()` has been changed to take a name parameter (which defaults to 'tint_symbol'). This can be used to create a new, unique named symbol (possibly with a suffix), which will not collide with any existing symbols. Note these symbols may still collide if `SymbolTable::Register()` is called with the same name. All Transforms that currently use `SymbolTable::Register()` will be fixed in another change. * The CloneContext has been updated to use `SymbolTable::New()` instead of `Register()`. This means that any symbols defined before a clone will not collide. * `CloneContext::CloneSymbols()` has been added which allows a transform to pre-clone all the symbols from the source program. This can be used to avoid the authored identifiers being suffixed with a number, in the case a transform calls New() before the symbol is cloned. * `Symbol::to_str()` has been changed to return `$<id>` instead of `tint_symbol_N`. This is to avoid any confusion between the actual name and the symbol ID. Bug: tint:711 Bug: tint:712 Change-Id: I526e4b49b7027545613859de487e6a275686107a Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/47631 Commit-Queue: Ben Clayton <bclayton@google.com> Reviewed-by: Antonio Maiorano <amaiorano@google.com>
518 lines
11 KiB
C++
518 lines
11 KiB
C++
// 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>
|
|
#include <vector>
|
|
|
|
#include "src/transform/test_helper.h"
|
|
|
|
namespace tint {
|
|
namespace transform {
|
|
namespace {
|
|
|
|
using FirstIndexOffsetTest = TransformTest;
|
|
|
|
TEST_F(FirstIndexOffsetTest, EmptyModule) {
|
|
auto* src = "";
|
|
auto* expect = "";
|
|
|
|
DataMap config;
|
|
config.Add<FirstIndexOffset::BindingPoint>(0, 0);
|
|
auto got = Run<FirstIndexOffset>(src, std::move(config));
|
|
|
|
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);
|
|
EXPECT_EQ(data->first_instance_offset, 0u);
|
|
}
|
|
|
|
TEST_F(FirstIndexOffsetTest, BasicModuleVertexIndex) {
|
|
auto* src = R"(
|
|
fn test(vert_idx : u32) -> u32 {
|
|
return vert_idx;
|
|
}
|
|
|
|
[[stage(vertex)]]
|
|
fn entry([[builtin(vertex_index)]] vert_idx : u32) {
|
|
test(vert_idx);
|
|
}
|
|
)";
|
|
|
|
auto* expect = R"(
|
|
[[block]]
|
|
struct tint_symbol {
|
|
first_vertex_index : u32;
|
|
};
|
|
|
|
[[binding(1), group(2)]] var<uniform> tint_symbol_1 : tint_symbol;
|
|
|
|
fn test(vert_idx : u32) -> u32 {
|
|
return vert_idx;
|
|
}
|
|
|
|
[[stage(vertex)]]
|
|
fn entry([[builtin(vertex_index)]] vert_idx : u32) {
|
|
test((vert_idx + tint_symbol_1.first_vertex_index));
|
|
}
|
|
)";
|
|
|
|
DataMap config;
|
|
config.Add<FirstIndexOffset::BindingPoint>(1, 2);
|
|
auto got = Run<FirstIndexOffset>(src, std::move(config));
|
|
|
|
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);
|
|
EXPECT_EQ(data->first_instance_offset, 0u);
|
|
}
|
|
|
|
TEST_F(FirstIndexOffsetTest, BasicModuleInstanceIndex) {
|
|
auto* src = R"(
|
|
fn test(inst_idx : u32) -> u32 {
|
|
return inst_idx;
|
|
}
|
|
|
|
[[stage(vertex)]]
|
|
fn entry([[builtin(instance_index)]] inst_idx : u32) {
|
|
test(inst_idx);
|
|
}
|
|
)";
|
|
|
|
auto* expect = R"(
|
|
[[block]]
|
|
struct tint_symbol {
|
|
first_instance_index : u32;
|
|
};
|
|
|
|
[[binding(1), group(7)]] var<uniform> tint_symbol_1 : tint_symbol;
|
|
|
|
fn test(inst_idx : u32) -> u32 {
|
|
return inst_idx;
|
|
}
|
|
|
|
[[stage(vertex)]]
|
|
fn entry([[builtin(instance_index)]] inst_idx : u32) {
|
|
test((inst_idx + tint_symbol_1.first_instance_index));
|
|
}
|
|
)";
|
|
|
|
DataMap config;
|
|
config.Add<FirstIndexOffset::BindingPoint>(1, 7);
|
|
auto got = Run<FirstIndexOffset>(src, std::move(config));
|
|
|
|
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);
|
|
EXPECT_EQ(data->first_instance_offset, 0u);
|
|
}
|
|
|
|
TEST_F(FirstIndexOffsetTest, BasicModuleBothIndex) {
|
|
auto* src = R"(
|
|
fn test(instance_idx : u32, vert_idx : u32) -> u32 {
|
|
return instance_idx + vert_idx;
|
|
}
|
|
|
|
struct Inputs {
|
|
[[builtin(instance_index)]] instance_idx : u32;
|
|
[[builtin(vertex_index)]] vert_idx : u32;
|
|
};
|
|
|
|
[[stage(vertex)]]
|
|
fn entry(inputs : Inputs) {
|
|
test(inputs.instance_idx, inputs.vert_idx);
|
|
}
|
|
)";
|
|
|
|
auto* expect = R"(
|
|
[[block]]
|
|
struct tint_symbol {
|
|
first_vertex_index : u32;
|
|
first_instance_index : u32;
|
|
};
|
|
|
|
[[binding(1), group(2)]] var<uniform> tint_symbol_1 : tint_symbol;
|
|
|
|
fn test(instance_idx : u32, vert_idx : u32) -> u32 {
|
|
return (instance_idx + vert_idx);
|
|
}
|
|
|
|
struct Inputs {
|
|
[[builtin(instance_index)]]
|
|
instance_idx : u32;
|
|
[[builtin(vertex_index)]]
|
|
vert_idx : u32;
|
|
};
|
|
|
|
[[stage(vertex)]]
|
|
fn entry(inputs : Inputs) {
|
|
test((inputs.instance_idx + tint_symbol_1.first_instance_index), (inputs.vert_idx + tint_symbol_1.first_vertex_index));
|
|
}
|
|
)";
|
|
|
|
DataMap config;
|
|
config.Add<FirstIndexOffset::BindingPoint>(1, 2);
|
|
auto got = Run<FirstIndexOffset>(src, std::move(config));
|
|
|
|
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);
|
|
EXPECT_EQ(data->first_instance_offset, 4u);
|
|
}
|
|
|
|
TEST_F(FirstIndexOffsetTest, NestedCalls) {
|
|
auto* src = R"(
|
|
fn func1(vert_idx : u32) -> u32 {
|
|
return vert_idx;
|
|
}
|
|
|
|
fn func2(vert_idx : u32) -> u32 {
|
|
return func1(vert_idx);
|
|
}
|
|
|
|
[[stage(vertex)]]
|
|
fn entry([[builtin(vertex_index)]] vert_idx : u32) {
|
|
func2(vert_idx);
|
|
}
|
|
)";
|
|
|
|
auto* expect = R"(
|
|
[[block]]
|
|
struct tint_symbol {
|
|
first_vertex_index : u32;
|
|
};
|
|
|
|
[[binding(1), group(2)]] var<uniform> tint_symbol_1 : tint_symbol;
|
|
|
|
fn func1(vert_idx : u32) -> u32 {
|
|
return vert_idx;
|
|
}
|
|
|
|
fn func2(vert_idx : u32) -> u32 {
|
|
return func1(vert_idx);
|
|
}
|
|
|
|
[[stage(vertex)]]
|
|
fn entry([[builtin(vertex_index)]] vert_idx : u32) {
|
|
func2((vert_idx + tint_symbol_1.first_vertex_index));
|
|
}
|
|
)";
|
|
|
|
DataMap config;
|
|
config.Add<FirstIndexOffset::BindingPoint>(1, 2);
|
|
auto got = Run<FirstIndexOffset>(src, std::move(config));
|
|
|
|
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);
|
|
EXPECT_EQ(data->first_instance_offset, 0u);
|
|
}
|
|
|
|
TEST_F(FirstIndexOffsetTest, MultipleEntryPoints) {
|
|
auto* src = R"(
|
|
fn func(i : u32) -> u32 {
|
|
return i;
|
|
}
|
|
|
|
[[stage(vertex)]]
|
|
fn entry_a([[builtin(vertex_index)]] vert_idx : u32) {
|
|
func(vert_idx);
|
|
}
|
|
|
|
[[stage(vertex)]]
|
|
fn entry_b([[builtin(vertex_index)]] vert_idx : u32, [[builtin(instance_index)]] inst_idx : u32) {
|
|
func(vert_idx + inst_idx);
|
|
}
|
|
|
|
[[stage(vertex)]]
|
|
fn entry_c([[builtin(instance_index)]] inst_idx : u32) {
|
|
func(inst_idx);
|
|
}
|
|
)";
|
|
|
|
auto* expect = R"(
|
|
[[block]]
|
|
struct tint_symbol {
|
|
first_vertex_index : u32;
|
|
first_instance_index : u32;
|
|
};
|
|
|
|
[[binding(1), group(2)]] var<uniform> tint_symbol_1 : tint_symbol;
|
|
|
|
fn func(i : u32) -> u32 {
|
|
return i;
|
|
}
|
|
|
|
[[stage(vertex)]]
|
|
fn entry_a([[builtin(vertex_index)]] vert_idx : u32) {
|
|
func((vert_idx + tint_symbol_1.first_vertex_index));
|
|
}
|
|
|
|
[[stage(vertex)]]
|
|
fn entry_b([[builtin(vertex_index)]] vert_idx : u32, [[builtin(instance_index)]] inst_idx : u32) {
|
|
func(((vert_idx + tint_symbol_1.first_vertex_index) + (inst_idx + tint_symbol_1.first_instance_index)));
|
|
}
|
|
|
|
[[stage(vertex)]]
|
|
fn entry_c([[builtin(instance_index)]] inst_idx : u32) {
|
|
func((inst_idx + tint_symbol_1.first_instance_index));
|
|
}
|
|
)";
|
|
|
|
DataMap config;
|
|
config.Add<FirstIndexOffset::BindingPoint>(1, 2);
|
|
auto got = Run<FirstIndexOffset>(src, std::move(config));
|
|
|
|
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);
|
|
EXPECT_EQ(data->first_instance_offset, 4u);
|
|
}
|
|
|
|
TEST_F(FirstIndexOffsetTest, OLD_BasicModuleVertexIndex) {
|
|
auto* src = R"(
|
|
[[builtin(vertex_index)]] var<in> vert_idx : u32;
|
|
|
|
fn test() -> u32 {
|
|
return vert_idx;
|
|
}
|
|
|
|
[[stage(vertex)]]
|
|
fn entry() {
|
|
test();
|
|
}
|
|
)";
|
|
|
|
auto* expect = R"(
|
|
[[block]]
|
|
struct tint_symbol {
|
|
first_vertex_index : u32;
|
|
};
|
|
|
|
[[binding(1), group(2)]] var<uniform> tint_symbol_1 : tint_symbol;
|
|
|
|
[[builtin(vertex_index)]] var<in> vert_idx : u32;
|
|
|
|
fn test() -> u32 {
|
|
return (vert_idx + tint_symbol_1.first_vertex_index);
|
|
}
|
|
|
|
[[stage(vertex)]]
|
|
fn entry() {
|
|
test();
|
|
}
|
|
)";
|
|
|
|
auto got = Run(src, std::make_unique<FirstIndexOffset>(1, 2));
|
|
|
|
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);
|
|
EXPECT_EQ(data->first_instance_offset, 0u);
|
|
}
|
|
|
|
TEST_F(FirstIndexOffsetTest, OLD_BasicModuleInstanceIndex) {
|
|
auto* src = R"(
|
|
[[builtin(instance_index)]] var<in> inst_idx : u32;
|
|
|
|
fn test() -> u32 {
|
|
return inst_idx;
|
|
}
|
|
|
|
[[stage(vertex)]]
|
|
fn entry() {
|
|
test();
|
|
}
|
|
)";
|
|
|
|
auto* expect = R"(
|
|
[[block]]
|
|
struct tint_symbol {
|
|
first_instance_index : u32;
|
|
};
|
|
|
|
[[binding(1), group(7)]] var<uniform> tint_symbol_1 : tint_symbol;
|
|
|
|
[[builtin(instance_index)]] var<in> inst_idx : u32;
|
|
|
|
fn test() -> u32 {
|
|
return (inst_idx + tint_symbol_1.first_instance_index);
|
|
}
|
|
|
|
[[stage(vertex)]]
|
|
fn entry() {
|
|
test();
|
|
}
|
|
)";
|
|
|
|
auto got = Run(src, std::make_unique<FirstIndexOffset>(1, 7));
|
|
|
|
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);
|
|
EXPECT_EQ(data->first_instance_offset, 0u);
|
|
}
|
|
|
|
TEST_F(FirstIndexOffsetTest, OLD_BasicModuleBothIndex) {
|
|
auto* src = R"(
|
|
[[builtin(instance_index)]] var<in> instance_idx : u32;
|
|
[[builtin(vertex_index)]] var<in> vert_idx : u32;
|
|
|
|
fn test() -> u32 {
|
|
return instance_idx + vert_idx;
|
|
}
|
|
|
|
[[stage(vertex)]]
|
|
fn entry() {
|
|
test();
|
|
}
|
|
)";
|
|
|
|
auto* expect = R"(
|
|
[[block]]
|
|
struct tint_symbol {
|
|
first_vertex_index : u32;
|
|
first_instance_index : u32;
|
|
};
|
|
|
|
[[binding(1), group(2)]] var<uniform> tint_symbol_1 : tint_symbol;
|
|
|
|
[[builtin(instance_index)]] var<in> instance_idx : u32;
|
|
|
|
[[builtin(vertex_index)]] var<in> vert_idx : u32;
|
|
|
|
fn test() -> u32 {
|
|
return ((instance_idx + tint_symbol_1.first_instance_index) + (vert_idx + tint_symbol_1.first_vertex_index));
|
|
}
|
|
|
|
[[stage(vertex)]]
|
|
fn entry() {
|
|
test();
|
|
}
|
|
)";
|
|
|
|
auto got = Run(src, std::make_unique<FirstIndexOffset>(1, 2));
|
|
|
|
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);
|
|
EXPECT_EQ(data->first_instance_offset, 4u);
|
|
}
|
|
|
|
TEST_F(FirstIndexOffsetTest, OLD_NestedCalls) {
|
|
auto* src = R"(
|
|
[[builtin(vertex_index)]] var<in> vert_idx : u32;
|
|
|
|
fn func1() -> u32 {
|
|
return vert_idx;
|
|
}
|
|
|
|
fn func2() -> u32 {
|
|
return func1();
|
|
}
|
|
|
|
[[stage(vertex)]]
|
|
fn entry() {
|
|
func2();
|
|
}
|
|
)";
|
|
|
|
auto* expect = R"(
|
|
[[block]]
|
|
struct tint_symbol {
|
|
first_vertex_index : u32;
|
|
};
|
|
|
|
[[binding(1), group(2)]] var<uniform> tint_symbol_1 : tint_symbol;
|
|
|
|
[[builtin(vertex_index)]] var<in> vert_idx : u32;
|
|
|
|
fn func1() -> u32 {
|
|
return (vert_idx + tint_symbol_1.first_vertex_index);
|
|
}
|
|
|
|
fn func2() -> u32 {
|
|
return func1();
|
|
}
|
|
|
|
[[stage(vertex)]]
|
|
fn entry() {
|
|
func2();
|
|
}
|
|
)";
|
|
|
|
auto got = Run(src, std::make_unique<FirstIndexOffset>(1, 2));
|
|
|
|
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);
|
|
EXPECT_EQ(data->first_instance_offset, 0u);
|
|
}
|
|
|
|
} // namespace
|
|
} // namespace transform
|
|
} // namespace tint
|