vk: Start Implementing vkGetFormatInfo()
[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 stub_return(VK_UNSUPPORTED);
1533 }
1534
1535 VkResult anv_EndDescriptorPoolUpdate(
1536 VkDevice device,
1537 VkCmdBuffer cmd)
1538 {
1539 stub_return(VK_UNSUPPORTED);
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 stub_return(VK_UNSUPPORTED);
1550 }
1551
1552 VkResult anv_ResetDescriptorPool(
1553 VkDevice device,
1554 VkDescriptorPool descriptorPool)
1555 {
1556 stub_return(VK_UNSUPPORTED);
1557 }
1558
1559 VkResult anv_AllocDescriptorSets(
1560 VkDevice _device,
1561 VkDescriptorPool descriptorPool,
1562 VkDescriptorSetUsage setUsage,
1563 uint32_t count,
1564 const VkDescriptorSetLayout* pSetLayouts,
1565 VkDescriptorSet* pDescriptorSets,
1566 uint32_t* pCount)
1567 {
1568 struct anv_device *device = (struct anv_device *) _device;
1569 const struct anv_descriptor_set_layout *layout;
1570 struct anv_descriptor_set *set;
1571 size_t size;
1572
1573 for (uint32_t i = 0; i < count; i++) {
1574 layout = (struct anv_descriptor_set_layout *) pSetLayouts[i];
1575 size = sizeof(*set) + layout->count * sizeof(set->descriptors[0]);
1576 set = anv_device_alloc(device, size, 8,
1577 VK_SYSTEM_ALLOC_TYPE_API_OBJECT);
1578 if (!set) {
1579 *pCount = i;
1580 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
1581 }
1582
1583 pDescriptorSets[i] = (VkDescriptorSet) set;
1584 }
1585
1586 *pCount = count;
1587
1588 return VK_UNSUPPORTED;
1589 }
1590
1591 void anv_ClearDescriptorSets(
1592 VkDevice device,
1593 VkDescriptorPool descriptorPool,
1594 uint32_t count,
1595 const VkDescriptorSet* pDescriptorSets)
1596 {
1597 stub();
1598 }
1599
1600 void anv_UpdateDescriptors(
1601 VkDevice _device,
1602 VkDescriptorSet descriptorSet,
1603 uint32_t updateCount,
1604 const void** ppUpdateArray)
1605 {
1606 struct anv_descriptor_set *set = (struct anv_descriptor_set *) descriptorSet;
1607 VkUpdateSamplers *update_samplers;
1608 VkUpdateSamplerTextures *update_sampler_textures;
1609 VkUpdateImages *update_images;
1610 VkUpdateBuffers *update_buffers;
1611 VkUpdateAsCopy *update_as_copy;
1612
1613 for (uint32_t i = 0; i < updateCount; i++) {
1614 const struct anv_common *common = ppUpdateArray[i];
1615
1616 switch (common->sType) {
1617 case VK_STRUCTURE_TYPE_UPDATE_SAMPLERS:
1618 update_samplers = (VkUpdateSamplers *) common;
1619
1620 for (uint32_t j = 0; j < update_samplers->count; j++) {
1621 set->descriptors[update_samplers->binding + j].sampler =
1622 (struct anv_sampler *) update_samplers->pSamplers[j];
1623 }
1624 break;
1625
1626 case VK_STRUCTURE_TYPE_UPDATE_SAMPLER_TEXTURES:
1627 /* FIXME: Shouldn't this be *_UPDATE_SAMPLER_IMAGES? */
1628 update_sampler_textures = (VkUpdateSamplerTextures *) common;
1629
1630 for (uint32_t j = 0; j < update_sampler_textures->count; j++) {
1631 set->descriptors[update_sampler_textures->binding + j].view =
1632 (struct anv_surface_view *)
1633 update_sampler_textures->pSamplerImageViews[j].pImageView->view;
1634 set->descriptors[update_sampler_textures->binding + j].sampler =
1635 (struct anv_sampler *)
1636 update_sampler_textures->pSamplerImageViews[j].sampler;
1637 }
1638 break;
1639
1640 case VK_STRUCTURE_TYPE_UPDATE_IMAGES:
1641 update_images = (VkUpdateImages *) common;
1642
1643 for (uint32_t j = 0; j < update_images->count; j++) {
1644 set->descriptors[update_images->binding + j].view =
1645 (struct anv_surface_view *) update_images->pImageViews[j].view;
1646 }
1647 break;
1648
1649 case VK_STRUCTURE_TYPE_UPDATE_BUFFERS:
1650 update_buffers = (VkUpdateBuffers *) common;
1651
1652 for (uint32_t j = 0; j < update_buffers->count; j++) {
1653 set->descriptors[update_buffers->binding + j].view =
1654 (struct anv_surface_view *) update_buffers->pBufferViews[j].view;
1655 }
1656 /* FIXME: descriptor arrays? */
1657 break;
1658
1659 case VK_STRUCTURE_TYPE_UPDATE_AS_COPY:
1660 update_as_copy = (VkUpdateAsCopy *) common;
1661 (void) update_as_copy;
1662 break;
1663
1664 default:
1665 break;
1666 }
1667 }
1668 }
1669
1670 // State object functions
1671
1672 static inline int64_t
1673 clamp_int64(int64_t x, int64_t min, int64_t max)
1674 {
1675 if (x < min)
1676 return min;
1677 else if (x < max)
1678 return x;
1679 else
1680 return max;
1681 }
1682
1683 VkResult anv_CreateDynamicViewportState(
1684 VkDevice _device,
1685 const VkDynamicVpStateCreateInfo* pCreateInfo,
1686 VkDynamicVpState* pState)
1687 {
1688 struct anv_device *device = (struct anv_device *) _device;
1689 struct anv_dynamic_vp_state *state;
1690
1691 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_DYNAMIC_VP_STATE_CREATE_INFO);
1692
1693 state = anv_device_alloc(device, sizeof(*state), 8,
1694 VK_SYSTEM_ALLOC_TYPE_API_OBJECT);
1695 if (state == NULL)
1696 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
1697
1698 unsigned count = pCreateInfo->viewportAndScissorCount;
1699 state->sf_clip_vp = anv_state_pool_alloc(&device->dynamic_state_pool,
1700 count * 64, 64);
1701 state->cc_vp = anv_state_pool_alloc(&device->dynamic_state_pool,
1702 count * 8, 32);
1703 state->scissor = anv_state_pool_alloc(&device->dynamic_state_pool,
1704 count * 32, 32);
1705
1706 for (uint32_t i = 0; i < pCreateInfo->viewportAndScissorCount; i++) {
1707 const VkViewport *vp = &pCreateInfo->pViewports[i];
1708 const VkRect *s = &pCreateInfo->pScissors[i];
1709
1710 struct GEN8_SF_CLIP_VIEWPORT sf_clip_viewport = {
1711 .ViewportMatrixElementm00 = vp->width / 2,
1712 .ViewportMatrixElementm11 = vp->height / 2,
1713 .ViewportMatrixElementm22 = (vp->maxDepth - vp->minDepth) / 2,
1714 .ViewportMatrixElementm30 = vp->originX + vp->width / 2,
1715 .ViewportMatrixElementm31 = vp->originY + vp->height / 2,
1716 .ViewportMatrixElementm32 = (vp->maxDepth + vp->minDepth) / 2,
1717 .XMinClipGuardband = -1.0f,
1718 .XMaxClipGuardband = 1.0f,
1719 .YMinClipGuardband = -1.0f,
1720 .YMaxClipGuardband = 1.0f,
1721 .XMinViewPort = vp->originX,
1722 .XMaxViewPort = vp->originX + vp->width - 1,
1723 .YMinViewPort = vp->originY,
1724 .YMaxViewPort = vp->originY + vp->height - 1,
1725 };
1726
1727 struct GEN8_CC_VIEWPORT cc_viewport = {
1728 .MinimumDepth = vp->minDepth,
1729 .MaximumDepth = vp->maxDepth
1730 };
1731
1732 /* Since xmax and ymax are inclusive, we have to have xmax < xmin or
1733 * ymax < ymin for empty clips. In case clip x, y, width height are all
1734 * 0, the clamps below produce 0 for xmin, ymin, xmax, ymax, which isn't
1735 * what we want. Just special case empty clips and produce a canonical
1736 * empty clip. */
1737 static const struct GEN8_SCISSOR_RECT empty_scissor = {
1738 .ScissorRectangleYMin = 1,
1739 .ScissorRectangleXMin = 1,
1740 .ScissorRectangleYMax = 0,
1741 .ScissorRectangleXMax = 0
1742 };
1743
1744 const int max = 0xffff;
1745 struct GEN8_SCISSOR_RECT scissor = {
1746 /* Do this math using int64_t so overflow gets clamped correctly. */
1747 .ScissorRectangleYMin = clamp_int64(s->offset.y, 0, max),
1748 .ScissorRectangleXMin = clamp_int64(s->offset.x, 0, max),
1749 .ScissorRectangleYMax = clamp_int64((uint64_t) s->offset.y + s->extent.height - 1, 0, max),
1750 .ScissorRectangleXMax = clamp_int64((uint64_t) s->offset.x + s->extent.width - 1, 0, max)
1751 };
1752
1753 GEN8_SF_CLIP_VIEWPORT_pack(NULL, state->sf_clip_vp.map + i * 64, &sf_clip_viewport);
1754 GEN8_CC_VIEWPORT_pack(NULL, state->cc_vp.map + i * 32, &cc_viewport);
1755
1756 if (s->extent.width <= 0 || s->extent.height <= 0) {
1757 GEN8_SCISSOR_RECT_pack(NULL, state->scissor.map + i * 32, &empty_scissor);
1758 } else {
1759 GEN8_SCISSOR_RECT_pack(NULL, state->scissor.map + i * 32, &scissor);
1760 }
1761 }
1762
1763 *pState = (VkDynamicVpState) state;
1764
1765 return VK_SUCCESS;
1766 }
1767
1768 VkResult anv_CreateDynamicRasterState(
1769 VkDevice _device,
1770 const VkDynamicRsStateCreateInfo* pCreateInfo,
1771 VkDynamicRsState* pState)
1772 {
1773 struct anv_device *device = (struct anv_device *) _device;
1774 struct anv_dynamic_rs_state *state;
1775
1776 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_DYNAMIC_RS_STATE_CREATE_INFO);
1777
1778 state = anv_device_alloc(device, sizeof(*state), 8,
1779 VK_SYSTEM_ALLOC_TYPE_API_OBJECT);
1780 if (state == NULL)
1781 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
1782
1783 /* Missing these:
1784 * float depthBias;
1785 * float depthBiasClamp;
1786 * float slopeScaledDepthBias;
1787 * float pointFadeThreshold;
1788 * // optional (GL45) - Size of point fade threshold
1789 */
1790
1791 struct GEN8_3DSTATE_SF sf = {
1792 GEN8_3DSTATE_SF_header,
1793 .LineWidth = pCreateInfo->lineWidth,
1794 .PointWidth = pCreateInfo->pointSize,
1795 };
1796
1797 GEN8_3DSTATE_SF_pack(NULL, state->state_sf, &sf);
1798
1799 *pState = (VkDynamicRsState) state;
1800
1801 return VK_SUCCESS;
1802 }
1803
1804 VkResult anv_CreateDynamicColorBlendState(
1805 VkDevice _device,
1806 const VkDynamicCbStateCreateInfo* pCreateInfo,
1807 VkDynamicCbState* pState)
1808 {
1809 struct anv_device *device = (struct anv_device *) _device;
1810 struct anv_dynamic_cb_state *state;
1811
1812 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_DYNAMIC_CB_STATE_CREATE_INFO);
1813
1814 state = anv_device_alloc(device, sizeof(*state), 8,
1815 VK_SYSTEM_ALLOC_TYPE_API_OBJECT);
1816 if (state == NULL)
1817 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
1818
1819 *pState = (VkDynamicCbState) state;
1820
1821 return VK_SUCCESS;
1822 }
1823
1824 VkResult anv_CreateDynamicDepthStencilState(
1825 VkDevice device,
1826 const VkDynamicDsStateCreateInfo* pCreateInfo,
1827 VkDynamicDsState* pState)
1828 {
1829 stub_return(VK_UNSUPPORTED);
1830 }
1831
1832 // Command buffer functions
1833
1834 VkResult anv_CreateCommandBuffer(
1835 VkDevice _device,
1836 const VkCmdBufferCreateInfo* pCreateInfo,
1837 VkCmdBuffer* pCmdBuffer)
1838 {
1839 struct anv_device *device = (struct anv_device *) _device;
1840 struct anv_cmd_buffer *cmd_buffer;
1841 VkResult result;
1842
1843 cmd_buffer = anv_device_alloc(device, sizeof(*cmd_buffer), 8,
1844 VK_SYSTEM_ALLOC_TYPE_API_OBJECT);
1845 if (cmd_buffer == NULL)
1846 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
1847
1848 cmd_buffer->device = device;
1849 cmd_buffer->rs_state = NULL;
1850 cmd_buffer->vp_state = NULL;
1851 memset(&cmd_buffer->default_bindings, 0, sizeof(cmd_buffer->default_bindings));
1852 cmd_buffer->bindings = &cmd_buffer->default_bindings;
1853
1854 result = anv_batch_init(&cmd_buffer->batch, device);
1855 if (result != VK_SUCCESS)
1856 goto fail;
1857
1858 cmd_buffer->exec2_objects =
1859 anv_device_alloc(device, 8192 * sizeof(cmd_buffer->exec2_objects[0]), 8,
1860 VK_SYSTEM_ALLOC_TYPE_API_OBJECT);
1861 if (cmd_buffer->exec2_objects == NULL) {
1862 result = vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
1863 goto fail_batch;
1864 }
1865
1866 cmd_buffer->exec2_bos =
1867 anv_device_alloc(device, 8192 * sizeof(cmd_buffer->exec2_bos[0]), 8,
1868 VK_SYSTEM_ALLOC_TYPE_API_OBJECT);
1869 if (cmd_buffer->exec2_bos == NULL) {
1870 result = vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
1871 goto fail_exec2_objects;
1872 }
1873
1874 anv_state_stream_init(&cmd_buffer->surface_state_stream,
1875 &device->surface_state_block_pool);
1876 anv_state_stream_init(&cmd_buffer->dynamic_state_stream,
1877 &device->dynamic_state_block_pool);
1878
1879 cmd_buffer->dirty = 0;
1880 cmd_buffer->vb_dirty = 0;
1881
1882 *pCmdBuffer = (VkCmdBuffer) cmd_buffer;
1883
1884 return VK_SUCCESS;
1885
1886 fail_exec2_objects:
1887 anv_device_free(device, cmd_buffer->exec2_objects);
1888 fail_batch:
1889 anv_batch_finish(&cmd_buffer->batch, device);
1890 fail:
1891 anv_device_free(device, cmd_buffer);
1892
1893 return result;
1894 }
1895
1896 VkResult anv_BeginCommandBuffer(
1897 VkCmdBuffer cmdBuffer,
1898 const VkCmdBufferBeginInfo* pBeginInfo)
1899 {
1900 struct anv_cmd_buffer *cmd_buffer = (struct anv_cmd_buffer *) cmdBuffer;
1901 struct anv_device *device = cmd_buffer->device;
1902
1903 anv_batch_emit(&cmd_buffer->batch, GEN8_PIPELINE_SELECT,
1904 .PipelineSelection = _3D);
1905 anv_batch_emit(&cmd_buffer->batch, GEN8_STATE_SIP);
1906
1907 anv_batch_emit(&cmd_buffer->batch, GEN8_STATE_BASE_ADDRESS,
1908 .GeneralStateBaseAddress = { NULL, 0 },
1909 .GeneralStateBaseAddressModifyEnable = true,
1910 .GeneralStateBufferSize = 0xfffff,
1911 .GeneralStateBufferSizeModifyEnable = true,
1912
1913 .SurfaceStateBaseAddress = { &device->surface_state_block_pool.bo, 0 },
1914 .SurfaceStateMemoryObjectControlState = 0, /* FIXME: MOCS */
1915 .SurfaceStateBaseAddressModifyEnable = true,
1916
1917 .DynamicStateBaseAddress = { &device->dynamic_state_block_pool.bo, 0 },
1918 .DynamicStateBaseAddressModifyEnable = true,
1919 .DynamicStateBufferSize = 0xfffff,
1920 .DynamicStateBufferSizeModifyEnable = true,
1921
1922 .IndirectObjectBaseAddress = { NULL, 0 },
1923 .IndirectObjectBaseAddressModifyEnable = true,
1924 .IndirectObjectBufferSize = 0xfffff,
1925 .IndirectObjectBufferSizeModifyEnable = true,
1926
1927 .InstructionBaseAddress = { &device->instruction_block_pool.bo, 0 },
1928 .InstructionBaseAddressModifyEnable = true,
1929 .InstructionBufferSize = 0xfffff,
1930 .InstructionBuffersizeModifyEnable = true);
1931
1932 anv_batch_emit(&cmd_buffer->batch, GEN8_3DSTATE_VF_STATISTICS,
1933 .StatisticsEnable = true);
1934 anv_batch_emit(&cmd_buffer->batch, GEN8_3DSTATE_HS, .Enable = false);
1935 anv_batch_emit(&cmd_buffer->batch, GEN8_3DSTATE_TE, .TEEnable = false);
1936 anv_batch_emit(&cmd_buffer->batch, GEN8_3DSTATE_DS, .FunctionEnable = false);
1937 anv_batch_emit(&cmd_buffer->batch, GEN8_3DSTATE_STREAMOUT, .SOFunctionEnable = false);
1938
1939 anv_batch_emit(&cmd_buffer->batch, GEN8_3DSTATE_PUSH_CONSTANT_ALLOC_VS,
1940 .ConstantBufferOffset = 0,
1941 .ConstantBufferSize = 4);
1942 anv_batch_emit(&cmd_buffer->batch, GEN8_3DSTATE_PUSH_CONSTANT_ALLOC_GS,
1943 .ConstantBufferOffset = 4,
1944 .ConstantBufferSize = 4);
1945 anv_batch_emit(&cmd_buffer->batch, GEN8_3DSTATE_PUSH_CONSTANT_ALLOC_PS,
1946 .ConstantBufferOffset = 8,
1947 .ConstantBufferSize = 4);
1948
1949 anv_batch_emit(&cmd_buffer->batch, GEN8_3DSTATE_WM_CHROMAKEY,
1950 .ChromaKeyKillEnable = false);
1951 anv_batch_emit(&cmd_buffer->batch, GEN8_3DSTATE_SBE_SWIZ);
1952 anv_batch_emit(&cmd_buffer->batch, GEN8_3DSTATE_AA_LINE_PARAMETERS);
1953
1954 /* Hardcoded state: */
1955 anv_batch_emit(&cmd_buffer->batch, GEN8_3DSTATE_DEPTH_BUFFER,
1956 .SurfaceType = SURFTYPE_2D,
1957 .Width = 1,
1958 .Height = 1,
1959 .SurfaceFormat = D16_UNORM,
1960 .SurfaceBaseAddress = { NULL, 0 },
1961 .HierarchicalDepthBufferEnable = 0);
1962
1963 anv_batch_emit(&cmd_buffer->batch, GEN8_3DSTATE_WM_DEPTH_STENCIL,
1964 .DepthTestEnable = false,
1965 .DepthBufferWriteEnable = false);
1966
1967 return VK_SUCCESS;
1968 }
1969
1970 static void
1971 anv_cmd_buffer_add_bo(struct anv_cmd_buffer *cmd_buffer,
1972 struct anv_bo *bo, struct anv_reloc_list *list)
1973 {
1974 struct drm_i915_gem_exec_object2 *obj;
1975
1976 bo->index = cmd_buffer->bo_count;
1977 obj = &cmd_buffer->exec2_objects[bo->index];
1978 cmd_buffer->exec2_bos[bo->index] = bo;
1979 cmd_buffer->bo_count++;
1980
1981 obj->handle = bo->gem_handle;
1982 obj->relocation_count = 0;
1983 obj->relocs_ptr = 0;
1984 obj->alignment = 0;
1985 obj->offset = bo->offset;
1986 obj->flags = 0;
1987 obj->rsvd1 = 0;
1988 obj->rsvd2 = 0;
1989
1990 if (list) {
1991 obj->relocation_count = list->num_relocs;
1992 obj->relocs_ptr = (uintptr_t) list->relocs;
1993 }
1994 }
1995
1996 static void
1997 anv_cmd_buffer_add_validate_bos(struct anv_cmd_buffer *cmd_buffer,
1998 struct anv_reloc_list *list)
1999 {
2000 struct anv_bo *bo, *batch_bo;
2001
2002 batch_bo = &cmd_buffer->batch.bo;
2003 for (size_t i = 0; i < list->num_relocs; i++) {
2004 bo = list->reloc_bos[i];
2005 /* Skip any relocations targeting the batch bo. We need to make sure
2006 * it's the last in the list so we'll add it manually later.
2007 */
2008 if (bo == batch_bo)
2009 continue;
2010 if (bo->index < cmd_buffer->bo_count && cmd_buffer->exec2_bos[bo->index] == bo)
2011 continue;
2012
2013 anv_cmd_buffer_add_bo(cmd_buffer, bo, NULL);
2014 }
2015 }
2016
2017 static void
2018 anv_cmd_buffer_process_relocs(struct anv_cmd_buffer *cmd_buffer,
2019 struct anv_reloc_list *list)
2020 {
2021 struct anv_bo *bo;
2022
2023 /* If the kernel supports I915_EXEC_NO_RELOC, it will compare offset in
2024 * struct drm_i915_gem_exec_object2 against the bos current offset and if
2025 * all bos haven't moved it will skip relocation processing alltogether.
2026 * If I915_EXEC_NO_RELOC is not supported, the kernel ignores the incoming
2027 * value of offset so we can set it either way. For that to work we need
2028 * to make sure all relocs use the same presumed offset.
2029 */
2030
2031 for (size_t i = 0; i < list->num_relocs; i++) {
2032 bo = list->reloc_bos[i];
2033 if (bo->offset != list->relocs[i].presumed_offset)
2034 cmd_buffer->need_reloc = true;
2035
2036 list->relocs[i].target_handle = bo->index;
2037 }
2038 }
2039
2040 VkResult anv_EndCommandBuffer(
2041 VkCmdBuffer cmdBuffer)
2042 {
2043 struct anv_cmd_buffer *cmd_buffer = (struct anv_cmd_buffer *) cmdBuffer;
2044 struct anv_device *device = cmd_buffer->device;
2045 struct anv_batch *batch = &cmd_buffer->batch;
2046
2047 anv_batch_emit(batch, GEN8_MI_BATCH_BUFFER_END);
2048
2049 /* Round batch up to an even number of dwords. */
2050 if ((batch->next - batch->bo.map) & 4)
2051 anv_batch_emit(batch, GEN8_MI_NOOP);
2052
2053 cmd_buffer->bo_count = 0;
2054 cmd_buffer->need_reloc = false;
2055
2056 /* Lock for access to bo->index. */
2057 pthread_mutex_lock(&device->mutex);
2058
2059 /* Add block pool bos first so we can add them with their relocs. */
2060 anv_cmd_buffer_add_bo(cmd_buffer, &device->surface_state_block_pool.bo,
2061 &batch->surf_relocs);
2062
2063 anv_cmd_buffer_add_validate_bos(cmd_buffer, &batch->surf_relocs);
2064 anv_cmd_buffer_add_validate_bos(cmd_buffer, &batch->cmd_relocs);
2065 anv_cmd_buffer_add_bo(cmd_buffer, &batch->bo, &batch->cmd_relocs);
2066 anv_cmd_buffer_process_relocs(cmd_buffer, &batch->surf_relocs);
2067 anv_cmd_buffer_process_relocs(cmd_buffer, &batch->cmd_relocs);
2068
2069 cmd_buffer->execbuf.buffers_ptr = (uintptr_t) cmd_buffer->exec2_objects;
2070 cmd_buffer->execbuf.buffer_count = cmd_buffer->bo_count;
2071 cmd_buffer->execbuf.batch_start_offset = 0;
2072 cmd_buffer->execbuf.batch_len = batch->next - batch->bo.map;
2073 cmd_buffer->execbuf.cliprects_ptr = 0;
2074 cmd_buffer->execbuf.num_cliprects = 0;
2075 cmd_buffer->execbuf.DR1 = 0;
2076 cmd_buffer->execbuf.DR4 = 0;
2077
2078 cmd_buffer->execbuf.flags = I915_EXEC_HANDLE_LUT;
2079 if (!cmd_buffer->need_reloc)
2080 cmd_buffer->execbuf.flags |= I915_EXEC_NO_RELOC;
2081 cmd_buffer->execbuf.flags |= I915_EXEC_RENDER;
2082 cmd_buffer->execbuf.rsvd1 = device->context_id;
2083 cmd_buffer->execbuf.rsvd2 = 0;
2084
2085 pthread_mutex_unlock(&device->mutex);
2086
2087 return VK_SUCCESS;
2088 }
2089
2090 VkResult anv_ResetCommandBuffer(
2091 VkCmdBuffer cmdBuffer)
2092 {
2093 struct anv_cmd_buffer *cmd_buffer = (struct anv_cmd_buffer *) cmdBuffer;
2094
2095 anv_batch_reset(&cmd_buffer->batch);
2096
2097 return VK_SUCCESS;
2098 }
2099
2100 // Command buffer building functions
2101
2102 void anv_CmdBindPipeline(
2103 VkCmdBuffer cmdBuffer,
2104 VkPipelineBindPoint pipelineBindPoint,
2105 VkPipeline _pipeline)
2106 {
2107 struct anv_cmd_buffer *cmd_buffer = (struct anv_cmd_buffer *) cmdBuffer;
2108
2109 cmd_buffer->pipeline = (struct anv_pipeline *) _pipeline;
2110 cmd_buffer->dirty |= ANV_CMD_BUFFER_PIPELINE_DIRTY;
2111 }
2112
2113 void anv_CmdBindDynamicStateObject(
2114 VkCmdBuffer cmdBuffer,
2115 VkStateBindPoint stateBindPoint,
2116 VkDynamicStateObject dynamicState)
2117 {
2118 struct anv_cmd_buffer *cmd_buffer = (struct anv_cmd_buffer *) cmdBuffer;
2119 struct anv_dynamic_vp_state *vp_state;
2120
2121 switch (stateBindPoint) {
2122 case VK_STATE_BIND_POINT_VIEWPORT:
2123 vp_state = (struct anv_dynamic_vp_state *) dynamicState;
2124 /* We emit state immediately, but set cmd_buffer->vp_state to indicate
2125 * that vp state has been set in this command buffer. */
2126 cmd_buffer->vp_state = vp_state;
2127 anv_batch_emit(&cmd_buffer->batch, GEN8_3DSTATE_SCISSOR_STATE_POINTERS,
2128 .ScissorRectPointer = vp_state->scissor.offset);
2129 anv_batch_emit(&cmd_buffer->batch, GEN8_3DSTATE_VIEWPORT_STATE_POINTERS_CC,
2130 .CCViewportPointer = vp_state->cc_vp.offset);
2131 anv_batch_emit(&cmd_buffer->batch, GEN8_3DSTATE_VIEWPORT_STATE_POINTERS_SF_CLIP,
2132 .SFClipViewportPointer = vp_state->sf_clip_vp.offset);
2133 break;
2134 case VK_STATE_BIND_POINT_RASTER:
2135 cmd_buffer->rs_state = (struct anv_dynamic_rs_state *) dynamicState;
2136 cmd_buffer->dirty |= ANV_CMD_BUFFER_RS_DIRTY;
2137 break;
2138 case VK_STATE_BIND_POINT_COLOR_BLEND:
2139 case VK_STATE_BIND_POINT_DEPTH_STENCIL:
2140 break;
2141 default:
2142 break;
2143 };
2144 }
2145
2146 void anv_CmdBindDescriptorSets(
2147 VkCmdBuffer cmdBuffer,
2148 VkPipelineBindPoint pipelineBindPoint,
2149 uint32_t firstSet,
2150 uint32_t setCount,
2151 const VkDescriptorSet* pDescriptorSets,
2152 uint32_t dynamicOffsetCount,
2153 const uint32_t* pDynamicOffsets)
2154 {
2155 struct anv_cmd_buffer *cmd_buffer = (struct anv_cmd_buffer *) cmdBuffer;
2156 struct anv_pipeline_layout *layout = cmd_buffer->pipeline->layout;
2157 struct anv_bindings *bindings = cmd_buffer->bindings;
2158
2159 uint32_t offset = 0;
2160 for (uint32_t i = 0; i < setCount; i++) {
2161 struct anv_descriptor_set *set =
2162 (struct anv_descriptor_set *) pDescriptorSets[i];
2163 struct anv_descriptor_set_layout *set_layout = layout->set[firstSet + i].layout;
2164
2165 for (uint32_t s = 0; s < VK_NUM_SHADER_STAGE; s++) {
2166 uint32_t *surface_to_desc = set_layout->stage[s].surface_start;
2167 uint32_t *sampler_to_desc = set_layout->stage[s].sampler_start;
2168 uint32_t bias = s == VK_SHADER_STAGE_FRAGMENT ? MAX_RTS : 0;
2169 uint32_t start;
2170
2171 start = bias + layout->set[firstSet + i].surface_start[s];
2172 for (uint32_t b = 0; b < set_layout->stage[s].surface_count; b++) {
2173 struct anv_surface_view *view = set->descriptors[surface_to_desc[b]].view;
2174
2175 bindings->descriptors[s].surfaces[start + b] =
2176 view->surface_state.offset;
2177 bindings->descriptors[s].relocs[start + b].bo = view->bo;
2178 bindings->descriptors[s].relocs[start + b].offset = view->offset;
2179 }
2180
2181 start = layout->set[firstSet + i].sampler_start[s];
2182 for (uint32_t b = 0; b < set_layout->stage[s].sampler_count; b++) {
2183 struct anv_sampler *sampler = set->descriptors[sampler_to_desc[b]].sampler;
2184
2185 memcpy(&bindings->descriptors[s].samplers[start + b],
2186 sampler->state, sizeof(sampler->state));
2187 }
2188 }
2189
2190 offset += layout->set[firstSet + i].layout->num_dynamic_buffers;
2191 }
2192
2193 cmd_buffer->dirty |= ANV_CMD_BUFFER_DESCRIPTOR_SET_DIRTY;
2194 }
2195
2196 void anv_CmdBindIndexBuffer(
2197 VkCmdBuffer cmdBuffer,
2198 VkBuffer _buffer,
2199 VkDeviceSize offset,
2200 VkIndexType indexType)
2201 {
2202 struct anv_cmd_buffer *cmd_buffer = (struct anv_cmd_buffer *) cmdBuffer;
2203 struct anv_buffer *buffer = (struct anv_buffer *) _buffer;
2204
2205 static const uint32_t vk_to_gen_index_type[] = {
2206 [VK_INDEX_TYPE_UINT8] = INDEX_BYTE,
2207 [VK_INDEX_TYPE_UINT16] = INDEX_WORD,
2208 [VK_INDEX_TYPE_UINT32] = INDEX_DWORD,
2209 };
2210
2211 anv_batch_emit(&cmd_buffer->batch, GEN8_3DSTATE_INDEX_BUFFER,
2212 .IndexFormat = vk_to_gen_index_type[indexType],
2213 .MemoryObjectControlState = 0,
2214 .BufferStartingAddress = { buffer->bo, buffer->offset + offset },
2215 .BufferSize = buffer->size - offset);
2216 }
2217
2218 void anv_CmdBindVertexBuffers(
2219 VkCmdBuffer cmdBuffer,
2220 uint32_t startBinding,
2221 uint32_t bindingCount,
2222 const VkBuffer* pBuffers,
2223 const VkDeviceSize* pOffsets)
2224 {
2225 struct anv_cmd_buffer *cmd_buffer = (struct anv_cmd_buffer *) cmdBuffer;
2226 struct anv_bindings *bindings = cmd_buffer->bindings;
2227
2228 /* We have to defer setting up vertex buffer since we need the buffer
2229 * stride from the pipeline. */
2230
2231 for (uint32_t i = 0; i < bindingCount; i++) {
2232 bindings->vb[startBinding + i].buffer = (struct anv_buffer *) pBuffers[i];
2233 bindings->vb[startBinding + i].offset = pOffsets[i];
2234 cmd_buffer->vb_dirty |= 1 << (startBinding + i);
2235 }
2236 }
2237
2238 static void
2239 flush_descriptor_sets(struct anv_cmd_buffer *cmd_buffer)
2240 {
2241 struct anv_pipeline_layout *layout = cmd_buffer->pipeline->layout;
2242 struct anv_bindings *bindings = cmd_buffer->bindings;
2243 uint32_t layers = cmd_buffer->framebuffer->layers;
2244
2245 for (uint32_t s = 0; s < VK_NUM_SHADER_STAGE; s++) {
2246 uint32_t bias;
2247
2248 if (s == VK_SHADER_STAGE_FRAGMENT) {
2249 bias = MAX_RTS;
2250 layers = cmd_buffer->framebuffer->layers;
2251 } else {
2252 bias = 0;
2253 layers = 0;
2254 }
2255
2256 /* This is a little awkward: layout can be NULL but we still have to
2257 * allocate and set a binding table for the PS stage for render
2258 * targets. */
2259 uint32_t surface_count = layout ? layout->stage[s].surface_count : 0;
2260
2261 if (layers + surface_count > 0) {
2262 struct anv_state state;
2263 uint32_t size;
2264
2265 size = (bias + surface_count) * sizeof(uint32_t);
2266 state = anv_state_stream_alloc(&cmd_buffer->surface_state_stream, size, 32);
2267 memcpy(state.map, bindings->descriptors[s].surfaces, size);
2268
2269 for (uint32_t i = 0; i < layers; i++)
2270 anv_reloc_list_add(&cmd_buffer->batch.surf_relocs,
2271 bindings->descriptors[s].surfaces[i] + 8 * sizeof(int32_t),
2272 bindings->descriptors[s].relocs[i].bo,
2273 bindings->descriptors[s].relocs[i].offset);
2274
2275 for (uint32_t i = 0; i < surface_count; i++)
2276 anv_reloc_list_add(&cmd_buffer->batch.surf_relocs,
2277 bindings->descriptors[s].surfaces[bias + i] + 8 * sizeof(int32_t),
2278 bindings->descriptors[s].relocs[bias + i].bo,
2279 bindings->descriptors[s].relocs[bias + i].offset);
2280
2281 static const uint32_t binding_table_opcodes[] = {
2282 [VK_SHADER_STAGE_VERTEX] = 38,
2283 [VK_SHADER_STAGE_TESS_CONTROL] = 39,
2284 [VK_SHADER_STAGE_TESS_EVALUATION] = 40,
2285 [VK_SHADER_STAGE_GEOMETRY] = 41,
2286 [VK_SHADER_STAGE_FRAGMENT] = 42,
2287 [VK_SHADER_STAGE_COMPUTE] = 0,
2288 };
2289
2290 anv_batch_emit(&cmd_buffer->batch,
2291 GEN8_3DSTATE_BINDING_TABLE_POINTERS_VS,
2292 ._3DCommandSubOpcode = binding_table_opcodes[s],
2293 .PointertoVSBindingTable = state.offset);
2294 }
2295
2296 if (layout && layout->stage[s].sampler_count > 0) {
2297 struct anv_state state;
2298 size_t size;
2299
2300 size = layout->stage[s].sampler_count * 16;
2301 state = anv_state_stream_alloc(&cmd_buffer->dynamic_state_stream, size, 32);
2302 memcpy(state.map, bindings->descriptors[s].samplers, size);
2303
2304 static const uint32_t sampler_state_opcodes[] = {
2305 [VK_SHADER_STAGE_VERTEX] = 43,
2306 [VK_SHADER_STAGE_TESS_CONTROL] = 44, /* HS */
2307 [VK_SHADER_STAGE_TESS_EVALUATION] = 45, /* DS */
2308 [VK_SHADER_STAGE_GEOMETRY] = 46,
2309 [VK_SHADER_STAGE_FRAGMENT] = 47,
2310 [VK_SHADER_STAGE_COMPUTE] = 0,
2311 };
2312
2313 anv_batch_emit(&cmd_buffer->batch,
2314 GEN8_3DSTATE_SAMPLER_STATE_POINTERS_VS,
2315 ._3DCommandSubOpcode = sampler_state_opcodes[s],
2316 .PointertoVSSamplerState = state.offset);
2317 }
2318 }
2319 }
2320
2321 static void
2322 anv_cmd_buffer_flush_state(struct anv_cmd_buffer *cmd_buffer)
2323 {
2324 struct anv_pipeline *pipeline = cmd_buffer->pipeline;
2325 struct anv_bindings *bindings = cmd_buffer->bindings;
2326 const uint32_t num_buffers = __builtin_popcount(cmd_buffer->vb_dirty);
2327 const uint32_t num_dwords = 1 + num_buffers * 4;
2328 uint32_t *p;
2329
2330 if (cmd_buffer->vb_dirty) {
2331 p = anv_batch_emitn(&cmd_buffer->batch, num_dwords,
2332 GEN8_3DSTATE_VERTEX_BUFFERS);
2333 uint32_t vb, i = 0;
2334 for_each_bit(vb, cmd_buffer->vb_dirty) {
2335 struct anv_buffer *buffer = bindings->vb[vb].buffer;
2336 uint32_t offset = bindings->vb[vb].offset;
2337
2338 struct GEN8_VERTEX_BUFFER_STATE state = {
2339 .VertexBufferIndex = vb,
2340 .MemoryObjectControlState = 0,
2341 .AddressModifyEnable = true,
2342 .BufferPitch = pipeline->binding_stride[vb],
2343 .BufferStartingAddress = { buffer->bo, buffer->offset + offset },
2344 .BufferSize = buffer->size - offset
2345 };
2346
2347 GEN8_VERTEX_BUFFER_STATE_pack(&cmd_buffer->batch, &p[1 + i * 4], &state);
2348 i++;
2349 }
2350 }
2351
2352 if (cmd_buffer->dirty & ANV_CMD_BUFFER_PIPELINE_DIRTY)
2353 anv_batch_emit_batch(&cmd_buffer->batch, &pipeline->batch);
2354
2355 if (cmd_buffer->dirty & ANV_CMD_BUFFER_DESCRIPTOR_SET_DIRTY)
2356 flush_descriptor_sets(cmd_buffer);
2357
2358 if (cmd_buffer->dirty & (ANV_CMD_BUFFER_PIPELINE_DIRTY | ANV_CMD_BUFFER_RS_DIRTY))
2359 anv_batch_emit_merge(&cmd_buffer->batch,
2360 cmd_buffer->rs_state->state_sf, pipeline->state_sf);
2361
2362 cmd_buffer->vb_dirty = 0;
2363 cmd_buffer->dirty = 0;
2364 }
2365
2366 void anv_CmdDraw(
2367 VkCmdBuffer cmdBuffer,
2368 uint32_t firstVertex,
2369 uint32_t vertexCount,
2370 uint32_t firstInstance,
2371 uint32_t instanceCount)
2372 {
2373 struct anv_cmd_buffer *cmd_buffer = (struct anv_cmd_buffer *) cmdBuffer;
2374
2375 anv_cmd_buffer_flush_state(cmd_buffer);
2376
2377 anv_batch_emit(&cmd_buffer->batch, GEN8_3DPRIMITIVE,
2378 .VertexAccessType = SEQUENTIAL,
2379 .VertexCountPerInstance = vertexCount,
2380 .StartVertexLocation = firstVertex,
2381 .InstanceCount = instanceCount,
2382 .StartInstanceLocation = firstInstance,
2383 .BaseVertexLocation = 0);
2384 }
2385
2386 void anv_CmdDrawIndexed(
2387 VkCmdBuffer cmdBuffer,
2388 uint32_t firstIndex,
2389 uint32_t indexCount,
2390 int32_t vertexOffset,
2391 uint32_t firstInstance,
2392 uint32_t instanceCount)
2393 {
2394 struct anv_cmd_buffer *cmd_buffer = (struct anv_cmd_buffer *) cmdBuffer;
2395
2396 anv_cmd_buffer_flush_state(cmd_buffer);
2397
2398 anv_batch_emit(&cmd_buffer->batch, GEN8_3DPRIMITIVE,
2399 .VertexAccessType = RANDOM,
2400 .VertexCountPerInstance = indexCount,
2401 .StartVertexLocation = firstIndex,
2402 .InstanceCount = instanceCount,
2403 .StartInstanceLocation = firstInstance,
2404 .BaseVertexLocation = 0);
2405 }
2406
2407 static void
2408 anv_batch_lrm(struct anv_batch *batch,
2409 uint32_t reg, struct anv_bo *bo, uint32_t offset)
2410 {
2411 anv_batch_emit(batch, GEN8_MI_LOAD_REGISTER_MEM,
2412 .RegisterAddress = reg,
2413 .MemoryAddress = { bo, offset });
2414 }
2415
2416 static void
2417 anv_batch_lri(struct anv_batch *batch, uint32_t reg, uint32_t imm)
2418 {
2419 anv_batch_emit(batch, GEN8_MI_LOAD_REGISTER_IMM,
2420 .RegisterOffset = reg,
2421 .DataDWord = imm);
2422 }
2423
2424 /* Auto-Draw / Indirect Registers */
2425 #define GEN7_3DPRIM_END_OFFSET 0x2420
2426 #define GEN7_3DPRIM_START_VERTEX 0x2430
2427 #define GEN7_3DPRIM_VERTEX_COUNT 0x2434
2428 #define GEN7_3DPRIM_INSTANCE_COUNT 0x2438
2429 #define GEN7_3DPRIM_START_INSTANCE 0x243C
2430 #define GEN7_3DPRIM_BASE_VERTEX 0x2440
2431
2432 void anv_CmdDrawIndirect(
2433 VkCmdBuffer cmdBuffer,
2434 VkBuffer _buffer,
2435 VkDeviceSize offset,
2436 uint32_t count,
2437 uint32_t stride)
2438 {
2439 struct anv_cmd_buffer *cmd_buffer = (struct anv_cmd_buffer *) cmdBuffer;
2440 struct anv_buffer *buffer = (struct anv_buffer *) _buffer;
2441 struct anv_bo *bo = buffer->bo;
2442 uint32_t bo_offset = buffer->offset + offset;
2443
2444 anv_cmd_buffer_flush_state(cmd_buffer);
2445
2446 anv_batch_lrm(&cmd_buffer->batch, GEN7_3DPRIM_VERTEX_COUNT, bo, bo_offset);
2447 anv_batch_lrm(&cmd_buffer->batch, GEN7_3DPRIM_INSTANCE_COUNT, bo, bo_offset + 4);
2448 anv_batch_lrm(&cmd_buffer->batch, GEN7_3DPRIM_START_VERTEX, bo, bo_offset + 8);
2449 anv_batch_lrm(&cmd_buffer->batch, GEN7_3DPRIM_START_INSTANCE, bo, bo_offset + 12);
2450 anv_batch_lri(&cmd_buffer->batch, GEN7_3DPRIM_BASE_VERTEX, 0);
2451
2452 anv_batch_emit(&cmd_buffer->batch, GEN8_3DPRIMITIVE,
2453 .IndirectParameterEnable = true,
2454 .VertexAccessType = SEQUENTIAL);
2455 }
2456
2457 void anv_CmdDrawIndexedIndirect(
2458 VkCmdBuffer cmdBuffer,
2459 VkBuffer _buffer,
2460 VkDeviceSize offset,
2461 uint32_t count,
2462 uint32_t stride)
2463 {
2464 struct anv_cmd_buffer *cmd_buffer = (struct anv_cmd_buffer *) cmdBuffer;
2465 struct anv_buffer *buffer = (struct anv_buffer *) _buffer;
2466 struct anv_bo *bo = buffer->bo;
2467 uint32_t bo_offset = buffer->offset + offset;
2468
2469 anv_cmd_buffer_flush_state(cmd_buffer);
2470
2471 anv_batch_lrm(&cmd_buffer->batch, GEN7_3DPRIM_VERTEX_COUNT, bo, bo_offset);
2472 anv_batch_lrm(&cmd_buffer->batch, GEN7_3DPRIM_INSTANCE_COUNT, bo, bo_offset + 4);
2473 anv_batch_lrm(&cmd_buffer->batch, GEN7_3DPRIM_START_VERTEX, bo, bo_offset + 8);
2474 anv_batch_lrm(&cmd_buffer->batch, GEN7_3DPRIM_BASE_VERTEX, bo, bo_offset + 12);
2475 anv_batch_lrm(&cmd_buffer->batch, GEN7_3DPRIM_START_INSTANCE, bo, bo_offset + 16);
2476
2477 anv_batch_emit(&cmd_buffer->batch, GEN8_3DPRIMITIVE,
2478 .IndirectParameterEnable = true,
2479 .VertexAccessType = RANDOM);
2480 }
2481
2482 void anv_CmdDispatch(
2483 VkCmdBuffer cmdBuffer,
2484 uint32_t x,
2485 uint32_t y,
2486 uint32_t z)
2487 {
2488 stub();
2489 }
2490
2491 void anv_CmdDispatchIndirect(
2492 VkCmdBuffer cmdBuffer,
2493 VkBuffer buffer,
2494 VkDeviceSize offset)
2495 {
2496 stub();
2497 }
2498
2499 void anv_CmdSetEvent(
2500 VkCmdBuffer cmdBuffer,
2501 VkEvent event,
2502 VkPipeEvent pipeEvent)
2503 {
2504 stub();
2505 }
2506
2507 void anv_CmdResetEvent(
2508 VkCmdBuffer cmdBuffer,
2509 VkEvent event,
2510 VkPipeEvent pipeEvent)
2511 {
2512 stub();
2513 }
2514
2515 void anv_CmdWaitEvents(
2516 VkCmdBuffer cmdBuffer,
2517 VkWaitEvent waitEvent,
2518 uint32_t eventCount,
2519 const VkEvent* pEvents,
2520 uint32_t memBarrierCount,
2521 const void** ppMemBarriers)
2522 {
2523 stub();
2524 }
2525
2526 void anv_CmdPipelineBarrier(
2527 VkCmdBuffer cmdBuffer,
2528 VkWaitEvent waitEvent,
2529 uint32_t pipeEventCount,
2530 const VkPipeEvent* pPipeEvents,
2531 uint32_t memBarrierCount,
2532 const void** ppMemBarriers)
2533 {
2534 stub();
2535 }
2536
2537 static void
2538 anv_batch_emit_ps_depth_count(struct anv_batch *batch,
2539 struct anv_bo *bo, uint32_t offset)
2540 {
2541 anv_batch_emit(batch, GEN8_PIPE_CONTROL,
2542 .DestinationAddressType = DAT_PPGTT,
2543 .PostSyncOperation = WritePSDepthCount,
2544 .Address = { bo, offset }); /* FIXME: This is only lower 32 bits */
2545 }
2546
2547 void anv_CmdBeginQuery(
2548 VkCmdBuffer cmdBuffer,
2549 VkQueryPool queryPool,
2550 uint32_t slot,
2551 VkQueryControlFlags flags)
2552 {
2553 struct anv_cmd_buffer *cmd_buffer = (struct anv_cmd_buffer *) cmdBuffer;
2554 struct anv_query_pool *pool = (struct anv_query_pool *) queryPool;
2555
2556 switch (pool->type) {
2557 case VK_QUERY_TYPE_OCCLUSION:
2558 anv_batch_emit_ps_depth_count(&cmd_buffer->batch, &pool->bo, slot * 16);
2559 break;
2560
2561 case VK_QUERY_TYPE_PIPELINE_STATISTICS:
2562 break;
2563
2564 default:
2565 break;
2566 }
2567 }
2568
2569 void anv_CmdEndQuery(
2570 VkCmdBuffer cmdBuffer,
2571 VkQueryPool queryPool,
2572 uint32_t slot)
2573 {
2574 struct anv_cmd_buffer *cmd_buffer = (struct anv_cmd_buffer *) cmdBuffer;
2575 struct anv_query_pool *pool = (struct anv_query_pool *) queryPool;
2576
2577 switch (pool->type) {
2578 case VK_QUERY_TYPE_OCCLUSION:
2579 anv_batch_emit_ps_depth_count(&cmd_buffer->batch, &pool->bo, slot * 16 + 8);
2580 break;
2581
2582 case VK_QUERY_TYPE_PIPELINE_STATISTICS:
2583 break;
2584
2585 default:
2586 break;
2587 }
2588 }
2589
2590 void anv_CmdResetQueryPool(
2591 VkCmdBuffer cmdBuffer,
2592 VkQueryPool queryPool,
2593 uint32_t startQuery,
2594 uint32_t queryCount)
2595 {
2596 stub();
2597 }
2598
2599 #define TIMESTAMP 0x44070
2600
2601 void anv_CmdWriteTimestamp(
2602 VkCmdBuffer cmdBuffer,
2603 VkTimestampType timestampType,
2604 VkBuffer destBuffer,
2605 VkDeviceSize destOffset)
2606 {
2607 struct anv_cmd_buffer *cmd_buffer = (struct anv_cmd_buffer *) cmdBuffer;
2608 struct anv_buffer *buffer = (struct anv_buffer *) destBuffer;
2609 struct anv_bo *bo = buffer->bo;
2610
2611 switch (timestampType) {
2612 case VK_TIMESTAMP_TYPE_TOP:
2613 anv_batch_emit(&cmd_buffer->batch, GEN8_MI_STORE_REGISTER_MEM,
2614 .RegisterAddress = TIMESTAMP,
2615 .MemoryAddress = { bo, buffer->offset + destOffset });
2616 break;
2617
2618 case VK_TIMESTAMP_TYPE_BOTTOM:
2619 anv_batch_emit(&cmd_buffer->batch, GEN8_PIPE_CONTROL,
2620 .DestinationAddressType = DAT_PPGTT,
2621 .PostSyncOperation = WriteTimestamp,
2622 .Address = /* FIXME: This is only lower 32 bits */
2623 { bo, buffer->offset + destOffset });
2624 break;
2625
2626 default:
2627 break;
2628 }
2629 }
2630
2631 void anv_CmdCopyQueryPoolResults(
2632 VkCmdBuffer cmdBuffer,
2633 VkQueryPool queryPool,
2634 uint32_t startQuery,
2635 uint32_t queryCount,
2636 VkBuffer destBuffer,
2637 VkDeviceSize destOffset,
2638 VkDeviceSize destStride,
2639 VkQueryResultFlags flags)
2640 {
2641 stub();
2642 }
2643
2644 void anv_CmdInitAtomicCounters(
2645 VkCmdBuffer cmdBuffer,
2646 VkPipelineBindPoint pipelineBindPoint,
2647 uint32_t startCounter,
2648 uint32_t counterCount,
2649 const uint32_t* pData)
2650 {
2651 stub();
2652 }
2653
2654 void anv_CmdLoadAtomicCounters(
2655 VkCmdBuffer cmdBuffer,
2656 VkPipelineBindPoint pipelineBindPoint,
2657 uint32_t startCounter,
2658 uint32_t counterCount,
2659 VkBuffer srcBuffer,
2660 VkDeviceSize srcOffset)
2661 {
2662 stub();
2663 }
2664
2665 void anv_CmdSaveAtomicCounters(
2666 VkCmdBuffer cmdBuffer,
2667 VkPipelineBindPoint pipelineBindPoint,
2668 uint32_t startCounter,
2669 uint32_t counterCount,
2670 VkBuffer destBuffer,
2671 VkDeviceSize destOffset)
2672 {
2673 stub();
2674 }
2675
2676 VkResult anv_CreateFramebuffer(
2677 VkDevice _device,
2678 const VkFramebufferCreateInfo* pCreateInfo,
2679 VkFramebuffer* pFramebuffer)
2680 {
2681 struct anv_device *device = (struct anv_device *) _device;
2682 struct anv_framebuffer *framebuffer;
2683
2684 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO);
2685
2686 framebuffer = anv_device_alloc(device, sizeof(*framebuffer), 8,
2687 VK_SYSTEM_ALLOC_TYPE_API_OBJECT);
2688 if (framebuffer == NULL)
2689 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
2690
2691 framebuffer->color_attachment_count = pCreateInfo->colorAttachmentCount;
2692 for (uint32_t i = 0; i < pCreateInfo->colorAttachmentCount; i++) {
2693 framebuffer->color_attachments[i] =
2694 (struct anv_surface_view *) pCreateInfo->pColorAttachments[i].view;
2695 }
2696
2697 if (pCreateInfo->pDepthStencilAttachment) {
2698 framebuffer->depth_stencil =
2699 (struct anv_depth_stencil_view *) pCreateInfo->pDepthStencilAttachment->view;
2700 }
2701
2702 framebuffer->sample_count = pCreateInfo->sampleCount;
2703 framebuffer->width = pCreateInfo->width;
2704 framebuffer->height = pCreateInfo->height;
2705 framebuffer->layers = pCreateInfo->layers;
2706
2707 vkCreateDynamicViewportState((VkDevice) device,
2708 &(VkDynamicVpStateCreateInfo) {
2709 .sType = VK_STRUCTURE_TYPE_DYNAMIC_VP_STATE_CREATE_INFO,
2710 .viewportAndScissorCount = 2,
2711 .pViewports = (VkViewport[]) {
2712 {
2713 .originX = 0,
2714 .originY = 0,
2715 .width = pCreateInfo->width,
2716 .height = pCreateInfo->height,
2717 .minDepth = 0,
2718 .maxDepth = 1
2719 },
2720 },
2721 .pScissors = (VkRect[]) {
2722 { { 0, 0 },
2723 { pCreateInfo->width, pCreateInfo->height } },
2724 }
2725 },
2726 &framebuffer->vp_state);
2727
2728 *pFramebuffer = (VkFramebuffer) framebuffer;
2729
2730 return VK_SUCCESS;
2731 }
2732
2733 VkResult anv_CreateRenderPass(
2734 VkDevice _device,
2735 const VkRenderPassCreateInfo* pCreateInfo,
2736 VkRenderPass* pRenderPass)
2737 {
2738 struct anv_device *device = (struct anv_device *) _device;
2739 struct anv_render_pass *pass;
2740 size_t size;
2741
2742 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO);
2743
2744 size = sizeof(*pass) +
2745 pCreateInfo->layers * sizeof(struct anv_render_pass_layer);
2746 pass = anv_device_alloc(device, size, 8,
2747 VK_SYSTEM_ALLOC_TYPE_API_OBJECT);
2748 if (pass == NULL)
2749 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
2750
2751 pass->render_area = pCreateInfo->renderArea;
2752
2753 pass->num_layers = pCreateInfo->layers;
2754
2755 pass->num_clear_layers = 0;
2756 for (uint32_t i = 0; i < pCreateInfo->layers; i++) {
2757 pass->layers[i].color_load_op = pCreateInfo->pColorLoadOps[i];
2758 pass->layers[i].clear_color = pCreateInfo->pColorLoadClearValues[i];
2759 if (pass->layers[i].color_load_op == VK_ATTACHMENT_LOAD_OP_CLEAR)
2760 pass->num_clear_layers++;
2761 }
2762
2763 *pRenderPass = (VkRenderPass) pass;
2764
2765 return VK_SUCCESS;
2766 }
2767
2768 void
2769 anv_cmd_buffer_fill_render_targets(struct anv_cmd_buffer *cmd_buffer)
2770 {
2771 struct anv_framebuffer *framebuffer = cmd_buffer->framebuffer;
2772 struct anv_bindings *bindings = cmd_buffer->bindings;
2773
2774 for (uint32_t i = 0; i < framebuffer->color_attachment_count; i++) {
2775 struct anv_surface_view *view = framebuffer->color_attachments[i];
2776
2777 bindings->descriptors[VK_SHADER_STAGE_FRAGMENT].surfaces[i] = view->surface_state.offset;
2778 bindings->descriptors[VK_SHADER_STAGE_FRAGMENT].relocs[i].bo = view->bo;
2779 bindings->descriptors[VK_SHADER_STAGE_FRAGMENT].relocs[i].offset = view->offset;
2780 }
2781 cmd_buffer->dirty |= ANV_CMD_BUFFER_DESCRIPTOR_SET_DIRTY;
2782 }
2783
2784 void anv_CmdBeginRenderPass(
2785 VkCmdBuffer cmdBuffer,
2786 const VkRenderPassBegin* pRenderPassBegin)
2787 {
2788 struct anv_cmd_buffer *cmd_buffer = (struct anv_cmd_buffer *) cmdBuffer;
2789 struct anv_render_pass *pass = (struct anv_render_pass *) pRenderPassBegin->renderPass;
2790 struct anv_framebuffer *framebuffer =
2791 (struct anv_framebuffer *) pRenderPassBegin->framebuffer;
2792
2793 cmd_buffer->framebuffer = framebuffer;
2794
2795 anv_batch_emit(&cmd_buffer->batch, GEN8_3DSTATE_DRAWING_RECTANGLE,
2796 .ClippedDrawingRectangleYMin = pass->render_area.offset.y,
2797 .ClippedDrawingRectangleXMin = pass->render_area.offset.x,
2798 .ClippedDrawingRectangleYMax =
2799 pass->render_area.offset.y + pass->render_area.extent.height - 1,
2800 .ClippedDrawingRectangleXMax =
2801 pass->render_area.offset.x + pass->render_area.extent.width - 1,
2802 .DrawingRectangleOriginY = 0,
2803 .DrawingRectangleOriginX = 0);
2804
2805 anv_cmd_buffer_fill_render_targets(cmd_buffer);
2806
2807 anv_cmd_buffer_clear(cmd_buffer, pass);
2808 }
2809
2810 void anv_CmdEndRenderPass(
2811 VkCmdBuffer cmdBuffer,
2812 VkRenderPass renderPass)
2813 {
2814 /* Emit a flushing pipe control at the end of a pass. This is kind of a
2815 * hack but it ensures that render targets always actually get written.
2816 * Eventually, we should do flushing based on image format transitions
2817 * or something of that nature.
2818 */
2819 struct anv_cmd_buffer *cmd_buffer = (struct anv_cmd_buffer *)cmdBuffer;
2820 anv_batch_emit(&cmd_buffer->batch, GEN8_PIPE_CONTROL,
2821 .PostSyncOperation = NoWrite,
2822 .RenderTargetCacheFlushEnable = true,
2823 .InstructionCacheInvalidateEnable = true,
2824 .DepthCacheFlushEnable = true,
2825 .VFCacheInvalidationEnable = true,
2826 .TextureCacheInvalidationEnable = true,
2827 .CommandStreamerStallEnable = true);
2828
2829 stub();
2830 }