vk/vulkan.h: Switch from GetImageSubresourceInfo to GetImageSubresourceLayout
[mesa.git] / src / vulkan / device.c
1 /*
2 * Copyright © 2015 Intel Corporation
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 <assert.h>
25 #include <stdbool.h>
26 #include <string.h>
27 #include <unistd.h>
28 #include <fcntl.h>
29
30 #include "private.h"
31
32 static int
33 anv_env_get_int(const char *name)
34 {
35 const char *val = getenv(name);
36
37 if (!val)
38 return 0;
39
40 return strtol(val, NULL, 0);
41 }
42
43 static VkResult
44 fill_physical_device(struct anv_physical_device *device,
45 struct anv_instance *instance,
46 const char *path)
47 {
48 int fd;
49
50 fd = open("/dev/dri/renderD128", O_RDWR | O_CLOEXEC);
51 if (fd < 0)
52 return vk_error(VK_ERROR_UNAVAILABLE);
53
54 device->instance = instance;
55 device->path = path;
56
57 device->chipset_id = anv_env_get_int("INTEL_DEVID_OVERRIDE");
58 device->no_hw = false;
59 if (device->chipset_id) {
60 /* INTEL_DEVID_OVERRIDE implies INTEL_NO_HW. */
61 device->no_hw = true;
62 } else {
63 device->chipset_id = anv_gem_get_param(fd, I915_PARAM_CHIPSET_ID);
64 }
65 if (!device->chipset_id)
66 goto fail;
67
68 device->name = brw_get_device_name(device->chipset_id);
69 device->info = brw_get_device_info(device->chipset_id, -1);
70 if (!device->info)
71 goto fail;
72
73 if (!anv_gem_get_param(fd, I915_PARAM_HAS_WAIT_TIMEOUT))
74 goto fail;
75
76 if (!anv_gem_get_param(fd, I915_PARAM_HAS_EXECBUF2))
77 goto fail;
78
79 if (!anv_gem_get_param(fd, I915_PARAM_HAS_LLC))
80 goto fail;
81
82 if (!anv_gem_get_param(fd, I915_PARAM_HAS_EXEC_CONSTANTS))
83 goto fail;
84
85 close(fd);
86
87 return VK_SUCCESS;
88
89 fail:
90 close(fd);
91
92 return vk_error(VK_ERROR_UNAVAILABLE);
93 }
94
95 static void *default_alloc(
96 void* pUserData,
97 size_t size,
98 size_t alignment,
99 VkSystemAllocType allocType)
100 {
101 return malloc(size);
102 }
103
104 static void default_free(
105 void* pUserData,
106 void* pMem)
107 {
108 free(pMem);
109 }
110
111 static const VkAllocCallbacks default_alloc_callbacks = {
112 .pUserData = NULL,
113 .pfnAlloc = default_alloc,
114 .pfnFree = default_free
115 };
116
117 VkResult anv_CreateInstance(
118 const VkInstanceCreateInfo* pCreateInfo,
119 VkInstance* pInstance)
120 {
121 struct anv_instance *instance;
122 const VkAllocCallbacks *alloc_callbacks = &default_alloc_callbacks;
123 void *user_data = NULL;
124 VkResult result;
125
126 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO);
127
128 if (pCreateInfo->pAllocCb) {
129 alloc_callbacks = pCreateInfo->pAllocCb;
130 user_data = pCreateInfo->pAllocCb->pUserData;
131 }
132 instance = alloc_callbacks->pfnAlloc(user_data, sizeof(*instance), 8,
133 VK_SYSTEM_ALLOC_TYPE_API_OBJECT);
134 if (!instance)
135 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
136
137 instance->pAllocUserData = alloc_callbacks->pUserData;
138 instance->pfnAlloc = alloc_callbacks->pfnAlloc;
139 instance->pfnFree = alloc_callbacks->pfnFree;
140 instance->apiVersion = pCreateInfo->pAppInfo->apiVersion;
141
142 instance->physicalDeviceCount = 0;
143 result = fill_physical_device(&instance->physicalDevice,
144 instance, "/dev/dri/renderD128");
145
146 if (result != VK_SUCCESS)
147 return result;
148
149 instance->physicalDeviceCount++;
150 *pInstance = (VkInstance) instance;
151
152 return VK_SUCCESS;
153 }
154
155 VkResult anv_DestroyInstance(
156 VkInstance _instance)
157 {
158 struct anv_instance *instance = (struct anv_instance *) _instance;
159
160 instance->pfnFree(instance->pAllocUserData, instance);
161
162 return VK_SUCCESS;
163 }
164
165 VkResult anv_EnumeratePhysicalDevices(
166 VkInstance _instance,
167 uint32_t* pPhysicalDeviceCount,
168 VkPhysicalDevice* pPhysicalDevices)
169 {
170 struct anv_instance *instance = (struct anv_instance *) _instance;
171
172 if (*pPhysicalDeviceCount >= 1)
173 pPhysicalDevices[0] = (VkPhysicalDevice) &instance->physicalDevice;
174 *pPhysicalDeviceCount = instance->physicalDeviceCount;
175
176 return VK_SUCCESS;
177 }
178
179 VkResult anv_GetPhysicalDeviceInfo(
180 VkPhysicalDevice physicalDevice,
181 VkPhysicalDeviceInfoType infoType,
182 size_t* pDataSize,
183 void* pData)
184 {
185 struct anv_physical_device *device = (struct anv_physical_device *) physicalDevice;
186 VkPhysicalDeviceProperties *properties;
187 VkPhysicalDevicePerformance *performance;
188 VkPhysicalDeviceQueueProperties *queue_properties;
189 VkPhysicalDeviceMemoryProperties *memory_properties;
190 VkDisplayPropertiesWSI *display_properties;
191 uint64_t ns_per_tick = 80;
192
193 switch ((uint32_t) infoType) {
194 case VK_PHYSICAL_DEVICE_INFO_TYPE_PROPERTIES:
195 properties = pData;
196
197 *pDataSize = sizeof(*properties);
198 if (pData == NULL)
199 return VK_SUCCESS;
200
201 properties->apiVersion = 1;
202 properties->driverVersion = 1;
203 properties->vendorId = 0x8086;
204 properties->deviceId = device->chipset_id;
205 properties->deviceType = VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU;
206 strcpy(properties->deviceName, device->name);
207 properties->maxInlineMemoryUpdateSize = 0;
208 properties->maxBoundDescriptorSets = MAX_SETS;
209 properties->maxThreadGroupSize = 512;
210 properties->timestampFrequency = 1000 * 1000 * 1000 / ns_per_tick;
211 properties->multiColorAttachmentClears = true;
212 properties->maxDescriptorSets = 8;
213 properties->maxViewports = 16;
214 properties->maxColorAttachments = 8;
215 return VK_SUCCESS;
216
217 case VK_PHYSICAL_DEVICE_INFO_TYPE_PERFORMANCE:
218 performance = pData;
219
220 *pDataSize = sizeof(*performance);
221 if (pData == NULL)
222 return VK_SUCCESS;
223
224 performance->maxDeviceClock = 1.0;
225 performance->aluPerClock = 1.0;
226 performance->texPerClock = 1.0;
227 performance->primsPerClock = 1.0;
228 performance->pixelsPerClock = 1.0;
229 return VK_SUCCESS;
230
231 case VK_PHYSICAL_DEVICE_INFO_TYPE_QUEUE_PROPERTIES:
232 queue_properties = pData;
233
234 *pDataSize = sizeof(*queue_properties);
235 if (pData == NULL)
236 return VK_SUCCESS;
237
238 queue_properties->queueFlags = 0;
239 queue_properties->queueCount = 1;
240 queue_properties->supportsTimestamps = true;
241 return VK_SUCCESS;
242
243 case VK_PHYSICAL_DEVICE_INFO_TYPE_MEMORY_PROPERTIES:
244 memory_properties = pData;
245
246 *pDataSize = sizeof(*memory_properties);
247 if (pData == NULL)
248 return VK_SUCCESS;
249
250 memory_properties->supportsMigration = false;
251 memory_properties->supportsPinning = false;
252 return VK_SUCCESS;
253
254 case VK_PHYSICAL_DEVICE_INFO_TYPE_DISPLAY_PROPERTIES_WSI:
255 anv_finishme("VK_PHYSICAL_DEVICE_INFO_TYPE_DISPLAY_PROPERTIES_WSI");
256
257 *pDataSize = sizeof(*display_properties);
258 if (pData == NULL)
259 return VK_SUCCESS;
260
261 display_properties = pData;
262 display_properties->display = 0;
263 display_properties->physicalResolution = (VkExtent2D) { 0, 0 };
264 return VK_SUCCESS;
265
266 case VK_PHYSICAL_DEVICE_INFO_TYPE_QUEUE_PRESENT_PROPERTIES_WSI:
267 anv_finishme("VK_PHYSICAL_DEVICE_INFO_TYPE_QUEUE_PRESENT_PROPERTIES_WSI");
268 return VK_SUCCESS;
269
270
271 default:
272 return VK_UNSUPPORTED;
273 }
274
275 }
276
277 void * vkGetProcAddr(
278 VkPhysicalDevice physicalDevice,
279 const char* pName)
280 {
281 return anv_lookup_entrypoint(pName);
282 }
283
284 static void
285 parse_debug_flags(struct anv_device *device)
286 {
287 const char *debug, *p, *end;
288
289 debug = getenv("INTEL_DEBUG");
290 device->dump_aub = false;
291 if (debug) {
292 for (p = debug; *p; p = end + 1) {
293 end = strchrnul(p, ',');
294 if (end - p == 3 && memcmp(p, "aub", 3) == 0)
295 device->dump_aub = true;
296 if (end - p == 5 && memcmp(p, "no_hw", 5) == 0)
297 device->no_hw = true;
298 if (*end == '\0')
299 break;
300 }
301 }
302 }
303
304 static VkResult
305 anv_queue_init(struct anv_device *device, struct anv_queue *queue)
306 {
307 queue->device = device;
308 queue->pool = &device->surface_state_pool;
309
310 queue->completed_serial = anv_state_pool_alloc(queue->pool, 4, 4);
311 if (queue->completed_serial.map == NULL)
312 return vk_error(VK_ERROR_OUT_OF_DEVICE_MEMORY);
313
314 *(uint32_t *)queue->completed_serial.map = 0;
315 queue->next_serial = 1;
316
317 return VK_SUCCESS;
318 }
319
320 static void
321 anv_queue_finish(struct anv_queue *queue)
322 {
323 #ifdef HAVE_VALGRIND
324 /* This gets torn down with the device so we only need to do this if
325 * valgrind is present.
326 */
327 anv_state_pool_free(queue->pool, queue->completed_serial);
328 #endif
329 }
330
331 static void
332 anv_device_init_border_colors(struct anv_device *device)
333 {
334 float float_border_colors[][4] = {
335 [VK_BORDER_COLOR_OPAQUE_WHITE] = { 1.0, 1.0, 1.0, 1.0 },
336 [VK_BORDER_COLOR_TRANSPARENT_BLACK] = { 0.0, 0.0, 0.0, 0.0 },
337 [VK_BORDER_COLOR_OPAQUE_BLACK] = { 0.0, 0.0, 0.0, 1.0 }
338 };
339
340 uint32_t uint32_border_colors[][4] = {
341 [VK_BORDER_COLOR_OPAQUE_WHITE] = { 1, 1, 1, 1 },
342 [VK_BORDER_COLOR_TRANSPARENT_BLACK] = { 0, 0, 0, 0 },
343 [VK_BORDER_COLOR_OPAQUE_BLACK] = { 0, 0, 0, 1 }
344 };
345
346 device->float_border_colors =
347 anv_state_pool_alloc(&device->dynamic_state_pool,
348 sizeof(float_border_colors), 32);
349 memcpy(device->float_border_colors.map,
350 float_border_colors, sizeof(float_border_colors));
351
352 device->uint32_border_colors =
353 anv_state_pool_alloc(&device->dynamic_state_pool,
354 sizeof(uint32_border_colors), 32);
355 memcpy(device->uint32_border_colors.map,
356 uint32_border_colors, sizeof(uint32_border_colors));
357
358 }
359
360 static const uint32_t BATCH_SIZE = 8192;
361
362 VkResult anv_CreateDevice(
363 VkPhysicalDevice _physicalDevice,
364 const VkDeviceCreateInfo* pCreateInfo,
365 VkDevice* pDevice)
366 {
367 struct anv_physical_device *physicalDevice =
368 (struct anv_physical_device *) _physicalDevice;
369 struct anv_instance *instance = physicalDevice->instance;
370 struct anv_device *device;
371
372 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO);
373
374 device = instance->pfnAlloc(instance->pAllocUserData,
375 sizeof(*device), 8,
376 VK_SYSTEM_ALLOC_TYPE_API_OBJECT);
377 if (!device)
378 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
379
380 device->no_hw = physicalDevice->no_hw;
381 parse_debug_flags(device);
382
383 device->instance = physicalDevice->instance;
384 device->fd = open("/dev/dri/renderD128", O_RDWR | O_CLOEXEC);
385 if (device->fd == -1)
386 goto fail_device;
387
388 device->context_id = anv_gem_create_context(device);
389 if (device->context_id == -1)
390 goto fail_fd;
391
392 anv_bo_pool_init(&device->batch_bo_pool, device, BATCH_SIZE);
393
394 anv_block_pool_init(&device->dynamic_state_block_pool, device, 2048);
395
396 anv_state_pool_init(&device->dynamic_state_pool,
397 &device->dynamic_state_block_pool);
398
399 anv_block_pool_init(&device->instruction_block_pool, device, 2048);
400 anv_block_pool_init(&device->surface_state_block_pool, device, 2048);
401
402 anv_state_pool_init(&device->surface_state_pool,
403 &device->surface_state_block_pool);
404
405 anv_block_pool_init(&device->scratch_block_pool, device, 0x10000);
406
407 device->info = *physicalDevice->info;
408
409 device->compiler = anv_compiler_create(device);
410 device->aub_writer = NULL;
411
412 pthread_mutex_init(&device->mutex, NULL);
413
414 anv_queue_init(device, &device->queue);
415
416 anv_device_init_meta(device);
417
418 anv_device_init_border_colors(device);
419
420 *pDevice = (VkDevice) device;
421
422 return VK_SUCCESS;
423
424 fail_fd:
425 close(device->fd);
426 fail_device:
427 anv_device_free(device, device);
428
429 return vk_error(VK_ERROR_UNAVAILABLE);
430 }
431
432 VkResult anv_DestroyDevice(
433 VkDevice _device)
434 {
435 struct anv_device *device = (struct anv_device *) _device;
436
437 anv_compiler_destroy(device->compiler);
438
439 anv_queue_finish(&device->queue);
440
441 anv_device_finish_meta(device);
442
443 #ifdef HAVE_VALGRIND
444 /* We only need to free these to prevent valgrind errors. The backing
445 * BO will go away in a couple of lines so we don't actually leak.
446 */
447 anv_state_pool_free(&device->dynamic_state_pool,
448 device->float_border_colors);
449 anv_state_pool_free(&device->dynamic_state_pool,
450 device->uint32_border_colors);
451 #endif
452
453 anv_bo_pool_finish(&device->batch_bo_pool);
454 anv_block_pool_finish(&device->dynamic_state_block_pool);
455 anv_block_pool_finish(&device->instruction_block_pool);
456 anv_block_pool_finish(&device->surface_state_block_pool);
457
458 close(device->fd);
459
460 if (device->aub_writer)
461 anv_aub_writer_destroy(device->aub_writer);
462
463 anv_device_free(device, device);
464
465 return VK_SUCCESS;
466 }
467
468 VkResult anv_GetGlobalExtensionInfo(
469 VkExtensionInfoType infoType,
470 uint32_t extensionIndex,
471 size_t* pDataSize,
472 void* pData)
473 {
474 static const VkExtensionProperties extensions[] = {
475 {
476 .extName = "VK_WSI_LunarG",
477 .version = 3
478 }
479 };
480 uint32_t count = ARRAY_SIZE(extensions);
481
482 switch (infoType) {
483 case VK_EXTENSION_INFO_TYPE_COUNT:
484 memcpy(pData, &count, sizeof(count));
485 *pDataSize = sizeof(count);
486 return VK_SUCCESS;
487
488 case VK_EXTENSION_INFO_TYPE_PROPERTIES:
489 if (extensionIndex >= count)
490 return vk_error(VK_ERROR_INVALID_EXTENSION);
491
492 memcpy(pData, &extensions[extensionIndex], sizeof(extensions[0]));
493 *pDataSize = sizeof(extensions[0]);
494 return VK_SUCCESS;
495
496 default:
497 return VK_UNSUPPORTED;
498 }
499 }
500
501 VkResult anv_GetPhysicalDeviceExtensionInfo(
502 VkPhysicalDevice physicalDevice,
503 VkExtensionInfoType infoType,
504 uint32_t extensionIndex,
505 size_t* pDataSize,
506 void* pData)
507 {
508 uint32_t *count;
509
510 switch (infoType) {
511 case VK_EXTENSION_INFO_TYPE_COUNT:
512 *pDataSize = 4;
513 if (pData == NULL)
514 return VK_SUCCESS;
515
516 count = pData;
517 *count = 0;
518 return VK_SUCCESS;
519
520 case VK_EXTENSION_INFO_TYPE_PROPERTIES:
521 return vk_error(VK_ERROR_INVALID_EXTENSION);
522
523 default:
524 return VK_UNSUPPORTED;
525 }
526 }
527
528 VkResult anv_EnumerateLayers(
529 VkPhysicalDevice physicalDevice,
530 size_t maxStringSize,
531 size_t* pLayerCount,
532 char* const* pOutLayers,
533 void* pReserved)
534 {
535 *pLayerCount = 0;
536
537 return VK_SUCCESS;
538 }
539
540 VkResult anv_GetDeviceQueue(
541 VkDevice _device,
542 uint32_t queueNodeIndex,
543 uint32_t queueIndex,
544 VkQueue* pQueue)
545 {
546 struct anv_device *device = (struct anv_device *) _device;
547
548 assert(queueIndex == 0);
549
550 *pQueue = (VkQueue) &device->queue;
551
552 return VK_SUCCESS;
553 }
554
555 VkResult
556 anv_reloc_list_init(struct anv_reloc_list *list, struct anv_device *device)
557 {
558 list->num_relocs = 0;
559 list->array_length = 256;
560 list->relocs =
561 anv_device_alloc(device, list->array_length * sizeof(*list->relocs), 8,
562 VK_SYSTEM_ALLOC_TYPE_INTERNAL);
563
564 if (list->relocs == NULL)
565 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
566
567 list->reloc_bos =
568 anv_device_alloc(device, list->array_length * sizeof(*list->reloc_bos), 8,
569 VK_SYSTEM_ALLOC_TYPE_INTERNAL);
570
571 if (list->relocs == NULL) {
572 anv_device_free(device, list->relocs);
573 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
574 }
575
576 return VK_SUCCESS;
577 }
578
579 void
580 anv_reloc_list_finish(struct anv_reloc_list *list, struct anv_device *device)
581 {
582 anv_device_free(device, list->relocs);
583 anv_device_free(device, list->reloc_bos);
584 }
585
586 static VkResult
587 anv_reloc_list_grow(struct anv_reloc_list *list, struct anv_device *device,
588 size_t num_additional_relocs)
589 {
590 if (list->num_relocs + num_additional_relocs <= list->array_length)
591 return VK_SUCCESS;
592
593 size_t new_length = list->array_length * 2;
594 while (new_length < list->num_relocs + num_additional_relocs)
595 new_length *= 2;
596
597 struct drm_i915_gem_relocation_entry *new_relocs =
598 anv_device_alloc(device, new_length * sizeof(*list->relocs), 8,
599 VK_SYSTEM_ALLOC_TYPE_INTERNAL);
600 if (new_relocs == NULL)
601 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
602
603 struct anv_bo **new_reloc_bos =
604 anv_device_alloc(device, new_length * sizeof(*list->reloc_bos), 8,
605 VK_SYSTEM_ALLOC_TYPE_INTERNAL);
606 if (new_relocs == NULL) {
607 anv_device_free(device, new_relocs);
608 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
609 }
610
611 memcpy(new_relocs, list->relocs, list->num_relocs * sizeof(*list->relocs));
612 memcpy(new_reloc_bos, list->reloc_bos,
613 list->num_relocs * sizeof(*list->reloc_bos));
614
615 anv_device_free(device, list->relocs);
616 anv_device_free(device, list->reloc_bos);
617
618 list->relocs = new_relocs;
619 list->reloc_bos = new_reloc_bos;
620
621 return VK_SUCCESS;
622 }
623
624 static VkResult
625 anv_batch_bo_create(struct anv_device *device, struct anv_batch_bo **bbo_out)
626 {
627 VkResult result;
628
629 struct anv_batch_bo *bbo =
630 anv_device_alloc(device, sizeof(*bbo), 8, VK_SYSTEM_ALLOC_TYPE_INTERNAL);
631 if (bbo == NULL)
632 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
633
634 bbo->num_relocs = 0;
635 bbo->prev_batch_bo = NULL;
636
637 result = anv_bo_pool_alloc(&device->batch_bo_pool, &bbo->bo);
638 if (result != VK_SUCCESS) {
639 anv_device_free(device, bbo);
640 return result;
641 }
642
643 *bbo_out = bbo;
644
645 return VK_SUCCESS;
646 }
647
648 static void
649 anv_batch_bo_start(struct anv_batch_bo *bbo, struct anv_batch *batch,
650 size_t batch_padding)
651 {
652 batch->next = batch->start = bbo->bo.map;
653 batch->end = bbo->bo.map + bbo->bo.size - batch_padding;
654 bbo->first_reloc = batch->relocs.num_relocs;
655 }
656
657 static void
658 anv_batch_bo_finish(struct anv_batch_bo *bbo, struct anv_batch *batch)
659 {
660 assert(batch->start == bbo->bo.map);
661 bbo->length = batch->next - batch->start;
662 VG(VALGRIND_CHECK_MEM_IS_DEFINED(batch->start, bbo->length));
663 bbo->num_relocs = batch->relocs.num_relocs - bbo->first_reloc;
664 }
665
666 static void
667 anv_batch_bo_destroy(struct anv_batch_bo *bbo, struct anv_device *device)
668 {
669 anv_bo_pool_free(&device->batch_bo_pool, &bbo->bo);
670 anv_device_free(device, bbo);
671 }
672
673 void *
674 anv_batch_emit_dwords(struct anv_batch *batch, int num_dwords)
675 {
676 if (batch->next + num_dwords * 4 > batch->end)
677 batch->extend_cb(batch, batch->user_data);
678
679 void *p = batch->next;
680
681 batch->next += num_dwords * 4;
682 assert(batch->next <= batch->end);
683
684 return p;
685 }
686
687 static void
688 anv_reloc_list_append(struct anv_reloc_list *list, struct anv_device *device,
689 struct anv_reloc_list *other, uint32_t offset)
690 {
691 anv_reloc_list_grow(list, device, other->num_relocs);
692 /* TODO: Handle failure */
693
694 memcpy(&list->relocs[list->num_relocs], &other->relocs[0],
695 other->num_relocs * sizeof(other->relocs[0]));
696 memcpy(&list->reloc_bos[list->num_relocs], &other->reloc_bos[0],
697 other->num_relocs * sizeof(other->reloc_bos[0]));
698
699 for (uint32_t i = 0; i < other->num_relocs; i++)
700 list->relocs[i + list->num_relocs].offset += offset;
701
702 list->num_relocs += other->num_relocs;
703 }
704
705 static uint64_t
706 anv_reloc_list_add(struct anv_reloc_list *list, struct anv_device *device,
707 uint32_t offset, struct anv_bo *target_bo, uint32_t delta)
708 {
709 struct drm_i915_gem_relocation_entry *entry;
710 int index;
711
712 anv_reloc_list_grow(list, device, 1);
713 /* TODO: Handle failure */
714
715 /* XXX: Can we use I915_EXEC_HANDLE_LUT? */
716 index = list->num_relocs++;
717 list->reloc_bos[index] = target_bo;
718 entry = &list->relocs[index];
719 entry->target_handle = target_bo->gem_handle;
720 entry->delta = delta;
721 entry->offset = offset;
722 entry->presumed_offset = target_bo->offset;
723 entry->read_domains = 0;
724 entry->write_domain = 0;
725
726 return target_bo->offset + delta;
727 }
728
729 void
730 anv_batch_emit_batch(struct anv_batch *batch, struct anv_batch *other)
731 {
732 uint32_t size, offset;
733
734 size = other->next - other->start;
735 assert(size % 4 == 0);
736
737 if (batch->next + size > batch->end)
738 batch->extend_cb(batch, batch->user_data);
739
740 assert(batch->next + size <= batch->end);
741
742 memcpy(batch->next, other->start, size);
743
744 offset = batch->next - batch->start;
745 anv_reloc_list_append(&batch->relocs, batch->device,
746 &other->relocs, offset);
747
748 batch->next += size;
749 }
750
751 uint64_t
752 anv_batch_emit_reloc(struct anv_batch *batch,
753 void *location, struct anv_bo *bo, uint32_t delta)
754 {
755 return anv_reloc_list_add(&batch->relocs, batch->device,
756 location - batch->start, bo, delta);
757 }
758
759 VkResult anv_QueueSubmit(
760 VkQueue _queue,
761 uint32_t cmdBufferCount,
762 const VkCmdBuffer* pCmdBuffers,
763 VkFence _fence)
764 {
765 struct anv_queue *queue = (struct anv_queue *) _queue;
766 struct anv_device *device = queue->device;
767 struct anv_fence *fence = (struct anv_fence *) _fence;
768 int ret;
769
770 for (uint32_t i = 0; i < cmdBufferCount; i++) {
771 struct anv_cmd_buffer *cmd_buffer =
772 (struct anv_cmd_buffer *) pCmdBuffers[i];
773
774 if (device->dump_aub)
775 anv_cmd_buffer_dump(cmd_buffer);
776
777 if (!device->no_hw) {
778 ret = anv_gem_execbuffer(device, &cmd_buffer->execbuf);
779 if (ret != 0)
780 return vk_error(VK_ERROR_UNKNOWN);
781
782 if (fence) {
783 ret = anv_gem_execbuffer(device, &fence->execbuf);
784 if (ret != 0)
785 return vk_error(VK_ERROR_UNKNOWN);
786 }
787
788 for (uint32_t i = 0; i < cmd_buffer->bo_count; i++)
789 cmd_buffer->exec2_bos[i]->offset = cmd_buffer->exec2_objects[i].offset;
790 } else {
791 *(uint32_t *)queue->completed_serial.map = cmd_buffer->serial;
792 }
793 }
794
795 return VK_SUCCESS;
796 }
797
798 VkResult anv_QueueWaitIdle(
799 VkQueue _queue)
800 {
801 struct anv_queue *queue = (struct anv_queue *) _queue;
802
803 return vkDeviceWaitIdle((VkDevice) queue->device);
804 }
805
806 VkResult anv_DeviceWaitIdle(
807 VkDevice _device)
808 {
809 struct anv_device *device = (struct anv_device *) _device;
810 struct anv_state state;
811 struct anv_batch batch;
812 struct drm_i915_gem_execbuffer2 execbuf;
813 struct drm_i915_gem_exec_object2 exec2_objects[1];
814 struct anv_bo *bo = NULL;
815 VkResult result;
816 int64_t timeout;
817 int ret;
818
819 state = anv_state_pool_alloc(&device->dynamic_state_pool, 32, 32);
820 bo = &device->dynamic_state_pool.block_pool->bo;
821 batch.start = batch.next = state.map;
822 batch.end = state.map + 32;
823 anv_batch_emit(&batch, GEN8_MI_BATCH_BUFFER_END);
824 anv_batch_emit(&batch, GEN8_MI_NOOP);
825
826 exec2_objects[0].handle = bo->gem_handle;
827 exec2_objects[0].relocation_count = 0;
828 exec2_objects[0].relocs_ptr = 0;
829 exec2_objects[0].alignment = 0;
830 exec2_objects[0].offset = bo->offset;
831 exec2_objects[0].flags = 0;
832 exec2_objects[0].rsvd1 = 0;
833 exec2_objects[0].rsvd2 = 0;
834
835 execbuf.buffers_ptr = (uintptr_t) exec2_objects;
836 execbuf.buffer_count = 1;
837 execbuf.batch_start_offset = state.offset;
838 execbuf.batch_len = batch.next - state.map;
839 execbuf.cliprects_ptr = 0;
840 execbuf.num_cliprects = 0;
841 execbuf.DR1 = 0;
842 execbuf.DR4 = 0;
843
844 execbuf.flags =
845 I915_EXEC_HANDLE_LUT | I915_EXEC_NO_RELOC | I915_EXEC_RENDER;
846 execbuf.rsvd1 = device->context_id;
847 execbuf.rsvd2 = 0;
848
849 if (!device->no_hw) {
850 ret = anv_gem_execbuffer(device, &execbuf);
851 if (ret != 0) {
852 result = vk_error(VK_ERROR_UNKNOWN);
853 goto fail;
854 }
855
856 timeout = INT64_MAX;
857 ret = anv_gem_wait(device, bo->gem_handle, &timeout);
858 if (ret != 0) {
859 result = vk_error(VK_ERROR_UNKNOWN);
860 goto fail;
861 }
862 }
863
864 anv_state_pool_free(&device->dynamic_state_pool, state);
865
866 return VK_SUCCESS;
867
868 fail:
869 anv_state_pool_free(&device->dynamic_state_pool, state);
870
871 return result;
872 }
873
874 void *
875 anv_device_alloc(struct anv_device * device,
876 size_t size,
877 size_t alignment,
878 VkSystemAllocType allocType)
879 {
880 return device->instance->pfnAlloc(device->instance->pAllocUserData,
881 size,
882 alignment,
883 allocType);
884 }
885
886 void
887 anv_device_free(struct anv_device * device,
888 void * mem)
889 {
890 return device->instance->pfnFree(device->instance->pAllocUserData,
891 mem);
892 }
893
894 VkResult
895 anv_bo_init_new(struct anv_bo *bo, struct anv_device *device, uint64_t size)
896 {
897 bo->gem_handle = anv_gem_create(device, size);
898 if (!bo->gem_handle)
899 return vk_error(VK_ERROR_OUT_OF_DEVICE_MEMORY);
900
901 bo->map = NULL;
902 bo->index = 0;
903 bo->offset = 0;
904 bo->size = size;
905
906 return VK_SUCCESS;
907 }
908
909 VkResult anv_AllocMemory(
910 VkDevice _device,
911 const VkMemoryAllocInfo* pAllocInfo,
912 VkDeviceMemory* pMem)
913 {
914 struct anv_device *device = (struct anv_device *) _device;
915 struct anv_device_memory *mem;
916 VkResult result;
917
918 assert(pAllocInfo->sType == VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO);
919
920 mem = anv_device_alloc(device, sizeof(*mem), 8,
921 VK_SYSTEM_ALLOC_TYPE_API_OBJECT);
922 if (mem == NULL)
923 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
924
925 result = anv_bo_init_new(&mem->bo, device, pAllocInfo->allocationSize);
926 if (result != VK_SUCCESS)
927 goto fail;
928
929 *pMem = (VkDeviceMemory) mem;
930
931 return VK_SUCCESS;
932
933 fail:
934 anv_device_free(device, mem);
935
936 return result;
937 }
938
939 VkResult anv_FreeMemory(
940 VkDevice _device,
941 VkDeviceMemory _mem)
942 {
943 struct anv_device *device = (struct anv_device *) _device;
944 struct anv_device_memory *mem = (struct anv_device_memory *) _mem;
945
946 if (mem->bo.map)
947 anv_gem_munmap(mem->bo.map, mem->bo.size);
948
949 if (mem->bo.gem_handle != 0)
950 anv_gem_close(device, mem->bo.gem_handle);
951
952 anv_device_free(device, mem);
953
954 return VK_SUCCESS;
955 }
956
957 VkResult anv_MapMemory(
958 VkDevice _device,
959 VkDeviceMemory _mem,
960 VkDeviceSize offset,
961 VkDeviceSize size,
962 VkMemoryMapFlags flags,
963 void** ppData)
964 {
965 struct anv_device *device = (struct anv_device *) _device;
966 struct anv_device_memory *mem = (struct anv_device_memory *) _mem;
967
968 /* FIXME: Is this supposed to be thread safe? Since vkUnmapMemory() only
969 * takes a VkDeviceMemory pointer, it seems like only one map of the memory
970 * at a time is valid. We could just mmap up front and return an offset
971 * pointer here, but that may exhaust virtual memory on 32 bit
972 * userspace. */
973
974 mem->map = anv_gem_mmap(device, mem->bo.gem_handle, offset, size);
975 mem->map_size = size;
976
977 *ppData = mem->map;
978
979 return VK_SUCCESS;
980 }
981
982 VkResult anv_UnmapMemory(
983 VkDevice _device,
984 VkDeviceMemory _mem)
985 {
986 struct anv_device_memory *mem = (struct anv_device_memory *) _mem;
987
988 anv_gem_munmap(mem->map, mem->map_size);
989
990 return VK_SUCCESS;
991 }
992
993 VkResult anv_FlushMappedMemoryRanges(
994 VkDevice device,
995 uint32_t memRangeCount,
996 const VkMappedMemoryRange* pMemRanges)
997 {
998 /* clflush here for !llc platforms */
999
1000 return VK_SUCCESS;
1001 }
1002
1003 VkResult anv_InvalidateMappedMemoryRanges(
1004 VkDevice device,
1005 uint32_t memRangeCount,
1006 const VkMappedMemoryRange* pMemRanges)
1007 {
1008 return anv_FlushMappedMemoryRanges(device, memRangeCount, pMemRanges);
1009 }
1010
1011 VkResult anv_DestroyObject(
1012 VkDevice _device,
1013 VkObjectType objType,
1014 VkObject _object)
1015 {
1016 struct anv_device *device = (struct anv_device *) _device;
1017 struct anv_object *object = (struct anv_object *) _object;
1018
1019 switch (objType) {
1020 case VK_OBJECT_TYPE_INSTANCE:
1021 return anv_DestroyInstance((VkInstance) _object);
1022
1023 case VK_OBJECT_TYPE_PHYSICAL_DEVICE:
1024 /* We don't want to actually destroy physical devices */
1025 return VK_SUCCESS;
1026
1027 case VK_OBJECT_TYPE_DEVICE:
1028 assert(_device == (VkDevice) _object);
1029 return anv_DestroyDevice((VkDevice) _object);
1030
1031 case VK_OBJECT_TYPE_QUEUE:
1032 /* TODO */
1033 return VK_SUCCESS;
1034
1035 case VK_OBJECT_TYPE_DEVICE_MEMORY:
1036 return anv_FreeMemory(_device, (VkDeviceMemory) _object);
1037
1038 case VK_OBJECT_TYPE_DESCRIPTOR_POOL:
1039 /* These are just dummys anyway, so we don't need to destroy them */
1040 return VK_SUCCESS;
1041
1042 case VK_OBJECT_TYPE_BUFFER:
1043 case VK_OBJECT_TYPE_IMAGE:
1044 case VK_OBJECT_TYPE_DEPTH_STENCIL_VIEW:
1045 case VK_OBJECT_TYPE_SHADER:
1046 case VK_OBJECT_TYPE_PIPELINE_LAYOUT:
1047 case VK_OBJECT_TYPE_SAMPLER:
1048 case VK_OBJECT_TYPE_DESCRIPTOR_SET:
1049 case VK_OBJECT_TYPE_DESCRIPTOR_SET_LAYOUT:
1050 case VK_OBJECT_TYPE_DYNAMIC_RS_STATE:
1051 case VK_OBJECT_TYPE_DYNAMIC_CB_STATE:
1052 case VK_OBJECT_TYPE_DYNAMIC_DS_STATE:
1053 case VK_OBJECT_TYPE_RENDER_PASS:
1054 /* These are trivially destroyable */
1055 anv_device_free(device, (void *) _object);
1056 return VK_SUCCESS;
1057
1058 case VK_OBJECT_TYPE_COMMAND_BUFFER:
1059 case VK_OBJECT_TYPE_PIPELINE:
1060 case VK_OBJECT_TYPE_DYNAMIC_VP_STATE:
1061 case VK_OBJECT_TYPE_FENCE:
1062 case VK_OBJECT_TYPE_QUERY_POOL:
1063 case VK_OBJECT_TYPE_FRAMEBUFFER:
1064 case VK_OBJECT_TYPE_BUFFER_VIEW:
1065 case VK_OBJECT_TYPE_IMAGE_VIEW:
1066 case VK_OBJECT_TYPE_COLOR_ATTACHMENT_VIEW:
1067 (object->destructor)(device, object, objType);
1068 return VK_SUCCESS;
1069
1070 case VK_OBJECT_TYPE_SEMAPHORE:
1071 case VK_OBJECT_TYPE_EVENT:
1072 stub_return(VK_UNSUPPORTED);
1073
1074 default:
1075 unreachable("Invalid object type");
1076 }
1077 }
1078
1079 VkResult anv_GetObjectMemoryRequirements(
1080 VkDevice device,
1081 VkObjectType objType,
1082 VkObject object,
1083 VkMemoryRequirements* pMemoryRequirements)
1084 {
1085 pMemoryRequirements->memPropsAllowed =
1086 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
1087 /* VK_MEMORY_PROPERTY_HOST_NON_COHERENT_BIT | */
1088 /* VK_MEMORY_PROPERTY_HOST_UNCACHED_BIT | */
1089 VK_MEMORY_PROPERTY_HOST_WRITE_COMBINED_BIT;
1090
1091 pMemoryRequirements->memPropsRequired = 0;
1092
1093 switch (objType) {
1094 case VK_OBJECT_TYPE_BUFFER: {
1095 struct anv_buffer *buffer = (struct anv_buffer *) object;
1096 pMemoryRequirements->size = buffer->size;
1097 pMemoryRequirements->alignment = 16;
1098 break;
1099 }
1100 case VK_OBJECT_TYPE_IMAGE: {
1101 struct anv_image *image = (struct anv_image *) object;
1102 pMemoryRequirements->size = image->size;
1103 pMemoryRequirements->alignment = image->alignment;
1104 break;
1105 }
1106 default:
1107 pMemoryRequirements->size = 0;
1108 break;
1109 }
1110
1111 return VK_SUCCESS;
1112 }
1113
1114 VkResult anv_QueueBindObjectMemory(
1115 VkQueue queue,
1116 VkObjectType objType,
1117 VkObject object,
1118 uint32_t allocationIdx,
1119 VkDeviceMemory _mem,
1120 VkDeviceSize memOffset)
1121 {
1122 struct anv_buffer *buffer;
1123 struct anv_image *image;
1124 struct anv_device_memory *mem = (struct anv_device_memory *) _mem;
1125
1126 switch (objType) {
1127 case VK_OBJECT_TYPE_BUFFER:
1128 buffer = (struct anv_buffer *) object;
1129 buffer->bo = &mem->bo;
1130 buffer->offset = memOffset;
1131 break;
1132 case VK_OBJECT_TYPE_IMAGE:
1133 image = (struct anv_image *) object;
1134 image->bo = &mem->bo;
1135 image->offset = memOffset;
1136 break;
1137 default:
1138 break;
1139 }
1140
1141 return VK_SUCCESS;
1142 }
1143
1144 VkResult anv_QueueBindObjectMemoryRange(
1145 VkQueue queue,
1146 VkObjectType objType,
1147 VkObject object,
1148 uint32_t allocationIdx,
1149 VkDeviceSize rangeOffset,
1150 VkDeviceSize rangeSize,
1151 VkDeviceMemory mem,
1152 VkDeviceSize memOffset)
1153 {
1154 stub_return(VK_UNSUPPORTED);
1155 }
1156
1157 VkResult anv_QueueBindImageMemoryRange(
1158 VkQueue queue,
1159 VkImage image,
1160 uint32_t allocationIdx,
1161 const VkImageMemoryBindInfo* pBindInfo,
1162 VkDeviceMemory mem,
1163 VkDeviceSize memOffset)
1164 {
1165 stub_return(VK_UNSUPPORTED);
1166 }
1167
1168 static void
1169 anv_fence_destroy(struct anv_device *device,
1170 struct anv_object *object,
1171 VkObjectType obj_type)
1172 {
1173 struct anv_fence *fence = (struct anv_fence *) object;
1174
1175 assert(obj_type == VK_OBJECT_TYPE_FENCE);
1176
1177 anv_gem_munmap(fence->bo.map, fence->bo.size);
1178 anv_gem_close(device, fence->bo.gem_handle);
1179 anv_device_free(device, fence);
1180 }
1181
1182 VkResult anv_CreateFence(
1183 VkDevice _device,
1184 const VkFenceCreateInfo* pCreateInfo,
1185 VkFence* pFence)
1186 {
1187 struct anv_device *device = (struct anv_device *) _device;
1188 struct anv_fence *fence;
1189 struct anv_batch batch;
1190 VkResult result;
1191
1192 const uint32_t fence_size = 128;
1193
1194 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_FENCE_CREATE_INFO);
1195
1196 fence = anv_device_alloc(device, sizeof(*fence), 8,
1197 VK_SYSTEM_ALLOC_TYPE_API_OBJECT);
1198 if (fence == NULL)
1199 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
1200
1201 result = anv_bo_init_new(&fence->bo, device, fence_size);
1202 if (result != VK_SUCCESS)
1203 goto fail;
1204
1205 fence->base.destructor = anv_fence_destroy;
1206
1207 fence->bo.map =
1208 anv_gem_mmap(device, fence->bo.gem_handle, 0, fence->bo.size);
1209 batch.next = batch.start = fence->bo.map;
1210 batch.end = fence->bo.map + fence->bo.size;
1211 anv_batch_emit(&batch, GEN8_MI_BATCH_BUFFER_END);
1212 anv_batch_emit(&batch, GEN8_MI_NOOP);
1213
1214 fence->exec2_objects[0].handle = fence->bo.gem_handle;
1215 fence->exec2_objects[0].relocation_count = 0;
1216 fence->exec2_objects[0].relocs_ptr = 0;
1217 fence->exec2_objects[0].alignment = 0;
1218 fence->exec2_objects[0].offset = fence->bo.offset;
1219 fence->exec2_objects[0].flags = 0;
1220 fence->exec2_objects[0].rsvd1 = 0;
1221 fence->exec2_objects[0].rsvd2 = 0;
1222
1223 fence->execbuf.buffers_ptr = (uintptr_t) fence->exec2_objects;
1224 fence->execbuf.buffer_count = 1;
1225 fence->execbuf.batch_start_offset = 0;
1226 fence->execbuf.batch_len = batch.next - fence->bo.map;
1227 fence->execbuf.cliprects_ptr = 0;
1228 fence->execbuf.num_cliprects = 0;
1229 fence->execbuf.DR1 = 0;
1230 fence->execbuf.DR4 = 0;
1231
1232 fence->execbuf.flags =
1233 I915_EXEC_HANDLE_LUT | I915_EXEC_NO_RELOC | I915_EXEC_RENDER;
1234 fence->execbuf.rsvd1 = device->context_id;
1235 fence->execbuf.rsvd2 = 0;
1236
1237 *pFence = (VkFence) fence;
1238
1239 return VK_SUCCESS;
1240
1241 fail:
1242 anv_device_free(device, fence);
1243
1244 return result;
1245 }
1246
1247 VkResult anv_ResetFences(
1248 VkDevice _device,
1249 uint32_t fenceCount,
1250 const VkFence* pFences)
1251 {
1252 struct anv_fence **fences = (struct anv_fence **) pFences;
1253
1254 for (uint32_t i = 0; i < fenceCount; i++)
1255 fences[i]->ready = false;
1256
1257 return VK_SUCCESS;
1258 }
1259
1260 VkResult anv_GetFenceStatus(
1261 VkDevice _device,
1262 VkFence _fence)
1263 {
1264 struct anv_device *device = (struct anv_device *) _device;
1265 struct anv_fence *fence = (struct anv_fence *) _fence;
1266 int64_t t = 0;
1267 int ret;
1268
1269 if (fence->ready)
1270 return VK_SUCCESS;
1271
1272 ret = anv_gem_wait(device, fence->bo.gem_handle, &t);
1273 if (ret == 0) {
1274 fence->ready = true;
1275 return VK_SUCCESS;
1276 }
1277
1278 return VK_NOT_READY;
1279 }
1280
1281 VkResult anv_WaitForFences(
1282 VkDevice _device,
1283 uint32_t fenceCount,
1284 const VkFence* pFences,
1285 bool32_t waitAll,
1286 uint64_t timeout)
1287 {
1288 struct anv_device *device = (struct anv_device *) _device;
1289 struct anv_fence **fences = (struct anv_fence **) pFences;
1290 int64_t t = timeout;
1291 int ret;
1292
1293 /* FIXME: handle !waitAll */
1294
1295 for (uint32_t i = 0; i < fenceCount; i++) {
1296 ret = anv_gem_wait(device, fences[i]->bo.gem_handle, &t);
1297 if (ret == -1 && errno == ETIME)
1298 return VK_TIMEOUT;
1299 else if (ret == -1)
1300 return vk_error(VK_ERROR_UNKNOWN);
1301 }
1302
1303 return VK_SUCCESS;
1304 }
1305
1306 // Queue semaphore functions
1307
1308 VkResult anv_CreateSemaphore(
1309 VkDevice device,
1310 const VkSemaphoreCreateInfo* pCreateInfo,
1311 VkSemaphore* pSemaphore)
1312 {
1313 stub_return(VK_UNSUPPORTED);
1314 }
1315
1316 VkResult anv_QueueSignalSemaphore(
1317 VkQueue queue,
1318 VkSemaphore semaphore)
1319 {
1320 stub_return(VK_UNSUPPORTED);
1321 }
1322
1323 VkResult anv_QueueWaitSemaphore(
1324 VkQueue queue,
1325 VkSemaphore semaphore)
1326 {
1327 stub_return(VK_UNSUPPORTED);
1328 }
1329
1330 // Event functions
1331
1332 VkResult anv_CreateEvent(
1333 VkDevice device,
1334 const VkEventCreateInfo* pCreateInfo,
1335 VkEvent* pEvent)
1336 {
1337 stub_return(VK_UNSUPPORTED);
1338 }
1339
1340 VkResult anv_GetEventStatus(
1341 VkDevice device,
1342 VkEvent event)
1343 {
1344 stub_return(VK_UNSUPPORTED);
1345 }
1346
1347 VkResult anv_SetEvent(
1348 VkDevice device,
1349 VkEvent event)
1350 {
1351 stub_return(VK_UNSUPPORTED);
1352 }
1353
1354 VkResult anv_ResetEvent(
1355 VkDevice device,
1356 VkEvent event)
1357 {
1358 stub_return(VK_UNSUPPORTED);
1359 }
1360
1361 // Buffer functions
1362
1363 VkResult anv_CreateBuffer(
1364 VkDevice _device,
1365 const VkBufferCreateInfo* pCreateInfo,
1366 VkBuffer* pBuffer)
1367 {
1368 struct anv_device *device = (struct anv_device *) _device;
1369 struct anv_buffer *buffer;
1370
1371 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO);
1372
1373 buffer = anv_device_alloc(device, sizeof(*buffer), 8,
1374 VK_SYSTEM_ALLOC_TYPE_API_OBJECT);
1375 if (buffer == NULL)
1376 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
1377
1378 buffer->size = pCreateInfo->size;
1379 buffer->bo = NULL;
1380 buffer->offset = 0;
1381
1382 *pBuffer = (VkBuffer) buffer;
1383
1384 return VK_SUCCESS;
1385 }
1386
1387 // Buffer view functions
1388
1389 static void
1390 fill_buffer_surface_state(void *state, VkFormat format,
1391 uint32_t offset, uint32_t range)
1392 {
1393 const struct anv_format *info;
1394
1395 info = anv_format_for_vk_format(format);
1396 /* This assumes RGBA float format. */
1397 uint32_t stride = 4;
1398 uint32_t num_elements = range / stride;
1399
1400 struct GEN8_RENDER_SURFACE_STATE surface_state = {
1401 .SurfaceType = SURFTYPE_BUFFER,
1402 .SurfaceArray = false,
1403 .SurfaceFormat = info->surface_format,
1404 .SurfaceVerticalAlignment = VALIGN4,
1405 .SurfaceHorizontalAlignment = HALIGN4,
1406 .TileMode = LINEAR,
1407 .VerticalLineStride = 0,
1408 .VerticalLineStrideOffset = 0,
1409 .SamplerL2BypassModeDisable = true,
1410 .RenderCacheReadWriteMode = WriteOnlyCache,
1411 .MemoryObjectControlState = GEN8_MOCS,
1412 .BaseMipLevel = 0.0,
1413 .SurfaceQPitch = 0,
1414 .Height = (num_elements >> 7) & 0x3fff,
1415 .Width = num_elements & 0x7f,
1416 .Depth = (num_elements >> 21) & 0x3f,
1417 .SurfacePitch = stride - 1,
1418 .MinimumArrayElement = 0,
1419 .NumberofMultisamples = MULTISAMPLECOUNT_1,
1420 .XOffset = 0,
1421 .YOffset = 0,
1422 .SurfaceMinLOD = 0,
1423 .MIPCountLOD = 0,
1424 .AuxiliarySurfaceMode = AUX_NONE,
1425 .RedClearColor = 0,
1426 .GreenClearColor = 0,
1427 .BlueClearColor = 0,
1428 .AlphaClearColor = 0,
1429 .ShaderChannelSelectRed = SCS_RED,
1430 .ShaderChannelSelectGreen = SCS_GREEN,
1431 .ShaderChannelSelectBlue = SCS_BLUE,
1432 .ShaderChannelSelectAlpha = SCS_ALPHA,
1433 .ResourceMinLOD = 0.0,
1434 /* FIXME: We assume that the image must be bound at this time. */
1435 .SurfaceBaseAddress = { NULL, offset },
1436 };
1437
1438 GEN8_RENDER_SURFACE_STATE_pack(NULL, state, &surface_state);
1439 }
1440
1441 VkResult anv_CreateBufferView(
1442 VkDevice _device,
1443 const VkBufferViewCreateInfo* pCreateInfo,
1444 VkBufferView* pView)
1445 {
1446 struct anv_device *device = (struct anv_device *) _device;
1447 struct anv_buffer *buffer = (struct anv_buffer *) pCreateInfo->buffer;
1448 struct anv_surface_view *view;
1449
1450 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO);
1451
1452 view = anv_device_alloc(device, sizeof(*view), 8,
1453 VK_SYSTEM_ALLOC_TYPE_API_OBJECT);
1454 if (view == NULL)
1455 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
1456
1457 view->base.destructor = anv_surface_view_destroy;
1458
1459 view->bo = buffer->bo;
1460 view->offset = buffer->offset + pCreateInfo->offset;
1461 view->surface_state =
1462 anv_state_pool_alloc(&device->surface_state_pool, 64, 64);
1463 view->format = pCreateInfo->format;
1464 view->range = pCreateInfo->range;
1465
1466 fill_buffer_surface_state(view->surface_state.map,
1467 pCreateInfo->format, view->offset, pCreateInfo->range);
1468
1469 *pView = (VkBufferView) view;
1470
1471 return VK_SUCCESS;
1472 }
1473
1474 // Sampler functions
1475
1476 VkResult anv_CreateSampler(
1477 VkDevice _device,
1478 const VkSamplerCreateInfo* pCreateInfo,
1479 VkSampler* pSampler)
1480 {
1481 struct anv_device *device = (struct anv_device *) _device;
1482 struct anv_sampler *sampler;
1483 uint32_t mag_filter, min_filter, max_anisotropy;
1484
1485 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO);
1486
1487 sampler = anv_device_alloc(device, sizeof(*sampler), 8,
1488 VK_SYSTEM_ALLOC_TYPE_API_OBJECT);
1489 if (!sampler)
1490 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
1491
1492 static const uint32_t vk_to_gen_tex_filter[] = {
1493 [VK_TEX_FILTER_NEAREST] = MAPFILTER_NEAREST,
1494 [VK_TEX_FILTER_LINEAR] = MAPFILTER_LINEAR
1495 };
1496
1497 static const uint32_t vk_to_gen_mipmap_mode[] = {
1498 [VK_TEX_MIPMAP_MODE_BASE] = MIPFILTER_NONE,
1499 [VK_TEX_MIPMAP_MODE_NEAREST] = MIPFILTER_NEAREST,
1500 [VK_TEX_MIPMAP_MODE_LINEAR] = MIPFILTER_LINEAR
1501 };
1502
1503 static const uint32_t vk_to_gen_tex_address[] = {
1504 [VK_TEX_ADDRESS_WRAP] = TCM_WRAP,
1505 [VK_TEX_ADDRESS_MIRROR] = TCM_MIRROR,
1506 [VK_TEX_ADDRESS_CLAMP] = TCM_CLAMP,
1507 [VK_TEX_ADDRESS_MIRROR_ONCE] = TCM_MIRROR_ONCE,
1508 [VK_TEX_ADDRESS_CLAMP_BORDER] = TCM_CLAMP_BORDER,
1509 };
1510
1511 static const uint32_t vk_to_gen_compare_op[] = {
1512 [VK_COMPARE_OP_NEVER] = PREFILTEROPNEVER,
1513 [VK_COMPARE_OP_LESS] = PREFILTEROPLESS,
1514 [VK_COMPARE_OP_EQUAL] = PREFILTEROPEQUAL,
1515 [VK_COMPARE_OP_LESS_EQUAL] = PREFILTEROPLEQUAL,
1516 [VK_COMPARE_OP_GREATER] = PREFILTEROPGREATER,
1517 [VK_COMPARE_OP_NOT_EQUAL] = PREFILTEROPNOTEQUAL,
1518 [VK_COMPARE_OP_GREATER_EQUAL] = PREFILTEROPGEQUAL,
1519 [VK_COMPARE_OP_ALWAYS] = PREFILTEROPALWAYS,
1520 };
1521
1522 if (pCreateInfo->maxAnisotropy > 1) {
1523 mag_filter = MAPFILTER_ANISOTROPIC;
1524 min_filter = MAPFILTER_ANISOTROPIC;
1525 max_anisotropy = (pCreateInfo->maxAnisotropy - 2) / 2;
1526 } else {
1527 mag_filter = vk_to_gen_tex_filter[pCreateInfo->magFilter];
1528 min_filter = vk_to_gen_tex_filter[pCreateInfo->minFilter];
1529 max_anisotropy = RATIO21;
1530 }
1531
1532 struct GEN8_SAMPLER_STATE sampler_state = {
1533 .SamplerDisable = false,
1534 .TextureBorderColorMode = DX10OGL,
1535 .LODPreClampMode = 0,
1536 .BaseMipLevel = 0.0,
1537 .MipModeFilter = vk_to_gen_mipmap_mode[pCreateInfo->mipMode],
1538 .MagModeFilter = mag_filter,
1539 .MinModeFilter = min_filter,
1540 .TextureLODBias = pCreateInfo->mipLodBias * 256,
1541 .AnisotropicAlgorithm = EWAApproximation,
1542 .MinLOD = pCreateInfo->minLod,
1543 .MaxLOD = pCreateInfo->maxLod,
1544 .ChromaKeyEnable = 0,
1545 .ChromaKeyIndex = 0,
1546 .ChromaKeyMode = 0,
1547 .ShadowFunction = vk_to_gen_compare_op[pCreateInfo->compareOp],
1548 .CubeSurfaceControlMode = 0,
1549
1550 .IndirectStatePointer =
1551 device->float_border_colors.offset +
1552 pCreateInfo->borderColor * sizeof(float) * 4,
1553
1554 .LODClampMagnificationMode = MIPNONE,
1555 .MaximumAnisotropy = max_anisotropy,
1556 .RAddressMinFilterRoundingEnable = 0,
1557 .RAddressMagFilterRoundingEnable = 0,
1558 .VAddressMinFilterRoundingEnable = 0,
1559 .VAddressMagFilterRoundingEnable = 0,
1560 .UAddressMinFilterRoundingEnable = 0,
1561 .UAddressMagFilterRoundingEnable = 0,
1562 .TrilinearFilterQuality = 0,
1563 .NonnormalizedCoordinateEnable = 0,
1564 .TCXAddressControlMode = vk_to_gen_tex_address[pCreateInfo->addressU],
1565 .TCYAddressControlMode = vk_to_gen_tex_address[pCreateInfo->addressV],
1566 .TCZAddressControlMode = vk_to_gen_tex_address[pCreateInfo->addressW],
1567 };
1568
1569 GEN8_SAMPLER_STATE_pack(NULL, sampler->state, &sampler_state);
1570
1571 *pSampler = (VkSampler) sampler;
1572
1573 return VK_SUCCESS;
1574 }
1575
1576 // Descriptor set functions
1577
1578 VkResult anv_CreateDescriptorSetLayout(
1579 VkDevice _device,
1580 const VkDescriptorSetLayoutCreateInfo* pCreateInfo,
1581 VkDescriptorSetLayout* pSetLayout)
1582 {
1583 struct anv_device *device = (struct anv_device *) _device;
1584 struct anv_descriptor_set_layout *set_layout;
1585
1586 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO);
1587
1588 uint32_t sampler_count[VK_SHADER_STAGE_NUM] = { 0, };
1589 uint32_t surface_count[VK_SHADER_STAGE_NUM] = { 0, };
1590 uint32_t num_dynamic_buffers = 0;
1591 uint32_t count = 0;
1592 uint32_t stages = 0;
1593 uint32_t s;
1594
1595 for (uint32_t i = 0; i < pCreateInfo->count; i++) {
1596 switch (pCreateInfo->pBinding[i].descriptorType) {
1597 case VK_DESCRIPTOR_TYPE_SAMPLER:
1598 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
1599 for_each_bit(s, pCreateInfo->pBinding[i].stageFlags)
1600 sampler_count[s] += pCreateInfo->pBinding[i].arraySize;
1601 break;
1602 default:
1603 break;
1604 }
1605
1606 switch (pCreateInfo->pBinding[i].descriptorType) {
1607 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
1608 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
1609 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE:
1610 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
1611 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
1612 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
1613 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
1614 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
1615 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
1616 for_each_bit(s, pCreateInfo->pBinding[i].stageFlags)
1617 surface_count[s] += pCreateInfo->pBinding[i].arraySize;
1618 break;
1619 default:
1620 break;
1621 }
1622
1623 switch (pCreateInfo->pBinding[i].descriptorType) {
1624 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
1625 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
1626 num_dynamic_buffers += pCreateInfo->pBinding[i].arraySize;
1627 break;
1628 default:
1629 break;
1630 }
1631
1632 stages |= pCreateInfo->pBinding[i].stageFlags;
1633 count += pCreateInfo->pBinding[i].arraySize;
1634 }
1635
1636 uint32_t sampler_total = 0;
1637 uint32_t surface_total = 0;
1638 for (uint32_t s = 0; s < VK_SHADER_STAGE_NUM; s++) {
1639 sampler_total += sampler_count[s];
1640 surface_total += surface_count[s];
1641 }
1642
1643 size_t size = sizeof(*set_layout) +
1644 (sampler_total + surface_total) * sizeof(set_layout->entries[0]);
1645 set_layout = anv_device_alloc(device, size, 8,
1646 VK_SYSTEM_ALLOC_TYPE_API_OBJECT);
1647 if (!set_layout)
1648 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
1649
1650 set_layout->num_dynamic_buffers = num_dynamic_buffers;
1651 set_layout->count = count;
1652 set_layout->shader_stages = stages;
1653
1654 struct anv_descriptor_slot *p = set_layout->entries;
1655 struct anv_descriptor_slot *sampler[VK_SHADER_STAGE_NUM];
1656 struct anv_descriptor_slot *surface[VK_SHADER_STAGE_NUM];
1657 for (uint32_t s = 0; s < VK_SHADER_STAGE_NUM; s++) {
1658 set_layout->stage[s].surface_count = surface_count[s];
1659 set_layout->stage[s].surface_start = surface[s] = p;
1660 p += surface_count[s];
1661 set_layout->stage[s].sampler_count = sampler_count[s];
1662 set_layout->stage[s].sampler_start = sampler[s] = p;
1663 p += sampler_count[s];
1664 }
1665
1666 uint32_t descriptor = 0;
1667 int8_t dynamic_slot = 0;
1668 bool is_dynamic;
1669 for (uint32_t i = 0; i < pCreateInfo->count; i++) {
1670 switch (pCreateInfo->pBinding[i].descriptorType) {
1671 case VK_DESCRIPTOR_TYPE_SAMPLER:
1672 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
1673 for_each_bit(s, pCreateInfo->pBinding[i].stageFlags)
1674 for (uint32_t j = 0; j < pCreateInfo->pBinding[i].arraySize; j++) {
1675 sampler[s]->index = descriptor + j;
1676 sampler[s]->dynamic_slot = -1;
1677 sampler[s]++;
1678 }
1679 break;
1680 default:
1681 break;
1682 }
1683
1684 switch (pCreateInfo->pBinding[i].descriptorType) {
1685 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
1686 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
1687 is_dynamic = true;
1688 break;
1689 default:
1690 is_dynamic = false;
1691 break;
1692 }
1693
1694 switch (pCreateInfo->pBinding[i].descriptorType) {
1695 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
1696 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
1697 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE:
1698 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
1699 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
1700 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
1701 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
1702 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
1703 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
1704 for_each_bit(s, pCreateInfo->pBinding[i].stageFlags)
1705 for (uint32_t j = 0; j < pCreateInfo->pBinding[i].arraySize; j++) {
1706 surface[s]->index = descriptor + j;
1707 if (is_dynamic)
1708 surface[s]->dynamic_slot = dynamic_slot + j;
1709 else
1710 surface[s]->dynamic_slot = -1;
1711 surface[s]++;
1712 }
1713 break;
1714 default:
1715 break;
1716 }
1717
1718 if (is_dynamic)
1719 dynamic_slot += pCreateInfo->pBinding[i].arraySize;
1720
1721 descriptor += pCreateInfo->pBinding[i].arraySize;
1722 }
1723
1724 *pSetLayout = (VkDescriptorSetLayout) set_layout;
1725
1726 return VK_SUCCESS;
1727 }
1728
1729 VkResult anv_CreateDescriptorPool(
1730 VkDevice device,
1731 VkDescriptorPoolUsage poolUsage,
1732 uint32_t maxSets,
1733 const VkDescriptorPoolCreateInfo* pCreateInfo,
1734 VkDescriptorPool* pDescriptorPool)
1735 {
1736 *pDescriptorPool = 1;
1737
1738 return VK_SUCCESS;
1739 }
1740
1741 VkResult anv_ResetDescriptorPool(
1742 VkDevice device,
1743 VkDescriptorPool descriptorPool)
1744 {
1745 return VK_SUCCESS;
1746 }
1747
1748 VkResult anv_AllocDescriptorSets(
1749 VkDevice _device,
1750 VkDescriptorPool descriptorPool,
1751 VkDescriptorSetUsage setUsage,
1752 uint32_t count,
1753 const VkDescriptorSetLayout* pSetLayouts,
1754 VkDescriptorSet* pDescriptorSets,
1755 uint32_t* pCount)
1756 {
1757 struct anv_device *device = (struct anv_device *) _device;
1758 const struct anv_descriptor_set_layout *layout;
1759 struct anv_descriptor_set *set;
1760 size_t size;
1761
1762 for (uint32_t i = 0; i < count; i++) {
1763 layout = (struct anv_descriptor_set_layout *) pSetLayouts[i];
1764 size = sizeof(*set) + layout->count * sizeof(set->descriptors[0]);
1765 set = anv_device_alloc(device, size, 8,
1766 VK_SYSTEM_ALLOC_TYPE_API_OBJECT);
1767 if (!set) {
1768 *pCount = i;
1769 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
1770 }
1771
1772 /* Descriptor sets may not be 100% filled out so we need to memset to
1773 * ensure that we can properly detect and handle holes.
1774 */
1775 memset(set, 0, size);
1776
1777 pDescriptorSets[i] = (VkDescriptorSet) set;
1778 }
1779
1780 *pCount = count;
1781
1782 return VK_SUCCESS;
1783 }
1784
1785 void anv_UpdateDescriptors(
1786 VkDevice _device,
1787 VkDescriptorSet descriptorSet,
1788 uint32_t updateCount,
1789 const void** ppUpdateArray)
1790 {
1791 struct anv_descriptor_set *set = (struct anv_descriptor_set *) descriptorSet;
1792 VkUpdateSamplers *update_samplers;
1793 VkUpdateSamplerTextures *update_sampler_textures;
1794 VkUpdateImages *update_images;
1795 VkUpdateBuffers *update_buffers;
1796 VkUpdateAsCopy *update_as_copy;
1797
1798 for (uint32_t i = 0; i < updateCount; i++) {
1799 const struct anv_common *common = ppUpdateArray[i];
1800
1801 switch (common->sType) {
1802 case VK_STRUCTURE_TYPE_UPDATE_SAMPLERS:
1803 update_samplers = (VkUpdateSamplers *) common;
1804
1805 for (uint32_t j = 0; j < update_samplers->count; j++) {
1806 set->descriptors[update_samplers->binding + j].sampler =
1807 (struct anv_sampler *) update_samplers->pSamplers[j];
1808 }
1809 break;
1810
1811 case VK_STRUCTURE_TYPE_UPDATE_SAMPLER_TEXTURES:
1812 /* FIXME: Shouldn't this be *_UPDATE_SAMPLER_IMAGES? */
1813 update_sampler_textures = (VkUpdateSamplerTextures *) common;
1814
1815 for (uint32_t j = 0; j < update_sampler_textures->count; j++) {
1816 set->descriptors[update_sampler_textures->binding + j].view =
1817 (struct anv_surface_view *)
1818 update_sampler_textures->pSamplerImageViews[j].pImageView->view;
1819 set->descriptors[update_sampler_textures->binding + j].sampler =
1820 (struct anv_sampler *)
1821 update_sampler_textures->pSamplerImageViews[j].sampler;
1822 }
1823 break;
1824
1825 case VK_STRUCTURE_TYPE_UPDATE_IMAGES:
1826 update_images = (VkUpdateImages *) common;
1827
1828 for (uint32_t j = 0; j < update_images->count; j++) {
1829 set->descriptors[update_images->binding + j].view =
1830 (struct anv_surface_view *) update_images->pImageViews[j].view;
1831 }
1832 break;
1833
1834 case VK_STRUCTURE_TYPE_UPDATE_BUFFERS:
1835 update_buffers = (VkUpdateBuffers *) common;
1836
1837 for (uint32_t j = 0; j < update_buffers->count; j++) {
1838 set->descriptors[update_buffers->binding + j].view =
1839 (struct anv_surface_view *) update_buffers->pBufferViews[j].view;
1840 }
1841 /* FIXME: descriptor arrays? */
1842 break;
1843
1844 case VK_STRUCTURE_TYPE_UPDATE_AS_COPY:
1845 update_as_copy = (VkUpdateAsCopy *) common;
1846 (void) update_as_copy;
1847 break;
1848
1849 default:
1850 break;
1851 }
1852 }
1853 }
1854
1855 // State object functions
1856
1857 static inline int64_t
1858 clamp_int64(int64_t x, int64_t min, int64_t max)
1859 {
1860 if (x < min)
1861 return min;
1862 else if (x < max)
1863 return x;
1864 else
1865 return max;
1866 }
1867
1868 static void
1869 anv_dynamic_vp_state_destroy(struct anv_device *device,
1870 struct anv_object *object,
1871 VkObjectType obj_type)
1872 {
1873 struct anv_dynamic_vp_state *state = (void *)object;
1874
1875 assert(obj_type == VK_OBJECT_TYPE_DYNAMIC_VP_STATE);
1876
1877 anv_state_pool_free(&device->dynamic_state_pool, state->sf_clip_vp);
1878 anv_state_pool_free(&device->dynamic_state_pool, state->cc_vp);
1879 anv_state_pool_free(&device->dynamic_state_pool, state->scissor);
1880
1881 anv_device_free(device, state);
1882 }
1883
1884 VkResult anv_CreateDynamicViewportState(
1885 VkDevice _device,
1886 const VkDynamicVpStateCreateInfo* pCreateInfo,
1887 VkDynamicVpState* pState)
1888 {
1889 struct anv_device *device = (struct anv_device *) _device;
1890 struct anv_dynamic_vp_state *state;
1891
1892 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_DYNAMIC_VP_STATE_CREATE_INFO);
1893
1894 state = anv_device_alloc(device, sizeof(*state), 8,
1895 VK_SYSTEM_ALLOC_TYPE_API_OBJECT);
1896 if (state == NULL)
1897 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
1898
1899 state->base.destructor = anv_dynamic_vp_state_destroy;
1900
1901 unsigned count = pCreateInfo->viewportAndScissorCount;
1902 state->sf_clip_vp = anv_state_pool_alloc(&device->dynamic_state_pool,
1903 count * 64, 64);
1904 state->cc_vp = anv_state_pool_alloc(&device->dynamic_state_pool,
1905 count * 8, 32);
1906 state->scissor = anv_state_pool_alloc(&device->dynamic_state_pool,
1907 count * 32, 32);
1908
1909 for (uint32_t i = 0; i < pCreateInfo->viewportAndScissorCount; i++) {
1910 const VkViewport *vp = &pCreateInfo->pViewports[i];
1911 const VkRect2D *s = &pCreateInfo->pScissors[i];
1912
1913 struct GEN8_SF_CLIP_VIEWPORT sf_clip_viewport = {
1914 .ViewportMatrixElementm00 = vp->width / 2,
1915 .ViewportMatrixElementm11 = vp->height / 2,
1916 .ViewportMatrixElementm22 = (vp->maxDepth - vp->minDepth) / 2,
1917 .ViewportMatrixElementm30 = vp->originX + vp->width / 2,
1918 .ViewportMatrixElementm31 = vp->originY + vp->height / 2,
1919 .ViewportMatrixElementm32 = (vp->maxDepth + vp->minDepth) / 2,
1920 .XMinClipGuardband = -1.0f,
1921 .XMaxClipGuardband = 1.0f,
1922 .YMinClipGuardband = -1.0f,
1923 .YMaxClipGuardband = 1.0f,
1924 .XMinViewPort = vp->originX,
1925 .XMaxViewPort = vp->originX + vp->width - 1,
1926 .YMinViewPort = vp->originY,
1927 .YMaxViewPort = vp->originY + vp->height - 1,
1928 };
1929
1930 struct GEN8_CC_VIEWPORT cc_viewport = {
1931 .MinimumDepth = vp->minDepth,
1932 .MaximumDepth = vp->maxDepth
1933 };
1934
1935 /* Since xmax and ymax are inclusive, we have to have xmax < xmin or
1936 * ymax < ymin for empty clips. In case clip x, y, width height are all
1937 * 0, the clamps below produce 0 for xmin, ymin, xmax, ymax, which isn't
1938 * what we want. Just special case empty clips and produce a canonical
1939 * empty clip. */
1940 static const struct GEN8_SCISSOR_RECT empty_scissor = {
1941 .ScissorRectangleYMin = 1,
1942 .ScissorRectangleXMin = 1,
1943 .ScissorRectangleYMax = 0,
1944 .ScissorRectangleXMax = 0
1945 };
1946
1947 const int max = 0xffff;
1948 struct GEN8_SCISSOR_RECT scissor = {
1949 /* Do this math using int64_t so overflow gets clamped correctly. */
1950 .ScissorRectangleYMin = clamp_int64(s->offset.y, 0, max),
1951 .ScissorRectangleXMin = clamp_int64(s->offset.x, 0, max),
1952 .ScissorRectangleYMax = clamp_int64((uint64_t) s->offset.y + s->extent.height - 1, 0, max),
1953 .ScissorRectangleXMax = clamp_int64((uint64_t) s->offset.x + s->extent.width - 1, 0, max)
1954 };
1955
1956 GEN8_SF_CLIP_VIEWPORT_pack(NULL, state->sf_clip_vp.map + i * 64, &sf_clip_viewport);
1957 GEN8_CC_VIEWPORT_pack(NULL, state->cc_vp.map + i * 32, &cc_viewport);
1958
1959 if (s->extent.width <= 0 || s->extent.height <= 0) {
1960 GEN8_SCISSOR_RECT_pack(NULL, state->scissor.map + i * 32, &empty_scissor);
1961 } else {
1962 GEN8_SCISSOR_RECT_pack(NULL, state->scissor.map + i * 32, &scissor);
1963 }
1964 }
1965
1966 *pState = (VkDynamicVpState) state;
1967
1968 return VK_SUCCESS;
1969 }
1970
1971 VkResult anv_CreateDynamicRasterState(
1972 VkDevice _device,
1973 const VkDynamicRsStateCreateInfo* pCreateInfo,
1974 VkDynamicRsState* pState)
1975 {
1976 struct anv_device *device = (struct anv_device *) _device;
1977 struct anv_dynamic_rs_state *state;
1978
1979 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_DYNAMIC_RS_STATE_CREATE_INFO);
1980
1981 state = anv_device_alloc(device, sizeof(*state), 8,
1982 VK_SYSTEM_ALLOC_TYPE_API_OBJECT);
1983 if (state == NULL)
1984 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
1985
1986 struct GEN8_3DSTATE_SF sf = {
1987 GEN8_3DSTATE_SF_header,
1988 .LineWidth = pCreateInfo->lineWidth,
1989 };
1990
1991 GEN8_3DSTATE_SF_pack(NULL, state->state_sf, &sf);
1992
1993 bool enable_bias = pCreateInfo->depthBias != 0.0f ||
1994 pCreateInfo->slopeScaledDepthBias != 0.0f;
1995 struct GEN8_3DSTATE_RASTER raster = {
1996 .GlobalDepthOffsetEnableSolid = enable_bias,
1997 .GlobalDepthOffsetEnableWireframe = enable_bias,
1998 .GlobalDepthOffsetEnablePoint = enable_bias,
1999 .GlobalDepthOffsetConstant = pCreateInfo->depthBias,
2000 .GlobalDepthOffsetScale = pCreateInfo->slopeScaledDepthBias,
2001 .GlobalDepthOffsetClamp = pCreateInfo->depthBiasClamp
2002 };
2003
2004 GEN8_3DSTATE_RASTER_pack(NULL, state->state_raster, &raster);
2005
2006 *pState = (VkDynamicRsState) state;
2007
2008 return VK_SUCCESS;
2009 }
2010
2011 VkResult anv_CreateDynamicColorBlendState(
2012 VkDevice _device,
2013 const VkDynamicCbStateCreateInfo* pCreateInfo,
2014 VkDynamicCbState* pState)
2015 {
2016 struct anv_device *device = (struct anv_device *) _device;
2017 struct anv_dynamic_cb_state *state;
2018
2019 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_DYNAMIC_CB_STATE_CREATE_INFO);
2020
2021 state = anv_device_alloc(device, sizeof(*state), 8,
2022 VK_SYSTEM_ALLOC_TYPE_API_OBJECT);
2023 if (state == NULL)
2024 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
2025
2026 struct GEN8_COLOR_CALC_STATE color_calc_state = {
2027 .BlendConstantColorRed = pCreateInfo->blendConst[0],
2028 .BlendConstantColorGreen = pCreateInfo->blendConst[1],
2029 .BlendConstantColorBlue = pCreateInfo->blendConst[2],
2030 .BlendConstantColorAlpha = pCreateInfo->blendConst[3]
2031 };
2032
2033 GEN8_COLOR_CALC_STATE_pack(NULL, state->state_color_calc, &color_calc_state);
2034
2035 *pState = (VkDynamicCbState) state;
2036
2037 return VK_SUCCESS;
2038 }
2039
2040 VkResult anv_CreateDynamicDepthStencilState(
2041 VkDevice _device,
2042 const VkDynamicDsStateCreateInfo* pCreateInfo,
2043 VkDynamicDsState* pState)
2044 {
2045 struct anv_device *device = (struct anv_device *) _device;
2046 struct anv_dynamic_ds_state *state;
2047
2048 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_DYNAMIC_DS_STATE_CREATE_INFO);
2049
2050 state = anv_device_alloc(device, sizeof(*state), 8,
2051 VK_SYSTEM_ALLOC_TYPE_API_OBJECT);
2052 if (state == NULL)
2053 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
2054
2055 struct GEN8_3DSTATE_WM_DEPTH_STENCIL wm_depth_stencil = {
2056 GEN8_3DSTATE_WM_DEPTH_STENCIL_header,
2057
2058 /* Is this what we need to do? */
2059 .StencilBufferWriteEnable = pCreateInfo->stencilWriteMask != 0,
2060
2061 .StencilTestMask = pCreateInfo->stencilReadMask & 0xff,
2062 .StencilWriteMask = pCreateInfo->stencilWriteMask & 0xff,
2063
2064 .BackfaceStencilTestMask = pCreateInfo->stencilReadMask & 0xff,
2065 .BackfaceStencilWriteMask = pCreateInfo->stencilWriteMask & 0xff,
2066 };
2067
2068 GEN8_3DSTATE_WM_DEPTH_STENCIL_pack(NULL, state->state_wm_depth_stencil,
2069 &wm_depth_stencil);
2070
2071 struct GEN8_COLOR_CALC_STATE color_calc_state = {
2072 .StencilReferenceValue = pCreateInfo->stencilFrontRef,
2073 .BackFaceStencilReferenceValue = pCreateInfo->stencilBackRef
2074 };
2075
2076 GEN8_COLOR_CALC_STATE_pack(NULL, state->state_color_calc, &color_calc_state);
2077
2078 *pState = (VkDynamicDsState) state;
2079
2080 return VK_SUCCESS;
2081 }
2082
2083 // Command buffer functions
2084
2085 static void
2086 anv_cmd_buffer_destroy(struct anv_device *device,
2087 struct anv_object *object,
2088 VkObjectType obj_type)
2089 {
2090 struct anv_cmd_buffer *cmd_buffer = (struct anv_cmd_buffer *) object;
2091
2092 assert(obj_type == VK_OBJECT_TYPE_COMMAND_BUFFER);
2093
2094 /* Destroy all of the batch buffers */
2095 struct anv_batch_bo *bbo = cmd_buffer->last_batch_bo;
2096 while (bbo) {
2097 struct anv_batch_bo *prev = bbo->prev_batch_bo;
2098 anv_batch_bo_destroy(bbo, device);
2099 bbo = prev;
2100 }
2101 anv_reloc_list_finish(&cmd_buffer->batch.relocs, device);
2102
2103 /* Destroy all of the surface state buffers */
2104 bbo = cmd_buffer->surface_batch_bo;
2105 while (bbo) {
2106 struct anv_batch_bo *prev = bbo->prev_batch_bo;
2107 anv_batch_bo_destroy(bbo, device);
2108 bbo = prev;
2109 }
2110 anv_reloc_list_finish(&cmd_buffer->surface_relocs, device);
2111
2112 anv_state_stream_finish(&cmd_buffer->surface_state_stream);
2113 anv_state_stream_finish(&cmd_buffer->dynamic_state_stream);
2114 anv_device_free(device, cmd_buffer->exec2_objects);
2115 anv_device_free(device, cmd_buffer->exec2_bos);
2116 anv_device_free(device, cmd_buffer);
2117 }
2118
2119 static VkResult
2120 anv_cmd_buffer_chain_batch(struct anv_batch *batch, void *_data)
2121 {
2122 struct anv_cmd_buffer *cmd_buffer = _data;
2123
2124 struct anv_batch_bo *new_bbo, *old_bbo = cmd_buffer->last_batch_bo;
2125
2126 VkResult result = anv_batch_bo_create(cmd_buffer->device, &new_bbo);
2127 if (result != VK_SUCCESS)
2128 return result;
2129
2130 /* We set the end of the batch a little short so we would be sure we
2131 * have room for the chaining command. Since we're about to emit the
2132 * chaining command, let's set it back where it should go.
2133 */
2134 batch->end += GEN8_MI_BATCH_BUFFER_START_length * 4;
2135 assert(batch->end == old_bbo->bo.map + old_bbo->bo.size);
2136
2137 anv_batch_emit(batch, GEN8_MI_BATCH_BUFFER_START,
2138 GEN8_MI_BATCH_BUFFER_START_header,
2139 ._2ndLevelBatchBuffer = _1stlevelbatch,
2140 .AddressSpaceIndicator = ASI_PPGTT,
2141 .BatchBufferStartAddress = { &new_bbo->bo, 0 },
2142 );
2143
2144 /* Pad out to a 2-dword aligned boundary with zeros */
2145 if ((uintptr_t)batch->next % 8 != 0) {
2146 *(uint32_t *)batch->next = 0;
2147 batch->next += 4;
2148 }
2149
2150 anv_batch_bo_finish(cmd_buffer->last_batch_bo, batch);
2151
2152 new_bbo->prev_batch_bo = old_bbo;
2153 cmd_buffer->last_batch_bo = new_bbo;
2154
2155 anv_batch_bo_start(new_bbo, batch, GEN8_MI_BATCH_BUFFER_START_length * 4);
2156
2157 return VK_SUCCESS;
2158 }
2159
2160 VkResult anv_CreateCommandBuffer(
2161 VkDevice _device,
2162 const VkCmdBufferCreateInfo* pCreateInfo,
2163 VkCmdBuffer* pCmdBuffer)
2164 {
2165 struct anv_device *device = (struct anv_device *) _device;
2166 struct anv_cmd_buffer *cmd_buffer;
2167 VkResult result;
2168
2169 cmd_buffer = anv_device_alloc(device, sizeof(*cmd_buffer), 8,
2170 VK_SYSTEM_ALLOC_TYPE_API_OBJECT);
2171 if (cmd_buffer == NULL)
2172 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
2173
2174 cmd_buffer->base.destructor = anv_cmd_buffer_destroy;
2175
2176 cmd_buffer->device = device;
2177 cmd_buffer->rs_state = NULL;
2178 cmd_buffer->vp_state = NULL;
2179 cmd_buffer->cb_state = NULL;
2180 cmd_buffer->ds_state = NULL;
2181 memset(&cmd_buffer->state_vf, 0, sizeof(cmd_buffer->state_vf));
2182 memset(&cmd_buffer->descriptors, 0, sizeof(cmd_buffer->descriptors));
2183
2184 result = anv_batch_bo_create(device, &cmd_buffer->last_batch_bo);
2185 if (result != VK_SUCCESS)
2186 goto fail;
2187
2188 result = anv_reloc_list_init(&cmd_buffer->batch.relocs, device);
2189 if (result != VK_SUCCESS)
2190 goto fail_batch_bo;
2191
2192 cmd_buffer->batch.device = device;
2193 cmd_buffer->batch.extend_cb = anv_cmd_buffer_chain_batch;
2194 cmd_buffer->batch.user_data = cmd_buffer;
2195
2196 anv_batch_bo_start(cmd_buffer->last_batch_bo, &cmd_buffer->batch,
2197 GEN8_MI_BATCH_BUFFER_START_length * 4);
2198
2199 result = anv_batch_bo_create(device, &cmd_buffer->surface_batch_bo);
2200 if (result != VK_SUCCESS)
2201 goto fail_batch_relocs;
2202 cmd_buffer->surface_batch_bo->first_reloc = 0;
2203
2204 result = anv_reloc_list_init(&cmd_buffer->surface_relocs, device);
2205 if (result != VK_SUCCESS)
2206 goto fail_ss_batch_bo;
2207
2208 /* Start surface_next at 1 so surface offset 0 is invalid. */
2209 cmd_buffer->surface_next = 1;
2210
2211 cmd_buffer->exec2_objects = NULL;
2212 cmd_buffer->exec2_bos = NULL;
2213 cmd_buffer->exec2_array_length = 0;
2214
2215 anv_state_stream_init(&cmd_buffer->surface_state_stream,
2216 &device->surface_state_block_pool);
2217 anv_state_stream_init(&cmd_buffer->dynamic_state_stream,
2218 &device->dynamic_state_block_pool);
2219
2220 cmd_buffer->dirty = 0;
2221 cmd_buffer->vb_dirty = 0;
2222 cmd_buffer->descriptors_dirty = 0;
2223 cmd_buffer->pipeline = NULL;
2224 cmd_buffer->vp_state = NULL;
2225 cmd_buffer->rs_state = NULL;
2226 cmd_buffer->ds_state = NULL;
2227
2228 *pCmdBuffer = (VkCmdBuffer) cmd_buffer;
2229
2230 return VK_SUCCESS;
2231
2232 fail_ss_batch_bo:
2233 anv_batch_bo_destroy(cmd_buffer->surface_batch_bo, device);
2234 fail_batch_relocs:
2235 anv_reloc_list_finish(&cmd_buffer->batch.relocs, device);
2236 fail_batch_bo:
2237 anv_batch_bo_destroy(cmd_buffer->last_batch_bo, device);
2238 fail:
2239 anv_device_free(device, cmd_buffer);
2240
2241 return result;
2242 }
2243
2244 static void
2245 anv_cmd_buffer_emit_state_base_address(struct anv_cmd_buffer *cmd_buffer)
2246 {
2247 struct anv_device *device = cmd_buffer->device;
2248 struct anv_bo *scratch_bo = NULL;
2249
2250 cmd_buffer->scratch_size = device->scratch_block_pool.size;
2251 if (cmd_buffer->scratch_size > 0)
2252 scratch_bo = &device->scratch_block_pool.bo;
2253
2254 anv_batch_emit(&cmd_buffer->batch, GEN8_STATE_BASE_ADDRESS,
2255 .GeneralStateBaseAddress = { scratch_bo, 0 },
2256 .GeneralStateMemoryObjectControlState = GEN8_MOCS,
2257 .GeneralStateBaseAddressModifyEnable = true,
2258 .GeneralStateBufferSize = 0xfffff,
2259 .GeneralStateBufferSizeModifyEnable = true,
2260
2261 .SurfaceStateBaseAddress = { &cmd_buffer->surface_batch_bo->bo, 0 },
2262 .SurfaceStateMemoryObjectControlState = GEN8_MOCS,
2263 .SurfaceStateBaseAddressModifyEnable = true,
2264
2265 .DynamicStateBaseAddress = { &device->dynamic_state_block_pool.bo, 0 },
2266 .DynamicStateMemoryObjectControlState = GEN8_MOCS,
2267 .DynamicStateBaseAddressModifyEnable = true,
2268 .DynamicStateBufferSize = 0xfffff,
2269 .DynamicStateBufferSizeModifyEnable = true,
2270
2271 .IndirectObjectBaseAddress = { NULL, 0 },
2272 .IndirectObjectMemoryObjectControlState = GEN8_MOCS,
2273 .IndirectObjectBaseAddressModifyEnable = true,
2274 .IndirectObjectBufferSize = 0xfffff,
2275 .IndirectObjectBufferSizeModifyEnable = true,
2276
2277 .InstructionBaseAddress = { &device->instruction_block_pool.bo, 0 },
2278 .InstructionMemoryObjectControlState = GEN8_MOCS,
2279 .InstructionBaseAddressModifyEnable = true,
2280 .InstructionBufferSize = 0xfffff,
2281 .InstructionBuffersizeModifyEnable = true);
2282 }
2283
2284 VkResult anv_BeginCommandBuffer(
2285 VkCmdBuffer cmdBuffer,
2286 const VkCmdBufferBeginInfo* pBeginInfo)
2287 {
2288 struct anv_cmd_buffer *cmd_buffer = (struct anv_cmd_buffer *) cmdBuffer;
2289
2290 anv_cmd_buffer_emit_state_base_address(cmd_buffer);
2291 cmd_buffer->current_pipeline = UINT32_MAX;
2292
2293 return VK_SUCCESS;
2294 }
2295
2296 static VkResult
2297 anv_cmd_buffer_add_bo(struct anv_cmd_buffer *cmd_buffer,
2298 struct anv_bo *bo,
2299 struct drm_i915_gem_relocation_entry *relocs,
2300 size_t num_relocs)
2301 {
2302 struct drm_i915_gem_exec_object2 *obj;
2303
2304 if (bo->index < cmd_buffer->bo_count &&
2305 cmd_buffer->exec2_bos[bo->index] == bo)
2306 return VK_SUCCESS;
2307
2308 if (cmd_buffer->bo_count >= cmd_buffer->exec2_array_length) {
2309 uint32_t new_len = cmd_buffer->exec2_objects ?
2310 cmd_buffer->exec2_array_length * 2 : 64;
2311
2312 struct drm_i915_gem_exec_object2 *new_objects =
2313 anv_device_alloc(cmd_buffer->device, new_len * sizeof(*new_objects),
2314 8, VK_SYSTEM_ALLOC_TYPE_INTERNAL);
2315 if (new_objects == NULL)
2316 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
2317
2318 struct anv_bo **new_bos =
2319 anv_device_alloc(cmd_buffer->device, new_len * sizeof(*new_bos),
2320 8, VK_SYSTEM_ALLOC_TYPE_INTERNAL);
2321 if (new_objects == NULL) {
2322 anv_device_free(cmd_buffer->device, new_objects);
2323 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
2324 }
2325
2326 if (cmd_buffer->exec2_objects) {
2327 memcpy(new_objects, cmd_buffer->exec2_objects,
2328 cmd_buffer->bo_count * sizeof(*new_objects));
2329 memcpy(new_bos, cmd_buffer->exec2_bos,
2330 cmd_buffer->bo_count * sizeof(*new_bos));
2331 }
2332
2333 cmd_buffer->exec2_objects = new_objects;
2334 cmd_buffer->exec2_bos = new_bos;
2335 cmd_buffer->exec2_array_length = new_len;
2336 }
2337
2338 assert(cmd_buffer->bo_count < cmd_buffer->exec2_array_length);
2339
2340 bo->index = cmd_buffer->bo_count++;
2341 obj = &cmd_buffer->exec2_objects[bo->index];
2342 cmd_buffer->exec2_bos[bo->index] = bo;
2343
2344 obj->handle = bo->gem_handle;
2345 obj->relocation_count = 0;
2346 obj->relocs_ptr = 0;
2347 obj->alignment = 0;
2348 obj->offset = bo->offset;
2349 obj->flags = 0;
2350 obj->rsvd1 = 0;
2351 obj->rsvd2 = 0;
2352
2353 if (relocs) {
2354 obj->relocation_count = num_relocs;
2355 obj->relocs_ptr = (uintptr_t) relocs;
2356 }
2357
2358 return VK_SUCCESS;
2359 }
2360
2361 static void
2362 anv_cmd_buffer_add_validate_bos(struct anv_cmd_buffer *cmd_buffer,
2363 struct anv_reloc_list *list)
2364 {
2365 for (size_t i = 0; i < list->num_relocs; i++)
2366 anv_cmd_buffer_add_bo(cmd_buffer, list->reloc_bos[i], NULL, 0);
2367 }
2368
2369 static void
2370 anv_cmd_buffer_process_relocs(struct anv_cmd_buffer *cmd_buffer,
2371 struct anv_reloc_list *list)
2372 {
2373 struct anv_bo *bo;
2374
2375 /* If the kernel supports I915_EXEC_NO_RELOC, it will compare offset in
2376 * struct drm_i915_gem_exec_object2 against the bos current offset and if
2377 * all bos haven't moved it will skip relocation processing alltogether.
2378 * If I915_EXEC_NO_RELOC is not supported, the kernel ignores the incoming
2379 * value of offset so we can set it either way. For that to work we need
2380 * to make sure all relocs use the same presumed offset.
2381 */
2382
2383 for (size_t i = 0; i < list->num_relocs; i++) {
2384 bo = list->reloc_bos[i];
2385 if (bo->offset != list->relocs[i].presumed_offset)
2386 cmd_buffer->need_reloc = true;
2387
2388 list->relocs[i].target_handle = bo->index;
2389 }
2390 }
2391
2392 VkResult anv_EndCommandBuffer(
2393 VkCmdBuffer cmdBuffer)
2394 {
2395 struct anv_cmd_buffer *cmd_buffer = (struct anv_cmd_buffer *) cmdBuffer;
2396 struct anv_device *device = cmd_buffer->device;
2397 struct anv_batch *batch = &cmd_buffer->batch;
2398
2399 anv_batch_emit(batch, GEN8_MI_BATCH_BUFFER_END);
2400
2401 /* Round batch up to an even number of dwords. */
2402 if ((batch->next - batch->start) & 4)
2403 anv_batch_emit(batch, GEN8_MI_NOOP);
2404
2405 anv_batch_bo_finish(cmd_buffer->last_batch_bo, &cmd_buffer->batch);
2406 cmd_buffer->surface_batch_bo->num_relocs =
2407 cmd_buffer->surface_relocs.num_relocs - cmd_buffer->surface_batch_bo->first_reloc;
2408 cmd_buffer->surface_batch_bo->length = cmd_buffer->surface_next;
2409
2410 cmd_buffer->bo_count = 0;
2411 cmd_buffer->need_reloc = false;
2412
2413 /* Lock for access to bo->index. */
2414 pthread_mutex_lock(&device->mutex);
2415
2416 /* Add surface state bos first so we can add them with their relocs. */
2417 for (struct anv_batch_bo *bbo = cmd_buffer->surface_batch_bo;
2418 bbo != NULL; bbo = bbo->prev_batch_bo) {
2419 anv_cmd_buffer_add_bo(cmd_buffer, &bbo->bo,
2420 &cmd_buffer->surface_relocs.relocs[bbo->first_reloc],
2421 bbo->num_relocs);
2422 }
2423
2424 /* Add all of the BOs referenced by surface state */
2425 anv_cmd_buffer_add_validate_bos(cmd_buffer, &cmd_buffer->surface_relocs);
2426
2427 /* Add all but the first batch BO */
2428 struct anv_batch_bo *batch_bo = cmd_buffer->last_batch_bo;
2429 while (batch_bo->prev_batch_bo) {
2430 anv_cmd_buffer_add_bo(cmd_buffer, &batch_bo->bo,
2431 &batch->relocs.relocs[batch_bo->first_reloc],
2432 batch_bo->num_relocs);
2433 batch_bo = batch_bo->prev_batch_bo;
2434 }
2435
2436 /* Add everything referenced by the batches */
2437 anv_cmd_buffer_add_validate_bos(cmd_buffer, &batch->relocs);
2438
2439 /* Add the first batch bo last */
2440 assert(batch_bo->prev_batch_bo == NULL && batch_bo->first_reloc == 0);
2441 anv_cmd_buffer_add_bo(cmd_buffer, &batch_bo->bo,
2442 &batch->relocs.relocs[batch_bo->first_reloc],
2443 batch_bo->num_relocs);
2444 assert(batch_bo->bo.index == cmd_buffer->bo_count - 1);
2445
2446 anv_cmd_buffer_process_relocs(cmd_buffer, &cmd_buffer->surface_relocs);
2447 anv_cmd_buffer_process_relocs(cmd_buffer, &batch->relocs);
2448
2449 cmd_buffer->execbuf.buffers_ptr = (uintptr_t) cmd_buffer->exec2_objects;
2450 cmd_buffer->execbuf.buffer_count = cmd_buffer->bo_count;
2451 cmd_buffer->execbuf.batch_start_offset = 0;
2452 cmd_buffer->execbuf.batch_len = batch->next - batch->start;
2453 cmd_buffer->execbuf.cliprects_ptr = 0;
2454 cmd_buffer->execbuf.num_cliprects = 0;
2455 cmd_buffer->execbuf.DR1 = 0;
2456 cmd_buffer->execbuf.DR4 = 0;
2457
2458 cmd_buffer->execbuf.flags = I915_EXEC_HANDLE_LUT;
2459 if (!cmd_buffer->need_reloc)
2460 cmd_buffer->execbuf.flags |= I915_EXEC_NO_RELOC;
2461 cmd_buffer->execbuf.flags |= I915_EXEC_RENDER;
2462 cmd_buffer->execbuf.rsvd1 = device->context_id;
2463 cmd_buffer->execbuf.rsvd2 = 0;
2464
2465 pthread_mutex_unlock(&device->mutex);
2466
2467 return VK_SUCCESS;
2468 }
2469
2470 VkResult anv_ResetCommandBuffer(
2471 VkCmdBuffer cmdBuffer)
2472 {
2473 struct anv_cmd_buffer *cmd_buffer = (struct anv_cmd_buffer *) cmdBuffer;
2474
2475 /* Delete all but the first batch bo */
2476 while (cmd_buffer->last_batch_bo->prev_batch_bo) {
2477 struct anv_batch_bo *prev = cmd_buffer->last_batch_bo->prev_batch_bo;
2478 anv_batch_bo_destroy(cmd_buffer->last_batch_bo, cmd_buffer->device);
2479 cmd_buffer->last_batch_bo = prev;
2480 }
2481 assert(cmd_buffer->last_batch_bo->prev_batch_bo == NULL);
2482
2483 cmd_buffer->batch.relocs.num_relocs = 0;
2484 anv_batch_bo_start(cmd_buffer->last_batch_bo, &cmd_buffer->batch,
2485 GEN8_MI_BATCH_BUFFER_START_length * 4);
2486
2487 /* Delete all but the first batch bo */
2488 while (cmd_buffer->surface_batch_bo->prev_batch_bo) {
2489 struct anv_batch_bo *prev = cmd_buffer->surface_batch_bo->prev_batch_bo;
2490 anv_batch_bo_destroy(cmd_buffer->surface_batch_bo, cmd_buffer->device);
2491 cmd_buffer->surface_batch_bo = prev;
2492 }
2493 assert(cmd_buffer->surface_batch_bo->prev_batch_bo == NULL);
2494
2495 cmd_buffer->surface_next = 1;
2496 cmd_buffer->surface_relocs.num_relocs = 0;
2497
2498 cmd_buffer->rs_state = NULL;
2499 cmd_buffer->vp_state = NULL;
2500 cmd_buffer->cb_state = NULL;
2501 cmd_buffer->ds_state = NULL;
2502
2503 return VK_SUCCESS;
2504 }
2505
2506 // Command buffer building functions
2507
2508 void anv_CmdBindPipeline(
2509 VkCmdBuffer cmdBuffer,
2510 VkPipelineBindPoint pipelineBindPoint,
2511 VkPipeline _pipeline)
2512 {
2513 struct anv_cmd_buffer *cmd_buffer = (struct anv_cmd_buffer *) cmdBuffer;
2514 struct anv_pipeline *pipeline = (struct anv_pipeline *) _pipeline;
2515
2516 switch (pipelineBindPoint) {
2517 case VK_PIPELINE_BIND_POINT_COMPUTE:
2518 cmd_buffer->compute_pipeline = pipeline;
2519 cmd_buffer->compute_dirty |= ANV_CMD_BUFFER_PIPELINE_DIRTY;
2520 break;
2521
2522 case VK_PIPELINE_BIND_POINT_GRAPHICS:
2523 cmd_buffer->pipeline = pipeline;
2524 cmd_buffer->vb_dirty |= pipeline->vb_used;
2525 cmd_buffer->dirty |= ANV_CMD_BUFFER_PIPELINE_DIRTY;
2526 break;
2527
2528 default:
2529 assert(!"invalid bind point");
2530 break;
2531 }
2532 }
2533
2534 void anv_CmdBindDynamicStateObject(
2535 VkCmdBuffer cmdBuffer,
2536 VkStateBindPoint stateBindPoint,
2537 VkDynamicStateObject dynamicState)
2538 {
2539 struct anv_cmd_buffer *cmd_buffer = (struct anv_cmd_buffer *) cmdBuffer;
2540
2541 switch (stateBindPoint) {
2542 case VK_STATE_BIND_POINT_VIEWPORT:
2543 cmd_buffer->vp_state = (struct anv_dynamic_vp_state *) dynamicState;
2544 cmd_buffer->dirty |= ANV_CMD_BUFFER_VP_DIRTY;
2545 break;
2546 case VK_STATE_BIND_POINT_RASTER:
2547 cmd_buffer->rs_state = (struct anv_dynamic_rs_state *) dynamicState;
2548 cmd_buffer->dirty |= ANV_CMD_BUFFER_RS_DIRTY;
2549 break;
2550 case VK_STATE_BIND_POINT_COLOR_BLEND:
2551 cmd_buffer->cb_state = (struct anv_dynamic_cb_state *) dynamicState;
2552 cmd_buffer->dirty |= ANV_CMD_BUFFER_CB_DIRTY;
2553 break;
2554 case VK_STATE_BIND_POINT_DEPTH_STENCIL:
2555 cmd_buffer->ds_state = (struct anv_dynamic_ds_state *) dynamicState;
2556 cmd_buffer->dirty |= ANV_CMD_BUFFER_DS_DIRTY;
2557 break;
2558 default:
2559 break;
2560 };
2561 }
2562
2563 static struct anv_state
2564 anv_cmd_buffer_alloc_surface_state(struct anv_cmd_buffer *cmd_buffer,
2565 uint32_t size, uint32_t alignment)
2566 {
2567 struct anv_state state;
2568
2569 state.offset = align_u32(cmd_buffer->surface_next, alignment);
2570 if (state.offset + size > cmd_buffer->surface_batch_bo->bo.size)
2571 return (struct anv_state) { 0 };
2572
2573 state.map = cmd_buffer->surface_batch_bo->bo.map + state.offset;
2574 state.alloc_size = size;
2575 cmd_buffer->surface_next = state.offset + size;
2576
2577 assert(state.offset + size <= cmd_buffer->surface_batch_bo->bo.size);
2578
2579 return state;
2580 }
2581
2582 static VkResult
2583 anv_cmd_buffer_new_surface_state_bo(struct anv_cmd_buffer *cmd_buffer)
2584 {
2585 struct anv_batch_bo *new_bbo, *old_bbo = cmd_buffer->surface_batch_bo;
2586
2587 /* Finish off the old buffer */
2588 old_bbo->num_relocs =
2589 cmd_buffer->surface_relocs.num_relocs - old_bbo->first_reloc;
2590 old_bbo->length = cmd_buffer->surface_next;
2591
2592 VkResult result = anv_batch_bo_create(cmd_buffer->device, &new_bbo);
2593 if (result != VK_SUCCESS)
2594 return result;
2595
2596 new_bbo->first_reloc = cmd_buffer->surface_relocs.num_relocs;
2597 cmd_buffer->surface_next = 1;
2598
2599 new_bbo->prev_batch_bo = old_bbo;
2600 cmd_buffer->surface_batch_bo = new_bbo;
2601
2602 /* Re-emit state base addresses so we get the new surface state base
2603 * address before we start emitting binding tables etc.
2604 */
2605 anv_cmd_buffer_emit_state_base_address(cmd_buffer);
2606
2607 /* It seems like just changing the state base addresses isn't enough.
2608 * Invalidating the cache seems to be enough to cause things to
2609 * propagate. However, I'm not 100% sure what we're supposed to do.
2610 */
2611 anv_batch_emit(&cmd_buffer->batch, GEN8_PIPE_CONTROL,
2612 .TextureCacheInvalidationEnable = true);
2613
2614 return VK_SUCCESS;
2615 }
2616
2617 void anv_CmdBindDescriptorSets(
2618 VkCmdBuffer cmdBuffer,
2619 VkPipelineBindPoint pipelineBindPoint,
2620 VkPipelineLayout _layout,
2621 uint32_t firstSet,
2622 uint32_t setCount,
2623 const VkDescriptorSet* pDescriptorSets,
2624 uint32_t dynamicOffsetCount,
2625 const uint32_t* pDynamicOffsets)
2626 {
2627 struct anv_cmd_buffer *cmd_buffer = (struct anv_cmd_buffer *) cmdBuffer;
2628 struct anv_pipeline_layout *layout = (struct anv_pipeline_layout *) _layout;
2629 struct anv_descriptor_set *set;
2630 struct anv_descriptor_set_layout *set_layout;
2631
2632 assert(firstSet + setCount < MAX_SETS);
2633
2634 uint32_t dynamic_slot = 0;
2635 for (uint32_t i = 0; i < setCount; i++) {
2636 set = (struct anv_descriptor_set *) pDescriptorSets[i];
2637 set_layout = layout->set[firstSet + i].layout;
2638
2639 cmd_buffer->descriptors[firstSet + i].set = set;
2640
2641 assert(set_layout->num_dynamic_buffers <
2642 ARRAY_SIZE(cmd_buffer->descriptors[0].dynamic_offsets));
2643 memcpy(cmd_buffer->descriptors[firstSet + i].dynamic_offsets,
2644 pDynamicOffsets + dynamic_slot,
2645 set_layout->num_dynamic_buffers * sizeof(*pDynamicOffsets));
2646
2647 cmd_buffer->descriptors_dirty |= set_layout->shader_stages;
2648
2649 dynamic_slot += set_layout->num_dynamic_buffers;
2650 }
2651 }
2652
2653 void anv_CmdBindIndexBuffer(
2654 VkCmdBuffer cmdBuffer,
2655 VkBuffer _buffer,
2656 VkDeviceSize offset,
2657 VkIndexType indexType)
2658 {
2659 struct anv_cmd_buffer *cmd_buffer = (struct anv_cmd_buffer *) cmdBuffer;
2660 struct anv_buffer *buffer = (struct anv_buffer *) _buffer;
2661
2662 static const uint32_t vk_to_gen_index_type[] = {
2663 [VK_INDEX_TYPE_UINT16] = INDEX_WORD,
2664 [VK_INDEX_TYPE_UINT32] = INDEX_DWORD,
2665 };
2666
2667 struct GEN8_3DSTATE_VF vf = {
2668 GEN8_3DSTATE_VF_header,
2669 .CutIndex = (indexType == VK_INDEX_TYPE_UINT16) ? UINT16_MAX : UINT32_MAX,
2670 };
2671 GEN8_3DSTATE_VF_pack(NULL, cmd_buffer->state_vf, &vf);
2672
2673 cmd_buffer->dirty |= ANV_CMD_BUFFER_INDEX_BUFFER_DIRTY;
2674
2675 anv_batch_emit(&cmd_buffer->batch, GEN8_3DSTATE_INDEX_BUFFER,
2676 .IndexFormat = vk_to_gen_index_type[indexType],
2677 .MemoryObjectControlState = GEN8_MOCS,
2678 .BufferStartingAddress = { buffer->bo, buffer->offset + offset },
2679 .BufferSize = buffer->size - offset);
2680 }
2681
2682 void anv_CmdBindVertexBuffers(
2683 VkCmdBuffer cmdBuffer,
2684 uint32_t startBinding,
2685 uint32_t bindingCount,
2686 const VkBuffer* pBuffers,
2687 const VkDeviceSize* pOffsets)
2688 {
2689 struct anv_cmd_buffer *cmd_buffer = (struct anv_cmd_buffer *) cmdBuffer;
2690 struct anv_vertex_binding *vb = cmd_buffer->vertex_bindings;
2691
2692 /* We have to defer setting up vertex buffer since we need the buffer
2693 * stride from the pipeline. */
2694
2695 assert(startBinding + bindingCount < MAX_VBS);
2696 for (uint32_t i = 0; i < bindingCount; i++) {
2697 vb[startBinding + i].buffer = (struct anv_buffer *) pBuffers[i];
2698 vb[startBinding + i].offset = pOffsets[i];
2699 cmd_buffer->vb_dirty |= 1 << (startBinding + i);
2700 }
2701 }
2702
2703 static VkResult
2704 cmd_buffer_emit_binding_table(struct anv_cmd_buffer *cmd_buffer,
2705 unsigned stage, struct anv_state *bt_state)
2706 {
2707 struct anv_pipeline_layout *layout;
2708 uint32_t color_attachments, bias, size;
2709
2710 if (stage == VK_SHADER_STAGE_COMPUTE)
2711 layout = cmd_buffer->compute_pipeline->layout;
2712 else
2713 layout = cmd_buffer->pipeline->layout;
2714
2715 if (stage == VK_SHADER_STAGE_FRAGMENT) {
2716 bias = MAX_RTS;
2717 color_attachments = cmd_buffer->framebuffer->color_attachment_count;
2718 } else {
2719 bias = 0;
2720 color_attachments = 0;
2721 }
2722
2723 /* This is a little awkward: layout can be NULL but we still have to
2724 * allocate and set a binding table for the PS stage for render
2725 * targets. */
2726 uint32_t surface_count = layout ? layout->stage[stage].surface_count : 0;
2727
2728 if (color_attachments + surface_count == 0)
2729 return VK_SUCCESS;
2730
2731 size = (bias + surface_count) * sizeof(uint32_t);
2732 *bt_state = anv_cmd_buffer_alloc_surface_state(cmd_buffer, size, 32);
2733 uint32_t *bt_map = bt_state->map;
2734
2735 if (bt_state->map == NULL)
2736 return VK_ERROR_OUT_OF_DEVICE_MEMORY;
2737
2738 for (uint32_t ca = 0; ca < color_attachments; ca++) {
2739 const struct anv_surface_view *view =
2740 cmd_buffer->framebuffer->color_attachments[ca];
2741
2742 struct anv_state state =
2743 anv_cmd_buffer_alloc_surface_state(cmd_buffer, 64, 64);
2744
2745 if (state.map == NULL)
2746 return VK_ERROR_OUT_OF_DEVICE_MEMORY;
2747
2748 memcpy(state.map, view->surface_state.map, 64);
2749
2750 /* The address goes in dwords 8 and 9 of the SURFACE_STATE */
2751 *(uint64_t *)(state.map + 8 * 4) =
2752 anv_reloc_list_add(&cmd_buffer->surface_relocs,
2753 cmd_buffer->device,
2754 state.offset + 8 * 4,
2755 view->bo, view->offset);
2756
2757 bt_map[ca] = state.offset;
2758 }
2759
2760 if (layout == NULL)
2761 return VK_SUCCESS;
2762
2763 for (uint32_t set = 0; set < layout->num_sets; set++) {
2764 struct anv_descriptor_set_binding *d = &cmd_buffer->descriptors[set];
2765 struct anv_descriptor_set_layout *set_layout = layout->set[set].layout;
2766 struct anv_descriptor_slot *surface_slots =
2767 set_layout->stage[stage].surface_start;
2768
2769 uint32_t start = bias + layout->set[set].surface_start[stage];
2770
2771 for (uint32_t b = 0; b < set_layout->stage[stage].surface_count; b++) {
2772 struct anv_surface_view *view =
2773 d->set->descriptors[surface_slots[b].index].view;
2774
2775 if (!view)
2776 continue;
2777
2778 struct anv_state state =
2779 anv_cmd_buffer_alloc_surface_state(cmd_buffer, 64, 64);
2780
2781 if (state.map == NULL)
2782 return VK_ERROR_OUT_OF_DEVICE_MEMORY;
2783
2784 uint32_t offset;
2785 if (surface_slots[b].dynamic_slot >= 0) {
2786 uint32_t dynamic_offset =
2787 d->dynamic_offsets[surface_slots[b].dynamic_slot];
2788
2789 offset = view->offset + dynamic_offset;
2790 fill_buffer_surface_state(state.map, view->format, offset,
2791 view->range - dynamic_offset);
2792 } else {
2793 offset = view->offset;
2794 memcpy(state.map, view->surface_state.map, 64);
2795 }
2796
2797 /* The address goes in dwords 8 and 9 of the SURFACE_STATE */
2798 *(uint64_t *)(state.map + 8 * 4) =
2799 anv_reloc_list_add(&cmd_buffer->surface_relocs,
2800 cmd_buffer->device,
2801 state.offset + 8 * 4,
2802 view->bo, offset);
2803
2804 bt_map[start + b] = state.offset;
2805 }
2806 }
2807
2808 return VK_SUCCESS;
2809 }
2810
2811 static VkResult
2812 cmd_buffer_emit_samplers(struct anv_cmd_buffer *cmd_buffer,
2813 unsigned stage, struct anv_state *state)
2814 {
2815 struct anv_pipeline_layout *layout;
2816 uint32_t sampler_count;
2817
2818 if (stage == VK_SHADER_STAGE_COMPUTE)
2819 layout = cmd_buffer->compute_pipeline->layout;
2820 else
2821 layout = cmd_buffer->pipeline->layout;
2822
2823 sampler_count = layout ? layout->stage[stage].sampler_count : 0;
2824 if (sampler_count == 0)
2825 return VK_SUCCESS;
2826
2827 uint32_t size = sampler_count * 16;
2828 *state = anv_state_stream_alloc(&cmd_buffer->dynamic_state_stream, size, 32);
2829
2830 if (state->map == NULL)
2831 return VK_ERROR_OUT_OF_DEVICE_MEMORY;
2832
2833 for (uint32_t set = 0; set < layout->num_sets; set++) {
2834 struct anv_descriptor_set_binding *d = &cmd_buffer->descriptors[set];
2835 struct anv_descriptor_set_layout *set_layout = layout->set[set].layout;
2836 struct anv_descriptor_slot *sampler_slots =
2837 set_layout->stage[stage].sampler_start;
2838
2839 uint32_t start = layout->set[set].sampler_start[stage];
2840
2841 for (uint32_t b = 0; b < set_layout->stage[stage].sampler_count; b++) {
2842 struct anv_sampler *sampler =
2843 d->set->descriptors[sampler_slots[b].index].sampler;
2844
2845 if (!sampler)
2846 continue;
2847
2848 memcpy(state->map + (start + b) * 16,
2849 sampler->state, sizeof(sampler->state));
2850 }
2851 }
2852
2853 return VK_SUCCESS;
2854 }
2855
2856 static VkResult
2857 flush_descriptor_set(struct anv_cmd_buffer *cmd_buffer, uint32_t stage)
2858 {
2859 struct anv_state surfaces = { 0, }, samplers = { 0, };
2860 VkResult result;
2861
2862 result = cmd_buffer_emit_samplers(cmd_buffer, stage, &samplers);
2863 if (result != VK_SUCCESS)
2864 return result;
2865 result = cmd_buffer_emit_binding_table(cmd_buffer, stage, &surfaces);
2866 if (result != VK_SUCCESS)
2867 return result;
2868
2869 static const uint32_t sampler_state_opcodes[] = {
2870 [VK_SHADER_STAGE_VERTEX] = 43,
2871 [VK_SHADER_STAGE_TESS_CONTROL] = 44, /* HS */
2872 [VK_SHADER_STAGE_TESS_EVALUATION] = 45, /* DS */
2873 [VK_SHADER_STAGE_GEOMETRY] = 46,
2874 [VK_SHADER_STAGE_FRAGMENT] = 47,
2875 [VK_SHADER_STAGE_COMPUTE] = 0,
2876 };
2877
2878 static const uint32_t binding_table_opcodes[] = {
2879 [VK_SHADER_STAGE_VERTEX] = 38,
2880 [VK_SHADER_STAGE_TESS_CONTROL] = 39,
2881 [VK_SHADER_STAGE_TESS_EVALUATION] = 40,
2882 [VK_SHADER_STAGE_GEOMETRY] = 41,
2883 [VK_SHADER_STAGE_FRAGMENT] = 42,
2884 [VK_SHADER_STAGE_COMPUTE] = 0,
2885 };
2886
2887 if (samplers.alloc_size > 0) {
2888 anv_batch_emit(&cmd_buffer->batch,
2889 GEN8_3DSTATE_SAMPLER_STATE_POINTERS_VS,
2890 ._3DCommandSubOpcode = sampler_state_opcodes[stage],
2891 .PointertoVSSamplerState = samplers.offset);
2892 }
2893
2894 if (surfaces.alloc_size > 0) {
2895 anv_batch_emit(&cmd_buffer->batch,
2896 GEN8_3DSTATE_BINDING_TABLE_POINTERS_VS,
2897 ._3DCommandSubOpcode = binding_table_opcodes[stage],
2898 .PointertoVSBindingTable = surfaces.offset);
2899 }
2900
2901 return VK_SUCCESS;
2902 }
2903
2904 static void
2905 flush_descriptor_sets(struct anv_cmd_buffer *cmd_buffer)
2906 {
2907 uint32_t s, dirty = cmd_buffer->descriptors_dirty &
2908 cmd_buffer->pipeline->active_stages;
2909
2910 VkResult result;
2911 for_each_bit(s, dirty) {
2912 result = flush_descriptor_set(cmd_buffer, s);
2913 if (result != VK_SUCCESS)
2914 break;
2915 }
2916
2917 if (result != VK_SUCCESS) {
2918 assert(result == VK_ERROR_OUT_OF_DEVICE_MEMORY);
2919
2920 result = anv_cmd_buffer_new_surface_state_bo(cmd_buffer);
2921 assert(result == VK_SUCCESS);
2922
2923 /* Re-emit all active binding tables */
2924 for_each_bit(s, cmd_buffer->pipeline->active_stages) {
2925 result = flush_descriptor_set(cmd_buffer, s);
2926
2927 /* It had better succeed this time */
2928 assert(result == VK_SUCCESS);
2929 }
2930 }
2931
2932 cmd_buffer->descriptors_dirty &= ~cmd_buffer->pipeline->active_stages;
2933 }
2934
2935 static struct anv_state
2936 anv_cmd_buffer_emit_dynamic(struct anv_cmd_buffer *cmd_buffer,
2937 uint32_t *a, uint32_t dwords, uint32_t alignment)
2938 {
2939 struct anv_state state;
2940
2941 state = anv_state_stream_alloc(&cmd_buffer->dynamic_state_stream,
2942 dwords * 4, alignment);
2943 memcpy(state.map, a, dwords * 4);
2944
2945 VG(VALGRIND_CHECK_MEM_IS_DEFINED(state.map, dwords * 4));
2946
2947 return state;
2948 }
2949
2950 static struct anv_state
2951 anv_cmd_buffer_merge_dynamic(struct anv_cmd_buffer *cmd_buffer,
2952 uint32_t *a, uint32_t *b,
2953 uint32_t dwords, uint32_t alignment)
2954 {
2955 struct anv_state state;
2956 uint32_t *p;
2957
2958 state = anv_state_stream_alloc(&cmd_buffer->dynamic_state_stream,
2959 dwords * 4, alignment);
2960 p = state.map;
2961 for (uint32_t i = 0; i < dwords; i++)
2962 p[i] = a[i] | b[i];
2963
2964 VG(VALGRIND_CHECK_MEM_IS_DEFINED(p, dwords * 4));
2965
2966 return state;
2967 }
2968
2969 static VkResult
2970 flush_compute_descriptor_set(struct anv_cmd_buffer *cmd_buffer)
2971 {
2972 struct anv_device *device = cmd_buffer->device;
2973 struct anv_pipeline *pipeline = cmd_buffer->compute_pipeline;
2974 struct anv_state surfaces = { 0, }, samplers = { 0, };
2975 VkResult result;
2976
2977 result = cmd_buffer_emit_samplers(cmd_buffer,
2978 VK_SHADER_STAGE_COMPUTE, &samplers);
2979 if (result != VK_SUCCESS)
2980 return result;
2981 result = cmd_buffer_emit_binding_table(cmd_buffer,
2982 VK_SHADER_STAGE_COMPUTE, &surfaces);
2983 if (result != VK_SUCCESS)
2984 return result;
2985
2986 struct GEN8_INTERFACE_DESCRIPTOR_DATA desc = {
2987 .KernelStartPointer = pipeline->cs_simd,
2988 .KernelStartPointerHigh = 0,
2989 .BindingTablePointer = surfaces.offset,
2990 .BindingTableEntryCount = 0,
2991 .SamplerStatePointer = samplers.offset,
2992 .SamplerCount = 0,
2993 .NumberofThreadsinGPGPUThreadGroup = 0 /* FIXME: Really? */
2994 };
2995
2996 uint32_t size = GEN8_INTERFACE_DESCRIPTOR_DATA_length * sizeof(uint32_t);
2997 struct anv_state state =
2998 anv_state_pool_alloc(&device->dynamic_state_pool, size, 64);
2999
3000 GEN8_INTERFACE_DESCRIPTOR_DATA_pack(NULL, state.map, &desc);
3001
3002 anv_batch_emit(&cmd_buffer->batch, GEN8_MEDIA_INTERFACE_DESCRIPTOR_LOAD,
3003 .InterfaceDescriptorTotalLength = size,
3004 .InterfaceDescriptorDataStartAddress = state.offset);
3005
3006 return VK_SUCCESS;
3007 }
3008
3009 static void
3010 anv_cmd_buffer_flush_compute_state(struct anv_cmd_buffer *cmd_buffer)
3011 {
3012 struct anv_pipeline *pipeline = cmd_buffer->compute_pipeline;
3013 VkResult result;
3014
3015 assert(pipeline->active_stages == VK_SHADER_STAGE_COMPUTE_BIT);
3016
3017 if (cmd_buffer->current_pipeline != GPGPU) {
3018 anv_batch_emit(&cmd_buffer->batch, GEN8_PIPELINE_SELECT,
3019 .PipelineSelection = GPGPU);
3020 cmd_buffer->current_pipeline = GPGPU;
3021 }
3022
3023 if (cmd_buffer->compute_dirty & ANV_CMD_BUFFER_PIPELINE_DIRTY)
3024 anv_batch_emit_batch(&cmd_buffer->batch, &pipeline->batch);
3025
3026 if ((cmd_buffer->descriptors_dirty & VK_SHADER_STAGE_COMPUTE_BIT) ||
3027 (cmd_buffer->compute_dirty & ANV_CMD_BUFFER_PIPELINE_DIRTY)) {
3028 result = flush_compute_descriptor_set(cmd_buffer);
3029 if (result != VK_SUCCESS) {
3030 result = anv_cmd_buffer_new_surface_state_bo(cmd_buffer);
3031 assert(result == VK_SUCCESS);
3032 result = flush_compute_descriptor_set(cmd_buffer);
3033 assert(result == VK_SUCCESS);
3034 }
3035 cmd_buffer->descriptors_dirty &= ~VK_SHADER_STAGE_COMPUTE;
3036 }
3037
3038 cmd_buffer->compute_dirty = 0;
3039 }
3040
3041 static void
3042 anv_cmd_buffer_flush_state(struct anv_cmd_buffer *cmd_buffer)
3043 {
3044 struct anv_pipeline *pipeline = cmd_buffer->pipeline;
3045 uint32_t *p;
3046
3047 uint32_t vb_emit = cmd_buffer->vb_dirty & pipeline->vb_used;
3048
3049 assert((pipeline->active_stages & VK_SHADER_STAGE_COMPUTE_BIT) == 0);
3050
3051 if (cmd_buffer->current_pipeline != _3D) {
3052 anv_batch_emit(&cmd_buffer->batch, GEN8_PIPELINE_SELECT,
3053 .PipelineSelection = _3D);
3054 cmd_buffer->current_pipeline = _3D;
3055 }
3056
3057 if (vb_emit) {
3058 const uint32_t num_buffers = __builtin_popcount(vb_emit);
3059 const uint32_t num_dwords = 1 + num_buffers * 4;
3060
3061 p = anv_batch_emitn(&cmd_buffer->batch, num_dwords,
3062 GEN8_3DSTATE_VERTEX_BUFFERS);
3063 uint32_t vb, i = 0;
3064 for_each_bit(vb, vb_emit) {
3065 struct anv_buffer *buffer = cmd_buffer->vertex_bindings[vb].buffer;
3066 uint32_t offset = cmd_buffer->vertex_bindings[vb].offset;
3067
3068 struct GEN8_VERTEX_BUFFER_STATE state = {
3069 .VertexBufferIndex = vb,
3070 .MemoryObjectControlState = GEN8_MOCS,
3071 .AddressModifyEnable = true,
3072 .BufferPitch = pipeline->binding_stride[vb],
3073 .BufferStartingAddress = { buffer->bo, buffer->offset + offset },
3074 .BufferSize = buffer->size - offset
3075 };
3076
3077 GEN8_VERTEX_BUFFER_STATE_pack(&cmd_buffer->batch, &p[1 + i * 4], &state);
3078 i++;
3079 }
3080 }
3081
3082 if (cmd_buffer->dirty & ANV_CMD_BUFFER_PIPELINE_DIRTY) {
3083 /* If somebody compiled a pipeline after starting a command buffer the
3084 * scratch bo may have grown since we started this cmd buffer (and
3085 * emitted STATE_BASE_ADDRESS). If we're binding that pipeline now,
3086 * reemit STATE_BASE_ADDRESS so that we use the bigger scratch bo. */
3087 if (cmd_buffer->scratch_size < pipeline->total_scratch)
3088 anv_cmd_buffer_emit_state_base_address(cmd_buffer);
3089
3090 anv_batch_emit_batch(&cmd_buffer->batch, &pipeline->batch);
3091 }
3092
3093 if (cmd_buffer->descriptors_dirty)
3094 flush_descriptor_sets(cmd_buffer);
3095
3096 if (cmd_buffer->dirty & ANV_CMD_BUFFER_VP_DIRTY) {
3097 anv_batch_emit(&cmd_buffer->batch, GEN8_3DSTATE_SCISSOR_STATE_POINTERS,
3098 .ScissorRectPointer = cmd_buffer->vp_state->scissor.offset);
3099 anv_batch_emit(&cmd_buffer->batch, GEN8_3DSTATE_VIEWPORT_STATE_POINTERS_CC,
3100 .CCViewportPointer = cmd_buffer->vp_state->cc_vp.offset);
3101 anv_batch_emit(&cmd_buffer->batch, GEN8_3DSTATE_VIEWPORT_STATE_POINTERS_SF_CLIP,
3102 .SFClipViewportPointer = cmd_buffer->vp_state->sf_clip_vp.offset);
3103 }
3104
3105 if (cmd_buffer->dirty & (ANV_CMD_BUFFER_PIPELINE_DIRTY | ANV_CMD_BUFFER_RS_DIRTY)) {
3106 anv_batch_emit_merge(&cmd_buffer->batch,
3107 cmd_buffer->rs_state->state_sf, pipeline->state_sf);
3108 anv_batch_emit_merge(&cmd_buffer->batch,
3109 cmd_buffer->rs_state->state_raster, pipeline->state_raster);
3110 }
3111
3112 if (cmd_buffer->ds_state &&
3113 (cmd_buffer->dirty & (ANV_CMD_BUFFER_PIPELINE_DIRTY | ANV_CMD_BUFFER_DS_DIRTY)))
3114 anv_batch_emit_merge(&cmd_buffer->batch,
3115 cmd_buffer->ds_state->state_wm_depth_stencil,
3116 pipeline->state_wm_depth_stencil);
3117
3118 if (cmd_buffer->dirty & (ANV_CMD_BUFFER_CB_DIRTY | ANV_CMD_BUFFER_DS_DIRTY)) {
3119 struct anv_state state;
3120 if (cmd_buffer->ds_state == NULL)
3121 state = anv_cmd_buffer_emit_dynamic(cmd_buffer,
3122 cmd_buffer->cb_state->state_color_calc,
3123 GEN8_COLOR_CALC_STATE_length, 64);
3124 else if (cmd_buffer->cb_state == NULL)
3125 state = anv_cmd_buffer_emit_dynamic(cmd_buffer,
3126 cmd_buffer->ds_state->state_color_calc,
3127 GEN8_COLOR_CALC_STATE_length, 64);
3128 else
3129 state = anv_cmd_buffer_merge_dynamic(cmd_buffer,
3130 cmd_buffer->ds_state->state_color_calc,
3131 cmd_buffer->cb_state->state_color_calc,
3132 GEN8_COLOR_CALC_STATE_length, 64);
3133
3134 anv_batch_emit(&cmd_buffer->batch,
3135 GEN8_3DSTATE_CC_STATE_POINTERS,
3136 .ColorCalcStatePointer = state.offset,
3137 .ColorCalcStatePointerValid = true);
3138 }
3139
3140 if (cmd_buffer->dirty & (ANV_CMD_BUFFER_PIPELINE_DIRTY | ANV_CMD_BUFFER_INDEX_BUFFER_DIRTY)) {
3141 anv_batch_emit_merge(&cmd_buffer->batch,
3142 cmd_buffer->state_vf, pipeline->state_vf);
3143 }
3144
3145 cmd_buffer->vb_dirty &= ~vb_emit;
3146 cmd_buffer->dirty = 0;
3147 }
3148
3149 void anv_CmdDraw(
3150 VkCmdBuffer cmdBuffer,
3151 uint32_t firstVertex,
3152 uint32_t vertexCount,
3153 uint32_t firstInstance,
3154 uint32_t instanceCount)
3155 {
3156 struct anv_cmd_buffer *cmd_buffer = (struct anv_cmd_buffer *) cmdBuffer;
3157
3158 anv_cmd_buffer_flush_state(cmd_buffer);
3159
3160 anv_batch_emit(&cmd_buffer->batch, GEN8_3DPRIMITIVE,
3161 .VertexAccessType = SEQUENTIAL,
3162 .VertexCountPerInstance = vertexCount,
3163 .StartVertexLocation = firstVertex,
3164 .InstanceCount = instanceCount,
3165 .StartInstanceLocation = firstInstance,
3166 .BaseVertexLocation = 0);
3167 }
3168
3169 void anv_CmdDrawIndexed(
3170 VkCmdBuffer cmdBuffer,
3171 uint32_t firstIndex,
3172 uint32_t indexCount,
3173 int32_t vertexOffset,
3174 uint32_t firstInstance,
3175 uint32_t instanceCount)
3176 {
3177 struct anv_cmd_buffer *cmd_buffer = (struct anv_cmd_buffer *) cmdBuffer;
3178
3179 anv_cmd_buffer_flush_state(cmd_buffer);
3180
3181 anv_batch_emit(&cmd_buffer->batch, GEN8_3DPRIMITIVE,
3182 .VertexAccessType = RANDOM,
3183 .VertexCountPerInstance = indexCount,
3184 .StartVertexLocation = firstIndex,
3185 .InstanceCount = instanceCount,
3186 .StartInstanceLocation = firstInstance,
3187 .BaseVertexLocation = vertexOffset);
3188 }
3189
3190 static void
3191 anv_batch_lrm(struct anv_batch *batch,
3192 uint32_t reg, struct anv_bo *bo, uint32_t offset)
3193 {
3194 anv_batch_emit(batch, GEN8_MI_LOAD_REGISTER_MEM,
3195 .RegisterAddress = reg,
3196 .MemoryAddress = { bo, offset });
3197 }
3198
3199 static void
3200 anv_batch_lri(struct anv_batch *batch, uint32_t reg, uint32_t imm)
3201 {
3202 anv_batch_emit(batch, GEN8_MI_LOAD_REGISTER_IMM,
3203 .RegisterOffset = reg,
3204 .DataDWord = imm);
3205 }
3206
3207 /* Auto-Draw / Indirect Registers */
3208 #define GEN7_3DPRIM_END_OFFSET 0x2420
3209 #define GEN7_3DPRIM_START_VERTEX 0x2430
3210 #define GEN7_3DPRIM_VERTEX_COUNT 0x2434
3211 #define GEN7_3DPRIM_INSTANCE_COUNT 0x2438
3212 #define GEN7_3DPRIM_START_INSTANCE 0x243C
3213 #define GEN7_3DPRIM_BASE_VERTEX 0x2440
3214
3215 void anv_CmdDrawIndirect(
3216 VkCmdBuffer cmdBuffer,
3217 VkBuffer _buffer,
3218 VkDeviceSize offset,
3219 uint32_t count,
3220 uint32_t stride)
3221 {
3222 struct anv_cmd_buffer *cmd_buffer = (struct anv_cmd_buffer *) cmdBuffer;
3223 struct anv_buffer *buffer = (struct anv_buffer *) _buffer;
3224 struct anv_bo *bo = buffer->bo;
3225 uint32_t bo_offset = buffer->offset + offset;
3226
3227 anv_cmd_buffer_flush_state(cmd_buffer);
3228
3229 anv_batch_lrm(&cmd_buffer->batch, GEN7_3DPRIM_VERTEX_COUNT, bo, bo_offset);
3230 anv_batch_lrm(&cmd_buffer->batch, GEN7_3DPRIM_INSTANCE_COUNT, bo, bo_offset + 4);
3231 anv_batch_lrm(&cmd_buffer->batch, GEN7_3DPRIM_START_VERTEX, bo, bo_offset + 8);
3232 anv_batch_lrm(&cmd_buffer->batch, GEN7_3DPRIM_START_INSTANCE, bo, bo_offset + 12);
3233 anv_batch_lri(&cmd_buffer->batch, GEN7_3DPRIM_BASE_VERTEX, 0);
3234
3235 anv_batch_emit(&cmd_buffer->batch, GEN8_3DPRIMITIVE,
3236 .IndirectParameterEnable = true,
3237 .VertexAccessType = SEQUENTIAL);
3238 }
3239
3240 void anv_CmdDrawIndexedIndirect(
3241 VkCmdBuffer cmdBuffer,
3242 VkBuffer _buffer,
3243 VkDeviceSize offset,
3244 uint32_t count,
3245 uint32_t stride)
3246 {
3247 struct anv_cmd_buffer *cmd_buffer = (struct anv_cmd_buffer *) cmdBuffer;
3248 struct anv_buffer *buffer = (struct anv_buffer *) _buffer;
3249 struct anv_bo *bo = buffer->bo;
3250 uint32_t bo_offset = buffer->offset + offset;
3251
3252 anv_cmd_buffer_flush_state(cmd_buffer);
3253
3254 anv_batch_lrm(&cmd_buffer->batch, GEN7_3DPRIM_VERTEX_COUNT, bo, bo_offset);
3255 anv_batch_lrm(&cmd_buffer->batch, GEN7_3DPRIM_INSTANCE_COUNT, bo, bo_offset + 4);
3256 anv_batch_lrm(&cmd_buffer->batch, GEN7_3DPRIM_START_VERTEX, bo, bo_offset + 8);
3257 anv_batch_lrm(&cmd_buffer->batch, GEN7_3DPRIM_BASE_VERTEX, bo, bo_offset + 12);
3258 anv_batch_lrm(&cmd_buffer->batch, GEN7_3DPRIM_START_INSTANCE, bo, bo_offset + 16);
3259
3260 anv_batch_emit(&cmd_buffer->batch, GEN8_3DPRIMITIVE,
3261 .IndirectParameterEnable = true,
3262 .VertexAccessType = RANDOM);
3263 }
3264
3265 void anv_CmdDispatch(
3266 VkCmdBuffer cmdBuffer,
3267 uint32_t x,
3268 uint32_t y,
3269 uint32_t z)
3270 {
3271 struct anv_cmd_buffer *cmd_buffer = (struct anv_cmd_buffer *) cmdBuffer;
3272 struct anv_pipeline *pipeline = cmd_buffer->compute_pipeline;
3273 struct brw_cs_prog_data *prog_data = &pipeline->cs_prog_data;
3274
3275 anv_cmd_buffer_flush_compute_state(cmd_buffer);
3276
3277 anv_batch_emit(&cmd_buffer->batch, GEN8_GPGPU_WALKER,
3278 .SIMDSize = prog_data->simd_size / 16,
3279 .ThreadDepthCounterMaximum = 0,
3280 .ThreadHeightCounterMaximum = 0,
3281 .ThreadWidthCounterMaximum = pipeline->cs_thread_width_max,
3282 .ThreadGroupIDXDimension = x,
3283 .ThreadGroupIDYDimension = y,
3284 .ThreadGroupIDZDimension = z,
3285 .RightExecutionMask = pipeline->cs_right_mask,
3286 .BottomExecutionMask = 0xffffffff);
3287
3288 anv_batch_emit(&cmd_buffer->batch, GEN8_MEDIA_STATE_FLUSH);
3289 }
3290
3291 #define GPGPU_DISPATCHDIMX 0x2500
3292 #define GPGPU_DISPATCHDIMY 0x2504
3293 #define GPGPU_DISPATCHDIMZ 0x2508
3294
3295 void anv_CmdDispatchIndirect(
3296 VkCmdBuffer cmdBuffer,
3297 VkBuffer _buffer,
3298 VkDeviceSize offset)
3299 {
3300 struct anv_cmd_buffer *cmd_buffer = (struct anv_cmd_buffer *) cmdBuffer;
3301 struct anv_pipeline *pipeline = cmd_buffer->compute_pipeline;
3302 struct brw_cs_prog_data *prog_data = &pipeline->cs_prog_data;
3303 struct anv_buffer *buffer = (struct anv_buffer *) _buffer;
3304 struct anv_bo *bo = buffer->bo;
3305 uint32_t bo_offset = buffer->offset + offset;
3306
3307 anv_cmd_buffer_flush_compute_state(cmd_buffer);
3308
3309 anv_batch_lrm(&cmd_buffer->batch, GPGPU_DISPATCHDIMX, bo, bo_offset);
3310 anv_batch_lrm(&cmd_buffer->batch, GPGPU_DISPATCHDIMY, bo, bo_offset + 4);
3311 anv_batch_lrm(&cmd_buffer->batch, GPGPU_DISPATCHDIMZ, bo, bo_offset + 8);
3312
3313 anv_batch_emit(&cmd_buffer->batch, GEN8_GPGPU_WALKER,
3314 .IndirectParameterEnable = true,
3315 .SIMDSize = prog_data->simd_size / 16,
3316 .ThreadDepthCounterMaximum = 0,
3317 .ThreadHeightCounterMaximum = 0,
3318 .ThreadWidthCounterMaximum = pipeline->cs_thread_width_max,
3319 .RightExecutionMask = pipeline->cs_right_mask,
3320 .BottomExecutionMask = 0xffffffff);
3321
3322 anv_batch_emit(&cmd_buffer->batch, GEN8_MEDIA_STATE_FLUSH);
3323 }
3324
3325 void anv_CmdSetEvent(
3326 VkCmdBuffer cmdBuffer,
3327 VkEvent event,
3328 VkPipeEvent pipeEvent)
3329 {
3330 stub();
3331 }
3332
3333 void anv_CmdResetEvent(
3334 VkCmdBuffer cmdBuffer,
3335 VkEvent event,
3336 VkPipeEvent pipeEvent)
3337 {
3338 stub();
3339 }
3340
3341 void anv_CmdWaitEvents(
3342 VkCmdBuffer cmdBuffer,
3343 VkWaitEvent waitEvent,
3344 uint32_t eventCount,
3345 const VkEvent* pEvents,
3346 VkPipeEventFlags pipeEventMask,
3347 uint32_t memBarrierCount,
3348 const void* const* ppMemBarriers)
3349 {
3350 stub();
3351 }
3352
3353 void anv_CmdPipelineBarrier(
3354 VkCmdBuffer cmdBuffer,
3355 VkWaitEvent waitEvent,
3356 VkPipeEventFlags pipeEventMask,
3357 uint32_t memBarrierCount,
3358 const void* const* ppMemBarriers)
3359 {
3360 struct anv_cmd_buffer *cmd_buffer = (struct anv_cmd_buffer *)cmdBuffer;
3361 uint32_t b, *dw;
3362
3363 struct GEN8_PIPE_CONTROL cmd = {
3364 GEN8_PIPE_CONTROL_header,
3365 .PostSyncOperation = NoWrite,
3366 };
3367
3368 /* XXX: I think waitEvent is a no-op on our HW. We should verify that. */
3369
3370 if (anv_clear_mask(&pipeEventMask, VK_PIPE_EVENT_TOP_OF_PIPE_BIT)) {
3371 /* This is just what PIPE_CONTROL does */
3372 }
3373
3374 if (anv_clear_mask(&pipeEventMask,
3375 VK_PIPE_EVENT_VERTEX_PROCESSING_COMPLETE_BIT |
3376 VK_PIPE_EVENT_LOCAL_FRAGMENT_PROCESSING_COMPLETE_BIT |
3377 VK_PIPE_EVENT_FRAGMENT_PROCESSING_COMPLETE_BIT)) {
3378 cmd.StallAtPixelScoreboard = true;
3379 }
3380
3381
3382 if (anv_clear_mask(&pipeEventMask,
3383 VK_PIPE_EVENT_GRAPHICS_PIPELINE_COMPLETE_BIT |
3384 VK_PIPE_EVENT_COMPUTE_PIPELINE_COMPLETE_BIT |
3385 VK_PIPE_EVENT_TRANSFER_COMPLETE_BIT |
3386 VK_PIPE_EVENT_COMMANDS_COMPLETE_BIT)) {
3387 cmd.CommandStreamerStallEnable = true;
3388 }
3389
3390 if (anv_clear_mask(&pipeEventMask, VK_PIPE_EVENT_CPU_SIGNAL_BIT)) {
3391 anv_finishme("VK_PIPE_EVENT_CPU_SIGNAL_BIT");
3392 }
3393
3394 /* We checked all known VkPipeEventFlags. */
3395 anv_assert(pipeEventMask == 0);
3396
3397 /* XXX: Right now, we're really dumb and just flush whatever categories
3398 * the app asks for. One of these days we may make this a bit better
3399 * but right now that's all the hardware allows for in most areas.
3400 */
3401 VkMemoryOutputFlags out_flags = 0;
3402 VkMemoryInputFlags in_flags = 0;
3403
3404 for (uint32_t i = 0; i < memBarrierCount; i++) {
3405 const struct anv_common *common = ppMemBarriers[i];
3406 switch (common->sType) {
3407 case VK_STRUCTURE_TYPE_MEMORY_BARRIER: {
3408 const VkMemoryBarrier *barrier = (VkMemoryBarrier *)common;
3409 out_flags |= barrier->outputMask;
3410 in_flags |= barrier->inputMask;
3411 break;
3412 }
3413 case VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER: {
3414 const VkBufferMemoryBarrier *barrier = (VkBufferMemoryBarrier *)common;
3415 out_flags |= barrier->outputMask;
3416 in_flags |= barrier->inputMask;
3417 break;
3418 }
3419 case VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER: {
3420 const VkImageMemoryBarrier *barrier = (VkImageMemoryBarrier *)common;
3421 out_flags |= barrier->outputMask;
3422 in_flags |= barrier->inputMask;
3423 break;
3424 }
3425 default:
3426 unreachable("Invalid memory barrier type");
3427 }
3428 }
3429
3430 for_each_bit(b, out_flags) {
3431 switch ((VkMemoryOutputFlags)(1 << b)) {
3432 case VK_MEMORY_OUTPUT_HOST_WRITE_BIT:
3433 break; /* FIXME: Little-core systems */
3434 case VK_MEMORY_OUTPUT_SHADER_WRITE_BIT:
3435 cmd.DCFlushEnable = true;
3436 break;
3437 case VK_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT:
3438 cmd.RenderTargetCacheFlushEnable = true;
3439 break;
3440 case VK_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT:
3441 cmd.DepthCacheFlushEnable = true;
3442 break;
3443 case VK_MEMORY_OUTPUT_TRANSFER_BIT:
3444 cmd.RenderTargetCacheFlushEnable = true;
3445 cmd.DepthCacheFlushEnable = true;
3446 break;
3447 default:
3448 unreachable("Invalid memory output flag");
3449 }
3450 }
3451
3452 for_each_bit(b, out_flags) {
3453 switch ((VkMemoryInputFlags)(1 << b)) {
3454 case VK_MEMORY_INPUT_HOST_READ_BIT:
3455 break; /* FIXME: Little-core systems */
3456 case VK_MEMORY_INPUT_INDIRECT_COMMAND_BIT:
3457 case VK_MEMORY_INPUT_INDEX_FETCH_BIT:
3458 case VK_MEMORY_INPUT_VERTEX_ATTRIBUTE_FETCH_BIT:
3459 cmd.VFCacheInvalidationEnable = true;
3460 break;
3461 case VK_MEMORY_INPUT_UNIFORM_READ_BIT:
3462 cmd.ConstantCacheInvalidationEnable = true;
3463 /* fallthrough */
3464 case VK_MEMORY_INPUT_SHADER_READ_BIT:
3465 cmd.DCFlushEnable = true;
3466 cmd.TextureCacheInvalidationEnable = true;
3467 break;
3468 case VK_MEMORY_INPUT_COLOR_ATTACHMENT_BIT:
3469 case VK_MEMORY_INPUT_DEPTH_STENCIL_ATTACHMENT_BIT:
3470 break; /* XXX: Hunh? */
3471 case VK_MEMORY_INPUT_TRANSFER_BIT:
3472 cmd.TextureCacheInvalidationEnable = true;
3473 break;
3474 }
3475 }
3476
3477 dw = anv_batch_emit_dwords(&cmd_buffer->batch, GEN8_PIPE_CONTROL_length);
3478 GEN8_PIPE_CONTROL_pack(&cmd_buffer->batch, dw, &cmd);
3479 }
3480
3481 static void
3482 anv_framebuffer_destroy(struct anv_device *device,
3483 struct anv_object *object,
3484 VkObjectType obj_type)
3485 {
3486 struct anv_framebuffer *fb = (struct anv_framebuffer *)object;
3487
3488 assert(obj_type == VK_OBJECT_TYPE_FRAMEBUFFER);
3489
3490 anv_DestroyObject((VkDevice) device,
3491 VK_OBJECT_TYPE_DYNAMIC_VP_STATE,
3492 fb->vp_state);
3493
3494 anv_device_free(device, fb);
3495 }
3496
3497 VkResult anv_CreateFramebuffer(
3498 VkDevice _device,
3499 const VkFramebufferCreateInfo* pCreateInfo,
3500 VkFramebuffer* pFramebuffer)
3501 {
3502 struct anv_device *device = (struct anv_device *) _device;
3503 struct anv_framebuffer *framebuffer;
3504
3505 static const struct anv_depth_stencil_view null_view =
3506 { .depth_format = D16_UNORM, .depth_stride = 0, .stencil_stride = 0 };
3507
3508 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO);
3509
3510 framebuffer = anv_device_alloc(device, sizeof(*framebuffer), 8,
3511 VK_SYSTEM_ALLOC_TYPE_API_OBJECT);
3512 if (framebuffer == NULL)
3513 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
3514
3515 framebuffer->base.destructor = anv_framebuffer_destroy;
3516
3517 framebuffer->color_attachment_count = pCreateInfo->colorAttachmentCount;
3518 for (uint32_t i = 0; i < pCreateInfo->colorAttachmentCount; i++) {
3519 framebuffer->color_attachments[i] =
3520 (struct anv_surface_view *) pCreateInfo->pColorAttachments[i].view;
3521 }
3522
3523 if (pCreateInfo->pDepthStencilAttachment) {
3524 framebuffer->depth_stencil =
3525 (struct anv_depth_stencil_view *) pCreateInfo->pDepthStencilAttachment->view;
3526 } else {
3527 framebuffer->depth_stencil = &null_view;
3528 }
3529
3530 framebuffer->sample_count = pCreateInfo->sampleCount;
3531 framebuffer->width = pCreateInfo->width;
3532 framebuffer->height = pCreateInfo->height;
3533 framebuffer->layers = pCreateInfo->layers;
3534
3535 anv_CreateDynamicViewportState((VkDevice) device,
3536 &(VkDynamicVpStateCreateInfo) {
3537 .sType = VK_STRUCTURE_TYPE_DYNAMIC_VP_STATE_CREATE_INFO,
3538 .viewportAndScissorCount = 1,
3539 .pViewports = (VkViewport[]) {
3540 {
3541 .originX = 0,
3542 .originY = 0,
3543 .width = pCreateInfo->width,
3544 .height = pCreateInfo->height,
3545 .minDepth = 0,
3546 .maxDepth = 1
3547 },
3548 },
3549 .pScissors = (VkRect2D[]) {
3550 { { 0, 0 },
3551 { pCreateInfo->width, pCreateInfo->height } },
3552 }
3553 },
3554 &framebuffer->vp_state);
3555
3556 *pFramebuffer = (VkFramebuffer) framebuffer;
3557
3558 return VK_SUCCESS;
3559 }
3560
3561 VkResult anv_CreateRenderPass(
3562 VkDevice _device,
3563 const VkRenderPassCreateInfo* pCreateInfo,
3564 VkRenderPass* pRenderPass)
3565 {
3566 struct anv_device *device = (struct anv_device *) _device;
3567 struct anv_render_pass *pass;
3568 size_t size;
3569
3570 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO);
3571
3572 size = sizeof(*pass) +
3573 pCreateInfo->layers * sizeof(struct anv_render_pass_layer);
3574 pass = anv_device_alloc(device, size, 8,
3575 VK_SYSTEM_ALLOC_TYPE_API_OBJECT);
3576 if (pass == NULL)
3577 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
3578
3579 pass->render_area = pCreateInfo->renderArea;
3580
3581 pass->num_layers = pCreateInfo->layers;
3582
3583 pass->num_clear_layers = 0;
3584 for (uint32_t i = 0; i < pCreateInfo->layers; i++) {
3585 pass->layers[i].color_load_op = pCreateInfo->pColorLoadOps[i];
3586 pass->layers[i].clear_color = pCreateInfo->pColorLoadClearValues[i];
3587 if (pass->layers[i].color_load_op == VK_ATTACHMENT_LOAD_OP_CLEAR)
3588 pass->num_clear_layers++;
3589 }
3590
3591 *pRenderPass = (VkRenderPass) pass;
3592
3593 return VK_SUCCESS;
3594 }
3595
3596 VkResult anv_GetRenderAreaGranularity(
3597 VkDevice device,
3598 VkRenderPass renderPass,
3599 VkExtent2D* pGranularity)
3600 {
3601 *pGranularity = (VkExtent2D) { 1, 1 };
3602
3603 return VK_SUCCESS;
3604 }
3605
3606 static void
3607 anv_cmd_buffer_emit_depth_stencil(struct anv_cmd_buffer *cmd_buffer,
3608 struct anv_render_pass *pass)
3609 {
3610 const struct anv_depth_stencil_view *view =
3611 cmd_buffer->framebuffer->depth_stencil;
3612
3613 /* FIXME: Implement the PMA stall W/A */
3614 /* FIXME: Width and Height are wrong */
3615
3616 anv_batch_emit(&cmd_buffer->batch, GEN8_3DSTATE_DEPTH_BUFFER,
3617 .SurfaceType = SURFTYPE_2D,
3618 .DepthWriteEnable = view->depth_stride > 0,
3619 .StencilWriteEnable = view->stencil_stride > 0,
3620 .HierarchicalDepthBufferEnable = false,
3621 .SurfaceFormat = view->depth_format,
3622 .SurfacePitch = view->depth_stride > 0 ? view->depth_stride - 1 : 0,
3623 .SurfaceBaseAddress = { view->bo, view->depth_offset },
3624 .Height = pass->render_area.extent.height - 1,
3625 .Width = pass->render_area.extent.width - 1,
3626 .LOD = 0,
3627 .Depth = 1 - 1,
3628 .MinimumArrayElement = 0,
3629 .DepthBufferObjectControlState = GEN8_MOCS,
3630 .RenderTargetViewExtent = 1 - 1,
3631 .SurfaceQPitch = view->depth_qpitch >> 2);
3632
3633 /* Disable hierarchial depth buffers. */
3634 anv_batch_emit(&cmd_buffer->batch, GEN8_3DSTATE_HIER_DEPTH_BUFFER);
3635
3636 anv_batch_emit(&cmd_buffer->batch, GEN8_3DSTATE_STENCIL_BUFFER,
3637 .StencilBufferEnable = view->stencil_stride > 0,
3638 .StencilBufferObjectControlState = GEN8_MOCS,
3639 .SurfacePitch = view->stencil_stride > 0 ? view->stencil_stride - 1 : 0,
3640 .SurfaceBaseAddress = { view->bo, view->stencil_offset },
3641 .SurfaceQPitch = view->stencil_qpitch >> 2);
3642
3643 /* Clear the clear params. */
3644 anv_batch_emit(&cmd_buffer->batch, GEN8_3DSTATE_CLEAR_PARAMS);
3645 }
3646
3647 void anv_CmdPushConstants(
3648 VkCmdBuffer cmdBuffer,
3649 VkPipelineLayout layout,
3650 VkShaderStageFlags stageFlags,
3651 uint32_t start,
3652 uint32_t length,
3653 const void* values)
3654 {
3655 stub();
3656 }
3657
3658 void anv_CmdBeginRenderPass(
3659 VkCmdBuffer cmdBuffer,
3660 const VkRenderPassBegin* pRenderPassBegin)
3661 {
3662 struct anv_cmd_buffer *cmd_buffer = (struct anv_cmd_buffer *) cmdBuffer;
3663 struct anv_render_pass *pass = (struct anv_render_pass *) pRenderPassBegin->renderPass;
3664 struct anv_framebuffer *framebuffer =
3665 (struct anv_framebuffer *) pRenderPassBegin->framebuffer;
3666
3667 cmd_buffer->framebuffer = framebuffer;
3668
3669 cmd_buffer->descriptors_dirty |= VK_SHADER_STAGE_FRAGMENT_BIT;
3670
3671 anv_batch_emit(&cmd_buffer->batch, GEN8_3DSTATE_DRAWING_RECTANGLE,
3672 .ClippedDrawingRectangleYMin = pass->render_area.offset.y,
3673 .ClippedDrawingRectangleXMin = pass->render_area.offset.x,
3674 .ClippedDrawingRectangleYMax =
3675 pass->render_area.offset.y + pass->render_area.extent.height - 1,
3676 .ClippedDrawingRectangleXMax =
3677 pass->render_area.offset.x + pass->render_area.extent.width - 1,
3678 .DrawingRectangleOriginY = 0,
3679 .DrawingRectangleOriginX = 0);
3680
3681 anv_cmd_buffer_emit_depth_stencil(cmd_buffer, pass);
3682
3683 anv_cmd_buffer_clear(cmd_buffer, pass);
3684 }
3685
3686 void anv_CmdEndRenderPass(
3687 VkCmdBuffer cmdBuffer)
3688 {
3689 /* Emit a flushing pipe control at the end of a pass. This is kind of a
3690 * hack but it ensures that render targets always actually get written.
3691 * Eventually, we should do flushing based on image format transitions
3692 * or something of that nature.
3693 */
3694 struct anv_cmd_buffer *cmd_buffer = (struct anv_cmd_buffer *)cmdBuffer;
3695 anv_batch_emit(&cmd_buffer->batch, GEN8_PIPE_CONTROL,
3696 .PostSyncOperation = NoWrite,
3697 .RenderTargetCacheFlushEnable = true,
3698 .InstructionCacheInvalidateEnable = true,
3699 .DepthCacheFlushEnable = true,
3700 .VFCacheInvalidationEnable = true,
3701 .TextureCacheInvalidationEnable = true,
3702 .CommandStreamerStallEnable = true);
3703 }
3704
3705 void anv_CmdExecuteCommands(
3706 VkCmdBuffer cmdBuffer,
3707 uint32_t cmdBuffersCount,
3708 const VkCmdBuffer* pCmdBuffers)
3709 {
3710 stub();
3711 }
3712
3713 void vkCmdDbgMarkerBegin(
3714 VkCmdBuffer cmdBuffer,
3715 const char* pMarker)
3716 __attribute__ ((visibility ("default")));
3717
3718 void vkCmdDbgMarkerEnd(
3719 VkCmdBuffer cmdBuffer)
3720 __attribute__ ((visibility ("default")));
3721
3722 VkResult vkDbgSetObjectTag(
3723 VkDevice device,
3724 VkObject object,
3725 size_t tagSize,
3726 const void* pTag)
3727 __attribute__ ((visibility ("default")));
3728
3729
3730 void vkCmdDbgMarkerBegin(
3731 VkCmdBuffer cmdBuffer,
3732 const char* pMarker)
3733 {
3734 }
3735
3736 void vkCmdDbgMarkerEnd(
3737 VkCmdBuffer cmdBuffer)
3738 {
3739 }
3740
3741 VkResult vkDbgSetObjectTag(
3742 VkDevice device,
3743 VkObject object,
3744 size_t tagSize,
3745 const void* pTag)
3746 {
3747 return VK_SUCCESS;
3748 }