sirit/tests/main.cpp

84 lines
2.8 KiB
C++
Raw Normal View History

2018-08-23 09:59:57 +02: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-08-27 04:28:39 +02:00
* Lesser General Public License version 2.1 or any later version.
2018-08-23 09:59:57 +02:00
*/
#include <sirit/sirit.h>
2018-08-26 01:16:37 +02:00
#include <cstdio>
#include <cstdlib>
class MyModule : public Sirit::Module {
public:
MyModule() {}
~MyModule() = default;
void Generate() {
AddCapability(spv::Capability::Shader);
SetMemoryModel(spv::AddressingModel::Logical, spv::MemoryModel::GLSL450);
2018-08-26 19:25:59 +02:00
// Type testing
TypeBool();
TypeBool();
TypeInt(64, false);
TypeInt(16, false);
TypeFloat(16);
TypeFloat(32);
TypeFloat(64);
TypeVector(TypeBool(), 4);
TypeVector(TypeBool(), 3);
TypeMatrix(TypeVector(TypeFloat(32), 4), 4);
TypeImage(TypeFloat(32), spv::Dim::Dim2D, 0, false, false, 0,
2018-08-26 20:48:10 +02:00
spv::ImageFormat::Rg32f);
2018-08-26 19:25:59 +02:00
TypeSampledImage(TypeImage(TypeFloat(32), spv::Dim::Rect, 0, false, false, 0,
2018-08-28 09:41:42 +02:00
spv::ImageFormat::Rg32f));
TypeVector(TypeInt(32, false), 4);
TypeVector(TypeInt(64, false), 4);
TypeRuntimeArray(TypeInt(32, false));
TypeStruct({TypeInt(32, false), TypeFloat(64)});
2018-08-26 20:48:10 +02:00
TypePointer(spv::StorageClass::Private, TypeFloat(16));
2018-08-27 00:35:48 +02:00
ConstantTrue(TypeBool());
ConstantTrue(TypeBool());
ConstantFalse(TypeBool());
2018-08-27 05:29:40 +02:00
Constant(TypeFloat(64), Literal(6342.21));
Constant(TypeFloat(32), Literal(6342.21f));
Constant(TypeFloat(16), Literal(30u));
Constant(TypeInt(32, false), Literal(30u));
Constant(TypeInt(16, false), Literal(30u));
Constant(TypeInt(8, false), Literal(30u));
2018-08-27 05:38:25 +02:00
ConstantComposite(TypeVector(TypeFloat(32), 2),
{Constant(TypeFloat(32), Literal(50.0f)),
Constant(TypeFloat(32), Literal(50.0f))});
2018-08-28 09:46:18 +02:00
ConstantNull(TypeVector(TypeInt(64, false), 4));
2018-08-26 19:25:59 +02:00
2018-08-31 09:40:15 +02:00
auto cont{Label()};
2018-08-31 09:25:59 +02:00
auto skip{Label()};
2018-08-31 09:40:15 +02:00
Name(skip, "skip");
2018-08-26 01:16:37 +02:00
auto main_type{TypeFunction(TypeVoid())};
2018-08-26 01:34:06 +02:00
auto main_func{Emit(Function(TypeVoid(), spv::FunctionControlMask::MaskNone, main_type))};
Emit(Label());
2018-08-31 09:40:15 +02:00
Emit(BranchConditional(ConstantTrue(TypeBool()), cont, skip, 5, 0));
Emit(cont);
Emit(Branch(skip));
2018-08-31 09:25:59 +02:00
Emit(skip);
2018-08-26 01:34:06 +02:00
Emit(Return());
Emit(FunctionEnd());
2018-08-26 01:16:37 +02:00
AddEntryPoint(spv::ExecutionModel::Vertex, main_func, "main");
}
};
int main(int argc, char** argv) {
MyModule module;
module.Generate();
module.Optimize(2);
2018-08-31 08:41:30 +02:00
std::vector<std::uint8_t> code{module.Assemble()};
2018-08-26 01:16:37 +02:00
FILE* file = fopen("sirit.spv", "wb");
fwrite(code.data(), 1, code.size(), file);
fclose(file);
2018-08-23 09:59:57 +02:00
return 0;
}