ast: Merge DecoratedVariable into Variable

Remove all Variable setters (with exception to set_storage_class() which is called by the TypeDeterminer)

Bug: tint:390
Change-Id: I172667e21e2b02e85dcea6703aa1e608ec718250
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/35015
Commit-Queue: Ben Clayton <bclayton@google.com>
Reviewed-by: dan sinclair <dsinclair@chromium.org>
This commit is contained in:
Ben Clayton
2020-12-11 13:07:02 +00:00
committed by Commit Bot service account
parent ccc67252ff
commit a80511e021
79 changed files with 4259 additions and 2268 deletions

View File

@@ -122,8 +122,7 @@ TEST_F(BoundArrayAccessorsTest, Ptrs_Clamp) {
Var("a", ast::StorageClass::kFunction, ty.array<f32, 3>());
Const("c", ast::StorageClass::kFunction, ty.u32);
Const("b", ast::StorageClass::kFunction,
ty.pointer<f32>(ast::StorageClass::kFunction))
->set_constructor(Index("a", "c"));
ty.pointer<f32>(ast::StorageClass::kFunction), Index("a", "c"), {});
}
};
@@ -171,8 +170,8 @@ TEST_F(BoundArrayAccessorsTest, Array_Idx_Nested_Scalar) {
Var("a", ast::StorageClass::kFunction, ty.array<f32, 3>());
Var("b", ast::StorageClass::kFunction, ty.array<f32, 5>());
Var("i", ast::StorageClass::kFunction, ty.u32);
Const("c", ast::StorageClass::kFunction, ty.f32)
->set_constructor(Index("a", Index("b", "i")));
Const("c", ast::StorageClass::kFunction, ty.f32,
Index("a", Index("b", "i")), {});
}
};
@@ -241,8 +240,7 @@ TEST_F(BoundArrayAccessorsTest, Array_Idx_Scalar) {
struct Builder : ModuleBuilder {
void Build() override {
Var("a", ast::StorageClass::kFunction, ty.array(ty.f32, 3));
Var("b", ast::StorageClass::kFunction, ty.f32)
->set_constructor(Index("a", 1u));
Var("b", ast::StorageClass::kFunction, ty.f32, Index("a", 1u), {});
}
};
@@ -274,8 +272,8 @@ TEST_F(BoundArrayAccessorsTest, Array_Idx_Expr) {
void Build() override {
Var("a", ast::StorageClass::kFunction, ty.array<f32, 3>());
Var("c", ast::StorageClass::kFunction, ty.u32);
Var("b", ast::StorageClass::kFunction, ty.f32)
->set_constructor(Index("a", Add("c", Sub(2u, 3u))));
Var("b", ast::StorageClass::kFunction, ty.f32,
Index("a", Add("c", Sub(2u, 3u))), {});
}
};
@@ -340,8 +338,7 @@ TEST_F(BoundArrayAccessorsTest, Array_Idx_Negative) {
struct Builder : ModuleBuilder {
void Build() override {
Var("a", ast::StorageClass::kFunction, ty.array<f32, 3>());
Var("b", ast::StorageClass::kFunction, ty.f32)
->set_constructor(Index("a", -1));
Var("b", ast::StorageClass::kFunction, ty.f32, Index("a", -1), {});
}
};
@@ -371,8 +368,7 @@ TEST_F(BoundArrayAccessorsTest, Array_Idx_OutOfBounds) {
struct Builder : ModuleBuilder {
void Build() override {
Var("a", ast::StorageClass::kFunction, ty.array<f32, 3>());
Var("b", ast::StorageClass::kFunction, ty.f32)
->set_constructor(Index("a", 3u));
Var("b", ast::StorageClass::kFunction, ty.f32, Index("a", 3u), {});
}
};
@@ -402,8 +398,7 @@ TEST_F(BoundArrayAccessorsTest, Vector_Idx_Scalar) {
struct Builder : ModuleBuilder {
void Build() override {
Var("a", ast::StorageClass::kFunction, ty.vec3<f32>());
Var("b", ast::StorageClass::kFunction, ty.f32)
->set_constructor(Index("a", 1u));
Var("b", ast::StorageClass::kFunction, ty.f32, Index("a", 1u), {});
}
};
@@ -435,8 +430,8 @@ TEST_F(BoundArrayAccessorsTest, Vector_Idx_Expr) {
void Build() override {
Var("a", ast::StorageClass::kFunction, ty.vec3<f32>());
Var("c", ast::StorageClass::kFunction, ty.u32);
Var("b", ast::StorageClass::kFunction, ty.f32)
->set_constructor(Index("a", Add("c", Sub(2u, 3u))));
Var("b", ast::StorageClass::kFunction, ty.f32,
Index("a", Add("c", Sub(2u, 3u))), {});
}
};
@@ -499,8 +494,7 @@ TEST_F(BoundArrayAccessorsTest, Vector_Idx_Negative) {
struct Builder : ModuleBuilder {
void Build() override {
Var("a", ast::StorageClass::kFunction, ty.vec3<f32>());
Var("b", ast::StorageClass::kFunction, ty.f32)
->set_constructor(Index("a", -1));
Var("b", ast::StorageClass::kFunction, ty.f32, Index("a", -1), {});
}
};
@@ -530,8 +524,7 @@ TEST_F(BoundArrayAccessorsTest, Vector_Idx_OutOfBounds) {
struct Builder : ModuleBuilder {
void Build() override {
Var("a", ast::StorageClass::kFunction, ty.vec3<f32>());
Var("b", ast::StorageClass::kFunction, ty.f32)
->set_constructor(Index("a", 3u));
Var("b", ast::StorageClass::kFunction, ty.f32, Index("a", 3u), {});
}
};
@@ -561,8 +554,8 @@ TEST_F(BoundArrayAccessorsTest, Matrix_Idx_Scalar) {
struct Builder : ModuleBuilder {
void Build() override {
Var("a", ast::StorageClass::kFunction, ty.mat3x2<f32>());
Var("b", ast::StorageClass::kFunction, ty.f32)
->set_constructor(Index(Index("a", 2u), 1u));
Var("b", ast::StorageClass::kFunction, ty.f32, Index(Index("a", 2u), 1u),
{});
}
};
@@ -607,8 +600,8 @@ TEST_F(BoundArrayAccessorsTest, Matrix_Idx_Expr_Column) {
void Build() override {
Var("a", ast::StorageClass::kFunction, ty.mat3x2<f32>());
Var("c", ast::StorageClass::kFunction, ty.u32);
Var("b", ast::StorageClass::kFunction, ty.f32)
->set_constructor(Index(Index("a", Add("c", Sub(2u, 3u))), 1u));
Var("b", ast::StorageClass::kFunction, ty.f32,
Index(Index("a", Add("c", Sub(2u, 3u))), 1u), {});
}
};
@@ -687,8 +680,8 @@ TEST_F(BoundArrayAccessorsTest, Matrix_Idx_Expr_Row) {
void Build() override {
Var("a", ast::StorageClass::kFunction, ty.mat3x2<f32>());
Var("c", ast::StorageClass::kFunction, ty.u32);
Var("b", ast::StorageClass::kFunction, ty.f32)
->set_constructor(Index(Index("a", 1u), Add("c", Sub(2u, 3u))));
Var("b", ast::StorageClass::kFunction, ty.f32,
Index(Index("a", 1u), Add("c", Sub(2u, 3u))), {});
}
};
@@ -765,8 +758,8 @@ TEST_F(BoundArrayAccessorsTest, Matrix_Idx_Negative_Column) {
struct Builder : ModuleBuilder {
void Build() override {
Var("a", ast::StorageClass::kFunction, ty.mat3x2<f32>());
Var("b", ast::StorageClass::kFunction, ty.f32)
->set_constructor(Index(Index("a", -1), 1));
Var("b", ast::StorageClass::kFunction, ty.f32, Index(Index("a", -1), 1),
{});
}
};
@@ -809,8 +802,8 @@ TEST_F(BoundArrayAccessorsTest, Matrix_Idx_Negative_Row) {
struct Builder : ModuleBuilder {
void Build() override {
Var("a", ast::StorageClass::kFunction, ty.mat3x2<f32>());
Var("b", ast::StorageClass::kFunction, ty.f32)
->set_constructor(Index(Index("a", 2), -1));
Var("b", ast::StorageClass::kFunction, ty.f32, Index(Index("a", 2), -1),
{});
}
};
@@ -853,8 +846,8 @@ TEST_F(BoundArrayAccessorsTest, Matrix_Idx_OutOfBounds_Column) {
struct Builder : ModuleBuilder {
void Build() override {
Var("a", ast::StorageClass::kFunction, ty.mat3x2<f32>());
Var("b", ast::StorageClass::kFunction, ty.f32)
->set_constructor(Index(Index("a", 5u), 1u));
Var("b", ast::StorageClass::kFunction, ty.f32, Index(Index("a", 5u), 1u),
{});
}
};
@@ -897,8 +890,8 @@ TEST_F(BoundArrayAccessorsTest, Matrix_Idx_OutOfBounds_Row) {
struct Builder : ModuleBuilder {
void Build() override {
Var("a", ast::StorageClass::kFunction, ty.mat3x2<f32>());
Var("b", ast::StorageClass::kFunction, ty.f32)
->set_constructor(Index(Index("a", 2u), 5u));
Var("b", ast::StorageClass::kFunction, ty.f32, Index(Index("a", 2u), 5u),
{});
}
};

View File

@@ -19,12 +19,12 @@
#include "src/ast/assignment_statement.h"
#include "src/ast/block_statement.h"
#include "src/ast/decorated_variable.h"
#include "src/ast/float_literal.h"
#include "src/ast/identifier_expression.h"
#include "src/ast/scalar_constructor_expression.h"
#include "src/ast/type/f32_type.h"
#include "src/ast/type_manager.h"
#include "src/ast/variable.h"
namespace tint {
namespace transform {
@@ -51,11 +51,17 @@ Transform::Output EmitVertexPointSize::Run(ast::Module* in) {
// Declare the pointsize builtin output variable.
auto* pointsize_var =
mod->create<ast::DecoratedVariable>(mod->create<ast::Variable>(
Source{}, kPointSizeVar, ast::StorageClass::kOutput, f32));
pointsize_var->set_decorations({
mod->create<ast::BuiltinDecoration>(ast::Builtin::kPointSize, Source{}),
});
mod->create<ast::Variable>(Source{}, // source
kPointSizeVar, // name
ast::StorageClass::kOutput, // storage_class
f32, // type
false, // is_const
nullptr, // constructor
ast::VariableDecorationList{
// decorations
mod->create<ast::BuiltinDecoration>(
ast::Builtin::kPointSize, Source{}),
});
mod->AddGlobalVariable(pointsize_var);
// Build the AST expression & statement for assigning pointsize one.

View File

@@ -83,7 +83,7 @@ TEST_F(EmitVertexPointSizeTest, VertexStageBasic) {
<< diag::Formatter().format(result.diagnostics);
auto* expected = R"(Module{
DecoratedVariable{
Variable{
Decorations{
BuiltinDecoration{pointsize}
}
@@ -148,7 +148,7 @@ TEST_F(EmitVertexPointSizeTest, VertexStageEmpty) {
<< diag::Formatter().format(result.diagnostics);
auto* expected = R"(Module{
DecoratedVariable{
Variable{
Decorations{
BuiltinDecoration{pointsize}
}

View File

@@ -27,7 +27,6 @@
#include "src/ast/case_statement.h"
#include "src/ast/clone_context.h"
#include "src/ast/constructor_expression.h"
#include "src/ast/decorated_variable.h"
#include "src/ast/else_statement.h"
#include "src/ast/expression.h"
#include "src/ast/fallthrough_statement.h"
@@ -62,18 +61,17 @@ constexpr char kFirstVertexName[] = "tint_first_vertex_index";
constexpr char kFirstInstanceName[] = "tint_first_instance_index";
constexpr char kIndexOffsetPrefix[] = "tint_first_index_offset_";
ast::DecoratedVariable* clone_variable_with_new_name(ast::CloneContext* ctx,
ast::DecoratedVariable* in,
std::string new_name) {
auto* var = ctx->mod->create<ast::Variable>(ctx->Clone(in->source()),
new_name, in->storage_class(),
ctx->Clone(in->type()));
var->set_is_const(in->is_const());
var->set_constructor(ctx->Clone(in->constructor()));
auto* out = ctx->mod->create<ast::DecoratedVariable>(var);
out->set_decorations(ctx->Clone(in->decorations()));
return out;
ast::Variable* clone_variable_with_new_name(ast::CloneContext* ctx,
ast::Variable* in,
std::string new_name) {
return ctx->mod->create<ast::Variable>(
ctx->Clone(in->source()), // source
new_name, // name
in->storage_class(), // storage_class
ctx->Clone(in->type()), // type
in->is_const(), // is_const
ctx->Clone(in->constructor()), // constructor
ctx->Clone(in->decorations())); // decorations
}
} // namespace
@@ -86,7 +84,7 @@ FirstIndexOffset::~FirstIndexOffset() = default;
Transform::Output FirstIndexOffset::Run(ast::Module* in) {
// First do a quick check to see if the transform has already been applied.
for (ast::Variable* var : in->global_variables()) {
if (auto* dec_var = var->As<ast::DecoratedVariable>()) {
if (auto* dec_var = var->As<ast::Variable>()) {
if (dec_var->name() == kBufferName) {
diag::Diagnostic err;
err.message = "First index offset transform has already been applied.";
@@ -129,7 +127,7 @@ Transform::Output FirstIndexOffset::Run(ast::Module* in) {
// a CreateFirstIndexOffset() statement to each function that uses one of
// these builtins.
ast::CloneContext ctx(&out.module);
ctx.ReplaceAll([&](ast::DecoratedVariable* var) -> ast::DecoratedVariable* {
ctx.ReplaceAll([&](ast::Variable* var) -> ast::Variable* {
for (ast::VariableDecoration* dec : var->decorations()) {
if (auto* blt_dec = dec->As<ast::BuiltinDecoration>()) {
ast::Builtin blt_type = blt_dec->value();
@@ -229,13 +227,17 @@ ast::Variable* FirstIndexOffset::AddUniformBuffer(ast::Module* mod) {
kStructName,
mod->create<ast::Struct>(std::move(decos), std::move(members)));
auto* idx_var =
mod->create<ast::DecoratedVariable>(mod->create<ast::Variable>(
Source{}, kBufferName, ast::StorageClass::kUniform, struct_type));
idx_var->set_decorations({
mod->create<ast::BindingDecoration>(binding_, Source{}),
mod->create<ast::SetDecoration>(set_, Source{}),
});
auto* idx_var = mod->create<ast::Variable>(
Source{}, // source
kBufferName, // name
ast::StorageClass::kUniform, // storage_class
struct_type, // type
false, // is_const
nullptr, // constructor
ast::VariableDecorationList{
mod->create<ast::BindingDecoration>(binding_, Source{}),
mod->create<ast::SetDecoration>(set_, Source{}),
}); // decorations
mod->AddGlobalVariable(idx_var);
@@ -250,16 +252,20 @@ ast::VariableDeclStatement* FirstIndexOffset::CreateFirstIndexOffset(
ast::Variable* buffer_var,
ast::Module* mod) {
auto* buffer = mod->create<ast::IdentifierExpression>(buffer_var->name());
auto* var = mod->create<ast::Variable>(Source{}, original_name,
ast::StorageClass::kNone,
mod->create<ast::type::U32>());
var->set_is_const(true);
var->set_constructor(mod->create<ast::BinaryExpression>(
auto* constructor = mod->create<ast::BinaryExpression>(
ast::BinaryOp::kAdd,
mod->create<ast::IdentifierExpression>(kIndexOffsetPrefix + var->name()),
mod->create<ast::IdentifierExpression>(kIndexOffsetPrefix +
original_name),
mod->create<ast::MemberAccessorExpression>(
buffer, mod->create<ast::IdentifierExpression>(field_name))));
buffer, mod->create<ast::IdentifierExpression>(field_name)));
auto* var =
mod->create<ast::Variable>(Source{}, // source
original_name, // name
ast::StorageClass::kNone, // storage_class
mod->create<ast::type::U32>(), // type
true, // is_const
constructor, // constructor
ast::VariableDecorationList{}); // decorations
return mod->create<ast::VariableDeclStatement>(var);
}

View File

@@ -25,7 +25,6 @@
#include "src/ast/builtin_decoration.h"
#include "src/ast/call_expression.h"
#include "src/ast/call_statement.h"
#include "src/ast/decorated_variable.h"
#include "src/ast/function.h"
#include "src/ast/identifier_expression.h"
#include "src/ast/module.h"
@@ -52,12 +51,9 @@ struct ModuleBuilder : public ast::BuilderWithModule {
protected:
void AddBuiltinInput(const std::string& name, ast::Builtin builtin) {
auto* var = Var(name, ast::StorageClass::kInput, ty.u32);
auto* dec_var = create<ast::DecoratedVariable>(var);
ast::VariableDecorationList decs;
decs.push_back(create<ast::BuiltinDecoration>(builtin, Source{}));
dec_var->set_decorations(std::move(decs));
mod->AddGlobalVariable(dec_var);
mod->AddGlobalVariable(
Var(name, ast::StorageClass::kInput, ty.u32, nullptr,
{create<ast::BuiltinDecoration>(builtin, Source{})}));
}
ast::Function* AddFunction(const std::string& name,
@@ -141,7 +137,7 @@ TEST_F(FirstIndexOffsetTest, BasicModuleVertexIndex) {
[[block]]
StructMember{[[ offset 0 ]] tint_first_vertex_index: __u32}
}
DecoratedVariable{
Variable{
Decorations{
BuiltinDecoration{vertex_idx}
}
@@ -149,7 +145,7 @@ TEST_F(FirstIndexOffsetTest, BasicModuleVertexIndex) {
in
__u32
}
DecoratedVariable{
Variable{
Decorations{
BindingDecoration{1}
SetDecoration{2}
@@ -216,7 +212,7 @@ TEST_F(FirstIndexOffsetTest, BasicModuleInstanceIndex) {
[[block]]
StructMember{[[ offset 0 ]] tint_first_instance_index: __u32}
}
DecoratedVariable{
Variable{
Decorations{
BuiltinDecoration{instance_idx}
}
@@ -224,7 +220,7 @@ TEST_F(FirstIndexOffsetTest, BasicModuleInstanceIndex) {
in
__u32
}
DecoratedVariable{
Variable{
Decorations{
BindingDecoration{1}
SetDecoration{7}
@@ -296,7 +292,7 @@ TEST_F(FirstIndexOffsetTest, BasicModuleBothIndex) {
StructMember{[[ offset 0 ]] tint_first_vertex_index: __u32}
StructMember{[[ offset 4 ]] tint_first_instance_index: __u32}
}
DecoratedVariable{
Variable{
Decorations{
BuiltinDecoration{instance_idx}
}
@@ -304,7 +300,7 @@ TEST_F(FirstIndexOffsetTest, BasicModuleBothIndex) {
in
__u32
}
DecoratedVariable{
Variable{
Decorations{
BuiltinDecoration{vertex_idx}
}
@@ -312,7 +308,7 @@ TEST_F(FirstIndexOffsetTest, BasicModuleBothIndex) {
in
__u32
}
DecoratedVariable{
Variable{
Decorations{
BindingDecoration{1}
SetDecoration{7}
@@ -376,7 +372,7 @@ TEST_F(FirstIndexOffsetTest, NestedCalls) {
[[block]]
StructMember{[[ offset 0 ]] tint_first_vertex_index: __u32}
}
DecoratedVariable{
Variable{
Decorations{
BuiltinDecoration{vertex_idx}
}
@@ -384,7 +380,7 @@ TEST_F(FirstIndexOffsetTest, NestedCalls) {
in
__u32
}
DecoratedVariable{
Variable{
Decorations{
BindingDecoration{2}
SetDecoration{2}

View File

@@ -20,7 +20,6 @@
#include "src/ast/assignment_statement.h"
#include "src/ast/binary_expression.h"
#include "src/ast/bitcast_expression.h"
#include "src/ast/decorated_variable.h"
#include "src/ast/member_accessor_expression.h"
#include "src/ast/scalar_constructor_expression.h"
#include "src/ast/stride_decoration.h"
@@ -37,6 +36,7 @@
#include "src/ast/type/vector_type.h"
#include "src/ast/type_constructor_expression.h"
#include "src/ast/uint_literal.h"
#include "src/ast/variable.h"
#include "src/ast/variable_decl_statement.h"
namespace tint {
@@ -143,13 +143,11 @@ void VertexPulling::State::FindOrInsertVertexIndexIfUsed() {
continue;
}
if (auto* decorated = v->As<ast::DecoratedVariable>()) {
for (auto* d : decorated->decorations()) {
if (auto* builtin = d->As<ast::BuiltinDecoration>()) {
if (builtin->value() == ast::Builtin::kVertexIdx) {
vertex_index_name = v->name();
return;
}
for (auto* d : v->decorations()) {
if (auto* builtin = d->As<ast::BuiltinDecoration>()) {
if (builtin->value() == ast::Builtin::kVertexIdx) {
vertex_index_name = v->name();
return;
}
}
}
@@ -158,14 +156,19 @@ void VertexPulling::State::FindOrInsertVertexIndexIfUsed() {
// We didn't find a vertex index builtin, so create one
vertex_index_name = kDefaultVertexIndexName;
auto* var = mod->create<ast::DecoratedVariable>(mod->create<ast::Variable>(
Source{}, vertex_index_name, ast::StorageClass::kInput, GetI32Type()));
auto* var =
mod->create<ast::Variable>(Source{}, // source
vertex_index_name, // name
ast::StorageClass::kInput, // storage_class
GetI32Type(), // type
false, // is_const
nullptr, // constructor
ast::VariableDecorationList{
// decorations
mod->create<ast::BuiltinDecoration>(
ast::Builtin::kVertexIdx, Source{}),
});
ast::VariableDecorationList decorations;
decorations.push_back(
mod->create<ast::BuiltinDecoration>(ast::Builtin::kVertexIdx, Source{}));
var->set_decorations(std::move(decorations));
mod->AddGlobalVariable(var);
}
@@ -187,13 +190,11 @@ void VertexPulling::State::FindOrInsertInstanceIndexIfUsed() {
continue;
}
if (auto* decorated = v->As<ast::DecoratedVariable>()) {
for (auto* d : decorated->decorations()) {
if (auto* builtin = d->As<ast::BuiltinDecoration>()) {
if (builtin->value() == ast::Builtin::kInstanceIdx) {
instance_index_name = v->name();
return;
}
for (auto* d : v->decorations()) {
if (auto* builtin = d->As<ast::BuiltinDecoration>()) {
if (builtin->value() == ast::Builtin::kInstanceIdx) {
instance_index_name = v->name();
return;
}
}
}
@@ -202,14 +203,18 @@ void VertexPulling::State::FindOrInsertInstanceIndexIfUsed() {
// We didn't find an instance index builtin, so create one
instance_index_name = kDefaultInstanceIndexName;
auto* var = mod->create<ast::DecoratedVariable>(mod->create<ast::Variable>(
Source{}, instance_index_name, ast::StorageClass::kInput, GetI32Type()));
ast::VariableDecorationList decorations;
decorations.push_back(mod->create<ast::BuiltinDecoration>(
ast::Builtin::kInstanceIdx, Source{}));
var->set_decorations(std::move(decorations));
auto* var =
mod->create<ast::Variable>(Source{}, // source
instance_index_name, // name
ast::StorageClass::kInput, // storage_class
GetI32Type(), // type
false, // is_const
nullptr, // constructor
ast::VariableDecorationList{
// decorations
mod->create<ast::BuiltinDecoration>(
ast::Builtin::kInstanceIdx, Source{}),
});
mod->AddGlobalVariable(var);
}
@@ -219,18 +224,22 @@ void VertexPulling::State::ConvertVertexInputVariablesToPrivate() {
continue;
}
if (auto* decorated = v->As<ast::DecoratedVariable>()) {
for (auto* d : decorated->decorations()) {
if (auto* l = d->As<ast::LocationDecoration>()) {
uint32_t location = l->value();
// This is where the replacement happens. Expressions use identifier
// strings instead of pointers, so we don't need to update any other
// place in the AST.
v = mod->create<ast::Variable>(
Source{}, v->name(), ast::StorageClass::kPrivate, v->type());
location_to_var[location] = v;
break;
}
for (auto* d : v->decorations()) {
if (auto* l = d->As<ast::LocationDecoration>()) {
uint32_t location = l->value();
// This is where the replacement happens. Expressions use identifier
// strings instead of pointers, so we don't need to update any other
// place in the AST.
v = mod->create<ast::Variable>(
Source{}, // source
v->name(), // name
ast::StorageClass::kPrivate, // storage_class
v->type(), // type
false, // is_const
nullptr, // constructor
ast::VariableDecorationList{}); // decorations
location_to_var[location] = v;
break;
}
}
}
@@ -263,17 +272,18 @@ void VertexPulling::State::AddVertexStorageBuffers() {
for (uint32_t i = 0; i < cfg.vertex_state.size(); ++i) {
// The decorated variable with struct type
auto* var = mod->create<ast::DecoratedVariable>(mod->create<ast::Variable>(
Source{}, GetVertexBufferName(i), ast::StorageClass::kStorageBuffer,
struct_type));
// Add decorations
ast::VariableDecorationList decorations;
decorations.push_back(mod->create<ast::BindingDecoration>(i, Source{}));
decorations.push_back(
mod->create<ast::SetDecoration>(cfg.pulling_set, Source{}));
var->set_decorations(std::move(decorations));
auto* var = mod->create<ast::Variable>(
Source{}, // source
GetVertexBufferName(i), // name
ast::StorageClass::kStorageBuffer, // storage_class
struct_type, // type
false, // is_const
nullptr, // constructor
ast::VariableDecorationList{
// decorations
mod->create<ast::BindingDecoration>(i, Source{}),
mod->create<ast::SetDecoration>(cfg.pulling_set, Source{}),
});
mod->AddGlobalVariable(var);
}
mod->AddConstructedType(struct_type);
@@ -288,9 +298,15 @@ void VertexPulling::State::AddVertexPullingPreamble(
auto* block = mod->create<ast::BlockStatement>();
// Declare the |kPullingPosVarName| variable in the shader
auto* pos_declaration = mod->create<ast::VariableDeclStatement>(
mod->create<ast::Variable>(Source{}, kPullingPosVarName,
ast::StorageClass::kFunction, GetI32Type()));
auto* pos_declaration =
mod->create<ast::VariableDeclStatement>(mod->create<ast::Variable>(
Source{}, // source
kPullingPosVarName, // name
ast::StorageClass::kFunction, // storage_class
GetI32Type(), // type
false, // is_const
nullptr, // constructor
ast::VariableDecorationList{})); // decorations
// |kPullingPosVarName| refers to the byte location of the current read. We
// declare a variable in the shader to avoid having to reuse Expression

View File

@@ -17,7 +17,6 @@
#include <utility>
#include "gtest/gtest.h"
#include "src/ast/decorated_variable.h"
#include "src/ast/function.h"
#include "src/ast/pipeline_stage.h"
#include "src/ast/stage_decoration.h"
@@ -25,6 +24,7 @@
#include "src/ast/type/f32_type.h"
#include "src/ast/type/i32_type.h"
#include "src/ast/type/void_type.h"
#include "src/ast/variable.h"
#include "src/diagnostic/formatter.h"
#include "src/transform/manager.h"
#include "src/type_determiner.h"
@@ -69,13 +69,18 @@ class VertexPullingHelper {
void AddVertexInputVariable(uint32_t location,
std::string name,
ast::type::Type* type) {
auto* var = create<ast::DecoratedVariable>(
create<ast::Variable>(Source{}, name, ast::StorageClass::kInput, type));
auto* var = create<ast::Variable>(
Source{}, // source
name, // name
ast::StorageClass::kInput, // storage_class
type, // type
false, // is_const
nullptr, // constructor
ast::VariableDecorationList{
// decorations
create<ast::LocationDecoration>(location, Source{}),
});
ast::VariableDecorationList decorations;
decorations.push_back(create<ast::LocationDecoration>(location, Source{}));
var->set_decorations(decorations);
mod_->AddGlobalVariable(var);
}
@@ -171,7 +176,7 @@ TEST_F(VertexPullingTest, OneAttribute) {
private
__f32
}
DecoratedVariable{
Variable{
Decorations{
BuiltinDecoration{vertex_idx}
}
@@ -179,7 +184,7 @@ TEST_F(VertexPullingTest, OneAttribute) {
in
__i32
}
DecoratedVariable{
Variable{
Decorations{
BindingDecoration{0}
SetDecoration{4}
@@ -257,7 +262,7 @@ TEST_F(VertexPullingTest, OneInstancedAttribute) {
private
__f32
}
DecoratedVariable{
Variable{
Decorations{
BuiltinDecoration{instance_idx}
}
@@ -265,7 +270,7 @@ TEST_F(VertexPullingTest, OneInstancedAttribute) {
in
__i32
}
DecoratedVariable{
Variable{
Decorations{
BindingDecoration{0}
SetDecoration{4}
@@ -343,7 +348,7 @@ TEST_F(VertexPullingTest, OneAttributeDifferentOutputSet) {
private
__f32
}
DecoratedVariable{
Variable{
Decorations{
BuiltinDecoration{vertex_idx}
}
@@ -351,7 +356,7 @@ TEST_F(VertexPullingTest, OneAttributeDifferentOutputSet) {
in
__i32
}
DecoratedVariable{
Variable{
Decorations{
BindingDecoration{0}
SetDecoration{5}
@@ -416,31 +421,30 @@ TEST_F(VertexPullingTest, ExistingVertexIndexAndInstanceIndex) {
AddVertexInputVariable(1, "var_b", &f32);
ast::type::I32 i32;
{
auto* vertex_index_var =
create<ast::DecoratedVariable>(create<ast::Variable>(
Source{}, "custom_vertex_index", ast::StorageClass::kInput, &i32));
ast::VariableDecorationList decorations;
decorations.push_back(
create<ast::BuiltinDecoration>(ast::Builtin::kVertexIdx, Source{}));
mod()->AddGlobalVariable(create<ast::Variable>(
Source{}, // source
"custom_vertex_index", // name
ast::StorageClass::kInput, // storage_class
&i32, // type
false, // is_const
nullptr, // constructor
ast::VariableDecorationList{
// decorations
create<ast::BuiltinDecoration>(ast::Builtin::kVertexIdx, Source{}),
}));
vertex_index_var->set_decorations(decorations);
mod()->AddGlobalVariable(vertex_index_var);
}
{
auto* instance_index_var = create<ast::DecoratedVariable>(
create<ast::Variable>(Source{}, "custom_instance_index",
ast::StorageClass::kInput, &i32));
ast::VariableDecorationList decorations;
decorations.push_back(
create<ast::BuiltinDecoration>(ast::Builtin::kInstanceIdx, Source{}));
instance_index_var->set_decorations(decorations);
mod()->AddGlobalVariable(instance_index_var);
}
mod()->AddGlobalVariable(create<ast::Variable>(
Source{}, // source
"custom_instance_index", // name
ast::StorageClass::kInput, // storage_class
&i32, // type
false, // is_const
nullptr, // constructor
ast::VariableDecorationList{
// decorations
create<ast::BuiltinDecoration>(ast::Builtin::kInstanceIdx, Source{}),
}));
InitTransform(
{{{4, InputStepMode::kVertex, {{VertexFormat::kF32, 0, 0}}},
@@ -464,7 +468,7 @@ TEST_F(VertexPullingTest, ExistingVertexIndexAndInstanceIndex) {
private
__f32
}
DecoratedVariable{
Variable{
Decorations{
BuiltinDecoration{vertex_idx}
}
@@ -472,7 +476,7 @@ TEST_F(VertexPullingTest, ExistingVertexIndexAndInstanceIndex) {
in
__i32
}
DecoratedVariable{
Variable{
Decorations{
BuiltinDecoration{instance_idx}
}
@@ -480,7 +484,7 @@ TEST_F(VertexPullingTest, ExistingVertexIndexAndInstanceIndex) {
in
__i32
}
DecoratedVariable{
Variable{
Decorations{
BindingDecoration{0}
SetDecoration{4}
@@ -489,7 +493,7 @@ TEST_F(VertexPullingTest, ExistingVertexIndexAndInstanceIndex) {
storage_buffer
__struct_TintVertexData
}
DecoratedVariable{
Variable{
Decorations{
BindingDecoration{1}
SetDecoration{4}
@@ -605,7 +609,7 @@ TEST_F(VertexPullingTest, TwoAttributesSameBuffer) {
private
__array__f32_4
}
DecoratedVariable{
Variable{
Decorations{
BuiltinDecoration{vertex_idx}
}
@@ -613,7 +617,7 @@ TEST_F(VertexPullingTest, TwoAttributesSameBuffer) {
in
__i32
}
DecoratedVariable{
Variable{
Decorations{
BindingDecoration{0}
SetDecoration{4}
@@ -796,7 +800,7 @@ TEST_F(VertexPullingTest, FloatVectorAttributes) {
private
__array__f32_4
}
DecoratedVariable{
Variable{
Decorations{
BuiltinDecoration{vertex_idx}
}
@@ -804,7 +808,7 @@ TEST_F(VertexPullingTest, FloatVectorAttributes) {
in
__i32
}
DecoratedVariable{
Variable{
Decorations{
BindingDecoration{0}
SetDecoration{4}
@@ -813,7 +817,7 @@ TEST_F(VertexPullingTest, FloatVectorAttributes) {
storage_buffer
__struct_TintVertexData
}
DecoratedVariable{
Variable{
Decorations{
BindingDecoration{1}
SetDecoration{4}
@@ -822,7 +826,7 @@ TEST_F(VertexPullingTest, FloatVectorAttributes) {
storage_buffer
__struct_TintVertexData
}
DecoratedVariable{
Variable{
Decorations{
BindingDecoration{2}
SetDecoration{4}