1 /*
2 * Copyright © 2022 Collabora Ltd
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24 #include "vk_meta_private.h"
25 #include "vk_meta_object_list.h"
26
27 #include "vk_command_buffer.h"
28 #include "vk_device.h"
29 #include "vk_pipeline.h"
30 #include "vk_util.h"
31
32 #include "util/hash_table.h"
33
34 #include <string.h>
35
36 struct cache_key {
37 VkObjectType obj_type;
38 uint32_t key_size;
39 const void *key_data;
40 };
41
42 static struct cache_key *
cache_key_create(VkObjectType obj_type,const void * key_data,size_t key_size)43 cache_key_create(VkObjectType obj_type, const void *key_data, size_t key_size)
44 {
45 assert(key_size <= UINT32_MAX);
46
47 struct cache_key *key = malloc(sizeof(*key) + key_size);
48 *key = (struct cache_key) {
49 .obj_type = obj_type,
50 .key_size = key_size,
51 .key_data = key + 1,
52 };
53 memcpy(key + 1, key_data, key_size);
54
55 return key;
56 }
57
58 static uint32_t
cache_key_hash(const void * _key)59 cache_key_hash(const void *_key)
60 {
61 const struct cache_key *key = _key;
62
63 assert(sizeof(key->obj_type) == 4);
64 uint32_t hash = _mesa_hash_u32(&key->obj_type);
65 return _mesa_hash_data_with_seed(key->key_data, key->key_size, hash);
66 }
67
68 static bool
cache_key_equal(const void * _a,const void * _b)69 cache_key_equal(const void *_a, const void *_b)
70 {
71 const struct cache_key *a = _a, *b = _b;
72 if (a->obj_type != b->obj_type || a->key_size != b->key_size)
73 return false;
74
75 return memcmp(a->key_data, b->key_data, a->key_size) == 0;
76 }
77
78 VkResult
vk_meta_device_init(struct vk_device * device,struct vk_meta_device * meta)79 vk_meta_device_init(struct vk_device *device,
80 struct vk_meta_device *meta)
81 {
82 memset(meta, 0, sizeof(*meta));
83
84 meta->cache = _mesa_hash_table_create(NULL, cache_key_hash,
85 cache_key_equal);
86 simple_mtx_init(&meta->cache_mtx, mtx_plain);
87
88 meta->cmd_draw_rects = vk_meta_draw_rects;
89 meta->cmd_draw_volume = vk_meta_draw_volume;
90
91 return VK_SUCCESS;
92 }
93
94 void
vk_meta_device_finish(struct vk_device * device,struct vk_meta_device * meta)95 vk_meta_device_finish(struct vk_device *device,
96 struct vk_meta_device *meta)
97 {
98 hash_table_foreach(meta->cache, entry) {
99 free((void *)entry->key);
100 vk_meta_destroy_object(device, entry->data);
101 }
102 _mesa_hash_table_destroy(meta->cache, NULL);
103 simple_mtx_destroy(&meta->cache_mtx);
104 }
105
106 uint64_t
vk_meta_lookup_object(struct vk_meta_device * meta,VkObjectType obj_type,const void * key_data,size_t key_size)107 vk_meta_lookup_object(struct vk_meta_device *meta,
108 VkObjectType obj_type,
109 const void *key_data, size_t key_size)
110 {
111 assert(key_size >= sizeof(enum vk_meta_object_key_type));
112 assert(*(enum vk_meta_object_key_type *)key_data !=
113 VK_META_OBJECT_KEY_TYPE_INVALD);
114
115 struct cache_key key = {
116 .obj_type = obj_type,
117 .key_size = key_size,
118 .key_data = key_data,
119 };
120
121 uint32_t hash = cache_key_hash(&key);
122
123 simple_mtx_lock(&meta->cache_mtx);
124 struct hash_entry *entry =
125 _mesa_hash_table_search_pre_hashed(meta->cache, hash, &key);
126 simple_mtx_unlock(&meta->cache_mtx);
127
128 if (entry == NULL)
129 return 0;
130
131 struct vk_object_base *obj = entry->data;
132 assert(obj->type == obj_type);
133
134 return (uint64_t)(uintptr_t)obj;
135 }
136
137 uint64_t
vk_meta_cache_object(struct vk_device * device,struct vk_meta_device * meta,const void * key_data,size_t key_size,VkObjectType obj_type,uint64_t handle)138 vk_meta_cache_object(struct vk_device *device,
139 struct vk_meta_device *meta,
140 const void *key_data, size_t key_size,
141 VkObjectType obj_type,
142 uint64_t handle)
143 {
144 assert(key_size >= sizeof(enum vk_meta_object_key_type));
145 assert(*(enum vk_meta_object_key_type *)key_data !=
146 VK_META_OBJECT_KEY_TYPE_INVALD);
147
148 struct cache_key *key = cache_key_create(obj_type, key_data, key_size);
149 struct vk_object_base *obj =
150 vk_object_base_from_u64_handle(handle, obj_type);
151
152 uint32_t hash = cache_key_hash(key);
153
154 simple_mtx_lock(&meta->cache_mtx);
155 struct hash_entry *entry =
156 _mesa_hash_table_search_pre_hashed(meta->cache, hash, key);
157 if (entry == NULL)
158 _mesa_hash_table_insert_pre_hashed(meta->cache, hash, key, obj);
159 simple_mtx_unlock(&meta->cache_mtx);
160
161 if (entry != NULL) {
162 /* We raced and found that object already in the cache */
163 free(key);
164 vk_meta_destroy_object(device, obj);
165 return (uint64_t)(uintptr_t)entry->data;
166 } else {
167 /* Return the newly inserted object */
168 return (uint64_t)(uintptr_t)obj;
169 }
170 }
171
172 VkResult
vk_meta_create_sampler(struct vk_device * device,struct vk_meta_device * meta,const VkSamplerCreateInfo * info,const void * key_data,size_t key_size,VkSampler * sampler_out)173 vk_meta_create_sampler(struct vk_device *device,
174 struct vk_meta_device *meta,
175 const VkSamplerCreateInfo *info,
176 const void *key_data, size_t key_size,
177 VkSampler *sampler_out)
178 {
179 const struct vk_device_dispatch_table *disp = &device->dispatch_table;
180 VkDevice _device = vk_device_to_handle(device);
181
182 VkSampler sampler;
183 VkResult result = disp->CreateSampler(_device, info, NULL, &sampler);
184 if (result != VK_SUCCESS)
185 return result;
186
187 *sampler_out = (VkSampler)
188 vk_meta_cache_object(device, meta, key_data, key_size,
189 VK_OBJECT_TYPE_SAMPLER,
190 (uint64_t)sampler);
191 return VK_SUCCESS;
192 }
193
194 VkResult
vk_meta_create_descriptor_set_layout(struct vk_device * device,struct vk_meta_device * meta,const VkDescriptorSetLayoutCreateInfo * info,const void * key_data,size_t key_size,VkDescriptorSetLayout * layout_out)195 vk_meta_create_descriptor_set_layout(struct vk_device *device,
196 struct vk_meta_device *meta,
197 const VkDescriptorSetLayoutCreateInfo *info,
198 const void *key_data, size_t key_size,
199 VkDescriptorSetLayout *layout_out)
200 {
201 const struct vk_device_dispatch_table *disp = &device->dispatch_table;
202 VkDevice _device = vk_device_to_handle(device);
203
204 VkDescriptorSetLayout layout;
205 VkResult result = disp->CreateDescriptorSetLayout(_device, info,
206 NULL, &layout);
207 if (result != VK_SUCCESS)
208 return result;
209
210 *layout_out = (VkDescriptorSetLayout)
211 vk_meta_cache_object(device, meta, key_data, key_size,
212 VK_OBJECT_TYPE_DESCRIPTOR_SET_LAYOUT,
213 (uint64_t)layout);
214 return VK_SUCCESS;
215 }
216
217 static VkResult
vk_meta_get_descriptor_set_layout(struct vk_device * device,struct vk_meta_device * meta,const VkDescriptorSetLayoutCreateInfo * info,const void * key_data,size_t key_size,VkDescriptorSetLayout * layout_out)218 vk_meta_get_descriptor_set_layout(struct vk_device *device,
219 struct vk_meta_device *meta,
220 const VkDescriptorSetLayoutCreateInfo *info,
221 const void *key_data, size_t key_size,
222 VkDescriptorSetLayout *layout_out)
223 {
224 VkDescriptorSetLayout cached =
225 vk_meta_lookup_descriptor_set_layout(meta, key_data, key_size);
226 if (cached != VK_NULL_HANDLE) {
227 *layout_out = cached;
228 return VK_SUCCESS;
229 }
230
231 return vk_meta_create_descriptor_set_layout(device, meta, info,
232 key_data, key_size,
233 layout_out);
234 }
235
236 VkResult
vk_meta_create_pipeline_layout(struct vk_device * device,struct vk_meta_device * meta,const VkPipelineLayoutCreateInfo * info,const void * key_data,size_t key_size,VkPipelineLayout * layout_out)237 vk_meta_create_pipeline_layout(struct vk_device *device,
238 struct vk_meta_device *meta,
239 const VkPipelineLayoutCreateInfo *info,
240 const void *key_data, size_t key_size,
241 VkPipelineLayout *layout_out)
242 {
243 const struct vk_device_dispatch_table *disp = &device->dispatch_table;
244 VkDevice _device = vk_device_to_handle(device);
245
246 VkPipelineLayout layout;
247 VkResult result = disp->CreatePipelineLayout(_device, info, NULL, &layout);
248 if (result != VK_SUCCESS)
249 return result;
250
251 *layout_out = (VkPipelineLayout)
252 vk_meta_cache_object(device, meta, key_data, key_size,
253 VK_OBJECT_TYPE_PIPELINE_LAYOUT,
254 (uint64_t)layout);
255 return VK_SUCCESS;
256 }
257
258 VkResult
vk_meta_get_pipeline_layout(struct vk_device * device,struct vk_meta_device * meta,const VkDescriptorSetLayoutCreateInfo * desc_info,const VkPushConstantRange * push_range,const void * key_data,size_t key_size,VkPipelineLayout * layout_out)259 vk_meta_get_pipeline_layout(struct vk_device *device,
260 struct vk_meta_device *meta,
261 const VkDescriptorSetLayoutCreateInfo *desc_info,
262 const VkPushConstantRange *push_range,
263 const void *key_data, size_t key_size,
264 VkPipelineLayout *layout_out)
265 {
266 VkPipelineLayout cached =
267 vk_meta_lookup_pipeline_layout(meta, key_data, key_size);
268 if (cached != VK_NULL_HANDLE) {
269 *layout_out = cached;
270 return VK_SUCCESS;
271 }
272
273 VkDescriptorSetLayout set_layout = VK_NULL_HANDLE;
274 if (desc_info != NULL) {
275 VkResult result =
276 vk_meta_get_descriptor_set_layout(device, meta, desc_info,
277 key_data, key_size, &set_layout);
278 if (result != VK_SUCCESS)
279 return result;
280 }
281
282 const VkPipelineLayoutCreateInfo layout_info = {
283 .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
284 .setLayoutCount = set_layout != VK_NULL_HANDLE ? 1 : 0,
285 .pSetLayouts = &set_layout,
286 .pushConstantRangeCount = push_range != NULL ? 1 : 0,
287 .pPushConstantRanges = push_range,
288 };
289
290 return vk_meta_create_pipeline_layout(device, meta, &layout_info,
291 key_data, key_size, layout_out);
292 }
293
294 static VkResult
create_rect_list_pipeline(struct vk_device * device,struct vk_meta_device * meta,const VkGraphicsPipelineCreateInfo * info,VkPipeline * pipeline_out)295 create_rect_list_pipeline(struct vk_device *device,
296 struct vk_meta_device *meta,
297 const VkGraphicsPipelineCreateInfo *info,
298 VkPipeline *pipeline_out)
299 {
300 const struct vk_device_dispatch_table *disp = &device->dispatch_table;
301 VkDevice _device = vk_device_to_handle(device);
302
303 VkGraphicsPipelineCreateInfo info_local = *info;
304
305 /* We always configure for layered rendering for now */
306 bool use_gs = meta->use_gs_for_layer;
307
308 STACK_ARRAY(VkPipelineShaderStageCreateInfo, stages,
309 info->stageCount + 1 + use_gs);
310 uint32_t stage_count = 0;
311
312 VkPipelineShaderStageNirCreateInfoMESA vs_nir_info = {
313 .sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_NIR_CREATE_INFO_MESA,
314 .nir = vk_meta_draw_rects_vs_nir(meta, use_gs),
315 };
316 stages[stage_count++] = (VkPipelineShaderStageCreateInfo) {
317 .sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,
318 .pNext = &vs_nir_info,
319 .stage = VK_SHADER_STAGE_VERTEX_BIT,
320 .pName = "main",
321 };
322
323 VkPipelineShaderStageNirCreateInfoMESA gs_nir_info;
324 if (use_gs) {
325 gs_nir_info = (VkPipelineShaderStageNirCreateInfoMESA) {
326 .sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_NIR_CREATE_INFO_MESA,
327 .nir = vk_meta_draw_rects_gs_nir(meta),
328 };
329 stages[stage_count++] = (VkPipelineShaderStageCreateInfo) {
330 .sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,
331 .pNext = &gs_nir_info,
332 .stage = VK_SHADER_STAGE_GEOMETRY_BIT,
333 .pName = "main",
334 };
335 }
336
337 for (uint32_t i = 0; i < info->stageCount; i++) {
338 assert(info->pStages[i].stage != VK_SHADER_STAGE_VERTEX_BIT);
339 if (use_gs)
340 assert(info->pStages[i].stage != VK_SHADER_STAGE_GEOMETRY_BIT);
341 stages[stage_count++] = info->pStages[i];
342 }
343
344 info_local.stageCount = stage_count;
345 info_local.pStages = stages;
346 info_local.pVertexInputState = &vk_meta_draw_rects_vi_state;
347 info_local.pViewportState = &vk_meta_draw_rects_vs_state;
348
349 uint32_t dyn_count = info->pDynamicState != NULL ?
350 info->pDynamicState->dynamicStateCount : 0;
351
352 STACK_ARRAY(VkDynamicState, dyn_state, dyn_count + 2);
353 for (uint32_t i = 0; i < dyn_count; i++)
354 dyn_state[i] = info->pDynamicState->pDynamicStates[i];
355
356 dyn_state[dyn_count + 0] = VK_DYNAMIC_STATE_VIEWPORT;
357 dyn_state[dyn_count + 1] = VK_DYNAMIC_STATE_SCISSOR;
358
359 const VkPipelineDynamicStateCreateInfo dyn_info = {
360 .sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO,
361 .dynamicStateCount = dyn_count + 2,
362 .pDynamicStates = dyn_state,
363 };
364
365 info_local.pDynamicState = &dyn_info;
366
367 VkResult result = disp->CreateGraphicsPipelines(_device, VK_NULL_HANDLE,
368 1, &info_local, NULL,
369 pipeline_out);
370
371 STACK_ARRAY_FINISH(dyn_state);
372 STACK_ARRAY_FINISH(stages);
373
374 return result;
375 }
376
377 static const VkPipelineRasterizationStateCreateInfo default_rs_info = {
378 .sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO,
379 .depthClampEnable = false,
380 .depthBiasEnable = false,
381 .polygonMode = VK_POLYGON_MODE_FILL,
382 .cullMode = VK_CULL_MODE_NONE,
383 .frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE,
384 };
385
386 static const VkPipelineDepthStencilStateCreateInfo default_ds_info = {
387 .sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO,
388 .depthTestEnable = false,
389 .depthBoundsTestEnable = false,
390 .stencilTestEnable = false,
391 };
392
393 VkResult
vk_meta_create_graphics_pipeline(struct vk_device * device,struct vk_meta_device * meta,const VkGraphicsPipelineCreateInfo * info,const struct vk_meta_rendering_info * render,const void * key_data,size_t key_size,VkPipeline * pipeline_out)394 vk_meta_create_graphics_pipeline(struct vk_device *device,
395 struct vk_meta_device *meta,
396 const VkGraphicsPipelineCreateInfo *info,
397 const struct vk_meta_rendering_info *render,
398 const void *key_data, size_t key_size,
399 VkPipeline *pipeline_out)
400 {
401 const struct vk_device_dispatch_table *disp = &device->dispatch_table;
402 VkDevice _device = vk_device_to_handle(device);
403 VkResult result;
404
405 VkGraphicsPipelineCreateInfo info_local = *info;
406
407 /* Add in the rendering info */
408 VkPipelineRenderingCreateInfo r_info = {
409 .sType = VK_STRUCTURE_TYPE_PIPELINE_RENDERING_CREATE_INFO,
410 .viewMask = render->view_mask,
411 .colorAttachmentCount = render->color_attachment_count,
412 .pColorAttachmentFormats = render->color_attachment_formats,
413 .depthAttachmentFormat = render->depth_attachment_format,
414 .stencilAttachmentFormat = render->stencil_attachment_format,
415 };
416 __vk_append_struct(&info_local, &r_info);
417
418 /* Assume rectangle pipelines */
419 if (info_local.pInputAssemblyState == NULL)
420 info_local.pInputAssemblyState = &vk_meta_draw_rects_ia_state;
421
422 if (info_local.pRasterizationState == NULL)
423 info_local.pRasterizationState = &default_rs_info;
424
425 VkPipelineMultisampleStateCreateInfo ms_info;
426 if (info_local.pMultisampleState == NULL) {
427 ms_info = (VkPipelineMultisampleStateCreateInfo) {
428 .sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO,
429 .rasterizationSamples = render->samples,
430 };
431 info_local.pMultisampleState = &ms_info;
432 }
433
434 if (info_local.pDepthStencilState == NULL)
435 info_local.pDepthStencilState = &default_ds_info;
436
437 VkPipelineColorBlendStateCreateInfo cb_info;
438 VkPipelineColorBlendAttachmentState cb_att[MESA_VK_MAX_COLOR_ATTACHMENTS];
439 if (info_local.pColorBlendState == NULL) {
440 for (uint32_t i = 0; i < render->color_attachment_count; i++) {
441 cb_att[i] = (VkPipelineColorBlendAttachmentState) {
442 .blendEnable = false,
443 .colorWriteMask = VK_COLOR_COMPONENT_R_BIT |
444 VK_COLOR_COMPONENT_G_BIT |
445 VK_COLOR_COMPONENT_B_BIT |
446 VK_COLOR_COMPONENT_A_BIT,
447 };
448 }
449 cb_info = (VkPipelineColorBlendStateCreateInfo) {
450 .sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO,
451 .attachmentCount = render->color_attachment_count,
452 .pAttachments = cb_att,
453 };
454 info_local.pColorBlendState = &cb_info;
455 }
456
457 VkPipeline pipeline;
458 if (info_local.pInputAssemblyState->topology ==
459 VK_PRIMITIVE_TOPOLOGY_META_RECT_LIST_MESA) {
460 result = create_rect_list_pipeline(device, meta,
461 &info_local,
462 &pipeline);
463 } else {
464 result = disp->CreateGraphicsPipelines(_device, VK_NULL_HANDLE,
465 1, &info_local,
466 NULL, &pipeline);
467 }
468 if (unlikely(result != VK_SUCCESS))
469 return result;
470
471 *pipeline_out = (VkPipeline)vk_meta_cache_object(device, meta,
472 key_data, key_size,
473 VK_OBJECT_TYPE_PIPELINE,
474 (uint64_t)pipeline);
475 return VK_SUCCESS;
476 }
477
478 VkResult
vk_meta_create_compute_pipeline(struct vk_device * device,struct vk_meta_device * meta,const VkComputePipelineCreateInfo * info,const void * key_data,size_t key_size,VkPipeline * pipeline_out)479 vk_meta_create_compute_pipeline(struct vk_device *device,
480 struct vk_meta_device *meta,
481 const VkComputePipelineCreateInfo *info,
482 const void *key_data, size_t key_size,
483 VkPipeline *pipeline_out)
484 {
485 const struct vk_device_dispatch_table *disp = &device->dispatch_table;
486 VkDevice _device = vk_device_to_handle(device);
487
488 VkPipeline pipeline;
489 VkResult result = disp->CreateComputePipelines(_device, VK_NULL_HANDLE,
490 1, info, NULL, &pipeline);
491 if (result != VK_SUCCESS)
492 return result;
493
494 *pipeline_out = (VkPipeline)vk_meta_cache_object(device, meta,
495 key_data, key_size,
496 VK_OBJECT_TYPE_PIPELINE,
497 (uint64_t)pipeline);
498 return VK_SUCCESS;
499 }
500
501 VkResult
vk_meta_create_buffer(struct vk_command_buffer * cmd,struct vk_meta_device * meta,const VkBufferCreateInfo * info,VkBuffer * buffer_out)502 vk_meta_create_buffer(struct vk_command_buffer *cmd,
503 struct vk_meta_device *meta,
504 const VkBufferCreateInfo *info,
505 VkBuffer *buffer_out)
506 {
507 struct vk_device *device = cmd->base.device;
508 const struct vk_device_dispatch_table *disp = &device->dispatch_table;
509 VkDevice _device = vk_device_to_handle(device);
510
511 VkResult result = disp->CreateBuffer(_device, info, NULL, buffer_out);
512 if (unlikely(result != VK_SUCCESS))
513 return result;
514
515 vk_meta_object_list_add_handle(&cmd->meta_objects,
516 VK_OBJECT_TYPE_BUFFER,
517 (uint64_t)*buffer_out);
518 return VK_SUCCESS;
519 }
520
521 VkResult
vk_meta_create_image_view(struct vk_command_buffer * cmd,struct vk_meta_device * meta,const VkImageViewCreateInfo * info,VkImageView * image_view_out)522 vk_meta_create_image_view(struct vk_command_buffer *cmd,
523 struct vk_meta_device *meta,
524 const VkImageViewCreateInfo *info,
525 VkImageView *image_view_out)
526 {
527 struct vk_device *device = cmd->base.device;
528 const struct vk_device_dispatch_table *disp = &device->dispatch_table;
529 VkDevice _device = vk_device_to_handle(device);
530
531 VkResult result = disp->CreateImageView(_device, info, NULL, image_view_out);
532 if (unlikely(result != VK_SUCCESS))
533 return result;
534
535 vk_meta_object_list_add_handle(&cmd->meta_objects,
536 VK_OBJECT_TYPE_IMAGE_VIEW,
537 (uint64_t)*image_view_out);
538 return VK_SUCCESS;
539 }
540