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.hpp
201 lines (169 loc) · 4.21 KB
/
gpp.hpp
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
#pragma once
#include "helpers.hpp"
namespace examples
{
// Define the layout of our pipeline in C++ simply thorugh the structure of a struct
struct layout
{
// We state that this layout is for a graphics pipeline
enum { graphics };
// Define the vertex inputs
struct vertex_input
{
// Of course in practice we'd use some custom types or macros here,
// this is just to show that there's no magic or special type involved
struct {
halp_meta(name, "v_position");
halp_flag(position);
static constexpr int location() { return 0; }
float data[3];
} vertex;
struct {
halp_meta(name, "v_texcoord");
halp_flag(texcoord);
static constexpr int location() { return 1; }
float data[2];
} texcoord;
} vertex_input;
// Define the vertex outputs
struct vertex_output {
struct {
halp_meta(name, "texcoord");
static constexpr int location() { return 0; }
float data[2];
} texcoord;
struct {
halp_meta(name, "gl_Position");
enum { per_vertex };
float data[4];
} position;
} vertex_output;
// Define the fragment inputs
struct fragment_input {
struct {
halp_meta(name, "texcoord");
static constexpr int location() { return 0; }
float data[2];
} texcoord;
} fragment_input;
// Define the fragment outputs
struct fragment_output {
struct {
halp_meta(name, "fragColor");
static constexpr int location() { return 0; }
float data[4];
} fragColor;
} fragment_output;
// Define the ubos, samplers, etc.
struct bindings
{
struct custom_ubo {
halp_meta(name, "custom");
enum { std140 };
enum { ubo };
static constexpr int binding() { return 0; }
struct
{
halp_meta(name, "Foo");
float value[2];
} pad;
struct
{
halp_meta(name, "Bar");
float value;
} slider;
} ubo;
struct sampler
{
halp_meta(name, "tex");
enum { sampler2D };
static constexpr int binding() { return 1; }
} texture_input;
} bindings;
};
struct GpuFilterExample
{
halp_meta(name, "My GPU pipeline");
halp_meta(uuid, "6addd716-5c9f-4b2d-a9ef-6b7a3dcf8697");
using layout = examples::layout;
using bindings = decltype(layout::bindings);
struct {
// If samplers & buffers are here they will be automatically allocated
// as they are expect to come from "outside"
// struct {
// halp_meta(name, "Brightness");
// enum widget { hslider };
// static constexpr auto uniform() { return &bindings::custom_ubo::slider; }
// } color_att;
} inputs;
struct {
struct {
halp_meta(name, "Color output");
enum attachment { color0 };
} color_att;
} outputs;
std::string_view vertex()
{
return R"_(
void main()
{
texcoord = v_texcoord;
gl_Position = vec4(v_position.x / 3., v_position.y / 3, 0.0, 1.);
}
)_";
}
std::string_view fragment()
{
return R"_(
void main()
{
fragColor = vec4(texture(tex, texcoord.xy).rgb, 1.0) ;
}
)_";
}
std::vector<float> buf;
std::vector<uint8_t> tex;
gpu::buffer_handle buf_handle{};
gpu::texture_handle tex_handle{};
gpu::co_update update()
{
constexpr int ubo_size = gpu::std140_size<bindings::custom_ubo>();
if (!buf_handle)
{
// Resize our local, CPU-side buffer
buf.resize(ubo_size);
// Request the creation of a GPU buffer
this->buf_handle = co_yield gpu::dynamic_ubo_allocation{
.binding = gpu::binding<bindings::custom_ubo>()
, .size = ubo_size
};
}
// Upload some data into it
co_yield gpu::dynamic_ubo_upload{
.handle = buf_handle,
.offset = 0,
.size = ubo_size,
.data = buf.data()
};
// Same for the texture
int sz = 16*16*4;
if(!tex_handle)
{
this->tex_handle = co_yield gpu::texture_allocation{
.binding = gpu::binding<bindings::sampler>()
, .width = 16
, .height = 16
};
}
tex.resize(sz);
for(int i = 0; i < sz; i++)
tex[i] = rand();
co_yield gpu::texture_upload{
.handle = tex_handle
, .offset = 0
, .size = sz
, .data = tex.data()
};
}
};
}