vk: Fix vkGetOjectInfo return values
[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, pData);
1053 return VK_SUCCESS;
1054
1055 case VK_OBJECT_INFO_TYPE_MEMORY_ALLOCATION_COUNT:
1056 *pDataSize = sizeof(count);
1057 if (pData == NULL)
1058 return VK_SUCCESS;
1059
1060 count = pData;
1061 *count = get_allocation_count(objType);
1062 return VK_SUCCESS;
1063
1064 default:
1065 return VK_UNSUPPORTED;
1066 }
1067
1068 }
1069
1070 VkResult anv_QueueBindObjectMemory(
1071 VkQueue queue,
1072 VkObjectType objType,
1073 VkObject object,
1074 uint32_t allocationIdx,
1075 VkDeviceMemory _mem,
1076 VkDeviceSize memOffset)
1077 {
1078 struct anv_buffer *buffer;
1079 struct anv_image *image;
1080 struct anv_device_memory *mem = (struct anv_device_memory *) _mem;
1081
1082 switch (objType) {
1083 case VK_OBJECT_TYPE_BUFFER:
1084 buffer = (struct anv_buffer *) object;
1085 buffer->bo = &mem->bo;
1086 buffer->offset = memOffset;
1087 break;
1088 case VK_OBJECT_TYPE_IMAGE:
1089 image = (struct anv_image *) object;
1090 image->bo = &mem->bo;
1091 image->offset = memOffset;
1092 break;
1093 default:
1094 break;
1095 }
1096
1097 return VK_SUCCESS;
1098 }
1099
1100 VkResult anv_QueueBindObjectMemoryRange(
1101 VkQueue queue,
1102 VkObjectType objType,
1103 VkObject object,
1104 uint32_t allocationIdx,
1105 VkDeviceSize rangeOffset,
1106 VkDeviceSize rangeSize,
1107 VkDeviceMemory mem,
1108 VkDeviceSize memOffset)
1109 {
1110 stub_return(VK_UNSUPPORTED);
1111 }
1112
1113 VkResult anv_QueueBindImageMemoryRange(
1114 VkQueue queue,
1115 VkImage image,
1116 uint32_t allocationIdx,
1117 const VkImageMemoryBindInfo* pBindInfo,
1118 VkDeviceMemory mem,
1119 VkDeviceSize memOffset)
1120 {
1121 stub_return(VK_UNSUPPORTED);
1122 }
1123
1124 VkResult anv_CreateFence(
1125 VkDevice _device,
1126 const VkFenceCreateInfo* pCreateInfo,
1127 VkFence* pFence)
1128 {
1129 struct anv_device *device = (struct anv_device *) _device;
1130 struct anv_fence *fence;
1131 struct anv_batch batch;
1132 VkResult result;
1133
1134 const uint32_t fence_size = 128;
1135
1136 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_FENCE_CREATE_INFO);
1137
1138 fence = anv_device_alloc(device, sizeof(*fence), 8,
1139 VK_SYSTEM_ALLOC_TYPE_API_OBJECT);
1140 if (fence == NULL)
1141 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
1142
1143 result = anv_bo_init_new(&fence->bo, device, fence_size);
1144 if (result != VK_SUCCESS)
1145 goto fail;
1146
1147 fence->bo.map =
1148 anv_gem_mmap(device, fence->bo.gem_handle, 0, fence->bo.size);
1149 batch.next = fence->bo.map;
1150 anv_batch_emit(&batch, GEN8_MI_BATCH_BUFFER_END);
1151 anv_batch_emit(&batch, GEN8_MI_NOOP);
1152
1153 fence->exec2_objects[0].handle = fence->bo.gem_handle;
1154 fence->exec2_objects[0].relocation_count = 0;
1155 fence->exec2_objects[0].relocs_ptr = 0;
1156 fence->exec2_objects[0].alignment = 0;
1157 fence->exec2_objects[0].offset = fence->bo.offset;
1158 fence->exec2_objects[0].flags = 0;
1159 fence->exec2_objects[0].rsvd1 = 0;
1160 fence->exec2_objects[0].rsvd2 = 0;
1161
1162 fence->execbuf.buffers_ptr = (uintptr_t) fence->exec2_objects;
1163 fence->execbuf.buffer_count = 1;
1164 fence->execbuf.batch_start_offset = 0;
1165 fence->execbuf.batch_len = batch.next - fence->bo.map;
1166 fence->execbuf.cliprects_ptr = 0;
1167 fence->execbuf.num_cliprects = 0;
1168 fence->execbuf.DR1 = 0;
1169 fence->execbuf.DR4 = 0;
1170
1171 fence->execbuf.flags =
1172 I915_EXEC_HANDLE_LUT | I915_EXEC_NO_RELOC | I915_EXEC_RENDER;
1173 fence->execbuf.rsvd1 = device->context_id;
1174 fence->execbuf.rsvd2 = 0;
1175
1176 *pFence = (VkQueryPool) fence;
1177
1178 return VK_SUCCESS;
1179
1180 fail:
1181 anv_device_free(device, fence);
1182
1183 return result;
1184 }
1185
1186 VkResult anv_ResetFences(
1187 VkDevice _device,
1188 uint32_t fenceCount,
1189 VkFence* pFences)
1190 {
1191 struct anv_fence **fences = (struct anv_fence **) pFences;
1192
1193 for (uint32_t i; i < fenceCount; i++)
1194 fences[i]->ready = false;
1195
1196 return VK_SUCCESS;
1197 }
1198
1199 VkResult anv_GetFenceStatus(
1200 VkDevice _device,
1201 VkFence _fence)
1202 {
1203 struct anv_device *device = (struct anv_device *) _device;
1204 struct anv_fence *fence = (struct anv_fence *) _fence;
1205 int64_t t = 0;
1206 int ret;
1207
1208 if (fence->ready)
1209 return VK_SUCCESS;
1210
1211 ret = anv_gem_wait(device, fence->bo.gem_handle, &t);
1212 if (ret == 0) {
1213 fence->ready = true;
1214 return VK_SUCCESS;
1215 }
1216
1217 return VK_NOT_READY;
1218 }
1219
1220 VkResult anv_WaitForFences(
1221 VkDevice _device,
1222 uint32_t fenceCount,
1223 const VkFence* pFences,
1224 bool32_t waitAll,
1225 uint64_t timeout)
1226 {
1227 struct anv_device *device = (struct anv_device *) _device;
1228 struct anv_fence **fences = (struct anv_fence **) pFences;
1229 int64_t t = timeout;
1230 int ret;
1231
1232 /* FIXME: handle !waitAll */
1233
1234 for (uint32_t i = 0; i < fenceCount; i++) {
1235 ret = anv_gem_wait(device, fences[i]->bo.gem_handle, &t);
1236 if (ret == -1 && errno == ETIME)
1237 return VK_TIMEOUT;
1238 else if (ret == -1)
1239 return vk_error(VK_ERROR_UNKNOWN);
1240 }
1241
1242 return VK_SUCCESS;
1243 }
1244
1245 // Queue semaphore functions
1246
1247 VkResult anv_CreateSemaphore(
1248 VkDevice device,
1249 const VkSemaphoreCreateInfo* pCreateInfo,
1250 VkSemaphore* pSemaphore)
1251 {
1252 stub_return(VK_UNSUPPORTED);
1253 }
1254
1255 VkResult anv_QueueSignalSemaphore(
1256 VkQueue queue,
1257 VkSemaphore semaphore)
1258 {
1259 stub_return(VK_UNSUPPORTED);
1260 }
1261
1262 VkResult anv_QueueWaitSemaphore(
1263 VkQueue queue,
1264 VkSemaphore semaphore)
1265 {
1266 stub_return(VK_UNSUPPORTED);
1267 }
1268
1269 // Event functions
1270
1271 VkResult anv_CreateEvent(
1272 VkDevice device,
1273 const VkEventCreateInfo* pCreateInfo,
1274 VkEvent* pEvent)
1275 {
1276 stub_return(VK_UNSUPPORTED);
1277 }
1278
1279 VkResult anv_GetEventStatus(
1280 VkDevice device,
1281 VkEvent event)
1282 {
1283 stub_return(VK_UNSUPPORTED);
1284 }
1285
1286 VkResult anv_SetEvent(
1287 VkDevice device,
1288 VkEvent event)
1289 {
1290 stub_return(VK_UNSUPPORTED);
1291 }
1292
1293 VkResult anv_ResetEvent(
1294 VkDevice device,
1295 VkEvent event)
1296 {
1297 stub_return(VK_UNSUPPORTED);
1298 }
1299
1300 // Query functions
1301
1302 struct anv_query_pool {
1303 VkQueryType type;
1304 uint32_t slots;
1305 struct anv_bo bo;
1306 };
1307
1308 VkResult anv_CreateQueryPool(
1309 VkDevice _device,
1310 const VkQueryPoolCreateInfo* pCreateInfo,
1311 VkQueryPool* pQueryPool)
1312 {
1313 struct anv_device *device = (struct anv_device *) _device;
1314 struct anv_query_pool *pool;
1315 VkResult result;
1316
1317 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_QUERY_POOL_CREATE_INFO);
1318
1319 pool = anv_device_alloc(device, sizeof(*pool), 8,
1320 VK_SYSTEM_ALLOC_TYPE_API_OBJECT);
1321 if (pool == NULL)
1322 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
1323
1324 pool->type = pCreateInfo->queryType;
1325 result = anv_bo_init_new(&pool->bo, device, pCreateInfo->slots * 16);
1326 if (result != VK_SUCCESS)
1327 goto fail;
1328
1329 *pQueryPool = (VkQueryPool) pool;
1330
1331 return VK_SUCCESS;
1332
1333 fail:
1334 anv_device_free(device, pool);
1335
1336 return result;
1337 }
1338
1339 VkResult anv_GetQueryPoolResults(
1340 VkDevice device,
1341 VkQueryPool queryPool,
1342 uint32_t startQuery,
1343 uint32_t queryCount,
1344 size_t* pDataSize,
1345 void* pData,
1346 VkQueryResultFlags flags)
1347 {
1348 stub_return(VK_UNSUPPORTED);
1349 }
1350
1351 // Buffer functions
1352
1353 VkResult anv_CreateBuffer(
1354 VkDevice _device,
1355 const VkBufferCreateInfo* pCreateInfo,
1356 VkBuffer* pBuffer)
1357 {
1358 struct anv_device *device = (struct anv_device *) _device;
1359 struct anv_buffer *buffer;
1360
1361 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO);
1362
1363 buffer = anv_device_alloc(device, sizeof(*buffer), 8,
1364 VK_SYSTEM_ALLOC_TYPE_API_OBJECT);
1365 if (buffer == NULL)
1366 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
1367
1368 buffer->size = pCreateInfo->size;
1369 buffer->bo = NULL;
1370 buffer->offset = 0;
1371
1372 *pBuffer = (VkBuffer) buffer;
1373
1374 return VK_SUCCESS;
1375 }
1376
1377 // Buffer view functions
1378
1379 VkResult anv_CreateBufferView(
1380 VkDevice _device,
1381 const VkBufferViewCreateInfo* pCreateInfo,
1382 VkBufferView* pView)
1383 {
1384 struct anv_device *device = (struct anv_device *) _device;
1385 struct anv_buffer *buffer = (struct anv_buffer *) pCreateInfo->buffer;
1386 struct anv_surface_view *view;
1387 const struct anv_format *format;
1388
1389 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO);
1390
1391 view = anv_device_alloc(device, sizeof(*view), 8,
1392 VK_SYSTEM_ALLOC_TYPE_API_OBJECT);
1393 if (view == NULL)
1394 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
1395
1396 view->bo = buffer->bo;
1397 view->offset = buffer->offset + pCreateInfo->offset;
1398 view->surface_state =
1399 anv_state_pool_alloc(&device->surface_state_pool, 64, 64);
1400 view->format = pCreateInfo->format;
1401
1402 format = anv_format_for_vk_format(pCreateInfo->format);
1403 /* This assumes RGBA float format. */
1404 uint32_t stride = 4;
1405 uint32_t num_elements = pCreateInfo->range / stride;
1406 struct GEN8_RENDER_SURFACE_STATE surface_state = {
1407 .SurfaceType = SURFTYPE_BUFFER,
1408 .SurfaceArray = false,
1409 .SurfaceFormat = format->format,
1410 .SurfaceVerticalAlignment = VALIGN4,
1411 .SurfaceHorizontalAlignment = HALIGN4,
1412 .TileMode = LINEAR,
1413 .VerticalLineStride = 0,
1414 .VerticalLineStrideOffset = 0,
1415 .SamplerL2BypassModeDisable = true,
1416 .RenderCacheReadWriteMode = WriteOnlyCache,
1417 .MemoryObjectControlState = 0, /* FIXME: MOCS */
1418 .BaseMipLevel = 0,
1419 .SurfaceQPitch = 0,
1420 .Height = (num_elements >> 7) & 0x3fff,
1421 .Width = num_elements & 0x7f,
1422 .Depth = (num_elements >> 21) & 0x3f,
1423 .SurfacePitch = stride - 1,
1424 .MinimumArrayElement = 0,
1425 .NumberofMultisamples = MULTISAMPLECOUNT_1,
1426 .XOffset = 0,
1427 .YOffset = 0,
1428 .SurfaceMinLOD = 0,
1429 .MIPCountLOD = 0,
1430 .AuxiliarySurfaceMode = AUX_NONE,
1431 .RedClearColor = 0,
1432 .GreenClearColor = 0,
1433 .BlueClearColor = 0,
1434 .AlphaClearColor = 0,
1435 .ShaderChannelSelectRed = SCS_RED,
1436 .ShaderChannelSelectGreen = SCS_GREEN,
1437 .ShaderChannelSelectBlue = SCS_BLUE,
1438 .ShaderChannelSelectAlpha = SCS_ALPHA,
1439 .ResourceMinLOD = 0,
1440 /* FIXME: We assume that the image must be bound at this time. */
1441 .SurfaceBaseAddress = { NULL, view->offset },
1442 };
1443
1444 GEN8_RENDER_SURFACE_STATE_pack(NULL, view->surface_state.map, &surface_state);
1445
1446 *pView = (VkImageView) view;
1447
1448 return VK_SUCCESS;
1449 }
1450
1451 // Sampler functions
1452
1453 VkResult anv_CreateSampler(
1454 VkDevice _device,
1455 const VkSamplerCreateInfo* pCreateInfo,
1456 VkSampler* pSampler)
1457 {
1458 struct anv_device *device = (struct anv_device *) _device;
1459 struct anv_sampler *sampler;
1460
1461 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO);
1462
1463 sampler = anv_device_alloc(device, sizeof(*sampler), 8,
1464 VK_SYSTEM_ALLOC_TYPE_API_OBJECT);
1465 if (!sampler)
1466 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
1467
1468 static const uint32_t vk_to_gen_tex_filter[] = {
1469 [VK_TEX_FILTER_NEAREST] = MAPFILTER_NEAREST,
1470 [VK_TEX_FILTER_LINEAR] = MAPFILTER_LINEAR
1471 };
1472
1473 static const uint32_t vk_to_gen_mipmap_mode[] = {
1474 [VK_TEX_MIPMAP_MODE_BASE] = MIPFILTER_NONE,
1475 [VK_TEX_MIPMAP_MODE_NEAREST] = MIPFILTER_NEAREST,
1476 [VK_TEX_MIPMAP_MODE_LINEAR] = MIPFILTER_LINEAR
1477 };
1478
1479 static const uint32_t vk_to_gen_tex_address[] = {
1480 [VK_TEX_ADDRESS_WRAP] = TCM_WRAP,
1481 [VK_TEX_ADDRESS_MIRROR] = TCM_MIRROR,
1482 [VK_TEX_ADDRESS_CLAMP] = TCM_CLAMP,
1483 [VK_TEX_ADDRESS_MIRROR_ONCE] = TCM_MIRROR_ONCE,
1484 [VK_TEX_ADDRESS_CLAMP_BORDER] = TCM_CLAMP_BORDER,
1485 };
1486
1487 static const uint32_t vk_to_gen_compare_op[] = {
1488 [VK_COMPARE_OP_NEVER] = PREFILTEROPNEVER,
1489 [VK_COMPARE_OP_LESS] = PREFILTEROPLESS,
1490 [VK_COMPARE_OP_EQUAL] = PREFILTEROPEQUAL,
1491 [VK_COMPARE_OP_LESS_EQUAL] = PREFILTEROPLEQUAL,
1492 [VK_COMPARE_OP_GREATER] = PREFILTEROPGREATER,
1493 [VK_COMPARE_OP_NOT_EQUAL] = PREFILTEROPNOTEQUAL,
1494 [VK_COMPARE_OP_GREATER_EQUAL] = PREFILTEROPGEQUAL,
1495 [VK_COMPARE_OP_ALWAYS] = PREFILTEROPALWAYS,
1496 };
1497
1498 if (pCreateInfo->maxAnisotropy > 0)
1499 anv_finishme("missing support for anisotropic filtering");
1500
1501 struct GEN8_SAMPLER_STATE sampler_state = {
1502 .SamplerDisable = false,
1503 .TextureBorderColorMode = DX10OGL,
1504 .LODPreClampMode = 0,
1505 .BaseMipLevel = 0,
1506 .MipModeFilter = vk_to_gen_mipmap_mode[pCreateInfo->mipMode],
1507 .MagModeFilter = vk_to_gen_tex_filter[pCreateInfo->magFilter],
1508 .MinModeFilter = vk_to_gen_tex_filter[pCreateInfo->minFilter],
1509 .TextureLODBias = pCreateInfo->mipLodBias * 256,
1510 .AnisotropicAlgorithm = EWAApproximation,
1511 .MinLOD = pCreateInfo->minLod * 256,
1512 .MaxLOD = pCreateInfo->maxLod * 256,
1513 .ChromaKeyEnable = 0,
1514 .ChromaKeyIndex = 0,
1515 .ChromaKeyMode = 0,
1516 .ShadowFunction = vk_to_gen_compare_op[pCreateInfo->compareOp],
1517 .CubeSurfaceControlMode = 0,
1518 .IndirectStatePointer = 0,
1519 .LODClampMagnificationMode = MIPNONE,
1520 .MaximumAnisotropy = 0,
1521 .RAddressMinFilterRoundingEnable = 0,
1522 .RAddressMagFilterRoundingEnable = 0,
1523 .VAddressMinFilterRoundingEnable = 0,
1524 .VAddressMagFilterRoundingEnable = 0,
1525 .UAddressMinFilterRoundingEnable = 0,
1526 .UAddressMagFilterRoundingEnable = 0,
1527 .TrilinearFilterQuality = 0,
1528 .NonnormalizedCoordinateEnable = 0,
1529 .TCXAddressControlMode = vk_to_gen_tex_address[pCreateInfo->addressU],
1530 .TCYAddressControlMode = vk_to_gen_tex_address[pCreateInfo->addressV],
1531 .TCZAddressControlMode = vk_to_gen_tex_address[pCreateInfo->addressW],
1532 };
1533
1534 GEN8_SAMPLER_STATE_pack(NULL, sampler->state, &sampler_state);
1535
1536 *pSampler = (VkSampler) sampler;
1537
1538 return VK_SUCCESS;
1539 }
1540
1541 // Descriptor set functions
1542
1543 VkResult anv_CreateDescriptorSetLayout(
1544 VkDevice _device,
1545 const VkDescriptorSetLayoutCreateInfo* pCreateInfo,
1546 VkDescriptorSetLayout* pSetLayout)
1547 {
1548 struct anv_device *device = (struct anv_device *) _device;
1549 struct anv_descriptor_set_layout *set_layout;
1550
1551 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO);
1552
1553 uint32_t sampler_count[VK_NUM_SHADER_STAGE] = { 0, };
1554 uint32_t surface_count[VK_NUM_SHADER_STAGE] = { 0, };
1555 uint32_t num_dynamic_buffers = 0;
1556 uint32_t count = 0;
1557 uint32_t s;
1558
1559 for (uint32_t i = 0; i < pCreateInfo->count; i++) {
1560 switch (pCreateInfo->pBinding[i].descriptorType) {
1561 case VK_DESCRIPTOR_TYPE_SAMPLER:
1562 for_each_bit(s, pCreateInfo->pBinding[i].stageFlags)
1563 sampler_count[s] += pCreateInfo->pBinding[i].count;
1564 break;
1565
1566 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
1567 for_each_bit(s, pCreateInfo->pBinding[i].stageFlags)
1568 sampler_count[s] += pCreateInfo->pBinding[i].count;
1569
1570 /* fall through */
1571
1572 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
1573 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE:
1574 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
1575 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
1576 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
1577 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
1578 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
1579 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
1580 for_each_bit(s, pCreateInfo->pBinding[i].stageFlags)
1581 surface_count[s] += pCreateInfo->pBinding[i].count;
1582 break;
1583 default:
1584 break;
1585 }
1586
1587 count += pCreateInfo->pBinding[i].count;
1588 }
1589
1590 for (uint32_t i = 0; i < pCreateInfo->count; i++) {
1591 switch (pCreateInfo->pBinding[i].descriptorType) {
1592 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
1593 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
1594 num_dynamic_buffers++;
1595 break;
1596 default:
1597 break;
1598 }
1599 }
1600
1601 uint32_t sampler_total = 0;
1602 uint32_t surface_total = 0;
1603 for (uint32_t s = 0; s < VK_NUM_SHADER_STAGE; s++) {
1604 sampler_total += sampler_count[s];
1605 surface_total += surface_count[s];
1606 }
1607
1608 size_t size = sizeof(*set_layout) +
1609 (sampler_total + surface_total) * sizeof(uint32_t);
1610 set_layout = anv_device_alloc(device, size, 8,
1611 VK_SYSTEM_ALLOC_TYPE_API_OBJECT);
1612 if (!set_layout)
1613 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
1614
1615 set_layout->num_dynamic_buffers = num_dynamic_buffers;
1616 set_layout->count = count;
1617
1618 uint32_t *p = set_layout->entries;
1619 uint32_t *sampler[VK_NUM_SHADER_STAGE];
1620 uint32_t *surface[VK_NUM_SHADER_STAGE];
1621 for (uint32_t s = 0; s < VK_NUM_SHADER_STAGE; s++) {
1622 set_layout->stage[s].surface_count = surface_count[s];
1623 set_layout->stage[s].surface_start = surface[s] = p;
1624 p += surface_count[s];
1625 set_layout->stage[s].sampler_count = sampler_count[s];
1626 set_layout->stage[s].sampler_start = sampler[s] = p;
1627 p += sampler_count[s];
1628 }
1629
1630 uint32_t descriptor = 0;
1631 for (uint32_t i = 0; i < pCreateInfo->count; i++) {
1632 switch (pCreateInfo->pBinding[i].descriptorType) {
1633 case VK_DESCRIPTOR_TYPE_SAMPLER:
1634 for_each_bit(s, pCreateInfo->pBinding[i].stageFlags)
1635 for (uint32_t j = 0; j < pCreateInfo->pBinding[i].count; j++)
1636 *(sampler[s])++ = descriptor + j;
1637 break;
1638
1639 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
1640 for_each_bit(s, pCreateInfo->pBinding[i].stageFlags)
1641 for (uint32_t j = 0; j < pCreateInfo->pBinding[i].count; j++)
1642 *(sampler[s])++ = descriptor + j;
1643
1644 /* fallthrough */
1645
1646 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
1647 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE:
1648 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
1649 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
1650 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
1651 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
1652 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
1653 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
1654 for_each_bit(s, pCreateInfo->pBinding[i].stageFlags)
1655 for (uint32_t j = 0; j < pCreateInfo->pBinding[i].count; j++) {
1656 *(surface[s])++ = descriptor + j;
1657 }
1658 break;
1659 default:
1660 unreachable("");
1661 }
1662 descriptor += pCreateInfo->pBinding[i].count;
1663 }
1664
1665 *pSetLayout = (VkDescriptorSetLayout) set_layout;
1666
1667 return VK_SUCCESS;
1668 }
1669
1670 VkResult anv_BeginDescriptorPoolUpdate(
1671 VkDevice device,
1672 VkDescriptorUpdateMode updateMode)
1673 {
1674 return VK_SUCCESS;
1675 }
1676
1677 VkResult anv_EndDescriptorPoolUpdate(
1678 VkDevice device,
1679 VkCmdBuffer cmd)
1680 {
1681 return VK_SUCCESS;
1682 }
1683
1684 VkResult anv_CreateDescriptorPool(
1685 VkDevice device,
1686 VkDescriptorPoolUsage poolUsage,
1687 uint32_t maxSets,
1688 const VkDescriptorPoolCreateInfo* pCreateInfo,
1689 VkDescriptorPool* pDescriptorPool)
1690 {
1691 *pDescriptorPool = 1;
1692
1693 return VK_SUCCESS;
1694 }
1695
1696 VkResult anv_ResetDescriptorPool(
1697 VkDevice device,
1698 VkDescriptorPool descriptorPool)
1699 {
1700 return VK_SUCCESS;
1701 }
1702
1703 VkResult anv_AllocDescriptorSets(
1704 VkDevice _device,
1705 VkDescriptorPool descriptorPool,
1706 VkDescriptorSetUsage setUsage,
1707 uint32_t count,
1708 const VkDescriptorSetLayout* pSetLayouts,
1709 VkDescriptorSet* pDescriptorSets,
1710 uint32_t* pCount)
1711 {
1712 struct anv_device *device = (struct anv_device *) _device;
1713 const struct anv_descriptor_set_layout *layout;
1714 struct anv_descriptor_set *set;
1715 size_t size;
1716
1717 for (uint32_t i = 0; i < count; i++) {
1718 layout = (struct anv_descriptor_set_layout *) pSetLayouts[i];
1719 size = sizeof(*set) + layout->count * sizeof(set->descriptors[0]);
1720 set = anv_device_alloc(device, size, 8,
1721 VK_SYSTEM_ALLOC_TYPE_API_OBJECT);
1722 if (!set) {
1723 *pCount = i;
1724 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
1725 }
1726
1727 pDescriptorSets[i] = (VkDescriptorSet) set;
1728 }
1729
1730 *pCount = count;
1731
1732 return VK_SUCCESS;
1733 }
1734
1735 void anv_ClearDescriptorSets(
1736 VkDevice device,
1737 VkDescriptorPool descriptorPool,
1738 uint32_t count,
1739 const VkDescriptorSet* pDescriptorSets)
1740 {
1741 }
1742
1743 void anv_UpdateDescriptors(
1744 VkDevice _device,
1745 VkDescriptorSet descriptorSet,
1746 uint32_t updateCount,
1747 const void** ppUpdateArray)
1748 {
1749 struct anv_descriptor_set *set = (struct anv_descriptor_set *) descriptorSet;
1750 VkUpdateSamplers *update_samplers;
1751 VkUpdateSamplerTextures *update_sampler_textures;
1752 VkUpdateImages *update_images;
1753 VkUpdateBuffers *update_buffers;
1754 VkUpdateAsCopy *update_as_copy;
1755
1756 for (uint32_t i = 0; i < updateCount; i++) {
1757 const struct anv_common *common = ppUpdateArray[i];
1758
1759 switch (common->sType) {
1760 case VK_STRUCTURE_TYPE_UPDATE_SAMPLERS:
1761 update_samplers = (VkUpdateSamplers *) common;
1762
1763 for (uint32_t j = 0; j < update_samplers->count; j++) {
1764 set->descriptors[update_samplers->binding + j].sampler =
1765 (struct anv_sampler *) update_samplers->pSamplers[j];
1766 }
1767 break;
1768
1769 case VK_STRUCTURE_TYPE_UPDATE_SAMPLER_TEXTURES:
1770 /* FIXME: Shouldn't this be *_UPDATE_SAMPLER_IMAGES? */
1771 update_sampler_textures = (VkUpdateSamplerTextures *) common;
1772
1773 for (uint32_t j = 0; j < update_sampler_textures->count; j++) {
1774 set->descriptors[update_sampler_textures->binding + j].view =
1775 (struct anv_surface_view *)
1776 update_sampler_textures->pSamplerImageViews[j].pImageView->view;
1777 set->descriptors[update_sampler_textures->binding + j].sampler =
1778 (struct anv_sampler *)
1779 update_sampler_textures->pSamplerImageViews[j].sampler;
1780 }
1781 break;
1782
1783 case VK_STRUCTURE_TYPE_UPDATE_IMAGES:
1784 update_images = (VkUpdateImages *) common;
1785
1786 for (uint32_t j = 0; j < update_images->count; j++) {
1787 set->descriptors[update_images->binding + j].view =
1788 (struct anv_surface_view *) update_images->pImageViews[j].view;
1789 }
1790 break;
1791
1792 case VK_STRUCTURE_TYPE_UPDATE_BUFFERS:
1793 update_buffers = (VkUpdateBuffers *) common;
1794
1795 for (uint32_t j = 0; j < update_buffers->count; j++) {
1796 set->descriptors[update_buffers->binding + j].view =
1797 (struct anv_surface_view *) update_buffers->pBufferViews[j].view;
1798 }
1799 /* FIXME: descriptor arrays? */
1800 break;
1801
1802 case VK_STRUCTURE_TYPE_UPDATE_AS_COPY:
1803 update_as_copy = (VkUpdateAsCopy *) common;
1804 (void) update_as_copy;
1805 break;
1806
1807 default:
1808 break;
1809 }
1810 }
1811 }
1812
1813 // State object functions
1814
1815 static inline int64_t
1816 clamp_int64(int64_t x, int64_t min, int64_t max)
1817 {
1818 if (x < min)
1819 return min;
1820 else if (x < max)
1821 return x;
1822 else
1823 return max;
1824 }
1825
1826 VkResult anv_CreateDynamicViewportState(
1827 VkDevice _device,
1828 const VkDynamicVpStateCreateInfo* pCreateInfo,
1829 VkDynamicVpState* pState)
1830 {
1831 struct anv_device *device = (struct anv_device *) _device;
1832 struct anv_dynamic_vp_state *state;
1833
1834 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_DYNAMIC_VP_STATE_CREATE_INFO);
1835
1836 state = anv_device_alloc(device, sizeof(*state), 8,
1837 VK_SYSTEM_ALLOC_TYPE_API_OBJECT);
1838 if (state == NULL)
1839 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
1840
1841 unsigned count = pCreateInfo->viewportAndScissorCount;
1842 state->sf_clip_vp = anv_state_pool_alloc(&device->dynamic_state_pool,
1843 count * 64, 64);
1844 state->cc_vp = anv_state_pool_alloc(&device->dynamic_state_pool,
1845 count * 8, 32);
1846 state->scissor = anv_state_pool_alloc(&device->dynamic_state_pool,
1847 count * 32, 32);
1848
1849 for (uint32_t i = 0; i < pCreateInfo->viewportAndScissorCount; i++) {
1850 const VkViewport *vp = &pCreateInfo->pViewports[i];
1851 const VkRect *s = &pCreateInfo->pScissors[i];
1852
1853 struct GEN8_SF_CLIP_VIEWPORT sf_clip_viewport = {
1854 .ViewportMatrixElementm00 = vp->width / 2,
1855 .ViewportMatrixElementm11 = vp->height / 2,
1856 .ViewportMatrixElementm22 = (vp->maxDepth - vp->minDepth) / 2,
1857 .ViewportMatrixElementm30 = vp->originX + vp->width / 2,
1858 .ViewportMatrixElementm31 = vp->originY + vp->height / 2,
1859 .ViewportMatrixElementm32 = (vp->maxDepth + vp->minDepth) / 2,
1860 .XMinClipGuardband = -1.0f,
1861 .XMaxClipGuardband = 1.0f,
1862 .YMinClipGuardband = -1.0f,
1863 .YMaxClipGuardband = 1.0f,
1864 .XMinViewPort = vp->originX,
1865 .XMaxViewPort = vp->originX + vp->width - 1,
1866 .YMinViewPort = vp->originY,
1867 .YMaxViewPort = vp->originY + vp->height - 1,
1868 };
1869
1870 struct GEN8_CC_VIEWPORT cc_viewport = {
1871 .MinimumDepth = vp->minDepth,
1872 .MaximumDepth = vp->maxDepth
1873 };
1874
1875 /* Since xmax and ymax are inclusive, we have to have xmax < xmin or
1876 * ymax < ymin for empty clips. In case clip x, y, width height are all
1877 * 0, the clamps below produce 0 for xmin, ymin, xmax, ymax, which isn't
1878 * what we want. Just special case empty clips and produce a canonical
1879 * empty clip. */
1880 static const struct GEN8_SCISSOR_RECT empty_scissor = {
1881 .ScissorRectangleYMin = 1,
1882 .ScissorRectangleXMin = 1,
1883 .ScissorRectangleYMax = 0,
1884 .ScissorRectangleXMax = 0
1885 };
1886
1887 const int max = 0xffff;
1888 struct GEN8_SCISSOR_RECT scissor = {
1889 /* Do this math using int64_t so overflow gets clamped correctly. */
1890 .ScissorRectangleYMin = clamp_int64(s->offset.y, 0, max),
1891 .ScissorRectangleXMin = clamp_int64(s->offset.x, 0, max),
1892 .ScissorRectangleYMax = clamp_int64((uint64_t) s->offset.y + s->extent.height - 1, 0, max),
1893 .ScissorRectangleXMax = clamp_int64((uint64_t) s->offset.x + s->extent.width - 1, 0, max)
1894 };
1895
1896 GEN8_SF_CLIP_VIEWPORT_pack(NULL, state->sf_clip_vp.map + i * 64, &sf_clip_viewport);
1897 GEN8_CC_VIEWPORT_pack(NULL, state->cc_vp.map + i * 32, &cc_viewport);
1898
1899 if (s->extent.width <= 0 || s->extent.height <= 0) {
1900 GEN8_SCISSOR_RECT_pack(NULL, state->scissor.map + i * 32, &empty_scissor);
1901 } else {
1902 GEN8_SCISSOR_RECT_pack(NULL, state->scissor.map + i * 32, &scissor);
1903 }
1904 }
1905
1906 *pState = (VkDynamicVpState) state;
1907
1908 return VK_SUCCESS;
1909 }
1910
1911 VkResult anv_CreateDynamicRasterState(
1912 VkDevice _device,
1913 const VkDynamicRsStateCreateInfo* pCreateInfo,
1914 VkDynamicRsState* pState)
1915 {
1916 struct anv_device *device = (struct anv_device *) _device;
1917 struct anv_dynamic_rs_state *state;
1918
1919 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_DYNAMIC_RS_STATE_CREATE_INFO);
1920
1921 state = anv_device_alloc(device, sizeof(*state), 8,
1922 VK_SYSTEM_ALLOC_TYPE_API_OBJECT);
1923 if (state == NULL)
1924 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
1925
1926 /* Missing these:
1927 * float depthBias;
1928 * float depthBiasClamp;
1929 * float slopeScaledDepthBias;
1930 * float pointFadeThreshold;
1931 * // optional (GL45) - Size of point fade threshold
1932 */
1933
1934 struct GEN8_3DSTATE_SF sf = {
1935 GEN8_3DSTATE_SF_header,
1936 .LineWidth = pCreateInfo->lineWidth,
1937 .PointWidth = pCreateInfo->pointSize,
1938 };
1939
1940 GEN8_3DSTATE_SF_pack(NULL, state->state_sf, &sf);
1941
1942 *pState = (VkDynamicRsState) state;
1943
1944 return VK_SUCCESS;
1945 }
1946
1947 VkResult anv_CreateDynamicColorBlendState(
1948 VkDevice _device,
1949 const VkDynamicCbStateCreateInfo* pCreateInfo,
1950 VkDynamicCbState* pState)
1951 {
1952 struct anv_device *device = (struct anv_device *) _device;
1953 struct anv_dynamic_cb_state *state;
1954
1955 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_DYNAMIC_CB_STATE_CREATE_INFO);
1956
1957 state = anv_device_alloc(device, sizeof(*state), 8,
1958 VK_SYSTEM_ALLOC_TYPE_API_OBJECT);
1959 if (state == NULL)
1960 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
1961
1962 *pState = (VkDynamicCbState) state;
1963
1964 return VK_SUCCESS;
1965 }
1966
1967 VkResult anv_CreateDynamicDepthStencilState(
1968 VkDevice device,
1969 const VkDynamicDsStateCreateInfo* pCreateInfo,
1970 VkDynamicDsState* pState)
1971 {
1972 stub_return(VK_UNSUPPORTED);
1973 }
1974
1975 // Command buffer functions
1976
1977 VkResult anv_CreateCommandBuffer(
1978 VkDevice _device,
1979 const VkCmdBufferCreateInfo* pCreateInfo,
1980 VkCmdBuffer* pCmdBuffer)
1981 {
1982 struct anv_device *device = (struct anv_device *) _device;
1983 struct anv_cmd_buffer *cmd_buffer;
1984 VkResult result;
1985
1986 cmd_buffer = anv_device_alloc(device, sizeof(*cmd_buffer), 8,
1987 VK_SYSTEM_ALLOC_TYPE_API_OBJECT);
1988 if (cmd_buffer == NULL)
1989 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
1990
1991 cmd_buffer->device = device;
1992 cmd_buffer->rs_state = NULL;
1993 cmd_buffer->vp_state = NULL;
1994 memset(&cmd_buffer->default_bindings, 0, sizeof(cmd_buffer->default_bindings));
1995 cmd_buffer->bindings = &cmd_buffer->default_bindings;
1996
1997 result = anv_batch_init(&cmd_buffer->batch, device);
1998 if (result != VK_SUCCESS)
1999 goto fail;
2000
2001 cmd_buffer->exec2_objects =
2002 anv_device_alloc(device, 8192 * sizeof(cmd_buffer->exec2_objects[0]), 8,
2003 VK_SYSTEM_ALLOC_TYPE_API_OBJECT);
2004 if (cmd_buffer->exec2_objects == NULL) {
2005 result = vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
2006 goto fail_batch;
2007 }
2008
2009 cmd_buffer->exec2_bos =
2010 anv_device_alloc(device, 8192 * sizeof(cmd_buffer->exec2_bos[0]), 8,
2011 VK_SYSTEM_ALLOC_TYPE_API_OBJECT);
2012 if (cmd_buffer->exec2_bos == NULL) {
2013 result = vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
2014 goto fail_exec2_objects;
2015 }
2016
2017 anv_state_stream_init(&cmd_buffer->surface_state_stream,
2018 &device->surface_state_block_pool);
2019 anv_state_stream_init(&cmd_buffer->dynamic_state_stream,
2020 &device->dynamic_state_block_pool);
2021
2022 cmd_buffer->dirty = 0;
2023 cmd_buffer->vb_dirty = 0;
2024
2025 *pCmdBuffer = (VkCmdBuffer) cmd_buffer;
2026
2027 return VK_SUCCESS;
2028
2029 fail_exec2_objects:
2030 anv_device_free(device, cmd_buffer->exec2_objects);
2031 fail_batch:
2032 anv_batch_finish(&cmd_buffer->batch, device);
2033 fail:
2034 anv_device_free(device, cmd_buffer);
2035
2036 return result;
2037 }
2038
2039 VkResult anv_BeginCommandBuffer(
2040 VkCmdBuffer cmdBuffer,
2041 const VkCmdBufferBeginInfo* pBeginInfo)
2042 {
2043 struct anv_cmd_buffer *cmd_buffer = (struct anv_cmd_buffer *) cmdBuffer;
2044 struct anv_device *device = cmd_buffer->device;
2045
2046 anv_batch_emit(&cmd_buffer->batch, GEN8_PIPELINE_SELECT,
2047 .PipelineSelection = _3D);
2048 anv_batch_emit(&cmd_buffer->batch, GEN8_STATE_SIP);
2049
2050 anv_batch_emit(&cmd_buffer->batch, GEN8_STATE_BASE_ADDRESS,
2051 .GeneralStateBaseAddress = { NULL, 0 },
2052 .GeneralStateBaseAddressModifyEnable = true,
2053 .GeneralStateBufferSize = 0xfffff,
2054 .GeneralStateBufferSizeModifyEnable = true,
2055
2056 .SurfaceStateBaseAddress = { &device->surface_state_block_pool.bo, 0 },
2057 .SurfaceStateMemoryObjectControlState = 0, /* FIXME: MOCS */
2058 .SurfaceStateBaseAddressModifyEnable = true,
2059
2060 .DynamicStateBaseAddress = { &device->dynamic_state_block_pool.bo, 0 },
2061 .DynamicStateBaseAddressModifyEnable = true,
2062 .DynamicStateBufferSize = 0xfffff,
2063 .DynamicStateBufferSizeModifyEnable = true,
2064
2065 .IndirectObjectBaseAddress = { NULL, 0 },
2066 .IndirectObjectBaseAddressModifyEnable = true,
2067 .IndirectObjectBufferSize = 0xfffff,
2068 .IndirectObjectBufferSizeModifyEnable = true,
2069
2070 .InstructionBaseAddress = { &device->instruction_block_pool.bo, 0 },
2071 .InstructionBaseAddressModifyEnable = true,
2072 .InstructionBufferSize = 0xfffff,
2073 .InstructionBuffersizeModifyEnable = true);
2074
2075 anv_batch_emit(&cmd_buffer->batch, GEN8_3DSTATE_VF_STATISTICS,
2076 .StatisticsEnable = true);
2077 anv_batch_emit(&cmd_buffer->batch, GEN8_3DSTATE_HS, .Enable = false);
2078 anv_batch_emit(&cmd_buffer->batch, GEN8_3DSTATE_TE, .TEEnable = false);
2079 anv_batch_emit(&cmd_buffer->batch, GEN8_3DSTATE_DS, .FunctionEnable = false);
2080 anv_batch_emit(&cmd_buffer->batch, GEN8_3DSTATE_STREAMOUT, .SOFunctionEnable = false);
2081
2082 anv_batch_emit(&cmd_buffer->batch, GEN8_3DSTATE_PUSH_CONSTANT_ALLOC_VS,
2083 .ConstantBufferOffset = 0,
2084 .ConstantBufferSize = 4);
2085 anv_batch_emit(&cmd_buffer->batch, GEN8_3DSTATE_PUSH_CONSTANT_ALLOC_GS,
2086 .ConstantBufferOffset = 4,
2087 .ConstantBufferSize = 4);
2088 anv_batch_emit(&cmd_buffer->batch, GEN8_3DSTATE_PUSH_CONSTANT_ALLOC_PS,
2089 .ConstantBufferOffset = 8,
2090 .ConstantBufferSize = 4);
2091
2092 anv_batch_emit(&cmd_buffer->batch, GEN8_3DSTATE_WM_CHROMAKEY,
2093 .ChromaKeyKillEnable = false);
2094 anv_batch_emit(&cmd_buffer->batch, GEN8_3DSTATE_SBE_SWIZ);
2095 anv_batch_emit(&cmd_buffer->batch, GEN8_3DSTATE_AA_LINE_PARAMETERS);
2096
2097 /* Hardcoded state: */
2098 anv_batch_emit(&cmd_buffer->batch, GEN8_3DSTATE_DEPTH_BUFFER,
2099 .SurfaceType = SURFTYPE_2D,
2100 .Width = 1,
2101 .Height = 1,
2102 .SurfaceFormat = D16_UNORM,
2103 .SurfaceBaseAddress = { NULL, 0 },
2104 .HierarchicalDepthBufferEnable = 0);
2105
2106 anv_batch_emit(&cmd_buffer->batch, GEN8_3DSTATE_WM_DEPTH_STENCIL,
2107 .DepthTestEnable = false,
2108 .DepthBufferWriteEnable = false);
2109
2110 return VK_SUCCESS;
2111 }
2112
2113 static void
2114 anv_cmd_buffer_add_bo(struct anv_cmd_buffer *cmd_buffer,
2115 struct anv_bo *bo, struct anv_reloc_list *list)
2116 {
2117 struct drm_i915_gem_exec_object2 *obj;
2118
2119 bo->index = cmd_buffer->bo_count;
2120 obj = &cmd_buffer->exec2_objects[bo->index];
2121 cmd_buffer->exec2_bos[bo->index] = bo;
2122 cmd_buffer->bo_count++;
2123
2124 obj->handle = bo->gem_handle;
2125 obj->relocation_count = 0;
2126 obj->relocs_ptr = 0;
2127 obj->alignment = 0;
2128 obj->offset = bo->offset;
2129 obj->flags = 0;
2130 obj->rsvd1 = 0;
2131 obj->rsvd2 = 0;
2132
2133 if (list) {
2134 obj->relocation_count = list->num_relocs;
2135 obj->relocs_ptr = (uintptr_t) list->relocs;
2136 }
2137 }
2138
2139 static void
2140 anv_cmd_buffer_add_validate_bos(struct anv_cmd_buffer *cmd_buffer,
2141 struct anv_reloc_list *list)
2142 {
2143 struct anv_bo *bo, *batch_bo;
2144
2145 batch_bo = &cmd_buffer->batch.bo;
2146 for (size_t i = 0; i < list->num_relocs; i++) {
2147 bo = list->reloc_bos[i];
2148 /* Skip any relocations targeting the batch bo. We need to make sure
2149 * it's the last in the list so we'll add it manually later.
2150 */
2151 if (bo == batch_bo)
2152 continue;
2153 if (bo->index < cmd_buffer->bo_count && cmd_buffer->exec2_bos[bo->index] == bo)
2154 continue;
2155
2156 anv_cmd_buffer_add_bo(cmd_buffer, bo, NULL);
2157 }
2158 }
2159
2160 static void
2161 anv_cmd_buffer_process_relocs(struct anv_cmd_buffer *cmd_buffer,
2162 struct anv_reloc_list *list)
2163 {
2164 struct anv_bo *bo;
2165
2166 /* If the kernel supports I915_EXEC_NO_RELOC, it will compare offset in
2167 * struct drm_i915_gem_exec_object2 against the bos current offset and if
2168 * all bos haven't moved it will skip relocation processing alltogether.
2169 * If I915_EXEC_NO_RELOC is not supported, the kernel ignores the incoming
2170 * value of offset so we can set it either way. For that to work we need
2171 * to make sure all relocs use the same presumed offset.
2172 */
2173
2174 for (size_t i = 0; i < list->num_relocs; i++) {
2175 bo = list->reloc_bos[i];
2176 if (bo->offset != list->relocs[i].presumed_offset)
2177 cmd_buffer->need_reloc = true;
2178
2179 list->relocs[i].target_handle = bo->index;
2180 }
2181 }
2182
2183 VkResult anv_EndCommandBuffer(
2184 VkCmdBuffer cmdBuffer)
2185 {
2186 struct anv_cmd_buffer *cmd_buffer = (struct anv_cmd_buffer *) cmdBuffer;
2187 struct anv_device *device = cmd_buffer->device;
2188 struct anv_batch *batch = &cmd_buffer->batch;
2189
2190 anv_batch_emit(batch, GEN8_MI_BATCH_BUFFER_END);
2191
2192 /* Round batch up to an even number of dwords. */
2193 if ((batch->next - batch->bo.map) & 4)
2194 anv_batch_emit(batch, GEN8_MI_NOOP);
2195
2196 cmd_buffer->bo_count = 0;
2197 cmd_buffer->need_reloc = false;
2198
2199 /* Lock for access to bo->index. */
2200 pthread_mutex_lock(&device->mutex);
2201
2202 /* Add block pool bos first so we can add them with their relocs. */
2203 anv_cmd_buffer_add_bo(cmd_buffer, &device->surface_state_block_pool.bo,
2204 &batch->surf_relocs);
2205
2206 anv_cmd_buffer_add_validate_bos(cmd_buffer, &batch->surf_relocs);
2207 anv_cmd_buffer_add_validate_bos(cmd_buffer, &batch->cmd_relocs);
2208 anv_cmd_buffer_add_bo(cmd_buffer, &batch->bo, &batch->cmd_relocs);
2209 anv_cmd_buffer_process_relocs(cmd_buffer, &batch->surf_relocs);
2210 anv_cmd_buffer_process_relocs(cmd_buffer, &batch->cmd_relocs);
2211
2212 cmd_buffer->execbuf.buffers_ptr = (uintptr_t) cmd_buffer->exec2_objects;
2213 cmd_buffer->execbuf.buffer_count = cmd_buffer->bo_count;
2214 cmd_buffer->execbuf.batch_start_offset = 0;
2215 cmd_buffer->execbuf.batch_len = batch->next - batch->bo.map;
2216 cmd_buffer->execbuf.cliprects_ptr = 0;
2217 cmd_buffer->execbuf.num_cliprects = 0;
2218 cmd_buffer->execbuf.DR1 = 0;
2219 cmd_buffer->execbuf.DR4 = 0;
2220
2221 cmd_buffer->execbuf.flags = I915_EXEC_HANDLE_LUT;
2222 if (!cmd_buffer->need_reloc)
2223 cmd_buffer->execbuf.flags |= I915_EXEC_NO_RELOC;
2224 cmd_buffer->execbuf.flags |= I915_EXEC_RENDER;
2225 cmd_buffer->execbuf.rsvd1 = device->context_id;
2226 cmd_buffer->execbuf.rsvd2 = 0;
2227
2228 pthread_mutex_unlock(&device->mutex);
2229
2230 return VK_SUCCESS;
2231 }
2232
2233 VkResult anv_ResetCommandBuffer(
2234 VkCmdBuffer cmdBuffer)
2235 {
2236 struct anv_cmd_buffer *cmd_buffer = (struct anv_cmd_buffer *) cmdBuffer;
2237
2238 anv_batch_reset(&cmd_buffer->batch);
2239
2240 return VK_SUCCESS;
2241 }
2242
2243 // Command buffer building functions
2244
2245 void anv_CmdBindPipeline(
2246 VkCmdBuffer cmdBuffer,
2247 VkPipelineBindPoint pipelineBindPoint,
2248 VkPipeline _pipeline)
2249 {
2250 struct anv_cmd_buffer *cmd_buffer = (struct anv_cmd_buffer *) cmdBuffer;
2251
2252 cmd_buffer->pipeline = (struct anv_pipeline *) _pipeline;
2253 cmd_buffer->dirty |= ANV_CMD_BUFFER_PIPELINE_DIRTY;
2254 }
2255
2256 void anv_CmdBindDynamicStateObject(
2257 VkCmdBuffer cmdBuffer,
2258 VkStateBindPoint stateBindPoint,
2259 VkDynamicStateObject dynamicState)
2260 {
2261 struct anv_cmd_buffer *cmd_buffer = (struct anv_cmd_buffer *) cmdBuffer;
2262 struct anv_dynamic_vp_state *vp_state;
2263
2264 switch (stateBindPoint) {
2265 case VK_STATE_BIND_POINT_VIEWPORT:
2266 vp_state = (struct anv_dynamic_vp_state *) dynamicState;
2267 /* We emit state immediately, but set cmd_buffer->vp_state to indicate
2268 * that vp state has been set in this command buffer. */
2269 cmd_buffer->vp_state = vp_state;
2270 anv_batch_emit(&cmd_buffer->batch, GEN8_3DSTATE_SCISSOR_STATE_POINTERS,
2271 .ScissorRectPointer = vp_state->scissor.offset);
2272 anv_batch_emit(&cmd_buffer->batch, GEN8_3DSTATE_VIEWPORT_STATE_POINTERS_CC,
2273 .CCViewportPointer = vp_state->cc_vp.offset);
2274 anv_batch_emit(&cmd_buffer->batch, GEN8_3DSTATE_VIEWPORT_STATE_POINTERS_SF_CLIP,
2275 .SFClipViewportPointer = vp_state->sf_clip_vp.offset);
2276 break;
2277 case VK_STATE_BIND_POINT_RASTER:
2278 cmd_buffer->rs_state = (struct anv_dynamic_rs_state *) dynamicState;
2279 cmd_buffer->dirty |= ANV_CMD_BUFFER_RS_DIRTY;
2280 break;
2281 case VK_STATE_BIND_POINT_COLOR_BLEND:
2282 case VK_STATE_BIND_POINT_DEPTH_STENCIL:
2283 break;
2284 default:
2285 break;
2286 };
2287 }
2288
2289 void anv_CmdBindDescriptorSets(
2290 VkCmdBuffer cmdBuffer,
2291 VkPipelineBindPoint pipelineBindPoint,
2292 uint32_t firstSet,
2293 uint32_t setCount,
2294 const VkDescriptorSet* pDescriptorSets,
2295 uint32_t dynamicOffsetCount,
2296 const uint32_t* pDynamicOffsets)
2297 {
2298 struct anv_cmd_buffer *cmd_buffer = (struct anv_cmd_buffer *) cmdBuffer;
2299 struct anv_pipeline_layout *layout = cmd_buffer->pipeline->layout;
2300 struct anv_bindings *bindings = cmd_buffer->bindings;
2301
2302 uint32_t offset = 0;
2303 for (uint32_t i = 0; i < setCount; i++) {
2304 struct anv_descriptor_set *set =
2305 (struct anv_descriptor_set *) pDescriptorSets[i];
2306 struct anv_descriptor_set_layout *set_layout = layout->set[firstSet + i].layout;
2307
2308 for (uint32_t s = 0; s < VK_NUM_SHADER_STAGE; s++) {
2309 uint32_t *surface_to_desc = set_layout->stage[s].surface_start;
2310 uint32_t *sampler_to_desc = set_layout->stage[s].sampler_start;
2311 uint32_t bias = s == VK_SHADER_STAGE_FRAGMENT ? MAX_RTS : 0;
2312 uint32_t start;
2313
2314 start = bias + layout->set[firstSet + i].surface_start[s];
2315 for (uint32_t b = 0; b < set_layout->stage[s].surface_count; b++) {
2316 struct anv_surface_view *view = set->descriptors[surface_to_desc[b]].view;
2317
2318 bindings->descriptors[s].surfaces[start + b] =
2319 view->surface_state.offset;
2320 bindings->descriptors[s].relocs[start + b].bo = view->bo;
2321 bindings->descriptors[s].relocs[start + b].offset = view->offset;
2322 }
2323
2324 start = layout->set[firstSet + i].sampler_start[s];
2325 for (uint32_t b = 0; b < set_layout->stage[s].sampler_count; b++) {
2326 struct anv_sampler *sampler = set->descriptors[sampler_to_desc[b]].sampler;
2327
2328 memcpy(&bindings->descriptors[s].samplers[start + b],
2329 sampler->state, sizeof(sampler->state));
2330 }
2331 }
2332
2333 offset += layout->set[firstSet + i].layout->num_dynamic_buffers;
2334 }
2335
2336 cmd_buffer->dirty |= ANV_CMD_BUFFER_DESCRIPTOR_SET_DIRTY;
2337 }
2338
2339 void anv_CmdBindIndexBuffer(
2340 VkCmdBuffer cmdBuffer,
2341 VkBuffer _buffer,
2342 VkDeviceSize offset,
2343 VkIndexType indexType)
2344 {
2345 struct anv_cmd_buffer *cmd_buffer = (struct anv_cmd_buffer *) cmdBuffer;
2346 struct anv_buffer *buffer = (struct anv_buffer *) _buffer;
2347
2348 static const uint32_t vk_to_gen_index_type[] = {
2349 [VK_INDEX_TYPE_UINT8] = INDEX_BYTE,
2350 [VK_INDEX_TYPE_UINT16] = INDEX_WORD,
2351 [VK_INDEX_TYPE_UINT32] = INDEX_DWORD,
2352 };
2353
2354 anv_batch_emit(&cmd_buffer->batch, GEN8_3DSTATE_INDEX_BUFFER,
2355 .IndexFormat = vk_to_gen_index_type[indexType],
2356 .MemoryObjectControlState = 0,
2357 .BufferStartingAddress = { buffer->bo, buffer->offset + offset },
2358 .BufferSize = buffer->size - offset);
2359 }
2360
2361 void anv_CmdBindVertexBuffers(
2362 VkCmdBuffer cmdBuffer,
2363 uint32_t startBinding,
2364 uint32_t bindingCount,
2365 const VkBuffer* pBuffers,
2366 const VkDeviceSize* pOffsets)
2367 {
2368 struct anv_cmd_buffer *cmd_buffer = (struct anv_cmd_buffer *) cmdBuffer;
2369 struct anv_bindings *bindings = cmd_buffer->bindings;
2370
2371 /* We have to defer setting up vertex buffer since we need the buffer
2372 * stride from the pipeline. */
2373
2374 for (uint32_t i = 0; i < bindingCount; i++) {
2375 bindings->vb[startBinding + i].buffer = (struct anv_buffer *) pBuffers[i];
2376 bindings->vb[startBinding + i].offset = pOffsets[i];
2377 cmd_buffer->vb_dirty |= 1 << (startBinding + i);
2378 }
2379 }
2380
2381 static void
2382 flush_descriptor_sets(struct anv_cmd_buffer *cmd_buffer)
2383 {
2384 struct anv_pipeline_layout *layout = cmd_buffer->pipeline->layout;
2385 struct anv_bindings *bindings = cmd_buffer->bindings;
2386 uint32_t layers = cmd_buffer->framebuffer->layers;
2387
2388 for (uint32_t s = 0; s < VK_NUM_SHADER_STAGE; s++) {
2389 uint32_t bias;
2390
2391 if (s == VK_SHADER_STAGE_FRAGMENT) {
2392 bias = MAX_RTS;
2393 layers = cmd_buffer->framebuffer->layers;
2394 } else {
2395 bias = 0;
2396 layers = 0;
2397 }
2398
2399 /* This is a little awkward: layout can be NULL but we still have to
2400 * allocate and set a binding table for the PS stage for render
2401 * targets. */
2402 uint32_t surface_count = layout ? layout->stage[s].surface_count : 0;
2403
2404 if (layers + surface_count > 0) {
2405 struct anv_state state;
2406 uint32_t size;
2407
2408 size = (bias + surface_count) * sizeof(uint32_t);
2409 state = anv_state_stream_alloc(&cmd_buffer->surface_state_stream, size, 32);
2410 memcpy(state.map, bindings->descriptors[s].surfaces, size);
2411
2412 for (uint32_t i = 0; i < layers; i++)
2413 anv_reloc_list_add(&cmd_buffer->batch.surf_relocs,
2414 bindings->descriptors[s].surfaces[i] + 8 * sizeof(int32_t),
2415 bindings->descriptors[s].relocs[i].bo,
2416 bindings->descriptors[s].relocs[i].offset);
2417
2418 for (uint32_t i = 0; i < surface_count; i++)
2419 anv_reloc_list_add(&cmd_buffer->batch.surf_relocs,
2420 bindings->descriptors[s].surfaces[bias + i] + 8 * sizeof(int32_t),
2421 bindings->descriptors[s].relocs[bias + i].bo,
2422 bindings->descriptors[s].relocs[bias + i].offset);
2423
2424 static const uint32_t binding_table_opcodes[] = {
2425 [VK_SHADER_STAGE_VERTEX] = 38,
2426 [VK_SHADER_STAGE_TESS_CONTROL] = 39,
2427 [VK_SHADER_STAGE_TESS_EVALUATION] = 40,
2428 [VK_SHADER_STAGE_GEOMETRY] = 41,
2429 [VK_SHADER_STAGE_FRAGMENT] = 42,
2430 [VK_SHADER_STAGE_COMPUTE] = 0,
2431 };
2432
2433 anv_batch_emit(&cmd_buffer->batch,
2434 GEN8_3DSTATE_BINDING_TABLE_POINTERS_VS,
2435 ._3DCommandSubOpcode = binding_table_opcodes[s],
2436 .PointertoVSBindingTable = state.offset);
2437 }
2438
2439 if (layout && layout->stage[s].sampler_count > 0) {
2440 struct anv_state state;
2441 size_t size;
2442
2443 size = layout->stage[s].sampler_count * 16;
2444 state = anv_state_stream_alloc(&cmd_buffer->dynamic_state_stream, size, 32);
2445 memcpy(state.map, bindings->descriptors[s].samplers, size);
2446
2447 static const uint32_t sampler_state_opcodes[] = {
2448 [VK_SHADER_STAGE_VERTEX] = 43,
2449 [VK_SHADER_STAGE_TESS_CONTROL] = 44, /* HS */
2450 [VK_SHADER_STAGE_TESS_EVALUATION] = 45, /* DS */
2451 [VK_SHADER_STAGE_GEOMETRY] = 46,
2452 [VK_SHADER_STAGE_FRAGMENT] = 47,
2453 [VK_SHADER_STAGE_COMPUTE] = 0,
2454 };
2455
2456 anv_batch_emit(&cmd_buffer->batch,
2457 GEN8_3DSTATE_SAMPLER_STATE_POINTERS_VS,
2458 ._3DCommandSubOpcode = sampler_state_opcodes[s],
2459 .PointertoVSSamplerState = state.offset);
2460 }
2461 }
2462 }
2463
2464 static void
2465 anv_cmd_buffer_flush_state(struct anv_cmd_buffer *cmd_buffer)
2466 {
2467 struct anv_pipeline *pipeline = cmd_buffer->pipeline;
2468 struct anv_bindings *bindings = cmd_buffer->bindings;
2469 const uint32_t num_buffers = __builtin_popcount(cmd_buffer->vb_dirty);
2470 const uint32_t num_dwords = 1 + num_buffers * 4;
2471 uint32_t *p;
2472
2473 if (cmd_buffer->vb_dirty) {
2474 p = anv_batch_emitn(&cmd_buffer->batch, num_dwords,
2475 GEN8_3DSTATE_VERTEX_BUFFERS);
2476 uint32_t vb, i = 0;
2477 for_each_bit(vb, cmd_buffer->vb_dirty) {
2478 struct anv_buffer *buffer = bindings->vb[vb].buffer;
2479 uint32_t offset = bindings->vb[vb].offset;
2480
2481 struct GEN8_VERTEX_BUFFER_STATE state = {
2482 .VertexBufferIndex = vb,
2483 .MemoryObjectControlState = 0,
2484 .AddressModifyEnable = true,
2485 .BufferPitch = pipeline->binding_stride[vb],
2486 .BufferStartingAddress = { buffer->bo, buffer->offset + offset },
2487 .BufferSize = buffer->size - offset
2488 };
2489
2490 GEN8_VERTEX_BUFFER_STATE_pack(&cmd_buffer->batch, &p[1 + i * 4], &state);
2491 i++;
2492 }
2493 }
2494
2495 if (cmd_buffer->dirty & ANV_CMD_BUFFER_PIPELINE_DIRTY)
2496 anv_batch_emit_batch(&cmd_buffer->batch, &pipeline->batch);
2497
2498 if (cmd_buffer->dirty & ANV_CMD_BUFFER_DESCRIPTOR_SET_DIRTY)
2499 flush_descriptor_sets(cmd_buffer);
2500
2501 if (cmd_buffer->dirty & (ANV_CMD_BUFFER_PIPELINE_DIRTY | ANV_CMD_BUFFER_RS_DIRTY))
2502 anv_batch_emit_merge(&cmd_buffer->batch,
2503 cmd_buffer->rs_state->state_sf, pipeline->state_sf);
2504
2505 cmd_buffer->vb_dirty = 0;
2506 cmd_buffer->dirty = 0;
2507 }
2508
2509 void anv_CmdDraw(
2510 VkCmdBuffer cmdBuffer,
2511 uint32_t firstVertex,
2512 uint32_t vertexCount,
2513 uint32_t firstInstance,
2514 uint32_t instanceCount)
2515 {
2516 struct anv_cmd_buffer *cmd_buffer = (struct anv_cmd_buffer *) cmdBuffer;
2517
2518 anv_cmd_buffer_flush_state(cmd_buffer);
2519
2520 anv_batch_emit(&cmd_buffer->batch, GEN8_3DPRIMITIVE,
2521 .VertexAccessType = SEQUENTIAL,
2522 .VertexCountPerInstance = vertexCount,
2523 .StartVertexLocation = firstVertex,
2524 .InstanceCount = instanceCount,
2525 .StartInstanceLocation = firstInstance,
2526 .BaseVertexLocation = 0);
2527 }
2528
2529 void anv_CmdDrawIndexed(
2530 VkCmdBuffer cmdBuffer,
2531 uint32_t firstIndex,
2532 uint32_t indexCount,
2533 int32_t vertexOffset,
2534 uint32_t firstInstance,
2535 uint32_t instanceCount)
2536 {
2537 struct anv_cmd_buffer *cmd_buffer = (struct anv_cmd_buffer *) cmdBuffer;
2538
2539 anv_cmd_buffer_flush_state(cmd_buffer);
2540
2541 anv_batch_emit(&cmd_buffer->batch, GEN8_3DPRIMITIVE,
2542 .VertexAccessType = RANDOM,
2543 .VertexCountPerInstance = indexCount,
2544 .StartVertexLocation = firstIndex,
2545 .InstanceCount = instanceCount,
2546 .StartInstanceLocation = firstInstance,
2547 .BaseVertexLocation = 0);
2548 }
2549
2550 static void
2551 anv_batch_lrm(struct anv_batch *batch,
2552 uint32_t reg, struct anv_bo *bo, uint32_t offset)
2553 {
2554 anv_batch_emit(batch, GEN8_MI_LOAD_REGISTER_MEM,
2555 .RegisterAddress = reg,
2556 .MemoryAddress = { bo, offset });
2557 }
2558
2559 static void
2560 anv_batch_lri(struct anv_batch *batch, uint32_t reg, uint32_t imm)
2561 {
2562 anv_batch_emit(batch, GEN8_MI_LOAD_REGISTER_IMM,
2563 .RegisterOffset = reg,
2564 .DataDWord = imm);
2565 }
2566
2567 /* Auto-Draw / Indirect Registers */
2568 #define GEN7_3DPRIM_END_OFFSET 0x2420
2569 #define GEN7_3DPRIM_START_VERTEX 0x2430
2570 #define GEN7_3DPRIM_VERTEX_COUNT 0x2434
2571 #define GEN7_3DPRIM_INSTANCE_COUNT 0x2438
2572 #define GEN7_3DPRIM_START_INSTANCE 0x243C
2573 #define GEN7_3DPRIM_BASE_VERTEX 0x2440
2574
2575 void anv_CmdDrawIndirect(
2576 VkCmdBuffer cmdBuffer,
2577 VkBuffer _buffer,
2578 VkDeviceSize offset,
2579 uint32_t count,
2580 uint32_t stride)
2581 {
2582 struct anv_cmd_buffer *cmd_buffer = (struct anv_cmd_buffer *) cmdBuffer;
2583 struct anv_buffer *buffer = (struct anv_buffer *) _buffer;
2584 struct anv_bo *bo = buffer->bo;
2585 uint32_t bo_offset = buffer->offset + offset;
2586
2587 anv_cmd_buffer_flush_state(cmd_buffer);
2588
2589 anv_batch_lrm(&cmd_buffer->batch, GEN7_3DPRIM_VERTEX_COUNT, bo, bo_offset);
2590 anv_batch_lrm(&cmd_buffer->batch, GEN7_3DPRIM_INSTANCE_COUNT, bo, bo_offset + 4);
2591 anv_batch_lrm(&cmd_buffer->batch, GEN7_3DPRIM_START_VERTEX, bo, bo_offset + 8);
2592 anv_batch_lrm(&cmd_buffer->batch, GEN7_3DPRIM_START_INSTANCE, bo, bo_offset + 12);
2593 anv_batch_lri(&cmd_buffer->batch, GEN7_3DPRIM_BASE_VERTEX, 0);
2594
2595 anv_batch_emit(&cmd_buffer->batch, GEN8_3DPRIMITIVE,
2596 .IndirectParameterEnable = true,
2597 .VertexAccessType = SEQUENTIAL);
2598 }
2599
2600 void anv_CmdDrawIndexedIndirect(
2601 VkCmdBuffer cmdBuffer,
2602 VkBuffer _buffer,
2603 VkDeviceSize offset,
2604 uint32_t count,
2605 uint32_t stride)
2606 {
2607 struct anv_cmd_buffer *cmd_buffer = (struct anv_cmd_buffer *) cmdBuffer;
2608 struct anv_buffer *buffer = (struct anv_buffer *) _buffer;
2609 struct anv_bo *bo = buffer->bo;
2610 uint32_t bo_offset = buffer->offset + offset;
2611
2612 anv_cmd_buffer_flush_state(cmd_buffer);
2613
2614 anv_batch_lrm(&cmd_buffer->batch, GEN7_3DPRIM_VERTEX_COUNT, bo, bo_offset);
2615 anv_batch_lrm(&cmd_buffer->batch, GEN7_3DPRIM_INSTANCE_COUNT, bo, bo_offset + 4);
2616 anv_batch_lrm(&cmd_buffer->batch, GEN7_3DPRIM_START_VERTEX, bo, bo_offset + 8);
2617 anv_batch_lrm(&cmd_buffer->batch, GEN7_3DPRIM_BASE_VERTEX, bo, bo_offset + 12);
2618 anv_batch_lrm(&cmd_buffer->batch, GEN7_3DPRIM_START_INSTANCE, bo, bo_offset + 16);
2619
2620 anv_batch_emit(&cmd_buffer->batch, GEN8_3DPRIMITIVE,
2621 .IndirectParameterEnable = true,
2622 .VertexAccessType = RANDOM);
2623 }
2624
2625 void anv_CmdDispatch(
2626 VkCmdBuffer cmdBuffer,
2627 uint32_t x,
2628 uint32_t y,
2629 uint32_t z)
2630 {
2631 stub();
2632 }
2633
2634 void anv_CmdDispatchIndirect(
2635 VkCmdBuffer cmdBuffer,
2636 VkBuffer buffer,
2637 VkDeviceSize offset)
2638 {
2639 stub();
2640 }
2641
2642 void anv_CmdSetEvent(
2643 VkCmdBuffer cmdBuffer,
2644 VkEvent event,
2645 VkPipeEvent pipeEvent)
2646 {
2647 stub();
2648 }
2649
2650 void anv_CmdResetEvent(
2651 VkCmdBuffer cmdBuffer,
2652 VkEvent event,
2653 VkPipeEvent pipeEvent)
2654 {
2655 stub();
2656 }
2657
2658 void anv_CmdWaitEvents(
2659 VkCmdBuffer cmdBuffer,
2660 VkWaitEvent waitEvent,
2661 uint32_t eventCount,
2662 const VkEvent* pEvents,
2663 uint32_t memBarrierCount,
2664 const void** ppMemBarriers)
2665 {
2666 stub();
2667 }
2668
2669 void anv_CmdPipelineBarrier(
2670 VkCmdBuffer cmdBuffer,
2671 VkWaitEvent waitEvent,
2672 uint32_t pipeEventCount,
2673 const VkPipeEvent* pPipeEvents,
2674 uint32_t memBarrierCount,
2675 const void** ppMemBarriers)
2676 {
2677 stub();
2678 }
2679
2680 static void
2681 anv_batch_emit_ps_depth_count(struct anv_batch *batch,
2682 struct anv_bo *bo, uint32_t offset)
2683 {
2684 anv_batch_emit(batch, GEN8_PIPE_CONTROL,
2685 .DestinationAddressType = DAT_PPGTT,
2686 .PostSyncOperation = WritePSDepthCount,
2687 .Address = { bo, offset }); /* FIXME: This is only lower 32 bits */
2688 }
2689
2690 void anv_CmdBeginQuery(
2691 VkCmdBuffer cmdBuffer,
2692 VkQueryPool queryPool,
2693 uint32_t slot,
2694 VkQueryControlFlags flags)
2695 {
2696 struct anv_cmd_buffer *cmd_buffer = (struct anv_cmd_buffer *) cmdBuffer;
2697 struct anv_query_pool *pool = (struct anv_query_pool *) queryPool;
2698
2699 switch (pool->type) {
2700 case VK_QUERY_TYPE_OCCLUSION:
2701 anv_batch_emit_ps_depth_count(&cmd_buffer->batch, &pool->bo, slot * 16);
2702 break;
2703
2704 case VK_QUERY_TYPE_PIPELINE_STATISTICS:
2705 break;
2706
2707 default:
2708 break;
2709 }
2710 }
2711
2712 void anv_CmdEndQuery(
2713 VkCmdBuffer cmdBuffer,
2714 VkQueryPool queryPool,
2715 uint32_t slot)
2716 {
2717 struct anv_cmd_buffer *cmd_buffer = (struct anv_cmd_buffer *) cmdBuffer;
2718 struct anv_query_pool *pool = (struct anv_query_pool *) queryPool;
2719
2720 switch (pool->type) {
2721 case VK_QUERY_TYPE_OCCLUSION:
2722 anv_batch_emit_ps_depth_count(&cmd_buffer->batch, &pool->bo, slot * 16 + 8);
2723 break;
2724
2725 case VK_QUERY_TYPE_PIPELINE_STATISTICS:
2726 break;
2727
2728 default:
2729 break;
2730 }
2731 }
2732
2733 void anv_CmdResetQueryPool(
2734 VkCmdBuffer cmdBuffer,
2735 VkQueryPool queryPool,
2736 uint32_t startQuery,
2737 uint32_t queryCount)
2738 {
2739 stub();
2740 }
2741
2742 #define TIMESTAMP 0x44070
2743
2744 void anv_CmdWriteTimestamp(
2745 VkCmdBuffer cmdBuffer,
2746 VkTimestampType timestampType,
2747 VkBuffer destBuffer,
2748 VkDeviceSize destOffset)
2749 {
2750 struct anv_cmd_buffer *cmd_buffer = (struct anv_cmd_buffer *) cmdBuffer;
2751 struct anv_buffer *buffer = (struct anv_buffer *) destBuffer;
2752 struct anv_bo *bo = buffer->bo;
2753
2754 switch (timestampType) {
2755 case VK_TIMESTAMP_TYPE_TOP:
2756 anv_batch_emit(&cmd_buffer->batch, GEN8_MI_STORE_REGISTER_MEM,
2757 .RegisterAddress = TIMESTAMP,
2758 .MemoryAddress = { bo, buffer->offset + destOffset });
2759 break;
2760
2761 case VK_TIMESTAMP_TYPE_BOTTOM:
2762 anv_batch_emit(&cmd_buffer->batch, GEN8_PIPE_CONTROL,
2763 .DestinationAddressType = DAT_PPGTT,
2764 .PostSyncOperation = WriteTimestamp,
2765 .Address = /* FIXME: This is only lower 32 bits */
2766 { bo, buffer->offset + destOffset });
2767 break;
2768
2769 default:
2770 break;
2771 }
2772 }
2773
2774 void anv_CmdCopyQueryPoolResults(
2775 VkCmdBuffer cmdBuffer,
2776 VkQueryPool queryPool,
2777 uint32_t startQuery,
2778 uint32_t queryCount,
2779 VkBuffer destBuffer,
2780 VkDeviceSize destOffset,
2781 VkDeviceSize destStride,
2782 VkQueryResultFlags flags)
2783 {
2784 stub();
2785 }
2786
2787 void anv_CmdInitAtomicCounters(
2788 VkCmdBuffer cmdBuffer,
2789 VkPipelineBindPoint pipelineBindPoint,
2790 uint32_t startCounter,
2791 uint32_t counterCount,
2792 const uint32_t* pData)
2793 {
2794 stub();
2795 }
2796
2797 void anv_CmdLoadAtomicCounters(
2798 VkCmdBuffer cmdBuffer,
2799 VkPipelineBindPoint pipelineBindPoint,
2800 uint32_t startCounter,
2801 uint32_t counterCount,
2802 VkBuffer srcBuffer,
2803 VkDeviceSize srcOffset)
2804 {
2805 stub();
2806 }
2807
2808 void anv_CmdSaveAtomicCounters(
2809 VkCmdBuffer cmdBuffer,
2810 VkPipelineBindPoint pipelineBindPoint,
2811 uint32_t startCounter,
2812 uint32_t counterCount,
2813 VkBuffer destBuffer,
2814 VkDeviceSize destOffset)
2815 {
2816 stub();
2817 }
2818
2819 VkResult anv_CreateFramebuffer(
2820 VkDevice _device,
2821 const VkFramebufferCreateInfo* pCreateInfo,
2822 VkFramebuffer* pFramebuffer)
2823 {
2824 struct anv_device *device = (struct anv_device *) _device;
2825 struct anv_framebuffer *framebuffer;
2826
2827 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO);
2828
2829 framebuffer = anv_device_alloc(device, sizeof(*framebuffer), 8,
2830 VK_SYSTEM_ALLOC_TYPE_API_OBJECT);
2831 if (framebuffer == NULL)
2832 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
2833
2834 framebuffer->color_attachment_count = pCreateInfo->colorAttachmentCount;
2835 for (uint32_t i = 0; i < pCreateInfo->colorAttachmentCount; i++) {
2836 framebuffer->color_attachments[i] =
2837 (struct anv_surface_view *) pCreateInfo->pColorAttachments[i].view;
2838 }
2839
2840 if (pCreateInfo->pDepthStencilAttachment) {
2841 framebuffer->depth_stencil =
2842 (struct anv_depth_stencil_view *) pCreateInfo->pDepthStencilAttachment->view;
2843 }
2844
2845 framebuffer->sample_count = pCreateInfo->sampleCount;
2846 framebuffer->width = pCreateInfo->width;
2847 framebuffer->height = pCreateInfo->height;
2848 framebuffer->layers = pCreateInfo->layers;
2849
2850 vkCreateDynamicViewportState((VkDevice) device,
2851 &(VkDynamicVpStateCreateInfo) {
2852 .sType = VK_STRUCTURE_TYPE_DYNAMIC_VP_STATE_CREATE_INFO,
2853 .viewportAndScissorCount = 2,
2854 .pViewports = (VkViewport[]) {
2855 {
2856 .originX = 0,
2857 .originY = 0,
2858 .width = pCreateInfo->width,
2859 .height = pCreateInfo->height,
2860 .minDepth = 0,
2861 .maxDepth = 1
2862 },
2863 },
2864 .pScissors = (VkRect[]) {
2865 { { 0, 0 },
2866 { pCreateInfo->width, pCreateInfo->height } },
2867 }
2868 },
2869 &framebuffer->vp_state);
2870
2871 *pFramebuffer = (VkFramebuffer) framebuffer;
2872
2873 return VK_SUCCESS;
2874 }
2875
2876 VkResult anv_CreateRenderPass(
2877 VkDevice _device,
2878 const VkRenderPassCreateInfo* pCreateInfo,
2879 VkRenderPass* pRenderPass)
2880 {
2881 struct anv_device *device = (struct anv_device *) _device;
2882 struct anv_render_pass *pass;
2883 size_t size;
2884
2885 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO);
2886
2887 size = sizeof(*pass) +
2888 pCreateInfo->layers * sizeof(struct anv_render_pass_layer);
2889 pass = anv_device_alloc(device, size, 8,
2890 VK_SYSTEM_ALLOC_TYPE_API_OBJECT);
2891 if (pass == NULL)
2892 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
2893
2894 pass->render_area = pCreateInfo->renderArea;
2895
2896 pass->num_layers = pCreateInfo->layers;
2897
2898 pass->num_clear_layers = 0;
2899 for (uint32_t i = 0; i < pCreateInfo->layers; i++) {
2900 pass->layers[i].color_load_op = pCreateInfo->pColorLoadOps[i];
2901 pass->layers[i].clear_color = pCreateInfo->pColorLoadClearValues[i];
2902 if (pass->layers[i].color_load_op == VK_ATTACHMENT_LOAD_OP_CLEAR)
2903 pass->num_clear_layers++;
2904 }
2905
2906 *pRenderPass = (VkRenderPass) pass;
2907
2908 return VK_SUCCESS;
2909 }
2910
2911 void
2912 anv_cmd_buffer_fill_render_targets(struct anv_cmd_buffer *cmd_buffer)
2913 {
2914 struct anv_framebuffer *framebuffer = cmd_buffer->framebuffer;
2915 struct anv_bindings *bindings = cmd_buffer->bindings;
2916
2917 for (uint32_t i = 0; i < framebuffer->color_attachment_count; i++) {
2918 struct anv_surface_view *view = framebuffer->color_attachments[i];
2919
2920 bindings->descriptors[VK_SHADER_STAGE_FRAGMENT].surfaces[i] = view->surface_state.offset;
2921 bindings->descriptors[VK_SHADER_STAGE_FRAGMENT].relocs[i].bo = view->bo;
2922 bindings->descriptors[VK_SHADER_STAGE_FRAGMENT].relocs[i].offset = view->offset;
2923 }
2924 cmd_buffer->dirty |= ANV_CMD_BUFFER_DESCRIPTOR_SET_DIRTY;
2925 }
2926
2927 void anv_CmdBeginRenderPass(
2928 VkCmdBuffer cmdBuffer,
2929 const VkRenderPassBegin* pRenderPassBegin)
2930 {
2931 struct anv_cmd_buffer *cmd_buffer = (struct anv_cmd_buffer *) cmdBuffer;
2932 struct anv_render_pass *pass = (struct anv_render_pass *) pRenderPassBegin->renderPass;
2933 struct anv_framebuffer *framebuffer =
2934 (struct anv_framebuffer *) pRenderPassBegin->framebuffer;
2935
2936 cmd_buffer->framebuffer = framebuffer;
2937
2938 anv_batch_emit(&cmd_buffer->batch, GEN8_3DSTATE_DRAWING_RECTANGLE,
2939 .ClippedDrawingRectangleYMin = pass->render_area.offset.y,
2940 .ClippedDrawingRectangleXMin = pass->render_area.offset.x,
2941 .ClippedDrawingRectangleYMax =
2942 pass->render_area.offset.y + pass->render_area.extent.height - 1,
2943 .ClippedDrawingRectangleXMax =
2944 pass->render_area.offset.x + pass->render_area.extent.width - 1,
2945 .DrawingRectangleOriginY = 0,
2946 .DrawingRectangleOriginX = 0);
2947
2948 anv_cmd_buffer_fill_render_targets(cmd_buffer);
2949
2950 anv_cmd_buffer_clear(cmd_buffer, pass);
2951 }
2952
2953 void anv_CmdEndRenderPass(
2954 VkCmdBuffer cmdBuffer,
2955 VkRenderPass renderPass)
2956 {
2957 /* Emit a flushing pipe control at the end of a pass. This is kind of a
2958 * hack but it ensures that render targets always actually get written.
2959 * Eventually, we should do flushing based on image format transitions
2960 * or something of that nature.
2961 */
2962 struct anv_cmd_buffer *cmd_buffer = (struct anv_cmd_buffer *)cmdBuffer;
2963 anv_batch_emit(&cmd_buffer->batch, GEN8_PIPE_CONTROL,
2964 .PostSyncOperation = NoWrite,
2965 .RenderTargetCacheFlushEnable = true,
2966 .InstructionCacheInvalidateEnable = true,
2967 .DepthCacheFlushEnable = true,
2968 .VFCacheInvalidationEnable = true,
2969 .TextureCacheInvalidationEnable = true,
2970 .CommandStreamerStallEnable = true);
2971
2972 stub();
2973 }