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_device_init_border_colors(struct anv_device
*device
)
309 float float_border_colors
[][4] = {
310 [VK_BORDER_COLOR_OPAQUE_WHITE
] = { 1.0, 1.0, 1.0, 1.0 },
311 [VK_BORDER_COLOR_TRANSPARENT_BLACK
] = { 0.0, 0.0, 0.0, 0.0 },
312 [VK_BORDER_COLOR_OPAQUE_BLACK
] = { 0.0, 0.0, 0.0, 1.0 }
315 uint32_t uint32_border_colors
[][4] = {
316 [VK_BORDER_COLOR_OPAQUE_WHITE
] = { 1, 1, 1, 1 },
317 [VK_BORDER_COLOR_TRANSPARENT_BLACK
] = { 0, 0, 0, 0 },
318 [VK_BORDER_COLOR_OPAQUE_BLACK
] = { 0, 0, 0, 1 }
321 device
->float_border_colors
=
322 anv_state_pool_alloc(&device
->dynamic_state_pool
,
323 sizeof(float_border_colors
), 32);
324 memcpy(device
->float_border_colors
.map
,
325 float_border_colors
, sizeof(float_border_colors
));
327 device
->uint32_border_colors
=
328 anv_state_pool_alloc(&device
->dynamic_state_pool
,
329 sizeof(uint32_border_colors
), 32);
330 memcpy(device
->uint32_border_colors
.map
,
331 uint32_border_colors
, sizeof(uint32_border_colors
));
335 static const uint32_t BATCH_SIZE
= 8192;
337 VkResult
anv_CreateDevice(
338 VkPhysicalDevice _physicalDevice
,
339 const VkDeviceCreateInfo
* pCreateInfo
,
342 struct anv_physical_device
*physicalDevice
=
343 (struct anv_physical_device
*) _physicalDevice
;
344 struct anv_instance
*instance
= physicalDevice
->instance
;
345 struct anv_device
*device
;
347 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO
);
349 device
= instance
->pfnAlloc(instance
->pAllocUserData
,
351 VK_SYSTEM_ALLOC_TYPE_API_OBJECT
);
353 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
355 device
->no_hw
= physicalDevice
->no_hw
;
356 parse_debug_flags(device
);
358 device
->instance
= physicalDevice
->instance
;
359 device
->fd
= open("/dev/dri/renderD128", O_RDWR
| O_CLOEXEC
);
360 if (device
->fd
== -1)
363 device
->context_id
= anv_gem_create_context(device
);
364 if (device
->context_id
== -1)
367 anv_bo_pool_init(&device
->batch_bo_pool
, device
, BATCH_SIZE
);
369 anv_block_pool_init(&device
->dynamic_state_block_pool
, device
, 2048);
371 anv_state_pool_init(&device
->dynamic_state_pool
,
372 &device
->dynamic_state_block_pool
);
374 anv_block_pool_init(&device
->instruction_block_pool
, device
, 2048);
375 anv_block_pool_init(&device
->surface_state_block_pool
, device
, 2048);
377 anv_state_pool_init(&device
->surface_state_pool
,
378 &device
->surface_state_block_pool
);
380 device
->info
= *physicalDevice
->info
;
382 device
->compiler
= anv_compiler_create(device
);
383 device
->aub_writer
= NULL
;
385 pthread_mutex_init(&device
->mutex
, NULL
);
387 anv_device_init_meta(device
);
389 anv_device_init_border_colors(device
);
391 *pDevice
= (VkDevice
) device
;
398 anv_device_free(device
, device
);
400 return vk_error(VK_ERROR_UNAVAILABLE
);
403 VkResult
anv_DestroyDevice(
406 struct anv_device
*device
= (struct anv_device
*) _device
;
408 /* FIXME: We should make device destruction actually safe. */
409 return VK_UNSUPPORTED
;
411 anv_compiler_destroy(device
->compiler
);
413 anv_device_finish_meta(device
);
416 /* We only need to free these to prevent valgrind errors. The backing
417 * BO will go away in a couple of lines so we don't actually leak.
419 anv_state_pool_free(&device
->dynamic_state_pool
,
420 device
->float_border_colors
);
421 anv_state_pool_free(&device
->dynamic_state_pool
,
422 device
->uint32_border_colors
);
425 anv_bo_pool_finish(&device
->batch_bo_pool
);
426 anv_block_pool_finish(&device
->dynamic_state_block_pool
);
427 anv_block_pool_finish(&device
->instruction_block_pool
);
428 anv_block_pool_finish(&device
->surface_state_block_pool
);
432 if (device
->aub_writer
)
433 anv_aub_writer_destroy(device
->aub_writer
);
435 anv_device_free(device
, device
);
440 VkResult
anv_GetGlobalExtensionInfo(
441 VkExtensionInfoType infoType
,
442 uint32_t extensionIndex
,
446 static const VkExtensionProperties extensions
[] = {
448 .extName
= "VK_WSI_LunarG",
452 uint32_t count
= ARRAY_SIZE(extensions
);
455 case VK_EXTENSION_INFO_TYPE_COUNT
:
456 memcpy(pData
, &count
, sizeof(count
));
457 *pDataSize
= sizeof(count
);
460 case VK_EXTENSION_INFO_TYPE_PROPERTIES
:
461 if (extensionIndex
>= count
)
462 return vk_error(VK_ERROR_INVALID_EXTENSION
);
464 memcpy(pData
, &extensions
[extensionIndex
], sizeof(extensions
[0]));
465 *pDataSize
= sizeof(extensions
[0]);
469 return VK_UNSUPPORTED
;
473 VkResult
anv_GetPhysicalDeviceExtensionInfo(
474 VkPhysicalDevice physicalDevice
,
475 VkExtensionInfoType infoType
,
476 uint32_t extensionIndex
,
483 case VK_EXTENSION_INFO_TYPE_COUNT
:
492 case VK_EXTENSION_INFO_TYPE_PROPERTIES
:
493 return vk_error(VK_ERROR_INVALID_EXTENSION
);
496 return VK_UNSUPPORTED
;
500 VkResult
anv_EnumerateLayers(
501 VkPhysicalDevice physicalDevice
,
502 size_t maxStringSize
,
504 char* const* pOutLayers
,
512 VkResult
anv_GetDeviceQueue(
514 uint32_t queueNodeIndex
,
518 struct anv_device
*device
= (struct anv_device
*) _device
;
519 struct anv_queue
*queue
;
521 /* FIXME: Should allocate these at device create time. */
523 queue
= anv_device_alloc(device
, sizeof(*queue
), 8,
524 VK_SYSTEM_ALLOC_TYPE_API_OBJECT
);
526 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
528 queue
->device
= device
;
529 queue
->pool
= &device
->surface_state_pool
;
531 queue
->completed_serial
= anv_state_pool_alloc(queue
->pool
, 4, 4);
532 *(uint32_t *)queue
->completed_serial
.map
= 0;
533 queue
->next_serial
= 1;
535 *pQueue
= (VkQueue
) queue
;
541 anv_reloc_list_init(struct anv_reloc_list
*list
, struct anv_device
*device
)
543 list
->num_relocs
= 0;
544 list
->array_length
= 256;
546 anv_device_alloc(device
, list
->array_length
* sizeof(*list
->relocs
), 8,
547 VK_SYSTEM_ALLOC_TYPE_INTERNAL
);
549 if (list
->relocs
== NULL
)
550 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
553 anv_device_alloc(device
, list
->array_length
* sizeof(*list
->reloc_bos
), 8,
554 VK_SYSTEM_ALLOC_TYPE_INTERNAL
);
556 if (list
->relocs
== NULL
) {
557 anv_device_free(device
, list
->relocs
);
558 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
565 anv_reloc_list_finish(struct anv_reloc_list
*list
, struct anv_device
*device
)
567 anv_device_free(device
, list
->relocs
);
568 anv_device_free(device
, list
->reloc_bos
);
572 anv_reloc_list_grow(struct anv_reloc_list
*list
, struct anv_device
*device
,
573 size_t num_additional_relocs
)
575 if (list
->num_relocs
+ num_additional_relocs
<= list
->array_length
)
578 size_t new_length
= list
->array_length
* 2;
579 while (new_length
< list
->num_relocs
+ num_additional_relocs
)
582 struct drm_i915_gem_relocation_entry
*new_relocs
=
583 anv_device_alloc(device
, new_length
* sizeof(*list
->relocs
), 8,
584 VK_SYSTEM_ALLOC_TYPE_INTERNAL
);
585 if (new_relocs
== NULL
)
586 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
588 struct anv_bo
**new_reloc_bos
=
589 anv_device_alloc(device
, new_length
* sizeof(*list
->reloc_bos
), 8,
590 VK_SYSTEM_ALLOC_TYPE_INTERNAL
);
591 if (new_relocs
== NULL
) {
592 anv_device_free(device
, new_relocs
);
593 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
596 memcpy(new_relocs
, list
->relocs
, list
->num_relocs
* sizeof(*list
->relocs
));
597 memcpy(new_reloc_bos
, list
->reloc_bos
,
598 list
->num_relocs
* sizeof(*list
->reloc_bos
));
600 anv_device_free(device
, list
->relocs
);
601 anv_device_free(device
, list
->reloc_bos
);
603 list
->relocs
= new_relocs
;
604 list
->reloc_bos
= new_reloc_bos
;
610 anv_batch_bo_create(struct anv_device
*device
, struct anv_batch_bo
**bbo_out
)
614 struct anv_batch_bo
*bbo
=
615 anv_device_alloc(device
, sizeof(*bbo
), 8, VK_SYSTEM_ALLOC_TYPE_INTERNAL
);
617 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
620 bbo
->prev_batch_bo
= NULL
;
622 result
= anv_bo_pool_alloc(&device
->batch_bo_pool
, &bbo
->bo
);
623 if (result
!= VK_SUCCESS
) {
624 anv_device_free(device
, bbo
);
634 anv_batch_bo_start(struct anv_batch_bo
*bbo
, struct anv_batch
*batch
,
635 size_t batch_padding
)
637 batch
->next
= batch
->start
= bbo
->bo
.map
;
638 batch
->end
= bbo
->bo
.map
+ bbo
->bo
.size
- batch_padding
;
639 bbo
->first_reloc
= batch
->relocs
.num_relocs
;
643 anv_batch_bo_finish(struct anv_batch_bo
*bbo
, struct anv_batch
*batch
)
645 assert(batch
->start
== bbo
->bo
.map
);
646 bbo
->length
= batch
->next
- batch
->start
;
647 bbo
->num_relocs
= batch
->relocs
.num_relocs
- bbo
->first_reloc
;
651 anv_batch_bo_destroy(struct anv_batch_bo
*bbo
, struct anv_device
*device
)
653 anv_bo_pool_free(&device
->batch_bo_pool
, &bbo
->bo
);
654 anv_device_free(device
, bbo
);
658 anv_batch_emit_dwords(struct anv_batch
*batch
, int num_dwords
)
660 if (batch
->next
+ num_dwords
* 4 > batch
->end
)
661 batch
->extend_cb(batch
, batch
->user_data
);
663 void *p
= batch
->next
;
665 batch
->next
+= num_dwords
* 4;
666 assert(batch
->next
<= batch
->end
);
672 anv_reloc_list_append(struct anv_reloc_list
*list
, struct anv_device
*device
,
673 struct anv_reloc_list
*other
, uint32_t offset
)
675 anv_reloc_list_grow(list
, device
, other
->num_relocs
);
676 /* TODO: Handle failure */
678 memcpy(&list
->relocs
[list
->num_relocs
], &other
->relocs
[0],
679 other
->num_relocs
* sizeof(other
->relocs
[0]));
680 memcpy(&list
->reloc_bos
[list
->num_relocs
], &other
->reloc_bos
[0],
681 other
->num_relocs
* sizeof(other
->reloc_bos
[0]));
683 for (uint32_t i
= 0; i
< other
->num_relocs
; i
++)
684 list
->relocs
[i
+ list
->num_relocs
].offset
+= offset
;
686 list
->num_relocs
+= other
->num_relocs
;
690 anv_reloc_list_add(struct anv_reloc_list
*list
, struct anv_device
*device
,
691 uint32_t offset
, struct anv_bo
*target_bo
, uint32_t delta
)
693 struct drm_i915_gem_relocation_entry
*entry
;
696 anv_reloc_list_grow(list
, device
, 1);
697 /* TODO: Handle failure */
699 /* XXX: Can we use I915_EXEC_HANDLE_LUT? */
700 index
= list
->num_relocs
++;
701 list
->reloc_bos
[index
] = target_bo
;
702 entry
= &list
->relocs
[index
];
703 entry
->target_handle
= target_bo
->gem_handle
;
704 entry
->delta
= delta
;
705 entry
->offset
= offset
;
706 entry
->presumed_offset
= target_bo
->offset
;
707 entry
->read_domains
= 0;
708 entry
->write_domain
= 0;
710 return target_bo
->offset
+ delta
;
714 anv_batch_emit_batch(struct anv_batch
*batch
, struct anv_batch
*other
)
716 uint32_t size
, offset
;
718 size
= other
->next
- other
->start
;
719 assert(size
% 4 == 0);
721 if (batch
->next
+ size
> batch
->end
)
722 batch
->extend_cb(batch
, batch
->user_data
);
724 assert(batch
->next
+ size
<= batch
->end
);
726 memcpy(batch
->next
, other
->start
, size
);
728 offset
= batch
->next
- batch
->start
;
729 anv_reloc_list_append(&batch
->relocs
, batch
->device
,
730 &other
->relocs
, offset
);
736 anv_batch_emit_reloc(struct anv_batch
*batch
,
737 void *location
, struct anv_bo
*bo
, uint32_t delta
)
739 return anv_reloc_list_add(&batch
->relocs
, batch
->device
,
740 location
- batch
->start
, bo
, delta
);
743 VkResult
anv_QueueSubmit(
745 uint32_t cmdBufferCount
,
746 const VkCmdBuffer
* pCmdBuffers
,
749 struct anv_queue
*queue
= (struct anv_queue
*) _queue
;
750 struct anv_device
*device
= queue
->device
;
751 struct anv_fence
*fence
= (struct anv_fence
*) _fence
;
754 for (uint32_t i
= 0; i
< cmdBufferCount
; i
++) {
755 struct anv_cmd_buffer
*cmd_buffer
=
756 (struct anv_cmd_buffer
*) pCmdBuffers
[i
];
758 if (device
->dump_aub
)
759 anv_cmd_buffer_dump(cmd_buffer
);
761 if (!device
->no_hw
) {
762 ret
= anv_gem_execbuffer(device
, &cmd_buffer
->execbuf
);
764 return vk_error(VK_ERROR_UNKNOWN
);
767 ret
= anv_gem_execbuffer(device
, &fence
->execbuf
);
769 return vk_error(VK_ERROR_UNKNOWN
);
772 for (uint32_t i
= 0; i
< cmd_buffer
->bo_count
; i
++)
773 cmd_buffer
->exec2_bos
[i
]->offset
= cmd_buffer
->exec2_objects
[i
].offset
;
775 *(uint32_t *)queue
->completed_serial
.map
= cmd_buffer
->serial
;
782 VkResult
anv_QueueAddMemReferences(
785 const VkDeviceMemory
* pMems
)
790 VkResult
anv_QueueRemoveMemReferences(
793 const VkDeviceMemory
* pMems
)
798 VkResult
anv_QueueWaitIdle(
801 struct anv_queue
*queue
= (struct anv_queue
*) _queue
;
803 return vkDeviceWaitIdle((VkDevice
) queue
->device
);
806 VkResult
anv_DeviceWaitIdle(
809 struct anv_device
*device
= (struct anv_device
*) _device
;
810 struct anv_state state
;
811 struct anv_batch batch
;
812 struct drm_i915_gem_execbuffer2 execbuf
;
813 struct drm_i915_gem_exec_object2 exec2_objects
[1];
814 struct anv_bo
*bo
= NULL
;
819 state
= anv_state_pool_alloc(&device
->dynamic_state_pool
, 32, 32);
820 bo
= &device
->dynamic_state_pool
.block_pool
->bo
;
821 batch
.start
= batch
.next
= state
.map
;
822 batch
.end
= state
.map
+ 32;
823 anv_batch_emit(&batch
, GEN8_MI_BATCH_BUFFER_END
);
824 anv_batch_emit(&batch
, GEN8_MI_NOOP
);
826 exec2_objects
[0].handle
= bo
->gem_handle
;
827 exec2_objects
[0].relocation_count
= 0;
828 exec2_objects
[0].relocs_ptr
= 0;
829 exec2_objects
[0].alignment
= 0;
830 exec2_objects
[0].offset
= bo
->offset
;
831 exec2_objects
[0].flags
= 0;
832 exec2_objects
[0].rsvd1
= 0;
833 exec2_objects
[0].rsvd2
= 0;
835 execbuf
.buffers_ptr
= (uintptr_t) exec2_objects
;
836 execbuf
.buffer_count
= 1;
837 execbuf
.batch_start_offset
= state
.offset
;
838 execbuf
.batch_len
= batch
.next
- state
.map
;
839 execbuf
.cliprects_ptr
= 0;
840 execbuf
.num_cliprects
= 0;
845 I915_EXEC_HANDLE_LUT
| I915_EXEC_NO_RELOC
| I915_EXEC_RENDER
;
846 execbuf
.rsvd1
= device
->context_id
;
849 if (!device
->no_hw
) {
850 ret
= anv_gem_execbuffer(device
, &execbuf
);
852 result
= vk_error(VK_ERROR_UNKNOWN
);
857 ret
= anv_gem_wait(device
, bo
->gem_handle
, &timeout
);
859 result
= vk_error(VK_ERROR_UNKNOWN
);
864 anv_state_pool_free(&device
->dynamic_state_pool
, state
);
869 anv_state_pool_free(&device
->dynamic_state_pool
, state
);
875 anv_device_alloc(struct anv_device
* device
,
878 VkSystemAllocType allocType
)
880 return device
->instance
->pfnAlloc(device
->instance
->pAllocUserData
,
887 anv_device_free(struct anv_device
* device
,
890 return device
->instance
->pfnFree(device
->instance
->pAllocUserData
,
895 anv_bo_init_new(struct anv_bo
*bo
, struct anv_device
*device
, uint64_t size
)
897 bo
->gem_handle
= anv_gem_create(device
, size
);
899 return vk_error(VK_ERROR_OUT_OF_DEVICE_MEMORY
);
909 VkResult
anv_AllocMemory(
911 const VkMemoryAllocInfo
* pAllocInfo
,
912 VkDeviceMemory
* pMem
)
914 struct anv_device
*device
= (struct anv_device
*) _device
;
915 struct anv_device_memory
*mem
;
918 assert(pAllocInfo
->sType
== VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO
);
920 mem
= anv_device_alloc(device
, sizeof(*mem
), 8,
921 VK_SYSTEM_ALLOC_TYPE_API_OBJECT
);
923 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
925 result
= anv_bo_init_new(&mem
->bo
, device
, pAllocInfo
->allocationSize
);
926 if (result
!= VK_SUCCESS
)
929 *pMem
= (VkDeviceMemory
) mem
;
934 anv_device_free(device
, mem
);
939 VkResult
anv_FreeMemory(
943 struct anv_device
*device
= (struct anv_device
*) _device
;
944 struct anv_device_memory
*mem
= (struct anv_device_memory
*) _mem
;
947 anv_gem_munmap(mem
->bo
.map
, mem
->bo
.size
);
949 if (mem
->bo
.gem_handle
!= 0)
950 anv_gem_close(device
, mem
->bo
.gem_handle
);
952 anv_device_free(device
, mem
);
957 VkResult
anv_SetMemoryPriority(
960 VkMemoryPriority priority
)
965 VkResult
anv_MapMemory(
970 VkMemoryMapFlags flags
,
973 struct anv_device
*device
= (struct anv_device
*) _device
;
974 struct anv_device_memory
*mem
= (struct anv_device_memory
*) _mem
;
976 /* FIXME: Is this supposed to be thread safe? Since vkUnmapMemory() only
977 * takes a VkDeviceMemory pointer, it seems like only one map of the memory
978 * at a time is valid. We could just mmap up front and return an offset
979 * pointer here, but that may exhaust virtual memory on 32 bit
982 mem
->map
= anv_gem_mmap(device
, mem
->bo
.gem_handle
, offset
, size
);
983 mem
->map_size
= size
;
990 VkResult
anv_UnmapMemory(
994 struct anv_device_memory
*mem
= (struct anv_device_memory
*) _mem
;
996 anv_gem_munmap(mem
->map
, mem
->map_size
);
1001 VkResult
anv_FlushMappedMemory(
1004 VkDeviceSize offset
,
1007 /* clflush here for !llc platforms */
1012 VkResult
anv_PinSystemMemory(
1014 const void* pSysMem
,
1016 VkDeviceMemory
* pMem
)
1021 VkResult
anv_GetMultiDeviceCompatibility(
1022 VkPhysicalDevice physicalDevice0
,
1023 VkPhysicalDevice physicalDevice1
,
1024 VkPhysicalDeviceCompatibilityInfo
* pInfo
)
1026 return VK_UNSUPPORTED
;
1029 VkResult
anv_OpenSharedMemory(
1031 const VkMemoryOpenInfo
* pOpenInfo
,
1032 VkDeviceMemory
* pMem
)
1034 return VK_UNSUPPORTED
;
1037 VkResult
anv_OpenSharedSemaphore(
1039 const VkSemaphoreOpenInfo
* pOpenInfo
,
1040 VkSemaphore
* pSemaphore
)
1042 return VK_UNSUPPORTED
;
1045 VkResult
anv_OpenPeerMemory(
1047 const VkPeerMemoryOpenInfo
* pOpenInfo
,
1048 VkDeviceMemory
* pMem
)
1050 return VK_UNSUPPORTED
;
1053 VkResult
anv_OpenPeerImage(
1055 const VkPeerImageOpenInfo
* pOpenInfo
,
1057 VkDeviceMemory
* pMem
)
1059 return VK_UNSUPPORTED
;
1062 VkResult
anv_DestroyObject(
1064 VkObjectType objType
,
1067 struct anv_device
*device
= (struct anv_device
*) _device
;
1068 struct anv_object
*object
= (struct anv_object
*) _object
;
1071 case VK_OBJECT_TYPE_INSTANCE
:
1072 return anv_DestroyInstance((VkInstance
) _object
);
1074 case VK_OBJECT_TYPE_PHYSICAL_DEVICE
:
1075 /* We don't want to actually destroy physical devices */
1078 case VK_OBJECT_TYPE_DEVICE
:
1079 assert(_device
== (VkDevice
) _object
);
1080 return anv_DestroyDevice((VkDevice
) _object
);
1082 case VK_OBJECT_TYPE_QUEUE
:
1086 case VK_OBJECT_TYPE_DEVICE_MEMORY
:
1087 return anv_FreeMemory(_device
, (VkDeviceMemory
) _object
);
1089 case VK_OBJECT_TYPE_DESCRIPTOR_POOL
:
1090 /* These are just dummys anyway, so we don't need to destroy them */
1093 case VK_OBJECT_TYPE_BUFFER
:
1094 case VK_OBJECT_TYPE_IMAGE
:
1095 case VK_OBJECT_TYPE_DEPTH_STENCIL_VIEW
:
1096 case VK_OBJECT_TYPE_SHADER
:
1097 case VK_OBJECT_TYPE_PIPELINE_LAYOUT
:
1098 case VK_OBJECT_TYPE_SAMPLER
:
1099 case VK_OBJECT_TYPE_DESCRIPTOR_SET
:
1100 case VK_OBJECT_TYPE_DESCRIPTOR_SET_LAYOUT
:
1101 case VK_OBJECT_TYPE_DYNAMIC_RS_STATE
:
1102 case VK_OBJECT_TYPE_DYNAMIC_CB_STATE
:
1103 case VK_OBJECT_TYPE_DYNAMIC_DS_STATE
:
1104 case VK_OBJECT_TYPE_RENDER_PASS
:
1105 /* These are trivially destroyable */
1106 anv_device_free(device
, (void *) _object
);
1109 case VK_OBJECT_TYPE_COMMAND_BUFFER
:
1110 case VK_OBJECT_TYPE_PIPELINE
:
1111 case VK_OBJECT_TYPE_DYNAMIC_VP_STATE
:
1112 case VK_OBJECT_TYPE_FENCE
:
1113 case VK_OBJECT_TYPE_QUERY_POOL
:
1114 case VK_OBJECT_TYPE_FRAMEBUFFER
:
1115 case VK_OBJECT_TYPE_BUFFER_VIEW
:
1116 case VK_OBJECT_TYPE_IMAGE_VIEW
:
1117 case VK_OBJECT_TYPE_COLOR_ATTACHMENT_VIEW
:
1118 (object
->destructor
)(device
, object
, objType
);
1121 case VK_OBJECT_TYPE_SEMAPHORE
:
1122 case VK_OBJECT_TYPE_EVENT
:
1123 stub_return(VK_UNSUPPORTED
);
1126 unreachable("Invalid object type");
1131 fill_memory_requirements(
1132 VkObjectType objType
,
1134 VkMemoryRequirements
* memory_requirements
)
1136 struct anv_buffer
*buffer
;
1137 struct anv_image
*image
;
1139 memory_requirements
->memPropsAllowed
=
1140 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT
|
1141 VK_MEMORY_PROPERTY_HOST_DEVICE_COHERENT_BIT
|
1142 /* VK_MEMORY_PROPERTY_HOST_UNCACHED_BIT | */
1143 VK_MEMORY_PROPERTY_HOST_WRITE_COMBINED_BIT
|
1144 VK_MEMORY_PROPERTY_PREFER_HOST_LOCAL
|
1145 VK_MEMORY_PROPERTY_SHAREABLE_BIT
;
1147 memory_requirements
->memPropsRequired
= 0;
1150 case VK_OBJECT_TYPE_BUFFER
:
1151 buffer
= (struct anv_buffer
*) object
;
1152 memory_requirements
->size
= buffer
->size
;
1153 memory_requirements
->alignment
= 16;
1155 case VK_OBJECT_TYPE_IMAGE
:
1156 image
= (struct anv_image
*) object
;
1157 memory_requirements
->size
= image
->size
;
1158 memory_requirements
->alignment
= image
->alignment
;
1161 memory_requirements
->size
= 0;
1167 get_allocation_count(VkObjectType objType
)
1170 case VK_OBJECT_TYPE_BUFFER
:
1171 case VK_OBJECT_TYPE_IMAGE
:
1178 VkResult
anv_GetObjectInfo(
1180 VkObjectType objType
,
1182 VkObjectInfoType infoType
,
1186 VkMemoryRequirements memory_requirements
;
1190 case VK_OBJECT_INFO_TYPE_MEMORY_REQUIREMENTS
:
1191 *pDataSize
= sizeof(memory_requirements
);
1195 fill_memory_requirements(objType
, object
, pData
);
1198 case VK_OBJECT_INFO_TYPE_MEMORY_ALLOCATION_COUNT
:
1199 *pDataSize
= sizeof(count
);
1204 *count
= get_allocation_count(objType
);
1208 return VK_UNSUPPORTED
;
1213 VkResult
anv_QueueBindObjectMemory(
1215 VkObjectType objType
,
1217 uint32_t allocationIdx
,
1218 VkDeviceMemory _mem
,
1219 VkDeviceSize memOffset
)
1221 struct anv_buffer
*buffer
;
1222 struct anv_image
*image
;
1223 struct anv_device_memory
*mem
= (struct anv_device_memory
*) _mem
;
1226 case VK_OBJECT_TYPE_BUFFER
:
1227 buffer
= (struct anv_buffer
*) object
;
1228 buffer
->bo
= &mem
->bo
;
1229 buffer
->offset
= memOffset
;
1231 case VK_OBJECT_TYPE_IMAGE
:
1232 image
= (struct anv_image
*) object
;
1233 image
->bo
= &mem
->bo
;
1234 image
->offset
= memOffset
;
1243 VkResult
anv_QueueBindObjectMemoryRange(
1245 VkObjectType objType
,
1247 uint32_t allocationIdx
,
1248 VkDeviceSize rangeOffset
,
1249 VkDeviceSize rangeSize
,
1251 VkDeviceSize memOffset
)
1253 stub_return(VK_UNSUPPORTED
);
1256 VkResult
anv_QueueBindImageMemoryRange(
1259 uint32_t allocationIdx
,
1260 const VkImageMemoryBindInfo
* pBindInfo
,
1262 VkDeviceSize memOffset
)
1264 stub_return(VK_UNSUPPORTED
);
1268 anv_fence_destroy(struct anv_device
*device
,
1269 struct anv_object
*object
,
1270 VkObjectType obj_type
)
1272 struct anv_fence
*fence
= (struct anv_fence
*) object
;
1274 assert(obj_type
== VK_OBJECT_TYPE_FENCE
);
1276 anv_gem_munmap(fence
->bo
.map
, fence
->bo
.size
);
1277 anv_gem_close(device
, fence
->bo
.gem_handle
);
1278 anv_device_free(device
, fence
);
1281 VkResult
anv_CreateFence(
1283 const VkFenceCreateInfo
* pCreateInfo
,
1286 struct anv_device
*device
= (struct anv_device
*) _device
;
1287 struct anv_fence
*fence
;
1288 struct anv_batch batch
;
1291 const uint32_t fence_size
= 128;
1293 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_FENCE_CREATE_INFO
);
1295 fence
= anv_device_alloc(device
, sizeof(*fence
), 8,
1296 VK_SYSTEM_ALLOC_TYPE_API_OBJECT
);
1298 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
1300 result
= anv_bo_init_new(&fence
->bo
, device
, fence_size
);
1301 if (result
!= VK_SUCCESS
)
1304 fence
->base
.destructor
= anv_fence_destroy
;
1307 anv_gem_mmap(device
, fence
->bo
.gem_handle
, 0, fence
->bo
.size
);
1308 batch
.next
= batch
.start
= fence
->bo
.map
;
1309 batch
.end
= fence
->bo
.map
+ fence
->bo
.size
;
1310 anv_batch_emit(&batch
, GEN8_MI_BATCH_BUFFER_END
);
1311 anv_batch_emit(&batch
, GEN8_MI_NOOP
);
1313 fence
->exec2_objects
[0].handle
= fence
->bo
.gem_handle
;
1314 fence
->exec2_objects
[0].relocation_count
= 0;
1315 fence
->exec2_objects
[0].relocs_ptr
= 0;
1316 fence
->exec2_objects
[0].alignment
= 0;
1317 fence
->exec2_objects
[0].offset
= fence
->bo
.offset
;
1318 fence
->exec2_objects
[0].flags
= 0;
1319 fence
->exec2_objects
[0].rsvd1
= 0;
1320 fence
->exec2_objects
[0].rsvd2
= 0;
1322 fence
->execbuf
.buffers_ptr
= (uintptr_t) fence
->exec2_objects
;
1323 fence
->execbuf
.buffer_count
= 1;
1324 fence
->execbuf
.batch_start_offset
= 0;
1325 fence
->execbuf
.batch_len
= batch
.next
- fence
->bo
.map
;
1326 fence
->execbuf
.cliprects_ptr
= 0;
1327 fence
->execbuf
.num_cliprects
= 0;
1328 fence
->execbuf
.DR1
= 0;
1329 fence
->execbuf
.DR4
= 0;
1331 fence
->execbuf
.flags
=
1332 I915_EXEC_HANDLE_LUT
| I915_EXEC_NO_RELOC
| I915_EXEC_RENDER
;
1333 fence
->execbuf
.rsvd1
= device
->context_id
;
1334 fence
->execbuf
.rsvd2
= 0;
1336 *pFence
= (VkFence
) fence
;
1341 anv_device_free(device
, fence
);
1346 VkResult
anv_ResetFences(
1348 uint32_t fenceCount
,
1351 struct anv_fence
**fences
= (struct anv_fence
**) pFences
;
1353 for (uint32_t i
= 0; i
< fenceCount
; i
++)
1354 fences
[i
]->ready
= false;
1359 VkResult
anv_GetFenceStatus(
1363 struct anv_device
*device
= (struct anv_device
*) _device
;
1364 struct anv_fence
*fence
= (struct anv_fence
*) _fence
;
1371 ret
= anv_gem_wait(device
, fence
->bo
.gem_handle
, &t
);
1373 fence
->ready
= true;
1377 return VK_NOT_READY
;
1380 VkResult
anv_WaitForFences(
1382 uint32_t fenceCount
,
1383 const VkFence
* pFences
,
1387 struct anv_device
*device
= (struct anv_device
*) _device
;
1388 struct anv_fence
**fences
= (struct anv_fence
**) pFences
;
1389 int64_t t
= timeout
;
1392 /* FIXME: handle !waitAll */
1394 for (uint32_t i
= 0; i
< fenceCount
; i
++) {
1395 ret
= anv_gem_wait(device
, fences
[i
]->bo
.gem_handle
, &t
);
1396 if (ret
== -1 && errno
== ETIME
)
1399 return vk_error(VK_ERROR_UNKNOWN
);
1405 // Queue semaphore functions
1407 VkResult
anv_CreateSemaphore(
1409 const VkSemaphoreCreateInfo
* pCreateInfo
,
1410 VkSemaphore
* pSemaphore
)
1412 stub_return(VK_UNSUPPORTED
);
1415 VkResult
anv_QueueSignalSemaphore(
1417 VkSemaphore semaphore
)
1419 stub_return(VK_UNSUPPORTED
);
1422 VkResult
anv_QueueWaitSemaphore(
1424 VkSemaphore semaphore
)
1426 stub_return(VK_UNSUPPORTED
);
1431 VkResult
anv_CreateEvent(
1433 const VkEventCreateInfo
* pCreateInfo
,
1436 stub_return(VK_UNSUPPORTED
);
1439 VkResult
anv_GetEventStatus(
1443 stub_return(VK_UNSUPPORTED
);
1446 VkResult
anv_SetEvent(
1450 stub_return(VK_UNSUPPORTED
);
1453 VkResult
anv_ResetEvent(
1457 stub_return(VK_UNSUPPORTED
);
1462 VkResult
anv_CreateBuffer(
1464 const VkBufferCreateInfo
* pCreateInfo
,
1467 struct anv_device
*device
= (struct anv_device
*) _device
;
1468 struct anv_buffer
*buffer
;
1470 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO
);
1472 buffer
= anv_device_alloc(device
, sizeof(*buffer
), 8,
1473 VK_SYSTEM_ALLOC_TYPE_API_OBJECT
);
1475 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
1477 buffer
->size
= pCreateInfo
->size
;
1481 *pBuffer
= (VkBuffer
) buffer
;
1486 // Buffer view functions
1489 fill_buffer_surface_state(void *state
, VkFormat format
,
1490 uint32_t offset
, uint32_t range
)
1492 const struct anv_format
*info
;
1494 info
= anv_format_for_vk_format(format
);
1495 /* This assumes RGBA float format. */
1496 uint32_t stride
= 4;
1497 uint32_t num_elements
= range
/ stride
;
1499 struct GEN8_RENDER_SURFACE_STATE surface_state
= {
1500 .SurfaceType
= SURFTYPE_BUFFER
,
1501 .SurfaceArray
= false,
1502 .SurfaceFormat
= info
->format
,
1503 .SurfaceVerticalAlignment
= VALIGN4
,
1504 .SurfaceHorizontalAlignment
= HALIGN4
,
1506 .VerticalLineStride
= 0,
1507 .VerticalLineStrideOffset
= 0,
1508 .SamplerL2BypassModeDisable
= true,
1509 .RenderCacheReadWriteMode
= WriteOnlyCache
,
1510 .MemoryObjectControlState
= GEN8_MOCS
,
1513 .Height
= (num_elements
>> 7) & 0x3fff,
1514 .Width
= num_elements
& 0x7f,
1515 .Depth
= (num_elements
>> 21) & 0x3f,
1516 .SurfacePitch
= stride
- 1,
1517 .MinimumArrayElement
= 0,
1518 .NumberofMultisamples
= MULTISAMPLECOUNT_1
,
1523 .AuxiliarySurfaceMode
= AUX_NONE
,
1525 .GreenClearColor
= 0,
1526 .BlueClearColor
= 0,
1527 .AlphaClearColor
= 0,
1528 .ShaderChannelSelectRed
= SCS_RED
,
1529 .ShaderChannelSelectGreen
= SCS_GREEN
,
1530 .ShaderChannelSelectBlue
= SCS_BLUE
,
1531 .ShaderChannelSelectAlpha
= SCS_ALPHA
,
1532 .ResourceMinLOD
= 0,
1533 /* FIXME: We assume that the image must be bound at this time. */
1534 .SurfaceBaseAddress
= { NULL
, offset
},
1537 GEN8_RENDER_SURFACE_STATE_pack(NULL
, state
, &surface_state
);
1540 VkResult
anv_CreateBufferView(
1542 const VkBufferViewCreateInfo
* pCreateInfo
,
1543 VkBufferView
* pView
)
1545 struct anv_device
*device
= (struct anv_device
*) _device
;
1546 struct anv_buffer
*buffer
= (struct anv_buffer
*) pCreateInfo
->buffer
;
1547 struct anv_surface_view
*view
;
1549 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO
);
1551 view
= anv_device_alloc(device
, sizeof(*view
), 8,
1552 VK_SYSTEM_ALLOC_TYPE_API_OBJECT
);
1554 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
1556 view
->base
.destructor
= anv_surface_view_destroy
;
1558 view
->bo
= buffer
->bo
;
1559 view
->offset
= buffer
->offset
+ pCreateInfo
->offset
;
1560 view
->surface_state
=
1561 anv_state_pool_alloc(&device
->surface_state_pool
, 64, 64);
1562 view
->format
= pCreateInfo
->format
;
1563 view
->range
= pCreateInfo
->range
;
1565 fill_buffer_surface_state(view
->surface_state
.map
,
1566 pCreateInfo
->format
, view
->offset
, pCreateInfo
->range
);
1568 *pView
= (VkBufferView
) view
;
1573 // Sampler functions
1575 VkResult
anv_CreateSampler(
1577 const VkSamplerCreateInfo
* pCreateInfo
,
1578 VkSampler
* pSampler
)
1580 struct anv_device
*device
= (struct anv_device
*) _device
;
1581 struct anv_sampler
*sampler
;
1582 uint32_t mag_filter
, min_filter
, max_anisotropy
;
1584 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO
);
1586 sampler
= anv_device_alloc(device
, sizeof(*sampler
), 8,
1587 VK_SYSTEM_ALLOC_TYPE_API_OBJECT
);
1589 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
1591 static const uint32_t vk_to_gen_tex_filter
[] = {
1592 [VK_TEX_FILTER_NEAREST
] = MAPFILTER_NEAREST
,
1593 [VK_TEX_FILTER_LINEAR
] = MAPFILTER_LINEAR
1596 static const uint32_t vk_to_gen_mipmap_mode
[] = {
1597 [VK_TEX_MIPMAP_MODE_BASE
] = MIPFILTER_NONE
,
1598 [VK_TEX_MIPMAP_MODE_NEAREST
] = MIPFILTER_NEAREST
,
1599 [VK_TEX_MIPMAP_MODE_LINEAR
] = MIPFILTER_LINEAR
1602 static const uint32_t vk_to_gen_tex_address
[] = {
1603 [VK_TEX_ADDRESS_WRAP
] = TCM_WRAP
,
1604 [VK_TEX_ADDRESS_MIRROR
] = TCM_MIRROR
,
1605 [VK_TEX_ADDRESS_CLAMP
] = TCM_CLAMP
,
1606 [VK_TEX_ADDRESS_MIRROR_ONCE
] = TCM_MIRROR_ONCE
,
1607 [VK_TEX_ADDRESS_CLAMP_BORDER
] = TCM_CLAMP_BORDER
,
1610 static const uint32_t vk_to_gen_compare_op
[] = {
1611 [VK_COMPARE_OP_NEVER
] = PREFILTEROPNEVER
,
1612 [VK_COMPARE_OP_LESS
] = PREFILTEROPLESS
,
1613 [VK_COMPARE_OP_EQUAL
] = PREFILTEROPEQUAL
,
1614 [VK_COMPARE_OP_LESS_EQUAL
] = PREFILTEROPLEQUAL
,
1615 [VK_COMPARE_OP_GREATER
] = PREFILTEROPGREATER
,
1616 [VK_COMPARE_OP_NOT_EQUAL
] = PREFILTEROPNOTEQUAL
,
1617 [VK_COMPARE_OP_GREATER_EQUAL
] = PREFILTEROPGEQUAL
,
1618 [VK_COMPARE_OP_ALWAYS
] = PREFILTEROPALWAYS
,
1621 if (pCreateInfo
->maxAnisotropy
> 1) {
1622 mag_filter
= MAPFILTER_ANISOTROPIC
;
1623 min_filter
= MAPFILTER_ANISOTROPIC
;
1624 max_anisotropy
= (pCreateInfo
->maxAnisotropy
- 2) / 2;
1626 mag_filter
= vk_to_gen_tex_filter
[pCreateInfo
->magFilter
];
1627 min_filter
= vk_to_gen_tex_filter
[pCreateInfo
->minFilter
];
1628 max_anisotropy
= RATIO21
;
1631 struct GEN8_SAMPLER_STATE sampler_state
= {
1632 .SamplerDisable
= false,
1633 .TextureBorderColorMode
= DX10OGL
,
1634 .LODPreClampMode
= 0,
1636 .MipModeFilter
= vk_to_gen_mipmap_mode
[pCreateInfo
->mipMode
],
1637 .MagModeFilter
= mag_filter
,
1638 .MinModeFilter
= min_filter
,
1639 .TextureLODBias
= pCreateInfo
->mipLodBias
* 256,
1640 .AnisotropicAlgorithm
= EWAApproximation
,
1641 .MinLOD
= pCreateInfo
->minLod
* 256,
1642 .MaxLOD
= pCreateInfo
->maxLod
* 256,
1643 .ChromaKeyEnable
= 0,
1644 .ChromaKeyIndex
= 0,
1646 .ShadowFunction
= vk_to_gen_compare_op
[pCreateInfo
->compareOp
],
1647 .CubeSurfaceControlMode
= 0,
1649 .IndirectStatePointer
=
1650 device
->float_border_colors
.offset
+
1651 pCreateInfo
->borderColor
* sizeof(float) * 4,
1653 .LODClampMagnificationMode
= MIPNONE
,
1654 .MaximumAnisotropy
= max_anisotropy
,
1655 .RAddressMinFilterRoundingEnable
= 0,
1656 .RAddressMagFilterRoundingEnable
= 0,
1657 .VAddressMinFilterRoundingEnable
= 0,
1658 .VAddressMagFilterRoundingEnable
= 0,
1659 .UAddressMinFilterRoundingEnable
= 0,
1660 .UAddressMagFilterRoundingEnable
= 0,
1661 .TrilinearFilterQuality
= 0,
1662 .NonnormalizedCoordinateEnable
= 0,
1663 .TCXAddressControlMode
= vk_to_gen_tex_address
[pCreateInfo
->addressU
],
1664 .TCYAddressControlMode
= vk_to_gen_tex_address
[pCreateInfo
->addressV
],
1665 .TCZAddressControlMode
= vk_to_gen_tex_address
[pCreateInfo
->addressW
],
1668 GEN8_SAMPLER_STATE_pack(NULL
, sampler
->state
, &sampler_state
);
1670 *pSampler
= (VkSampler
) sampler
;
1675 // Descriptor set functions
1677 VkResult
anv_CreateDescriptorSetLayout(
1679 const VkDescriptorSetLayoutCreateInfo
* pCreateInfo
,
1680 VkDescriptorSetLayout
* pSetLayout
)
1682 struct anv_device
*device
= (struct anv_device
*) _device
;
1683 struct anv_descriptor_set_layout
*set_layout
;
1685 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO
);
1687 uint32_t sampler_count
[VK_NUM_SHADER_STAGE
] = { 0, };
1688 uint32_t surface_count
[VK_NUM_SHADER_STAGE
] = { 0, };
1689 uint32_t num_dynamic_buffers
= 0;
1691 uint32_t stages
= 0;
1694 for (uint32_t i
= 0; i
< pCreateInfo
->count
; i
++) {
1695 switch (pCreateInfo
->pBinding
[i
].descriptorType
) {
1696 case VK_DESCRIPTOR_TYPE_SAMPLER
:
1697 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
:
1698 for_each_bit(s
, pCreateInfo
->pBinding
[i
].stageFlags
)
1699 sampler_count
[s
] += pCreateInfo
->pBinding
[i
].count
;
1705 switch (pCreateInfo
->pBinding
[i
].descriptorType
) {
1706 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
:
1707 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE
:
1708 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE
:
1709 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER
:
1710 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER
:
1711 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER
:
1712 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER
:
1713 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
:
1714 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
:
1715 for_each_bit(s
, pCreateInfo
->pBinding
[i
].stageFlags
)
1716 surface_count
[s
] += pCreateInfo
->pBinding
[i
].count
;
1722 switch (pCreateInfo
->pBinding
[i
].descriptorType
) {
1723 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
:
1724 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
:
1725 num_dynamic_buffers
+= pCreateInfo
->pBinding
[i
].count
;
1731 stages
|= pCreateInfo
->pBinding
[i
].stageFlags
;
1732 count
+= pCreateInfo
->pBinding
[i
].count
;
1735 uint32_t sampler_total
= 0;
1736 uint32_t surface_total
= 0;
1737 for (uint32_t s
= 0; s
< VK_NUM_SHADER_STAGE
; s
++) {
1738 sampler_total
+= sampler_count
[s
];
1739 surface_total
+= surface_count
[s
];
1742 size_t size
= sizeof(*set_layout
) +
1743 (sampler_total
+ surface_total
) * sizeof(set_layout
->entries
[0]);
1744 set_layout
= anv_device_alloc(device
, size
, 8,
1745 VK_SYSTEM_ALLOC_TYPE_API_OBJECT
);
1747 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
1749 set_layout
->num_dynamic_buffers
= num_dynamic_buffers
;
1750 set_layout
->count
= count
;
1751 set_layout
->shader_stages
= stages
;
1753 struct anv_descriptor_slot
*p
= set_layout
->entries
;
1754 struct anv_descriptor_slot
*sampler
[VK_NUM_SHADER_STAGE
];
1755 struct anv_descriptor_slot
*surface
[VK_NUM_SHADER_STAGE
];
1756 for (uint32_t s
= 0; s
< VK_NUM_SHADER_STAGE
; s
++) {
1757 set_layout
->stage
[s
].surface_count
= surface_count
[s
];
1758 set_layout
->stage
[s
].surface_start
= surface
[s
] = p
;
1759 p
+= surface_count
[s
];
1760 set_layout
->stage
[s
].sampler_count
= sampler_count
[s
];
1761 set_layout
->stage
[s
].sampler_start
= sampler
[s
] = p
;
1762 p
+= sampler_count
[s
];
1765 uint32_t descriptor
= 0;
1766 int8_t dynamic_slot
= 0;
1768 for (uint32_t i
= 0; i
< pCreateInfo
->count
; i
++) {
1769 switch (pCreateInfo
->pBinding
[i
].descriptorType
) {
1770 case VK_DESCRIPTOR_TYPE_SAMPLER
:
1771 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
:
1772 for_each_bit(s
, pCreateInfo
->pBinding
[i
].stageFlags
)
1773 for (uint32_t j
= 0; j
< pCreateInfo
->pBinding
[i
].count
; j
++) {
1774 sampler
[s
]->index
= descriptor
+ j
;
1775 sampler
[s
]->dynamic_slot
= -1;
1783 switch (pCreateInfo
->pBinding
[i
].descriptorType
) {
1784 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
:
1785 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
:
1793 switch (pCreateInfo
->pBinding
[i
].descriptorType
) {
1794 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
:
1795 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE
:
1796 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE
:
1797 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER
:
1798 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER
:
1799 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER
:
1800 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER
:
1801 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
:
1802 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
:
1803 for_each_bit(s
, pCreateInfo
->pBinding
[i
].stageFlags
)
1804 for (uint32_t j
= 0; j
< pCreateInfo
->pBinding
[i
].count
; j
++) {
1805 surface
[s
]->index
= descriptor
+ j
;
1807 surface
[s
]->dynamic_slot
= dynamic_slot
+ j
;
1809 surface
[s
]->dynamic_slot
= -1;
1818 dynamic_slot
+= pCreateInfo
->pBinding
[i
].count
;
1820 descriptor
+= pCreateInfo
->pBinding
[i
].count
;
1823 *pSetLayout
= (VkDescriptorSetLayout
) set_layout
;
1828 VkResult
anv_BeginDescriptorPoolUpdate(
1830 VkDescriptorUpdateMode updateMode
)
1835 VkResult
anv_EndDescriptorPoolUpdate(
1842 VkResult
anv_CreateDescriptorPool(
1844 VkDescriptorPoolUsage poolUsage
,
1846 const VkDescriptorPoolCreateInfo
* pCreateInfo
,
1847 VkDescriptorPool
* pDescriptorPool
)
1849 *pDescriptorPool
= 1;
1854 VkResult
anv_ResetDescriptorPool(
1856 VkDescriptorPool descriptorPool
)
1861 VkResult
anv_AllocDescriptorSets(
1863 VkDescriptorPool descriptorPool
,
1864 VkDescriptorSetUsage setUsage
,
1866 const VkDescriptorSetLayout
* pSetLayouts
,
1867 VkDescriptorSet
* pDescriptorSets
,
1870 struct anv_device
*device
= (struct anv_device
*) _device
;
1871 const struct anv_descriptor_set_layout
*layout
;
1872 struct anv_descriptor_set
*set
;
1875 for (uint32_t i
= 0; i
< count
; i
++) {
1876 layout
= (struct anv_descriptor_set_layout
*) pSetLayouts
[i
];
1877 size
= sizeof(*set
) + layout
->count
* sizeof(set
->descriptors
[0]);
1878 set
= anv_device_alloc(device
, size
, 8,
1879 VK_SYSTEM_ALLOC_TYPE_API_OBJECT
);
1882 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
1885 /* Descriptor sets may not be 100% filled out so we need to memset to
1886 * ensure that we can properly detect and handle holes.
1888 memset(set
, 0, size
);
1890 pDescriptorSets
[i
] = (VkDescriptorSet
) set
;
1898 void anv_ClearDescriptorSets(
1900 VkDescriptorPool descriptorPool
,
1902 const VkDescriptorSet
* pDescriptorSets
)
1906 void anv_UpdateDescriptors(
1908 VkDescriptorSet descriptorSet
,
1909 uint32_t updateCount
,
1910 const void** ppUpdateArray
)
1912 struct anv_descriptor_set
*set
= (struct anv_descriptor_set
*) descriptorSet
;
1913 VkUpdateSamplers
*update_samplers
;
1914 VkUpdateSamplerTextures
*update_sampler_textures
;
1915 VkUpdateImages
*update_images
;
1916 VkUpdateBuffers
*update_buffers
;
1917 VkUpdateAsCopy
*update_as_copy
;
1919 for (uint32_t i
= 0; i
< updateCount
; i
++) {
1920 const struct anv_common
*common
= ppUpdateArray
[i
];
1922 switch (common
->sType
) {
1923 case VK_STRUCTURE_TYPE_UPDATE_SAMPLERS
:
1924 update_samplers
= (VkUpdateSamplers
*) common
;
1926 for (uint32_t j
= 0; j
< update_samplers
->count
; j
++) {
1927 set
->descriptors
[update_samplers
->binding
+ j
].sampler
=
1928 (struct anv_sampler
*) update_samplers
->pSamplers
[j
];
1932 case VK_STRUCTURE_TYPE_UPDATE_SAMPLER_TEXTURES
:
1933 /* FIXME: Shouldn't this be *_UPDATE_SAMPLER_IMAGES? */
1934 update_sampler_textures
= (VkUpdateSamplerTextures
*) common
;
1936 for (uint32_t j
= 0; j
< update_sampler_textures
->count
; j
++) {
1937 set
->descriptors
[update_sampler_textures
->binding
+ j
].view
=
1938 (struct anv_surface_view
*)
1939 update_sampler_textures
->pSamplerImageViews
[j
].pImageView
->view
;
1940 set
->descriptors
[update_sampler_textures
->binding
+ j
].sampler
=
1941 (struct anv_sampler
*)
1942 update_sampler_textures
->pSamplerImageViews
[j
].sampler
;
1946 case VK_STRUCTURE_TYPE_UPDATE_IMAGES
:
1947 update_images
= (VkUpdateImages
*) common
;
1949 for (uint32_t j
= 0; j
< update_images
->count
; j
++) {
1950 set
->descriptors
[update_images
->binding
+ j
].view
=
1951 (struct anv_surface_view
*) update_images
->pImageViews
[j
].view
;
1955 case VK_STRUCTURE_TYPE_UPDATE_BUFFERS
:
1956 update_buffers
= (VkUpdateBuffers
*) common
;
1958 for (uint32_t j
= 0; j
< update_buffers
->count
; j
++) {
1959 set
->descriptors
[update_buffers
->binding
+ j
].view
=
1960 (struct anv_surface_view
*) update_buffers
->pBufferViews
[j
].view
;
1962 /* FIXME: descriptor arrays? */
1965 case VK_STRUCTURE_TYPE_UPDATE_AS_COPY
:
1966 update_as_copy
= (VkUpdateAsCopy
*) common
;
1967 (void) update_as_copy
;
1976 // State object functions
1978 static inline int64_t
1979 clamp_int64(int64_t x
, int64_t min
, int64_t max
)
1990 anv_dynamic_vp_state_destroy(struct anv_device
*device
,
1991 struct anv_object
*object
,
1992 VkObjectType obj_type
)
1994 struct anv_dynamic_vp_state
*state
= (void *)object
;
1996 assert(obj_type
== VK_OBJECT_TYPE_DYNAMIC_VP_STATE
);
1998 anv_state_pool_free(&device
->dynamic_state_pool
, state
->sf_clip_vp
);
1999 anv_state_pool_free(&device
->dynamic_state_pool
, state
->cc_vp
);
2000 anv_state_pool_free(&device
->dynamic_state_pool
, state
->scissor
);
2002 anv_device_free(device
, state
);
2005 VkResult
anv_CreateDynamicViewportState(
2007 const VkDynamicVpStateCreateInfo
* pCreateInfo
,
2008 VkDynamicVpState
* pState
)
2010 struct anv_device
*device
= (struct anv_device
*) _device
;
2011 struct anv_dynamic_vp_state
*state
;
2013 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_DYNAMIC_VP_STATE_CREATE_INFO
);
2015 state
= anv_device_alloc(device
, sizeof(*state
), 8,
2016 VK_SYSTEM_ALLOC_TYPE_API_OBJECT
);
2018 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
2020 state
->base
.destructor
= anv_dynamic_vp_state_destroy
;
2022 unsigned count
= pCreateInfo
->viewportAndScissorCount
;
2023 state
->sf_clip_vp
= anv_state_pool_alloc(&device
->dynamic_state_pool
,
2025 state
->cc_vp
= anv_state_pool_alloc(&device
->dynamic_state_pool
,
2027 state
->scissor
= anv_state_pool_alloc(&device
->dynamic_state_pool
,
2030 for (uint32_t i
= 0; i
< pCreateInfo
->viewportAndScissorCount
; i
++) {
2031 const VkViewport
*vp
= &pCreateInfo
->pViewports
[i
];
2032 const VkRect
*s
= &pCreateInfo
->pScissors
[i
];
2034 struct GEN8_SF_CLIP_VIEWPORT sf_clip_viewport
= {
2035 .ViewportMatrixElementm00
= vp
->width
/ 2,
2036 .ViewportMatrixElementm11
= vp
->height
/ 2,
2037 .ViewportMatrixElementm22
= (vp
->maxDepth
- vp
->minDepth
) / 2,
2038 .ViewportMatrixElementm30
= vp
->originX
+ vp
->width
/ 2,
2039 .ViewportMatrixElementm31
= vp
->originY
+ vp
->height
/ 2,
2040 .ViewportMatrixElementm32
= (vp
->maxDepth
+ vp
->minDepth
) / 2,
2041 .XMinClipGuardband
= -1.0f
,
2042 .XMaxClipGuardband
= 1.0f
,
2043 .YMinClipGuardband
= -1.0f
,
2044 .YMaxClipGuardband
= 1.0f
,
2045 .XMinViewPort
= vp
->originX
,
2046 .XMaxViewPort
= vp
->originX
+ vp
->width
- 1,
2047 .YMinViewPort
= vp
->originY
,
2048 .YMaxViewPort
= vp
->originY
+ vp
->height
- 1,
2051 struct GEN8_CC_VIEWPORT cc_viewport
= {
2052 .MinimumDepth
= vp
->minDepth
,
2053 .MaximumDepth
= vp
->maxDepth
2056 /* Since xmax and ymax are inclusive, we have to have xmax < xmin or
2057 * ymax < ymin for empty clips. In case clip x, y, width height are all
2058 * 0, the clamps below produce 0 for xmin, ymin, xmax, ymax, which isn't
2059 * what we want. Just special case empty clips and produce a canonical
2061 static const struct GEN8_SCISSOR_RECT empty_scissor
= {
2062 .ScissorRectangleYMin
= 1,
2063 .ScissorRectangleXMin
= 1,
2064 .ScissorRectangleYMax
= 0,
2065 .ScissorRectangleXMax
= 0
2068 const int max
= 0xffff;
2069 struct GEN8_SCISSOR_RECT scissor
= {
2070 /* Do this math using int64_t so overflow gets clamped correctly. */
2071 .ScissorRectangleYMin
= clamp_int64(s
->offset
.y
, 0, max
),
2072 .ScissorRectangleXMin
= clamp_int64(s
->offset
.x
, 0, max
),
2073 .ScissorRectangleYMax
= clamp_int64((uint64_t) s
->offset
.y
+ s
->extent
.height
- 1, 0, max
),
2074 .ScissorRectangleXMax
= clamp_int64((uint64_t) s
->offset
.x
+ s
->extent
.width
- 1, 0, max
)
2077 GEN8_SF_CLIP_VIEWPORT_pack(NULL
, state
->sf_clip_vp
.map
+ i
* 64, &sf_clip_viewport
);
2078 GEN8_CC_VIEWPORT_pack(NULL
, state
->cc_vp
.map
+ i
* 32, &cc_viewport
);
2080 if (s
->extent
.width
<= 0 || s
->extent
.height
<= 0) {
2081 GEN8_SCISSOR_RECT_pack(NULL
, state
->scissor
.map
+ i
* 32, &empty_scissor
);
2083 GEN8_SCISSOR_RECT_pack(NULL
, state
->scissor
.map
+ i
* 32, &scissor
);
2087 *pState
= (VkDynamicVpState
) state
;
2092 VkResult
anv_CreateDynamicRasterState(
2094 const VkDynamicRsStateCreateInfo
* pCreateInfo
,
2095 VkDynamicRsState
* pState
)
2097 struct anv_device
*device
= (struct anv_device
*) _device
;
2098 struct anv_dynamic_rs_state
*state
;
2100 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_DYNAMIC_RS_STATE_CREATE_INFO
);
2102 state
= anv_device_alloc(device
, sizeof(*state
), 8,
2103 VK_SYSTEM_ALLOC_TYPE_API_OBJECT
);
2105 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
2108 * float pointFadeThreshold;
2109 * // optional (GL45) - Size of point fade threshold
2112 struct GEN8_3DSTATE_SF sf
= {
2113 GEN8_3DSTATE_SF_header
,
2114 .LineWidth
= pCreateInfo
->lineWidth
,
2115 .PointWidth
= pCreateInfo
->pointSize
,
2118 GEN8_3DSTATE_SF_pack(NULL
, state
->state_sf
, &sf
);
2120 bool enable_bias
= pCreateInfo
->depthBias
!= 0.0f
||
2121 pCreateInfo
->slopeScaledDepthBias
!= 0.0f
;
2122 struct GEN8_3DSTATE_RASTER raster
= {
2123 .GlobalDepthOffsetEnableSolid
= enable_bias
,
2124 .GlobalDepthOffsetEnableWireframe
= enable_bias
,
2125 .GlobalDepthOffsetEnablePoint
= enable_bias
,
2126 .GlobalDepthOffsetConstant
= pCreateInfo
->depthBias
,
2127 .GlobalDepthOffsetScale
= pCreateInfo
->slopeScaledDepthBias
,
2128 .GlobalDepthOffsetClamp
= pCreateInfo
->depthBiasClamp
2131 GEN8_3DSTATE_RASTER_pack(NULL
, state
->state_raster
, &raster
);
2133 *pState
= (VkDynamicRsState
) state
;
2138 VkResult
anv_CreateDynamicColorBlendState(
2140 const VkDynamicCbStateCreateInfo
* pCreateInfo
,
2141 VkDynamicCbState
* pState
)
2143 struct anv_device
*device
= (struct anv_device
*) _device
;
2144 struct anv_dynamic_cb_state
*state
;
2146 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_DYNAMIC_CB_STATE_CREATE_INFO
);
2148 state
= anv_device_alloc(device
, sizeof(*state
), 8,
2149 VK_SYSTEM_ALLOC_TYPE_API_OBJECT
);
2151 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
2153 struct GEN8_COLOR_CALC_STATE color_calc_state
= {
2154 .BlendConstantColorRed
= pCreateInfo
->blendConst
[0],
2155 .BlendConstantColorGreen
= pCreateInfo
->blendConst
[1],
2156 .BlendConstantColorBlue
= pCreateInfo
->blendConst
[2],
2157 .BlendConstantColorAlpha
= pCreateInfo
->blendConst
[3]
2160 GEN8_COLOR_CALC_STATE_pack(NULL
, state
->state_color_calc
, &color_calc_state
);
2162 *pState
= (VkDynamicCbState
) state
;
2167 VkResult
anv_CreateDynamicDepthStencilState(
2169 const VkDynamicDsStateCreateInfo
* pCreateInfo
,
2170 VkDynamicDsState
* pState
)
2172 struct anv_device
*device
= (struct anv_device
*) _device
;
2173 struct anv_dynamic_ds_state
*state
;
2175 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_DYNAMIC_DS_STATE_CREATE_INFO
);
2177 state
= anv_device_alloc(device
, sizeof(*state
), 8,
2178 VK_SYSTEM_ALLOC_TYPE_API_OBJECT
);
2180 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
2182 struct GEN8_3DSTATE_WM_DEPTH_STENCIL wm_depth_stencil
= {
2183 GEN8_3DSTATE_WM_DEPTH_STENCIL_header
,
2185 /* Is this what we need to do? */
2186 .StencilBufferWriteEnable
= pCreateInfo
->stencilWriteMask
!= 0,
2188 .StencilTestMask
= pCreateInfo
->stencilReadMask
& 0xff,
2189 .StencilWriteMask
= pCreateInfo
->stencilWriteMask
& 0xff,
2191 .BackfaceStencilTestMask
= pCreateInfo
->stencilReadMask
& 0xff,
2192 .BackfaceStencilWriteMask
= pCreateInfo
->stencilWriteMask
& 0xff,
2195 GEN8_3DSTATE_WM_DEPTH_STENCIL_pack(NULL
, state
->state_wm_depth_stencil
,
2198 struct GEN8_COLOR_CALC_STATE color_calc_state
= {
2199 .StencilReferenceValue
= pCreateInfo
->stencilFrontRef
,
2200 .BackFaceStencilReferenceValue
= pCreateInfo
->stencilBackRef
2203 GEN8_COLOR_CALC_STATE_pack(NULL
, state
->state_color_calc
, &color_calc_state
);
2205 *pState
= (VkDynamicDsState
) state
;
2210 // Command buffer functions
2213 anv_cmd_buffer_destroy(struct anv_device
*device
,
2214 struct anv_object
*object
,
2215 VkObjectType obj_type
)
2217 struct anv_cmd_buffer
*cmd_buffer
= (struct anv_cmd_buffer
*) object
;
2219 assert(obj_type
== VK_OBJECT_TYPE_COMMAND_BUFFER
);
2221 /* Destroy all of the batch buffers */
2222 struct anv_batch_bo
*bbo
= cmd_buffer
->last_batch_bo
;
2224 struct anv_batch_bo
*prev
= bbo
->prev_batch_bo
;
2225 anv_batch_bo_destroy(bbo
, device
);
2228 anv_reloc_list_finish(&cmd_buffer
->batch
.relocs
, device
);
2230 /* Destroy all of the surface state buffers */
2231 bbo
= cmd_buffer
->surface_batch_bo
;
2233 struct anv_batch_bo
*prev
= bbo
->prev_batch_bo
;
2234 anv_batch_bo_destroy(bbo
, device
);
2237 anv_reloc_list_finish(&cmd_buffer
->surface_relocs
, device
);
2239 anv_state_stream_finish(&cmd_buffer
->surface_state_stream
);
2240 anv_state_stream_finish(&cmd_buffer
->dynamic_state_stream
);
2241 anv_device_free(device
, cmd_buffer
->exec2_objects
);
2242 anv_device_free(device
, cmd_buffer
->exec2_bos
);
2243 anv_device_free(device
, cmd_buffer
);
2247 anv_cmd_buffer_chain_batch(struct anv_batch
*batch
, void *_data
)
2249 struct anv_cmd_buffer
*cmd_buffer
= _data
;
2251 struct anv_batch_bo
*new_bbo
, *old_bbo
= cmd_buffer
->last_batch_bo
;
2253 VkResult result
= anv_batch_bo_create(cmd_buffer
->device
, &new_bbo
);
2254 if (result
!= VK_SUCCESS
)
2257 /* We set the end of the batch a little short so we would be sure we
2258 * have room for the chaining command. Since we're about to emit the
2259 * chaining command, let's set it back where it should go.
2261 batch
->end
+= GEN8_MI_BATCH_BUFFER_START_length
* 4;
2262 assert(batch
->end
== old_bbo
->bo
.map
+ old_bbo
->bo
.size
);
2264 anv_batch_emit(batch
, GEN8_MI_BATCH_BUFFER_START
,
2265 GEN8_MI_BATCH_BUFFER_START_header
,
2266 ._2ndLevelBatchBuffer
= _1stlevelbatch
,
2267 .AddressSpaceIndicator
= ASI_PPGTT
,
2268 .BatchBufferStartAddress
= { &new_bbo
->bo
, 0 },
2271 /* Pad out to a 2-dword aligned boundary with zeros */
2272 if ((uintptr_t)batch
->next
% 8 != 0) {
2273 *(uint32_t *)batch
->next
= 0;
2277 anv_batch_bo_finish(cmd_buffer
->last_batch_bo
, batch
);
2279 new_bbo
->prev_batch_bo
= old_bbo
;
2280 cmd_buffer
->last_batch_bo
= new_bbo
;
2282 anv_batch_bo_start(new_bbo
, batch
, GEN8_MI_BATCH_BUFFER_START_length
* 4);
2287 VkResult
anv_CreateCommandBuffer(
2289 const VkCmdBufferCreateInfo
* pCreateInfo
,
2290 VkCmdBuffer
* pCmdBuffer
)
2292 struct anv_device
*device
= (struct anv_device
*) _device
;
2293 struct anv_cmd_buffer
*cmd_buffer
;
2296 cmd_buffer
= anv_device_alloc(device
, sizeof(*cmd_buffer
), 8,
2297 VK_SYSTEM_ALLOC_TYPE_API_OBJECT
);
2298 if (cmd_buffer
== NULL
)
2299 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
2301 cmd_buffer
->base
.destructor
= anv_cmd_buffer_destroy
;
2303 cmd_buffer
->device
= device
;
2304 cmd_buffer
->rs_state
= NULL
;
2305 cmd_buffer
->vp_state
= NULL
;
2306 cmd_buffer
->cb_state
= NULL
;
2307 memset(&cmd_buffer
->descriptors
, 0, sizeof(cmd_buffer
->descriptors
));
2309 result
= anv_batch_bo_create(device
, &cmd_buffer
->last_batch_bo
);
2310 if (result
!= VK_SUCCESS
)
2313 result
= anv_reloc_list_init(&cmd_buffer
->batch
.relocs
, device
);
2314 if (result
!= VK_SUCCESS
)
2317 cmd_buffer
->batch
.device
= device
;
2318 cmd_buffer
->batch
.extend_cb
= anv_cmd_buffer_chain_batch
;
2319 cmd_buffer
->batch
.user_data
= cmd_buffer
;
2321 anv_batch_bo_start(cmd_buffer
->last_batch_bo
, &cmd_buffer
->batch
,
2322 GEN8_MI_BATCH_BUFFER_START_length
* 4);
2324 result
= anv_batch_bo_create(device
, &cmd_buffer
->surface_batch_bo
);
2325 if (result
!= VK_SUCCESS
)
2326 goto fail_batch_relocs
;
2327 cmd_buffer
->surface_batch_bo
->first_reloc
= 0;
2329 result
= anv_reloc_list_init(&cmd_buffer
->surface_relocs
, device
);
2330 if (result
!= VK_SUCCESS
)
2331 goto fail_ss_batch_bo
;
2333 /* Start surface_next at 1 so surface offset 0 is invalid. */
2334 cmd_buffer
->surface_next
= 1;
2336 cmd_buffer
->exec2_objects
= NULL
;
2337 cmd_buffer
->exec2_bos
= NULL
;
2338 cmd_buffer
->exec2_array_length
= 0;
2340 anv_state_stream_init(&cmd_buffer
->surface_state_stream
,
2341 &device
->surface_state_block_pool
);
2342 anv_state_stream_init(&cmd_buffer
->dynamic_state_stream
,
2343 &device
->dynamic_state_block_pool
);
2345 cmd_buffer
->dirty
= 0;
2346 cmd_buffer
->vb_dirty
= 0;
2347 cmd_buffer
->descriptors_dirty
= 0;
2348 cmd_buffer
->pipeline
= NULL
;
2349 cmd_buffer
->vp_state
= NULL
;
2350 cmd_buffer
->rs_state
= NULL
;
2351 cmd_buffer
->ds_state
= NULL
;
2353 *pCmdBuffer
= (VkCmdBuffer
) cmd_buffer
;
2358 anv_batch_bo_destroy(cmd_buffer
->surface_batch_bo
, device
);
2360 anv_reloc_list_finish(&cmd_buffer
->batch
.relocs
, device
);
2362 anv_batch_bo_destroy(cmd_buffer
->last_batch_bo
, device
);
2364 anv_device_free(device
, cmd_buffer
);
2370 anv_cmd_buffer_emit_state_base_address(struct anv_cmd_buffer
*cmd_buffer
)
2372 struct anv_device
*device
= cmd_buffer
->device
;
2374 anv_batch_emit(&cmd_buffer
->batch
, GEN8_STATE_BASE_ADDRESS
,
2375 .GeneralStateBaseAddress
= { NULL
, 0 },
2376 .GeneralStateMemoryObjectControlState
= GEN8_MOCS
,
2377 .GeneralStateBaseAddressModifyEnable
= true,
2378 .GeneralStateBufferSize
= 0xfffff,
2379 .GeneralStateBufferSizeModifyEnable
= true,
2381 .SurfaceStateBaseAddress
= { &cmd_buffer
->surface_batch_bo
->bo
, 0 },
2382 .SurfaceStateMemoryObjectControlState
= GEN8_MOCS
,
2383 .SurfaceStateBaseAddressModifyEnable
= true,
2385 .DynamicStateBaseAddress
= { &device
->dynamic_state_block_pool
.bo
, 0 },
2386 .DynamicStateMemoryObjectControlState
= GEN8_MOCS
,
2387 .DynamicStateBaseAddressModifyEnable
= true,
2388 .DynamicStateBufferSize
= 0xfffff,
2389 .DynamicStateBufferSizeModifyEnable
= true,
2391 .IndirectObjectBaseAddress
= { NULL
, 0 },
2392 .IndirectObjectMemoryObjectControlState
= GEN8_MOCS
,
2393 .IndirectObjectBaseAddressModifyEnable
= true,
2394 .IndirectObjectBufferSize
= 0xfffff,
2395 .IndirectObjectBufferSizeModifyEnable
= true,
2397 .InstructionBaseAddress
= { &device
->instruction_block_pool
.bo
, 0 },
2398 .InstructionMemoryObjectControlState
= GEN8_MOCS
,
2399 .InstructionBaseAddressModifyEnable
= true,
2400 .InstructionBufferSize
= 0xfffff,
2401 .InstructionBuffersizeModifyEnable
= true);
2404 VkResult
anv_BeginCommandBuffer(
2405 VkCmdBuffer cmdBuffer
,
2406 const VkCmdBufferBeginInfo
* pBeginInfo
)
2408 struct anv_cmd_buffer
*cmd_buffer
= (struct anv_cmd_buffer
*) cmdBuffer
;
2410 anv_batch_emit(&cmd_buffer
->batch
, GEN8_PIPELINE_SELECT
,
2411 .PipelineSelection
= _3D
);
2412 anv_batch_emit(&cmd_buffer
->batch
, GEN8_STATE_SIP
);
2414 anv_cmd_buffer_emit_state_base_address(cmd_buffer
);
2416 anv_batch_emit(&cmd_buffer
->batch
, GEN8_3DSTATE_VF_STATISTICS
,
2417 .StatisticsEnable
= true);
2418 anv_batch_emit(&cmd_buffer
->batch
, GEN8_3DSTATE_HS
, .Enable
= false);
2419 anv_batch_emit(&cmd_buffer
->batch
, GEN8_3DSTATE_TE
, .TEEnable
= false);
2420 anv_batch_emit(&cmd_buffer
->batch
, GEN8_3DSTATE_DS
, .FunctionEnable
= false);
2421 anv_batch_emit(&cmd_buffer
->batch
, GEN8_3DSTATE_STREAMOUT
, .SOFunctionEnable
= false);
2423 anv_batch_emit(&cmd_buffer
->batch
, GEN8_3DSTATE_PUSH_CONSTANT_ALLOC_VS
,
2424 .ConstantBufferOffset
= 0,
2425 .ConstantBufferSize
= 4);
2426 anv_batch_emit(&cmd_buffer
->batch
, GEN8_3DSTATE_PUSH_CONSTANT_ALLOC_GS
,
2427 .ConstantBufferOffset
= 4,
2428 .ConstantBufferSize
= 4);
2429 anv_batch_emit(&cmd_buffer
->batch
, GEN8_3DSTATE_PUSH_CONSTANT_ALLOC_PS
,
2430 .ConstantBufferOffset
= 8,
2431 .ConstantBufferSize
= 4);
2433 anv_batch_emit(&cmd_buffer
->batch
, GEN8_3DSTATE_WM_CHROMAKEY
,
2434 .ChromaKeyKillEnable
= false);
2435 anv_batch_emit(&cmd_buffer
->batch
, GEN8_3DSTATE_SBE_SWIZ
);
2436 anv_batch_emit(&cmd_buffer
->batch
, GEN8_3DSTATE_AA_LINE_PARAMETERS
);
2442 anv_cmd_buffer_add_bo(struct anv_cmd_buffer
*cmd_buffer
,
2444 struct drm_i915_gem_relocation_entry
*relocs
,
2447 struct drm_i915_gem_exec_object2
*obj
;
2449 if (bo
->index
< cmd_buffer
->bo_count
&&
2450 cmd_buffer
->exec2_bos
[bo
->index
] == bo
)
2453 if (cmd_buffer
->bo_count
>= cmd_buffer
->exec2_array_length
) {
2454 uint32_t new_len
= cmd_buffer
->exec2_objects
?
2455 cmd_buffer
->exec2_array_length
* 2 : 64;
2457 struct drm_i915_gem_exec_object2
*new_objects
=
2458 anv_device_alloc(cmd_buffer
->device
, new_len
* sizeof(*new_objects
),
2459 8, VK_SYSTEM_ALLOC_TYPE_INTERNAL
);
2460 if (new_objects
== NULL
)
2461 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
2463 struct anv_bo
**new_bos
=
2464 anv_device_alloc(cmd_buffer
->device
, new_len
* sizeof(*new_bos
),
2465 8, VK_SYSTEM_ALLOC_TYPE_INTERNAL
);
2466 if (new_objects
== NULL
) {
2467 anv_device_free(cmd_buffer
->device
, new_objects
);
2468 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
2471 if (cmd_buffer
->exec2_objects
) {
2472 memcpy(new_objects
, cmd_buffer
->exec2_objects
,
2473 cmd_buffer
->bo_count
* sizeof(*new_objects
));
2474 memcpy(new_bos
, cmd_buffer
->exec2_bos
,
2475 cmd_buffer
->bo_count
* sizeof(*new_bos
));
2478 cmd_buffer
->exec2_objects
= new_objects
;
2479 cmd_buffer
->exec2_bos
= new_bos
;
2480 cmd_buffer
->exec2_array_length
= new_len
;
2483 assert(cmd_buffer
->bo_count
< cmd_buffer
->exec2_array_length
);
2485 bo
->index
= cmd_buffer
->bo_count
++;
2486 obj
= &cmd_buffer
->exec2_objects
[bo
->index
];
2487 cmd_buffer
->exec2_bos
[bo
->index
] = bo
;
2489 obj
->handle
= bo
->gem_handle
;
2490 obj
->relocation_count
= 0;
2491 obj
->relocs_ptr
= 0;
2493 obj
->offset
= bo
->offset
;
2499 obj
->relocation_count
= num_relocs
;
2500 obj
->relocs_ptr
= (uintptr_t) relocs
;
2507 anv_cmd_buffer_add_validate_bos(struct anv_cmd_buffer
*cmd_buffer
,
2508 struct anv_reloc_list
*list
)
2510 for (size_t i
= 0; i
< list
->num_relocs
; i
++)
2511 anv_cmd_buffer_add_bo(cmd_buffer
, list
->reloc_bos
[i
], NULL
, 0);
2515 anv_cmd_buffer_process_relocs(struct anv_cmd_buffer
*cmd_buffer
,
2516 struct anv_reloc_list
*list
)
2520 /* If the kernel supports I915_EXEC_NO_RELOC, it will compare offset in
2521 * struct drm_i915_gem_exec_object2 against the bos current offset and if
2522 * all bos haven't moved it will skip relocation processing alltogether.
2523 * If I915_EXEC_NO_RELOC is not supported, the kernel ignores the incoming
2524 * value of offset so we can set it either way. For that to work we need
2525 * to make sure all relocs use the same presumed offset.
2528 for (size_t i
= 0; i
< list
->num_relocs
; i
++) {
2529 bo
= list
->reloc_bos
[i
];
2530 if (bo
->offset
!= list
->relocs
[i
].presumed_offset
)
2531 cmd_buffer
->need_reloc
= true;
2533 list
->relocs
[i
].target_handle
= bo
->index
;
2537 VkResult
anv_EndCommandBuffer(
2538 VkCmdBuffer cmdBuffer
)
2540 struct anv_cmd_buffer
*cmd_buffer
= (struct anv_cmd_buffer
*) cmdBuffer
;
2541 struct anv_device
*device
= cmd_buffer
->device
;
2542 struct anv_batch
*batch
= &cmd_buffer
->batch
;
2544 anv_batch_emit(batch
, GEN8_MI_BATCH_BUFFER_END
);
2546 /* Round batch up to an even number of dwords. */
2547 if ((batch
->next
- batch
->start
) & 4)
2548 anv_batch_emit(batch
, GEN8_MI_NOOP
);
2550 anv_batch_bo_finish(cmd_buffer
->last_batch_bo
, &cmd_buffer
->batch
);
2551 cmd_buffer
->surface_batch_bo
->num_relocs
=
2552 cmd_buffer
->surface_relocs
.num_relocs
- cmd_buffer
->surface_batch_bo
->first_reloc
;
2553 cmd_buffer
->surface_batch_bo
->length
= cmd_buffer
->surface_next
;
2555 cmd_buffer
->bo_count
= 0;
2556 cmd_buffer
->need_reloc
= false;
2558 /* Lock for access to bo->index. */
2559 pthread_mutex_lock(&device
->mutex
);
2561 /* Add surface state bos first so we can add them with their relocs. */
2562 for (struct anv_batch_bo
*bbo
= cmd_buffer
->surface_batch_bo
;
2563 bbo
!= NULL
; bbo
= bbo
->prev_batch_bo
) {
2564 anv_cmd_buffer_add_bo(cmd_buffer
, &bbo
->bo
,
2565 &cmd_buffer
->surface_relocs
.relocs
[bbo
->first_reloc
],
2569 /* Add all of the BOs referenced by surface state */
2570 anv_cmd_buffer_add_validate_bos(cmd_buffer
, &cmd_buffer
->surface_relocs
);
2572 /* Add all but the first batch BO */
2573 struct anv_batch_bo
*batch_bo
= cmd_buffer
->last_batch_bo
;
2574 while (batch_bo
->prev_batch_bo
) {
2575 anv_cmd_buffer_add_bo(cmd_buffer
, &batch_bo
->bo
,
2576 &batch
->relocs
.relocs
[batch_bo
->first_reloc
],
2577 batch_bo
->num_relocs
);
2578 batch_bo
= batch_bo
->prev_batch_bo
;
2581 /* Add everything referenced by the batches */
2582 anv_cmd_buffer_add_validate_bos(cmd_buffer
, &batch
->relocs
);
2584 /* Add the first batch bo last */
2585 assert(batch_bo
->prev_batch_bo
== NULL
&& batch_bo
->first_reloc
== 0);
2586 anv_cmd_buffer_add_bo(cmd_buffer
, &batch_bo
->bo
,
2587 &batch
->relocs
.relocs
[batch_bo
->first_reloc
],
2588 batch_bo
->num_relocs
);
2589 assert(batch_bo
->bo
.index
== cmd_buffer
->bo_count
- 1);
2591 anv_cmd_buffer_process_relocs(cmd_buffer
, &cmd_buffer
->surface_relocs
);
2592 anv_cmd_buffer_process_relocs(cmd_buffer
, &batch
->relocs
);
2594 cmd_buffer
->execbuf
.buffers_ptr
= (uintptr_t) cmd_buffer
->exec2_objects
;
2595 cmd_buffer
->execbuf
.buffer_count
= cmd_buffer
->bo_count
;
2596 cmd_buffer
->execbuf
.batch_start_offset
= 0;
2597 cmd_buffer
->execbuf
.batch_len
= batch
->next
- batch
->start
;
2598 cmd_buffer
->execbuf
.cliprects_ptr
= 0;
2599 cmd_buffer
->execbuf
.num_cliprects
= 0;
2600 cmd_buffer
->execbuf
.DR1
= 0;
2601 cmd_buffer
->execbuf
.DR4
= 0;
2603 cmd_buffer
->execbuf
.flags
= I915_EXEC_HANDLE_LUT
;
2604 if (!cmd_buffer
->need_reloc
)
2605 cmd_buffer
->execbuf
.flags
|= I915_EXEC_NO_RELOC
;
2606 cmd_buffer
->execbuf
.flags
|= I915_EXEC_RENDER
;
2607 cmd_buffer
->execbuf
.rsvd1
= device
->context_id
;
2608 cmd_buffer
->execbuf
.rsvd2
= 0;
2610 pthread_mutex_unlock(&device
->mutex
);
2615 VkResult
anv_ResetCommandBuffer(
2616 VkCmdBuffer cmdBuffer
)
2618 struct anv_cmd_buffer
*cmd_buffer
= (struct anv_cmd_buffer
*) cmdBuffer
;
2620 /* Delete all but the first batch bo */
2621 while (cmd_buffer
->last_batch_bo
->prev_batch_bo
) {
2622 struct anv_batch_bo
*prev
= cmd_buffer
->last_batch_bo
->prev_batch_bo
;
2623 anv_batch_bo_destroy(cmd_buffer
->last_batch_bo
, cmd_buffer
->device
);
2624 cmd_buffer
->last_batch_bo
= prev
;
2626 assert(cmd_buffer
->last_batch_bo
->prev_batch_bo
== NULL
);
2628 cmd_buffer
->batch
.relocs
.num_relocs
= 0;
2629 anv_batch_bo_start(cmd_buffer
->last_batch_bo
, &cmd_buffer
->batch
,
2630 GEN8_MI_BATCH_BUFFER_START_length
* 4);
2632 /* Delete all but the first batch bo */
2633 while (cmd_buffer
->surface_batch_bo
->prev_batch_bo
) {
2634 struct anv_batch_bo
*prev
= cmd_buffer
->surface_batch_bo
->prev_batch_bo
;
2635 anv_batch_bo_destroy(cmd_buffer
->surface_batch_bo
, cmd_buffer
->device
);
2636 cmd_buffer
->surface_batch_bo
= prev
;
2638 assert(cmd_buffer
->surface_batch_bo
->prev_batch_bo
== NULL
);
2640 cmd_buffer
->surface_next
= 1;
2641 cmd_buffer
->surface_relocs
.num_relocs
= 0;
2646 // Command buffer building functions
2648 void anv_CmdBindPipeline(
2649 VkCmdBuffer cmdBuffer
,
2650 VkPipelineBindPoint pipelineBindPoint
,
2651 VkPipeline _pipeline
)
2653 struct anv_cmd_buffer
*cmd_buffer
= (struct anv_cmd_buffer
*) cmdBuffer
;
2654 struct anv_pipeline
*pipeline
= (struct anv_pipeline
*) _pipeline
;
2656 cmd_buffer
->pipeline
= pipeline
;
2657 cmd_buffer
->vb_dirty
|= pipeline
->vb_used
;
2658 cmd_buffer
->dirty
|= ANV_CMD_BUFFER_PIPELINE_DIRTY
;
2661 void anv_CmdBindDynamicStateObject(
2662 VkCmdBuffer cmdBuffer
,
2663 VkStateBindPoint stateBindPoint
,
2664 VkDynamicStateObject dynamicState
)
2666 struct anv_cmd_buffer
*cmd_buffer
= (struct anv_cmd_buffer
*) cmdBuffer
;
2667 struct anv_dynamic_vp_state
*vp_state
;
2669 switch (stateBindPoint
) {
2670 case VK_STATE_BIND_POINT_VIEWPORT
:
2671 vp_state
= (struct anv_dynamic_vp_state
*) dynamicState
;
2672 /* We emit state immediately, but set cmd_buffer->vp_state to indicate
2673 * that vp state has been set in this command buffer. */
2674 cmd_buffer
->vp_state
= vp_state
;
2675 anv_batch_emit(&cmd_buffer
->batch
, GEN8_3DSTATE_SCISSOR_STATE_POINTERS
,
2676 .ScissorRectPointer
= vp_state
->scissor
.offset
);
2677 anv_batch_emit(&cmd_buffer
->batch
, GEN8_3DSTATE_VIEWPORT_STATE_POINTERS_CC
,
2678 .CCViewportPointer
= vp_state
->cc_vp
.offset
);
2679 anv_batch_emit(&cmd_buffer
->batch
, GEN8_3DSTATE_VIEWPORT_STATE_POINTERS_SF_CLIP
,
2680 .SFClipViewportPointer
= vp_state
->sf_clip_vp
.offset
);
2682 case VK_STATE_BIND_POINT_RASTER
:
2683 cmd_buffer
->rs_state
= (struct anv_dynamic_rs_state
*) dynamicState
;
2684 cmd_buffer
->dirty
|= ANV_CMD_BUFFER_RS_DIRTY
;
2686 case VK_STATE_BIND_POINT_COLOR_BLEND
:
2687 cmd_buffer
->cb_state
= (struct anv_dynamic_cb_state
*) dynamicState
;
2688 cmd_buffer
->dirty
|= ANV_CMD_BUFFER_CB_DIRTY
;
2690 case VK_STATE_BIND_POINT_DEPTH_STENCIL
:
2691 cmd_buffer
->ds_state
= (struct anv_dynamic_ds_state
*) dynamicState
;
2692 cmd_buffer
->dirty
|= ANV_CMD_BUFFER_DS_DIRTY
;
2699 static struct anv_state
2700 anv_cmd_buffer_alloc_surface_state(struct anv_cmd_buffer
*cmd_buffer
,
2701 uint32_t size
, uint32_t alignment
)
2703 struct anv_state state
;
2705 state
.offset
= ALIGN_U32(cmd_buffer
->surface_next
, alignment
);
2706 if (state
.offset
+ size
> cmd_buffer
->surface_batch_bo
->bo
.size
)
2707 return (struct anv_state
) { 0 };
2709 state
.map
= cmd_buffer
->surface_batch_bo
->bo
.map
+ state
.offset
;
2710 state
.alloc_size
= size
;
2711 cmd_buffer
->surface_next
= state
.offset
+ size
;
2713 assert(state
.offset
+ size
<= cmd_buffer
->surface_batch_bo
->bo
.size
);
2719 anv_cmd_buffer_new_surface_state_bo(struct anv_cmd_buffer
*cmd_buffer
)
2721 struct anv_batch_bo
*new_bbo
, *old_bbo
= cmd_buffer
->surface_batch_bo
;
2723 /* Finish off the old buffer */
2724 old_bbo
->num_relocs
=
2725 cmd_buffer
->surface_relocs
.num_relocs
- old_bbo
->first_reloc
;
2726 old_bbo
->length
= cmd_buffer
->surface_next
;
2728 VkResult result
= anv_batch_bo_create(cmd_buffer
->device
, &new_bbo
);
2729 if (result
!= VK_SUCCESS
)
2732 new_bbo
->first_reloc
= cmd_buffer
->surface_relocs
.num_relocs
;
2733 cmd_buffer
->surface_next
= 1;
2735 new_bbo
->prev_batch_bo
= old_bbo
;
2736 cmd_buffer
->surface_batch_bo
= new_bbo
;
2738 /* Re-emit state base addresses so we get the new surface state base
2739 * address before we start emitting binding tables etc.
2741 anv_cmd_buffer_emit_state_base_address(cmd_buffer
);
2743 /* It seems like just changing the state base addresses isn't enough.
2744 * Invalidating the cache seems to be enough to cause things to
2745 * propagate. However, I'm not 100% sure what we're supposed to do.
2747 anv_batch_emit(&cmd_buffer
->batch
, GEN8_PIPE_CONTROL
,
2748 .TextureCacheInvalidationEnable
= true);
2753 void anv_CmdBindDescriptorSets(
2754 VkCmdBuffer cmdBuffer
,
2755 VkPipelineBindPoint pipelineBindPoint
,
2758 const VkDescriptorSet
* pDescriptorSets
,
2759 uint32_t dynamicOffsetCount
,
2760 const uint32_t* pDynamicOffsets
)
2762 struct anv_cmd_buffer
*cmd_buffer
= (struct anv_cmd_buffer
*) cmdBuffer
;
2763 struct anv_pipeline_layout
*layout
= cmd_buffer
->pipeline
->layout
;
2764 struct anv_descriptor_set
*set
;
2765 struct anv_descriptor_set_layout
*set_layout
;
2767 assert(firstSet
+ setCount
< MAX_SETS
);
2769 uint32_t dynamic_slot
= 0;
2770 for (uint32_t i
= 0; i
< setCount
; i
++) {
2771 set
= (struct anv_descriptor_set
*) pDescriptorSets
[i
];
2772 set_layout
= layout
->set
[firstSet
+ i
].layout
;
2774 cmd_buffer
->descriptors
[firstSet
+ i
].set
= set
;
2776 assert(set_layout
->num_dynamic_buffers
<
2777 ARRAY_SIZE(cmd_buffer
->descriptors
[0].dynamic_offsets
));
2778 memcpy(cmd_buffer
->descriptors
[firstSet
+ i
].dynamic_offsets
,
2779 pDynamicOffsets
+ dynamic_slot
,
2780 set_layout
->num_dynamic_buffers
* sizeof(*pDynamicOffsets
));
2782 cmd_buffer
->descriptors_dirty
|= set_layout
->shader_stages
;
2784 dynamic_slot
+= set_layout
->num_dynamic_buffers
;
2788 void anv_CmdBindIndexBuffer(
2789 VkCmdBuffer cmdBuffer
,
2791 VkDeviceSize offset
,
2792 VkIndexType indexType
)
2794 struct anv_cmd_buffer
*cmd_buffer
= (struct anv_cmd_buffer
*) cmdBuffer
;
2795 struct anv_buffer
*buffer
= (struct anv_buffer
*) _buffer
;
2797 static const uint32_t vk_to_gen_index_type
[] = {
2798 [VK_INDEX_TYPE_UINT8
] = INDEX_BYTE
,
2799 [VK_INDEX_TYPE_UINT16
] = INDEX_WORD
,
2800 [VK_INDEX_TYPE_UINT32
] = INDEX_DWORD
,
2803 anv_batch_emit(&cmd_buffer
->batch
, GEN8_3DSTATE_INDEX_BUFFER
,
2804 .IndexFormat
= vk_to_gen_index_type
[indexType
],
2805 .MemoryObjectControlState
= GEN8_MOCS
,
2806 .BufferStartingAddress
= { buffer
->bo
, buffer
->offset
+ offset
},
2807 .BufferSize
= buffer
->size
- offset
);
2810 void anv_CmdBindVertexBuffers(
2811 VkCmdBuffer cmdBuffer
,
2812 uint32_t startBinding
,
2813 uint32_t bindingCount
,
2814 const VkBuffer
* pBuffers
,
2815 const VkDeviceSize
* pOffsets
)
2817 struct anv_cmd_buffer
*cmd_buffer
= (struct anv_cmd_buffer
*) cmdBuffer
;
2818 struct anv_vertex_binding
*vb
= cmd_buffer
->vertex_bindings
;
2820 /* We have to defer setting up vertex buffer since we need the buffer
2821 * stride from the pipeline. */
2823 assert(startBinding
+ bindingCount
< MAX_VBS
);
2824 for (uint32_t i
= 0; i
< bindingCount
; i
++) {
2825 vb
[startBinding
+ i
].buffer
= (struct anv_buffer
*) pBuffers
[i
];
2826 vb
[startBinding
+ i
].offset
= pOffsets
[i
];
2827 cmd_buffer
->vb_dirty
|= 1 << (startBinding
+ i
);
2832 cmd_buffer_emit_binding_table(struct anv_cmd_buffer
*cmd_buffer
,
2835 struct anv_pipeline_layout
*layout
= cmd_buffer
->pipeline
->layout
;
2836 uint32_t color_attachments
, bias
, size
;
2837 struct anv_state bt_state
;
2839 if (stage
== VK_SHADER_STAGE_FRAGMENT
) {
2841 color_attachments
= cmd_buffer
->framebuffer
->color_attachment_count
;
2844 color_attachments
= 0;
2847 /* This is a little awkward: layout can be NULL but we still have to
2848 * allocate and set a binding table for the PS stage for render
2850 uint32_t surface_count
= layout
? layout
->stage
[stage
].surface_count
: 0;
2852 if (color_attachments
+ surface_count
== 0)
2855 size
= (bias
+ surface_count
) * sizeof(uint32_t);
2856 bt_state
= anv_cmd_buffer_alloc_surface_state(cmd_buffer
, size
, 32);
2857 uint32_t *bt_map
= bt_state
.map
;
2859 if (bt_state
.map
== NULL
)
2860 return VK_ERROR_OUT_OF_DEVICE_MEMORY
;
2862 static const uint32_t binding_table_opcodes
[] = {
2863 [VK_SHADER_STAGE_VERTEX
] = 38,
2864 [VK_SHADER_STAGE_TESS_CONTROL
] = 39,
2865 [VK_SHADER_STAGE_TESS_EVALUATION
] = 40,
2866 [VK_SHADER_STAGE_GEOMETRY
] = 41,
2867 [VK_SHADER_STAGE_FRAGMENT
] = 42,
2868 [VK_SHADER_STAGE_COMPUTE
] = 0,
2871 anv_batch_emit(&cmd_buffer
->batch
,
2872 GEN8_3DSTATE_BINDING_TABLE_POINTERS_VS
,
2873 ._3DCommandSubOpcode
= binding_table_opcodes
[stage
],
2874 .PointertoVSBindingTable
= bt_state
.offset
);
2876 for (uint32_t ca
= 0; ca
< color_attachments
; ca
++) {
2877 const struct anv_surface_view
*view
=
2878 cmd_buffer
->framebuffer
->color_attachments
[ca
];
2880 struct anv_state state
=
2881 anv_cmd_buffer_alloc_surface_state(cmd_buffer
, 64, 64);
2883 if (state
.map
== NULL
)
2884 return VK_ERROR_OUT_OF_DEVICE_MEMORY
;
2886 memcpy(state
.map
, view
->surface_state
.map
, 64);
2888 /* The address goes in dwords 8 and 9 of the SURFACE_STATE */
2889 *(uint64_t *)(state
.map
+ 8 * 4) =
2890 anv_reloc_list_add(&cmd_buffer
->surface_relocs
,
2892 state
.offset
+ 8 * 4,
2893 view
->bo
, view
->offset
);
2895 bt_map
[ca
] = state
.offset
;
2901 for (uint32_t set
= 0; set
< layout
->num_sets
; set
++) {
2902 struct anv_descriptor_set_binding
*d
= &cmd_buffer
->descriptors
[set
];
2903 struct anv_descriptor_set_layout
*set_layout
= layout
->set
[set
].layout
;
2904 struct anv_descriptor_slot
*surface_slots
=
2905 set_layout
->stage
[stage
].surface_start
;
2907 uint32_t start
= bias
+ layout
->set
[set
].surface_start
[stage
];
2909 for (uint32_t b
= 0; b
< set_layout
->stage
[stage
].surface_count
; b
++) {
2910 struct anv_surface_view
*view
=
2911 d
->set
->descriptors
[surface_slots
[b
].index
].view
;
2916 struct anv_state state
=
2917 anv_cmd_buffer_alloc_surface_state(cmd_buffer
, 64, 64);
2919 if (state
.map
== NULL
)
2920 return VK_ERROR_OUT_OF_DEVICE_MEMORY
;
2923 if (surface_slots
[b
].dynamic_slot
>= 0) {
2924 uint32_t dynamic_offset
=
2925 d
->dynamic_offsets
[surface_slots
[b
].dynamic_slot
];
2927 offset
= view
->offset
+ dynamic_offset
;
2928 fill_buffer_surface_state(state
.map
, view
->format
, offset
,
2929 view
->range
- dynamic_offset
);
2931 offset
= view
->offset
;
2932 memcpy(state
.map
, view
->surface_state
.map
, 64);
2935 /* The address goes in dwords 8 and 9 of the SURFACE_STATE */
2936 *(uint64_t *)(state
.map
+ 8 * 4) =
2937 anv_reloc_list_add(&cmd_buffer
->surface_relocs
,
2939 state
.offset
+ 8 * 4,
2942 bt_map
[start
+ b
] = state
.offset
;
2950 cmd_buffer_emit_samplers(struct anv_cmd_buffer
*cmd_buffer
, unsigned stage
)
2952 struct anv_pipeline_layout
*layout
= cmd_buffer
->pipeline
->layout
;
2953 struct anv_state state
;
2958 uint32_t sampler_count
= layout
->stage
[stage
].sampler_count
;
2960 if (sampler_count
== 0)
2963 uint32_t size
= sampler_count
* 16;
2964 state
= anv_state_stream_alloc(&cmd_buffer
->dynamic_state_stream
, size
, 32);
2966 if (state
.map
== NULL
)
2967 return VK_ERROR_OUT_OF_DEVICE_MEMORY
;
2969 static const uint32_t sampler_state_opcodes
[] = {
2970 [VK_SHADER_STAGE_VERTEX
] = 43,
2971 [VK_SHADER_STAGE_TESS_CONTROL
] = 44, /* HS */
2972 [VK_SHADER_STAGE_TESS_EVALUATION
] = 45, /* DS */
2973 [VK_SHADER_STAGE_GEOMETRY
] = 46,
2974 [VK_SHADER_STAGE_FRAGMENT
] = 47,
2975 [VK_SHADER_STAGE_COMPUTE
] = 0,
2978 anv_batch_emit(&cmd_buffer
->batch
,
2979 GEN8_3DSTATE_SAMPLER_STATE_POINTERS_VS
,
2980 ._3DCommandSubOpcode
= sampler_state_opcodes
[stage
],
2981 .PointertoVSSamplerState
= state
.offset
);
2983 for (uint32_t set
= 0; set
< layout
->num_sets
; set
++) {
2984 struct anv_descriptor_set_binding
*d
= &cmd_buffer
->descriptors
[set
];
2985 struct anv_descriptor_set_layout
*set_layout
= layout
->set
[set
].layout
;
2986 struct anv_descriptor_slot
*sampler_slots
=
2987 set_layout
->stage
[stage
].sampler_start
;
2989 uint32_t start
= layout
->set
[set
].sampler_start
[stage
];
2991 for (uint32_t b
= 0; b
< set_layout
->stage
[stage
].sampler_count
; b
++) {
2992 struct anv_sampler
*sampler
=
2993 d
->set
->descriptors
[sampler_slots
[b
].index
].sampler
;
2998 memcpy(state
.map
+ (start
+ b
) * 16,
2999 sampler
->state
, sizeof(sampler
->state
));
3007 flush_descriptor_sets(struct anv_cmd_buffer
*cmd_buffer
)
3009 uint32_t s
, dirty
= cmd_buffer
->descriptors_dirty
&
3010 cmd_buffer
->pipeline
->active_stages
;
3013 for_each_bit(s
, dirty
) {
3014 result
= cmd_buffer_emit_binding_table(cmd_buffer
, s
);
3015 if (result
!= VK_SUCCESS
)
3018 result
= cmd_buffer_emit_samplers(cmd_buffer
, s
);
3019 if (result
!= VK_SUCCESS
)
3023 if (result
!= VK_SUCCESS
) {
3024 assert(result
== VK_ERROR_OUT_OF_DEVICE_MEMORY
);
3026 result
= anv_cmd_buffer_new_surface_state_bo(cmd_buffer
);
3027 assert(result
== VK_SUCCESS
);
3029 /* Re-emit all active binding tables */
3030 for_each_bit(s
, cmd_buffer
->pipeline
->active_stages
) {
3031 result
= cmd_buffer_emit_binding_table(cmd_buffer
, s
);
3032 result
= cmd_buffer_emit_samplers(cmd_buffer
, s
);
3035 /* It had better succeed this time */
3036 assert(result
== VK_SUCCESS
);
3039 cmd_buffer
->descriptors_dirty
&= ~cmd_buffer
->pipeline
->active_stages
;
3042 static struct anv_state
3043 anv_cmd_buffer_emit_dynamic(struct anv_cmd_buffer
*cmd_buffer
,
3044 uint32_t *a
, uint32_t dwords
, uint32_t alignment
)
3046 struct anv_state state
;
3048 state
= anv_state_stream_alloc(&cmd_buffer
->dynamic_state_stream
,
3049 dwords
* 4, alignment
);
3050 memcpy(state
.map
, a
, dwords
* 4);
3055 static struct anv_state
3056 anv_cmd_buffer_merge_dynamic(struct anv_cmd_buffer
*cmd_buffer
,
3057 uint32_t *a
, uint32_t *b
,
3058 uint32_t dwords
, uint32_t alignment
)
3060 struct anv_state state
;
3063 state
= anv_state_stream_alloc(&cmd_buffer
->dynamic_state_stream
,
3064 dwords
* 4, alignment
);
3066 for (uint32_t i
= 0; i
< dwords
; i
++)
3073 anv_cmd_buffer_flush_state(struct anv_cmd_buffer
*cmd_buffer
)
3075 struct anv_pipeline
*pipeline
= cmd_buffer
->pipeline
;
3078 uint32_t vb_emit
= cmd_buffer
->vb_dirty
& pipeline
->vb_used
;
3081 const uint32_t num_buffers
= __builtin_popcount(vb_emit
);
3082 const uint32_t num_dwords
= 1 + num_buffers
* 4;
3084 p
= anv_batch_emitn(&cmd_buffer
->batch
, num_dwords
,
3085 GEN8_3DSTATE_VERTEX_BUFFERS
);
3087 for_each_bit(vb
, vb_emit
) {
3088 struct anv_buffer
*buffer
= cmd_buffer
->vertex_bindings
[vb
].buffer
;
3089 uint32_t offset
= cmd_buffer
->vertex_bindings
[vb
].offset
;
3091 struct GEN8_VERTEX_BUFFER_STATE state
= {
3092 .VertexBufferIndex
= vb
,
3093 .MemoryObjectControlState
= GEN8_MOCS
,
3094 .AddressModifyEnable
= true,
3095 .BufferPitch
= pipeline
->binding_stride
[vb
],
3096 .BufferStartingAddress
= { buffer
->bo
, buffer
->offset
+ offset
},
3097 .BufferSize
= buffer
->size
- offset
3100 GEN8_VERTEX_BUFFER_STATE_pack(&cmd_buffer
->batch
, &p
[1 + i
* 4], &state
);
3105 if (cmd_buffer
->dirty
& ANV_CMD_BUFFER_PIPELINE_DIRTY
)
3106 anv_batch_emit_batch(&cmd_buffer
->batch
, &pipeline
->batch
);
3108 if (cmd_buffer
->descriptors_dirty
)
3109 flush_descriptor_sets(cmd_buffer
);
3111 if (cmd_buffer
->dirty
& (ANV_CMD_BUFFER_PIPELINE_DIRTY
| ANV_CMD_BUFFER_RS_DIRTY
)) {
3112 anv_batch_emit_merge(&cmd_buffer
->batch
,
3113 cmd_buffer
->rs_state
->state_sf
, pipeline
->state_sf
);
3114 anv_batch_emit_merge(&cmd_buffer
->batch
,
3115 cmd_buffer
->rs_state
->state_raster
, pipeline
->state_raster
);
3118 if (cmd_buffer
->ds_state
&&
3119 (cmd_buffer
->dirty
& (ANV_CMD_BUFFER_PIPELINE_DIRTY
| ANV_CMD_BUFFER_DS_DIRTY
)))
3120 anv_batch_emit_merge(&cmd_buffer
->batch
,
3121 cmd_buffer
->ds_state
->state_wm_depth_stencil
,
3122 pipeline
->state_wm_depth_stencil
);
3124 if (cmd_buffer
->dirty
& (ANV_CMD_BUFFER_CB_DIRTY
| ANV_CMD_BUFFER_DS_DIRTY
)) {
3125 struct anv_state state
;
3126 if (cmd_buffer
->ds_state
== NULL
)
3127 state
= anv_cmd_buffer_emit_dynamic(cmd_buffer
,
3128 cmd_buffer
->cb_state
->state_color_calc
,
3129 GEN8_COLOR_CALC_STATE_length
, 64);
3130 else if (cmd_buffer
->cb_state
== NULL
)
3131 state
= anv_cmd_buffer_emit_dynamic(cmd_buffer
,
3132 cmd_buffer
->ds_state
->state_color_calc
,
3133 GEN8_COLOR_CALC_STATE_length
, 64);
3135 state
= anv_cmd_buffer_merge_dynamic(cmd_buffer
,
3136 cmd_buffer
->ds_state
->state_color_calc
,
3137 cmd_buffer
->cb_state
->state_color_calc
,
3138 GEN8_COLOR_CALC_STATE_length
, 64);
3140 anv_batch_emit(&cmd_buffer
->batch
,
3141 GEN8_3DSTATE_CC_STATE_POINTERS
,
3142 .ColorCalcStatePointer
= state
.offset
,
3143 .ColorCalcStatePointerValid
= true);
3146 cmd_buffer
->vb_dirty
&= ~vb_emit
;
3147 cmd_buffer
->dirty
= 0;
3151 VkCmdBuffer cmdBuffer
,
3152 uint32_t firstVertex
,
3153 uint32_t vertexCount
,
3154 uint32_t firstInstance
,
3155 uint32_t instanceCount
)
3157 struct anv_cmd_buffer
*cmd_buffer
= (struct anv_cmd_buffer
*) cmdBuffer
;
3159 anv_cmd_buffer_flush_state(cmd_buffer
);
3161 anv_batch_emit(&cmd_buffer
->batch
, GEN8_3DPRIMITIVE
,
3162 .VertexAccessType
= SEQUENTIAL
,
3163 .VertexCountPerInstance
= vertexCount
,
3164 .StartVertexLocation
= firstVertex
,
3165 .InstanceCount
= instanceCount
,
3166 .StartInstanceLocation
= firstInstance
,
3167 .BaseVertexLocation
= 0);
3170 void anv_CmdDrawIndexed(
3171 VkCmdBuffer cmdBuffer
,
3172 uint32_t firstIndex
,
3173 uint32_t indexCount
,
3174 int32_t vertexOffset
,
3175 uint32_t firstInstance
,
3176 uint32_t instanceCount
)
3178 struct anv_cmd_buffer
*cmd_buffer
= (struct anv_cmd_buffer
*) cmdBuffer
;
3180 anv_cmd_buffer_flush_state(cmd_buffer
);
3182 anv_batch_emit(&cmd_buffer
->batch
, GEN8_3DPRIMITIVE
,
3183 .VertexAccessType
= RANDOM
,
3184 .VertexCountPerInstance
= indexCount
,
3185 .StartVertexLocation
= firstIndex
,
3186 .InstanceCount
= instanceCount
,
3187 .StartInstanceLocation
= firstInstance
,
3188 .BaseVertexLocation
= vertexOffset
);
3192 anv_batch_lrm(struct anv_batch
*batch
,
3193 uint32_t reg
, struct anv_bo
*bo
, uint32_t offset
)
3195 anv_batch_emit(batch
, GEN8_MI_LOAD_REGISTER_MEM
,
3196 .RegisterAddress
= reg
,
3197 .MemoryAddress
= { bo
, offset
});
3201 anv_batch_lri(struct anv_batch
*batch
, uint32_t reg
, uint32_t imm
)
3203 anv_batch_emit(batch
, GEN8_MI_LOAD_REGISTER_IMM
,
3204 .RegisterOffset
= reg
,
3208 /* Auto-Draw / Indirect Registers */
3209 #define GEN7_3DPRIM_END_OFFSET 0x2420
3210 #define GEN7_3DPRIM_START_VERTEX 0x2430
3211 #define GEN7_3DPRIM_VERTEX_COUNT 0x2434
3212 #define GEN7_3DPRIM_INSTANCE_COUNT 0x2438
3213 #define GEN7_3DPRIM_START_INSTANCE 0x243C
3214 #define GEN7_3DPRIM_BASE_VERTEX 0x2440
3216 void anv_CmdDrawIndirect(
3217 VkCmdBuffer cmdBuffer
,
3219 VkDeviceSize offset
,
3223 struct anv_cmd_buffer
*cmd_buffer
= (struct anv_cmd_buffer
*) cmdBuffer
;
3224 struct anv_buffer
*buffer
= (struct anv_buffer
*) _buffer
;
3225 struct anv_bo
*bo
= buffer
->bo
;
3226 uint32_t bo_offset
= buffer
->offset
+ offset
;
3228 anv_cmd_buffer_flush_state(cmd_buffer
);
3230 anv_batch_lrm(&cmd_buffer
->batch
, GEN7_3DPRIM_VERTEX_COUNT
, bo
, bo_offset
);
3231 anv_batch_lrm(&cmd_buffer
->batch
, GEN7_3DPRIM_INSTANCE_COUNT
, bo
, bo_offset
+ 4);
3232 anv_batch_lrm(&cmd_buffer
->batch
, GEN7_3DPRIM_START_VERTEX
, bo
, bo_offset
+ 8);
3233 anv_batch_lrm(&cmd_buffer
->batch
, GEN7_3DPRIM_START_INSTANCE
, bo
, bo_offset
+ 12);
3234 anv_batch_lri(&cmd_buffer
->batch
, GEN7_3DPRIM_BASE_VERTEX
, 0);
3236 anv_batch_emit(&cmd_buffer
->batch
, GEN8_3DPRIMITIVE
,
3237 .IndirectParameterEnable
= true,
3238 .VertexAccessType
= SEQUENTIAL
);
3241 void anv_CmdDrawIndexedIndirect(
3242 VkCmdBuffer cmdBuffer
,
3244 VkDeviceSize offset
,
3248 struct anv_cmd_buffer
*cmd_buffer
= (struct anv_cmd_buffer
*) cmdBuffer
;
3249 struct anv_buffer
*buffer
= (struct anv_buffer
*) _buffer
;
3250 struct anv_bo
*bo
= buffer
->bo
;
3251 uint32_t bo_offset
= buffer
->offset
+ offset
;
3253 anv_cmd_buffer_flush_state(cmd_buffer
);
3255 anv_batch_lrm(&cmd_buffer
->batch
, GEN7_3DPRIM_VERTEX_COUNT
, bo
, bo_offset
);
3256 anv_batch_lrm(&cmd_buffer
->batch
, GEN7_3DPRIM_INSTANCE_COUNT
, bo
, bo_offset
+ 4);
3257 anv_batch_lrm(&cmd_buffer
->batch
, GEN7_3DPRIM_START_VERTEX
, bo
, bo_offset
+ 8);
3258 anv_batch_lrm(&cmd_buffer
->batch
, GEN7_3DPRIM_BASE_VERTEX
, bo
, bo_offset
+ 12);
3259 anv_batch_lrm(&cmd_buffer
->batch
, GEN7_3DPRIM_START_INSTANCE
, bo
, bo_offset
+ 16);
3261 anv_batch_emit(&cmd_buffer
->batch
, GEN8_3DPRIMITIVE
,
3262 .IndirectParameterEnable
= true,
3263 .VertexAccessType
= RANDOM
);
3266 void anv_CmdDispatch(
3267 VkCmdBuffer cmdBuffer
,
3275 void anv_CmdDispatchIndirect(
3276 VkCmdBuffer cmdBuffer
,
3278 VkDeviceSize offset
)
3283 void anv_CmdSetEvent(
3284 VkCmdBuffer cmdBuffer
,
3286 VkPipeEvent pipeEvent
)
3291 void anv_CmdResetEvent(
3292 VkCmdBuffer cmdBuffer
,
3294 VkPipeEvent pipeEvent
)
3299 void anv_CmdWaitEvents(
3300 VkCmdBuffer cmdBuffer
,
3301 VkWaitEvent waitEvent
,
3302 uint32_t eventCount
,
3303 const VkEvent
* pEvents
,
3304 uint32_t memBarrierCount
,
3305 const void** ppMemBarriers
)
3310 void anv_CmdPipelineBarrier(
3311 VkCmdBuffer cmdBuffer
,
3312 VkWaitEvent waitEvent
,
3313 uint32_t pipeEventCount
,
3314 const VkPipeEvent
* pPipeEvents
,
3315 uint32_t memBarrierCount
,
3316 const void** ppMemBarriers
)
3321 void anv_CmdInitAtomicCounters(
3322 VkCmdBuffer cmdBuffer
,
3323 VkPipelineBindPoint pipelineBindPoint
,
3324 uint32_t startCounter
,
3325 uint32_t counterCount
,
3326 const uint32_t* pData
)
3331 void anv_CmdLoadAtomicCounters(
3332 VkCmdBuffer cmdBuffer
,
3333 VkPipelineBindPoint pipelineBindPoint
,
3334 uint32_t startCounter
,
3335 uint32_t counterCount
,
3337 VkDeviceSize srcOffset
)
3342 void anv_CmdSaveAtomicCounters(
3343 VkCmdBuffer cmdBuffer
,
3344 VkPipelineBindPoint pipelineBindPoint
,
3345 uint32_t startCounter
,
3346 uint32_t counterCount
,
3347 VkBuffer destBuffer
,
3348 VkDeviceSize destOffset
)
3354 anv_framebuffer_destroy(struct anv_device
*device
,
3355 struct anv_object
*object
,
3356 VkObjectType obj_type
)
3358 struct anv_framebuffer
*fb
= (struct anv_framebuffer
*)object
;
3360 assert(obj_type
== VK_OBJECT_TYPE_FRAMEBUFFER
);
3362 anv_DestroyObject((VkDevice
) device
,
3363 VK_OBJECT_TYPE_DYNAMIC_VP_STATE
,
3366 anv_device_free(device
, fb
);
3369 VkResult
anv_CreateFramebuffer(
3371 const VkFramebufferCreateInfo
* pCreateInfo
,
3372 VkFramebuffer
* pFramebuffer
)
3374 struct anv_device
*device
= (struct anv_device
*) _device
;
3375 struct anv_framebuffer
*framebuffer
;
3377 static const struct anv_depth_stencil_view null_view
=
3378 { .depth_format
= D16_UNORM
, .depth_stride
= 0, .stencil_stride
= 0 };
3380 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO
);
3382 framebuffer
= anv_device_alloc(device
, sizeof(*framebuffer
), 8,
3383 VK_SYSTEM_ALLOC_TYPE_API_OBJECT
);
3384 if (framebuffer
== NULL
)
3385 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
3387 framebuffer
->base
.destructor
= anv_framebuffer_destroy
;
3389 framebuffer
->color_attachment_count
= pCreateInfo
->colorAttachmentCount
;
3390 for (uint32_t i
= 0; i
< pCreateInfo
->colorAttachmentCount
; i
++) {
3391 framebuffer
->color_attachments
[i
] =
3392 (struct anv_surface_view
*) pCreateInfo
->pColorAttachments
[i
].view
;
3395 if (pCreateInfo
->pDepthStencilAttachment
) {
3396 framebuffer
->depth_stencil
=
3397 (struct anv_depth_stencil_view
*) pCreateInfo
->pDepthStencilAttachment
->view
;
3399 framebuffer
->depth_stencil
= &null_view
;
3402 framebuffer
->sample_count
= pCreateInfo
->sampleCount
;
3403 framebuffer
->width
= pCreateInfo
->width
;
3404 framebuffer
->height
= pCreateInfo
->height
;
3405 framebuffer
->layers
= pCreateInfo
->layers
;
3407 vkCreateDynamicViewportState((VkDevice
) device
,
3408 &(VkDynamicVpStateCreateInfo
) {
3409 .sType
= VK_STRUCTURE_TYPE_DYNAMIC_VP_STATE_CREATE_INFO
,
3410 .viewportAndScissorCount
= 1,
3411 .pViewports
= (VkViewport
[]) {
3415 .width
= pCreateInfo
->width
,
3416 .height
= pCreateInfo
->height
,
3421 .pScissors
= (VkRect
[]) {
3423 { pCreateInfo
->width
, pCreateInfo
->height
} },
3426 &framebuffer
->vp_state
);
3428 *pFramebuffer
= (VkFramebuffer
) framebuffer
;
3433 VkResult
anv_CreateRenderPass(
3435 const VkRenderPassCreateInfo
* pCreateInfo
,
3436 VkRenderPass
* pRenderPass
)
3438 struct anv_device
*device
= (struct anv_device
*) _device
;
3439 struct anv_render_pass
*pass
;
3442 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO
);
3444 size
= sizeof(*pass
) +
3445 pCreateInfo
->layers
* sizeof(struct anv_render_pass_layer
);
3446 pass
= anv_device_alloc(device
, size
, 8,
3447 VK_SYSTEM_ALLOC_TYPE_API_OBJECT
);
3449 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
3451 pass
->render_area
= pCreateInfo
->renderArea
;
3453 pass
->num_layers
= pCreateInfo
->layers
;
3455 pass
->num_clear_layers
= 0;
3456 for (uint32_t i
= 0; i
< pCreateInfo
->layers
; i
++) {
3457 pass
->layers
[i
].color_load_op
= pCreateInfo
->pColorLoadOps
[i
];
3458 pass
->layers
[i
].clear_color
= pCreateInfo
->pColorLoadClearValues
[i
];
3459 if (pass
->layers
[i
].color_load_op
== VK_ATTACHMENT_LOAD_OP_CLEAR
)
3460 pass
->num_clear_layers
++;
3463 *pRenderPass
= (VkRenderPass
) pass
;
3469 anv_cmd_buffer_emit_depth_stencil(struct anv_cmd_buffer
*cmd_buffer
,
3470 struct anv_render_pass
*pass
)
3472 const struct anv_depth_stencil_view
*view
=
3473 cmd_buffer
->framebuffer
->depth_stencil
;
3475 /* FIXME: Implement the PMA stall W/A */
3477 anv_batch_emit(&cmd_buffer
->batch
, GEN8_3DSTATE_DEPTH_BUFFER
,
3478 .SurfaceType
= SURFTYPE_2D
,
3479 .DepthWriteEnable
= view
->depth_stride
> 0,
3480 .StencilWriteEnable
= view
->stencil_stride
> 0,
3481 .HierarchicalDepthBufferEnable
= false,
3482 .SurfaceFormat
= view
->depth_format
,
3483 .SurfacePitch
= view
->depth_stride
> 0 ? view
->depth_stride
- 1 : 0,
3484 .SurfaceBaseAddress
= { view
->bo
, view
->depth_offset
},
3485 .Height
= pass
->render_area
.extent
.height
- 1,
3486 .Width
= pass
->render_area
.extent
.width
- 1,
3489 .MinimumArrayElement
= 0,
3490 .DepthBufferObjectControlState
= GEN8_MOCS
,
3491 .RenderTargetViewExtent
= 1 - 1,
3492 .SurfaceQPitch
= 0);
3494 /* Disable hierarchial depth buffers. */
3495 anv_batch_emit(&cmd_buffer
->batch
, GEN8_3DSTATE_HIER_DEPTH_BUFFER
);
3497 anv_batch_emit(&cmd_buffer
->batch
, GEN8_3DSTATE_STENCIL_BUFFER
,
3498 .StencilBufferEnable
= view
->stencil_stride
> 0,
3499 .StencilBufferObjectControlState
= GEN8_MOCS
,
3500 .SurfacePitch
= view
->stencil_stride
> 0 ? view
->stencil_stride
- 1 : 0,
3501 .SurfaceBaseAddress
= { view
->bo
, view
->stencil_offset
},
3502 .SurfaceQPitch
= 0);
3504 /* Clear the clear params. */
3505 anv_batch_emit(&cmd_buffer
->batch
, GEN8_3DSTATE_CLEAR_PARAMS
);
3508 void anv_CmdBeginRenderPass(
3509 VkCmdBuffer cmdBuffer
,
3510 const VkRenderPassBegin
* pRenderPassBegin
)
3512 struct anv_cmd_buffer
*cmd_buffer
= (struct anv_cmd_buffer
*) cmdBuffer
;
3513 struct anv_render_pass
*pass
= (struct anv_render_pass
*) pRenderPassBegin
->renderPass
;
3514 struct anv_framebuffer
*framebuffer
=
3515 (struct anv_framebuffer
*) pRenderPassBegin
->framebuffer
;
3517 cmd_buffer
->framebuffer
= framebuffer
;
3519 cmd_buffer
->descriptors_dirty
|= VK_SHADER_STAGE_FRAGMENT_BIT
;
3521 anv_batch_emit(&cmd_buffer
->batch
, GEN8_3DSTATE_DRAWING_RECTANGLE
,
3522 .ClippedDrawingRectangleYMin
= pass
->render_area
.offset
.y
,
3523 .ClippedDrawingRectangleXMin
= pass
->render_area
.offset
.x
,
3524 .ClippedDrawingRectangleYMax
=
3525 pass
->render_area
.offset
.y
+ pass
->render_area
.extent
.height
- 1,
3526 .ClippedDrawingRectangleXMax
=
3527 pass
->render_area
.offset
.x
+ pass
->render_area
.extent
.width
- 1,
3528 .DrawingRectangleOriginY
= 0,
3529 .DrawingRectangleOriginX
= 0);
3531 anv_cmd_buffer_emit_depth_stencil(cmd_buffer
, pass
);
3533 anv_cmd_buffer_clear(cmd_buffer
, pass
);
3536 void anv_CmdEndRenderPass(
3537 VkCmdBuffer cmdBuffer
,
3538 VkRenderPass renderPass
)
3540 /* Emit a flushing pipe control at the end of a pass. This is kind of a
3541 * hack but it ensures that render targets always actually get written.
3542 * Eventually, we should do flushing based on image format transitions
3543 * or something of that nature.
3545 struct anv_cmd_buffer
*cmd_buffer
= (struct anv_cmd_buffer
*)cmdBuffer
;
3546 anv_batch_emit(&cmd_buffer
->batch
, GEN8_PIPE_CONTROL
,
3547 .PostSyncOperation
= NoWrite
,
3548 .RenderTargetCacheFlushEnable
= true,
3549 .InstructionCacheInvalidateEnable
= true,
3550 .DepthCacheFlushEnable
= true,
3551 .VFCacheInvalidationEnable
= true,
3552 .TextureCacheInvalidationEnable
= true,
3553 .CommandStreamerStallEnable
= true);
3556 void vkCmdDbgMarkerBegin(
3557 VkCmdBuffer cmdBuffer
,
3558 const char* pMarker
)
3559 __attribute__ ((visibility ("default")));
3561 void vkCmdDbgMarkerEnd(
3562 VkCmdBuffer cmdBuffer
)
3563 __attribute__ ((visibility ("default")));
3565 VkResult
vkDbgSetObjectTag(
3570 __attribute__ ((visibility ("default")));
3573 void vkCmdDbgMarkerBegin(
3574 VkCmdBuffer cmdBuffer
,
3575 const char* pMarker
)
3579 void vkCmdDbgMarkerEnd(
3580 VkCmdBuffer cmdBuffer
)
3584 VkResult
vkDbgSetObjectTag(