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 <cassert>
|
|
|
|
#include <utility>
|
|
|
|
|
|
|
|
#include "src/ast/array_accessor_expression.h"
|
|
|
|
#include "src/ast/assignment_statement.h"
|
|
|
|
#include "src/ast/binary_expression.h"
|
|
|
|
#include "src/ast/bitcast_expression.h"
|
|
|
|
#include "src/ast/break_statement.h"
|
|
|
|
#include "src/ast/builtin_decoration.h"
|
|
|
|
#include "src/ast/call_statement.h"
|
|
|
|
#include "src/ast/case_statement.h"
|
|
|
|
#include "src/ast/constructor_expression.h"
|
|
|
|
#include "src/ast/else_statement.h"
|
|
|
|
#include "src/ast/expression.h"
|
|
|
|
#include "src/ast/fallthrough_statement.h"
|
|
|
|
#include "src/ast/identifier_expression.h"
|
|
|
|
#include "src/ast/if_statement.h"
|
|
|
|
#include "src/ast/loop_statement.h"
|
|
|
|
#include "src/ast/member_accessor_expression.h"
|
|
|
|
#include "src/ast/return_statement.h"
|
|
|
|
#include "src/ast/scalar_constructor_expression.h"
|
|
|
|
#include "src/ast/struct.h"
|
|
|
|
#include "src/ast/struct_block_decoration.h"
|
|
|
|
#include "src/ast/struct_decoration.h"
|
|
|
|
#include "src/ast/struct_member.h"
|
|
|
|
#include "src/ast/struct_member_offset_decoration.h"
|
|
|
|
#include "src/ast/switch_statement.h"
|
|
|
|
#include "src/ast/type_constructor_expression.h"
|
|
|
|
#include "src/ast/unary_op_expression.h"
|
|
|
|
#include "src/ast/variable.h"
|
|
|
|
#include "src/ast/variable_decl_statement.h"
|
|
|
|
#include "src/ast/variable_decoration.h"
|
2021-01-21 16:20:40 +00:00
|
|
|
#include "src/clone_context.h"
|
2021-01-21 15:42:10 +00:00
|
|
|
#include "src/type/struct_type.h"
|
|
|
|
#include "src/type/u32_type.h"
|
2020-12-08 21:07:24 +00:00
|
|
|
#include "src/type_determiner.h"
|
|
|
|
|
|
|
|
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_";
|
|
|
|
|
2021-01-21 16:20:40 +00:00
|
|
|
ast::Variable* clone_variable_with_new_name(CloneContext* ctx,
|
2020-12-11 13:07:02 +00:00
|
|
|
ast::Variable* in,
|
|
|
|
std::string new_name) {
|
2021-01-22 13:41:06 +00:00
|
|
|
return ctx->dst->create<ast::Variable>(
|
2020-12-16 21:42:30 +00:00
|
|
|
ctx->Clone(in->source()), // source
|
2021-01-22 13:41:06 +00:00
|
|
|
ctx->dst->RegisterSymbol(new_name), // symbol
|
2020-12-16 21:42:30 +00:00
|
|
|
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
|
2020-12-10 17:47:41 +00:00
|
|
|
}
|
|
|
|
|
2020-12-08 21:07:24 +00:00
|
|
|
} // namespace
|
|
|
|
|
2021-01-15 12:22:16 +00:00
|
|
|
FirstIndexOffset::FirstIndexOffset(uint32_t binding, uint32_t group)
|
|
|
|
: binding_(binding), group_(group) {}
|
2020-12-08 21:07:24 +00:00
|
|
|
|
|
|
|
FirstIndexOffset::~FirstIndexOffset() = default;
|
|
|
|
|
2021-01-26 16:57:10 +00:00
|
|
|
Transform::Output FirstIndexOffset::Run(const Program* in) {
|
2020-12-10 17:47:41 +00:00
|
|
|
// First do a quick check to see if the transform has already been applied.
|
2021-01-26 16:57:10 +00:00
|
|
|
for (ast::Variable* var : in->AST().GlobalVariables()) {
|
2020-12-11 13:07:02 +00:00
|
|
|
if (auto* dec_var = var->As<ast::Variable>()) {
|
2021-01-26 16:57:10 +00:00
|
|
|
if (dec_var->symbol() == in->GetSymbol(kBufferName)) {
|
2020-12-08 21:07:24 +00:00
|
|
|
diag::Diagnostic err;
|
|
|
|
err.message = "First index offset transform has already been applied.";
|
|
|
|
err.severity = diag::Severity::Error;
|
2020-12-10 17:47:41 +00:00
|
|
|
Output out;
|
2020-12-08 21:07:24 +00:00
|
|
|
out.diagnostics.add(std::move(err));
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-10 17:47:41 +00:00
|
|
|
// Running TypeDeterminer as we require local_referenced_builtin_variables()
|
|
|
|
// to be populated. TODO(bclayton) - it should not be necessary to re-run the
|
|
|
|
// type determiner if semantic information is already generated. Remove.
|
2021-01-26 16:57:10 +00:00
|
|
|
// TODO(https://crbug.com/tint/390): Remove this const_cast hack!
|
|
|
|
TypeDeterminer td(const_cast<Program*>(in));
|
2020-12-10 17:47:41 +00:00
|
|
|
if (!td.Determine()) {
|
|
|
|
diag::Diagnostic err;
|
|
|
|
err.severity = diag::Severity::Error;
|
|
|
|
err.message = td.error();
|
|
|
|
Output out;
|
|
|
|
out.diagnostics.add(std::move(err));
|
2020-12-08 21:07:24 +00:00
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2021-01-11 16:24:32 +00:00
|
|
|
Symbol vertex_index_sym;
|
|
|
|
Symbol instance_index_sym;
|
2020-12-08 21:07:24 +00:00
|
|
|
|
2020-12-10 17:47:41 +00:00
|
|
|
// Lazilly construct the UniformBuffer on first call to
|
|
|
|
// maybe_create_buffer_var()
|
|
|
|
ast::Variable* buffer_var = nullptr;
|
2021-01-26 16:57:10 +00:00
|
|
|
auto maybe_create_buffer_var = [&](Program* program) {
|
2020-12-10 17:47:41 +00:00
|
|
|
if (buffer_var == nullptr) {
|
2021-01-26 16:57:10 +00:00
|
|
|
buffer_var = AddUniformBuffer(program);
|
2020-12-10 17:47:41 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-01-18 15:51:13 +00:00
|
|
|
// Clone the AST, renaming the kVertexIndex and kInstanceIndex builtins, and
|
|
|
|
// add a CreateFirstIndexOffset() statement to each function that uses one of
|
2020-12-10 17:47:41 +00:00
|
|
|
// these builtins.
|
2020-12-14 20:31:17 +00:00
|
|
|
|
|
|
|
Output out;
|
2021-01-26 16:57:10 +00:00
|
|
|
CloneContext(&out.program, in)
|
2021-01-21 16:20:40 +00:00
|
|
|
.ReplaceAll([&](CloneContext* ctx, 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();
|
|
|
|
if (blt_type == ast::Builtin::kVertexIndex) {
|
|
|
|
vertex_index_sym = var->symbol();
|
|
|
|
has_vertex_index_ = true;
|
|
|
|
return clone_variable_with_new_name(
|
|
|
|
ctx, var,
|
|
|
|
kIndexOffsetPrefix + in->SymbolToName(var->symbol()));
|
|
|
|
} else if (blt_type == ast::Builtin::kInstanceIndex) {
|
|
|
|
instance_index_sym = var->symbol();
|
|
|
|
has_instance_index_ = true;
|
|
|
|
return clone_variable_with_new_name(
|
|
|
|
ctx, var,
|
|
|
|
kIndexOffsetPrefix + in->SymbolToName(var->symbol()));
|
2020-12-15 12:32:18 +00:00
|
|
|
}
|
2021-01-21 16:20:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nullptr; // Just clone var
|
|
|
|
})
|
2020-12-15 12:32:18 +00:00
|
|
|
.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.
|
2021-01-21 16:20:40 +00:00
|
|
|
[&](CloneContext* ctx, ast::Function* func) -> ast::Function* {
|
2021-01-22 13:41:06 +00:00
|
|
|
maybe_create_buffer_var(ctx->dst);
|
2020-12-15 12:32:18 +00:00
|
|
|
if (buffer_var == nullptr) {
|
|
|
|
return nullptr; // no transform need, just clone func
|
|
|
|
}
|
|
|
|
ast::StatementList statements;
|
|
|
|
for (const auto& data :
|
|
|
|
func->local_referenced_builtin_variables()) {
|
2021-01-18 15:51:13 +00:00
|
|
|
if (data.second->value() == ast::Builtin::kVertexIndex) {
|
2020-12-15 12:32:18 +00:00
|
|
|
statements.emplace_back(CreateFirstIndexOffset(
|
2021-01-11 16:24:32 +00:00
|
|
|
in->SymbolToName(vertex_index_sym), kFirstVertexName,
|
2021-01-22 13:41:06 +00:00
|
|
|
buffer_var, ctx->dst));
|
2021-01-18 15:51:13 +00:00
|
|
|
} else if (data.second->value() == ast::Builtin::kInstanceIndex) {
|
2020-12-15 12:32:18 +00:00
|
|
|
statements.emplace_back(CreateFirstIndexOffset(
|
2021-01-11 16:24:32 +00:00
|
|
|
in->SymbolToName(instance_index_sym), kFirstInstanceName,
|
2021-01-22 13:41:06 +00:00
|
|
|
buffer_var, ctx->dst));
|
2020-12-15 12:32:18 +00:00
|
|
|
}
|
2020-12-14 20:31:17 +00:00
|
|
|
}
|
2020-12-15 12:32:18 +00:00
|
|
|
return CloneWithStatementsAtStart(ctx, func, statements);
|
|
|
|
})
|
|
|
|
.Clone();
|
2020-12-14 20:31:17 +00:00
|
|
|
|
2020-12-08 21:07:24 +00:00
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FirstIndexOffset::HasVertexIndex() {
|
|
|
|
return has_vertex_index_;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FirstIndexOffset::HasInstanceIndex() {
|
|
|
|
return has_instance_index_;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t FirstIndexOffset::GetFirstVertexOffset() {
|
|
|
|
assert(has_vertex_index_);
|
|
|
|
return vertex_index_offset_;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t FirstIndexOffset::GetFirstInstanceOffset() {
|
|
|
|
assert(has_instance_index_);
|
|
|
|
return instance_index_offset_;
|
|
|
|
}
|
|
|
|
|
2021-01-26 16:57:10 +00:00
|
|
|
ast::Variable* FirstIndexOffset::AddUniformBuffer(Program* dst) {
|
|
|
|
auto* u32_type = dst->create<type::U32>();
|
2020-12-08 21:07:24 +00:00
|
|
|
ast::StructMemberList members;
|
|
|
|
uint32_t offset = 0;
|
|
|
|
if (has_vertex_index_) {
|
|
|
|
ast::StructMemberDecorationList member_dec;
|
|
|
|
member_dec.push_back(
|
2021-01-26 16:57:10 +00:00
|
|
|
dst->create<ast::StructMemberOffsetDecoration>(Source{}, offset));
|
|
|
|
members.push_back(dst->create<ast::StructMember>(
|
|
|
|
Source{}, dst->RegisterSymbol(kFirstVertexName), u32_type,
|
2021-01-11 16:24:32 +00:00
|
|
|
std::move(member_dec)));
|
2020-12-08 21:07:24 +00:00
|
|
|
vertex_index_offset_ = offset;
|
|
|
|
offset += 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (has_instance_index_) {
|
|
|
|
ast::StructMemberDecorationList member_dec;
|
|
|
|
member_dec.push_back(
|
2021-01-26 16:57:10 +00:00
|
|
|
dst->create<ast::StructMemberOffsetDecoration>(Source{}, offset));
|
|
|
|
members.push_back(dst->create<ast::StructMember>(
|
|
|
|
Source{}, dst->RegisterSymbol(kFirstInstanceName), u32_type,
|
2021-01-11 16:24:32 +00:00
|
|
|
std::move(member_dec)));
|
2020-12-08 21:07:24 +00:00
|
|
|
instance_index_offset_ = offset;
|
|
|
|
offset += 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
ast::StructDecorationList decos;
|
2021-01-26 16:57:10 +00:00
|
|
|
decos.push_back(dst->create<ast::StructBlockDecoration>(Source{}));
|
2020-12-08 21:07:24 +00:00
|
|
|
|
2021-01-26 16:57:10 +00:00
|
|
|
auto* struct_type = dst->create<type::Struct>(
|
|
|
|
dst->RegisterSymbol(kStructName),
|
|
|
|
dst->create<ast::Struct>(Source{}, std::move(members), std::move(decos)));
|
2020-12-08 21:07:24 +00:00
|
|
|
|
2021-01-26 16:57:10 +00:00
|
|
|
auto* idx_var = dst->create<ast::Variable>(
|
2020-12-16 21:42:30 +00:00
|
|
|
Source{}, // source
|
2021-01-26 16:57:10 +00:00
|
|
|
dst->RegisterSymbol(kBufferName), // symbol
|
2020-12-16 21:42:30 +00:00
|
|
|
ast::StorageClass::kUniform, // storage_class
|
|
|
|
struct_type, // type
|
|
|
|
false, // is_const
|
|
|
|
nullptr, // constructor
|
2020-12-11 13:07:02 +00:00
|
|
|
ast::VariableDecorationList{
|
2021-01-26 16:57:10 +00:00
|
|
|
dst->create<ast::BindingDecoration>(Source{}, binding_),
|
|
|
|
dst->create<ast::GroupDecoration>(Source{}, group_),
|
2020-12-11 13:07:02 +00:00
|
|
|
}); // decorations
|
2020-12-08 21:07:24 +00:00
|
|
|
|
2021-01-26 16:57:10 +00:00
|
|
|
dst->AST().AddGlobalVariable(idx_var);
|
2020-12-08 21:07:24 +00:00
|
|
|
|
2021-01-26 16:57:10 +00:00
|
|
|
dst->AddConstructedType(struct_type);
|
2020-12-08 21:07:24 +00:00
|
|
|
|
|
|
|
return idx_var;
|
|
|
|
}
|
|
|
|
|
2020-12-10 17:47:41 +00:00
|
|
|
ast::VariableDeclStatement* FirstIndexOffset::CreateFirstIndexOffset(
|
|
|
|
const std::string& original_name,
|
|
|
|
const std::string& field_name,
|
|
|
|
ast::Variable* buffer_var,
|
2021-01-26 16:57:10 +00:00
|
|
|
Program* dst) {
|
2021-01-11 16:24:32 +00:00
|
|
|
auto* buffer =
|
2021-01-26 16:57:10 +00:00
|
|
|
dst->create<ast::IdentifierExpression>(Source{}, buffer_var->symbol());
|
2021-01-11 16:24:32 +00:00
|
|
|
|
|
|
|
auto lhs_name = kIndexOffsetPrefix + original_name;
|
2021-01-26 16:57:10 +00:00
|
|
|
auto* constructor = dst->create<ast::BinaryExpression>(
|
2020-12-12 01:38:13 +00:00
|
|
|
Source{}, ast::BinaryOp::kAdd,
|
2021-01-26 16:57:10 +00:00
|
|
|
dst->create<ast::IdentifierExpression>(Source{},
|
|
|
|
dst->RegisterSymbol(lhs_name)),
|
|
|
|
dst->create<ast::MemberAccessorExpression>(
|
2020-12-12 01:38:13 +00:00
|
|
|
Source{}, buffer,
|
2021-01-26 16:57:10 +00:00
|
|
|
dst->create<ast::IdentifierExpression>(
|
|
|
|
Source{}, dst->RegisterSymbol(field_name))));
|
2020-12-11 13:07:02 +00:00
|
|
|
auto* var =
|
2021-01-26 16:57:10 +00:00
|
|
|
dst->create<ast::Variable>(Source{}, // source
|
|
|
|
dst->RegisterSymbol(original_name), // symbol
|
2020-12-11 13:07:02 +00:00
|
|
|
ast::StorageClass::kNone, // storage_class
|
2021-01-26 16:57:10 +00:00
|
|
|
dst->create<type::U32>(), // type
|
2021-01-21 15:42:10 +00:00
|
|
|
true, // is_const
|
|
|
|
constructor, // constructor
|
2020-12-11 13:07:02 +00:00
|
|
|
ast::VariableDecorationList{}); // decorations
|
2021-01-26 16:57:10 +00:00
|
|
|
return dst->create<ast::VariableDeclStatement>(Source{}, var);
|
2020-12-08 21:07:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace transform
|
|
|
|
} // namespace tint
|