Add EmitVertexPointSizeTransform

EmitVertexPointSizeTransform is a Transformer that adds a PointSize builtin global output variable to the module which is assigned 1.0 as the new first statement for all vertex stage entry points.

If the module does not contain a vertex pipeline stage entry point then then this transformer is a no-op.

Bug: tint:321
Change-Id: I0e01236339d9fa1ceab3622af0931a1199c33b99
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/34561
Commit-Queue: Ben Clayton <bclayton@google.com>
Reviewed-by: dan sinclair <dsinclair@chromium.org>
This commit is contained in:
Ben Clayton
2020-12-03 14:54:09 +00:00
committed by Commit Bot service account
parent 685cb02ea8
commit 76d12f0f5a
12 changed files with 344 additions and 1 deletions

View File

@@ -0,0 +1,78 @@
// 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/emit_vertex_point_size_transform.h"
#include <memory>
#include <utility>
#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"
namespace tint {
namespace transform {
namespace {
const char kPointSizeVar[] = "tint_pointsize";
} // namespace
EmitVertexPointSizeTransform::EmitVertexPointSizeTransform(ast::Module* mod)
: Transformer(mod) {}
EmitVertexPointSizeTransform::~EmitVertexPointSizeTransform() = default;
bool EmitVertexPointSizeTransform::Run() {
if (!mod_->HasStage(ast::PipelineStage::kVertex)) {
// If the module doesn't have any vertex stages, then there's nothing to do.
return true;
}
auto* f32 = mod_->create<ast::type::F32>();
// Declare the pointsize builtin output variable.
auto* pointsize_var =
mod_->create<ast::DecoratedVariable>(mod_->create<ast::Variable>(
kPointSizeVar, ast::StorageClass::kOutput, f32));
pointsize_var->set_decorations({
mod_->create<ast::BuiltinDecoration>(ast::Builtin::kPointSize, Source{}),
});
mod_->AddGlobalVariable(pointsize_var);
// Build the AST expression & statement for assigning pointsize one.
auto* one = mod_->create<ast::ScalarConstructorExpression>(
mod_->create<ast::FloatLiteral>(f32, 1.0f));
auto* pointsize_ident =
mod_->create<ast::IdentifierExpression>(Source{}, kPointSizeVar);
auto* pointsize_assign =
mod_->create<ast::AssignmentStatement>(pointsize_ident, one);
// Add the pointsize assignment statement to the front of all vertex stages.
for (auto* func : mod_->functions()) {
if (func->pipeline_stage() == ast::PipelineStage::kVertex) {
func->body()->insert(0, pointsize_assign);
}
}
return true;
}
} // namespace transform
} // namespace tint

View File

@@ -0,0 +1,45 @@
// 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.
#ifndef SRC_TRANSFORM_EMIT_VERTEX_POINT_SIZE_TRANSFORM_H_
#define SRC_TRANSFORM_EMIT_VERTEX_POINT_SIZE_TRANSFORM_H_
#include "src/transform/transformer.h"
namespace tint {
namespace transform {
/// EmitVertexPointSizeTransform is a Transformer that adds a PointSize builtin
/// global output variable to the module which is assigned 1.0 as the new first
/// statement for all vertex stage entry points.
/// If the module does not contain a vertex pipeline stage entry point then then
/// this transformer is a no-op.
class EmitVertexPointSizeTransform : public Transformer {
public:
/// Constructor
/// @param mod the module transform
explicit EmitVertexPointSizeTransform(ast::Module* mod);
~EmitVertexPointSizeTransform() override;
/// Users of Tint should register the transform with transform manager and
/// invoke its Run(), instead of directly calling the transform's Run().
/// Calling Run() directly does not perform module state cleanup operations.
/// @returns true if the transformation was successful
bool Run() override;
};
} // namespace transform
} // namespace tint
#endif // SRC_TRANSFORM_EMIT_VERTEX_POINT_SIZE_TRANSFORM_H_

View File

