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
;
933 VkResult
anv_DestroyObject(
935 VkObjectType objType
,
938 struct anv_device
*device
= (struct anv_device
*) _device
;
939 struct anv_object
*object
= (struct anv_object
*) _object
;
942 case VK_OBJECT_TYPE_INSTANCE
:
943 return anv_DestroyInstance((VkInstance
) _object
);
945 case VK_OBJECT_TYPE_PHYSICAL_DEVICE
:
946 /* We don't want to actually destroy physical devices */
949 case VK_OBJECT_TYPE_DEVICE
:
950 assert(_device
== (VkDevice
) _object
);
951 return anv_DestroyDevice((VkDevice
) _object
);
953 case VK_OBJECT_TYPE_QUEUE
:
957 case VK_OBJECT_TYPE_DEVICE_MEMORY
:
958 return anv_FreeMemory(_device
, (VkDeviceMemory
) _object
);
960 case VK_OBJECT_TYPE_DESCRIPTOR_POOL
:
961 /* These are just dummys anyway, so we don't need to destroy them */
964 case VK_OBJECT_TYPE_BUFFER
:
965 case VK_OBJECT_TYPE_BUFFER_VIEW
:
966 case VK_OBJECT_TYPE_IMAGE
:
967 case VK_OBJECT_TYPE_IMAGE_VIEW
:
968 case VK_OBJECT_TYPE_COLOR_ATTACHMENT_VIEW
:
969 case VK_OBJECT_TYPE_DEPTH_STENCIL_VIEW
:
970 case VK_OBJECT_TYPE_SHADER
:
971 case VK_OBJECT_TYPE_PIPELINE_LAYOUT
:
972 case VK_OBJECT_TYPE_SAMPLER
:
973 case VK_OBJECT_TYPE_DESCRIPTOR_SET
:
974 case VK_OBJECT_TYPE_DESCRIPTOR_SET_LAYOUT
:
975 case VK_OBJECT_TYPE_DYNAMIC_RS_STATE
:
976 case VK_OBJECT_TYPE_DYNAMIC_CB_STATE
:
977 case VK_OBJECT_TYPE_DYNAMIC_DS_STATE
:
978 case VK_OBJECT_TYPE_RENDER_PASS
:
979 /* These are trivially destroyable */
980 anv_device_free(device
, (void *) _object
);
983 case VK_OBJECT_TYPE_COMMAND_BUFFER
:
984 case VK_OBJECT_TYPE_PIPELINE
:
985 case VK_OBJECT_TYPE_DYNAMIC_VP_STATE
:
986 case VK_OBJECT_TYPE_FENCE
:
987 case VK_OBJECT_TYPE_QUERY_POOL
:
988 case VK_OBJECT_TYPE_FRAMEBUFFER
:
989 (object
->destructor
)(device
, object
, objType
);
992 case VK_OBJECT_TYPE_SEMAPHORE
:
993 case VK_OBJECT_TYPE_EVENT
:
994 stub_return(VK_UNSUPPORTED
);
997 unreachable("Invalid object type");
1002 fill_memory_requirements(
1003 VkObjectType objType
,
1005 VkMemoryRequirements
* memory_requirements
)
1007 struct anv_buffer
*buffer
;
1008 struct anv_image
*image
;
1010 memory_requirements
->memPropsAllowed
=
1011 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT
|
1012 VK_MEMORY_PROPERTY_HOST_DEVICE_COHERENT_BIT
|
1013 /* VK_MEMORY_PROPERTY_HOST_UNCACHED_BIT | */
1014 VK_MEMORY_PROPERTY_HOST_WRITE_COMBINED_BIT
|
1015 VK_MEMORY_PROPERTY_PREFER_HOST_LOCAL
|
1016 VK_MEMORY_PROPERTY_SHAREABLE_BIT
;
1018 memory_requirements
->memPropsRequired
= 0;
1021 case VK_OBJECT_TYPE_BUFFER
:
1022 buffer
= (struct anv_buffer
*) object
;
1023 memory_requirements
->size
= buffer
->size
;
1024 memory_requirements
->alignment
= 16;
1026 case VK_OBJECT_TYPE_IMAGE
:
1027 image
= (struct anv_image
*) object
;
1028 memory_requirements
->size
= image
->size
;
1029 memory_requirements
->alignment
= image
->alignment
;
1032 memory_requirements
->size
= 0;
1038 get_allocation_count(VkObjectType objType
)
1041 case VK_OBJECT_TYPE_BUFFER
:
1042 case VK_OBJECT_TYPE_IMAGE
:
1049 VkResult
anv_GetObjectInfo(
1051 VkObjectType objType
,
1053 VkObjectInfoType infoType
,
1057 VkMemoryRequirements memory_requirements
;
1061 case VK_OBJECT_INFO_TYPE_MEMORY_REQUIREMENTS
:
1062 *pDataSize
= sizeof(memory_requirements
);
1066 fill_memory_requirements(objType
, object
, pData
);
1069 case VK_OBJECT_INFO_TYPE_MEMORY_ALLOCATION_COUNT
:
1070 *pDataSize
= sizeof(count
);
1075 *count
= get_allocation_count(objType
);
1079 return VK_UNSUPPORTED
;
1084 VkResult
anv_QueueBindObjectMemory(
1086 VkObjectType objType
,
1088 uint32_t allocationIdx
,
1089 VkDeviceMemory _mem
,
1090 VkDeviceSize memOffset
)
1092 struct anv_buffer
*buffer
;
1093 struct anv_image
*image
;
1094 struct anv_device_memory
*mem
= (struct anv_device_memory
*) _mem
;
1097 case VK_OBJECT_TYPE_BUFFER
:
1098 buffer
= (struct anv_buffer
*) object
;
1099 buffer
->bo
= &mem
->bo
;
1100 buffer
->offset
= memOffset
;
1102 case VK_OBJECT_TYPE_IMAGE
:
1103 image
= (struct anv_image
*) object
;
1104 image
->bo
= &mem
->bo
;
1105 image
->offset
= memOffset
;
1114 VkResult
anv_QueueBindObjectMemoryRange(
1116 VkObjectType objType
,
1118 uint32_t allocationIdx
,
1119 VkDeviceSize rangeOffset
,
1120 VkDeviceSize rangeSize
,
1122 VkDeviceSize memOffset
)
1124 stub_return(VK_UNSUPPORTED
);
1127 VkResult
anv_QueueBindImageMemoryRange(
1130 uint32_t allocationIdx
,
1131 const VkImageMemoryBindInfo
* pBindInfo
,
1133 VkDeviceSize memOffset
)
1135 stub_return(VK_UNSUPPORTED
);
1139 anv_fence_destroy(struct anv_device
*device
,
1140 struct anv_object
*object
,
1141 VkObjectType obj_type
)
1143 struct anv_fence
*fence
= (struct anv_fence
*) object
;
1145 assert(obj_type
== VK_OBJECT_TYPE_FENCE
);
1147 anv_gem_munmap(fence
->bo
.map
, fence
->bo
.size
);
1148 anv_gem_close(device
, fence
->bo
.gem_handle
);
1149 anv_device_free(device
, fence
);
1152 VkResult
anv_CreateFence(
1154 const VkFenceCreateInfo
* pCreateInfo
,
1157 struct anv_device
*device
= (struct anv_device
*) _device
;
1158 struct anv_fence
*fence
;
1159 struct anv_batch batch
;
1162 const uint32_t fence_size
= 128;
1164 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_FENCE_CREATE_INFO
);
1166 fence
= anv_device_alloc(device
, sizeof(*fence
), 8,
1167 VK_SYSTEM_ALLOC_TYPE_API_OBJECT
);
1169 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
1171 result
= anv_bo_init_new(&fence
->bo
, device
, fence_size
);
1172 if (result
!= VK_SUCCESS
)
1175 fence
->base
.destructor
= anv_fence_destroy
;
1178 anv_gem_mmap(device
, fence
->bo
.gem_handle
, 0, fence
->bo
.size
);
1179 batch
.next
= fence
->bo
.map
;
1180 anv_batch_emit(&batch
, GEN8_MI_BATCH_BUFFER_END
);
1181 anv_batch_emit(&batch
, GEN8_MI_NOOP
);
1183 fence
->exec2_objects
[0].handle
= fence
->bo
.gem_handle
;
1184 fence
->exec2_objects
[0].relocation_count
= 0;
1185 fence
->exec2_objects
[0].relocs_ptr
= 0;
1186 fence
->exec2_objects
[0].alignment
= 0;
1187 fence
->exec2_objects
[0].offset
= fence
->bo
.offset
;
1188 fence
->exec2_objects
[0].flags
= 0;
1189 fence
->exec2_objects
[0].rsvd1
= 0;
1190 fence
->exec2_objects
[0].rsvd2
= 0;
1192 fence
->execbuf
.buffers_ptr
= (uintptr_t) fence
->exec2_objects
;
1193 fence
->execbuf
.buffer_count
= 1;
1194 fence
->execbuf
.batch_start_offset
= 0;
1195 fence
->execbuf
.batch_len
= batch
.next
- fence
->bo
.map
;
1196 fence
->execbuf
.cliprects_ptr
= 0;
1197 fence
->execbuf
.num_cliprects
= 0;
1198 fence
->execbuf
.DR1
= 0;
1199 fence
->execbuf
.DR4
= 0;
1201 fence
->execbuf
.flags
=
1202 I915_EXEC_HANDLE_LUT
| I915_EXEC_NO_RELOC
| I915_EXEC_RENDER
;
1203 fence
->execbuf
.rsvd1
= device
->context_id
;
1204 fence
->execbuf
.rsvd2
= 0;
1206 *pFence
= (VkQueryPool
) fence
;
1211 anv_device_free(device
, fence
);
1216 VkResult
anv_ResetFences(
1218 uint32_t fenceCount
,
1221 struct anv_fence
**fences
= (struct anv_fence
**) pFences
;
1223 for (uint32_t i
; i
< fenceCount
; i
++)
1224 fences
[i
]->ready
= false;
1229 VkResult
anv_GetFenceStatus(
1233 struct anv_device
*device
= (struct anv_device
*) _device
;
1234 struct anv_fence
*fence
= (struct anv_fence
*) _fence
;
1241 ret
= anv_gem_wait(device
, fence
->bo
.gem_handle
, &t
);
1243 fence
->ready
= true;
1247 return VK_NOT_READY
;
1250 VkResult
anv_WaitForFences(
1252 uint32_t fenceCount
,
1253 const VkFence
* pFences
,
1257 struct anv_device
*device
= (struct anv_device
*) _device
;
1258 struct anv_fence
**fences
= (struct anv_fence
**) pFences
;
1259 int64_t t
= timeout
;
1262 /* FIXME: handle !waitAll */
1264 for (uint32_t i
= 0; i
< fenceCount
; i
++) {
1265 ret
= anv_gem_wait(device
, fences
[i
]->bo
.gem_handle
, &t
);
1266 if (ret
== -1 && errno
== ETIME
)
1269 return vk_error(VK_ERROR_UNKNOWN
);
1275 // Queue semaphore functions
1277 VkResult
anv_CreateSemaphore(
1279 const VkSemaphoreCreateInfo
* pCreateInfo
,
1280 VkSemaphore
* pSemaphore
)
1282 stub_return(VK_UNSUPPORTED
);
1285 VkResult
anv_QueueSignalSemaphore(
1287 VkSemaphore semaphore
)
1289 stub_return(VK_UNSUPPORTED
);
1292 VkResult
anv_QueueWaitSemaphore(
1294 VkSemaphore semaphore
)
1296 stub_return(VK_UNSUPPORTED
);
1301 VkResult
anv_CreateEvent(
1303 const VkEventCreateInfo
* pCreateInfo
,
1306 stub_return(VK_UNSUPPORTED
);
1309 VkResult
anv_GetEventStatus(
1313 stub_return(VK_UNSUPPORTED
);
1316 VkResult
anv_SetEvent(
1320 stub_return(VK_UNSUPPORTED
);
1323 VkResult
anv_ResetEvent(
1327 stub_return(VK_UNSUPPORTED
);
1333 anv_query_pool_destroy(struct anv_device
*device
,
1334 struct anv_object
*object
,
1335 VkObjectType obj_type
)
1337 struct anv_query_pool
*pool
= (struct anv_query_pool
*) object
;
1339 assert(obj_type
== VK_OBJECT_TYPE_QUERY_POOL
);
1341 anv_gem_munmap(pool
->bo
.map
, pool
->bo
.size
);
1342 anv_gem_close(device
, pool
->bo
.gem_handle
);
1343 anv_device_free(device
, pool
);
1346 VkResult
anv_CreateQueryPool(
1348 const VkQueryPoolCreateInfo
* pCreateInfo
,
1349 VkQueryPool
* pQueryPool
)
1351 struct anv_device
*device
= (struct anv_device
*) _device
;
1352 struct anv_query_pool
*pool
;
1356 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_QUERY_POOL_CREATE_INFO
);
1358 switch (pCreateInfo
->queryType
) {
1359 case VK_QUERY_TYPE_OCCLUSION
:
1361 case VK_QUERY_TYPE_PIPELINE_STATISTICS
:
1362 return VK_UNSUPPORTED
;
1367 pool
= anv_device_alloc(device
, sizeof(*pool
), 8,
1368 VK_SYSTEM_ALLOC_TYPE_API_OBJECT
);
1370 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
1372 pool
->base
.destructor
= anv_query_pool_destroy
;
1374 pool
->type
= pCreateInfo
->queryType
;
1375 size
= pCreateInfo
->slots
* sizeof(struct anv_query_pool_slot
);
1376 result
= anv_bo_init_new(&pool
->bo
, device
, size
);
1377 if (result
!= VK_SUCCESS
)
1380 pool
->bo
.map
= anv_gem_mmap(device
, pool
->bo
.gem_handle
, 0, size
);
1382 *pQueryPool
= (VkQueryPool
) pool
;
1387 anv_device_free(device
, pool
);
1392 VkResult
anv_GetQueryPoolResults(
1394 VkQueryPool queryPool
,
1395 uint32_t startQuery
,
1396 uint32_t queryCount
,
1399 VkQueryResultFlags flags
)
1401 struct anv_device
*device
= (struct anv_device
*) _device
;
1402 struct anv_query_pool
*pool
= (struct anv_query_pool
*) queryPool
;
1403 struct anv_query_pool_slot
*slot
= pool
->bo
.map
;
1404 int64_t timeout
= INT64_MAX
;
1405 uint32_t *dst32
= pData
;
1406 uint64_t *dst64
= pData
;
1410 if (flags
& VK_QUERY_RESULT_WITH_AVAILABILITY_BIT
) {
1411 /* Where is the availabilty info supposed to go? */
1412 anv_finishme("VK_QUERY_RESULT_WITH_AVAILABILITY_BIT");
1413 return VK_UNSUPPORTED
;
1416 assert(pool
->type
== VK_QUERY_TYPE_OCCLUSION
);
1418 if (flags
& VK_QUERY_RESULT_64_BIT
)
1419 *pDataSize
= queryCount
* sizeof(uint64_t);
1421 *pDataSize
= queryCount
* sizeof(uint32_t);
1426 if (flags
& VK_QUERY_RESULT_WAIT_BIT
) {
1427 ret
= anv_gem_wait(device
, pool
->bo
.gem_handle
, &timeout
);
1429 return vk_error(VK_ERROR_UNKNOWN
);
1432 for (uint32_t i
= 0; i
< queryCount
; i
++) {
1433 result
= slot
[startQuery
+ i
].end
- slot
[startQuery
+ i
].begin
;
1434 if (flags
& VK_QUERY_RESULT_64_BIT
) {
1437 if (result
> UINT32_MAX
)
1438 result
= UINT32_MAX
;
1448 VkResult
anv_CreateBuffer(
1450 const VkBufferCreateInfo
* pCreateInfo
,
1453 struct anv_device
*device
= (struct anv_device
*) _device
;
1454 struct anv_buffer
*buffer
;
1456 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO
);
1458 buffer
= anv_device_alloc(device
, sizeof(*buffer
), 8,
1459 VK_SYSTEM_ALLOC_TYPE_API_OBJECT
);
1461 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
1463 buffer
->size
= pCreateInfo
->size
;
1467 *pBuffer
= (VkBuffer
) buffer
;
1472 // Buffer view functions
1474 VkResult
anv_CreateBufferView(
1476 const VkBufferViewCreateInfo
* pCreateInfo
,
1477 VkBufferView
* pView
)
1479 struct anv_device
*device
= (struct anv_device
*) _device
;
1480 struct anv_buffer
*buffer
= (struct anv_buffer
*) pCreateInfo
->buffer
;
1481 struct anv_surface_view
*view
;
1482 const struct anv_format
*format
;
1484 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO
);
1486 view
= anv_device_alloc(device
, sizeof(*view
), 8,
1487 VK_SYSTEM_ALLOC_TYPE_API_OBJECT
);
1489 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
1491 view
->bo
= buffer
->bo
;
1492 view
->offset
= buffer
->offset
+ pCreateInfo
->offset
;
1493 view
->surface_state
=
1494 anv_state_pool_alloc(&device
->surface_state_pool
, 64, 64);
1495 view
->format
= pCreateInfo
->format
;
1497 format
= anv_format_for_vk_format(pCreateInfo
->format
);
1498 /* This assumes RGBA float format. */
1499 uint32_t stride
= 4;
1500 uint32_t num_elements
= pCreateInfo
->range
/ stride
;
1501 struct GEN8_RENDER_SURFACE_STATE surface_state
= {
1502 .SurfaceType
= SURFTYPE_BUFFER
,
1503 .SurfaceArray
= false,
1504 .SurfaceFormat
= format
->format
,
1505 .SurfaceVerticalAlignment
= VALIGN4
,
1506 .SurfaceHorizontalAlignment
= HALIGN4
,
1508 .VerticalLineStride
= 0,
1509 .VerticalLineStrideOffset
= 0,
1510 .SamplerL2BypassModeDisable
= true,
1511 .RenderCacheReadWriteMode
= WriteOnlyCache
,
1512 .MemoryObjectControlState
= GEN8_MOCS
,
1515 .Height
= (num_elements
>> 7) & 0x3fff,
1516 .Width
= num_elements
& 0x7f,
1517 .Depth
= (num_elements
>> 21) & 0x3f,
1518 .SurfacePitch
= stride
- 1,
1519 .MinimumArrayElement
= 0,
1520 .NumberofMultisamples
= MULTISAMPLECOUNT_1
,
1525 .AuxiliarySurfaceMode
= AUX_NONE
,
1527 .GreenClearColor
= 0,
1528 .BlueClearColor
= 0,
1529 .AlphaClearColor
= 0,
1530 .ShaderChannelSelectRed
= SCS_RED
,
1531 .ShaderChannelSelectGreen
= SCS_GREEN
,
1532 .ShaderChannelSelectBlue
= SCS_BLUE
,
1533 .ShaderChannelSelectAlpha
= SCS_ALPHA
,
1534 .ResourceMinLOD
= 0,
1535 /* FIXME: We assume that the image must be bound at this time. */
1536 .SurfaceBaseAddress
= { NULL
, view
->offset
},
1539 GEN8_RENDER_SURFACE_STATE_pack(NULL
, view
->surface_state
.map
, &surface_state
);
1541 *pView
= (VkImageView
) view
;
1546 // Sampler functions
1548 VkResult
anv_CreateSampler(
1550 const VkSamplerCreateInfo
* pCreateInfo
,
1551 VkSampler
* pSampler
)
1553 struct anv_device
*device
= (struct anv_device
*) _device
;
1554 struct anv_sampler
*sampler
;
1556 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO
);
1558 sampler
= anv_device_alloc(device
, sizeof(*sampler
), 8,
1559 VK_SYSTEM_ALLOC_TYPE_API_OBJECT
);
1561 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
1563 static const uint32_t vk_to_gen_tex_filter
[] = {
1564 [VK_TEX_FILTER_NEAREST
] = MAPFILTER_NEAREST
,
1565 [VK_TEX_FILTER_LINEAR
] = MAPFILTER_LINEAR
1568 static const uint32_t vk_to_gen_mipmap_mode
[] = {
1569 [VK_TEX_MIPMAP_MODE_BASE
] = MIPFILTER_NONE
,
1570 [VK_TEX_MIPMAP_MODE_NEAREST
] = MIPFILTER_NEAREST
,
1571 [VK_TEX_MIPMAP_MODE_LINEAR
] = MIPFILTER_LINEAR
1574 static const uint32_t vk_to_gen_tex_address
[] = {
1575 [VK_TEX_ADDRESS_WRAP
] = TCM_WRAP
,
1576 [VK_TEX_ADDRESS_MIRROR
] = TCM_MIRROR
,
1577 [VK_TEX_ADDRESS_CLAMP
] = TCM_CLAMP
,
1578 [VK_TEX_ADDRESS_MIRROR_ONCE
] = TCM_MIRROR_ONCE
,
1579 [VK_TEX_ADDRESS_CLAMP_BORDER
] = TCM_CLAMP_BORDER
,
1582 static const uint32_t vk_to_gen_compare_op
[] = {
1583 [VK_COMPARE_OP_NEVER
] = PREFILTEROPNEVER
,
1584 [VK_COMPARE_OP_LESS
] = PREFILTEROPLESS
,
1585 [VK_COMPARE_OP_EQUAL
] = PREFILTEROPEQUAL
,
1586 [VK_COMPARE_OP_LESS_EQUAL
] = PREFILTEROPLEQUAL
,
1587 [VK_COMPARE_OP_GREATER
] = PREFILTEROPGREATER
,
1588 [VK_COMPARE_OP_NOT_EQUAL
] = PREFILTEROPNOTEQUAL
,
1589 [VK_COMPARE_OP_GREATER_EQUAL
] = PREFILTEROPGEQUAL
,
1590 [VK_COMPARE_OP_ALWAYS
] = PREFILTEROPALWAYS
,
1593 if (pCreateInfo
->maxAnisotropy
> 0)
1594 anv_finishme("missing support for anisotropic filtering");
1596 struct GEN8_SAMPLER_STATE sampler_state
= {
1597 .SamplerDisable
= false,
1598 .TextureBorderColorMode
= DX10OGL
,
1599 .LODPreClampMode
= 0,
1601 .MipModeFilter
= vk_to_gen_mipmap_mode
[pCreateInfo
->mipMode
],
1602 .MagModeFilter
= vk_to_gen_tex_filter
[pCreateInfo
->magFilter
],
1603 .MinModeFilter
= vk_to_gen_tex_filter
[pCreateInfo
->minFilter
],
1604 .TextureLODBias
= pCreateInfo
->mipLodBias
* 256,
1605 .AnisotropicAlgorithm
= EWAApproximation
,
1606 .MinLOD
= pCreateInfo
->minLod
* 256,
1607 .MaxLOD
= pCreateInfo
->maxLod
* 256,
1608 .ChromaKeyEnable
= 0,
1609 .ChromaKeyIndex
= 0,
1611 .ShadowFunction
= vk_to_gen_compare_op
[pCreateInfo
->compareOp
],
1612 .CubeSurfaceControlMode
= 0,
1613 .IndirectStatePointer
= 0,
1614 .LODClampMagnificationMode
= MIPNONE
,
1615 .MaximumAnisotropy
= 0,
1616 .RAddressMinFilterRoundingEnable
= 0,
1617 .RAddressMagFilterRoundingEnable
= 0,
1618 .VAddressMinFilterRoundingEnable
= 0,
1619 .VAddressMagFilterRoundingEnable
= 0,
1620 .UAddressMinFilterRoundingEnable
= 0,
1621 .UAddressMagFilterRoundingEnable
= 0,
1622 .TrilinearFilterQuality
= 0,
1623 .NonnormalizedCoordinateEnable
= 0,
1624 .TCXAddressControlMode
= vk_to_gen_tex_address
[pCreateInfo
->addressU
],
1625 .TCYAddressControlMode
= vk_to_gen_tex_address
[pCreateInfo
->addressV
],
1626 .TCZAddressControlMode
= vk_to_gen_tex_address
[pCreateInfo
->addressW
],
1629 GEN8_SAMPLER_STATE_pack(NULL
, sampler
->state
, &sampler_state
);
1631 *pSampler
= (VkSampler
) sampler
;
1636 // Descriptor set functions
1638 VkResult
anv_CreateDescriptorSetLayout(
1640 const VkDescriptorSetLayoutCreateInfo
* pCreateInfo
,
1641 VkDescriptorSetLayout
* pSetLayout
)
1643 struct anv_device
*device
= (struct anv_device
*) _device
;
1644 struct anv_descriptor_set_layout
*set_layout
;
1646 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO
);
1648 uint32_t sampler_count
[VK_NUM_SHADER_STAGE
] = { 0, };
1649 uint32_t surface_count
[VK_NUM_SHADER_STAGE
] = { 0, };
1650 uint32_t num_dynamic_buffers
= 0;
1654 for (uint32_t i
= 0; i
< pCreateInfo
->count
; i
++) {
1655 switch (pCreateInfo
->pBinding
[i
].descriptorType
) {
1656 case VK_DESCRIPTOR_TYPE_SAMPLER
:
1657 for_each_bit(s
, pCreateInfo
->pBinding
[i
].stageFlags
)
1658 sampler_count
[s
] += pCreateInfo
->pBinding
[i
].count
;
1661 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
:
1662 for_each_bit(s
, pCreateInfo
->pBinding
[i
].stageFlags
)
1663 sampler_count
[s
] += pCreateInfo
->pBinding
[i
].count
;
1667 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE
:
1668 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE
:
1669 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER
:
1670 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER
:
1671 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER
:
1672 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER
:
1673 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
:
1674 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
:
1675 for_each_bit(s
, pCreateInfo
->pBinding
[i
].stageFlags
)
1676 surface_count
[s
] += pCreateInfo
->pBinding
[i
].count
;
1682 count
+= pCreateInfo
->pBinding
[i
].count
;
1685 for (uint32_t i
= 0; i
< pCreateInfo
->count
; i
++) {
1686 switch (pCreateInfo
->pBinding
[i
].descriptorType
) {
1687 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
:
1688 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
:
1689 num_dynamic_buffers
++;
1696 uint32_t sampler_total
= 0;
1697 uint32_t surface_total
= 0;
1698 for (uint32_t s
= 0; s
< VK_NUM_SHADER_STAGE
; s
++) {
1699 sampler_total
+= sampler_count
[s
];
1700 surface_total
+= surface_count
[s
];
1703 size_t size
= sizeof(*set_layout
) +
1704 (sampler_total
+ surface_total
) * sizeof(uint32_t);
1705 set_layout
= anv_device_alloc(device
, size
, 8,
1706 VK_SYSTEM_ALLOC_TYPE_API_OBJECT
);
1708 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
1710 set_layout
->num_dynamic_buffers
= num_dynamic_buffers
;
1711 set_layout
->count
= count
;
1713 uint32_t *p
= set_layout
->entries
;
1714 uint32_t *sampler
[VK_NUM_SHADER_STAGE
];
1715 uint32_t *surface
[VK_NUM_SHADER_STAGE
];
1716 for (uint32_t s
= 0; s
< VK_NUM_SHADER_STAGE
; s
++) {
1717 set_layout
->stage
[s
].surface_count
= surface_count
[s
];
1718 set_layout
->stage
[s
].surface_start
= surface
[s
] = p
;
1719 p
+= surface_count
[s
];
1720 set_layout
->stage
[s
].sampler_count
= sampler_count
[s
];
1721 set_layout
->stage
[s
].sampler_start
= sampler
[s
] = p
;
1722 p
+= sampler_count
[s
];
1725 uint32_t descriptor
= 0;
1726 for (uint32_t i
= 0; i
< pCreateInfo
->count
; i
++) {
1727 switch (pCreateInfo
->pBinding
[i
].descriptorType
) {
1728 case VK_DESCRIPTOR_TYPE_SAMPLER
:
1729 for_each_bit(s
, pCreateInfo
->pBinding
[i
].stageFlags
)
1730 for (uint32_t j
= 0; j
< pCreateInfo
->pBinding
[i
].count
; j
++)
1731 *(sampler
[s
])++ = descriptor
+ j
;
1734 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
:
1735 for_each_bit(s
, pCreateInfo
->pBinding
[i
].stageFlags
)
1736 for (uint32_t j
= 0; j
< pCreateInfo
->pBinding
[i
].count
; j
++)
1737 *(sampler
[s
])++ = descriptor
+ j
;
1741 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE
:
1742 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE
:
1743 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER
:
1744 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER
:
1745 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER
:
1746 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER
:
1747 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
:
1748 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
:
1749 for_each_bit(s
, pCreateInfo
->pBinding
[i
].stageFlags
)
1750 for (uint32_t j
= 0; j
< pCreateInfo
->pBinding
[i
].count
; j
++) {
1751 *(surface
[s
])++ = descriptor
+ j
;
1757 descriptor
+= pCreateInfo
->pBinding
[i
].count
;
1760 *pSetLayout
= (VkDescriptorSetLayout
) set_layout
;
1765 VkResult
anv_BeginDescriptorPoolUpdate(
1767 VkDescriptorUpdateMode updateMode
)
1772 VkResult
anv_EndDescriptorPoolUpdate(
1779 VkResult
anv_CreateDescriptorPool(
1781 VkDescriptorPoolUsage poolUsage
,
1783 const VkDescriptorPoolCreateInfo
* pCreateInfo
,
1784 VkDescriptorPool
* pDescriptorPool
)
1786 *pDescriptorPool
= 1;
1791 VkResult
anv_ResetDescriptorPool(
1793 VkDescriptorPool descriptorPool
)
1798 VkResult
anv_AllocDescriptorSets(
1800 VkDescriptorPool descriptorPool
,
1801 VkDescriptorSetUsage setUsage
,
1803 const VkDescriptorSetLayout
* pSetLayouts
,
1804 VkDescriptorSet
* pDescriptorSets
,
1807 struct anv_device
*device
= (struct anv_device
*) _device
;
1808 const struct anv_descriptor_set_layout
*layout
;
1809 struct anv_descriptor_set
*set
;
1812 for (uint32_t i
= 0; i
< count
; i
++) {
1813 layout
= (struct anv_descriptor_set_layout
*) pSetLayouts
[i
];
1814 size
= sizeof(*set
) + layout
->count
* sizeof(set
->descriptors
[0]);
1815 set
= anv_device_alloc(device
, size
, 8,
1816 VK_SYSTEM_ALLOC_TYPE_API_OBJECT
);
1819 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
1822 /* Descriptor sets may not be 100% filled out so we need to memset to
1823 * ensure that we can properly detect and handle holes.
1825 memset(set
, 0, size
);
1827 pDescriptorSets
[i
] = (VkDescriptorSet
) set
;
1835 void anv_ClearDescriptorSets(
1837 VkDescriptorPool descriptorPool
,
1839 const VkDescriptorSet
* pDescriptorSets
)
1843 void anv_UpdateDescriptors(
1845 VkDescriptorSet descriptorSet
,
1846 uint32_t updateCount
,
1847 const void** ppUpdateArray
)
1849 struct anv_descriptor_set
*set
= (struct anv_descriptor_set
*) descriptorSet
;
1850 VkUpdateSamplers
*update_samplers
;
1851 VkUpdateSamplerTextures
*update_sampler_textures
;
1852 VkUpdateImages
*update_images
;
1853 VkUpdateBuffers
*update_buffers
;
1854 VkUpdateAsCopy
*update_as_copy
;
1856 for (uint32_t i
= 0; i
< updateCount
; i
++) {
1857 const struct anv_common
*common
= ppUpdateArray
[i
];
1859 switch (common
->sType
) {
1860 case VK_STRUCTURE_TYPE_UPDATE_SAMPLERS
:
1861 update_samplers
= (VkUpdateSamplers
*) common
;
1863 for (uint32_t j
= 0; j
< update_samplers
->count
; j
++) {
1864 set
->descriptors
[update_samplers
->binding
+ j
].sampler
=
1865 (struct anv_sampler
*) update_samplers
->pSamplers
[j
];
1869 case VK_STRUCTURE_TYPE_UPDATE_SAMPLER_TEXTURES
:
1870 /* FIXME: Shouldn't this be *_UPDATE_SAMPLER_IMAGES? */
1871 update_sampler_textures
= (VkUpdateSamplerTextures
*) common
;
1873 for (uint32_t j
= 0; j
< update_sampler_textures
->count
; j
++) {
1874 set
->descriptors
[update_sampler_textures
->binding
+ j
].view
=
1875 (struct anv_surface_view
*)
1876 update_sampler_textures
->pSamplerImageViews
[j
].pImageView
->view
;
1877 set
->descriptors
[update_sampler_textures
->binding
+ j
].sampler
=
1878 (struct anv_sampler
*)
1879 update_sampler_textures
->pSamplerImageViews
[j
].sampler
;
1883 case VK_STRUCTURE_TYPE_UPDATE_IMAGES
:
1884 update_images
= (VkUpdateImages
*) common
;
1886 for (uint32_t j
= 0; j
< update_images
->count
; j
++) {
1887 set
->descriptors
[update_images
->binding
+ j
].view
=
1888 (struct anv_surface_view
*) update_images
->pImageViews
[j
].view
;
1892 case VK_STRUCTURE_TYPE_UPDATE_BUFFERS
:
1893 update_buffers
= (VkUpdateBuffers
*) common
;
1895 for (uint32_t j
= 0; j
< update_buffers
->count
; j
++) {
1896 set
->descriptors
[update_buffers
->binding
+ j
].view
=
1897 (struct anv_surface_view
*) update_buffers
->pBufferViews
[j
].view
;
1899 /* FIXME: descriptor arrays? */
1902 case VK_STRUCTURE_TYPE_UPDATE_AS_COPY
:
1903 update_as_copy
= (VkUpdateAsCopy
*) common
;
1904 (void) update_as_copy
;
1913 // State object functions
1915 static inline int64_t
1916 clamp_int64(int64_t x
, int64_t min
, int64_t max
)
1927 anv_dynamic_vp_state_destroy(struct anv_device
*device
,
1928 struct anv_object
*object
,
1929 VkObjectType obj_type
)
1931 struct anv_dynamic_vp_state
*state
= (void *)object
;
1933 assert(obj_type
== VK_OBJECT_TYPE_DYNAMIC_VP_STATE
);
1935 anv_state_pool_free(&device
->dynamic_state_pool
, state
->sf_clip_vp
);
1936 anv_state_pool_free(&device
->dynamic_state_pool
, state
->cc_vp
);
1937 anv_state_pool_free(&device
->dynamic_state_pool
, state
->scissor
);
1939 anv_device_free(device
, state
);
1942 VkResult
anv_CreateDynamicViewportState(
1944 const VkDynamicVpStateCreateInfo
* pCreateInfo
,
1945 VkDynamicVpState
* pState
)
1947 struct anv_device
*device
= (struct anv_device
*) _device
;
1948 struct anv_dynamic_vp_state
*state
;
1950 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_DYNAMIC_VP_STATE_CREATE_INFO
);
1952 state
= anv_device_alloc(device
, sizeof(*state
), 8,
1953 VK_SYSTEM_ALLOC_TYPE_API_OBJECT
);
1955 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
1957 state
->base
.destructor
= anv_dynamic_vp_state_destroy
;
1959 unsigned count
= pCreateInfo
->viewportAndScissorCount
;
1960 state
->sf_clip_vp
= anv_state_pool_alloc(&device
->dynamic_state_pool
,
1962 state
->cc_vp
= anv_state_pool_alloc(&device
->dynamic_state_pool
,
1964 state
->scissor
= anv_state_pool_alloc(&device
->dynamic_state_pool
,
1967 for (uint32_t i
= 0; i
< pCreateInfo
->viewportAndScissorCount
; i
++) {
1968 const VkViewport
*vp
= &pCreateInfo
->pViewports
[i
];
1969 const VkRect
*s
= &pCreateInfo
->pScissors
[i
];
1971 struct GEN8_SF_CLIP_VIEWPORT sf_clip_viewport
= {
1972 .ViewportMatrixElementm00
= vp
->width
/ 2,
1973 .ViewportMatrixElementm11
= vp
->height
/ 2,
1974 .ViewportMatrixElementm22
= (vp
->maxDepth
- vp
->minDepth
) / 2,
1975 .ViewportMatrixElementm30
= vp
->originX
+ vp
->width
/ 2,
1976 .ViewportMatrixElementm31
= vp
->originY
+ vp
->height
/ 2,
1977 .ViewportMatrixElementm32
= (vp
->maxDepth
+ vp
->minDepth
) / 2,
1978 .XMinClipGuardband
= -1.0f
,
1979 .XMaxClipGuardband
= 1.0f
,
1980 .YMinClipGuardband
= -1.0f
,
1981 .YMaxClipGuardband
= 1.0f
,
1982 .XMinViewPort
= vp
->originX
,
1983 .XMaxViewPort
= vp
->originX
+ vp
->width
- 1,
1984 .YMinViewPort
= vp
->originY
,
1985 .YMaxViewPort
= vp
->originY
+ vp
->height
- 1,
1988 struct GEN8_CC_VIEWPORT cc_viewport
= {
1989 .MinimumDepth
= vp
->minDepth
,
1990 .MaximumDepth
= vp
->maxDepth
1993 /* Since xmax and ymax are inclusive, we have to have xmax < xmin or
1994 * ymax < ymin for empty clips. In case clip x, y, width height are all
1995 * 0, the clamps below produce 0 for xmin, ymin, xmax, ymax, which isn't
1996 * what we want. Just special case empty clips and produce a canonical
1998 static const struct GEN8_SCISSOR_RECT empty_scissor
= {
1999 .ScissorRectangleYMin
= 1,
2000 .ScissorRectangleXMin
= 1,
2001 .ScissorRectangleYMax
= 0,
2002 .ScissorRectangleXMax
= 0
2005 const int max
= 0xffff;
2006 struct GEN8_SCISSOR_RECT scissor
= {
2007 /* Do this math using int64_t so overflow gets clamped correctly. */
2008 .ScissorRectangleYMin
= clamp_int64(s
->offset
.y
, 0, max
),
2009 .ScissorRectangleXMin
= clamp_int64(s
->offset
.x
, 0, max
),
2010 .ScissorRectangleYMax
= clamp_int64((uint64_t) s
->offset
.y
+ s
->extent
.height
- 1, 0, max
),
2011 .ScissorRectangleXMax
= clamp_int64((uint64_t) s
->offset
.x
+ s
->extent
.width
- 1, 0, max
)
2014 GEN8_SF_CLIP_VIEWPORT_pack(NULL
, state
->sf_clip_vp
.map
+ i
* 64, &sf_clip_viewport
);
2015 GEN8_CC_VIEWPORT_pack(NULL
, state
->cc_vp
.map
+ i
* 32, &cc_viewport
);
2017 if (s
->extent
.width
<= 0 || s
->extent
.height
<= 0) {
2018 GEN8_SCISSOR_RECT_pack(NULL
, state
->scissor
.map
+ i
* 32, &empty_scissor
);
2020 GEN8_SCISSOR_RECT_pack(NULL
, state
->scissor
.map
+ i
* 32, &scissor
);
2024 *pState
= (VkDynamicVpState
) state
;
2029 VkResult
anv_CreateDynamicRasterState(
2031 const VkDynamicRsStateCreateInfo
* pCreateInfo
,
2032 VkDynamicRsState
* pState
)
2034 struct anv_device
*device
= (struct anv_device
*) _device
;
2035 struct anv_dynamic_rs_state
*state
;
2037 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_DYNAMIC_RS_STATE_CREATE_INFO
);
2039 state
= anv_device_alloc(device
, sizeof(*state
), 8,
2040 VK_SYSTEM_ALLOC_TYPE_API_OBJECT
);
2042 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
2045 * float pointFadeThreshold;
2046 * // optional (GL45) - Size of point fade threshold
2049 struct GEN8_3DSTATE_SF sf
= {
2050 GEN8_3DSTATE_SF_header
,
2051 .LineWidth
= pCreateInfo
->lineWidth
,
2052 .PointWidth
= pCreateInfo
->pointSize
,
2055 GEN8_3DSTATE_SF_pack(NULL
, state
->state_sf
, &sf
);
2057 bool enable_bias
= pCreateInfo
->depthBias
!= 0.0f
||
2058 pCreateInfo
->slopeScaledDepthBias
!= 0.0f
;
2059 struct GEN8_3DSTATE_RASTER raster
= {
2060 .GlobalDepthOffsetEnableSolid
= enable_bias
,
2061 .GlobalDepthOffsetEnableWireframe
= enable_bias
,
2062 .GlobalDepthOffsetEnablePoint
= enable_bias
,
2063 .GlobalDepthOffsetConstant
= pCreateInfo
->depthBias
,
2064 .GlobalDepthOffsetScale
= pCreateInfo
->slopeScaledDepthBias
,
2065 .GlobalDepthOffsetClamp
= pCreateInfo
->depthBiasClamp
2068 GEN8_3DSTATE_RASTER_pack(NULL
, state
->state_raster
, &raster
);
2070 *pState
= (VkDynamicRsState
) state
;
2075 VkResult
anv_CreateDynamicColorBlendState(
2077 const VkDynamicCbStateCreateInfo
* pCreateInfo
,
2078 VkDynamicCbState
* pState
)
2080 struct anv_device
*device
= (struct anv_device
*) _device
;
2081 struct anv_dynamic_cb_state
*state
;
2083 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_DYNAMIC_CB_STATE_CREATE_INFO
);
2085 state
= anv_device_alloc(device
, sizeof(*state
), 8,
2086 VK_SYSTEM_ALLOC_TYPE_API_OBJECT
);
2088 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
2090 struct GEN8_COLOR_CALC_STATE color_calc_state
= {
2091 .BlendConstantColorRed
= pCreateInfo
->blendConst
[0],
2092 .BlendConstantColorGreen
= pCreateInfo
->blendConst
[1],
2093 .BlendConstantColorBlue
= pCreateInfo
->blendConst
[2],
2094 .BlendConstantColorAlpha
= pCreateInfo
->blendConst
[3]
2097 GEN8_COLOR_CALC_STATE_pack(NULL
, state
->state_color_calc
, &color_calc_state
);
2099 *pState
= (VkDynamicCbState
) state
;
2104 VkResult
anv_CreateDynamicDepthStencilState(
2106 const VkDynamicDsStateCreateInfo
* pCreateInfo
,
2107 VkDynamicDsState
* pState
)
2109 struct anv_device
*device
= (struct anv_device
*) _device
;
2110 struct anv_dynamic_ds_state
*state
;
2112 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_DYNAMIC_DS_STATE_CREATE_INFO
);
2114 state
= anv_device_alloc(device
, sizeof(*state
), 8,
2115 VK_SYSTEM_ALLOC_TYPE_API_OBJECT
);
2117 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
2119 struct GEN8_3DSTATE_WM_DEPTH_STENCIL wm_depth_stencil
= {
2120 GEN8_3DSTATE_WM_DEPTH_STENCIL_header
,
2122 /* Is this what we need to do? */
2123 .StencilBufferWriteEnable
= pCreateInfo
->stencilWriteMask
!= 0,
2125 .StencilTestMask
= pCreateInfo
->stencilReadMask
,
2126 .StencilWriteMask
= pCreateInfo
->stencilWriteMask
,
2128 .BackfaceStencilTestMask
= pCreateInfo
->stencilReadMask
,
2129 .BackfaceStencilWriteMask
= pCreateInfo
->stencilWriteMask
,
2132 GEN8_3DSTATE_WM_DEPTH_STENCIL_pack(NULL
, state
->state_wm_depth_stencil
,
2135 struct GEN8_COLOR_CALC_STATE color_calc_state
= {
2136 .StencilReferenceValue
= pCreateInfo
->stencilFrontRef
,
2137 .BackFaceStencilReferenceValue
= pCreateInfo
->stencilBackRef
2140 GEN8_COLOR_CALC_STATE_pack(NULL
, state
->state_color_calc
, &color_calc_state
);
2142 *pState
= (VkDynamicDsState
) state
;
2147 // Command buffer functions
2150 anv_cmd_buffer_destroy(struct anv_device
*device
,
2151 struct anv_object
*object
,
2152 VkObjectType obj_type
)
2154 struct anv_cmd_buffer
*cmd_buffer
= (struct anv_cmd_buffer
*) object
;
2156 assert(obj_type
== VK_OBJECT_TYPE_COMMAND_BUFFER
);
2158 anv_gem_munmap(cmd_buffer
->surface_bo
.map
, BATCH_SIZE
);
2159 anv_gem_close(device
, cmd_buffer
->surface_bo
.gem_handle
);
2160 anv_state_stream_finish(&cmd_buffer
->surface_state_stream
);
2161 anv_state_stream_finish(&cmd_buffer
->dynamic_state_stream
);
2162 anv_state_stream_finish(&cmd_buffer
->binding_table_state_stream
);
2163 anv_batch_finish(&cmd_buffer
->batch
, device
);
2164 anv_device_free(device
, cmd_buffer
->exec2_objects
);
2165 anv_device_free(device
, cmd_buffer
->exec2_bos
);
2166 anv_device_free(device
, cmd_buffer
);
2169 VkResult
anv_CreateCommandBuffer(
2171 const VkCmdBufferCreateInfo
* pCreateInfo
,
2172 VkCmdBuffer
* pCmdBuffer
)
2174 struct anv_device
*device
= (struct anv_device
*) _device
;
2175 struct anv_cmd_buffer
*cmd_buffer
;
2178 cmd_buffer
= anv_device_alloc(device
, sizeof(*cmd_buffer
), 8,
2179 VK_SYSTEM_ALLOC_TYPE_API_OBJECT
);
2180 if (cmd_buffer
== NULL
)
2181 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
2183 cmd_buffer
->base
.destructor
= anv_cmd_buffer_destroy
;
2185 cmd_buffer
->device
= device
;
2186 cmd_buffer
->rs_state
= NULL
;
2187 cmd_buffer
->vp_state
= NULL
;
2188 memset(&cmd_buffer
->default_bindings
, 0, sizeof(cmd_buffer
->default_bindings
));
2189 cmd_buffer
->bindings
= &cmd_buffer
->default_bindings
;
2191 result
= anv_batch_init(&cmd_buffer
->batch
, device
);
2192 if (result
!= VK_SUCCESS
)
2195 result
= anv_bo_init_new(&cmd_buffer
->surface_bo
, device
, BATCH_SIZE
);
2196 if (result
!= VK_SUCCESS
)
2199 cmd_buffer
->surface_bo
.map
=
2200 anv_gem_mmap(device
, cmd_buffer
->surface_bo
.gem_handle
, 0, BATCH_SIZE
);
2201 if (cmd_buffer
->surface_bo
.map
== NULL
) {
2202 result
= vk_error(VK_ERROR_MEMORY_MAP_FAILED
);
2203 goto fail_surface_bo
;
2206 /* Start surface_next at 1 so surface offset 0 is invalid. */
2207 cmd_buffer
->surface_next
= 1;
2208 cmd_buffer
->surface_relocs
.num_relocs
= 0;
2210 cmd_buffer
->exec2_objects
=
2211 anv_device_alloc(device
, 8192 * sizeof(cmd_buffer
->exec2_objects
[0]), 8,
2212 VK_SYSTEM_ALLOC_TYPE_API_OBJECT
);
2213 if (cmd_buffer
->exec2_objects
== NULL
) {
2214 result
= vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
2215 goto fail_surface_map
;
2218 cmd_buffer
->exec2_bos
=
2219 anv_device_alloc(device
, 8192 * sizeof(cmd_buffer
->exec2_bos
[0]), 8,
2220 VK_SYSTEM_ALLOC_TYPE_API_OBJECT
);
2221 if (cmd_buffer
->exec2_bos
== NULL
) {
2222 result
= vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
2223 goto fail_exec2_objects
;
2226 anv_state_stream_init(&cmd_buffer
->binding_table_state_stream
,
2227 &device
->binding_table_block_pool
);
2228 anv_state_stream_init(&cmd_buffer
->surface_state_stream
,
2229 &device
->surface_state_block_pool
);
2230 anv_state_stream_init(&cmd_buffer
->dynamic_state_stream
,
2231 &device
->dynamic_state_block_pool
);
2233 cmd_buffer
->dirty
= 0;
2234 cmd_buffer
->vb_dirty
= 0;
2235 cmd_buffer
->pipeline
= NULL
;
2237 *pCmdBuffer
= (VkCmdBuffer
) cmd_buffer
;
2242 anv_device_free(device
, cmd_buffer
->exec2_objects
);
2244 anv_gem_munmap(cmd_buffer
->surface_bo
.map
, BATCH_SIZE
);
2246 anv_gem_close(device
, cmd_buffer
->surface_bo
.gem_handle
);
2248 anv_batch_finish(&cmd_buffer
->batch
, device
);
2250 anv_device_free(device
, cmd_buffer
);
2255 VkResult
anv_BeginCommandBuffer(
2256 VkCmdBuffer cmdBuffer
,
2257 const VkCmdBufferBeginInfo
* pBeginInfo
)
2259 struct anv_cmd_buffer
*cmd_buffer
= (struct anv_cmd_buffer
*) cmdBuffer
;
2260 struct anv_device
*device
= cmd_buffer
->device
;
2262 anv_batch_emit(&cmd_buffer
->batch
, GEN8_PIPELINE_SELECT
,
2263 .PipelineSelection
= _3D
);
2264 anv_batch_emit(&cmd_buffer
->batch
, GEN8_STATE_SIP
);
2266 anv_batch_emit(&cmd_buffer
->batch
, GEN8_STATE_BASE_ADDRESS
,
2267 .GeneralStateBaseAddress
= { NULL
, 0 },
2268 .GeneralStateMemoryObjectControlState
= GEN8_MOCS
,
2269 .GeneralStateBaseAddressModifyEnable
= true,
2270 .GeneralStateBufferSize
= 0xfffff,
2271 .GeneralStateBufferSizeModifyEnable
= true,
2273 .SurfaceStateBaseAddress
= { &cmd_buffer
->surface_bo
, 0 },
2274 .SurfaceStateMemoryObjectControlState
= GEN8_MOCS
,
2275 .SurfaceStateBaseAddressModifyEnable
= true,
2277 .DynamicStateBaseAddress
= { &device
->dynamic_state_block_pool
.bo
, 0 },
2278 .DynamicStateMemoryObjectControlState
= GEN8_MOCS
,
2279 .DynamicStateBaseAddressModifyEnable
= true,
2280 .DynamicStateBufferSize
= 0xfffff,
2281 .DynamicStateBufferSizeModifyEnable
= true,
2283 .IndirectObjectBaseAddress
= { NULL
, 0 },
2284 .IndirectObjectMemoryObjectControlState
= GEN8_MOCS
,
2285 .IndirectObjectBaseAddressModifyEnable
= true,
2286 .IndirectObjectBufferSize
= 0xfffff,
2287 .IndirectObjectBufferSizeModifyEnable
= true,
2289 .InstructionBaseAddress
= { &device
->instruction_block_pool
.bo
, 0 },
2290 .InstructionMemoryObjectControlState
= GEN8_MOCS
,
2291 .InstructionBaseAddressModifyEnable
= true,
2292 .InstructionBufferSize
= 0xfffff,
2293 .InstructionBuffersizeModifyEnable
= true);
2295 anv_batch_emit(&cmd_buffer
->batch
, GEN8_3DSTATE_VF_STATISTICS
,
2296 .StatisticsEnable
= true);
2297 anv_batch_emit(&cmd_buffer
->batch
, GEN8_3DSTATE_HS
, .Enable
= false);
2298 anv_batch_emit(&cmd_buffer
->batch
, GEN8_3DSTATE_TE
, .TEEnable
= false);
2299 anv_batch_emit(&cmd_buffer
->batch
, GEN8_3DSTATE_DS
, .FunctionEnable
= false);
2300 anv_batch_emit(&cmd_buffer
->batch
, GEN8_3DSTATE_STREAMOUT
, .SOFunctionEnable
= false);
2302 anv_batch_emit(&cmd_buffer
->batch
, GEN8_3DSTATE_PUSH_CONSTANT_ALLOC_VS
,
2303 .ConstantBufferOffset
= 0,
2304 .ConstantBufferSize
= 4);
2305 anv_batch_emit(&cmd_buffer
->batch
, GEN8_3DSTATE_PUSH_CONSTANT_ALLOC_GS
,
2306 .ConstantBufferOffset
= 4,
2307 .ConstantBufferSize
= 4);
2308 anv_batch_emit(&cmd_buffer
->batch
, GEN8_3DSTATE_PUSH_CONSTANT_ALLOC_PS
,
2309 .ConstantBufferOffset
= 8,
2310 .ConstantBufferSize
= 4);
2312 anv_batch_emit(&cmd_buffer
->batch
, GEN8_3DSTATE_WM_CHROMAKEY
,
2313 .ChromaKeyKillEnable
= false);
2314 anv_batch_emit(&cmd_buffer
->batch
, GEN8_3DSTATE_SBE_SWIZ
);
2315 anv_batch_emit(&cmd_buffer
->batch
, GEN8_3DSTATE_AA_LINE_PARAMETERS
);
2321 anv_cmd_buffer_add_bo(struct anv_cmd_buffer
*cmd_buffer
,
2322 struct anv_bo
*bo
, struct anv_reloc_list
*list
)
2324 struct drm_i915_gem_exec_object2
*obj
;
2326 bo
->index
= cmd_buffer
->bo_count
;
2327 obj
= &cmd_buffer
->exec2_objects
[bo
->index
];
2328 cmd_buffer
->exec2_bos
[bo
->index
] = bo
;
2329 cmd_buffer
->bo_count
++;
2331 obj
->handle
= bo
->gem_handle
;
2332 obj
->relocation_count
= 0;
2333 obj
->relocs_ptr
= 0;
2335 obj
->offset
= bo
->offset
;
2341 obj
->relocation_count
= list
->num_relocs
;
2342 obj
->relocs_ptr
= (uintptr_t) list
->relocs
;
2347 anv_cmd_buffer_add_validate_bos(struct anv_cmd_buffer
*cmd_buffer
,
2348 struct anv_reloc_list
*list
)
2350 struct anv_bo
*bo
, *batch_bo
;
2352 batch_bo
= &cmd_buffer
->batch
.bo
;
2353 for (size_t i
= 0; i
< list
->num_relocs
; i
++) {
2354 bo
= list
->reloc_bos
[i
];
2355 /* Skip any relocations targeting the batch bo. We need to make sure
2356 * it's the last in the list so we'll add it manually later.
2360 if (bo
->index
< cmd_buffer
->bo_count
&& cmd_buffer
->exec2_bos
[bo
->index
] == bo
)
2363 anv_cmd_buffer_add_bo(cmd_buffer
, bo
, NULL
);
2368 anv_cmd_buffer_process_relocs(struct anv_cmd_buffer
*cmd_buffer
,
2369 struct anv_reloc_list
*list
)
2373 /* If the kernel supports I915_EXEC_NO_RELOC, it will compare offset in
2374 * struct drm_i915_gem_exec_object2 against the bos current offset and if
2375 * all bos haven't moved it will skip relocation processing alltogether.
2376 * If I915_EXEC_NO_RELOC is not supported, the kernel ignores the incoming
2377 * value of offset so we can set it either way. For that to work we need
2378 * to make sure all relocs use the same presumed offset.
2381 for (size_t i
= 0; i
< list
->num_relocs
; i
++) {
2382 bo
= list
->reloc_bos
[i
];
2383 if (bo
->offset
!= list
->relocs
[i
].presumed_offset
)
2384 cmd_buffer
->need_reloc
= true;
2386 list
->relocs
[i
].target_handle
= bo
->index
;
2390 VkResult
anv_EndCommandBuffer(
2391 VkCmdBuffer cmdBuffer
)
2393 struct anv_cmd_buffer
*cmd_buffer
= (struct anv_cmd_buffer
*) cmdBuffer
;
2394 struct anv_device
*device
= cmd_buffer
->device
;
2395 struct anv_batch
*batch
= &cmd_buffer
->batch
;
2397 anv_batch_emit(batch
, GEN8_MI_BATCH_BUFFER_END
);
2399 /* Round batch up to an even number of dwords. */
2400 if ((batch
->next
- batch
->bo
.map
) & 4)
2401 anv_batch_emit(batch
, GEN8_MI_NOOP
);
2403 cmd_buffer
->bo_count
= 0;
2404 cmd_buffer
->need_reloc
= false;
2406 /* Lock for access to bo->index. */
2407 pthread_mutex_lock(&device
->mutex
);
2409 /* Add block pool bos first so we can add them with their relocs. */
2410 anv_cmd_buffer_add_bo(cmd_buffer
, &cmd_buffer
->surface_bo
,
2411 &cmd_buffer
->surface_relocs
);
2413 anv_cmd_buffer_add_validate_bos(cmd_buffer
, &cmd_buffer
->surface_relocs
);
2414 anv_cmd_buffer_add_validate_bos(cmd_buffer
, &batch
->cmd_relocs
);
2415 anv_cmd_buffer_add_bo(cmd_buffer
, &batch
->bo
, &batch
->cmd_relocs
);
2416 anv_cmd_buffer_process_relocs(cmd_buffer
, &cmd_buffer
->surface_relocs
);
2417 anv_cmd_buffer_process_relocs(cmd_buffer
, &batch
->cmd_relocs
);
2419 cmd_buffer
->execbuf
.buffers_ptr
= (uintptr_t) cmd_buffer
->exec2_objects
;
2420 cmd_buffer
->execbuf
.buffer_count
= cmd_buffer
->bo_count
;
2421 cmd_buffer
->execbuf
.batch_start_offset
= 0;
2422 cmd_buffer
->execbuf
.batch_len
= batch
->next
- batch
->bo
.map
;
2423 cmd_buffer
->execbuf
.cliprects_ptr
= 0;
2424 cmd_buffer
->execbuf
.num_cliprects
= 0;
2425 cmd_buffer
->execbuf
.DR1
= 0;
2426 cmd_buffer
->execbuf
.DR4
= 0;
2428 cmd_buffer
->execbuf
.flags
= I915_EXEC_HANDLE_LUT
;
2429 if (!cmd_buffer
->need_reloc
)
2430 cmd_buffer
->execbuf
.flags
|= I915_EXEC_NO_RELOC
;
2431 cmd_buffer
->execbuf
.flags
|= I915_EXEC_RENDER
;
2432 cmd_buffer
->execbuf
.rsvd1
= device
->context_id
;
2433 cmd_buffer
->execbuf
.rsvd2
= 0;
2435 pthread_mutex_unlock(&device
->mutex
);
2440 VkResult
anv_ResetCommandBuffer(
2441 VkCmdBuffer cmdBuffer
)
2443 struct anv_cmd_buffer
*cmd_buffer
= (struct anv_cmd_buffer
*) cmdBuffer
;
2445 anv_batch_reset(&cmd_buffer
->batch
);
2446 cmd_buffer
->surface_next
= 0;
2447 cmd_buffer
->surface_relocs
.num_relocs
= 0;
2452 // Command buffer building functions
2454 void anv_CmdBindPipeline(
2455 VkCmdBuffer cmdBuffer
,
2456 VkPipelineBindPoint pipelineBindPoint
,
2457 VkPipeline _pipeline
)
2459 struct anv_cmd_buffer
*cmd_buffer
= (struct anv_cmd_buffer
*) cmdBuffer
;
2460 struct anv_pipeline
*pipeline
= (struct anv_pipeline
*) _pipeline
;
2462 cmd_buffer
->pipeline
= pipeline
;
2463 cmd_buffer
->vb_dirty
|= pipeline
->vb_used
;
2464 cmd_buffer
->dirty
|= ANV_CMD_BUFFER_PIPELINE_DIRTY
;
2467 void anv_CmdBindDynamicStateObject(
2468 VkCmdBuffer cmdBuffer
,
2469 VkStateBindPoint stateBindPoint
,
2470 VkDynamicStateObject dynamicState
)
2472 struct anv_cmd_buffer
*cmd_buffer
= (struct anv_cmd_buffer
*) cmdBuffer
;
2473 struct anv_dynamic_vp_state
*vp_state
;
2475 switch (stateBindPoint
) {
2476 case VK_STATE_BIND_POINT_VIEWPORT
:
2477 vp_state
= (struct anv_dynamic_vp_state
*) dynamicState
;
2478 /* We emit state immediately, but set cmd_buffer->vp_state to indicate
2479 * that vp state has been set in this command buffer. */
2480 cmd_buffer
->vp_state
= vp_state
;
2481 anv_batch_emit(&cmd_buffer
->batch
, GEN8_3DSTATE_SCISSOR_STATE_POINTERS
,
2482 .ScissorRectPointer
= vp_state
->scissor
.offset
);
2483 anv_batch_emit(&cmd_buffer
->batch
, GEN8_3DSTATE_VIEWPORT_STATE_POINTERS_CC
,
2484 .CCViewportPointer
= vp_state
->cc_vp
.offset
);
2485 anv_batch_emit(&cmd_buffer
->batch
, GEN8_3DSTATE_VIEWPORT_STATE_POINTERS_SF_CLIP
,
2486 .SFClipViewportPointer
= vp_state
->sf_clip_vp
.offset
);
2488 case VK_STATE_BIND_POINT_RASTER
:
2489 cmd_buffer
->rs_state
= (struct anv_dynamic_rs_state
*) dynamicState
;
2490 cmd_buffer
->dirty
|= ANV_CMD_BUFFER_RS_DIRTY
;
2492 case VK_STATE_BIND_POINT_COLOR_BLEND
:
2494 case VK_STATE_BIND_POINT_DEPTH_STENCIL
:
2495 cmd_buffer
->ds_state
= (struct anv_dynamic_ds_state
*) dynamicState
;
2496 cmd_buffer
->dirty
|= ANV_CMD_BUFFER_DS_DIRTY
;
2503 static struct anv_state
2504 anv_cmd_buffer_alloc_surface_state(struct anv_cmd_buffer
*cmd_buffer
,
2505 uint32_t size
, uint32_t alignment
)
2507 struct anv_state state
;
2509 state
.offset
= ALIGN_U32(cmd_buffer
->surface_next
, alignment
);
2510 state
.map
= cmd_buffer
->surface_bo
.map
+ state
.offset
;
2511 state
.alloc_size
= size
;
2512 cmd_buffer
->surface_next
= state
.offset
+ size
;
2514 assert(state
.offset
+ size
< cmd_buffer
->surface_bo
.size
);
2519 void anv_CmdBindDescriptorSets(
2520 VkCmdBuffer cmdBuffer
,
2521 VkPipelineBindPoint pipelineBindPoint
,
2524 const VkDescriptorSet
* pDescriptorSets
,
2525 uint32_t dynamicOffsetCount
,
2526 const uint32_t* pDynamicOffsets
)
2528 struct anv_cmd_buffer
*cmd_buffer
= (struct anv_cmd_buffer
*) cmdBuffer
;
2529 struct anv_pipeline_layout
*layout
= cmd_buffer
->pipeline
->layout
;
2530 struct anv_bindings
*bindings
= cmd_buffer
->bindings
;
2532 uint32_t offset
= 0;
2533 for (uint32_t i
= 0; i
< setCount
; i
++) {
2534 struct anv_descriptor_set
*set
=
2535 (struct anv_descriptor_set
*) pDescriptorSets
[i
];
2536 struct anv_descriptor_set_layout
*set_layout
= layout
->set
[firstSet
+ i
].layout
;
2538 for (uint32_t s
= 0; s
< VK_NUM_SHADER_STAGE
; s
++) {
2539 uint32_t *surface_to_desc
= set_layout
->stage
[s
].surface_start
;
2540 uint32_t *sampler_to_desc
= set_layout
->stage
[s
].sampler_start
;
2541 uint32_t bias
= s
== VK_SHADER_STAGE_FRAGMENT
? MAX_RTS
: 0;
2544 start
= bias
+ layout
->set
[firstSet
+ i
].surface_start
[s
];
2545 for (uint32_t b
= 0; b
< set_layout
->stage
[s
].surface_count
; b
++) {
2546 struct anv_surface_view
*view
= set
->descriptors
[surface_to_desc
[b
]].view
;
2550 struct anv_state state
=
2551 anv_cmd_buffer_alloc_surface_state(cmd_buffer
, 64, 64);
2552 memcpy(state
.map
, view
->surface_state
.map
, 64);
2554 /* The address goes in dwords 8 and 9 of the SURFACE_STATE */
2555 *(uint64_t *)(state
.map
+ 8 * 4) =
2556 anv_reloc_list_add(&cmd_buffer
->surface_relocs
,
2557 state
.offset
+ 8 * 4,
2558 view
->bo
, view
->offset
);
2560 bindings
->descriptors
[s
].surfaces
[start
+ b
] = state
.offset
;
2563 start
= layout
->set
[firstSet
+ i
].sampler_start
[s
];
2564 for (uint32_t b
= 0; b
< set_layout
->stage
[s
].sampler_count
; b
++) {
2565 struct anv_sampler
*sampler
= set
->descriptors
[sampler_to_desc
[b
]].sampler
;
2569 memcpy(&bindings
->descriptors
[s
].samplers
[start
+ b
],
2570 sampler
->state
, sizeof(sampler
->state
));
2574 offset
+= layout
->set
[firstSet
+ i
].layout
->num_dynamic_buffers
;
2577 cmd_buffer
->dirty
|= ANV_CMD_BUFFER_DESCRIPTOR_SET_DIRTY
;
2580 void anv_CmdBindIndexBuffer(
2581 VkCmdBuffer cmdBuffer
,
2583 VkDeviceSize offset
,
2584 VkIndexType indexType
)
2586 struct anv_cmd_buffer
*cmd_buffer
= (struct anv_cmd_buffer
*) cmdBuffer
;
2587 struct anv_buffer
*buffer
= (struct anv_buffer
*) _buffer
;
2589 static const uint32_t vk_to_gen_index_type
[] = {
2590 [VK_INDEX_TYPE_UINT8
] = INDEX_BYTE
,
2591 [VK_INDEX_TYPE_UINT16
] = INDEX_WORD
,
2592 [VK_INDEX_TYPE_UINT32
] = INDEX_DWORD
,
2595 anv_batch_emit(&cmd_buffer
->batch
, GEN8_3DSTATE_INDEX_BUFFER
,
2596 .IndexFormat
= vk_to_gen_index_type
[indexType
],
2597 .MemoryObjectControlState
= GEN8_MOCS
,
2598 .BufferStartingAddress
= { buffer
->bo
, buffer
->offset
+ offset
},
2599 .BufferSize
= buffer
->size
- offset
);
2602 void anv_CmdBindVertexBuffers(
2603 VkCmdBuffer cmdBuffer
,
2604 uint32_t startBinding
,
2605 uint32_t bindingCount
,
2606 const VkBuffer
* pBuffers
,
2607 const VkDeviceSize
* pOffsets
)
2609 struct anv_cmd_buffer
*cmd_buffer
= (struct anv_cmd_buffer
*) cmdBuffer
;
2610 struct anv_bindings
*bindings
= cmd_buffer
->bindings
;
2612 /* We have to defer setting up vertex buffer since we need the buffer
2613 * stride from the pipeline. */
2615 for (uint32_t i
= 0; i
< bindingCount
; i
++) {
2616 bindings
->vb
[startBinding
+ i
].buffer
= (struct anv_buffer
*) pBuffers
[i
];
2617 bindings
->vb
[startBinding
+ i
].offset
= pOffsets
[i
];
2618 cmd_buffer
->vb_dirty
|= 1 << (startBinding
+ i
);
2623 flush_descriptor_sets(struct anv_cmd_buffer
*cmd_buffer
)
2625 struct anv_pipeline_layout
*layout
= cmd_buffer
->pipeline
->layout
;
2626 struct anv_bindings
*bindings
= cmd_buffer
->bindings
;
2627 uint32_t layers
= cmd_buffer
->framebuffer
->layers
;
2629 for (uint32_t s
= 0; s
< VK_NUM_SHADER_STAGE
; s
++) {
2632 if (s
== VK_SHADER_STAGE_FRAGMENT
) {
2634 layers
= cmd_buffer
->framebuffer
->layers
;
2640 /* This is a little awkward: layout can be NULL but we still have to
2641 * allocate and set a binding table for the PS stage for render
2643 uint32_t surface_count
= layout
? layout
->stage
[s
].surface_count
: 0;
2645 if (layers
+ surface_count
> 0) {
2646 struct anv_state state
;
2649 size
= (bias
+ surface_count
) * sizeof(uint32_t);
2650 state
= anv_cmd_buffer_alloc_surface_state(cmd_buffer
, size
, 32);
2651 memcpy(state
.map
, bindings
->descriptors
[s
].surfaces
, size
);
2653 static const uint32_t binding_table_opcodes
[] = {
2654 [VK_SHADER_STAGE_VERTEX
] = 38,
2655 [VK_SHADER_STAGE_TESS_CONTROL
] = 39,
2656 [VK_SHADER_STAGE_TESS_EVALUATION
] = 40,
2657 [VK_SHADER_STAGE_GEOMETRY
] = 41,
2658 [VK_SHADER_STAGE_FRAGMENT
] = 42,
2659 [VK_SHADER_STAGE_COMPUTE
] = 0,
2662 anv_batch_emit(&cmd_buffer
->batch
,
2663 GEN8_3DSTATE_BINDING_TABLE_POINTERS_VS
,
2664 ._3DCommandSubOpcode
= binding_table_opcodes
[s
],
2665 .PointertoVSBindingTable
= state
.offset
);
2668 if (layout
&& layout
->stage
[s
].sampler_count
> 0) {
2669 struct anv_state state
;
2672 size
= layout
->stage
[s
].sampler_count
* 16;
2673 state
= anv_cmd_buffer_alloc_surface_state(cmd_buffer
, size
, 32);
2674 memcpy(state
.map
, bindings
->descriptors
[s
].samplers
, size
);
2676 static const uint32_t sampler_state_opcodes
[] = {
2677 [VK_SHADER_STAGE_VERTEX
] = 43,
2678 [VK_SHADER_STAGE_TESS_CONTROL
] = 44, /* HS */
2679 [VK_SHADER_STAGE_TESS_EVALUATION
] = 45, /* DS */
2680 [VK_SHADER_STAGE_GEOMETRY
] = 46,
2681 [VK_SHADER_STAGE_FRAGMENT
] = 47,
2682 [VK_SHADER_STAGE_COMPUTE
] = 0,
2685 anv_batch_emit(&cmd_buffer
->batch
,
2686 GEN8_3DSTATE_SAMPLER_STATE_POINTERS_VS
,
2687 ._3DCommandSubOpcode
= sampler_state_opcodes
[s
],
2688 .PointertoVSSamplerState
= state
.offset
);
2693 static struct anv_state
2694 anv_cmd_buffer_emit_dynamic(struct anv_cmd_buffer
*cmd_buffer
,
2695 uint32_t *a
, uint32_t dwords
, uint32_t alignment
)
2697 struct anv_device
*device
= cmd_buffer
->device
;
2698 struct anv_state state
;
2700 state
= anv_state_pool_alloc(&device
->dynamic_state_pool
, dwords
* 4, alignment
);
2701 memcpy(state
.map
, a
, dwords
* 4);
2706 static struct anv_state
2707 anv_cmd_buffer_merge_dynamic(struct anv_cmd_buffer
*cmd_buffer
,
2708 uint32_t *a
, uint32_t *b
, uint32_t dwords
, uint32_t alignment
)
2710 struct anv_device
*device
= cmd_buffer
->device
;
2711 struct anv_state state
;
2714 state
= anv_state_pool_alloc(&device
->dynamic_state_pool
, dwords
* 4, alignment
);
2716 for (uint32_t i
= 0; i
< dwords
; i
++)
2723 anv_cmd_buffer_flush_state(struct anv_cmd_buffer
*cmd_buffer
)
2725 struct anv_pipeline
*pipeline
= cmd_buffer
->pipeline
;
2726 struct anv_bindings
*bindings
= cmd_buffer
->bindings
;
2729 uint32_t vb_emit
= cmd_buffer
->vb_dirty
& pipeline
->vb_used
;
2730 const uint32_t num_buffers
= __builtin_popcount(vb_emit
);
2731 const uint32_t num_dwords
= 1 + num_buffers
* 4;
2734 p
= anv_batch_emitn(&cmd_buffer
->batch
, num_dwords
,
2735 GEN8_3DSTATE_VERTEX_BUFFERS
);
2737 for_each_bit(vb
, vb_emit
) {
2738 struct anv_buffer
*buffer
= bindings
->vb
[vb
].buffer
;
2739 uint32_t offset
= bindings
->vb
[vb
].offset
;
2741 struct GEN8_VERTEX_BUFFER_STATE state
= {
2742 .VertexBufferIndex
= vb
,
2743 .MemoryObjectControlState
= GEN8_MOCS
,
2744 .AddressModifyEnable
= true,
2745 .BufferPitch
= pipeline
->binding_stride
[vb
],
2746 .BufferStartingAddress
= { buffer
->bo
, buffer
->offset
+ offset
},
2747 .BufferSize
= buffer
->size
- offset
2750 GEN8_VERTEX_BUFFER_STATE_pack(&cmd_buffer
->batch
, &p
[1 + i
* 4], &state
);
2755 if (cmd_buffer
->dirty
& ANV_CMD_BUFFER_PIPELINE_DIRTY
)
2756 anv_batch_emit_batch(&cmd_buffer
->batch
, &pipeline
->batch
);
2758 if (cmd_buffer
->dirty
& ANV_CMD_BUFFER_DESCRIPTOR_SET_DIRTY
)
2759 flush_descriptor_sets(cmd_buffer
);
2761 if (cmd_buffer
->dirty
& (ANV_CMD_BUFFER_PIPELINE_DIRTY
| ANV_CMD_BUFFER_RS_DIRTY
)) {
2762 anv_batch_emit_merge(&cmd_buffer
->batch
,
2763 cmd_buffer
->rs_state
->state_sf
, pipeline
->state_sf
);
2764 anv_batch_emit_merge(&cmd_buffer
->batch
,
2765 cmd_buffer
->rs_state
->state_raster
, pipeline
->state_raster
);
2768 if (cmd_buffer
->ds_state
&&
2769 (cmd_buffer
->dirty
& (ANV_CMD_BUFFER_PIPELINE_DIRTY
| ANV_CMD_BUFFER_DS_DIRTY
)))
2770 anv_batch_emit_merge(&cmd_buffer
->batch
,
2771 cmd_buffer
->ds_state
->state_wm_depth_stencil
,
2772 pipeline
->state_wm_depth_stencil
);
2774 if (cmd_buffer
->dirty
& (ANV_CMD_BUFFER_CB_DIRTY
| ANV_CMD_BUFFER_DS_DIRTY
)) {
2775 struct anv_state state
;
2776 if (cmd_buffer
->ds_state
)
2777 state
= anv_cmd_buffer_merge_dynamic(cmd_buffer
,
2778 cmd_buffer
->ds_state
->state_color_calc
,
2779 cmd_buffer
->cb_state
->state_color_calc
,
2780 GEN8_COLOR_CALC_STATE_length
, 32);
2782 state
= anv_cmd_buffer_emit_dynamic(cmd_buffer
,
2783 cmd_buffer
->cb_state
->state_color_calc
,
2784 GEN8_COLOR_CALC_STATE_length
, 32);
2786 anv_batch_emit(&cmd_buffer
->batch
,
2787 GEN8_3DSTATE_CC_STATE_POINTERS
,
2788 .ColorCalcStatePointer
= state
.offset
,
2789 .ColorCalcStatePointerValid
= true);
2792 cmd_buffer
->vb_dirty
&= ~vb_emit
;
2793 cmd_buffer
->dirty
= 0;
2797 VkCmdBuffer cmdBuffer
,
2798 uint32_t firstVertex
,
2799 uint32_t vertexCount
,
2800 uint32_t firstInstance
,
2801 uint32_t instanceCount
)
2803 struct anv_cmd_buffer
*cmd_buffer
= (struct anv_cmd_buffer
*) cmdBuffer
;
2805 anv_cmd_buffer_flush_state(cmd_buffer
);
2807 anv_batch_emit(&cmd_buffer
->batch
, GEN8_3DPRIMITIVE
,
2808 .VertexAccessType
= SEQUENTIAL
,
2809 .VertexCountPerInstance
= vertexCount
,
2810 .StartVertexLocation
= firstVertex
,
2811 .InstanceCount
= instanceCount
,
2812 .StartInstanceLocation
= firstInstance
,
2813 .BaseVertexLocation
= 0);
2816 void anv_CmdDrawIndexed(
2817 VkCmdBuffer cmdBuffer
,
2818 uint32_t firstIndex
,
2819 uint32_t indexCount
,
2820 int32_t vertexOffset
,
2821 uint32_t firstInstance
,
2822 uint32_t instanceCount
)
2824 struct anv_cmd_buffer
*cmd_buffer
= (struct anv_cmd_buffer
*) cmdBuffer
;
2826 anv_cmd_buffer_flush_state(cmd_buffer
);
2828 anv_batch_emit(&cmd_buffer
->batch
, GEN8_3DPRIMITIVE
,
2829 .VertexAccessType
= RANDOM
,
2830 .VertexCountPerInstance
= indexCount
,
2831 .StartVertexLocation
= firstIndex
,
2832 .InstanceCount
= instanceCount
,
2833 .StartInstanceLocation
= firstInstance
,
2834 .BaseVertexLocation
= 0);
2838 anv_batch_lrm(struct anv_batch
*batch
,
2839 uint32_t reg
, struct anv_bo
*bo
, uint32_t offset
)
2841 anv_batch_emit(batch
, GEN8_MI_LOAD_REGISTER_MEM
,
2842 .RegisterAddress
= reg
,
2843 .MemoryAddress
= { bo
, offset
});
2847 anv_batch_lri(struct anv_batch
*batch
, uint32_t reg
, uint32_t imm
)
2849 anv_batch_emit(batch
, GEN8_MI_LOAD_REGISTER_IMM
,
2850 .RegisterOffset
= reg
,
2854 /* Auto-Draw / Indirect Registers */
2855 #define GEN7_3DPRIM_END_OFFSET 0x2420
2856 #define GEN7_3DPRIM_START_VERTEX 0x2430
2857 #define GEN7_3DPRIM_VERTEX_COUNT 0x2434
2858 #define GEN7_3DPRIM_INSTANCE_COUNT 0x2438
2859 #define GEN7_3DPRIM_START_INSTANCE 0x243C
2860 #define GEN7_3DPRIM_BASE_VERTEX 0x2440
2862 void anv_CmdDrawIndirect(
2863 VkCmdBuffer cmdBuffer
,
2865 VkDeviceSize offset
,
2869 struct anv_cmd_buffer
*cmd_buffer
= (struct anv_cmd_buffer
*) cmdBuffer
;
2870 struct anv_buffer
*buffer
= (struct anv_buffer
*) _buffer
;
2871 struct anv_bo
*bo
= buffer
->bo
;
2872 uint32_t bo_offset
= buffer
->offset
+ offset
;
2874 anv_cmd_buffer_flush_state(cmd_buffer
);
2876 anv_batch_lrm(&cmd_buffer
->batch
, GEN7_3DPRIM_VERTEX_COUNT
, bo
, bo_offset
);
2877 anv_batch_lrm(&cmd_buffer
->batch
, GEN7_3DPRIM_INSTANCE_COUNT
, bo
, bo_offset
+ 4);
2878 anv_batch_lrm(&cmd_buffer
->batch
, GEN7_3DPRIM_START_VERTEX
, bo
, bo_offset
+ 8);
2879 anv_batch_lrm(&cmd_buffer
->batch
, GEN7_3DPRIM_START_INSTANCE
, bo
, bo_offset
+ 12);
2880 anv_batch_lri(&cmd_buffer
->batch
, GEN7_3DPRIM_BASE_VERTEX
, 0);
2882 anv_batch_emit(&cmd_buffer
->batch
, GEN8_3DPRIMITIVE
,
2883 .IndirectParameterEnable
= true,
2884 .VertexAccessType
= SEQUENTIAL
);
2887 void anv_CmdDrawIndexedIndirect(
2888 VkCmdBuffer cmdBuffer
,
2890 VkDeviceSize offset
,
2894 struct anv_cmd_buffer
*cmd_buffer
= (struct anv_cmd_buffer
*) cmdBuffer
;
2895 struct anv_buffer
*buffer
= (struct anv_buffer
*) _buffer
;
2896 struct anv_bo
*bo
= buffer
->bo
;
2897 uint32_t bo_offset
= buffer
->offset
+ offset
;
2899 anv_cmd_buffer_flush_state(cmd_buffer
);
2901 anv_batch_lrm(&cmd_buffer
->batch
, GEN7_3DPRIM_VERTEX_COUNT
, bo
, bo_offset
);
2902 anv_batch_lrm(&cmd_buffer
->batch
, GEN7_3DPRIM_INSTANCE_COUNT
, bo
, bo_offset
+ 4);
2903 anv_batch_lrm(&cmd_buffer
->batch
, GEN7_3DPRIM_START_VERTEX
, bo
, bo_offset
+ 8);
2904 anv_batch_lrm(&cmd_buffer
->batch
, GEN7_3DPRIM_BASE_VERTEX
, bo
, bo_offset
+ 12);
2905 anv_batch_lrm(&cmd_buffer
->batch
, GEN7_3DPRIM_START_INSTANCE
, bo
, bo_offset
+ 16);
2907 anv_batch_emit(&cmd_buffer
->batch
, GEN8_3DPRIMITIVE
,
2908 .IndirectParameterEnable
= true,
2909 .VertexAccessType
= RANDOM
);
2912 void anv_CmdDispatch(
2913 VkCmdBuffer cmdBuffer
,
2921 void anv_CmdDispatchIndirect(
2922 VkCmdBuffer cmdBuffer
,
2924 VkDeviceSize offset
)
2929 void anv_CmdSetEvent(
2930 VkCmdBuffer cmdBuffer
,
2932 VkPipeEvent pipeEvent
)
2937 void anv_CmdResetEvent(
2938 VkCmdBuffer cmdBuffer
,
2940 VkPipeEvent pipeEvent
)
2945 void anv_CmdWaitEvents(
2946 VkCmdBuffer cmdBuffer
,
2947 VkWaitEvent waitEvent
,
2948 uint32_t eventCount
,
2949 const VkEvent
* pEvents
,
2950 uint32_t memBarrierCount
,
2951 const void** ppMemBarriers
)
2956 void anv_CmdPipelineBarrier(
2957 VkCmdBuffer cmdBuffer
,
2958 VkWaitEvent waitEvent
,
2959 uint32_t pipeEventCount
,
2960 const VkPipeEvent
* pPipeEvents
,
2961 uint32_t memBarrierCount
,
2962 const void** ppMemBarriers
)
2968 anv_batch_emit_ps_depth_count(struct anv_batch
*batch
,
2969 struct anv_bo
*bo
, uint32_t offset
)
2971 anv_batch_emit(batch
, GEN8_PIPE_CONTROL
,
2972 .DestinationAddressType
= DAT_PPGTT
,
2973 .PostSyncOperation
= WritePSDepthCount
,
2974 .Address
= { bo
, offset
}); /* FIXME: This is only lower 32 bits */
2977 void anv_CmdBeginQuery(
2978 VkCmdBuffer cmdBuffer
,
2979 VkQueryPool queryPool
,
2981 VkQueryControlFlags flags
)
2983 struct anv_cmd_buffer
*cmd_buffer
= (struct anv_cmd_buffer
*) cmdBuffer
;
2984 struct anv_query_pool
*pool
= (struct anv_query_pool
*) queryPool
;
2986 switch (pool
->type
) {
2987 case VK_QUERY_TYPE_OCCLUSION
:
2988 anv_batch_emit_ps_depth_count(&cmd_buffer
->batch
, &pool
->bo
,
2989 slot
* sizeof(struct anv_query_pool_slot
));
2992 case VK_QUERY_TYPE_PIPELINE_STATISTICS
:
2998 void anv_CmdEndQuery(
2999 VkCmdBuffer cmdBuffer
,
3000 VkQueryPool queryPool
,
3003 struct anv_cmd_buffer
*cmd_buffer
= (struct anv_cmd_buffer
*) cmdBuffer
;
3004 struct anv_query_pool
*pool
= (struct anv_query_pool
*) queryPool
;
3006 switch (pool
->type
) {
3007 case VK_QUERY_TYPE_OCCLUSION
:
3008 anv_batch_emit_ps_depth_count(&cmd_buffer
->batch
, &pool
->bo
,
3009 slot
* sizeof(struct anv_query_pool_slot
) + 8);
3012 case VK_QUERY_TYPE_PIPELINE_STATISTICS
:
3018 void anv_CmdResetQueryPool(
3019 VkCmdBuffer cmdBuffer
,
3020 VkQueryPool queryPool
,
3021 uint32_t startQuery
,
3022 uint32_t queryCount
)
3027 #define TIMESTAMP 0x2358
3029 void anv_CmdWriteTimestamp(
3030 VkCmdBuffer cmdBuffer
,
3031 VkTimestampType timestampType
,
3032 VkBuffer destBuffer
,
3033 VkDeviceSize destOffset
)
3035 struct anv_cmd_buffer
*cmd_buffer
= (struct anv_cmd_buffer
*) cmdBuffer
;
3036 struct anv_buffer
*buffer
= (struct anv_buffer
*) destBuffer
;
3037 struct anv_bo
*bo
= buffer
->bo
;
3039 switch (timestampType
) {
3040 case VK_TIMESTAMP_TYPE_TOP
:
3041 anv_batch_emit(&cmd_buffer
->batch
, GEN8_MI_STORE_REGISTER_MEM
,
3042 .RegisterAddress
= TIMESTAMP
,
3043 .MemoryAddress
= { bo
, buffer
->offset
+ destOffset
});
3044 anv_batch_emit(&cmd_buffer
->batch
, GEN8_MI_STORE_REGISTER_MEM
,
3045 .RegisterAddress
= TIMESTAMP
+ 4,
3046 .MemoryAddress
= { bo
, buffer
->offset
+ destOffset
+ 4 });
3049 case VK_TIMESTAMP_TYPE_BOTTOM
:
3050 anv_batch_emit(&cmd_buffer
->batch
, GEN8_PIPE_CONTROL
,
3051 .DestinationAddressType
= DAT_PPGTT
,
3052 .PostSyncOperation
= WriteTimestamp
,
3053 .Address
= /* FIXME: This is only lower 32 bits */
3054 { bo
, buffer
->offset
+ destOffset
});
3062 #define alu_opcode(v) __gen_field((v), 20, 31)
3063 #define alu_operand1(v) __gen_field((v), 10, 19)
3064 #define alu_operand2(v) __gen_field((v), 0, 9)
3065 #define alu(opcode, operand1, operand2) \
3066 alu_opcode(opcode) | alu_operand1(operand1) | alu_operand2(operand2)
3068 #define OPCODE_NOOP 0x000
3069 #define OPCODE_LOAD 0x080
3070 #define OPCODE_LOADINV 0x480
3071 #define OPCODE_LOAD0 0x081
3072 #define OPCODE_LOAD1 0x481
3073 #define OPCODE_ADD 0x100
3074 #define OPCODE_SUB 0x101
3075 #define OPCODE_AND 0x102
3076 #define OPCODE_OR 0x103
3077 #define OPCODE_XOR 0x104
3078 #define OPCODE_STORE 0x180
3079 #define OPCODE_STOREINV 0x580
3081 #define OPERAND_R0 0x00
3082 #define OPERAND_R1 0x01
3083 #define OPERAND_R2 0x02
3084 #define OPERAND_R3 0x03
3085 #define OPERAND_R4 0x04
3086 #define OPERAND_SRCA 0x20
3087 #define OPERAND_SRCB 0x21
3088 #define OPERAND_ACCU 0x31
3089 #define OPERAND_ZF 0x32
3090 #define OPERAND_CF 0x33
3092 #define CS_GPR(n) (0x2600 + (n) * 8)
3095 emit_load_alu_reg_u64(struct anv_batch
*batch
, uint32_t reg
,
3096 struct anv_bo
*bo
, uint32_t offset
)
3098 anv_batch_emit(batch
, GEN8_MI_LOAD_REGISTER_MEM
,
3099 .RegisterAddress
= reg
,
3100 .MemoryAddress
= { bo
, offset
});
3101 anv_batch_emit(batch
, GEN8_MI_LOAD_REGISTER_MEM
,
3102 .RegisterAddress
= reg
+ 4,
3103 .MemoryAddress
= { bo
, offset
+ 4 });
3106 void anv_CmdCopyQueryPoolResults(
3107 VkCmdBuffer cmdBuffer
,
3108 VkQueryPool queryPool
,
3109 uint32_t startQuery
,
3110 uint32_t queryCount
,
3111 VkBuffer destBuffer
,
3112 VkDeviceSize destOffset
,
3113 VkDeviceSize destStride
,
3114 VkQueryResultFlags flags
)
3116 struct anv_cmd_buffer
*cmd_buffer
= (struct anv_cmd_buffer
*) cmdBuffer
;
3117 struct anv_query_pool
*pool
= (struct anv_query_pool
*) queryPool
;
3118 struct anv_buffer
*buffer
= (struct anv_buffer
*) destBuffer
;
3119 uint32_t slot_offset
, dst_offset
;
3121 if (flags
& VK_QUERY_RESULT_WITH_AVAILABILITY_BIT
) {
3122 /* Where is the availabilty info supposed to go? */
3123 anv_finishme("VK_QUERY_RESULT_WITH_AVAILABILITY_BIT");
3127 assert(pool
->type
== VK_QUERY_TYPE_OCCLUSION
);
3129 /* FIXME: If we're not waiting, should we just do this on the CPU? */
3130 if (flags
& VK_QUERY_RESULT_WAIT_BIT
)
3131 anv_batch_emit(&cmd_buffer
->batch
, GEN8_PIPE_CONTROL
,
3132 .CommandStreamerStallEnable
= true);
3134 dst_offset
= buffer
->offset
+ destOffset
;
3135 for (uint32_t i
= 0; i
< queryCount
; i
++) {
3137 slot_offset
= (startQuery
+ i
) * sizeof(struct anv_query_pool_slot
);
3139 emit_load_alu_reg_u64(&cmd_buffer
->batch
, CS_GPR(0), &pool
->bo
, slot_offset
);
3140 emit_load_alu_reg_u64(&cmd_buffer
->batch
, CS_GPR(1), &pool
->bo
, slot_offset
+ 8);
3142 /* FIXME: We need to clamp the result for 32 bit. */
3144 uint32_t *dw
= anv_batch_emitn(&cmd_buffer
->batch
, 5, GEN8_MI_MATH
);
3145 dw
[1] = alu(OPCODE_LOAD
, OPERAND_SRCA
, OPERAND_R1
);
3146 dw
[2] = alu(OPCODE_LOAD
, OPERAND_SRCB
, OPERAND_R0
);
3147 dw
[3] = alu(OPCODE_SUB
, 0, 0);
3148 dw
[4] = alu(OPCODE_STORE
, OPERAND_R2
, OPERAND_ACCU
);
3150 anv_batch_emit(&cmd_buffer
->batch
, GEN8_MI_STORE_REGISTER_MEM
,
3151 .RegisterAddress
= CS_GPR(2),
3152 /* FIXME: This is only lower 32 bits */
3153 .MemoryAddress
= { buffer
->bo
, dst_offset
});
3155 if (flags
& VK_QUERY_RESULT_64_BIT
)
3156 anv_batch_emit(&cmd_buffer
->batch
, GEN8_MI_STORE_REGISTER_MEM
,
3157 .RegisterAddress
= CS_GPR(2) + 4,
3158 /* FIXME: This is only lower 32 bits */
3159 .MemoryAddress
= { buffer
->bo
, dst_offset
+ 4 });
3161 dst_offset
+= destStride
;
3165 void anv_CmdInitAtomicCounters(
3166 VkCmdBuffer cmdBuffer
,
3167 VkPipelineBindPoint pipelineBindPoint
,
3168 uint32_t startCounter
,
3169 uint32_t counterCount
,
3170 const uint32_t* pData
)
3175 void anv_CmdLoadAtomicCounters(
3176 VkCmdBuffer cmdBuffer
,
3177 VkPipelineBindPoint pipelineBindPoint
,
3178 uint32_t startCounter
,
3179 uint32_t counterCount
,
3181 VkDeviceSize srcOffset
)
3186 void anv_CmdSaveAtomicCounters(
3187 VkCmdBuffer cmdBuffer
,
3188 VkPipelineBindPoint pipelineBindPoint
,
3189 uint32_t startCounter
,
3190 uint32_t counterCount
,
3191 VkBuffer destBuffer
,
3192 VkDeviceSize destOffset
)
3198 anv_framebuffer_destroy(struct anv_device
*device
,
3199 struct anv_object
*object
,
3200 VkObjectType obj_type
)
3202 struct anv_framebuffer
*fb
= (struct anv_framebuffer
*)object
;
3204 assert(obj_type
== VK_OBJECT_TYPE_FRAMEBUFFER
);
3206 anv_DestroyObject((VkDevice
) device
,
3207 VK_OBJECT_TYPE_DYNAMIC_VP_STATE
,
3210 anv_device_free(device
, fb
);
3213 VkResult
anv_CreateFramebuffer(
3215 const VkFramebufferCreateInfo
* pCreateInfo
,
3216 VkFramebuffer
* pFramebuffer
)
3218 struct anv_device
*device
= (struct anv_device
*) _device
;
3219 struct anv_framebuffer
*framebuffer
;
3221 static const struct anv_depth_stencil_view null_view
=
3222 { .depth_format
= D16_UNORM
, .depth_stride
= 0, .stencil_stride
= 0 };
3224 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO
);
3226 framebuffer
= anv_device_alloc(device
, sizeof(*framebuffer
), 8,
3227 VK_SYSTEM_ALLOC_TYPE_API_OBJECT
);
3228 if (framebuffer
== NULL
)
3229 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
3231 framebuffer
->base
.destructor
= anv_framebuffer_destroy
;
3233 framebuffer
->color_attachment_count
= pCreateInfo
->colorAttachmentCount
;
3234 for (uint32_t i
= 0; i
< pCreateInfo
->colorAttachmentCount
; i
++) {
3235 framebuffer
->color_attachments
[i
] =
3236 (struct anv_surface_view
*) pCreateInfo
->pColorAttachments
[i
].view
;
3239 if (pCreateInfo
->pDepthStencilAttachment
) {
3240 framebuffer
->depth_stencil
=
3241 (struct anv_depth_stencil_view
*) pCreateInfo
->pDepthStencilAttachment
->view
;
3243 framebuffer
->depth_stencil
= &null_view
;
3246 framebuffer
->sample_count
= pCreateInfo
->sampleCount
;
3247 framebuffer
->width
= pCreateInfo
->width
;
3248 framebuffer
->height
= pCreateInfo
->height
;
3249 framebuffer
->layers
= pCreateInfo
->layers
;
3251 vkCreateDynamicViewportState((VkDevice
) device
,
3252 &(VkDynamicVpStateCreateInfo
) {
3253 .sType
= VK_STRUCTURE_TYPE_DYNAMIC_VP_STATE_CREATE_INFO
,
3254 .viewportAndScissorCount
= 1,
3255 .pViewports
= (VkViewport
[]) {
3259 .width
= pCreateInfo
->width
,
3260 .height
= pCreateInfo
->height
,
3265 .pScissors
= (VkRect
[]) {
3267 { pCreateInfo
->width
, pCreateInfo
->height
} },
3270 &framebuffer
->vp_state
);
3272 *pFramebuffer
= (VkFramebuffer
) framebuffer
;
3277 VkResult
anv_CreateRenderPass(
3279 const VkRenderPassCreateInfo
* pCreateInfo
,
3280 VkRenderPass
* pRenderPass
)
3282 struct anv_device
*device
= (struct anv_device
*) _device
;
3283 struct anv_render_pass
*pass
;
3286 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO
);
3288 size
= sizeof(*pass
) +
3289 pCreateInfo
->layers
* sizeof(struct anv_render_pass_layer
);
3290 pass
= anv_device_alloc(device
, size
, 8,
3291 VK_SYSTEM_ALLOC_TYPE_API_OBJECT
);
3293 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
3295 pass
->render_area
= pCreateInfo
->renderArea
;
3297 pass
->num_layers
= pCreateInfo
->layers
;
3299 pass
->num_clear_layers
= 0;
3300 for (uint32_t i
= 0; i
< pCreateInfo
->layers
; i
++) {
3301 pass
->layers
[i
].color_load_op
= pCreateInfo
->pColorLoadOps
[i
];
3302 pass
->layers
[i
].clear_color
= pCreateInfo
->pColorLoadClearValues
[i
];
3303 if (pass
->layers
[i
].color_load_op
== VK_ATTACHMENT_LOAD_OP_CLEAR
)
3304 pass
->num_clear_layers
++;
3307 *pRenderPass
= (VkRenderPass
) pass
;
3313 anv_cmd_buffer_fill_render_targets(struct anv_cmd_buffer
*cmd_buffer
)
3315 struct anv_framebuffer
*framebuffer
= cmd_buffer
->framebuffer
;
3316 struct anv_bindings
*bindings
= cmd_buffer
->bindings
;
3318 for (uint32_t i
= 0; i
< framebuffer
->color_attachment_count
; i
++) {
3319 const struct anv_surface_view
*view
= framebuffer
->color_attachments
[i
];
3321 struct anv_state state
=
3322 anv_cmd_buffer_alloc_surface_state(cmd_buffer
, 64, 64);
3323 memcpy(state
.map
, view
->surface_state
.map
, 64);
3325 /* The address goes in dwords 8 and 9 of the SURFACE_STATE */
3326 *(uint64_t *)(state
.map
+ 8 * 4) =
3327 anv_reloc_list_add(&cmd_buffer
->surface_relocs
,
3328 state
.offset
+ 8 * 4,
3329 view
->bo
, view
->offset
);
3331 bindings
->descriptors
[VK_SHADER_STAGE_FRAGMENT
].surfaces
[i
] = state
.offset
;
3333 cmd_buffer
->dirty
|= ANV_CMD_BUFFER_DESCRIPTOR_SET_DIRTY
;
3337 anv_cmd_buffer_emit_depth_stencil(struct anv_cmd_buffer
*cmd_buffer
,
3338 struct anv_render_pass
*pass
)
3340 const struct anv_depth_stencil_view
*view
=
3341 cmd_buffer
->framebuffer
->depth_stencil
;
3343 /* FIXME: Implement the PMA stall W/A */
3345 anv_batch_emit(&cmd_buffer
->batch
, GEN8_3DSTATE_DEPTH_BUFFER
,
3346 .SurfaceType
= SURFTYPE_2D
,
3347 .DepthWriteEnable
= view
->depth_stride
> 0,
3348 .StencilWriteEnable
= view
->stencil_stride
> 0,
3349 .HierarchicalDepthBufferEnable
= false,
3350 .SurfaceFormat
= view
->depth_format
,
3351 .SurfacePitch
= view
->depth_stride
> 0 ? view
->depth_stride
- 1 : 0,
3352 .SurfaceBaseAddress
= { view
->bo
, view
->depth_offset
},
3353 .Height
= pass
->render_area
.extent
.height
- 1,
3354 .Width
= pass
->render_area
.extent
.width
- 1,
3357 .MinimumArrayElement
= 0,
3358 .DepthBufferObjectControlState
= GEN8_MOCS
,
3359 .RenderTargetViewExtent
= 1 - 1,
3360 .SurfaceQPitch
= 0);
3362 /* Disable hierarchial depth buffers. */
3363 anv_batch_emit(&cmd_buffer
->batch
, GEN8_3DSTATE_HIER_DEPTH_BUFFER
);
3365 anv_batch_emit(&cmd_buffer
->batch
, GEN8_3DSTATE_STENCIL_BUFFER
,
3366 .StencilBufferEnable
= view
->stencil_stride
> 0,
3367 .StencilBufferObjectControlState
= GEN8_MOCS
,
3368 .SurfacePitch
= view
->stencil_stride
> 0 ? view
->stencil_stride
- 1 : 0,
3369 .SurfaceBaseAddress
= { view
->bo
, view
->stencil_offset
},
3370 .SurfaceQPitch
= 0);
3372 /* Clear the clear params. */
3373 anv_batch_emit(&cmd_buffer
->batch
, GEN8_3DSTATE_CLEAR_PARAMS
);
3376 void anv_CmdBeginRenderPass(
3377 VkCmdBuffer cmdBuffer
,
3378 const VkRenderPassBegin
* pRenderPassBegin
)
3380 struct anv_cmd_buffer
*cmd_buffer
= (struct anv_cmd_buffer
*) cmdBuffer
;
3381 struct anv_render_pass
*pass
= (struct anv_render_pass
*) pRenderPassBegin
->renderPass
;
3382 struct anv_framebuffer
*framebuffer
=
3383 (struct anv_framebuffer
*) pRenderPassBegin
->framebuffer
;
3385 cmd_buffer
->framebuffer
= framebuffer
;
3387 anv_batch_emit(&cmd_buffer
->batch
, GEN8_3DSTATE_DRAWING_RECTANGLE
,
3388 .ClippedDrawingRectangleYMin
= pass
->render_area
.offset
.y
,
3389 .ClippedDrawingRectangleXMin
= pass
->render_area
.offset
.x
,
3390 .ClippedDrawingRectangleYMax
=
3391 pass
->render_area
.offset
.y
+ pass
->render_area
.extent
.height
- 1,
3392 .ClippedDrawingRectangleXMax
=
3393 pass
->render_area
.offset
.x
+ pass
->render_area
.extent
.width
- 1,
3394 .DrawingRectangleOriginY
= 0,
3395 .DrawingRectangleOriginX
= 0);
3397 anv_cmd_buffer_fill_render_targets(cmd_buffer
);
3399 anv_cmd_buffer_emit_depth_stencil(cmd_buffer
, pass
);
3401 anv_cmd_buffer_clear(cmd_buffer
, pass
);
3404 void anv_CmdEndRenderPass(
3405 VkCmdBuffer cmdBuffer
,
3406 VkRenderPass renderPass
)
3408 /* Emit a flushing pipe control at the end of a pass. This is kind of a
3409 * hack but it ensures that render targets always actually get written.
3410 * Eventually, we should do flushing based on image format transitions
3411 * or something of that nature.
3413 struct anv_cmd_buffer
*cmd_buffer
= (struct anv_cmd_buffer
*)cmdBuffer
;
3414 anv_batch_emit(&cmd_buffer
->batch
, GEN8_PIPE_CONTROL
,
3415 .PostSyncOperation
= NoWrite
,
3416 .RenderTargetCacheFlushEnable
= true,
3417 .InstructionCacheInvalidateEnable
= true,
3418 .DepthCacheFlushEnable
= true,
3419 .VFCacheInvalidationEnable
= true,
3420 .TextureCacheInvalidationEnable
= true,
3421 .CommandStreamerStallEnable
= true);
3424 void vkCmdDbgMarkerBegin(
3425 VkCmdBuffer cmdBuffer
,
3426 const char* pMarker
)
3427 __attribute__ ((visibility ("default")));
3429 void vkCmdDbgMarkerEnd(
3430 VkCmdBuffer cmdBuffer
)
3431 __attribute__ ((visibility ("default")));
3433 VkResult
vkDbgSetObjectTag(
3438 __attribute__ ((visibility ("default")));
3441 void vkCmdDbgMarkerBegin(
3442 VkCmdBuffer cmdBuffer
,
3443 const char* pMarker
)
3447 void vkCmdDbgMarkerEnd(
3448 VkCmdBuffer cmdBuffer
)
3452 VkResult
vkDbgSetObjectTag(