-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathkernel.cpp
472 lines (413 loc) · 17.7 KB
/
kernel.cpp
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
// Copyright 2018 The clvk authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <algorithm>
#include "clspv/Sampler.h"
#include "kernel.hpp"
#include "memory.hpp"
cl_ulong cvk_kernel::local_mem_size() const {
cl_ulong ret = 0; // FIXME take the compile-time allocations into account
for (uint32_t i = 0; i < m_args.size(); i++) {
auto const& arg = m_args[i];
if (arg.kind == kernel_argument_kind::local) {
ret += m_argument_values->local_arg_size(i);
}
}
return ret;
}
cl_int cvk_kernel::init() {
cl_int errcode;
m_entry_point = m_program->get_entry_point(m_name, &errcode);
if (!m_entry_point) {
return errcode;
}
// Store a copy of the arguments
m_args = m_entry_point->args();
if (const auto* md = m_entry_point->image_metadata()) {
m_image_metadata = md;
}
if (const auto* md = m_entry_point->sampler_metadata()) {
m_sampler_metadata = md;
}
// Init argument values
m_argument_values = cvk_kernel_argument_values::create(m_entry_point);
if (m_argument_values == nullptr) {
return CL_OUT_OF_RESOURCES;
}
return CL_SUCCESS;
}
VkPipeline
cvk_kernel::create_pipeline(const cvk_spec_constant_map& spec_constants) {
return m_entry_point->create_pipeline(spec_constants);
}
std::unique_ptr<cvk_kernel> cvk_kernel::clone(cl_int* errcode_ret) const {
auto kernel = std::make_unique<cvk_kernel>(m_program, m_name.c_str());
*errcode_ret = kernel->init();
if (*errcode_ret != CL_SUCCESS) {
return nullptr;
}
kernel->m_argument_values =
cvk_kernel_argument_values::create(*m_argument_values.get());
return kernel;
}
void cvk_kernel::set_image_metadata(cl_uint index, const void* image) {
if (!m_image_metadata) {
return;
}
auto md = m_image_metadata->find(index);
if (md != m_image_metadata->end()) {
auto mem = icd_downcast(*reinterpret_cast<const cl_mem*>(image));
assert(mem->is_image_type());
auto format = static_cast<cvk_image*>(mem)->format();
if (md->second.has_valid_order()) {
auto order_offset = md->second.order_offset;
auto order = format.image_channel_order;
m_argument_values->set_pod_data(order_offset, sizeof(order),
&order);
}
if (md->second.has_valid_data_type()) {
auto data_type_offset = md->second.data_type_offset;
auto data_type = format.image_channel_data_type;
m_argument_values->set_pod_data(data_type_offset, sizeof(data_type),
&data_type);
}
}
}
void cvk_kernel::set_sampler_metadata(cl_uint index, const void* sampler) {
if (!m_sampler_metadata) {
return;
}
auto md = m_sampler_metadata->find(index);
if (md != m_sampler_metadata->end()) {
auto apisampler = *reinterpret_cast<const cl_sampler*>(sampler);
auto offset = md->second;
auto sampler = icd_downcast(apisampler);
uint32_t sampler_mask = (sampler->normalized_coords()
? clspv::CLK_NORMALIZED_COORDS_TRUE
: clspv::CLK_NORMALIZED_COORDS_FALSE) |
(sampler->filter_mode() == CL_FILTER_NEAREST
? clspv::CLK_FILTER_NEAREST
: clspv::CLK_FILTER_LINEAR);
switch (sampler->addressing_mode()) {
case CL_ADDRESS_NONE:
sampler_mask |= clspv::CLK_ADDRESS_NONE;
break;
case CL_ADDRESS_CLAMP:
sampler_mask |= clspv::CLK_ADDRESS_CLAMP;
break;
case CL_ADDRESS_REPEAT:
sampler_mask |= clspv::CLK_ADDRESS_REPEAT;
break;
case CL_ADDRESS_CLAMP_TO_EDGE:
sampler_mask |= clspv::CLK_ADDRESS_CLAMP_TO_EDGE;
break;
case CL_ADDRESS_MIRRORED_REPEAT:
sampler_mask |= clspv::CLK_ADDRESS_MIRRORED_REPEAT;
break;
default:
break;
}
m_argument_values->set_pod_data(offset, sizeof(sampler_mask),
&sampler_mask);
}
}
cl_int cvk_kernel::set_arg(cl_uint index, size_t size, const void* value) {
std::lock_guard<std::mutex> lock(m_lock);
// Clone argument values if they have been used in an enqueue
if (m_argument_values->is_enqueued()) {
m_argument_values =
cvk_kernel_argument_values::create(*m_argument_values);
if (m_argument_values == nullptr) {
return CL_OUT_OF_RESOURCES;
}
}
auto const& arg = m_args[index];
cl_int ret = m_argument_values->set_arg(arg, size, value);
// if the argument is an image, we need to set its metadata
// (channel_order/channel_data_type).
if (arg.kind == kernel_argument_kind::sampled_image ||
arg.kind == kernel_argument_kind::storage_image ||
arg.kind == kernel_argument_kind::storage_texel_buffer ||
arg.kind == kernel_argument_kind::uniform_texel_buffer) {
set_image_metadata(index, value);
}
if (arg.kind == kernel_argument_kind::sampler) {
set_sampler_metadata(index, value);
}
return ret;
}
bool cvk_kernel::args_valid() const { return m_argument_values->args_valid(); }
bool cvk_kernel_argument_values::setup_descriptor_sets() {
std::lock_guard<std::mutex> lock(m_lock);
auto program = m_entry_point->program();
auto dev = program->context()->device()->vulkan_device();
// Do nothing if these argument values have already been used in an enqueue
if (m_is_enqueued) {
return true;
}
// Allocate descriptor sets
if (!m_entry_point->allocate_descriptor_sets(descriptor_sets())) {
return false;
}
VkDescriptorSet* ds = descriptor_sets();
// Make enough space to store all descriptor write structures
size_t max_descriptor_writes =
m_args.size() // upper bound that includes POD buffers
+ program->literal_sampler_descs().size() +
1; // module constant data buffer
std::vector<VkWriteDescriptorSet> descriptor_writes;
std::vector<VkDescriptorBufferInfo> buffer_info;
std::vector<VkDescriptorImageInfo> image_info;
std::vector<VkBufferView> buffer_views;
descriptor_writes.reserve(max_descriptor_writes);
buffer_info.reserve(max_descriptor_writes);
image_info.reserve(max_descriptor_writes);
buffer_views.reserve(max_descriptor_writes);
// Setup module-scope variables
if (program->module_constant_data_buffer() != nullptr &&
program->module_constant_data_buffer_info()->type ==
module_buffer_type::storage_buffer) {
auto buffer = program->module_constant_data_buffer();
auto info = program->module_constant_data_buffer_info();
cvk_debug_fn(
"constant data buffer %p, size = %zu @ set = %u, binding = %u",
buffer->vulkan_buffer(), buffer->size(), info->set, info->binding);
// Update descriptors
VkDescriptorBufferInfo bufferInfo = {buffer->vulkan_buffer(),
0, // offset
VK_WHOLE_SIZE};
buffer_info.push_back(bufferInfo);
VkWriteDescriptorSet writeDescriptorSet = {
VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
nullptr,
ds[info->set],
info->binding, // dstBinding
0, // dstArrayElement
1, // descriptorCount
VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, // descriptorType
nullptr, // pImageInfo
&buffer_info.back(),
nullptr, // pTexelBufferView
};
descriptor_writes.push_back(writeDescriptorSet);
}
// Setup descriptors for POD arguments
if (m_entry_point->has_pod_buffer_arguments()) {
// Create POD buffer
if (!create_pod_buffer()) {
return false;
}
// Update descriptors
cvk_debug_fn("pod buffer %p, size = %zu @ set = %u, binding = %u",
m_pod_buffer->vulkan_buffer(), m_pod_buffer->size(),
m_pod_arg->descriptorSet, m_pod_arg->binding);
VkDescriptorBufferInfo bufferInfo = {m_pod_buffer->vulkan_buffer(),
0, // offset
VK_WHOLE_SIZE};
buffer_info.push_back(bufferInfo);
VkWriteDescriptorSet writeDescriptorSet = {
VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
nullptr,
ds[m_pod_arg->descriptorSet],
m_pod_arg->binding, // dstBinding
0, // dstArrayElement
1, // descriptorCount
m_entry_point->pod_descriptor_type(), // descriptorType
nullptr, // pImageInfo
&buffer_info.back(),
nullptr, // pTexelBufferView
};
descriptor_writes.push_back(writeDescriptorSet);
}
// Setup other kernel argument descriptors
for (cl_uint i = 0; i < m_args.size(); i++) {
auto const& arg = m_args[i];
switch (arg.kind) {
case kernel_argument_kind::buffer:
case kernel_argument_kind::buffer_ubo: {
auto buffer = static_cast<cvk_buffer*>(get_arg_value(arg));
if (buffer == nullptr) {
cvk_debug_fn("ignoring NULL buffer argument");
break;
}
auto vkbuf = buffer->vulkan_buffer();
cvk_debug_fn(
"buffer %p, offset = %zu, size = %zu @ set = %u, binding = %u",
buffer->vulkan_buffer(), buffer->vulkan_buffer_offset(),
buffer->size(), arg.descriptorSet, arg.binding);
VkDescriptorBufferInfo bufferInfo = {
vkbuf,
buffer->vulkan_buffer_offset(), // offset
buffer->size()};
buffer_info.push_back(bufferInfo);
auto descriptor_type = arg.kind == kernel_argument_kind::buffer
? VK_DESCRIPTOR_TYPE_STORAGE_BUFFER
: VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
VkWriteDescriptorSet writeDescriptorSet = {
VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
nullptr,
ds[arg.descriptorSet],
arg.binding, // dstBinding
0, // dstArrayElement
1, // descriptorCount
descriptor_type,
nullptr, // pImageInfo
&buffer_info.back(),
nullptr, // pTexelBufferView
};
descriptor_writes.push_back(writeDescriptorSet);
break;
}
case kernel_argument_kind::sampler: {
auto clsampler = static_cast<cvk_sampler*>(get_arg_value(arg));
bool normalized_coord_sampler_required = false;
if (auto md = m_entry_point->sampler_metadata()) {
normalized_coord_sampler_required = md->find(i) != md->end();
}
auto sampler =
normalized_coord_sampler_required &&
!clsampler->normalized_coords()
? clsampler
->get_or_create_vulkan_sampler_with_normalized_coords()
: clsampler->vulkan_sampler();
if (sampler == VK_NULL_HANDLE) {
cvk_error_fn("Could not set descriptor for sampler");
return false;
}
cvk_debug_fn("sampler %p @ set = %u, binding = %u", sampler,
arg.descriptorSet, arg.binding);
VkDescriptorImageInfo imageInfo = {
sampler,
VK_NULL_HANDLE, // imageView
VK_IMAGE_LAYOUT_UNDEFINED // imageLayout
};
image_info.push_back(imageInfo);
VkWriteDescriptorSet writeDescriptorSet = {
VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
nullptr,
ds[arg.descriptorSet],
arg.binding, // dstBinding
0, // dstArrayElement
1, // descriptorCount
VK_DESCRIPTOR_TYPE_SAMPLER,
&image_info.back(), // pImageInfo
nullptr, // pBufferInfo
nullptr, // pTexelBufferView
};
descriptor_writes.push_back(writeDescriptorSet);
break;
}
case kernel_argument_kind::sampled_image:
case kernel_argument_kind::storage_image: {
auto image = static_cast<cvk_image*>(get_arg_value(arg));
bool sampled = arg.kind == kernel_argument_kind::sampled_image;
auto view = sampled ? image->vulkan_sampled_view()
: image->vulkan_storage_view();
cvk_debug_fn("image view %p @ set = %u, binding = %u", view,
arg.descriptorSet, arg.binding);
VkDescriptorImageInfo imageInfo = {
VK_NULL_HANDLE,
view, // imageView
VK_IMAGE_LAYOUT_GENERAL // imageLayout
};
image_info.push_back(imageInfo);
VkDescriptorType dtype = sampled ? VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE
: VK_DESCRIPTOR_TYPE_STORAGE_IMAGE;
VkWriteDescriptorSet writeDescriptorSet = {
VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
nullptr,
ds[arg.descriptorSet],
arg.binding, // dstBinding
0, // dstArrayElement
1, // descriptorCount
dtype,
&image_info.back(), // pImageInfo
nullptr, // pBufferInfo
nullptr, // pTexelBufferView
};
descriptor_writes.push_back(writeDescriptorSet);
break;
}
case kernel_argument_kind::storage_texel_buffer:
case kernel_argument_kind::uniform_texel_buffer: {
auto image = static_cast<cvk_image*>(get_arg_value(arg));
bool uniform =
arg.kind == kernel_argument_kind::uniform_texel_buffer;
auto view = image->vulkan_buffer_view();
buffer_views.push_back(view);
cvk_debug_fn("buffer view %p @ set = %u, binding = %u", view,
arg.descriptorSet, arg.binding);
VkDescriptorType dtype =
uniform ? VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER
: VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER;
VkWriteDescriptorSet writeDescriptorSet = {
VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
nullptr,
ds[arg.descriptorSet],
arg.binding, // dstBinding
0, // dstArrayElement
1, // descriptorCount
dtype,
nullptr, // pImageInfo
nullptr, // pBufferInfo
&buffer_views.back(), // pTexelBufferView
};
descriptor_writes.push_back(writeDescriptorSet);
break;
}
case kernel_argument_kind::pod: // skip POD arguments
case kernel_argument_kind::pod_ubo:
case kernel_argument_kind::pod_pushconstant:
case kernel_argument_kind::pointer_ubo:
case kernel_argument_kind::pointer_pushconstant:
break;
case kernel_argument_kind::local: // nothing to do?
break;
case kernel_argument_kind::unused:
break;
default:
cvk_error_fn("unsupported argument type");
return false;
}
}
// Setup literal samplers
for (size_t i = 0; i < program->literal_sampler_descs().size(); i++) {
auto desc = program->literal_sampler_descs()[i];
auto clsampler = icd_downcast(program->literal_samplers()[i]);
auto sampler = clsampler->vulkan_sampler();
VkDescriptorImageInfo imageInfo = {
sampler,
VK_NULL_HANDLE, // imageView
VK_IMAGE_LAYOUT_UNDEFINED // imageLayout
};
image_info.push_back(imageInfo);
VkWriteDescriptorSet writeDescriptorSet = {
VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
nullptr,
ds[desc.descriptorSet],
desc.binding, // dstBinding
0, // dstArrayElement
1, // descriptorCount
VK_DESCRIPTOR_TYPE_SAMPLER,
&image_info.back(), // pImageInfo
nullptr, // pBufferInfo
nullptr, // pTexelBufferView
};
descriptor_writes.push_back(writeDescriptorSet);
}
m_is_enqueued = true;
// Write descriptors to device
vkUpdateDescriptorSets(dev, static_cast<uint32_t>(descriptor_writes.size()),
descriptor_writes.data(), 0, nullptr);
return true;
}