2018-08-23 08:59:57 +01: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 07:10:10 +00:00
|
|
|
* Lesser General Public License version 3 or any later version.
|
2018-08-23 08:59:57 +01:00
|
|
|
*/
|
|
|
|
|
2018-08-26 00:16:37 +01:00
|
|
|
#include <cstdio>
|
|
|
|
#include <cstdlib>
|
2019-03-11 07:17:29 +00:00
|
|
|
#include <sirit/sirit.h>
|
2018-08-26 00:16:37 +01:00
|
|
|
|
2018-10-31 08:05:23 +00:00
|
|
|
using u32 = uint32_t;
|
|
|
|
|
2018-08-26 00:16:37 +01:00
|
|
|
class MyModule : public Sirit::Module {
|
|
|
|
public:
|
2018-10-31 08:05:23 +00:00
|
|
|
MyModule() = default;
|
2018-08-26 00:16:37 +01:00
|
|
|
~MyModule() = default;
|
|
|
|
|
|
|
|
void Generate() {
|
|
|
|
AddCapability(spv::Capability::Shader);
|
|
|
|
SetMemoryModel(spv::AddressingModel::Logical, spv::MemoryModel::GLSL450);
|
2019-03-11 07:17:29 +00:00
|
|
|
|
2019-03-14 07:33:54 +00:00
|
|
|
const auto t_void = Name(TypeVoid(), "void");
|
|
|
|
const auto t_uint = Name(TypeInt(32, false), "uint");
|
|
|
|
const auto t_float = Name(TypeFloat(32), "float");
|
2018-10-31 08:05:23 +00:00
|
|
|
|
2019-03-14 07:33:54 +00:00
|
|
|
const auto float4 = Name(TypeVector(t_float, 4), "float4");
|
|
|
|
const auto in_float = Name(TypePointer(spv::StorageClass::Input, t_float), "in_float");
|
|
|
|
const auto in_float4 = Name(TypePointer(spv::StorageClass::Input, float4), "in_float4");
|
2019-03-11 07:17:29 +00:00
|
|
|
const auto out_float4 =
|
2019-03-14 07:33:54 +00:00
|
|
|
Name(TypePointer(spv::StorageClass::Output, float4), "out_float4");
|
2019-03-11 07:17:29 +00:00
|
|
|
|
2019-03-14 07:33:54 +00:00
|
|
|
const auto gl_per_vertex = Name(TypeStruct(float4), "gl_PerVertex");
|
2019-03-11 07:17:29 +00:00
|
|
|
const auto gl_per_vertex_ptr =
|
2019-03-14 07:33:54 +00:00
|
|
|
Name(TypePointer(spv::StorageClass::Output, gl_per_vertex), "out_gl_PerVertex");
|
2018-10-31 08:05:23 +00:00
|
|
|
|
2018-11-01 08:13:30 +00:00
|
|
|
const auto in_pos = Name(OpVariable(in_float4, spv::StorageClass::Input), "in_pos");
|
2019-03-11 07:17:29 +00:00
|
|
|
const auto per_vertex =
|
|
|
|
Name(OpVariable(gl_per_vertex_ptr, spv::StorageClass::Output), "per_vertex");
|
2018-10-31 08:05:23 +00:00
|
|
|
|
2019-03-11 07:17:29 +00:00
|
|
|
Decorate(in_pos, spv::Decoration::Location, 0);
|
2018-10-31 08:05:23 +00:00
|
|
|
Decorate(gl_per_vertex, spv::Decoration::Block);
|
2018-11-01 08:13:30 +00:00
|
|
|
Decorate(gl_per_vertex, spv::Decoration::Block);
|
2019-03-11 07:17:29 +00:00
|
|
|
MemberDecorate(gl_per_vertex, 0, spv::Decoration::BuiltIn,
|
|
|
|
static_cast<u32>(spv::BuiltIn::Position));
|
|
|
|
|
2018-10-31 08:05:23 +00:00
|
|
|
AddGlobalVariable(in_pos);
|
|
|
|
AddGlobalVariable(per_vertex);
|
|
|
|
|
2019-03-11 07:17:29 +00:00
|
|
|
const auto main_func = Emit(
|
2019-03-14 07:33:54 +00:00
|
|
|
Name(OpFunction(t_void, spv::FunctionControlMask::MaskNone, TypeFunction(t_void)),
|
2019-03-11 07:17:29 +00:00
|
|
|
"main"));
|
2018-11-01 08:13:30 +00:00
|
|
|
Emit(OpLabel());
|
2018-10-31 08:05:23 +00:00
|
|
|
|
2019-03-11 07:17:29 +00:00
|
|
|
const auto ptr_pos_x = Emit(OpAccessChain(in_float, in_pos, Constant(t_uint, 0u)));
|
|
|
|
const auto ptr_pos_y = Emit(OpAccessChain(in_float, in_pos, Constant(t_uint, 1u)));
|
2018-10-31 08:05:23 +00:00
|
|
|
|
2018-11-01 08:13:30 +00:00
|
|
|
const auto pos_x = Emit(OpLoad(t_float, ptr_pos_x));
|
|
|
|
const auto pos_y = Emit(OpLoad(t_float, ptr_pos_y));
|
2018-10-31 08:05:23 +00:00
|
|
|
|
2018-11-01 08:13:30 +00:00
|
|
|
auto tmp_position = Emit(OpUndef(float4));
|
2019-03-11 07:17:29 +00:00
|
|
|
Decorate(tmp_position, spv::Decoration::FPRoundingMode,
|
|
|
|
static_cast<u32>(spv::FPRoundingMode::RTE));
|
|
|
|
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, Constant(t_float, 0.f), tmp_position, 2));
|
|
|
|
tmp_position = Emit(OpCompositeInsert(float4, Constant(t_float, 1.f), tmp_position, 3));
|
|
|
|
|
|
|
|
const auto gl_position = Emit(OpAccessChain(out_float4, per_vertex, Constant(t_uint, 0u)));
|
2018-11-01 08:13:30 +00:00
|
|
|
Emit(OpStore(gl_position, tmp_position));
|
2018-10-31 08:05:23 +00:00
|
|
|
|
2018-11-01 08:13:30 +00:00
|
|
|
Emit(OpReturn());
|
|
|
|
Emit(OpFunctionEnd());
|
2018-08-26 00:16:37 +01:00
|
|
|
|
2019-03-11 07:17:29 +00:00
|
|
|
AddEntryPoint(spv::ExecutionModel::Vertex, main_func, "main", in_pos, per_vertex);
|
2018-08-26 00:16:37 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
int main(int argc, char** argv) {
|
|
|
|
MyModule module;
|
|
|
|
module.Generate();
|
|
|
|
|
2018-08-31 07:41:30 +01:00
|
|
|
std::vector<std::uint8_t> code{module.Assemble()};
|
2018-08-26 00:16:37 +01:00
|
|
|
|
|
|
|
FILE* file = fopen("sirit.spv", "wb");
|
|
|
|
fwrite(code.data(), 1, code.size(), file);
|
|
|
|
fclose(file);
|
2018-08-23 08:59:57 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|