-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathogler_compute.hpp
214 lines (198 loc) · 7.43 KB
/
ogler_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
/*
Ogler - Use GLSL shaders in REAPER
Copyright (C) 2023 Francesco Bertolaccini <[email protected]>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
Additional permission under GNU GPL version 3 section 7
If you modify this Program, or any covered work, by linking or
combining it with Sciter (or a modified version of that library),
containing parts covered by the terms of Sciter's EULA, the licensors
of this Program grant you additional permission to convey the
resulting work.
*/
#pragma once
#include "ogler.hpp"
#include "ogler_uniforms.hpp"
namespace ogler {
struct SpecializationData {
unsigned gmem_size;
int ogler_version_maj;
int ogler_version_min;
int ogler_version_rev;
};
struct Ogler::Compute {
vk::raii::ShaderModule shader;
vk::raii::DescriptorSetLayout descriptor_set_layout;
vk::raii::DescriptorPool descriptor_pool;
vk::raii::DescriptorSet descriptor_set;
vk::raii::PipelineCache pipeline_cache;
vk::raii::PipelineLayout pipeline_layout;
std::array<vk::SpecializationMapEntry, 4> pipeline_spec_entries{
// ogler_gmem_size
vk::SpecializationMapEntry{
.constantID = 0,
.offset =
static_cast<uint32_t>(offsetof(SpecializationData, gmem_size)),
.size = sizeof(SpecializationData::gmem_size),
},
// ogler_version_maj
vk::SpecializationMapEntry{
.constantID = 1,
.offset = static_cast<uint32_t>(
offsetof(SpecializationData, ogler_version_maj)),
.size = sizeof(SpecializationData::ogler_version_maj),
},
// ogler_version_min
vk::SpecializationMapEntry{
.constantID = 2,
.offset = static_cast<uint32_t>(
offsetof(SpecializationData, ogler_version_min)),
.size = sizeof(SpecializationData::ogler_version_min),
},
// ogler_version_rev
vk::SpecializationMapEntry{
.constantID = 3,
.offset = static_cast<uint32_t>(
offsetof(SpecializationData, ogler_version_rev)),
.size = sizeof(SpecializationData::ogler_version_rev),
},
};
SpecializationData pipeline_spec_data{
.gmem_size = gmem_size,
.ogler_version_maj = version::major,
.ogler_version_min = version::minor,
.ogler_version_rev = version::revision,
};
vk::SpecializationInfo pipeline_spec_info{
.mapEntryCount = static_cast<uint32_t>(pipeline_spec_entries.size()),
.pMapEntries = pipeline_spec_entries.data(),
.dataSize = static_cast<uint32_t>(sizeof(pipeline_spec_data)),
.pData = &pipeline_spec_data,
};
vk::raii::Pipeline pipeline;
static inline vk::raii::DescriptorSetLayout
create_descriptor_set_layout(VulkanContext &ctx) {
std::vector<vk::DescriptorSetLayoutBinding> bindings = {
// Input texture
{
.binding = 1,
.descriptorType = vk::DescriptorType::eCombinedImageSampler,
.descriptorCount = max_num_inputs,
.stageFlags = vk::ShaderStageFlagBits::eCompute,
},
// Output texture
{
.binding = 2,
.descriptorType = vk::DescriptorType::eStorageImage,
.descriptorCount = 1,
.stageFlags = vk::ShaderStageFlagBits::eCompute,
},
// gmem
{
.binding = 3,
.descriptorType = vk::DescriptorType::eStorageBuffer,
.descriptorCount = 1,
.stageFlags = vk::ShaderStageFlagBits::eCompute,
},
// iChannelResolution[]
{
.binding = 4,
.descriptorType = vk::DescriptorType::eUniformBuffer,
.descriptorCount = 1,
.stageFlags = vk::ShaderStageFlagBits::eCompute,
},
// ogler_previous_frame
{
.binding = 5,
.descriptorType = vk::DescriptorType::eCombinedImageSampler,
.descriptorCount = 1,
.stageFlags = vk::ShaderStageFlagBits::eCompute,
},
// Params
{
.binding = 0,
.descriptorType = vk::DescriptorType::eUniformBuffer,
.descriptorCount = 1,
.stageFlags = vk::ShaderStageFlagBits::eCompute,
},
};
vk::DescriptorSetLayoutCreateInfo layout_info{
.bindingCount = static_cast<uint32_t>(bindings.size()),
.pBindings = bindings.data(),
};
return ctx.device.createDescriptorSetLayout(layout_info);
}
static inline vk::raii::DescriptorPool
create_descriptor_pool(VulkanContext &ctx) {
std::vector<vk::DescriptorPoolSize> pool_sizes = {
// Input texture
{
.type = vk::DescriptorType::eCombinedImageSampler,
.descriptorCount = max_num_inputs,
},
// Output texture
{
.type = vk::DescriptorType::eStorageImage,
.descriptorCount = 1,
},
// gmem
{
.type = vk::DescriptorType::eStorageBuffer,
.descriptorCount = 1,
},
// iChannelResolution[]
{
.type = vk::DescriptorType::eUniformBuffer,
.descriptorCount = 1,
},
// ogler_previous_frame
{
.type = vk::DescriptorType::eCombinedImageSampler,
.descriptorCount = 1,
},
// Params
{
.type = vk::DescriptorType::eUniformBuffer,
.descriptorCount = 1,
},
};
vk::DescriptorPoolCreateInfo create_info{
.flags = vk::DescriptorPoolCreateFlagBits::eFreeDescriptorSet,
.maxSets = 1,
.poolSizeCount = static_cast<uint32_t>(pool_sizes.size()),
.pPoolSizes = pool_sizes.data(),
};
return ctx.device.createDescriptorPool(create_info);
}
static inline vk::raii::DescriptorSet
create_descriptor_set(VulkanContext &ctx, vk::raii::DescriptorPool &pool,
vk::raii::DescriptorSetLayout &layout) {
vk::DescriptorSetAllocateInfo alloc_info{
.descriptorPool = *pool,
.descriptorSetCount = 1,
.pSetLayouts = &*layout,
};
return std::move(ctx.device.allocateDescriptorSets(alloc_info).front());
}
Compute(VulkanContext &ctx, const std::vector<unsigned> &shader_code)
: shader(ctx.create_shader_module(shader_code)),
descriptor_set_layout(create_descriptor_set_layout(ctx)),
descriptor_pool(create_descriptor_pool(ctx)),
descriptor_set(
create_descriptor_set(ctx, descriptor_pool, descriptor_set_layout)),
pipeline_cache(ctx.create_pipeline_cache()),
pipeline_layout(ctx.create_pipeline_layout(descriptor_set_layout,
sizeof(Uniforms))),
pipeline(ctx.create_compute_pipeline(shader, "main", pipeline_layout,
pipeline_cache,
&pipeline_spec_info)) {}
};
} // namespace ogler