mirror of
https://github.com/encounter/dawn-cmake.git
synced 2025-12-09 21:47:47 +00:00
[spirv-writer] Add preliminary support for GLSL methods
This CL adds the type determination and builder code to support outputting the GLSL Round call. Bug: tint:5 Change-Id: I84dadebaf19aee3361fb13b5f32ce1a9f1b0c421 Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/19923 Reviewed-by: David Neto <dneto@google.com>
This commit is contained in:
committed by
dan sinclair
parent
d4d87edc85
commit
fd5d4ca16c
@@ -22,6 +22,7 @@
|
||||
#include "src/ast/binding_decoration.h"
|
||||
#include "src/ast/bool_literal.h"
|
||||
#include "src/ast/builtin_decoration.h"
|
||||
#include "src/ast/call_expression.h"
|
||||
#include "src/ast/constructor_expression.h"
|
||||
#include "src/ast/decorated_variable.h"
|
||||
#include "src/ast/else_statement.h"
|
||||
@@ -430,10 +431,18 @@ bool Builder::GenerateGlobalVariable(ast::Variable* var) {
|
||||
|
||||
uint32_t Builder::GenerateIdentifierExpression(
|
||||
ast::IdentifierExpression* expr) {
|
||||
// TODO(dsinclair): handle names with namespaces in them ...
|
||||
|
||||
uint32_t val = 0;
|
||||
if (!scope_stack_.get(expr->name(), &val)) {
|
||||
if (expr->has_path()) {
|
||||
auto* imp = mod_->FindImportByName(expr->path());
|
||||
if (imp == nullptr) {
|
||||
error_ = "unable to find import for " + expr->path();
|
||||
return 0;
|
||||
}
|
||||
val = imp->GetIdForMethod(expr->name());
|
||||
if (val == 0) {
|
||||
error_ = "unable to lookup: " + expr->name() + " in " + expr->path();
|
||||
}
|
||||
} else if (!scope_stack_.get(expr->name(), &val)) {
|
||||
error_ = "unable to find name for identifier: " + expr->name();
|
||||
return 0;
|
||||
}
|
||||
@@ -691,6 +700,59 @@ uint32_t Builder::GenerateBinaryExpression(ast::BinaryExpression* expr) {
|
||||
return result_id;
|
||||
}
|
||||
|
||||
uint32_t Builder::GenerateCallExpression(ast::CallExpression* expr) {
|
||||
// TODO(dsinclair): Support regular function calls
|
||||
if (!expr->func()->IsIdentifier() ||
|
||||
!expr->func()->AsIdentifier()->has_path()) {
|
||||
error_ = "function calls not supported yet.";
|
||||
return 0;
|
||||
}
|
||||
|
||||
auto* ident = expr->func()->AsIdentifier();
|
||||
|
||||
auto type_id = GenerateTypeIfNeeded(expr->func()->result_type());
|
||||
if (type_id == 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
auto set_iter = import_name_to_id_.find(ident->path());
|
||||
if (set_iter == import_name_to_id_.end()) {
|
||||
error_ = "unknown import " + ident->path();
|
||||
return 0;
|
||||
}
|
||||
auto set_id = set_iter->second;
|
||||
|
||||
auto* imp = mod_->FindImportByName(ident->path());
|
||||
if (imp == nullptr) {
|
||||
error_ = "unknown import " + ident->path();
|
||||
return 0;
|
||||
}
|
||||
|
||||
auto inst_id = imp->GetIdForMethod(ident->name());
|
||||
if (inst_id == 0) {
|
||||
error_ = "unknown method " + ident->name();
|
||||
return 0;
|
||||
}
|
||||
|
||||
auto result = result_op();
|
||||
auto result_id = result.to_i();
|
||||
|
||||
std::vector<Operand> ops{Operand::Int(type_id), result, Operand::Int(set_id),
|
||||
Operand::Int(inst_id)};
|
||||
|
||||
for (const auto& param : expr->params()) {
|
||||
auto id = GenerateExpression(param.get());
|
||||
if (id == 0) {
|
||||
return 0;
|
||||
}
|
||||
ops.push_back(Operand::Int(id));
|
||||
}
|
||||
|
||||
push_function_inst(spv::Op::OpExtInst, std::move(ops));
|
||||
|
||||
return result_id;
|
||||
}
|
||||
|
||||
bool Builder::GenerateConditionalBlock(
|
||||
ast::Expression* cond,
|
||||
const ast::StatementList& true_body,
|
||||
|
||||
@@ -202,6 +202,10 @@ class Builder {
|
||||
/// @param expr the expression to generate
|
||||
/// @returns the expression ID on success or 0 otherwise
|
||||
uint32_t GenerateBinaryExpression(ast::BinaryExpression* expr);
|
||||
/// Generates a call expression
|
||||
/// @param expr the expression to generate
|
||||
/// @returns the expression ID on success or 0 otherwise
|
||||
uint32_t GenerateCallExpression(ast::CallExpression* expr);
|
||||
/// Generates a loop statement
|
||||
/// @param stmt the statement to generate
|
||||
/// @returns true on successful generation
|
||||
|
||||
81
src/writer/spirv/builder_call_test.cc
Normal file
81
src/writer/spirv/builder_call_test.cc
Normal file
@@ -0,0 +1,81 @@
|
||||
// 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 <memory>
|
||||
|
||||
#include "gtest/gtest.h"
|
||||
#include "src/ast/call_expression.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/void_type.h"
|
||||
#include "src/context.h"
|
||||
#include "src/type_determiner.h"
|
||||
#include "src/writer/spirv/builder.h"
|
||||
#include "src/writer/spirv/spv_dump.h"
|
||||
|
||||
namespace tint {
|
||||
namespace writer {
|
||||
namespace spirv {
|
||||
namespace {
|
||||
|
||||
using BuilderTest = testing::Test;
|
||||
|
||||
TEST_F(BuilderTest, Call_GLSLMethod) {
|
||||
ast::type::F32Type f32;
|
||||
ast::type::VoidType void_type;
|
||||
|
||||
ast::ExpressionList params;
|
||||
params.push_back(std::make_unique<ast::ScalarConstructorExpression>(
|
||||
std::make_unique<ast::FloatLiteral>(&f32, 1.f)));
|
||||
|
||||
ast::CallExpression expr(std::make_unique<ast::IdentifierExpression>(
|
||||
std::vector<std::string>{"std", "round"}),
|
||||
std::move(params));
|
||||
|
||||
Context ctx;
|
||||
ast::Module mod;
|
||||
TypeDeterminer td(&ctx, &mod);
|
||||
|
||||
auto imp = std::make_unique<ast::Import>("GLSL.std.450", "std");
|
||||
auto* glsl = imp.get();
|
||||
mod.AddImport(std::move(imp));
|
||||
|
||||
ASSERT_TRUE(td.DetermineResultType(&expr)) << td.error();
|
||||
|
||||
ast::Function func("a_func", {}, &void_type);
|
||||
|
||||
Builder b(&mod);
|
||||
b.GenerateImport(glsl);
|
||||
ASSERT_TRUE(b.GenerateFunction(&func)) << b.error();
|
||||
|
||||
EXPECT_EQ(b.GenerateCallExpression(&expr), 7u) << b.error();
|
||||
EXPECT_EQ(DumpBuilder(b), R"(%1 = OpExtInstImport "GLSL.std.450"
|
||||
OpName %4 "a_func"
|
||||
%3 = OpTypeVoid
|
||||
%2 = OpTypeFunction %3
|
||||
%6 = OpTypeFloat 32
|
||||
%8 = OpConstant %6 1
|
||||
%4 = OpFunction %3 None %2
|
||||
%5 = OpLabel
|
||||
%7 = OpExtInst %6 %1 Round %8
|
||||
OpFunctionEnd
|
||||
)");
|
||||
}
|
||||
|
||||
} // namespace
|
||||
} // namespace spirv
|
||||
} // namespace writer
|
||||
} // namespace tint
|
||||
@@ -217,6 +217,31 @@ TEST_F(BuilderTest, IdentifierExpression_NoLoadConst) {
|
||||
)");
|
||||
}
|
||||
|
||||
TEST_F(BuilderTest, IdentifierExpression_ImportMethod) {
|
||||
auto imp = std::make_unique<ast::Import>("GLSL.std.450", "std");
|
||||
imp->AddMethodId("round", 42u);
|
||||
|
||||
ast::Module mod;
|
||||
mod.AddImport(std::move(imp));
|
||||
Builder b(&mod);
|
||||
|
||||
ast::IdentifierExpression expr(std::vector<std::string>({"std", "round"}));
|
||||
EXPECT_EQ(b.GenerateIdentifierExpression(&expr), 42u) << b.error();
|
||||
}
|
||||
|
||||
TEST_F(BuilderTest, IdentifierExpression_ImportMethod_NotFound) {
|
||||
auto imp = std::make_unique<ast::Import>("GLSL.std.450", "std");
|
||||
|
||||
ast::Module mod;
|
||||
mod.AddImport(std::move(imp));
|
||||
Builder b(&mod);
|
||||
|
||||
ast::IdentifierExpression expr(std::vector<std::string>({"std", "ceil"}));
|
||||
EXPECT_EQ(b.GenerateIdentifierExpression(&expr), 0u);
|
||||
ASSERT_TRUE(b.has_error());
|
||||
EXPECT_EQ(b.error(), "unable to lookup: ceil in std");
|
||||
}
|
||||
|
||||
} // namespace
|
||||
} // namespace spirv
|
||||
} // namespace writer
|
||||
|
||||
Reference in New Issue
Block a user