blob: 5ae76506265cf213e70e491bc84a876de31a83ab [file] [log] [blame]
/// Copyright 2021 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/tint/writer/glsl/generator_impl.h"
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <limits>
#include <set>
#include <utility>
#include <vector>
#include "src/tint/ast/call_statement.h"
#include "src/tint/ast/id_attribute.h"
#include "src/tint/ast/internal_attribute.h"
#include "src/tint/ast/interpolate_attribute.h"
#include "src/tint/ast/variable_decl_statement.h"
#include "src/tint/constant/value.h"
#include "src/tint/debug.h"
#include "src/tint/sem/block_statement.h"
#include "src/tint/sem/call.h"
#include "src/tint/sem/function.h"
#include "src/tint/sem/member_accessor_expression.h"
#include "src/tint/sem/module.h"
#include "src/tint/sem/statement.h"
#include "src/tint/sem/struct.h"
#include "src/tint/sem/switch_statement.h"
#include "src/tint/sem/value_constructor.h"
#include "src/tint/sem/value_conversion.h"
#include "src/tint/sem/variable.h"
#include "src/tint/switch.h"
#include "src/tint/transform/add_block_attribute.h"
#include "src/tint/transform/add_empty_entry_point.h"
#include "src/tint/transform/binding_remapper.h"
#include "src/tint/transform/builtin_polyfill.h"
#include "src/tint/transform/canonicalize_entry_point_io.h"
#include "src/tint/transform/combine_samplers.h"
#include "src/tint/transform/decompose_memory_access.h"
#include "src/tint/transform/demote_to_helper.h"
#include "src/tint/transform/direct_variable_access.h"
#include "src/tint/transform/disable_uniformity_analysis.h"
#include "src/tint/transform/expand_compound_assignment.h"
#include "src/tint/transform/manager.h"
#include "src/tint/transform/multiplanar_external_texture.h"
#include "src/tint/transform/pad_structs.h"
#include "src/tint/transform/preserve_padding.h"
#include "src/tint/transform/promote_initializers_to_let.h"
#include "src/tint/transform/promote_side_effects_to_decl.h"
#include "src/tint/transform/remove_phonies.h"
#include "src/tint/transform/renamer.h"
#include "src/tint/transform/robustness.h"
#include "src/tint/transform/simplify_pointers.h"
#include "src/tint/transform/single_entry_point.h"
#include "src/tint/transform/std140.h"
#include "src/tint/transform/texture_1d_to_2d.h"
#include "src/tint/transform/unshadow.h"
#include "src/tint/transform/zero_init_workgroup_memory.h"
#include "src/tint/type/array.h"
#include "src/tint/type/atomic.h"
#include "src/tint/type/depth_multisampled_texture.h"
#include "src/tint/type/depth_texture.h"
#include "src/tint/type/multisampled_texture.h"
#include "src/tint/type/sampled_texture.h"
#include "src/tint/type/storage_texture.h"
#include "src/tint/type/texture_dimension.h"
#include "src/tint/utils/defer.h"
#include "src/tint/utils/map.h"
#include "src/tint/utils/scoped_assignment.h"
#include "src/tint/utils/string.h"
#include "src/tint/utils/string_stream.h"
#include "src/tint/writer/append_vector.h"
#include "src/tint/writer/float_to_string.h"
using namespace tint::number_suffixes; // NOLINT
namespace tint::writer::glsl {
namespace {
const char kTempNamePrefix[] = "tint_tmp";
bool last_is_break(const ast::BlockStatement* stmts) {
return IsAnyOf<ast::BreakStatement>(stmts->Last());
}
bool IsRelational(tint::ast::BinaryOp op) {
return op == tint::ast::BinaryOp::kEqual || op == tint::ast::BinaryOp::kNotEqual ||
op == tint::ast::BinaryOp::kLessThan || op == tint::ast::BinaryOp::kGreaterThan ||
op == tint::ast::BinaryOp::kLessThanEqual ||
op == tint::ast::BinaryOp::kGreaterThanEqual;
}
bool RequiresOESSampleVariables(tint::builtin::BuiltinValue builtin) {
switch (builtin) {
case tint::builtin::BuiltinValue::kSampleIndex:
case tint::builtin::BuiltinValue::kSampleMask:
return true;
default:
return false;
}
}
void PrintI32(utils::StringStream& out, int32_t value) {
// GLSL parses `-2147483648` as a unary minus and `2147483648` as separate tokens, and the
// latter doesn't fit into an (32-bit) `int`. Emit `(-2147483647 - 1)` instead, which ensures
// the expression type is `int`.
if (auto int_min = std::numeric_limits<int32_t>::min(); value == int_min) {
out << "(" << int_min + 1 << " - 1)";
} else {
out << value;
}
}
void PrintF32(utils::StringStream& out, float value) {
if (std::isinf(value)) {
out << "0.0f " << (value >= 0 ? "/* inf */" : "/* -inf */");
} else if (std::isnan(value)) {
out << "0.0f /* nan */";
} else {
out << FloatToString(value) << "f";
}
}
void PrintF16(utils::StringStream& out, float value) {
if (std::isinf(value)) {
out << "0.0hf " << (value >= 0 ? "/* inf */" : "/* -inf */");
} else if (std::isnan(value)) {
out << "0.0hf /* nan */";
} else {
out << FloatToString(value) << "hf";
}
}
} // namespace
SanitizedResult::SanitizedResult() = default;
SanitizedResult::~SanitizedResult() = default;
SanitizedResult::SanitizedResult(SanitizedResult&&) = default;
SanitizedResult Sanitize(const Program* in,
const Options& options,
const std::string& entry_point) {
transform::Manager manager;
transform::DataMap data;
manager.Add<transform::DisableUniformityAnalysis>();
// ExpandCompoundAssignment must come before BuiltinPolyfill
manager.Add<transform::ExpandCompoundAssignment>();
if (!entry_point.empty()) {
manager.Add<transform::SingleEntryPoint>();
data.Add<transform::SingleEntryPoint::Config>(entry_point);
}
manager.Add<transform::Renamer>();
data.Add<transform::Renamer::Config>(transform::Renamer::Target::kGlslKeywords,
/* preserve_unicode */ false);
manager.Add<transform::PreservePadding>(); // Must come before DirectVariableAccess
manager.Add<transform::Unshadow>(); // Must come before DirectVariableAccess
manager.Add<transform::DirectVariableAccess>();
manager.Add<transform::PromoteSideEffectsToDecl>();
if (!options.disable_robustness) {
// Robustness must come after PromoteSideEffectsToDecl
// Robustness must come before BuiltinPolyfill and CanonicalizeEntryPointIO
manager.Add<transform::Robustness>();
}
// Note: it is more efficient for MultiplanarExternalTexture to come after Robustness
data.Add<transform::MultiplanarExternalTexture::NewBindingPoints>(
options.external_texture_options.bindings_map);
manager.Add<transform::MultiplanarExternalTexture>();
{ // Builtin polyfills
transform::BuiltinPolyfill::Builtins polyfills;
polyfills.acosh = transform::BuiltinPolyfill::Level::kRangeCheck;
polyfills.atanh = transform::BuiltinPolyfill::Level::kRangeCheck;
polyfills.bgra8unorm = true;
polyfills.bitshift_modulo = true;
polyfills.conv_f32_to_iu32 = true;
polyfills.count_leading_zeros = true;
polyfills.count_trailing_zeros = true;
polyfills.extract_bits = transform::BuiltinPolyfill::Level::kClampParameters;
polyfills.first_leading_bit = true;
polyfills.first_trailing_bit = true;
polyfills.insert_bits = transform::BuiltinPolyfill::Level::kClampParameters;
polyfills.int_div_mod = true;
polyfills.saturate = true;
polyfills.texture_sample_base_clamp_to_edge_2d_f32 = true;
polyfills.workgroup_uniform_load = true;
data.Add<transform::BuiltinPolyfill::Config>(polyfills);
manager.Add<transform::BuiltinPolyfill>();
}
if (!options.disable_workgroup_init) {
// ZeroInitWorkgroupMemory must come before CanonicalizeEntryPointIO as
// ZeroInitWorkgroupMemory may inject new builtin parameters.
manager.Add<transform::ZeroInitWorkgroupMemory>();
}
// CanonicalizeEntryPointIO must come after Robustness
manager.Add<transform::CanonicalizeEntryPointIO>();
// PadStructs must come after CanonicalizeEntryPointIO
manager.Add<transform::PadStructs>();
// DemoteToHelper must come after PromoteSideEffectsToDecl and ExpandCompoundAssignment.
manager.Add<transform::DemoteToHelper>();
manager.Add<transform::RemovePhonies>();
data.Add<transform::CombineSamplers::BindingInfo>(options.binding_map,
options.placeholder_binding_point);
manager.Add<transform::CombineSamplers>();
data.Add<transform::BindingRemapper::Remappings>(
options.binding_points, options.access_controls, options.allow_collisions);
manager.Add<transform::BindingRemapper>();
manager.Add<transform::PromoteInitializersToLet>();
manager.Add<transform::AddEmptyEntryPoint>();
manager.Add<transform::AddBlockAttribute>();
// Std140 must come after PromoteSideEffectsToDecl and before SimplifyPointers.
manager.Add<transform::Std140>();
manager.Add<transform::Texture1DTo2D>();
manager.Add<transform::SimplifyPointers>();
data.Add<transform::CanonicalizeEntryPointIO::Config>(
transform::CanonicalizeEntryPointIO::ShaderStyle::kGlsl);
auto out = manager.Run(in, data);
SanitizedResult result;
result.program = std::move(out.program);
return result;
}
GeneratorImpl::GeneratorImpl(const Program* program, const Version& version)
: TextGenerator(program), version_(version) {}
GeneratorImpl::~GeneratorImpl() = default;
bool GeneratorImpl::Generate() {
{
auto out = line();
out << "#version " << version_.major_version << version_.minor_version << "0";
if (version_.IsES()) {
out << " es";
}
}
auto helpers_insertion_point = current_buffer_->lines.size();
line();
auto* mod = builder_.Sem().Module();
for (auto* decl : mod->DependencyOrderedDeclarations()) {
if (decl->IsAnyOf<ast::Alias, ast::ConstAssert, ast::DiagnosticDirective>()) {
continue; // These are not emitted.
}
bool ok = Switch(
decl, //
[&](const ast::Variable* global) { return EmitGlobalVariable(global); },
[&](const ast::Struct* str) {
auto* sem = builder_.Sem().Get(str);
bool has_rt_arr = false;
if (auto* arr = sem->Members().Back()->Type()->As<type::Array>()) {
has_rt_arr = arr->Count()->Is<type::RuntimeArrayCount>();
}
bool is_block = ast::HasAttribute<transform::AddBlockAttribute::BlockAttribute>(
str->attributes);
if (!has_rt_arr && !is_block) {
if (!EmitStructType(current_buffer_, sem)) {
return false;
}
}
return true;
},
[&](const ast::Function* func) {
if (func->IsEntryPoint()) {
return EmitEntryPointFunction(func);
}
return EmitFunction(func);
},
[&](const ast::Enable* enable) {
// Record the required extension for generating extension directive later
return RecordExtension(enable);
},
[&](Default) {
TINT_ICE(Writer, diagnostics_)
<< "unhandled module-scope declaration: " << decl->TypeInfo().name;
return false;
});
if (TINT_UNLIKELY(!ok)) {
return false;
}
}
TextBuffer extensions;
if (version_.IsES() && requires_oes_sample_variables_) {
extensions.Append("#extension GL_OES_sample_variables : require");
}
if (requires_f16_extension_) {
extensions.Append("#extension GL_AMD_gpu_shader_half_float : require");
}
auto indent = current_buffer_->current_indent;
if (!extensions.lines.empty()) {
current_buffer_->Insert(extensions, helpers_insertion_point, indent);
helpers_insertion_point += extensions.lines.size();
}
if (version_.IsES() && requires_default_precision_qualifier_) {
current_buffer_->Insert("precision highp float;", helpers_insertion_point++, indent);
}
if (!helpers_.lines.empty()) {
current_buffer_->Insert("", helpers_insertion_point++, indent);
current_buffer_->Insert(helpers_, helpers_insertion_point, indent);
helpers_insertion_point += helpers_.lines.size();
}
return true;
}
bool GeneratorImpl::RecordExtension(const ast::Enable* enable) {
// Deal with extension node here, recording it within the generator for later emition.
if (enable->HasExtension(builtin::Extension::kF16)) {
requires_f16_extension_ = true;
}
return true;
}
bool GeneratorImpl::EmitIndexAccessor(utils::StringStream& out,
const ast::IndexAccessorExpression* expr) {
if (!EmitExpression(out, expr->object)) {
return false;
}
out << "[";
if (!EmitExpression(out, expr->index)) {
return false;
}
out << "]";
return true;
}
bool GeneratorImpl::EmitBitcast(utils::StringStream& out, const ast::BitcastExpression* expr) {
auto* src_type = TypeOf(expr->expr)->UnwrapRef();
auto* dst_type = TypeOf(expr)->UnwrapRef();
if (!dst_type->is_integer_scalar_or_vector() && !dst_type->is_float_scalar_or_vector()) {
diagnostics_.add_error(
diag::System::Writer,
"Unable to do bitcast to type " + dst_type->FriendlyName(builder_.Symbols()));
return false;
}
if (src_type == dst_type) {
return EmitExpression(out, expr->expr);
}
if (src_type->is_float_scalar_or_vector() && dst_type->is_signed_integer_scalar_or_vector()) {
out << "floatBitsToInt";
} else if (src_type->is_float_scalar_or_vector() &&
dst_type->is_unsigned_integer_scalar_or_vector()) {
out << "floatBitsToUint";
} else if (src_type->is_signed_integer_scalar_or_vector() &&
dst_type->is_float_scalar_or_vector()) {
out << "intBitsToFloat";
} else if (src_type->is_unsigned_integer_scalar_or_vector() &&
dst_type->is_float_scalar_or_vector()) {
out << "uintBitsToFloat";
} else {
if (!EmitType(out, dst_type, builtin::AddressSpace::kUndefined, builtin::Access::kReadWrite,
"")) {
return false;
}
}
ScopedParen sp(out);
if (!EmitExpression(out, expr->expr)) {
return false;
}
return true;
}
bool GeneratorImpl::EmitAssign(const ast::AssignmentStatement* stmt) {
auto out = line();
if (!EmitExpression(out, stmt->lhs)) {
return false;
}
out << " = ";
if (!EmitExpression(out, stmt->rhs)) {
return false;
}
out << ";";
return true;
}
bool GeneratorImpl::EmitVectorRelational(utils::StringStream& out,
const ast::BinaryExpression* expr) {
switch (expr->op) {
case ast::BinaryOp::kEqual:
out << "equal";
break;
case ast::BinaryOp::kNotEqual:
out << "notEqual";
break;
case ast::BinaryOp::kLessThan:
out << "lessThan";
break;
case ast::BinaryOp::kGreaterThan:
out << "greaterThan";
break;
case ast::BinaryOp::kLessThanEqual:
out << "lessThanEqual";
break;
case ast::BinaryOp::kGreaterThanEqual:
out << "greaterThanEqual";
break;
default:
break;
}
ScopedParen sp(out);
if (!EmitExpression(out, expr->lhs)) {
return false;
}
out << ", ";
if (!EmitExpression(out, expr->rhs)) {
return false;
}
return true;
}
bool GeneratorImpl::EmitBitwiseBoolOp(utils::StringStream& out, const ast::BinaryExpression* expr) {
auto* bool_type = TypeOf(expr->lhs)->UnwrapRef();
auto* uint_type = BoolTypeToUint(bool_type);
// Cast result to bool scalar or vector type.
if (!EmitType(out, bool_type, builtin::AddressSpace::kUndefined, builtin::Access::kReadWrite,
"")) {
return false;
}
ScopedParen outerCastParen(out);
// Cast LHS to uint scalar or vector type.
if (!EmitType(out, uint_type, builtin::AddressSpace::kUndefined, builtin::Access::kReadWrite,
"")) {
return false;
}
{
ScopedParen innerCastParen(out);
// Emit LHS.
if (!EmitExpression(out, expr->lhs)) {
return false;
}
}
// Emit operator.
if (expr->op == ast::BinaryOp::kAnd) {
out << " & ";
} else if (TINT_LIKELY(expr->op == ast::BinaryOp::kOr)) {
out << " | ";
} else {
TINT_ICE(Writer, diagnostics_) << "unexpected binary op: " << FriendlyName(expr->op);
return false;
}
// Cast RHS to uint scalar or vector type.
if (!EmitType(out, uint_type, builtin::AddressSpace::kUndefined, builtin::Access::kReadWrite,
"")) {
return false;
}
{
ScopedParen innerCastParen(out);
// Emit RHS.
if (!EmitExpression(out, expr->rhs)) {
return false;
}
}
return true;
}
bool GeneratorImpl::EmitFloatModulo(utils::StringStream& out, const ast::BinaryExpression* expr) {
std::string fn;
auto* ret_ty = TypeOf(expr)->UnwrapRef();
auto* lhs_ty = TypeOf(expr->lhs)->UnwrapRef();
auto* rhs_ty = TypeOf(expr->rhs)->UnwrapRef();
fn = utils::GetOrCreate(
float_modulo_funcs_, BinaryOperandType{{lhs_ty, rhs_ty}}, [&]() -> std::string {
TextBuffer b;
TINT_DEFER(helpers_.Append(b));
auto fn_name = UniqueIdentifier("tint_float_modulo");
std::vector<std::string> parameter_names;
{
auto decl = line(&b);
if (!EmitTypeAndName(decl, ret_ty, builtin::AddressSpace::kUndefined,
builtin::Access::kUndefined, fn_name)) {
return "";
}
{
ScopedParen sp(decl);
const auto* ty = TypeOf(expr->lhs)->UnwrapRef();
if (!EmitTypeAndName(decl, ty, builtin::AddressSpace::kUndefined,
builtin::Access::kUndefined, "lhs")) {
return "";
}
decl << ", ";
ty = TypeOf(expr->rhs)->UnwrapRef();
if (!EmitTypeAndName(decl, ty, builtin::AddressSpace::kUndefined,
builtin::Access::kUndefined, "rhs")) {
return "";
}
}
decl << " {";
}
{
ScopedIndent si(&b);
line(&b) << "return (lhs - rhs * trunc(lhs / rhs));";
}
line(&b) << "}";
line(&b);
return fn_name;
});
if (fn.empty()) {
return false;
}
// Call the helper
out << fn;
{
ScopedParen sp(out);
if (!EmitExpression(out, expr->lhs)) {
return false;
}
out << ", ";
if (!EmitExpression(out, expr->rhs)) {
return false;
}
}
return true;
}
bool GeneratorImpl::EmitBinary(utils::StringStream& out, const ast::BinaryExpression* expr) {
if (IsRelational(expr->op) && !TypeOf(expr->lhs)->UnwrapRef()->is_scalar()) {
return EmitVectorRelational(out, expr);
}
if (expr->op == ast::BinaryOp::kLogicalAnd || expr->op == ast::BinaryOp::kLogicalOr) {
auto name = UniqueIdentifier(kTempNamePrefix);
{
auto pre = line();
pre << "bool " << name << " = ";
if (!EmitExpression(pre, expr->lhs)) {
return false;
}
pre << ";";
}
if (expr->op == ast::BinaryOp::kLogicalOr) {
line() << "if (!" << name << ") {";
} else {
line() << "if (" << name << ") {";
}
{
ScopedIndent si(this);
auto pre = line();
pre << name << " = ";
if (!EmitExpression(pre, expr->rhs)) {
return false;
}
pre << ";";
}
line() << "}";
out << "(" << name << ")";
return true;
}
if ((expr->op == ast::BinaryOp::kAnd || expr->op == ast::BinaryOp::kOr) &&
TypeOf(expr->lhs)->UnwrapRef()->is_bool_scalar_or_vector()) {
return EmitBitwiseBoolOp(out, expr);
}
if (expr->op == ast::BinaryOp::kModulo &&
(TypeOf(expr->lhs)->UnwrapRef()->is_float_scalar_or_vector() ||
TypeOf(expr->rhs)->UnwrapRef()->is_float_scalar_or_vector())) {
return EmitFloatModulo(out, expr);
}
ScopedParen sp(out);
if (!EmitExpression(out, expr->lhs)) {
return false;
}
out << " ";
switch (expr->op) {
case ast::BinaryOp::kAnd:
out << "&";
break;
case ast::BinaryOp::kOr:
out << "|";
break;
case ast::BinaryOp::kXor:
out << "^";
break;
case ast::BinaryOp::kLogicalAnd:
case ast::BinaryOp::kLogicalOr: {
// These are both handled above.
TINT_UNREACHABLE(Writer, diagnostics_);
return false;
}
case ast::BinaryOp::kEqual:
out << "==";
break;
case ast::BinaryOp::kNotEqual:
out << "!=";
break;
case ast::BinaryOp::kLessThan:
out << "<";
break;
case ast::BinaryOp::kGreaterThan:
out << ">";
break;
case ast::BinaryOp::kLessThanEqual:
out << "<=";
break;
case ast::BinaryOp::kGreaterThanEqual:
out << ">=";
break;
case ast::BinaryOp::kShiftLeft:
out << "<<";
break;
case ast::BinaryOp::kShiftRight:
// TODO(dsinclair): MSL is based on C++14, and >> in C++14 has
// implementation-defined behaviour for negative LHS. We may have to
// generate extra code to implement WGSL-specified behaviour for negative
// LHS.
out << R"(>>)";
break;
case ast::BinaryOp::kAdd:
out << "+";
break;
case ast::BinaryOp::kSubtract:
out << "-";
break;
case ast::BinaryOp::kMultiply:
out << "*";
break;
case ast::BinaryOp::kDivide:
out << "/";
break;
case ast::BinaryOp::kModulo:
out << "%";
break;
case ast::BinaryOp::kNone:
diagnostics_.add_error(diag::System::Writer, "missing binary operation type");
return false;
}
out << " ";
if (!EmitExpression(out, expr->rhs)) {
return false;
}
return true;
}
bool GeneratorImpl::EmitStatements(utils::VectorRef<const ast::Statement*> stmts) {
for (auto* s : stmts) {
if (!EmitStatement(s)) {
return false;
}
}
return true;
}
bool GeneratorImpl::EmitStatementsWithIndent(utils::VectorRef<const ast::Statement*> stmts) {
ScopedIndent si(this);
return EmitStatements(stmts);
}
bool GeneratorImpl::EmitBlock(const ast::BlockStatement* stmt) {
line() << "{";
if (!EmitStatementsWithIndent(stmt->statements)) {
return false;
}
line() << "}";
return true;
}
bool GeneratorImpl::EmitBreak(const ast::BreakStatement*) {
line() << "break;";
return true;
}
bool GeneratorImpl::EmitBreakIf(const ast::BreakIfStatement* b) {
auto out = line();
out << "if (";
if (!EmitExpression(out, b->condition)) {
return false;
}
out << ") { break; }";
return true;
}
bool GeneratorImpl::EmitCall(utils::StringStream& out, const ast::CallExpression* expr) {
auto* call = builder_.Sem().Get<sem::Call>(expr);
return Switch(
call->Target(), //
[&](const sem::Function* fn) { return EmitFunctionCall(out, call, fn); },
[&](const sem::Builtin* builtin) { return EmitBuiltinCall(out, call, builtin); },
[&](const sem::ValueConversion* conv) { return EmitValueConversion(out, call, conv); },
[&](const sem::ValueConstructor* ctor) { return EmitValueConstructor(out, call, ctor); },
[&](Default) {
TINT_ICE(Writer, diagnostics_)
<< "unhandled call target: " << call->Target()->TypeInfo().name;
return false;
});
}
bool GeneratorImpl::EmitFunctionCall(utils::StringStream& out,
const sem::Call* call,
const sem::Function* fn) {
const auto& args = call->Arguments();
auto* ident = fn->Declaration()->name;
out << builder_.Symbols().NameFor(ident->symbol);
ScopedParen sp(out);
bool first = true;
for (auto* arg : args) {
if (!first) {
out << ", ";
}
first = false;
if (!EmitExpression(out, arg->Declaration())) {
return false;
}
}
return true;
}
bool GeneratorImpl::EmitBuiltinCall(utils::StringStream& out,
const sem::Call* call,
const sem::Builtin* builtin) {
auto* expr = call->Declaration();
if (builtin->IsTexture()) {
return EmitTextureCall(out, call, builtin);
}
if (builtin->Type() == builtin::Function::kCountOneBits) {
return EmitCountOneBitsCall(out, expr);
}
if (builtin->Type() == builtin::Function::kSelect) {
return EmitSelectCall(out, expr, builtin);
}
if (builtin->Type() == builtin::Function::kDot) {
return EmitDotCall(out, expr, builtin);
}
if (builtin->Type() == builtin::Function::kModf) {
return EmitModfCall(out, expr, builtin);
}
if (builtin->Type() == builtin::Function::kFrexp) {
return EmitFrexpCall(out, expr, builtin);
}
if (builtin->Type() == builtin::Function::kDegrees) {
return EmitDegreesCall(out, expr, builtin);
}
if (builtin->Type() == builtin::Function::kRadians) {
return EmitRadiansCall(out, expr, builtin);
}
if (builtin->Type() == builtin::Function::kQuantizeToF16) {
return EmitQuantizeToF16Call(out, expr, builtin);
}
if (builtin->Type() == builtin::Function::kArrayLength) {
return EmitArrayLength(out, expr);
}
if (builtin->Type() == builtin::Function::kExtractBits) {
return EmitExtractBits(out, expr);
}
if (builtin->Type() == builtin::Function::kInsertBits) {
return EmitInsertBits(out, expr);
}
if (builtin->Type() == builtin::Function::kFma && version_.IsES()) {
return EmitEmulatedFMA(out, expr);
}
if (builtin->Type() == builtin::Function::kAbs &&
TypeOf(expr->args[0])->UnwrapRef()->is_unsigned_integer_scalar_or_vector()) {
// GLSL does not support abs() on unsigned arguments. However, it's a no-op.
return EmitExpression(out, expr->args[0]);
}
if ((builtin->Type() == builtin::Function::kAny ||
builtin->Type() == builtin::Function::kAll) &&
TypeOf(expr->args[0])->UnwrapRef()->is_scalar()) {
// GLSL does not support any() or all() on scalar arguments. It's a no-op.
return EmitExpression(out, expr->args[0]);
}
if (builtin->IsBarrier()) {
return EmitBarrierCall(out, builtin);
}
if (builtin->IsAtomic()) {
return EmitWorkgroupAtomicCall(out, expr, builtin);
}
auto name = generate_builtin_name(builtin);
if (name.empty()) {
return false;
}
out << name;
ScopedParen sp(out);
bool first = true;
for (auto* arg : call->Arguments()) {
if (!first) {
out << ", ";
}
first = false;
if (!EmitExpression(out, arg->Declaration())) {
return false;
}
}
return true;
}
bool GeneratorImpl::EmitValueConversion(utils::StringStream& out,
const sem::Call* call,
const sem::ValueConversion* conv) {
if (!EmitType(out, conv->Target(), builtin::AddressSpace::kUndefined,
builtin::Access::kReadWrite, "")) {
return false;
}
ScopedParen sp(out);
if (!EmitExpression(out, call->Arguments()[0]->Declaration())) {
return false;
}
return true;
}
bool GeneratorImpl::EmitValueConstructor(utils::StringStream& out,
const sem::Call* call,
const sem::ValueConstructor* ctor) {
auto* type = ctor->ReturnType();
// If the value constructor is empty then we need to construct with the zero value for all
// components.
if (call->Arguments().IsEmpty()) {
return EmitZeroValue(out, type);
}
if (!EmitType(out, type, builtin::AddressSpace::kUndefined, builtin::Access::kReadWrite, "")) {
return false;
}
ScopedParen sp(out);
bool first = true;
for (auto* arg : call->Arguments()) {
if (!first) {
out << ", ";
}
first = false;
if (!EmitExpression(out, arg->Declaration())) {
return false;
}
}
return true;
}
bool GeneratorImpl::EmitWorkgroupAtomicCall(utils::StringStream& out,
const ast::CallExpression* expr,
const sem::Builtin* builtin) {
auto call = [&](const char* name) {
out << name;
{
ScopedParen sp(out);
for (size_t i = 0; i < expr->args.Length(); i++) {
auto* arg = expr->args[i];
if (i > 0) {
out << ", ";
}
if (!EmitExpression(out, arg)) {
return false;
}
}
}
return true;
};
switch (builtin->Type()) {
case builtin::Function::kAtomicLoad: {
// GLSL does not have an atomicLoad, so we emulate it with
// atomicOr using 0 as the OR value
out << "atomicOr";
{
ScopedParen sp(out);
if (!EmitExpression(out, expr->args[0])) {
return false;
}
out << ", 0";
if (builtin->ReturnType()->Is<type::U32>()) {
out << "u";
}
}
return true;
}
case builtin::Function::kAtomicCompareExchangeWeak: {
if (!EmitStructType(&helpers_, builtin->ReturnType()->As<sem::Struct>())) {
return false;
}
auto* dest = expr->args[0];
auto* compare_value = expr->args[1];
auto* value = expr->args[2];
std::string result = UniqueIdentifier("atomic_compare_result");
{
auto pre = line();
if (!EmitTypeAndName(pre, builtin->ReturnType(), builtin::AddressSpace::kUndefined,
builtin::Access::kUndefined, result)) {
return false;
}
pre << ";";
}
{
auto pre = line();
pre << result << ".old_value = atomicCompSwap";
{
ScopedParen sp(pre);
if (!EmitExpression(pre, dest)) {
return false;
}
pre << ", ";
if (!EmitExpression(pre, compare_value)) {
return false;
}
pre << ", ";
if (!EmitExpression(pre, value)) {
return false;
}
}
pre << ";";
}
{
auto pre = line();
pre << result << ".exchanged = " << result << ".old_value == ";
if (!EmitExpression(pre, compare_value)) {
return false;
}
pre << ";";
}
out << result;
return true;
}
case builtin::Function::kAtomicAdd:
case builtin::Function::kAtomicSub:
return call("atomicAdd");
case builtin::Function::kAtomicMax:
return call("atomicMax");
case builtin::Function::kAtomicMin:
return call("atomicMin");
case builtin::Function::kAtomicAnd:
return call("atomicAnd");
case builtin::Function::kAtomicOr:
return call("atomicOr");
case builtin::Function::kAtomicXor:
return call("atomicXor");
case builtin::Function::kAtomicExchange:
case builtin::Function::kAtomicStore:
// GLSL does not have an atomicStore, so we emulate it with
// atomicExchange.
return call("atomicExchange");
default:
break;
}
TINT_UNREACHABLE(Writer, diagnostics_) << "unsupported atomic builtin: " << builtin->Type();
return false;
}
bool GeneratorImpl::EmitArrayLength(utils::StringStream& out, const ast::CallExpression* expr) {
out << "uint(";
if (!EmitExpression(out, expr->args[0])) {
return false;
}
out << ".length())";
return true;
}
bool GeneratorImpl::EmitExtractBits(utils::StringStream& out, const ast::CallExpression* expr) {
out << "bitfieldExtract(";
if (!EmitExpression(out, expr->args[0])) {
return false;
}
out << ", int(";
if (!EmitExpression(out, expr->args[1])) {
return false;
}
out << "), int(";
if (!EmitExpression(out, expr->args[2])) {
return false;
}
out << "))";
return true;
}
bool GeneratorImpl::EmitInsertBits(utils::StringStream& out, const ast::CallExpression* expr) {
out << "bitfieldInsert(";
if (!EmitExpression(out, expr->args[0])) {
return false;
}
out << ", ";
if (!EmitExpression(out, expr->args[1])) {
return false;
}
out << ", int(";
if (!EmitExpression(out, expr->args[2])) {
return false;
}
out << "), int(";
if (!EmitExpression(out, expr->args[3])) {
return false;
}
out << "))";
return true;
}
bool GeneratorImpl::EmitEmulatedFMA(utils::StringStream& out, const ast::CallExpression* expr) {
out << "((";
if (!EmitExpression(out, expr->args[0])) {
return false;
}
out << ") * (";
if (!EmitExpression(out, expr->args[1])) {
return false;
}
out << ") + (";
if (!EmitExpression(out, expr->args[2])) {
return false;
}
out << "))";
return true;
}
bool GeneratorImpl::EmitCountOneBitsCall(utils::StringStream& out,
const ast::CallExpression* expr) {
// GLSL's bitCount returns an integer type, so cast it to the appropriate
// unsigned type.
if (!EmitType(out, TypeOf(expr)->UnwrapRef(), builtin::AddressSpace::kUndefined,
builtin::Access::kReadWrite, "")) {
return false;
}
out << "(bitCount(";
if (!EmitExpression(out, expr->args[0])) {
return false;
}
out << "))";
return true;
}
bool GeneratorImpl::EmitSelectCall(utils::StringStream& out,
const ast::CallExpression* expr,
const sem::Builtin* builtin) {
// GLSL does not support ternary expressions with a bool vector conditional,
// so polyfill with a helper.
if (auto* vec = builtin->Parameters()[2]->Type()->As<type::Vector>()) {
return CallBuiltinHelper(
out, expr, builtin, [&](TextBuffer* b, const std::vector<std::string>& params) {
auto l = line(b);
l << " return ";
if (!EmitType(l, builtin->ReturnType(), builtin::AddressSpace::kUndefined,
builtin::Access::kUndefined, "")) {
return false;
}
{
ScopedParen sp(l);
for (uint32_t i = 0; i < vec->Width(); i++) {
if (i > 0) {
l << ", ";
}
l << params[2] << "[" << i << "] ? " << params[1] << "[" << i
<< "] : " << params[0] << "[" << i << "]";
}
}
l << ";";
return true;
});
}
auto* expr_false = expr->args[0];
auto* expr_true = expr->args[1];
auto* expr_cond = expr->args[2];
ScopedParen paren(out);
if (!EmitExpression(out, expr_cond)) {
return false;
}
out << " ? ";
if (!EmitExpression(out, expr_true)) {
return false;
}
out << " : ";
if (!EmitExpression(out, expr_false)) {
return false;
}
return true;
}
bool GeneratorImpl::EmitDotCall(utils::StringStream& out,
const ast::CallExpression* expr,
const sem::Builtin* builtin) {
auto* vec_ty = builtin->Parameters()[0]->Type()->As<type::Vector>();
std::string fn = "dot";
if (vec_ty->type()->is_integer_scalar()) {
// GLSL does not have a builtin for dot() with integer vector types.
// Generate the helper function if it hasn't been created already
fn = utils::GetOrCreate(int_dot_funcs_, vec_ty, [&]() -> std::string {
TextBuffer b;
TINT_DEFER(helpers_.Append(b));
auto fn_name = UniqueIdentifier("tint_int_dot");
std::string v;
{
utils::StringStream s;
if (!EmitType(s, vec_ty->type(), builtin::AddressSpace::kUndefined,
builtin::Access::kRead, "")) {
return "";
}
v = s.str();
}
{ // (u)int tint_int_dot([i|u]vecN a, [i|u]vecN b) {
auto l = line(&b);
if (!EmitType(l, vec_ty->type(), builtin::AddressSpace::kUndefined,
builtin::Access::kRead, "")) {
return "";
}
l << " " << fn_name << "(";
if (!EmitType(l, vec_ty, builtin::AddressSpace::kUndefined, builtin::Access::kRead,
"")) {
return "";
}
l << " a, ";
if (!EmitType(l, vec_ty, builtin::AddressSpace::kUndefined, builtin::Access::kRead,
"")) {
return "";
}
l << " b) {";
}
{
auto l = line(&b);
l << " return ";
for (uint32_t i = 0; i < vec_ty->Width(); i++) {
if (i > 0) {
l << " + ";
}
l << "a[" << i << "]*b[" << i << "]";
}
l << ";";
}
line(&b) << "}";
return fn_name;
});
if (fn.empty()) {
return false;
}
}
out << fn;
ScopedParen sp(out);
if (!EmitExpression(out, expr->args[0])) {
return false;
}
out << ", ";
if (!EmitExpression(out, expr->args[1])) {
return false;
}
return true;
}
bool GeneratorImpl::EmitModfCall(utils::StringStream& out,
const ast::CallExpression* expr,
const sem::Builtin* builtin) {
TINT_ASSERT(Writer, expr->args.Length() == 1);
return CallBuiltinHelper(
out, expr, builtin, [&](TextBuffer* b, const std::vector<std::string>& params) {
// Emit the builtin return type unique to this overload. This does not
// exist in the AST, so it will not be generated in Generate().
if (!EmitStructType(&helpers_, builtin->ReturnType()->As<sem::Struct>())) {
return false;
}
{
auto l = line(b);
if (!EmitType(l, builtin->ReturnType(), builtin::AddressSpace::kUndefined,
builtin::Access::kUndefined, "")) {
return false;
}
l << " result;";
}
line(b) << "result.fract = modf(" << params[0] << ", result.whole);";
line(b) << "return result;";
return true;
});
}
bool GeneratorImpl::EmitFrexpCall(utils::StringStream& out,
const ast::CallExpression* expr,
const sem::Builtin* builtin) {
TINT_ASSERT(Writer, expr->args.Length() == 1);
return CallBuiltinHelper(
out, expr, builtin, [&](TextBuffer* b, const std::vector<std::string>& params) {
// Emit the builtin return type unique to this overload. This does not
// exist in the AST, so it will not be generated in Generate().
if (!EmitStructType(&helpers_, builtin->ReturnType()->As<sem::Struct>())) {
return false;
}
{
auto l = line(b);
if (!EmitType(l, builtin->ReturnType(), builtin::AddressSpace::kUndefined,
builtin::Access::kUndefined, "")) {
return false;
}
l << " result;";
}
line(b) << "result.fract = frexp(" << params[0] << ", result.exp);";
line(b) << "return result;";
return true;
});
}
bool GeneratorImpl::EmitDegreesCall(utils::StringStream& out,
const ast::CallExpression* expr,
const sem::Builtin* builtin) {
auto* return_elem_type = type::Type::DeepestElementOf(builtin->ReturnType());
const std::string suffix = Is<type::F16>(return_elem_type) ? "hf" : "f";
return CallBuiltinHelper(out, expr, builtin,
[&](TextBuffer* b, const std::vector<std::string>& params) {
line(b) << "return " << params[0] << " * " << std::setprecision(20)
<< sem::kRadToDeg << suffix << ";";
return true;
});
}
bool GeneratorImpl::EmitRadiansCall(utils::StringStream& out,
const ast::CallExpression* expr,
const sem::Builtin* builtin) {
auto* return_elem_type = type::Type::DeepestElementOf(builtin->ReturnType());
const std::string suffix = Is<type::F16>(return_elem_type) ? "hf" : "f";
return CallBuiltinHelper(out, expr, builtin,
[&](TextBuffer* b, const std::vector<std::string>& params) {
line(b) << "return " << params[0] << " * " << std::setprecision(20)
<< sem::kDegToRad << suffix << ";";
return true;
});
}
bool GeneratorImpl::EmitQuantizeToF16Call(utils::StringStream& out,
const ast::CallExpression* expr,
const sem::Builtin* builtin) {
// Emulate by casting to f16 and back again.
return CallBuiltinHelper(
out, expr, builtin, [&](TextBuffer* b, const std::vector<std::string>& params) {
const auto v = params[0];
if (auto* vec = builtin->ReturnType()->As<type::Vector>()) {
switch (vec->Width()) {
case 2: {
line(b) << "return unpackHalf2x16(packHalf2x16(" << v << "));";
return true;
}
case 3: {
line(b) << "return vec3(";
line(b) << " unpackHalf2x16(packHalf2x16(" << v << ".xy)),";
line(b) << " unpackHalf2x16(packHalf2x16(" << v << ".zz)).x);";
return true;
}
default: {
line(b) << "return vec4(";
line(b) << " unpackHalf2x16(packHalf2x16(" << v << ".xy)),";
line(b) << " unpackHalf2x16(packHalf2x16(" << v << ".zw)));";
return true;
}
}
}
line(b) << "return unpackHalf2x16(packHalf2x16(vec2(" << v << "))).x;";
return true;
});
}
bool GeneratorImpl::EmitBarrierCall(utils::StringStream& out, const sem::Builtin* builtin) {
// TODO(crbug.com/tint/661): Combine sequential barriers to a single
// instruction.
if (builtin->Type() == builtin::Function::kWorkgroupBarrier) {
out << "barrier()";
} else if (builtin->Type() == builtin::Function::kStorageBarrier) {
out << "{ barrier(); memoryBarrierBuffer(); }";
} else {
TINT_UNREACHABLE(Writer, diagnostics_)
<< "unexpected barrier builtin type " << builtin::str(builtin->Type());
return false;
}
return true;
}
const ast::Expression* GeneratorImpl::CreateF32Zero(const sem::Statement* stmt) {
auto* zero = builder_.Expr(0_f);
auto* f32 = builder_.create<type::F32>();
auto* sem_zero = builder_.create<sem::ValueExpression>(
zero, f32, sem::EvaluationStage::kRuntime, stmt, /* constant_value */ nullptr,
/* has_side_effects */ false);
builder_.Sem().Add(zero, sem_zero);
return zero;
}
bool GeneratorImpl::EmitTextureCall(utils::StringStream& out,
const sem::Call* call,
const sem::Builtin* builtin) {
using Usage = sem::ParameterUsage;
auto& signature = builtin->Signature();
auto* expr = call->Declaration();
auto arguments = expr->args;
// Returns the argument with the given usage
auto arg = [&](Usage usage) {
auto idx = signature.IndexOf(usage);
return (idx >= 0) ? arguments[static_cast<size_t>(idx)] : nullptr;
};
auto* texture = arg(Usage::kTexture);
if (TINT_UNLIKELY(!texture)) {
TINT_ICE(Writer, diagnostics_) << "missing texture argument";
return false;
}
auto* texture_type = TypeOf(texture)->UnwrapRef()->As<type::Texture>();
auto emit_signed_int_type = [&](const type::Type* ty) {
uint32_t width = 0;
type::Type::ElementOf(ty, &width);
if (width > 1) {
out << "ivec" << width;
} else {
out << "int";
}
};
auto emit_unsigned_int_type = [&](const type::Type* ty) {
uint32_t width = 0;
type::Type::ElementOf(ty, &width);
if (width > 1) {
out << "uvec" << width;
} else {
out << "uint";
}
};
auto emit_expr_as_signed = [&](const ast::Expression* e) {
auto* ty = TypeOf(e)->UnwrapRef();
if (!ty->is_unsigned_integer_scalar_or_vector()) {
return EmitExpression(out, e);
}
emit_signed_int_type(ty);
ScopedParen sp(out);
return EmitExpression(out, e);
};
switch (builtin->Type()) {
case builtin::Function::kTextureDimensions: {
// textureDimensions() returns an unsigned scalar / vector in WGSL.
// textureSize() / imageSize() returns a signed scalar / vector in GLSL.
// Cast.
emit_unsigned_int_type(call->Type());
ScopedParen sp(out);
if (texture_type->Is<type::StorageTexture>()) {
out << "imageSize(";
} else {
out << "textureSize(";
}
if (!EmitExpression(out, texture)) {
return false;
}
// The LOD parameter is mandatory on textureSize() for non-multisampled
// textures.
if (!texture_type->Is<type::StorageTexture>() &&
!texture_type->Is<type::MultisampledTexture>() &&
!texture_type->Is<type::DepthMultisampledTexture>()) {
out << ", ";
if (auto* level_arg = arg(Usage::kLevel)) {
if (!emit_expr_as_signed(level_arg)) {
return false;
}
} else {
out << "0";
}
}
out << ")";
// textureSize() on array samplers returns the array size in the
// final component, so strip it out.
if (texture_type->dim() == type::TextureDimension::k2dArray ||
texture_type->dim() == type::TextureDimension::kCubeArray) {
out << ".xy";
}
return true;
}
case builtin::Function::kTextureNumLayers: {
// textureNumLayers() returns an unsigned scalar in WGSL.
// textureSize() / imageSize() returns a signed scalar / vector in GLSL.
// Cast.
out << "uint";
ScopedParen sp(out);
if (texture_type->Is<type::StorageTexture>()) {
out << "imageSize(";
} else {
out << "textureSize(";
}
// textureSize() on sampler2dArray returns the array size in the
// final component, so return it
if (!EmitExpression(out, texture)) {
return false;
}
// The LOD parameter is mandatory on textureSize() for non-multisampled
// textures.
if (!texture_type->Is<type::StorageTexture>() &&
!texture_type->Is<type::MultisampledTexture>() &&
!texture_type->Is<type::DepthMultisampledTexture>()) {
out << ", ";
if (auto* level_arg = arg(Usage::kLevel)) {
if (!emit_expr_as_signed(level_arg)) {
return false;
}
} else {
out << "0";
}
}
out << ").z";
return true;
}
case builtin::Function::kTextureNumLevels: {
// textureNumLevels() returns an unsigned scalar in WGSL.
// textureQueryLevels() returns a signed scalar in GLSL.
// Cast.
out << "uint";
ScopedParen sp(out);
out << "textureQueryLevels(";
if (!EmitExpression(out, texture)) {
return false;
}
out << ")";
return true;
}
case builtin::Function::kTextureNumSamples: {
// textureNumSamples() returns an unsigned scalar in WGSL.
// textureSamples() returns a signed scalar in GLSL.
// Cast.
out << "uint";
ScopedParen sp(out);
out << "textureSamples(";
if (!EmitExpression(out, texture)) {
return false;
}
out << ")";
return true;
}
default:
break;
}
uint32_t glsl_ret_width = 4u;
bool append_depth_ref_to_coords = true;
bool is_depth = texture_type->Is<type::DepthTexture>();
switch (builtin->Type()) {
case builtin::Function::kTextureSample:
case builtin::Function::kTextureSampleBias:
out << "texture";
if (is_depth) {
glsl_ret_width = 1u;
}
break;
case builtin::Function::kTextureSampleLevel:
out << "textureLod";
if (is_depth) {
glsl_ret_width = 1u;
}
break;
case builtin::Function::kTextureGather:
case builtin::Function::kTextureGatherCompare:
out << "textureGather";
append_depth_ref_to_coords = false;
break;
case builtin::Function::kTextureSampleGrad:
out << "textureGrad";
break;
case builtin::Function::kTextureSampleCompare:
case builtin::Function::kTextureSampleCompareLevel:
out << "texture";
glsl_ret_width = 1;
break;
case builtin::Function::kTextureLoad:
out << "texelFetch";
break;
case builtin::Function::kTextureStore:
out << "imageStore";
break;
default:
diagnostics_.add_error(diag::System::Writer,
"Internal compiler error: Unhandled texture builtin '" +
std::string(builtin->str()) + "'");
return false;
}
if (builtin->Signature().IndexOf(sem::ParameterUsage::kOffset) >= 0) {
out << "Offset";
}
out << "(";
if (!EmitExpression(out, texture)) {
return false;
}
out << ", ";
auto* param_coords = arg(Usage::kCoords);
if (TINT_UNLIKELY(!param_coords)) {
TINT_ICE(Writer, diagnostics_) << "missing coords argument";
return false;
}
if (auto* array_index = arg(Usage::kArrayIndex)) {
// Array index needs to be appended to the coordinates.
param_coords = AppendVector(&builder_, param_coords, array_index)->Declaration();
}
// GLSL requires Dref to be appended to the coordinates, *unless* it's
// samplerCubeArrayShadow, in which case it will be handled as a separate
// parameter.
if (texture_type->dim() == type::TextureDimension::kCubeArray) {
append_depth_ref_to_coords = false;
}
if (is_depth && append_depth_ref_to_coords) {
auto* depth_ref = arg(Usage::kDepthRef);
if (!depth_ref) {
// Sampling a depth texture in GLSL always requires a depth reference, so
// append zero here.
depth_ref = CreateF32Zero(builder_.Sem().Get(param_coords)->Stmt());
}
param_coords = AppendVector(&builder_, param_coords, depth_ref)->Declaration();
}
if (!emit_expr_as_signed(param_coords)) {
return false;
}
for (auto usage : {Usage::kLevel, Usage::kDdx, Usage::kDdy, Usage::kSampleIndex}) {
if (auto* e = arg(usage)) {
out << ", ";
if (usage == Usage::kLevel && is_depth) {
// WGSL's textureSampleLevel() "level" param is i32 for depth textures,
// whereas GLSL's textureLod() "lod" param is always float, so cast it.
out << "float(";
if (!EmitExpression(out, e)) {
return false;
}
out << ")";
} else if (!emit_expr_as_signed(e)) {
return false;
}
}
}
if (auto* e = arg(Usage::kValue)) {
out << ", ";
if (!EmitExpression(out, e)) {
return false;
}
}
// GLSL's textureGather always requires a refZ parameter.
if (is_depth && builtin->Type() == builtin::Function::kTextureGather) {
out << ", 0.0";
}
// [1] samplerCubeArrayShadow requires a separate depthRef parameter
if (is_depth && !append_depth_ref_to_coords) {
if (auto* e = arg(Usage::kDepthRef)) {
out << ", ";
if (!EmitExpression(out, e)) {
return false;
}
} else if (builtin->Type() == builtin::Function::kTextureSample) {
out << ", 0.0f";
}
}
for (auto usage : {Usage::kOffset, Usage::kComponent, Usage::kBias}) {
if (auto* e = arg(usage)) {
out << ", ";
if (!emit_expr_as_signed(e)) {
return false;
}
}
}
out << ")";
if (builtin->ReturnType()->Is<type::Void>()) {
return true;
}
// If the builtin return type does not match the number of elements of the
// GLSL builtin, we need to swizzle the expression to generate the correct
// number of components.
uint32_t wgsl_ret_width = 1;
if (auto* vec = builtin->ReturnType()->As<type::Vector>()) {
wgsl_ret_width = vec->Width();
}
if (wgsl_ret_width < glsl_ret_width) {
out << ".";
for (uint32_t i = 0; i < wgsl_ret_width; i++) {
out << "xyz"[i];
}
}
if (TINT_UNLIKELY(wgsl_ret_width > glsl_ret_width)) {
TINT_ICE(Writer, diagnostics_)
<< "WGSL return width (" << wgsl_ret_width << ") is wider than GLSL return width ("
<< glsl_ret_width << ") for " << builtin->Type();
return false;
}
return true;
}
std::string GeneratorImpl::generate_builtin_name(const sem::Builtin* builtin) {
switch (builtin->Type()) {
case builtin::Function::kAbs:
case builtin::Function::kAcos:
case builtin::Function::kAcosh:
case builtin::Function::kAll:
case builtin::Function::kAny:
case builtin::Function::kAsin:
case builtin::Function::kAsinh:
case builtin::Function::kAtan:
case builtin::Function::kAtanh:
case builtin::Function::kCeil:
case builtin::Function::kClamp:
case builtin::Function::kCos:
case builtin::Function::kCosh:
case builtin::Function::kCross:
case builtin::Function::kDeterminant:
case builtin::Function::kDistance:
case builtin::Function::kDot:
case builtin::Function::kExp:
case builtin::Function::kExp2:
case builtin::Function::kFloor:
case builtin::Function::kFrexp:
case builtin::Function::kLdexp:
case builtin::Function::kLength:
case builtin::Function::kLog:
case builtin::Function::kLog2:
case builtin::Function::kMax:
case builtin::Function::kMin:
case builtin::Function::kModf:
case builtin::Function::kNormalize:
case builtin::Function::kPow:
case builtin::Function::kReflect:
case builtin::Function::kRefract:
case builtin::Function::kRound:
case builtin::Function::kSign:
case builtin::Function::kSin:
case builtin::Function::kSinh:
case builtin::Function::kSqrt:
case builtin::Function::kStep:
case builtin::Function::kTan:
case builtin::Function::kTanh:
case builtin::Function::kTranspose:
case builtin::Function::kTrunc:
return builtin->str();
case builtin::Function::kAtan2:
return "atan";
case builtin::Function::kCountOneBits:
return "bitCount";
case builtin::Function::kDpdx:
return "dFdx";
case builtin::Function::kDpdxCoarse:
if (version_.IsES()) {
return "dFdx";
}
return "dFdxCoarse";
case builtin::Function::kDpdxFine:
if (version_.IsES()) {
return "dFdx";
}
return "dFdxFine";
case builtin::Function::kDpdy:
return "dFdy";
case builtin::Function::kDpdyCoarse:
if (version_.IsES()) {
return "dFdy";
}
return "dFdyCoarse";
case builtin::Function::kDpdyFine:
if (version_.IsES()) {
return "dFdy";
}
return "dFdyFine";
case builtin::Function::kFaceForward:
return "faceforward";
case builtin::Function::kFract:
return "fract";
case builtin::Function::kFma:
return "fma";
case builtin::Function::kFwidth:
case builtin::Function::kFwidthCoarse:
case builtin::Function::kFwidthFine:
return "fwidth";
case builtin::Function::kInverseSqrt:
return "inversesqrt";
case builtin::Function::kMix:
return "mix";
case builtin::Function::kPack2X16Float:
return "packHalf2x16";
case builtin::Function::kPack2X16Snorm:
return "packSnorm2x16";
case builtin::Function::kPack2X16Unorm:
return "packUnorm2x16";
case builtin::Function::kPack4X8Snorm:
return "packSnorm4x8";
case builtin::Function::kPack4X8Unorm:
return "packUnorm4x8";
case builtin::Function::kReverseBits:
return "bitfieldReverse";
case builtin::Function::kSmoothstep:
return "smoothstep";
case builtin::Function::kUnpack2X16Float:
return "unpackHalf2x16";
case builtin::Function::kUnpack2X16Snorm:
return "unpackSnorm2x16";
case builtin::Function::kUnpack2X16Unorm:
return "unpackUnorm2x16";
case builtin::Function::kUnpack4X8Snorm:
return "unpackSnorm4x8";
case builtin::Function::kUnpack4X8Unorm:
return "unpackUnorm4x8";
default:
diagnostics_.add_error(diag::System::Writer,
"Unknown builtin method: " + std::string(builtin->str()));
}
return "";
}
bool GeneratorImpl::EmitCase(const ast::CaseStatement* stmt) {
auto* sem = builder_.Sem().Get<sem::CaseStatement>(stmt);
for (auto* selector : sem->Selectors()) {
auto out = line();
if (selector->IsDefault()) {
out << "default";
} else {
out << "case ";
if (!EmitConstant(out, selector->Value())) {
return false;
}
}
out << ":";
if (selector == sem->Selectors().back()) {
out << " {";
}
}
{
ScopedIndent si(this);
if (!EmitStatements(stmt->body->statements)) {
return false;
}
if (!last_is_break(stmt->body)) {
line() << "break;";
}
}
line() << "}";
return true;
}
bool GeneratorImpl::EmitContinue(const ast::ContinueStatement*) {
if (!emit_continuing_ || !emit_continuing_()) {
return false;
}
line() << "continue;";
return true;
}
bool GeneratorImpl::EmitDiscard(const ast::DiscardStatement*) {
// TODO(dsinclair): Verify this is correct when the discard semantics are
// defined for WGSL (https://github.com/gpuweb/gpuweb/issues/361)
line() << "discard;";
return true;
}
bool GeneratorImpl::EmitExpression(utils::StringStream& out, const ast::Expression* expr) {
if (auto* sem = builder_.Sem().GetVal(expr)) {
if (auto* constant = sem->ConstantValue()) {
return EmitConstant(out, constant);
}
}
return Switch(
expr, //
[&](const ast::IndexAccessorExpression* a) { return EmitIndexAccessor(out, a); },
[&](const ast::BinaryExpression* b) { return EmitBinary(out, b); },
[&](const ast::BitcastExpression* b) { return EmitBitcast(out, b); },
[&](const ast::CallExpression* c) { return EmitCall(out, c); },
[&](const ast::IdentifierExpression* i) { return EmitIdentifier(out, i); },
[&](const ast::LiteralExpression* l) { return EmitLiteral(out, l); },
[&](const ast::MemberAccessorExpression* m) { return EmitMemberAccessor(out, m); },
[&](const ast::UnaryOpExpression* u) { return EmitUnaryOp(out, u); },
[&](Default) { //
diagnostics_.add_error(diag::System::Writer, "unknown expression type: " +
std::string(expr->TypeInfo().name));
return false;
});
}
bool GeneratorImpl::EmitIdentifier(utils::StringStream& out,
const ast::IdentifierExpression* expr) {
out << builder_.Symbols().NameFor(expr->identifier->symbol);
return true;
}
bool GeneratorImpl::EmitIf(const ast::IfStatement* stmt) {
{
auto out = line();
out << "if (";
if (!EmitExpression(out, stmt->condition)) {
return false;
}
out << ") {";
}
if (!EmitStatementsWithIndent(stmt->body->statements)) {
return false;
}
if (stmt->else_statement) {
line() << "} else {";
if (auto* block = stmt->else_statement->As<ast::BlockStatement>()) {
if (!EmitStatementsWithIndent(block->statements)) {
return false;
}
} else {
if (!EmitStatementsWithIndent(utils::Vector{stmt->else_statement})) {
return false;
}
}
}
line() << "}";
return true;
}
bool GeneratorImpl::EmitFunction(const ast::Function* func) {
auto* sem = builder_.Sem().Get(func);
if (ast::HasAttribute<ast::InternalAttribute>(func->attributes)) {
// An internal function. Do not emit.
return true;
}
{
auto out = line();
auto name = builder_.Symbols().NameFor(func->name->symbol);
if (!EmitType(out, sem->ReturnType(), builtin::AddressSpace::kUndefined,
builtin::Access::kReadWrite, "")) {
return false;
}
out << " " << name << "(";
bool first = true;
for (auto* v : sem->Parameters()) {
if (!first) {
out << ", ";
}
first = false;
auto const* type = v->Type();
if (auto* ptr = type->As<type::Pointer>()) {
// Transform pointer parameters in to `inout` parameters.
// The WGSL spec is highly restrictive in what can be passed in pointer
// parameters, which allows for this transformation. See:
// https://gpuweb.github.io/gpuweb/wgsl/#function-restriction
out << "inout ";
type = ptr->StoreType();
}
// Note: WGSL only allows for AddressSpace::kUndefined on parameters, however
// the sanitizer transforms generates load / store functions for storage
// or uniform buffers. These functions have a buffer parameter with
// AddressSpace::kStorage or AddressSpace::kUniform. This is required to
// correctly translate the parameter to a [RW]ByteAddressBuffer for
// storage buffers and a uint4[N] for uniform buffers.
if (!EmitTypeAndName(out, type, v->AddressSpace(), v->Access(),
builder_.Symbols().NameFor(v->Declaration()->name->symbol))) {
return false;
}
}
out << ") {";
}
if (!EmitStatementsWithIndent(func->body->statements)) {
return false;
}
line() << "}";
line();
return true;
}
bool GeneratorImpl::EmitGlobalVariable(const ast::Variable* global) {
return Switch(
global, //
[&](const ast::Var* var) {
auto* sem = builder_.Sem().Get<sem::GlobalVariable>(global);
switch (sem->AddressSpace()) {
case builtin::AddressSpace::kUniform:
return EmitUniformVariable(var, sem);
case builtin::AddressSpace::kStorage:
return EmitStorageVariable(var, sem);
case builtin::AddressSpace::kHandle:
return EmitHandleVariable(var, sem);
case builtin::AddressSpace::kPrivate:
return EmitPrivateVariable(sem);
case builtin::AddressSpace::kWorkgroup:
return EmitWorkgroupVariable(sem);
case builtin::AddressSpace::kIn:
case builtin::AddressSpace::kOut:
return EmitIOVariable(sem);
case builtin::AddressSpace::kPushConstant:
diagnostics_.add_error(
diag::System::Writer,
"unhandled address space " + utils::ToString(sem->AddressSpace()));
return false;
default: {
TINT_ICE(Writer, diagnostics_)
<< "unhandled address space " << sem->AddressSpace();
return false;
}
}
},
[&](const ast::Let* let) { return EmitProgramConstVariable(let); },
[&](const ast::Override*) {
// Override is removed with SubstituteOverride
diagnostics_.add_error(diag::System::Writer,
"override-expressions should have been removed with the "
"SubstituteOverride transform");
return false;
},
[&](const ast::Const*) {
return true; // Constants are embedded at their use
},
[&](Default) {
TINT_ICE(Writer, diagnostics_)
<< "unhandled global variable type " << global->TypeInfo().name;
return false;
});
}
bool GeneratorImpl::EmitUniformVariable(const ast::Var* var, const sem::Variable* sem) {
auto* type = sem->Type()->UnwrapRef();
auto* str = type->As<sem::Struct>();
if (TINT_UNLIKELY(!str)) {
TINT_ICE(Writer, builder_.Diagnostics()) << "storage variable must be of struct type";
return false;
}
auto bp = sem->As<sem::GlobalVariable>()->BindingPoint();
{
auto out = line();
out << "layout(binding = " << bp.binding << ", std140";
out << ") uniform " << UniqueIdentifier(StructName(str) + "_ubo") << " {";
}
EmitStructMembers(current_buffer_, str);
auto name = builder_.Symbols().NameFor(var->name->symbol);
line() << "} " << name << ";";
line();
return true;
}
bool GeneratorImpl::EmitStorageVariable(const ast::Var* var, const sem::Variable* sem) {
auto* type = sem->Type()->UnwrapRef();
auto* str = type->As<sem::Struct>();
if (TINT_UNLIKELY(!str)) {
TINT_ICE(Writer, builder_.Diagnostics()) << "storage variable must be of struct type";
return false;
}
auto bp = sem->As<sem::GlobalVariable>()->BindingPoint();
line() << "layout(binding = " << bp.binding << ", std430) buffer "
<< UniqueIdentifier(StructName(str) + "_ssbo") << " {";
EmitStructMembers(current_buffer_, str);
auto name = builder_.Symbols().NameFor(var->name->symbol);
line() << "} " << name << ";";
line();
return true;
}
bool GeneratorImpl::EmitHandleVariable(const ast::Var* var, const sem::Variable* sem) {
auto out = line();
auto name = builder_.Symbols().NameFor(var->name->symbol);
auto* type = sem->Type()->UnwrapRef();
if (type->Is<type::Sampler>()) {
// GLSL ignores Sampler variables.
return true;
}
if (auto* storage = type->As<type::StorageTexture>()) {
out << "layout(";
switch (storage->texel_format()) {
case builtin::TexelFormat::kBgra8Unorm:
TINT_ICE(Writer, diagnostics_)
<< "bgra8unorm should have been polyfilled to rgba8unorm";
break;
case builtin::TexelFormat::kR32Uint:
out << "r32ui";
break;
case builtin::TexelFormat::kR32Sint:
out << "r32i";
break;
case builtin::TexelFormat::kR32Float:
out << "r32f";
break;
case builtin::TexelFormat::kRgba8Unorm:
out << "rgba8";
break;
case builtin::TexelFormat::kRgba8Snorm:
out << "rgba8_snorm";
break;
case builtin::TexelFormat::kRgba8Uint:
out << "rgba8ui";
break;
case builtin::TexelFormat::kRgba8Sint:
out << "rgba8i";
break;
case builtin::TexelFormat::kRg32Uint:
out << "rg32ui";
break;
case builtin::TexelFormat::kRg32Sint:
out << "rg32i";
break;
case builtin::TexelFormat::kRg32Float:
out << "rg32f";
break;
case builtin::TexelFormat::kRgba16Uint:
out << "rgba16ui";
break;
case builtin::TexelFormat::kRgba16Sint:
out << "rgba16i";
break;
case builtin::TexelFormat::kRgba16Float:
out << "rgba16f";
break;
case builtin::TexelFormat::kRgba32Uint:
out << "rgba32ui";
break;
case builtin::TexelFormat::kRgba32Sint:
out << "rgba32i";
break;
case builtin::TexelFormat::kRgba32Float:
out << "rgba32f";
break;
case builtin::TexelFormat::kUndefined:
TINT_ICE(Writer, diagnostics_) << "invalid texel format";
return false;
}
out << ") ";
}
if (!EmitTypeAndName(out, type, sem->AddressSpace(), sem->Access(), name)) {
return false;
}
out << ";";
return true;
}
bool GeneratorImpl::EmitPrivateVariable(const sem::Variable* var) {
auto* decl = var->Declaration();
auto out = line();
auto name = builder_.Symbols().NameFor(decl->name->symbol);
auto* type = var->Type()->UnwrapRef();
if (!EmitTypeAndName(out, type, var->AddressSpace(), var->Access(), name)) {
return false;
}
out << " = ";
if (auto* initializer = decl->initializer) {
if (!EmitExpression(out, initializer)) {
return false;
}
} else {
if (!EmitZeroValue(out, var->Type()->UnwrapRef())) {
return false;
}
}
out << ";";
return true;
}
bool GeneratorImpl::EmitWorkgroupVariable(const sem::Variable* var) {
auto* decl = var->Declaration();
auto out = line();
out << "shared ";
auto name = builder_.Symbols().NameFor(decl->name->symbol);
auto* type = var->Type()->UnwrapRef();
if (!EmitTypeAndName(out, type, var->AddressSpace(), var->Access(), name)) {
return false;
}
if (auto* initializer = decl->initializer) {
out << " = ";
if (!EmitExpression(out, initializer)) {
return false;
}
}
out << ";";
return true;
}
bool GeneratorImpl::EmitIOVariable(const sem::GlobalVariable* var) {
auto* decl = var->Declaration();
if (auto* attr = ast::GetAttribute<ast::BuiltinAttribute>(decl->attributes)) {
auto builtin = program_->Sem().Get(attr)->Value();
// Use of gl_SampleID requires the GL_OES_sample_variables extension
if (RequiresOESSampleVariables(builtin)) {
requires_oes_sample_variables_ = true;
}
// Do not emit builtin (gl_) variables.
return true;
}
auto out = line();
EmitAttributes(out, var, decl->attributes);
EmitInterpolationQualifiers(out, decl->attributes);
auto name = builder_.Symbols().NameFor(decl->name->symbol);
auto* type = var->Type()->UnwrapRef();
if (!EmitTypeAndName(out, type, var->AddressSpace(), var->Access(), name)) {
return false;
}
if (auto* initializer = decl->initializer) {
out << " = ";
if (!EmitExpression(out, initializer)) {
return false;
}
}
out << ";";
return true;
}
void GeneratorImpl::EmitInterpolationQualifiers(
utils::StringStream& out,
utils::VectorRef<const ast::Attribute*> attributes) {
for (auto* attr : attributes) {
if (auto* interpolate = attr->As<ast::InterpolateAttribute>()) {
auto& sem = program_->Sem();
auto i_type =
sem.Get<sem::BuiltinEnumExpression<builtin::InterpolationType>>(interpolate->type)
->Value();
switch (i_type) {
case builtin::InterpolationType::kPerspective:
case builtin::InterpolationType::kLinear:
case builtin::InterpolationType::kUndefined:
break;
case builtin::InterpolationType::kFlat:
out << "flat ";
break;
}
if (interpolate->sampling) {
auto i_smpl = sem.Get<sem::BuiltinEnumExpression<builtin::InterpolationSampling>>(
interpolate->sampling)
->Value();
switch (i_smpl) {
case builtin::InterpolationSampling::kCentroid:
out << "centroid ";
break;
case builtin::InterpolationSampling::kSample:
case builtin::InterpolationSampling::kCenter:
case builtin::InterpolationSampling::kUndefined:
break;
}
}
}
}
}
bool GeneratorImpl::EmitAttributes(utils::StringStream& out,
const sem::GlobalVariable* var,
utils::VectorRef<const ast::Attribute*> attributes) {
if (attributes.IsEmpty()) {
return true;
}
bool first = true;
for (auto* attr : attributes) {
if (attr->As<ast::LocationAttribute>()) {
out << (first ? "layout(" : ", ");
out << "location = " << std::to_string(var->Location().value());
first = false;
}
}
if (!first) {
out << ") ";
}
return true;
}
bool GeneratorImpl::EmitEntryPointFunction(const ast::Function* func) {
auto* func_sem = builder_.Sem().Get(func);
if (func->PipelineStage() == ast::PipelineStage::kFragment) {
requires_default_precision_qualifier_ = true;
}
if (func->PipelineStage() == ast::PipelineStage::kCompute) {
auto out = line();
// Emit the layout(local_size) attributes.
auto wgsize = func_sem->WorkgroupSize();
out << "layout(";
for (size_t i = 0; i < 3; i++) {
if (i > 0) {
out << ", ";
}
out << "local_size_" << (i == 0 ? "x" : i == 1 ? "y" : "z") << " = ";
if (!wgsize[i].has_value()) {
diagnostics_.add_error(
diag::System::Writer,
"override-expressions should have been removed with the SubstituteOverride "
"transform");
return false;
}
out << std::to_string(wgsize[i].value());
}
out << ") in;";
}
// Emit original entry point signature
{
auto out = line();
if (!EmitTypeAndName(out, func_sem->ReturnType(), builtin::AddressSpace::kUndefined,
builtin::Access::kUndefined,
builder_.Symbols().NameFor(func->name->symbol))) {
return false;
}
out << "(";
bool first = true;
// Emit entry point parameters.
for (auto* var : func->params) {
auto* sem = builder_.Sem().Get(var);
auto* type = sem->Type();
if (TINT_UNLIKELY(!type->Is<sem::Struct>())) {
// ICE likely indicates that the CanonicalizeEntryPointIO transform was
// not run, or a builtin parameter was added after it was run.
TINT_ICE(Writer, diagnostics_) << "Unsupported non-struct entry point parameter";
}
if (!first) {
out << ", ";
}
first = false;
if (!EmitTypeAndName(out, type, sem->AddressSpace(), sem->Access(),
builder_.Symbols().NameFor(var->name->symbol))) {
return false;
}
}
out << ") {";
}
// Emit original entry point function body
{
ScopedIndent si(this);
if (func->PipelineStage() == ast::PipelineStage::kVertex) {
line() << "gl_PointSize = 1.0;";
}
if (!EmitStatements(func->body->statements)) {
return false;
}
if (!Is<ast::ReturnStatement>(func->body->Last())) {
ast::ReturnStatement ret(ProgramID{}, ast::NodeID{}, Source{});
if (!EmitStatement(&ret)) {
return false;
}
}
}
line() << "}";
return true;
}
bool GeneratorImpl::EmitConstant(utils::StringStream& out, const constant::Value* constant) {
return Switch(
constant->Type(), //
[&](const type::Bool*) {
out << (constant->ValueAs<AInt>() ? "true" : "false");
return true;
},
[&](const type::F32*) {
PrintF32(out, constant->ValueAs<f32>());
return true;
},
[&](const type::F16*) {
PrintF16(out, constant->ValueAs<f16>());
return true;
},
[&](const type::I32*) {
PrintI32(out, constant->ValueAs<i32>());
return true;
},
[&](const type::U32*) {
out << constant->ValueAs<AInt>() << "u";
return true;
},
[&](const type::Vector* v) {
if (!EmitType(out, v, builtin::AddressSpace::kUndefined, builtin::Access::kUndefined,
"")) {
return false;
}
ScopedParen sp(out);
if (auto* splat = constant->As<constant::Splat>()) {
return EmitConstant(out, splat->el);
}
for (size_t i = 0; i < v->Width(); i++) {
if (i > 0) {
out << ", ";
}
if (!EmitConstant(out, constant->Index(i))) {
return false;
}
}
return true;
},
[&](const type::Matrix* m) {
if (!EmitType(out, m, builtin::AddressSpace::kUndefined, builtin::Access::kUndefined,
"")) {
return false;
}
ScopedParen sp(out);
for (size_t column_idx = 0; column_idx < m->columns(); column_idx++) {
if (column_idx > 0) {
out << ", ";
}
if (!EmitConstant(out, constant->Index(column_idx))) {
return false;
}
}
return true;
},
[&](const type::Array* a) {
if (!EmitType(out, a, builtin::AddressSpace::kUndefined, builtin::Access::kUndefined,
"")) {
return false;
}
ScopedParen sp(out);
auto count = a->ConstantCount();
if (!count) {
diagnostics_.add_error(diag::System::Writer,
type::Array::kErrExpectedConstantCount);
return false;
}
for (size_t i = 0; i < count; i++) {
if (i > 0) {
out << ", ";
}
if (!EmitConstant(out, constant->Index(i))) {
return false;
}
}
return true;
},
[&](const sem::Struct* s) {
if (!EmitStructType(&helpers_, s)) {
return false;
}
out << StructName(s);
ScopedParen sp(out);
for (size_t i = 0; i < s->Members().Length(); i++) {
if (i > 0) {
out << ", ";
}
if (!EmitConstant(out, constant->Index(i))) {
return false;
}
}
return true;
},
[&](Default) {
diagnostics_.add_error(
diag::System::Writer,
"unhandled constant type: " + builder_.FriendlyName(constant->Type()));
return false;
});
}
bool GeneratorImpl::EmitLiteral(utils::StringStream& out, const ast::LiteralExpression* lit) {
return Switch(
lit,
[&](const ast::BoolLiteralExpression* l) {
out << (l->value ? "true" : "false");
return true;
},
[&](const ast::FloatLiteralExpression* l) {
if (l->suffix == ast::FloatLiteralExpression::Suffix::kH) {
PrintF16(out, static_cast<float>(l->value));
} else {
PrintF32(out, static_cast<float>(l->value));
}
return true;
},
[&](const ast::IntLiteralExpression* i) {
switch (i->suffix) {
case ast::IntLiteralExpression::Suffix::kNone:
case ast::IntLiteralExpression::Suffix::kI: {
PrintI32(out, static_cast<int32_t>(i->value));
return true;
}
case ast::IntLiteralExpression::Suffix::kU: {
out << i->value << "u";
return true;
}
}
diagnostics_.add_error(diag::System::Writer, "unknown integer literal suffix type");
return false;
},
[&](Default) {
diagnostics_.add_error(diag::System::Writer, "unknown literal type");
return false;
});
}
bool GeneratorImpl::EmitZeroValue(utils::StringStream& out, const type::Type* type) {
if (type->Is<type::Bool>()) {
out << "false";
} else if (type->Is<type::F32>()) {
out << "0.0f";
} else if (type->Is<type::F16>()) {
out << "0.0hf";
} else if (type->Is<type::I32>()) {
out << "0";
} else if (type->Is<type::U32>()) {
out << "0u";
} else if (auto* vec = type->As<type::Vector>()) {
if (!EmitType(out, type, builtin::AddressSpace::kUndefined, builtin::Access::kReadWrite,
"")) {
return false;
}
ScopedParen sp(out);
for (uint32_t i = 0; i < vec->Width(); i++) {
if (i != 0) {
out << ", ";
}
if (!EmitZeroValue(out, vec->type())) {
return false;
}
}
} else if (auto* mat = type->As<type::Matrix>()) {
if (!EmitType(out, type, builtin::AddressSpace::kUndefined, builtin::Access::kReadWrite,
"")) {
return false;
}
ScopedParen sp(out);
for (uint32_t i = 0; i < (mat->rows() * mat->columns()); i++) {
if (i != 0) {
out << ", ";
}
if (!EmitZeroValue(out, mat->type())) {
return false;
}
}
} else if (auto* str = type->As<sem::Struct>()) {
if (!EmitType(out, type, builtin::AddressSpace::kUndefined, builtin::Access::kUndefined,
"")) {
return false;
}
bool first = true;
ScopedParen sp(out);
for (auto* member : str->Members()) {
if (!first) {
out << ", ";
} else {
first = false;
}
EmitZeroValue(out, member->Type());
}
} else if (auto* arr = type->As<type::Array>()) {
if (!EmitType(out, type, builtin::AddressSpace::kUndefined, builtin::Access::kUndefined,
"")) {
return false;
}
ScopedParen sp(out);
auto count = arr->ConstantCount();
if (!count) {
diagnostics_.add_error(diag::System::Writer, type::Array::kErrExpectedConstantCount);
return false;
}
for (uint32_t i = 0; i < count; i++) {
if (i != 0) {
out << ", ";
}
EmitZeroValue(out, arr->ElemType());
}
} else {
diagnostics_.add_error(diag::System::Writer, "Invalid type for zero emission: " +
type->FriendlyName(builder_.Symbols()));
return false;
}
return true;
}
bool GeneratorImpl::EmitLoop(const ast::LoopStatement* stmt) {
auto emit_continuing = [this, stmt]() {
if (stmt->continuing && !stmt->continuing->Empty()) {
if (!EmitBlock(stmt->continuing)) {
return false;
}
}
return true;
};
TINT_SCOPED_ASSIGNMENT(emit_continuing_, emit_continuing);
line() << "while (true) {";
{
ScopedIndent si(this);
if (!EmitStatements(stmt->body->statements)) {
return false;
}
if (!emit_continuing_()) {
return false;
}
}
line() << "}";
return true;
}
bool GeneratorImpl::EmitForLoop(const ast::ForLoopStatement* stmt) {
// Nest a for loop with a new block. In HLSL the initializer scope is not
// nested by the for-loop, so we may get variable redefinitions.
line() << "{";
increment_indent();
TINT_DEFER({
decrement_indent();
line() << "}";
});
TextBuffer init_buf;
if (auto* init = stmt->initializer) {
TINT_SCOPED_ASSIGNMENT(current_buffer_, &init_buf);
if (!EmitStatement(init)) {
return false;
}
}
TextBuffer cond_pre;
utils::StringStream cond_buf;
if (auto* cond = stmt->condition) {
TINT_SCOPED_ASSIGNMENT(current_buffer_, &cond_pre);
if (!EmitExpression(cond_buf, cond)) {
return false;
}
}
TextBuffer cont_buf;
if (auto* cont = stmt->continuing) {
TINT_SCOPED_ASSIGNMENT(current_buffer_, &cont_buf);
if (!EmitStatement(cont)) {
return false;
}
}
// If the for-loop has a multi-statement conditional and / or continuing, then
// we cannot emit this as a regular for-loop in HLSL. Instead we need to
// generate a `while(true)` loop.
bool emit_as_loop = cond_pre.lines.size() > 0 || cont_buf.lines.size() > 1;
// If the for-loop has multi-statement initializer, or is going to be emitted
// as a `while(true)` loop, then declare the initializer statement(s) before
// the loop.
if (init_buf.lines.size() > 1 || (stmt->initializer && emit_as_loop)) {
current_buffer_->Append(init_buf);
init_buf.lines.clear(); // Don't emit the initializer again in the 'for'
}
if (emit_as_loop) {
auto emit_continuing = [&]() {
current_buffer_->Append(cont_buf);
return true;
};
TINT_SCOPED_ASSIGNMENT(emit_continuing_, emit_continuing);
line() << "while (true) {";
increment_indent();
TINT_DEFER({
decrement_indent();
line() << "}";
});
if (stmt->condition) {
current_buffer_->Append(cond_pre);
line() << "if (!(" << cond_buf.str() << ")) { break; }";
}
if (!EmitStatements(stmt->body->statements)) {
return false;
}
if (!emit_continuing_()) {
return false;
}
} else {
// For-loop can be generated.