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;
307 anv_queue_init(struct anv_device
*device
, struct anv_queue
*queue
)
309 queue
->device
= device
;
310 queue
->pool
= &device
->surface_state_pool
;
312 queue
->completed_serial
= anv_state_pool_alloc(queue
->pool
, 4, 4);
313 if (queue
->completed_serial
.map
== NULL
)
314 return vk_error(VK_ERROR_OUT_OF_DEVICE_MEMORY
);
316 *(uint32_t *)queue
->completed_serial
.map
= 0;
317 queue
->next_serial
= 1;
323 anv_queue_finish(struct anv_queue
*queue
)
326 /* This gets torn down with the device so we only need to do this if
327 * valgrind is present.
329 anv_state_pool_free(queue
->pool
, queue
->completed_serial
);
334 anv_device_init_border_colors(struct anv_device
*device
)
336 float float_border_colors
[][4] = {
337 [VK_BORDER_COLOR_OPAQUE_WHITE
] = { 1.0, 1.0, 1.0, 1.0 },
338 [VK_BORDER_COLOR_TRANSPARENT_BLACK
] = { 0.0, 0.0, 0.0, 0.0 },
339 [VK_BORDER_COLOR_OPAQUE_BLACK
] = { 0.0, 0.0, 0.0, 1.0 }
342 uint32_t uint32_border_colors
[][4] = {
343 [VK_BORDER_COLOR_OPAQUE_WHITE
] = { 1, 1, 1, 1 },
344 [VK_BORDER_COLOR_TRANSPARENT_BLACK
] = { 0, 0, 0, 0 },
345 [VK_BORDER_COLOR_OPAQUE_BLACK
] = { 0, 0, 0, 1 }
348 device
->float_border_colors
=
349 anv_state_pool_alloc(&device
->dynamic_state_pool
,
350 sizeof(float_border_colors
), 32);
351 memcpy(device
->float_border_colors
.map
,
352 float_border_colors
, sizeof(float_border_colors
));
354 device
->uint32_border_colors
=
355 anv_state_pool_alloc(&device
->dynamic_state_pool
,
356 sizeof(uint32_border_colors
), 32);
357 memcpy(device
->uint32_border_colors
.map
,
358 uint32_border_colors
, sizeof(uint32_border_colors
));
362 static const uint32_t BATCH_SIZE
= 8192;
364 VkResult
anv_CreateDevice(
365 VkPhysicalDevice _physicalDevice
,
366 const VkDeviceCreateInfo
* pCreateInfo
,
369 struct anv_physical_device
*physicalDevice
=
370 (struct anv_physical_device
*) _physicalDevice
;
371 struct anv_instance
*instance
= physicalDevice
->instance
;
372 struct anv_device
*device
;
374 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO
);
376 device
= instance
->pfnAlloc(instance
->pAllocUserData
,
378 VK_SYSTEM_ALLOC_TYPE_API_OBJECT
);
380 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
382 device
->no_hw
= physicalDevice
->no_hw
;
383 parse_debug_flags(device
);
385 device
->instance
= physicalDevice
->instance
;
386 device
->fd
= open("/dev/dri/renderD128", O_RDWR
| O_CLOEXEC
);
387 if (device
->fd
== -1)
390 device
->context_id
= anv_gem_create_context(device
);
391 if (device
->context_id
== -1)
394 anv_bo_pool_init(&device
->batch_bo_pool
, device
, BATCH_SIZE
);
396 anv_block_pool_init(&device
->dynamic_state_block_pool
, device
, 2048);
398 anv_state_pool_init(&device
->dynamic_state_pool
,
399 &device
->dynamic_state_block_pool
);
401 anv_block_pool_init(&device
->instruction_block_pool
, device
, 2048);
402 anv_block_pool_init(&device
->surface_state_block_pool
, device
, 2048);
404 anv_state_pool_init(&device
->surface_state_pool
,
405 &device
->surface_state_block_pool
);
407 device
->info
= *physicalDevice
->info
;
409 device
->compiler
= anv_compiler_create(device
);
410 device
->aub_writer
= NULL
;
412 pthread_mutex_init(&device
->mutex
, NULL
);
414 anv_queue_init(device
, &device
->queue
);
416 anv_device_init_meta(device
);
418 anv_device_init_border_colors(device
);
420 *pDevice
= (VkDevice
) device
;
427 anv_device_free(device
, device
);
429 return vk_error(VK_ERROR_UNAVAILABLE
);
432 VkResult
anv_DestroyDevice(
435 struct anv_device
*device
= (struct anv_device
*) _device
;
437 anv_compiler_destroy(device
->compiler
);
439 anv_queue_finish(&device
->queue
);
441 anv_device_finish_meta(device
);
444 /* We only need to free these to prevent valgrind errors. The backing
445 * BO will go away in a couple of lines so we don't actually leak.
447 anv_state_pool_free(&device
->dynamic_state_pool
,
448 device
->float_border_colors
);
449 anv_state_pool_free(&device
->dynamic_state_pool
,
450 device
->uint32_border_colors
);
453 anv_bo_pool_finish(&device
->batch_bo_pool
);
454 anv_block_pool_finish(&device
->dynamic_state_block_pool
);
455 anv_block_pool_finish(&device
->instruction_block_pool
);
456 anv_block_pool_finish(&device
->surface_state_block_pool
);
460 if (device
->aub_writer
)
461 anv_aub_writer_destroy(device
->aub_writer
);
463 anv_device_free(device
, device
);
468 VkResult
anv_GetGlobalExtensionInfo(
469 VkExtensionInfoType infoType
,
470 uint32_t extensionIndex
,
474 static const VkExtensionProperties extensions
[] = {
476 .extName
= "VK_WSI_LunarG",
480 uint32_t count
= ARRAY_SIZE(extensions
);
483 case VK_EXTENSION_INFO_TYPE_COUNT
:
484 memcpy(pData
, &count
, sizeof(count
));
485 *pDataSize
= sizeof(count
);
488 case VK_EXTENSION_INFO_TYPE_PROPERTIES
:
489 if (extensionIndex
>= count
)
490 return vk_error(VK_ERROR_INVALID_EXTENSION
);
492 memcpy(pData
, &extensions
[extensionIndex
], sizeof(extensions
[0]));
493 *pDataSize
= sizeof(extensions
[0]);
497 return VK_UNSUPPORTED
;
501 VkResult
anv_GetPhysicalDeviceExtensionInfo(
502 VkPhysicalDevice physicalDevice
,
503 VkExtensionInfoType infoType
,
504 uint32_t extensionIndex
,
511 case VK_EXTENSION_INFO_TYPE_COUNT
:
520 case VK_EXTENSION_INFO_TYPE_PROPERTIES
:
521 return vk_error(VK_ERROR_INVALID_EXTENSION
);
524 return VK_UNSUPPORTED
;
528 VkResult
anv_EnumerateLayers(
529 VkPhysicalDevice physicalDevice
,
530 size_t maxStringSize
,
532 char* const* pOutLayers
,
540 VkResult
anv_GetDeviceQueue(
542 uint32_t queueNodeIndex
,
546 struct anv_device
*device
= (struct anv_device
*) _device
;
548 assert(queueIndex
== 0);
550 *pQueue
= (VkQueue
) &device
->queue
;
556 anv_reloc_list_init(struct anv_reloc_list
*list
, struct anv_device
*device
)
558 list
->num_relocs
= 0;
559 list
->array_length
= 256;
561 anv_device_alloc(device
, list
->array_length
* sizeof(*list
->relocs
), 8,
562 VK_SYSTEM_ALLOC_TYPE_INTERNAL
);
564 if (list
->relocs
== NULL
)
565 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
568 anv_device_alloc(device
, list
->array_length
* sizeof(*list
->reloc_bos
), 8,
569 VK_SYSTEM_ALLOC_TYPE_INTERNAL
);
571 if (list
->relocs
== NULL
) {
572 anv_device_free(device
, list
->relocs
);
573 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
580 anv_reloc_list_finish(struct anv_reloc_list
*list
, struct anv_device
*device
)
582 anv_device_free(device
, list
->relocs
);
583 anv_device_free(device
, list
->reloc_bos
);
587 anv_reloc_list_grow(struct anv_reloc_list
*list
, struct anv_device
*device
,
588 size_t num_additional_relocs
)
590 if (list
->num_relocs
+ num_additional_relocs
<= list
->array_length
)
593 size_t new_length
= list
->array_length
* 2;
594 while (new_length
< list
->num_relocs
+ num_additional_relocs
)
597 struct drm_i915_gem_relocation_entry
*new_relocs
=
598 anv_device_alloc(device
, new_length
* sizeof(*list
->relocs
), 8,
599 VK_SYSTEM_ALLOC_TYPE_INTERNAL
);
600 if (new_relocs
== NULL
)
601 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
603 struct anv_bo
**new_reloc_bos
=
604 anv_device_alloc(device
, new_length
* sizeof(*list
->reloc_bos
), 8,
605 VK_SYSTEM_ALLOC_TYPE_INTERNAL
);
606 if (new_relocs
== NULL
) {
607 anv_device_free(device
, new_relocs
);
608 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
611 memcpy(new_relocs
, list
->relocs
, list
->num_relocs
* sizeof(*list
->relocs
));
612 memcpy(new_reloc_bos
, list
->reloc_bos
,
613 list
->num_relocs
* sizeof(*list
->reloc_bos
));
615 anv_device_free(device
, list
->relocs
);
616 anv_device_free(device
, list
->reloc_bos
);
618 list
->relocs
= new_relocs
;
619 list
->reloc_bos
= new_reloc_bos
;
625 anv_batch_bo_create(struct anv_device
*device
, struct anv_batch_bo
**bbo_out
)
629 struct anv_batch_bo
*bbo
=
630 anv_device_alloc(device
, sizeof(*bbo
), 8, VK_SYSTEM_ALLOC_TYPE_INTERNAL
);
632 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
635 bbo
->prev_batch_bo
= NULL
;
637 result
= anv_bo_pool_alloc(&device
->batch_bo_pool
, &bbo
->bo
);
638 if (result
!= VK_SUCCESS
) {
639 anv_device_free(device
, bbo
);
649 anv_batch_bo_start(struct anv_batch_bo
*bbo
, struct anv_batch
*batch
,
650 size_t batch_padding
)
652 batch
->next
= batch
->start
= bbo
->bo
.map
;
653 batch
->end
= bbo
->bo
.map
+ bbo
->bo
.size
- batch_padding
;
654 bbo
->first_reloc
= batch
->relocs
.num_relocs
;
658 anv_batch_bo_finish(struct anv_batch_bo
*bbo
, struct anv_batch
*batch
)
660 assert(batch
->start
== bbo
->bo
.map
);
661 bbo
->length
= batch
->next
- batch
->start
;
662 bbo
->num_relocs
= batch
->relocs
.num_relocs
- bbo
->first_reloc
;
666 anv_batch_bo_destroy(struct anv_batch_bo
*bbo
, struct anv_device
*device
)
668 anv_bo_pool_free(&device
->batch_bo_pool
, &bbo
->bo
);
669 anv_device_free(device
, bbo
);
673 anv_batch_emit_dwords(struct anv_batch
*batch
, int num_dwords
)
675 if (batch
->next
+ num_dwords
* 4 > batch
->end
)
676 batch
->extend_cb(batch
, batch
->user_data
);
678 void *p
= batch
->next
;
680 batch
->next
+= num_dwords
* 4;
681 assert(batch
->next
<= batch
->end
);
687 anv_reloc_list_append(struct anv_reloc_list
*list
, struct anv_device
*device
,
688 struct anv_reloc_list
*other
, uint32_t offset
)
690 anv_reloc_list_grow(list
, device
, other
->num_relocs
);
691 /* TODO: Handle failure */
693 memcpy(&list
->relocs
[list
->num_relocs
], &other
->relocs
[0],
694 other
->num_relocs
* sizeof(other
->relocs
[0]));
695 memcpy(&list
->reloc_bos
[list
->num_relocs
], &other
->reloc_bos
[0],
696 other
->num_relocs
* sizeof(other
->reloc_bos
[0]));
698 for (uint32_t i
= 0; i
< other
->num_relocs
; i
++)
699 list
->relocs
[i
+ list
->num_relocs
].offset
+= offset
;
701 list
->num_relocs
+= other
->num_relocs
;
705 anv_reloc_list_add(struct anv_reloc_list
*list
, struct anv_device
*device
,
706 uint32_t offset
, struct anv_bo
*target_bo
, uint32_t delta
)
708 struct drm_i915_gem_relocation_entry
*entry
;
711 anv_reloc_list_grow(list
, device
, 1);
712 /* TODO: Handle failure */
714 /* XXX: Can we use I915_EXEC_HANDLE_LUT? */
715 index
= list
->num_relocs
++;
716 list
->reloc_bos
[index
] = target_bo
;
717 entry
= &list
->relocs
[index
];
718 entry
->target_handle
= target_bo
->gem_handle
;
719 entry
->delta
= delta
;
720 entry
->offset
= offset
;
721 entry
->presumed_offset
= target_bo
->offset
;
722 entry
->read_domains
= 0;
723 entry
->write_domain
= 0;
725 return target_bo
->offset
+ delta
;
729 anv_batch_emit_batch(struct anv_batch
*batch
, struct anv_batch
*other
)
731 uint32_t size
, offset
;
733 size
= other
->next
- other
->start
;
734 assert(size
% 4 == 0);
736 if (batch
->next
+ size
> batch
->end
)
737 batch
->extend_cb(batch
, batch
->user_data
);
739 assert(batch
->next
+ size
<= batch
->end
);
741 memcpy(batch
->next
, other
->start
, size
);
743 offset
= batch
->next
- batch
->start
;
744 anv_reloc_list_append(&batch
->relocs
, batch
->device
,
745 &other
->relocs
, offset
);
751 anv_batch_emit_reloc(struct anv_batch
*batch
,
752 void *location
, struct anv_bo
*bo
, uint32_t delta
)
754 return anv_reloc_list_add(&batch
->relocs
, batch
->device
,
755 location
- batch
->start
, bo
, delta
);
758 VkResult
anv_QueueSubmit(
760 uint32_t cmdBufferCount
,
761 const VkCmdBuffer
* pCmdBuffers
,
764 struct anv_queue
*queue
= (struct anv_queue
*) _queue
;
765 struct anv_device
*device
= queue
->device
;
766 struct anv_fence
*fence
= (struct anv_fence
*) _fence
;
769 for (uint32_t i
= 0; i
< cmdBufferCount
; i
++) {
770 struct anv_cmd_buffer
*cmd_buffer
=
771 (struct anv_cmd_buffer
*) pCmdBuffers
[i
];
773 if (device
->dump_aub
)
774 anv_cmd_buffer_dump(cmd_buffer
);
776 if (!device
->no_hw
) {
777 ret
= anv_gem_execbuffer(device
, &cmd_buffer
->execbuf
);
779 return vk_error(VK_ERROR_UNKNOWN
);
782 ret
= anv_gem_execbuffer(device
, &fence
->execbuf
);
784 return vk_error(VK_ERROR_UNKNOWN
);
787 for (uint32_t i
= 0; i
< cmd_buffer
->bo_count
; i
++)
788 cmd_buffer
->exec2_bos
[i
]->offset
= cmd_buffer
->exec2_objects
[i
].offset
;
790 *(uint32_t *)queue
->completed_serial
.map
= cmd_buffer
->serial
;
797 VkResult
anv_QueueAddMemReferences(
800 const VkDeviceMemory
* pMems
)
805 VkResult
anv_QueueRemoveMemReferences(
808 const VkDeviceMemory
* pMems
)
813 VkResult
anv_QueueWaitIdle(
816 struct anv_queue
*queue
= (struct anv_queue
*) _queue
;
818 return vkDeviceWaitIdle((VkDevice
) queue
->device
);
821 VkResult
anv_DeviceWaitIdle(
824 struct anv_device
*device
= (struct anv_device
*) _device
;
825 struct anv_state state
;
826 struct anv_batch batch
;
827 struct drm_i915_gem_execbuffer2 execbuf
;
828 struct drm_i915_gem_exec_object2 exec2_objects
[1];
829 struct anv_bo
*bo
= NULL
;
834 state
= anv_state_pool_alloc(&device
->dynamic_state_pool
, 32, 32);
835 bo
= &device
->dynamic_state_pool
.block_pool
->bo
;
836 batch
.start
= batch
.next
= state
.map
;
837 batch
.end
= state
.map
+ 32;
838 anv_batch_emit(&batch
, GEN8_MI_BATCH_BUFFER_END
);
839 anv_batch_emit(&batch
, GEN8_MI_NOOP
);
841 exec2_objects
[0].handle
= bo
->gem_handle
;
842 exec2_objects
[0].relocation_count
= 0;
843 exec2_objects
[0].relocs_ptr
= 0;
844 exec2_objects
[0].alignment
= 0;
845 exec2_objects
[0].offset
= bo
->offset
;
846 exec2_objects
[0].flags
= 0;
847 exec2_objects
[0].rsvd1
= 0;
848 exec2_objects
[0].rsvd2
= 0;
850 execbuf
.buffers_ptr
= (uintptr_t) exec2_objects
;
851 execbuf
.buffer_count
= 1;
852 execbuf
.batch_start_offset
= state
.offset
;
853 execbuf
.batch_len
= batch
.next
- state
.map
;
854 execbuf
.cliprects_ptr
= 0;
855 execbuf
.num_cliprects
= 0;
860 I915_EXEC_HANDLE_LUT
| I915_EXEC_NO_RELOC
| I915_EXEC_RENDER
;
861 execbuf
.rsvd1
= device
->context_id
;
864 if (!device
->no_hw
) {
865 ret
= anv_gem_execbuffer(device
, &execbuf
);
867 result
= vk_error(VK_ERROR_UNKNOWN
);
872 ret
= anv_gem_wait(device
, bo
->gem_handle
, &timeout
);
874 result
= vk_error(VK_ERROR_UNKNOWN
);
879 anv_state_pool_free(&device
->dynamic_state_pool
, state
);
884 anv_state_pool_free(&device
->dynamic_state_pool
, state
);
890 anv_device_alloc(struct anv_device
* device
,
893 VkSystemAllocType allocType
)
895 return device
->instance
->pfnAlloc(device
->instance
->pAllocUserData
,
902 anv_device_free(struct anv_device
* device
,
905 return device
->instance
->pfnFree(device
->instance
->pAllocUserData
,
910 anv_bo_init_new(struct anv_bo
*bo
, struct anv_device
*device
, uint64_t size
)
912 bo
->gem_handle
= anv_gem_create(device
, size
);
914 return vk_error(VK_ERROR_OUT_OF_DEVICE_MEMORY
);
924 VkResult
anv_AllocMemory(
926 const VkMemoryAllocInfo
* pAllocInfo
,
927 VkDeviceMemory
* pMem
)
929 struct anv_device
*device
= (struct anv_device
*) _device
;
930 struct anv_device_memory
*mem
;
933 assert(pAllocInfo
->sType
== VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO
);
935 mem
= anv_device_alloc(device
, sizeof(*mem
), 8,
936 VK_SYSTEM_ALLOC_TYPE_API_OBJECT
);
938 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
940 result
= anv_bo_init_new(&mem
->bo
, device
, pAllocInfo
->allocationSize
);
941 if (result
!= VK_SUCCESS
)
944 *pMem
= (VkDeviceMemory
) mem
;
949 anv_device_free(device
, mem
);
954 VkResult
anv_FreeMemory(
958 struct anv_device
*device
= (struct anv_device
*) _device
;
959 struct anv_device_memory
*mem
= (struct anv_device_memory
*) _mem
;
962 anv_gem_munmap(mem
->bo
.map
, mem
->bo
.size
);
964 if (mem
->bo
.gem_handle
!= 0)
965 anv_gem_close(device
, mem
->bo
.gem_handle
);
967 anv_device_free(device
, mem
);
972 VkResult
anv_SetMemoryPriority(
975 VkMemoryPriority priority
)
980 VkResult
anv_MapMemory(
985 VkMemoryMapFlags flags
,
988 struct anv_device
*device
= (struct anv_device
*) _device
;
989 struct anv_device_memory
*mem
= (struct anv_device_memory
*) _mem
;
991 /* FIXME: Is this supposed to be thread safe? Since vkUnmapMemory() only
992 * takes a VkDeviceMemory pointer, it seems like only one map of the memory
993 * at a time is valid. We could just mmap up front and return an offset
994 * pointer here, but that may exhaust virtual memory on 32 bit
997 mem
->map
= anv_gem_mmap(device
, mem
->bo
.gem_handle
, offset
, size
);
998 mem
->map_size
= size
;
1005 VkResult
anv_UnmapMemory(
1007 VkDeviceMemory _mem
)
1009 struct anv_device_memory
*mem
= (struct anv_device_memory
*) _mem
;
1011 anv_gem_munmap(mem
->map
, mem
->map_size
);
1016 VkResult
anv_FlushMappedMemory(
1019 VkDeviceSize offset
,
1022 /* clflush here for !llc platforms */
1027 VkResult
anv_PinSystemMemory(
1029 const void* pSysMem
,
1031 VkDeviceMemory
* pMem
)
1036 VkResult
anv_GetMultiDeviceCompatibility(
1037 VkPhysicalDevice physicalDevice0
,
1038 VkPhysicalDevice physicalDevice1
,
1039 VkPhysicalDeviceCompatibilityInfo
* pInfo
)
1041 return VK_UNSUPPORTED
;
1044 VkResult
anv_OpenSharedMemory(
1046 const VkMemoryOpenInfo
* pOpenInfo
,
1047 VkDeviceMemory
* pMem
)
1049 return VK_UNSUPPORTED
;
1052 VkResult
anv_OpenSharedSemaphore(
1054 const VkSemaphoreOpenInfo
* pOpenInfo
,
1055 VkSemaphore
* pSemaphore
)
1057 return VK_UNSUPPORTED
;
1060 VkResult
anv_OpenPeerMemory(
1062 const VkPeerMemoryOpenInfo
* pOpenInfo
,
1063 VkDeviceMemory
* pMem
)
1065 return VK_UNSUPPORTED
;
1068 VkResult
anv_OpenPeerImage(
1070 const VkPeerImageOpenInfo
* pOpenInfo
,
1072 VkDeviceMemory
* pMem
)
1074 return VK_UNSUPPORTED
;
1077 VkResult
anv_DestroyObject(
1079 VkObjectType objType
,
1082 struct anv_device
*device
= (struct anv_device
*) _device
;
1083 struct anv_object
*object
= (struct anv_object
*) _object
;
1086 case VK_OBJECT_TYPE_INSTANCE
:
1087 return anv_DestroyInstance((VkInstance
) _object
);
1089 case VK_OBJECT_TYPE_PHYSICAL_DEVICE
:
1090 /* We don't want to actually destroy physical devices */
1093 case VK_OBJECT_TYPE_DEVICE
:
1094 assert(_device
== (VkDevice
) _object
);
1095 return anv_DestroyDevice((VkDevice
) _object
);
1097 case VK_OBJECT_TYPE_QUEUE
:
1101 case VK_OBJECT_TYPE_DEVICE_MEMORY
:
1102 return anv_FreeMemory(_device
, (VkDeviceMemory
) _object
);
1104 case VK_OBJECT_TYPE_DESCRIPTOR_POOL
:
1105 /* These are just dummys anyway, so we don't need to destroy them */
1108 case VK_OBJECT_TYPE_BUFFER
:
1109 case VK_OBJECT_TYPE_IMAGE
:
1110 case VK_OBJECT_TYPE_DEPTH_STENCIL_VIEW
:
1111 case VK_OBJECT_TYPE_SHADER
:
1112 case VK_OBJECT_TYPE_PIPELINE_LAYOUT
:
1113 case VK_OBJECT_TYPE_SAMPLER
:
1114 case VK_OBJECT_TYPE_DESCRIPTOR_SET
:
1115 case VK_OBJECT_TYPE_DESCRIPTOR_SET_LAYOUT
:
1116 case VK_OBJECT_TYPE_DYNAMIC_RS_STATE
:
1117 case VK_OBJECT_TYPE_DYNAMIC_CB_STATE
:
1118 case VK_OBJECT_TYPE_DYNAMIC_DS_STATE
:
1119 case VK_OBJECT_TYPE_RENDER_PASS
:
1120 /* These are trivially destroyable */
1121 anv_device_free(device
, (void *) _object
);
1124 case VK_OBJECT_TYPE_COMMAND_BUFFER
:
1125 case VK_OBJECT_TYPE_PIPELINE
:
1126 case VK_OBJECT_TYPE_DYNAMIC_VP_STATE
:
1127 case VK_OBJECT_TYPE_FENCE
:
1128 case VK_OBJECT_TYPE_QUERY_POOL
:
1129 case VK_OBJECT_TYPE_FRAMEBUFFER
:
1130 case VK_OBJECT_TYPE_BUFFER_VIEW
:
1131 case VK_OBJECT_TYPE_IMAGE_VIEW
:
1132 case VK_OBJECT_TYPE_COLOR_ATTACHMENT_VIEW
:
1133 (object
->destructor
)(device
, object
, objType
);
1136 case VK_OBJECT_TYPE_SEMAPHORE
:
1137 case VK_OBJECT_TYPE_EVENT
:
1138 stub_return(VK_UNSUPPORTED
);
1141 unreachable("Invalid object type");
1146 fill_memory_requirements(
1147 VkObjectType objType
,
1149 VkMemoryRequirements
* memory_requirements
)
1151 struct anv_buffer
*buffer
;
1152 struct anv_image
*image
;
1154 memory_requirements
->memPropsAllowed
=
1155 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT
|
1156 VK_MEMORY_PROPERTY_HOST_DEVICE_COHERENT_BIT
|
1157 /* VK_MEMORY_PROPERTY_HOST_UNCACHED_BIT | */
1158 VK_MEMORY_PROPERTY_HOST_WRITE_COMBINED_BIT
|
1159 VK_MEMORY_PROPERTY_PREFER_HOST_LOCAL
|
1160 VK_MEMORY_PROPERTY_SHAREABLE_BIT
;
1162 memory_requirements
->memPropsRequired
= 0;
1165 case VK_OBJECT_TYPE_BUFFER
:
1166 buffer
= (struct anv_buffer
*) object
;
1167 memory_requirements
->size
= buffer
->size
;
1168 memory_requirements
->alignment
= 16;
1170 case VK_OBJECT_TYPE_IMAGE
:
1171 image
= (struct anv_image
*) object
;
1172 memory_requirements
->size
= image
->size
;
1173 memory_requirements
->alignment
= image
->alignment
;
1176 memory_requirements
->size
= 0;
1182 get_allocation_count(VkObjectType objType
)
1185 case VK_OBJECT_TYPE_BUFFER
:
1186 case VK_OBJECT_TYPE_IMAGE
:
1193 VkResult
anv_GetObjectInfo(
1195 VkObjectType objType
,
1197 VkObjectInfoType infoType
,
1201 VkMemoryRequirements memory_requirements
;
1205 case VK_OBJECT_INFO_TYPE_MEMORY_REQUIREMENTS
:
1206 *pDataSize
= sizeof(memory_requirements
);
1210 fill_memory_requirements(objType
, object
, pData
);
1213 case VK_OBJECT_INFO_TYPE_MEMORY_ALLOCATION_COUNT
:
1214 *pDataSize
= sizeof(count
);
1219 *count
= get_allocation_count(objType
);
1223 return VK_UNSUPPORTED
;
1228 VkResult
anv_QueueBindObjectMemory(
1230 VkObjectType objType
,
1232 uint32_t allocationIdx
,
1233 VkDeviceMemory _mem
,
1234 VkDeviceSize memOffset
)
1236 struct anv_buffer
*buffer
;
1237 struct anv_image
*image
;
1238 struct anv_device_memory
*mem
= (struct anv_device_memory
*) _mem
;
1241 case VK_OBJECT_TYPE_BUFFER
:
1242 buffer
= (struct anv_buffer
*) object
;
1243 buffer
->bo
= &mem
->bo
;
1244 buffer
->offset
= memOffset
;
1246 case VK_OBJECT_TYPE_IMAGE
:
1247 image
= (struct anv_image
*) object
;
1248 image
->bo
= &mem
->bo
;
1249 image
->offset
= memOffset
;
1258 VkResult
anv_QueueBindObjectMemoryRange(
1260 VkObjectType objType
,
1262 uint32_t allocationIdx
,
1263 VkDeviceSize rangeOffset
,
1264 VkDeviceSize rangeSize
,
1266 VkDeviceSize memOffset
)
1268 stub_return(VK_UNSUPPORTED
);
1271 VkResult
anv_QueueBindImageMemoryRange(
1274 uint32_t allocationIdx
,
1275 const VkImageMemoryBindInfo
* pBindInfo
,
1277 VkDeviceSize memOffset
)
1279 stub_return(VK_UNSUPPORTED
);
1283 anv_fence_destroy(struct anv_device
*device
,
1284 struct anv_object
*object
,
1285 VkObjectType obj_type
)
1287 struct anv_fence
*fence
= (struct anv_fence
*) object
;
1289 assert(obj_type
== VK_OBJECT_TYPE_FENCE
);
1291 anv_gem_munmap(fence
->bo
.map
, fence
->bo
.size
);
1292 anv_gem_close(device
, fence
->bo
.gem_handle
);
1293 anv_device_free(device
, fence
);
1296 VkResult
anv_CreateFence(
1298 const VkFenceCreateInfo
* pCreateInfo
,
1301 struct anv_device
*device
= (struct anv_device
*) _device
;
1302 struct anv_fence
*fence
;
1303 struct anv_batch batch
;
1306 const uint32_t fence_size
= 128;
1308 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_FENCE_CREATE_INFO
);
1310 fence
= anv_device_alloc(device
, sizeof(*fence
), 8,
1311 VK_SYSTEM_ALLOC_TYPE_API_OBJECT
);
1313 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
1315 result
= anv_bo_init_new(&fence
->bo
, device
, fence_size
);
1316 if (result
!= VK_SUCCESS
)
1319 fence
->base
.destructor
= anv_fence_destroy
;
1322 anv_gem_mmap(device
, fence
->bo
.gem_handle
, 0, fence
->bo
.size
);
1323 batch
.next
= batch
.start
= fence
->bo
.map
;
1324 batch
.end
= fence
->bo
.map
+ fence
->bo
.size
;
1325 anv_batch_emit(&batch
, GEN8_MI_BATCH_BUFFER_END
);
1326 anv_batch_emit(&batch
, GEN8_MI_NOOP
);
1328 fence
->exec2_objects
[0].handle
= fence
->bo
.gem_handle
;
1329 fence
->exec2_objects
[0].relocation_count
= 0;
1330 fence
->exec2_objects
[0].relocs_ptr
= 0;
1331 fence
->exec2_objects
[0].alignment
= 0;
1332 fence
->exec2_objects
[0].offset
= fence
->bo
.offset
;
1333 fence
->exec2_objects
[0].flags
= 0;
1334 fence
->exec2_objects
[0].rsvd1
= 0;
1335 fence
->exec2_objects
[0].rsvd2
= 0;
1337 fence
->execbuf
.buffers_ptr
= (uintptr_t) fence
->exec2_objects
;
1338 fence
->execbuf
.buffer_count
= 1;
1339 fence
->execbuf
.batch_start_offset
= 0;
1340 fence
->execbuf
.batch_len
= batch
.next
- fence
->bo
.map
;
1341 fence
->execbuf
.cliprects_ptr
= 0;
1342 fence
->execbuf
.num_cliprects
= 0;
1343 fence
->execbuf
.DR1
= 0;
1344 fence
->execbuf
.DR4
= 0;
1346 fence
->execbuf
.flags
=
1347 I915_EXEC_HANDLE_LUT
| I915_EXEC_NO_RELOC
| I915_EXEC_RENDER
;
1348 fence
->execbuf
.rsvd1
= device
->context_id
;
1349 fence
->execbuf
.rsvd2
= 0;
1351 *pFence
= (VkFence
) fence
;
1356 anv_device_free(device
, fence
);
1361 VkResult
anv_ResetFences(
1363 uint32_t fenceCount
,
1366 struct anv_fence
**fences
= (struct anv_fence
**) pFences
;
1368 for (uint32_t i
= 0; i
< fenceCount
; i
++)
1369 fences
[i
]->ready
= false;
1374 VkResult
anv_GetFenceStatus(
1378 struct anv_device
*device
= (struct anv_device
*) _device
;
1379 struct anv_fence
*fence
= (struct anv_fence
*) _fence
;
1386 ret
= anv_gem_wait(device
, fence
->bo
.gem_handle
, &t
);
1388 fence
->ready
= true;
1392 return VK_NOT_READY
;
1395 VkResult
anv_WaitForFences(
1397 uint32_t fenceCount
,
1398 const VkFence
* pFences
,
1402 struct anv_device
*device
= (struct anv_device
*) _device
;
1403 struct anv_fence
**fences
= (struct anv_fence
**) pFences
;
1404 int64_t t
= timeout
;
1407 /* FIXME: handle !waitAll */
1409 for (uint32_t i
= 0; i
< fenceCount
; i
++) {
1410 ret
= anv_gem_wait(device
, fences
[i
]->bo
.gem_handle
, &t
);
1411 if (ret
== -1 && errno
== ETIME
)
1414 return vk_error(VK_ERROR_UNKNOWN
);
1420 // Queue semaphore functions
1422 VkResult
anv_CreateSemaphore(
1424 const VkSemaphoreCreateInfo
* pCreateInfo
,
1425 VkSemaphore
* pSemaphore
)
1427 stub_return(VK_UNSUPPORTED
);
1430 VkResult
anv_QueueSignalSemaphore(
1432 VkSemaphore semaphore
)
1434 stub_return(VK_UNSUPPORTED
);
1437 VkResult
anv_QueueWaitSemaphore(
1439 VkSemaphore semaphore
)
1441 stub_return(VK_UNSUPPORTED
);
1446 VkResult
anv_CreateEvent(
1448 const VkEventCreateInfo
* pCreateInfo
,
1451 stub_return(VK_UNSUPPORTED
);
1454 VkResult
anv_GetEventStatus(
1458 stub_return(VK_UNSUPPORTED
);
1461 VkResult
anv_SetEvent(
1465 stub_return(VK_UNSUPPORTED
);
1468 VkResult
anv_ResetEvent(
1472 stub_return(VK_UNSUPPORTED
);
1477 VkResult
anv_CreateBuffer(
1479 const VkBufferCreateInfo
* pCreateInfo
,
1482 struct anv_device
*device
= (struct anv_device
*) _device
;
1483 struct anv_buffer
*buffer
;
1485 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO
);
1487 buffer
= anv_device_alloc(device
, sizeof(*buffer
), 8,
1488 VK_SYSTEM_ALLOC_TYPE_API_OBJECT
);
1490 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
1492 buffer
->size
= pCreateInfo
->size
;
1496 *pBuffer
= (VkBuffer
) buffer
;
1501 // Buffer view functions
1504 fill_buffer_surface_state(void *state
, VkFormat format
,
1505 uint32_t offset
, uint32_t range
)
1507 const struct anv_format
*info
;
1509 info
= anv_format_for_vk_format(format
);
1510 /* This assumes RGBA float format. */
1511 uint32_t stride
= 4;
1512 uint32_t num_elements
= range
/ stride
;
1514 struct GEN8_RENDER_SURFACE_STATE surface_state
= {
1515 .SurfaceType
= SURFTYPE_BUFFER
,
1516 .SurfaceArray
= false,
1517 .SurfaceFormat
= info
->format
,
1518 .SurfaceVerticalAlignment
= VALIGN4
,
1519 .SurfaceHorizontalAlignment
= HALIGN4
,
1521 .VerticalLineStride
= 0,
1522 .VerticalLineStrideOffset
= 0,
1523 .SamplerL2BypassModeDisable
= true,
1524 .RenderCacheReadWriteMode
= WriteOnlyCache
,
1525 .MemoryObjectControlState
= GEN8_MOCS
,
1528 .Height
= (num_elements
>> 7) & 0x3fff,
1529 .Width
= num_elements
& 0x7f,
1530 .Depth
= (num_elements
>> 21) & 0x3f,
1531 .SurfacePitch
= stride
- 1,
1532 .MinimumArrayElement
= 0,
1533 .NumberofMultisamples
= MULTISAMPLECOUNT_1
,
1538 .AuxiliarySurfaceMode
= AUX_NONE
,
1540 .GreenClearColor
= 0,
1541 .BlueClearColor
= 0,
1542 .AlphaClearColor
= 0,
1543 .ShaderChannelSelectRed
= SCS_RED
,
1544 .ShaderChannelSelectGreen
= SCS_GREEN
,
1545 .ShaderChannelSelectBlue
= SCS_BLUE
,
1546 .ShaderChannelSelectAlpha
= SCS_ALPHA
,
1547 .ResourceMinLOD
= 0,
1548 /* FIXME: We assume that the image must be bound at this time. */
1549 .SurfaceBaseAddress
= { NULL
, offset
},
1552 GEN8_RENDER_SURFACE_STATE_pack(NULL
, state
, &surface_state
);
1555 VkResult
anv_CreateBufferView(
1557 const VkBufferViewCreateInfo
* pCreateInfo
,
1558 VkBufferView
* pView
)
1560 struct anv_device
*device
= (struct anv_device
*) _device
;
1561 struct anv_buffer
*buffer
= (struct anv_buffer
*) pCreateInfo
->buffer
;
1562 struct anv_surface_view
*view
;
1564 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO
);
1566 view
= anv_device_alloc(device
, sizeof(*view
), 8,
1567 VK_SYSTEM_ALLOC_TYPE_API_OBJECT
);
1569 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
1571 view
->base
.destructor
= anv_surface_view_destroy
;
1573 view
->bo
= buffer
->bo
;
1574 view
->offset
= buffer
->offset
+ pCreateInfo
->offset
;
1575 view
->surface_state
=
1576 anv_state_pool_alloc(&device
->surface_state_pool
, 64, 64);
1577 view
->format
= pCreateInfo
->format
;
1578 view
->range
= pCreateInfo
->range
;
1580 fill_buffer_surface_state(view
->surface_state
.map
,
1581 pCreateInfo
->format
, view
->offset
, pCreateInfo
->range
);
1583 *pView
= (VkBufferView
) view
;
1588 // Sampler functions
1590 VkResult
anv_CreateSampler(
1592 const VkSamplerCreateInfo
* pCreateInfo
,
1593 VkSampler
* pSampler
)
1595 struct anv_device
*device
= (struct anv_device
*) _device
;
1596 struct anv_sampler
*sampler
;
1597 uint32_t mag_filter
, min_filter
, max_anisotropy
;
1599 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO
);
1601 sampler
= anv_device_alloc(device
, sizeof(*sampler
), 8,
1602 VK_SYSTEM_ALLOC_TYPE_API_OBJECT
);
1604 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
1606 static const uint32_t vk_to_gen_tex_filter
[] = {
1607 [VK_TEX_FILTER_NEAREST
] = MAPFILTER_NEAREST
,
1608 [VK_TEX_FILTER_LINEAR
] = MAPFILTER_LINEAR
1611 static const uint32_t vk_to_gen_mipmap_mode
[] = {
1612 [VK_TEX_MIPMAP_MODE_BASE
] = MIPFILTER_NONE
,
1613 [VK_TEX_MIPMAP_MODE_NEAREST
] = MIPFILTER_NEAREST
,
1614 [VK_TEX_MIPMAP_MODE_LINEAR
] = MIPFILTER_LINEAR
1617 static const uint32_t vk_to_gen_tex_address
[] = {
1618 [VK_TEX_ADDRESS_WRAP
] = TCM_WRAP
,
1619 [VK_TEX_ADDRESS_MIRROR
] = TCM_MIRROR
,
1620 [VK_TEX_ADDRESS_CLAMP
] = TCM_CLAMP
,
1621 [VK_TEX_ADDRESS_MIRROR_ONCE
] = TCM_MIRROR_ONCE
,
1622 [VK_TEX_ADDRESS_CLAMP_BORDER
] = TCM_CLAMP_BORDER
,
1625 static const uint32_t vk_to_gen_compare_op
[] = {
1626 [VK_COMPARE_OP_NEVER
] = PREFILTEROPNEVER
,
1627 [VK_COMPARE_OP_LESS
] = PREFILTEROPLESS
,
1628 [VK_COMPARE_OP_EQUAL
] = PREFILTEROPEQUAL
,
1629 [VK_COMPARE_OP_LESS_EQUAL
] = PREFILTEROPLEQUAL
,
1630 [VK_COMPARE_OP_GREATER
] = PREFILTEROPGREATER
,
1631 [VK_COMPARE_OP_NOT_EQUAL
] = PREFILTEROPNOTEQUAL
,
1632 [VK_COMPARE_OP_GREATER_EQUAL
] = PREFILTEROPGEQUAL
,
1633 [VK_COMPARE_OP_ALWAYS
] = PREFILTEROPALWAYS
,
1636 if (pCreateInfo
->maxAnisotropy
> 1) {
1637 mag_filter
= MAPFILTER_ANISOTROPIC
;
1638 min_filter
= MAPFILTER_ANISOTROPIC
;
1639 max_anisotropy
= (pCreateInfo
->maxAnisotropy
- 2) / 2;
1641 mag_filter
= vk_to_gen_tex_filter
[pCreateInfo
->magFilter
];
1642 min_filter
= vk_to_gen_tex_filter
[pCreateInfo
->minFilter
];
1643 max_anisotropy
= RATIO21
;
1646 struct GEN8_SAMPLER_STATE sampler_state
= {
1647 .SamplerDisable
= false,
1648 .TextureBorderColorMode
= DX10OGL
,
1649 .LODPreClampMode
= 0,
1651 .MipModeFilter
= vk_to_gen_mipmap_mode
[pCreateInfo
->mipMode
],
1652 .MagModeFilter
= mag_filter
,
1653 .MinModeFilter
= min_filter
,
1654 .TextureLODBias
= pCreateInfo
->mipLodBias
* 256,
1655 .AnisotropicAlgorithm
= EWAApproximation
,
1656 .MinLOD
= pCreateInfo
->minLod
* 256,
1657 .MaxLOD
= pCreateInfo
->maxLod
* 256,
1658 .ChromaKeyEnable
= 0,
1659 .ChromaKeyIndex
= 0,
1661 .ShadowFunction
= vk_to_gen_compare_op
[pCreateInfo
->compareOp
],
1662 .CubeSurfaceControlMode
= 0,
1664 .IndirectStatePointer
=
1665 device
->float_border_colors
.offset
+
1666 pCreateInfo
->borderColor
* sizeof(float) * 4,
1668 .LODClampMagnificationMode
= MIPNONE
,
1669 .MaximumAnisotropy
= max_anisotropy
,
1670 .RAddressMinFilterRoundingEnable
= 0,
1671 .RAddressMagFilterRoundingEnable
= 0,
1672 .VAddressMinFilterRoundingEnable
= 0,
1673 .VAddressMagFilterRoundingEnable
= 0,
1674 .UAddressMinFilterRoundingEnable
= 0,
1675 .UAddressMagFilterRoundingEnable
= 0,
1676 .TrilinearFilterQuality
= 0,
1677 .NonnormalizedCoordinateEnable
= 0,
1678 .TCXAddressControlMode
= vk_to_gen_tex_address
[pCreateInfo
->addressU
],
1679 .TCYAddressControlMode
= vk_to_gen_tex_address
[pCreateInfo
->addressV
],
1680 .TCZAddressControlMode
= vk_to_gen_tex_address
[pCreateInfo
->addressW
],
1683 GEN8_SAMPLER_STATE_pack(NULL
, sampler
->state
, &sampler_state
);
1685 *pSampler
= (VkSampler
) sampler
;
1690 // Descriptor set functions
1692 VkResult
anv_CreateDescriptorSetLayout(
1694 const VkDescriptorSetLayoutCreateInfo
* pCreateInfo
,
1695 VkDescriptorSetLayout
* pSetLayout
)
1697 struct anv_device
*device
= (struct anv_device
*) _device
;
1698 struct anv_descriptor_set_layout
*set_layout
;
1700 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO
);
1702 uint32_t sampler_count
[VK_NUM_SHADER_STAGE
] = { 0, };
1703 uint32_t surface_count
[VK_NUM_SHADER_STAGE
] = { 0, };
1704 uint32_t num_dynamic_buffers
= 0;
1706 uint32_t stages
= 0;
1709 for (uint32_t i
= 0; i
< pCreateInfo
->count
; i
++) {
1710 switch (pCreateInfo
->pBinding
[i
].descriptorType
) {
1711 case VK_DESCRIPTOR_TYPE_SAMPLER
:
1712 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
:
1713 for_each_bit(s
, pCreateInfo
->pBinding
[i
].stageFlags
)
1714 sampler_count
[s
] += pCreateInfo
->pBinding
[i
].count
;
1720 switch (pCreateInfo
->pBinding
[i
].descriptorType
) {
1721 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
:
1722 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE
:
1723 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE
:
1724 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER
:
1725 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER
:
1726 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER
:
1727 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER
:
1728 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
:
1729 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
:
1730 for_each_bit(s
, pCreateInfo
->pBinding
[i
].stageFlags
)
1731 surface_count
[s
] += pCreateInfo
->pBinding
[i
].count
;
1737 switch (pCreateInfo
->pBinding
[i
].descriptorType
) {
1738 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
:
1739 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
:
1740 num_dynamic_buffers
+= pCreateInfo
->pBinding
[i
].count
;
1746 stages
|= pCreateInfo
->pBinding
[i
].stageFlags
;
1747 count
+= pCreateInfo
->pBinding
[i
].count
;
1750 uint32_t sampler_total
= 0;
1751 uint32_t surface_total
= 0;
1752 for (uint32_t s
= 0; s
< VK_NUM_SHADER_STAGE
; s
++) {
1753 sampler_total
+= sampler_count
[s
];
1754 surface_total
+= surface_count
[s
];
1757 size_t size
= sizeof(*set_layout
) +
1758 (sampler_total
+ surface_total
) * sizeof(set_layout
->entries
[0]);
1759 set_layout
= anv_device_alloc(device
, size
, 8,
1760 VK_SYSTEM_ALLOC_TYPE_API_OBJECT
);
1762 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
1764 set_layout
->num_dynamic_buffers
= num_dynamic_buffers
;
1765 set_layout
->count
= count
;
1766 set_layout
->shader_stages
= stages
;
1768 struct anv_descriptor_slot
*p
= set_layout
->entries
;
1769 struct anv_descriptor_slot
*sampler
[VK_NUM_SHADER_STAGE
];
1770 struct anv_descriptor_slot
*surface
[VK_NUM_SHADER_STAGE
];
1771 for (uint32_t s
= 0; s
< VK_NUM_SHADER_STAGE
; s
++) {
1772 set_layout
->stage
[s
].surface_count
= surface_count
[s
];
1773 set_layout
->stage
[s
].surface_start
= surface
[s
] = p
;
1774 p
+= surface_count
[s
];
1775 set_layout
->stage
[s
].sampler_count
= sampler_count
[s
];
1776 set_layout
->stage
[s
].sampler_start
= sampler
[s
] = p
;
1777 p
+= sampler_count
[s
];
1780 uint32_t descriptor
= 0;
1781 int8_t dynamic_slot
= 0;
1783 for (uint32_t i
= 0; i
< pCreateInfo
->count
; i
++) {
1784 switch (pCreateInfo
->pBinding
[i
].descriptorType
) {
1785 case VK_DESCRIPTOR_TYPE_SAMPLER
:
1786 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
:
1787 for_each_bit(s
, pCreateInfo
->pBinding
[i
].stageFlags
)
1788 for (uint32_t j
= 0; j
< pCreateInfo
->pBinding
[i
].count
; j
++) {
1789 sampler
[s
]->index
= descriptor
+ j
;
1790 sampler
[s
]->dynamic_slot
= -1;
1798 switch (pCreateInfo
->pBinding
[i
].descriptorType
) {
1799 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
:
1800 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
:
1808 switch (pCreateInfo
->pBinding
[i
].descriptorType
) {
1809 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
:
1810 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE
:
1811 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE
:
1812 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER
:
1813 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER
:
1814 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER
:
1815 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER
:
1816 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
:
1817 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
:
1818 for_each_bit(s
, pCreateInfo
->pBinding
[i
].stageFlags
)
1819 for (uint32_t j
= 0; j
< pCreateInfo
->pBinding
[i
].count
; j
++) {
1820 surface
[s
]->index
= descriptor
+ j
;
1822 surface
[s
]->dynamic_slot
= dynamic_slot
+ j
;
1824 surface
[s
]->dynamic_slot
= -1;
1833 dynamic_slot
+= pCreateInfo
->pBinding
[i
].count
;
1835 descriptor
+= pCreateInfo
->pBinding
[i
].count
;
1838 *pSetLayout
= (VkDescriptorSetLayout
) set_layout
;
1843 VkResult
anv_BeginDescriptorPoolUpdate(
1845 VkDescriptorUpdateMode updateMode
)
1850 VkResult
anv_EndDescriptorPoolUpdate(
1857 VkResult
anv_CreateDescriptorPool(
1859 VkDescriptorPoolUsage poolUsage
,
1861 const VkDescriptorPoolCreateInfo
* pCreateInfo
,
1862 VkDescriptorPool
* pDescriptorPool
)
1864 *pDescriptorPool
= 1;
1869 VkResult
anv_ResetDescriptorPool(
1871 VkDescriptorPool descriptorPool
)
1876 VkResult
anv_AllocDescriptorSets(
1878 VkDescriptorPool descriptorPool
,
1879 VkDescriptorSetUsage setUsage
,
1881 const VkDescriptorSetLayout
* pSetLayouts
,
1882 VkDescriptorSet
* pDescriptorSets
,
1885 struct anv_device
*device
= (struct anv_device
*) _device
;
1886 const struct anv_descriptor_set_layout
*layout
;
1887 struct anv_descriptor_set
*set
;
1890 for (uint32_t i
= 0; i
< count
; i
++) {
1891 layout
= (struct anv_descriptor_set_layout
*) pSetLayouts
[i
];
1892 size
= sizeof(*set
) + layout
->count
* sizeof(set
->descriptors
[0]);
1893 set
= anv_device_alloc(device
, size
, 8,
1894 VK_SYSTEM_ALLOC_TYPE_API_OBJECT
);
1897 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
1900 /* Descriptor sets may not be 100% filled out so we need to memset to
1901 * ensure that we can properly detect and handle holes.
1903 memset(set
, 0, size
);
1905 pDescriptorSets
[i
] = (VkDescriptorSet
) set
;
1913 void anv_ClearDescriptorSets(
1915 VkDescriptorPool descriptorPool
,
1917 const VkDescriptorSet
* pDescriptorSets
)
1921 void anv_UpdateDescriptors(
1923 VkDescriptorSet descriptorSet
,
1924 uint32_t updateCount
,
1925 const void** ppUpdateArray
)
1927 struct anv_descriptor_set
*set
= (struct anv_descriptor_set
*) descriptorSet
;
1928 VkUpdateSamplers
*update_samplers
;
1929 VkUpdateSamplerTextures
*update_sampler_textures
;
1930 VkUpdateImages
*update_images
;
1931 VkUpdateBuffers
*update_buffers
;
1932 VkUpdateAsCopy
*update_as_copy
;
1934 for (uint32_t i
= 0; i
< updateCount
; i
++) {
1935 const struct anv_common
*common
= ppUpdateArray
[i
];
1937 switch (common
->sType
) {
1938 case VK_STRUCTURE_TYPE_UPDATE_SAMPLERS
:
1939 update_samplers
= (VkUpdateSamplers
*) common
;
1941 for (uint32_t j
= 0; j
< update_samplers
->count
; j
++) {
1942 set
->descriptors
[update_samplers
->binding
+ j
].sampler
=
1943 (struct anv_sampler
*) update_samplers
->pSamplers
[j
];
1947 case VK_STRUCTURE_TYPE_UPDATE_SAMPLER_TEXTURES
:
1948 /* FIXME: Shouldn't this be *_UPDATE_SAMPLER_IMAGES? */
1949 update_sampler_textures
= (VkUpdateSamplerTextures
*) common
;
1951 for (uint32_t j
= 0; j
< update_sampler_textures
->count
; j
++) {
1952 set
->descriptors
[update_sampler_textures
->binding
+ j
].view
=
1953 (struct anv_surface_view
*)
1954 update_sampler_textures
->pSamplerImageViews
[j
].pImageView
->view
;
1955 set
->descriptors
[update_sampler_textures
->binding
+ j
].sampler
=
1956 (struct anv_sampler
*)
1957 update_sampler_textures
->pSamplerImageViews
[j
].sampler
;
1961 case VK_STRUCTURE_TYPE_UPDATE_IMAGES
:
1962 update_images
= (VkUpdateImages
*) common
;
1964 for (uint32_t j
= 0; j
< update_images
->count
; j
++) {
1965 set
->descriptors
[update_images
->binding
+ j
].view
=
1966 (struct anv_surface_view
*) update_images
->pImageViews
[j
].view
;
1970 case VK_STRUCTURE_TYPE_UPDATE_BUFFERS
:
1971 update_buffers
= (VkUpdateBuffers
*) common
;
1973 for (uint32_t j
= 0; j
< update_buffers
->count
; j
++) {
1974 set
->descriptors
[update_buffers
->binding
+ j
].view
=
1975 (struct anv_surface_view
*) update_buffers
->pBufferViews
[j
].view
;
1977 /* FIXME: descriptor arrays? */
1980 case VK_STRUCTURE_TYPE_UPDATE_AS_COPY
:
1981 update_as_copy
= (VkUpdateAsCopy
*) common
;
1982 (void) update_as_copy
;
1991 // State object functions
1993 static inline int64_t
1994 clamp_int64(int64_t x
, int64_t min
, int64_t max
)
2005 anv_dynamic_vp_state_destroy(struct anv_device
*device
,
2006 struct anv_object
*object
,
2007 VkObjectType obj_type
)
2009 struct anv_dynamic_vp_state
*state
= (void *)object
;
2011 assert(obj_type
== VK_OBJECT_TYPE_DYNAMIC_VP_STATE
);
2013 anv_state_pool_free(&device
->dynamic_state_pool
, state
->sf_clip_vp
);
2014 anv_state_pool_free(&device
->dynamic_state_pool
, state
->cc_vp
);
2015 anv_state_pool_free(&device
->dynamic_state_pool
, state
->scissor
);
2017 anv_device_free(device
, state
);
2020 VkResult
anv_CreateDynamicViewportState(
2022 const VkDynamicVpStateCreateInfo
* pCreateInfo
,
2023 VkDynamicVpState
* pState
)
2025 struct anv_device
*device
= (struct anv_device
*) _device
;
2026 struct anv_dynamic_vp_state
*state
;
2028 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_DYNAMIC_VP_STATE_CREATE_INFO
);
2030 state
= anv_device_alloc(device
, sizeof(*state
), 8,
2031 VK_SYSTEM_ALLOC_TYPE_API_OBJECT
);
2033 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
2035 state
->base
.destructor
= anv_dynamic_vp_state_destroy
;
2037 unsigned count
= pCreateInfo
->viewportAndScissorCount
;
2038 state
->sf_clip_vp
= anv_state_pool_alloc(&device
->dynamic_state_pool
,
2040 state
->cc_vp
= anv_state_pool_alloc(&device
->dynamic_state_pool
,
2042 state
->scissor
= anv_state_pool_alloc(&device
->dynamic_state_pool
,
2045 for (uint32_t i
= 0; i
< pCreateInfo
->viewportAndScissorCount
; i
++) {
2046 const VkViewport
*vp
= &pCreateInfo
->pViewports
[i
];
2047 const VkRect
*s
= &pCreateInfo
->pScissors
[i
];
2049 struct GEN8_SF_CLIP_VIEWPORT sf_clip_viewport
= {
2050 .ViewportMatrixElementm00
= vp
->width
/ 2,
2051 .ViewportMatrixElementm11
= vp
->height
/ 2,
2052 .ViewportMatrixElementm22
= (vp
->maxDepth
- vp
->minDepth
) / 2,
2053 .ViewportMatrixElementm30
= vp
->originX
+ vp
->width
/ 2,
2054 .ViewportMatrixElementm31
= vp
->originY
+ vp
->height
/ 2,
2055 .ViewportMatrixElementm32
= (vp
->maxDepth
+ vp
->minDepth
) / 2,
2056 .XMinClipGuardband
= -1.0f
,
2057 .XMaxClipGuardband
= 1.0f
,
2058 .YMinClipGuardband
= -1.0f
,
2059 .YMaxClipGuardband
= 1.0f
,
2060 .XMinViewPort
= vp
->originX
,
2061 .XMaxViewPort
= vp
->originX
+ vp
->width
- 1,
2062 .YMinViewPort
= vp
->originY
,
2063 .YMaxViewPort
= vp
->originY
+ vp
->height
- 1,
2066 struct GEN8_CC_VIEWPORT cc_viewport
= {
2067 .MinimumDepth
= vp
->minDepth
,
2068 .MaximumDepth
= vp
->maxDepth
2071 /* Since xmax and ymax are inclusive, we have to have xmax < xmin or
2072 * ymax < ymin for empty clips. In case clip x, y, width height are all
2073 * 0, the clamps below produce 0 for xmin, ymin, xmax, ymax, which isn't
2074 * what we want. Just special case empty clips and produce a canonical
2076 static const struct GEN8_SCISSOR_RECT empty_scissor
= {
2077 .ScissorRectangleYMin
= 1,
2078 .ScissorRectangleXMin
= 1,
2079 .ScissorRectangleYMax
= 0,
2080 .ScissorRectangleXMax
= 0
2083 const int max
= 0xffff;
2084 struct GEN8_SCISSOR_RECT scissor
= {
2085 /* Do this math using int64_t so overflow gets clamped correctly. */
2086 .ScissorRectangleYMin
= clamp_int64(s
->offset
.y
, 0, max
),
2087 .ScissorRectangleXMin
= clamp_int64(s
->offset
.x
, 0, max
),
2088 .ScissorRectangleYMax
= clamp_int64((uint64_t) s
->offset
.y
+ s
->extent
.height
- 1, 0, max
),
2089 .ScissorRectangleXMax
= clamp_int64((uint64_t) s
->offset
.x
+ s
->extent
.width
- 1, 0, max
)
2092 GEN8_SF_CLIP_VIEWPORT_pack(NULL
, state
->sf_clip_vp
.map
+ i
* 64, &sf_clip_viewport
);
2093 GEN8_CC_VIEWPORT_pack(NULL
, state
->cc_vp
.map
+ i
* 32, &cc_viewport
);
2095 if (s
->extent
.width
<= 0 || s
->extent
.height
<= 0) {
2096 GEN8_SCISSOR_RECT_pack(NULL
, state
->scissor
.map
+ i
* 32, &empty_scissor
);
2098 GEN8_SCISSOR_RECT_pack(NULL
, state
->scissor
.map
+ i
* 32, &scissor
);
2102 *pState
= (VkDynamicVpState
) state
;
2107 VkResult
anv_CreateDynamicRasterState(
2109 const VkDynamicRsStateCreateInfo
* pCreateInfo
,
2110 VkDynamicRsState
* pState
)
2112 struct anv_device
*device
= (struct anv_device
*) _device
;
2113 struct anv_dynamic_rs_state
*state
;
2115 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_DYNAMIC_RS_STATE_CREATE_INFO
);
2117 state
= anv_device_alloc(device
, sizeof(*state
), 8,
2118 VK_SYSTEM_ALLOC_TYPE_API_OBJECT
);
2120 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
2123 * float pointFadeThreshold;
2124 * // optional (GL45) - Size of point fade threshold
2127 struct GEN8_3DSTATE_SF sf
= {
2128 GEN8_3DSTATE_SF_header
,
2129 .LineWidth
= pCreateInfo
->lineWidth
,
2130 .PointWidth
= pCreateInfo
->pointSize
,
2133 GEN8_3DSTATE_SF_pack(NULL
, state
->state_sf
, &sf
);
2135 bool enable_bias
= pCreateInfo
->depthBias
!= 0.0f
||
2136 pCreateInfo
->slopeScaledDepthBias
!= 0.0f
;
2137 struct GEN8_3DSTATE_RASTER raster
= {
2138 .GlobalDepthOffsetEnableSolid
= enable_bias
,
2139 .GlobalDepthOffsetEnableWireframe
= enable_bias
,
2140 .GlobalDepthOffsetEnablePoint
= enable_bias
,
2141 .GlobalDepthOffsetConstant
= pCreateInfo
->depthBias
,
2142 .GlobalDepthOffsetScale
= pCreateInfo
->slopeScaledDepthBias
,
2143 .GlobalDepthOffsetClamp
= pCreateInfo
->depthBiasClamp
2146 GEN8_3DSTATE_RASTER_pack(NULL
, state
->state_raster
, &raster
);
2148 *pState
= (VkDynamicRsState
) state
;
2153 VkResult
anv_CreateDynamicColorBlendState(
2155 const VkDynamicCbStateCreateInfo
* pCreateInfo
,
2156 VkDynamicCbState
* pState
)
2158 struct anv_device
*device
= (struct anv_device
*) _device
;
2159 struct anv_dynamic_cb_state
*state
;
2161 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_DYNAMIC_CB_STATE_CREATE_INFO
);
2163 state
= anv_device_alloc(device
, sizeof(*state
), 8,
2164 VK_SYSTEM_ALLOC_TYPE_API_OBJECT
);
2166 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
2168 struct GEN8_COLOR_CALC_STATE color_calc_state
= {
2169 .BlendConstantColorRed
= pCreateInfo
->blendConst
[0],
2170 .BlendConstantColorGreen
= pCreateInfo
->blendConst
[1],
2171 .BlendConstantColorBlue
= pCreateInfo
->blendConst
[2],
2172 .BlendConstantColorAlpha
= pCreateInfo
->blendConst
[3]
2175 GEN8_COLOR_CALC_STATE_pack(NULL
, state
->state_color_calc
, &color_calc_state
);
2177 *pState
= (VkDynamicCbState
) state
;
2182 VkResult
anv_CreateDynamicDepthStencilState(
2184 const VkDynamicDsStateCreateInfo
* pCreateInfo
,
2185 VkDynamicDsState
* pState
)
2187 struct anv_device
*device
= (struct anv_device
*) _device
;
2188 struct anv_dynamic_ds_state
*state
;
2190 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_DYNAMIC_DS_STATE_CREATE_INFO
);
2192 state
= anv_device_alloc(device
, sizeof(*state
), 8,
2193 VK_SYSTEM_ALLOC_TYPE_API_OBJECT
);
2195 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
2197 struct GEN8_3DSTATE_WM_DEPTH_STENCIL wm_depth_stencil
= {
2198 GEN8_3DSTATE_WM_DEPTH_STENCIL_header
,
2200 /* Is this what we need to do? */
2201 .StencilBufferWriteEnable
= pCreateInfo
->stencilWriteMask
!= 0,
2203 .StencilTestMask
= pCreateInfo
->stencilReadMask
& 0xff,
2204 .StencilWriteMask
= pCreateInfo
->stencilWriteMask
& 0xff,
2206 .BackfaceStencilTestMask
= pCreateInfo
->stencilReadMask
& 0xff,
2207 .BackfaceStencilWriteMask
= pCreateInfo
->stencilWriteMask
& 0xff,
2210 GEN8_3DSTATE_WM_DEPTH_STENCIL_pack(NULL
, state
->state_wm_depth_stencil
,
2213 struct GEN8_COLOR_CALC_STATE color_calc_state
= {
2214 .StencilReferenceValue
= pCreateInfo
->stencilFrontRef
,
2215 .BackFaceStencilReferenceValue
= pCreateInfo
->stencilBackRef
2218 GEN8_COLOR_CALC_STATE_pack(NULL
, state
->state_color_calc
, &color_calc_state
);
2220 *pState
= (VkDynamicDsState
) state
;
2225 // Command buffer functions
2228 anv_cmd_buffer_destroy(struct anv_device
*device
,
2229 struct anv_object
*object
,
2230 VkObjectType obj_type
)
2232 struct anv_cmd_buffer
*cmd_buffer
= (struct anv_cmd_buffer
*) object
;
2234 assert(obj_type
== VK_OBJECT_TYPE_COMMAND_BUFFER
);
2236 /* Destroy all of the batch buffers */
2237 struct anv_batch_bo
*bbo
= cmd_buffer
->last_batch_bo
;
2239 struct anv_batch_bo
*prev
= bbo
->prev_batch_bo
;
2240 anv_batch_bo_destroy(bbo
, device
);
2243 anv_reloc_list_finish(&cmd_buffer
->batch
.relocs
, device
);
2245 /* Destroy all of the surface state buffers */
2246 bbo
= cmd_buffer
->surface_batch_bo
;
2248 struct anv_batch_bo
*prev
= bbo
->prev_batch_bo
;
2249 anv_batch_bo_destroy(bbo
, device
);
2252 anv_reloc_list_finish(&cmd_buffer
->surface_relocs
, device
);
2254 anv_state_stream_finish(&cmd_buffer
->surface_state_stream
);
2255 anv_state_stream_finish(&cmd_buffer
->dynamic_state_stream
);
2256 anv_device_free(device
, cmd_buffer
->exec2_objects
);
2257 anv_device_free(device
, cmd_buffer
->exec2_bos
);
2258 anv_device_free(device
, cmd_buffer
);
2262 anv_cmd_buffer_chain_batch(struct anv_batch
*batch
, void *_data
)
2264 struct anv_cmd_buffer
*cmd_buffer
= _data
;
2266 struct anv_batch_bo
*new_bbo
, *old_bbo
= cmd_buffer
->last_batch_bo
;
2268 VkResult result
= anv_batch_bo_create(cmd_buffer
->device
, &new_bbo
);
2269 if (result
!= VK_SUCCESS
)
2272 /* We set the end of the batch a little short so we would be sure we
2273 * have room for the chaining command. Since we're about to emit the
2274 * chaining command, let's set it back where it should go.
2276 batch
->end
+= GEN8_MI_BATCH_BUFFER_START_length
* 4;
2277 assert(batch
->end
== old_bbo
->bo
.map
+ old_bbo
->bo
.size
);
2279 anv_batch_emit(batch
, GEN8_MI_BATCH_BUFFER_START
,
2280 GEN8_MI_BATCH_BUFFER_START_header
,
2281 ._2ndLevelBatchBuffer
= _1stlevelbatch
,
2282 .AddressSpaceIndicator
= ASI_PPGTT
,
2283 .BatchBufferStartAddress
= { &new_bbo
->bo
, 0 },
2286 /* Pad out to a 2-dword aligned boundary with zeros */
2287 if ((uintptr_t)batch
->next
% 8 != 0) {
2288 *(uint32_t *)batch
->next
= 0;
2292 anv_batch_bo_finish(cmd_buffer
->last_batch_bo
, batch
);
2294 new_bbo
->prev_batch_bo
= old_bbo
;
2295 cmd_buffer
->last_batch_bo
= new_bbo
;
2297 anv_batch_bo_start(new_bbo
, batch
, GEN8_MI_BATCH_BUFFER_START_length
* 4);
2302 VkResult
anv_CreateCommandBuffer(
2304 const VkCmdBufferCreateInfo
* pCreateInfo
,
2305 VkCmdBuffer
* pCmdBuffer
)
2307 struct anv_device
*device
= (struct anv_device
*) _device
;
2308 struct anv_cmd_buffer
*cmd_buffer
;
2311 cmd_buffer
= anv_device_alloc(device
, sizeof(*cmd_buffer
), 8,
2312 VK_SYSTEM_ALLOC_TYPE_API_OBJECT
);
2313 if (cmd_buffer
== NULL
)
2314 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
2316 cmd_buffer
->base
.destructor
= anv_cmd_buffer_destroy
;
2318 cmd_buffer
->device
= device
;
2319 cmd_buffer
->rs_state
= NULL
;
2320 cmd_buffer
->vp_state
= NULL
;
2321 cmd_buffer
->cb_state
= NULL
;
2322 cmd_buffer
->ds_state
= NULL
;
2323 memset(&cmd_buffer
->descriptors
, 0, sizeof(cmd_buffer
->descriptors
));
2325 result
= anv_batch_bo_create(device
, &cmd_buffer
->last_batch_bo
);
2326 if (result
!= VK_SUCCESS
)
2329 result
= anv_reloc_list_init(&cmd_buffer
->batch
.relocs
, device
);
2330 if (result
!= VK_SUCCESS
)
2333 cmd_buffer
->batch
.device
= device
;
2334 cmd_buffer
->batch
.extend_cb
= anv_cmd_buffer_chain_batch
;
2335 cmd_buffer
->batch
.user_data
= cmd_buffer
;
2337 anv_batch_bo_start(cmd_buffer
->last_batch_bo
, &cmd_buffer
->batch
,
2338 GEN8_MI_BATCH_BUFFER_START_length
* 4);
2340 result
= anv_batch_bo_create(device
, &cmd_buffer
->surface_batch_bo
);
2341 if (result
!= VK_SUCCESS
)
2342 goto fail_batch_relocs
;
2343 cmd_buffer
->surface_batch_bo
->first_reloc
= 0;
2345 result
= anv_reloc_list_init(&cmd_buffer
->surface_relocs
, device
);
2346 if (result
!= VK_SUCCESS
)
2347 goto fail_ss_batch_bo
;
2349 /* Start surface_next at 1 so surface offset 0 is invalid. */
2350 cmd_buffer
->surface_next
= 1;
2352 cmd_buffer
->exec2_objects
= NULL
;
2353 cmd_buffer
->exec2_bos
= NULL
;
2354 cmd_buffer
->exec2_array_length
= 0;
2356 anv_state_stream_init(&cmd_buffer
->surface_state_stream
,
2357 &device
->surface_state_block_pool
);
2358 anv_state_stream_init(&cmd_buffer
->dynamic_state_stream
,
2359 &device
->dynamic_state_block_pool
);
2361 cmd_buffer
->dirty
= 0;
2362 cmd_buffer
->vb_dirty
= 0;
2363 cmd_buffer
->descriptors_dirty
= 0;
2364 cmd_buffer
->pipeline
= NULL
;
2365 cmd_buffer
->vp_state
= NULL
;
2366 cmd_buffer
->rs_state
= NULL
;
2367 cmd_buffer
->ds_state
= NULL
;
2369 *pCmdBuffer
= (VkCmdBuffer
) cmd_buffer
;
2374 anv_batch_bo_destroy(cmd_buffer
->surface_batch_bo
, device
);
2376 anv_reloc_list_finish(&cmd_buffer
->batch
.relocs
, device
);
2378 anv_batch_bo_destroy(cmd_buffer
->last_batch_bo
, device
);
2380 anv_device_free(device
, cmd_buffer
);
2386 anv_cmd_buffer_emit_state_base_address(struct anv_cmd_buffer
*cmd_buffer
)
2388 struct anv_device
*device
= cmd_buffer
->device
;
2390 anv_batch_emit(&cmd_buffer
->batch
, GEN8_STATE_BASE_ADDRESS
,
2391 .GeneralStateBaseAddress
= { NULL
, 0 },
2392 .GeneralStateMemoryObjectControlState
= GEN8_MOCS
,
2393 .GeneralStateBaseAddressModifyEnable
= true,
2394 .GeneralStateBufferSize
= 0xfffff,
2395 .GeneralStateBufferSizeModifyEnable
= true,
2397 .SurfaceStateBaseAddress
= { &cmd_buffer
->surface_batch_bo
->bo
, 0 },
2398 .SurfaceStateMemoryObjectControlState
= GEN8_MOCS
,
2399 .SurfaceStateBaseAddressModifyEnable
= true,
2401 .DynamicStateBaseAddress
= { &device
->dynamic_state_block_pool
.bo
, 0 },
2402 .DynamicStateMemoryObjectControlState
= GEN8_MOCS
,
2403 .DynamicStateBaseAddressModifyEnable
= true,
2404 .DynamicStateBufferSize
= 0xfffff,
2405 .DynamicStateBufferSizeModifyEnable
= true,
2407 .IndirectObjectBaseAddress
= { NULL
, 0 },
2408 .IndirectObjectMemoryObjectControlState
= GEN8_MOCS
,
2409 .IndirectObjectBaseAddressModifyEnable
= true,
2410 .IndirectObjectBufferSize
= 0xfffff,
2411 .IndirectObjectBufferSizeModifyEnable
= true,
2413 .InstructionBaseAddress
= { &device
->instruction_block_pool
.bo
, 0 },
2414 .InstructionMemoryObjectControlState
= GEN8_MOCS
,
2415 .InstructionBaseAddressModifyEnable
= true,
2416 .InstructionBufferSize
= 0xfffff,
2417 .InstructionBuffersizeModifyEnable
= true);
2420 VkResult
anv_BeginCommandBuffer(
2421 VkCmdBuffer cmdBuffer
,
2422 const VkCmdBufferBeginInfo
* pBeginInfo
)
2424 struct anv_cmd_buffer
*cmd_buffer
= (struct anv_cmd_buffer
*) cmdBuffer
;
2426 anv_batch_emit(&cmd_buffer
->batch
, GEN8_PIPELINE_SELECT
,
2427 .PipelineSelection
= _3D
);
2428 anv_batch_emit(&cmd_buffer
->batch
, GEN8_STATE_SIP
);
2430 anv_cmd_buffer_emit_state_base_address(cmd_buffer
);
2432 anv_batch_emit(&cmd_buffer
->batch
, GEN8_3DSTATE_VF_STATISTICS
,
2433 .StatisticsEnable
= true);
2434 anv_batch_emit(&cmd_buffer
->batch
, GEN8_3DSTATE_HS
, .Enable
= false);
2435 anv_batch_emit(&cmd_buffer
->batch
, GEN8_3DSTATE_TE
, .TEEnable
= false);
2436 anv_batch_emit(&cmd_buffer
->batch
, GEN8_3DSTATE_DS
, .FunctionEnable
= false);
2437 anv_batch_emit(&cmd_buffer
->batch
, GEN8_3DSTATE_STREAMOUT
, .SOFunctionEnable
= false);
2439 anv_batch_emit(&cmd_buffer
->batch
, GEN8_3DSTATE_PUSH_CONSTANT_ALLOC_VS
,
2440 .ConstantBufferOffset
= 0,
2441 .ConstantBufferSize
= 4);
2442 anv_batch_emit(&cmd_buffer
->batch
, GEN8_3DSTATE_PUSH_CONSTANT_ALLOC_GS
,
2443 .ConstantBufferOffset
= 4,
2444 .ConstantBufferSize
= 4);
2445 anv_batch_emit(&cmd_buffer
->batch
, GEN8_3DSTATE_PUSH_CONSTANT_ALLOC_PS
,
2446 .ConstantBufferOffset
= 8,
2447 .ConstantBufferSize
= 4);
2449 anv_batch_emit(&cmd_buffer
->batch
, GEN8_3DSTATE_WM_CHROMAKEY
,
2450 .ChromaKeyKillEnable
= false);
2451 anv_batch_emit(&cmd_buffer
->batch
, GEN8_3DSTATE_SBE_SWIZ
);
2452 anv_batch_emit(&cmd_buffer
->batch
, GEN8_3DSTATE_AA_LINE_PARAMETERS
);
2458 anv_cmd_buffer_add_bo(struct anv_cmd_buffer
*cmd_buffer
,
2460 struct drm_i915_gem_relocation_entry
*relocs
,
2463 struct drm_i915_gem_exec_object2
*obj
;
2465 if (bo
->index
< cmd_buffer
->bo_count
&&
2466 cmd_buffer
->exec2_bos
[bo
->index
] == bo
)
2469 if (cmd_buffer
->bo_count
>= cmd_buffer
->exec2_array_length
) {
2470 uint32_t new_len
= cmd_buffer
->exec2_objects
?
2471 cmd_buffer
->exec2_array_length
* 2 : 64;
2473 struct drm_i915_gem_exec_object2
*new_objects
=
2474 anv_device_alloc(cmd_buffer
->device
, new_len
* sizeof(*new_objects
),
2475 8, VK_SYSTEM_ALLOC_TYPE_INTERNAL
);
2476 if (new_objects
== NULL
)
2477 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
2479 struct anv_bo
**new_bos
=
2480 anv_device_alloc(cmd_buffer
->device
, new_len
* sizeof(*new_bos
),
2481 8, VK_SYSTEM_ALLOC_TYPE_INTERNAL
);
2482 if (new_objects
== NULL
) {
2483 anv_device_free(cmd_buffer
->device
, new_objects
);
2484 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
2487 if (cmd_buffer
->exec2_objects
) {
2488 memcpy(new_objects
, cmd_buffer
->exec2_objects
,
2489 cmd_buffer
->bo_count
* sizeof(*new_objects
));
2490 memcpy(new_bos
, cmd_buffer
->exec2_bos
,
2491 cmd_buffer
->bo_count
* sizeof(*new_bos
));
2494 cmd_buffer
->exec2_objects
= new_objects
;
2495 cmd_buffer
->exec2_bos
= new_bos
;
2496 cmd_buffer
->exec2_array_length
= new_len
;
2499 assert(cmd_buffer
->bo_count
< cmd_buffer
->exec2_array_length
);
2501 bo
->index
= cmd_buffer
->bo_count
++;
2502 obj
= &cmd_buffer
->exec2_objects
[bo
->index
];
2503 cmd_buffer
->exec2_bos
[bo
->index
] = bo
;
2505 obj
->handle
= bo
->gem_handle
;
2506 obj
->relocation_count
= 0;
2507 obj
->relocs_ptr
= 0;
2509 obj
->offset
= bo
->offset
;
2515 obj
->relocation_count
= num_relocs
;
2516 obj
->relocs_ptr
= (uintptr_t) relocs
;
2523 anv_cmd_buffer_add_validate_bos(struct anv_cmd_buffer
*cmd_buffer
,
2524 struct anv_reloc_list
*list
)
2526 for (size_t i
= 0; i
< list
->num_relocs
; i
++)
2527 anv_cmd_buffer_add_bo(cmd_buffer
, list
->reloc_bos
[i
], NULL
, 0);
2531 anv_cmd_buffer_process_relocs(struct anv_cmd_buffer
*cmd_buffer
,
2532 struct anv_reloc_list
*list
)
2536 /* If the kernel supports I915_EXEC_NO_RELOC, it will compare offset in
2537 * struct drm_i915_gem_exec_object2 against the bos current offset and if
2538 * all bos haven't moved it will skip relocation processing alltogether.
2539 * If I915_EXEC_NO_RELOC is not supported, the kernel ignores the incoming
2540 * value of offset so we can set it either way. For that to work we need
2541 * to make sure all relocs use the same presumed offset.
2544 for (size_t i
= 0; i
< list
->num_relocs
; i
++) {
2545 bo
= list
->reloc_bos
[i
];
2546 if (bo
->offset
!= list
->relocs
[i
].presumed_offset
)
2547 cmd_buffer
->need_reloc
= true;
2549 list
->relocs
[i
].target_handle
= bo
->index
;
2553 VkResult
anv_EndCommandBuffer(
2554 VkCmdBuffer cmdBuffer
)
2556 struct anv_cmd_buffer
*cmd_buffer
= (struct anv_cmd_buffer
*) cmdBuffer
;
2557 struct anv_device
*device
= cmd_buffer
->device
;
2558 struct anv_batch
*batch
= &cmd_buffer
->batch
;
2560 anv_batch_emit(batch
, GEN8_MI_BATCH_BUFFER_END
);
2562 /* Round batch up to an even number of dwords. */
2563 if ((batch
->next
- batch
->start
) & 4)
2564 anv_batch_emit(batch
, GEN8_MI_NOOP
);
2566 anv_batch_bo_finish(cmd_buffer
->last_batch_bo
, &cmd_buffer
->batch
);
2567 cmd_buffer
->surface_batch_bo
->num_relocs
=
2568 cmd_buffer
->surface_relocs
.num_relocs
- cmd_buffer
->surface_batch_bo
->first_reloc
;
2569 cmd_buffer
->surface_batch_bo
->length
= cmd_buffer
->surface_next
;
2571 cmd_buffer
->bo_count
= 0;
2572 cmd_buffer
->need_reloc
= false;
2574 /* Lock for access to bo->index. */
2575 pthread_mutex_lock(&device
->mutex
);
2577 /* Add surface state bos first so we can add them with their relocs. */
2578 for (struct anv_batch_bo
*bbo
= cmd_buffer
->surface_batch_bo
;
2579 bbo
!= NULL
; bbo
= bbo
->prev_batch_bo
) {
2580 anv_cmd_buffer_add_bo(cmd_buffer
, &bbo
->bo
,
2581 &cmd_buffer
->surface_relocs
.relocs
[bbo
->first_reloc
],
2585 /* Add all of the BOs referenced by surface state */
2586 anv_cmd_buffer_add_validate_bos(cmd_buffer
, &cmd_buffer
->surface_relocs
);
2588 /* Add all but the first batch BO */
2589 struct anv_batch_bo
*batch_bo
= cmd_buffer
->last_batch_bo
;
2590 while (batch_bo
->prev_batch_bo
) {
2591 anv_cmd_buffer_add_bo(cmd_buffer
, &batch_bo
->bo
,
2592 &batch
->relocs
.relocs
[batch_bo
->first_reloc
],
2593 batch_bo
->num_relocs
);
2594 batch_bo
= batch_bo
->prev_batch_bo
;
2597 /* Add everything referenced by the batches */
2598 anv_cmd_buffer_add_validate_bos(cmd_buffer
, &batch
->relocs
);
2600 /* Add the first batch bo last */
2601 assert(batch_bo
->prev_batch_bo
== NULL
&& batch_bo
->first_reloc
== 0);
2602 anv_cmd_buffer_add_bo(cmd_buffer
, &batch_bo
->bo
,
2603 &batch
->relocs
.relocs
[batch_bo
->first_reloc
],
2604 batch_bo
->num_relocs
);
2605 assert(batch_bo
->bo
.index
== cmd_buffer
->bo_count
- 1);
2607 anv_cmd_buffer_process_relocs(cmd_buffer
, &cmd_buffer
->surface_relocs
);
2608 anv_cmd_buffer_process_relocs(cmd_buffer
, &batch
->relocs
);
2610 cmd_buffer
->execbuf
.buffers_ptr
= (uintptr_t) cmd_buffer
->exec2_objects
;
2611 cmd_buffer
->execbuf
.buffer_count
= cmd_buffer
->bo_count
;
2612 cmd_buffer
->execbuf
.batch_start_offset
= 0;
2613 cmd_buffer
->execbuf
.batch_len
= batch
->next
- batch
->start
;
2614 cmd_buffer
->execbuf
.cliprects_ptr
= 0;
2615 cmd_buffer
->execbuf
.num_cliprects
= 0;
2616 cmd_buffer
->execbuf
.DR1
= 0;
2617 cmd_buffer
->execbuf
.DR4
= 0;
2619 cmd_buffer
->execbuf
.flags
= I915_EXEC_HANDLE_LUT
;
2620 if (!cmd_buffer
->need_reloc
)
2621 cmd_buffer
->execbuf
.flags
|= I915_EXEC_NO_RELOC
;
2622 cmd_buffer
->execbuf
.flags
|= I915_EXEC_RENDER
;
2623 cmd_buffer
->execbuf
.rsvd1
= device
->context_id
;
2624 cmd_buffer
->execbuf
.rsvd2
= 0;
2626 pthread_mutex_unlock(&device
->mutex
);
2631 VkResult
anv_ResetCommandBuffer(
2632 VkCmdBuffer cmdBuffer
)
2634 struct anv_cmd_buffer
*cmd_buffer
= (struct anv_cmd_buffer
*) cmdBuffer
;
2636 /* Delete all but the first batch bo */
2637 while (cmd_buffer
->last_batch_bo
->prev_batch_bo
) {
2638 struct anv_batch_bo
*prev
= cmd_buffer
->last_batch_bo
->prev_batch_bo
;
2639 anv_batch_bo_destroy(cmd_buffer
->last_batch_bo
, cmd_buffer
->device
);
2640 cmd_buffer
->last_batch_bo
= prev
;
2642 assert(cmd_buffer
->last_batch_bo
->prev_batch_bo
== NULL
);
2644 cmd_buffer
->batch
.relocs
.num_relocs
= 0;
2645 anv_batch_bo_start(cmd_buffer
->last_batch_bo
, &cmd_buffer
->batch
,
2646 GEN8_MI_BATCH_BUFFER_START_length
* 4);
2648 /* Delete all but the first batch bo */
2649 while (cmd_buffer
->surface_batch_bo
->prev_batch_bo
) {
2650 struct anv_batch_bo
*prev
= cmd_buffer
->surface_batch_bo
->prev_batch_bo
;
2651 anv_batch_bo_destroy(cmd_buffer
->surface_batch_bo
, cmd_buffer
->device
);
2652 cmd_buffer
->surface_batch_bo
= prev
;
2654 assert(cmd_buffer
->surface_batch_bo
->prev_batch_bo
== NULL
);
2656 cmd_buffer
->surface_next
= 1;
2657 cmd_buffer
->surface_relocs
.num_relocs
= 0;
2659 cmd_buffer
->rs_state
= NULL
;
2660 cmd_buffer
->vp_state
= NULL
;
2661 cmd_buffer
->cb_state
= NULL
;
2662 cmd_buffer
->ds_state
= NULL
;
2667 // Command buffer building functions
2669 void anv_CmdBindPipeline(
2670 VkCmdBuffer cmdBuffer
,
2671 VkPipelineBindPoint pipelineBindPoint
,
2672 VkPipeline _pipeline
)
2674 struct anv_cmd_buffer
*cmd_buffer
= (struct anv_cmd_buffer
*) cmdBuffer
;
2675 struct anv_pipeline
*pipeline
= (struct anv_pipeline
*) _pipeline
;
2677 cmd_buffer
->pipeline
= pipeline
;
2678 cmd_buffer
->vb_dirty
|= pipeline
->vb_used
;
2679 cmd_buffer
->dirty
|= ANV_CMD_BUFFER_PIPELINE_DIRTY
;
2682 void anv_CmdBindDynamicStateObject(
2683 VkCmdBuffer cmdBuffer
,
2684 VkStateBindPoint stateBindPoint
,
2685 VkDynamicStateObject dynamicState
)
2687 struct anv_cmd_buffer
*cmd_buffer
= (struct anv_cmd_buffer
*) cmdBuffer
;
2688 struct anv_dynamic_vp_state
*vp_state
;
2690 switch (stateBindPoint
) {
2691 case VK_STATE_BIND_POINT_VIEWPORT
:
2692 vp_state
= (struct anv_dynamic_vp_state
*) dynamicState
;
2693 /* We emit state immediately, but set cmd_buffer->vp_state to indicate
2694 * that vp state has been set in this command buffer. */
2695 cmd_buffer
->vp_state
= vp_state
;
2696 anv_batch_emit(&cmd_buffer
->batch
, GEN8_3DSTATE_SCISSOR_STATE_POINTERS
,
2697 .ScissorRectPointer
= vp_state
->scissor
.offset
);
2698 anv_batch_emit(&cmd_buffer
->batch
, GEN8_3DSTATE_VIEWPORT_STATE_POINTERS_CC
,
2699 .CCViewportPointer
= vp_state
->cc_vp
.offset
);
2700 anv_batch_emit(&cmd_buffer
->batch
, GEN8_3DSTATE_VIEWPORT_STATE_POINTERS_SF_CLIP
,
2701 .SFClipViewportPointer
= vp_state
->sf_clip_vp
.offset
);
2703 case VK_STATE_BIND_POINT_RASTER
:
2704 cmd_buffer
->rs_state
= (struct anv_dynamic_rs_state
*) dynamicState
;
2705 cmd_buffer
->dirty
|= ANV_CMD_BUFFER_RS_DIRTY
;
2707 case VK_STATE_BIND_POINT_COLOR_BLEND
:
2708 cmd_buffer
->cb_state
= (struct anv_dynamic_cb_state
*) dynamicState
;
2709 cmd_buffer
->dirty
|= ANV_CMD_BUFFER_CB_DIRTY
;
2711 case VK_STATE_BIND_POINT_DEPTH_STENCIL
:
2712 cmd_buffer
->ds_state
= (struct anv_dynamic_ds_state
*) dynamicState
;
2713 cmd_buffer
->dirty
|= ANV_CMD_BUFFER_DS_DIRTY
;
2720 static struct anv_state
2721 anv_cmd_buffer_alloc_surface_state(struct anv_cmd_buffer
*cmd_buffer
,
2722 uint32_t size
, uint32_t alignment
)
2724 struct anv_state state
;
2726 state
.offset
= ALIGN_U32(cmd_buffer
->surface_next
, alignment
);
2727 if (state
.offset
+ size
> cmd_buffer
->surface_batch_bo
->bo
.size
)
2728 return (struct anv_state
) { 0 };
2730 state
.map
= cmd_buffer
->surface_batch_bo
->bo
.map
+ state
.offset
;
2731 state
.alloc_size
= size
;
2732 cmd_buffer
->surface_next
= state
.offset
+ size
;
2734 assert(state
.offset
+ size
<= cmd_buffer
->surface_batch_bo
->bo
.size
);
2740 anv_cmd_buffer_new_surface_state_bo(struct anv_cmd_buffer
*cmd_buffer
)
2742 struct anv_batch_bo
*new_bbo
, *old_bbo
= cmd_buffer
->surface_batch_bo
;
2744 /* Finish off the old buffer */
2745 old_bbo
->num_relocs
=
2746 cmd_buffer
->surface_relocs
.num_relocs
- old_bbo
->first_reloc
;
2747 old_bbo
->length
= cmd_buffer
->surface_next
;
2749 VkResult result
= anv_batch_bo_create(cmd_buffer
->device
, &new_bbo
);
2750 if (result
!= VK_SUCCESS
)
2753 new_bbo
->first_reloc
= cmd_buffer
->surface_relocs
.num_relocs
;
2754 cmd_buffer
->surface_next
= 1;
2756 new_bbo
->prev_batch_bo
= old_bbo
;
2757 cmd_buffer
->surface_batch_bo
= new_bbo
;
2759 /* Re-emit state base addresses so we get the new surface state base
2760 * address before we start emitting binding tables etc.
2762 anv_cmd_buffer_emit_state_base_address(cmd_buffer
);
2764 /* It seems like just changing the state base addresses isn't enough.
2765 * Invalidating the cache seems to be enough to cause things to
2766 * propagate. However, I'm not 100% sure what we're supposed to do.
2768 anv_batch_emit(&cmd_buffer
->batch
, GEN8_PIPE_CONTROL
,
2769 .TextureCacheInvalidationEnable
= true);
2774 void anv_CmdBindDescriptorSets(
2775 VkCmdBuffer cmdBuffer
,
2776 VkPipelineBindPoint pipelineBindPoint
,
2779 const VkDescriptorSet
* pDescriptorSets
,
2780 uint32_t dynamicOffsetCount
,
2781 const uint32_t* pDynamicOffsets
)
2783 struct anv_cmd_buffer
*cmd_buffer
= (struct anv_cmd_buffer
*) cmdBuffer
;
2784 struct anv_pipeline_layout
*layout
= cmd_buffer
->pipeline
->layout
;
2785 struct anv_descriptor_set
*set
;
2786 struct anv_descriptor_set_layout
*set_layout
;
2788 assert(firstSet
+ setCount
< MAX_SETS
);
2790 uint32_t dynamic_slot
= 0;
2791 for (uint32_t i
= 0; i
< setCount
; i
++) {
2792 set
= (struct anv_descriptor_set
*) pDescriptorSets
[i
];
2793 set_layout
= layout
->set
[firstSet
+ i
].layout
;
2795 cmd_buffer
->descriptors
[firstSet
+ i
].set
= set
;
2797 assert(set_layout
->num_dynamic_buffers
<
2798 ARRAY_SIZE(cmd_buffer
->descriptors
[0].dynamic_offsets
));
2799 memcpy(cmd_buffer
->descriptors
[firstSet
+ i
].dynamic_offsets
,
2800 pDynamicOffsets
+ dynamic_slot
,
2801 set_layout
->num_dynamic_buffers
* sizeof(*pDynamicOffsets
));
2803 cmd_buffer
->descriptors_dirty
|= set_layout
->shader_stages
;
2805 dynamic_slot
+= set_layout
->num_dynamic_buffers
;
2809 void anv_CmdBindIndexBuffer(
2810 VkCmdBuffer cmdBuffer
,
2812 VkDeviceSize offset
,
2813 VkIndexType indexType
)
2815 struct anv_cmd_buffer
*cmd_buffer
= (struct anv_cmd_buffer
*) cmdBuffer
;
2816 struct anv_buffer
*buffer
= (struct anv_buffer
*) _buffer
;
2818 static const uint32_t vk_to_gen_index_type
[] = {
2819 [VK_INDEX_TYPE_UINT8
] = INDEX_BYTE
,
2820 [VK_INDEX_TYPE_UINT16
] = INDEX_WORD
,
2821 [VK_INDEX_TYPE_UINT32
] = INDEX_DWORD
,
2824 anv_batch_emit(&cmd_buffer
->batch
, GEN8_3DSTATE_INDEX_BUFFER
,
2825 .IndexFormat
= vk_to_gen_index_type
[indexType
],
2826 .MemoryObjectControlState
= GEN8_MOCS
,
2827 .BufferStartingAddress
= { buffer
->bo
, buffer
->offset
+ offset
},
2828 .BufferSize
= buffer
->size
- offset
);
2831 void anv_CmdBindVertexBuffers(
2832 VkCmdBuffer cmdBuffer
,
2833 uint32_t startBinding
,
2834 uint32_t bindingCount
,
2835 const VkBuffer
* pBuffers
,
2836 const VkDeviceSize
* pOffsets
)
2838 struct anv_cmd_buffer
*cmd_buffer
= (struct anv_cmd_buffer
*) cmdBuffer
;
2839 struct anv_vertex_binding
*vb
= cmd_buffer
->vertex_bindings
;
2841 /* We have to defer setting up vertex buffer since we need the buffer
2842 * stride from the pipeline. */
2844 assert(startBinding
+ bindingCount
< MAX_VBS
);
2845 for (uint32_t i
= 0; i
< bindingCount
; i
++) {
2846 vb
[startBinding
+ i
].buffer
= (struct anv_buffer
*) pBuffers
[i
];
2847 vb
[startBinding
+ i
].offset
= pOffsets
[i
];
2848 cmd_buffer
->vb_dirty
|= 1 << (startBinding
+ i
);
2853 cmd_buffer_emit_binding_table(struct anv_cmd_buffer
*cmd_buffer
,
2856 struct anv_pipeline_layout
*layout
= cmd_buffer
->pipeline
->layout
;
2857 uint32_t color_attachments
, bias
, size
;
2858 struct anv_state bt_state
;
2860 if (stage
== VK_SHADER_STAGE_FRAGMENT
) {
2862 color_attachments
= cmd_buffer
->framebuffer
->color_attachment_count
;
2865 color_attachments
= 0;
2868 /* This is a little awkward: layout can be NULL but we still have to
2869 * allocate and set a binding table for the PS stage for render
2871 uint32_t surface_count
= layout
? layout
->stage
[stage
].surface_count
: 0;
2873 if (color_attachments
+ surface_count
== 0)
2876 size
= (bias
+ surface_count
) * sizeof(uint32_t);
2877 bt_state
= anv_cmd_buffer_alloc_surface_state(cmd_buffer
, size
, 32);
2878 uint32_t *bt_map
= bt_state
.map
;
2880 if (bt_state
.map
== NULL
)
2881 return VK_ERROR_OUT_OF_DEVICE_MEMORY
;
2883 static const uint32_t binding_table_opcodes
[] = {
2884 [VK_SHADER_STAGE_VERTEX
] = 38,
2885 [VK_SHADER_STAGE_TESS_CONTROL
] = 39,
2886 [VK_SHADER_STAGE_TESS_EVALUATION
] = 40,
2887 [VK_SHADER_STAGE_GEOMETRY
] = 41,
2888 [VK_SHADER_STAGE_FRAGMENT
] = 42,
2889 [VK_SHADER_STAGE_COMPUTE
] = 0,
2892 anv_batch_emit(&cmd_buffer
->batch
,
2893 GEN8_3DSTATE_BINDING_TABLE_POINTERS_VS
,
2894 ._3DCommandSubOpcode
= binding_table_opcodes
[stage
],
2895 .PointertoVSBindingTable
= bt_state
.offset
);
2897 for (uint32_t ca
= 0; ca
< color_attachments
; ca
++) {
2898 const struct anv_surface_view
*view
=
2899 cmd_buffer
->framebuffer
->color_attachments
[ca
];
2901 struct anv_state state
=
2902 anv_cmd_buffer_alloc_surface_state(cmd_buffer
, 64, 64);
2904 if (state
.map
== NULL
)
2905 return VK_ERROR_OUT_OF_DEVICE_MEMORY
;
2907 memcpy(state
.map
, view
->surface_state
.map
, 64);
2909 /* The address goes in dwords 8 and 9 of the SURFACE_STATE */
2910 *(uint64_t *)(state
.map
+ 8 * 4) =
2911 anv_reloc_list_add(&cmd_buffer
->surface_relocs
,
2913 state
.offset
+ 8 * 4,
2914 view
->bo
, view
->offset
);
2916 bt_map
[ca
] = state
.offset
;
2922 for (uint32_t set
= 0; set
< layout
->num_sets
; set
++) {
2923 struct anv_descriptor_set_binding
*d
= &cmd_buffer
->descriptors
[set
];
2924 struct anv_descriptor_set_layout
*set_layout
= layout
->set
[set
].layout
;
2925 struct anv_descriptor_slot
*surface_slots
=
2926 set_layout
->stage
[stage
].surface_start
;
2928 uint32_t start
= bias
+ layout
->set
[set
].surface_start
[stage
];
2930 for (uint32_t b
= 0; b
< set_layout
->stage
[stage
].surface_count
; b
++) {
2931 struct anv_surface_view
*view
=
2932 d
->set
->descriptors
[surface_slots
[b
].index
].view
;
2937 struct anv_state state
=
2938 anv_cmd_buffer_alloc_surface_state(cmd_buffer
, 64, 64);
2940 if (state
.map
== NULL
)
2941 return VK_ERROR_OUT_OF_DEVICE_MEMORY
;
2944 if (surface_slots
[b
].dynamic_slot
>= 0) {
2945 uint32_t dynamic_offset
=
2946 d
->dynamic_offsets
[surface_slots
[b
].dynamic_slot
];
2948 offset
= view
->offset
+ dynamic_offset
;
2949 fill_buffer_surface_state(state
.map
, view
->format
, offset
,
2950 view
->range
- dynamic_offset
);
2952 offset
= view
->offset
;
2953 memcpy(state
.map
, view
->surface_state
.map
, 64);
2956 /* The address goes in dwords 8 and 9 of the SURFACE_STATE */
2957 *(uint64_t *)(state
.map
+ 8 * 4) =
2958 anv_reloc_list_add(&cmd_buffer
->surface_relocs
,
2960 state
.offset
+ 8 * 4,
2963 bt_map
[start
+ b
] = state
.offset
;
2971 cmd_buffer_emit_samplers(struct anv_cmd_buffer
*cmd_buffer
, unsigned stage
)
2973 struct anv_pipeline_layout
*layout
= cmd_buffer
->pipeline
->layout
;
2974 struct anv_state state
;
2979 uint32_t sampler_count
= layout
->stage
[stage
].sampler_count
;
2981 if (sampler_count
== 0)
2984 uint32_t size
= sampler_count
* 16;
2985 state
= anv_state_stream_alloc(&cmd_buffer
->dynamic_state_stream
, size
, 32);
2987 if (state
.map
== NULL
)
2988 return VK_ERROR_OUT_OF_DEVICE_MEMORY
;
2990 static const uint32_t sampler_state_opcodes
[] = {
2991 [VK_SHADER_STAGE_VERTEX
] = 43,
2992 [VK_SHADER_STAGE_TESS_CONTROL
] = 44, /* HS */
2993 [VK_SHADER_STAGE_TESS_EVALUATION
] = 45, /* DS */
2994 [VK_SHADER_STAGE_GEOMETRY
] = 46,
2995 [VK_SHADER_STAGE_FRAGMENT
] = 47,
2996 [VK_SHADER_STAGE_COMPUTE
] = 0,
2999 anv_batch_emit(&cmd_buffer
->batch
,
3000 GEN8_3DSTATE_SAMPLER_STATE_POINTERS_VS
,
3001 ._3DCommandSubOpcode
= sampler_state_opcodes
[stage
],
3002 .PointertoVSSamplerState
= state
.offset
);
3004 for (uint32_t set
= 0; set
< layout
->num_sets
; set
++) {
3005 struct anv_descriptor_set_binding
*d
= &cmd_buffer
->descriptors
[set
];
3006 struct anv_descriptor_set_layout
*set_layout
= layout
->set
[set
].layout
;
3007 struct anv_descriptor_slot
*sampler_slots
=
3008 set_layout
->stage
[stage
].sampler_start
;
3010 uint32_t start
= layout
->set
[set
].sampler_start
[stage
];
3012 for (uint32_t b
= 0; b
< set_layout
->stage
[stage
].sampler_count
; b
++) {
3013 struct anv_sampler
*sampler
=
3014 d
->set
->descriptors
[sampler_slots
[b
].index
].sampler
;
3019 memcpy(state
.map
+ (start
+ b
) * 16,
3020 sampler
->state
, sizeof(sampler
->state
));
3028 flush_descriptor_sets(struct anv_cmd_buffer
*cmd_buffer
)
3030 uint32_t s
, dirty
= cmd_buffer
->descriptors_dirty
&
3031 cmd_buffer
->pipeline
->active_stages
;
3034 for_each_bit(s
, dirty
) {
3035 result
= cmd_buffer_emit_binding_table(cmd_buffer
, s
);
3036 if (result
!= VK_SUCCESS
)
3039 result
= cmd_buffer_emit_samplers(cmd_buffer
, s
);
3040 if (result
!= VK_SUCCESS
)
3044 if (result
!= VK_SUCCESS
) {
3045 assert(result
== VK_ERROR_OUT_OF_DEVICE_MEMORY
);
3047 result
= anv_cmd_buffer_new_surface_state_bo(cmd_buffer
);
3048 assert(result
== VK_SUCCESS
);
3050 /* Re-emit all active binding tables */
3051 for_each_bit(s
, cmd_buffer
->pipeline
->active_stages
) {
3052 result
= cmd_buffer_emit_binding_table(cmd_buffer
, s
);
3053 result
= cmd_buffer_emit_samplers(cmd_buffer
, s
);
3056 /* It had better succeed this time */
3057 assert(result
== VK_SUCCESS
);
3060 cmd_buffer
->descriptors_dirty
&= ~cmd_buffer
->pipeline
->active_stages
;
3063 static struct anv_state
3064 anv_cmd_buffer_emit_dynamic(struct anv_cmd_buffer
*cmd_buffer
,
3065 uint32_t *a
, uint32_t dwords
, uint32_t alignment
)
3067 struct anv_state state
;
3069 state
= anv_state_stream_alloc(&cmd_buffer
->dynamic_state_stream
,
3070 dwords
* 4, alignment
);
3071 memcpy(state
.map
, a
, dwords
* 4);
3076 static struct anv_state
3077 anv_cmd_buffer_merge_dynamic(struct anv_cmd_buffer
*cmd_buffer
,
3078 uint32_t *a
, uint32_t *b
,
3079 uint32_t dwords
, uint32_t alignment
)
3081 struct anv_state state
;
3084 state
= anv_state_stream_alloc(&cmd_buffer
->dynamic_state_stream
,
3085 dwords
* 4, alignment
);
3087 for (uint32_t i
= 0; i
< dwords
; i
++)
3094 anv_cmd_buffer_flush_state(struct anv_cmd_buffer
*cmd_buffer
)
3096 struct anv_pipeline
*pipeline
= cmd_buffer
->pipeline
;
3099 uint32_t vb_emit
= cmd_buffer
->vb_dirty
& pipeline
->vb_used
;
3102 const uint32_t num_buffers
= __builtin_popcount(vb_emit
);
3103 const uint32_t num_dwords
= 1 + num_buffers
* 4;
3105 p
= anv_batch_emitn(&cmd_buffer
->batch
, num_dwords
,
3106 GEN8_3DSTATE_VERTEX_BUFFERS
);
3108 for_each_bit(vb
, vb_emit
) {
3109 struct anv_buffer
*buffer
= cmd_buffer
->vertex_bindings
[vb
].buffer
;
3110 uint32_t offset
= cmd_buffer
->vertex_bindings
[vb
].offset
;
3112 struct GEN8_VERTEX_BUFFER_STATE state
= {
3113 .VertexBufferIndex
= vb
,
3114 .MemoryObjectControlState
= GEN8_MOCS
,
3115 .AddressModifyEnable
= true,
3116 .BufferPitch
= pipeline
->binding_stride
[vb
],
3117 .BufferStartingAddress
= { buffer
->bo
, buffer
->offset
+ offset
},
3118 .BufferSize
= buffer
->size
- offset
3121 GEN8_VERTEX_BUFFER_STATE_pack(&cmd_buffer
->batch
, &p
[1 + i
* 4], &state
);
3126 if (cmd_buffer
->dirty
& ANV_CMD_BUFFER_PIPELINE_DIRTY
)
3127 anv_batch_emit_batch(&cmd_buffer
->batch
, &pipeline
->batch
);
3129 if (cmd_buffer
->descriptors_dirty
)
3130 flush_descriptor_sets(cmd_buffer
);
3132 if (cmd_buffer
->dirty
& (ANV_CMD_BUFFER_PIPELINE_DIRTY
| ANV_CMD_BUFFER_RS_DIRTY
)) {
3133 anv_batch_emit_merge(&cmd_buffer
->batch
,
3134 cmd_buffer
->rs_state
->state_sf
, pipeline
->state_sf
);
3135 anv_batch_emit_merge(&cmd_buffer
->batch
,
3136 cmd_buffer
->rs_state
->state_raster
, pipeline
->state_raster
);
3139 if (cmd_buffer
->ds_state
&&
3140 (cmd_buffer
->dirty
& (ANV_CMD_BUFFER_PIPELINE_DIRTY
| ANV_CMD_BUFFER_DS_DIRTY
)))
3141 anv_batch_emit_merge(&cmd_buffer
->batch
,
3142 cmd_buffer
->ds_state
->state_wm_depth_stencil
,
3143 pipeline
->state_wm_depth_stencil
);
3145 if (cmd_buffer
->dirty
& (ANV_CMD_BUFFER_CB_DIRTY
| ANV_CMD_BUFFER_DS_DIRTY
)) {
3146 struct anv_state state
;
3147 if (cmd_buffer
->ds_state
== NULL
)
3148 state
= anv_cmd_buffer_emit_dynamic(cmd_buffer
,
3149 cmd_buffer
->cb_state
->state_color_calc
,
3150 GEN8_COLOR_CALC_STATE_length
, 64);
3151 else if (cmd_buffer
->cb_state
== NULL
)
3152 state
= anv_cmd_buffer_emit_dynamic(cmd_buffer
,
3153 cmd_buffer
->ds_state
->state_color_calc
,
3154 GEN8_COLOR_CALC_STATE_length
, 64);
3156 state
= anv_cmd_buffer_merge_dynamic(cmd_buffer
,
3157 cmd_buffer
->ds_state
->state_color_calc
,
3158 cmd_buffer
->cb_state
->state_color_calc
,
3159 GEN8_COLOR_CALC_STATE_length
, 64);
3161 anv_batch_emit(&cmd_buffer
->batch
,
3162 GEN8_3DSTATE_CC_STATE_POINTERS
,
3163 .ColorCalcStatePointer
= state
.offset
,
3164 .ColorCalcStatePointerValid
= true);
3167 cmd_buffer
->vb_dirty
&= ~vb_emit
;
3168 cmd_buffer
->dirty
= 0;
3172 VkCmdBuffer cmdBuffer
,
3173 uint32_t firstVertex
,
3174 uint32_t vertexCount
,
3175 uint32_t firstInstance
,
3176 uint32_t instanceCount
)
3178 struct anv_cmd_buffer
*cmd_buffer
= (struct anv_cmd_buffer
*) cmdBuffer
;
3180 anv_cmd_buffer_flush_state(cmd_buffer
);
3182 anv_batch_emit(&cmd_buffer
->batch
, GEN8_3DPRIMITIVE
,
3183 .VertexAccessType
= SEQUENTIAL
,
3184 .VertexCountPerInstance
= vertexCount
,
3185 .StartVertexLocation
= firstVertex
,
3186 .InstanceCount
= instanceCount
,
3187 .StartInstanceLocation
= firstInstance
,
3188 .BaseVertexLocation
= 0);
3191 void anv_CmdDrawIndexed(
3192 VkCmdBuffer cmdBuffer
,
3193 uint32_t firstIndex
,
3194 uint32_t indexCount
,
3195 int32_t vertexOffset
,
3196 uint32_t firstInstance
,
3197 uint32_t instanceCount
)
3199 struct anv_cmd_buffer
*cmd_buffer
= (struct anv_cmd_buffer
*) cmdBuffer
;
3201 anv_cmd_buffer_flush_state(cmd_buffer
);
3203 anv_batch_emit(&cmd_buffer
->batch
, GEN8_3DPRIMITIVE
,
3204 .VertexAccessType
= RANDOM
,
3205 .VertexCountPerInstance
= indexCount
,
3206 .StartVertexLocation
= firstIndex
,
3207 .InstanceCount
= instanceCount
,
3208 .StartInstanceLocation
= firstInstance
,
3209 .BaseVertexLocation
= vertexOffset
);
3213 anv_batch_lrm(struct anv_batch
*batch
,
3214 uint32_t reg
, struct anv_bo
*bo
, uint32_t offset
)
3216 anv_batch_emit(batch
, GEN8_MI_LOAD_REGISTER_MEM
,
3217 .RegisterAddress
= reg
,
3218 .MemoryAddress
= { bo
, offset
});
3222 anv_batch_lri(struct anv_batch
*batch
, uint32_t reg
, uint32_t imm
)
3224 anv_batch_emit(batch
, GEN8_MI_LOAD_REGISTER_IMM
,
3225 .RegisterOffset
= reg
,
3229 /* Auto-Draw / Indirect Registers */
3230 #define GEN7_3DPRIM_END_OFFSET 0x2420
3231 #define GEN7_3DPRIM_START_VERTEX 0x2430
3232 #define GEN7_3DPRIM_VERTEX_COUNT 0x2434
3233 #define GEN7_3DPRIM_INSTANCE_COUNT 0x2438
3234 #define GEN7_3DPRIM_START_INSTANCE 0x243C
3235 #define GEN7_3DPRIM_BASE_VERTEX 0x2440
3237 void anv_CmdDrawIndirect(
3238 VkCmdBuffer cmdBuffer
,
3240 VkDeviceSize offset
,
3244 struct anv_cmd_buffer
*cmd_buffer
= (struct anv_cmd_buffer
*) cmdBuffer
;
3245 struct anv_buffer
*buffer
= (struct anv_buffer
*) _buffer
;
3246 struct anv_bo
*bo
= buffer
->bo
;
3247 uint32_t bo_offset
= buffer
->offset
+ offset
;
3249 anv_cmd_buffer_flush_state(cmd_buffer
);
3251 anv_batch_lrm(&cmd_buffer
->batch
, GEN7_3DPRIM_VERTEX_COUNT
, bo
, bo_offset
);
3252 anv_batch_lrm(&cmd_buffer
->batch
, GEN7_3DPRIM_INSTANCE_COUNT
, bo
, bo_offset
+ 4);
3253 anv_batch_lrm(&cmd_buffer
->batch
, GEN7_3DPRIM_START_VERTEX
, bo
, bo_offset
+ 8);
3254 anv_batch_lrm(&cmd_buffer
->batch
, GEN7_3DPRIM_START_INSTANCE
, bo
, bo_offset
+ 12);
3255 anv_batch_lri(&cmd_buffer
->batch
, GEN7_3DPRIM_BASE_VERTEX
, 0);
3257 anv_batch_emit(&cmd_buffer
->batch
, GEN8_3DPRIMITIVE
,
3258 .IndirectParameterEnable
= true,
3259 .VertexAccessType
= SEQUENTIAL
);
3262 void anv_CmdDrawIndexedIndirect(
3263 VkCmdBuffer cmdBuffer
,
3265 VkDeviceSize offset
,
3269 struct anv_cmd_buffer
*cmd_buffer
= (struct anv_cmd_buffer
*) cmdBuffer
;
3270 struct anv_buffer
*buffer
= (struct anv_buffer
*) _buffer
;
3271 struct anv_bo
*bo
= buffer
->bo
;
3272 uint32_t bo_offset
= buffer
->offset
+ offset
;
3274 anv_cmd_buffer_flush_state(cmd_buffer
);
3276 anv_batch_lrm(&cmd_buffer
->batch
, GEN7_3DPRIM_VERTEX_COUNT
, bo
, bo_offset
);
3277 anv_batch_lrm(&cmd_buffer
->batch
, GEN7_3DPRIM_INSTANCE_COUNT
, bo
, bo_offset
+ 4);
3278 anv_batch_lrm(&cmd_buffer
->batch
, GEN7_3DPRIM_START_VERTEX
, bo
, bo_offset
+ 8);
3279 anv_batch_lrm(&cmd_buffer
->batch
, GEN7_3DPRIM_BASE_VERTEX
, bo
, bo_offset
+ 12);
3280 anv_batch_lrm(&cmd_buffer
->batch
, GEN7_3DPRIM_START_INSTANCE
, bo
, bo_offset
+ 16);
3282 anv_batch_emit(&cmd_buffer
->batch
, GEN8_3DPRIMITIVE
,
3283 .IndirectParameterEnable
= true,
3284 .VertexAccessType
= RANDOM
);
3287 void anv_CmdDispatch(
3288 VkCmdBuffer cmdBuffer
,
3296 void anv_CmdDispatchIndirect(
3297 VkCmdBuffer cmdBuffer
,
3299 VkDeviceSize offset
)
3304 void anv_CmdSetEvent(
3305 VkCmdBuffer cmdBuffer
,
3307 VkPipeEvent pipeEvent
)
3312 void anv_CmdResetEvent(
3313 VkCmdBuffer cmdBuffer
,
3315 VkPipeEvent pipeEvent
)
3320 void anv_CmdWaitEvents(
3321 VkCmdBuffer cmdBuffer
,
3322 VkWaitEvent waitEvent
,
3323 uint32_t eventCount
,
3324 const VkEvent
* pEvents
,
3325 uint32_t memBarrierCount
,
3326 const void** ppMemBarriers
)
3331 void anv_CmdPipelineBarrier(
3332 VkCmdBuffer cmdBuffer
,
3333 VkWaitEvent waitEvent
,
3334 uint32_t pipeEventCount
,
3335 const VkPipeEvent
* pPipeEvents
,
3336 uint32_t memBarrierCount
,
3337 const void** ppMemBarriers
)
3342 void anv_CmdInitAtomicCounters(
3343 VkCmdBuffer cmdBuffer
,
3344 VkPipelineBindPoint pipelineBindPoint
,
3345 uint32_t startCounter
,
3346 uint32_t counterCount
,
3347 const uint32_t* pData
)
3352 void anv_CmdLoadAtomicCounters(
3353 VkCmdBuffer cmdBuffer
,
3354 VkPipelineBindPoint pipelineBindPoint
,
3355 uint32_t startCounter
,
3356 uint32_t counterCount
,
3358 VkDeviceSize srcOffset
)
3363 void anv_CmdSaveAtomicCounters(
3364 VkCmdBuffer cmdBuffer
,
3365 VkPipelineBindPoint pipelineBindPoint
,
3366 uint32_t startCounter
,
3367 uint32_t counterCount
,
3368 VkBuffer destBuffer
,
3369 VkDeviceSize destOffset
)
3375 anv_framebuffer_destroy(struct anv_device
*device
,
3376 struct anv_object
*object
,
3377 VkObjectType obj_type
)
3379 struct anv_framebuffer
*fb
= (struct anv_framebuffer
*)object
;
3381 assert(obj_type
== VK_OBJECT_TYPE_FRAMEBUFFER
);
3383 anv_DestroyObject((VkDevice
) device
,
3384 VK_OBJECT_TYPE_DYNAMIC_VP_STATE
,
3387 anv_device_free(device
, fb
);
3390 VkResult
anv_CreateFramebuffer(
3392 const VkFramebufferCreateInfo
* pCreateInfo
,
3393 VkFramebuffer
* pFramebuffer
)
3395 struct anv_device
*device
= (struct anv_device
*) _device
;
3396 struct anv_framebuffer
*framebuffer
;
3398 static const struct anv_depth_stencil_view null_view
=
3399 { .depth_format
= D16_UNORM
, .depth_stride
= 0, .stencil_stride
= 0 };
3401 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO
);
3403 framebuffer
= anv_device_alloc(device
, sizeof(*framebuffer
), 8,
3404 VK_SYSTEM_ALLOC_TYPE_API_OBJECT
);
3405 if (framebuffer
== NULL
)
3406 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
3408 framebuffer
->base
.destructor
= anv_framebuffer_destroy
;
3410 framebuffer
->color_attachment_count
= pCreateInfo
->colorAttachmentCount
;
3411 for (uint32_t i
= 0; i
< pCreateInfo
->colorAttachmentCount
; i
++) {
3412 framebuffer
->color_attachments
[i
] =
3413 (struct anv_surface_view
*) pCreateInfo
->pColorAttachments
[i
].view
;
3416 if (pCreateInfo
->pDepthStencilAttachment
) {
3417 framebuffer
->depth_stencil
=
3418 (struct anv_depth_stencil_view
*) pCreateInfo
->pDepthStencilAttachment
->view
;
3420 framebuffer
->depth_stencil
= &null_view
;
3423 framebuffer
->sample_count
= pCreateInfo
->sampleCount
;
3424 framebuffer
->width
= pCreateInfo
->width
;
3425 framebuffer
->height
= pCreateInfo
->height
;
3426 framebuffer
->layers
= pCreateInfo
->layers
;
3428 anv_CreateDynamicViewportState((VkDevice
) device
,
3429 &(VkDynamicVpStateCreateInfo
) {
3430 .sType
= VK_STRUCTURE_TYPE_DYNAMIC_VP_STATE_CREATE_INFO
,
3431 .viewportAndScissorCount
= 1,
3432 .pViewports
= (VkViewport
[]) {
3436 .width
= pCreateInfo
->width
,
3437 .height
= pCreateInfo
->height
,
3442 .pScissors
= (VkRect
[]) {
3444 { pCreateInfo
->width
, pCreateInfo
->height
} },
3447 &framebuffer
->vp_state
);
3449 *pFramebuffer
= (VkFramebuffer
) framebuffer
;
3454 VkResult
anv_CreateRenderPass(
3456 const VkRenderPassCreateInfo
* pCreateInfo
,
3457 VkRenderPass
* pRenderPass
)
3459 struct anv_device
*device
= (struct anv_device
*) _device
;
3460 struct anv_render_pass
*pass
;
3463 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO
);
3465 size
= sizeof(*pass
) +
3466 pCreateInfo
->layers
* sizeof(struct anv_render_pass_layer
);
3467 pass
= anv_device_alloc(device
, size
, 8,
3468 VK_SYSTEM_ALLOC_TYPE_API_OBJECT
);
3470 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
3472 pass
->render_area
= pCreateInfo
->renderArea
;
3474 pass
->num_layers
= pCreateInfo
->layers
;
3476 pass
->num_clear_layers
= 0;
3477 for (uint32_t i
= 0; i
< pCreateInfo
->layers
; i
++) {
3478 pass
->layers
[i
].color_load_op
= pCreateInfo
->pColorLoadOps
[i
];
3479 pass
->layers
[i
].clear_color
= pCreateInfo
->pColorLoadClearValues
[i
];
3480 if (pass
->layers
[i
].color_load_op
== VK_ATTACHMENT_LOAD_OP_CLEAR
)
3481 pass
->num_clear_layers
++;
3484 *pRenderPass
= (VkRenderPass
) pass
;
3490 anv_cmd_buffer_emit_depth_stencil(struct anv_cmd_buffer
*cmd_buffer
,
3491 struct anv_render_pass
*pass
)
3493 const struct anv_depth_stencil_view
*view
=
3494 cmd_buffer
->framebuffer
->depth_stencil
;
3496 /* FIXME: Implement the PMA stall W/A */
3498 anv_batch_emit(&cmd_buffer
->batch
, GEN8_3DSTATE_DEPTH_BUFFER
,
3499 .SurfaceType
= SURFTYPE_2D
,
3500 .DepthWriteEnable
= view
->depth_stride
> 0,
3501 .StencilWriteEnable
= view
->stencil_stride
> 0,
3502 .HierarchicalDepthBufferEnable
= false,
3503 .SurfaceFormat
= view
->depth_format
,
3504 .SurfacePitch
= view
->depth_stride
> 0 ? view
->depth_stride
- 1 : 0,
3505 .SurfaceBaseAddress
= { view
->bo
, view
->depth_offset
},
3506 .Height
= pass
->render_area
.extent
.height
- 1,
3507 .Width
= pass
->render_area
.extent
.width
- 1,
3510 .MinimumArrayElement
= 0,
3511 .DepthBufferObjectControlState
= GEN8_MOCS
,
3512 .RenderTargetViewExtent
= 1 - 1,
3513 .SurfaceQPitch
= 0);
3515 /* Disable hierarchial depth buffers. */
3516 anv_batch_emit(&cmd_buffer
->batch
, GEN8_3DSTATE_HIER_DEPTH_BUFFER
);
3518 anv_batch_emit(&cmd_buffer
->batch
, GEN8_3DSTATE_STENCIL_BUFFER
,
3519 .StencilBufferEnable
= view
->stencil_stride
> 0,
3520 .StencilBufferObjectControlState
= GEN8_MOCS
,
3521 .SurfacePitch
= view
->stencil_stride
> 0 ? view
->stencil_stride
- 1 : 0,
3522 .SurfaceBaseAddress
= { view
->bo
, view
->stencil_offset
},
3523 .SurfaceQPitch
= 0);
3525 /* Clear the clear params. */
3526 anv_batch_emit(&cmd_buffer
->batch
, GEN8_3DSTATE_CLEAR_PARAMS
);
3529 void anv_CmdBeginRenderPass(
3530 VkCmdBuffer cmdBuffer
,
3531 const VkRenderPassBegin
* pRenderPassBegin
)
3533 struct anv_cmd_buffer
*cmd_buffer
= (struct anv_cmd_buffer
*) cmdBuffer
;
3534 struct anv_render_pass
*pass
= (struct anv_render_pass
*) pRenderPassBegin
->renderPass
;
3535 struct anv_framebuffer
*framebuffer
=
3536 (struct anv_framebuffer
*) pRenderPassBegin
->framebuffer
;
3538 cmd_buffer
->framebuffer
= framebuffer
;
3540 cmd_buffer
->descriptors_dirty
|= VK_SHADER_STAGE_FRAGMENT_BIT
;
3542 anv_batch_emit(&cmd_buffer
->batch
, GEN8_3DSTATE_DRAWING_RECTANGLE
,
3543 .ClippedDrawingRectangleYMin
= pass
->render_area
.offset
.y
,
3544 .ClippedDrawingRectangleXMin
= pass
->render_area
.offset
.x
,
3545 .ClippedDrawingRectangleYMax
=
3546 pass
->render_area
.offset
.y
+ pass
->render_area
.extent
.height
- 1,
3547 .ClippedDrawingRectangleXMax
=
3548 pass
->render_area
.offset
.x
+ pass
->render_area
.extent
.width
- 1,
3549 .DrawingRectangleOriginY
= 0,
3550 .DrawingRectangleOriginX
= 0);
3552 anv_cmd_buffer_emit_depth_stencil(cmd_buffer
, pass
);
3554 anv_cmd_buffer_clear(cmd_buffer
, pass
);
3557 void anv_CmdEndRenderPass(
3558 VkCmdBuffer cmdBuffer
,
3559 VkRenderPass renderPass
)
3561 /* Emit a flushing pipe control at the end of a pass. This is kind of a
3562 * hack but it ensures that render targets always actually get written.
3563 * Eventually, we should do flushing based on image format transitions
3564 * or something of that nature.
3566 struct anv_cmd_buffer
*cmd_buffer
= (struct anv_cmd_buffer
*)cmdBuffer
;
3567 anv_batch_emit(&cmd_buffer
->batch
, GEN8_PIPE_CONTROL
,
3568 .PostSyncOperation
= NoWrite
,
3569 .RenderTargetCacheFlushEnable
= true,
3570 .InstructionCacheInvalidateEnable
= true,
3571 .DepthCacheFlushEnable
= true,
3572 .VFCacheInvalidationEnable
= true,
3573 .TextureCacheInvalidationEnable
= true,
3574 .CommandStreamerStallEnable
= true);
3577 void vkCmdDbgMarkerBegin(
3578 VkCmdBuffer cmdBuffer
,
3579 const char* pMarker
)
3580 __attribute__ ((visibility ("default")));
3582 void vkCmdDbgMarkerEnd(
3583 VkCmdBuffer cmdBuffer
)
3584 __attribute__ ((visibility ("default")));
3586 VkResult
vkDbgSetObjectTag(
3591 __attribute__ ((visibility ("default")));
3594 void vkCmdDbgMarkerBegin(
3595 VkCmdBuffer cmdBuffer
,
3596 const char* pMarker
)
3600 void vkCmdDbgMarkerEnd(
3601 VkCmdBuffer cmdBuffer
)
3605 VkResult
vkDbgSetObjectTag(