transform: Reimplement FirstIndexOffset

* It didn't always produce valid WGSL (crbug.com/tint/687)
* It didn't handle builtins as entry point parameters
* It used hard-coded symbols that could collide
* It didn't use DataMap for input.

The new implementation addresses all of this.

Bug: tint:687
Change-Id: I447bec530b45414ebb8baeb4ee18261d73d1c0d2
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/47222
Commit-Queue: Ben Clayton <bclayton@chromium.org>
Commit-Queue: Ben Clayton <bclayton@google.com>
Reviewed-by: James Price <jrprice@google.com>
This commit is contained in:
Ben Clayton 2021-04-09 16:19:48 +00:00 committed by Commit Bot service account
parent e9c4984489
commit 1d618b1664
4 changed files with 462 additions and 273 deletions

View File

@ -445,9 +445,10 @@ class ProgramBuilder {
/// @param name the alias name
/// @param type the alias type
/// @returns the alias pointer
type::Alias* alias(const std::string& name, type::Type* type) const {
return builder->create<type::Alias>(builder->Symbols().Register(name),
type);
template <typename NAME>
type::Alias* alias(NAME&& name, type::Type* type) const {
return builder->create<type::Alias>(
builder->Sym(std::forward<NAME>(name)), type);
}
/// @return the tint AST pointer to `type` with the given ast::StorageClass
@ -469,9 +470,10 @@ class ProgramBuilder {
/// @param name the struct name
/// @param impl the struct implementation
/// @returns a struct pointer
type::Struct* struct_(const std::string& name, ast::Struct* impl) const {
return builder->create<type::Struct>(builder->Symbols().Register(name),
impl);
template <typename NAME>
type::Struct* struct_(NAME&& name, ast::Struct* impl) const {
return builder->create<type::Struct>(
builder->Sym(std::forward<NAME>(name)), impl);
}
private:
@ -1075,13 +1077,14 @@ class ProgramBuilder {
/// @param members the struct members
/// @param decorations the optional struct decorations
/// @returns the struct type
template <typename NAME>
type::Struct* Structure(const Source& source,
const std::string& name,
NAME&& name,
ast::StructMemberList members,
ast::DecorationList decorations = {}) {
auto* impl =
create<ast::Struct>(source, std::move(members), std::move(decorations));
auto* type = ty.struct_(name, impl);
auto* type = ty.struct_(Sym(std::forward<NAME>(name)), impl);
AST().AddConstructedType(type);
return type;
}
@ -1092,12 +1095,13 @@ class ProgramBuilder {
/// @param members the struct members
/// @param decorations the optional struct decorations
/// @returns the struct type
type::Struct* Structure(const std::string& name,
template <typename NAME>
type::Struct* Structure(NAME&& name,
ast::StructMemberList members,
ast::DecorationList decorations = {}) {
auto* impl =
create<ast::Struct>(std::move(members), std::move(decorations));
auto* type = ty.struct_(name, impl);
auto* type = ty.struct_(Sym(std::forward<NAME>(name)), impl);
AST().AddConstructedType(type);
return type;
}

View File

@ -15,40 +15,34 @@
#include "src/transform/first_index_offset.h"
#include <memory>
#include <unordered_map>
#include <utility>
#include "src/ast/struct_block_decoration.h"
#include "src/program_builder.h"
#include "src/semantic/function.h"
#include "src/semantic/member_accessor_expression.h"
#include "src/semantic/struct.h"
#include "src/semantic/variable.h"
TINT_INSTANTIATE_TYPEINFO(tint::transform::FirstIndexOffset::BindingPoint);
TINT_INSTANTIATE_TYPEINFO(tint::transform::FirstIndexOffset::Data);
namespace tint {
namespace transform {
namespace {
constexpr char kStructName[] = "TintFirstIndexOffsetData";
constexpr char kBufferName[] = "tint_first_index_data";
constexpr char kFirstVertexName[] = "tint_first_vertex_index";
constexpr char kFirstInstanceName[] = "tint_first_instance_index";
constexpr char kIndexOffsetPrefix[] = "tint_first_index_offset_";
ast::Variable* clone_variable_with_new_name(CloneContext* ctx,
ast::Variable* in,
std::string new_name) {
// Clone arguments outside of create() call to have deterministic ordering
auto source = ctx->Clone(in->source());
auto symbol = ctx->dst->Symbols().Register(new_name);
auto* type = ctx->Clone(in->declared_type());
auto* constructor = ctx->Clone(in->constructor());
auto decorations = ctx->Clone(in->decorations());
return ctx->dst->create<ast::Variable>(
source, symbol, in->declared_storage_class(), type, in->is_const(),
constructor, decorations);
}
// Uniform buffer member names
constexpr char kFirstVertexName[] = "first_vertex_index";
constexpr char kFirstInstanceName[] = "first_instance_index";
} // namespace
FirstIndexOffset::BindingPoint::BindingPoint() = default;
FirstIndexOffset::BindingPoint::BindingPoint(uint32_t b, uint32_t g)
: binding(b), group(g) {}
FirstIndexOffset::BindingPoint::~BindingPoint() = default;
FirstIndexOffset::Data::Data(bool has_vtx_index,
bool has_inst_index,
uint32_t first_vtx_offset,
@ -57,172 +51,134 @@ FirstIndexOffset::Data::Data(bool has_vtx_index,
has_instance_index(has_inst_index),
first_vertex_offset(first_vtx_offset),
first_instance_offset(first_inst_offset) {}
FirstIndexOffset::Data::Data(const Data&) = default;
FirstIndexOffset::Data::~Data() = default;
FirstIndexOffset::FirstIndexOffset() = default;
FirstIndexOffset::FirstIndexOffset(uint32_t binding, uint32_t group)
: binding_(binding), group_(group) {}
FirstIndexOffset::~FirstIndexOffset() = default;
Transform::Output FirstIndexOffset::Run(const Program* in, const DataMap&) {
ProgramBuilder out;
// First do a quick check to see if the transform has already been applied.
for (ast::Variable* var : in->AST().GlobalVariables()) {
if (auto* dec_var = var->As<ast::Variable>()) {
if (dec_var->symbol() == in->Symbols().Get(kBufferName)) {
out.Diagnostics().add_error(
"First index offset transform has already been applied.");
return Output(Program(std::move(out)));
}
}
Transform::Output FirstIndexOffset::Run(const Program* in,
const DataMap& data) {
// Get the uniform buffer binding point
uint32_t ub_binding = binding_;
uint32_t ub_group = group_;
if (auto* binding_point = data.Get<BindingPoint>()) {
ub_binding = binding_point->binding;
ub_group = binding_point->group;
}
State state{&out, binding_, group_};
Symbol vertex_index_sym;
Symbol instance_index_sym;
// Lazily construct the UniformBuffer on first call to
// maybe_create_buffer_var()
ast::Variable* buffer_var = nullptr;
auto maybe_create_buffer_var = [&]() {
if (buffer_var == nullptr) {
buffer_var = state.AddUniformBuffer();
}
};
// Clone the AST, renaming the kVertexIndex and kInstanceIndex builtins, and
// add a CreateFirstIndexOffset() statement to each function that uses one of
// these builtins.
ProgramBuilder out;
CloneContext ctx(&out, in);
ctx.ReplaceAll([&](ast::Variable* var) -> ast::Variable* {
for (ast::Decoration* dec : var->decorations()) {
if (auto* blt_dec = dec->As<ast::BuiltinDecoration>()) {
ast::Builtin blt_type = blt_dec->value();
if (blt_type == ast::Builtin::kVertexIndex) {
vertex_index_sym = var->symbol();
state.has_vertex_index = true;
return clone_variable_with_new_name(
&ctx, var,
kIndexOffsetPrefix + in->Symbols().NameFor(var->symbol()));
} else if (blt_type == ast::Builtin::kInstanceIndex) {
instance_index_sym = var->symbol();
state.has_instance_index = true;
return clone_variable_with_new_name(
&ctx, var,
kIndexOffsetPrefix + in->Symbols().NameFor(var->symbol()));
}
}
}
return nullptr; // Just clone var
});
ctx.ReplaceAll( // Note: This happens in the same pass as the rename above
// which determines the original builtin variable names,
// but this should be fine, as variables are cloned first.
[&](ast::Function* func) -> ast::Function* {
maybe_create_buffer_var();
if (buffer_var == nullptr) {
return nullptr; // no transform need, just clone func
}
auto* func_sem = in->Sem().Get(func);
ast::StatementList statements;
for (const auto& data : func_sem->LocalReferencedBuiltinVariables()) {
if (data.second->value() == ast::Builtin::kVertexIndex) {
statements.emplace_back(state.CreateFirstIndexOffset(
in->Symbols().NameFor(vertex_index_sym), kFirstVertexName,
buffer_var));
} else if (data.second->value() == ast::Builtin::kInstanceIndex) {
statements.emplace_back(state.CreateFirstIndexOffset(
in->Symbols().NameFor(instance_index_sym), kFirstInstanceName,
buffer_var));
// Map of builtin usages
std::unordered_map<const semantic::Variable*, const char*> builtin_vars;
std::unordered_map<const semantic::StructMember*, const char*>
builtin_members;
bool has_vertex_index = false;
bool has_instance_index = false;
// Traverse the AST scanning for builtin accesses via variables (includes
// parameters) or structure member accesses.
for (auto* node : in->ASTNodes().Objects()) {
if (auto* var = node->As<ast::Variable>()) {
for (ast::Decoration* dec : var->decorations()) {
if (auto* builtin_dec = dec->As<ast::BuiltinDecoration>()) {
ast::Builtin builtin = builtin_dec->value();
if (builtin == ast::Builtin::kVertexIndex) {
auto* sem_var = ctx.src->Sem().Get(var);
builtin_vars.emplace(sem_var, kFirstVertexName);
has_vertex_index = true;
}
if (builtin == ast::Builtin::kInstanceIndex) {
auto* sem_var = ctx.src->Sem().Get(var);
builtin_vars.emplace(sem_var, kFirstInstanceName);
has_instance_index = true;
}
}
return CloneWithStatementsAtStart(&ctx, func, statements);
});
}
}
if (auto* member = node->As<ast::StructMember>()) {
for (ast::Decoration* dec : member->decorations()) {
if (auto* builtin_dec = dec->As<ast::BuiltinDecoration>()) {
ast::Builtin builtin = builtin_dec->value();
if (builtin == ast::Builtin::kVertexIndex) {
auto* sem_mem = ctx.src->Sem().Get(member);
builtin_members.emplace(sem_mem, kFirstVertexName);
has_vertex_index = true;
}
if (builtin == ast::Builtin::kInstanceIndex) {
auto* sem_mem = ctx.src->Sem().Get(member);
builtin_members.emplace(sem_mem, kFirstInstanceName);
has_instance_index = true;
}
}
}
}
}
// Byte offsets on the uniform buffer
uint32_t vertex_index_offset = 0;
uint32_t instance_index_offset = 0;
if (has_vertex_index || has_instance_index) {
// Add uniform buffer members and calculate byte offsets
uint32_t offset = 0;
ast::StructMemberList members;
if (has_vertex_index) {
members.push_back(ctx.dst->Member(kFirstVertexName, ctx.dst->ty.u32()));
vertex_index_offset = offset;
offset += 4;
}
if (has_instance_index) {
members.push_back(ctx.dst->Member(kFirstInstanceName, ctx.dst->ty.u32()));
instance_index_offset = offset;
offset += 4;
}
auto* struct_type =
ctx.dst->Structure(ctx.dst->Symbols().New(), std::move(members),
{ctx.dst->create<ast::StructBlockDecoration>()});
// Create a global to hold the uniform buffer
Symbol buffer_name = ctx.dst->Symbols().New();
ctx.dst->Global(buffer_name, struct_type, ast::StorageClass::kUniform,
nullptr,
ast::DecorationList{
ctx.dst->create<ast::BindingDecoration>(ub_binding),
ctx.dst->create<ast::GroupDecoration>(ub_group),
});
// Fix up all references to the builtins with the offsets
ctx.ReplaceAll([=, &ctx](ast::Expression* expr) -> ast::Expression* {
auto* sem = ctx.src->Sem().Get(expr);
if (auto* user = sem->As<semantic::VariableUser>()) {
auto it = builtin_vars.find(user->Variable());
if (it != builtin_vars.end()) {
return ctx.dst->Add(ctx.CloneWithoutTransform(expr),
ctx.dst->MemberAccessor(buffer_name, it->second));
}
}
if (auto* access = sem->As<semantic::StructMemberAccess>()) {
auto it = builtin_members.find(access->Member());
if (it != builtin_members.end()) {
return ctx.dst->Add(ctx.CloneWithoutTransform(expr),
ctx.dst->MemberAccessor(buffer_name, it->second));
}
}
// Not interested in this experssion. Just clone.
return nullptr;
});
}
ctx.Clone();
return Output(Program(std::move(out)),
std::make_unique<Data>(
state.has_vertex_index, state.has_instance_index,
state.vertex_index_offset, state.instance_index_offset));
}
ast::Variable* FirstIndexOffset::State::AddUniformBuffer() {
auto* u32_type = dst->create<type::U32>();
uint32_t offset = 0;
ast::StructMemberList members;
if (has_vertex_index) {
members.push_back(dst->create<ast::StructMember>(
Source{}, dst->Symbols().Register(kFirstVertexName), u32_type,
ast::DecorationList{}));
vertex_index_offset = offset;
offset += 4;
}
if (has_instance_index) {
members.push_back(dst->create<ast::StructMember>(
Source{}, dst->Symbols().Register(kFirstInstanceName), u32_type,
ast::DecorationList{}));
instance_index_offset = offset;
offset += 4;
}
ast::DecorationList decos;
decos.push_back(dst->create<ast::StructBlockDecoration>(Source{}));
auto* struct_type = dst->create<type::Struct>(
dst->Symbols().Register(kStructName),
dst->create<ast::Struct>(Source{}, std::move(members), std::move(decos)));
auto* idx_var = dst->create<ast::Variable>(
Source{}, // source
dst->Symbols().Register(kBufferName), // symbol
ast::StorageClass::kUniform, // storage_class
struct_type, // type
false, // is_const
nullptr, // constructor
ast::DecorationList{
dst->create<ast::BindingDecoration>(Source{}, binding),
dst->create<ast::GroupDecoration>(Source{}, group),
});
dst->AST().AddConstructedType(struct_type);
dst->AST().AddGlobalVariable(idx_var);
return idx_var;
}
ast::VariableDeclStatement* FirstIndexOffset::State::CreateFirstIndexOffset(
const std::string& original_name,
const std::string& field_name,
ast::Variable* buffer_var) {
auto* buffer =
dst->create<ast::IdentifierExpression>(Source{}, buffer_var->symbol());
auto lhs_name = kIndexOffsetPrefix + original_name;
auto* constructor = dst->create<ast::BinaryExpression>(
Source{}, ast::BinaryOp::kAdd,
dst->create<ast::IdentifierExpression>(Source{},
dst->Symbols().Register(lhs_name)),
dst->create<ast::MemberAccessorExpression>(
Source{}, buffer,
dst->create<ast::IdentifierExpression>(
Source{}, dst->Symbols().Register(field_name))));
auto* var = dst->create<ast::Variable>(
Source{}, // source
dst->Symbols().Register(original_name), // symbol
ast::StorageClass::kNone, // storage_class
dst->create<type::U32>(), // type
true, // is_const
constructor, // constructor
ast::DecorationList{}); // decorations
return dst->create<ast::VariableDeclStatement>(Source{}, var);
return Output(
Program(std::move(out)),
std::make_unique<Data>(has_vertex_index, has_instance_index,
vertex_index_offset, instance_index_offset));
}
} // namespace transform

View File

@ -15,9 +15,6 @@
#ifndef SRC_TRANSFORM_FIRST_INDEX_OFFSET_H_
#define SRC_TRANSFORM_FIRST_INDEX_OFFSET_H_
#include <string>
#include "src/ast/variable_decl_statement.h"
#include "src/transform/transform.h"
namespace tint {
@ -60,6 +57,27 @@ namespace transform {
///
class FirstIndexOffset : public Transform {
public:
/// BindingPoint is consumed by the FirstIndexOffset transform.
/// BindingPoint specifies the binding point of the first index uniform
/// buffer.
struct BindingPoint : public Castable<BindingPoint, transform::Data> {
/// Constructor
BindingPoint();
/// Constructor
/// @param b the binding index
/// @param g the binding group
BindingPoint(uint32_t b, uint32_t g);
/// Destructor
~BindingPoint() override;
/// [[binding()]] for the first vertex / first instance uniform buffer
uint32_t binding = 0;
/// [[group()]] for the first vertex / first instance uniform buffer
uint32_t group = 0;
};
/// Data is outputted by the FirstIndexOffset transform.
/// Data holds information about shader usage and constant buffer offsets.
struct Data : public Castable<Data, transform::Data> {
@ -90,6 +108,9 @@ class FirstIndexOffset : public Transform {
};
/// Constructor
FirstIndexOffset();
/// Constructor
/// [DEPRECATED] - pass BindingPoint as part of the `data` to Run()
/// @param binding the binding() for firstVertex/Instance uniform
/// @param group the group() for firstVertex/Instance uniform
FirstIndexOffset(uint32_t binding, uint32_t group);
@ -102,32 +123,10 @@ class FirstIndexOffset : public Transform {
Output Run(const Program* program, const DataMap& data = {}) override;
private:
struct State {
/// Adds uniform buffer with firstVertex/Instance to the program builder
/// @returns variable of new uniform buffer
ast::Variable* AddUniformBuffer();
/// Adds constant with modified original_name builtin to func
/// @param original_name the name of the original builtin used in function
/// @param field_name name of field in firstVertex/Instance buffer
/// @param buffer_var variable of firstVertex/Instance buffer
ast::VariableDeclStatement* CreateFirstIndexOffset(
const std::string& original_name,
const std::string& field_name,
ast::Variable* buffer_var);
ProgramBuilder* const dst;
uint32_t const binding;
uint32_t const group;
bool has_vertex_index = false;
bool has_instance_index = false;
uint32_t vertex_index_offset = 0;
uint32_t instance_index_offset = 0;
};
uint32_t binding_;
uint32_t group_;
uint32_t binding_ = 0;
uint32_t group_ = 0;
};
} // namespace transform
} // namespace tint

View File

@ -26,45 +26,13 @@ namespace {
using FirstIndexOffsetTest = TransformTest;
TEST_F(FirstIndexOffsetTest, Error_AlreadyTransformed) {
auto* src = R"(
[[builtin(vertex_index)]] var<in> vert_idx : u32;
fn test() -> u32 {
return vert_idx;
}
[[stage(vertex)]]
fn entry() {
test();
}
)";
auto* expect =
"error: First index offset transform has already been applied.";
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));
auto got = Run(src, std::move(transforms));
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, EmptyModule) {
auto* src = "";
auto* expect = "";
auto got = Run(src, std::make_unique<FirstIndexOffset>(0, 0));
DataMap config;
config.Add<FirstIndexOffset::BindingPoint>(0, 0);
auto got = Run<FirstIndexOffset>(src, std::move(config));
EXPECT_EQ(expect, str(got));
@ -79,6 +47,273 @@ TEST_F(FirstIndexOffsetTest, EmptyModule) {
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_2 {
first_vertex_index : u32;
};
[[binding(1), group(2)]] var<uniform> tint_symbol_3 : tint_symbol_2;
fn test(vert_idx : u32) -> u32 {
return vert_idx;
}
[[stage(vertex)]]
fn entry([[builtin(vertex_index)]] vert_idx : u32) {
test((vert_idx + tint_symbol_3.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_2 {
first_instance_index : u32;
};
[[binding(1), group(7)]] var<uniform> tint_symbol_3 : tint_symbol_2;
fn test(inst_idx : u32) -> u32 {
return inst_idx;
}
[[stage(vertex)]]
fn entry([[builtin(instance_index)]] inst_idx : u32) {
test((inst_idx + tint_symbol_3.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_3 {
first_vertex_index : u32;
first_instance_index : u32;
};
[[binding(1), group(2)]] var<uniform> tint_symbol_4 : tint_symbol_3;
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_4.first_instance_index), (inputs.vert_idx + tint_symbol_4.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_2 {
first_vertex_index : u32;
};
[[binding(1), group(2)]] var<uniform> tint_symbol_3 : tint_symbol_2;
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_3.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_3 {
first_vertex_index : u32;
first_instance_index : u32;
};
[[binding(1), group(2)]] var<uniform> tint_symbol_4 : tint_symbol_3;
fn func(i : u32) -> u32 {
return i;
}
[[stage(vertex)]]
fn entry_a([[builtin(vertex_index)]] vert_idx : u32) {
func((vert_idx + tint_symbol_4.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_4.first_vertex_index) + (inst_idx + tint_symbol_4.first_instance_index)));
}
[[stage(vertex)]]
fn entry_c([[builtin(instance_index)]] inst_idx : u32) {
func((inst_idx + tint_symbol_4.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 {
@ -93,17 +328,16 @@ fn entry() {
auto* expect = R"(
[[block]]
struct TintFirstIndexOffsetData {
tint_first_vertex_index : u32;
struct tint_symbol_2 {
first_vertex_index : u32;
};
[[binding(1), group(2)]] var<uniform> tint_first_index_data : TintFirstIndexOffsetData;
[[binding(1), group(2)]] var<uniform> tint_symbol_3 : tint_symbol_2;
[[builtin(vertex_index)]] var<in> tint_first_index_offset_vert_idx : u32;
[[builtin(vertex_index)]] var<in> vert_idx : u32;
fn test() -> u32 {
let vert_idx : u32 = (tint_first_index_offset_vert_idx + tint_first_index_data.tint_first_vertex_index);
return vert_idx;
return (vert_idx + tint_symbol_3.first_vertex_index);
}
[[stage(vertex)]]
@ -125,7 +359,7 @@ fn entry() {
EXPECT_EQ(data->first_instance_offset, 0u);
}
TEST_F(FirstIndexOffsetTest, BasicModuleInstanceIndex) {
TEST_F(FirstIndexOffsetTest, OLD_BasicModuleInstanceIndex) {
auto* src = R"(
[[builtin(instance_index)]] var<in> inst_idx : u32;
@ -141,17 +375,16 @@ fn entry() {
auto* expect = R"(
[[block]]
struct TintFirstIndexOffsetData {
tint_first_instance_index : u32;
struct tint_symbol_2 {
first_instance_index : u32;
};
[[binding(1), group(7)]] var<uniform> tint_first_index_data : TintFirstIndexOffsetData;
[[binding(1), group(7)]] var<uniform> tint_symbol_3 : tint_symbol_2;
[[builtin(instance_index)]] var<in> tint_first_index_offset_inst_idx : u32;
[[builtin(instance_index)]] var<in> inst_idx : u32;
fn test() -> u32 {
let inst_idx : u32 = (tint_first_index_offset_inst_idx + tint_first_index_data.tint_first_instance_index);
return inst_idx;
return (inst_idx + tint_symbol_3.first_instance_index);
}
[[stage(vertex)]]
@ -173,7 +406,7 @@ fn entry() {
EXPECT_EQ(data->first_instance_offset, 0u);
}
TEST_F(FirstIndexOffsetTest, BasicModuleBothIndex) {
TEST_F(FirstIndexOffsetTest, OLD_BasicModuleBothIndex) {
auto* src = R"(
[[builtin(instance_index)]] var<in> instance_idx : u32;
[[builtin(vertex_index)]] var<in> vert_idx : u32;
@ -190,21 +423,19 @@ fn entry() {
auto* expect = R"(
[[block]]
struct TintFirstIndexOffsetData {
tint_first_vertex_index : u32;
tint_first_instance_index : u32;
struct tint_symbol_3 {
first_vertex_index : u32;
first_instance_index : u32;
};
[[binding(1), group(2)]] var<uniform> tint_first_index_data : TintFirstIndexOffsetData;
[[binding(1), group(2)]] var<uniform> tint_symbol_4 : tint_symbol_3;
[[builtin(instance_index)]] var<in> tint_first_index_offset_instance_idx : u32;
[[builtin(instance_index)]] var<in> instance_idx : u32;
[[builtin(vertex_index)]] var<in> tint_first_index_offset_vert_idx : u32;
[[builtin(vertex_index)]] var<in> vert_idx : u32;
fn test() -> u32 {
let instance_idx : u32 = (tint_first_index_offset_instance_idx + tint_first_index_data.tint_first_instance_index);
let vert_idx : u32 = (tint_first_index_offset_vert_idx + tint_first_index_data.tint_first_vertex_index);
return (instance_idx + vert_idx);
return ((instance_idx + tint_symbol_4.first_instance_index) + (vert_idx + tint_symbol_4.first_vertex_index));
}
[[stage(vertex)]]
@ -226,7 +457,7 @@ fn entry() {
EXPECT_EQ(data->first_instance_offset, 4u);
}
TEST_F(FirstIndexOffsetTest, NestedCalls) {
TEST_F(FirstIndexOffsetTest, OLD_NestedCalls) {
auto* src = R"(
[[builtin(vertex_index)]] var<in> vert_idx : u32;
@ -246,17 +477,16 @@ fn entry() {
auto* expect = R"(
[[block]]
struct TintFirstIndexOffsetData {
tint_first_vertex_index : u32;
struct tint_symbol_2 {
first_vertex_index : u32;
};
[[binding(1), group(2)]] var<uniform> tint_first_index_data : TintFirstIndexOffsetData;
[[binding(1), group(2)]] var<uniform> tint_symbol_3 : tint_symbol_2;
[[builtin(vertex_index)]] var<in> tint_first_index_offset_vert_idx : u32;
[[builtin(vertex_index)]] var<in> vert_idx : u32;
fn func1() -> u32 {
let vert_idx : u32 = (tint_first_index_offset_vert_idx + tint_first_index_data.tint_first_vertex_index);
return vert_idx;
return (vert_idx + tint_symbol_3.first_vertex_index);
}
fn func2() -> u32 {