@@ -0,0 +1,186 @@
// 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/emit_vertex_point_size_transform.h"
#include <memory>
#include <utility>
#include "gtest/gtest.h"
#include "src/ast/builder.h"
#include "src/ast/call_statement.h"
#include "src/ast/stage_decoration.h"
#include "src/transform/manager.h"
namespace tint {
namespace transform {
namespace {
class EmitVertexPointSizeTransformTest : public testing::Test,
public ast::BuilderWithModule {
public:
EmitVertexPointSizeTransformTest() {
auto transform = std::make_unique<EmitVertexPointSizeTransform>(mod);
manager = std::make_unique<Manager>();
manager->append(std::move(transform));
}
std::unique_ptr<Manager> manager;
};
TEST_F(EmitVertexPointSizeTransformTest, VertexStageBasic) {
auto* block = create<ast::BlockStatement>(Source{});
block->append(create<ast::CallStatement>(create<ast::CallExpression>(
Source{},
create<ast::IdentifierExpression>(
Source{}, "builtin_assignments_should_happen_before_this"),
ast::ExpressionList{})));
mod->AddFunction(create<ast::Function>(
"non_entry_a", ast::VariableList{}, create<ast::type::Void>(),
create<ast::BlockStatement>(Source{})));
auto* entry = create<ast::Function>("entry", ast::VariableList{},
create<ast::type::Void>(), block);
entry->set_decorations(
{create<ast::StageDecoration>(ast::PipelineStage::kVertex, Source{})});
mod->AddFunction(entry);
mod->AddFunction(create<ast::Function>(
"non_entry_b", ast::VariableList{}, create<ast::type::Void>(),
create<ast::BlockStatement>(Source{})));
manager->Run(mod);
auto* expected = R"(Module{
DecoratedVariable{
Decorations{
BuiltinDecoration{pointsize}
}
tint_pointsize
out
__f32
}
Function non_entry_a -> __void
()
{
}
Function entry -> __void
StageDecoration{vertex}
()
{
Assignment{
Identifier[__ptr_out__f32]{tint_pointsize}
ScalarConstructor[__f32]{1.000000}
}
Call[not set]{
Identifier[not set]{builtin_assignments_should_happen_before_this}
(
)
}
}
Function non_entry_b -> __void
()
{
}
}
)";
EXPECT_EQ(expected, mod->to_str());
}
TEST_F(EmitVertexPointSizeTransformTest, VertexStageEmpty) {
mod->AddFunction(create<ast::Function>(
"non_entry_a", ast::VariableList{}, create<ast::type::Void>(),
create<ast::BlockStatement>(Source{})));
auto* entry = create<ast::Function>("entry", ast::VariableList{},
create<ast::type::Void>(),
create<ast::BlockStatement>(Source{}));
entry->set_decorations(
{create<ast::StageDecoration>(ast::PipelineStage::kVertex, Source{})});
mod->AddFunction(entry);
mod->AddFunction(create<ast::Function>(
"non_entry_b", ast::VariableList{}, create<ast::type::Void>(),
create<ast::BlockStatement>(Source{})));
manager->Run(mod);
auto* expected = R"(Module{
DecoratedVariable{
Decorations{
BuiltinDecoration{pointsize}
}
tint_pointsize
out
__f32
}
Function non_entry_a -> __void
()
{
}
Function entry -> __void
StageDecoration{vertex}
()
{
Assignment{
Identifier[__ptr_out__f32]{tint_pointsize}
ScalarConstructor[__f32]{1.000000}
}
}
Function non_entry_b -> __void
()
{
}
}
)";
EXPECT_EQ(expected, mod->to_str());
}
TEST_F(EmitVertexPointSizeTransformTest, NonVertexStage) {
auto* fragment_entry = create<ast::Function>(
"fragment_entry", ast::VariableList{}, create<ast::type::Void>(),
create<ast::BlockStatement>(Source{}));
fragment_entry->set_decorations(
{create<ast::StageDecoration>(ast::PipelineStage::kFragment, Source{})});
mod->AddFunction(fragment_entry);
auto* compute_entry = create<ast::Function>(
"compute_entry", ast::VariableList{}, create<ast::type::Void>(),
create<ast::BlockStatement>(Source{}));
compute_entry->set_decorations(
{create<ast::StageDecoration>(ast::PipelineStage::kCompute, Source{})});
mod->AddFunction(compute_entry);
manager->Run(mod);
auto* expected = R"(Module{
Function fragment_entry -> __void
StageDecoration{fragment}
()
{
}
Function compute_entry -> __void
StageDecoration{compute}
()
{
}
}
)";
EXPECT_EQ(expected, mod->to_str());
}
} // namespace
} // namespace transform
} // namespace tint