This repository has been archived by the owner on Apr 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gpp.cpp
194 lines (169 loc) · 5.8 KB
/
gpp.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#include "gpp.hpp"
#include <iostream>
#include <string>
#include <fmt/format.h>
// the parsing code here does not depend on the actual implementation
// of the graphics object, only that it follows a certain shape
struct handle_command
{
// If the command type is only variant<buffer_allocation, buffer_upload> then
// the other "cases" won't even be generated
template <typename C>
void* operator()(C& command)
{
if constexpr (requires { C::static_buffer_allocation; })
{
std::cerr << "static buffer allocation requested\n";
std::cerr << " -> binding: " << command.binding << " ; sz: " << command.size << "\n";
return new int{1};
}
else if constexpr (requires { C::dynamic_buffer_allocation; })
{
std::cerr << "dynamic buffer allocation requested\n";
std::cerr << " -> binding: " << command.binding << " ; sz: " << command.size << "\n";
return new int{2};
}
else if constexpr (requires { C::immutable_buffer_allocation; })
{
std::cerr << "immutable buffer allocation requested\n";
std::cerr << " -> binding: " << command.binding << " ; sz: " << command.size << "\n";
return new int{3};
}
else if constexpr (requires { C::texture_allocation; })
{
std::cerr << "texture allocation requested\n";
return new int{4};
}
else if constexpr (requires { C::static_buffer_upload; })
{
std::cerr << "static buffer upload requested\n";
std::cerr << " -> handle: " << *(int*) command.handle << "\n";
std::cerr << " -> offset: " << command.offset << " ; sz: " << command.size << "\n";
return nullptr;
}
else if constexpr (requires { C::dynamic_buffer_upload; })
{
std::cerr << "dynamic buffer upload requested\n";
std::cerr << " -> handle: " << *(int*) command.handle << "\n";
std::cerr << " -> offset: " << command.offset << " ; sz: " << command.size << "\n";
return nullptr;
}
else if constexpr (requires { C::immutable_buffer_upload; })
{
std::cerr << "immutable buffer upload requested\n";
std::cerr << " -> handle: " << *(int*) command.handle << "\n";
std::cerr << " -> offset: " << command.offset << " ; sz: " << command.size << "\n";
return nullptr;
}
else if constexpr (requires { C::texture_upload; })
{
std::cerr << "texture upload requested\n";
std::cerr << " -> handle: " << *(int*) command.handle << "\n";
std::cerr << " -> sz: " << command.size << "\n";
return nullptr;
}
}
};
template <typename T>
void handle_update(T& object)
{
for (auto& promise : object.update())
{
promise.feedback_value
= std::visit(handle_command{}, promise.current_command);
}
}
static constexpr std::string_view field_type(float) { return "float"; }
static constexpr std::string_view field_type(const float (&)[2]) { return "vec2"; }
static constexpr std::string_view field_type(const float (&)[3]) { return "vec3"; }
static constexpr std::string_view field_type(const float (&)[4]) { return "vec4"; }
static constexpr std::string_view field_type(int) { return "int"; }
static constexpr std::string_view field_type(const int (&)[2]) { return "ivec2"; }
static constexpr std::string_view field_type(const int (&)[3]) { return "ivec3"; }
static constexpr std::string_view field_type(const int (&)[4]) { return "ivec4"; }
struct write_input
{
std::string& shader;
template<typename T>
void operator()(const T& field)
{
shader += fmt::format(
"layout(location = {}) in {} {};\n"
, field.location()
, field_type(field.data)
, field.name());
}
};
struct write_output
{
std::string& shader;
template<typename T>
void operator()(const T& field)
{
if constexpr(requires { field.location(); })
{
}
if constexpr(requires { field.location(); })
{
shader += fmt::format(
"layout(location = {}) out {} {};\n"
, field.location()
, field_type(field.data)
, field.name());
}
}
};
struct write_binding
{
std::string& shader;
template<typename T>
void operator()(const T& field)
{
shader += fmt::format(
" {} {};\n"
, field_type(field.value)
, field.name());
}
};
struct write_bindings
{
std::string& shader;
template<typename C>
void operator()(const C& field)
{
if constexpr (requires { C::sampler2D; }) {
shader += fmt::format(
"layout(binding = {}) uniform sampler2D {};\n\n"
, field.binding()
, field.name());
}
else if constexpr (requires { C::ubo; }) {
shader += fmt::format(
"layout({}, binding = {}) uniform {}\n{{\n"
, "std140" // TODO
, field.binding()
, field.name());
boost::pfr::for_each_field(field, write_binding{shader});
shader += fmt::format("}};\n\n");
}
}
};
int main() {
examples::GpuFilterExample ex;
std::string vstr = "#version 450\n\n";
using layout = examples::GpuFilterExample::layout;
static constexpr auto lay = layout{};
boost::pfr::for_each_field(lay.vertex_input, write_input{vstr});
boost::pfr::for_each_field(lay.vertex_output, write_output{vstr});
vstr += "\n";
boost::pfr::for_each_field(lay.bindings, write_bindings{vstr});
std::cout << "\n --- Vertex --- \n\n" << vstr << ex.vertex() << std::endl;
std::string fstr = "#version 450\n\n";
boost::pfr::for_each_field(lay.fragment_input, write_input{fstr});
boost::pfr::for_each_field(lay.fragment_output, write_output{fstr});
fstr += "\n";
boost::pfr::for_each_field(lay.bindings, write_bindings{fstr});
std::cout << "\n --- Fragment --- \n\n" << fstr << ex.fragment() << std::endl;
std::cout << "\n --- Fake commands --- \n" << std::endl;
handle_update(ex);
}