Files
sirit/src/insts/type.cpp

238 lines
7.5 KiB
C++
Raw Normal View History

2018-08-26 04:49:16 -03:00
/* This file is part of the sirit project.
* Copyright (c) 2018 ReinUsesLisp
* This software may be used and distributed according to the terms of the GNU
2018-11-16 04:10:10 -03:00
* Lesser General Public License version 3 or any later version.
2018-08-26 04:49:16 -03:00
*/
2018-11-01 05:13:30 -03:00
#include <memory>
2018-08-26 14:25:59 -03:00
#include <cassert>
2018-10-03 00:32:45 -03:00
#include <optional>
2018-10-20 02:52:55 -03:00
2018-11-01 05:13:30 -03:00
#include "op.h"
#include "sirit/sirit.h"
2018-08-26 04:49:16 -03:00
namespace Sirit {
2018-10-31 22:20:49 -03:00
Id Module::OpTypeVoid() {
2018-11-01 05:13:30 -03:00
return AddDeclaration(std::make_unique<Op>(spv::Op::OpTypeVoid, bound));
2018-08-26 04:49:16 -03:00
}
2018-10-31 22:20:49 -03:00
Id Module::OpTypeBool() {
2018-11-01 05:13:30 -03:00
return AddDeclaration(std::make_unique<Op>(spv::Op::OpTypeBool, bound));
2018-08-26 14:25:59 -03:00
}
2018-10-31 22:20:49 -03:00
Id Module::OpTypeInt(int width, bool is_signed) {
2018-08-26 14:25:59 -03:00
if (width == 8) {
AddCapability(spv::Capability::Int8);
} else if (width == 16) {
AddCapability(spv::Capability::Int16);
} else if (width == 64) {
AddCapability(spv::Capability::Int64);
}
2018-11-01 05:13:30 -03:00
auto op{std::make_unique<Op>(spv::Op::OpTypeInt, bound)};
2018-08-26 14:25:59 -03:00
op->Add(width);
op->Add(is_signed ? 1 : 0);
2018-11-01 05:13:30 -03:00
return AddDeclaration(std::move(op));
2018-08-26 14:25:59 -03:00
}
2018-10-31 22:20:49 -03:00
Id Module::OpTypeFloat(int width) {
2018-08-26 14:25:59 -03:00
if (width == 16) {
AddCapability(spv::Capability::Float16);
} else if (width == 64) {
AddCapability(spv::Capability::Float64);
}
2018-11-01 05:13:30 -03:00
auto op{std::make_unique<Op>(spv::Op::OpTypeFloat, bound)};
2018-08-26 14:25:59 -03:00
op->Add(width);
2018-11-01 05:13:30 -03:00
return AddDeclaration(std::move(op));
2018-08-26 14:25:59 -03:00
}
2018-10-31 22:20:49 -03:00
Id Module::OpTypeVector(Id component_type, int component_count) {
2018-08-26 14:25:59 -03:00
assert(component_count >= 2);
2018-11-01 05:13:30 -03:00
auto op{std::make_unique<Op>(spv::Op::OpTypeVector, bound)};
2018-08-26 14:25:59 -03:00
op->Add(component_type);
op->Add(component_count);
2018-11-01 05:13:30 -03:00
return AddDeclaration(std::move(op));
2018-08-26 14:25:59 -03:00
}
2018-10-31 22:20:49 -03:00
Id Module::OpTypeMatrix(Id column_type, int column_count) {
2018-08-26 14:25:59 -03:00
assert(column_count >= 2);
AddCapability(spv::Capability::Matrix);
2018-11-01 05:13:30 -03:00
auto op{std::make_unique<Op>(spv::Op::OpTypeMatrix, bound)};
2018-08-26 14:25:59 -03:00
op->Add(column_type);
op->Add(column_count);
2018-11-01 05:13:30 -03:00
return AddDeclaration(std::move(op));
2018-08-26 14:25:59 -03:00
}
2018-10-31 22:20:49 -03:00
Id Module::OpTypeImage(Id sampled_type, spv::Dim dim, int depth, bool arrayed,
bool ms, int sampled, spv::ImageFormat image_format,
std::optional<spv::AccessQualifier> access_qualifier) {
2018-08-26 14:25:59 -03:00
switch (dim) {
case spv::Dim::Dim1D:
AddCapability(spv::Capability::Sampled1D);
break;
case spv::Dim::Cube:
AddCapability(spv::Capability::Shader);
break;
case spv::Dim::Rect:
AddCapability(spv::Capability::SampledRect);
break;
case spv::Dim::Buffer:
AddCapability(spv::Capability::SampledBuffer);
break;
case spv::Dim::SubpassData:
AddCapability(spv::Capability::InputAttachment);
break;
2018-08-26 14:25:59 -03:00
}
switch (image_format) {
case spv::ImageFormat::Rgba32f:
case spv::ImageFormat::Rgba16f:
case spv::ImageFormat::R32f:
case spv::ImageFormat::Rgba8:
case spv::ImageFormat::Rgba8Snorm:
case spv::ImageFormat::Rgba32i:
case spv::ImageFormat::Rgba16i:
case spv::ImageFormat::Rgba8i:
case spv::ImageFormat::R32i:
case spv::ImageFormat::Rgba32ui:
case spv::ImageFormat::Rgba16ui:
case spv::ImageFormat::Rgba8ui:
case spv::ImageFormat::R32ui:
AddCapability(spv::Capability::Shader);
break;
case spv::ImageFormat::Rg32f:
case spv::ImageFormat::Rg16f:
case spv::ImageFormat::R11fG11fB10f:
case spv::ImageFormat::R16f:
case spv::ImageFormat::Rgba16:
case spv::ImageFormat::Rgb10A2:
case spv::ImageFormat::Rg16:
case spv::ImageFormat::Rg8:
case spv::ImageFormat::R16:
case spv::ImageFormat::R8:
case spv::ImageFormat::Rgba16Snorm:
case spv::ImageFormat::Rg16Snorm:
case spv::ImageFormat::Rg8Snorm:
case spv::ImageFormat::Rg32i:
case spv::ImageFormat::Rg16i:
case spv::ImageFormat::Rg8i:
case spv::ImageFormat::R16i:
case spv::ImageFormat::R8i:
case spv::ImageFormat::Rgb10a2ui:
case spv::ImageFormat::Rg32ui:
case spv::ImageFormat::Rg16ui:
case spv::ImageFormat::Rg8ui:
case spv::ImageFormat::R16ui:
case spv::ImageFormat::R8ui:
AddCapability(spv::Capability::StorageImageExtendedFormats);
break;
2018-08-26 14:25:59 -03:00
}
2018-11-01 05:13:30 -03:00
auto op{std::make_unique<Op>(spv::Op::OpTypeImage, bound)};
2018-08-26 14:25:59 -03:00
op->Add(sampled_type);
op->Add(static_cast<u32>(dim));
op->Add(depth);
op->Add(arrayed ? 1 : 0);
op->Add(ms ? 1 : 0);
op->Add(sampled);
op->Add(static_cast<u32>(image_format));
2018-10-03 00:32:45 -03:00
if (access_qualifier.has_value()) {
2018-08-26 14:25:59 -03:00
AddCapability(spv::Capability::Kernel);
2018-10-03 00:32:45 -03:00
op->Add(static_cast<u32>(access_qualifier.value()));
2018-08-26 14:25:59 -03:00
}
2018-11-01 05:13:30 -03:00
return AddDeclaration(std::move(op));
2018-08-26 14:25:59 -03:00
}
2018-10-31 22:20:49 -03:00
Id Module::OpTypeSampler() {
2018-11-01 05:13:30 -03:00
return AddDeclaration(std::make_unique<Op>(spv::Op::OpTypeSampler, bound));
2018-08-26 14:25:59 -03:00
}
2018-10-31 22:20:49 -03:00
Id Module::OpTypeSampledImage(Id image_type) {
2018-11-01 05:13:30 -03:00
auto op{std::make_unique<Op>(spv::Op::OpTypeSampledImage, bound)};
2018-08-26 14:25:59 -03:00
op->Add(image_type);
2018-11-01 05:13:30 -03:00
return AddDeclaration(std::move(op));
2018-08-26 14:25:59 -03:00
}
2018-10-31 22:20:49 -03:00
Id Module::OpTypeArray(Id element_type, Id length) {
2018-11-01 05:13:30 -03:00
auto op{std::make_unique<Op>(spv::Op::OpTypeArray, bound)};
2018-08-26 15:48:10 -03:00
op->Add(element_type);
op->Add(length);
2018-11-01 05:13:30 -03:00
return AddDeclaration(std::move(op));
2018-08-26 15:48:10 -03:00
}
2018-10-31 22:20:49 -03:00
Id Module::OpTypeRuntimeArray(Id element_type) {
2018-08-26 15:48:10 -03:00
AddCapability(spv::Capability::Shader);
2018-11-01 05:13:30 -03:00
auto op{std::make_unique<Op>(spv::Op::OpTypeRuntimeArray, bound)};
2018-08-26 15:48:10 -03:00
op->Add(element_type);
2018-11-01 05:13:30 -03:00
return AddDeclaration(std::move(op));
2018-08-26 15:48:10 -03:00
}
2018-10-31 22:20:49 -03:00
Id Module::OpTypeStruct(const std::vector<Id>& members) {
2018-11-01 05:13:30 -03:00
auto op{std::make_unique<Op>(spv::Op::OpTypeStruct, bound)};
2018-08-26 15:48:10 -03:00
op->Add(members);
2018-11-01 05:13:30 -03:00
return AddDeclaration(std::move(op));
2018-08-26 15:48:10 -03:00
}
2018-10-31 22:20:49 -03:00
Id Module::OpTypeOpaque(const std::string& name) {
2018-08-26 15:48:10 -03:00
AddCapability(spv::Capability::Kernel);
2018-11-01 05:13:30 -03:00
auto op{std::make_unique<Op>(spv::Op::OpTypeOpaque, bound)};
2018-08-26 15:48:10 -03:00
op->Add(name);
2018-11-01 05:13:30 -03:00
return AddDeclaration(std::move(op));
2018-08-26 15:48:10 -03:00
}
2018-10-31 22:20:49 -03:00
Id Module::OpTypePointer(spv::StorageClass storage_class, Id type) {
2018-08-26 15:48:10 -03:00
switch (storage_class) {
case spv::StorageClass::Uniform:
case spv::StorageClass::Output:
case spv::StorageClass::Private:
case spv::StorageClass::PushConstant:
case spv::StorageClass::StorageBuffer:
AddCapability(spv::Capability::Shader);
break;
case spv::StorageClass::Generic:
AddCapability(spv::Capability::GenericPointer);
break;
case spv::StorageClass::AtomicCounter:
AddCapability(spv::Capability::AtomicStorage);
break;
2018-08-26 15:48:10 -03:00
}
2018-11-01 05:13:30 -03:00
auto op{std::make_unique<Op>(spv::Op::OpTypePointer, bound)};
2018-08-26 15:48:10 -03:00
op->Add(static_cast<u32>(storage_class));
op->Add(type);
2018-11-01 05:13:30 -03:00
return AddDeclaration(std::move(op));
2018-08-26 15:48:10 -03:00
}
2018-10-31 22:20:49 -03:00
Id Module::OpTypeFunction(Id return_type, const std::vector<Id>& arguments) {
2018-11-01 05:13:30 -03:00
auto op{std::make_unique<Op>(spv::Op::OpTypeFunction, bound)};
2018-08-26 19:21:34 -03:00
op->Add(return_type);
op->Add(arguments);
2018-11-01 05:13:30 -03:00
return AddDeclaration(std::move(op));
2018-08-26 19:21:34 -03:00
}
2018-10-31 22:20:49 -03:00
Id Module::OpTypeEvent() {
2018-08-26 19:21:34 -03:00
AddCapability(spv::Capability::Kernel);
2018-11-01 05:13:30 -03:00
return AddDeclaration(std::make_unique<Op>(spv::Op::OpTypeEvent, bound));
2018-08-26 19:21:34 -03:00
}
2018-10-31 22:20:49 -03:00
Id Module::OpTypeDeviceEvent() {
2018-08-26 19:21:34 -03:00
AddCapability(spv::Capability::DeviceEnqueue);
2018-11-01 05:13:30 -03:00
return AddDeclaration(std::make_unique<Op>(spv::Op::OpTypeDeviceEvent, bound));
2018-08-26 19:21:34 -03:00
}
2018-10-31 22:20:49 -03:00
Id Module::OpTypeReserveId() {
2018-08-26 19:21:34 -03:00
AddCapability(spv::Capability::Pipes);
2018-11-01 05:13:30 -03:00
return AddDeclaration(std::make_unique<Op>(spv::Op::OpTypeReserveId, bound));
2018-08-26 19:21:34 -03:00
}
2018-10-31 22:20:49 -03:00
Id Module::OpTypeQueue() {
2018-08-26 19:21:34 -03:00
AddCapability(spv::Capability::DeviceEnqueue);
2018-11-01 05:13:30 -03:00
return AddDeclaration(std::make_unique<Op>(spv::Op::OpTypeQueue, bound));
2018-08-26 19:21:34 -03:00
}
2018-10-31 22:20:49 -03:00
Id Module::OpTypePipe(spv::AccessQualifier access_qualifier) {
2018-08-26 19:21:34 -03:00
AddCapability(spv::Capability::Pipes);
2018-11-01 05:13:30 -03:00
auto op{std::make_unique<Op>(spv::Op::OpTypePipe, bound)};
2018-08-26 19:35:48 -03:00
op->Add(static_cast<u32>(access_qualifier));
2018-11-01 05:13:30 -03:00
return AddDeclaration(std::move(op));
2018-08-26 04:49:16 -03:00
}
} // namespace Sirit