2 * Copyright © 2015 Intel Corporation
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:
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
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
33 anv_env_get_int(const char *name
)
35 const char *val
= getenv(name
);
40 return strtol(val
, NULL
, 0);
44 fill_physical_device(struct anv_physical_device
*device
,
45 struct anv_instance
*instance
,
50 fd
= open("/dev/dri/renderD128", O_RDWR
| O_CLOEXEC
);
52 return vk_error(VK_ERROR_UNAVAILABLE
);
54 device
->instance
= instance
;
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. */
63 device
->chipset_id
= anv_gem_get_param(fd
, I915_PARAM_CHIPSET_ID
);
65 if (!device
->chipset_id
)
68 device
->name
= brw_get_device_name(device
->chipset_id
);
69 device
->info
= brw_get_device_info(device
->chipset_id
, -1);
73 if (!anv_gem_get_param(fd
, I915_PARAM_HAS_WAIT_TIMEOUT
))
76 if (!anv_gem_get_param(fd
, I915_PARAM_HAS_EXECBUF2
))
79 if (!anv_gem_get_param(fd
, I915_PARAM_HAS_LLC
))
82 if (!anv_gem_get_param(fd
, I915_PARAM_HAS_EXEC_CONSTANTS
))
92 return vk_error(VK_ERROR_UNAVAILABLE
);
95 static void *default_alloc(
99 VkSystemAllocType allocType
)
104 static void default_free(
111 static const VkAllocCallbacks default_alloc_callbacks
= {
113 .pfnAlloc
= default_alloc
,
114 .pfnFree
= default_free
117 VkResult
anv_CreateInstance(
118 const VkInstanceCreateInfo
* pCreateInfo
,
119 VkInstance
* pInstance
)
121 struct anv_instance
*instance
;
122 const VkAllocCallbacks
*alloc_callbacks
= &default_alloc_callbacks
;
123 void *user_data
= NULL
;
126 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO
);
128 if (pCreateInfo
->pAllocCb
) {
129 alloc_callbacks
= pCreateInfo
->pAllocCb
;
130 user_data
= pCreateInfo
->pAllocCb
->pUserData
;
132 instance
= alloc_callbacks
->pfnAlloc(user_data
, sizeof(*instance
), 8,
133 VK_SYSTEM_ALLOC_TYPE_API_OBJECT
);
135 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
137 instance
->pAllocUserData
= alloc_callbacks
->pUserData
;
138 instance
->pfnAlloc
= alloc_callbacks
->pfnAlloc
;
139 instance
->pfnFree
= alloc_callbacks
->pfnFree
;
140 instance
->apiVersion
= pCreateInfo
->pAppInfo
->apiVersion
;
142 instance
->physicalDeviceCount
= 0;
143 result
= fill_physical_device(&instance
->physicalDevice
,
144 instance
, "/dev/dri/renderD128");
146 if (result
!= VK_SUCCESS
)
149 instance
->physicalDeviceCount
++;
150 *pInstance
= (VkInstance
) instance
;
155 VkResult
anv_DestroyInstance(
156 VkInstance _instance
)
158 struct anv_instance
*instance
= (struct anv_instance
*) _instance
;
160 instance
->pfnFree(instance
->pAllocUserData
, instance
);
165 VkResult
anv_EnumeratePhysicalDevices(
166 VkInstance _instance
,
167 uint32_t* pPhysicalDeviceCount
,
168 VkPhysicalDevice
* pPhysicalDevices
)
170 struct anv_instance
*instance
= (struct anv_instance
*) _instance
;
172 if (*pPhysicalDeviceCount
>= 1)
173 pPhysicalDevices
[0] = (VkPhysicalDevice
) &instance
->physicalDevice
;
174 *pPhysicalDeviceCount
= instance
->physicalDeviceCount
;
179 VkResult
anv_GetPhysicalDeviceInfo(
180 VkPhysicalDevice physicalDevice
,
181 VkPhysicalDeviceInfoType infoType
,
185 struct anv_physical_device
*device
= (struct anv_physical_device
*) physicalDevice
;
186 VkPhysicalDeviceProperties
*properties
;
187 VkPhysicalDevicePerformance
*performance
;
188 VkPhysicalDeviceQueueProperties
*queue_properties
;
189 VkPhysicalDeviceMemoryProperties
*memory_properties
;
190 VkDisplayPropertiesWSI
*display_properties
;
191 uint64_t ns_per_tick
= 80;
193 switch ((uint32_t) infoType
) {
194 case VK_PHYSICAL_DEVICE_INFO_TYPE_PROPERTIES
:
197 *pDataSize
= sizeof(*properties
);
201 properties
->apiVersion
= 1;
202 properties
->driverVersion
= 1;
203 properties
->vendorId
= 0x8086;
204 properties
->deviceId
= device
->chipset_id
;
205 properties
->deviceType
= VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU
;
206 strcpy(properties
->deviceName
, device
->name
);
207 properties
->maxInlineMemoryUpdateSize
= 0;
208 properties
->maxBoundDescriptorSets
= MAX_SETS
;
209 properties
->maxThreadGroupSize
= 512;
210 properties
->timestampFrequency
= 1000 * 1000 * 1000 / ns_per_tick
;
211 properties
->multiColorAttachmentClears
= true;
212 properties
->maxDescriptorSets
= 8;
213 properties
->maxViewports
= 16;
214 properties
->maxColorAttachments
= 8;
217 case VK_PHYSICAL_DEVICE_INFO_TYPE_PERFORMANCE
:
220 *pDataSize
= sizeof(*performance
);
224 performance
->maxDeviceClock
= 1.0;
225 performance
->aluPerClock
= 1.0;
226 performance
->texPerClock
= 1.0;
227 performance
->primsPerClock
= 1.0;
228 performance
->pixelsPerClock
= 1.0;
231 case VK_PHYSICAL_DEVICE_INFO_TYPE_QUEUE_PROPERTIES
:
232 queue_properties
= pData
;
234 *pDataSize
= sizeof(*queue_properties
);
238 queue_properties
->queueFlags
= 0;
239 queue_properties
->queueCount
= 1;
240 queue_properties
->maxAtomicCounters
= 0;
241 queue_properties
->supportsTimestamps
= true;
242 queue_properties
->maxMemReferences
= 256;
245 case VK_PHYSICAL_DEVICE_INFO_TYPE_MEMORY_PROPERTIES
:
246 memory_properties
= pData
;
248 *pDataSize
= sizeof(*memory_properties
);
252 memory_properties
->supportsMigration
= false;
253 memory_properties
->supportsPinning
= false;
256 case VK_PHYSICAL_DEVICE_INFO_TYPE_DISPLAY_PROPERTIES_WSI
:
257 anv_finishme("VK_PHYSICAL_DEVICE_INFO_TYPE_DISPLAY_PROPERTIES_WSI");
259 *pDataSize
= sizeof(*display_properties
);
263 display_properties
= pData
;
264 display_properties
->display
= 0;
265 display_properties
->physicalResolution
= (VkExtent2D
) { 0, 0 };
268 case VK_PHYSICAL_DEVICE_INFO_TYPE_QUEUE_PRESENT_PROPERTIES_WSI
:
269 anv_finishme("VK_PHYSICAL_DEVICE_INFO_TYPE_QUEUE_PRESENT_PROPERTIES_WSI");
274 return VK_UNSUPPORTED
;
279 void * vkGetProcAddr(
280 VkPhysicalDevice physicalDevice
,
283 return anv_lookup_entrypoint(pName
);
287 parse_debug_flags(struct anv_device
*device
)
289 const char *debug
, *p
, *end
;
291 debug
= getenv("INTEL_DEBUG");
292 device
->dump_aub
= false;
294 for (p
= debug
; *p
; p
= end
+ 1) {
295 end
= strchrnul(p
, ',');
296 if (end
- p
== 3 && memcmp(p
, "aub", 3) == 0)
297 device
->dump_aub
= true;
298 if (end
- p
== 5 && memcmp(p
, "no_hw", 5) == 0)
299 device
->no_hw
= true;
306 VkResult
anv_CreateDevice(
307 VkPhysicalDevice _physicalDevice
,
308 const VkDeviceCreateInfo
* pCreateInfo
,
311 struct anv_physical_device
*physicalDevice
=
312 (struct anv_physical_device
*) _physicalDevice
;
313 struct anv_instance
*instance
= physicalDevice
->instance
;
314 struct anv_device
*device
;
316 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO
);
318 device
= instance
->pfnAlloc(instance
->pAllocUserData
,
320 VK_SYSTEM_ALLOC_TYPE_API_OBJECT
);
322 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
324 device
->no_hw
= physicalDevice
->no_hw
;
325 parse_debug_flags(device
);
327 device
->instance
= physicalDevice
->instance
;
328 device
->fd
= open("/dev/dri/renderD128", O_RDWR
| O_CLOEXEC
);
329 if (device
->fd
== -1)
332 device
->context_id
= anv_gem_create_context(device
);
333 if (device
->context_id
== -1)
336 anv_block_pool_init(&device
->dynamic_state_block_pool
, device
, 2048);
338 anv_state_pool_init(&device
->dynamic_state_pool
,
339 &device
->dynamic_state_block_pool
);
341 anv_block_pool_init(&device
->instruction_block_pool
, device
, 2048);
342 anv_block_pool_init(&device
->surface_state_block_pool
, device
, 2048);
345 /* Binding table pointers are only 16 bits so we have to make sure that
346 * they get allocated at the beginning of the surface state BO. To
347 * handle this, we create a separate block pool that works out of the
348 * first 64 KB of the surface state BO.
350 anv_block_pool_init_slave(&device
->binding_table_block_pool
,
351 &device
->surface_state_block_pool
, 32);
353 anv_state_pool_init(&device
->surface_state_pool
,
354 &device
->surface_state_block_pool
);
356 device
->compiler
= anv_compiler_create(device
->fd
);
357 device
->aub_writer
= NULL
;
359 device
->info
= *physicalDevice
->info
;
361 pthread_mutex_init(&device
->mutex
, NULL
);
363 anv_device_init_meta(device
);
365 *pDevice
= (VkDevice
) device
;
372 anv_device_free(device
, device
);
374 return vk_error(VK_ERROR_UNAVAILABLE
);
377 VkResult
anv_DestroyDevice(
380 struct anv_device
*device
= (struct anv_device
*) _device
;
382 anv_compiler_destroy(device
->compiler
);
384 anv_block_pool_finish(&device
->dynamic_state_block_pool
);
385 anv_block_pool_finish(&device
->instruction_block_pool
);
386 anv_block_pool_finish(&device
->surface_state_block_pool
);
390 if (device
->aub_writer
)
391 anv_aub_writer_destroy(device
->aub_writer
);
393 anv_device_free(device
, device
);
398 VkResult
anv_GetGlobalExtensionInfo(
399 VkExtensionInfoType infoType
,
400 uint32_t extensionIndex
,
404 static const VkExtensionProperties extensions
[] = {
406 .extName
= "VK_WSI_LunarG",
410 uint32_t count
= ARRAY_SIZE(extensions
);
413 case VK_EXTENSION_INFO_TYPE_COUNT
:
414 memcpy(pData
, &count
, sizeof(count
));
415 *pDataSize
= sizeof(count
);
418 case VK_EXTENSION_INFO_TYPE_PROPERTIES
:
419 if (extensionIndex
>= count
)
420 return vk_error(VK_ERROR_INVALID_EXTENSION
);
422 memcpy(pData
, &extensions
[extensionIndex
], sizeof(extensions
[0]));
423 *pDataSize
= sizeof(extensions
[0]);
427 return VK_UNSUPPORTED
;
431 VkResult
anv_GetPhysicalDeviceExtensionInfo(
432 VkPhysicalDevice physicalDevice
,
433 VkExtensionInfoType infoType
,
434 uint32_t extensionIndex
,
441 case VK_EXTENSION_INFO_TYPE_COUNT
:
450 case VK_EXTENSION_INFO_TYPE_PROPERTIES
:
451 return vk_error(VK_ERROR_INVALID_EXTENSION
);
454 return VK_UNSUPPORTED
;
458 VkResult
anv_EnumerateLayers(
459 VkPhysicalDevice physicalDevice
,
460 size_t maxStringSize
,
462 char* const* pOutLayers
,
470 VkResult
anv_GetDeviceQueue(
472 uint32_t queueNodeIndex
,
476 struct anv_device
*device
= (struct anv_device
*) _device
;
477 struct anv_queue
*queue
;
479 /* FIXME: Should allocate these at device create time. */
481 queue
= anv_device_alloc(device
, sizeof(*queue
), 8,
482 VK_SYSTEM_ALLOC_TYPE_API_OBJECT
);
484 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
486 queue
->device
= device
;
487 queue
->pool
= &device
->surface_state_pool
;
489 queue
->completed_serial
= anv_state_pool_alloc(queue
->pool
, 4, 4);
490 *(uint32_t *)queue
->completed_serial
.map
= 0;
491 queue
->next_serial
= 1;
493 *pQueue
= (VkQueue
) queue
;
498 static const uint32_t BATCH_SIZE
= 8192;
501 anv_batch_init(struct anv_batch
*batch
, struct anv_device
*device
)
505 result
= anv_bo_init_new(&batch
->bo
, device
, BATCH_SIZE
);
506 if (result
!= VK_SUCCESS
)
510 anv_gem_mmap(device
, batch
->bo
.gem_handle
, 0, BATCH_SIZE
);
511 if (batch
->bo
.map
== NULL
) {
512 result
= vk_error(VK_ERROR_MEMORY_MAP_FAILED
);
516 batch
->cmd_relocs
.num_relocs
= 0;
517 batch
->next
= batch
->bo
.map
;
522 anv_gem_close(device
, batch
->bo
.gem_handle
);
529 anv_batch_finish(struct anv_batch
*batch
, struct anv_device
*device
)
531 anv_gem_munmap(batch
->bo
.map
, BATCH_SIZE
);
532 anv_gem_close(device
, batch
->bo
.gem_handle
);
536 anv_batch_reset(struct anv_batch
*batch
)
538 batch
->next
= batch
->bo
.map
;
539 batch
->cmd_relocs
.num_relocs
= 0;
543 anv_batch_emit_dwords(struct anv_batch
*batch
, int num_dwords
)
545 void *p
= batch
->next
;
547 batch
->next
+= num_dwords
* 4;
553 anv_reloc_list_append(struct anv_reloc_list
*list
,
554 struct anv_reloc_list
*other
, uint32_t offset
)
558 count
= list
->num_relocs
;
559 memcpy(&list
->relocs
[count
], &other
->relocs
[0],
560 other
->num_relocs
* sizeof(other
->relocs
[0]));
561 memcpy(&list
->reloc_bos
[count
], &other
->reloc_bos
[0],
562 other
->num_relocs
* sizeof(other
->reloc_bos
[0]));
563 for (i
= 0; i
< other
->num_relocs
; i
++)
564 list
->relocs
[i
+ count
].offset
+= offset
;
566 count
+= other
->num_relocs
;
570 anv_reloc_list_add(struct anv_reloc_list
*list
,
572 struct anv_bo
*target_bo
, uint32_t delta
)
574 struct drm_i915_gem_relocation_entry
*entry
;
577 assert(list
->num_relocs
< ANV_BATCH_MAX_RELOCS
);
579 /* XXX: Can we use I915_EXEC_HANDLE_LUT? */
580 index
= list
->num_relocs
++;
581 list
->reloc_bos
[index
] = target_bo
;
582 entry
= &list
->relocs
[index
];
583 entry
->target_handle
= target_bo
->gem_handle
;
584 entry
->delta
= delta
;
585 entry
->offset
= offset
;
586 entry
->presumed_offset
= target_bo
->offset
;
587 entry
->read_domains
= 0;
588 entry
->write_domain
= 0;
590 return target_bo
->offset
+ delta
;
594 anv_batch_emit_batch(struct anv_batch
*batch
, struct anv_batch
*other
)
596 uint32_t size
, offset
;
598 size
= other
->next
- other
->bo
.map
;
599 memcpy(batch
->next
, other
->bo
.map
, size
);
601 offset
= batch
->next
- batch
->bo
.map
;
602 anv_reloc_list_append(&batch
->cmd_relocs
, &other
->cmd_relocs
, offset
);
608 anv_batch_emit_reloc(struct anv_batch
*batch
,
609 void *location
, struct anv_bo
*bo
, uint32_t delta
)
611 return anv_reloc_list_add(&batch
->cmd_relocs
,
612 location
- batch
->bo
.map
, bo
, delta
);
615 VkResult
anv_QueueSubmit(
617 uint32_t cmdBufferCount
,
618 const VkCmdBuffer
* pCmdBuffers
,
621 struct anv_queue
*queue
= (struct anv_queue
*) _queue
;
622 struct anv_device
*device
= queue
->device
;
623 struct anv_fence
*fence
= (struct anv_fence
*) _fence
;
626 for (uint32_t i
= 0; i
< cmdBufferCount
; i
++) {
627 struct anv_cmd_buffer
*cmd_buffer
=
628 (struct anv_cmd_buffer
*) pCmdBuffers
[i
];
630 if (device
->dump_aub
)
631 anv_cmd_buffer_dump(cmd_buffer
);
633 if (!device
->no_hw
) {
634 ret
= anv_gem_execbuffer(device
, &cmd_buffer
->execbuf
);
636 return vk_error(VK_ERROR_UNKNOWN
);
639 ret
= anv_gem_execbuffer(device
, &fence
->execbuf
);
641 return vk_error(VK_ERROR_UNKNOWN
);
644 for (uint32_t i
= 0; i
< cmd_buffer
->bo_count
; i
++)
645 cmd_buffer
->exec2_bos
[i
]->offset
= cmd_buffer
->exec2_objects
[i
].offset
;
647 *(uint32_t *)queue
->completed_serial
.map
= cmd_buffer
->serial
;
654 VkResult
anv_QueueAddMemReferences(
657 const VkDeviceMemory
* pMems
)
662 VkResult
anv_QueueRemoveMemReferences(
665 const VkDeviceMemory
* pMems
)
670 VkResult
anv_QueueWaitIdle(
673 struct anv_queue
*queue
= (struct anv_queue
*) _queue
;
675 return vkDeviceWaitIdle((VkDevice
) queue
->device
);
678 VkResult
anv_DeviceWaitIdle(
681 struct anv_device
*device
= (struct anv_device
*) _device
;
682 struct anv_state state
;
683 struct anv_batch batch
;
684 struct drm_i915_gem_execbuffer2 execbuf
;
685 struct drm_i915_gem_exec_object2 exec2_objects
[1];
686 struct anv_bo
*bo
= NULL
;
691 state
= anv_state_pool_alloc(&device
->dynamic_state_pool
, 32, 32);
692 bo
= &device
->dynamic_state_pool
.block_pool
->bo
;
693 batch
.next
= state
.map
;
694 anv_batch_emit(&batch
, GEN8_MI_BATCH_BUFFER_END
);
695 anv_batch_emit(&batch
, GEN8_MI_NOOP
);
697 exec2_objects
[0].handle
= bo
->gem_handle
;
698 exec2_objects
[0].relocation_count
= 0;
699 exec2_objects
[0].relocs_ptr
= 0;
700 exec2_objects
[0].alignment
= 0;
701 exec2_objects
[0].offset
= bo
->offset
;
702 exec2_objects
[0].flags
= 0;
703 exec2_objects
[0].rsvd1
= 0;
704 exec2_objects
[0].rsvd2
= 0;
706 execbuf
.buffers_ptr
= (uintptr_t) exec2_objects
;
707 execbuf
.buffer_count
= 1;
708 execbuf
.batch_start_offset
= state
.offset
;
709 execbuf
.batch_len
= batch
.next
- state
.map
;
710 execbuf
.cliprects_ptr
= 0;
711 execbuf
.num_cliprects
= 0;
716 I915_EXEC_HANDLE_LUT
| I915_EXEC_NO_RELOC
| I915_EXEC_RENDER
;
717 execbuf
.rsvd1
= device
->context_id
;
720 if (!device
->no_hw
) {
721 ret
= anv_gem_execbuffer(device
, &execbuf
);
723 result
= vk_error(VK_ERROR_UNKNOWN
);
728 ret
= anv_gem_wait(device
, bo
->gem_handle
, &timeout
);
730 result
= vk_error(VK_ERROR_UNKNOWN
);
735 anv_state_pool_free(&device
->dynamic_state_pool
, state
);
740 anv_state_pool_free(&device
->dynamic_state_pool
, state
);
746 anv_device_alloc(struct anv_device
* device
,
749 VkSystemAllocType allocType
)
751 return device
->instance
->pfnAlloc(device
->instance
->pAllocUserData
,
758 anv_device_free(struct anv_device
* device
,
761 return device
->instance
->pfnFree(device
->instance
->pAllocUserData
,
766 anv_bo_init_new(struct anv_bo
*bo
, struct anv_device
*device
, uint64_t size
)
768 bo
->gem_handle
= anv_gem_create(device
, size
);
770 return vk_error(VK_ERROR_OUT_OF_DEVICE_MEMORY
);
780 VkResult
anv_AllocMemory(
782 const VkMemoryAllocInfo
* pAllocInfo
,
783 VkDeviceMemory
* pMem
)
785 struct anv_device
*device
= (struct anv_device
*) _device
;
786 struct anv_device_memory
*mem
;
789 assert(pAllocInfo
->sType
== VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO
);
791 mem
= anv_device_alloc(device
, sizeof(*mem
), 8,
792 VK_SYSTEM_ALLOC_TYPE_API_OBJECT
);
794 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
796 result
= anv_bo_init_new(&mem
->bo
, device
, pAllocInfo
->allocationSize
);
797 if (result
!= VK_SUCCESS
)
800 *pMem
= (VkDeviceMemory
) mem
;
805 anv_device_free(device
, mem
);
810 VkResult
anv_FreeMemory(
814 struct anv_device
*device
= (struct anv_device
*) _device
;
815 struct anv_device_memory
*mem
= (struct anv_device_memory
*) _mem
;
818 anv_gem_munmap(mem
->bo
.map
, mem
->bo
.size
);
820 if (mem
->bo
.gem_handle
!= 0)
821 anv_gem_close(device
, mem
->bo
.gem_handle
);
823 anv_device_free(device
, mem
);
828 VkResult
anv_SetMemoryPriority(
831 VkMemoryPriority priority
)
836 VkResult
anv_MapMemory(
841 VkMemoryMapFlags flags
,
844 struct anv_device
*device
= (struct anv_device
*) _device
;
845 struct anv_device_memory
*mem
= (struct anv_device_memory
*) _mem
;
847 /* FIXME: Is this supposed to be thread safe? Since vkUnmapMemory() only
848 * takes a VkDeviceMemory pointer, it seems like only one map of the memory
849 * at a time is valid. We could just mmap up front and return an offset
850 * pointer here, but that may exhaust virtual memory on 32 bit
853 mem
->map
= anv_gem_mmap(device
, mem
->bo
.gem_handle
, offset
, size
);
854 mem
->map_size
= size
;
861 VkResult
anv_UnmapMemory(
865 struct anv_device_memory
*mem
= (struct anv_device_memory
*) _mem
;
867 anv_gem_munmap(mem
->map
, mem
->map_size
);
872 VkResult
anv_FlushMappedMemory(
878 /* clflush here for !llc platforms */
883 VkResult
anv_PinSystemMemory(
887 VkDeviceMemory
* pMem
)
892 VkResult
anv_GetMultiDeviceCompatibility(
893 VkPhysicalDevice physicalDevice0
,
894 VkPhysicalDevice physicalDevice1
,
895 VkPhysicalDeviceCompatibilityInfo
* pInfo
)
897 return VK_UNSUPPORTED
;
900 VkResult
anv_OpenSharedMemory(
902 const VkMemoryOpenInfo
* pOpenInfo
,
903 VkDeviceMemory
* pMem
)
905 return VK_UNSUPPORTED
;
908 VkResult
anv_OpenSharedSemaphore(
910 const VkSemaphoreOpenInfo
* pOpenInfo
,
911 VkSemaphore
* pSemaphore
)
913 return VK_UNSUPPORTED
;
916 VkResult
anv_OpenPeerMemory(
918 const VkPeerMemoryOpenInfo
* pOpenInfo
,
919 VkDeviceMemory
* pMem
)
921 return VK_UNSUPPORTED
;
924 VkResult
anv_OpenPeerImage(
926 const VkPeerImageOpenInfo
* pOpenInfo
,
928 VkDeviceMemory
* pMem
)
930 return VK_UNSUPPORTED
;
934 anv_instance_destructor(struct anv_device
* device
,
937 return vkDestroyInstance(object
);
941 anv_noop_destructor(struct anv_device
* device
,
948 anv_device_destructor(struct anv_device
* device
,
951 return vkDestroyDevice(object
);
955 anv_cmd_buffer_destructor(struct anv_device
* device
,
958 struct anv_cmd_buffer
*cmd_buffer
= (struct anv_cmd_buffer
*) object
;
960 anv_gem_munmap(cmd_buffer
->surface_bo
.map
, BATCH_SIZE
);
961 anv_gem_close(device
, cmd_buffer
->surface_bo
.gem_handle
);
962 anv_state_stream_finish(&cmd_buffer
->surface_state_stream
);
963 anv_state_stream_finish(&cmd_buffer
->dynamic_state_stream
);
964 anv_state_stream_finish(&cmd_buffer
->binding_table_state_stream
);
965 anv_batch_finish(&cmd_buffer
->batch
, device
);
966 anv_device_free(device
, cmd_buffer
->exec2_objects
);
967 anv_device_free(device
, cmd_buffer
->exec2_bos
);
968 anv_device_free(device
, cmd_buffer
);
974 anv_pipeline_destructor(struct anv_device
* device
,
977 struct anv_pipeline
*pipeline
= (struct anv_pipeline
*) object
;
979 return anv_pipeline_destroy(pipeline
);
983 anv_free_destructor(struct anv_device
* device
,
986 anv_device_free(device
, (void *) object
);
992 anv_fence_destructor(struct anv_device
* device
,
995 struct anv_fence
*fence
= (struct anv_fence
*) object
;
997 anv_gem_munmap(fence
->bo
.map
, fence
->bo
.size
);
998 anv_gem_close(device
, fence
->bo
.gem_handle
);
999 anv_device_free(device
, fence
);
1005 anv_query_pool_destructor(struct anv_device
* device
,
1008 struct anv_query_pool
*pool
= (struct anv_query_pool
*) object
;
1010 anv_gem_munmap(pool
->bo
.map
, pool
->bo
.size
);
1011 anv_gem_close(device
, pool
->bo
.gem_handle
);
1012 anv_device_free(device
, pool
);
1017 static VkResult (*anv_object_destructors
[])(struct anv_device
*device
,
1018 VkObject object
) = {
1019 [VK_OBJECT_TYPE_INSTANCE
] = anv_instance_destructor
,
1020 [VK_OBJECT_TYPE_PHYSICAL_DEVICE
] = anv_noop_destructor
,
1021 [VK_OBJECT_TYPE_DEVICE
] = anv_device_destructor
,
1022 [VK_OBJECT_TYPE_QUEUE
] = anv_noop_destructor
,
1023 [VK_OBJECT_TYPE_COMMAND_BUFFER
] = anv_cmd_buffer_destructor
,
1024 [VK_OBJECT_TYPE_PIPELINE
] = anv_pipeline_destructor
,
1025 [VK_OBJECT_TYPE_SHADER
] = anv_free_destructor
,
1026 [VK_OBJECT_TYPE_BUFFER
] = anv_free_destructor
,
1027 [VK_OBJECT_TYPE_IMAGE
] = anv_free_destructor
,
1028 [VK_OBJECT_TYPE_RENDER_PASS
] = anv_free_destructor
,
1029 [VK_OBJECT_TYPE_FENCE
] = anv_fence_destructor
,
1030 [VK_OBJECT_TYPE_QUERY_POOL
] = anv_query_pool_destructor
1033 VkResult
anv_DestroyObject(
1035 VkObjectType objType
,
1038 struct anv_device
*device
= (struct anv_device
*) _device
;
1040 assert(objType
< ARRAY_SIZE(anv_object_destructors
) &&
1041 anv_object_destructors
[objType
] != NULL
);
1043 return anv_object_destructors
[objType
](device
, object
);
1047 fill_memory_requirements(
1048 VkObjectType objType
,
1050 VkMemoryRequirements
* memory_requirements
)
1052 struct anv_buffer
*buffer
;
1053 struct anv_image
*image
;
1055 memory_requirements
->memPropsAllowed
=
1056 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT
|
1057 VK_MEMORY_PROPERTY_HOST_DEVICE_COHERENT_BIT
|
1058 /* VK_MEMORY_PROPERTY_HOST_UNCACHED_BIT | */
1059 VK_MEMORY_PROPERTY_HOST_WRITE_COMBINED_BIT
|
1060 VK_MEMORY_PROPERTY_PREFER_HOST_LOCAL
|
1061 VK_MEMORY_PROPERTY_SHAREABLE_BIT
;
1063 memory_requirements
->memPropsRequired
= 0;
1066 case VK_OBJECT_TYPE_BUFFER
:
1067 buffer
= (struct anv_buffer
*) object
;
1068 memory_requirements
->size
= buffer
->size
;
1069 memory_requirements
->alignment
= 16;
1071 case VK_OBJECT_TYPE_IMAGE
:
1072 image
= (struct anv_image
*) object
;
1073 memory_requirements
->size
= image
->size
;
1074 memory_requirements
->alignment
= image
->alignment
;
1077 memory_requirements
->size
= 0;
1083 get_allocation_count(VkObjectType objType
)
1086 case VK_OBJECT_TYPE_BUFFER
:
1087 case VK_OBJECT_TYPE_IMAGE
:
1094 VkResult
anv_GetObjectInfo(
1096 VkObjectType objType
,
1098 VkObjectInfoType infoType
,
1102 VkMemoryRequirements memory_requirements
;
1106 case VK_OBJECT_INFO_TYPE_MEMORY_REQUIREMENTS
:
1107 *pDataSize
= sizeof(memory_requirements
);
1111 fill_memory_requirements(objType
, object
, pData
);
1114 case VK_OBJECT_INFO_TYPE_MEMORY_ALLOCATION_COUNT
:
1115 *pDataSize
= sizeof(count
);
1120 *count
= get_allocation_count(objType
);
1124 return VK_UNSUPPORTED
;
1129 VkResult
anv_QueueBindObjectMemory(
1131 VkObjectType objType
,
1133 uint32_t allocationIdx
,
1134 VkDeviceMemory _mem
,
1135 VkDeviceSize memOffset
)
1137 struct anv_buffer
*buffer
;
1138 struct anv_image
*image
;
1139 struct anv_device_memory
*mem
= (struct anv_device_memory
*) _mem
;
1142 case VK_OBJECT_TYPE_BUFFER
:
1143 buffer
= (struct anv_buffer
*) object
;
1144 buffer
->bo
= &mem
->bo
;
1145 buffer
->offset
= memOffset
;
1147 case VK_OBJECT_TYPE_IMAGE
:
1148 image
= (struct anv_image
*) object
;
1149 image
->bo
= &mem
->bo
;
1150 image
->offset
= memOffset
;
1159 VkResult
anv_QueueBindObjectMemoryRange(
1161 VkObjectType objType
,
1163 uint32_t allocationIdx
,
1164 VkDeviceSize rangeOffset
,
1165 VkDeviceSize rangeSize
,
1167 VkDeviceSize memOffset
)
1169 stub_return(VK_UNSUPPORTED
);
1172 VkResult
anv_QueueBindImageMemoryRange(
1175 uint32_t allocationIdx
,
1176 const VkImageMemoryBindInfo
* pBindInfo
,
1178 VkDeviceSize memOffset
)
1180 stub_return(VK_UNSUPPORTED
);
1183 VkResult
anv_CreateFence(
1185 const VkFenceCreateInfo
* pCreateInfo
,
1188 struct anv_device
*device
= (struct anv_device
*) _device
;
1189 struct anv_fence
*fence
;
1190 struct anv_batch batch
;
1193 const uint32_t fence_size
= 128;
1195 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_FENCE_CREATE_INFO
);
1197 fence
= anv_device_alloc(device
, sizeof(*fence
), 8,
1198 VK_SYSTEM_ALLOC_TYPE_API_OBJECT
);
1200 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
1202 result
= anv_bo_init_new(&fence
->bo
, device
, fence_size
);
1203 if (result
!= VK_SUCCESS
)
1207 anv_gem_mmap(device
, fence
->bo
.gem_handle
, 0, fence
->bo
.size
);
1208 batch
.next
= fence
->bo
.map
;
1209 anv_batch_emit(&batch
, GEN8_MI_BATCH_BUFFER_END
);
1210 anv_batch_emit(&batch
, GEN8_MI_NOOP
);
1212 fence
->exec2_objects
[0].handle
= fence
->bo
.gem_handle
;
1213 fence
->exec2_objects
[0].relocation_count
= 0;
1214 fence
->exec2_objects
[0].relocs_ptr
= 0;
1215 fence
->exec2_objects
[0].alignment
= 0;
1216 fence
->exec2_objects
[0].offset
= fence
->bo
.offset
;
1217 fence
->exec2_objects
[0].flags
= 0;
1218 fence
->exec2_objects
[0].rsvd1
= 0;
1219 fence
->exec2_objects
[0].rsvd2
= 0;
1221 fence
->execbuf
.buffers_ptr
= (uintptr_t) fence
->exec2_objects
;
1222 fence
->execbuf
.buffer_count
= 1;
1223 fence
->execbuf
.batch_start_offset
= 0;
1224 fence
->execbuf
.batch_len
= batch
.next
- fence
->bo
.map
;
1225 fence
->execbuf
.cliprects_ptr
= 0;
1226 fence
->execbuf
.num_cliprects
= 0;
1227 fence
->execbuf
.DR1
= 0;
1228 fence
->execbuf
.DR4
= 0;
1230 fence
->execbuf
.flags
=
1231 I915_EXEC_HANDLE_LUT
| I915_EXEC_NO_RELOC
| I915_EXEC_RENDER
;
1232 fence
->execbuf
.rsvd1
= device
->context_id
;
1233 fence
->execbuf
.rsvd2
= 0;
1235 *pFence
= (VkQueryPool
) fence
;
1240 anv_device_free(device
, fence
);
1245 VkResult
anv_ResetFences(
1247 uint32_t fenceCount
,
1250 struct anv_fence
**fences
= (struct anv_fence
**) pFences
;
1252 for (uint32_t i
; i
< fenceCount
; i
++)
1253 fences
[i
]->ready
= false;
1258 VkResult
anv_GetFenceStatus(
1262 struct anv_device
*device
= (struct anv_device
*) _device
;
1263 struct anv_fence
*fence
= (struct anv_fence
*) _fence
;
1270 ret
= anv_gem_wait(device
, fence
->bo
.gem_handle
, &t
);
1272 fence
->ready
= true;
1276 return VK_NOT_READY
;
1279 VkResult
anv_WaitForFences(
1281 uint32_t fenceCount
,
1282 const VkFence
* pFences
,
1286 struct anv_device
*device
= (struct anv_device
*) _device
;
1287 struct anv_fence
**fences
= (struct anv_fence
**) pFences
;
1288 int64_t t
= timeout
;
1291 /* FIXME: handle !waitAll */
1293 for (uint32_t i
= 0; i
< fenceCount
; i
++) {
1294 ret
= anv_gem_wait(device
, fences
[i
]->bo
.gem_handle
, &t
);
1295 if (ret
== -1 && errno
== ETIME
)
1298 return vk_error(VK_ERROR_UNKNOWN
);
1304 // Queue semaphore functions
1306 VkResult
anv_CreateSemaphore(
1308 const VkSemaphoreCreateInfo
* pCreateInfo
,
1309 VkSemaphore
* pSemaphore
)
1311 stub_return(VK_UNSUPPORTED
);
1314 VkResult
anv_QueueSignalSemaphore(
1316 VkSemaphore semaphore
)
1318 stub_return(VK_UNSUPPORTED
);
1321 VkResult
anv_QueueWaitSemaphore(
1323 VkSemaphore semaphore
)
1325 stub_return(VK_UNSUPPORTED
);
1330 VkResult
anv_CreateEvent(
1332 const VkEventCreateInfo
* pCreateInfo
,
1335 stub_return(VK_UNSUPPORTED
);
1338 VkResult
anv_GetEventStatus(
1342 stub_return(VK_UNSUPPORTED
);
1345 VkResult
anv_SetEvent(
1349 stub_return(VK_UNSUPPORTED
);
1352 VkResult
anv_ResetEvent(
1356 stub_return(VK_UNSUPPORTED
);
1361 VkResult
anv_CreateQueryPool(
1363 const VkQueryPoolCreateInfo
* pCreateInfo
,
1364 VkQueryPool
* pQueryPool
)
1366 struct anv_device
*device
= (struct anv_device
*) _device
;
1367 struct anv_query_pool
*pool
;
1371 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_QUERY_POOL_CREATE_INFO
);
1373 switch (pCreateInfo
->queryType
) {
1374 case VK_QUERY_TYPE_OCCLUSION
:
1376 case VK_QUERY_TYPE_PIPELINE_STATISTICS
:
1377 return VK_UNSUPPORTED
;
1382 pool
= anv_device_alloc(device
, sizeof(*pool
), 8,
1383 VK_SYSTEM_ALLOC_TYPE_API_OBJECT
);
1385 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
1387 pool
->type
= pCreateInfo
->queryType
;
1388 size
= pCreateInfo
->slots
* sizeof(struct anv_query_pool_slot
);
1389 result
= anv_bo_init_new(&pool
->bo
, device
, size
);
1390 if (result
!= VK_SUCCESS
)
1393 pool
->bo
.map
= anv_gem_mmap(device
, pool
->bo
.gem_handle
, 0, size
);
1395 *pQueryPool
= (VkQueryPool
) pool
;
1400 anv_device_free(device
, pool
);
1405 VkResult
anv_GetQueryPoolResults(
1407 VkQueryPool queryPool
,
1408 uint32_t startQuery
,
1409 uint32_t queryCount
,
1412 VkQueryResultFlags flags
)
1414 struct anv_device
*device
= (struct anv_device
*) _device
;
1415 struct anv_query_pool
*pool
= (struct anv_query_pool
*) queryPool
;
1416 struct anv_query_pool_slot
*slot
= pool
->bo
.map
;
1417 int64_t timeout
= INT64_MAX
;
1418 uint32_t *dst32
= pData
;
1419 uint64_t *dst64
= pData
;
1423 if (flags
& VK_QUERY_RESULT_WITH_AVAILABILITY_BIT
) {
1424 /* Where is the availabilty info supposed to go? */
1425 anv_finishme("VK_QUERY_RESULT_WITH_AVAILABILITY_BIT");
1426 return VK_UNSUPPORTED
;
1429 assert(pool
->type
== VK_QUERY_TYPE_OCCLUSION
);
1431 if (flags
& VK_QUERY_RESULT_64_BIT
)
1432 *pDataSize
= queryCount
* sizeof(uint64_t);
1434 *pDataSize
= queryCount
* sizeof(uint32_t);
1439 if (flags
& VK_QUERY_RESULT_WAIT_BIT
) {
1440 ret
= anv_gem_wait(device
, pool
->bo
.gem_handle
, &timeout
);
1442 return vk_error(VK_ERROR_UNKNOWN
);
1445 for (uint32_t i
= 0; i
< queryCount
; i
++) {
1446 result
= slot
[startQuery
+ i
].end
- slot
[startQuery
+ i
].begin
;
1447 if (flags
& VK_QUERY_RESULT_64_BIT
) {
1450 if (result
> UINT32_MAX
)
1451 result
= UINT32_MAX
;
1461 VkResult
anv_CreateBuffer(
1463 const VkBufferCreateInfo
* pCreateInfo
,
1466 struct anv_device
*device
= (struct anv_device
*) _device
;
1467 struct anv_buffer
*buffer
;
1469 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO
);
1471 buffer
= anv_device_alloc(device
, sizeof(*buffer
), 8,
1472 VK_SYSTEM_ALLOC_TYPE_API_OBJECT
);
1474 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
1476 buffer
->size
= pCreateInfo
->size
;
1480 *pBuffer
= (VkBuffer
) buffer
;
1485 // Buffer view functions
1487 VkResult
anv_CreateBufferView(
1489 const VkBufferViewCreateInfo
* pCreateInfo
,
1490 VkBufferView
* pView
)
1492 struct anv_device
*device
= (struct anv_device
*) _device
;
1493 struct anv_buffer
*buffer
= (struct anv_buffer
*) pCreateInfo
->buffer
;
1494 struct anv_surface_view
*view
;
1495 const struct anv_format
*format
;
1497 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO
);
1499 view
= anv_device_alloc(device
, sizeof(*view
), 8,
1500 VK_SYSTEM_ALLOC_TYPE_API_OBJECT
);
1502 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
1504 view
->bo
= buffer
->bo
;
1505 view
->offset
= buffer
->offset
+ pCreateInfo
->offset
;
1506 view
->surface_state
=
1507 anv_state_pool_alloc(&device
->surface_state_pool
, 64, 64);
1508 view
->format
= pCreateInfo
->format
;
1510 format
= anv_format_for_vk_format(pCreateInfo
->format
);
1511 /* This assumes RGBA float format. */
1512 uint32_t stride
= 4;
1513 uint32_t num_elements
= pCreateInfo
->range
/ stride
;
1514 struct GEN8_RENDER_SURFACE_STATE surface_state
= {
1515 .SurfaceType
= SURFTYPE_BUFFER
,
1516 .SurfaceArray
= false,
1517 .SurfaceFormat
= format
->format
,
1518 .SurfaceVerticalAlignment
= VALIGN4
,
1519 .SurfaceHorizontalAlignment
= HALIGN4
,
1521 .VerticalLineStride
= 0,
1522 .VerticalLineStrideOffset
= 0,
1523 .SamplerL2BypassModeDisable
= true,
1524 .RenderCacheReadWriteMode
= WriteOnlyCache
,
1525 .MemoryObjectControlState
= 0, /* FIXME: MOCS */
1528 .Height
= (num_elements
>> 7) & 0x3fff,
1529 .Width
= num_elements
& 0x7f,
1530 .Depth
= (num_elements
>> 21) & 0x3f,
1531 .SurfacePitch
= stride
- 1,
1532 .MinimumArrayElement
= 0,
1533 .NumberofMultisamples
= MULTISAMPLECOUNT_1
,
1538 .AuxiliarySurfaceMode
= AUX_NONE
,
1540 .GreenClearColor
= 0,
1541 .BlueClearColor
= 0,
1542 .AlphaClearColor
= 0,
1543 .ShaderChannelSelectRed
= SCS_RED
,
1544 .ShaderChannelSelectGreen
= SCS_GREEN
,
1545 .ShaderChannelSelectBlue
= SCS_BLUE
,
1546 .ShaderChannelSelectAlpha
= SCS_ALPHA
,
1547 .ResourceMinLOD
= 0,
1548 /* FIXME: We assume that the image must be bound at this time. */
1549 .SurfaceBaseAddress
= { NULL
, view
->offset
},
1552 GEN8_RENDER_SURFACE_STATE_pack(NULL
, view
->surface_state
.map
, &surface_state
);
1554 *pView
= (VkImageView
) view
;
1559 // Sampler functions
1561 VkResult
anv_CreateSampler(
1563 const VkSamplerCreateInfo
* pCreateInfo
,
1564 VkSampler
* pSampler
)
1566 struct anv_device
*device
= (struct anv_device
*) _device
;
1567 struct anv_sampler
*sampler
;
1569 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO
);
1571 sampler
= anv_device_alloc(device
, sizeof(*sampler
), 8,
1572 VK_SYSTEM_ALLOC_TYPE_API_OBJECT
);
1574 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
1576 static const uint32_t vk_to_gen_tex_filter
[] = {
1577 [VK_TEX_FILTER_NEAREST
] = MAPFILTER_NEAREST
,
1578 [VK_TEX_FILTER_LINEAR
] = MAPFILTER_LINEAR
1581 static const uint32_t vk_to_gen_mipmap_mode
[] = {
1582 [VK_TEX_MIPMAP_MODE_BASE
] = MIPFILTER_NONE
,
1583 [VK_TEX_MIPMAP_MODE_NEAREST
] = MIPFILTER_NEAREST
,
1584 [VK_TEX_MIPMAP_MODE_LINEAR
] = MIPFILTER_LINEAR
1587 static const uint32_t vk_to_gen_tex_address
[] = {
1588 [VK_TEX_ADDRESS_WRAP
] = TCM_WRAP
,
1589 [VK_TEX_ADDRESS_MIRROR
] = TCM_MIRROR
,
1590 [VK_TEX_ADDRESS_CLAMP
] = TCM_CLAMP
,
1591 [VK_TEX_ADDRESS_MIRROR_ONCE
] = TCM_MIRROR_ONCE
,
1592 [VK_TEX_ADDRESS_CLAMP_BORDER
] = TCM_CLAMP_BORDER
,
1595 static const uint32_t vk_to_gen_compare_op
[] = {
1596 [VK_COMPARE_OP_NEVER
] = PREFILTEROPNEVER
,
1597 [VK_COMPARE_OP_LESS
] = PREFILTEROPLESS
,
1598 [VK_COMPARE_OP_EQUAL
] = PREFILTEROPEQUAL
,
1599 [VK_COMPARE_OP_LESS_EQUAL
] = PREFILTEROPLEQUAL
,
1600 [VK_COMPARE_OP_GREATER
] = PREFILTEROPGREATER
,
1601 [VK_COMPARE_OP_NOT_EQUAL
] = PREFILTEROPNOTEQUAL
,
1602 [VK_COMPARE_OP_GREATER_EQUAL
] = PREFILTEROPGEQUAL
,
1603 [VK_COMPARE_OP_ALWAYS
] = PREFILTEROPALWAYS
,
1606 if (pCreateInfo
->maxAnisotropy
> 0)
1607 anv_finishme("missing support for anisotropic filtering");
1609 struct GEN8_SAMPLER_STATE sampler_state
= {
1610 .SamplerDisable
= false,
1611 .TextureBorderColorMode
= DX10OGL
,
1612 .LODPreClampMode
= 0,
1614 .MipModeFilter
= vk_to_gen_mipmap_mode
[pCreateInfo
->mipMode
],
1615 .MagModeFilter
= vk_to_gen_tex_filter
[pCreateInfo
->magFilter
],
1616 .MinModeFilter
= vk_to_gen_tex_filter
[pCreateInfo
->minFilter
],
1617 .TextureLODBias
= pCreateInfo
->mipLodBias
* 256,
1618 .AnisotropicAlgorithm
= EWAApproximation
,
1619 .MinLOD
= pCreateInfo
->minLod
* 256,
1620 .MaxLOD
= pCreateInfo
->maxLod
* 256,
1621 .ChromaKeyEnable
= 0,
1622 .ChromaKeyIndex
= 0,
1624 .ShadowFunction
= vk_to_gen_compare_op
[pCreateInfo
->compareOp
],
1625 .CubeSurfaceControlMode
= 0,
1626 .IndirectStatePointer
= 0,
1627 .LODClampMagnificationMode
= MIPNONE
,
1628 .MaximumAnisotropy
= 0,
1629 .RAddressMinFilterRoundingEnable
= 0,
1630 .RAddressMagFilterRoundingEnable
= 0,
1631 .VAddressMinFilterRoundingEnable
= 0,
1632 .VAddressMagFilterRoundingEnable
= 0,
1633 .UAddressMinFilterRoundingEnable
= 0,
1634 .UAddressMagFilterRoundingEnable
= 0,
1635 .TrilinearFilterQuality
= 0,
1636 .NonnormalizedCoordinateEnable
= 0,
1637 .TCXAddressControlMode
= vk_to_gen_tex_address
[pCreateInfo
->addressU
],
1638 .TCYAddressControlMode
= vk_to_gen_tex_address
[pCreateInfo
->addressV
],
1639 .TCZAddressControlMode
= vk_to_gen_tex_address
[pCreateInfo
->addressW
],
1642 GEN8_SAMPLER_STATE_pack(NULL
, sampler
->state
, &sampler_state
);
1644 *pSampler
= (VkSampler
) sampler
;
1649 // Descriptor set functions
1651 VkResult
anv_CreateDescriptorSetLayout(
1653 const VkDescriptorSetLayoutCreateInfo
* pCreateInfo
,
1654 VkDescriptorSetLayout
* pSetLayout
)
1656 struct anv_device
*device
= (struct anv_device
*) _device
;
1657 struct anv_descriptor_set_layout
*set_layout
;
1659 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO
);
1661 uint32_t sampler_count
[VK_NUM_SHADER_STAGE
] = { 0, };
1662 uint32_t surface_count
[VK_NUM_SHADER_STAGE
] = { 0, };
1663 uint32_t num_dynamic_buffers
= 0;
1667 for (uint32_t i
= 0; i
< pCreateInfo
->count
; i
++) {
1668 switch (pCreateInfo
->pBinding
[i
].descriptorType
) {
1669 case VK_DESCRIPTOR_TYPE_SAMPLER
:
1670 for_each_bit(s
, pCreateInfo
->pBinding
[i
].stageFlags
)
1671 sampler_count
[s
] += pCreateInfo
->pBinding
[i
].count
;
1674 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
:
1675 for_each_bit(s
, pCreateInfo
->pBinding
[i
].stageFlags
)
1676 sampler_count
[s
] += pCreateInfo
->pBinding
[i
].count
;
1680 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE
:
1681 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE
:
1682 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER
:
1683 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER
:
1684 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER
:
1685 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER
:
1686 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
:
1687 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
:
1688 for_each_bit(s
, pCreateInfo
->pBinding
[i
].stageFlags
)
1689 surface_count
[s
] += pCreateInfo
->pBinding
[i
].count
;
1695 count
+= pCreateInfo
->pBinding
[i
].count
;
1698 for (uint32_t i
= 0; i
< pCreateInfo
->count
; i
++) {
1699 switch (pCreateInfo
->pBinding
[i
].descriptorType
) {
1700 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
:
1701 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
:
1702 num_dynamic_buffers
++;
1709 uint32_t sampler_total
= 0;
1710 uint32_t surface_total
= 0;
1711 for (uint32_t s
= 0; s
< VK_NUM_SHADER_STAGE
; s
++) {
1712 sampler_total
+= sampler_count
[s
];
1713 surface_total
+= surface_count
[s
];
1716 size_t size
= sizeof(*set_layout
) +
1717 (sampler_total
+ surface_total
) * sizeof(uint32_t);
1718 set_layout
= anv_device_alloc(device
, size
, 8,
1719 VK_SYSTEM_ALLOC_TYPE_API_OBJECT
);
1721 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
1723 set_layout
->num_dynamic_buffers
= num_dynamic_buffers
;
1724 set_layout
->count
= count
;
1726 uint32_t *p
= set_layout
->entries
;
1727 uint32_t *sampler
[VK_NUM_SHADER_STAGE
];
1728 uint32_t *surface
[VK_NUM_SHADER_STAGE
];
1729 for (uint32_t s
= 0; s
< VK_NUM_SHADER_STAGE
; s
++) {
1730 set_layout
->stage
[s
].surface_count
= surface_count
[s
];
1731 set_layout
->stage
[s
].surface_start
= surface
[s
] = p
;
1732 p
+= surface_count
[s
];
1733 set_layout
->stage
[s
].sampler_count
= sampler_count
[s
];
1734 set_layout
->stage
[s
].sampler_start
= sampler
[s
] = p
;
1735 p
+= sampler_count
[s
];
1738 uint32_t descriptor
= 0;
1739 for (uint32_t i
= 0; i
< pCreateInfo
->count
; i
++) {
1740 switch (pCreateInfo
->pBinding
[i
].descriptorType
) {
1741 case VK_DESCRIPTOR_TYPE_SAMPLER
:
1742 for_each_bit(s
, pCreateInfo
->pBinding
[i
].stageFlags
)
1743 for (uint32_t j
= 0; j
< pCreateInfo
->pBinding
[i
].count
; j
++)
1744 *(sampler
[s
])++ = descriptor
+ j
;
1747 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
:
1748 for_each_bit(s
, pCreateInfo
->pBinding
[i
].stageFlags
)
1749 for (uint32_t j
= 0; j
< pCreateInfo
->pBinding
[i
].count
; j
++)
1750 *(sampler
[s
])++ = descriptor
+ j
;
1754 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE
:
1755 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE
:
1756 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER
:
1757 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER
:
1758 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER
:
1759 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER
:
1760 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
:
1761 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
:
1762 for_each_bit(s
, pCreateInfo
->pBinding
[i
].stageFlags
)
1763 for (uint32_t j
= 0; j
< pCreateInfo
->pBinding
[i
].count
; j
++) {
1764 *(surface
[s
])++ = descriptor
+ j
;
1770 descriptor
+= pCreateInfo
->pBinding
[i
].count
;
1773 *pSetLayout
= (VkDescriptorSetLayout
) set_layout
;
1778 VkResult
anv_BeginDescriptorPoolUpdate(
1780 VkDescriptorUpdateMode updateMode
)
1785 VkResult
anv_EndDescriptorPoolUpdate(
1792 VkResult
anv_CreateDescriptorPool(
1794 VkDescriptorPoolUsage poolUsage
,
1796 const VkDescriptorPoolCreateInfo
* pCreateInfo
,
1797 VkDescriptorPool
* pDescriptorPool
)
1799 *pDescriptorPool
= 1;
1804 VkResult
anv_ResetDescriptorPool(
1806 VkDescriptorPool descriptorPool
)
1811 VkResult
anv_AllocDescriptorSets(
1813 VkDescriptorPool descriptorPool
,
1814 VkDescriptorSetUsage setUsage
,
1816 const VkDescriptorSetLayout
* pSetLayouts
,
1817 VkDescriptorSet
* pDescriptorSets
,
1820 struct anv_device
*device
= (struct anv_device
*) _device
;
1821 const struct anv_descriptor_set_layout
*layout
;
1822 struct anv_descriptor_set
*set
;
1825 for (uint32_t i
= 0; i
< count
; i
++) {
1826 layout
= (struct anv_descriptor_set_layout
*) pSetLayouts
[i
];
1827 size
= sizeof(*set
) + layout
->count
* sizeof(set
->descriptors
[0]);
1828 set
= anv_device_alloc(device
, size
, 8,
1829 VK_SYSTEM_ALLOC_TYPE_API_OBJECT
);
1832 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
1835 /* Descriptor sets may not be 100% filled out so we need to memset to
1836 * ensure that we can properly detect and handle holes.
1838 memset(set
, 0, size
);
1840 pDescriptorSets
[i
] = (VkDescriptorSet
) set
;
1848 void anv_ClearDescriptorSets(
1850 VkDescriptorPool descriptorPool
,
1852 const VkDescriptorSet
* pDescriptorSets
)
1856 void anv_UpdateDescriptors(
1858 VkDescriptorSet descriptorSet
,
1859 uint32_t updateCount
,
1860 const void** ppUpdateArray
)
1862 struct anv_descriptor_set
*set
= (struct anv_descriptor_set
*) descriptorSet
;
1863 VkUpdateSamplers
*update_samplers
;
1864 VkUpdateSamplerTextures
*update_sampler_textures
;
1865 VkUpdateImages
*update_images
;
1866 VkUpdateBuffers
*update_buffers
;
1867 VkUpdateAsCopy
*update_as_copy
;
1869 for (uint32_t i
= 0; i
< updateCount
; i
++) {
1870 const struct anv_common
*common
= ppUpdateArray
[i
];
1872 switch (common
->sType
) {
1873 case VK_STRUCTURE_TYPE_UPDATE_SAMPLERS
:
1874 update_samplers
= (VkUpdateSamplers
*) common
;
1876 for (uint32_t j
= 0; j
< update_samplers
->count
; j
++) {
1877 set
->descriptors
[update_samplers
->binding
+ j
].sampler
=
1878 (struct anv_sampler
*) update_samplers
->pSamplers
[j
];
1882 case VK_STRUCTURE_TYPE_UPDATE_SAMPLER_TEXTURES
:
1883 /* FIXME: Shouldn't this be *_UPDATE_SAMPLER_IMAGES? */
1884 update_sampler_textures
= (VkUpdateSamplerTextures
*) common
;
1886 for (uint32_t j
= 0; j
< update_sampler_textures
->count
; j
++) {
1887 set
->descriptors
[update_sampler_textures
->binding
+ j
].view
=
1888 (struct anv_surface_view
*)
1889 update_sampler_textures
->pSamplerImageViews
[j
].pImageView
->view
;
1890 set
->descriptors
[update_sampler_textures
->binding
+ j
].sampler
=
1891 (struct anv_sampler
*)
1892 update_sampler_textures
->pSamplerImageViews
[j
].sampler
;
1896 case VK_STRUCTURE_TYPE_UPDATE_IMAGES
:
1897 update_images
= (VkUpdateImages
*) common
;
1899 for (uint32_t j
= 0; j
< update_images
->count
; j
++) {
1900 set
->descriptors
[update_images
->binding
+ j
].view
=
1901 (struct anv_surface_view
*) update_images
->pImageViews
[j
].view
;
1905 case VK_STRUCTURE_TYPE_UPDATE_BUFFERS
:
1906 update_buffers
= (VkUpdateBuffers
*) common
;
1908 for (uint32_t j
= 0; j
< update_buffers
->count
; j
++) {
1909 set
->descriptors
[update_buffers
->binding
+ j
].view
=
1910 (struct anv_surface_view
*) update_buffers
->pBufferViews
[j
].view
;
1912 /* FIXME: descriptor arrays? */
1915 case VK_STRUCTURE_TYPE_UPDATE_AS_COPY
:
1916 update_as_copy
= (VkUpdateAsCopy
*) common
;
1917 (void) update_as_copy
;
1926 // State object functions
1928 static inline int64_t
1929 clamp_int64(int64_t x
, int64_t min
, int64_t max
)
1939 VkResult
anv_CreateDynamicViewportState(
1941 const VkDynamicVpStateCreateInfo
* pCreateInfo
,
1942 VkDynamicVpState
* pState
)
1944 struct anv_device
*device
= (struct anv_device
*) _device
;
1945 struct anv_dynamic_vp_state
*state
;
1947 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_DYNAMIC_VP_STATE_CREATE_INFO
);
1949 state
= anv_device_alloc(device
, sizeof(*state
), 8,
1950 VK_SYSTEM_ALLOC_TYPE_API_OBJECT
);
1952 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
1954 unsigned count
= pCreateInfo
->viewportAndScissorCount
;
1955 state
->sf_clip_vp
= anv_state_pool_alloc(&device
->dynamic_state_pool
,
1957 state
->cc_vp
= anv_state_pool_alloc(&device
->dynamic_state_pool
,
1959 state
->scissor
= anv_state_pool_alloc(&device
->dynamic_state_pool
,
1962 for (uint32_t i
= 0; i
< pCreateInfo
->viewportAndScissorCount
; i
++) {
1963 const VkViewport
*vp
= &pCreateInfo
->pViewports
[i
];
1964 const VkRect
*s
= &pCreateInfo
->pScissors
[i
];
1966 struct GEN8_SF_CLIP_VIEWPORT sf_clip_viewport
= {
1967 .ViewportMatrixElementm00
= vp
->width
/ 2,
1968 .ViewportMatrixElementm11
= vp
->height
/ 2,
1969 .ViewportMatrixElementm22
= (vp
->maxDepth
- vp
->minDepth
) / 2,
1970 .ViewportMatrixElementm30
= vp
->originX
+ vp
->width
/ 2,
1971 .ViewportMatrixElementm31
= vp
->originY
+ vp
->height
/ 2,
1972 .ViewportMatrixElementm32
= (vp
->maxDepth
+ vp
->minDepth
) / 2,
1973 .XMinClipGuardband
= -1.0f
,
1974 .XMaxClipGuardband
= 1.0f
,
1975 .YMinClipGuardband
= -1.0f
,
1976 .YMaxClipGuardband
= 1.0f
,
1977 .XMinViewPort
= vp
->originX
,
1978 .XMaxViewPort
= vp
->originX
+ vp
->width
- 1,
1979 .YMinViewPort
= vp
->originY
,
1980 .YMaxViewPort
= vp
->originY
+ vp
->height
- 1,
1983 struct GEN8_CC_VIEWPORT cc_viewport
= {
1984 .MinimumDepth
= vp
->minDepth
,
1985 .MaximumDepth
= vp
->maxDepth
1988 /* Since xmax and ymax are inclusive, we have to have xmax < xmin or
1989 * ymax < ymin for empty clips. In case clip x, y, width height are all
1990 * 0, the clamps below produce 0 for xmin, ymin, xmax, ymax, which isn't
1991 * what we want. Just special case empty clips and produce a canonical
1993 static const struct GEN8_SCISSOR_RECT empty_scissor
= {
1994 .ScissorRectangleYMin
= 1,
1995 .ScissorRectangleXMin
= 1,
1996 .ScissorRectangleYMax
= 0,
1997 .ScissorRectangleXMax
= 0
2000 const int max
= 0xffff;
2001 struct GEN8_SCISSOR_RECT scissor
= {
2002 /* Do this math using int64_t so overflow gets clamped correctly. */
2003 .ScissorRectangleYMin
= clamp_int64(s
->offset
.y
, 0, max
),
2004 .ScissorRectangleXMin
= clamp_int64(s
->offset
.x
, 0, max
),
2005 .ScissorRectangleYMax
= clamp_int64((uint64_t) s
->offset
.y
+ s
->extent
.height
- 1, 0, max
),
2006 .ScissorRectangleXMax
= clamp_int64((uint64_t) s
->offset
.x
+ s
->extent
.width
- 1, 0, max
)
2009 GEN8_SF_CLIP_VIEWPORT_pack(NULL
, state
->sf_clip_vp
.map
+ i
* 64, &sf_clip_viewport
);
2010 GEN8_CC_VIEWPORT_pack(NULL
, state
->cc_vp
.map
+ i
* 32, &cc_viewport
);
2012 if (s
->extent
.width
<= 0 || s
->extent
.height
<= 0) {
2013 GEN8_SCISSOR_RECT_pack(NULL
, state
->scissor
.map
+ i
* 32, &empty_scissor
);
2015 GEN8_SCISSOR_RECT_pack(NULL
, state
->scissor
.map
+ i
* 32, &scissor
);
2019 *pState
= (VkDynamicVpState
) state
;
2024 VkResult
anv_CreateDynamicRasterState(
2026 const VkDynamicRsStateCreateInfo
* pCreateInfo
,
2027 VkDynamicRsState
* pState
)
2029 struct anv_device
*device
= (struct anv_device
*) _device
;
2030 struct anv_dynamic_rs_state
*state
;
2032 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_DYNAMIC_RS_STATE_CREATE_INFO
);
2034 state
= anv_device_alloc(device
, sizeof(*state
), 8,
2035 VK_SYSTEM_ALLOC_TYPE_API_OBJECT
);
2037 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
2041 * float depthBiasClamp;
2042 * float slopeScaledDepthBias;
2043 * float pointFadeThreshold;
2044 * // optional (GL45) - Size of point fade threshold
2047 struct GEN8_3DSTATE_SF sf
= {
2048 GEN8_3DSTATE_SF_header
,
2049 .LineWidth
= pCreateInfo
->lineWidth
,
2050 .PointWidth
= pCreateInfo
->pointSize
,
2053 GEN8_3DSTATE_SF_pack(NULL
, state
->state_sf
, &sf
);
2055 *pState
= (VkDynamicRsState
) state
;
2060 VkResult
anv_CreateDynamicColorBlendState(
2062 const VkDynamicCbStateCreateInfo
* pCreateInfo
,
2063 VkDynamicCbState
* pState
)
2065 struct anv_device
*device
= (struct anv_device
*) _device
;
2066 struct anv_dynamic_cb_state
*state
;
2068 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_DYNAMIC_CB_STATE_CREATE_INFO
);
2070 state
= anv_device_alloc(device
, sizeof(*state
), 8,
2071 VK_SYSTEM_ALLOC_TYPE_API_OBJECT
);
2073 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
2075 *pState
= (VkDynamicCbState
) state
;
2080 VkResult
anv_CreateDynamicDepthStencilState(
2082 const VkDynamicDsStateCreateInfo
* pCreateInfo
,
2083 VkDynamicDsState
* pState
)
2085 stub_return(VK_UNSUPPORTED
);
2088 // Command buffer functions
2090 VkResult
anv_CreateCommandBuffer(
2092 const VkCmdBufferCreateInfo
* pCreateInfo
,
2093 VkCmdBuffer
* pCmdBuffer
)
2095 struct anv_device
*device
= (struct anv_device
*) _device
;
2096 struct anv_cmd_buffer
*cmd_buffer
;
2099 cmd_buffer
= anv_device_alloc(device
, sizeof(*cmd_buffer
), 8,
2100 VK_SYSTEM_ALLOC_TYPE_API_OBJECT
);
2101 if (cmd_buffer
== NULL
)
2102 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
2104 cmd_buffer
->device
= device
;
2105 cmd_buffer
->rs_state
= NULL
;
2106 cmd_buffer
->vp_state
= NULL
;
2107 memset(&cmd_buffer
->default_bindings
, 0, sizeof(cmd_buffer
->default_bindings
));
2108 cmd_buffer
->bindings
= &cmd_buffer
->default_bindings
;
2110 result
= anv_batch_init(&cmd_buffer
->batch
, device
);
2111 if (result
!= VK_SUCCESS
)
2114 result
= anv_bo_init_new(&cmd_buffer
->surface_bo
, device
, BATCH_SIZE
);
2115 if (result
!= VK_SUCCESS
)
2118 cmd_buffer
->surface_bo
.map
=
2119 anv_gem_mmap(device
, cmd_buffer
->surface_bo
.gem_handle
, 0, BATCH_SIZE
);
2120 if (cmd_buffer
->surface_bo
.map
== NULL
) {
2121 result
= vk_error(VK_ERROR_MEMORY_MAP_FAILED
);
2122 goto fail_surface_bo
;
2125 /* Start surface_next at 1 so surface offset 0 is invalid. */
2126 cmd_buffer
->surface_next
= 1;
2127 cmd_buffer
->surface_relocs
.num_relocs
= 0;
2129 cmd_buffer
->exec2_objects
=
2130 anv_device_alloc(device
, 8192 * sizeof(cmd_buffer
->exec2_objects
[0]), 8,
2131 VK_SYSTEM_ALLOC_TYPE_API_OBJECT
);
2132 if (cmd_buffer
->exec2_objects
== NULL
) {
2133 result
= vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
2134 goto fail_surface_map
;
2137 cmd_buffer
->exec2_bos
=
2138 anv_device_alloc(device
, 8192 * sizeof(cmd_buffer
->exec2_bos
[0]), 8,
2139 VK_SYSTEM_ALLOC_TYPE_API_OBJECT
);
2140 if (cmd_buffer
->exec2_bos
== NULL
) {
2141 result
= vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
2142 goto fail_exec2_objects
;
2145 anv_state_stream_init(&cmd_buffer
->binding_table_state_stream
,
2146 &device
->binding_table_block_pool
);
2147 anv_state_stream_init(&cmd_buffer
->surface_state_stream
,
2148 &device
->surface_state_block_pool
);
2149 anv_state_stream_init(&cmd_buffer
->dynamic_state_stream
,
2150 &device
->dynamic_state_block_pool
);
2152 cmd_buffer
->dirty
= 0;
2153 cmd_buffer
->vb_dirty
= 0;
2155 *pCmdBuffer
= (VkCmdBuffer
) cmd_buffer
;
2160 anv_device_free(device
, cmd_buffer
->exec2_objects
);
2162 anv_gem_munmap(cmd_buffer
->surface_bo
.map
, BATCH_SIZE
);
2164 anv_gem_close(device
, cmd_buffer
->surface_bo
.gem_handle
);
2166 anv_batch_finish(&cmd_buffer
->batch
, device
);
2168 anv_device_free(device
, cmd_buffer
);
2173 VkResult
anv_BeginCommandBuffer(
2174 VkCmdBuffer cmdBuffer
,
2175 const VkCmdBufferBeginInfo
* pBeginInfo
)
2177 struct anv_cmd_buffer
*cmd_buffer
= (struct anv_cmd_buffer
*) cmdBuffer
;
2178 struct anv_device
*device
= cmd_buffer
->device
;
2180 anv_batch_emit(&cmd_buffer
->batch
, GEN8_PIPELINE_SELECT
,
2181 .PipelineSelection
= _3D
);
2182 anv_batch_emit(&cmd_buffer
->batch
, GEN8_STATE_SIP
);
2184 anv_batch_emit(&cmd_buffer
->batch
, GEN8_STATE_BASE_ADDRESS
,
2185 .GeneralStateBaseAddress
= { NULL
, 0 },
2186 .GeneralStateBaseAddressModifyEnable
= true,
2187 .GeneralStateBufferSize
= 0xfffff,
2188 .GeneralStateBufferSizeModifyEnable
= true,
2190 .SurfaceStateBaseAddress
= { &cmd_buffer
->surface_bo
, 0 },
2191 .SurfaceStateMemoryObjectControlState
= 0, /* FIXME: MOCS */
2192 .SurfaceStateBaseAddressModifyEnable
= true,
2194 .DynamicStateBaseAddress
= { &device
->dynamic_state_block_pool
.bo
, 0 },
2195 .DynamicStateBaseAddressModifyEnable
= true,
2196 .DynamicStateBufferSize
= 0xfffff,
2197 .DynamicStateBufferSizeModifyEnable
= true,
2199 .IndirectObjectBaseAddress
= { NULL
, 0 },
2200 .IndirectObjectBaseAddressModifyEnable
= true,
2201 .IndirectObjectBufferSize
= 0xfffff,
2202 .IndirectObjectBufferSizeModifyEnable
= true,
2204 .InstructionBaseAddress
= { &device
->instruction_block_pool
.bo
, 0 },
2205 .InstructionBaseAddressModifyEnable
= true,
2206 .InstructionBufferSize
= 0xfffff,
2207 .InstructionBuffersizeModifyEnable
= true);
2209 anv_batch_emit(&cmd_buffer
->batch
, GEN8_3DSTATE_VF_STATISTICS
,
2210 .StatisticsEnable
= true);
2211 anv_batch_emit(&cmd_buffer
->batch
, GEN8_3DSTATE_HS
, .Enable
= false);
2212 anv_batch_emit(&cmd_buffer
->batch
, GEN8_3DSTATE_TE
, .TEEnable
= false);
2213 anv_batch_emit(&cmd_buffer
->batch
, GEN8_3DSTATE_DS
, .FunctionEnable
= false);
2214 anv_batch_emit(&cmd_buffer
->batch
, GEN8_3DSTATE_STREAMOUT
, .SOFunctionEnable
= false);
2216 anv_batch_emit(&cmd_buffer
->batch
, GEN8_3DSTATE_PUSH_CONSTANT_ALLOC_VS
,
2217 .ConstantBufferOffset
= 0,
2218 .ConstantBufferSize
= 4);
2219 anv_batch_emit(&cmd_buffer
->batch
, GEN8_3DSTATE_PUSH_CONSTANT_ALLOC_GS
,
2220 .ConstantBufferOffset
= 4,
2221 .ConstantBufferSize
= 4);
2222 anv_batch_emit(&cmd_buffer
->batch
, GEN8_3DSTATE_PUSH_CONSTANT_ALLOC_PS
,
2223 .ConstantBufferOffset
= 8,
2224 .ConstantBufferSize
= 4);
2226 anv_batch_emit(&cmd_buffer
->batch
, GEN8_3DSTATE_WM_CHROMAKEY
,
2227 .ChromaKeyKillEnable
= false);
2228 anv_batch_emit(&cmd_buffer
->batch
, GEN8_3DSTATE_SBE_SWIZ
);
2229 anv_batch_emit(&cmd_buffer
->batch
, GEN8_3DSTATE_AA_LINE_PARAMETERS
);
2231 /* Hardcoded state: */
2232 anv_batch_emit(&cmd_buffer
->batch
, GEN8_3DSTATE_DEPTH_BUFFER
,
2233 .SurfaceType
= SURFTYPE_2D
,
2236 .SurfaceFormat
= D16_UNORM
,
2237 .SurfaceBaseAddress
= { NULL
, 0 },
2238 .HierarchicalDepthBufferEnable
= 0);
2240 anv_batch_emit(&cmd_buffer
->batch
, GEN8_3DSTATE_WM_DEPTH_STENCIL
,
2241 .DepthTestEnable
= false,
2242 .DepthBufferWriteEnable
= false);
2248 anv_cmd_buffer_add_bo(struct anv_cmd_buffer
*cmd_buffer
,
2249 struct anv_bo
*bo
, struct anv_reloc_list
*list
)
2251 struct drm_i915_gem_exec_object2
*obj
;
2253 bo
->index
= cmd_buffer
->bo_count
;
2254 obj
= &cmd_buffer
->exec2_objects
[bo
->index
];
2255 cmd_buffer
->exec2_bos
[bo
->index
] = bo
;
2256 cmd_buffer
->bo_count
++;
2258 obj
->handle
= bo
->gem_handle
;
2259 obj
->relocation_count
= 0;
2260 obj
->relocs_ptr
= 0;
2262 obj
->offset
= bo
->offset
;
2268 obj
->relocation_count
= list
->num_relocs
;
2269 obj
->relocs_ptr
= (uintptr_t) list
->relocs
;
2274 anv_cmd_buffer_add_validate_bos(struct anv_cmd_buffer
*cmd_buffer
,
2275 struct anv_reloc_list
*list
)
2277 struct anv_bo
*bo
, *batch_bo
;
2279 batch_bo
= &cmd_buffer
->batch
.bo
;
2280 for (size_t i
= 0; i
< list
->num_relocs
; i
++) {
2281 bo
= list
->reloc_bos
[i
];
2282 /* Skip any relocations targeting the batch bo. We need to make sure
2283 * it's the last in the list so we'll add it manually later.
2287 if (bo
->index
< cmd_buffer
->bo_count
&& cmd_buffer
->exec2_bos
[bo
->index
] == bo
)
2290 anv_cmd_buffer_add_bo(cmd_buffer
, bo
, NULL
);
2295 anv_cmd_buffer_process_relocs(struct anv_cmd_buffer
*cmd_buffer
,
2296 struct anv_reloc_list
*list
)
2300 /* If the kernel supports I915_EXEC_NO_RELOC, it will compare offset in
2301 * struct drm_i915_gem_exec_object2 against the bos current offset and if
2302 * all bos haven't moved it will skip relocation processing alltogether.
2303 * If I915_EXEC_NO_RELOC is not supported, the kernel ignores the incoming
2304 * value of offset so we can set it either way. For that to work we need
2305 * to make sure all relocs use the same presumed offset.
2308 for (size_t i
= 0; i
< list
->num_relocs
; i
++) {
2309 bo
= list
->reloc_bos
[i
];
2310 if (bo
->offset
!= list
->relocs
[i
].presumed_offset
)
2311 cmd_buffer
->need_reloc
= true;
2313 list
->relocs
[i
].target_handle
= bo
->index
;
2317 VkResult
anv_EndCommandBuffer(
2318 VkCmdBuffer cmdBuffer
)
2320 struct anv_cmd_buffer
*cmd_buffer
= (struct anv_cmd_buffer
*) cmdBuffer
;
2321 struct anv_device
*device
= cmd_buffer
->device
;
2322 struct anv_batch
*batch
= &cmd_buffer
->batch
;
2324 anv_batch_emit(batch
, GEN8_MI_BATCH_BUFFER_END
);
2326 /* Round batch up to an even number of dwords. */
2327 if ((batch
->next
- batch
->bo
.map
) & 4)
2328 anv_batch_emit(batch
, GEN8_MI_NOOP
);
2330 cmd_buffer
->bo_count
= 0;
2331 cmd_buffer
->need_reloc
= false;
2333 /* Lock for access to bo->index. */
2334 pthread_mutex_lock(&device
->mutex
);
2336 /* Add block pool bos first so we can add them with their relocs. */
2337 anv_cmd_buffer_add_bo(cmd_buffer
, &cmd_buffer
->surface_bo
,
2338 &cmd_buffer
->surface_relocs
);
2340 anv_cmd_buffer_add_validate_bos(cmd_buffer
, &cmd_buffer
->surface_relocs
);
2341 anv_cmd_buffer_add_validate_bos(cmd_buffer
, &batch
->cmd_relocs
);
2342 anv_cmd_buffer_add_bo(cmd_buffer
, &batch
->bo
, &batch
->cmd_relocs
);
2343 anv_cmd_buffer_process_relocs(cmd_buffer
, &cmd_buffer
->surface_relocs
);
2344 anv_cmd_buffer_process_relocs(cmd_buffer
, &batch
->cmd_relocs
);
2346 cmd_buffer
->execbuf
.buffers_ptr
= (uintptr_t) cmd_buffer
->exec2_objects
;
2347 cmd_buffer
->execbuf
.buffer_count
= cmd_buffer
->bo_count
;
2348 cmd_buffer
->execbuf
.batch_start_offset
= 0;
2349 cmd_buffer
->execbuf
.batch_len
= batch
->next
- batch
->bo
.map
;
2350 cmd_buffer
->execbuf
.cliprects_ptr
= 0;
2351 cmd_buffer
->execbuf
.num_cliprects
= 0;
2352 cmd_buffer
->execbuf
.DR1
= 0;
2353 cmd_buffer
->execbuf
.DR4
= 0;
2355 cmd_buffer
->execbuf
.flags
= I915_EXEC_HANDLE_LUT
;
2356 if (!cmd_buffer
->need_reloc
)
2357 cmd_buffer
->execbuf
.flags
|= I915_EXEC_NO_RELOC
;
2358 cmd_buffer
->execbuf
.flags
|= I915_EXEC_RENDER
;
2359 cmd_buffer
->execbuf
.rsvd1
= device
->context_id
;
2360 cmd_buffer
->execbuf
.rsvd2
= 0;
2362 pthread_mutex_unlock(&device
->mutex
);
2367 VkResult
anv_ResetCommandBuffer(
2368 VkCmdBuffer cmdBuffer
)
2370 struct anv_cmd_buffer
*cmd_buffer
= (struct anv_cmd_buffer
*) cmdBuffer
;
2372 anv_batch_reset(&cmd_buffer
->batch
);
2373 cmd_buffer
->surface_next
= 0;
2374 cmd_buffer
->surface_relocs
.num_relocs
= 0;
2379 // Command buffer building functions
2381 void anv_CmdBindPipeline(
2382 VkCmdBuffer cmdBuffer
,
2383 VkPipelineBindPoint pipelineBindPoint
,
2384 VkPipeline _pipeline
)
2386 struct anv_cmd_buffer
*cmd_buffer
= (struct anv_cmd_buffer
*) cmdBuffer
;
2387 struct anv_pipeline
*pipeline
= (struct anv_pipeline
*) _pipeline
;
2389 cmd_buffer
->pipeline
= pipeline
;
2390 cmd_buffer
->vb_dirty
|= pipeline
->vb_used
;
2391 cmd_buffer
->dirty
|= ANV_CMD_BUFFER_PIPELINE_DIRTY
;
2394 void anv_CmdBindDynamicStateObject(
2395 VkCmdBuffer cmdBuffer
,
2396 VkStateBindPoint stateBindPoint
,
2397 VkDynamicStateObject dynamicState
)
2399 struct anv_cmd_buffer
*cmd_buffer
= (struct anv_cmd_buffer
*) cmdBuffer
;
2400 struct anv_dynamic_vp_state
*vp_state
;
2402 switch (stateBindPoint
) {
2403 case VK_STATE_BIND_POINT_VIEWPORT
:
2404 vp_state
= (struct anv_dynamic_vp_state
*) dynamicState
;
2405 /* We emit state immediately, but set cmd_buffer->vp_state to indicate
2406 * that vp state has been set in this command buffer. */
2407 cmd_buffer
->vp_state
= vp_state
;
2408 anv_batch_emit(&cmd_buffer
->batch
, GEN8_3DSTATE_SCISSOR_STATE_POINTERS
,
2409 .ScissorRectPointer
= vp_state
->scissor
.offset
);
2410 anv_batch_emit(&cmd_buffer
->batch
, GEN8_3DSTATE_VIEWPORT_STATE_POINTERS_CC
,
2411 .CCViewportPointer
= vp_state
->cc_vp
.offset
);
2412 anv_batch_emit(&cmd_buffer
->batch
, GEN8_3DSTATE_VIEWPORT_STATE_POINTERS_SF_CLIP
,
2413 .SFClipViewportPointer
= vp_state
->sf_clip_vp
.offset
);
2415 case VK_STATE_BIND_POINT_RASTER
:
2416 cmd_buffer
->rs_state
= (struct anv_dynamic_rs_state
*) dynamicState
;
2417 cmd_buffer
->dirty
|= ANV_CMD_BUFFER_RS_DIRTY
;
2419 case VK_STATE_BIND_POINT_COLOR_BLEND
:
2420 case VK_STATE_BIND_POINT_DEPTH_STENCIL
:
2427 static struct anv_state
2428 anv_cmd_buffer_alloc_surface_state(struct anv_cmd_buffer
*cmd_buffer
,
2429 uint32_t size
, uint32_t alignment
)
2431 struct anv_state state
;
2433 state
.offset
= ALIGN_U32(cmd_buffer
->surface_next
, alignment
);
2434 state
.map
= cmd_buffer
->surface_bo
.map
+ state
.offset
;
2435 state
.alloc_size
= size
;
2436 cmd_buffer
->surface_next
= state
.offset
+ size
;
2438 assert(state
.offset
+ size
< cmd_buffer
->surface_bo
.size
);
2443 void anv_CmdBindDescriptorSets(
2444 VkCmdBuffer cmdBuffer
,
2445 VkPipelineBindPoint pipelineBindPoint
,
2448 const VkDescriptorSet
* pDescriptorSets
,
2449 uint32_t dynamicOffsetCount
,
2450 const uint32_t* pDynamicOffsets
)
2452 struct anv_cmd_buffer
*cmd_buffer
= (struct anv_cmd_buffer
*) cmdBuffer
;
2453 struct anv_pipeline_layout
*layout
= cmd_buffer
->pipeline
->layout
;
2454 struct anv_bindings
*bindings
= cmd_buffer
->bindings
;
2456 uint32_t offset
= 0;
2457 for (uint32_t i
= 0; i
< setCount
; i
++) {
2458 struct anv_descriptor_set
*set
=
2459 (struct anv_descriptor_set
*) pDescriptorSets
[i
];
2460 struct anv_descriptor_set_layout
*set_layout
= layout
->set
[firstSet
+ i
].layout
;
2462 for (uint32_t s
= 0; s
< VK_NUM_SHADER_STAGE
; s
++) {
2463 uint32_t *surface_to_desc
= set_layout
->stage
[s
].surface_start
;
2464 uint32_t *sampler_to_desc
= set_layout
->stage
[s
].sampler_start
;
2465 uint32_t bias
= s
== VK_SHADER_STAGE_FRAGMENT
? MAX_RTS
: 0;
2468 start
= bias
+ layout
->set
[firstSet
+ i
].surface_start
[s
];
2469 for (uint32_t b
= 0; b
< set_layout
->stage
[s
].surface_count
; b
++) {
2470 struct anv_surface_view
*view
= set
->descriptors
[surface_to_desc
[b
]].view
;
2474 struct anv_state state
=
2475 anv_cmd_buffer_alloc_surface_state(cmd_buffer
, 64, 64);
2476 memcpy(state
.map
, view
->surface_state
.map
, 64);
2478 /* The address goes in dwords 8 and 9 of the SURFACE_STATE */
2479 *(uint64_t *)(state
.map
+ 8 * 4) =
2480 anv_reloc_list_add(&cmd_buffer
->surface_relocs
,
2481 state
.offset
+ 8 * 4,
2482 view
->bo
, view
->offset
);
2484 bindings
->descriptors
[s
].surfaces
[start
+ b
] = state
.offset
;
2487 start
= layout
->set
[firstSet
+ i
].sampler_start
[s
];
2488 for (uint32_t b
= 0; b
< set_layout
->stage
[s
].sampler_count
; b
++) {
2489 struct anv_sampler
*sampler
= set
->descriptors
[sampler_to_desc
[b
]].sampler
;
2493 memcpy(&bindings
->descriptors
[s
].samplers
[start
+ b
],
2494 sampler
->state
, sizeof(sampler
->state
));
2498 offset
+= layout
->set
[firstSet
+ i
].layout
->num_dynamic_buffers
;
2501 cmd_buffer
->dirty
|= ANV_CMD_BUFFER_DESCRIPTOR_SET_DIRTY
;
2504 void anv_CmdBindIndexBuffer(
2505 VkCmdBuffer cmdBuffer
,
2507 VkDeviceSize offset
,
2508 VkIndexType indexType
)
2510 struct anv_cmd_buffer
*cmd_buffer
= (struct anv_cmd_buffer
*) cmdBuffer
;
2511 struct anv_buffer
*buffer
= (struct anv_buffer
*) _buffer
;
2513 static const uint32_t vk_to_gen_index_type
[] = {
2514 [VK_INDEX_TYPE_UINT8
] = INDEX_BYTE
,
2515 [VK_INDEX_TYPE_UINT16
] = INDEX_WORD
,
2516 [VK_INDEX_TYPE_UINT32
] = INDEX_DWORD
,
2519 anv_batch_emit(&cmd_buffer
->batch
, GEN8_3DSTATE_INDEX_BUFFER
,
2520 .IndexFormat
= vk_to_gen_index_type
[indexType
],
2521 .MemoryObjectControlState
= 0,
2522 .BufferStartingAddress
= { buffer
->bo
, buffer
->offset
+ offset
},
2523 .BufferSize
= buffer
->size
- offset
);
2526 void anv_CmdBindVertexBuffers(
2527 VkCmdBuffer cmdBuffer
,
2528 uint32_t startBinding
,
2529 uint32_t bindingCount
,
2530 const VkBuffer
* pBuffers
,
2531 const VkDeviceSize
* pOffsets
)
2533 struct anv_cmd_buffer
*cmd_buffer
= (struct anv_cmd_buffer
*) cmdBuffer
;
2534 struct anv_bindings
*bindings
= cmd_buffer
->bindings
;
2536 /* We have to defer setting up vertex buffer since we need the buffer
2537 * stride from the pipeline. */
2539 for (uint32_t i
= 0; i
< bindingCount
; i
++) {
2540 bindings
->vb
[startBinding
+ i
].buffer
= (struct anv_buffer
*) pBuffers
[i
];
2541 bindings
->vb
[startBinding
+ i
].offset
= pOffsets
[i
];
2542 cmd_buffer
->vb_dirty
|= 1 << (startBinding
+ i
);
2547 flush_descriptor_sets(struct anv_cmd_buffer
*cmd_buffer
)
2549 struct anv_pipeline_layout
*layout
= cmd_buffer
->pipeline
->layout
;
2550 struct anv_bindings
*bindings
= cmd_buffer
->bindings
;
2551 uint32_t layers
= cmd_buffer
->framebuffer
->layers
;
2553 for (uint32_t s
= 0; s
< VK_NUM_SHADER_STAGE
; s
++) {
2556 if (s
== VK_SHADER_STAGE_FRAGMENT
) {
2558 layers
= cmd_buffer
->framebuffer
->layers
;
2564 /* This is a little awkward: layout can be NULL but we still have to
2565 * allocate and set a binding table for the PS stage for render
2567 uint32_t surface_count
= layout
? layout
->stage
[s
].surface_count
: 0;
2569 if (layers
+ surface_count
> 0) {
2570 struct anv_state state
;
2573 size
= (bias
+ surface_count
) * sizeof(uint32_t);
2574 state
= anv_cmd_buffer_alloc_surface_state(cmd_buffer
, size
, 32);
2575 memcpy(state
.map
, bindings
->descriptors
[s
].surfaces
, size
);
2577 static const uint32_t binding_table_opcodes
[] = {
2578 [VK_SHADER_STAGE_VERTEX
] = 38,
2579 [VK_SHADER_STAGE_TESS_CONTROL
] = 39,
2580 [VK_SHADER_STAGE_TESS_EVALUATION
] = 40,
2581 [VK_SHADER_STAGE_GEOMETRY
] = 41,
2582 [VK_SHADER_STAGE_FRAGMENT
] = 42,
2583 [VK_SHADER_STAGE_COMPUTE
] = 0,
2586 anv_batch_emit(&cmd_buffer
->batch
,
2587 GEN8_3DSTATE_BINDING_TABLE_POINTERS_VS
,
2588 ._3DCommandSubOpcode
= binding_table_opcodes
[s
],
2589 .PointertoVSBindingTable
= state
.offset
);
2592 if (layout
&& layout
->stage
[s
].sampler_count
> 0) {
2593 struct anv_state state
;
2596 size
= layout
->stage
[s
].sampler_count
* 16;
2597 state
= anv_cmd_buffer_alloc_surface_state(cmd_buffer
, size
, 32);
2598 memcpy(state
.map
, bindings
->descriptors
[s
].samplers
, size
);
2600 static const uint32_t sampler_state_opcodes
[] = {
2601 [VK_SHADER_STAGE_VERTEX
] = 43,
2602 [VK_SHADER_STAGE_TESS_CONTROL
] = 44, /* HS */
2603 [VK_SHADER_STAGE_TESS_EVALUATION
] = 45, /* DS */
2604 [VK_SHADER_STAGE_GEOMETRY
] = 46,
2605 [VK_SHADER_STAGE_FRAGMENT
] = 47,
2606 [VK_SHADER_STAGE_COMPUTE
] = 0,
2609 anv_batch_emit(&cmd_buffer
->batch
,
2610 GEN8_3DSTATE_SAMPLER_STATE_POINTERS_VS
,
2611 ._3DCommandSubOpcode
= sampler_state_opcodes
[s
],
2612 .PointertoVSSamplerState
= state
.offset
);
2618 anv_cmd_buffer_flush_state(struct anv_cmd_buffer
*cmd_buffer
)
2620 struct anv_pipeline
*pipeline
= cmd_buffer
->pipeline
;
2621 struct anv_bindings
*bindings
= cmd_buffer
->bindings
;
2624 uint32_t vb_emit
= cmd_buffer
->vb_dirty
& pipeline
->vb_used
;
2625 const uint32_t num_buffers
= __builtin_popcount(vb_emit
);
2626 const uint32_t num_dwords
= 1 + num_buffers
* 4;
2629 p
= anv_batch_emitn(&cmd_buffer
->batch
, num_dwords
,
2630 GEN8_3DSTATE_VERTEX_BUFFERS
);
2632 for_each_bit(vb
, vb_emit
) {
2633 struct anv_buffer
*buffer
= bindings
->vb
[vb
].buffer
;
2634 uint32_t offset
= bindings
->vb
[vb
].offset
;
2636 struct GEN8_VERTEX_BUFFER_STATE state
= {
2637 .VertexBufferIndex
= vb
,
2638 .MemoryObjectControlState
= 0,
2639 .AddressModifyEnable
= true,
2640 .BufferPitch
= pipeline
->binding_stride
[vb
],
2641 .BufferStartingAddress
= { buffer
->bo
, buffer
->offset
+ offset
},
2642 .BufferSize
= buffer
->size
- offset
2645 GEN8_VERTEX_BUFFER_STATE_pack(&cmd_buffer
->batch
, &p
[1 + i
* 4], &state
);
2650 if (cmd_buffer
->dirty
& ANV_CMD_BUFFER_PIPELINE_DIRTY
)
2651 anv_batch_emit_batch(&cmd_buffer
->batch
, &pipeline
->batch
);
2653 if (cmd_buffer
->dirty
& ANV_CMD_BUFFER_DESCRIPTOR_SET_DIRTY
)
2654 flush_descriptor_sets(cmd_buffer
);
2656 if (cmd_buffer
->dirty
& (ANV_CMD_BUFFER_PIPELINE_DIRTY
| ANV_CMD_BUFFER_RS_DIRTY
))
2657 anv_batch_emit_merge(&cmd_buffer
->batch
,
2658 cmd_buffer
->rs_state
->state_sf
, pipeline
->state_sf
);
2660 cmd_buffer
->vb_dirty
&= ~vb_emit
;
2661 cmd_buffer
->dirty
= 0;
2665 VkCmdBuffer cmdBuffer
,
2666 uint32_t firstVertex
,
2667 uint32_t vertexCount
,
2668 uint32_t firstInstance
,
2669 uint32_t instanceCount
)
2671 struct anv_cmd_buffer
*cmd_buffer
= (struct anv_cmd_buffer
*) cmdBuffer
;
2673 anv_cmd_buffer_flush_state(cmd_buffer
);
2675 anv_batch_emit(&cmd_buffer
->batch
, GEN8_3DPRIMITIVE
,
2676 .VertexAccessType
= SEQUENTIAL
,
2677 .VertexCountPerInstance
= vertexCount
,
2678 .StartVertexLocation
= firstVertex
,
2679 .InstanceCount
= instanceCount
,
2680 .StartInstanceLocation
= firstInstance
,
2681 .BaseVertexLocation
= 0);
2684 void anv_CmdDrawIndexed(
2685 VkCmdBuffer cmdBuffer
,
2686 uint32_t firstIndex
,
2687 uint32_t indexCount
,
2688 int32_t vertexOffset
,
2689 uint32_t firstInstance
,
2690 uint32_t instanceCount
)
2692 struct anv_cmd_buffer
*cmd_buffer
= (struct anv_cmd_buffer
*) cmdBuffer
;
2694 anv_cmd_buffer_flush_state(cmd_buffer
);
2696 anv_batch_emit(&cmd_buffer
->batch
, GEN8_3DPRIMITIVE
,
2697 .VertexAccessType
= RANDOM
,
2698 .VertexCountPerInstance
= indexCount
,
2699 .StartVertexLocation
= firstIndex
,
2700 .InstanceCount
= instanceCount
,
2701 .StartInstanceLocation
= firstInstance
,
2702 .BaseVertexLocation
= 0);
2706 anv_batch_lrm(struct anv_batch
*batch
,
2707 uint32_t reg
, struct anv_bo
*bo
, uint32_t offset
)
2709 anv_batch_emit(batch
, GEN8_MI_LOAD_REGISTER_MEM
,
2710 .RegisterAddress
= reg
,
2711 .MemoryAddress
= { bo
, offset
});
2715 anv_batch_lri(struct anv_batch
*batch
, uint32_t reg
, uint32_t imm
)
2717 anv_batch_emit(batch
, GEN8_MI_LOAD_REGISTER_IMM
,
2718 .RegisterOffset
= reg
,
2722 /* Auto-Draw / Indirect Registers */
2723 #define GEN7_3DPRIM_END_OFFSET 0x2420
2724 #define GEN7_3DPRIM_START_VERTEX 0x2430
2725 #define GEN7_3DPRIM_VERTEX_COUNT 0x2434
2726 #define GEN7_3DPRIM_INSTANCE_COUNT 0x2438
2727 #define GEN7_3DPRIM_START_INSTANCE 0x243C
2728 #define GEN7_3DPRIM_BASE_VERTEX 0x2440
2730 void anv_CmdDrawIndirect(
2731 VkCmdBuffer cmdBuffer
,
2733 VkDeviceSize offset
,
2737 struct anv_cmd_buffer
*cmd_buffer
= (struct anv_cmd_buffer
*) cmdBuffer
;
2738 struct anv_buffer
*buffer
= (struct anv_buffer
*) _buffer
;
2739 struct anv_bo
*bo
= buffer
->bo
;
2740 uint32_t bo_offset
= buffer
->offset
+ offset
;
2742 anv_cmd_buffer_flush_state(cmd_buffer
);
2744 anv_batch_lrm(&cmd_buffer
->batch
, GEN7_3DPRIM_VERTEX_COUNT
, bo
, bo_offset
);
2745 anv_batch_lrm(&cmd_buffer
->batch
, GEN7_3DPRIM_INSTANCE_COUNT
, bo
, bo_offset
+ 4);
2746 anv_batch_lrm(&cmd_buffer
->batch
, GEN7_3DPRIM_START_VERTEX
, bo
, bo_offset
+ 8);
2747 anv_batch_lrm(&cmd_buffer
->batch
, GEN7_3DPRIM_START_INSTANCE
, bo
, bo_offset
+ 12);
2748 anv_batch_lri(&cmd_buffer
->batch
, GEN7_3DPRIM_BASE_VERTEX
, 0);
2750 anv_batch_emit(&cmd_buffer
->batch
, GEN8_3DPRIMITIVE
,
2751 .IndirectParameterEnable
= true,
2752 .VertexAccessType
= SEQUENTIAL
);
2755 void anv_CmdDrawIndexedIndirect(
2756 VkCmdBuffer cmdBuffer
,
2758 VkDeviceSize offset
,
2762 struct anv_cmd_buffer
*cmd_buffer
= (struct anv_cmd_buffer
*) cmdBuffer
;
2763 struct anv_buffer
*buffer
= (struct anv_buffer
*) _buffer
;
2764 struct anv_bo
*bo
= buffer
->bo
;
2765 uint32_t bo_offset
= buffer
->offset
+ offset
;
2767 anv_cmd_buffer_flush_state(cmd_buffer
);
2769 anv_batch_lrm(&cmd_buffer
->batch
, GEN7_3DPRIM_VERTEX_COUNT
, bo
, bo_offset
);
2770 anv_batch_lrm(&cmd_buffer
->batch
, GEN7_3DPRIM_INSTANCE_COUNT
, bo
, bo_offset
+ 4);
2771 anv_batch_lrm(&cmd_buffer
->batch
, GEN7_3DPRIM_START_VERTEX
, bo
, bo_offset
+ 8);
2772 anv_batch_lrm(&cmd_buffer
->batch
, GEN7_3DPRIM_BASE_VERTEX
, bo
, bo_offset
+ 12);
2773 anv_batch_lrm(&cmd_buffer
->batch
, GEN7_3DPRIM_START_INSTANCE
, bo
, bo_offset
+ 16);
2775 anv_batch_emit(&cmd_buffer
->batch
, GEN8_3DPRIMITIVE
,
2776 .IndirectParameterEnable
= true,
2777 .VertexAccessType
= RANDOM
);
2780 void anv_CmdDispatch(
2781 VkCmdBuffer cmdBuffer
,
2789 void anv_CmdDispatchIndirect(
2790 VkCmdBuffer cmdBuffer
,
2792 VkDeviceSize offset
)
2797 void anv_CmdSetEvent(
2798 VkCmdBuffer cmdBuffer
,
2800 VkPipeEvent pipeEvent
)
2805 void anv_CmdResetEvent(
2806 VkCmdBuffer cmdBuffer
,
2808 VkPipeEvent pipeEvent
)
2813 void anv_CmdWaitEvents(
2814 VkCmdBuffer cmdBuffer
,
2815 VkWaitEvent waitEvent
,
2816 uint32_t eventCount
,
2817 const VkEvent
* pEvents
,
2818 uint32_t memBarrierCount
,
2819 const void** ppMemBarriers
)
2824 void anv_CmdPipelineBarrier(
2825 VkCmdBuffer cmdBuffer
,
2826 VkWaitEvent waitEvent
,
2827 uint32_t pipeEventCount
,
2828 const VkPipeEvent
* pPipeEvents
,
2829 uint32_t memBarrierCount
,
2830 const void** ppMemBarriers
)
2836 anv_batch_emit_ps_depth_count(struct anv_batch
*batch
,
2837 struct anv_bo
*bo
, uint32_t offset
)
2839 anv_batch_emit(batch
, GEN8_PIPE_CONTROL
,
2840 .DestinationAddressType
= DAT_PPGTT
,
2841 .PostSyncOperation
= WritePSDepthCount
,
2842 .Address
= { bo
, offset
}); /* FIXME: This is only lower 32 bits */
2845 void anv_CmdBeginQuery(
2846 VkCmdBuffer cmdBuffer
,
2847 VkQueryPool queryPool
,
2849 VkQueryControlFlags flags
)
2851 struct anv_cmd_buffer
*cmd_buffer
= (struct anv_cmd_buffer
*) cmdBuffer
;
2852 struct anv_query_pool
*pool
= (struct anv_query_pool
*) queryPool
;
2854 switch (pool
->type
) {
2855 case VK_QUERY_TYPE_OCCLUSION
:
2856 anv_batch_emit_ps_depth_count(&cmd_buffer
->batch
, &pool
->bo
,
2857 slot
* sizeof(struct anv_query_pool_slot
));
2860 case VK_QUERY_TYPE_PIPELINE_STATISTICS
:
2866 void anv_CmdEndQuery(
2867 VkCmdBuffer cmdBuffer
,
2868 VkQueryPool queryPool
,
2871 struct anv_cmd_buffer
*cmd_buffer
= (struct anv_cmd_buffer
*) cmdBuffer
;
2872 struct anv_query_pool
*pool
= (struct anv_query_pool
*) queryPool
;
2874 switch (pool
->type
) {
2875 case VK_QUERY_TYPE_OCCLUSION
:
2876 anv_batch_emit_ps_depth_count(&cmd_buffer
->batch
, &pool
->bo
,
2877 slot
* sizeof(struct anv_query_pool_slot
) + 8);
2880 case VK_QUERY_TYPE_PIPELINE_STATISTICS
:
2886 void anv_CmdResetQueryPool(
2887 VkCmdBuffer cmdBuffer
,
2888 VkQueryPool queryPool
,
2889 uint32_t startQuery
,
2890 uint32_t queryCount
)
2895 #define TIMESTAMP 0x2358
2897 void anv_CmdWriteTimestamp(
2898 VkCmdBuffer cmdBuffer
,
2899 VkTimestampType timestampType
,
2900 VkBuffer destBuffer
,
2901 VkDeviceSize destOffset
)
2903 struct anv_cmd_buffer
*cmd_buffer
= (struct anv_cmd_buffer
*) cmdBuffer
;
2904 struct anv_buffer
*buffer
= (struct anv_buffer
*) destBuffer
;
2905 struct anv_bo
*bo
= buffer
->bo
;
2907 switch (timestampType
) {
2908 case VK_TIMESTAMP_TYPE_TOP
:
2909 anv_batch_emit(&cmd_buffer
->batch
, GEN8_MI_STORE_REGISTER_MEM
,
2910 .RegisterAddress
= TIMESTAMP
,
2911 .MemoryAddress
= { bo
, buffer
->offset
+ destOffset
});
2912 anv_batch_emit(&cmd_buffer
->batch
, GEN8_MI_STORE_REGISTER_MEM
,
2913 .RegisterAddress
= TIMESTAMP
+ 4,
2914 .MemoryAddress
= { bo
, buffer
->offset
+ destOffset
+ 4 });
2917 case VK_TIMESTAMP_TYPE_BOTTOM
:
2918 anv_batch_emit(&cmd_buffer
->batch
, GEN8_PIPE_CONTROL
,
2919 .DestinationAddressType
= DAT_PPGTT
,
2920 .PostSyncOperation
= WriteTimestamp
,
2921 .Address
= /* FIXME: This is only lower 32 bits */
2922 { bo
, buffer
->offset
+ destOffset
});
2930 #define alu_opcode(v) __gen_field((v), 20, 31)
2931 #define alu_operand1(v) __gen_field((v), 10, 19)
2932 #define alu_operand2(v) __gen_field((v), 0, 9)
2933 #define alu(opcode, operand1, operand2) \
2934 alu_opcode(opcode) | alu_operand1(operand1) | alu_operand2(operand2)
2936 #define OPCODE_NOOP 0x000
2937 #define OPCODE_LOAD 0x080
2938 #define OPCODE_LOADINV 0x480
2939 #define OPCODE_LOAD0 0x081
2940 #define OPCODE_LOAD1 0x481
2941 #define OPCODE_ADD 0x100
2942 #define OPCODE_SUB 0x101
2943 #define OPCODE_AND 0x102
2944 #define OPCODE_OR 0x103
2945 #define OPCODE_XOR 0x104
2946 #define OPCODE_STORE 0x180
2947 #define OPCODE_STOREINV 0x580
2949 #define OPERAND_R0 0x00
2950 #define OPERAND_R1 0x01
2951 #define OPERAND_R2 0x02
2952 #define OPERAND_R3 0x03
2953 #define OPERAND_R4 0x04
2954 #define OPERAND_SRCA 0x20
2955 #define OPERAND_SRCB 0x21
2956 #define OPERAND_ACCU 0x31
2957 #define OPERAND_ZF 0x32
2958 #define OPERAND_CF 0x33
2960 #define CS_GPR(n) (0x2600 + (n) * 8)
2963 emit_load_alu_reg_u64(struct anv_batch
*batch
, uint32_t reg
,
2964 struct anv_bo
*bo
, uint32_t offset
)
2966 anv_batch_emit(batch
, GEN8_MI_LOAD_REGISTER_MEM
,
2967 .RegisterAddress
= reg
,
2968 .MemoryAddress
= { bo
, offset
});
2969 anv_batch_emit(batch
, GEN8_MI_LOAD_REGISTER_MEM
,
2970 .RegisterAddress
= reg
+ 4,
2971 .MemoryAddress
= { bo
, offset
+ 4 });
2974 void anv_CmdCopyQueryPoolResults(
2975 VkCmdBuffer cmdBuffer
,
2976 VkQueryPool queryPool
,
2977 uint32_t startQuery
,
2978 uint32_t queryCount
,
2979 VkBuffer destBuffer
,
2980 VkDeviceSize destOffset
,
2981 VkDeviceSize destStride
,
2982 VkQueryResultFlags flags
)
2984 struct anv_cmd_buffer
*cmd_buffer
= (struct anv_cmd_buffer
*) cmdBuffer
;
2985 struct anv_query_pool
*pool
= (struct anv_query_pool
*) queryPool
;
2986 struct anv_buffer
*buffer
= (struct anv_buffer
*) destBuffer
;
2987 uint32_t slot_offset
, dst_offset
;
2989 if (flags
& VK_QUERY_RESULT_WITH_AVAILABILITY_BIT
) {
2990 /* Where is the availabilty info supposed to go? */
2991 anv_finishme("VK_QUERY_RESULT_WITH_AVAILABILITY_BIT");
2995 assert(pool
->type
== VK_QUERY_TYPE_OCCLUSION
);
2997 /* FIXME: If we're not waiting, should we just do this on the CPU? */
2998 if (flags
& VK_QUERY_RESULT_WAIT_BIT
)
2999 anv_batch_emit(&cmd_buffer
->batch
, GEN8_PIPE_CONTROL
,
3000 .CommandStreamerStallEnable
= true);
3002 dst_offset
= buffer
->offset
+ destOffset
;
3003 for (uint32_t i
= 0; i
< queryCount
; i
++) {
3005 slot_offset
= (startQuery
+ i
) * sizeof(struct anv_query_pool_slot
);
3007 emit_load_alu_reg_u64(&cmd_buffer
->batch
, CS_GPR(0), &pool
->bo
, slot_offset
);
3008 emit_load_alu_reg_u64(&cmd_buffer
->batch
, CS_GPR(1), &pool
->bo
, slot_offset
+ 8);
3010 /* FIXME: We need to clamp the result for 32 bit. */
3012 uint32_t *dw
= anv_batch_emitn(&cmd_buffer
->batch
, 5, GEN8_MI_MATH
);
3013 dw
[1] = alu(OPCODE_LOAD
, OPERAND_SRCA
, OPERAND_R1
);
3014 dw
[2] = alu(OPCODE_LOAD
, OPERAND_SRCB
, OPERAND_R0
);
3015 dw
[3] = alu(OPCODE_SUB
, 0, 0);
3016 dw
[4] = alu(OPCODE_STORE
, OPERAND_R2
, OPERAND_ACCU
);
3018 anv_batch_emit(&cmd_buffer
->batch
, GEN8_MI_STORE_REGISTER_MEM
,
3019 .RegisterAddress
= CS_GPR(2),
3020 /* FIXME: This is only lower 32 bits */
3021 .MemoryAddress
= { buffer
->bo
, dst_offset
});
3023 if (flags
& VK_QUERY_RESULT_64_BIT
)
3024 anv_batch_emit(&cmd_buffer
->batch
, GEN8_MI_STORE_REGISTER_MEM
,
3025 .RegisterAddress
= CS_GPR(2) + 4,
3026 /* FIXME: This is only lower 32 bits */
3027 .MemoryAddress
= { buffer
->bo
, dst_offset
+ 4 });
3029 dst_offset
+= destStride
;
3033 void anv_CmdInitAtomicCounters(
3034 VkCmdBuffer cmdBuffer
,
3035 VkPipelineBindPoint pipelineBindPoint
,
3036 uint32_t startCounter
,
3037 uint32_t counterCount
,
3038 const uint32_t* pData
)
3043 void anv_CmdLoadAtomicCounters(
3044 VkCmdBuffer cmdBuffer
,
3045 VkPipelineBindPoint pipelineBindPoint
,
3046 uint32_t startCounter
,
3047 uint32_t counterCount
,
3049 VkDeviceSize srcOffset
)
3054 void anv_CmdSaveAtomicCounters(
3055 VkCmdBuffer cmdBuffer
,
3056 VkPipelineBindPoint pipelineBindPoint
,
3057 uint32_t startCounter
,
3058 uint32_t counterCount
,
3059 VkBuffer destBuffer
,
3060 VkDeviceSize destOffset
)
3065 VkResult
anv_CreateFramebuffer(
3067 const VkFramebufferCreateInfo
* pCreateInfo
,
3068 VkFramebuffer
* pFramebuffer
)
3070 struct anv_device
*device
= (struct anv_device
*) _device
;
3071 struct anv_framebuffer
*framebuffer
;
3073 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO
);
3075 framebuffer
= anv_device_alloc(device
, sizeof(*framebuffer
), 8,
3076 VK_SYSTEM_ALLOC_TYPE_API_OBJECT
);
3077 if (framebuffer
== NULL
)
3078 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
3080 framebuffer
->color_attachment_count
= pCreateInfo
->colorAttachmentCount
;
3081 for (uint32_t i
= 0; i
< pCreateInfo
->colorAttachmentCount
; i
++) {
3082 framebuffer
->color_attachments
[i
] =
3083 (struct anv_surface_view
*) pCreateInfo
->pColorAttachments
[i
].view
;
3086 if (pCreateInfo
->pDepthStencilAttachment
) {
3087 framebuffer
->depth_stencil
=
3088 (struct anv_depth_stencil_view
*) pCreateInfo
->pDepthStencilAttachment
->view
;
3091 framebuffer
->sample_count
= pCreateInfo
->sampleCount
;
3092 framebuffer
->width
= pCreateInfo
->width
;
3093 framebuffer
->height
= pCreateInfo
->height
;
3094 framebuffer
->layers
= pCreateInfo
->layers
;
3096 vkCreateDynamicViewportState((VkDevice
) device
,
3097 &(VkDynamicVpStateCreateInfo
) {
3098 .sType
= VK_STRUCTURE_TYPE_DYNAMIC_VP_STATE_CREATE_INFO
,
3099 .viewportAndScissorCount
= 2,
3100 .pViewports
= (VkViewport
[]) {
3104 .width
= pCreateInfo
->width
,
3105 .height
= pCreateInfo
->height
,
3110 .pScissors
= (VkRect
[]) {
3112 { pCreateInfo
->width
, pCreateInfo
->height
} },
3115 &framebuffer
->vp_state
);
3117 *pFramebuffer
= (VkFramebuffer
) framebuffer
;
3122 VkResult
anv_CreateRenderPass(
3124 const VkRenderPassCreateInfo
* pCreateInfo
,
3125 VkRenderPass
* pRenderPass
)
3127 struct anv_device
*device
= (struct anv_device
*) _device
;
3128 struct anv_render_pass
*pass
;
3131 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO
);
3133 size
= sizeof(*pass
) +
3134 pCreateInfo
->layers
* sizeof(struct anv_render_pass_layer
);
3135 pass
= anv_device_alloc(device
, size
, 8,
3136 VK_SYSTEM_ALLOC_TYPE_API_OBJECT
);
3138 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
3140 pass
->render_area
= pCreateInfo
->renderArea
;
3142 pass
->num_layers
= pCreateInfo
->layers
;
3144 pass
->num_clear_layers
= 0;
3145 for (uint32_t i
= 0; i
< pCreateInfo
->layers
; i
++) {
3146 pass
->layers
[i
].color_load_op
= pCreateInfo
->pColorLoadOps
[i
];
3147 pass
->layers
[i
].clear_color
= pCreateInfo
->pColorLoadClearValues
[i
];
3148 if (pass
->layers
[i
].color_load_op
== VK_ATTACHMENT_LOAD_OP_CLEAR
)
3149 pass
->num_clear_layers
++;
3152 *pRenderPass
= (VkRenderPass
) pass
;
3158 anv_cmd_buffer_fill_render_targets(struct anv_cmd_buffer
*cmd_buffer
)
3160 struct anv_framebuffer
*framebuffer
= cmd_buffer
->framebuffer
;
3161 struct anv_bindings
*bindings
= cmd_buffer
->bindings
;
3163 for (uint32_t i
= 0; i
< framebuffer
->color_attachment_count
; i
++) {
3164 struct anv_surface_view
*view
= framebuffer
->color_attachments
[i
];
3166 struct anv_state state
=
3167 anv_cmd_buffer_alloc_surface_state(cmd_buffer
, 64, 64);
3168 memcpy(state
.map
, view
->surface_state
.map
, 64);
3170 /* The address goes in dwords 8 and 9 of the SURFACE_STATE */
3171 *(uint64_t *)(state
.map
+ 8 * 4) =
3172 anv_reloc_list_add(&cmd_buffer
->surface_relocs
,
3173 state
.offset
+ 8 * 4,
3174 view
->bo
, view
->offset
);
3176 bindings
->descriptors
[VK_SHADER_STAGE_FRAGMENT
].surfaces
[i
] = state
.offset
;
3178 cmd_buffer
->dirty
|= ANV_CMD_BUFFER_DESCRIPTOR_SET_DIRTY
;
3181 void anv_CmdBeginRenderPass(
3182 VkCmdBuffer cmdBuffer
,
3183 const VkRenderPassBegin
* pRenderPassBegin
)
3185 struct anv_cmd_buffer
*cmd_buffer
= (struct anv_cmd_buffer
*) cmdBuffer
;
3186 struct anv_render_pass
*pass
= (struct anv_render_pass
*) pRenderPassBegin
->renderPass
;
3187 struct anv_framebuffer
*framebuffer
=
3188 (struct anv_framebuffer
*) pRenderPassBegin
->framebuffer
;
3190 cmd_buffer
->framebuffer
= framebuffer
;
3192 anv_batch_emit(&cmd_buffer
->batch
, GEN8_3DSTATE_DRAWING_RECTANGLE
,
3193 .ClippedDrawingRectangleYMin
= pass
->render_area
.offset
.y
,
3194 .ClippedDrawingRectangleXMin
= pass
->render_area
.offset
.x
,
3195 .ClippedDrawingRectangleYMax
=
3196 pass
->render_area
.offset
.y
+ pass
->render_area
.extent
.height
- 1,
3197 .ClippedDrawingRectangleXMax
=
3198 pass
->render_area
.offset
.x
+ pass
->render_area
.extent
.width
- 1,
3199 .DrawingRectangleOriginY
= 0,
3200 .DrawingRectangleOriginX
= 0);
3202 anv_cmd_buffer_fill_render_targets(cmd_buffer
);
3204 anv_cmd_buffer_clear(cmd_buffer
, pass
);
3207 void anv_CmdEndRenderPass(
3208 VkCmdBuffer cmdBuffer
,
3209 VkRenderPass renderPass
)
3211 /* Emit a flushing pipe control at the end of a pass. This is kind of a
3212 * hack but it ensures that render targets always actually get written.
3213 * Eventually, we should do flushing based on image format transitions
3214 * or something of that nature.
3216 struct anv_cmd_buffer
*cmd_buffer
= (struct anv_cmd_buffer
*)cmdBuffer
;
3217 anv_batch_emit(&cmd_buffer
->batch
, GEN8_PIPE_CONTROL
,
3218 .PostSyncOperation
= NoWrite
,
3219 .RenderTargetCacheFlushEnable
= true,
3220 .InstructionCacheInvalidateEnable
= true,
3221 .DepthCacheFlushEnable
= true,
3222 .VFCacheInvalidationEnable
= true,
3223 .TextureCacheInvalidationEnable
= true,
3224 .CommandStreamerStallEnable
= true);
3227 void vkCmdDbgMarkerBegin(
3228 VkCmdBuffer cmdBuffer
,
3229 const char* pMarker
)
3230 __attribute__ ((visibility ("default")));
3232 void vkCmdDbgMarkerEnd(
3233 VkCmdBuffer cmdBuffer
)
3234 __attribute__ ((visibility ("default")));
3236 VkResult
vkDbgSetObjectTag(
3241 __attribute__ ((visibility ("default")));
3244 void vkCmdDbgMarkerBegin(
3245 VkCmdBuffer cmdBuffer
,
3246 const char* pMarker
)
3250 void vkCmdDbgMarkerEnd(
3251 VkCmdBuffer cmdBuffer
)
3255 VkResult
vkDbgSetObjectTag(