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