diff --git a/src/insts/memory.cpp b/src/insts/memory.cpp index d7fc3ff..bffa2c0 100644 --- a/src/insts/memory.cpp +++ b/src/insts/memory.cpp @@ -4,7 +4,7 @@ * Lesser General Public License version 2.1 or any later version. */ -#include "insts.h" +#include "op.h" #include "sirit/sirit.h" #include @@ -12,51 +12,51 @@ namespace Sirit { Id Module::OpVariable(Id result_type, spv::StorageClass storage_class, Id initializer) { - auto op{new Op(spv::Op::OpVariable, bound++, result_type)}; - AddEnum(op, storage_class); + auto op{std::make_unique(spv::Op::OpVariable, bound++, result_type)}; + op->Add(static_cast(storage_class)); if (initializer) { op->Add(initializer); } - return AddCode(op); + return AddCode(std::move(op)); } Id Module::OpLoad(Id result_type, Id pointer, std::optional memory_access) { - auto op{new Op(spv::Op::OpLoad, bound++, result_type)}; + auto op{std::make_unique(spv::Op::OpLoad, bound++, result_type)}; op->Add(pointer); if (memory_access) { - AddEnum(op, *memory_access); + op->Add(static_cast(*memory_access)); } - return AddCode(op); + return AddCode(std::move(op)); } Id Module::OpStore(Id pointer, Id object, std::optional memory_access) { - auto op{new Op(spv::Op::OpStore)}; + auto op{std::make_unique(spv::Op::OpStore)}; op->Add(pointer); op->Add(object); if (memory_access) { - AddEnum(op, *memory_access); + op->Add(static_cast(*memory_access)); } - return AddCode(op); + return AddCode(std::move(op)); } Id Module::OpAccessChain(Id result_type, Id base, const std::vector& indexes) { assert(indexes.size() > 0); - auto op{new Op(spv::Op::OpAccessChain, bound++, result_type)}; + auto op{std::make_unique(spv::Op::OpAccessChain, bound++, result_type)}; op->Add(base); op->Add(indexes); - return AddCode(op); + return AddCode(std::move(op)); } Id Module::OpCompositeInsert(Id result_type, Id object, Id composite, const std::vector& indexes) { - auto op{new Op(spv::Op::OpCompositeInsert, bound++, result_type)}; + auto op{std::make_unique(spv::Op::OpCompositeInsert, bound++, result_type)}; op->Add(object); op->Add(composite); op->Add(indexes); - return AddCode(op); + return AddCode(std::move(op)); } } // namespace Sirit diff --git a/src/insts/misc.cpp b/src/insts/misc.cpp index 2cd544d..835e8fd 100644 --- a/src/insts/misc.cpp +++ b/src/insts/misc.cpp @@ -4,14 +4,14 @@ * Lesser General Public License version 2.1 or any later version. */ -#include "insts.h" +#include "op.h" #include "sirit/sirit.h" #include namespace Sirit { Id Module::OpUndef(Id result_type) { - return AddCode(new Op(spv::Op::OpUndef, bound++, result_type)); + return AddCode(std::make_unique(spv::Op::OpUndef, bound++, result_type)); } } // namespace Sirit diff --git a/src/insts/type.cpp b/src/insts/type.cpp index 290b1ff..8ad58af 100644 --- a/src/insts/type.cpp +++ b/src/insts/type.cpp @@ -4,20 +4,21 @@ * Lesser General Public License version 2.1 or any later version. */ +#include #include #include -#include "insts.h" +#include "op.h" #include "sirit/sirit.h" namespace Sirit { Id Module::OpTypeVoid() { - return AddDeclaration(new Op(spv::Op::OpTypeVoid, bound)); + return AddDeclaration(std::make_unique(spv::Op::OpTypeVoid, bound)); } Id Module::OpTypeBool() { - return AddDeclaration(new Op(spv::Op::OpTypeBool, bound)); + return AddDeclaration(std::make_unique(spv::Op::OpTypeBool, bound)); } Id Module::OpTypeInt(int width, bool is_signed) { @@ -28,10 +29,10 @@ Id Module::OpTypeInt(int width, bool is_signed) { } else if (width == 64) { AddCapability(spv::Capability::Int64); } - auto op{new Op(spv::Op::OpTypeInt, bound)}; + auto op{std::make_unique(spv::Op::OpTypeInt, bound)}; op->Add(width); op->Add(is_signed ? 1 : 0); - return AddDeclaration(op); + return AddDeclaration(std::move(op)); } Id Module::OpTypeFloat(int width) { @@ -40,26 +41,26 @@ Id Module::OpTypeFloat(int width) { } else if (width == 64) { AddCapability(spv::Capability::Float64); } - auto op{new Op(spv::Op::OpTypeFloat, bound)}; + auto op{std::make_unique(spv::Op::OpTypeFloat, bound)}; op->Add(width); - return AddDeclaration(op); + return AddDeclaration(std::move(op)); } Id Module::OpTypeVector(Id component_type, int component_count) { assert(component_count >= 2); - auto op{new Op(spv::Op::OpTypeVector, bound)}; + auto op{std::make_unique(spv::Op::OpTypeVector, bound)}; op->Add(component_type); op->Add(component_count); - return AddDeclaration(op); + return AddDeclaration(std::move(op)); } Id Module::OpTypeMatrix(Id column_type, int column_count) { assert(column_count >= 2); AddCapability(spv::Capability::Matrix); - Op* op{new Op(spv::Op::OpTypeMatrix, bound)}; + auto op{std::make_unique(spv::Op::OpTypeMatrix, bound)}; op->Add(column_type); op->Add(column_count); - return AddDeclaration(op); + return AddDeclaration(std::move(op)); } Id Module::OpTypeImage(Id sampled_type, spv::Dim dim, int depth, bool arrayed, @@ -125,7 +126,7 @@ Id Module::OpTypeImage(Id sampled_type, spv::Dim dim, int depth, bool arrayed, AddCapability(spv::Capability::StorageImageExtendedFormats); break; } - auto op{new Op(spv::Op::OpTypeImage, bound)}; + auto op{std::make_unique(spv::Op::OpTypeImage, bound)}; op->Add(sampled_type); op->Add(static_cast(dim)); op->Add(depth); @@ -137,44 +138,44 @@ Id Module::OpTypeImage(Id sampled_type, spv::Dim dim, int depth, bool arrayed, AddCapability(spv::Capability::Kernel); op->Add(static_cast(access_qualifier.value())); } - return AddDeclaration(op); + return AddDeclaration(std::move(op)); } Id Module::OpTypeSampler() { - return AddDeclaration(new Op(spv::Op::OpTypeSampler, bound)); + return AddDeclaration(std::make_unique(spv::Op::OpTypeSampler, bound)); } Id Module::OpTypeSampledImage(Id image_type) { - auto op{new Op(spv::Op::OpTypeSampledImage, bound)}; + auto op{std::make_unique(spv::Op::OpTypeSampledImage, bound)}; op->Add(image_type); - return AddDeclaration(op); + return AddDeclaration(std::move(op)); } Id Module::OpTypeArray(Id element_type, Id length) { - auto op{new Op(spv::Op::OpTypeArray, bound)}; + auto op{std::make_unique(spv::Op::OpTypeArray, bound)}; op->Add(element_type); op->Add(length); - return AddDeclaration(op); + return AddDeclaration(std::move(op)); } Id Module::OpTypeRuntimeArray(Id element_type) { AddCapability(spv::Capability::Shader); - auto op{new Op(spv::Op::OpTypeRuntimeArray, bound)}; + auto op{std::make_unique(spv::Op::OpTypeRuntimeArray, bound)}; op->Add(element_type); - return AddDeclaration(op); + return AddDeclaration(std::move(op)); } Id Module::OpTypeStruct(const std::vector& members) { - auto op{new Op(spv::Op::OpTypeStruct, bound)}; + auto op{std::make_unique(spv::Op::OpTypeStruct, bound)}; op->Add(members); - return AddDeclaration(op); + return AddDeclaration(std::move(op)); } Id Module::OpTypeOpaque(const std::string& name) { AddCapability(spv::Capability::Kernel); - auto op{new Op(spv::Op::OpTypeOpaque, bound)}; + auto op{std::make_unique(spv::Op::OpTypeOpaque, bound)}; op->Add(name); - return AddDeclaration(op); + return AddDeclaration(std::move(op)); } Id Module::OpTypePointer(spv::StorageClass storage_class, Id type) { @@ -193,44 +194,44 @@ Id Module::OpTypePointer(spv::StorageClass storage_class, Id type) { AddCapability(spv::Capability::AtomicStorage); break; } - auto op{new Op(spv::Op::OpTypePointer, bound)}; + auto op{std::make_unique(spv::Op::OpTypePointer, bound)}; op->Add(static_cast(storage_class)); op->Add(type); - return AddDeclaration(op); + return AddDeclaration(std::move(op)); } Id Module::OpTypeFunction(Id return_type, const std::vector& arguments) { - auto op{new Op(spv::Op::OpTypeFunction, bound)}; + auto op{std::make_unique(spv::Op::OpTypeFunction, bound)}; op->Add(return_type); op->Add(arguments); - return AddDeclaration(op); + return AddDeclaration(std::move(op)); } Id Module::OpTypeEvent() { AddCapability(spv::Capability::Kernel); - return AddDeclaration(new Op(spv::Op::OpTypeEvent, bound)); + return AddDeclaration(std::make_unique(spv::Op::OpTypeEvent, bound)); } Id Module::OpTypeDeviceEvent() { AddCapability(spv::Capability::DeviceEnqueue); - return AddDeclaration(new Op(spv::Op::OpTypeDeviceEvent, bound)); + return AddDeclaration(std::make_unique(spv::Op::OpTypeDeviceEvent, bound)); } Id Module::OpTypeReserveId() { AddCapability(spv::Capability::Pipes); - return AddDeclaration(new Op(spv::Op::OpTypeReserveId, bound)); + return AddDeclaration(std::make_unique(spv::Op::OpTypeReserveId, bound)); } Id Module::OpTypeQueue() { AddCapability(spv::Capability::DeviceEnqueue); - return AddDeclaration(new Op(spv::Op::OpTypeQueue, bound)); + return AddDeclaration(std::make_unique(spv::Op::OpTypeQueue, bound)); } Id Module::OpTypePipe(spv::AccessQualifier access_qualifier) { AddCapability(spv::Capability::Pipes); - auto op{new Op(spv::Op::OpTypePipe, bound)}; + auto op{std::make_unique(spv::Op::OpTypePipe, bound)}; op->Add(static_cast(access_qualifier)); - return AddDeclaration(op); + return AddDeclaration(std::move(op)); } } // namespace Sirit diff --git a/src/op.cpp b/src/op.cpp index 86a4126..7d832a6 100644 --- a/src/op.cpp +++ b/src/op.cpp @@ -113,7 +113,7 @@ void Op::Add(const std::vector& ids) { } u16 Op::WordCount() const { - u16 count{1}; + u16 count = 1; if (result_type) { count++; } diff --git a/src/sirit.cpp b/src/sirit.cpp index d4b7fdd..fd90205 100644 --- a/src/sirit.cpp +++ b/src/sirit.cpp @@ -75,12 +75,12 @@ void Module::SetMemoryModel(spv::AddressingModel addressing_model, void Module::AddEntryPoint(spv::ExecutionModel execution_model, Id entry_point, const std::string& name, const std::vector& interfaces) { - auto const op{new Op(spv::Op::OpEntryPoint)}; + auto op{std::make_unique(spv::Op::OpEntryPoint)}; op->Add(static_cast(execution_model)); op->Add(entry_point); op->Add(name); op->Add(interfaces); - entry_points.push_back(std::unique_ptr(op)); + entry_points.push_back(std::move(op)); } Id Module::Emit(Id op) { @@ -95,8 +95,9 @@ Id Module::AddGlobalVariable(Id variable) { } Id Module::AddCode(std::unique_ptr op) { + const auto id = op.get(); code_store.push_back(std::move(op)); - return op.get(); + return id; } Id Module::AddCode(spv::Op opcode, std::optional id) { @@ -117,8 +118,9 @@ Id Module::AddDeclaration(std::unique_ptr op) { } Id Module::AddAnnotation(std::unique_ptr op) { + const auto id = op.get(); annotations.push_back(std::move(op)); - return op.get(); + return id; } } // namespace Sirit diff --git a/tests/main.cpp b/tests/main.cpp index 5258920..29c91f1 100644 --- a/tests/main.cpp +++ b/tests/main.cpp @@ -19,48 +19,49 @@ public: AddCapability(spv::Capability::Shader); SetMemoryModel(spv::AddressingModel::Logical, spv::MemoryModel::GLSL450); - const auto t_void = OpName(OpTypeVoid(), "void"); - const auto t_uint = OpName(OpTypeInt(32, false), "uint"); - const auto t_float = OpName(OpTypeFloat(32), "float"); + const auto t_void = Name(OpTypeVoid(), "void"); + const auto t_uint = Name(OpTypeInt(32, false), "uint"); + const auto t_float = Name(OpTypeFloat(32), "float"); - const auto float4 = OpName(OpTypeVector(t_float, 4), "float4"); - const auto in_float = OpName(OpTypePointer(spv::StorageClass::Input, t_float), "in_float"); - const auto in_float4 = OpName(OpTypePointer(spv::StorageClass::Input, float4), "in_float4"); - const auto out_float4 = OpName(OpTypePointer(spv::StorageClass::Output, float4), "out_float4"); + const auto float4 = Name(OpTypeVector(t_float, 4), "float4"); + const auto in_float = Name(OpTypePointer(spv::StorageClass::Input, t_float), "in_float"); + const auto in_float4 = Name(OpTypePointer(spv::StorageClass::Input, float4), "in_float4"); + const auto out_float4 = Name(OpTypePointer(spv::StorageClass::Output, float4), "out_float4"); - const auto gl_per_vertex = OpName(OpTypeStruct({float4}), "gl_PerVertex"); - const auto gl_per_vertex_ptr = OpName(OpTypePointer(spv::StorageClass::Output, gl_per_vertex), "out_gl_PerVertex"); + const auto gl_per_vertex = Name(OpTypeStruct({float4}), "gl_PerVertex"); + const auto gl_per_vertex_ptr = Name(OpTypePointer(spv::StorageClass::Output, gl_per_vertex), "out_gl_PerVertex"); - const auto in_pos = OpName(OpVariable(in_float4, spv::StorageClass::Input), "in_pos"); - const auto per_vertex = OpName(OpVariable(gl_per_vertex_ptr, spv::StorageClass::Output), "per_vertex"); + const auto in_pos = Name(OpVariable(in_float4, spv::StorageClass::Input), "in_pos"); + const auto per_vertex = Name(OpVariable(gl_per_vertex_ptr, spv::StorageClass::Output), "per_vertex"); Decorate(in_pos, spv::Decoration::Location, {0}); Decorate(gl_per_vertex, spv::Decoration::Block); + Decorate(gl_per_vertex, spv::Decoration::Block); MemberDecorate(gl_per_vertex, 0, spv::Decoration::BuiltIn, {static_cast(spv::BuiltIn::Position)}); AddGlobalVariable(in_pos); AddGlobalVariable(per_vertex); - const auto main_func = Emit(OpName(OpFunction(t_void, spv::FunctionControlMask::MaskNone, TypeFunction(t_void)), "main")); - Emit(Label()); + const auto main_func = Emit(Name(OpFunction(t_void, spv::FunctionControlMask::MaskNone, OpTypeFunction(t_void)), "main")); + Emit(OpLabel()); const auto ptr_pos_x = Emit(OpAccessChain(in_float, in_pos, {OpConstant(t_uint, 0u)})); const auto ptr_pos_y = Emit(OpAccessChain(in_float, in_pos, {OpConstant(t_uint, 1u)})); - const auto pos_x = Emit(Load(t_float, ptr_pos_x)); - const auto pos_y = Emit(Load(t_float, ptr_pos_y)); + const auto pos_x = Emit(OpLoad(t_float, ptr_pos_x)); + const auto pos_y = Emit(OpLoad(t_float, ptr_pos_y)); - auto tmp_position = Emit(Undef(float4)); + auto tmp_position = Emit(OpUndef(float4)); tmp_position = Emit(OpCompositeInsert(float4, pos_x, tmp_position, {0})); tmp_position = Emit(OpCompositeInsert(float4, pos_y, tmp_position, {1})); tmp_position = Emit(OpCompositeInsert(float4, OpConstant(t_float, 0.f), tmp_position, {2})); tmp_position = Emit(OpCompositeInsert(float4, OpConstant(t_float, 1.f), tmp_position, {3})); const auto gl_position = Emit(OpAccessChain(out_float4, per_vertex, {OpConstant(t_uint, 0u)})); - Emit(Store(gl_position, tmp_position)); + Emit(OpStore(gl_position, tmp_position)); - Emit(Return()); - Emit(FunctionEnd()); + Emit(OpReturn()); + Emit(OpFunctionEnd()); AddEntryPoint(spv::ExecutionModel::Vertex, main_func, "main", {in_pos, per_vertex}); }