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
);
415 anv_bo_pool_finish(&device
->batch_bo_pool
);
416 anv_block_pool_finish(&device
->dynamic_state_block_pool
);
417 anv_block_pool_finish(&device
->instruction_block_pool
);
418 anv_block_pool_finish(&device
->surface_state_block_pool
);
422 if (device
->aub_writer
)
423 anv_aub_writer_destroy(device
->aub_writer
);
425 anv_device_free(device
, device
);
430 VkResult
anv_GetGlobalExtensionInfo(
431 VkExtensionInfoType infoType
,
432 uint32_t extensionIndex
,
436 static const VkExtensionProperties extensions
[] = {
438 .extName
= "VK_WSI_LunarG",
442 uint32_t count
= ARRAY_SIZE(extensions
);
445 case VK_EXTENSION_INFO_TYPE_COUNT
:
446 memcpy(pData
, &count
, sizeof(count
));
447 *pDataSize
= sizeof(count
);
450 case VK_EXTENSION_INFO_TYPE_PROPERTIES
:
451 if (extensionIndex
>= count
)
452 return vk_error(VK_ERROR_INVALID_EXTENSION
);
454 memcpy(pData
, &extensions
[extensionIndex
], sizeof(extensions
[0]));
455 *pDataSize
= sizeof(extensions
[0]);
459 return VK_UNSUPPORTED
;
463 VkResult
anv_GetPhysicalDeviceExtensionInfo(
464 VkPhysicalDevice physicalDevice
,
465 VkExtensionInfoType infoType
,
466 uint32_t extensionIndex
,
473 case VK_EXTENSION_INFO_TYPE_COUNT
:
482 case VK_EXTENSION_INFO_TYPE_PROPERTIES
:
483 return vk_error(VK_ERROR_INVALID_EXTENSION
);
486 return VK_UNSUPPORTED
;
490 VkResult
anv_EnumerateLayers(
491 VkPhysicalDevice physicalDevice
,
492 size_t maxStringSize
,
494 char* const* pOutLayers
,
502 VkResult
anv_GetDeviceQueue(
504 uint32_t queueNodeIndex
,
508 struct anv_device
*device
= (struct anv_device
*) _device
;
509 struct anv_queue
*queue
;
511 /* FIXME: Should allocate these at device create time. */
513 queue
= anv_device_alloc(device
, sizeof(*queue
), 8,
514 VK_SYSTEM_ALLOC_TYPE_API_OBJECT
);
516 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
518 queue
->device
= device
;
519 queue
->pool
= &device
->surface_state_pool
;
521 queue
->completed_serial
= anv_state_pool_alloc(queue
->pool
, 4, 4);
522 *(uint32_t *)queue
->completed_serial
.map
= 0;
523 queue
->next_serial
= 1;
525 *pQueue
= (VkQueue
) queue
;
531 anv_reloc_list_init(struct anv_reloc_list
*list
, struct anv_device
*device
)
533 list
->num_relocs
= 0;
534 list
->array_length
= 256;
536 anv_device_alloc(device
, list
->array_length
* sizeof(*list
->relocs
), 8,
537 VK_SYSTEM_ALLOC_TYPE_INTERNAL
);
539 if (list
->relocs
== NULL
)
540 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
543 anv_device_alloc(device
, list
->array_length
* sizeof(*list
->reloc_bos
), 8,
544 VK_SYSTEM_ALLOC_TYPE_INTERNAL
);
546 if (list
->relocs
== NULL
) {
547 anv_device_free(device
, list
->relocs
);
548 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
555 anv_reloc_list_finish(struct anv_reloc_list
*list
, struct anv_device
*device
)
557 anv_device_free(device
, list
->relocs
);
558 anv_device_free(device
, list
->reloc_bos
);
562 anv_reloc_list_grow(struct anv_reloc_list
*list
, struct anv_device
*device
,
563 size_t num_additional_relocs
)
565 if (list
->num_relocs
+ num_additional_relocs
<= list
->array_length
)
568 size_t new_length
= list
->array_length
* 2;
569 while (new_length
< list
->num_relocs
+ num_additional_relocs
)
572 struct drm_i915_gem_relocation_entry
*new_relocs
=
573 anv_device_alloc(device
, new_length
* sizeof(*list
->relocs
), 8,
574 VK_SYSTEM_ALLOC_TYPE_INTERNAL
);
575 if (new_relocs
== NULL
)
576 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
578 struct anv_bo
**new_reloc_bos
=
579 anv_device_alloc(device
, new_length
* sizeof(*list
->reloc_bos
), 8,
580 VK_SYSTEM_ALLOC_TYPE_INTERNAL
);
581 if (new_relocs
== NULL
) {
582 anv_device_free(device
, new_relocs
);
583 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
586 memcpy(new_relocs
, list
->relocs
, list
->num_relocs
* sizeof(*list
->relocs
));
587 memcpy(new_reloc_bos
, list
->reloc_bos
,
588 list
->num_relocs
* sizeof(*list
->reloc_bos
));
590 anv_device_free(device
, list
->relocs
);
591 anv_device_free(device
, list
->reloc_bos
);
593 list
->relocs
= new_relocs
;
594 list
->reloc_bos
= new_reloc_bos
;
600 anv_batch_bo_create(struct anv_device
*device
, struct anv_batch_bo
**bbo_out
)
604 struct anv_batch_bo
*bbo
=
605 anv_device_alloc(device
, sizeof(*bbo
), 8, VK_SYSTEM_ALLOC_TYPE_INTERNAL
);
607 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
610 bbo
->prev_batch_bo
= NULL
;
612 result
= anv_bo_pool_alloc(&device
->batch_bo_pool
, &bbo
->bo
);
613 if (result
!= VK_SUCCESS
) {
614 anv_device_free(device
, bbo
);
624 anv_batch_bo_start(struct anv_batch_bo
*bbo
, struct anv_batch
*batch
,
625 size_t batch_padding
)
627 batch
->next
= batch
->start
= bbo
->bo
.map
;
628 batch
->end
= bbo
->bo
.map
+ bbo
->bo
.size
- batch_padding
;
629 bbo
->first_reloc
= batch
->relocs
.num_relocs
;
633 anv_batch_bo_finish(struct anv_batch_bo
*bbo
, struct anv_batch
*batch
)
635 assert(batch
->start
== bbo
->bo
.map
);
636 bbo
->length
= batch
->next
- batch
->start
;
637 bbo
->num_relocs
= batch
->relocs
.num_relocs
- bbo
->first_reloc
;
641 anv_batch_bo_destroy(struct anv_batch_bo
*bbo
, struct anv_device
*device
)
643 anv_bo_pool_free(&device
->batch_bo_pool
, &bbo
->bo
);
644 anv_device_free(device
, bbo
);
648 anv_batch_emit_dwords(struct anv_batch
*batch
, int num_dwords
)
650 if (batch
->next
+ num_dwords
* 4 > batch
->end
)
651 batch
->extend_cb(batch
, batch
->user_data
);
653 void *p
= batch
->next
;
655 batch
->next
+= num_dwords
* 4;
656 assert(batch
->next
<= batch
->end
);
662 anv_reloc_list_append(struct anv_reloc_list
*list
, struct anv_device
*device
,
663 struct anv_reloc_list
*other
, uint32_t offset
)
665 anv_reloc_list_grow(list
, device
, other
->num_relocs
);
666 /* TODO: Handle failure */
668 memcpy(&list
->relocs
[list
->num_relocs
], &other
->relocs
[0],
669 other
->num_relocs
* sizeof(other
->relocs
[0]));
670 memcpy(&list
->reloc_bos
[list
->num_relocs
], &other
->reloc_bos
[0],
671 other
->num_relocs
* sizeof(other
->reloc_bos
[0]));
673 for (uint32_t i
= 0; i
< other
->num_relocs
; i
++)
674 list
->relocs
[i
+ list
->num_relocs
].offset
+= offset
;
676 list
->num_relocs
+= other
->num_relocs
;
680 anv_reloc_list_add(struct anv_reloc_list
*list
, struct anv_device
*device
,
681 uint32_t offset
, struct anv_bo
*target_bo
, uint32_t delta
)
683 struct drm_i915_gem_relocation_entry
*entry
;
686 anv_reloc_list_grow(list
, device
, 1);
687 /* TODO: Handle failure */
689 /* XXX: Can we use I915_EXEC_HANDLE_LUT? */
690 index
= list
->num_relocs
++;
691 list
->reloc_bos
[index
] = target_bo
;
692 entry
= &list
->relocs
[index
];
693 entry
->target_handle
= target_bo
->gem_handle
;
694 entry
->delta
= delta
;
695 entry
->offset
= offset
;
696 entry
->presumed_offset
= target_bo
->offset
;
697 entry
->read_domains
= 0;
698 entry
->write_domain
= 0;
700 return target_bo
->offset
+ delta
;
704 anv_batch_emit_batch(struct anv_batch
*batch
, struct anv_batch
*other
)
706 uint32_t size
, offset
;
708 size
= other
->next
- other
->start
;
709 assert(size
% 4 == 0);
711 if (batch
->next
+ size
> batch
->end
)
712 batch
->extend_cb(batch
, batch
->user_data
);
714 assert(batch
->next
+ size
<= batch
->end
);
716 memcpy(batch
->next
, other
->start
, size
);
718 offset
= batch
->next
- batch
->start
;
719 anv_reloc_list_append(&batch
->relocs
, batch
->device
,
720 &other
->relocs
, offset
);
726 anv_batch_emit_reloc(struct anv_batch
*batch
,
727 void *location
, struct anv_bo
*bo
, uint32_t delta
)
729 return anv_reloc_list_add(&batch
->relocs
, batch
->device
,
730 location
- batch
->start
, bo
, delta
);
733 VkResult
anv_QueueSubmit(
735 uint32_t cmdBufferCount
,
736 const VkCmdBuffer
* pCmdBuffers
,
739 struct anv_queue
*queue
= (struct anv_queue
*) _queue
;
740 struct anv_device
*device
= queue
->device
;
741 struct anv_fence
*fence
= (struct anv_fence
*) _fence
;
744 for (uint32_t i
= 0; i
< cmdBufferCount
; i
++) {
745 struct anv_cmd_buffer
*cmd_buffer
=
746 (struct anv_cmd_buffer
*) pCmdBuffers
[i
];
748 if (device
->dump_aub
)
749 anv_cmd_buffer_dump(cmd_buffer
);
751 if (!device
->no_hw
) {
752 ret
= anv_gem_execbuffer(device
, &cmd_buffer
->execbuf
);
754 return vk_error(VK_ERROR_UNKNOWN
);
757 ret
= anv_gem_execbuffer(device
, &fence
->execbuf
);
759 return vk_error(VK_ERROR_UNKNOWN
);
762 for (uint32_t i
= 0; i
< cmd_buffer
->bo_count
; i
++)
763 cmd_buffer
->exec2_bos
[i
]->offset
= cmd_buffer
->exec2_objects
[i
].offset
;
765 *(uint32_t *)queue
->completed_serial
.map
= cmd_buffer
->serial
;
772 VkResult
anv_QueueAddMemReferences(
775 const VkDeviceMemory
* pMems
)
780 VkResult
anv_QueueRemoveMemReferences(
783 const VkDeviceMemory
* pMems
)
788 VkResult
anv_QueueWaitIdle(
791 struct anv_queue
*queue
= (struct anv_queue
*) _queue
;
793 return vkDeviceWaitIdle((VkDevice
) queue
->device
);
796 VkResult
anv_DeviceWaitIdle(
799 struct anv_device
*device
= (struct anv_device
*) _device
;
800 struct anv_state state
;
801 struct anv_batch batch
;
802 struct drm_i915_gem_execbuffer2 execbuf
;
803 struct drm_i915_gem_exec_object2 exec2_objects
[1];
804 struct anv_bo
*bo
= NULL
;
809 state
= anv_state_pool_alloc(&device
->dynamic_state_pool
, 32, 32);
810 bo
= &device
->dynamic_state_pool
.block_pool
->bo
;
811 batch
.start
= batch
.next
= state
.map
;
812 batch
.end
= state
.map
+ 32;
813 anv_batch_emit(&batch
, GEN8_MI_BATCH_BUFFER_END
);
814 anv_batch_emit(&batch
, GEN8_MI_NOOP
);
816 exec2_objects
[0].handle
= bo
->gem_handle
;
817 exec2_objects
[0].relocation_count
= 0;
818 exec2_objects
[0].relocs_ptr
= 0;
819 exec2_objects
[0].alignment
= 0;
820 exec2_objects
[0].offset
= bo
->offset
;
821 exec2_objects
[0].flags
= 0;
822 exec2_objects
[0].rsvd1
= 0;
823 exec2_objects
[0].rsvd2
= 0;
825 execbuf
.buffers_ptr
= (uintptr_t) exec2_objects
;
826 execbuf
.buffer_count
= 1;
827 execbuf
.batch_start_offset
= state
.offset
;
828 execbuf
.batch_len
= batch
.next
- state
.map
;
829 execbuf
.cliprects_ptr
= 0;
830 execbuf
.num_cliprects
= 0;
835 I915_EXEC_HANDLE_LUT
| I915_EXEC_NO_RELOC
| I915_EXEC_RENDER
;
836 execbuf
.rsvd1
= device
->context_id
;
839 if (!device
->no_hw
) {
840 ret
= anv_gem_execbuffer(device
, &execbuf
);
842 result
= vk_error(VK_ERROR_UNKNOWN
);
847 ret
= anv_gem_wait(device
, bo
->gem_handle
, &timeout
);
849 result
= vk_error(VK_ERROR_UNKNOWN
);
854 anv_state_pool_free(&device
->dynamic_state_pool
, state
);
859 anv_state_pool_free(&device
->dynamic_state_pool
, state
);
865 anv_device_alloc(struct anv_device
* device
,
868 VkSystemAllocType allocType
)
870 return device
->instance
->pfnAlloc(device
->instance
->pAllocUserData
,
877 anv_device_free(struct anv_device
* device
,
880 return device
->instance
->pfnFree(device
->instance
->pAllocUserData
,
885 anv_bo_init_new(struct anv_bo
*bo
, struct anv_device
*device
, uint64_t size
)
887 bo
->gem_handle
= anv_gem_create(device
, size
);
889 return vk_error(VK_ERROR_OUT_OF_DEVICE_MEMORY
);
899 VkResult
anv_AllocMemory(
901 const VkMemoryAllocInfo
* pAllocInfo
,
902 VkDeviceMemory
* pMem
)
904 struct anv_device
*device
= (struct anv_device
*) _device
;
905 struct anv_device_memory
*mem
;
908 assert(pAllocInfo
->sType
== VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO
);
910 mem
= anv_device_alloc(device
, sizeof(*mem
), 8,
911 VK_SYSTEM_ALLOC_TYPE_API_OBJECT
);
913 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
915 result
= anv_bo_init_new(&mem
->bo
, device
, pAllocInfo
->allocationSize
);
916 if (result
!= VK_SUCCESS
)
919 *pMem
= (VkDeviceMemory
) mem
;
924 anv_device_free(device
, mem
);
929 VkResult
anv_FreeMemory(
933 struct anv_device
*device
= (struct anv_device
*) _device
;
934 struct anv_device_memory
*mem
= (struct anv_device_memory
*) _mem
;
937 anv_gem_munmap(mem
->bo
.map
, mem
->bo
.size
);
939 if (mem
->bo
.gem_handle
!= 0)
940 anv_gem_close(device
, mem
->bo
.gem_handle
);
942 anv_device_free(device
, mem
);
947 VkResult
anv_SetMemoryPriority(
950 VkMemoryPriority priority
)
955 VkResult
anv_MapMemory(
960 VkMemoryMapFlags flags
,
963 struct anv_device
*device
= (struct anv_device
*) _device
;
964 struct anv_device_memory
*mem
= (struct anv_device_memory
*) _mem
;
966 /* FIXME: Is this supposed to be thread safe? Since vkUnmapMemory() only
967 * takes a VkDeviceMemory pointer, it seems like only one map of the memory
968 * at a time is valid. We could just mmap up front and return an offset
969 * pointer here, but that may exhaust virtual memory on 32 bit
972 mem
->map
= anv_gem_mmap(device
, mem
->bo
.gem_handle
, offset
, size
);
973 mem
->map_size
= size
;
980 VkResult
anv_UnmapMemory(
984 struct anv_device_memory
*mem
= (struct anv_device_memory
*) _mem
;
986 anv_gem_munmap(mem
->map
, mem
->map_size
);
991 VkResult
anv_FlushMappedMemory(
997 /* clflush here for !llc platforms */
1002 VkResult
anv_PinSystemMemory(
1004 const void* pSysMem
,
1006 VkDeviceMemory
* pMem
)
1011 VkResult
anv_GetMultiDeviceCompatibility(
1012 VkPhysicalDevice physicalDevice0
,
1013 VkPhysicalDevice physicalDevice1
,
1014 VkPhysicalDeviceCompatibilityInfo
* pInfo
)
1016 return VK_UNSUPPORTED
;
1019 VkResult
anv_OpenSharedMemory(
1021 const VkMemoryOpenInfo
* pOpenInfo
,
1022 VkDeviceMemory
* pMem
)
1024 return VK_UNSUPPORTED
;
1027 VkResult
anv_OpenSharedSemaphore(
1029 const VkSemaphoreOpenInfo
* pOpenInfo
,
1030 VkSemaphore
* pSemaphore
)
1032 return VK_UNSUPPORTED
;
1035 VkResult
anv_OpenPeerMemory(
1037 const VkPeerMemoryOpenInfo
* pOpenInfo
,
1038 VkDeviceMemory
* pMem
)
1040 return VK_UNSUPPORTED
;
1043 VkResult
anv_OpenPeerImage(
1045 const VkPeerImageOpenInfo
* pOpenInfo
,
1047 VkDeviceMemory
* pMem
)
1049 return VK_UNSUPPORTED
;
1052 VkResult
anv_DestroyObject(
1054 VkObjectType objType
,
1057 struct anv_device
*device
= (struct anv_device
*) _device
;
1058 struct anv_object
*object
= (struct anv_object
*) _object
;
1061 case VK_OBJECT_TYPE_INSTANCE
:
1062 return anv_DestroyInstance((VkInstance
) _object
);
1064 case VK_OBJECT_TYPE_PHYSICAL_DEVICE
:
1065 /* We don't want to actually destroy physical devices */
1068 case VK_OBJECT_TYPE_DEVICE
:
1069 assert(_device
== (VkDevice
) _object
);
1070 return anv_DestroyDevice((VkDevice
) _object
);
1072 case VK_OBJECT_TYPE_QUEUE
:
1076 case VK_OBJECT_TYPE_DEVICE_MEMORY
:
1077 return anv_FreeMemory(_device
, (VkDeviceMemory
) _object
);
1079 case VK_OBJECT_TYPE_DESCRIPTOR_POOL
:
1080 /* These are just dummys anyway, so we don't need to destroy them */
1083 case VK_OBJECT_TYPE_BUFFER
:
1084 case VK_OBJECT_TYPE_IMAGE
:
1085 case VK_OBJECT_TYPE_DEPTH_STENCIL_VIEW
:
1086 case VK_OBJECT_TYPE_SHADER
:
1087 case VK_OBJECT_TYPE_PIPELINE_LAYOUT
:
1088 case VK_OBJECT_TYPE_SAMPLER
:
1089 case VK_OBJECT_TYPE_DESCRIPTOR_SET
:
1090 case VK_OBJECT_TYPE_DESCRIPTOR_SET_LAYOUT
:
1091 case VK_OBJECT_TYPE_DYNAMIC_RS_STATE
:
1092 case VK_OBJECT_TYPE_DYNAMIC_CB_STATE
:
1093 case VK_OBJECT_TYPE_DYNAMIC_DS_STATE
:
1094 case VK_OBJECT_TYPE_RENDER_PASS
:
1095 /* These are trivially destroyable */
1096 anv_device_free(device
, (void *) _object
);
1099 case VK_OBJECT_TYPE_COMMAND_BUFFER
:
1100 case VK_OBJECT_TYPE_PIPELINE
:
1101 case VK_OBJECT_TYPE_DYNAMIC_VP_STATE
:
1102 case VK_OBJECT_TYPE_FENCE
:
1103 case VK_OBJECT_TYPE_QUERY_POOL
:
1104 case VK_OBJECT_TYPE_FRAMEBUFFER
:
1105 case VK_OBJECT_TYPE_BUFFER_VIEW
:
1106 case VK_OBJECT_TYPE_IMAGE_VIEW
:
1107 case VK_OBJECT_TYPE_COLOR_ATTACHMENT_VIEW
:
1108 (object
->destructor
)(device
, object
, objType
);
1111 case VK_OBJECT_TYPE_SEMAPHORE
:
1112 case VK_OBJECT_TYPE_EVENT
:
1113 stub_return(VK_UNSUPPORTED
);
1116 unreachable("Invalid object type");
1121 fill_memory_requirements(
1122 VkObjectType objType
,
1124 VkMemoryRequirements
* memory_requirements
)
1126 struct anv_buffer
*buffer
;
1127 struct anv_image
*image
;
1129 memory_requirements
->memPropsAllowed
=
1130 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT
|
1131 VK_MEMORY_PROPERTY_HOST_DEVICE_COHERENT_BIT
|
1132 /* VK_MEMORY_PROPERTY_HOST_UNCACHED_BIT | */
1133 VK_MEMORY_PROPERTY_HOST_WRITE_COMBINED_BIT
|
1134 VK_MEMORY_PROPERTY_PREFER_HOST_LOCAL
|
1135 VK_MEMORY_PROPERTY_SHAREABLE_BIT
;
1137 memory_requirements
->memPropsRequired
= 0;
1140 case VK_OBJECT_TYPE_BUFFER
:
1141 buffer
= (struct anv_buffer
*) object
;
1142 memory_requirements
->size
= buffer
->size
;
1143 memory_requirements
->alignment
= 16;
1145 case VK_OBJECT_TYPE_IMAGE
:
1146 image
= (struct anv_image
*) object
;
1147 memory_requirements
->size
= image
->size
;
1148 memory_requirements
->alignment
= image
->alignment
;
1151 memory_requirements
->size
= 0;
1157 get_allocation_count(VkObjectType objType
)
1160 case VK_OBJECT_TYPE_BUFFER
:
1161 case VK_OBJECT_TYPE_IMAGE
:
1168 VkResult
anv_GetObjectInfo(
1170 VkObjectType objType
,
1172 VkObjectInfoType infoType
,
1176 VkMemoryRequirements memory_requirements
;
1180 case VK_OBJECT_INFO_TYPE_MEMORY_REQUIREMENTS
:
1181 *pDataSize
= sizeof(memory_requirements
);
1185 fill_memory_requirements(objType
, object
, pData
);
1188 case VK_OBJECT_INFO_TYPE_MEMORY_ALLOCATION_COUNT
:
1189 *pDataSize
= sizeof(count
);
1194 *count
= get_allocation_count(objType
);
1198 return VK_UNSUPPORTED
;
1203 VkResult
anv_QueueBindObjectMemory(
1205 VkObjectType objType
,
1207 uint32_t allocationIdx
,
1208 VkDeviceMemory _mem
,
1209 VkDeviceSize memOffset
)
1211 struct anv_buffer
*buffer
;
1212 struct anv_image
*image
;
1213 struct anv_device_memory
*mem
= (struct anv_device_memory
*) _mem
;
1216 case VK_OBJECT_TYPE_BUFFER
:
1217 buffer
= (struct anv_buffer
*) object
;
1218 buffer
->bo
= &mem
->bo
;
1219 buffer
->offset
= memOffset
;
1221 case VK_OBJECT_TYPE_IMAGE
:
1222 image
= (struct anv_image
*) object
;
1223 image
->bo
= &mem
->bo
;
1224 image
->offset
= memOffset
;
1233 VkResult
anv_QueueBindObjectMemoryRange(
1235 VkObjectType objType
,
1237 uint32_t allocationIdx
,
1238 VkDeviceSize rangeOffset
,
1239 VkDeviceSize rangeSize
,
1241 VkDeviceSize memOffset
)
1243 stub_return(VK_UNSUPPORTED
);
1246 VkResult
anv_QueueBindImageMemoryRange(
1249 uint32_t allocationIdx
,
1250 const VkImageMemoryBindInfo
* pBindInfo
,
1252 VkDeviceSize memOffset
)
1254 stub_return(VK_UNSUPPORTED
);
1258 anv_fence_destroy(struct anv_device
*device
,
1259 struct anv_object
*object
,
1260 VkObjectType obj_type
)
1262 struct anv_fence
*fence
= (struct anv_fence
*) object
;
1264 assert(obj_type
== VK_OBJECT_TYPE_FENCE
);
1266 anv_gem_munmap(fence
->bo
.map
, fence
->bo
.size
);
1267 anv_gem_close(device
, fence
->bo
.gem_handle
);
1268 anv_device_free(device
, fence
);
1271 VkResult
anv_CreateFence(
1273 const VkFenceCreateInfo
* pCreateInfo
,
1276 struct anv_device
*device
= (struct anv_device
*) _device
;
1277 struct anv_fence
*fence
;
1278 struct anv_batch batch
;
1281 const uint32_t fence_size
= 128;
1283 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_FENCE_CREATE_INFO
);
1285 fence
= anv_device_alloc(device
, sizeof(*fence
), 8,
1286 VK_SYSTEM_ALLOC_TYPE_API_OBJECT
);
1288 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
1290 result
= anv_bo_init_new(&fence
->bo
, device
, fence_size
);
1291 if (result
!= VK_SUCCESS
)
1294 fence
->base
.destructor
= anv_fence_destroy
;
1297 anv_gem_mmap(device
, fence
->bo
.gem_handle
, 0, fence
->bo
.size
);
1298 batch
.next
= batch
.start
= fence
->bo
.map
;
1299 batch
.end
= fence
->bo
.map
+ fence
->bo
.size
;
1300 anv_batch_emit(&batch
, GEN8_MI_BATCH_BUFFER_END
);
1301 anv_batch_emit(&batch
, GEN8_MI_NOOP
);
1303 fence
->exec2_objects
[0].handle
= fence
->bo
.gem_handle
;
1304 fence
->exec2_objects
[0].relocation_count
= 0;
1305 fence
->exec2_objects
[0].relocs_ptr
= 0;
1306 fence
->exec2_objects
[0].alignment
= 0;
1307 fence
->exec2_objects
[0].offset
= fence
->bo
.offset
;
1308 fence
->exec2_objects
[0].flags
= 0;
1309 fence
->exec2_objects
[0].rsvd1
= 0;
1310 fence
->exec2_objects
[0].rsvd2
= 0;
1312 fence
->execbuf
.buffers_ptr
= (uintptr_t) fence
->exec2_objects
;
1313 fence
->execbuf
.buffer_count
= 1;
1314 fence
->execbuf
.batch_start_offset
= 0;
1315 fence
->execbuf
.batch_len
= batch
.next
- fence
->bo
.map
;
1316 fence
->execbuf
.cliprects_ptr
= 0;
1317 fence
->execbuf
.num_cliprects
= 0;
1318 fence
->execbuf
.DR1
= 0;
1319 fence
->execbuf
.DR4
= 0;
1321 fence
->execbuf
.flags
=
1322 I915_EXEC_HANDLE_LUT
| I915_EXEC_NO_RELOC
| I915_EXEC_RENDER
;
1323 fence
->execbuf
.rsvd1
= device
->context_id
;
1324 fence
->execbuf
.rsvd2
= 0;
1326 *pFence
= (VkFence
) fence
;
1331 anv_device_free(device
, fence
);
1336 VkResult
anv_ResetFences(
1338 uint32_t fenceCount
,
1341 struct anv_fence
**fences
= (struct anv_fence
**) pFences
;
1343 for (uint32_t i
= 0; i
< fenceCount
; i
++)
1344 fences
[i
]->ready
= false;
1349 VkResult
anv_GetFenceStatus(
1353 struct anv_device
*device
= (struct anv_device
*) _device
;
1354 struct anv_fence
*fence
= (struct anv_fence
*) _fence
;
1361 ret
= anv_gem_wait(device
, fence
->bo
.gem_handle
, &t
);
1363 fence
->ready
= true;
1367 return VK_NOT_READY
;
1370 VkResult
anv_WaitForFences(
1372 uint32_t fenceCount
,
1373 const VkFence
* pFences
,
1377 struct anv_device
*device
= (struct anv_device
*) _device
;
1378 struct anv_fence
**fences
= (struct anv_fence
**) pFences
;
1379 int64_t t
= timeout
;
1382 /* FIXME: handle !waitAll */
1384 for (uint32_t i
= 0; i
< fenceCount
; i
++) {
1385 ret
= anv_gem_wait(device
, fences
[i
]->bo
.gem_handle
, &t
);
1386 if (ret
== -1 && errno
== ETIME
)
1389 return vk_error(VK_ERROR_UNKNOWN
);
1395 // Queue semaphore functions
1397 VkResult
anv_CreateSemaphore(
1399 const VkSemaphoreCreateInfo
* pCreateInfo
,
1400 VkSemaphore
* pSemaphore
)
1402 stub_return(VK_UNSUPPORTED
);
1405 VkResult
anv_QueueSignalSemaphore(
1407 VkSemaphore semaphore
)
1409 stub_return(VK_UNSUPPORTED
);
1412 VkResult
anv_QueueWaitSemaphore(
1414 VkSemaphore semaphore
)
1416 stub_return(VK_UNSUPPORTED
);
1421 VkResult
anv_CreateEvent(
1423 const VkEventCreateInfo
* pCreateInfo
,
1426 stub_return(VK_UNSUPPORTED
);
1429 VkResult
anv_GetEventStatus(
1433 stub_return(VK_UNSUPPORTED
);
1436 VkResult
anv_SetEvent(
1440 stub_return(VK_UNSUPPORTED
);
1443 VkResult
anv_ResetEvent(
1447 stub_return(VK_UNSUPPORTED
);
1452 VkResult
anv_CreateBuffer(
1454 const VkBufferCreateInfo
* pCreateInfo
,
1457 struct anv_device
*device
= (struct anv_device
*) _device
;
1458 struct anv_buffer
*buffer
;
1460 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO
);
1462 buffer
= anv_device_alloc(device
, sizeof(*buffer
), 8,
1463 VK_SYSTEM_ALLOC_TYPE_API_OBJECT
);
1465 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
1467 buffer
->size
= pCreateInfo
->size
;
1471 *pBuffer
= (VkBuffer
) buffer
;
1476 // Buffer view functions
1479 fill_buffer_surface_state(void *state
, VkFormat format
,
1480 uint32_t offset
, uint32_t range
)
1482 const struct anv_format
*info
;
1484 info
= anv_format_for_vk_format(format
);
1485 /* This assumes RGBA float format. */
1486 uint32_t stride
= 4;
1487 uint32_t num_elements
= range
/ stride
;
1489 struct GEN8_RENDER_SURFACE_STATE surface_state
= {
1490 .SurfaceType
= SURFTYPE_BUFFER
,
1491 .SurfaceArray
= false,
1492 .SurfaceFormat
= info
->format
,
1493 .SurfaceVerticalAlignment
= VALIGN4
,
1494 .SurfaceHorizontalAlignment
= HALIGN4
,
1496 .VerticalLineStride
= 0,
1497 .VerticalLineStrideOffset
= 0,
1498 .SamplerL2BypassModeDisable
= true,
1499 .RenderCacheReadWriteMode
= WriteOnlyCache
,
1500 .MemoryObjectControlState
= GEN8_MOCS
,
1503 .Height
= (num_elements
>> 7) & 0x3fff,
1504 .Width
= num_elements
& 0x7f,
1505 .Depth
= (num_elements
>> 21) & 0x3f,
1506 .SurfacePitch
= stride
- 1,
1507 .MinimumArrayElement
= 0,
1508 .NumberofMultisamples
= MULTISAMPLECOUNT_1
,
1513 .AuxiliarySurfaceMode
= AUX_NONE
,
1515 .GreenClearColor
= 0,
1516 .BlueClearColor
= 0,
1517 .AlphaClearColor
= 0,
1518 .ShaderChannelSelectRed
= SCS_RED
,
1519 .ShaderChannelSelectGreen
= SCS_GREEN
,
1520 .ShaderChannelSelectBlue
= SCS_BLUE
,
1521 .ShaderChannelSelectAlpha
= SCS_ALPHA
,
1522 .ResourceMinLOD
= 0,
1523 /* FIXME: We assume that the image must be bound at this time. */
1524 .SurfaceBaseAddress
= { NULL
, offset
},
1527 GEN8_RENDER_SURFACE_STATE_pack(NULL
, state
, &surface_state
);
1530 VkResult
anv_CreateBufferView(
1532 const VkBufferViewCreateInfo
* pCreateInfo
,
1533 VkBufferView
* pView
)
1535 struct anv_device
*device
= (struct anv_device
*) _device
;
1536 struct anv_buffer
*buffer
= (struct anv_buffer
*) pCreateInfo
->buffer
;
1537 struct anv_surface_view
*view
;
1539 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO
);
1541 view
= anv_device_alloc(device
, sizeof(*view
), 8,
1542 VK_SYSTEM_ALLOC_TYPE_API_OBJECT
);
1544 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
1546 view
->base
.destructor
= anv_surface_view_destroy
;
1548 view
->bo
= buffer
->bo
;
1549 view
->offset
= buffer
->offset
+ pCreateInfo
->offset
;
1550 view
->surface_state
=
1551 anv_state_pool_alloc(&device
->surface_state_pool
, 64, 64);
1552 view
->format
= pCreateInfo
->format
;
1553 view
->range
= pCreateInfo
->range
;
1555 fill_buffer_surface_state(view
->surface_state
.map
,
1556 pCreateInfo
->format
, view
->offset
, pCreateInfo
->range
);
1558 *pView
= (VkBufferView
) view
;
1563 // Sampler functions
1565 VkResult
anv_CreateSampler(
1567 const VkSamplerCreateInfo
* pCreateInfo
,
1568 VkSampler
* pSampler
)
1570 struct anv_device
*device
= (struct anv_device
*) _device
;
1571 struct anv_sampler
*sampler
;
1572 uint32_t mag_filter
, min_filter
, max_anisotropy
;
1574 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO
);
1576 sampler
= anv_device_alloc(device
, sizeof(*sampler
), 8,
1577 VK_SYSTEM_ALLOC_TYPE_API_OBJECT
);
1579 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
1581 static const uint32_t vk_to_gen_tex_filter
[] = {
1582 [VK_TEX_FILTER_NEAREST
] = MAPFILTER_NEAREST
,
1583 [VK_TEX_FILTER_LINEAR
] = MAPFILTER_LINEAR
1586 static const uint32_t vk_to_gen_mipmap_mode
[] = {
1587 [VK_TEX_MIPMAP_MODE_BASE
] = MIPFILTER_NONE
,
1588 [VK_TEX_MIPMAP_MODE_NEAREST
] = MIPFILTER_NEAREST
,
1589 [VK_TEX_MIPMAP_MODE_LINEAR
] = MIPFILTER_LINEAR
1592 static const uint32_t vk_to_gen_tex_address
[] = {
1593 [VK_TEX_ADDRESS_WRAP
] = TCM_WRAP
,
1594 [VK_TEX_ADDRESS_MIRROR
] = TCM_MIRROR
,
1595 [VK_TEX_ADDRESS_CLAMP
] = TCM_CLAMP
,
1596 [VK_TEX_ADDRESS_MIRROR_ONCE
] = TCM_MIRROR_ONCE
,
1597 [VK_TEX_ADDRESS_CLAMP_BORDER
] = TCM_CLAMP_BORDER
,
1600 static const uint32_t vk_to_gen_compare_op
[] = {
1601 [VK_COMPARE_OP_NEVER
] = PREFILTEROPNEVER
,
1602 [VK_COMPARE_OP_LESS
] = PREFILTEROPLESS
,
1603 [VK_COMPARE_OP_EQUAL
] = PREFILTEROPEQUAL
,
1604 [VK_COMPARE_OP_LESS_EQUAL
] = PREFILTEROPLEQUAL
,
1605 [VK_COMPARE_OP_GREATER
] = PREFILTEROPGREATER
,
1606 [VK_COMPARE_OP_NOT_EQUAL
] = PREFILTEROPNOTEQUAL
,
1607 [VK_COMPARE_OP_GREATER_EQUAL
] = PREFILTEROPGEQUAL
,
1608 [VK_COMPARE_OP_ALWAYS
] = PREFILTEROPALWAYS
,
1611 if (pCreateInfo
->maxAnisotropy
> 1) {
1612 mag_filter
= MAPFILTER_ANISOTROPIC
;
1613 min_filter
= MAPFILTER_ANISOTROPIC
;
1614 max_anisotropy
= (pCreateInfo
->maxAnisotropy
- 2) / 2;
1616 mag_filter
= vk_to_gen_tex_filter
[pCreateInfo
->magFilter
];
1617 min_filter
= vk_to_gen_tex_filter
[pCreateInfo
->minFilter
];
1618 max_anisotropy
= RATIO21
;
1621 struct GEN8_SAMPLER_STATE sampler_state
= {
1622 .SamplerDisable
= false,
1623 .TextureBorderColorMode
= DX10OGL
,
1624 .LODPreClampMode
= 0,
1626 .MipModeFilter
= vk_to_gen_mipmap_mode
[pCreateInfo
->mipMode
],
1627 .MagModeFilter
= mag_filter
,
1628 .MinModeFilter
= min_filter
,
1629 .TextureLODBias
= pCreateInfo
->mipLodBias
* 256,
1630 .AnisotropicAlgorithm
= EWAApproximation
,
1631 .MinLOD
= pCreateInfo
->minLod
* 256,
1632 .MaxLOD
= pCreateInfo
->maxLod
* 256,
1633 .ChromaKeyEnable
= 0,
1634 .ChromaKeyIndex
= 0,
1636 .ShadowFunction
= vk_to_gen_compare_op
[pCreateInfo
->compareOp
],
1637 .CubeSurfaceControlMode
= 0,
1639 .IndirectStatePointer
=
1640 device
->float_border_colors
.offset
+
1641 pCreateInfo
->borderColor
* sizeof(float) * 4,
1643 .LODClampMagnificationMode
= MIPNONE
,
1644 .MaximumAnisotropy
= max_anisotropy
,
1645 .RAddressMinFilterRoundingEnable
= 0,
1646 .RAddressMagFilterRoundingEnable
= 0,
1647 .VAddressMinFilterRoundingEnable
= 0,
1648 .VAddressMagFilterRoundingEnable
= 0,
1649 .UAddressMinFilterRoundingEnable
= 0,
1650 .UAddressMagFilterRoundingEnable
= 0,
1651 .TrilinearFilterQuality
= 0,
1652 .NonnormalizedCoordinateEnable
= 0,
1653 .TCXAddressControlMode
= vk_to_gen_tex_address
[pCreateInfo
->addressU
],
1654 .TCYAddressControlMode
= vk_to_gen_tex_address
[pCreateInfo
->addressV
],
1655 .TCZAddressControlMode
= vk_to_gen_tex_address
[pCreateInfo
->addressW
],
1658 GEN8_SAMPLER_STATE_pack(NULL
, sampler
->state
, &sampler_state
);
1660 *pSampler
= (VkSampler
) sampler
;
1665 // Descriptor set functions
1667 VkResult
anv_CreateDescriptorSetLayout(
1669 const VkDescriptorSetLayoutCreateInfo
* pCreateInfo
,
1670 VkDescriptorSetLayout
* pSetLayout
)
1672 struct anv_device
*device
= (struct anv_device
*) _device
;
1673 struct anv_descriptor_set_layout
*set_layout
;
1675 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO
);
1677 uint32_t sampler_count
[VK_NUM_SHADER_STAGE
] = { 0, };
1678 uint32_t surface_count
[VK_NUM_SHADER_STAGE
] = { 0, };
1679 uint32_t num_dynamic_buffers
= 0;
1681 uint32_t stages
= 0;
1684 for (uint32_t i
= 0; i
< pCreateInfo
->count
; i
++) {
1685 switch (pCreateInfo
->pBinding
[i
].descriptorType
) {
1686 case VK_DESCRIPTOR_TYPE_SAMPLER
:
1687 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
:
1688 for_each_bit(s
, pCreateInfo
->pBinding
[i
].stageFlags
)
1689 sampler_count
[s
] += pCreateInfo
->pBinding
[i
].count
;
1695 switch (pCreateInfo
->pBinding
[i
].descriptorType
) {
1696 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
:
1697 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE
:
1698 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE
:
1699 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER
:
1700 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER
:
1701 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER
:
1702 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER
:
1703 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
:
1704 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
:
1705 for_each_bit(s
, pCreateInfo
->pBinding
[i
].stageFlags
)
1706 surface_count
[s
] += pCreateInfo
->pBinding
[i
].count
;
1712 switch (pCreateInfo
->pBinding
[i
].descriptorType
) {
1713 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
:
1714 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
:
1715 num_dynamic_buffers
+= pCreateInfo
->pBinding
[i
].count
;
1721 stages
|= pCreateInfo
->pBinding
[i
].stageFlags
;
1722 count
+= pCreateInfo
->pBinding
[i
].count
;
1725 uint32_t sampler_total
= 0;
1726 uint32_t surface_total
= 0;
1727 for (uint32_t s
= 0; s
< VK_NUM_SHADER_STAGE
; s
++) {
1728 sampler_total
+= sampler_count
[s
];
1729 surface_total
+= surface_count
[s
];
1732 size_t size
= sizeof(*set_layout
) +
1733 (sampler_total
+ surface_total
) * sizeof(set_layout
->entries
[0]);
1734 set_layout
= anv_device_alloc(device
, size
, 8,
1735 VK_SYSTEM_ALLOC_TYPE_API_OBJECT
);
1737 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
1739 set_layout
->num_dynamic_buffers
= num_dynamic_buffers
;
1740 set_layout
->count
= count
;
1741 set_layout
->shader_stages
= stages
;
1743 struct anv_descriptor_slot
*p
= set_layout
->entries
;
1744 struct anv_descriptor_slot
*sampler
[VK_NUM_SHADER_STAGE
];
1745 struct anv_descriptor_slot
*surface
[VK_NUM_SHADER_STAGE
];
1746 for (uint32_t s
= 0; s
< VK_NUM_SHADER_STAGE
; s
++) {
1747 set_layout
->stage
[s
].surface_count
= surface_count
[s
];
1748 set_layout
->stage
[s
].surface_start
= surface
[s
] = p
;
1749 p
+= surface_count
[s
];
1750 set_layout
->stage
[s
].sampler_count
= sampler_count
[s
];
1751 set_layout
->stage
[s
].sampler_start
= sampler
[s
] = p
;
1752 p
+= sampler_count
[s
];
1755 uint32_t descriptor
= 0;
1756 int8_t dynamic_slot
= 0;
1758 for (uint32_t i
= 0; i
< pCreateInfo
->count
; i
++) {
1759 switch (pCreateInfo
->pBinding
[i
].descriptorType
) {
1760 case VK_DESCRIPTOR_TYPE_SAMPLER
:
1761 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
:
1762 for_each_bit(s
, pCreateInfo
->pBinding
[i
].stageFlags
)
1763 for (uint32_t j
= 0; j
< pCreateInfo
->pBinding
[i
].count
; j
++) {
1764 sampler
[s
]->index
= descriptor
+ j
;
1765 sampler
[s
]->dynamic_slot
= -1;
1773 switch (pCreateInfo
->pBinding
[i
].descriptorType
) {
1774 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
:
1775 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
:
1783 switch (pCreateInfo
->pBinding
[i
].descriptorType
) {
1784 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
:
1785 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE
:
1786 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE
:
1787 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER
:
1788 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER
:
1789 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER
:
1790 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER
:
1791 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
:
1792 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
:
1793 for_each_bit(s
, pCreateInfo
->pBinding
[i
].stageFlags
)
1794 for (uint32_t j
= 0; j
< pCreateInfo
->pBinding
[i
].count
; j
++) {
1795 surface
[s
]->index
= descriptor
+ j
;
1797 surface
[s
]->dynamic_slot
= dynamic_slot
+ j
;
1799 surface
[s
]->dynamic_slot
= -1;
1808 dynamic_slot
+= pCreateInfo
->pBinding
[i
].count
;
1810 descriptor
+= pCreateInfo
->pBinding
[i
].count
;
1813 *pSetLayout
= (VkDescriptorSetLayout
) set_layout
;
1818 VkResult
anv_BeginDescriptorPoolUpdate(
1820 VkDescriptorUpdateMode updateMode
)
1825 VkResult
anv_EndDescriptorPoolUpdate(
1832 VkResult
anv_CreateDescriptorPool(
1834 VkDescriptorPoolUsage poolUsage
,
1836 const VkDescriptorPoolCreateInfo
* pCreateInfo
,
1837 VkDescriptorPool
* pDescriptorPool
)
1839 *pDescriptorPool
= 1;
1844 VkResult
anv_ResetDescriptorPool(
1846 VkDescriptorPool descriptorPool
)
1851 VkResult
anv_AllocDescriptorSets(
1853 VkDescriptorPool descriptorPool
,
1854 VkDescriptorSetUsage setUsage
,
1856 const VkDescriptorSetLayout
* pSetLayouts
,
1857 VkDescriptorSet
* pDescriptorSets
,
1860 struct anv_device
*device
= (struct anv_device
*) _device
;
1861 const struct anv_descriptor_set_layout
*layout
;
1862 struct anv_descriptor_set
*set
;
1865 for (uint32_t i
= 0; i
< count
; i
++) {
1866 layout
= (struct anv_descriptor_set_layout
*) pSetLayouts
[i
];
1867 size
= sizeof(*set
) + layout
->count
* sizeof(set
->descriptors
[0]);
1868 set
= anv_device_alloc(device
, size
, 8,
1869 VK_SYSTEM_ALLOC_TYPE_API_OBJECT
);
1872 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
1875 /* Descriptor sets may not be 100% filled out so we need to memset to
1876 * ensure that we can properly detect and handle holes.
1878 memset(set
, 0, size
);
1880 pDescriptorSets
[i
] = (VkDescriptorSet
) set
;
1888 void anv_ClearDescriptorSets(
1890 VkDescriptorPool descriptorPool
,
1892 const VkDescriptorSet
* pDescriptorSets
)
1896 void anv_UpdateDescriptors(
1898 VkDescriptorSet descriptorSet
,
1899 uint32_t updateCount
,
1900 const void** ppUpdateArray
)
1902 struct anv_descriptor_set
*set
= (struct anv_descriptor_set
*) descriptorSet
;
1903 VkUpdateSamplers
*update_samplers
;
1904 VkUpdateSamplerTextures
*update_sampler_textures
;
1905 VkUpdateImages
*update_images
;
1906 VkUpdateBuffers
*update_buffers
;
1907 VkUpdateAsCopy
*update_as_copy
;
1909 for (uint32_t i
= 0; i
< updateCount
; i
++) {
1910 const struct anv_common
*common
= ppUpdateArray
[i
];
1912 switch (common
->sType
) {
1913 case VK_STRUCTURE_TYPE_UPDATE_SAMPLERS
:
1914 update_samplers
= (VkUpdateSamplers
*) common
;
1916 for (uint32_t j
= 0; j
< update_samplers
->count
; j
++) {
1917 set
->descriptors
[update_samplers
->binding
+ j
].sampler
=
1918 (struct anv_sampler
*) update_samplers
->pSamplers
[j
];
1922 case VK_STRUCTURE_TYPE_UPDATE_SAMPLER_TEXTURES
:
1923 /* FIXME: Shouldn't this be *_UPDATE_SAMPLER_IMAGES? */
1924 update_sampler_textures
= (VkUpdateSamplerTextures
*) common
;
1926 for (uint32_t j
= 0; j
< update_sampler_textures
->count
; j
++) {
1927 set
->descriptors
[update_sampler_textures
->binding
+ j
].view
=
1928 (struct anv_surface_view
*)
1929 update_sampler_textures
->pSamplerImageViews
[j
].pImageView
->view
;
1930 set
->descriptors
[update_sampler_textures
->binding
+ j
].sampler
=
1931 (struct anv_sampler
*)
1932 update_sampler_textures
->pSamplerImageViews
[j
].sampler
;
1936 case VK_STRUCTURE_TYPE_UPDATE_IMAGES
:
1937 update_images
= (VkUpdateImages
*) common
;
1939 for (uint32_t j
= 0; j
< update_images
->count
; j
++) {
1940 set
->descriptors
[update_images
->binding
+ j
].view
=
1941 (struct anv_surface_view
*) update_images
->pImageViews
[j
].view
;
1945 case VK_STRUCTURE_TYPE_UPDATE_BUFFERS
:
1946 update_buffers
= (VkUpdateBuffers
*) common
;
1948 for (uint32_t j
= 0; j
< update_buffers
->count
; j
++) {
1949 set
->descriptors
[update_buffers
->binding
+ j
].view
=
1950 (struct anv_surface_view
*) update_buffers
->pBufferViews
[j
].view
;
1952 /* FIXME: descriptor arrays? */
1955 case VK_STRUCTURE_TYPE_UPDATE_AS_COPY
:
1956 update_as_copy
= (VkUpdateAsCopy
*) common
;
1957 (void) update_as_copy
;
1966 // State object functions
1968 static inline int64_t
1969 clamp_int64(int64_t x
, int64_t min
, int64_t max
)
1980 anv_dynamic_vp_state_destroy(struct anv_device
*device
,
1981 struct anv_object
*object
,
1982 VkObjectType obj_type
)
1984 struct anv_dynamic_vp_state
*state
= (void *)object
;
1986 assert(obj_type
== VK_OBJECT_TYPE_DYNAMIC_VP_STATE
);
1988 anv_state_pool_free(&device
->dynamic_state_pool
, state
->sf_clip_vp
);
1989 anv_state_pool_free(&device
->dynamic_state_pool
, state
->cc_vp
);
1990 anv_state_pool_free(&device
->dynamic_state_pool
, state
->scissor
);
1992 anv_device_free(device
, state
);
1995 VkResult
anv_CreateDynamicViewportState(
1997 const VkDynamicVpStateCreateInfo
* pCreateInfo
,
1998 VkDynamicVpState
* pState
)
2000 struct anv_device
*device
= (struct anv_device
*) _device
;
2001 struct anv_dynamic_vp_state
*state
;
2003 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_DYNAMIC_VP_STATE_CREATE_INFO
);
2005 state
= anv_device_alloc(device
, sizeof(*state
), 8,
2006 VK_SYSTEM_ALLOC_TYPE_API_OBJECT
);
2008 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
2010 state
->base
.destructor
= anv_dynamic_vp_state_destroy
;
2012 unsigned count
= pCreateInfo
->viewportAndScissorCount
;
2013 state
->sf_clip_vp
= anv_state_pool_alloc(&device
->dynamic_state_pool
,
2015 state
->cc_vp
= anv_state_pool_alloc(&device
->dynamic_state_pool
,
2017 state
->scissor
= anv_state_pool_alloc(&device
->dynamic_state_pool
,
2020 for (uint32_t i
= 0; i
< pCreateInfo
->viewportAndScissorCount
; i
++) {
2021 const VkViewport
*vp
= &pCreateInfo
->pViewports
[i
];
2022 const VkRect
*s
= &pCreateInfo
->pScissors
[i
];
2024 struct GEN8_SF_CLIP_VIEWPORT sf_clip_viewport
= {
2025 .ViewportMatrixElementm00
= vp
->width
/ 2,
2026 .ViewportMatrixElementm11
= vp
->height
/ 2,
2027 .ViewportMatrixElementm22
= (vp
->maxDepth
- vp
->minDepth
) / 2,
2028 .ViewportMatrixElementm30
= vp
->originX
+ vp
->width
/ 2,
2029 .ViewportMatrixElementm31
= vp
->originY
+ vp
->height
/ 2,
2030 .ViewportMatrixElementm32
= (vp
->maxDepth
+ vp
->minDepth
) / 2,
2031 .XMinClipGuardband
= -1.0f
,
2032 .XMaxClipGuardband
= 1.0f
,
2033 .YMinClipGuardband
= -1.0f
,
2034 .YMaxClipGuardband
= 1.0f
,
2035 .XMinViewPort
= vp
->originX
,
2036 .XMaxViewPort
= vp
->originX
+ vp
->width
- 1,
2037 .YMinViewPort
= vp
->originY
,
2038 .YMaxViewPort
= vp
->originY
+ vp
->height
- 1,
2041 struct GEN8_CC_VIEWPORT cc_viewport
= {
2042 .MinimumDepth
= vp
->minDepth
,
2043 .MaximumDepth
= vp
->maxDepth
2046 /* Since xmax and ymax are inclusive, we have to have xmax < xmin or
2047 * ymax < ymin for empty clips. In case clip x, y, width height are all
2048 * 0, the clamps below produce 0 for xmin, ymin, xmax, ymax, which isn't
2049 * what we want. Just special case empty clips and produce a canonical
2051 static const struct GEN8_SCISSOR_RECT empty_scissor
= {
2052 .ScissorRectangleYMin
= 1,
2053 .ScissorRectangleXMin
= 1,
2054 .ScissorRectangleYMax
= 0,
2055 .ScissorRectangleXMax
= 0
2058 const int max
= 0xffff;
2059 struct GEN8_SCISSOR_RECT scissor
= {
2060 /* Do this math using int64_t so overflow gets clamped correctly. */
2061 .ScissorRectangleYMin
= clamp_int64(s
->offset
.y
, 0, max
),
2062 .ScissorRectangleXMin
= clamp_int64(s
->offset
.x
, 0, max
),
2063 .ScissorRectangleYMax
= clamp_int64((uint64_t) s
->offset
.y
+ s
->extent
.height
- 1, 0, max
),
2064 .ScissorRectangleXMax
= clamp_int64((uint64_t) s
->offset
.x
+ s
->extent
.width
- 1, 0, max
)
2067 GEN8_SF_CLIP_VIEWPORT_pack(NULL
, state
->sf_clip_vp
.map
+ i
* 64, &sf_clip_viewport
);
2068 GEN8_CC_VIEWPORT_pack(NULL
, state
->cc_vp
.map
+ i
* 32, &cc_viewport
);
2070 if (s
->extent
.width
<= 0 || s
->extent
.height
<= 0) {
2071 GEN8_SCISSOR_RECT_pack(NULL
, state
->scissor
.map
+ i
* 32, &empty_scissor
);
2073 GEN8_SCISSOR_RECT_pack(NULL
, state
->scissor
.map
+ i
* 32, &scissor
);
2077 *pState
= (VkDynamicVpState
) state
;
2082 VkResult
anv_CreateDynamicRasterState(
2084 const VkDynamicRsStateCreateInfo
* pCreateInfo
,
2085 VkDynamicRsState
* pState
)
2087 struct anv_device
*device
= (struct anv_device
*) _device
;
2088 struct anv_dynamic_rs_state
*state
;
2090 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_DYNAMIC_RS_STATE_CREATE_INFO
);
2092 state
= anv_device_alloc(device
, sizeof(*state
), 8,
2093 VK_SYSTEM_ALLOC_TYPE_API_OBJECT
);
2095 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
2098 * float pointFadeThreshold;
2099 * // optional (GL45) - Size of point fade threshold
2102 struct GEN8_3DSTATE_SF sf
= {
2103 GEN8_3DSTATE_SF_header
,
2104 .LineWidth
= pCreateInfo
->lineWidth
,
2105 .PointWidth
= pCreateInfo
->pointSize
,
2108 GEN8_3DSTATE_SF_pack(NULL
, state
->state_sf
, &sf
);
2110 bool enable_bias
= pCreateInfo
->depthBias
!= 0.0f
||
2111 pCreateInfo
->slopeScaledDepthBias
!= 0.0f
;
2112 struct GEN8_3DSTATE_RASTER raster
= {
2113 .GlobalDepthOffsetEnableSolid
= enable_bias
,
2114 .GlobalDepthOffsetEnableWireframe
= enable_bias
,
2115 .GlobalDepthOffsetEnablePoint
= enable_bias
,
2116 .GlobalDepthOffsetConstant
= pCreateInfo
->depthBias
,
2117 .GlobalDepthOffsetScale
= pCreateInfo
->slopeScaledDepthBias
,
2118 .GlobalDepthOffsetClamp
= pCreateInfo
->depthBiasClamp
2121 GEN8_3DSTATE_RASTER_pack(NULL
, state
->state_raster
, &raster
);
2123 *pState
= (VkDynamicRsState
) state
;
2128 VkResult
anv_CreateDynamicColorBlendState(
2130 const VkDynamicCbStateCreateInfo
* pCreateInfo
,
2131 VkDynamicCbState
* pState
)
2133 struct anv_device
*device
= (struct anv_device
*) _device
;
2134 struct anv_dynamic_cb_state
*state
;
2136 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_DYNAMIC_CB_STATE_CREATE_INFO
);
2138 state
= anv_device_alloc(device
, sizeof(*state
), 8,
2139 VK_SYSTEM_ALLOC_TYPE_API_OBJECT
);
2141 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
2143 struct GEN8_COLOR_CALC_STATE color_calc_state
= {
2144 .BlendConstantColorRed
= pCreateInfo
->blendConst
[0],
2145 .BlendConstantColorGreen
= pCreateInfo
->blendConst
[1],
2146 .BlendConstantColorBlue
= pCreateInfo
->blendConst
[2],
2147 .BlendConstantColorAlpha
= pCreateInfo
->blendConst
[3]
2150 GEN8_COLOR_CALC_STATE_pack(NULL
, state
->state_color_calc
, &color_calc_state
);
2152 *pState
= (VkDynamicCbState
) state
;
2157 VkResult
anv_CreateDynamicDepthStencilState(
2159 const VkDynamicDsStateCreateInfo
* pCreateInfo
,
2160 VkDynamicDsState
* pState
)
2162 struct anv_device
*device
= (struct anv_device
*) _device
;
2163 struct anv_dynamic_ds_state
*state
;
2165 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_DYNAMIC_DS_STATE_CREATE_INFO
);
2167 state
= anv_device_alloc(device
, sizeof(*state
), 8,
2168 VK_SYSTEM_ALLOC_TYPE_API_OBJECT
);
2170 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
2172 struct GEN8_3DSTATE_WM_DEPTH_STENCIL wm_depth_stencil
= {
2173 GEN8_3DSTATE_WM_DEPTH_STENCIL_header
,
2175 /* Is this what we need to do? */
2176 .StencilBufferWriteEnable
= pCreateInfo
->stencilWriteMask
!= 0,
2178 .StencilTestMask
= pCreateInfo
->stencilReadMask
& 0xff,
2179 .StencilWriteMask
= pCreateInfo
->stencilWriteMask
& 0xff,
2181 .BackfaceStencilTestMask
= pCreateInfo
->stencilReadMask
& 0xff,
2182 .BackfaceStencilWriteMask
= pCreateInfo
->stencilWriteMask
& 0xff,
2185 GEN8_3DSTATE_WM_DEPTH_STENCIL_pack(NULL
, state
->state_wm_depth_stencil
,
2188 struct GEN8_COLOR_CALC_STATE color_calc_state
= {
2189 .StencilReferenceValue
= pCreateInfo
->stencilFrontRef
,
2190 .BackFaceStencilReferenceValue
= pCreateInfo
->stencilBackRef
2193 GEN8_COLOR_CALC_STATE_pack(NULL
, state
->state_color_calc
, &color_calc_state
);
2195 *pState
= (VkDynamicDsState
) state
;
2200 // Command buffer functions
2203 anv_cmd_buffer_destroy(struct anv_device
*device
,
2204 struct anv_object
*object
,
2205 VkObjectType obj_type
)
2207 struct anv_cmd_buffer
*cmd_buffer
= (struct anv_cmd_buffer
*) object
;
2209 assert(obj_type
== VK_OBJECT_TYPE_COMMAND_BUFFER
);
2211 /* Destroy all of the batch buffers */
2212 struct anv_batch_bo
*bbo
= cmd_buffer
->last_batch_bo
;
2213 while (bbo
->prev_batch_bo
) {
2214 struct anv_batch_bo
*prev
= bbo
->prev_batch_bo
;
2215 anv_batch_bo_destroy(bbo
, device
);
2218 anv_reloc_list_finish(&cmd_buffer
->batch
.relocs
, device
);
2220 /* Destroy all of the surface state buffers */
2221 bbo
= cmd_buffer
->surface_batch_bo
;
2222 while (bbo
->prev_batch_bo
) {
2223 struct anv_batch_bo
*prev
= bbo
->prev_batch_bo
;
2224 anv_batch_bo_destroy(bbo
, device
);
2227 anv_reloc_list_finish(&cmd_buffer
->surface_relocs
, device
);
2229 anv_state_stream_finish(&cmd_buffer
->surface_state_stream
);
2230 anv_state_stream_finish(&cmd_buffer
->dynamic_state_stream
);
2231 anv_device_free(device
, cmd_buffer
->exec2_objects
);
2232 anv_device_free(device
, cmd_buffer
->exec2_bos
);
2233 anv_device_free(device
, cmd_buffer
);
2237 anv_cmd_buffer_chain_batch(struct anv_batch
*batch
, void *_data
)
2239 struct anv_cmd_buffer
*cmd_buffer
= _data
;
2241 struct anv_batch_bo
*new_bbo
, *old_bbo
= cmd_buffer
->last_batch_bo
;
2243 VkResult result
= anv_batch_bo_create(cmd_buffer
->device
, &new_bbo
);
2244 if (result
!= VK_SUCCESS
)
2247 /* We set the end of the batch a little short so we would be sure we
2248 * have room for the chaining command. Since we're about to emit the
2249 * chaining command, let's set it back where it should go.
2251 batch
->end
+= GEN8_MI_BATCH_BUFFER_START_length
* 4;
2252 assert(batch
->end
== old_bbo
->bo
.map
+ old_bbo
->bo
.size
);
2254 anv_batch_emit(batch
, GEN8_MI_BATCH_BUFFER_START
,
2255 GEN8_MI_BATCH_BUFFER_START_header
,
2256 ._2ndLevelBatchBuffer
= _1stlevelbatch
,
2257 .AddressSpaceIndicator
= ASI_PPGTT
,
2258 .BatchBufferStartAddress
= { &new_bbo
->bo
, 0 },
2261 /* Pad out to a 2-dword aligned boundary with zeros */
2262 if ((uintptr_t)batch
->next
% 8 != 0) {
2263 *(uint32_t *)batch
->next
= 0;
2267 anv_batch_bo_finish(cmd_buffer
->last_batch_bo
, batch
);
2269 new_bbo
->prev_batch_bo
= old_bbo
;
2270 cmd_buffer
->last_batch_bo
= new_bbo
;
2272 anv_batch_bo_start(new_bbo
, batch
, GEN8_MI_BATCH_BUFFER_START_length
* 4);
2277 VkResult
anv_CreateCommandBuffer(
2279 const VkCmdBufferCreateInfo
* pCreateInfo
,
2280 VkCmdBuffer
* pCmdBuffer
)
2282 struct anv_device
*device
= (struct anv_device
*) _device
;
2283 struct anv_cmd_buffer
*cmd_buffer
;
2286 cmd_buffer
= anv_device_alloc(device
, sizeof(*cmd_buffer
), 8,
2287 VK_SYSTEM_ALLOC_TYPE_API_OBJECT
);
2288 if (cmd_buffer
== NULL
)
2289 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
2291 cmd_buffer
->base
.destructor
= anv_cmd_buffer_destroy
;
2293 cmd_buffer
->device
= device
;
2294 cmd_buffer
->rs_state
= NULL
;
2295 cmd_buffer
->vp_state
= NULL
;
2296 cmd_buffer
->cb_state
= NULL
;
2297 memset(&cmd_buffer
->descriptors
, 0, sizeof(cmd_buffer
->descriptors
));
2299 result
= anv_batch_bo_create(device
, &cmd_buffer
->last_batch_bo
);
2300 if (result
!= VK_SUCCESS
)
2303 result
= anv_reloc_list_init(&cmd_buffer
->batch
.relocs
, device
);
2304 if (result
!= VK_SUCCESS
)
2307 cmd_buffer
->batch
.device
= device
;
2308 cmd_buffer
->batch
.extend_cb
= anv_cmd_buffer_chain_batch
;
2309 cmd_buffer
->batch
.user_data
= cmd_buffer
;
2311 anv_batch_bo_start(cmd_buffer
->last_batch_bo
, &cmd_buffer
->batch
,
2312 GEN8_MI_BATCH_BUFFER_START_length
* 4);
2314 result
= anv_batch_bo_create(device
, &cmd_buffer
->surface_batch_bo
);
2315 if (result
!= VK_SUCCESS
)
2316 goto fail_batch_relocs
;
2317 cmd_buffer
->surface_batch_bo
->first_reloc
= 0;
2319 result
= anv_reloc_list_init(&cmd_buffer
->surface_relocs
, device
);
2320 if (result
!= VK_SUCCESS
)
2321 goto fail_ss_batch_bo
;
2323 /* Start surface_next at 1 so surface offset 0 is invalid. */
2324 cmd_buffer
->surface_next
= 1;
2326 cmd_buffer
->exec2_objects
= NULL
;
2327 cmd_buffer
->exec2_bos
= NULL
;
2328 cmd_buffer
->exec2_array_length
= 0;
2330 anv_state_stream_init(&cmd_buffer
->surface_state_stream
,
2331 &device
->surface_state_block_pool
);
2332 anv_state_stream_init(&cmd_buffer
->dynamic_state_stream
,
2333 &device
->dynamic_state_block_pool
);
2335 cmd_buffer
->dirty
= 0;
2336 cmd_buffer
->vb_dirty
= 0;
2337 cmd_buffer
->descriptors_dirty
= 0;
2338 cmd_buffer
->pipeline
= NULL
;
2339 cmd_buffer
->vp_state
= NULL
;
2340 cmd_buffer
->rs_state
= NULL
;
2341 cmd_buffer
->ds_state
= NULL
;
2343 *pCmdBuffer
= (VkCmdBuffer
) cmd_buffer
;
2348 anv_batch_bo_destroy(cmd_buffer
->surface_batch_bo
, device
);
2350 anv_reloc_list_finish(&cmd_buffer
->batch
.relocs
, device
);
2352 anv_batch_bo_destroy(cmd_buffer
->last_batch_bo
, device
);
2354 anv_device_free(device
, cmd_buffer
);
2360 anv_cmd_buffer_emit_state_base_address(struct anv_cmd_buffer
*cmd_buffer
)
2362 struct anv_device
*device
= cmd_buffer
->device
;
2364 anv_batch_emit(&cmd_buffer
->batch
, GEN8_STATE_BASE_ADDRESS
,
2365 .GeneralStateBaseAddress
= { NULL
, 0 },
2366 .GeneralStateMemoryObjectControlState
= GEN8_MOCS
,
2367 .GeneralStateBaseAddressModifyEnable
= true,
2368 .GeneralStateBufferSize
= 0xfffff,
2369 .GeneralStateBufferSizeModifyEnable
= true,
2371 .SurfaceStateBaseAddress
= { &cmd_buffer
->surface_batch_bo
->bo
, 0 },
2372 .SurfaceStateMemoryObjectControlState
= GEN8_MOCS
,
2373 .SurfaceStateBaseAddressModifyEnable
= true,
2375 .DynamicStateBaseAddress
= { &device
->dynamic_state_block_pool
.bo
, 0 },
2376 .DynamicStateMemoryObjectControlState
= GEN8_MOCS
,
2377 .DynamicStateBaseAddressModifyEnable
= true,
2378 .DynamicStateBufferSize
= 0xfffff,
2379 .DynamicStateBufferSizeModifyEnable
= true,
2381 .IndirectObjectBaseAddress
= { NULL
, 0 },
2382 .IndirectObjectMemoryObjectControlState
= GEN8_MOCS
,
2383 .IndirectObjectBaseAddressModifyEnable
= true,
2384 .IndirectObjectBufferSize
= 0xfffff,
2385 .IndirectObjectBufferSizeModifyEnable
= true,
2387 .InstructionBaseAddress
= { &device
->instruction_block_pool
.bo
, 0 },
2388 .InstructionMemoryObjectControlState
= GEN8_MOCS
,
2389 .InstructionBaseAddressModifyEnable
= true,
2390 .InstructionBufferSize
= 0xfffff,
2391 .InstructionBuffersizeModifyEnable
= true);
2394 VkResult
anv_BeginCommandBuffer(
2395 VkCmdBuffer cmdBuffer
,
2396 const VkCmdBufferBeginInfo
* pBeginInfo
)
2398 struct anv_cmd_buffer
*cmd_buffer
= (struct anv_cmd_buffer
*) cmdBuffer
;
2400 anv_batch_emit(&cmd_buffer
->batch
, GEN8_PIPELINE_SELECT
,
2401 .PipelineSelection
= _3D
);
2402 anv_batch_emit(&cmd_buffer
->batch
, GEN8_STATE_SIP
);
2404 anv_cmd_buffer_emit_state_base_address(cmd_buffer
);
2406 anv_batch_emit(&cmd_buffer
->batch
, GEN8_3DSTATE_VF_STATISTICS
,
2407 .StatisticsEnable
= true);
2408 anv_batch_emit(&cmd_buffer
->batch
, GEN8_3DSTATE_HS
, .Enable
= false);
2409 anv_batch_emit(&cmd_buffer
->batch
, GEN8_3DSTATE_TE
, .TEEnable
= false);
2410 anv_batch_emit(&cmd_buffer
->batch
, GEN8_3DSTATE_DS
, .FunctionEnable
= false);
2411 anv_batch_emit(&cmd_buffer
->batch
, GEN8_3DSTATE_STREAMOUT
, .SOFunctionEnable
= false);
2413 anv_batch_emit(&cmd_buffer
->batch
, GEN8_3DSTATE_PUSH_CONSTANT_ALLOC_VS
,
2414 .ConstantBufferOffset
= 0,
2415 .ConstantBufferSize
= 4);
2416 anv_batch_emit(&cmd_buffer
->batch
, GEN8_3DSTATE_PUSH_CONSTANT_ALLOC_GS
,
2417 .ConstantBufferOffset
= 4,
2418 .ConstantBufferSize
= 4);
2419 anv_batch_emit(&cmd_buffer
->batch
, GEN8_3DSTATE_PUSH_CONSTANT_ALLOC_PS
,
2420 .ConstantBufferOffset
= 8,
2421 .ConstantBufferSize
= 4);
2423 anv_batch_emit(&cmd_buffer
->batch
, GEN8_3DSTATE_WM_CHROMAKEY
,
2424 .ChromaKeyKillEnable
= false);
2425 anv_batch_emit(&cmd_buffer
->batch
, GEN8_3DSTATE_SBE_SWIZ
);
2426 anv_batch_emit(&cmd_buffer
->batch
, GEN8_3DSTATE_AA_LINE_PARAMETERS
);
2432 anv_cmd_buffer_add_bo(struct anv_cmd_buffer
*cmd_buffer
,
2434 struct drm_i915_gem_relocation_entry
*relocs
,
2437 struct drm_i915_gem_exec_object2
*obj
;
2439 if (bo
->index
< cmd_buffer
->bo_count
&&
2440 cmd_buffer
->exec2_bos
[bo
->index
] == bo
)
2443 if (cmd_buffer
->bo_count
>= cmd_buffer
->exec2_array_length
) {
2444 uint32_t new_len
= cmd_buffer
->exec2_objects
?
2445 cmd_buffer
->exec2_array_length
* 2 : 64;
2447 struct drm_i915_gem_exec_object2
*new_objects
=
2448 anv_device_alloc(cmd_buffer
->device
, new_len
* sizeof(*new_objects
),
2449 8, VK_SYSTEM_ALLOC_TYPE_INTERNAL
);
2450 if (new_objects
== NULL
)
2451 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
2453 struct anv_bo
**new_bos
=
2454 anv_device_alloc(cmd_buffer
->device
, new_len
* sizeof(*new_bos
),
2455 8, VK_SYSTEM_ALLOC_TYPE_INTERNAL
);
2456 if (new_objects
== NULL
) {
2457 anv_device_free(cmd_buffer
->device
, new_objects
);
2458 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
2461 if (cmd_buffer
->exec2_objects
) {
2462 memcpy(new_objects
, cmd_buffer
->exec2_objects
,
2463 cmd_buffer
->bo_count
* sizeof(*new_objects
));
2464 memcpy(new_bos
, cmd_buffer
->exec2_bos
,
2465 cmd_buffer
->bo_count
* sizeof(*new_bos
));
2468 cmd_buffer
->exec2_objects
= new_objects
;
2469 cmd_buffer
->exec2_bos
= new_bos
;
2470 cmd_buffer
->exec2_array_length
= new_len
;
2473 assert(cmd_buffer
->bo_count
< cmd_buffer
->exec2_array_length
);
2475 bo
->index
= cmd_buffer
->bo_count
++;
2476 obj
= &cmd_buffer
->exec2_objects
[bo
->index
];
2477 cmd_buffer
->exec2_bos
[bo
->index
] = bo
;
2479 obj
->handle
= bo
->gem_handle
;
2480 obj
->relocation_count
= 0;
2481 obj
->relocs_ptr
= 0;
2483 obj
->offset
= bo
->offset
;
2489 obj
->relocation_count
= num_relocs
;
2490 obj
->relocs_ptr
= (uintptr_t) relocs
;
2497 anv_cmd_buffer_add_validate_bos(struct anv_cmd_buffer
*cmd_buffer
,
2498 struct anv_reloc_list
*list
)
2500 for (size_t i
= 0; i
< list
->num_relocs
; i
++)
2501 anv_cmd_buffer_add_bo(cmd_buffer
, list
->reloc_bos
[i
], NULL
, 0);
2505 anv_cmd_buffer_process_relocs(struct anv_cmd_buffer
*cmd_buffer
,
2506 struct anv_reloc_list
*list
)
2510 /* If the kernel supports I915_EXEC_NO_RELOC, it will compare offset in
2511 * struct drm_i915_gem_exec_object2 against the bos current offset and if
2512 * all bos haven't moved it will skip relocation processing alltogether.
2513 * If I915_EXEC_NO_RELOC is not supported, the kernel ignores the incoming
2514 * value of offset so we can set it either way. For that to work we need
2515 * to make sure all relocs use the same presumed offset.
2518 for (size_t i
= 0; i
< list
->num_relocs
; i
++) {
2519 bo
= list
->reloc_bos
[i
];
2520 if (bo
->offset
!= list
->relocs
[i
].presumed_offset
)
2521 cmd_buffer
->need_reloc
= true;
2523 list
->relocs
[i
].target_handle
= bo
->index
;
2527 VkResult
anv_EndCommandBuffer(
2528 VkCmdBuffer cmdBuffer
)
2530 struct anv_cmd_buffer
*cmd_buffer
= (struct anv_cmd_buffer
*) cmdBuffer
;
2531 struct anv_device
*device
= cmd_buffer
->device
;
2532 struct anv_batch
*batch
= &cmd_buffer
->batch
;
2534 anv_batch_emit(batch
, GEN8_MI_BATCH_BUFFER_END
);
2536 /* Round batch up to an even number of dwords. */
2537 if ((batch
->next
- batch
->start
) & 4)
2538 anv_batch_emit(batch
, GEN8_MI_NOOP
);
2540 anv_batch_bo_finish(cmd_buffer
->last_batch_bo
, &cmd_buffer
->batch
);
2541 cmd_buffer
->surface_batch_bo
->num_relocs
=
2542 cmd_buffer
->surface_relocs
.num_relocs
- cmd_buffer
->surface_batch_bo
->first_reloc
;
2543 cmd_buffer
->surface_batch_bo
->length
= cmd_buffer
->surface_next
;
2545 cmd_buffer
->bo_count
= 0;
2546 cmd_buffer
->need_reloc
= false;
2548 /* Lock for access to bo->index. */
2549 pthread_mutex_lock(&device
->mutex
);
2551 /* Add surface state bos first so we can add them with their relocs. */
2552 for (struct anv_batch_bo
*bbo
= cmd_buffer
->surface_batch_bo
;
2553 bbo
!= NULL
; bbo
= bbo
->prev_batch_bo
) {
2554 anv_cmd_buffer_add_bo(cmd_buffer
, &bbo
->bo
,
2555 &cmd_buffer
->surface_relocs
.relocs
[bbo
->first_reloc
],
2559 /* Add all of the BOs referenced by surface state */
2560 anv_cmd_buffer_add_validate_bos(cmd_buffer
, &cmd_buffer
->surface_relocs
);
2562 /* Add all but the first batch BO */
2563 struct anv_batch_bo
*batch_bo
= cmd_buffer
->last_batch_bo
;
2564 while (batch_bo
->prev_batch_bo
) {
2565 anv_cmd_buffer_add_bo(cmd_buffer
, &batch_bo
->bo
,
2566 &batch
->relocs
.relocs
[batch_bo
->first_reloc
],
2567 batch_bo
->num_relocs
);
2568 batch_bo
= batch_bo
->prev_batch_bo
;
2571 /* Add everything referenced by the batches */
2572 anv_cmd_buffer_add_validate_bos(cmd_buffer
, &batch
->relocs
);
2574 /* Add the first batch bo last */
2575 assert(batch_bo
->prev_batch_bo
== NULL
&& batch_bo
->first_reloc
== 0);
2576 anv_cmd_buffer_add_bo(cmd_buffer
, &batch_bo
->bo
,
2577 &batch
->relocs
.relocs
[batch_bo
->first_reloc
],
2578 batch_bo
->num_relocs
);
2579 assert(batch_bo
->bo
.index
== cmd_buffer
->bo_count
- 1);
2581 anv_cmd_buffer_process_relocs(cmd_buffer
, &cmd_buffer
->surface_relocs
);
2582 anv_cmd_buffer_process_relocs(cmd_buffer
, &batch
->relocs
);
2584 cmd_buffer
->execbuf
.buffers_ptr
= (uintptr_t) cmd_buffer
->exec2_objects
;
2585 cmd_buffer
->execbuf
.buffer_count
= cmd_buffer
->bo_count
;
2586 cmd_buffer
->execbuf
.batch_start_offset
= 0;
2587 cmd_buffer
->execbuf
.batch_len
= batch
->next
- batch
->start
;
2588 cmd_buffer
->execbuf
.cliprects_ptr
= 0;
2589 cmd_buffer
->execbuf
.num_cliprects
= 0;
2590 cmd_buffer
->execbuf
.DR1
= 0;
2591 cmd_buffer
->execbuf
.DR4
= 0;
2593 cmd_buffer
->execbuf
.flags
= I915_EXEC_HANDLE_LUT
;
2594 if (!cmd_buffer
->need_reloc
)
2595 cmd_buffer
->execbuf
.flags
|= I915_EXEC_NO_RELOC
;
2596 cmd_buffer
->execbuf
.flags
|= I915_EXEC_RENDER
;
2597 cmd_buffer
->execbuf
.rsvd1
= device
->context_id
;
2598 cmd_buffer
->execbuf
.rsvd2
= 0;
2600 pthread_mutex_unlock(&device
->mutex
);
2605 VkResult
anv_ResetCommandBuffer(
2606 VkCmdBuffer cmdBuffer
)
2608 struct anv_cmd_buffer
*cmd_buffer
= (struct anv_cmd_buffer
*) cmdBuffer
;
2610 /* Delete all but the first batch bo */
2611 while (cmd_buffer
->last_batch_bo
->prev_batch_bo
) {
2612 struct anv_batch_bo
*prev
= cmd_buffer
->last_batch_bo
->prev_batch_bo
;
2613 anv_batch_bo_destroy(cmd_buffer
->last_batch_bo
, cmd_buffer
->device
);
2614 cmd_buffer
->last_batch_bo
= prev
;
2616 assert(cmd_buffer
->last_batch_bo
->prev_batch_bo
== NULL
);
2618 cmd_buffer
->batch
.relocs
.num_relocs
= 0;
2619 anv_batch_bo_start(cmd_buffer
->last_batch_bo
, &cmd_buffer
->batch
,
2620 GEN8_MI_BATCH_BUFFER_START_length
* 4);
2622 /* Delete all but the first batch bo */
2623 while (cmd_buffer
->surface_batch_bo
->prev_batch_bo
) {
2624 struct anv_batch_bo
*prev
= cmd_buffer
->surface_batch_bo
->prev_batch_bo
;
2625 anv_batch_bo_destroy(cmd_buffer
->surface_batch_bo
, cmd_buffer
->device
);
2626 cmd_buffer
->surface_batch_bo
= prev
;
2628 assert(cmd_buffer
->surface_batch_bo
->prev_batch_bo
== NULL
);
2630 cmd_buffer
->surface_next
= 1;
2631 cmd_buffer
->surface_relocs
.num_relocs
= 0;
2636 // Command buffer building functions
2638 void anv_CmdBindPipeline(
2639 VkCmdBuffer cmdBuffer
,
2640 VkPipelineBindPoint pipelineBindPoint
,
2641 VkPipeline _pipeline
)
2643 struct anv_cmd_buffer
*cmd_buffer
= (struct anv_cmd_buffer
*) cmdBuffer
;
2644 struct anv_pipeline
*pipeline
= (struct anv_pipeline
*) _pipeline
;
2646 cmd_buffer
->pipeline
= pipeline
;
2647 cmd_buffer
->vb_dirty
|= pipeline
->vb_used
;
2648 cmd_buffer
->dirty
|= ANV_CMD_BUFFER_PIPELINE_DIRTY
;
2651 void anv_CmdBindDynamicStateObject(
2652 VkCmdBuffer cmdBuffer
,
2653 VkStateBindPoint stateBindPoint
,
2654 VkDynamicStateObject dynamicState
)
2656 struct anv_cmd_buffer
*cmd_buffer
= (struct anv_cmd_buffer
*) cmdBuffer
;
2657 struct anv_dynamic_vp_state
*vp_state
;
2659 switch (stateBindPoint
) {
2660 case VK_STATE_BIND_POINT_VIEWPORT
:
2661 vp_state
= (struct anv_dynamic_vp_state
*) dynamicState
;
2662 /* We emit state immediately, but set cmd_buffer->vp_state to indicate
2663 * that vp state has been set in this command buffer. */
2664 cmd_buffer
->vp_state
= vp_state
;
2665 anv_batch_emit(&cmd_buffer
->batch
, GEN8_3DSTATE_SCISSOR_STATE_POINTERS
,
2666 .ScissorRectPointer
= vp_state
->scissor
.offset
);
2667 anv_batch_emit(&cmd_buffer
->batch
, GEN8_3DSTATE_VIEWPORT_STATE_POINTERS_CC
,
2668 .CCViewportPointer
= vp_state
->cc_vp
.offset
);
2669 anv_batch_emit(&cmd_buffer
->batch
, GEN8_3DSTATE_VIEWPORT_STATE_POINTERS_SF_CLIP
,
2670 .SFClipViewportPointer
= vp_state
->sf_clip_vp
.offset
);
2672 case VK_STATE_BIND_POINT_RASTER
:
2673 cmd_buffer
->rs_state
= (struct anv_dynamic_rs_state
*) dynamicState
;
2674 cmd_buffer
->dirty
|= ANV_CMD_BUFFER_RS_DIRTY
;
2676 case VK_STATE_BIND_POINT_COLOR_BLEND
:
2677 cmd_buffer
->cb_state
= (struct anv_dynamic_cb_state
*) dynamicState
;
2678 cmd_buffer
->dirty
|= ANV_CMD_BUFFER_CB_DIRTY
;
2680 case VK_STATE_BIND_POINT_DEPTH_STENCIL
:
2681 cmd_buffer
->ds_state
= (struct anv_dynamic_ds_state
*) dynamicState
;
2682 cmd_buffer
->dirty
|= ANV_CMD_BUFFER_DS_DIRTY
;
2689 static struct anv_state
2690 anv_cmd_buffer_alloc_surface_state(struct anv_cmd_buffer
*cmd_buffer
,
2691 uint32_t size
, uint32_t alignment
)
2693 struct anv_state state
;
2695 state
.offset
= ALIGN_U32(cmd_buffer
->surface_next
, alignment
);
2696 if (state
.offset
+ size
> cmd_buffer
->surface_batch_bo
->bo
.size
)
2697 return (struct anv_state
) { 0 };
2699 state
.map
= cmd_buffer
->surface_batch_bo
->bo
.map
+ state
.offset
;
2700 state
.alloc_size
= size
;
2701 cmd_buffer
->surface_next
= state
.offset
+ size
;
2703 assert(state
.offset
+ size
<= cmd_buffer
->surface_batch_bo
->bo
.size
);
2709 anv_cmd_buffer_new_surface_state_bo(struct anv_cmd_buffer
*cmd_buffer
)
2711 struct anv_batch_bo
*new_bbo
, *old_bbo
= cmd_buffer
->surface_batch_bo
;
2713 /* Finish off the old buffer */
2714 old_bbo
->num_relocs
=
2715 cmd_buffer
->surface_relocs
.num_relocs
- old_bbo
->first_reloc
;
2716 old_bbo
->length
= cmd_buffer
->surface_next
;
2718 VkResult result
= anv_batch_bo_create(cmd_buffer
->device
, &new_bbo
);
2719 if (result
!= VK_SUCCESS
)
2722 new_bbo
->first_reloc
= cmd_buffer
->surface_relocs
.num_relocs
;
2723 cmd_buffer
->surface_next
= 1;
2725 new_bbo
->prev_batch_bo
= old_bbo
;
2726 cmd_buffer
->surface_batch_bo
= new_bbo
;
2728 /* Re-emit state base addresses so we get the new surface state base
2729 * address before we start emitting binding tables etc.
2731 anv_cmd_buffer_emit_state_base_address(cmd_buffer
);
2733 /* It seems like just changing the state base addresses isn't enough.
2734 * Invalidating the cache seems to be enough to cause things to
2735 * propagate. However, I'm not 100% sure what we're supposed to do.
2737 anv_batch_emit(&cmd_buffer
->batch
, GEN8_PIPE_CONTROL
,
2738 .TextureCacheInvalidationEnable
= true);
2743 void anv_CmdBindDescriptorSets(
2744 VkCmdBuffer cmdBuffer
,
2745 VkPipelineBindPoint pipelineBindPoint
,
2748 const VkDescriptorSet
* pDescriptorSets
,
2749 uint32_t dynamicOffsetCount
,
2750 const uint32_t* pDynamicOffsets
)
2752 struct anv_cmd_buffer
*cmd_buffer
= (struct anv_cmd_buffer
*) cmdBuffer
;
2753 struct anv_pipeline_layout
*layout
= cmd_buffer
->pipeline
->layout
;
2754 struct anv_descriptor_set
*set
;
2755 struct anv_descriptor_set_layout
*set_layout
;
2757 assert(firstSet
+ setCount
< MAX_SETS
);
2759 uint32_t dynamic_slot
= 0;
2760 for (uint32_t i
= 0; i
< setCount
; i
++) {
2761 set
= (struct anv_descriptor_set
*) pDescriptorSets
[i
];
2762 set_layout
= layout
->set
[firstSet
+ i
].layout
;
2764 cmd_buffer
->descriptors
[firstSet
+ i
].set
= set
;
2766 assert(set_layout
->num_dynamic_buffers
<
2767 ARRAY_SIZE(cmd_buffer
->descriptors
[0].dynamic_offsets
));
2768 memcpy(cmd_buffer
->descriptors
[firstSet
+ i
].dynamic_offsets
,
2769 pDynamicOffsets
+ dynamic_slot
,
2770 set_layout
->num_dynamic_buffers
* sizeof(*pDynamicOffsets
));
2772 cmd_buffer
->descriptors_dirty
|= set_layout
->shader_stages
;
2774 dynamic_slot
+= set_layout
->num_dynamic_buffers
;
2778 void anv_CmdBindIndexBuffer(
2779 VkCmdBuffer cmdBuffer
,
2781 VkDeviceSize offset
,
2782 VkIndexType indexType
)
2784 struct anv_cmd_buffer
*cmd_buffer
= (struct anv_cmd_buffer
*) cmdBuffer
;
2785 struct anv_buffer
*buffer
= (struct anv_buffer
*) _buffer
;
2787 static const uint32_t vk_to_gen_index_type
[] = {
2788 [VK_INDEX_TYPE_UINT8
] = INDEX_BYTE
,
2789 [VK_INDEX_TYPE_UINT16
] = INDEX_WORD
,
2790 [VK_INDEX_TYPE_UINT32
] = INDEX_DWORD
,
2793 anv_batch_emit(&cmd_buffer
->batch
, GEN8_3DSTATE_INDEX_BUFFER
,
2794 .IndexFormat
= vk_to_gen_index_type
[indexType
],
2795 .MemoryObjectControlState
= GEN8_MOCS
,
2796 .BufferStartingAddress
= { buffer
->bo
, buffer
->offset
+ offset
},
2797 .BufferSize
= buffer
->size
- offset
);
2800 void anv_CmdBindVertexBuffers(
2801 VkCmdBuffer cmdBuffer
,
2802 uint32_t startBinding
,
2803 uint32_t bindingCount
,
2804 const VkBuffer
* pBuffers
,
2805 const VkDeviceSize
* pOffsets
)
2807 struct anv_cmd_buffer
*cmd_buffer
= (struct anv_cmd_buffer
*) cmdBuffer
;
2808 struct anv_vertex_binding
*vb
= cmd_buffer
->vertex_bindings
;
2810 /* We have to defer setting up vertex buffer since we need the buffer
2811 * stride from the pipeline. */
2813 assert(startBinding
+ bindingCount
< MAX_VBS
);
2814 for (uint32_t i
= 0; i
< bindingCount
; i
++) {
2815 vb
[startBinding
+ i
].buffer
= (struct anv_buffer
*) pBuffers
[i
];
2816 vb
[startBinding
+ i
].offset
= pOffsets
[i
];
2817 cmd_buffer
->vb_dirty
|= 1 << (startBinding
+ i
);
2822 cmd_buffer_emit_binding_table(struct anv_cmd_buffer
*cmd_buffer
,
2825 struct anv_pipeline_layout
*layout
= cmd_buffer
->pipeline
->layout
;
2826 uint32_t color_attachments
, bias
, size
;
2827 struct anv_state bt_state
;
2829 if (stage
== VK_SHADER_STAGE_FRAGMENT
) {
2831 color_attachments
= cmd_buffer
->framebuffer
->color_attachment_count
;
2834 color_attachments
= 0;
2837 /* This is a little awkward: layout can be NULL but we still have to
2838 * allocate and set a binding table for the PS stage for render
2840 uint32_t surface_count
= layout
? layout
->stage
[stage
].surface_count
: 0;
2842 if (color_attachments
+ surface_count
== 0)
2845 size
= (bias
+ surface_count
) * sizeof(uint32_t);
2846 bt_state
= anv_cmd_buffer_alloc_surface_state(cmd_buffer
, size
, 32);
2847 uint32_t *bt_map
= bt_state
.map
;
2849 if (bt_state
.map
== NULL
)
2850 return VK_ERROR_OUT_OF_DEVICE_MEMORY
;
2852 static const uint32_t binding_table_opcodes
[] = {
2853 [VK_SHADER_STAGE_VERTEX
] = 38,
2854 [VK_SHADER_STAGE_TESS_CONTROL
] = 39,
2855 [VK_SHADER_STAGE_TESS_EVALUATION
] = 40,
2856 [VK_SHADER_STAGE_GEOMETRY
] = 41,
2857 [VK_SHADER_STAGE_FRAGMENT
] = 42,
2858 [VK_SHADER_STAGE_COMPUTE
] = 0,
2861 anv_batch_emit(&cmd_buffer
->batch
,
2862 GEN8_3DSTATE_BINDING_TABLE_POINTERS_VS
,
2863 ._3DCommandSubOpcode
= binding_table_opcodes
[stage
],
2864 .PointertoVSBindingTable
= bt_state
.offset
);
2866 for (uint32_t ca
= 0; ca
< color_attachments
; ca
++) {
2867 const struct anv_surface_view
*view
=
2868 cmd_buffer
->framebuffer
->color_attachments
[ca
];
2870 struct anv_state state
=
2871 anv_cmd_buffer_alloc_surface_state(cmd_buffer
, 64, 64);
2873 if (state
.map
== NULL
)
2874 return VK_ERROR_OUT_OF_DEVICE_MEMORY
;
2876 memcpy(state
.map
, view
->surface_state
.map
, 64);
2878 /* The address goes in dwords 8 and 9 of the SURFACE_STATE */
2879 *(uint64_t *)(state
.map
+ 8 * 4) =
2880 anv_reloc_list_add(&cmd_buffer
->surface_relocs
,
2882 state
.offset
+ 8 * 4,
2883 view
->bo
, view
->offset
);
2885 bt_map
[ca
] = state
.offset
;
2891 for (uint32_t set
= 0; set
< layout
->num_sets
; set
++) {
2892 struct anv_descriptor_set_binding
*d
= &cmd_buffer
->descriptors
[set
];
2893 struct anv_descriptor_set_layout
*set_layout
= layout
->set
[set
].layout
;
2894 struct anv_descriptor_slot
*surface_slots
=
2895 set_layout
->stage
[stage
].surface_start
;
2897 uint32_t start
= bias
+ layout
->set
[set
].surface_start
[stage
];
2899 for (uint32_t b
= 0; b
< set_layout
->stage
[stage
].surface_count
; b
++) {
2900 struct anv_surface_view
*view
=
2901 d
->set
->descriptors
[surface_slots
[b
].index
].view
;
2906 struct anv_state state
=
2907 anv_cmd_buffer_alloc_surface_state(cmd_buffer
, 64, 64);
2909 if (state
.map
== NULL
)
2910 return VK_ERROR_OUT_OF_DEVICE_MEMORY
;
2913 if (surface_slots
[b
].dynamic_slot
>= 0) {
2914 uint32_t dynamic_offset
=
2915 d
->dynamic_offsets
[surface_slots
[b
].dynamic_slot
];
2917 offset
= view
->offset
+ dynamic_offset
;
2918 fill_buffer_surface_state(state
.map
, view
->format
, offset
,
2919 view
->range
- dynamic_offset
);
2921 offset
= view
->offset
;
2922 memcpy(state
.map
, view
->surface_state
.map
, 64);
2925 /* The address goes in dwords 8 and 9 of the SURFACE_STATE */
2926 *(uint64_t *)(state
.map
+ 8 * 4) =
2927 anv_reloc_list_add(&cmd_buffer
->surface_relocs
,
2929 state
.offset
+ 8 * 4,
2932 bt_map
[start
+ b
] = state
.offset
;
2940 cmd_buffer_emit_samplers(struct anv_cmd_buffer
*cmd_buffer
, unsigned stage
)
2942 struct anv_pipeline_layout
*layout
= cmd_buffer
->pipeline
->layout
;
2943 struct anv_state state
;
2948 uint32_t sampler_count
= layout
->stage
[stage
].sampler_count
;
2950 if (sampler_count
== 0)
2953 uint32_t size
= sampler_count
* 16;
2954 state
= anv_state_stream_alloc(&cmd_buffer
->dynamic_state_stream
, size
, 32);
2956 if (state
.map
== NULL
)
2957 return VK_ERROR_OUT_OF_DEVICE_MEMORY
;
2959 static const uint32_t sampler_state_opcodes
[] = {
2960 [VK_SHADER_STAGE_VERTEX
] = 43,
2961 [VK_SHADER_STAGE_TESS_CONTROL
] = 44, /* HS */
2962 [VK_SHADER_STAGE_TESS_EVALUATION
] = 45, /* DS */
2963 [VK_SHADER_STAGE_GEOMETRY
] = 46,
2964 [VK_SHADER_STAGE_FRAGMENT
] = 47,
2965 [VK_SHADER_STAGE_COMPUTE
] = 0,
2968 anv_batch_emit(&cmd_buffer
->batch
,
2969 GEN8_3DSTATE_SAMPLER_STATE_POINTERS_VS
,
2970 ._3DCommandSubOpcode
= sampler_state_opcodes
[stage
],
2971 .PointertoVSSamplerState
= state
.offset
);
2973 for (uint32_t set
= 0; set
< layout
->num_sets
; set
++) {
2974 struct anv_descriptor_set_binding
*d
= &cmd_buffer
->descriptors
[set
];
2975 struct anv_descriptor_set_layout
*set_layout
= layout
->set
[set
].layout
;
2976 struct anv_descriptor_slot
*sampler_slots
=
2977 set_layout
->stage
[stage
].sampler_start
;
2979 uint32_t start
= layout
->set
[set
].sampler_start
[stage
];
2981 for (uint32_t b
= 0; b
< set_layout
->stage
[stage
].sampler_count
; b
++) {
2982 struct anv_sampler
*sampler
=
2983 d
->set
->descriptors
[sampler_slots
[b
].index
].sampler
;
2988 memcpy(state
.map
+ (start
+ b
) * 16,
2989 sampler
->state
, sizeof(sampler
->state
));
2997 flush_descriptor_sets(struct anv_cmd_buffer
*cmd_buffer
)
2999 uint32_t s
, dirty
= cmd_buffer
->descriptors_dirty
&
3000 cmd_buffer
->pipeline
->active_stages
;
3003 for_each_bit(s
, dirty
) {
3004 result
= cmd_buffer_emit_binding_table(cmd_buffer
, s
);
3005 if (result
!= VK_SUCCESS
)
3008 result
= cmd_buffer_emit_samplers(cmd_buffer
, s
);
3009 if (result
!= VK_SUCCESS
)
3013 if (result
!= VK_SUCCESS
) {
3014 assert(result
== VK_ERROR_OUT_OF_DEVICE_MEMORY
);
3016 result
= anv_cmd_buffer_new_surface_state_bo(cmd_buffer
);
3017 assert(result
== VK_SUCCESS
);
3019 /* Re-emit all active binding tables */
3020 for_each_bit(s
, cmd_buffer
->pipeline
->active_stages
) {
3021 result
= cmd_buffer_emit_binding_table(cmd_buffer
, s
);
3022 result
= cmd_buffer_emit_samplers(cmd_buffer
, s
);
3025 /* It had better succeed this time */
3026 assert(result
== VK_SUCCESS
);
3029 cmd_buffer
->descriptors_dirty
&= ~cmd_buffer
->pipeline
->active_stages
;
3032 static struct anv_state
3033 anv_cmd_buffer_emit_dynamic(struct anv_cmd_buffer
*cmd_buffer
,
3034 uint32_t *a
, uint32_t dwords
, uint32_t alignment
)
3036 struct anv_state state
;
3038 state
= anv_state_stream_alloc(&cmd_buffer
->dynamic_state_stream
,
3039 dwords
* 4, alignment
);
3040 memcpy(state
.map
, a
, dwords
* 4);
3045 static struct anv_state
3046 anv_cmd_buffer_merge_dynamic(struct anv_cmd_buffer
*cmd_buffer
,
3047 uint32_t *a
, uint32_t *b
,
3048 uint32_t dwords
, uint32_t alignment
)
3050 struct anv_state state
;
3053 state
= anv_state_stream_alloc(&cmd_buffer
->dynamic_state_stream
,
3054 dwords
* 4, alignment
);
3056 for (uint32_t i
= 0; i
< dwords
; i
++)
3063 anv_cmd_buffer_flush_state(struct anv_cmd_buffer
*cmd_buffer
)
3065 struct anv_pipeline
*pipeline
= cmd_buffer
->pipeline
;
3068 uint32_t vb_emit
= cmd_buffer
->vb_dirty
& pipeline
->vb_used
;
3071 const uint32_t num_buffers
= __builtin_popcount(vb_emit
);
3072 const uint32_t num_dwords
= 1 + num_buffers
* 4;
3074 p
= anv_batch_emitn(&cmd_buffer
->batch
, num_dwords
,
3075 GEN8_3DSTATE_VERTEX_BUFFERS
);
3077 for_each_bit(vb
, vb_emit
) {
3078 struct anv_buffer
*buffer
= cmd_buffer
->vertex_bindings
[vb
].buffer
;
3079 uint32_t offset
= cmd_buffer
->vertex_bindings
[vb
].offset
;
3081 struct GEN8_VERTEX_BUFFER_STATE state
= {
3082 .VertexBufferIndex
= vb
,
3083 .MemoryObjectControlState
= GEN8_MOCS
,
3084 .AddressModifyEnable
= true,
3085 .BufferPitch
= pipeline
->binding_stride
[vb
],
3086 .BufferStartingAddress
= { buffer
->bo
, buffer
->offset
+ offset
},
3087 .BufferSize
= buffer
->size
- offset
3090 GEN8_VERTEX_BUFFER_STATE_pack(&cmd_buffer
->batch
, &p
[1 + i
* 4], &state
);
3095 if (cmd_buffer
->dirty
& ANV_CMD_BUFFER_PIPELINE_DIRTY
)
3096 anv_batch_emit_batch(&cmd_buffer
->batch
, &pipeline
->batch
);
3098 if (cmd_buffer
->descriptors_dirty
)
3099 flush_descriptor_sets(cmd_buffer
);
3101 if (cmd_buffer
->dirty
& (ANV_CMD_BUFFER_PIPELINE_DIRTY
| ANV_CMD_BUFFER_RS_DIRTY
)) {
3102 anv_batch_emit_merge(&cmd_buffer
->batch
,
3103 cmd_buffer
->rs_state
->state_sf
, pipeline
->state_sf
);
3104 anv_batch_emit_merge(&cmd_buffer
->batch
,
3105 cmd_buffer
->rs_state
->state_raster
, pipeline
->state_raster
);
3108 if (cmd_buffer
->ds_state
&&
3109 (cmd_buffer
->dirty
& (ANV_CMD_BUFFER_PIPELINE_DIRTY
| ANV_CMD_BUFFER_DS_DIRTY
)))
3110 anv_batch_emit_merge(&cmd_buffer
->batch
,
3111 cmd_buffer
->ds_state
->state_wm_depth_stencil
,
3112 pipeline
->state_wm_depth_stencil
);
3114 if (cmd_buffer
->dirty
& (ANV_CMD_BUFFER_CB_DIRTY
| ANV_CMD_BUFFER_DS_DIRTY
)) {
3115 struct anv_state state
;
3116 if (cmd_buffer
->ds_state
== NULL
)
3117 state
= anv_cmd_buffer_emit_dynamic(cmd_buffer
,
3118 cmd_buffer
->cb_state
->state_color_calc
,
3119 GEN8_COLOR_CALC_STATE_length
, 64);
3120 else if (cmd_buffer
->cb_state
== NULL
)
3121 state
= anv_cmd_buffer_emit_dynamic(cmd_buffer
,
3122 cmd_buffer
->ds_state
->state_color_calc
,
3123 GEN8_COLOR_CALC_STATE_length
, 64);
3125 state
= anv_cmd_buffer_merge_dynamic(cmd_buffer
,
3126 cmd_buffer
->ds_state
->state_color_calc
,
3127 cmd_buffer
->cb_state
->state_color_calc
,
3128 GEN8_COLOR_CALC_STATE_length
, 64);
3130 anv_batch_emit(&cmd_buffer
->batch
,
3131 GEN8_3DSTATE_CC_STATE_POINTERS
,
3132 .ColorCalcStatePointer
= state
.offset
,
3133 .ColorCalcStatePointerValid
= true);
3136 cmd_buffer
->vb_dirty
&= ~vb_emit
;
3137 cmd_buffer
->dirty
= 0;
3141 VkCmdBuffer cmdBuffer
,
3142 uint32_t firstVertex
,
3143 uint32_t vertexCount
,
3144 uint32_t firstInstance
,
3145 uint32_t instanceCount
)
3147 struct anv_cmd_buffer
*cmd_buffer
= (struct anv_cmd_buffer
*) cmdBuffer
;
3149 anv_cmd_buffer_flush_state(cmd_buffer
);
3151 anv_batch_emit(&cmd_buffer
->batch
, GEN8_3DPRIMITIVE
,
3152 .VertexAccessType
= SEQUENTIAL
,
3153 .VertexCountPerInstance
= vertexCount
,
3154 .StartVertexLocation
= firstVertex
,
3155 .InstanceCount
= instanceCount
,
3156 .StartInstanceLocation
= firstInstance
,
3157 .BaseVertexLocation
= 0);
3160 void anv_CmdDrawIndexed(
3161 VkCmdBuffer cmdBuffer
,
3162 uint32_t firstIndex
,
3163 uint32_t indexCount
,
3164 int32_t vertexOffset
,
3165 uint32_t firstInstance
,
3166 uint32_t instanceCount
)
3168 struct anv_cmd_buffer
*cmd_buffer
= (struct anv_cmd_buffer
*) cmdBuffer
;
3170 anv_cmd_buffer_flush_state(cmd_buffer
);
3172 anv_batch_emit(&cmd_buffer
->batch
, GEN8_3DPRIMITIVE
,
3173 .VertexAccessType
= RANDOM
,
3174 .VertexCountPerInstance
= indexCount
,
3175 .StartVertexLocation
= firstIndex
,
3176 .InstanceCount
= instanceCount
,
3177 .StartInstanceLocation
= firstInstance
,
3178 .BaseVertexLocation
= vertexOffset
);
3182 anv_batch_lrm(struct anv_batch
*batch
,
3183 uint32_t reg
, struct anv_bo
*bo
, uint32_t offset
)
3185 anv_batch_emit(batch
, GEN8_MI_LOAD_REGISTER_MEM
,
3186 .RegisterAddress
= reg
,
3187 .MemoryAddress
= { bo
, offset
});
3191 anv_batch_lri(struct anv_batch
*batch
, uint32_t reg
, uint32_t imm
)
3193 anv_batch_emit(batch
, GEN8_MI_LOAD_REGISTER_IMM
,
3194 .RegisterOffset
= reg
,
3198 /* Auto-Draw / Indirect Registers */
3199 #define GEN7_3DPRIM_END_OFFSET 0x2420
3200 #define GEN7_3DPRIM_START_VERTEX 0x2430
3201 #define GEN7_3DPRIM_VERTEX_COUNT 0x2434
3202 #define GEN7_3DPRIM_INSTANCE_COUNT 0x2438
3203 #define GEN7_3DPRIM_START_INSTANCE 0x243C
3204 #define GEN7_3DPRIM_BASE_VERTEX 0x2440
3206 void anv_CmdDrawIndirect(
3207 VkCmdBuffer cmdBuffer
,
3209 VkDeviceSize offset
,
3213 struct anv_cmd_buffer
*cmd_buffer
= (struct anv_cmd_buffer
*) cmdBuffer
;
3214 struct anv_buffer
*buffer
= (struct anv_buffer
*) _buffer
;
3215 struct anv_bo
*bo
= buffer
->bo
;
3216 uint32_t bo_offset
= buffer
->offset
+ offset
;
3218 anv_cmd_buffer_flush_state(cmd_buffer
);
3220 anv_batch_lrm(&cmd_buffer
->batch
, GEN7_3DPRIM_VERTEX_COUNT
, bo
, bo_offset
);
3221 anv_batch_lrm(&cmd_buffer
->batch
, GEN7_3DPRIM_INSTANCE_COUNT
, bo
, bo_offset
+ 4);
3222 anv_batch_lrm(&cmd_buffer
->batch
, GEN7_3DPRIM_START_VERTEX
, bo
, bo_offset
+ 8);
3223 anv_batch_lrm(&cmd_buffer
->batch
, GEN7_3DPRIM_START_INSTANCE
, bo
, bo_offset
+ 12);
3224 anv_batch_lri(&cmd_buffer
->batch
, GEN7_3DPRIM_BASE_VERTEX
, 0);
3226 anv_batch_emit(&cmd_buffer
->batch
, GEN8_3DPRIMITIVE
,
3227 .IndirectParameterEnable
= true,
3228 .VertexAccessType
= SEQUENTIAL
);
3231 void anv_CmdDrawIndexedIndirect(
3232 VkCmdBuffer cmdBuffer
,
3234 VkDeviceSize offset
,
3238 struct anv_cmd_buffer
*cmd_buffer
= (struct anv_cmd_buffer
*) cmdBuffer
;
3239 struct anv_buffer
*buffer
= (struct anv_buffer
*) _buffer
;
3240 struct anv_bo
*bo
= buffer
->bo
;
3241 uint32_t bo_offset
= buffer
->offset
+ offset
;
3243 anv_cmd_buffer_flush_state(cmd_buffer
);
3245 anv_batch_lrm(&cmd_buffer
->batch
, GEN7_3DPRIM_VERTEX_COUNT
, bo
, bo_offset
);
3246 anv_batch_lrm(&cmd_buffer
->batch
, GEN7_3DPRIM_INSTANCE_COUNT
, bo
, bo_offset
+ 4);
3247 anv_batch_lrm(&cmd_buffer
->batch
, GEN7_3DPRIM_START_VERTEX
, bo
, bo_offset
+ 8);
3248 anv_batch_lrm(&cmd_buffer
->batch
, GEN7_3DPRIM_BASE_VERTEX
, bo
, bo_offset
+ 12);
3249 anv_batch_lrm(&cmd_buffer
->batch
, GEN7_3DPRIM_START_INSTANCE
, bo
, bo_offset
+ 16);
3251 anv_batch_emit(&cmd_buffer
->batch
, GEN8_3DPRIMITIVE
,
3252 .IndirectParameterEnable
= true,
3253 .VertexAccessType
= RANDOM
);
3256 void anv_CmdDispatch(
3257 VkCmdBuffer cmdBuffer
,
3265 void anv_CmdDispatchIndirect(
3266 VkCmdBuffer cmdBuffer
,
3268 VkDeviceSize offset
)
3273 void anv_CmdSetEvent(
3274 VkCmdBuffer cmdBuffer
,
3276 VkPipeEvent pipeEvent
)
3281 void anv_CmdResetEvent(
3282 VkCmdBuffer cmdBuffer
,
3284 VkPipeEvent pipeEvent
)
3289 void anv_CmdWaitEvents(
3290 VkCmdBuffer cmdBuffer
,
3291 VkWaitEvent waitEvent
,
3292 uint32_t eventCount
,
3293 const VkEvent
* pEvents
,
3294 uint32_t memBarrierCount
,
3295 const void** ppMemBarriers
)
3300 void anv_CmdPipelineBarrier(
3301 VkCmdBuffer cmdBuffer
,
3302 VkWaitEvent waitEvent
,
3303 uint32_t pipeEventCount
,
3304 const VkPipeEvent
* pPipeEvents
,
3305 uint32_t memBarrierCount
,
3306 const void** ppMemBarriers
)
3311 void anv_CmdInitAtomicCounters(
3312 VkCmdBuffer cmdBuffer
,
3313 VkPipelineBindPoint pipelineBindPoint
,
3314 uint32_t startCounter
,
3315 uint32_t counterCount
,
3316 const uint32_t* pData
)
3321 void anv_CmdLoadAtomicCounters(
3322 VkCmdBuffer cmdBuffer
,
3323 VkPipelineBindPoint pipelineBindPoint
,
3324 uint32_t startCounter
,
3325 uint32_t counterCount
,
3327 VkDeviceSize srcOffset
)
3332 void anv_CmdSaveAtomicCounters(
3333 VkCmdBuffer cmdBuffer
,
3334 VkPipelineBindPoint pipelineBindPoint
,
3335 uint32_t startCounter
,
3336 uint32_t counterCount
,
3337 VkBuffer destBuffer
,
3338 VkDeviceSize destOffset
)
3344 anv_framebuffer_destroy(struct anv_device
*device
,
3345 struct anv_object
*object
,
3346 VkObjectType obj_type
)
3348 struct anv_framebuffer
*fb
= (struct anv_framebuffer
*)object
;
3350 assert(obj_type
== VK_OBJECT_TYPE_FRAMEBUFFER
);
3352 anv_DestroyObject((VkDevice
) device
,
3353 VK_OBJECT_TYPE_DYNAMIC_VP_STATE
,
3356 anv_device_free(device
, fb
);
3359 VkResult
anv_CreateFramebuffer(
3361 const VkFramebufferCreateInfo
* pCreateInfo
,
3362 VkFramebuffer
* pFramebuffer
)
3364 struct anv_device
*device
= (struct anv_device
*) _device
;
3365 struct anv_framebuffer
*framebuffer
;
3367 static const struct anv_depth_stencil_view null_view
=
3368 { .depth_format
= D16_UNORM
, .depth_stride
= 0, .stencil_stride
= 0 };
3370 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO
);
3372 framebuffer
= anv_device_alloc(device
, sizeof(*framebuffer
), 8,
3373 VK_SYSTEM_ALLOC_TYPE_API_OBJECT
);
3374 if (framebuffer
== NULL
)
3375 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
3377 framebuffer
->base
.destructor
= anv_framebuffer_destroy
;
3379 framebuffer
->color_attachment_count
= pCreateInfo
->colorAttachmentCount
;
3380 for (uint32_t i
= 0; i
< pCreateInfo
->colorAttachmentCount
; i
++) {
3381 framebuffer
->color_attachments
[i
] =
3382 (struct anv_surface_view
*) pCreateInfo
->pColorAttachments
[i
].view
;
3385 if (pCreateInfo
->pDepthStencilAttachment
) {
3386 framebuffer
->depth_stencil
=
3387 (struct anv_depth_stencil_view
*) pCreateInfo
->pDepthStencilAttachment
->view
;
3389 framebuffer
->depth_stencil
= &null_view
;
3392 framebuffer
->sample_count
= pCreateInfo
->sampleCount
;
3393 framebuffer
->width
= pCreateInfo
->width
;
3394 framebuffer
->height
= pCreateInfo
->height
;
3395 framebuffer
->layers
= pCreateInfo
->layers
;
3397 vkCreateDynamicViewportState((VkDevice
) device
,
3398 &(VkDynamicVpStateCreateInfo
) {
3399 .sType
= VK_STRUCTURE_TYPE_DYNAMIC_VP_STATE_CREATE_INFO
,
3400 .viewportAndScissorCount
= 1,
3401 .pViewports
= (VkViewport
[]) {
3405 .width
= pCreateInfo
->width
,
3406 .height
= pCreateInfo
->height
,
3411 .pScissors
= (VkRect
[]) {
3413 { pCreateInfo
->width
, pCreateInfo
->height
} },
3416 &framebuffer
->vp_state
);
3418 *pFramebuffer
= (VkFramebuffer
) framebuffer
;
3423 VkResult
anv_CreateRenderPass(
3425 const VkRenderPassCreateInfo
* pCreateInfo
,
3426 VkRenderPass
* pRenderPass
)
3428 struct anv_device
*device
= (struct anv_device
*) _device
;
3429 struct anv_render_pass
*pass
;
3432 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO
);
3434 size
= sizeof(*pass
) +
3435 pCreateInfo
->layers
* sizeof(struct anv_render_pass_layer
);
3436 pass
= anv_device_alloc(device
, size
, 8,
3437 VK_SYSTEM_ALLOC_TYPE_API_OBJECT
);
3439 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
3441 pass
->render_area
= pCreateInfo
->renderArea
;
3443 pass
->num_layers
= pCreateInfo
->layers
;
3445 pass
->num_clear_layers
= 0;
3446 for (uint32_t i
= 0; i
< pCreateInfo
->layers
; i
++) {
3447 pass
->layers
[i
].color_load_op
= pCreateInfo
->pColorLoadOps
[i
];
3448 pass
->layers
[i
].clear_color
= pCreateInfo
->pColorLoadClearValues
[i
];
3449 if (pass
->layers
[i
].color_load_op
== VK_ATTACHMENT_LOAD_OP_CLEAR
)
3450 pass
->num_clear_layers
++;
3453 *pRenderPass
= (VkRenderPass
) pass
;
3459 anv_cmd_buffer_emit_depth_stencil(struct anv_cmd_buffer
*cmd_buffer
,
3460 struct anv_render_pass
*pass
)
3462 const struct anv_depth_stencil_view
*view
=
3463 cmd_buffer
->framebuffer
->depth_stencil
;
3465 /* FIXME: Implement the PMA stall W/A */
3467 anv_batch_emit(&cmd_buffer
->batch
, GEN8_3DSTATE_DEPTH_BUFFER
,
3468 .SurfaceType
= SURFTYPE_2D
,
3469 .DepthWriteEnable
= view
->depth_stride
> 0,
3470 .StencilWriteEnable
= view
->stencil_stride
> 0,
3471 .HierarchicalDepthBufferEnable
= false,
3472 .SurfaceFormat
= view
->depth_format
,
3473 .SurfacePitch
= view
->depth_stride
> 0 ? view
->depth_stride
- 1 : 0,
3474 .SurfaceBaseAddress
= { view
->bo
, view
->depth_offset
},
3475 .Height
= pass
->render_area
.extent
.height
- 1,
3476 .Width
= pass
->render_area
.extent
.width
- 1,
3479 .MinimumArrayElement
= 0,
3480 .DepthBufferObjectControlState
= GEN8_MOCS
,
3481 .RenderTargetViewExtent
= 1 - 1,
3482 .SurfaceQPitch
= 0);
3484 /* Disable hierarchial depth buffers. */
3485 anv_batch_emit(&cmd_buffer
->batch
, GEN8_3DSTATE_HIER_DEPTH_BUFFER
);
3487 anv_batch_emit(&cmd_buffer
->batch
, GEN8_3DSTATE_STENCIL_BUFFER
,
3488 .StencilBufferEnable
= view
->stencil_stride
> 0,
3489 .StencilBufferObjectControlState
= GEN8_MOCS
,
3490 .SurfacePitch
= view
->stencil_stride
> 0 ? view
->stencil_stride
- 1 : 0,
3491 .SurfaceBaseAddress
= { view
->bo
, view
->stencil_offset
},
3492 .SurfaceQPitch
= 0);
3494 /* Clear the clear params. */
3495 anv_batch_emit(&cmd_buffer
->batch
, GEN8_3DSTATE_CLEAR_PARAMS
);
3498 void anv_CmdBeginRenderPass(
3499 VkCmdBuffer cmdBuffer
,
3500 const VkRenderPassBegin
* pRenderPassBegin
)
3502 struct anv_cmd_buffer
*cmd_buffer
= (struct anv_cmd_buffer
*) cmdBuffer
;
3503 struct anv_render_pass
*pass
= (struct anv_render_pass
*) pRenderPassBegin
->renderPass
;
3504 struct anv_framebuffer
*framebuffer
=
3505 (struct anv_framebuffer
*) pRenderPassBegin
->framebuffer
;
3507 cmd_buffer
->framebuffer
= framebuffer
;
3509 cmd_buffer
->descriptors_dirty
|= VK_SHADER_STAGE_FRAGMENT_BIT
;
3511 anv_batch_emit(&cmd_buffer
->batch
, GEN8_3DSTATE_DRAWING_RECTANGLE
,
3512 .ClippedDrawingRectangleYMin
= pass
->render_area
.offset
.y
,
3513 .ClippedDrawingRectangleXMin
= pass
->render_area
.offset
.x
,
3514 .ClippedDrawingRectangleYMax
=
3515 pass
->render_area
.offset
.y
+ pass
->render_area
.extent
.height
- 1,
3516 .ClippedDrawingRectangleXMax
=
3517 pass
->render_area
.offset
.x
+ pass
->render_area
.extent
.width
- 1,
3518 .DrawingRectangleOriginY
= 0,
3519 .DrawingRectangleOriginX
= 0);
3521 anv_cmd_buffer_emit_depth_stencil(cmd_buffer
, pass
);
3523 anv_cmd_buffer_clear(cmd_buffer
, pass
);
3526 void anv_CmdEndRenderPass(
3527 VkCmdBuffer cmdBuffer
,
3528 VkRenderPass renderPass
)
3530 /* Emit a flushing pipe control at the end of a pass. This is kind of a
3531 * hack but it ensures that render targets always actually get written.
3532 * Eventually, we should do flushing based on image format transitions
3533 * or something of that nature.
3535 struct anv_cmd_buffer
*cmd_buffer
= (struct anv_cmd_buffer
*)cmdBuffer
;
3536 anv_batch_emit(&cmd_buffer
->batch
, GEN8_PIPE_CONTROL
,
3537 .PostSyncOperation
= NoWrite
,
3538 .RenderTargetCacheFlushEnable
= true,
3539 .InstructionCacheInvalidateEnable
= true,
3540 .DepthCacheFlushEnable
= true,
3541 .VFCacheInvalidationEnable
= true,
3542 .TextureCacheInvalidationEnable
= true,
3543 .CommandStreamerStallEnable
= true);
3546 void vkCmdDbgMarkerBegin(
3547 VkCmdBuffer cmdBuffer
,
3548 const char* pMarker
)
3549 __attribute__ ((visibility ("default")));
3551 void vkCmdDbgMarkerEnd(
3552 VkCmdBuffer cmdBuffer
)
3553 __attribute__ ((visibility ("default")));
3555 VkResult
vkDbgSetObjectTag(
3560 __attribute__ ((visibility ("default")));
3563 void vkCmdDbgMarkerBegin(
3564 VkCmdBuffer cmdBuffer
,
3565 const char* pMarker
)
3569 void vkCmdDbgMarkerEnd(
3570 VkCmdBuffer cmdBuffer
)
3574 VkResult
vkDbgSetObjectTag(