forked from liblava/liblava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerics.cpp
More file actions
294 lines (238 loc) · 10.6 KB
/
generics.cpp
File metadata and controls
294 lines (238 loc) · 10.6 KB
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/**
* @file liblava-demo/generics.cpp
* @brief Generics demo
* @authors Lava Block OÜ and contributors
* @copyright Copyright (c) 2018-present, MIT License
*/
#include "imgui.h"
#include "liblava/lava.hpp"
using namespace lava;
//-----------------------------------------------------------------------------
name _triangle_frag_ = "triangle_frag";
name _float_triangle_ = "float_triangle";
name _int_triangle_ = "int_triangle";
name _double_triangle_ = "double_triangle";
//-----------------------------------------------------------------------------
#ifdef LAVA_DEMO
LAVA_STAGE(2, "generics") {
#else
int main(int argc, char* argv[]) {
argh::parser argh(argc, argv);
#endif
engine app("lava generics", argh);
bool int_supported = false;
bool double_supported = false;
app.platform.on_create_param = [&](device::create_param& param) {
auto& physical_device = param.physical_device;
// check int support
int_supported = support_vertex_buffer_format(physical_device->get(),
VK_FORMAT_R32G32B32_SINT);
if (!int_supported)
logger()->warn("int vertex buffer format is not supported");
// check double support
if (physical_device->get_features().shaderFloat64
&& support_vertex_buffer_format(physical_device->get(),
VK_FORMAT_R64G64B64_SFLOAT)) {
// activate device feature
param.features.shaderFloat64 = true;
double_supported = true;
} else
logger()->warn("double vertex buffer format is not supported");
};
app.props.add(_triangle_frag_, "generics/triangle.frag");
app.props.add(_float_triangle_, "generics/float_triangle.vert");
app.props.add(_int_triangle_, "generics/int_triangle.vert");
app.props.add(_double_triangle_, "generics/double_triangle.vert");
if (!app.setup())
return error::not_ready;
// initialize a float triangle
mesh::s_ptr float_triangle;
// these template arguments are optional
float_triangle = create_mesh<vertex,
false, true, false>(app.device,
mesh_type::triangle);
if (!float_triangle)
return error::create_failed;
mesh_data& float_triangle_data = float_triangle->get_data();
float_triangle_data.vertices.at(0).color = v4(1.f, 0.f, 0.f, 1.f);
float_triangle_data.vertices.at(1).color = v4(0.f, 1.f, 0.f, 1.f);
float_triangle_data.vertices.at(2).color = v4(0.f, 0.f, 1.f, 1.f);
float_triangle_data.scale(0.5f);
float_triangle_data.move({0.5f, 0, 0});
if (!float_triangle->reload())
return error::create_failed;
// initialize an int triangle
struct int_vertex {
std::array<int, 3> position;
v4 color;
};
mesh_template<int_vertex>::s_ptr int_triangle;
if (int_supported) {
// except for the first one, these template arguments are optional
int_triangle = create_mesh<int_vertex,
false, true, false,
true, false, false>(app.device,
mesh_type::triangle);
if (!int_triangle)
return error::create_failed;
mesh_template_data<int_vertex>& int_triangle_data = int_triangle->get_data();
int_triangle_data.vertices.at(0).color = v4(1.f, 0.5f, 0.5f, 1.f);
int_triangle_data.vertices.at(1).color = v4(0.5f, 1.f, 0.5f, 1.f);
int_triangle_data.vertices.at(2).color = v4(0.5f, 0.5f, 1.f, 1.f);
int_triangle_data.scale(2);
if (!int_triangle->reload())
return error::create_failed;
}
// initialize a double triangle
struct double_vertex {
std::array<double, 3> position;
v4 color;
};
mesh_template<double_vertex>::s_ptr double_triangle;
if (double_supported) {
// except for the first one, these template arguments are optional
double_triangle = create_mesh<double_vertex,
false, true, false,
true, false, false>(app.device,
mesh_type::triangle);
if (!double_triangle)
return error::create_failed;
mesh_template_data<double_vertex>& double_triangle_data = double_triangle->get_data();
double_triangle_data.vertices.at(0).color = v4(1.f, 0.f, 0.5f, 1.f);
double_triangle_data.vertices.at(1).color = v4(0.f, 1.f, 0.5f, 1.f);
double_triangle_data.vertices.at(2).color = v4(0.f, 0.5f, 1.f, 1.f);
double_triangle_data.scale(0.854);
if (!double_triangle->reload())
return error::create_failed;
}
render_pipeline::s_ptr float_pipeline;
render_pipeline::s_ptr int_pipeline;
render_pipeline::s_ptr double_pipeline;
pipeline_layout::s_ptr layout;
app.on_create = [&]() {
render_pass::s_ptr render_pass = app.shading.get_pass();
layout = pipeline_layout::make();
if (!layout->create(app.device))
return false;
// making a float triangle pipeline
float_pipeline = render_pipeline::make(app.device, app.pipeline_cache);
float_pipeline->add_color_blend_attachment();
float_pipeline->on_process = [&](VkCommandBuffer cmd_buf) {
float_triangle->bind_draw(cmd_buf);
};
if (int_supported) {
// making an int triangle pipeline
int_pipeline = render_pipeline::make(app.device, app.pipeline_cache);
int_pipeline->add_color_blend_attachment();
int_pipeline->on_process = [&](VkCommandBuffer cmd_buf) {
int_triangle->bind_draw(cmd_buf);
};
}
if (double_supported) {
// making an double triangle pipeline
double_pipeline = render_pipeline::make(app.device, app.pipeline_cache);
double_pipeline->add_color_blend_attachment();
double_pipeline->on_process = [&](VkCommandBuffer cmd_buf) {
double_triangle->bind_draw(cmd_buf);
};
}
pipeline::shader_stage::s_ptr shader_stage = create_pipeline_shader_stage(
app.device,
app.producer.get_shader(_triangle_frag_),
VK_SHADER_STAGE_FRAGMENT_BIT);
if (!shader_stage)
return false;
// describe the float triangle
if (!float_pipeline->add_shader(app.producer.get_shader(_float_triangle_),
VK_SHADER_STAGE_VERTEX_BIT))
return false;
float_pipeline->add(shader_stage);
float_pipeline->set_vertex_input_binding({0, sizeof(vertex),
VK_VERTEX_INPUT_RATE_VERTEX});
float_pipeline->set_vertex_input_attributes({
{0, 0, VK_FORMAT_R32G32B32_SFLOAT, to_ui32(offsetof(vertex, position))},
{1, 0, VK_FORMAT_R32G32B32A32_SFLOAT, to_ui32(offsetof(vertex, color))},
});
float_pipeline->set_layout(layout);
if (!float_pipeline->create(render_pass->get()))
return false;
if (int_supported) {
// describe the int triangle
if (!int_pipeline->add_shader(app.producer.get_shader(_int_triangle_),
VK_SHADER_STAGE_VERTEX_BIT))
return false;
int_pipeline->add(shader_stage);
int_pipeline->set_vertex_input_binding({0,
sizeof(int_vertex),
VK_VERTEX_INPUT_RATE_VERTEX});
int_pipeline->set_vertex_input_attributes({
{0, 0, VK_FORMAT_R32G32B32_SINT, to_ui32(offsetof(int_vertex, position))},
{1, 0, VK_FORMAT_R32G32B32A32_SFLOAT, to_ui32(offsetof(int_vertex, color))},
});
int_pipeline->set_layout(layout);
if (!int_pipeline->create(render_pass->get()))
return false;
}
if (double_supported) {
// describe the double triangle
if (!double_pipeline->add_shader(app.producer.get_shader(_double_triangle_),
VK_SHADER_STAGE_VERTEX_BIT))
return false;
double_pipeline->add(shader_stage);
double_pipeline->set_vertex_input_binding({0,
sizeof(double_vertex),
VK_VERTEX_INPUT_RATE_VERTEX});
double_pipeline->set_vertex_input_attributes({
{0, 0, VK_FORMAT_R64G64B64_SFLOAT,
to_ui32(offsetof(double_vertex, position))},
{2, 0, VK_FORMAT_R32G32B32A32_SFLOAT,
to_ui32(offsetof(double_vertex, color))},
});
double_pipeline->set_layout(layout);
if (!double_pipeline->create(render_pass->get()))
return false;
}
render_pass->add_front(float_pipeline);
if (double_supported)
render_pass->add_front(double_pipeline);
if (int_supported)
render_pass->add_front(int_pipeline);
return true;
};
app.on_destroy = [&]() {
float_pipeline->destroy();
if (int_supported)
int_pipeline->destroy();
if (double_supported)
double_pipeline->destroy();
layout->destroy();
};
app.imgui.layers.add("info", [&]() {
ImGui::SetNextWindowPos({30, 30}, ImGuiCond_FirstUseEver);
ImGui::SetNextWindowSize({220, 200}, ImGuiCond_FirstUseEver);
ImGui::Begin(app.get_name());
bool float_active = float_pipeline->activated();
if (ImGui::Checkbox("float triangle", &float_active))
float_pipeline->toggle();
if (int_supported) {
bool int_active = int_pipeline->activated();
if (ImGui::Checkbox("int triangle", &int_active))
int_pipeline->toggle();
}
if (double_supported) {
bool double_active = double_pipeline->activated();
if (ImGui::Checkbox("double triangle", &double_active))
double_pipeline->toggle();
}
app.draw_about();
ImGui::End();
});
app.add_run_end([&]() {
float_pipeline->destroy();
if (int_supported)
int_triangle->destroy();
if (double_supported)
double_triangle->destroy();
});
return app.run();
}