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 VG(VALGRIND_CHECK_MEM_IS_DEFINED(batch
->start
, bbo
->length
));
663 bbo
->num_relocs
= batch
->relocs
.num_relocs
- bbo
->first_reloc
;
667 anv_batch_bo_destroy(struct anv_batch_bo
*bbo
, struct anv_device
*device
)
669 anv_bo_pool_free(&device
->batch_bo_pool
, &bbo
->bo
);
670 anv_device_free(device
, bbo
);
674 anv_batch_emit_dwords(struct anv_batch
*batch
, int num_dwords
)
676 if (batch
->next
+ num_dwords
* 4 > batch
->end
)
677 batch
->extend_cb(batch
, batch
->user_data
);
679 void *p
= batch
->next
;
681 batch
->next
+= num_dwords
* 4;
682 assert(batch
->next
<= batch
->end
);
688 anv_reloc_list_append(struct anv_reloc_list
*list
, struct anv_device
*device
,
689 struct anv_reloc_list
*other
, uint32_t offset
)
691 anv_reloc_list_grow(list
, device
, other
->num_relocs
);
692 /* TODO: Handle failure */
694 memcpy(&list
->relocs
[list
->num_relocs
], &other
->relocs
[0],
695 other
->num_relocs
* sizeof(other
->relocs
[0]));
696 memcpy(&list
->reloc_bos
[list
->num_relocs
], &other
->reloc_bos
[0],
697 other
->num_relocs
* sizeof(other
->reloc_bos
[0]));
699 for (uint32_t i
= 0; i
< other
->num_relocs
; i
++)
700 list
->relocs
[i
+ list
->num_relocs
].offset
+= offset
;
702 list
->num_relocs
+= other
->num_relocs
;
706 anv_reloc_list_add(struct anv_reloc_list
*list
, struct anv_device
*device
,
707 uint32_t offset
, struct anv_bo
*target_bo
, uint32_t delta
)
709 struct drm_i915_gem_relocation_entry
*entry
;
712 anv_reloc_list_grow(list
, device
, 1);
713 /* TODO: Handle failure */
715 /* XXX: Can we use I915_EXEC_HANDLE_LUT? */
716 index
= list
->num_relocs
++;
717 list
->reloc_bos
[index
] = target_bo
;
718 entry
= &list
->relocs
[index
];
719 entry
->target_handle
= target_bo
->gem_handle
;
720 entry
->delta
= delta
;
721 entry
->offset
= offset
;
722 entry
->presumed_offset
= target_bo
->offset
;
723 entry
->read_domains
= 0;
724 entry
->write_domain
= 0;
726 return target_bo
->offset
+ delta
;
730 anv_batch_emit_batch(struct anv_batch
*batch
, struct anv_batch
*other
)
732 uint32_t size
, offset
;
734 size
= other
->next
- other
->start
;
735 assert(size
% 4 == 0);
737 if (batch
->next
+ size
> batch
->end
)
738 batch
->extend_cb(batch
, batch
->user_data
);
740 assert(batch
->next
+ size
<= batch
->end
);
742 memcpy(batch
->next
, other
->start
, size
);
744 offset
= batch
->next
- batch
->start
;
745 anv_reloc_list_append(&batch
->relocs
, batch
->device
,
746 &other
->relocs
, offset
);
752 anv_batch_emit_reloc(struct anv_batch
*batch
,
753 void *location
, struct anv_bo
*bo
, uint32_t delta
)
755 return anv_reloc_list_add(&batch
->relocs
, batch
->device
,
756 location
- batch
->start
, bo
, delta
);
759 VkResult
anv_QueueSubmit(
761 uint32_t cmdBufferCount
,
762 const VkCmdBuffer
* pCmdBuffers
,
765 struct anv_queue
*queue
= (struct anv_queue
*) _queue
;
766 struct anv_device
*device
= queue
->device
;
767 struct anv_fence
*fence
= (struct anv_fence
*) _fence
;
770 for (uint32_t i
= 0; i
< cmdBufferCount
; i
++) {
771 struct anv_cmd_buffer
*cmd_buffer
=
772 (struct anv_cmd_buffer
*) pCmdBuffers
[i
];
774 if (device
->dump_aub
)
775 anv_cmd_buffer_dump(cmd_buffer
);
777 if (!device
->no_hw
) {
778 ret
= anv_gem_execbuffer(device
, &cmd_buffer
->execbuf
);
780 return vk_error(VK_ERROR_UNKNOWN
);
783 ret
= anv_gem_execbuffer(device
, &fence
->execbuf
);
785 return vk_error(VK_ERROR_UNKNOWN
);
788 for (uint32_t i
= 0; i
< cmd_buffer
->bo_count
; i
++)
789 cmd_buffer
->exec2_bos
[i
]->offset
= cmd_buffer
->exec2_objects
[i
].offset
;
791 *(uint32_t *)queue
->completed_serial
.map
= cmd_buffer
->serial
;
798 VkResult
anv_QueueAddMemReferences(
801 const VkDeviceMemory
* pMems
)
806 VkResult
anv_QueueRemoveMemReferences(
809 const VkDeviceMemory
* pMems
)
814 VkResult
anv_QueueWaitIdle(
817 struct anv_queue
*queue
= (struct anv_queue
*) _queue
;
819 return vkDeviceWaitIdle((VkDevice
) queue
->device
);
822 VkResult
anv_DeviceWaitIdle(
825 struct anv_device
*device
= (struct anv_device
*) _device
;
826 struct anv_state state
;
827 struct anv_batch batch
;
828 struct drm_i915_gem_execbuffer2 execbuf
;
829 struct drm_i915_gem_exec_object2 exec2_objects
[1];
830 struct anv_bo
*bo
= NULL
;
835 state
= anv_state_pool_alloc(&device
->dynamic_state_pool
, 32, 32);
836 bo
= &device
->dynamic_state_pool
.block_pool
->bo
;
837 batch
.start
= batch
.next
= state
.map
;
838 batch
.end
= state
.map
+ 32;
839 anv_batch_emit(&batch
, GEN8_MI_BATCH_BUFFER_END
);
840 anv_batch_emit(&batch
, GEN8_MI_NOOP
);
842 exec2_objects
[0].handle
= bo
->gem_handle
;
843 exec2_objects
[0].relocation_count
= 0;
844 exec2_objects
[0].relocs_ptr
= 0;
845 exec2_objects
[0].alignment
= 0;
846 exec2_objects
[0].offset
= bo
->offset
;
847 exec2_objects
[0].flags
= 0;
848 exec2_objects
[0].rsvd1
= 0;
849 exec2_objects
[0].rsvd2
= 0;
851 execbuf
.buffers_ptr
= (uintptr_t) exec2_objects
;
852 execbuf
.buffer_count
= 1;
853 execbuf
.batch_start_offset
= state
.offset
;
854 execbuf
.batch_len
= batch
.next
- state
.map
;
855 execbuf
.cliprects_ptr
= 0;
856 execbuf
.num_cliprects
= 0;
861 I915_EXEC_HANDLE_LUT
| I915_EXEC_NO_RELOC
| I915_EXEC_RENDER
;
862 execbuf
.rsvd1
= device
->context_id
;
865 if (!device
->no_hw
) {
866 ret
= anv_gem_execbuffer(device
, &execbuf
);
868 result
= vk_error(VK_ERROR_UNKNOWN
);
873 ret
= anv_gem_wait(device
, bo
->gem_handle
, &timeout
);
875 result
= vk_error(VK_ERROR_UNKNOWN
);
880 anv_state_pool_free(&device
->dynamic_state_pool
, state
);
885 anv_state_pool_free(&device
->dynamic_state_pool
, state
);
891 anv_device_alloc(struct anv_device
* device
,
894 VkSystemAllocType allocType
)
896 return device
->instance
->pfnAlloc(device
->instance
->pAllocUserData
,
903 anv_device_free(struct anv_device
* device
,
906 return device
->instance
->pfnFree(device
->instance
->pAllocUserData
,
911 anv_bo_init_new(struct anv_bo
*bo
, struct anv_device
*device
, uint64_t size
)
913 bo
->gem_handle
= anv_gem_create(device
, size
);
915 return vk_error(VK_ERROR_OUT_OF_DEVICE_MEMORY
);
925 VkResult
anv_AllocMemory(
927 const VkMemoryAllocInfo
* pAllocInfo
,
928 VkDeviceMemory
* pMem
)
930 struct anv_device
*device
= (struct anv_device
*) _device
;
931 struct anv_device_memory
*mem
;
934 assert(pAllocInfo
->sType
== VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO
);
936 mem
= anv_device_alloc(device
, sizeof(*mem
), 8,
937 VK_SYSTEM_ALLOC_TYPE_API_OBJECT
);
939 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
941 result
= anv_bo_init_new(&mem
->bo
, device
, pAllocInfo
->allocationSize
);
942 if (result
!= VK_SUCCESS
)
945 *pMem
= (VkDeviceMemory
) mem
;
950 anv_device_free(device
, mem
);
955 VkResult
anv_FreeMemory(
959 struct anv_device
*device
= (struct anv_device
*) _device
;
960 struct anv_device_memory
*mem
= (struct anv_device_memory
*) _mem
;
963 anv_gem_munmap(mem
->bo
.map
, mem
->bo
.size
);
965 if (mem
->bo
.gem_handle
!= 0)
966 anv_gem_close(device
, mem
->bo
.gem_handle
);
968 anv_device_free(device
, mem
);
973 VkResult
anv_SetMemoryPriority(
976 VkMemoryPriority priority
)
981 VkResult
anv_MapMemory(
986 VkMemoryMapFlags flags
,
989 struct anv_device
*device
= (struct anv_device
*) _device
;
990 struct anv_device_memory
*mem
= (struct anv_device_memory
*) _mem
;
992 /* FIXME: Is this supposed to be thread safe? Since vkUnmapMemory() only
993 * takes a VkDeviceMemory pointer, it seems like only one map of the memory
994 * at a time is valid. We could just mmap up front and return an offset
995 * pointer here, but that may exhaust virtual memory on 32 bit
998 mem
->map
= anv_gem_mmap(device
, mem
->bo
.gem_handle
, offset
, size
);
999 mem
->map_size
= size
;
1006 VkResult
anv_UnmapMemory(
1008 VkDeviceMemory _mem
)
1010 struct anv_device_memory
*mem
= (struct anv_device_memory
*) _mem
;
1012 anv_gem_munmap(mem
->map
, mem
->map_size
);
1017 VkResult
anv_FlushMappedMemory(
1020 VkDeviceSize offset
,
1023 /* clflush here for !llc platforms */
1028 VkResult
anv_PinSystemMemory(
1030 const void* pSysMem
,
1032 VkDeviceMemory
* pMem
)
1037 VkResult
anv_GetMultiDeviceCompatibility(
1038 VkPhysicalDevice physicalDevice0
,
1039 VkPhysicalDevice physicalDevice1
,
1040 VkPhysicalDeviceCompatibilityInfo
* pInfo
)
1042 return VK_UNSUPPORTED
;
1045 VkResult
anv_OpenSharedMemory(
1047 const VkMemoryOpenInfo
* pOpenInfo
,
1048 VkDeviceMemory
* pMem
)
1050 return VK_UNSUPPORTED
;
1053 VkResult
anv_OpenSharedSemaphore(
1055 const VkSemaphoreOpenInfo
* pOpenInfo
,
1056 VkSemaphore
* pSemaphore
)
1058 return VK_UNSUPPORTED
;
1061 VkResult
anv_OpenPeerMemory(
1063 const VkPeerMemoryOpenInfo
* pOpenInfo
,
1064 VkDeviceMemory
* pMem
)
1066 return VK_UNSUPPORTED
;
1069 VkResult
anv_OpenPeerImage(
1071 const VkPeerImageOpenInfo
* pOpenInfo
,
1073 VkDeviceMemory
* pMem
)
1075 return VK_UNSUPPORTED
;
1078 VkResult
anv_DestroyObject(
1080 VkObjectType objType
,
1083 struct anv_device
*device
= (struct anv_device
*) _device
;
1084 struct anv_object
*object
= (struct anv_object
*) _object
;
1087 case VK_OBJECT_TYPE_INSTANCE
:
1088 return anv_DestroyInstance((VkInstance
) _object
);
1090 case VK_OBJECT_TYPE_PHYSICAL_DEVICE
:
1091 /* We don't want to actually destroy physical devices */
1094 case VK_OBJECT_TYPE_DEVICE
:
1095 assert(_device
== (VkDevice
) _object
);
1096 return anv_DestroyDevice((VkDevice
) _object
);
1098 case VK_OBJECT_TYPE_QUEUE
:
1102 case VK_OBJECT_TYPE_DEVICE_MEMORY
:
1103 return anv_FreeMemory(_device
, (VkDeviceMemory
) _object
);
1105 case VK_OBJECT_TYPE_DESCRIPTOR_POOL
:
1106 /* These are just dummys anyway, so we don't need to destroy them */
1109 case VK_OBJECT_TYPE_BUFFER
:
1110 case VK_OBJECT_TYPE_IMAGE
:
1111 case VK_OBJECT_TYPE_DEPTH_STENCIL_VIEW
:
1112 case VK_OBJECT_TYPE_SHADER
:
1113 case VK_OBJECT_TYPE_PIPELINE_LAYOUT
:
1114 case VK_OBJECT_TYPE_SAMPLER
:
1115 case VK_OBJECT_TYPE_DESCRIPTOR_SET
:
1116 case VK_OBJECT_TYPE_DESCRIPTOR_SET_LAYOUT
:
1117 case VK_OBJECT_TYPE_DYNAMIC_RS_STATE
:
1118 case VK_OBJECT_TYPE_DYNAMIC_CB_STATE
:
1119 case VK_OBJECT_TYPE_DYNAMIC_DS_STATE
:
1120 case VK_OBJECT_TYPE_RENDER_PASS
:
1121 /* These are trivially destroyable */
1122 anv_device_free(device
, (void *) _object
);
1125 case VK_OBJECT_TYPE_COMMAND_BUFFER
:
1126 case VK_OBJECT_TYPE_PIPELINE
:
1127 case VK_OBJECT_TYPE_DYNAMIC_VP_STATE
:
1128 case VK_OBJECT_TYPE_FENCE
:
1129 case VK_OBJECT_TYPE_QUERY_POOL
:
1130 case VK_OBJECT_TYPE_FRAMEBUFFER
:
1131 case VK_OBJECT_TYPE_BUFFER_VIEW
:
1132 case VK_OBJECT_TYPE_IMAGE_VIEW
:
1133 case VK_OBJECT_TYPE_COLOR_ATTACHMENT_VIEW
:
1134 (object
->destructor
)(device
, object
, objType
);
1137 case VK_OBJECT_TYPE_SEMAPHORE
:
1138 case VK_OBJECT_TYPE_EVENT
:
1139 stub_return(VK_UNSUPPORTED
);
1142 unreachable("Invalid object type");
1147 fill_memory_requirements(
1148 VkObjectType objType
,
1150 VkMemoryRequirements
* memory_requirements
)
1152 struct anv_buffer
*buffer
;
1153 struct anv_image
*image
;
1155 memory_requirements
->memPropsAllowed
=
1156 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT
|
1157 VK_MEMORY_PROPERTY_HOST_DEVICE_COHERENT_BIT
|
1158 /* VK_MEMORY_PROPERTY_HOST_UNCACHED_BIT | */
1159 VK_MEMORY_PROPERTY_HOST_WRITE_COMBINED_BIT
|
1160 VK_MEMORY_PROPERTY_PREFER_HOST_LOCAL
|
1161 VK_MEMORY_PROPERTY_SHAREABLE_BIT
;
1163 memory_requirements
->memPropsRequired
= 0;
1166 case VK_OBJECT_TYPE_BUFFER
:
1167 buffer
= (struct anv_buffer
*) object
;
1168 memory_requirements
->size
= buffer
->size
;
1169 memory_requirements
->alignment
= 16;
1171 case VK_OBJECT_TYPE_IMAGE
:
1172 image
= (struct anv_image
*) object
;
1173 memory_requirements
->size
= image
->size
;
1174 memory_requirements
->alignment
= image
->alignment
;
1177 memory_requirements
->size
= 0;
1183 get_allocation_count(VkObjectType objType
)
1186 case VK_OBJECT_TYPE_BUFFER
:
1187 case VK_OBJECT_TYPE_IMAGE
:
1194 VkResult
anv_GetObjectInfo(
1196 VkObjectType objType
,
1198 VkObjectInfoType infoType
,
1202 VkMemoryRequirements memory_requirements
;
1206 case VK_OBJECT_INFO_TYPE_MEMORY_REQUIREMENTS
:
1207 *pDataSize
= sizeof(memory_requirements
);
1211 fill_memory_requirements(objType
, object
, pData
);
1214 case VK_OBJECT_INFO_TYPE_MEMORY_ALLOCATION_COUNT
:
1215 *pDataSize
= sizeof(count
);
1220 *count
= get_allocation_count(objType
);
1224 return VK_UNSUPPORTED
;
1229 VkResult
anv_QueueBindObjectMemory(
1231 VkObjectType objType
,
1233 uint32_t allocationIdx
,
1234 VkDeviceMemory _mem
,
1235 VkDeviceSize memOffset
)
1237 struct anv_buffer
*buffer
;
1238 struct anv_image
*image
;
1239 struct anv_device_memory
*mem
= (struct anv_device_memory
*) _mem
;
1242 case VK_OBJECT_TYPE_BUFFER
:
1243 buffer
= (struct anv_buffer
*) object
;
1244 buffer
->bo
= &mem
->bo
;
1245 buffer
->offset
= memOffset
;
1247 case VK_OBJECT_TYPE_IMAGE
:
1248 image
= (struct anv_image
*) object
;
1249 image
->bo
= &mem
->bo
;
1250 image
->offset
= memOffset
;
1259 VkResult
anv_QueueBindObjectMemoryRange(
1261 VkObjectType objType
,
1263 uint32_t allocationIdx
,
1264 VkDeviceSize rangeOffset
,
1265 VkDeviceSize rangeSize
,
1267 VkDeviceSize memOffset
)
1269 stub_return(VK_UNSUPPORTED
);
1272 VkResult
anv_QueueBindImageMemoryRange(
1275 uint32_t allocationIdx
,
1276 const VkImageMemoryBindInfo
* pBindInfo
,
1278 VkDeviceSize memOffset
)
1280 stub_return(VK_UNSUPPORTED
);
1284 anv_fence_destroy(struct anv_device
*device
,
1285 struct anv_object
*object
,
1286 VkObjectType obj_type
)
1288 struct anv_fence
*fence
= (struct anv_fence
*) object
;
1290 assert(obj_type
== VK_OBJECT_TYPE_FENCE
);
1292 anv_gem_munmap(fence
->bo
.map
, fence
->bo
.size
);
1293 anv_gem_close(device
, fence
->bo
.gem_handle
);
1294 anv_device_free(device
, fence
);
1297 VkResult
anv_CreateFence(
1299 const VkFenceCreateInfo
* pCreateInfo
,
1302 struct anv_device
*device
= (struct anv_device
*) _device
;
1303 struct anv_fence
*fence
;
1304 struct anv_batch batch
;
1307 const uint32_t fence_size
= 128;
1309 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_FENCE_CREATE_INFO
);
1311 fence
= anv_device_alloc(device
, sizeof(*fence
), 8,
1312 VK_SYSTEM_ALLOC_TYPE_API_OBJECT
);
1314 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
1316 result
= anv_bo_init_new(&fence
->bo
, device
, fence_size
);
1317 if (result
!= VK_SUCCESS
)
1320 fence
->base
.destructor
= anv_fence_destroy
;
1323 anv_gem_mmap(device
, fence
->bo
.gem_handle
, 0, fence
->bo
.size
);
1324 batch
.next
= batch
.start
= fence
->bo
.map
;
1325 batch
.end
= fence
->bo
.map
+ fence
->bo
.size
;
1326 anv_batch_emit(&batch
, GEN8_MI_BATCH_BUFFER_END
);
1327 anv_batch_emit(&batch
, GEN8_MI_NOOP
);
1329 fence
->exec2_objects
[0].handle
= fence
->bo
.gem_handle
;
1330 fence
->exec2_objects
[0].relocation_count
= 0;
1331 fence
->exec2_objects
[0].relocs_ptr
= 0;
1332 fence
->exec2_objects
[0].alignment
= 0;
1333 fence
->exec2_objects
[0].offset
= fence
->bo
.offset
;
1334 fence
->exec2_objects
[0].flags
= 0;
1335 fence
->exec2_objects
[0].rsvd1
= 0;
1336 fence
->exec2_objects
[0].rsvd2
= 0;
1338 fence
->execbuf
.buffers_ptr
= (uintptr_t) fence
->exec2_objects
;
1339 fence
->execbuf
.buffer_count
= 1;
1340 fence
->execbuf
.batch_start_offset
= 0;
1341 fence
->execbuf
.batch_len
= batch
.next
- fence
->bo
.map
;
1342 fence
->execbuf
.cliprects_ptr
= 0;
1343 fence
->execbuf
.num_cliprects
= 0;
1344 fence
->execbuf
.DR1
= 0;
1345 fence
->execbuf
.DR4
= 0;
1347 fence
->execbuf
.flags
=
1348 I915_EXEC_HANDLE_LUT
| I915_EXEC_NO_RELOC
| I915_EXEC_RENDER
;
1349 fence
->execbuf
.rsvd1
= device
->context_id
;
1350 fence
->execbuf
.rsvd2
= 0;
1352 *pFence
= (VkFence
) fence
;
1357 anv_device_free(device
, fence
);
1362 VkResult
anv_ResetFences(
1364 uint32_t fenceCount
,
1367 struct anv_fence
**fences
= (struct anv_fence
**) pFences
;
1369 for (uint32_t i
= 0; i
< fenceCount
; i
++)
1370 fences
[i
]->ready
= false;
1375 VkResult
anv_GetFenceStatus(
1379 struct anv_device
*device
= (struct anv_device
*) _device
;
1380 struct anv_fence
*fence
= (struct anv_fence
*) _fence
;
1387 ret
= anv_gem_wait(device
, fence
->bo
.gem_handle
, &t
);
1389 fence
->ready
= true;
1393 return VK_NOT_READY
;
1396 VkResult
anv_WaitForFences(
1398 uint32_t fenceCount
,
1399 const VkFence
* pFences
,
1403 struct anv_device
*device
= (struct anv_device
*) _device
;
1404 struct anv_fence
**fences
= (struct anv_fence
**) pFences
;
1405 int64_t t
= timeout
;
1408 /* FIXME: handle !waitAll */
1410 for (uint32_t i
= 0; i
< fenceCount
; i
++) {
1411 ret
= anv_gem_wait(device
, fences
[i
]->bo
.gem_handle
, &t
);
1412 if (ret
== -1 && errno
== ETIME
)
1415 return vk_error(VK_ERROR_UNKNOWN
);
1421 // Queue semaphore functions
1423 VkResult
anv_CreateSemaphore(
1425 const VkSemaphoreCreateInfo
* pCreateInfo
,
1426 VkSemaphore
* pSemaphore
)
1428 stub_return(VK_UNSUPPORTED
);
1431 VkResult
anv_QueueSignalSemaphore(
1433 VkSemaphore semaphore
)
1435 stub_return(VK_UNSUPPORTED
);
1438 VkResult
anv_QueueWaitSemaphore(
1440 VkSemaphore semaphore
)
1442 stub_return(VK_UNSUPPORTED
);
1447 VkResult
anv_CreateEvent(
1449 const VkEventCreateInfo
* pCreateInfo
,
1452 stub_return(VK_UNSUPPORTED
);
1455 VkResult
anv_GetEventStatus(
1459 stub_return(VK_UNSUPPORTED
);
1462 VkResult
anv_SetEvent(
1466 stub_return(VK_UNSUPPORTED
);
1469 VkResult
anv_ResetEvent(
1473 stub_return(VK_UNSUPPORTED
);
1478 VkResult
anv_CreateBuffer(
1480 const VkBufferCreateInfo
* pCreateInfo
,
1483 struct anv_device
*device
= (struct anv_device
*) _device
;
1484 struct anv_buffer
*buffer
;
1486 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO
);
1488 buffer
= anv_device_alloc(device
, sizeof(*buffer
), 8,
1489 VK_SYSTEM_ALLOC_TYPE_API_OBJECT
);
1491 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
1493 buffer
->size
= pCreateInfo
->size
;
1497 *pBuffer
= (VkBuffer
) buffer
;
1502 // Buffer view functions
1505 fill_buffer_surface_state(void *state
, VkFormat format
,
1506 uint32_t offset
, uint32_t range
)
1508 const struct anv_format
*info
;
1510 info
= anv_format_for_vk_format(format
);
1511 /* This assumes RGBA float format. */
1512 uint32_t stride
= 4;
1513 uint32_t num_elements
= range
/ stride
;
1515 struct GEN8_RENDER_SURFACE_STATE surface_state
= {
1516 .SurfaceType
= SURFTYPE_BUFFER
,
1517 .SurfaceArray
= false,
1518 .SurfaceFormat
= info
->format
,
1519 .SurfaceVerticalAlignment
= VALIGN4
,
1520 .SurfaceHorizontalAlignment
= HALIGN4
,
1522 .VerticalLineStride
= 0,
1523 .VerticalLineStrideOffset
= 0,
1524 .SamplerL2BypassModeDisable
= true,
1525 .RenderCacheReadWriteMode
= WriteOnlyCache
,
1526 .MemoryObjectControlState
= GEN8_MOCS
,
1529 .Height
= (num_elements
>> 7) & 0x3fff,
1530 .Width
= num_elements
& 0x7f,
1531 .Depth
= (num_elements
>> 21) & 0x3f,
1532 .SurfacePitch
= stride
- 1,
1533 .MinimumArrayElement
= 0,
1534 .NumberofMultisamples
= MULTISAMPLECOUNT_1
,
1539 .AuxiliarySurfaceMode
= AUX_NONE
,
1541 .GreenClearColor
= 0,
1542 .BlueClearColor
= 0,
1543 .AlphaClearColor
= 0,
1544 .ShaderChannelSelectRed
= SCS_RED
,
1545 .ShaderChannelSelectGreen
= SCS_GREEN
,
1546 .ShaderChannelSelectBlue
= SCS_BLUE
,
1547 .ShaderChannelSelectAlpha
= SCS_ALPHA
,
1548 .ResourceMinLOD
= 0,
1549 /* FIXME: We assume that the image must be bound at this time. */
1550 .SurfaceBaseAddress
= { NULL
, offset
},
1553 GEN8_RENDER_SURFACE_STATE_pack(NULL
, state
, &surface_state
);
1556 VkResult
anv_CreateBufferView(
1558 const VkBufferViewCreateInfo
* pCreateInfo
,
1559 VkBufferView
* pView
)
1561 struct anv_device
*device
= (struct anv_device
*) _device
;
1562 struct anv_buffer
*buffer
= (struct anv_buffer
*) pCreateInfo
->buffer
;
1563 struct anv_surface_view
*view
;
1565 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO
);
1567 view
= anv_device_alloc(device
, sizeof(*view
), 8,
1568 VK_SYSTEM_ALLOC_TYPE_API_OBJECT
);
1570 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
1572 view
->base
.destructor
= anv_surface_view_destroy
;
1574 view
->bo
= buffer
->bo
;
1575 view
->offset
= buffer
->offset
+ pCreateInfo
->offset
;
1576 view
->surface_state
=
1577 anv_state_pool_alloc(&device
->surface_state_pool
, 64, 64);
1578 view
->format
= pCreateInfo
->format
;
1579 view
->range
= pCreateInfo
->range
;
1581 fill_buffer_surface_state(view
->surface_state
.map
,
1582 pCreateInfo
->format
, view
->offset
, pCreateInfo
->range
);
1584 *pView
= (VkBufferView
) view
;
1589 // Sampler functions
1591 VkResult
anv_CreateSampler(
1593 const VkSamplerCreateInfo
* pCreateInfo
,
1594 VkSampler
* pSampler
)
1596 struct anv_device
*device
= (struct anv_device
*) _device
;
1597 struct anv_sampler
*sampler
;
1598 uint32_t mag_filter
, min_filter
, max_anisotropy
;
1600 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO
);
1602 sampler
= anv_device_alloc(device
, sizeof(*sampler
), 8,
1603 VK_SYSTEM_ALLOC_TYPE_API_OBJECT
);
1605 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
1607 static const uint32_t vk_to_gen_tex_filter
[] = {
1608 [VK_TEX_FILTER_NEAREST
] = MAPFILTER_NEAREST
,
1609 [VK_TEX_FILTER_LINEAR
] = MAPFILTER_LINEAR
1612 static const uint32_t vk_to_gen_mipmap_mode
[] = {
1613 [VK_TEX_MIPMAP_MODE_BASE
] = MIPFILTER_NONE
,
1614 [VK_TEX_MIPMAP_MODE_NEAREST
] = MIPFILTER_NEAREST
,
1615 [VK_TEX_MIPMAP_MODE_LINEAR
] = MIPFILTER_LINEAR
1618 static const uint32_t vk_to_gen_tex_address
[] = {
1619 [VK_TEX_ADDRESS_WRAP
] = TCM_WRAP
,
1620 [VK_TEX_ADDRESS_MIRROR
] = TCM_MIRROR
,
1621 [VK_TEX_ADDRESS_CLAMP
] = TCM_CLAMP
,
1622 [VK_TEX_ADDRESS_MIRROR_ONCE
] = TCM_MIRROR_ONCE
,
1623 [VK_TEX_ADDRESS_CLAMP_BORDER
] = TCM_CLAMP_BORDER
,
1626 static const uint32_t vk_to_gen_compare_op
[] = {
1627 [VK_COMPARE_OP_NEVER
] = PREFILTEROPNEVER
,
1628 [VK_COMPARE_OP_LESS
] = PREFILTEROPLESS
,
1629 [VK_COMPARE_OP_EQUAL
] = PREFILTEROPEQUAL
,
1630 [VK_COMPARE_OP_LESS_EQUAL
] = PREFILTEROPLEQUAL
,
1631 [VK_COMPARE_OP_GREATER
] = PREFILTEROPGREATER
,
1632 [VK_COMPARE_OP_NOT_EQUAL
] = PREFILTEROPNOTEQUAL
,
1633 [VK_COMPARE_OP_GREATER_EQUAL
] = PREFILTEROPGEQUAL
,
1634 [VK_COMPARE_OP_ALWAYS
] = PREFILTEROPALWAYS
,
1637 if (pCreateInfo
->maxAnisotropy
> 1) {
1638 mag_filter
= MAPFILTER_ANISOTROPIC
;
1639 min_filter
= MAPFILTER_ANISOTROPIC
;
1640 max_anisotropy
= (pCreateInfo
->maxAnisotropy
- 2) / 2;
1642 mag_filter
= vk_to_gen_tex_filter
[pCreateInfo
->magFilter
];
1643 min_filter
= vk_to_gen_tex_filter
[pCreateInfo
->minFilter
];
1644 max_anisotropy
= RATIO21
;
1647 struct GEN8_SAMPLER_STATE sampler_state
= {
1648 .SamplerDisable
= false,
1649 .TextureBorderColorMode
= DX10OGL
,
1650 .LODPreClampMode
= 0,
1652 .MipModeFilter
= vk_to_gen_mipmap_mode
[pCreateInfo
->mipMode
],
1653 .MagModeFilter
= mag_filter
,
1654 .MinModeFilter
= min_filter
,
1655 .TextureLODBias
= pCreateInfo
->mipLodBias
* 256,
1656 .AnisotropicAlgorithm
= EWAApproximation
,
1657 .MinLOD
= pCreateInfo
->minLod
* 256,
1658 .MaxLOD
= pCreateInfo
->maxLod
* 256,
1659 .ChromaKeyEnable
= 0,
1660 .ChromaKeyIndex
= 0,
1662 .ShadowFunction
= vk_to_gen_compare_op
[pCreateInfo
->compareOp
],
1663 .CubeSurfaceControlMode
= 0,
1665 .IndirectStatePointer
=
1666 device
->float_border_colors
.offset
+
1667 pCreateInfo
->borderColor
* sizeof(float) * 4,
1669 .LODClampMagnificationMode
= MIPNONE
,
1670 .MaximumAnisotropy
= max_anisotropy
,
1671 .RAddressMinFilterRoundingEnable
= 0,
1672 .RAddressMagFilterRoundingEnable
= 0,
1673 .VAddressMinFilterRoundingEnable
= 0,
1674 .VAddressMagFilterRoundingEnable
= 0,
1675 .UAddressMinFilterRoundingEnable
= 0,
1676 .UAddressMagFilterRoundingEnable
= 0,
1677 .TrilinearFilterQuality
= 0,
1678 .NonnormalizedCoordinateEnable
= 0,
1679 .TCXAddressControlMode
= vk_to_gen_tex_address
[pCreateInfo
->addressU
],
1680 .TCYAddressControlMode
= vk_to_gen_tex_address
[pCreateInfo
->addressV
],
1681 .TCZAddressControlMode
= vk_to_gen_tex_address
[pCreateInfo
->addressW
],
1684 GEN8_SAMPLER_STATE_pack(NULL
, sampler
->state
, &sampler_state
);
1686 *pSampler
= (VkSampler
) sampler
;
1691 // Descriptor set functions
1693 VkResult
anv_CreateDescriptorSetLayout(
1695 const VkDescriptorSetLayoutCreateInfo
* pCreateInfo
,
1696 VkDescriptorSetLayout
* pSetLayout
)
1698 struct anv_device
*device
= (struct anv_device
*) _device
;
1699 struct anv_descriptor_set_layout
*set_layout
;
1701 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO
);
1703 uint32_t sampler_count
[VK_NUM_SHADER_STAGE
] = { 0, };
1704 uint32_t surface_count
[VK_NUM_SHADER_STAGE
] = { 0, };
1705 uint32_t num_dynamic_buffers
= 0;
1707 uint32_t stages
= 0;
1710 for (uint32_t i
= 0; i
< pCreateInfo
->count
; i
++) {
1711 switch (pCreateInfo
->pBinding
[i
].descriptorType
) {
1712 case VK_DESCRIPTOR_TYPE_SAMPLER
:
1713 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
:
1714 for_each_bit(s
, pCreateInfo
->pBinding
[i
].stageFlags
)
1715 sampler_count
[s
] += pCreateInfo
->pBinding
[i
].count
;
1721 switch (pCreateInfo
->pBinding
[i
].descriptorType
) {
1722 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
:
1723 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE
:
1724 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE
:
1725 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER
:
1726 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER
:
1727 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER
:
1728 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER
:
1729 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
:
1730 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
:
1731 for_each_bit(s
, pCreateInfo
->pBinding
[i
].stageFlags
)
1732 surface_count
[s
] += pCreateInfo
->pBinding
[i
].count
;
1738 switch (pCreateInfo
->pBinding
[i
].descriptorType
) {
1739 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
:
1740 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
:
1741 num_dynamic_buffers
+= pCreateInfo
->pBinding
[i
].count
;
1747 stages
|= pCreateInfo
->pBinding
[i
].stageFlags
;
1748 count
+= pCreateInfo
->pBinding
[i
].count
;
1751 uint32_t sampler_total
= 0;
1752 uint32_t surface_total
= 0;
1753 for (uint32_t s
= 0; s
< VK_NUM_SHADER_STAGE
; s
++) {
1754 sampler_total
+= sampler_count
[s
];
1755 surface_total
+= surface_count
[s
];
1758 size_t size
= sizeof(*set_layout
) +
1759 (sampler_total
+ surface_total
) * sizeof(set_layout
->entries
[0]);
1760 set_layout
= anv_device_alloc(device
, size
, 8,
1761 VK_SYSTEM_ALLOC_TYPE_API_OBJECT
);
1763 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
1765 set_layout
->num_dynamic_buffers
= num_dynamic_buffers
;
1766 set_layout
->count
= count
;
1767 set_layout
->shader_stages
= stages
;
1769 struct anv_descriptor_slot
*p
= set_layout
->entries
;
1770 struct anv_descriptor_slot
*sampler
[VK_NUM_SHADER_STAGE
];
1771 struct anv_descriptor_slot
*surface
[VK_NUM_SHADER_STAGE
];
1772 for (uint32_t s
= 0; s
< VK_NUM_SHADER_STAGE
; s
++) {
1773 set_layout
->stage
[s
].surface_count
= surface_count
[s
];
1774 set_layout
->stage
[s
].surface_start
= surface
[s
] = p
;
1775 p
+= surface_count
[s
];
1776 set_layout
->stage
[s
].sampler_count
= sampler_count
[s
];
1777 set_layout
->stage
[s
].sampler_start
= sampler
[s
] = p
;
1778 p
+= sampler_count
[s
];
1781 uint32_t descriptor
= 0;
1782 int8_t dynamic_slot
= 0;
1784 for (uint32_t i
= 0; i
< pCreateInfo
->count
; i
++) {
1785 switch (pCreateInfo
->pBinding
[i
].descriptorType
) {
1786 case VK_DESCRIPTOR_TYPE_SAMPLER
:
1787 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
:
1788 for_each_bit(s
, pCreateInfo
->pBinding
[i
].stageFlags
)
1789 for (uint32_t j
= 0; j
< pCreateInfo
->pBinding
[i
].count
; j
++) {
1790 sampler
[s
]->index
= descriptor
+ j
;
1791 sampler
[s
]->dynamic_slot
= -1;
1799 switch (pCreateInfo
->pBinding
[i
].descriptorType
) {
1800 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
:
1801 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
:
1809 switch (pCreateInfo
->pBinding
[i
].descriptorType
) {
1810 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
:
1811 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE
:
1812 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE
:
1813 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER
:
1814 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER
:
1815 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER
:
1816 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER
:
1817 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
:
1818 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
:
1819 for_each_bit(s
, pCreateInfo
->pBinding
[i
].stageFlags
)
1820 for (uint32_t j
= 0; j
< pCreateInfo
->pBinding
[i
].count
; j
++) {
1821 surface
[s
]->index
= descriptor
+ j
;
1823 surface
[s
]->dynamic_slot
= dynamic_slot
+ j
;
1825 surface
[s
]->dynamic_slot
= -1;
1834 dynamic_slot
+= pCreateInfo
->pBinding
[i
].count
;
1836 descriptor
+= pCreateInfo
->pBinding
[i
].count
;
1839 *pSetLayout
= (VkDescriptorSetLayout
) set_layout
;
1844 VkResult
anv_BeginDescriptorPoolUpdate(
1846 VkDescriptorUpdateMode updateMode
)
1851 VkResult
anv_EndDescriptorPoolUpdate(
1858 VkResult
anv_CreateDescriptorPool(
1860 VkDescriptorPoolUsage poolUsage
,
1862 const VkDescriptorPoolCreateInfo
* pCreateInfo
,
1863 VkDescriptorPool
* pDescriptorPool
)
1865 *pDescriptorPool
= 1;
1870 VkResult
anv_ResetDescriptorPool(
1872 VkDescriptorPool descriptorPool
)
1877 VkResult
anv_AllocDescriptorSets(
1879 VkDescriptorPool descriptorPool
,
1880 VkDescriptorSetUsage setUsage
,
1882 const VkDescriptorSetLayout
* pSetLayouts
,
1883 VkDescriptorSet
* pDescriptorSets
,
1886 struct anv_device
*device
= (struct anv_device
*) _device
;
1887 const struct anv_descriptor_set_layout
*layout
;
1888 struct anv_descriptor_set
*set
;
1891 for (uint32_t i
= 0; i
< count
; i
++) {
1892 layout
= (struct anv_descriptor_set_layout
*) pSetLayouts
[i
];
1893 size
= sizeof(*set
) + layout
->count
* sizeof(set
->descriptors
[0]);
1894 set
= anv_device_alloc(device
, size
, 8,
1895 VK_SYSTEM_ALLOC_TYPE_API_OBJECT
);
1898 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
1901 /* Descriptor sets may not be 100% filled out so we need to memset to
1902 * ensure that we can properly detect and handle holes.
1904 memset(set
, 0, size
);
1906 pDescriptorSets
[i
] = (VkDescriptorSet
) set
;
1914 void anv_ClearDescriptorSets(
1916 VkDescriptorPool descriptorPool
,
1918 const VkDescriptorSet
* pDescriptorSets
)
1922 void anv_UpdateDescriptors(
1924 VkDescriptorSet descriptorSet
,
1925 uint32_t updateCount
,
1926 const void** ppUpdateArray
)
1928 struct anv_descriptor_set
*set
= (struct anv_descriptor_set
*) descriptorSet
;
1929 VkUpdateSamplers
*update_samplers
;
1930 VkUpdateSamplerTextures
*update_sampler_textures
;
1931 VkUpdateImages
*update_images
;
1932 VkUpdateBuffers
*update_buffers
;
1933 VkUpdateAsCopy
*update_as_copy
;
1935 for (uint32_t i
= 0; i
< updateCount
; i
++) {
1936 const struct anv_common
*common
= ppUpdateArray
[i
];
1938 switch (common
->sType
) {
1939 case VK_STRUCTURE_TYPE_UPDATE_SAMPLERS
:
1940 update_samplers
= (VkUpdateSamplers
*) common
;
1942 for (uint32_t j
= 0; j
< update_samplers
->count
; j
++) {
1943 set
->descriptors
[update_samplers
->binding
+ j
].sampler
=
1944 (struct anv_sampler
*) update_samplers
->pSamplers
[j
];
1948 case VK_STRUCTURE_TYPE_UPDATE_SAMPLER_TEXTURES
:
1949 /* FIXME: Shouldn't this be *_UPDATE_SAMPLER_IMAGES? */
1950 update_sampler_textures
= (VkUpdateSamplerTextures
*) common
;
1952 for (uint32_t j
= 0; j
< update_sampler_textures
->count
; j
++) {
1953 set
->descriptors
[update_sampler_textures
->binding
+ j
].view
=
1954 (struct anv_surface_view
*)
1955 update_sampler_textures
->pSamplerImageViews
[j
].pImageView
->view
;
1956 set
->descriptors
[update_sampler_textures
->binding
+ j
].sampler
=
1957 (struct anv_sampler
*)
1958 update_sampler_textures
->pSamplerImageViews
[j
].sampler
;
1962 case VK_STRUCTURE_TYPE_UPDATE_IMAGES
:
1963 update_images
= (VkUpdateImages
*) common
;
1965 for (uint32_t j
= 0; j
< update_images
->count
; j
++) {
1966 set
->descriptors
[update_images
->binding
+ j
].view
=
1967 (struct anv_surface_view
*) update_images
->pImageViews
[j
].view
;
1971 case VK_STRUCTURE_TYPE_UPDATE_BUFFERS
:
1972 update_buffers
= (VkUpdateBuffers
*) common
;
1974 for (uint32_t j
= 0; j
< update_buffers
->count
; j
++) {
1975 set
->descriptors
[update_buffers
->binding
+ j
].view
=
1976 (struct anv_surface_view
*) update_buffers
->pBufferViews
[j
].view
;
1978 /* FIXME: descriptor arrays? */
1981 case VK_STRUCTURE_TYPE_UPDATE_AS_COPY
:
1982 update_as_copy
= (VkUpdateAsCopy
*) common
;
1983 (void) update_as_copy
;
1992 // State object functions
1994 static inline int64_t
1995 clamp_int64(int64_t x
, int64_t min
, int64_t max
)
2006 anv_dynamic_vp_state_destroy(struct anv_device
*device
,
2007 struct anv_object
*object
,
2008 VkObjectType obj_type
)
2010 struct anv_dynamic_vp_state
*state
= (void *)object
;
2012 assert(obj_type
== VK_OBJECT_TYPE_DYNAMIC_VP_STATE
);
2014 anv_state_pool_free(&device
->dynamic_state_pool
, state
->sf_clip_vp
);
2015 anv_state_pool_free(&device
->dynamic_state_pool
, state
->cc_vp
);
2016 anv_state_pool_free(&device
->dynamic_state_pool
, state
->scissor
);
2018 anv_device_free(device
, state
);
2021 VkResult
anv_CreateDynamicViewportState(
2023 const VkDynamicVpStateCreateInfo
* pCreateInfo
,
2024 VkDynamicVpState
* pState
)
2026 struct anv_device
*device
= (struct anv_device
*) _device
;
2027 struct anv_dynamic_vp_state
*state
;
2029 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_DYNAMIC_VP_STATE_CREATE_INFO
);
2031 state
= anv_device_alloc(device
, sizeof(*state
), 8,
2032 VK_SYSTEM_ALLOC_TYPE_API_OBJECT
);
2034 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
2036 state
->base
.destructor
= anv_dynamic_vp_state_destroy
;
2038 unsigned count
= pCreateInfo
->viewportAndScissorCount
;
2039 state
->sf_clip_vp
= anv_state_pool_alloc(&device
->dynamic_state_pool
,
2041 state
->cc_vp
= anv_state_pool_alloc(&device
->dynamic_state_pool
,
2043 state
->scissor
= anv_state_pool_alloc(&device
->dynamic_state_pool
,
2046 for (uint32_t i
= 0; i
< pCreateInfo
->viewportAndScissorCount
; i
++) {
2047 const VkViewport
*vp
= &pCreateInfo
->pViewports
[i
];
2048 const VkRect
*s
= &pCreateInfo
->pScissors
[i
];
2050 struct GEN8_SF_CLIP_VIEWPORT sf_clip_viewport
= {
2051 .ViewportMatrixElementm00
= vp
->width
/ 2,
2052 .ViewportMatrixElementm11
= vp
->height
/ 2,
2053 .ViewportMatrixElementm22
= (vp
->maxDepth
- vp
->minDepth
) / 2,
2054 .ViewportMatrixElementm30
= vp
->originX
+ vp
->width
/ 2,
2055 .ViewportMatrixElementm31
= vp
->originY
+ vp
->height
/ 2,
2056 .ViewportMatrixElementm32
= (vp
->maxDepth
+ vp
->minDepth
) / 2,
2057 .XMinClipGuardband
= -1.0f
,
2058 .XMaxClipGuardband
= 1.0f
,
2059 .YMinClipGuardband
= -1.0f
,
2060 .YMaxClipGuardband
= 1.0f
,
2061 .XMinViewPort
= vp
->originX
,
2062 .XMaxViewPort
= vp
->originX
+ vp
->width
- 1,
2063 .YMinViewPort
= vp
->originY
,
2064 .YMaxViewPort
= vp
->originY
+ vp
->height
- 1,
2067 struct GEN8_CC_VIEWPORT cc_viewport
= {
2068 .MinimumDepth
= vp
->minDepth
,
2069 .MaximumDepth
= vp
->maxDepth
2072 /* Since xmax and ymax are inclusive, we have to have xmax < xmin or
2073 * ymax < ymin for empty clips. In case clip x, y, width height are all
2074 * 0, the clamps below produce 0 for xmin, ymin, xmax, ymax, which isn't
2075 * what we want. Just special case empty clips and produce a canonical
2077 static const struct GEN8_SCISSOR_RECT empty_scissor
= {
2078 .ScissorRectangleYMin
= 1,
2079 .ScissorRectangleXMin
= 1,
2080 .ScissorRectangleYMax
= 0,
2081 .ScissorRectangleXMax
= 0
2084 const int max
= 0xffff;
2085 struct GEN8_SCISSOR_RECT scissor
= {
2086 /* Do this math using int64_t so overflow gets clamped correctly. */
2087 .ScissorRectangleYMin
= clamp_int64(s
->offset
.y
, 0, max
),
2088 .ScissorRectangleXMin
= clamp_int64(s
->offset
.x
, 0, max
),
2089 .ScissorRectangleYMax
= clamp_int64((uint64_t) s
->offset
.y
+ s
->extent
.height
- 1, 0, max
),
2090 .ScissorRectangleXMax
= clamp_int64((uint64_t) s
->offset
.x
+ s
->extent
.width
- 1, 0, max
)
2093 GEN8_SF_CLIP_VIEWPORT_pack(NULL
, state
->sf_clip_vp
.map
+ i
* 64, &sf_clip_viewport
);
2094 GEN8_CC_VIEWPORT_pack(NULL
, state
->cc_vp
.map
+ i
* 32, &cc_viewport
);
2096 if (s
->extent
.width
<= 0 || s
->extent
.height
<= 0) {
2097 GEN8_SCISSOR_RECT_pack(NULL
, state
->scissor
.map
+ i
* 32, &empty_scissor
);
2099 GEN8_SCISSOR_RECT_pack(NULL
, state
->scissor
.map
+ i
* 32, &scissor
);
2103 *pState
= (VkDynamicVpState
) state
;
2108 VkResult
anv_CreateDynamicRasterState(
2110 const VkDynamicRsStateCreateInfo
* pCreateInfo
,
2111 VkDynamicRsState
* pState
)
2113 struct anv_device
*device
= (struct anv_device
*) _device
;
2114 struct anv_dynamic_rs_state
*state
;
2116 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_DYNAMIC_RS_STATE_CREATE_INFO
);
2118 state
= anv_device_alloc(device
, sizeof(*state
), 8,
2119 VK_SYSTEM_ALLOC_TYPE_API_OBJECT
);
2121 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
2124 * float pointFadeThreshold;
2125 * // optional (GL45) - Size of point fade threshold
2128 struct GEN8_3DSTATE_SF sf
= {
2129 GEN8_3DSTATE_SF_header
,
2130 .LineWidth
= pCreateInfo
->lineWidth
,
2131 .PointWidth
= pCreateInfo
->pointSize
,
2134 GEN8_3DSTATE_SF_pack(NULL
, state
->state_sf
, &sf
);
2136 bool enable_bias
= pCreateInfo
->depthBias
!= 0.0f
||
2137 pCreateInfo
->slopeScaledDepthBias
!= 0.0f
;
2138 struct GEN8_3DSTATE_RASTER raster
= {
2139 .GlobalDepthOffsetEnableSolid
= enable_bias
,
2140 .GlobalDepthOffsetEnableWireframe
= enable_bias
,
2141 .GlobalDepthOffsetEnablePoint
= enable_bias
,
2142 .GlobalDepthOffsetConstant
= pCreateInfo
->depthBias
,
2143 .GlobalDepthOffsetScale
= pCreateInfo
->slopeScaledDepthBias
,
2144 .GlobalDepthOffsetClamp
= pCreateInfo
->depthBiasClamp
2147 GEN8_3DSTATE_RASTER_pack(NULL
, state
->state_raster
, &raster
);
2149 *pState
= (VkDynamicRsState
) state
;
2154 VkResult
anv_CreateDynamicColorBlendState(
2156 const VkDynamicCbStateCreateInfo
* pCreateInfo
,
2157 VkDynamicCbState
* pState
)
2159 struct anv_device
*device
= (struct anv_device
*) _device
;
2160 struct anv_dynamic_cb_state
*state
;
2162 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_DYNAMIC_CB_STATE_CREATE_INFO
);
2164 state
= anv_device_alloc(device
, sizeof(*state
), 8,
2165 VK_SYSTEM_ALLOC_TYPE_API_OBJECT
);
2167 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
2169 struct GEN8_COLOR_CALC_STATE color_calc_state
= {
2170 .BlendConstantColorRed
= pCreateInfo
->blendConst
[0],
2171 .BlendConstantColorGreen
= pCreateInfo
->blendConst
[1],
2172 .BlendConstantColorBlue
= pCreateInfo
->blendConst
[2],
2173 .BlendConstantColorAlpha
= pCreateInfo
->blendConst
[3]
2176 GEN8_COLOR_CALC_STATE_pack(NULL
, state
->state_color_calc
, &color_calc_state
);
2178 *pState
= (VkDynamicCbState
) state
;
2183 VkResult
anv_CreateDynamicDepthStencilState(
2185 const VkDynamicDsStateCreateInfo
* pCreateInfo
,
2186 VkDynamicDsState
* pState
)
2188 struct anv_device
*device
= (struct anv_device
*) _device
;
2189 struct anv_dynamic_ds_state
*state
;
2191 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_DYNAMIC_DS_STATE_CREATE_INFO
);
2193 state
= anv_device_alloc(device
, sizeof(*state
), 8,
2194 VK_SYSTEM_ALLOC_TYPE_API_OBJECT
);
2196 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
2198 struct GEN8_3DSTATE_WM_DEPTH_STENCIL wm_depth_stencil
= {
2199 GEN8_3DSTATE_WM_DEPTH_STENCIL_header
,
2201 /* Is this what we need to do? */
2202 .StencilBufferWriteEnable
= pCreateInfo
->stencilWriteMask
!= 0,
2204 .StencilTestMask
= pCreateInfo
->stencilReadMask
& 0xff,
2205 .StencilWriteMask
= pCreateInfo
->stencilWriteMask
& 0xff,
2207 .BackfaceStencilTestMask
= pCreateInfo
->stencilReadMask
& 0xff,
2208 .BackfaceStencilWriteMask
= pCreateInfo
->stencilWriteMask
& 0xff,
2211 GEN8_3DSTATE_WM_DEPTH_STENCIL_pack(NULL
, state
->state_wm_depth_stencil
,
2214 struct GEN8_COLOR_CALC_STATE color_calc_state
= {
2215 .StencilReferenceValue
= pCreateInfo
->stencilFrontRef
,
2216 .BackFaceStencilReferenceValue
= pCreateInfo
->stencilBackRef
2219 GEN8_COLOR_CALC_STATE_pack(NULL
, state
->state_color_calc
, &color_calc_state
);
2221 *pState
= (VkDynamicDsState
) state
;
2226 // Command buffer functions
2229 anv_cmd_buffer_destroy(struct anv_device
*device
,
2230 struct anv_object
*object
,
2231 VkObjectType obj_type
)
2233 struct anv_cmd_buffer
*cmd_buffer
= (struct anv_cmd_buffer
*) object
;
2235 assert(obj_type
== VK_OBJECT_TYPE_COMMAND_BUFFER
);
2237 /* Destroy all of the batch buffers */
2238 struct anv_batch_bo
*bbo
= cmd_buffer
->last_batch_bo
;
2240 struct anv_batch_bo
*prev
= bbo
->prev_batch_bo
;
2241 anv_batch_bo_destroy(bbo
, device
);
2244 anv_reloc_list_finish(&cmd_buffer
->batch
.relocs
, device
);
2246 /* Destroy all of the surface state buffers */
2247 bbo
= cmd_buffer
->surface_batch_bo
;
2249 struct anv_batch_bo
*prev
= bbo
->prev_batch_bo
;
2250 anv_batch_bo_destroy(bbo
, device
);
2253 anv_reloc_list_finish(&cmd_buffer
->surface_relocs
, device
);
2255 anv_state_stream_finish(&cmd_buffer
->surface_state_stream
);
2256 anv_state_stream_finish(&cmd_buffer
->dynamic_state_stream
);
2257 anv_device_free(device
, cmd_buffer
->exec2_objects
);
2258 anv_device_free(device
, cmd_buffer
->exec2_bos
);
2259 anv_device_free(device
, cmd_buffer
);
2263 anv_cmd_buffer_chain_batch(struct anv_batch
*batch
, void *_data
)
2265 struct anv_cmd_buffer
*cmd_buffer
= _data
;
2267 struct anv_batch_bo
*new_bbo
, *old_bbo
= cmd_buffer
->last_batch_bo
;
2269 VkResult result
= anv_batch_bo_create(cmd_buffer
->device
, &new_bbo
);
2270 if (result
!= VK_SUCCESS
)
2273 /* We set the end of the batch a little short so we would be sure we
2274 * have room for the chaining command. Since we're about to emit the
2275 * chaining command, let's set it back where it should go.
2277 batch
->end
+= GEN8_MI_BATCH_BUFFER_START_length
* 4;
2278 assert(batch
->end
== old_bbo
->bo
.map
+ old_bbo
->bo
.size
);
2280 anv_batch_emit(batch
, GEN8_MI_BATCH_BUFFER_START
,
2281 GEN8_MI_BATCH_BUFFER_START_header
,
2282 ._2ndLevelBatchBuffer
= _1stlevelbatch
,
2283 .AddressSpaceIndicator
= ASI_PPGTT
,
2284 .BatchBufferStartAddress
= { &new_bbo
->bo
, 0 },
2287 /* Pad out to a 2-dword aligned boundary with zeros */
2288 if ((uintptr_t)batch
->next
% 8 != 0) {
2289 *(uint32_t *)batch
->next
= 0;
2293 anv_batch_bo_finish(cmd_buffer
->last_batch_bo
, batch
);
2295 new_bbo
->prev_batch_bo
= old_bbo
;
2296 cmd_buffer
->last_batch_bo
= new_bbo
;
2298 anv_batch_bo_start(new_bbo
, batch
, GEN8_MI_BATCH_BUFFER_START_length
* 4);
2303 VkResult
anv_CreateCommandBuffer(
2305 const VkCmdBufferCreateInfo
* pCreateInfo
,
2306 VkCmdBuffer
* pCmdBuffer
)
2308 struct anv_device
*device
= (struct anv_device
*) _device
;
2309 struct anv_cmd_buffer
*cmd_buffer
;
2312 cmd_buffer
= anv_device_alloc(device
, sizeof(*cmd_buffer
), 8,
2313 VK_SYSTEM_ALLOC_TYPE_API_OBJECT
);
2314 if (cmd_buffer
== NULL
)
2315 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
2317 cmd_buffer
->base
.destructor
= anv_cmd_buffer_destroy
;
2319 cmd_buffer
->device
= device
;
2320 cmd_buffer
->rs_state
= NULL
;
2321 cmd_buffer
->vp_state
= NULL
;
2322 cmd_buffer
->cb_state
= NULL
;
2323 cmd_buffer
->ds_state
= NULL
;
2324 memset(&cmd_buffer
->descriptors
, 0, sizeof(cmd_buffer
->descriptors
));
2326 result
= anv_batch_bo_create(device
, &cmd_buffer
->last_batch_bo
);
2327 if (result
!= VK_SUCCESS
)
2330 result
= anv_reloc_list_init(&cmd_buffer
->batch
.relocs
, device
);
2331 if (result
!= VK_SUCCESS
)
2334 cmd_buffer
->batch
.device
= device
;
2335 cmd_buffer
->batch
.extend_cb
= anv_cmd_buffer_chain_batch
;
2336 cmd_buffer
->batch
.user_data
= cmd_buffer
;
2338 anv_batch_bo_start(cmd_buffer
->last_batch_bo
, &cmd_buffer
->batch
,
2339 GEN8_MI_BATCH_BUFFER_START_length
* 4);
2341 result
= anv_batch_bo_create(device
, &cmd_buffer
->surface_batch_bo
);
2342 if (result
!= VK_SUCCESS
)
2343 goto fail_batch_relocs
;
2344 cmd_buffer
->surface_batch_bo
->first_reloc
= 0;
2346 result
= anv_reloc_list_init(&cmd_buffer
->surface_relocs
, device
);
2347 if (result
!= VK_SUCCESS
)
2348 goto fail_ss_batch_bo
;
2350 /* Start surface_next at 1 so surface offset 0 is invalid. */
2351 cmd_buffer
->surface_next
= 1;
2353 cmd_buffer
->exec2_objects
= NULL
;
2354 cmd_buffer
->exec2_bos
= NULL
;
2355 cmd_buffer
->exec2_array_length
= 0;
2357 anv_state_stream_init(&cmd_buffer
->surface_state_stream
,
2358 &device
->surface_state_block_pool
);
2359 anv_state_stream_init(&cmd_buffer
->dynamic_state_stream
,
2360 &device
->dynamic_state_block_pool
);
2362 cmd_buffer
->dirty
= 0;
2363 cmd_buffer
->vb_dirty
= 0;
2364 cmd_buffer
->descriptors_dirty
= 0;
2365 cmd_buffer
->pipeline
= NULL
;
2366 cmd_buffer
->vp_state
= NULL
;
2367 cmd_buffer
->rs_state
= NULL
;
2368 cmd_buffer
->ds_state
= NULL
;
2370 *pCmdBuffer
= (VkCmdBuffer
) cmd_buffer
;
2375 anv_batch_bo_destroy(cmd_buffer
->surface_batch_bo
, device
);
2377 anv_reloc_list_finish(&cmd_buffer
->batch
.relocs
, device
);
2379 anv_batch_bo_destroy(cmd_buffer
->last_batch_bo
, device
);
2381 anv_device_free(device
, cmd_buffer
);
2387 anv_cmd_buffer_emit_state_base_address(struct anv_cmd_buffer
*cmd_buffer
)
2389 struct anv_device
*device
= cmd_buffer
->device
;
2391 anv_batch_emit(&cmd_buffer
->batch
, GEN8_STATE_BASE_ADDRESS
,
2392 .GeneralStateBaseAddress
= { NULL
, 0 },
2393 .GeneralStateMemoryObjectControlState
= GEN8_MOCS
,
2394 .GeneralStateBaseAddressModifyEnable
= true,
2395 .GeneralStateBufferSize
= 0xfffff,
2396 .GeneralStateBufferSizeModifyEnable
= true,
2398 .SurfaceStateBaseAddress
= { &cmd_buffer
->surface_batch_bo
->bo
, 0 },
2399 .SurfaceStateMemoryObjectControlState
= GEN8_MOCS
,
2400 .SurfaceStateBaseAddressModifyEnable
= true,
2402 .DynamicStateBaseAddress
= { &device
->dynamic_state_block_pool
.bo
, 0 },
2403 .DynamicStateMemoryObjectControlState
= GEN8_MOCS
,
2404 .DynamicStateBaseAddressModifyEnable
= true,
2405 .DynamicStateBufferSize
= 0xfffff,
2406 .DynamicStateBufferSizeModifyEnable
= true,
2408 .IndirectObjectBaseAddress
= { NULL
, 0 },
2409 .IndirectObjectMemoryObjectControlState
= GEN8_MOCS
,
2410 .IndirectObjectBaseAddressModifyEnable
= true,
2411 .IndirectObjectBufferSize
= 0xfffff,
2412 .IndirectObjectBufferSizeModifyEnable
= true,
2414 .InstructionBaseAddress
= { &device
->instruction_block_pool
.bo
, 0 },
2415 .InstructionMemoryObjectControlState
= GEN8_MOCS
,
2416 .InstructionBaseAddressModifyEnable
= true,
2417 .InstructionBufferSize
= 0xfffff,
2418 .InstructionBuffersizeModifyEnable
= true);
2421 VkResult
anv_BeginCommandBuffer(
2422 VkCmdBuffer cmdBuffer
,
2423 const VkCmdBufferBeginInfo
* pBeginInfo
)
2425 struct anv_cmd_buffer
*cmd_buffer
= (struct anv_cmd_buffer
*) cmdBuffer
;
2427 anv_batch_emit(&cmd_buffer
->batch
, GEN8_PIPELINE_SELECT
,
2428 .PipelineSelection
= _3D
);
2429 anv_batch_emit(&cmd_buffer
->batch
, GEN8_STATE_SIP
);
2431 anv_cmd_buffer_emit_state_base_address(cmd_buffer
);
2433 anv_batch_emit(&cmd_buffer
->batch
, GEN8_3DSTATE_VF_STATISTICS
,
2434 .StatisticsEnable
= true);
2435 anv_batch_emit(&cmd_buffer
->batch
, GEN8_3DSTATE_HS
, .Enable
= false);
2436 anv_batch_emit(&cmd_buffer
->batch
, GEN8_3DSTATE_TE
, .TEEnable
= false);
2437 anv_batch_emit(&cmd_buffer
->batch
, GEN8_3DSTATE_DS
, .FunctionEnable
= false);
2438 anv_batch_emit(&cmd_buffer
->batch
, GEN8_3DSTATE_STREAMOUT
, .SOFunctionEnable
= false);
2440 anv_batch_emit(&cmd_buffer
->batch
, GEN8_3DSTATE_PUSH_CONSTANT_ALLOC_VS
,
2441 .ConstantBufferOffset
= 0,
2442 .ConstantBufferSize
= 4);
2443 anv_batch_emit(&cmd_buffer
->batch
, GEN8_3DSTATE_PUSH_CONSTANT_ALLOC_GS
,
2444 .ConstantBufferOffset
= 4,
2445 .ConstantBufferSize
= 4);
2446 anv_batch_emit(&cmd_buffer
->batch
, GEN8_3DSTATE_PUSH_CONSTANT_ALLOC_PS
,
2447 .ConstantBufferOffset
= 8,
2448 .ConstantBufferSize
= 4);
2450 anv_batch_emit(&cmd_buffer
->batch
, GEN8_3DSTATE_WM_CHROMAKEY
,
2451 .ChromaKeyKillEnable
= false);
2452 anv_batch_emit(&cmd_buffer
->batch
, GEN8_3DSTATE_SBE_SWIZ
);
2453 anv_batch_emit(&cmd_buffer
->batch
, GEN8_3DSTATE_AA_LINE_PARAMETERS
);
2459 anv_cmd_buffer_add_bo(struct anv_cmd_buffer
*cmd_buffer
,
2461 struct drm_i915_gem_relocation_entry
*relocs
,
2464 struct drm_i915_gem_exec_object2
*obj
;
2466 if (bo
->index
< cmd_buffer
->bo_count
&&
2467 cmd_buffer
->exec2_bos
[bo
->index
] == bo
)
2470 if (cmd_buffer
->bo_count
>= cmd_buffer
->exec2_array_length
) {
2471 uint32_t new_len
= cmd_buffer
->exec2_objects
?
2472 cmd_buffer
->exec2_array_length
* 2 : 64;
2474 struct drm_i915_gem_exec_object2
*new_objects
=
2475 anv_device_alloc(cmd_buffer
->device
, new_len
* sizeof(*new_objects
),
2476 8, VK_SYSTEM_ALLOC_TYPE_INTERNAL
);
2477 if (new_objects
== NULL
)
2478 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
2480 struct anv_bo
**new_bos
=
2481 anv_device_alloc(cmd_buffer
->device
, new_len
* sizeof(*new_bos
),
2482 8, VK_SYSTEM_ALLOC_TYPE_INTERNAL
);
2483 if (new_objects
== NULL
) {
2484 anv_device_free(cmd_buffer
->device
, new_objects
);
2485 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
2488 if (cmd_buffer
->exec2_objects
) {
2489 memcpy(new_objects
, cmd_buffer
->exec2_objects
,
2490 cmd_buffer
->bo_count
* sizeof(*new_objects
));
2491 memcpy(new_bos
, cmd_buffer
->exec2_bos
,
2492 cmd_buffer
->bo_count
* sizeof(*new_bos
));
2495 cmd_buffer
->exec2_objects
= new_objects
;
2496 cmd_buffer
->exec2_bos
= new_bos
;
2497 cmd_buffer
->exec2_array_length
= new_len
;
2500 assert(cmd_buffer
->bo_count
< cmd_buffer
->exec2_array_length
);
2502 bo
->index
= cmd_buffer
->bo_count
++;
2503 obj
= &cmd_buffer
->exec2_objects
[bo
->index
];
2504 cmd_buffer
->exec2_bos
[bo
->index
] = bo
;
2506 obj
->handle
= bo
->gem_handle
;
2507 obj
->relocation_count
= 0;
2508 obj
->relocs_ptr
= 0;
2510 obj
->offset
= bo
->offset
;
2516 obj
->relocation_count
= num_relocs
;
2517 obj
->relocs_ptr
= (uintptr_t) relocs
;
2524 anv_cmd_buffer_add_validate_bos(struct anv_cmd_buffer
*cmd_buffer
,
2525 struct anv_reloc_list
*list
)
2527 for (size_t i
= 0; i
< list
->num_relocs
; i
++)
2528 anv_cmd_buffer_add_bo(cmd_buffer
, list
->reloc_bos
[i
], NULL
, 0);
2532 anv_cmd_buffer_process_relocs(struct anv_cmd_buffer
*cmd_buffer
,
2533 struct anv_reloc_list
*list
)
2537 /* If the kernel supports I915_EXEC_NO_RELOC, it will compare offset in
2538 * struct drm_i915_gem_exec_object2 against the bos current offset and if
2539 * all bos haven't moved it will skip relocation processing alltogether.
2540 * If I915_EXEC_NO_RELOC is not supported, the kernel ignores the incoming
2541 * value of offset so we can set it either way. For that to work we need
2542 * to make sure all relocs use the same presumed offset.
2545 for (size_t i
= 0; i
< list
->num_relocs
; i
++) {
2546 bo
= list
->reloc_bos
[i
];
2547 if (bo
->offset
!= list
->relocs
[i
].presumed_offset
)
2548 cmd_buffer
->need_reloc
= true;
2550 list
->relocs
[i
].target_handle
= bo
->index
;
2554 VkResult
anv_EndCommandBuffer(
2555 VkCmdBuffer cmdBuffer
)
2557 struct anv_cmd_buffer
*cmd_buffer
= (struct anv_cmd_buffer
*) cmdBuffer
;
2558 struct anv_device
*device
= cmd_buffer
->device
;
2559 struct anv_batch
*batch
= &cmd_buffer
->batch
;
2561 anv_batch_emit(batch
, GEN8_MI_BATCH_BUFFER_END
);
2563 /* Round batch up to an even number of dwords. */
2564 if ((batch
->next
- batch
->start
) & 4)
2565 anv_batch_emit(batch
, GEN8_MI_NOOP
);
2567 anv_batch_bo_finish(cmd_buffer
->last_batch_bo
, &cmd_buffer
->batch
);
2568 cmd_buffer
->surface_batch_bo
->num_relocs
=
2569 cmd_buffer
->surface_relocs
.num_relocs
- cmd_buffer
->surface_batch_bo
->first_reloc
;
2570 cmd_buffer
->surface_batch_bo
->length
= cmd_buffer
->surface_next
;
2572 cmd_buffer
->bo_count
= 0;
2573 cmd_buffer
->need_reloc
= false;
2575 /* Lock for access to bo->index. */
2576 pthread_mutex_lock(&device
->mutex
);
2578 /* Add surface state bos first so we can add them with their relocs. */
2579 for (struct anv_batch_bo
*bbo
= cmd_buffer
->surface_batch_bo
;
2580 bbo
!= NULL
; bbo
= bbo
->prev_batch_bo
) {
2581 anv_cmd_buffer_add_bo(cmd_buffer
, &bbo
->bo
,
2582 &cmd_buffer
->surface_relocs
.relocs
[bbo
->first_reloc
],
2586 /* Add all of the BOs referenced by surface state */
2587 anv_cmd_buffer_add_validate_bos(cmd_buffer
, &cmd_buffer
->surface_relocs
);
2589 /* Add all but the first batch BO */
2590 struct anv_batch_bo
*batch_bo
= cmd_buffer
->last_batch_bo
;
2591 while (batch_bo
->prev_batch_bo
) {
2592 anv_cmd_buffer_add_bo(cmd_buffer
, &batch_bo
->bo
,
2593 &batch
->relocs
.relocs
[batch_bo
->first_reloc
],
2594 batch_bo
->num_relocs
);
2595 batch_bo
= batch_bo
->prev_batch_bo
;
2598 /* Add everything referenced by the batches */
2599 anv_cmd_buffer_add_validate_bos(cmd_buffer
, &batch
->relocs
);
2601 /* Add the first batch bo last */
2602 assert(batch_bo
->prev_batch_bo
== NULL
&& batch_bo
->first_reloc
== 0);
2603 anv_cmd_buffer_add_bo(cmd_buffer
, &batch_bo
->bo
,
2604 &batch
->relocs
.relocs
[batch_bo
->first_reloc
],
2605 batch_bo
->num_relocs
);
2606 assert(batch_bo
->bo
.index
== cmd_buffer
->bo_count
- 1);
2608 anv_cmd_buffer_process_relocs(cmd_buffer
, &cmd_buffer
->surface_relocs
);
2609 anv_cmd_buffer_process_relocs(cmd_buffer
, &batch
->relocs
);
2611 cmd_buffer
->execbuf
.buffers_ptr
= (uintptr_t) cmd_buffer
->exec2_objects
;
2612 cmd_buffer
->execbuf
.buffer_count
= cmd_buffer
->bo_count
;
2613 cmd_buffer
->execbuf
.batch_start_offset
= 0;
2614 cmd_buffer
->execbuf
.batch_len
= batch
->next
- batch
->start
;
2615 cmd_buffer
->execbuf
.cliprects_ptr
= 0;
2616 cmd_buffer
->execbuf
.num_cliprects
= 0;
2617 cmd_buffer
->execbuf
.DR1
= 0;
2618 cmd_buffer
->execbuf
.DR4
= 0;
2620 cmd_buffer
->execbuf
.flags
= I915_EXEC_HANDLE_LUT
;
2621 if (!cmd_buffer
->need_reloc
)
2622 cmd_buffer
->execbuf
.flags
|= I915_EXEC_NO_RELOC
;
2623 cmd_buffer
->execbuf
.flags
|= I915_EXEC_RENDER
;
2624 cmd_buffer
->execbuf
.rsvd1
= device
->context_id
;
2625 cmd_buffer
->execbuf
.rsvd2
= 0;
2627 pthread_mutex_unlock(&device
->mutex
);
2632 VkResult
anv_ResetCommandBuffer(
2633 VkCmdBuffer cmdBuffer
)
2635 struct anv_cmd_buffer
*cmd_buffer
= (struct anv_cmd_buffer
*) cmdBuffer
;
2637 /* Delete all but the first batch bo */
2638 while (cmd_buffer
->last_batch_bo
->prev_batch_bo
) {
2639 struct anv_batch_bo
*prev
= cmd_buffer
->last_batch_bo
->prev_batch_bo
;
2640 anv_batch_bo_destroy(cmd_buffer
->last_batch_bo
, cmd_buffer
->device
);
2641 cmd_buffer
->last_batch_bo
= prev
;
2643 assert(cmd_buffer
->last_batch_bo
->prev_batch_bo
== NULL
);
2645 cmd_buffer
->batch
.relocs
.num_relocs
= 0;
2646 anv_batch_bo_start(cmd_buffer
->last_batch_bo
, &cmd_buffer
->batch
,
2647 GEN8_MI_BATCH_BUFFER_START_length
* 4);
2649 /* Delete all but the first batch bo */
2650 while (cmd_buffer
->surface_batch_bo
->prev_batch_bo
) {
2651 struct anv_batch_bo
*prev
= cmd_buffer
->surface_batch_bo
->prev_batch_bo
;
2652 anv_batch_bo_destroy(cmd_buffer
->surface_batch_bo
, cmd_buffer
->device
);
2653 cmd_buffer
->surface_batch_bo
= prev
;
2655 assert(cmd_buffer
->surface_batch_bo
->prev_batch_bo
== NULL
);
2657 cmd_buffer
->surface_next
= 1;
2658 cmd_buffer
->surface_relocs
.num_relocs
= 0;
2660 cmd_buffer
->rs_state
= NULL
;
2661 cmd_buffer
->vp_state
= NULL
;
2662 cmd_buffer
->cb_state
= NULL
;
2663 cmd_buffer
->ds_state
= NULL
;
2668 // Command buffer building functions
2670 void anv_CmdBindPipeline(
2671 VkCmdBuffer cmdBuffer
,
2672 VkPipelineBindPoint pipelineBindPoint
,
2673 VkPipeline _pipeline
)
2675 struct anv_cmd_buffer
*cmd_buffer
= (struct anv_cmd_buffer
*) cmdBuffer
;
2676 struct anv_pipeline
*pipeline
= (struct anv_pipeline
*) _pipeline
;
2678 cmd_buffer
->pipeline
= pipeline
;
2679 cmd_buffer
->vb_dirty
|= pipeline
->vb_used
;
2680 cmd_buffer
->dirty
|= ANV_CMD_BUFFER_PIPELINE_DIRTY
;
2683 void anv_CmdBindDynamicStateObject(
2684 VkCmdBuffer cmdBuffer
,
2685 VkStateBindPoint stateBindPoint
,
2686 VkDynamicStateObject dynamicState
)
2688 struct anv_cmd_buffer
*cmd_buffer
= (struct anv_cmd_buffer
*) cmdBuffer
;
2689 struct anv_dynamic_vp_state
*vp_state
;
2691 switch (stateBindPoint
) {
2692 case VK_STATE_BIND_POINT_VIEWPORT
:
2693 vp_state
= (struct anv_dynamic_vp_state
*) dynamicState
;
2694 /* We emit state immediately, but set cmd_buffer->vp_state to indicate
2695 * that vp state has been set in this command buffer. */
2696 cmd_buffer
->vp_state
= vp_state
;
2697 anv_batch_emit(&cmd_buffer
->batch
, GEN8_3DSTATE_SCISSOR_STATE_POINTERS
,
2698 .ScissorRectPointer
= vp_state
->scissor
.offset
);
2699 anv_batch_emit(&cmd_buffer
->batch
, GEN8_3DSTATE_VIEWPORT_STATE_POINTERS_CC
,
2700 .CCViewportPointer
= vp_state
->cc_vp
.offset
);
2701 anv_batch_emit(&cmd_buffer
->batch
, GEN8_3DSTATE_VIEWPORT_STATE_POINTERS_SF_CLIP
,
2702 .SFClipViewportPointer
= vp_state
->sf_clip_vp
.offset
);
2704 case VK_STATE_BIND_POINT_RASTER
:
2705 cmd_buffer
->rs_state
= (struct anv_dynamic_rs_state
*) dynamicState
;
2706 cmd_buffer
->dirty
|= ANV_CMD_BUFFER_RS_DIRTY
;
2708 case VK_STATE_BIND_POINT_COLOR_BLEND
:
2709 cmd_buffer
->cb_state
= (struct anv_dynamic_cb_state
*) dynamicState
;
2710 cmd_buffer
->dirty
|= ANV_CMD_BUFFER_CB_DIRTY
;
2712 case VK_STATE_BIND_POINT_DEPTH_STENCIL
:
2713 cmd_buffer
->ds_state
= (struct anv_dynamic_ds_state
*) dynamicState
;
2714 cmd_buffer
->dirty
|= ANV_CMD_BUFFER_DS_DIRTY
;
2721 static struct anv_state
2722 anv_cmd_buffer_alloc_surface_state(struct anv_cmd_buffer
*cmd_buffer
,
2723 uint32_t size
, uint32_t alignment
)
2725 struct anv_state state
;
2727 state
.offset
= ALIGN_U32(cmd_buffer
->surface_next
, alignment
);
2728 if (state
.offset
+ size
> cmd_buffer
->surface_batch_bo
->bo
.size
)
2729 return (struct anv_state
) { 0 };
2731 state
.map
= cmd_buffer
->surface_batch_bo
->bo
.map
+ state
.offset
;
2732 state
.alloc_size
= size
;
2733 cmd_buffer
->surface_next
= state
.offset
+ size
;
2735 assert(state
.offset
+ size
<= cmd_buffer
->surface_batch_bo
->bo
.size
);
2741 anv_cmd_buffer_new_surface_state_bo(struct anv_cmd_buffer
*cmd_buffer
)
2743 struct anv_batch_bo
*new_bbo
, *old_bbo
= cmd_buffer
->surface_batch_bo
;
2745 /* Finish off the old buffer */
2746 old_bbo
->num_relocs
=
2747 cmd_buffer
->surface_relocs
.num_relocs
- old_bbo
->first_reloc
;
2748 old_bbo
->length
= cmd_buffer
->surface_next
;
2750 VkResult result
= anv_batch_bo_create(cmd_buffer
->device
, &new_bbo
);
2751 if (result
!= VK_SUCCESS
)
2754 new_bbo
->first_reloc
= cmd_buffer
->surface_relocs
.num_relocs
;
2755 cmd_buffer
->surface_next
= 1;
2757 new_bbo
->prev_batch_bo
= old_bbo
;
2758 cmd_buffer
->surface_batch_bo
= new_bbo
;
2760 /* Re-emit state base addresses so we get the new surface state base
2761 * address before we start emitting binding tables etc.
2763 anv_cmd_buffer_emit_state_base_address(cmd_buffer
);
2765 /* It seems like just changing the state base addresses isn't enough.
2766 * Invalidating the cache seems to be enough to cause things to
2767 * propagate. However, I'm not 100% sure what we're supposed to do.
2769 anv_batch_emit(&cmd_buffer
->batch
, GEN8_PIPE_CONTROL
,
2770 .TextureCacheInvalidationEnable
= true);
2775 void anv_CmdBindDescriptorSets(
2776 VkCmdBuffer cmdBuffer
,
2777 VkPipelineBindPoint pipelineBindPoint
,
2780 const VkDescriptorSet
* pDescriptorSets
,
2781 uint32_t dynamicOffsetCount
,
2782 const uint32_t* pDynamicOffsets
)
2784 struct anv_cmd_buffer
*cmd_buffer
= (struct anv_cmd_buffer
*) cmdBuffer
;
2785 struct anv_pipeline_layout
*layout
= cmd_buffer
->pipeline
->layout
;
2786 struct anv_descriptor_set
*set
;
2787 struct anv_descriptor_set_layout
*set_layout
;
2789 assert(firstSet
+ setCount
< MAX_SETS
);
2791 uint32_t dynamic_slot
= 0;
2792 for (uint32_t i
= 0; i
< setCount
; i
++) {
2793 set
= (struct anv_descriptor_set
*) pDescriptorSets
[i
];
2794 set_layout
= layout
->set
[firstSet
+ i
].layout
;
2796 cmd_buffer
->descriptors
[firstSet
+ i
].set
= set
;
2798 assert(set_layout
->num_dynamic_buffers
<
2799 ARRAY_SIZE(cmd_buffer
->descriptors
[0].dynamic_offsets
));
2800 memcpy(cmd_buffer
->descriptors
[firstSet
+ i
].dynamic_offsets
,
2801 pDynamicOffsets
+ dynamic_slot
,
2802 set_layout
->num_dynamic_buffers
* sizeof(*pDynamicOffsets
));
2804 cmd_buffer
->descriptors_dirty
|= set_layout
->shader_stages
;
2806 dynamic_slot
+= set_layout
->num_dynamic_buffers
;
2810 void anv_CmdBindIndexBuffer(
2811 VkCmdBuffer cmdBuffer
,
2813 VkDeviceSize offset
,
2814 VkIndexType indexType
)
2816 struct anv_cmd_buffer
*cmd_buffer
= (struct anv_cmd_buffer
*) cmdBuffer
;
2817 struct anv_buffer
*buffer
= (struct anv_buffer
*) _buffer
;
2819 static const uint32_t vk_to_gen_index_type
[] = {
2820 [VK_INDEX_TYPE_UINT8
] = INDEX_BYTE
,
2821 [VK_INDEX_TYPE_UINT16
] = INDEX_WORD
,
2822 [VK_INDEX_TYPE_UINT32
] = INDEX_DWORD
,
2825 anv_batch_emit(&cmd_buffer
->batch
, GEN8_3DSTATE_INDEX_BUFFER
,
2826 .IndexFormat
= vk_to_gen_index_type
[indexType
],
2827 .MemoryObjectControlState
= GEN8_MOCS
,
2828 .BufferStartingAddress
= { buffer
->bo
, buffer
->offset
+ offset
},
2829 .BufferSize
= buffer
->size
- offset
);
2832 void anv_CmdBindVertexBuffers(
2833 VkCmdBuffer cmdBuffer
,
2834 uint32_t startBinding
,
2835 uint32_t bindingCount
,
2836 const VkBuffer
* pBuffers
,
2837 const VkDeviceSize
* pOffsets
)
2839 struct anv_cmd_buffer
*cmd_buffer
= (struct anv_cmd_buffer
*) cmdBuffer
;
2840 struct anv_vertex_binding
*vb
= cmd_buffer
->vertex_bindings
;
2842 /* We have to defer setting up vertex buffer since we need the buffer
2843 * stride from the pipeline. */
2845 assert(startBinding
+ bindingCount
< MAX_VBS
);
2846 for (uint32_t i
= 0; i
< bindingCount
; i
++) {
2847 vb
[startBinding
+ i
].buffer
= (struct anv_buffer
*) pBuffers
[i
];
2848 vb
[startBinding
+ i
].offset
= pOffsets
[i
];
2849 cmd_buffer
->vb_dirty
|= 1 << (startBinding
+ i
);
2854 cmd_buffer_emit_binding_table(struct anv_cmd_buffer
*cmd_buffer
,
2857 struct anv_pipeline_layout
*layout
= cmd_buffer
->pipeline
->layout
;
2858 uint32_t color_attachments
, bias
, size
;
2859 struct anv_state bt_state
;
2861 if (stage
== VK_SHADER_STAGE_FRAGMENT
) {
2863 color_attachments
= cmd_buffer
->framebuffer
->color_attachment_count
;
2866 color_attachments
= 0;
2869 /* This is a little awkward: layout can be NULL but we still have to
2870 * allocate and set a binding table for the PS stage for render
2872 uint32_t surface_count
= layout
? layout
->stage
[stage
].surface_count
: 0;
2874 if (color_attachments
+ surface_count
== 0)
2877 size
= (bias
+ surface_count
) * sizeof(uint32_t);
2878 bt_state
= anv_cmd_buffer_alloc_surface_state(cmd_buffer
, size
, 32);
2879 uint32_t *bt_map
= bt_state
.map
;
2881 if (bt_state
.map
== NULL
)
2882 return VK_ERROR_OUT_OF_DEVICE_MEMORY
;
2884 static const uint32_t binding_table_opcodes
[] = {
2885 [VK_SHADER_STAGE_VERTEX
] = 38,
2886 [VK_SHADER_STAGE_TESS_CONTROL
] = 39,
2887 [VK_SHADER_STAGE_TESS_EVALUATION
] = 40,
2888 [VK_SHADER_STAGE_GEOMETRY
] = 41,
2889 [VK_SHADER_STAGE_FRAGMENT
] = 42,
2890 [VK_SHADER_STAGE_COMPUTE
] = 0,
2893 anv_batch_emit(&cmd_buffer
->batch
,
2894 GEN8_3DSTATE_BINDING_TABLE_POINTERS_VS
,
2895 ._3DCommandSubOpcode
= binding_table_opcodes
[stage
],
2896 .PointertoVSBindingTable
= bt_state
.offset
);
2898 for (uint32_t ca
= 0; ca
< color_attachments
; ca
++) {
2899 const struct anv_surface_view
*view
=
2900 cmd_buffer
->framebuffer
->color_attachments
[ca
];
2902 struct anv_state state
=
2903 anv_cmd_buffer_alloc_surface_state(cmd_buffer
, 64, 64);
2905 if (state
.map
== NULL
)
2906 return VK_ERROR_OUT_OF_DEVICE_MEMORY
;
2908 memcpy(state
.map
, view
->surface_state
.map
, 64);
2910 /* The address goes in dwords 8 and 9 of the SURFACE_STATE */
2911 *(uint64_t *)(state
.map
+ 8 * 4) =
2912 anv_reloc_list_add(&cmd_buffer
->surface_relocs
,
2914 state
.offset
+ 8 * 4,
2915 view
->bo
, view
->offset
);
2917 bt_map
[ca
] = state
.offset
;
2923 for (uint32_t set
= 0; set
< layout
->num_sets
; set
++) {
2924 struct anv_descriptor_set_binding
*d
= &cmd_buffer
->descriptors
[set
];
2925 struct anv_descriptor_set_layout
*set_layout
= layout
->set
[set
].layout
;
2926 struct anv_descriptor_slot
*surface_slots
=
2927 set_layout
->stage
[stage
].surface_start
;
2929 uint32_t start
= bias
+ layout
->set
[set
].surface_start
[stage
];
2931 for (uint32_t b
= 0; b
< set_layout
->stage
[stage
].surface_count
; b
++) {
2932 struct anv_surface_view
*view
=
2933 d
->set
->descriptors
[surface_slots
[b
].index
].view
;
2938 struct anv_state state
=
2939 anv_cmd_buffer_alloc_surface_state(cmd_buffer
, 64, 64);
2941 if (state
.map
== NULL
)
2942 return VK_ERROR_OUT_OF_DEVICE_MEMORY
;
2945 if (surface_slots
[b
].dynamic_slot
>= 0) {
2946 uint32_t dynamic_offset
=
2947 d
->dynamic_offsets
[surface_slots
[b
].dynamic_slot
];
2949 offset
= view
->offset
+ dynamic_offset
;
2950 fill_buffer_surface_state(state
.map
, view
->format
, offset
,
2951 view
->range
- dynamic_offset
);
2953 offset
= view
->offset
;
2954 memcpy(state
.map
, view
->surface_state
.map
, 64);
2957 /* The address goes in dwords 8 and 9 of the SURFACE_STATE */
2958 *(uint64_t *)(state
.map
+ 8 * 4) =
2959 anv_reloc_list_add(&cmd_buffer
->surface_relocs
,
2961 state
.offset
+ 8 * 4,
2964 bt_map
[start
+ b
] = state
.offset
;
2972 cmd_buffer_emit_samplers(struct anv_cmd_buffer
*cmd_buffer
, unsigned stage
)
2974 struct anv_pipeline_layout
*layout
= cmd_buffer
->pipeline
->layout
;
2975 struct anv_state state
;
2980 uint32_t sampler_count
= layout
->stage
[stage
].sampler_count
;
2982 if (sampler_count
== 0)
2985 uint32_t size
= sampler_count
* 16;
2986 state
= anv_state_stream_alloc(&cmd_buffer
->dynamic_state_stream
, size
, 32);
2988 if (state
.map
== NULL
)
2989 return VK_ERROR_OUT_OF_DEVICE_MEMORY
;
2991 static const uint32_t sampler_state_opcodes
[] = {
2992 [VK_SHADER_STAGE_VERTEX
] = 43,
2993 [VK_SHADER_STAGE_TESS_CONTROL
] = 44, /* HS */
2994 [VK_SHADER_STAGE_TESS_EVALUATION
] = 45, /* DS */
2995 [VK_SHADER_STAGE_GEOMETRY
] = 46,
2996 [VK_SHADER_STAGE_FRAGMENT
] = 47,
2997 [VK_SHADER_STAGE_COMPUTE
] = 0,
3000 anv_batch_emit(&cmd_buffer
->batch
,
3001 GEN8_3DSTATE_SAMPLER_STATE_POINTERS_VS
,
3002 ._3DCommandSubOpcode
= sampler_state_opcodes
[stage
],
3003 .PointertoVSSamplerState
= state
.offset
);
3005 for (uint32_t set
= 0; set
< layout
->num_sets
; set
++) {
3006 struct anv_descriptor_set_binding
*d
= &cmd_buffer
->descriptors
[set
];
3007 struct anv_descriptor_set_layout
*set_layout
= layout
->set
[set
].layout
;
3008 struct anv_descriptor_slot
*sampler_slots
=
3009 set_layout
->stage
[stage
].sampler_start
;
3011 uint32_t start
= layout
->set
[set
].sampler_start
[stage
];
3013 for (uint32_t b
= 0; b
< set_layout
->stage
[stage
].sampler_count
; b
++) {
3014 struct anv_sampler
*sampler
=
3015 d
->set
->descriptors
[sampler_slots
[b
].index
].sampler
;
3020 memcpy(state
.map
+ (start
+ b
) * 16,
3021 sampler
->state
, sizeof(sampler
->state
));
3029 flush_descriptor_sets(struct anv_cmd_buffer
*cmd_buffer
)
3031 uint32_t s
, dirty
= cmd_buffer
->descriptors_dirty
&
3032 cmd_buffer
->pipeline
->active_stages
;
3035 for_each_bit(s
, dirty
) {
3036 result
= cmd_buffer_emit_binding_table(cmd_buffer
, s
);
3037 if (result
!= VK_SUCCESS
)
3040 result
= cmd_buffer_emit_samplers(cmd_buffer
, s
);
3041 if (result
!= VK_SUCCESS
)
3045 if (result
!= VK_SUCCESS
) {
3046 assert(result
== VK_ERROR_OUT_OF_DEVICE_MEMORY
);
3048 result
= anv_cmd_buffer_new_surface_state_bo(cmd_buffer
);
3049 assert(result
== VK_SUCCESS
);
3051 /* Re-emit all active binding tables */
3052 for_each_bit(s
, cmd_buffer
->pipeline
->active_stages
) {
3053 result
= cmd_buffer_emit_binding_table(cmd_buffer
, s
);
3054 result
= cmd_buffer_emit_samplers(cmd_buffer
, s
);
3057 /* It had better succeed this time */
3058 assert(result
== VK_SUCCESS
);
3061 cmd_buffer
->descriptors_dirty
&= ~cmd_buffer
->pipeline
->active_stages
;
3064 static struct anv_state
3065 anv_cmd_buffer_emit_dynamic(struct anv_cmd_buffer
*cmd_buffer
,
3066 uint32_t *a
, uint32_t dwords
, uint32_t alignment
)
3068 struct anv_state state
;
3070 state
= anv_state_stream_alloc(&cmd_buffer
->dynamic_state_stream
,
3071 dwords
* 4, alignment
);
3072 memcpy(state
.map
, a
, dwords
* 4);
3074 VG(VALGRIND_CHECK_MEM_IS_DEFINED(state
.map
, dwords
* 4));
3079 static struct anv_state
3080 anv_cmd_buffer_merge_dynamic(struct anv_cmd_buffer
*cmd_buffer
,
3081 uint32_t *a
, uint32_t *b
,
3082 uint32_t dwords
, uint32_t alignment
)
3084 struct anv_state state
;
3087 state
= anv_state_stream_alloc(&cmd_buffer
->dynamic_state_stream
,
3088 dwords
* 4, alignment
);
3090 for (uint32_t i
= 0; i
< dwords
; i
++)
3093 VG(VALGRIND_CHECK_MEM_IS_DEFINED(p
, dwords
* 4));
3099 anv_cmd_buffer_flush_state(struct anv_cmd_buffer
*cmd_buffer
)
3101 struct anv_pipeline
*pipeline
= cmd_buffer
->pipeline
;
3104 uint32_t vb_emit
= cmd_buffer
->vb_dirty
& pipeline
->vb_used
;
3107 const uint32_t num_buffers
= __builtin_popcount(vb_emit
);
3108 const uint32_t num_dwords
= 1 + num_buffers
* 4;
3110 p
= anv_batch_emitn(&cmd_buffer
->batch
, num_dwords
,
3111 GEN8_3DSTATE_VERTEX_BUFFERS
);
3113 for_each_bit(vb
, vb_emit
) {
3114 struct anv_buffer
*buffer
= cmd_buffer
->vertex_bindings
[vb
].buffer
;
3115 uint32_t offset
= cmd_buffer
->vertex_bindings
[vb
].offset
;
3117 struct GEN8_VERTEX_BUFFER_STATE state
= {
3118 .VertexBufferIndex
= vb
,
3119 .MemoryObjectControlState
= GEN8_MOCS
,
3120 .AddressModifyEnable
= true,
3121 .BufferPitch
= pipeline
->binding_stride
[vb
],
3122 .BufferStartingAddress
= { buffer
->bo
, buffer
->offset
+ offset
},
3123 .BufferSize
= buffer
->size
- offset
3126 GEN8_VERTEX_BUFFER_STATE_pack(&cmd_buffer
->batch
, &p
[1 + i
* 4], &state
);
3131 if (cmd_buffer
->dirty
& ANV_CMD_BUFFER_PIPELINE_DIRTY
)
3132 anv_batch_emit_batch(&cmd_buffer
->batch
, &pipeline
->batch
);
3134 if (cmd_buffer
->descriptors_dirty
)
3135 flush_descriptor_sets(cmd_buffer
);
3137 if (cmd_buffer
->dirty
& (ANV_CMD_BUFFER_PIPELINE_DIRTY
| ANV_CMD_BUFFER_RS_DIRTY
)) {
3138 anv_batch_emit_merge(&cmd_buffer
->batch
,
3139 cmd_buffer
->rs_state
->state_sf
, pipeline
->state_sf
);
3140 anv_batch_emit_merge(&cmd_buffer
->batch
,
3141 cmd_buffer
->rs_state
->state_raster
, pipeline
->state_raster
);
3144 if (cmd_buffer
->ds_state
&&
3145 (cmd_buffer
->dirty
& (ANV_CMD_BUFFER_PIPELINE_DIRTY
| ANV_CMD_BUFFER_DS_DIRTY
)))
3146 anv_batch_emit_merge(&cmd_buffer
->batch
,
3147 cmd_buffer
->ds_state
->state_wm_depth_stencil
,
3148 pipeline
->state_wm_depth_stencil
);
3150 if (cmd_buffer
->dirty
& (ANV_CMD_BUFFER_CB_DIRTY
| ANV_CMD_BUFFER_DS_DIRTY
)) {
3151 struct anv_state state
;
3152 if (cmd_buffer
->ds_state
== NULL
)
3153 state
= anv_cmd_buffer_emit_dynamic(cmd_buffer
,
3154 cmd_buffer
->cb_state
->state_color_calc
,
3155 GEN8_COLOR_CALC_STATE_length
, 64);
3156 else if (cmd_buffer
->cb_state
== NULL
)
3157 state
= anv_cmd_buffer_emit_dynamic(cmd_buffer
,
3158 cmd_buffer
->ds_state
->state_color_calc
,
3159 GEN8_COLOR_CALC_STATE_length
, 64);
3161 state
= anv_cmd_buffer_merge_dynamic(cmd_buffer
,
3162 cmd_buffer
->ds_state
->state_color_calc
,
3163 cmd_buffer
->cb_state
->state_color_calc
,
3164 GEN8_COLOR_CALC_STATE_length
, 64);
3166 anv_batch_emit(&cmd_buffer
->batch
,
3167 GEN8_3DSTATE_CC_STATE_POINTERS
,
3168 .ColorCalcStatePointer
= state
.offset
,
3169 .ColorCalcStatePointerValid
= true);
3172 cmd_buffer
->vb_dirty
&= ~vb_emit
;
3173 cmd_buffer
->dirty
= 0;
3177 VkCmdBuffer cmdBuffer
,
3178 uint32_t firstVertex
,
3179 uint32_t vertexCount
,
3180 uint32_t firstInstance
,
3181 uint32_t instanceCount
)
3183 struct anv_cmd_buffer
*cmd_buffer
= (struct anv_cmd_buffer
*) cmdBuffer
;
3185 anv_cmd_buffer_flush_state(cmd_buffer
);
3187 anv_batch_emit(&cmd_buffer
->batch
, GEN8_3DPRIMITIVE
,
3188 .VertexAccessType
= SEQUENTIAL
,
3189 .VertexCountPerInstance
= vertexCount
,
3190 .StartVertexLocation
= firstVertex
,
3191 .InstanceCount
= instanceCount
,
3192 .StartInstanceLocation
= firstInstance
,
3193 .BaseVertexLocation
= 0);
3196 void anv_CmdDrawIndexed(
3197 VkCmdBuffer cmdBuffer
,
3198 uint32_t firstIndex
,
3199 uint32_t indexCount
,
3200 int32_t vertexOffset
,
3201 uint32_t firstInstance
,
3202 uint32_t instanceCount
)
3204 struct anv_cmd_buffer
*cmd_buffer
= (struct anv_cmd_buffer
*) cmdBuffer
;
3206 anv_cmd_buffer_flush_state(cmd_buffer
);
3208 anv_batch_emit(&cmd_buffer
->batch
, GEN8_3DPRIMITIVE
,
3209 .VertexAccessType
= RANDOM
,
3210 .VertexCountPerInstance
= indexCount
,
3211 .StartVertexLocation
= firstIndex
,
3212 .InstanceCount
= instanceCount
,
3213 .StartInstanceLocation
= firstInstance
,
3214 .BaseVertexLocation
= vertexOffset
);
3218 anv_batch_lrm(struct anv_batch
*batch
,
3219 uint32_t reg
, struct anv_bo
*bo
, uint32_t offset
)
3221 anv_batch_emit(batch
, GEN8_MI_LOAD_REGISTER_MEM
,
3222 .RegisterAddress
= reg
,
3223 .MemoryAddress
= { bo
, offset
});
3227 anv_batch_lri(struct anv_batch
*batch
, uint32_t reg
, uint32_t imm
)
3229 anv_batch_emit(batch
, GEN8_MI_LOAD_REGISTER_IMM
,
3230 .RegisterOffset
= reg
,
3234 /* Auto-Draw / Indirect Registers */
3235 #define GEN7_3DPRIM_END_OFFSET 0x2420
3236 #define GEN7_3DPRIM_START_VERTEX 0x2430
3237 #define GEN7_3DPRIM_VERTEX_COUNT 0x2434
3238 #define GEN7_3DPRIM_INSTANCE_COUNT 0x2438
3239 #define GEN7_3DPRIM_START_INSTANCE 0x243C
3240 #define GEN7_3DPRIM_BASE_VERTEX 0x2440
3242 void anv_CmdDrawIndirect(
3243 VkCmdBuffer cmdBuffer
,
3245 VkDeviceSize offset
,
3249 struct anv_cmd_buffer
*cmd_buffer
= (struct anv_cmd_buffer
*) cmdBuffer
;
3250 struct anv_buffer
*buffer
= (struct anv_buffer
*) _buffer
;
3251 struct anv_bo
*bo
= buffer
->bo
;
3252 uint32_t bo_offset
= buffer
->offset
+ offset
;
3254 anv_cmd_buffer_flush_state(cmd_buffer
);
3256 anv_batch_lrm(&cmd_buffer
->batch
, GEN7_3DPRIM_VERTEX_COUNT
, bo
, bo_offset
);
3257 anv_batch_lrm(&cmd_buffer
->batch
, GEN7_3DPRIM_INSTANCE_COUNT
, bo
, bo_offset
+ 4);
3258 anv_batch_lrm(&cmd_buffer
->batch
, GEN7_3DPRIM_START_VERTEX
, bo
, bo_offset
+ 8);
3259 anv_batch_lrm(&cmd_buffer
->batch
, GEN7_3DPRIM_START_INSTANCE
, bo
, bo_offset
+ 12);
3260 anv_batch_lri(&cmd_buffer
->batch
, GEN7_3DPRIM_BASE_VERTEX
, 0);
3262 anv_batch_emit(&cmd_buffer
->batch
, GEN8_3DPRIMITIVE
,
3263 .IndirectParameterEnable
= true,
3264 .VertexAccessType
= SEQUENTIAL
);
3267 void anv_CmdDrawIndexedIndirect(
3268 VkCmdBuffer cmdBuffer
,
3270 VkDeviceSize offset
,
3274 struct anv_cmd_buffer
*cmd_buffer
= (struct anv_cmd_buffer
*) cmdBuffer
;
3275 struct anv_buffer
*buffer
= (struct anv_buffer
*) _buffer
;
3276 struct anv_bo
*bo
= buffer
->bo
;
3277 uint32_t bo_offset
= buffer
->offset
+ offset
;
3279 anv_cmd_buffer_flush_state(cmd_buffer
);
3281 anv_batch_lrm(&cmd_buffer
->batch
, GEN7_3DPRIM_VERTEX_COUNT
, bo
, bo_offset
);
3282 anv_batch_lrm(&cmd_buffer
->batch
, GEN7_3DPRIM_INSTANCE_COUNT
, bo
, bo_offset
+ 4);
3283 anv_batch_lrm(&cmd_buffer
->batch
, GEN7_3DPRIM_START_VERTEX
, bo
, bo_offset
+ 8);
3284 anv_batch_lrm(&cmd_buffer
->batch
, GEN7_3DPRIM_BASE_VERTEX
, bo
, bo_offset
+ 12);
3285 anv_batch_lrm(&cmd_buffer
->batch
, GEN7_3DPRIM_START_INSTANCE
, bo
, bo_offset
+ 16);
3287 anv_batch_emit(&cmd_buffer
->batch
, GEN8_3DPRIMITIVE
,
3288 .IndirectParameterEnable
= true,
3289 .VertexAccessType
= RANDOM
);
3292 void anv_CmdDispatch(
3293 VkCmdBuffer cmdBuffer
,
3301 void anv_CmdDispatchIndirect(
3302 VkCmdBuffer cmdBuffer
,
3304 VkDeviceSize offset
)
3309 void anv_CmdSetEvent(
3310 VkCmdBuffer cmdBuffer
,
3312 VkPipeEvent pipeEvent
)
3317 void anv_CmdResetEvent(
3318 VkCmdBuffer cmdBuffer
,
3320 VkPipeEvent pipeEvent
)
3325 void anv_CmdWaitEvents(
3326 VkCmdBuffer cmdBuffer
,
3327 VkWaitEvent waitEvent
,
3328 uint32_t eventCount
,
3329 const VkEvent
* pEvents
,
3330 uint32_t memBarrierCount
,
3331 const void** ppMemBarriers
)
3336 void anv_CmdPipelineBarrier(
3337 VkCmdBuffer cmdBuffer
,
3338 VkWaitEvent waitEvent
,
3339 uint32_t pipeEventCount
,
3340 const VkPipeEvent
* pPipeEvents
,
3341 uint32_t memBarrierCount
,
3342 const void** ppMemBarriers
)
3344 struct anv_cmd_buffer
*cmd_buffer
= (struct anv_cmd_buffer
*)cmdBuffer
;
3347 struct GEN8_PIPE_CONTROL cmd
= {
3348 GEN8_PIPE_CONTROL_header
,
3349 .PostSyncOperation
= NoWrite
,
3352 /* XXX: I think waitEvent is a no-op on our HW. We should verify that. */
3354 for (uint32_t i
= 0; i
< pipeEventCount
; i
++) {
3355 switch (pPipeEvents
[i
]) {
3356 case VK_PIPE_EVENT_TOP_OF_PIPE
:
3357 /* This is just what PIPE_CONTROL does */
3359 case VK_PIPE_EVENT_VERTEX_PROCESSING_COMPLETE
:
3360 case VK_PIPE_EVENT_LOCAL_FRAGMENT_PROCESSING_COMPLETE
:
3361 case VK_PIPE_EVENT_FRAGMENT_PROCESSING_COMPLETE
:
3362 cmd
.StallAtPixelScoreboard
= true;
3364 case VK_PIPE_EVENT_GRAPHICS_PIPELINE_COMPLETE
:
3365 case VK_PIPE_EVENT_COMPUTE_PIPELINE_COMPLETE
:
3366 case VK_PIPE_EVENT_TRANSFER_COMPLETE
:
3367 case VK_PIPE_EVENT_COMMANDS_COMPLETE
:
3368 cmd
.CommandStreamerStallEnable
= true;
3371 unreachable("Invalid VkPipeEvent");
3375 /* XXX: Right now, we're really dumb and just flush whatever categories
3376 * the app asks for. One of these days we may make this a bit better
3377 * but right now that's all the hardware allows for in most areas.
3379 VkMemoryOutputFlags out_flags
= 0;
3380 VkMemoryInputFlags in_flags
= 0;
3382 for (uint32_t i
= 0; i
< memBarrierCount
; i
++) {
3383 const struct anv_common
*common
= ppMemBarriers
[i
];
3384 switch (common
->sType
) {
3385 case VK_STRUCTURE_TYPE_MEMORY_BARRIER
: {
3386 const VkMemoryBarrier
*barrier
= (VkMemoryBarrier
*)common
;
3387 out_flags
|= barrier
->outputMask
;
3388 in_flags
|= barrier
->inputMask
;
3391 case VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER
: {
3392 const VkBufferMemoryBarrier
*barrier
= (VkBufferMemoryBarrier
*)common
;
3393 out_flags
|= barrier
->outputMask
;
3394 in_flags
|= barrier
->inputMask
;
3397 case VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER
: {
3398 const VkImageMemoryBarrier
*barrier
= (VkImageMemoryBarrier
*)common
;
3399 out_flags
|= barrier
->outputMask
;
3400 in_flags
|= barrier
->inputMask
;
3404 unreachable("Invalid memory barrier type");
3408 for_each_bit(b
, out_flags
) {
3409 switch ((VkMemoryOutputFlags
)(1 << b
)) {
3410 case VK_MEMORY_OUTPUT_CPU_WRITE_BIT
:
3411 break; /* FIXME: Little-core systems */
3412 case VK_MEMORY_OUTPUT_SHADER_WRITE_BIT
:
3413 cmd
.DCFlushEnable
= true;
3415 case VK_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT
:
3416 cmd
.RenderTargetCacheFlushEnable
= true;
3418 case VK_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT
:
3419 cmd
.DepthCacheFlushEnable
= true;
3421 case VK_MEMORY_OUTPUT_TRANSFER_BIT
:
3422 cmd
.RenderTargetCacheFlushEnable
= true;
3423 cmd
.DepthCacheFlushEnable
= true;
3426 unreachable("Invalid memory output flag");
3430 for_each_bit(b
, out_flags
) {
3431 switch ((VkMemoryInputFlags
)(1 << b
)) {
3432 case VK_MEMORY_INPUT_CPU_READ_BIT
:
3433 break; /* FIXME: Little-core systems */
3434 case VK_MEMORY_INPUT_INDIRECT_COMMAND_BIT
:
3435 case VK_MEMORY_INPUT_INDEX_FETCH_BIT
:
3436 case VK_MEMORY_INPUT_VERTEX_ATTRIBUTE_FETCH_BIT
:
3437 cmd
.VFCacheInvalidationEnable
= true;
3439 case VK_MEMORY_INPUT_UNIFORM_READ_BIT
:
3440 cmd
.ConstantCacheInvalidationEnable
= true;
3442 case VK_MEMORY_INPUT_SHADER_READ_BIT
:
3443 cmd
.DCFlushEnable
= true;
3444 cmd
.TextureCacheInvalidationEnable
= true;
3446 case VK_MEMORY_INPUT_COLOR_ATTACHMENT_BIT
:
3447 case VK_MEMORY_INPUT_DEPTH_STENCIL_ATTACHMENT_BIT
:
3448 break; /* XXX: Hunh? */
3449 case VK_MEMORY_INPUT_TRANSFER_BIT
:
3450 cmd
.TextureCacheInvalidationEnable
= true;
3455 dw
= anv_batch_emit_dwords(&cmd_buffer
->batch
, GEN8_PIPE_CONTROL_length
);
3456 GEN8_PIPE_CONTROL_pack(&cmd_buffer
->batch
, dw
, &cmd
);
3459 void anv_CmdInitAtomicCounters(
3460 VkCmdBuffer cmdBuffer
,
3461 VkPipelineBindPoint pipelineBindPoint
,
3462 uint32_t startCounter
,
3463 uint32_t counterCount
,
3464 const uint32_t* pData
)
3469 void anv_CmdLoadAtomicCounters(
3470 VkCmdBuffer cmdBuffer
,
3471 VkPipelineBindPoint pipelineBindPoint
,
3472 uint32_t startCounter
,
3473 uint32_t counterCount
,
3475 VkDeviceSize srcOffset
)
3480 void anv_CmdSaveAtomicCounters(
3481 VkCmdBuffer cmdBuffer
,
3482 VkPipelineBindPoint pipelineBindPoint
,
3483 uint32_t startCounter
,
3484 uint32_t counterCount
,
3485 VkBuffer destBuffer
,
3486 VkDeviceSize destOffset
)
3492 anv_framebuffer_destroy(struct anv_device
*device
,
3493 struct anv_object
*object
,
3494 VkObjectType obj_type
)
3496 struct anv_framebuffer
*fb
= (struct anv_framebuffer
*)object
;
3498 assert(obj_type
== VK_OBJECT_TYPE_FRAMEBUFFER
);
3500 anv_DestroyObject((VkDevice
) device
,
3501 VK_OBJECT_TYPE_DYNAMIC_VP_STATE
,
3504 anv_device_free(device
, fb
);
3507 VkResult
anv_CreateFramebuffer(
3509 const VkFramebufferCreateInfo
* pCreateInfo
,
3510 VkFramebuffer
* pFramebuffer
)
3512 struct anv_device
*device
= (struct anv_device
*) _device
;
3513 struct anv_framebuffer
*framebuffer
;
3515 static const struct anv_depth_stencil_view null_view
=
3516 { .depth_format
= D16_UNORM
, .depth_stride
= 0, .stencil_stride
= 0 };
3518 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO
);
3520 framebuffer
= anv_device_alloc(device
, sizeof(*framebuffer
), 8,
3521 VK_SYSTEM_ALLOC_TYPE_API_OBJECT
);
3522 if (framebuffer
== NULL
)
3523 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
3525 framebuffer
->base
.destructor
= anv_framebuffer_destroy
;
3527 framebuffer
->color_attachment_count
= pCreateInfo
->colorAttachmentCount
;
3528 for (uint32_t i
= 0; i
< pCreateInfo
->colorAttachmentCount
; i
++) {
3529 framebuffer
->color_attachments
[i
] =
3530 (struct anv_surface_view
*) pCreateInfo
->pColorAttachments
[i
].view
;
3533 if (pCreateInfo
->pDepthStencilAttachment
) {
3534 framebuffer
->depth_stencil
=
3535 (struct anv_depth_stencil_view
*) pCreateInfo
->pDepthStencilAttachment
->view
;
3537 framebuffer
->depth_stencil
= &null_view
;
3540 framebuffer
->sample_count
= pCreateInfo
->sampleCount
;
3541 framebuffer
->width
= pCreateInfo
->width
;
3542 framebuffer
->height
= pCreateInfo
->height
;
3543 framebuffer
->layers
= pCreateInfo
->layers
;
3545 anv_CreateDynamicViewportState((VkDevice
) device
,
3546 &(VkDynamicVpStateCreateInfo
) {
3547 .sType
= VK_STRUCTURE_TYPE_DYNAMIC_VP_STATE_CREATE_INFO
,
3548 .viewportAndScissorCount
= 1,
3549 .pViewports
= (VkViewport
[]) {
3553 .width
= pCreateInfo
->width
,
3554 .height
= pCreateInfo
->height
,
3559 .pScissors
= (VkRect
[]) {
3561 { pCreateInfo
->width
, pCreateInfo
->height
} },
3564 &framebuffer
->vp_state
);
3566 *pFramebuffer
= (VkFramebuffer
) framebuffer
;
3571 VkResult
anv_CreateRenderPass(
3573 const VkRenderPassCreateInfo
* pCreateInfo
,
3574 VkRenderPass
* pRenderPass
)
3576 struct anv_device
*device
= (struct anv_device
*) _device
;
3577 struct anv_render_pass
*pass
;
3580 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO
);
3582 size
= sizeof(*pass
) +
3583 pCreateInfo
->layers
* sizeof(struct anv_render_pass_layer
);
3584 pass
= anv_device_alloc(device
, size
, 8,
3585 VK_SYSTEM_ALLOC_TYPE_API_OBJECT
);
3587 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
3589 pass
->render_area
= pCreateInfo
->renderArea
;
3591 pass
->num_layers
= pCreateInfo
->layers
;
3593 pass
->num_clear_layers
= 0;
3594 for (uint32_t i
= 0; i
< pCreateInfo
->layers
; i
++) {
3595 pass
->layers
[i
].color_load_op
= pCreateInfo
->pColorLoadOps
[i
];
3596 pass
->layers
[i
].clear_color
= pCreateInfo
->pColorLoadClearValues
[i
];
3597 if (pass
->layers
[i
].color_load_op
== VK_ATTACHMENT_LOAD_OP_CLEAR
)
3598 pass
->num_clear_layers
++;
3601 *pRenderPass
= (VkRenderPass
) pass
;
3607 anv_cmd_buffer_emit_depth_stencil(struct anv_cmd_buffer
*cmd_buffer
,
3608 struct anv_render_pass
*pass
)
3610 const struct anv_depth_stencil_view
*view
=
3611 cmd_buffer
->framebuffer
->depth_stencil
;
3613 /* FIXME: Implement the PMA stall W/A */
3615 anv_batch_emit(&cmd_buffer
->batch
, GEN8_3DSTATE_DEPTH_BUFFER
,
3616 .SurfaceType
= SURFTYPE_2D
,
3617 .DepthWriteEnable
= view
->depth_stride
> 0,
3618 .StencilWriteEnable
= view
->stencil_stride
> 0,
3619 .HierarchicalDepthBufferEnable
= false,
3620 .SurfaceFormat
= view
->depth_format
,
3621 .SurfacePitch
= view
->depth_stride
> 0 ? view
->depth_stride
- 1 : 0,
3622 .SurfaceBaseAddress
= { view
->bo
, view
->depth_offset
},
3623 .Height
= pass
->render_area
.extent
.height
- 1,
3624 .Width
= pass
->render_area
.extent
.width
- 1,
3627 .MinimumArrayElement
= 0,
3628 .DepthBufferObjectControlState
= GEN8_MOCS
,
3629 .RenderTargetViewExtent
= 1 - 1,
3630 .SurfaceQPitch
= 0);
3632 /* Disable hierarchial depth buffers. */
3633 anv_batch_emit(&cmd_buffer
->batch
, GEN8_3DSTATE_HIER_DEPTH_BUFFER
);
3635 anv_batch_emit(&cmd_buffer
->batch
, GEN8_3DSTATE_STENCIL_BUFFER
,
3636 .StencilBufferEnable
= view
->stencil_stride
> 0,
3637 .StencilBufferObjectControlState
= GEN8_MOCS
,
3638 .SurfacePitch
= view
->stencil_stride
> 0 ? view
->stencil_stride
- 1 : 0,
3639 .SurfaceBaseAddress
= { view
->bo
, view
->stencil_offset
},
3640 .SurfaceQPitch
= 0);
3642 /* Clear the clear params. */
3643 anv_batch_emit(&cmd_buffer
->batch
, GEN8_3DSTATE_CLEAR_PARAMS
);
3646 void anv_CmdBeginRenderPass(
3647 VkCmdBuffer cmdBuffer
,
3648 const VkRenderPassBegin
* pRenderPassBegin
)
3650 struct anv_cmd_buffer
*cmd_buffer
= (struct anv_cmd_buffer
*) cmdBuffer
;
3651 struct anv_render_pass
*pass
= (struct anv_render_pass
*) pRenderPassBegin
->renderPass
;
3652 struct anv_framebuffer
*framebuffer
=
3653 (struct anv_framebuffer
*) pRenderPassBegin
->framebuffer
;
3655 cmd_buffer
->framebuffer
= framebuffer
;
3657 cmd_buffer
->descriptors_dirty
|= VK_SHADER_STAGE_FRAGMENT_BIT
;
3659 anv_batch_emit(&cmd_buffer
->batch
, GEN8_3DSTATE_DRAWING_RECTANGLE
,
3660 .ClippedDrawingRectangleYMin
= pass
->render_area
.offset
.y
,
3661 .ClippedDrawingRectangleXMin
= pass
->render_area
.offset
.x
,
3662 .ClippedDrawingRectangleYMax
=
3663 pass
->render_area
.offset
.y
+ pass
->render_area
.extent
.height
- 1,
3664 .ClippedDrawingRectangleXMax
=
3665 pass
->render_area
.offset
.x
+ pass
->render_area
.extent
.width
- 1,
3666 .DrawingRectangleOriginY
= 0,
3667 .DrawingRectangleOriginX
= 0);
3669 anv_cmd_buffer_emit_depth_stencil(cmd_buffer
, pass
);
3671 anv_cmd_buffer_clear(cmd_buffer
, pass
);
3674 void anv_CmdEndRenderPass(
3675 VkCmdBuffer cmdBuffer
,
3676 VkRenderPass renderPass
)
3678 /* Emit a flushing pipe control at the end of a pass. This is kind of a
3679 * hack but it ensures that render targets always actually get written.
3680 * Eventually, we should do flushing based on image format transitions
3681 * or something of that nature.
3683 struct anv_cmd_buffer
*cmd_buffer
= (struct anv_cmd_buffer
*)cmdBuffer
;
3684 anv_batch_emit(&cmd_buffer
->batch
, GEN8_PIPE_CONTROL
,
3685 .PostSyncOperation
= NoWrite
,
3686 .RenderTargetCacheFlushEnable
= true,
3687 .InstructionCacheInvalidateEnable
= true,
3688 .DepthCacheFlushEnable
= true,
3689 .VFCacheInvalidationEnable
= true,
3690 .TextureCacheInvalidationEnable
= true,
3691 .CommandStreamerStallEnable
= true);
3694 void vkCmdDbgMarkerBegin(
3695 VkCmdBuffer cmdBuffer
,
3696 const char* pMarker
)
3697 __attribute__ ((visibility ("default")));
3699 void vkCmdDbgMarkerEnd(
3700 VkCmdBuffer cmdBuffer
)
3701 __attribute__ ((visibility ("default")));
3703 VkResult
vkDbgSetObjectTag(
3708 __attribute__ ((visibility ("default")));
3711 void vkCmdDbgMarkerBegin(
3712 VkCmdBuffer cmdBuffer
,
3713 const char* pMarker
)
3717 void vkCmdDbgMarkerEnd(
3718 VkCmdBuffer cmdBuffer
)
3722 VkResult
vkDbgSetObjectTag(