2020-03-02 20:47:43 +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/type_determiner.h"
|
|
|
|
|
2020-04-07 12:57:42 +00:00
|
|
|
#include <memory>
|
|
|
|
|
2020-04-20 15:46:18 +00:00
|
|
|
#include "spirv/unified1/GLSL.std.450.h"
|
2020-04-07 12:57:42 +00:00
|
|
|
#include "src/ast/array_accessor_expression.h"
|
2020-04-07 12:57:52 +00:00
|
|
|
#include "src/ast/as_expression.h"
|
2020-04-07 12:47:23 +00:00
|
|
|
#include "src/ast/assignment_statement.h"
|
2020-04-07 19:27:41 +00:00
|
|
|
#include "src/ast/binary_expression.h"
|
2020-04-07 12:54:10 +00:00
|
|
|
#include "src/ast/break_statement.h"
|
2020-04-07 16:41:10 +00:00
|
|
|
#include "src/ast/call_expression.h"
|
2020-04-07 12:54:20 +00:00
|
|
|
#include "src/ast/case_statement.h"
|
2020-04-07 16:41:23 +00:00
|
|
|
#include "src/ast/cast_expression.h"
|
2020-04-07 12:54:29 +00:00
|
|
|
#include "src/ast/continue_statement.h"
|
2020-04-07 12:54:37 +00:00
|
|
|
#include "src/ast/else_statement.h"
|
2020-04-07 12:57:27 +00:00
|
|
|
#include "src/ast/identifier_expression.h"
|
2020-04-07 12:55:25 +00:00
|
|
|
#include "src/ast/if_statement.h"
|
2020-04-07 12:55:51 +00:00
|
|
|
#include "src/ast/loop_statement.h"
|
2020-04-07 16:41:33 +00:00
|
|
|
#include "src/ast/member_accessor_expression.h"
|
2020-04-07 12:56:24 +00:00
|
|
|
#include "src/ast/return_statement.h"
|
2020-04-07 12:46:30 +00:00
|
|
|
#include "src/ast/scalar_constructor_expression.h"
|
2020-04-07 12:56:45 +00:00
|
|
|
#include "src/ast/switch_statement.h"
|
2020-04-07 12:57:42 +00:00
|
|
|
#include "src/ast/type/array_type.h"
|
2020-04-07 19:26:39 +00:00
|
|
|
#include "src/ast/type/bool_type.h"
|
2020-04-07 19:27:00 +00:00
|
|
|
#include "src/ast/type/f32_type.h"
|
2020-04-07 12:57:42 +00:00
|
|
|
#include "src/ast/type/matrix_type.h"
|
2020-04-23 22:26:52 +00:00
|
|
|
#include "src/ast/type/pointer_type.h"
|
2020-04-07 16:41:33 +00:00
|
|
|
#include "src/ast/type/struct_type.h"
|
2020-04-07 12:57:42 +00:00
|
|
|
#include "src/ast/type/vector_type.h"
|
2020-04-07 12:46:30 +00:00
|
|
|
#include "src/ast/type_constructor_expression.h"
|
2020-04-07 19:26:49 +00:00
|
|
|
#include "src/ast/unary_derivative_expression.h"
|
2020-04-07 19:27:00 +00:00
|
|
|
#include "src/ast/unary_method_expression.h"
|
2020-04-07 19:27:21 +00:00
|
|
|
#include "src/ast/unary_op_expression.h"
|
2020-04-07 12:56:57 +00:00
|
|
|
#include "src/ast/unless_statement.h"
|
2020-04-07 12:57:12 +00:00
|
|
|
#include "src/ast/variable_decl_statement.h"
|
2020-04-07 12:46:30 +00:00
|
|
|
|
2020-03-02 20:47:43 +00:00
|
|
|
namespace tint {
|
|
|
|
|
2020-04-20 14:20:01 +00:00
|
|
|
TypeDeterminer::TypeDeterminer(Context* ctx, ast::Module* mod)
|
|
|
|
: ctx_(*ctx), mod_(mod) {}
|
2020-03-02 20:47:43 +00:00
|
|
|
|
|
|
|
TypeDeterminer::~TypeDeterminer() = default;
|
|
|
|
|
2020-04-08 19:58:35 +00:00
|
|
|
void TypeDeterminer::set_error(const Source& src, const std::string& msg) {
|
|
|
|
error_ = "";
|
|
|
|
if (src.line > 0) {
|
|
|
|
error_ +=
|
|
|
|
std::to_string(src.line) + ":" + std::to_string(src.column) + ": ";
|
|
|
|
}
|
|
|
|
error_ += msg;
|
|
|
|
}
|
|
|
|
|
2020-04-20 14:20:01 +00:00
|
|
|
bool TypeDeterminer::Determine() {
|
|
|
|
for (const auto& var : mod_->global_variables()) {
|
2020-04-06 21:07:41 +00:00
|
|
|
variable_stack_.set_global(var->name(), var.get());
|
|
|
|
}
|
|
|
|
|
2020-04-20 14:20:01 +00:00
|
|
|
for (const auto& func : mod_->functions()) {
|
2020-04-06 21:07:41 +00:00
|
|
|
name_to_function_[func->name()] = func.get();
|
|
|
|
}
|
|
|
|
|
2020-04-20 14:20:01 +00:00
|
|
|
if (!DetermineFunctions(mod_->functions())) {
|
2020-04-06 21:07:41 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TypeDeterminer::DetermineFunctions(const ast::FunctionList& funcs) {
|
|
|
|
for (const auto& func : funcs) {
|
|
|
|
if (!DetermineFunction(func.get())) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TypeDeterminer::DetermineFunction(ast::Function* func) {
|
|
|
|
variable_stack_.push_scope();
|
2020-04-08 19:58:20 +00:00
|
|
|
if (!DetermineStatements(func->body())) {
|
2020-04-06 21:07:41 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
variable_stack_.pop_scope();
|
|
|
|
|
2020-03-02 20:47:43 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-04-08 19:58:20 +00:00
|
|
|
bool TypeDeterminer::DetermineStatements(const ast::StatementList& stmts) {
|
2020-04-06 21:07:41 +00:00
|
|
|
for (const auto& stmt : stmts) {
|
2020-04-08 19:58:20 +00:00
|
|
|
if (!DetermineVariableStorageClass(stmt.get())) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-04-06 21:07:41 +00:00
|
|
|
if (!DetermineResultType(stmt.get())) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-04-08 19:58:20 +00:00
|
|
|
bool TypeDeterminer::DetermineVariableStorageClass(ast::Statement* stmt) {
|
|
|
|
if (!stmt->IsVariableDecl()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-04-09 18:52:06 +00:00
|
|
|
auto* var = stmt->AsVariableDecl()->variable();
|
2020-04-08 19:58:20 +00:00
|
|
|
// Nothing to do for const
|
|
|
|
if (var->is_const()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (var->storage_class() == ast::StorageClass::kFunction) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (var->storage_class() != ast::StorageClass::kNone) {
|
2020-04-21 13:05:42 +00:00
|
|
|
set_error(stmt->source(),
|
|
|
|
"function variable has a non-function storage class");
|
2020-04-08 19:58:20 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
var->set_storage_class(ast::StorageClass::kFunction);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-04-07 12:47:23 +00:00
|
|
|
bool TypeDeterminer::DetermineResultType(ast::Statement* stmt) {
|
|
|
|
if (stmt->IsAssign()) {
|
2020-04-09 18:52:06 +00:00
|
|
|
auto* a = stmt->AsAssign();
|
2020-04-07 12:47:23 +00:00
|
|
|
return DetermineResultType(a->lhs()) && DetermineResultType(a->rhs());
|
|
|
|
}
|
2020-04-07 12:54:10 +00:00
|
|
|
if (stmt->IsBreak()) {
|
2020-04-09 18:52:06 +00:00
|
|
|
auto* b = stmt->AsBreak();
|
2020-04-07 12:54:10 +00:00
|
|
|
return DetermineResultType(b->conditional());
|
|
|
|
}
|
2020-04-07 12:54:20 +00:00
|
|
|
if (stmt->IsCase()) {
|
2020-04-09 18:52:06 +00:00
|
|
|
auto* c = stmt->AsCase();
|
2020-04-08 19:58:20 +00:00
|
|
|
return DetermineStatements(c->body());
|
2020-04-07 12:54:20 +00:00
|
|
|
}
|
2020-04-07 12:54:29 +00:00
|
|
|
if (stmt->IsContinue()) {
|
2020-04-09 18:52:06 +00:00
|
|
|
auto* c = stmt->AsContinue();
|
2020-04-07 12:54:29 +00:00
|
|
|
return DetermineResultType(c->conditional());
|
|
|
|
}
|
2020-04-07 12:54:37 +00:00
|
|
|
if (stmt->IsElse()) {
|
2020-04-09 18:52:06 +00:00
|
|
|
auto* e = stmt->AsElse();
|
2020-04-07 12:54:37 +00:00
|
|
|
return DetermineResultType(e->condition()) &&
|
2020-04-08 19:58:20 +00:00
|
|
|
DetermineStatements(e->body());
|
2020-04-07 12:54:37 +00:00
|
|
|
}
|
2020-04-07 12:54:59 +00:00
|
|
|
if (stmt->IsFallthrough()) {
|
|
|
|
return true;
|
|
|
|
}
|
2020-04-07 12:55:25 +00:00
|
|
|
if (stmt->IsIf()) {
|
2020-04-09 18:52:06 +00:00
|
|
|
auto* i = stmt->AsIf();
|
2020-04-07 12:55:25 +00:00
|
|
|
if (!DetermineResultType(i->condition()) ||
|
2020-04-08 19:58:20 +00:00
|
|
|
!DetermineStatements(i->body())) {
|
2020-04-07 12:55:25 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const auto& else_stmt : i->else_statements()) {
|
|
|
|
if (!DetermineResultType(else_stmt.get())) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2020-04-07 12:54:59 +00:00
|
|
|
if (stmt->IsKill()) {
|
|
|
|
return true;
|
|
|
|
}
|
2020-04-07 12:55:51 +00:00
|
|
|
if (stmt->IsLoop()) {
|
2020-04-09 18:52:06 +00:00
|
|
|
auto* l = stmt->AsLoop();
|
2020-04-08 19:58:20 +00:00
|
|
|
return DetermineStatements(l->body()) &&
|
|
|
|
DetermineStatements(l->continuing());
|
2020-04-07 12:55:51 +00:00
|
|
|
}
|
2020-04-07 12:56:24 +00:00
|
|
|
if (stmt->IsReturn()) {
|
2020-04-09 18:52:06 +00:00
|
|
|
auto* r = stmt->AsReturn();
|
2020-04-07 12:56:24 +00:00
|
|
|
return DetermineResultType(r->value());
|
|
|
|
}
|
2020-04-07 12:56:45 +00:00
|
|
|
if (stmt->IsSwitch()) {
|
2020-04-09 18:52:06 +00:00
|
|
|
auto* s = stmt->AsSwitch();
|
2020-04-07 12:56:45 +00:00
|
|
|
if (!DetermineResultType(s->condition())) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
for (const auto& case_stmt : s->body()) {
|
|
|
|
if (!DetermineResultType(case_stmt.get())) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2020-04-07 12:54:59 +00:00
|
|
|
return true;
|
|
|
|
}
|
2020-04-07 12:56:57 +00:00
|
|
|
if (stmt->IsUnless()) {
|
2020-04-09 18:52:06 +00:00
|
|
|
auto* u = stmt->AsUnless();
|
2020-04-07 12:56:57 +00:00
|
|
|
return DetermineResultType(u->condition()) &&
|
2020-04-08 19:58:20 +00:00
|
|
|
DetermineStatements(u->body());
|
2020-04-07 12:56:57 +00:00
|
|
|
}
|
2020-04-07 12:57:12 +00:00
|
|
|
if (stmt->IsVariableDecl()) {
|
2020-04-09 18:52:06 +00:00
|
|
|
auto* v = stmt->AsVariableDecl();
|
2020-04-07 12:57:12 +00:00
|
|
|
variable_stack_.set(v->variable()->name(), v->variable());
|
|
|
|
return DetermineResultType(v->variable()->constructor());
|
|
|
|
}
|
2020-04-07 12:47:23 +00:00
|
|
|
|
2020-04-08 19:58:35 +00:00
|
|
|
set_error(stmt->source(), "unknown statement type for type determination");
|
2020-04-06 21:07:41 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-04-20 15:46:18 +00:00
|
|
|
bool TypeDeterminer::DetermineResultType(const ast::ExpressionList& list) {
|
|
|
|
for (const auto& expr : list) {
|
|
|
|
if (!DetermineResultType(expr.get())) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-04-06 21:07:41 +00:00
|
|
|
bool TypeDeterminer::DetermineResultType(ast::Expression* expr) {
|
|
|
|
// This is blindly called above, so in some cases the expression won't exist.
|
|
|
|
if (!expr) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-04-07 12:57:42 +00:00
|
|
|
if (expr->IsArrayAccessor()) {
|
|
|
|
return DetermineArrayAccessor(expr->AsArrayAccessor());
|
|
|
|
}
|
2020-04-07 12:57:52 +00:00
|
|
|
if (expr->IsAs()) {
|
|
|
|
return DetermineAs(expr->AsAs());
|
|
|
|
}
|
2020-04-07 19:27:41 +00:00
|
|
|
if (expr->IsBinary()) {
|
|
|
|
return DetermineBinary(expr->AsBinary());
|
|
|
|
}
|
2020-04-07 16:41:10 +00:00
|
|
|
if (expr->IsCall()) {
|
|
|
|
return DetermineCall(expr->AsCall());
|
|
|
|
}
|
2020-04-07 16:41:23 +00:00
|
|
|
if (expr->IsCast()) {
|
|
|
|
return DetermineCast(expr->AsCast());
|
|
|
|
}
|
2020-04-07 12:46:30 +00:00
|
|
|
if (expr->IsConstructor()) {
|
|
|
|
return DetermineConstructor(expr->AsConstructor());
|
|
|
|
}
|
2020-04-07 12:57:27 +00:00
|
|
|
if (expr->IsIdentifier()) {
|
|
|
|
return DetermineIdentifier(expr->AsIdentifier());
|
|
|
|
}
|
2020-04-07 16:41:33 +00:00
|
|
|
if (expr->IsMemberAccessor()) {
|
|
|
|
return DetermineMemberAccessor(expr->AsMemberAccessor());
|
|
|
|
}
|
2020-04-07 19:26:49 +00:00
|
|
|
if (expr->IsUnaryDerivative()) {
|
|
|
|
return DetermineUnaryDerivative(expr->AsUnaryDerivative());
|
|
|
|
}
|
2020-04-07 19:27:00 +00:00
|
|
|
if (expr->IsUnaryMethod()) {
|
|
|
|
return DetermineUnaryMethod(expr->AsUnaryMethod());
|
|
|
|
}
|
2020-04-07 19:27:11 +00:00
|
|
|
if (expr->IsUnaryOp()) {
|
|
|
|
return DetermineUnaryOp(expr->AsUnaryOp());
|
|
|
|
}
|
2020-04-07 12:46:30 +00:00
|
|
|
|
2020-04-08 19:58:35 +00:00
|
|
|
set_error(expr->source(), "unknown expression for type determination");
|
2020-04-06 21:07:41 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-04-07 12:57:42 +00:00
|
|
|
bool TypeDeterminer::DetermineArrayAccessor(
|
|
|
|
ast::ArrayAccessorExpression* expr) {
|
|
|
|
if (!DetermineResultType(expr->array())) {
|
|
|
|
return false;
|
|
|
|
}
|
2020-05-01 16:17:03 +00:00
|
|
|
if (!DetermineResultType(expr->idx_expr())) {
|
|
|
|
return false;
|
|
|
|
}
|
2020-04-23 22:26:52 +00:00
|
|
|
|
|
|
|
auto* res = expr->array()->result_type();
|
|
|
|
auto* parent_type = res->UnwrapPtrIfNeeded();
|
|
|
|
ast::type::Type* ret = nullptr;
|
2020-04-07 12:57:42 +00:00
|
|
|
if (parent_type->IsArray()) {
|
2020-04-23 22:26:52 +00:00
|
|
|
ret = parent_type->AsArray()->type();
|
2020-04-07 12:57:42 +00:00
|
|
|
} else if (parent_type->IsVector()) {
|
2020-04-23 22:26:52 +00:00
|
|
|
ret = parent_type->AsVector()->type();
|
2020-04-07 12:57:42 +00:00
|
|
|
} else if (parent_type->IsMatrix()) {
|
2020-04-09 18:52:06 +00:00
|
|
|
auto* m = parent_type->AsMatrix();
|
2020-04-23 22:26:52 +00:00
|
|
|
ret = ctx_.type_mgr().Get(
|
|
|
|
std::make_unique<ast::type::VectorType>(m->type(), m->rows()));
|
2020-04-07 12:57:42 +00:00
|
|
|
} else {
|
2020-04-08 19:58:35 +00:00
|
|
|
set_error(expr->source(), "invalid parent type in array accessor");
|
2020-04-07 12:57:42 +00:00
|
|
|
return false;
|
|
|
|
}
|
2020-04-23 22:26:52 +00:00
|
|
|
|
|
|
|
// If we're extracting from a pointer, we return a pointer.
|
|
|
|
if (res->IsPointer()) {
|
|
|
|
ret = ctx_.type_mgr().Get(std::make_unique<ast::type::PointerType>(
|
|
|
|
ret, res->AsPointer()->storage_class()));
|
|
|
|
}
|
|
|
|
expr->set_result_type(ret);
|
|
|
|
|
2020-04-07 12:57:42 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-04-07 12:57:52 +00:00
|
|
|
bool TypeDeterminer::DetermineAs(ast::AsExpression* expr) {
|
|
|
|
expr->set_result_type(expr->type());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-04-07 16:41:10 +00:00
|
|
|
bool TypeDeterminer::DetermineCall(ast::CallExpression* expr) {
|
2020-04-20 15:46:18 +00:00
|
|
|
if (!DetermineResultType(expr->params())) {
|
|
|
|
return false;
|
2020-04-20 14:18:54 +00:00
|
|
|
}
|
|
|
|
|
2020-04-20 15:46:18 +00:00
|
|
|
// The expression has to be an identifier as you can't store function pointers
|
|
|
|
// but, if it isn't we'll just use the normal result determination to be on
|
|
|
|
// the safe side.
|
|
|
|
if (expr->func()->IsIdentifier()) {
|
|
|
|
auto* ident = expr->func()->AsIdentifier();
|
|
|
|
|
|
|
|
if (ident->has_path()) {
|
|
|
|
auto* imp = mod_->FindImportByName(ident->path());
|
|
|
|
if (imp == nullptr) {
|
2020-04-21 13:05:42 +00:00
|
|
|
set_error(expr->source(), "Unable to find import for " + ident->name());
|
2020-04-20 15:46:18 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t ext_id = 0;
|
2020-04-21 13:05:42 +00:00
|
|
|
auto* result_type = GetImportData(expr->source(), imp->path(),
|
|
|
|
ident->name(), expr->params(), &ext_id);
|
2020-04-20 15:46:18 +00:00
|
|
|
if (result_type == nullptr) {
|
2020-04-24 00:40:12 +00:00
|
|
|
if (error_.empty()) {
|
|
|
|
set_error(expr->source(),
|
|
|
|
"Unable to determine result type for GLSL expression " +
|
|
|
|
ident->name());
|
|
|
|
}
|
2020-04-20 15:46:18 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
imp->AddMethodId(ident->name(), ext_id);
|
|
|
|
expr->func()->set_result_type(result_type);
|
|
|
|
} else {
|
|
|
|
// An identifier with a single name is a function call, not an import
|
|
|
|
// lookup which we can handle with the regular identifier lookup.
|
|
|
|
if (!DetermineResultType(ident)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (!DetermineResultType(expr->func())) {
|
|
|
|
return false;
|
|
|
|
}
|
2020-04-07 16:41:10 +00:00
|
|
|
}
|
|
|
|
expr->set_result_type(expr->func()->result_type());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-04-07 16:41:23 +00:00
|
|
|
bool TypeDeterminer::DetermineCast(ast::CastExpression* expr) {
|
2020-05-01 19:05:55 +00:00
|
|
|
if (!DetermineResultType(expr->expr())) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-04-07 16:41:23 +00:00
|
|
|
expr->set_result_type(expr->type());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-04-07 12:46:30 +00:00
|
|
|
bool TypeDeterminer::DetermineConstructor(ast::ConstructorExpression* expr) {
|
|
|
|
if (expr->IsTypeConstructor()) {
|
2020-04-24 00:41:12 +00:00
|
|
|
auto* ty = expr->AsTypeConstructor();
|
|
|
|
for (const auto& value : ty->values()) {
|
|
|
|
if (!DetermineResultType(value.get())) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
expr->set_result_type(ty->type());
|
2020-04-07 12:46:30 +00:00
|
|
|
} else {
|
|
|
|
expr->set_result_type(expr->AsScalarConstructor()->literal()->type());
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-04-07 12:57:27 +00:00
|
|
|
bool TypeDeterminer::DetermineIdentifier(ast::IdentifierExpression* expr) {
|
2020-04-20 14:21:00 +00:00
|
|
|
if (expr->has_path()) {
|
2020-04-20 15:46:18 +00:00
|
|
|
set_error(expr->source(),
|
|
|
|
"determine identifier should not be called with imports");
|
2020-04-07 12:57:27 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-04-20 14:21:00 +00:00
|
|
|
auto name = expr->name();
|
2020-04-07 12:57:27 +00:00
|
|
|
ast::Variable* var;
|
|
|
|
if (variable_stack_.get(name, &var)) {
|
2020-04-23 22:26:52 +00:00
|
|
|
// A constant is the type, but a variable is always a pointer so synthesize
|
|
|
|
// the pointer around the variable type.
|
|
|
|
if (var->is_const()) {
|
|
|
|
expr->set_result_type(var->type());
|
|
|
|
} else {
|
|
|
|
expr->set_result_type(
|
|
|
|
ctx_.type_mgr().Get(std::make_unique<ast::type::PointerType>(
|
|
|
|
var->type(), var->storage_class())));
|
|
|
|
}
|
2020-04-07 12:57:27 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto iter = name_to_function_.find(name);
|
|
|
|
if (iter != name_to_function_.end()) {
|
|
|
|
expr->set_result_type(iter->second->return_type());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-04-07 16:41:33 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TypeDeterminer::DetermineMemberAccessor(
|
|
|
|
ast::MemberAccessorExpression* expr) {
|
|
|
|
if (!DetermineResultType(expr->structure())) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-04-23 22:26:52 +00:00
|
|
|
auto* res = expr->structure()->result_type();
|
|
|
|
auto* data_type = res->UnwrapPtrIfNeeded();
|
2020-04-24 00:40:45 +00:00
|
|
|
|
|
|
|
while (data_type->IsAlias()) {
|
|
|
|
data_type = data_type->AsAlias()->type();
|
|
|
|
}
|
|
|
|
|
2020-04-23 22:26:52 +00:00
|
|
|
ast::type::Type* ret = nullptr;
|
2020-04-07 16:41:33 +00:00
|
|
|
if (data_type->IsStruct()) {
|
2020-04-09 18:52:06 +00:00
|
|
|
auto* strct = data_type->AsStruct()->impl();
|
2020-04-20 14:21:00 +00:00
|
|
|
auto name = expr->member()->name();
|
2020-04-07 16:41:33 +00:00
|
|
|
|
|
|
|
for (const auto& member : strct->members()) {
|
2020-04-23 22:26:52 +00:00
|
|
|
if (member->name() == name) {
|
|
|
|
ret = member->type();
|
|
|
|
break;
|
2020-04-07 16:41:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-23 22:26:52 +00:00
|
|
|
if (ret == nullptr) {
|
|
|
|
set_error(expr->source(), "struct member " + name + " not found");
|
|
|
|
return false;
|
|
|
|
}
|
2020-05-01 16:17:03 +00:00
|
|
|
|
|
|
|
// If we're extracting from a pointer, we return a pointer.
|
|
|
|
if (res->IsPointer()) {
|
|
|
|
ret = ctx_.type_mgr().Get(std::make_unique<ast::type::PointerType>(
|
|
|
|
ret, res->AsPointer()->storage_class()));
|
|
|
|
}
|
2020-04-23 22:26:52 +00:00
|
|
|
} else if (data_type->IsVector()) {
|
2020-04-09 18:52:06 +00:00
|
|
|
auto* vec = data_type->AsVector();
|
2020-04-07 16:41:33 +00:00
|
|
|
|
2020-04-21 13:05:34 +00:00
|
|
|
auto size = expr->member()->name().size();
|
|
|
|
if (size == 1) {
|
|
|
|
// A single element swizzle is just the type of the vector.
|
2020-04-23 22:26:52 +00:00
|
|
|
ret = vec->type();
|
2020-05-01 16:17:03 +00:00
|
|
|
// If we're extracting from a pointer, we return a pointer.
|
|
|
|
if (res->IsPointer()) {
|
|
|
|
ret = ctx_.type_mgr().Get(std::make_unique<ast::type::PointerType>(
|
|
|
|
ret, res->AsPointer()->storage_class()));
|
|
|
|
}
|
2020-04-21 13:05:34 +00:00
|
|
|
} else {
|
|
|
|
// The vector will have a number of components equal to the length of the
|
|
|
|
// swizzle. This assumes the validator will check that the swizzle
|
|
|
|
// is correct.
|
2020-04-23 22:26:52 +00:00
|
|
|
ret = ctx_.type_mgr().Get(
|
|
|
|
std::make_unique<ast::type::VectorType>(vec->type(), size));
|
2020-04-21 13:05:34 +00:00
|
|
|
}
|
2020-04-23 22:26:52 +00:00
|
|
|
} else {
|
|
|
|
set_error(expr->source(),
|
|
|
|
"invalid type " + data_type->type_name() + " in member accessor");
|
|
|
|
return false;
|
2020-04-07 16:41:33 +00:00
|
|
|
}
|
|
|
|
|
2020-04-23 22:26:52 +00:00
|
|
|
expr->set_result_type(ret);
|
|
|
|
|
|
|
|
return true;
|
2020-04-07 12:57:27 +00:00
|
|
|
}
|
|
|
|
|
2020-04-07 19:27:41 +00:00
|
|
|
bool TypeDeterminer::DetermineBinary(ast::BinaryExpression* expr) {
|
2020-04-07 19:26:39 +00:00
|
|
|
if (!DetermineResultType(expr->lhs()) || !DetermineResultType(expr->rhs())) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Result type matches first parameter type
|
|
|
|
if (expr->IsAnd() || expr->IsOr() || expr->IsXor() || expr->IsShiftLeft() ||
|
|
|
|
expr->IsShiftRight() || expr->IsShiftRightArith() || expr->IsAdd() ||
|
|
|
|
expr->IsSubtract() || expr->IsDivide() || expr->IsModulo()) {
|
2020-04-23 22:26:52 +00:00
|
|
|
expr->set_result_type(expr->lhs()->result_type()->UnwrapPtrIfNeeded());
|
2020-04-07 19:26:39 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// Result type is a scalar or vector of boolean type
|
|
|
|
if (expr->IsLogicalAnd() || expr->IsLogicalOr() || expr->IsEqual() ||
|
|
|
|
expr->IsNotEqual() || expr->IsLessThan() || expr->IsGreaterThan() ||
|
|
|
|
expr->IsLessThanEqual() || expr->IsGreaterThanEqual()) {
|
2020-04-09 18:52:06 +00:00
|
|
|
auto* bool_type =
|
2020-04-07 19:26:39 +00:00
|
|
|
ctx_.type_mgr().Get(std::make_unique<ast::type::BoolType>());
|
2020-04-23 22:26:52 +00:00
|
|
|
auto* param_type = expr->lhs()->result_type()->UnwrapPtrIfNeeded();
|
2020-04-07 19:26:39 +00:00
|
|
|
if (param_type->IsVector()) {
|
|
|
|
expr->set_result_type(
|
|
|
|
ctx_.type_mgr().Get(std::make_unique<ast::type::VectorType>(
|
|
|
|
bool_type, param_type->AsVector()->size())));
|
|
|
|
} else {
|
|
|
|
expr->set_result_type(bool_type);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (expr->IsMultiply()) {
|
2020-04-23 22:26:52 +00:00
|
|
|
auto* lhs_type = expr->lhs()->result_type()->UnwrapPtrIfNeeded();
|
|
|
|
auto* rhs_type = expr->rhs()->result_type()->UnwrapPtrIfNeeded();
|
2020-04-07 19:26:39 +00:00
|
|
|
|
|
|
|
// Note, the ordering here matters. The later checks depend on the prior
|
|
|
|
// checks having been done.
|
|
|
|
if (lhs_type->IsMatrix() && rhs_type->IsMatrix()) {
|
|
|
|
expr->set_result_type(
|
|
|
|
ctx_.type_mgr().Get(std::make_unique<ast::type::MatrixType>(
|
|
|
|
lhs_type->AsMatrix()->type(), lhs_type->AsMatrix()->rows(),
|
|
|
|
rhs_type->AsMatrix()->columns())));
|
|
|
|
|
|
|
|
} else if (lhs_type->IsMatrix() && rhs_type->IsVector()) {
|
2020-04-09 18:52:06 +00:00
|
|
|
auto* mat = lhs_type->AsMatrix();
|
2020-04-07 19:27:21 +00:00
|
|
|
expr->set_result_type(ctx_.type_mgr().Get(
|
|
|
|
std::make_unique<ast::type::VectorType>(mat->type(), mat->rows())));
|
2020-04-07 19:26:39 +00:00
|
|
|
} else if (lhs_type->IsVector() && rhs_type->IsMatrix()) {
|
2020-04-09 18:52:06 +00:00
|
|
|
auto* mat = rhs_type->AsMatrix();
|
2020-04-07 19:26:39 +00:00
|
|
|
expr->set_result_type(
|
|
|
|
ctx_.type_mgr().Get(std::make_unique<ast::type::VectorType>(
|
|
|
|
mat->type(), mat->columns())));
|
|
|
|
} else if (lhs_type->IsMatrix()) {
|
|
|
|
// matrix * scalar
|
|
|
|
expr->set_result_type(lhs_type);
|
|
|
|
} else if (rhs_type->IsMatrix()) {
|
|
|
|
// scalar * matrix
|
|
|
|
expr->set_result_type(rhs_type);
|
|
|
|
} else if (lhs_type->IsVector() && rhs_type->IsVector()) {
|
|
|
|
expr->set_result_type(lhs_type);
|
|
|
|
} else if (lhs_type->IsVector()) {
|
|
|
|
// Vector * scalar
|
|
|
|
expr->set_result_type(lhs_type);
|
|
|
|
} else if (rhs_type->IsVector()) {
|
|
|
|
// Scalar * vector
|
|
|
|
expr->set_result_type(rhs_type);
|
|
|
|
} else {
|
|
|
|
// Scalar * Scalar
|
|
|
|
expr->set_result_type(lhs_type);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-04-21 13:05:42 +00:00
|
|
|
set_error(expr->source(), "Unknown binary expression");
|
2020-04-07 19:26:39 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-04-07 19:26:49 +00:00
|
|
|
bool TypeDeterminer::DetermineUnaryDerivative(
|
|
|
|
ast::UnaryDerivativeExpression* expr) {
|
|
|
|
// The result type must be the same as the type of the parameter.
|
|
|
|
if (!DetermineResultType(expr->param())) {
|
|
|
|
return false;
|
|
|
|
}
|
2020-04-23 22:26:52 +00:00
|
|
|
expr->set_result_type(expr->param()->result_type()->UnwrapPtrIfNeeded());
|
2020-04-07 19:26:49 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-04-07 19:27:00 +00:00
|
|
|
bool TypeDeterminer::DetermineUnaryMethod(ast::UnaryMethodExpression* expr) {
|
2020-04-20 15:46:18 +00:00
|
|
|
if (!DetermineResultType(expr->params())) {
|
|
|
|
return false;
|
2020-04-07 19:27:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
switch (expr->op()) {
|
|
|
|
case ast::UnaryMethod::kAny:
|
|
|
|
case ast::UnaryMethod::kAll: {
|
|
|
|
expr->set_result_type(
|
|
|
|
ctx_.type_mgr().Get(std::make_unique<ast::type::BoolType>()));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case ast::UnaryMethod::kIsNan:
|
|
|
|
case ast::UnaryMethod::kIsInf:
|
|
|
|
case ast::UnaryMethod::kIsFinite:
|
|
|
|
case ast::UnaryMethod::kIsNormal: {
|
|
|
|
if (expr->params().empty()) {
|
2020-04-08 19:58:35 +00:00
|
|
|
set_error(expr->source(), "incorrect number of parameters");
|
2020-04-07 19:27:00 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-04-09 18:52:06 +00:00
|
|
|
auto* bool_type =
|
2020-04-07 19:27:00 +00:00
|
|
|
ctx_.type_mgr().Get(std::make_unique<ast::type::BoolType>());
|
2020-04-23 22:26:52 +00:00
|
|
|
auto* param_type = expr->params()[0]->result_type()->UnwrapPtrIfNeeded();
|
2020-04-07 19:27:00 +00:00
|
|
|
if (param_type->IsVector()) {
|
|
|
|
expr->set_result_type(
|
|
|
|
ctx_.type_mgr().Get(std::make_unique<ast::type::VectorType>(
|
|
|
|
bool_type, param_type->AsVector()->size())));
|
|
|
|
} else {
|
|
|
|
expr->set_result_type(bool_type);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case ast::UnaryMethod::kDot: {
|
|
|
|
expr->set_result_type(
|
|
|
|
ctx_.type_mgr().Get(std::make_unique<ast::type::F32Type>()));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case ast::UnaryMethod::kOuterProduct: {
|
|
|
|
if (expr->params().size() != 2) {
|
2020-04-08 19:58:35 +00:00
|
|
|
set_error(expr->source(),
|
|
|
|
"incorrect number of parameters for outer product");
|
2020-04-07 19:27:00 +00:00
|
|
|
return false;
|
|
|
|
}
|
2020-04-23 22:26:52 +00:00
|
|
|
auto* param0_type = expr->params()[0]->result_type()->UnwrapPtrIfNeeded();
|
|
|
|
auto* param1_type = expr->params()[1]->result_type()->UnwrapPtrIfNeeded();
|
2020-04-07 19:27:00 +00:00
|
|
|
if (!param0_type->IsVector() || !param1_type->IsVector()) {
|
2020-04-08 19:58:35 +00:00
|
|
|
set_error(expr->source(), "invalid parameter type for outer product");
|
2020-04-07 19:27:00 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
expr->set_result_type(
|
|
|
|
ctx_.type_mgr().Get(std::make_unique<ast::type::MatrixType>(
|
|
|
|
ctx_.type_mgr().Get(std::make_unique<ast::type::F32Type>()),
|
|
|
|
param0_type->AsVector()->size(),
|
|
|
|
param1_type->AsVector()->size())));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-04-07 19:27:11 +00:00
|
|
|
bool TypeDeterminer::DetermineUnaryOp(ast::UnaryOpExpression* expr) {
|
2020-04-07 19:27:21 +00:00
|
|
|
// Result type matches the parameter type.
|
|
|
|
if (!DetermineResultType(expr->expr())) {
|
|
|
|
return false;
|
|
|
|
}
|
2020-04-23 22:26:52 +00:00
|
|
|
expr->set_result_type(expr->expr()->result_type()->UnwrapPtrIfNeeded());
|
2020-04-07 19:27:21 +00:00
|
|
|
return true;
|
2020-04-07 19:27:11 +00:00
|
|
|
}
|
|
|
|
|
2020-04-20 15:46:18 +00:00
|
|
|
ast::type::Type* TypeDeterminer::GetImportData(
|
2020-04-21 13:05:42 +00:00
|
|
|
const Source& source,
|
2020-04-20 15:46:18 +00:00
|
|
|
const std::string& path,
|
|
|
|
const std::string& name,
|
|
|
|
const ast::ExpressionList& params,
|
|
|
|
uint32_t* id) {
|
|
|
|
if (path != "GLSL.std.450") {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2020-04-20 15:49:33 +00:00
|
|
|
// Most of these are floating-point general except the below which are only
|
|
|
|
// FP16 and FP32. We only have FP32 at this point so the below works, if we
|
|
|
|
// get FP64 support or otherwise we'll need to differentiate.
|
|
|
|
// * radians
|
|
|
|
// * degrees
|
2020-04-20 15:49:43 +00:00
|
|
|
// * sin, cos, tan
|
|
|
|
// * asin, acos, atan
|
|
|
|
// * sinh, cosh, tanh
|
|
|
|
// * asinh, acosh, atanh
|
2020-04-20 15:49:50 +00:00
|
|
|
// * exp, exp2
|
|
|
|
// * log, log2
|
2020-04-20 15:49:33 +00:00
|
|
|
|
2020-04-20 15:48:13 +00:00
|
|
|
if (name == "round" || name == "roundeven" || name == "trunc" ||
|
2020-04-20 15:49:07 +00:00
|
|
|
name == "fabs" || name == "fsign" || name == "floor" || name == "ceil" ||
|
2020-04-20 15:49:43 +00:00
|
|
|
name == "fract" || name == "radians" || name == "degrees" ||
|
|
|
|
name == "sin" || name == "cos" || name == "tan" || name == "asin" ||
|
|
|
|
name == "acos" || name == "atan" || name == "sinh" || name == "cosh" ||
|
2020-04-20 15:49:50 +00:00
|
|
|
name == "tanh" || name == "asinh" || name == "acosh" || name == "atanh" ||
|
|
|
|
name == "exp" || name == "log" || name == "exp2" || name == "log2" ||
|
2020-04-21 13:04:15 +00:00
|
|
|
name == "sqrt" || name == "inversesqrt" || name == "normalize" ||
|
|
|
|
name == "length") {
|
2020-04-20 15:46:18 +00:00
|
|
|
if (params.size() != 1) {
|
2020-04-21 13:05:42 +00:00
|
|
|
set_error(source, "incorrect number of parameters for " + name +
|
|
|
|
". Expected 1 got " +
|
|
|
|
std::to_string(params.size()));
|
2020-04-20 15:46:18 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
2020-04-24 00:40:12 +00:00
|
|
|
|
|
|
|
auto* result_type = params[0]->result_type()->UnwrapPtrIfNeeded();
|
|
|
|
if (!result_type->is_float_scalar_or_vector()) {
|
2020-04-21 13:05:42 +00:00
|
|
|
set_error(source, "incorrect type for " + name +
|
|
|
|
". Requires a float scalar or a float vector");
|
2020-04-20 15:46:18 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2020-04-20 15:47:55 +00:00
|
|
|
if (name == "round") {
|
|
|
|
*id = GLSLstd450Round;
|
|
|
|
} else if (name == "roundeven") {
|
|
|
|
*id = GLSLstd450RoundEven;
|
2020-04-20 15:48:03 +00:00
|
|
|
} else if (name == "trunc") {
|
|
|
|
*id = GLSLstd450Trunc;
|
2020-04-20 15:48:13 +00:00
|
|
|
} else if (name == "fabs") {
|
|
|
|
*id = GLSLstd450FAbs;
|
2020-04-20 15:48:20 +00:00
|
|
|
} else if (name == "fsign") {
|
|
|
|
*id = GLSLstd450FSign;
|
2020-04-20 15:48:26 +00:00
|
|
|
} else if (name == "floor") {
|
|
|
|
*id = GLSLstd450Floor;
|
2020-04-20 15:48:56 +00:00
|
|
|
} else if (name == "ceil") {
|
|
|
|
*id = GLSLstd450Ceil;
|
2020-04-20 15:49:07 +00:00
|
|
|
} else if (name == "fract") {
|
|
|
|
*id = GLSLstd450Fract;
|
2020-04-20 15:49:33 +00:00
|
|
|
} else if (name == "radians") {
|
|
|
|
*id = GLSLstd450Radians;
|
|
|
|
} else if (name == "degrees") {
|
|
|
|
*id = GLSLstd450Degrees;
|
2020-04-20 15:49:43 +00:00
|
|
|
} else if (name == "sin") {
|
|
|
|
*id = GLSLstd450Sin;
|
|
|
|
} else if (name == "cos") {
|
|
|
|
*id = GLSLstd450Cos;
|
|
|
|
} else if (name == "tan") {
|
|
|
|
*id = GLSLstd450Tan;
|
|
|
|
} else if (name == "asin") {
|
|
|
|
*id = GLSLstd450Asin;
|
|
|
|
} else if (name == "acos") {
|
|
|
|
*id = GLSLstd450Acos;
|
|
|
|
} else if (name == "atan") {
|
|
|
|
*id = GLSLstd450Atan;
|
|
|
|
} else if (name == "sinh") {
|
|
|
|
*id = GLSLstd450Sinh;
|
|
|
|
} else if (name == "cosh") {
|
|
|
|
*id = GLSLstd450Cosh;
|
|
|
|
} else if (name == "tanh") {
|
|
|
|
*id = GLSLstd450Tanh;
|
|
|
|
} else if (name == "asinh") {
|
|
|
|
*id = GLSLstd450Asinh;
|
|
|
|
} else if (name == "acosh") {
|
|
|
|
*id = GLSLstd450Acosh;
|
|
|
|
} else if (name == "atanh") {
|
|
|
|
*id = GLSLstd450Atanh;
|
2020-04-20 15:49:50 +00:00
|
|
|
} else if (name == "exp") {
|
|
|
|
*id = GLSLstd450Exp;
|
|
|
|
} else if (name == "log") {
|
|
|
|
*id = GLSLstd450Log;
|
|
|
|
} else if (name == "exp2") {
|
|
|
|
*id = GLSLstd450Exp2;
|
|
|
|
} else if (name == "log2") {
|
|
|
|
*id = GLSLstd450Log2;
|
|
|
|
} else if (name == "sqrt") {
|
|
|
|
*id = GLSLstd450Sqrt;
|
|
|
|
} else if (name == "inversesqrt") {
|
|
|
|
*id = GLSLstd450InverseSqrt;
|
2020-04-20 15:51:18 +00:00
|
|
|
} else if (name == "normalize") {
|
|
|
|
*id = GLSLstd450Normalize;
|
2020-04-21 13:04:15 +00:00
|
|
|
} else if (name == "length") {
|
|
|
|
*id = GLSLstd450Length;
|
2020-04-20 15:48:03 +00:00
|
|
|
|
2020-04-21 13:04:15 +00:00
|
|
|
// Length returns a scalar of the same type as the parameter.
|
|
|
|
return result_type->is_float_scalar() ? result_type
|
|
|
|
: result_type->AsVector()->type();
|
2020-04-20 21:09:14 +00:00
|
|
|
}
|
|
|
|
|
2020-04-21 13:04:15 +00:00
|
|
|
return result_type;
|
2020-04-21 12:58:35 +00:00
|
|
|
} else if (name == "atan2" || name == "pow" || name == "fmin" ||
|
|
|
|
name == "fmax" || name == "step" || name == "reflect" ||
|
2020-04-21 13:04:15 +00:00
|
|
|
name == "nmin" || name == "nmax" || name == "distance") {
|
2020-04-21 12:55:06 +00:00
|
|
|
if (params.size() != 2) {
|
|
|
|
error_ = "incorrect number of parameters for " + name +
|
|
|
|
". Expected 2 got " + std::to_string(params.size());
|
|
|
|
return nullptr;
|
|
|
|
}
|
2020-04-24 00:40:12 +00:00
|
|
|
|
|
|
|
auto* result_type_0 = params[0]->result_type()->UnwrapPtrIfNeeded();
|
|
|
|
auto* result_type_1 = params[1]->result_type()->UnwrapPtrIfNeeded();
|
|
|
|
if (!result_type_0->is_float_scalar_or_vector() ||
|
|
|
|
!result_type_1->is_float_scalar_or_vector()) {
|
2020-04-21 12:55:06 +00:00
|
|
|
error_ = "incorrect type for " + name +
|
|
|
|
". Requires float scalar or a float vector values";
|
|
|
|
return nullptr;
|
|
|
|
}
|
2020-04-24 00:40:12 +00:00
|
|
|
if (result_type_0 != result_type_1) {
|
2020-04-21 12:55:06 +00:00
|
|
|
error_ = "mismatched parameter types for " + name;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (name == "atan2") {
|
|
|
|
*id = GLSLstd450Atan2;
|
2020-04-21 12:58:35 +00:00
|
|
|
} else if (name == "pow") {
|
|
|
|
*id = GLSLstd450Pow;
|
|
|
|
} else if (name == "fmin") {
|
|
|
|
*id = GLSLstd450FMin;
|
|
|
|
} else if (name == "fmax") {
|
|
|
|
*id = GLSLstd450FMax;
|
|
|
|
} else if (name == "step") {
|
|
|
|
*id = GLSLstd450Step;
|
|
|
|
} else if (name == "reflect") {
|
|
|
|
*id = GLSLstd450Reflect;
|
|
|
|
} else if (name == "nmin") {
|
|
|
|
*id = GLSLstd450NMin;
|
|
|
|
} else if (name == "nmax") {
|
|
|
|
*id = GLSLstd450NMax;
|
2020-04-21 13:04:15 +00:00
|
|
|
} else if (name == "distance") {
|
|
|
|
*id = GLSLstd450Distance;
|
|
|
|
|
|
|
|
// Distance returns a scalar of the same type as the parameter.
|
2020-04-24 00:40:12 +00:00
|
|
|
return result_type_0->is_float_scalar()
|
|
|
|
? result_type_0
|
|
|
|
: result_type_0->AsVector()->type();
|
2020-04-21 12:55:06 +00:00
|
|
|
}
|
|
|
|
|
2020-04-24 00:40:12 +00:00
|
|
|
return result_type_0;
|
2020-04-22 00:23:57 +00:00
|
|
|
} else if (name == "fclamp" || name == "fmix" || name == "smoothstep" ||
|
|
|
|
name == "fma" || name == "nclamp" || name == "faceforward") {
|
|
|
|
if (params.size() != 3) {
|
|
|
|
error_ = "incorrect number of parameters for " + name +
|
|
|
|
". Expected 3 got " + std::to_string(params.size());
|
|
|
|
return nullptr;
|
|
|
|
}
|
2020-04-24 00:40:12 +00:00
|
|
|
|
|
|
|
auto* result_type_0 = params[0]->result_type()->UnwrapPtrIfNeeded();
|
|
|
|
auto* result_type_1 = params[1]->result_type()->UnwrapPtrIfNeeded();
|
|
|
|
auto* result_type_2 = params[2]->result_type()->UnwrapPtrIfNeeded();
|
|
|
|
if (!result_type_0->is_float_scalar_or_vector() ||
|
|
|
|
!result_type_1->is_float_scalar_or_vector() ||
|
|
|
|
!result_type_2->is_float_scalar_or_vector()) {
|
2020-04-22 00:23:57 +00:00
|
|
|
error_ = "incorrect type for " + name +
|
|
|
|
". Requires float scalar or a float vector values";
|
|
|
|
return nullptr;
|
|
|
|
}
|
2020-04-24 00:40:12 +00:00
|
|
|
if (result_type_0 != result_type_1 || result_type_0 != result_type_2) {
|
2020-04-22 00:23:57 +00:00
|
|
|
error_ = "mismatched parameter types for " + name;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (name == "fclamp") {
|
|
|
|
*id = GLSLstd450FClamp;
|
|
|
|
} else if (name == "fmix") {
|
|
|
|
*id = GLSLstd450FMix;
|
|
|
|
} else if (name == "smoothstep") {
|
|
|
|
*id = GLSLstd450SmoothStep;
|
|
|
|
} else if (name == "fma") {
|
|
|
|
*id = GLSLstd450Fma;
|
|
|
|
} else if (name == "nclamp") {
|
|
|
|
*id = GLSLstd450NClamp;
|
|
|
|
} else if (name == "faceforward") {
|
|
|
|
*id = GLSLstd450FaceForward;
|
|
|
|
}
|
|
|
|
|
2020-04-24 00:40:12 +00:00
|
|
|
return result_type_0;
|
2020-04-20 15:46:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2020-03-02 20:47:43 +00:00
|
|
|
} // namespace tint
|