vk: Implement fences
[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 if (result == VK_SUCCESS)
146 instance->physicalDeviceCount++;
147
148 *pInstance = (VkInstance) instance;
149
150 return VK_SUCCESS;
151 }
152
153 VkResult anv_DestroyInstance(
154 VkInstance _instance)
155 {
156 struct anv_instance *instance = (struct anv_instance *) _instance;
157
158 instance->pfnFree(instance->pAllocUserData, instance);
159
160 return VK_SUCCESS;
161 }
162
163 VkResult anv_EnumeratePhysicalDevices(
164 VkInstance _instance,
165 uint32_t* pPhysicalDeviceCount,
166 VkPhysicalDevice* pPhysicalDevices)
167 {
168 struct anv_instance *instance = (struct anv_instance *) _instance;
169
170 if (*pPhysicalDeviceCount >= 1)
171 pPhysicalDevices[0] = (VkPhysicalDevice) &instance->physicalDevice;
172 *pPhysicalDeviceCount = instance->physicalDeviceCount;
173
174 return VK_SUCCESS;
175 }
176
177 VkResult anv_GetPhysicalDeviceInfo(
178 VkPhysicalDevice physicalDevice,
179 VkPhysicalDeviceInfoType infoType,
180 size_t* pDataSize,
181 void* pData)
182 {
183 struct anv_physical_device *device = (struct anv_physical_device *) physicalDevice;
184 VkPhysicalDeviceProperties *properties;
185 VkPhysicalDevicePerformance *performance;
186 VkPhysicalDeviceQueueProperties *queue_properties;
187 VkPhysicalDeviceMemoryProperties *memory_properties;
188 uint64_t ns_per_tick = 80;
189
190 switch (infoType) {
191 case VK_PHYSICAL_DEVICE_INFO_TYPE_PROPERTIES:
192 properties = pData;
193
194 *pDataSize = sizeof(*properties);
195 if (pData == NULL)
196 return VK_SUCCESS;
197
198 properties->apiVersion = 1;
199 properties->driverVersion = 1;
200 properties->vendorId = 0x8086;
201 properties->deviceId = device->chipset_id;
202 properties->deviceType = VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU;
203 strcpy(properties->deviceName, device->name);
204 properties->maxInlineMemoryUpdateSize = 0;
205 properties->maxBoundDescriptorSets = 0;
206 properties->maxThreadGroupSize = 0;
207 properties->timestampFrequency = 1000 * 1000 * 1000 / ns_per_tick;
208 properties->multiColorAttachmentClears = 0;
209 properties->maxDescriptorSets = 2;
210 properties->maxViewports = 16;
211 properties->maxColorAttachments = 8;
212 return VK_SUCCESS;
213
214 case VK_PHYSICAL_DEVICE_INFO_TYPE_PERFORMANCE:
215 performance = pData;
216
217 *pDataSize = sizeof(*performance);
218 if (pData == NULL)
219 return VK_SUCCESS;
220
221 performance->maxDeviceClock = 1.0;
222 performance->aluPerClock = 1.0;
223 performance->texPerClock = 1.0;
224 performance->primsPerClock = 1.0;
225 performance->pixelsPerClock = 1.0;
226 return VK_SUCCESS;
227
228 case VK_PHYSICAL_DEVICE_INFO_TYPE_QUEUE_PROPERTIES:
229 queue_properties = pData;
230
231 *pDataSize = sizeof(*queue_properties);
232 if (pData == NULL)
233 return VK_SUCCESS;
234
235 queue_properties->queueFlags = 0;
236 queue_properties->queueCount = 1;
237 queue_properties->maxAtomicCounters = 0;
238 queue_properties->supportsTimestamps = 0;
239 queue_properties->maxMemReferences = 0;
240 return VK_SUCCESS;
241
242 case VK_PHYSICAL_DEVICE_INFO_TYPE_MEMORY_PROPERTIES:
243 memory_properties = pData;
244
245 *pDataSize = sizeof(*memory_properties);
246 if (pData == NULL)
247 return VK_SUCCESS;
248
249 memory_properties->supportsMigration = false;
250 memory_properties->supportsPinning = false;
251 return VK_SUCCESS;
252
253 default:
254 return VK_UNSUPPORTED;
255 }
256
257 }
258
259 void * vkGetProcAddr(
260 VkPhysicalDevice physicalDevice,
261 const char* pName)
262 {
263 return anv_lookup_entrypoint(pName);
264 }
265
266 static void
267 parse_debug_flags(struct anv_device *device)
268 {
269 const char *debug, *p, *end;
270
271 debug = getenv("INTEL_DEBUG");
272 device->dump_aub = false;
273 if (debug) {
274 for (p = debug; *p; p = end + 1) {
275 end = strchrnul(p, ',');
276 if (end - p == 3 && memcmp(p, "aub", 3) == 0)
277 device->dump_aub = true;
278 if (end - p == 5 && memcmp(p, "no_hw", 5) == 0)
279 device->no_hw = true;
280 if (*end == '\0')
281 break;
282 }
283 }
284 }
285
286 VkResult anv_CreateDevice(
287 VkPhysicalDevice _physicalDevice,
288 const VkDeviceCreateInfo* pCreateInfo,
289 VkDevice* pDevice)
290 {
291 struct anv_physical_device *physicalDevice =
292 (struct anv_physical_device *) _physicalDevice;
293 struct anv_instance *instance = physicalDevice->instance;
294 struct anv_device *device;
295
296 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO);
297
298 device = instance->pfnAlloc(instance->pAllocUserData,
299 sizeof(*device), 8,
300 VK_SYSTEM_ALLOC_TYPE_API_OBJECT);
301 if (!device)
302 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
303
304 device->no_hw = physicalDevice->no_hw;
305 parse_debug_flags(device);
306
307 device->instance = physicalDevice->instance;
308 device->fd = open("/dev/dri/renderD128", O_RDWR | O_CLOEXEC);
309 if (device->fd == -1)
310 goto fail_device;
311
312 device->context_id = anv_gem_create_context(device);
313 if (device->context_id == -1)
314 goto fail_fd;
315
316 anv_block_pool_init(&device->dynamic_state_block_pool, device, 2048);
317
318 anv_state_pool_init(&device->dynamic_state_pool,
319 &device->dynamic_state_block_pool);
320
321 anv_block_pool_init(&device->instruction_block_pool, device, 2048);
322 anv_block_pool_init(&device->surface_state_block_pool, device, 2048);
323
324 anv_state_pool_init(&device->surface_state_pool,
325 &device->surface_state_block_pool);
326
327 device->compiler = anv_compiler_create(device->fd);
328 device->aub_writer = NULL;
329
330 device->info = *physicalDevice->info;
331
332 pthread_mutex_init(&device->mutex, NULL);
333
334 anv_device_init_meta(device);
335
336 *pDevice = (VkDevice) device;
337
338 return VK_SUCCESS;
339
340 fail_fd:
341 close(device->fd);
342 fail_device:
343 anv_device_free(device, device);
344
345 return vk_error(VK_ERROR_UNAVAILABLE);
346 }
347
348 VkResult anv_DestroyDevice(
349 VkDevice _device)
350 {
351 struct anv_device *device = (struct anv_device *) _device;
352
353 anv_compiler_destroy(device->compiler);
354
355 anv_block_pool_finish(&device->dynamic_state_block_pool);
356 anv_block_pool_finish(&device->instruction_block_pool);
357 anv_block_pool_finish(&device->surface_state_block_pool);
358
359 close(device->fd);
360
361 if (device->aub_writer)
362 anv_aub_writer_destroy(device->aub_writer);
363
364 anv_device_free(device, device);
365
366 return VK_SUCCESS;
367 }
368
369 VkResult anv_GetGlobalExtensionInfo(
370 VkExtensionInfoType infoType,
371 uint32_t extensionIndex,
372 size_t* pDataSize,
373 void* pData)
374 {
375 uint32_t *count;
376
377 switch (infoType) {
378 case VK_EXTENSION_INFO_TYPE_COUNT:
379 count = pData;
380 assert(*pDataSize == 4);
381 *count = 0;
382 return VK_SUCCESS;
383
384 case VK_EXTENSION_INFO_TYPE_PROPERTIES:
385 return vk_error(VK_ERROR_INVALID_EXTENSION);
386
387 default:
388 return VK_UNSUPPORTED;
389 }
390 }
391
392 VkResult anv_GetPhysicalDeviceExtensionInfo(
393 VkPhysicalDevice physicalDevice,
394 VkExtensionInfoType infoType,
395 uint32_t extensionIndex,
396 size_t* pDataSize,
397 void* pData)
398 {
399 uint32_t *count;
400
401 switch (infoType) {
402 case VK_EXTENSION_INFO_TYPE_COUNT:
403 *pDataSize = 4;
404 if (pData == NULL)
405 return VK_SUCCESS;
406
407 count = pData;
408 *count = 0;
409 return VK_SUCCESS;
410
411 case VK_EXTENSION_INFO_TYPE_PROPERTIES:
412 return vk_error(VK_ERROR_INVALID_EXTENSION);
413
414 default:
415 return VK_UNSUPPORTED;
416 }
417 }
418
419 VkResult anv_EnumerateLayers(
420 VkPhysicalDevice physicalDevice,
421 size_t maxStringSize,
422 size_t* pLayerCount,
423 char* const* pOutLayers,
424 void* pReserved)
425 {
426 *pLayerCount = 0;
427
428 return VK_SUCCESS;
429 }
430
431 VkResult anv_GetDeviceQueue(
432 VkDevice _device,
433 uint32_t queueNodeIndex,
434 uint32_t queueIndex,
435 VkQueue* pQueue)
436 {
437 struct anv_device *device = (struct anv_device *) _device;
438 struct anv_queue *queue;
439
440 /* FIXME: Should allocate these at device create time. */
441
442 queue = anv_device_alloc(device, sizeof(*queue), 8,
443 VK_SYSTEM_ALLOC_TYPE_API_OBJECT);
444 if (queue == NULL)
445 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
446
447 queue->device = device;
448 queue->pool = &device->surface_state_pool;
449
450 queue->completed_serial = anv_state_pool_alloc(queue->pool, 4, 4);
451 *(uint32_t *)queue->completed_serial.map = 0;
452 queue->next_serial = 1;
453
454 *pQueue = (VkQueue) queue;
455
456 return VK_SUCCESS;
457 }
458
459 static const uint32_t BATCH_SIZE = 8192;
460
461 VkResult
462 anv_batch_init(struct anv_batch *batch, struct anv_device *device)
463 {
464 VkResult result;
465
466 result = anv_bo_init_new(&batch->bo, device, BATCH_SIZE);
467 if (result != VK_SUCCESS)
468 return result;
469
470 batch->bo.map =
471 anv_gem_mmap(device, batch->bo.gem_handle, 0, BATCH_SIZE);
472 if (batch->bo.map == NULL) {
473 anv_gem_close(device, batch->bo.gem_handle);
474 return vk_error(VK_ERROR_MEMORY_MAP_FAILED);
475 }
476
477 batch->cmd_relocs.num_relocs = 0;
478 batch->surf_relocs.num_relocs = 0;
479 batch->next = batch->bo.map;
480
481 return VK_SUCCESS;
482 }
483
484 void
485 anv_batch_finish(struct anv_batch *batch, struct anv_device *device)
486 {
487 anv_gem_munmap(batch->bo.map, BATCH_SIZE);
488 anv_gem_close(device, batch->bo.gem_handle);
489 }
490
491 void
492 anv_batch_reset(struct anv_batch *batch)
493 {
494 batch->next = batch->bo.map;
495 batch->cmd_relocs.num_relocs = 0;
496 batch->surf_relocs.num_relocs = 0;
497 }
498
499 void *
500 anv_batch_emit_dwords(struct anv_batch *batch, int num_dwords)
501 {
502 void *p = batch->next;
503
504 batch->next += num_dwords * 4;
505
506 return p;
507 }
508
509 static void
510 anv_reloc_list_append(struct anv_reloc_list *list,
511 struct anv_reloc_list *other, uint32_t offset)
512 {
513 uint32_t i, count;
514
515 count = list->num_relocs;
516 memcpy(&list->relocs[count], &other->relocs[0],
517 other->num_relocs * sizeof(other->relocs[0]));
518 memcpy(&list->reloc_bos[count], &other->reloc_bos[0],
519 other->num_relocs * sizeof(other->reloc_bos[0]));
520 for (i = 0; i < other->num_relocs; i++)
521 list->relocs[i + count].offset += offset;
522
523 count += other->num_relocs;
524 }
525
526 static uint64_t
527 anv_reloc_list_add(struct anv_reloc_list *list,
528 uint32_t offset,
529 struct anv_bo *target_bo, uint32_t delta)
530 {
531 struct drm_i915_gem_relocation_entry *entry;
532 int index;
533
534 assert(list->num_relocs < ANV_BATCH_MAX_RELOCS);
535
536 /* XXX: Can we use I915_EXEC_HANDLE_LUT? */
537 index = list->num_relocs++;
538 list->reloc_bos[index] = target_bo;
539 entry = &list->relocs[index];
540 entry->target_handle = target_bo->gem_handle;
541 entry->delta = delta;
542 entry->offset = offset;
543 entry->presumed_offset = target_bo->offset;
544 entry->read_domains = 0;
545 entry->write_domain = 0;
546
547 return target_bo->offset + delta;
548 }
549
550 void
551 anv_batch_emit_batch(struct anv_batch *batch, struct anv_batch *other)
552 {
553 uint32_t size, offset;
554
555 size = other->next - other->bo.map;
556 memcpy(batch->next, other->bo.map, size);
557
558 offset = batch->next - batch->bo.map;
559 anv_reloc_list_append(&batch->cmd_relocs, &other->cmd_relocs, offset);
560 anv_reloc_list_append(&batch->surf_relocs, &other->surf_relocs, offset);
561
562 batch->next += size;
563 }
564
565 uint64_t
566 anv_batch_emit_reloc(struct anv_batch *batch,
567 void *location, struct anv_bo *bo, uint32_t delta)
568 {
569 return anv_reloc_list_add(&batch->cmd_relocs,
570 location - batch->bo.map, bo, delta);
571 }
572
573 VkResult anv_QueueSubmit(
574 VkQueue _queue,
575 uint32_t cmdBufferCount,
576 const VkCmdBuffer* pCmdBuffers,
577 VkFence _fence)
578 {
579 struct anv_queue *queue = (struct anv_queue *) _queue;
580 struct anv_device *device = queue->device;
581 struct anv_fence *fence = (struct anv_fence *) _fence;
582 int ret;
583
584 for (uint32_t i = 0; i < cmdBufferCount; i++) {
585 struct anv_cmd_buffer *cmd_buffer =
586 (struct anv_cmd_buffer *) pCmdBuffers[i];
587
588 if (device->dump_aub)
589 anv_cmd_buffer_dump(cmd_buffer);
590
591 if (!device->no_hw) {
592 ret = anv_gem_execbuffer(device, &cmd_buffer->execbuf);
593 if (ret != 0)
594 return vk_error(VK_ERROR_UNKNOWN);
595
596 if (fence) {
597 ret = anv_gem_execbuffer(device, &fence->execbuf);
598 if (ret != 0)
599 return vk_error(VK_ERROR_UNKNOWN);
600 }
601
602 for (uint32_t i = 0; i < cmd_buffer->bo_count; i++)
603 cmd_buffer->exec2_bos[i]->offset = cmd_buffer->exec2_objects[i].offset;
604 } else {
605 *(uint32_t *)queue->completed_serial.map = cmd_buffer->serial;
606 }
607 }
608
609 return VK_SUCCESS;
610 }
611
612 VkResult anv_QueueAddMemReferences(
613 VkQueue queue,
614 uint32_t count,
615 const VkDeviceMemory* pMems)
616 {
617 return VK_SUCCESS;
618 }
619
620 VkResult anv_QueueRemoveMemReferences(
621 VkQueue queue,
622 uint32_t count,
623 const VkDeviceMemory* pMems)
624 {
625 return VK_SUCCESS;
626 }
627
628 VkResult anv_QueueWaitIdle(
629 VkQueue _queue)
630 {
631 struct anv_queue *queue = (struct anv_queue *) _queue;
632
633 return vkDeviceWaitIdle((VkDevice) queue->device);
634 }
635
636 VkResult anv_DeviceWaitIdle(
637 VkDevice _device)
638 {
639 struct anv_device *device = (struct anv_device *) _device;
640 struct anv_state state;
641 struct anv_batch batch;
642 struct drm_i915_gem_execbuffer2 execbuf;
643 struct drm_i915_gem_exec_object2 exec2_objects[1];
644 struct anv_bo *bo = NULL;
645 VkResult result;
646 int64_t timeout;
647 int ret;
648
649 state = anv_state_pool_alloc(&device->dynamic_state_pool, 32, 32);
650 bo = &device->dynamic_state_pool.block_pool->bo;
651 batch.next = state.map;
652 anv_batch_emit(&batch, GEN8_MI_BATCH_BUFFER_END);
653 anv_batch_emit(&batch, GEN8_MI_NOOP);
654
655 exec2_objects[0].handle = bo->gem_handle;
656 exec2_objects[0].relocation_count = 0;
657 exec2_objects[0].relocs_ptr = 0;
658 exec2_objects[0].alignment = 0;
659 exec2_objects[0].offset = bo->offset;
660 exec2_objects[0].flags = 0;
661 exec2_objects[0].rsvd1 = 0;
662 exec2_objects[0].rsvd2 = 0;
663
664 execbuf.buffers_ptr = (uintptr_t) exec2_objects;
665 execbuf.buffer_count = 1;
666 execbuf.batch_start_offset = state.offset;
667 execbuf.batch_len = batch.next - state.map;
668 execbuf.cliprects_ptr = 0;
669 execbuf.num_cliprects = 0;
670 execbuf.DR1 = 0;
671 execbuf.DR4 = 0;
672
673 execbuf.flags =
674 I915_EXEC_HANDLE_LUT | I915_EXEC_NO_RELOC | I915_EXEC_RENDER;
675 execbuf.rsvd1 = device->context_id;
676 execbuf.rsvd2 = 0;
677
678 if (!device->no_hw) {
679 ret = anv_gem_execbuffer(device, &execbuf);
680 if (ret != 0) {
681 result = vk_error(VK_ERROR_UNKNOWN);
682 goto fail;
683 }
684
685 timeout = INT64_MAX;
686 ret = anv_gem_wait(device, bo->gem_handle, &timeout);
687 if (ret != 0) {
688 result = vk_error(VK_ERROR_UNKNOWN);
689 goto fail;
690 }
691 }
692
693 anv_state_pool_free(&device->dynamic_state_pool, state);
694
695 return VK_SUCCESS;
696
697 fail:
698 anv_state_pool_free(&device->dynamic_state_pool, state);
699
700 return result;
701 }
702
703 void *
704 anv_device_alloc(struct anv_device * device,
705 size_t size,
706 size_t alignment,
707 VkSystemAllocType allocType)
708 {
709 return device->instance->pfnAlloc(device->instance->pAllocUserData,
710 size,
711 alignment,
712 allocType);
713 }
714
715 void
716 anv_device_free(struct anv_device * device,
717 void * mem)
718 {
719 return device->instance->pfnFree(device->instance->pAllocUserData,
720 mem);
721 }
722
723 VkResult
724 anv_bo_init_new(struct anv_bo *bo, struct anv_device *device, uint64_t size)
725 {
726 bo->gem_handle = anv_gem_create(device, size);
727 if (!bo->gem_handle)
728 return vk_error(VK_ERROR_OUT_OF_DEVICE_MEMORY);
729
730 bo->map = NULL;
731 bo->index = 0;
732 bo->offset = 0;
733 bo->size = size;
734
735 return VK_SUCCESS;
736 }
737
738 VkResult anv_AllocMemory(
739 VkDevice _device,
740 const VkMemoryAllocInfo* pAllocInfo,
741 VkDeviceMemory* pMem)
742 {
743 struct anv_device *device = (struct anv_device *) _device;
744 struct anv_device_memory *mem;
745 VkResult result;
746
747 assert(pAllocInfo->sType == VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO);
748
749 mem = anv_device_alloc(device, sizeof(*mem), 8,
750 VK_SYSTEM_ALLOC_TYPE_API_OBJECT);
751 if (mem == NULL)
752 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
753
754 result = anv_bo_init_new(&mem->bo, device, pAllocInfo->allocationSize);
755 if (result != VK_SUCCESS)
756 goto fail;
757
758 *pMem = (VkDeviceMemory) mem;
759
760 return VK_SUCCESS;
761
762 fail:
763 anv_device_free(device, mem);
764
765 return result;
766 }
767
768 VkResult anv_FreeMemory(
769 VkDevice _device,
770 VkDeviceMemory _mem)
771 {
772 struct anv_device *device = (struct anv_device *) _device;
773 struct anv_device_memory *mem = (struct anv_device_memory *) _mem;
774
775 if (mem->bo.map)
776 anv_gem_munmap(mem->bo.map, mem->bo.size);
777
778 if (mem->bo.gem_handle != 0)
779 anv_gem_close(device, mem->bo.gem_handle);
780
781 anv_device_free(device, mem);
782
783 return VK_SUCCESS;
784 }
785
786 VkResult anv_SetMemoryPriority(
787 VkDevice device,
788 VkDeviceMemory mem,
789 VkMemoryPriority priority)
790 {
791 return VK_SUCCESS;
792 }
793
794 VkResult anv_MapMemory(
795 VkDevice _device,
796 VkDeviceMemory _mem,
797 VkDeviceSize offset,
798 VkDeviceSize size,
799 VkMemoryMapFlags flags,
800 void** ppData)
801 {
802 struct anv_device *device = (struct anv_device *) _device;
803 struct anv_device_memory *mem = (struct anv_device_memory *) _mem;
804
805 /* FIXME: Is this supposed to be thread safe? Since vkUnmapMemory() only
806 * takes a VkDeviceMemory pointer, it seems like only one map of the memory
807 * at a time is valid. We could just mmap up front and return an offset
808 * pointer here, but that may exhaust virtual memory on 32 bit
809 * userspace. */
810
811 mem->map = anv_gem_mmap(device, mem->bo.gem_handle, offset, size);
812 mem->map_size = size;
813
814 *ppData = mem->map;
815
816 return VK_SUCCESS;
817 }
818
819 VkResult anv_UnmapMemory(
820 VkDevice _device,
821 VkDeviceMemory _mem)
822 {
823 struct anv_device_memory *mem = (struct anv_device_memory *) _mem;
824
825 anv_gem_munmap(mem->map, mem->map_size);
826
827 return VK_SUCCESS;
828 }
829
830 VkResult anv_FlushMappedMemory(
831 VkDevice device,
832 VkDeviceMemory mem,
833 VkDeviceSize offset,
834 VkDeviceSize size)
835 {
836 /* clflush here for !llc platforms */
837
838 return VK_SUCCESS;
839 }
840
841 VkResult anv_PinSystemMemory(
842 VkDevice device,
843 const void* pSysMem,
844 size_t memSize,
845 VkDeviceMemory* pMem)
846 {
847 return VK_SUCCESS;
848 }
849
850 VkResult anv_GetMultiDeviceCompatibility(
851 VkPhysicalDevice physicalDevice0,
852 VkPhysicalDevice physicalDevice1,
853 VkPhysicalDeviceCompatibilityInfo* pInfo)
854 {
855 return VK_UNSUPPORTED;
856 }
857
858 VkResult anv_OpenSharedMemory(
859 VkDevice device,
860 const VkMemoryOpenInfo* pOpenInfo,
861 VkDeviceMemory* pMem)
862 {
863 return VK_UNSUPPORTED;
864 }
865
866 VkResult anv_OpenSharedSemaphore(
867 VkDevice device,
868 const VkSemaphoreOpenInfo* pOpenInfo,
869 VkSemaphore* pSemaphore)
870 {
871 return VK_UNSUPPORTED;
872 }
873
874 VkResult anv_OpenPeerMemory(
875 VkDevice device,
876 const VkPeerMemoryOpenInfo* pOpenInfo,
877 VkDeviceMemory* pMem)
878 {
879 return VK_UNSUPPORTED;
880 }
881
882 VkResult anv_OpenPeerImage(
883 VkDevice device,
884 const VkPeerImageOpenInfo* pOpenInfo,
885 VkImage* pImage,
886 VkDeviceMemory* pMem)
887 {
888 return VK_UNSUPPORTED;
889 }
890
891 static VkResult
892 anv_instance_destructor(struct anv_device * device,
893 VkObject object)
894 {
895 return vkDestroyInstance(object);
896 }
897
898 static VkResult
899 anv_noop_destructor(struct anv_device * device,
900 VkObject object)
901 {
902 return VK_SUCCESS;
903 }
904
905 static VkResult
906 anv_device_destructor(struct anv_device * device,
907 VkObject object)
908 {
909 return vkDestroyDevice(object);
910 }
911
912 static VkResult
913 anv_cmd_buffer_destructor(struct anv_device * device,
914 VkObject object)
915 {
916 struct anv_cmd_buffer *cmd_buffer = (struct anv_cmd_buffer *) object;
917
918 anv_state_stream_finish(&cmd_buffer->surface_state_stream);
919 anv_state_stream_finish(&cmd_buffer->dynamic_state_stream);
920 anv_batch_finish(&cmd_buffer->batch, device);
921 anv_device_free(device, cmd_buffer->exec2_objects);
922 anv_device_free(device, cmd_buffer->exec2_bos);
923 anv_device_free(device, cmd_buffer);
924
925 return VK_SUCCESS;
926 }
927
928 static VkResult
929 anv_pipeline_destructor(struct anv_device * device,
930 VkObject object)
931 {
932 struct anv_pipeline *pipeline = (struct anv_pipeline *) object;
933
934 return anv_pipeline_destroy(pipeline);
935 }
936
937 static VkResult
938 anv_free_destructor(struct anv_device * device,
939 VkObject object)
940 {
941 anv_device_free(device, (void *) object);
942
943 return VK_SUCCESS;
944 }
945
946 static VkResult
947 anv_fence_destructor(struct anv_device * device,
948 VkObject object)
949 {
950 struct anv_fence *fence = (struct anv_fence *) object;
951
952 anv_gem_munmap(fence->bo.map, fence->bo.size);
953 anv_gem_close(device, fence->bo.gem_handle);
954 anv_device_free(device, fence);
955
956 return VK_SUCCESS;
957 }
958
959 static VkResult (*anv_object_destructors[])(struct anv_device *device,
960 VkObject object) = {
961 [VK_OBJECT_TYPE_INSTANCE] = anv_instance_destructor,
962 [VK_OBJECT_TYPE_PHYSICAL_DEVICE] = anv_noop_destructor,
963 [VK_OBJECT_TYPE_DEVICE] = anv_device_destructor,
964 [VK_OBJECT_TYPE_QUEUE] = anv_noop_destructor,
965 [VK_OBJECT_TYPE_COMMAND_BUFFER] = anv_cmd_buffer_destructor,
966 [VK_OBJECT_TYPE_PIPELINE] = anv_pipeline_destructor,
967 [VK_OBJECT_TYPE_SHADER] = anv_free_destructor,
968 [VK_OBJECT_TYPE_BUFFER] = anv_free_destructor,
969 [VK_OBJECT_TYPE_IMAGE] = anv_free_destructor,
970 [VK_OBJECT_TYPE_RENDER_PASS] = anv_free_destructor,
971 [VK_OBJECT_TYPE_FENCE] = anv_fence_destructor
972 };
973
974 VkResult anv_DestroyObject(
975 VkDevice _device,
976 VkObjectType objType,
977 VkObject object)
978 {
979 struct anv_device *device = (struct anv_device *) _device;
980
981 assert(objType < ARRAY_SIZE(anv_object_destructors) &&
982 anv_object_destructors[objType] != NULL);
983
984 return anv_object_destructors[objType](device, object);
985 }
986
987 static void
988 fill_memory_requirements(
989 VkObjectType objType,
990 VkObject object,
991 VkMemoryRequirements * memory_requirements)
992 {
993 struct anv_buffer *buffer;
994 struct anv_image *image;
995
996 memory_requirements->memPropsAllowed =
997 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
998 VK_MEMORY_PROPERTY_HOST_DEVICE_COHERENT_BIT |
999 /* VK_MEMORY_PROPERTY_HOST_UNCACHED_BIT | */
1000 VK_MEMORY_PROPERTY_HOST_WRITE_COMBINED_BIT |
1001 VK_MEMORY_PROPERTY_PREFER_HOST_LOCAL |
1002 VK_MEMORY_PROPERTY_SHAREABLE_BIT;
1003
1004 memory_requirements->memPropsRequired = 0;
1005
1006 switch (objType) {
1007 case VK_OBJECT_TYPE_BUFFER:
1008 buffer = (struct anv_buffer *) object;
1009 memory_requirements->size = buffer->size;
1010 memory_requirements->alignment = 16;
1011 break;
1012 case VK_OBJECT_TYPE_IMAGE:
1013 image = (struct anv_image *) object;
1014 memory_requirements->size = image->size;
1015 memory_requirements->alignment = image->alignment;
1016 break;
1017 default:
1018 memory_requirements->size = 0;
1019 break;
1020 }
1021 }
1022
1023 static uint32_t
1024 get_allocation_count(VkObjectType objType)
1025 {
1026 switch (objType) {
1027 case VK_OBJECT_TYPE_BUFFER:
1028 case VK_OBJECT_TYPE_IMAGE:
1029 return 1;
1030 default:
1031 return 0;
1032 }
1033 }
1034
1035 VkResult anv_GetObjectInfo(
1036 VkDevice _device,
1037 VkObjectType objType,
1038 VkObject object,
1039 VkObjectInfoType infoType,
1040 size_t* pDataSize,
1041 void* pData)
1042 {
1043 VkMemoryRequirements memory_requirements;
1044 uint32_t count;
1045
1046 switch (infoType) {
1047 case VK_OBJECT_INFO_TYPE_MEMORY_REQUIREMENTS:
1048 *pDataSize = sizeof(memory_requirements);
1049 if (pData == NULL)
1050 return VK_SUCCESS;
1051
1052 fill_memory_requirements(objType, object, &memory_requirements);
1053 memcpy(pData, &memory_requirements,
1054 MIN2(*pDataSize, sizeof(memory_requirements)));
1055 return VK_SUCCESS;
1056
1057 case VK_OBJECT_INFO_TYPE_MEMORY_ALLOCATION_COUNT:
1058 *pDataSize = sizeof(count);
1059 if (pData == NULL)
1060 return VK_SUCCESS;
1061
1062 count = get_allocation_count(objType);
1063 return VK_SUCCESS;
1064
1065 default:
1066 return VK_UNSUPPORTED;
1067 }
1068
1069 }
1070
1071 VkResult anv_QueueBindObjectMemory(
1072 VkQueue queue,
1073 VkObjectType objType,
1074 VkObject object,
1075 uint32_t allocationIdx,
1076 VkDeviceMemory _mem,
1077 VkDeviceSize memOffset)
1078 {
1079 struct anv_buffer *buffer;
1080 struct anv_image *image;
1081 struct anv_device_memory *mem = (struct anv_device_memory *) _mem;
1082
1083 switch (objType) {
1084 case VK_OBJECT_TYPE_BUFFER:
1085 buffer = (struct anv_buffer *) object;
1086 buffer->bo = &mem->bo;
1087 buffer->offset = memOffset;
1088 break;
1089 case VK_OBJECT_TYPE_IMAGE:
1090 image = (struct anv_image *) object;
1091 image->bo = &mem->bo;
1092 image->offset = memOffset;
1093 break;
1094 default:
1095 break;
1096 }
1097
1098 return VK_SUCCESS;
1099 }
1100
1101 VkResult anv_QueueBindObjectMemoryRange(
1102 VkQueue queue,
1103 VkObjectType objType,
1104 VkObject object,
1105 uint32_t allocationIdx,
1106 VkDeviceSize rangeOffset,
1107 VkDeviceSize rangeSize,
1108 VkDeviceMemory mem,
1109 VkDeviceSize memOffset)
1110 {
1111 stub_return(VK_UNSUPPORTED);
1112 }
1113
1114 VkResult anv_QueueBindImageMemoryRange(
1115 VkQueue queue,
1116 VkImage image,
1117 uint32_t allocationIdx,
1118 const VkImageMemoryBindInfo* pBindInfo,
1119 VkDeviceMemory mem,
1120 VkDeviceSize memOffset)
1121 {
1122 stub_return(VK_UNSUPPORTED);
1123 }
1124
1125 VkResult anv_CreateFence(
1126 VkDevice _device,
1127 const VkFenceCreateInfo* pCreateInfo,
1128 VkFence* pFence)
1129 {
1130 struct anv_device *device = (struct anv_device *) _device;
1131 struct anv_fence *fence;
1132 struct anv_batch batch;
1133 VkResult result;
1134
1135 const uint32_t fence_size = 128;
1136
1137 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_FENCE_CREATE_INFO);
1138
1139 fence = anv_device_alloc(device, sizeof(*fence), 8,
1140 VK_SYSTEM_ALLOC_TYPE_API_OBJECT);
1141 if (fence == NULL)
1142 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
1143
1144 result = anv_bo_init_new(&fence->bo, device, fence_size);
1145 if (result != VK_SUCCESS)
1146 goto fail;
1147
1148 fence->bo.map =
1149 anv_gem_mmap(device, fence->bo.gem_handle, 0, fence->bo.size);
1150 batch.next = fence->bo.map;
1151 anv_batch_emit(&batch, GEN8_MI_BATCH_BUFFER_END);
1152 anv_batch_emit(&batch, GEN8_MI_NOOP);
1153
1154 fence->exec2_objects[0].handle = fence->bo.gem_handle;
1155 fence->exec2_objects[0].relocation_count = 0;
1156 fence->exec2_objects[0].relocs_ptr = 0;
1157 fence->exec2_objects[0].alignment = 0;
1158 fence->exec2_objects[0].offset = fence->bo.offset;
1159 fence->exec2_objects[0].flags = 0;
1160 fence->exec2_objects[0].rsvd1 = 0;
1161 fence->exec2_objects[0].rsvd2 = 0;
1162
1163 fence->execbuf.buffers_ptr = (uintptr_t) fence->exec2_objects;
1164 fence->execbuf.buffer_count = 1;
1165 fence->execbuf.batch_start_offset = 0;
1166 fence->execbuf.batch_len = batch.next - fence->bo.map;
1167 fence->execbuf.cliprects_ptr = 0;
1168 fence->execbuf.num_cliprects = 0;
1169 fence->execbuf.DR1 = 0;
1170 fence->execbuf.DR4 = 0;
1171
1172 fence->execbuf.flags =
1173 I915_EXEC_HANDLE_LUT | I915_EXEC_NO_RELOC | I915_EXEC_RENDER;
1174 fence->execbuf.rsvd1 = device->context_id;
1175 fence->execbuf.rsvd2 = 0;
1176
1177 *pFence = (VkQueryPool) fence;
1178
1179 return VK_SUCCESS;
1180
1181 fail:
1182 anv_device_free(device, fence);
1183
1184 return result;
1185 }
1186
1187 VkResult anv_ResetFences(
1188 VkDevice _device,
1189 uint32_t fenceCount,
1190 VkFence* pFences)
1191 {
1192 struct anv_fence **fences = (struct anv_fence **) pFences;
1193
1194 for (uint32_t i; i < fenceCount; i++)
1195 fences[i]->ready = false;
1196
1197 return VK_SUCCESS;
1198 }
1199
1200 VkResult anv_GetFenceStatus(
1201 VkDevice _device,
1202 VkFence _fence)
1203 {
1204 struct anv_device *device = (struct anv_device *) _device;
1205 struct anv_fence *fence = (struct anv_fence *) _fence;
1206 int64_t t = 0;
1207 int ret;
1208
1209 if (fence->ready)
1210 return VK_SUCCESS;
1211
1212 ret = anv_gem_wait(device, fence->bo.gem_handle, &t);
1213 if (ret == 0) {
1214 fence->ready = true;
1215 return VK_SUCCESS;
1216 }
1217
1218 return VK_NOT_READY;
1219 }
1220
1221 VkResult anv_WaitForFences(
1222 VkDevice _device,
1223 uint32_t fenceCount,
1224 const VkFence* pFences,
1225 bool32_t waitAll,
1226 uint64_t timeout)
1227 {
1228 struct anv_device *device = (struct anv_device *) _device;
1229 struct anv_fence **fences = (struct anv_fence **) pFences;
1230 int64_t t = timeout;
1231 int ret;
1232
1233 /* FIXME: handle !waitAll */
1234
1235 for (uint32_t i = 0; i < fenceCount; i++) {
1236 ret = anv_gem_wait(device, fences[i]->bo.gem_handle, &t);
1237 if (ret == -1 && errno == ETIME)
1238 return VK_TIMEOUT;
1239 else if (ret == -1)
1240 return vk_error(VK_ERROR_UNKNOWN);
1241 }
1242
1243 return VK_SUCCESS;
1244 }
1245
1246 // Queue semaphore functions
1247
1248 VkResult anv_CreateSemaphore(
1249 VkDevice device,
1250 const VkSemaphoreCreateInfo* pCreateInfo,
1251 VkSemaphore* pSemaphore)
1252 {
1253 stub_return(VK_UNSUPPORTED);
1254 }
1255
1256 VkResult anv_QueueSignalSemaphore(
1257 VkQueue queue,
1258 VkSemaphore semaphore)
1259 {
1260 stub_return(VK_UNSUPPORTED);
1261 }
1262
1263 VkResult anv_QueueWaitSemaphore(
1264 VkQueue queue,
1265 VkSemaphore semaphore)
1266 {
1267 stub_return(VK_UNSUPPORTED);
1268 }
1269
1270 // Event functions
1271
1272 VkResult anv_CreateEvent(
1273 VkDevice device,
1274 const VkEventCreateInfo* pCreateInfo,
1275 VkEvent* pEvent)
1276 {
1277 stub_return(VK_UNSUPPORTED);
1278 }
1279
1280 VkResult anv_GetEventStatus(
1281 VkDevice device,
1282 VkEvent event)
1283 {
1284 stub_return(VK_UNSUPPORTED);
1285 }
1286
1287 VkResult anv_SetEvent(
1288 VkDevice device,
1289 VkEvent event)
1290 {
1291 stub_return(VK_UNSUPPORTED);
1292 }
1293
1294 VkResult anv_ResetEvent(
1295 VkDevice device,
1296 VkEvent event)
1297 {
1298 stub_return(VK_UNSUPPORTED);
1299 }
1300
1301 // Query functions
1302
1303 struct anv_query_pool {
1304 VkQueryType type;
1305 uint32_t slots;
1306 struct anv_bo bo;
1307 };
1308
1309 VkResult anv_CreateQueryPool(
1310 VkDevice _device,
1311 const VkQueryPoolCreateInfo* pCreateInfo,
1312 VkQueryPool* pQueryPool)
1313 {
1314 struct anv_device *device = (struct anv_device *) _device;
1315 struct anv_query_pool *pool;
1316 VkResult result;
1317
1318 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_QUERY_POOL_CREATE_INFO);
1319
1320 pool = anv_device_alloc(device, sizeof(*pool), 8,
1321 VK_SYSTEM_ALLOC_TYPE_API_OBJECT);
1322 if (pool == NULL)
1323 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
1324
1325 pool->type = pCreateInfo->queryType;
1326 result = anv_bo_init_new(&pool->bo, device, pCreateInfo->slots * 16);
1327 if (result != VK_SUCCESS)
1328 goto fail;
1329
1330 *pQueryPool = (VkQueryPool) pool;
1331
1332 return VK_SUCCESS;
1333
1334 fail:
1335 anv_device_free(device, pool);
1336
1337 return result;
1338 }
1339
1340 VkResult anv_GetQueryPoolResults(
1341 VkDevice device,
1342 VkQueryPool queryPool,
1343 uint32_t startQuery,
1344 uint32_t queryCount,
1345 size_t* pDataSize,
1346 void* pData,
1347 VkQueryResultFlags flags)
1348 {
1349 stub_return(VK_UNSUPPORTED);
1350 }
1351
1352 // Buffer functions
1353
1354 VkResult anv_CreateBuffer(
1355 VkDevice _device,
1356 const VkBufferCreateInfo* pCreateInfo,
1357 VkBuffer* pBuffer)
1358 {
1359 struct anv_device *device = (struct anv_device *) _device;
1360 struct anv_buffer *buffer;
1361
1362 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO);
1363
1364 buffer = anv_device_alloc(device, sizeof(*buffer), 8,
1365 VK_SYSTEM_ALLOC_TYPE_API_OBJECT);
1366 if (buffer == NULL)
1367 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
1368
1369 buffer->size = pCreateInfo->size;
1370 buffer->bo = NULL;
1371 buffer->offset = 0;
1372
1373 *pBuffer = (VkBuffer) buffer;
1374
1375 return VK_SUCCESS;
1376 }
1377
1378 // Buffer view functions
1379
1380 VkResult anv_CreateBufferView(
1381 VkDevice _device,
1382 const VkBufferViewCreateInfo* pCreateInfo,
1383 VkBufferView* pView)
1384 {
1385 struct anv_device *device = (struct anv_device *) _device;
1386 struct anv_buffer *buffer = (struct anv_buffer *) pCreateInfo->buffer;
1387 struct anv_surface_view *view;
1388 const struct anv_format *format;
1389
1390 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO);
1391
1392 view = anv_device_alloc(device, sizeof(*view), 8,
1393 VK_SYSTEM_ALLOC_TYPE_API_OBJECT);
1394 if (view == NULL)
1395 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
1396
1397 view->bo = buffer->bo;
1398 view->offset = buffer->offset + pCreateInfo->offset;
1399 view->surface_state =
1400 anv_state_pool_alloc(&device->surface_state_pool, 64, 64);
1401 view->format = pCreateInfo->format;
1402
1403 format = anv_format_for_vk_format(pCreateInfo->format);
1404 /* This assumes RGBA float format. */
1405 uint32_t stride = 4;
1406 uint32_t num_elements = pCreateInfo->range / stride;
1407 struct GEN8_RENDER_SURFACE_STATE surface_state = {
1408 .SurfaceType = SURFTYPE_BUFFER,
1409 .SurfaceArray = false,
1410 .SurfaceFormat = format->format,
1411 .SurfaceVerticalAlignment = VALIGN4,
1412 .SurfaceHorizontalAlignment = HALIGN4,
1413 .TileMode = LINEAR,
1414 .VerticalLineStride = 0,
1415 .VerticalLineStrideOffset = 0,
1416 .SamplerL2BypassModeDisable = true,
1417 .RenderCacheReadWriteMode = WriteOnlyCache,
1418 .MemoryObjectControlState = 0, /* FIXME: MOCS */
1419 .BaseMipLevel = 0,
1420 .SurfaceQPitch = 0,
1421 .Height = (num_elements >> 7) & 0x3fff,
1422 .Width = num_elements & 0x7f,
1423 .Depth = (num_elements >> 21) & 0x3f,
1424 .SurfacePitch = stride - 1,
1425 .MinimumArrayElement = 0,
1426 .NumberofMultisamples = MULTISAMPLECOUNT_1,
1427 .XOffset = 0,
1428 .YOffset = 0,
1429 .SurfaceMinLOD = 0,
1430 .MIPCountLOD = 0,
1431 .AuxiliarySurfaceMode = AUX_NONE,
1432 .RedClearColor = 0,
1433 .GreenClearColor = 0,
1434 .BlueClearColor = 0,
1435 .AlphaClearColor = 0,
1436 .ShaderChannelSelectRed = SCS_RED,
1437 .ShaderChannelSelectGreen = SCS_GREEN,
1438 .ShaderChannelSelectBlue = SCS_BLUE,
1439 .ShaderChannelSelectAlpha = SCS_ALPHA,
1440 .ResourceMinLOD = 0,
1441 /* FIXME: We assume that the image must be bound at this time. */
1442 .SurfaceBaseAddress = { NULL, view->offset },
1443 };
1444
1445 GEN8_RENDER_SURFACE_STATE_pack(NULL, view->surface_state.map, &surface_state);
1446
1447 *pView = (VkImageView) view;
1448
1449 return VK_SUCCESS;
1450 }
1451
1452 // Sampler functions
1453
1454 VkResult anv_CreateSampler(
1455 VkDevice _device,
1456 const VkSamplerCreateInfo* pCreateInfo,
1457 VkSampler* pSampler)
1458 {
1459 struct anv_device *device = (struct anv_device *) _device;
1460 struct anv_sampler *sampler;
1461
1462 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO);
1463
1464 sampler = anv_device_alloc(device, sizeof(*sampler), 8,
1465 VK_SYSTEM_ALLOC_TYPE_API_OBJECT);
1466 if (!sampler)
1467 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
1468
1469 static const uint32_t vk_to_gen_tex_filter[] = {
1470 [VK_TEX_FILTER_NEAREST] = MAPFILTER_NEAREST,
1471 [VK_TEX_FILTER_LINEAR] = MAPFILTER_LINEAR
1472 };
1473
1474 static const uint32_t vk_to_gen_mipmap_mode[] = {
1475 [VK_TEX_MIPMAP_MODE_BASE] = MIPFILTER_NONE,
1476 [VK_TEX_MIPMAP_MODE_NEAREST] = MIPFILTER_NEAREST,
1477 [VK_TEX_MIPMAP_MODE_LINEAR] = MIPFILTER_LINEAR
1478 };
1479
1480 static const uint32_t vk_to_gen_tex_address[] = {
1481 [VK_TEX_ADDRESS_WRAP] = TCM_WRAP,
1482 [VK_TEX_ADDRESS_MIRROR] = TCM_MIRROR,
1483 [VK_TEX_ADDRESS_CLAMP] = TCM_CLAMP,
1484 [VK_TEX_ADDRESS_MIRROR_ONCE] = TCM_MIRROR_ONCE,
1485 [VK_TEX_ADDRESS_CLAMP_BORDER] = TCM_CLAMP_BORDER,
1486 };
1487
1488 static const uint32_t vk_to_gen_compare_op[] = {
1489 [VK_COMPARE_OP_NEVER] = PREFILTEROPNEVER,
1490 [VK_COMPARE_OP_LESS] = PREFILTEROPLESS,
1491 [VK_COMPARE_OP_EQUAL] = PREFILTEROPEQUAL,
1492 [VK_COMPARE_OP_LESS_EQUAL] = PREFILTEROPLEQUAL,
1493 [VK_COMPARE_OP_GREATER] = PREFILTEROPGREATER,
1494 [VK_COMPARE_OP_NOT_EQUAL] = PREFILTEROPNOTEQUAL,
1495 [VK_COMPARE_OP_GREATER_EQUAL] = PREFILTEROPGEQUAL,
1496 [VK_COMPARE_OP_ALWAYS] = PREFILTEROPALWAYS,
1497 };
1498
1499 if (pCreateInfo->maxAnisotropy > 0)
1500 anv_finishme("missing support for anisotropic filtering");
1501
1502 struct GEN8_SAMPLER_STATE sampler_state = {
1503 .SamplerDisable = false,
1504 .TextureBorderColorMode = DX10OGL,
1505 .LODPreClampMode = 0,
1506 .BaseMipLevel = 0,
1507 .MipModeFilter = vk_to_gen_mipmap_mode[pCreateInfo->mipMode],
1508 .MagModeFilter = vk_to_gen_tex_filter[pCreateInfo->magFilter],
1509 .MinModeFilter = vk_to_gen_tex_filter[pCreateInfo->minFilter],
1510 .TextureLODBias = pCreateInfo->mipLodBias * 256,
1511 .AnisotropicAlgorithm = EWAApproximation,
1512 .MinLOD = pCreateInfo->minLod * 256,
1513 .MaxLOD = pCreateInfo->maxLod * 256,
1514 .ChromaKeyEnable = 0,
1515 .ChromaKeyIndex = 0,
1516 .ChromaKeyMode = 0,
1517 .ShadowFunction = vk_to_gen_compare_op[pCreateInfo->compareOp],
1518 .CubeSurfaceControlMode = 0,
1519 .IndirectStatePointer = 0,
1520 .LODClampMagnificationMode = MIPNONE,
1521 .MaximumAnisotropy = 0,
1522 .RAddressMinFilterRoundingEnable = 0,
1523 .RAddressMagFilterRoundingEnable = 0,
1524 .VAddressMinFilterRoundingEnable = 0,
1525 .VAddressMagFilterRoundingEnable = 0,
1526 .UAddressMinFilterRoundingEnable = 0,
1527 .UAddressMagFilterRoundingEnable = 0,
1528 .TrilinearFilterQuality = 0,
1529 .NonnormalizedCoordinateEnable = 0,
1530 .TCXAddressControlMode = vk_to_gen_tex_address[pCreateInfo->addressU],
1531 .TCYAddressControlMode = vk_to_gen_tex_address[pCreateInfo->addressV],
1532 .TCZAddressControlMode = vk_to_gen_tex_address[pCreateInfo->addressW],
1533 };
1534
1535 GEN8_SAMPLER_STATE_pack(NULL, sampler->state, &sampler_state);
1536
1537 *pSampler = (VkSampler) sampler;
1538
1539 return VK_SUCCESS;
1540 }
1541
1542 // Descriptor set functions
1543
1544 VkResult anv_CreateDescriptorSetLayout(
1545 VkDevice _device,
1546 const VkDescriptorSetLayoutCreateInfo* pCreateInfo,
1547 VkDescriptorSetLayout* pSetLayout)
1548 {
1549 struct anv_device *device = (struct anv_device *) _device;
1550 struct anv_descriptor_set_layout *set_layout;
1551
1552 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO);
1553
1554 uint32_t sampler_count[VK_NUM_SHADER_STAGE] = { 0, };
1555 uint32_t surface_count[VK_NUM_SHADER_STAGE] = { 0, };
1556 uint32_t num_dynamic_buffers = 0;
1557 uint32_t count = 0;
1558 uint32_t s;
1559
1560 for (uint32_t i = 0; i < pCreateInfo->count; i++) {
1561 switch (pCreateInfo->pBinding[i].descriptorType) {
1562 case VK_DESCRIPTOR_TYPE_SAMPLER:
1563 for_each_bit(s, pCreateInfo->pBinding[i].stageFlags)
1564 sampler_count[s] += pCreateInfo->pBinding[i].count;
1565 break;
1566
1567 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
1568 for_each_bit(s, pCreateInfo->pBinding[i].stageFlags)
1569 sampler_count[s] += pCreateInfo->pBinding[i].count;
1570
1571 /* fall through */
1572
1573 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
1574 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE:
1575 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
1576 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
1577 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
1578 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
1579 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
1580 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
1581 for_each_bit(s, pCreateInfo->pBinding[i].stageFlags)
1582 surface_count[s] += pCreateInfo->pBinding[i].count;
1583 break;
1584 default:
1585 break;
1586 }
1587
1588 count += pCreateInfo->pBinding[i].count;
1589 }
1590
1591 for (uint32_t i = 0; i < pCreateInfo->count; i++) {
1592 switch (pCreateInfo->pBinding[i].descriptorType) {
1593 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
1594 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
1595 num_dynamic_buffers++;
1596 break;
1597 default:
1598 break;
1599 }
1600 }
1601
1602 uint32_t sampler_total = 0;
1603 uint32_t surface_total = 0;
1604 for (uint32_t s = 0; s < VK_NUM_SHADER_STAGE; s++) {
1605 sampler_total += sampler_count[s];
1606 surface_total += surface_count[s];
1607 }
1608
1609 size_t size = sizeof(*set_layout) +
1610 (sampler_total + surface_total) * sizeof(uint32_t);
1611 set_layout = anv_device_alloc(device, size, 8,
1612 VK_SYSTEM_ALLOC_TYPE_API_OBJECT);
1613 if (!set_layout)
1614 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
1615
1616 set_layout->num_dynamic_buffers = num_dynamic_buffers;
1617 set_layout->count = count;
1618
1619 uint32_t *p = set_layout->entries;
1620 uint32_t *sampler[VK_NUM_SHADER_STAGE];
1621 uint32_t *surface[VK_NUM_SHADER_STAGE];
1622 for (uint32_t s = 0; s < VK_NUM_SHADER_STAGE; s++) {
1623 set_layout->stage[s].surface_count = surface_count[s];
1624 set_layout->stage[s].surface_start = surface[s] = p;
1625 p += surface_count[s];
1626 set_layout->stage[s].sampler_count = sampler_count[s];
1627 set_layout->stage[s].sampler_start = sampler[s] = p;
1628 p += sampler_count[s];
1629 }
1630
1631 uint32_t descriptor = 0;
1632 for (uint32_t i = 0; i < pCreateInfo->count; i++) {
1633 switch (pCreateInfo->pBinding[i].descriptorType) {
1634 case VK_DESCRIPTOR_TYPE_SAMPLER:
1635 for_each_bit(s, pCreateInfo->pBinding[i].stageFlags)
1636 for (uint32_t j = 0; j < pCreateInfo->pBinding[i].count; j++)
1637 *(sampler[s])++ = descriptor + j;
1638 break;
1639
1640 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
1641 for_each_bit(s, pCreateInfo->pBinding[i].stageFlags)
1642 for (uint32_t j = 0; j < pCreateInfo->pBinding[i].count; j++)
1643 *(sampler[s])++ = descriptor + j;
1644
1645 /* fallthrough */
1646
1647 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
1648 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE:
1649 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
1650 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
1651 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
1652 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
1653 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
1654 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
1655 for_each_bit(s, pCreateInfo->pBinding[i].stageFlags)
1656 for (uint32_t j = 0; j < pCreateInfo->pBinding[i].count; j++) {
1657 *(surface[s])++ = descriptor + j;
1658 }
1659 break;
1660 default:
1661 unreachable("");
1662 }
1663 descriptor += pCreateInfo->pBinding[i].count;
1664 }
1665
1666 *pSetLayout = (VkDescriptorSetLayout) set_layout;
1667
1668 return VK_SUCCESS;
1669 }
1670
1671 VkResult anv_BeginDescriptorPoolUpdate(
1672 VkDevice device,
1673 VkDescriptorUpdateMode updateMode)
1674 {
1675 return VK_SUCCESS;
1676 }
1677
1678 VkResult anv_EndDescriptorPoolUpdate(
1679 VkDevice device,
1680 VkCmdBuffer cmd)
1681 {
1682 return VK_SUCCESS;
1683 }
1684
1685 VkResult anv_CreateDescriptorPool(
1686 VkDevice device,
1687 VkDescriptorPoolUsage poolUsage,
1688 uint32_t maxSets,
1689 const VkDescriptorPoolCreateInfo* pCreateInfo,
1690 VkDescriptorPool* pDescriptorPool)
1691 {
1692 *pDescriptorPool = 1;
1693
1694 return VK_SUCCESS;
1695 }
1696
1697 VkResult anv_ResetDescriptorPool(
1698 VkDevice device,
1699 VkDescriptorPool descriptorPool)
1700 {
1701 return VK_SUCCESS;
1702 }
1703
1704 VkResult anv_AllocDescriptorSets(
1705 VkDevice _device,
1706 VkDescriptorPool descriptorPool,
1707 VkDescriptorSetUsage setUsage,
1708 uint32_t count,
1709 const VkDescriptorSetLayout* pSetLayouts,
1710 VkDescriptorSet* pDescriptorSets,
1711 uint32_t* pCount)
1712 {
1713 struct anv_device *device = (struct anv_device *) _device;
1714 const struct anv_descriptor_set_layout *layout;
1715 struct anv_descriptor_set *set;
1716 size_t size;
1717
1718 for (uint32_t i = 0; i < count; i++) {
1719 layout = (struct anv_descriptor_set_layout *) pSetLayouts[i];
1720 size = sizeof(*set) + layout->count * sizeof(set->descriptors[0]);
1721 set = anv_device_alloc(device, size, 8,
1722 VK_SYSTEM_ALLOC_TYPE_API_OBJECT);
1723 if (!set) {
1724 *pCount = i;
1725 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
1726 }
1727
1728 pDescriptorSets[i] = (VkDescriptorSet) set;
1729 }
1730
1731 *pCount = count;
1732
1733 return VK_SUCCESS;
1734 }
1735
1736 void anv_ClearDescriptorSets(
1737 VkDevice device,
1738 VkDescriptorPool descriptorPool,
1739 uint32_t count,
1740 const VkDescriptorSet* pDescriptorSets)
1741 {
1742 }
1743
1744 void anv_UpdateDescriptors(
1745 VkDevice _device,
1746 VkDescriptorSet descriptorSet,
1747 uint32_t updateCount,
1748 const void** ppUpdateArray)
1749 {
1750 struct anv_descriptor_set *set = (struct anv_descriptor_set *) descriptorSet;
1751 VkUpdateSamplers *update_samplers;
1752 VkUpdateSamplerTextures *update_sampler_textures;
1753 VkUpdateImages *update_images;
1754 VkUpdateBuffers *update_buffers;
1755 VkUpdateAsCopy *update_as_copy;
1756
1757 for (uint32_t i = 0; i < updateCount; i++) {
1758 const struct anv_common *common = ppUpdateArray[i];
1759
1760 switch (common->sType) {
1761 case VK_STRUCTURE_TYPE_UPDATE_SAMPLERS:
1762 update_samplers = (VkUpdateSamplers *) common;
1763
1764 for (uint32_t j = 0; j < update_samplers->count; j++) {
1765 set->descriptors[update_samplers->binding + j].sampler =
1766 (struct anv_sampler *) update_samplers->pSamplers[j];
1767 }
1768 break;
1769
1770 case VK_STRUCTURE_TYPE_UPDATE_SAMPLER_TEXTURES:
1771 /* FIXME: Shouldn't this be *_UPDATE_SAMPLER_IMAGES? */
1772 update_sampler_textures = (VkUpdateSamplerTextures *) common;
1773
1774 for (uint32_t j = 0; j < update_sampler_textures->count; j++) {
1775 set->descriptors[update_sampler_textures->binding + j].view =
1776 (struct anv_surface_view *)
1777 update_sampler_textures->pSamplerImageViews[j].pImageView->view;
1778 set->descriptors[update_sampler_textures->binding + j].sampler =
1779 (struct anv_sampler *)
1780 update_sampler_textures->pSamplerImageViews[j].sampler;
1781 }
1782 break;
1783
1784 case VK_STRUCTURE_TYPE_UPDATE_IMAGES:
1785 update_images = (VkUpdateImages *) common;
1786
1787 for (uint32_t j = 0; j < update_images->count; j++) {
1788 set->descriptors[update_images->binding + j].view =
1789 (struct anv_surface_view *) update_images->pImageViews[j].view;
1790 }
1791 break;
1792
1793 case VK_STRUCTURE_TYPE_UPDATE_BUFFERS:
1794 update_buffers = (VkUpdateBuffers *) common;
1795
1796 for (uint32_t j = 0; j < update_buffers->count; j++) {
1797 set->descriptors[update_buffers->binding + j].view =
1798 (struct anv_surface_view *) update_buffers->pBufferViews[j].view;
1799 }
1800 /* FIXME: descriptor arrays? */
1801 break;
1802
1803 case VK_STRUCTURE_TYPE_UPDATE_AS_COPY:
1804 update_as_copy = (VkUpdateAsCopy *) common;
1805 (void) update_as_copy;
1806 break;
1807
1808 default:
1809 break;
1810 }
1811 }
1812 }
1813
1814 // State object functions
1815
1816 static inline int64_t
1817 clamp_int64(int64_t x, int64_t min, int64_t max)
1818 {
1819 if (x < min)
1820 return min;
1821 else if (x < max)
1822 return x;
1823 else
1824 return max;
1825 }
1826
1827 VkResult anv_CreateDynamicViewportState(
1828 VkDevice _device,
1829 const VkDynamicVpStateCreateInfo* pCreateInfo,
1830 VkDynamicVpState* pState)
1831 {
1832 struct anv_device *device = (struct anv_device *) _device;
1833 struct anv_dynamic_vp_state *state;
1834
1835 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_DYNAMIC_VP_STATE_CREATE_INFO);
1836
1837 state = anv_device_alloc(device, sizeof(*state), 8,
1838 VK_SYSTEM_ALLOC_TYPE_API_OBJECT);
1839 if (state == NULL)
1840 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
1841
1842 unsigned count = pCreateInfo->viewportAndScissorCount;
1843 state->sf_clip_vp = anv_state_pool_alloc(&device->dynamic_state_pool,
1844 count * 64, 64);
1845 state->cc_vp = anv_state_pool_alloc(&device->dynamic_state_pool,
1846 count * 8, 32);
1847 state->scissor = anv_state_pool_alloc(&device->dynamic_state_pool,
1848 count * 32, 32);
1849
1850 for (uint32_t i = 0; i < pCreateInfo->viewportAndScissorCount; i++) {
1851 const VkViewport *vp = &pCreateInfo->pViewports[i];
1852 const VkRect *s = &pCreateInfo->pScissors[i];
1853
1854 struct GEN8_SF_CLIP_VIEWPORT sf_clip_viewport = {
1855 .ViewportMatrixElementm00 = vp->width / 2,
1856 .ViewportMatrixElementm11 = vp->height / 2,
1857 .ViewportMatrixElementm22 = (vp->maxDepth - vp->minDepth) / 2,
1858 .ViewportMatrixElementm30 = vp->originX + vp->width / 2,
1859 .ViewportMatrixElementm31 = vp->originY + vp->height / 2,
1860 .ViewportMatrixElementm32 = (vp->maxDepth + vp->minDepth) / 2,
1861 .XMinClipGuardband = -1.0f,
1862 .XMaxClipGuardband = 1.0f,
1863 .YMinClipGuardband = -1.0f,
1864 .YMaxClipGuardband = 1.0f,
1865 .XMinViewPort = vp->originX,
1866 .XMaxViewPort = vp->originX + vp->width - 1,
1867 .YMinViewPort = vp->originY,
1868 .YMaxViewPort = vp->originY + vp->height - 1,
1869 };
1870
1871 struct GEN8_CC_VIEWPORT cc_viewport = {
1872 .MinimumDepth = vp->minDepth,
1873 .MaximumDepth = vp->maxDepth
1874 };
1875
1876 /* Since xmax and ymax are inclusive, we have to have xmax < xmin or
1877 * ymax < ymin for empty clips. In case clip x, y, width height are all
1878 * 0, the clamps below produce 0 for xmin, ymin, xmax, ymax, which isn't
1879 * what we want. Just special case empty clips and produce a canonical
1880 * empty clip. */
1881 static const struct GEN8_SCISSOR_RECT empty_scissor = {
1882 .ScissorRectangleYMin = 1,
1883 .ScissorRectangleXMin = 1,
1884 .ScissorRectangleYMax = 0,
1885 .ScissorRectangleXMax = 0
1886 };
1887
1888 const int max = 0xffff;
1889 struct GEN8_SCISSOR_RECT scissor = {
1890 /* Do this math using int64_t so overflow gets clamped correctly. */
1891 .ScissorRectangleYMin = clamp_int64(s->offset.y, 0, max),
1892 .ScissorRectangleXMin = clamp_int64(s->offset.x, 0, max),
1893 .ScissorRectangleYMax = clamp_int64((uint64_t) s->offset.y + s->extent.height - 1, 0, max),
1894 .ScissorRectangleXMax = clamp_int64((uint64_t) s->offset.x + s->extent.width - 1, 0, max)
1895 };
1896
1897 GEN8_SF_CLIP_VIEWPORT_pack(NULL, state->sf_clip_vp.map + i * 64, &sf_clip_viewport);
1898 GEN8_CC_VIEWPORT_pack(NULL, state->cc_vp.map + i * 32, &cc_viewport);
1899
1900 if (s->extent.width <= 0 || s->extent.height <= 0) {
1901 GEN8_SCISSOR_RECT_pack(NULL, state->scissor.map + i * 32, &empty_scissor);
1902 } else {
1903 GEN8_SCISSOR_RECT_pack(NULL, state->scissor.map + i * 32, &scissor);
1904 }
1905 }
1906
1907 *pState = (VkDynamicVpState) state;
1908
1909 return VK_SUCCESS;
1910 }
1911
1912 VkResult anv_CreateDynamicRasterState(
1913 VkDevice _device,
1914 const VkDynamicRsStateCreateInfo* pCreateInfo,
1915 VkDynamicRsState* pState)
1916 {
1917 struct anv_device *device = (struct anv_device *) _device;
1918 struct anv_dynamic_rs_state *state;
1919
1920 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_DYNAMIC_RS_STATE_CREATE_INFO);
1921
1922 state = anv_device_alloc(device, sizeof(*state), 8,
1923 VK_SYSTEM_ALLOC_TYPE_API_OBJECT);
1924 if (state == NULL)
1925 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
1926
1927 /* Missing these:
1928 * float depthBias;
1929 * float depthBiasClamp;
1930 * float slopeScaledDepthBias;
1931 * float pointFadeThreshold;
1932 * // optional (GL45) - Size of point fade threshold
1933 */
1934
1935 struct GEN8_3DSTATE_SF sf = {
1936 GEN8_3DSTATE_SF_header,
1937 .LineWidth = pCreateInfo->lineWidth,
1938 .PointWidth = pCreateInfo->pointSize,
1939 };
1940
1941 GEN8_3DSTATE_SF_pack(NULL, state->state_sf, &sf);
1942
1943 *pState = (VkDynamicRsState) state;
1944
1945 return VK_SUCCESS;
1946 }
1947
1948 VkResult anv_CreateDynamicColorBlendState(
1949 VkDevice _device,
1950 const VkDynamicCbStateCreateInfo* pCreateInfo,
1951 VkDynamicCbState* pState)
1952 {
1953 struct anv_device *device = (struct anv_device *) _device;
1954 struct anv_dynamic_cb_state *state;
1955
1956 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_DYNAMIC_CB_STATE_CREATE_INFO);
1957
1958 state = anv_device_alloc(device, sizeof(*state), 8,
1959 VK_SYSTEM_ALLOC_TYPE_API_OBJECT);
1960 if (state == NULL)
1961 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
1962
1963 *pState = (VkDynamicCbState) state;
1964
1965 return VK_SUCCESS;
1966 }
1967
1968 VkResult anv_CreateDynamicDepthStencilState(
1969 VkDevice device,
1970 const VkDynamicDsStateCreateInfo* pCreateInfo,
1971 VkDynamicDsState* pState)
1972 {
1973 stub_return(VK_UNSUPPORTED);
1974 }
1975
1976 // Command buffer functions
1977
1978 VkResult anv_CreateCommandBuffer(
1979 VkDevice _device,
1980 const VkCmdBufferCreateInfo* pCreateInfo,
1981 VkCmdBuffer* pCmdBuffer)
1982 {
1983 struct anv_device *device = (struct anv_device *) _device;
1984 struct anv_cmd_buffer *cmd_buffer;
1985 VkResult result;
1986
1987 cmd_buffer = anv_device_alloc(device, sizeof(*cmd_buffer), 8,
1988 VK_SYSTEM_ALLOC_TYPE_API_OBJECT);
1989 if (cmd_buffer == NULL)
1990 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
1991
1992 cmd_buffer->device = device;
1993 cmd_buffer->rs_state = NULL;
1994 cmd_buffer->vp_state = NULL;
1995 memset(&cmd_buffer->default_bindings, 0, sizeof(cmd_buffer->default_bindings));
1996 cmd_buffer->bindings = &cmd_buffer->default_bindings;
1997
1998 result = anv_batch_init(&cmd_buffer->batch, device);
1999 if (result != VK_SUCCESS)
2000 goto fail;
2001
2002 cmd_buffer->exec2_objects =
2003 anv_device_alloc(device, 8192 * sizeof(cmd_buffer->exec2_objects[0]), 8,
2004 VK_SYSTEM_ALLOC_TYPE_API_OBJECT);
2005 if (cmd_buffer->exec2_objects == NULL) {
2006 result = vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
2007 goto fail_batch;
2008 }
2009
2010 cmd_buffer->exec2_bos =
2011 anv_device_alloc(device, 8192 * sizeof(cmd_buffer->exec2_bos[0]), 8,
2012 VK_SYSTEM_ALLOC_TYPE_API_OBJECT);
2013 if (cmd_buffer->exec2_bos == NULL) {
2014 result = vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
2015 goto fail_exec2_objects;
2016 }
2017
2018 anv_state_stream_init(&cmd_buffer->surface_state_stream,
2019 &device->surface_state_block_pool);
2020 anv_state_stream_init(&cmd_buffer->dynamic_state_stream,
2021 &device->dynamic_state_block_pool);
2022
2023 cmd_buffer->dirty = 0;
2024 cmd_buffer->vb_dirty = 0;
2025
2026 *pCmdBuffer = (VkCmdBuffer) cmd_buffer;
2027
2028 return VK_SUCCESS;
2029
2030 fail_exec2_objects:
2031 anv_device_free(device, cmd_buffer->exec2_objects);
2032 fail_batch:
2033 anv_batch_finish(&cmd_buffer->batch, device);
2034 fail:
2035 anv_device_free(device, cmd_buffer);
2036
2037 return result;
2038 }
2039
2040 VkResult anv_BeginCommandBuffer(
2041 VkCmdBuffer cmdBuffer,
2042 const VkCmdBufferBeginInfo* pBeginInfo)
2043 {
2044 struct anv_cmd_buffer *cmd_buffer = (struct anv_cmd_buffer *) cmdBuffer;
2045 struct anv_device *device = cmd_buffer->device;
2046
2047 anv_batch_emit(&cmd_buffer->batch, GEN8_PIPELINE_SELECT,
2048 .PipelineSelection = _3D);
2049 anv_batch_emit(&cmd_buffer->batch, GEN8_STATE_SIP);
2050
2051 anv_batch_emit(&cmd_buffer->batch, GEN8_STATE_BASE_ADDRESS,
2052 .GeneralStateBaseAddress = { NULL, 0 },
2053 .GeneralStateBaseAddressModifyEnable = true,
2054 .GeneralStateBufferSize = 0xfffff,
2055 .GeneralStateBufferSizeModifyEnable = true,
2056
2057 .SurfaceStateBaseAddress = { &device->surface_state_block_pool.bo, 0 },
2058 .SurfaceStateMemoryObjectControlState = 0, /* FIXME: MOCS */
2059 .SurfaceStateBaseAddressModifyEnable = true,
2060
2061 .DynamicStateBaseAddress = { &device->dynamic_state_block_pool.bo, 0 },
2062 .DynamicStateBaseAddressModifyEnable = true,
2063 .DynamicStateBufferSize = 0xfffff,
2064 .DynamicStateBufferSizeModifyEnable = true,
2065
2066 .IndirectObjectBaseAddress = { NULL, 0 },
2067 .IndirectObjectBaseAddressModifyEnable = true,
2068 .IndirectObjectBufferSize = 0xfffff,
2069 .IndirectObjectBufferSizeModifyEnable = true,
2070
2071 .InstructionBaseAddress = { &device->instruction_block_pool.bo, 0 },
2072 .InstructionBaseAddressModifyEnable = true,
2073 .InstructionBufferSize = 0xfffff,
2074 .InstructionBuffersizeModifyEnable = true);
2075
2076 anv_batch_emit(&cmd_buffer->batch, GEN8_3DSTATE_VF_STATISTICS,
2077 .StatisticsEnable = true);
2078 anv_batch_emit(&cmd_buffer->batch, GEN8_3DSTATE_HS, .Enable = false);
2079 anv_batch_emit(&cmd_buffer->batch, GEN8_3DSTATE_TE, .TEEnable = false);
2080 anv_batch_emit(&cmd_buffer->batch, GEN8_3DSTATE_DS, .FunctionEnable = false);
2081 anv_batch_emit(&cmd_buffer->batch, GEN8_3DSTATE_STREAMOUT, .SOFunctionEnable = false);
2082
2083 anv_batch_emit(&cmd_buffer->batch, GEN8_3DSTATE_PUSH_CONSTANT_ALLOC_VS,
2084 .ConstantBufferOffset = 0,
2085 .ConstantBufferSize = 4);
2086 anv_batch_emit(&cmd_buffer->batch, GEN8_3DSTATE_PUSH_CONSTANT_ALLOC_GS,
2087 .ConstantBufferOffset = 4,
2088 .ConstantBufferSize = 4);
2089 anv_batch_emit(&cmd_buffer->batch, GEN8_3DSTATE_PUSH_CONSTANT_ALLOC_PS,
2090 .ConstantBufferOffset = 8,
2091 .ConstantBufferSize = 4);
2092
2093 anv_batch_emit(&cmd_buffer->batch, GEN8_3DSTATE_WM_CHROMAKEY,
2094 .ChromaKeyKillEnable = false);
2095 anv_batch_emit(&cmd_buffer->batch, GEN8_3DSTATE_SBE_SWIZ);
2096 anv_batch_emit(&cmd_buffer->batch, GEN8_3DSTATE_AA_LINE_PARAMETERS);
2097
2098 /* Hardcoded state: */
2099 anv_batch_emit(&cmd_buffer->batch, GEN8_3DSTATE_DEPTH_BUFFER,
2100 .SurfaceType = SURFTYPE_2D,
2101 .Width = 1,
2102 .Height = 1,
2103 .SurfaceFormat = D16_UNORM,
2104 .SurfaceBaseAddress = { NULL, 0 },
2105 .HierarchicalDepthBufferEnable = 0);
2106
2107 anv_batch_emit(&cmd_buffer->batch, GEN8_3DSTATE_WM_DEPTH_STENCIL,
2108 .DepthTestEnable = false,
2109 .DepthBufferWriteEnable = false);
2110
2111 return VK_SUCCESS;
2112 }
2113
2114 static void
2115 anv_cmd_buffer_add_bo(struct anv_cmd_buffer *cmd_buffer,
2116 struct anv_bo *bo, struct anv_reloc_list *list)
2117 {
2118 struct drm_i915_gem_exec_object2 *obj;
2119
2120 bo->index = cmd_buffer->bo_count;
2121 obj = &cmd_buffer->exec2_objects[bo->index];
2122 cmd_buffer->exec2_bos[bo->index] = bo;
2123 cmd_buffer->bo_count++;
2124
2125 obj->handle = bo->gem_handle;
2126 obj->relocation_count = 0;
2127 obj->relocs_ptr = 0;
2128 obj->alignment = 0;
2129 obj->offset = bo->offset;
2130 obj->flags = 0;
2131 obj->rsvd1 = 0;
2132 obj->rsvd2 = 0;
2133
2134 if (list) {
2135 obj->relocation_count = list->num_relocs;
2136 obj->relocs_ptr = (uintptr_t) list->relocs;
2137 }
2138 }
2139
2140 static void
2141 anv_cmd_buffer_add_validate_bos(struct anv_cmd_buffer *cmd_buffer,
2142 struct anv_reloc_list *list)
2143 {
2144 struct anv_bo *bo, *batch_bo;
2145
2146 batch_bo = &cmd_buffer->batch.bo;
2147 for (size_t i = 0; i < list->num_relocs; i++) {
2148 bo = list->reloc_bos[i];
2149 /* Skip any relocations targeting the batch bo. We need to make sure
2150 * it's the last in the list so we'll add it manually later.
2151 */
2152 if (bo == batch_bo)
2153 continue;
2154 if (bo->index < cmd_buffer->bo_count && cmd_buffer->exec2_bos[bo->index] == bo)
2155 continue;
2156
2157 anv_cmd_buffer_add_bo(cmd_buffer, bo, NULL);
2158 }
2159 }
2160
2161 static void
2162 anv_cmd_buffer_process_relocs(struct anv_cmd_buffer *cmd_buffer,
2163 struct anv_reloc_list *list)
2164 {
2165 struct anv_bo *bo;
2166
2167 /* If the kernel supports I915_EXEC_NO_RELOC, it will compare offset in
2168 * struct drm_i915_gem_exec_object2 against the bos current offset and if
2169 * all bos haven't moved it will skip relocation processing alltogether.
2170 * If I915_EXEC_NO_RELOC is not supported, the kernel ignores the incoming
2171 * value of offset so we can set it either way. For that to work we need
2172 * to make sure all relocs use the same presumed offset.
2173 */
2174
2175 for (size_t i = 0; i < list->num_relocs; i++) {
2176 bo = list->reloc_bos[i];
2177 if (bo->offset != list->relocs[i].presumed_offset)
2178 cmd_buffer->need_reloc = true;
2179
2180 list->relocs[i].target_handle = bo->index;
2181 }
2182 }
2183
2184 VkResult anv_EndCommandBuffer(
2185 VkCmdBuffer cmdBuffer)
2186 {
2187 struct anv_cmd_buffer *cmd_buffer = (struct anv_cmd_buffer *) cmdBuffer;
2188 struct anv_device *device = cmd_buffer->device;
2189 struct anv_batch *batch = &cmd_buffer->batch;
2190
2191 anv_batch_emit(batch, GEN8_MI_BATCH_BUFFER_END);
2192
2193 /* Round batch up to an even number of dwords. */
2194 if ((batch->next - batch->bo.map) & 4)
2195 anv_batch_emit(batch, GEN8_MI_NOOP);
2196
2197 cmd_buffer->bo_count = 0;
2198 cmd_buffer->need_reloc = false;
2199
2200 /* Lock for access to bo->index. */
2201 pthread_mutex_lock(&device->mutex);
2202
2203 /* Add block pool bos first so we can add them with their relocs. */
2204 anv_cmd_buffer_add_bo(cmd_buffer, &device->surface_state_block_pool.bo,
2205 &batch->surf_relocs);
2206
2207 anv_cmd_buffer_add_validate_bos(cmd_buffer, &batch->surf_relocs);
2208 anv_cmd_buffer_add_validate_bos(cmd_buffer, &batch->cmd_relocs);
2209 anv_cmd_buffer_add_bo(cmd_buffer, &batch->bo, &batch->cmd_relocs);
2210 anv_cmd_buffer_process_relocs(cmd_buffer, &batch->surf_relocs);
2211 anv_cmd_buffer_process_relocs(cmd_buffer, &batch->cmd_relocs);
2212
2213 cmd_buffer->execbuf.buffers_ptr = (uintptr_t) cmd_buffer->exec2_objects;
2214 cmd_buffer->execbuf.buffer_count = cmd_buffer->bo_count;
2215 cmd_buffer->execbuf.batch_start_offset = 0;
2216 cmd_buffer->execbuf.batch_len = batch->next - batch->bo.map;
2217 cmd_buffer->execbuf.cliprects_ptr = 0;
2218 cmd_buffer->execbuf.num_cliprects = 0;
2219 cmd_buffer->execbuf.DR1 = 0;
2220 cmd_buffer->execbuf.DR4 = 0;
2221
2222 cmd_buffer->execbuf.flags = I915_EXEC_HANDLE_LUT;
2223 if (!cmd_buffer->need_reloc)
2224 cmd_buffer->execbuf.flags |= I915_EXEC_NO_RELOC;
2225 cmd_buffer->execbuf.flags |= I915_EXEC_RENDER;
2226 cmd_buffer->execbuf.rsvd1 = device->context_id;
2227 cmd_buffer->execbuf.rsvd2 = 0;
2228
2229 pthread_mutex_unlock(&device->mutex);
2230
2231 return VK_SUCCESS;
2232 }
2233
2234 VkResult anv_ResetCommandBuffer(
2235 VkCmdBuffer cmdBuffer)
2236 {
2237 struct anv_cmd_buffer *cmd_buffer = (struct anv_cmd_buffer *) cmdBuffer;
2238
2239 anv_batch_reset(&cmd_buffer->batch);
2240
2241 return VK_SUCCESS;
2242 }
2243
2244 // Command buffer building functions
2245
2246 void anv_CmdBindPipeline(
2247 VkCmdBuffer cmdBuffer,
2248 VkPipelineBindPoint pipelineBindPoint,
2249 VkPipeline _pipeline)
2250 {
2251 struct anv_cmd_buffer *cmd_buffer = (struct anv_cmd_buffer *) cmdBuffer;
2252
2253 cmd_buffer->pipeline = (struct anv_pipeline *) _pipeline;
2254 cmd_buffer->dirty |= ANV_CMD_BUFFER_PIPELINE_DIRTY;
2255 }
2256
2257 void anv_CmdBindDynamicStateObject(
2258 VkCmdBuffer cmdBuffer,
2259 VkStateBindPoint stateBindPoint,
2260 VkDynamicStateObject dynamicState)
2261 {
2262 struct anv_cmd_buffer *cmd_buffer = (struct anv_cmd_buffer *) cmdBuffer;
2263 struct anv_dynamic_vp_state *vp_state;
2264
2265 switch (stateBindPoint) {
2266 case VK_STATE_BIND_POINT_VIEWPORT:
2267 vp_state = (struct anv_dynamic_vp_state *) dynamicState;
2268 /* We emit state immediately, but set cmd_buffer->vp_state to indicate
2269 * that vp state has been set in this command buffer. */
2270 cmd_buffer->vp_state = vp_state;
2271 anv_batch_emit(&cmd_buffer->batch, GEN8_3DSTATE_SCISSOR_STATE_POINTERS,
2272 .ScissorRectPointer = vp_state->scissor.offset);
2273 anv_batch_emit(&cmd_buffer->batch, GEN8_3DSTATE_VIEWPORT_STATE_POINTERS_CC,
2274 .CCViewportPointer = vp_state->cc_vp.offset);
2275 anv_batch_emit(&cmd_buffer->batch, GEN8_3DSTATE_VIEWPORT_STATE_POINTERS_SF_CLIP,
2276 .SFClipViewportPointer = vp_state->sf_clip_vp.offset);
2277 break;
2278 case VK_STATE_BIND_POINT_RASTER:
2279 cmd_buffer->rs_state = (struct anv_dynamic_rs_state *) dynamicState;
2280 cmd_buffer->dirty |= ANV_CMD_BUFFER_RS_DIRTY;
2281 break;
2282 case VK_STATE_BIND_POINT_COLOR_BLEND:
2283 case VK_STATE_BIND_POINT_DEPTH_STENCIL:
2284 break;
2285 default:
2286 break;
2287 };
2288 }
2289
2290 void anv_CmdBindDescriptorSets(
2291 VkCmdBuffer cmdBuffer,
2292 VkPipelineBindPoint pipelineBindPoint,
2293 uint32_t firstSet,
2294 uint32_t setCount,
2295 const VkDescriptorSet* pDescriptorSets,
2296 uint32_t dynamicOffsetCount,
2297 const uint32_t* pDynamicOffsets)
2298 {
2299 struct anv_cmd_buffer *cmd_buffer = (struct anv_cmd_buffer *) cmdBuffer;
2300 struct anv_pipeline_layout *layout = cmd_buffer->pipeline->layout;
2301 struct anv_bindings *bindings = cmd_buffer->bindings;
2302
2303 uint32_t offset = 0;
2304 for (uint32_t i = 0; i < setCount; i++) {
2305 struct anv_descriptor_set *set =
2306 (struct anv_descriptor_set *) pDescriptorSets[i];
2307 struct anv_descriptor_set_layout *set_layout = layout->set[firstSet + i].layout;
2308
2309 for (uint32_t s = 0; s < VK_NUM_SHADER_STAGE; s++) {
2310 uint32_t *surface_to_desc = set_layout->stage[s].surface_start;
2311 uint32_t *sampler_to_desc = set_layout->stage[s].sampler_start;
2312 uint32_t bias = s == VK_SHADER_STAGE_FRAGMENT ? MAX_RTS : 0;
2313 uint32_t start;
2314
2315 start = bias + layout->set[firstSet + i].surface_start[s];
2316 for (uint32_t b = 0; b < set_layout->stage[s].surface_count; b++) {
2317 struct anv_surface_view *view = set->descriptors[surface_to_desc[b]].view;
2318
2319 bindings->descriptors[s].surfaces[start + b] =
2320 view->surface_state.offset;
2321 bindings->descriptors[s].relocs[start + b].bo = view->bo;
2322 bindings->descriptors[s].relocs[start + b].offset = view->offset;
2323 }
2324
2325 start = layout->set[firstSet + i].sampler_start[s];
2326 for (uint32_t b = 0; b < set_layout->stage[s].sampler_count; b++) {
2327 struct anv_sampler *sampler = set->descriptors[sampler_to_desc[b]].sampler;
2328
2329 memcpy(&bindings->descriptors[s].samplers[start + b],
2330 sampler->state, sizeof(sampler->state));
2331 }
2332 }
2333
2334 offset += layout->set[firstSet + i].layout->num_dynamic_buffers;
2335 }
2336
2337 cmd_buffer->dirty |= ANV_CMD_BUFFER_DESCRIPTOR_SET_DIRTY;
2338 }
2339
2340 void anv_CmdBindIndexBuffer(
2341 VkCmdBuffer cmdBuffer,
2342 VkBuffer _buffer,
2343 VkDeviceSize offset,
2344 VkIndexType indexType)
2345 {
2346 struct anv_cmd_buffer *cmd_buffer = (struct anv_cmd_buffer *) cmdBuffer;
2347 struct anv_buffer *buffer = (struct anv_buffer *) _buffer;
2348
2349 static const uint32_t vk_to_gen_index_type[] = {
2350 [VK_INDEX_TYPE_UINT8] = INDEX_BYTE,
2351 [VK_INDEX_TYPE_UINT16] = INDEX_WORD,
2352 [VK_INDEX_TYPE_UINT32] = INDEX_DWORD,
2353 };
2354
2355 anv_batch_emit(&cmd_buffer->batch, GEN8_3DSTATE_INDEX_BUFFER,
2356 .IndexFormat = vk_to_gen_index_type[indexType],
2357 .MemoryObjectControlState = 0,
2358 .BufferStartingAddress = { buffer->bo, buffer->offset + offset },
2359 .BufferSize = buffer->size - offset);
2360 }
2361
2362 void anv_CmdBindVertexBuffers(
2363 VkCmdBuffer cmdBuffer,
2364 uint32_t startBinding,
2365 uint32_t bindingCount,
2366 const VkBuffer* pBuffers,
2367 const VkDeviceSize* pOffsets)
2368 {
2369 struct anv_cmd_buffer *cmd_buffer = (struct anv_cmd_buffer *) cmdBuffer;
2370 struct anv_bindings *bindings = cmd_buffer->bindings;
2371
2372 /* We have to defer setting up vertex buffer since we need the buffer
2373 * stride from the pipeline. */
2374
2375 for (uint32_t i = 0; i < bindingCount; i++) {
2376 bindings->vb[startBinding + i].buffer = (struct anv_buffer *) pBuffers[i];
2377 bindings->vb[startBinding + i].offset = pOffsets[i];
2378 cmd_buffer->vb_dirty |= 1 << (startBinding + i);
2379 }
2380 }
2381
2382 static void
2383 flush_descriptor_sets(struct anv_cmd_buffer *cmd_buffer)
2384 {
2385 struct anv_pipeline_layout *layout = cmd_buffer->pipeline->layout;
2386 struct anv_bindings *bindings = cmd_buffer->bindings;
2387 uint32_t layers = cmd_buffer->framebuffer->layers;
2388
2389 for (uint32_t s = 0; s < VK_NUM_SHADER_STAGE; s++) {
2390 uint32_t bias;
2391
2392 if (s == VK_SHADER_STAGE_FRAGMENT) {
2393 bias = MAX_RTS;
2394 layers = cmd_buffer->framebuffer->layers;
2395 } else {
2396 bias = 0;
2397 layers = 0;
2398 }
2399
2400 /* This is a little awkward: layout can be NULL but we still have to
2401 * allocate and set a binding table for the PS stage for render
2402 * targets. */
2403 uint32_t surface_count = layout ? layout->stage[s].surface_count : 0;
2404
2405 if (layers + surface_count > 0) {
2406 struct anv_state state;
2407 uint32_t size;
2408
2409 size = (bias + surface_count) * sizeof(uint32_t);
2410 state = anv_state_stream_alloc(&cmd_buffer->surface_state_stream, size, 32);
2411 memcpy(state.map, bindings->descriptors[s].surfaces, size);
2412
2413 for (uint32_t i = 0; i < layers; i++)
2414 anv_reloc_list_add(&cmd_buffer->batch.surf_relocs,
2415 bindings->descriptors[s].surfaces[i] + 8 * sizeof(int32_t),
2416 bindings->descriptors[s].relocs[i].bo,
2417 bindings->descriptors[s].relocs[i].offset);
2418
2419 for (uint32_t i = 0; i < surface_count; i++)
2420 anv_reloc_list_add(&cmd_buffer->batch.surf_relocs,
2421 bindings->descriptors[s].surfaces[bias + i] + 8 * sizeof(int32_t),
2422 bindings->descriptors[s].relocs[bias + i].bo,
2423 bindings->descriptors[s].relocs[bias + i].offset);
2424
2425 static const uint32_t binding_table_opcodes[] = {
2426 [VK_SHADER_STAGE_VERTEX] = 38,
2427 [VK_SHADER_STAGE_TESS_CONTROL] = 39,
2428 [VK_SHADER_STAGE_TESS_EVALUATION] = 40,
2429 [VK_SHADER_STAGE_GEOMETRY] = 41,
2430 [VK_SHADER_STAGE_FRAGMENT] = 42,
2431 [VK_SHADER_STAGE_COMPUTE] = 0,
2432 };
2433
2434 anv_batch_emit(&cmd_buffer->batch,
2435 GEN8_3DSTATE_BINDING_TABLE_POINTERS_VS,
2436 ._3DCommandSubOpcode = binding_table_opcodes[s],
2437 .PointertoVSBindingTable = state.offset);
2438 }
2439
2440 if (layout && layout->stage[s].sampler_count > 0) {
2441 struct anv_state state;
2442 size_t size;
2443
2444 size = layout->stage[s].sampler_count * 16;
2445 state = anv_state_stream_alloc(&cmd_buffer->dynamic_state_stream, size, 32);
2446 memcpy(state.map, bindings->descriptors[s].samplers, size);
2447
2448 static const uint32_t sampler_state_opcodes[] = {
2449 [VK_SHADER_STAGE_VERTEX] = 43,
2450 [VK_SHADER_STAGE_TESS_CONTROL] = 44, /* HS */
2451 [VK_SHADER_STAGE_TESS_EVALUATION] = 45, /* DS */
2452 [VK_SHADER_STAGE_GEOMETRY] = 46,
2453 [VK_SHADER_STAGE_FRAGMENT] = 47,
2454 [VK_SHADER_STAGE_COMPUTE] = 0,
2455 };
2456
2457 anv_batch_emit(&cmd_buffer->batch,
2458 GEN8_3DSTATE_SAMPLER_STATE_POINTERS_VS,
2459 ._3DCommandSubOpcode = sampler_state_opcodes[s],
2460 .PointertoVSSamplerState = state.offset);
2461 }
2462 }
2463 }
2464
2465 static void
2466 anv_cmd_buffer_flush_state(struct anv_cmd_buffer *cmd_buffer)
2467 {
2468 struct anv_pipeline *pipeline = cmd_buffer->pipeline;
2469 struct anv_bindings *bindings = cmd_buffer->bindings;
2470 const uint32_t num_buffers = __builtin_popcount(cmd_buffer->vb_dirty);
2471 const uint32_t num_dwords = 1 + num_buffers * 4;
2472 uint32_t *p;
2473
2474 if (cmd_buffer->vb_dirty) {
2475 p = anv_batch_emitn(&cmd_buffer->batch, num_dwords,
2476 GEN8_3DSTATE_VERTEX_BUFFERS);
2477 uint32_t vb, i = 0;
2478 for_each_bit(vb, cmd_buffer->vb_dirty) {
2479 struct anv_buffer *buffer = bindings->vb[vb].buffer;
2480 uint32_t offset = bindings->vb[vb].offset;
2481
2482 struct GEN8_VERTEX_BUFFER_STATE state = {
2483 .VertexBufferIndex = vb,
2484 .MemoryObjectControlState = 0,
2485 .AddressModifyEnable = true,
2486 .BufferPitch = pipeline->binding_stride[vb],
2487 .BufferStartingAddress = { buffer->bo, buffer->offset + offset },
2488 .BufferSize = buffer->size - offset
2489 };
2490
2491 GEN8_VERTEX_BUFFER_STATE_pack(&cmd_buffer->batch, &p[1 + i * 4], &state);
2492 i++;
2493 }
2494 }
2495
2496 if (cmd_buffer->dirty & ANV_CMD_BUFFER_PIPELINE_DIRTY)
2497 anv_batch_emit_batch(&cmd_buffer->batch, &pipeline->batch);
2498
2499 if (cmd_buffer->dirty & ANV_CMD_BUFFER_DESCRIPTOR_SET_DIRTY)
2500 flush_descriptor_sets(cmd_buffer);
2501
2502 if (cmd_buffer->dirty & (ANV_CMD_BUFFER_PIPELINE_DIRTY | ANV_CMD_BUFFER_RS_DIRTY))
2503 anv_batch_emit_merge(&cmd_buffer->batch,
2504 cmd_buffer->rs_state->state_sf, pipeline->state_sf);
2505
2506 cmd_buffer->vb_dirty = 0;
2507 cmd_buffer->dirty = 0;
2508 }
2509
2510 void anv_CmdDraw(
2511 VkCmdBuffer cmdBuffer,
2512 uint32_t firstVertex,
2513 uint32_t vertexCount,
2514 uint32_t firstInstance,
2515 uint32_t instanceCount)
2516 {
2517 struct anv_cmd_buffer *cmd_buffer = (struct anv_cmd_buffer *) cmdBuffer;
2518
2519 anv_cmd_buffer_flush_state(cmd_buffer);
2520
2521 anv_batch_emit(&cmd_buffer->batch, GEN8_3DPRIMITIVE,
2522 .VertexAccessType = SEQUENTIAL,
2523 .VertexCountPerInstance = vertexCount,
2524 .StartVertexLocation = firstVertex,
2525 .InstanceCount = instanceCount,
2526 .StartInstanceLocation = firstInstance,
2527 .BaseVertexLocation = 0);
2528 }
2529
2530 void anv_CmdDrawIndexed(
2531 VkCmdBuffer cmdBuffer,
2532 uint32_t firstIndex,
2533 uint32_t indexCount,
2534 int32_t vertexOffset,
2535 uint32_t firstInstance,
2536 uint32_t instanceCount)
2537 {
2538 struct anv_cmd_buffer *cmd_buffer = (struct anv_cmd_buffer *) cmdBuffer;
2539
2540 anv_cmd_buffer_flush_state(cmd_buffer);
2541
2542 anv_batch_emit(&cmd_buffer->batch, GEN8_3DPRIMITIVE,
2543 .VertexAccessType = RANDOM,
2544 .VertexCountPerInstance = indexCount,
2545 .StartVertexLocation = firstIndex,
2546 .InstanceCount = instanceCount,
2547 .StartInstanceLocation = firstInstance,
2548 .BaseVertexLocation = 0);
2549 }
2550
2551 static void
2552 anv_batch_lrm(struct anv_batch *batch,
2553 uint32_t reg, struct anv_bo *bo, uint32_t offset)
2554 {
2555 anv_batch_emit(batch, GEN8_MI_LOAD_REGISTER_MEM,
2556 .RegisterAddress = reg,
2557 .MemoryAddress = { bo, offset });
2558 }
2559
2560 static void
2561 anv_batch_lri(struct anv_batch *batch, uint32_t reg, uint32_t imm)
2562 {
2563 anv_batch_emit(batch, GEN8_MI_LOAD_REGISTER_IMM,
2564 .RegisterOffset = reg,
2565 .DataDWord = imm);
2566 }
2567
2568 /* Auto-Draw / Indirect Registers */
2569 #define GEN7_3DPRIM_END_OFFSET 0x2420
2570 #define GEN7_3DPRIM_START_VERTEX 0x2430
2571 #define GEN7_3DPRIM_VERTEX_COUNT 0x2434
2572 #define GEN7_3DPRIM_INSTANCE_COUNT 0x2438
2573 #define GEN7_3DPRIM_START_INSTANCE 0x243C
2574 #define GEN7_3DPRIM_BASE_VERTEX 0x2440
2575
2576 void anv_CmdDrawIndirect(
2577 VkCmdBuffer cmdBuffer,
2578 VkBuffer _buffer,
2579 VkDeviceSize offset,
2580 uint32_t count,
2581 uint32_t stride)
2582 {
2583 struct anv_cmd_buffer *cmd_buffer = (struct anv_cmd_buffer *) cmdBuffer;
2584 struct anv_buffer *buffer = (struct anv_buffer *) _buffer;
2585 struct anv_bo *bo = buffer->bo;
2586 uint32_t bo_offset = buffer->offset + offset;
2587
2588 anv_cmd_buffer_flush_state(cmd_buffer);
2589
2590 anv_batch_lrm(&cmd_buffer->batch, GEN7_3DPRIM_VERTEX_COUNT, bo, bo_offset);
2591 anv_batch_lrm(&cmd_buffer->batch, GEN7_3DPRIM_INSTANCE_COUNT, bo, bo_offset + 4);
2592 anv_batch_lrm(&cmd_buffer->batch, GEN7_3DPRIM_START_VERTEX, bo, bo_offset + 8);
2593 anv_batch_lrm(&cmd_buffer->batch, GEN7_3DPRIM_START_INSTANCE, bo, bo_offset + 12);
2594 anv_batch_lri(&cmd_buffer->batch, GEN7_3DPRIM_BASE_VERTEX, 0);
2595
2596 anv_batch_emit(&cmd_buffer->batch, GEN8_3DPRIMITIVE,
2597 .IndirectParameterEnable = true,
2598 .VertexAccessType = SEQUENTIAL);
2599 }
2600
2601 void anv_CmdDrawIndexedIndirect(
2602 VkCmdBuffer cmdBuffer,
2603 VkBuffer _buffer,
2604 VkDeviceSize offset,
2605 uint32_t count,
2606 uint32_t stride)
2607 {
2608 struct anv_cmd_buffer *cmd_buffer = (struct anv_cmd_buffer *) cmdBuffer;
2609 struct anv_buffer *buffer = (struct anv_buffer *) _buffer;
2610 struct anv_bo *bo = buffer->bo;
2611 uint32_t bo_offset = buffer->offset + offset;
2612
2613 anv_cmd_buffer_flush_state(cmd_buffer);
2614
2615 anv_batch_lrm(&cmd_buffer->batch, GEN7_3DPRIM_VERTEX_COUNT, bo, bo_offset);
2616 anv_batch_lrm(&cmd_buffer->batch, GEN7_3DPRIM_INSTANCE_COUNT, bo, bo_offset + 4);
2617 anv_batch_lrm(&cmd_buffer->batch, GEN7_3DPRIM_START_VERTEX, bo, bo_offset + 8);
2618 anv_batch_lrm(&cmd_buffer->batch, GEN7_3DPRIM_BASE_VERTEX, bo, bo_offset + 12);
2619 anv_batch_lrm(&cmd_buffer->batch, GEN7_3DPRIM_START_INSTANCE, bo, bo_offset + 16);
2620
2621 anv_batch_emit(&cmd_buffer->batch, GEN8_3DPRIMITIVE,
2622 .IndirectParameterEnable = true,
2623 .VertexAccessType = RANDOM);
2624 }
2625
2626 void anv_CmdDispatch(
2627 VkCmdBuffer cmdBuffer,
2628 uint32_t x,
2629 uint32_t y,
2630 uint32_t z)
2631 {
2632 stub();
2633 }
2634
2635 void anv_CmdDispatchIndirect(
2636 VkCmdBuffer cmdBuffer,
2637 VkBuffer buffer,
2638 VkDeviceSize offset)
2639 {
2640 stub();
2641 }
2642
2643 void anv_CmdSetEvent(
2644 VkCmdBuffer cmdBuffer,
2645 VkEvent event,
2646 VkPipeEvent pipeEvent)
2647 {
2648 stub();
2649 }
2650
2651 void anv_CmdResetEvent(
2652 VkCmdBuffer cmdBuffer,
2653 VkEvent event,
2654 VkPipeEvent pipeEvent)
2655 {
2656 stub();
2657 }
2658
2659 void anv_CmdWaitEvents(
2660 VkCmdBuffer cmdBuffer,
2661 VkWaitEvent waitEvent,
2662 uint32_t eventCount,
2663 const VkEvent* pEvents,
2664 uint32_t memBarrierCount,
2665 const void** ppMemBarriers)
2666 {
2667 stub();
2668 }
2669
2670 void anv_CmdPipelineBarrier(
2671 VkCmdBuffer cmdBuffer,
2672 VkWaitEvent waitEvent,
2673 uint32_t pipeEventCount,
2674 const VkPipeEvent* pPipeEvents,
2675 uint32_t memBarrierCount,
2676 const void** ppMemBarriers)
2677 {
2678 stub();
2679 }
2680
2681 static void
2682 anv_batch_emit_ps_depth_count(struct anv_batch *batch,
2683 struct anv_bo *bo, uint32_t offset)
2684 {
2685 anv_batch_emit(batch, GEN8_PIPE_CONTROL,
2686 .DestinationAddressType = DAT_PPGTT,
2687 .PostSyncOperation = WritePSDepthCount,
2688 .Address = { bo, offset }); /* FIXME: This is only lower 32 bits */
2689 }
2690
2691 void anv_CmdBeginQuery(
2692 VkCmdBuffer cmdBuffer,
2693 VkQueryPool queryPool,
2694 uint32_t slot,
2695 VkQueryControlFlags flags)
2696 {
2697 struct anv_cmd_buffer *cmd_buffer = (struct anv_cmd_buffer *) cmdBuffer;
2698 struct anv_query_pool *pool = (struct anv_query_pool *) queryPool;
2699
2700 switch (pool->type) {
2701 case VK_QUERY_TYPE_OCCLUSION:
2702 anv_batch_emit_ps_depth_count(&cmd_buffer->batch, &pool->bo, slot * 16);
2703 break;
2704
2705 case VK_QUERY_TYPE_PIPELINE_STATISTICS:
2706 break;
2707
2708 default:
2709 break;
2710 }
2711 }
2712
2713 void anv_CmdEndQuery(
2714 VkCmdBuffer cmdBuffer,
2715 VkQueryPool queryPool,
2716 uint32_t slot)
2717 {
2718 struct anv_cmd_buffer *cmd_buffer = (struct anv_cmd_buffer *) cmdBuffer;
2719 struct anv_query_pool *pool = (struct anv_query_pool *) queryPool;
2720
2721 switch (pool->type) {
2722 case VK_QUERY_TYPE_OCCLUSION:
2723 anv_batch_emit_ps_depth_count(&cmd_buffer->batch, &pool->bo, slot * 16 + 8);
2724 break;
2725
2726 case VK_QUERY_TYPE_PIPELINE_STATISTICS:
2727 break;
2728
2729 default:
2730 break;
2731 }
2732 }
2733
2734 void anv_CmdResetQueryPool(
2735 VkCmdBuffer cmdBuffer,
2736 VkQueryPool queryPool,
2737 uint32_t startQuery,
2738 uint32_t queryCount)
2739 {
2740 stub();
2741 }
2742
2743 #define TIMESTAMP 0x44070
2744
2745 void anv_CmdWriteTimestamp(
2746 VkCmdBuffer cmdBuffer,
2747 VkTimestampType timestampType,
2748 VkBuffer destBuffer,
2749 VkDeviceSize destOffset)
2750 {
2751 struct anv_cmd_buffer *cmd_buffer = (struct anv_cmd_buffer *) cmdBuffer;
2752 struct anv_buffer *buffer = (struct anv_buffer *) destBuffer;
2753 struct anv_bo *bo = buffer->bo;
2754
2755 switch (timestampType) {
2756 case VK_TIMESTAMP_TYPE_TOP:
2757 anv_batch_emit(&cmd_buffer->batch, GEN8_MI_STORE_REGISTER_MEM,
2758 .RegisterAddress = TIMESTAMP,
2759 .MemoryAddress = { bo, buffer->offset + destOffset });
2760 break;
2761
2762 case VK_TIMESTAMP_TYPE_BOTTOM:
2763 anv_batch_emit(&cmd_buffer->batch, GEN8_PIPE_CONTROL,
2764 .DestinationAddressType = DAT_PPGTT,
2765 .PostSyncOperation = WriteTimestamp,
2766 .Address = /* FIXME: This is only lower 32 bits */
2767 { bo, buffer->offset + destOffset });
2768 break;
2769
2770 default:
2771 break;
2772 }
2773 }
2774
2775 void anv_CmdCopyQueryPoolResults(
2776 VkCmdBuffer cmdBuffer,
2777 VkQueryPool queryPool,
2778 uint32_t startQuery,
2779 uint32_t queryCount,
2780 VkBuffer destBuffer,
2781 VkDeviceSize destOffset,
2782 VkDeviceSize destStride,
2783 VkQueryResultFlags flags)
2784 {
2785 stub();
2786 }
2787
2788 void anv_CmdInitAtomicCounters(
2789 VkCmdBuffer cmdBuffer,
2790 VkPipelineBindPoint pipelineBindPoint,
2791 uint32_t startCounter,
2792 uint32_t counterCount,
2793 const uint32_t* pData)
2794 {
2795 stub();
2796 }
2797
2798 void anv_CmdLoadAtomicCounters(
2799 VkCmdBuffer cmdBuffer,
2800 VkPipelineBindPoint pipelineBindPoint,
2801 uint32_t startCounter,
2802 uint32_t counterCount,
2803 VkBuffer srcBuffer,
2804 VkDeviceSize srcOffset)
2805 {
2806 stub();
2807 }
2808
2809 void anv_CmdSaveAtomicCounters(
2810 VkCmdBuffer cmdBuffer,
2811 VkPipelineBindPoint pipelineBindPoint,
2812 uint32_t startCounter,
2813 uint32_t counterCount,
2814 VkBuffer destBuffer,
2815 VkDeviceSize destOffset)
2816 {
2817 stub();
2818 }
2819
2820 VkResult anv_CreateFramebuffer(
2821 VkDevice _device,
2822 const VkFramebufferCreateInfo* pCreateInfo,
2823 VkFramebuffer* pFramebuffer)
2824 {
2825 struct anv_device *device = (struct anv_device *) _device;
2826 struct anv_framebuffer *framebuffer;
2827
2828 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO);
2829
2830 framebuffer = anv_device_alloc(device, sizeof(*framebuffer), 8,
2831 VK_SYSTEM_ALLOC_TYPE_API_OBJECT);
2832 if (framebuffer == NULL)
2833 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
2834
2835 framebuffer->color_attachment_count = pCreateInfo->colorAttachmentCount;
2836 for (uint32_t i = 0; i < pCreateInfo->colorAttachmentCount; i++) {
2837 framebuffer->color_attachments[i] =
2838 (struct anv_surface_view *) pCreateInfo->pColorAttachments[i].view;
2839 }
2840
2841 if (pCreateInfo->pDepthStencilAttachment) {
2842 framebuffer->depth_stencil =
2843 (struct anv_depth_stencil_view *) pCreateInfo->pDepthStencilAttachment->view;
2844 }
2845
2846 framebuffer->sample_count = pCreateInfo->sampleCount;
2847 framebuffer->width = pCreateInfo->width;
2848 framebuffer->height = pCreateInfo->height;
2849 framebuffer->layers = pCreateInfo->layers;
2850
2851 vkCreateDynamicViewportState((VkDevice) device,
2852 &(VkDynamicVpStateCreateInfo) {
2853 .sType = VK_STRUCTURE_TYPE_DYNAMIC_VP_STATE_CREATE_INFO,
2854 .viewportAndScissorCount = 2,
2855 .pViewports = (VkViewport[]) {
2856 {
2857 .originX = 0,
2858 .originY = 0,
2859 .width = pCreateInfo->width,
2860 .height = pCreateInfo->height,
2861 .minDepth = 0,
2862 .maxDepth = 1
2863 },
2864 },
2865 .pScissors = (VkRect[]) {
2866 { { 0, 0 },
2867 { pCreateInfo->width, pCreateInfo->height } },
2868 }
2869 },
2870 &framebuffer->vp_state);
2871
2872 *pFramebuffer = (VkFramebuffer) framebuffer;
2873
2874 return VK_SUCCESS;
2875 }
2876
2877 VkResult anv_CreateRenderPass(
2878 VkDevice _device,
2879 const VkRenderPassCreateInfo* pCreateInfo,
2880 VkRenderPass* pRenderPass)
2881 {
2882 struct anv_device *device = (struct anv_device *) _device;
2883 struct anv_render_pass *pass;
2884 size_t size;
2885
2886 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO);
2887
2888 size = sizeof(*pass) +
2889 pCreateInfo->layers * sizeof(struct anv_render_pass_layer);
2890 pass = anv_device_alloc(device, size, 8,
2891 VK_SYSTEM_ALLOC_TYPE_API_OBJECT);
2892 if (pass == NULL)
2893 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
2894
2895 pass->render_area = pCreateInfo->renderArea;
2896
2897 pass->num_layers = pCreateInfo->layers;
2898
2899 pass->num_clear_layers = 0;
2900 for (uint32_t i = 0; i < pCreateInfo->layers; i++) {
2901 pass->layers[i].color_load_op = pCreateInfo->pColorLoadOps[i];
2902 pass->layers[i].clear_color = pCreateInfo->pColorLoadClearValues[i];
2903 if (pass->layers[i].color_load_op == VK_ATTACHMENT_LOAD_OP_CLEAR)
2904 pass->num_clear_layers++;
2905 }
2906
2907 *pRenderPass = (VkRenderPass) pass;
2908
2909 return VK_SUCCESS;
2910 }
2911
2912 void
2913 anv_cmd_buffer_fill_render_targets(struct anv_cmd_buffer *cmd_buffer)
2914 {
2915 struct anv_framebuffer *framebuffer = cmd_buffer->framebuffer;
2916 struct anv_bindings *bindings = cmd_buffer->bindings;
2917
2918 for (uint32_t i = 0; i < framebuffer->color_attachment_count; i++) {
2919 struct anv_surface_view *view = framebuffer->color_attachments[i];
2920
2921 bindings->descriptors[VK_SHADER_STAGE_FRAGMENT].surfaces[i] = view->surface_state.offset;
2922 bindings->descriptors[VK_SHADER_STAGE_FRAGMENT].relocs[i].bo = view->bo;
2923 bindings->descriptors[VK_SHADER_STAGE_FRAGMENT].relocs[i].offset = view->offset;
2924 }
2925 cmd_buffer->dirty |= ANV_CMD_BUFFER_DESCRIPTOR_SET_DIRTY;
2926 }
2927
2928 void anv_CmdBeginRenderPass(
2929 VkCmdBuffer cmdBuffer,
2930 const VkRenderPassBegin* pRenderPassBegin)
2931 {
2932 struct anv_cmd_buffer *cmd_buffer = (struct anv_cmd_buffer *) cmdBuffer;
2933 struct anv_render_pass *pass = (struct anv_render_pass *) pRenderPassBegin->renderPass;
2934 struct anv_framebuffer *framebuffer =
2935 (struct anv_framebuffer *) pRenderPassBegin->framebuffer;
2936
2937 cmd_buffer->framebuffer = framebuffer;
2938
2939 anv_batch_emit(&cmd_buffer->batch, GEN8_3DSTATE_DRAWING_RECTANGLE,
2940 .ClippedDrawingRectangleYMin = pass->render_area.offset.y,
2941 .ClippedDrawingRectangleXMin = pass->render_area.offset.x,
2942 .ClippedDrawingRectangleYMax =
2943 pass->render_area.offset.y + pass->render_area.extent.height - 1,
2944 .ClippedDrawingRectangleXMax =
2945 pass->render_area.offset.x + pass->render_area.extent.width - 1,
2946 .DrawingRectangleOriginY = 0,
2947 .DrawingRectangleOriginX = 0);
2948
2949 anv_cmd_buffer_fill_render_targets(cmd_buffer);
2950
2951 anv_cmd_buffer_clear(cmd_buffer, pass);
2952 }
2953
2954 void anv_CmdEndRenderPass(
2955 VkCmdBuffer cmdBuffer,
2956 VkRenderPass renderPass)
2957 {
2958 /* Emit a flushing pipe control at the end of a pass. This is kind of a
2959 * hack but it ensures that render targets always actually get written.
2960 * Eventually, we should do flushing based on image format transitions
2961 * or something of that nature.
2962 */
2963 struct anv_cmd_buffer *cmd_buffer = (struct anv_cmd_buffer *)cmdBuffer;
2964 anv_batch_emit(&cmd_buffer->batch, GEN8_PIPE_CONTROL,
2965 .PostSyncOperation = NoWrite,
2966 .RenderTargetCacheFlushEnable = true,
2967 .InstructionCacheInvalidateEnable = true,
2968 .DepthCacheFlushEnable = true,
2969 .VFCacheInvalidationEnable = true,
2970 .TextureCacheInvalidationEnable = true,
2971 .CommandStreamerStallEnable = true);
2972
2973 stub();
2974 }