blob: 1a386db3150c4b69d0464e0b1a10a72fb1e5a692 [file] [log] [blame]
// 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/writer/spirv/builder.h"
#include "spirv/unified1/spirv.h"
#include "src/ast/type/matrix_type.h"
#include "src/ast/type/vector_type.h"
namespace tint {
namespace writer {
namespace spirv {
namespace {
uint32_t size_of(const std::vector<Instruction>& instructions) {
uint32_t size = 0;
for (const auto& inst : instructions)
size += inst.word_length();
return size;
}
uint32_t pipeline_stage_to_execution_model(ast::PipelineStage stage) {
SpvExecutionModel model = SpvExecutionModelVertex;
switch (stage) {
case ast::PipelineStage::kFragment:
model = SpvExecutionModelFragment;
break;
case ast::PipelineStage::kVertex:
model = SpvExecutionModelVertex;
break;
case ast::PipelineStage::kCompute:
model = SpvExecutionModelGLCompute;
break;
case ast::PipelineStage::kNone:
model = SpvExecutionModelMax;
break;
}
return model;
}
} // namespace
Builder::Builder() = default;
Builder::~Builder() = default;
bool Builder::Build(const ast::Module& m) {
push_preamble(spv::Op::OpCapability, {Operand::Int(SpvCapabilityShader)});
push_preamble(spv::Op::OpCapability,
{Operand::Int(SpvCapabilityVulkanMemoryModel)});
for (const auto& imp : m.imports()) {
GenerateImport(imp.get());
}
push_preamble(spv::Op::OpMemoryModel,
{Operand::Int(SpvAddressingModelLogical),
Operand::Int(SpvMemoryModelVulkanKHR)});
for (const auto& ep : m.entry_points()) {
if (!GenerateEntryPoint(ep.get())) {
return false;
}
}
return true;
}
Operand Builder::result_op() {
return Operand::Int(next_id());
}
uint32_t Builder::total_size() const {
// The 5 covers the magic, version, generator, id bound and reserved.
uint32_t size = 5;
size += size_of(preamble_);
size += size_of(debug_);
size += size_of(annotations_);
size += size_of(types_);
size += size_of(instructions_);
return size;
}
void Builder::iterate(std::function<void(const Instruction&)> cb) const {
for (const auto& inst : preamble_) {
cb(inst);
}
for (const auto& inst : debug_) {
cb(inst);
}
for (const auto& inst : annotations_) {
cb(inst);
}
for (const auto& inst : types_) {
cb(inst);
}
for (const auto& inst : instructions_) {
cb(inst);
}
}
bool Builder::GenerateEntryPoint(ast::EntryPoint* ep) {
auto name = ep->name();
if (name.empty()) {
name = ep->function_name();
}
auto id = id_for_func_name(ep->function_name());
if (id == 0) {
error_ = "unable to find ID for function: " + ep->function_name();
return false;
}
auto stage = pipeline_stage_to_execution_model(ep->stage());
if (stage == SpvExecutionModelMax) {
error_ = "Unknown pipeline stage provided";
return false;
}
push_preamble(spv::Op::OpEntryPoint,
{Operand::Int(stage), Operand::Int(id), Operand::String(name)});
return true;
}
void Builder::GenerateImport(ast::Import* imp) {
auto result = result_op();
auto id = result.to_i();
push_preamble(spv::Op::OpExtInstImport,
{result, Operand::String(imp->path())});
import_name_to_id_[imp->name()] = id;
}
uint32_t Builder::GenerateTypeIfNeeded(ast::type::Type* type) {
if (type->IsAlias()) {
return GenerateTypeIfNeeded(type->AsAlias()->type());
}
auto val = type_name_to_id_.find(type->type_name());
if (val != type_name_to_id_.end()) {
return val->second;
}
auto result = result_op();
auto id = result.to_i();
if (type->IsBool()) {
push_type(spv::Op::OpTypeBool, {result});
} else if (type->IsF32()) {
push_type(spv::Op::OpTypeFloat, {result, Operand::Int(32)});
} else if (type->IsI32()) {
push_type(spv::Op::OpTypeInt, {result, Operand::Int(32), Operand::Int(1)});
} else if (type->IsMatrix()) {
auto mat = type->AsMatrix();
ast::type::VectorType col_type(mat->type(), mat->rows());
auto type_id = GenerateTypeIfNeeded(&col_type);
if (has_error()) {
return 0;
}
push_type(spv::Op::OpTypeMatrix,
{result, Operand::Int(type_id), Operand::Int(mat->columns())});
} else if (type->IsU32()) {
push_type(spv::Op::OpTypeInt, {result, Operand::Int(32), Operand::Int(0)});
} else if (type->IsVector()) {
auto vec = type->AsVector();
auto col_type_id = GenerateTypeIfNeeded(vec->type());
if (has_error()) {
return 0;
}
push_type(spv::Op::OpTypeVector,
{result, Operand::Int(col_type_id), Operand::Int(vec->size())});
} else if (type->IsVoid()) {
push_type(spv::Op::OpTypeVoid, {result});
} else {
error_ = "unable to convert type: " + type->type_name();
return 0;
}
type_name_to_id_[type->type_name()] = id;
return id;
}
} // namespace spirv
} // namespace writer
} // namespace tint