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-helpers.hpp
149 lines (120 loc) · 3.49 KB
/
gpp-helpers.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
#pragma once
#include "helpers.hpp"
#include <halp/static_string.hpp>
#include <halp/controls.hpp>
#include <avnd/common/member_reflection.hpp>
namespace examples
{
// Define the layout of our pipeline in C++ simply through the structure of a struct
struct layout
{
halp_flags(graphics);
struct vertex_input {
gpp_attribute(0, v_position, float[3], position) pos;
gpp_attribute(1, v_texcoord, float[2], texcoord) tex;
} vertex_input;
struct vertex_output {
gpp_attribute(0, texcoord,float[2], texcoord) tex;
gpu::vertex_position_out position;
} vertex_output;
struct fragment_input {
gpp_attribute(0, texcoord, float[2], texcoord) tex;
} fragment_input;
struct fragment_output {
gpp_attribute(0, fragColor, float[4], color) col;
} fragment_output;
// Define the ubos, samplers, etc.
struct bindings
{
struct custom_ubo {
halp_meta(name, "custom");
halp_flags(std140, ubo);
static constexpr int binding() { return 0; }
gpu::uniform<"foo", float[2]> pad;
gpu::uniform<"bar", float> slider;
} ubo;
gpu::sampler<"tex", 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);
using uniforms = decltype(bindings::ubo);
static constexpr auto lay = layout{};
struct {
// If samplers & buffers are referenced here the GPU side of things
// will be automatically allocated as they are expect to come from "outside"
gpu::uniform_control_port<halp::hslider_f32<"Alpha">, &uniforms::slider> bright;
// It's also possible to have purely CPU-side controls to manage e.g. texture sizes, etc...
halp::hslider_f32<"Other"> other;
} inputs;
struct {
gpu::color_attachment_port<"Main out", &layout::fragment_output::col> fragColor;
} 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 * foo).rgb, bar) ;
}
)_";
}
std::vector<uint8_t> tex;
gpu::texture_handle tex_handle{};
gpu::co_update update()
{
// In this example we test the automatic UBO filling with the inputs declared above.
// Here the surrounding environment makes sure that the UBO already has a handle
auto ubo = co_yield gpu::get_ubo_handle{
.binding = lay.bindings.ubo.binding()
};
// Upload some data into it, using an input (non-uniform) of our node
using namespace std;
float xy[2] = {cos(inputs.other), sin(inputs.other)};
co_yield gpu::dynamic_ubo_upload{
.handle = ubo,
.offset = 0,
.size = sizeof(xy),
.data = &xy
};
// The sampler is not used by the inputs block, so we have to allocate it ourselves
int sz = 16*16*4;
if(!tex_handle)
{
this->tex_handle = co_yield gpu::texture_allocation{
.binding = lay.bindings.texture_input.binding()
, .width = 16
, .height = 16
};
}
// And upload some data
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()
};
}
gpu::co_release release()
{
co_yield gpu::texture_release{.handle = tex_handle};
}
};
}