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-compute.hpp
214 lines (179 loc) · 5.16 KB
/
gpp-compute.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
202
203
204
205
206
207
208
209
210
211
212
213
214
#pragma once
#include "helpers.hpp"
#include <halp/static_string.hpp>
#include <halp/controls.hpp>
#include <avnd/common/member_reflection.hpp>
#include <fmt/format.h>
#include <fmt/printf.h>
namespace examples
{
struct GpuComputeExample
{
// halp_meta is a short hand for defining a static function:
// #define halp_meta(name, val) static constexpr auto name() return { val; }
halp_meta(name, "My GPU pipeline");
halp_meta(uuid, "03bce361-a2ca-4959-95b4-6aac3b6c07b5");
static constexpr int downscale = 16;
// Define the layout of our pipeline in C++ simply through the structure of a struct
struct layout
{
halp_meta(local_size_x, 16)
halp_meta(local_size_y, 16)
halp_meta(local_size_z, 1)
halp_flags(compute);
struct bindings
{
// Each binding is a struct member
struct {
halp_meta(name, "my_buf");
halp_meta(binding, 0);
halp_flags(std140, buffer, load, store);
using color = float[4];
gpu::uniform<"result", color*> values;
} my_buf;
// Define the members of our ubos
struct custom_ubo {
halp_meta(name, "custom");
halp_meta(binding, 1);
halp_flags(std140, ubo);
gpu::uniform<"width", int> width;
gpu::uniform<"height", int> height;
} ubo;
struct {
halp_meta(name, "img")
halp_meta(format, "rgba32f")
halp_meta(binding, 2);
halp_flags(image2D, readonly);
} image;
} bindings;
};
using bindings = decltype(layout::bindings);
using uniforms = decltype(bindings::ubo);
// Definition of our ports which will get parsed by the
// software that instantiate this class
struct {
// Here we use some helper types in the usual fashion
gpu::image_input_port<"Image", &bindings::image> tex;
gpu::uniform_control_port<
halp::hslider_i32<"Width", halp::range{1, 1000, 100}>
, &uniforms::width
> width;
gpu::uniform_control_port<
halp::hslider_i32<"Height", halp::range{1, 1000, 100}>
, &uniforms::height
> height;
} inputs;
// The output port on which we write the average color
struct {
struct {
halp_meta(name, "color")
float value[4];
} color_out;
} outputs;
std::string_view compute()
{
return R"_(
void main()
{
// Note: the algorithm is most likely wrong as I know FUCK ALL
// about compute shaders ; fixes welcome ;p
ivec2 call = ivec2(gl_GlobalInvocationID.xy);
vec4 color = vec4(0,0,0,0);
for(int i = 0; i < gl_WorkGroupSize.x; i++)
{
for(int j = 0; j < gl_WorkGroupSize.y; j++)
{
uint x = call.x * gl_WorkGroupSize.x + i;
uint y = call.y * gl_WorkGroupSize.y + j;
if (x < width && y < height)
{
color += imageLoad(img, ivec2(x,y));
}
}
}
if(gl_LocalInvocationIndex < ((width * height) / gl_WorkGroupSize.x * gl_WorkGroupSize.y))
{
result[gl_GlobalInvocationID.y * gl_WorkGroupSize.x + gl_GlobalInvocationID.x] = color;
}
}
)_";
}
// Allocate and update buffers
gpu::co_update update()
{
// Deallocate if the size changed
const int w = this->inputs.width / downscale;
const int h = this->inputs.height / downscale;
if(last_w != w || last_h != h)
{
if(this->buf) {
co_yield gpu::buffer_release{.handle = buf};
buf = nullptr;
}
last_w = w;
last_h = h;
}
if(w > 0 && h > 0)
{
// No buffer: reallocate
const int bytes = w * h * sizeof(float) * 4;
if(!this->buf)
{
this->buf = co_yield gpu::static_allocation{
.binding = lay.bindings.my_buf.binding()
, .size = bytes
};
}
}
}
// Relaease allocated data
gpu::co_release release()
{
if(buf) {
co_yield gpu::buffer_release{.handle = buf};
buf = nullptr;
}
}
// Do the GPU dispatch call
gpu::co_dispatch dispatch()
{
if(!buf)
co_return;
const int w = this->inputs.width / downscale;
const int h = this->inputs.height / downscale;
const int bytes = w * h * sizeof(float) * 4;
// Run a pass
co_yield gpu::begin_compute_pass{};
co_yield gpu::compute_dispatch{.x = 1, .y = 1, .z = 1};
// Request an asynchronous readback
gpu::buffer_awaiter readback = co_yield gpu::readback_buffer{
.handle = buf
, .offset = 0
, .size = bytes
};
co_yield gpu::end_compute_pass{};
// The readback can be fetched once the compute pass is done
// (this needs to be improved in terms of asyncness)
auto [data, size] = co_yield readback;
using color = float[4];
auto flt = reinterpret_cast<const color*>(data);
// finish summing on the cpu
auto& final = outputs.color_out.value;
std::ranges::fill(final, 0.f);
for(int i = 0; i < w * h; i++) {
for(int j = 0; j < 4; j++)
final[j] += flt[i][j];
}
double pixels_total = this->inputs.width * this->inputs.height;
final[0] /= pixels_total;
final[1] /= pixels_total;
final[2] /= pixels_total;
final[3] /= pixels_total;
}
private:
static constexpr auto lay = layout{};
int last_w{}, last_h{};
gpu::buffer_handle buf{};
std::vector<float> zeros{};
};
}