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);
378 /* Binding table pointers are only 16 bits so we have to make sure that
379 * they get allocated at the beginning of the surface state BO. To
380 * handle this, we create a separate block pool that works out of the
381 * first 64 KB of the surface state BO.
383 anv_block_pool_init_slave(&device
->binding_table_block_pool
,
384 &device
->surface_state_block_pool
, 32);
386 anv_state_pool_init(&device
->surface_state_pool
,
387 &device
->surface_state_block_pool
);
389 device
->compiler
= anv_compiler_create(device
->fd
);
390 device
->aub_writer
= NULL
;
392 device
->info
= *physicalDevice
->info
;
394 pthread_mutex_init(&device
->mutex
, NULL
);
396 anv_device_init_meta(device
);
398 anv_device_init_border_colors(device
);
400 *pDevice
= (VkDevice
) device
;
407 anv_device_free(device
, device
);
409 return vk_error(VK_ERROR_UNAVAILABLE
);
412 VkResult
anv_DestroyDevice(
415 struct anv_device
*device
= (struct anv_device
*) _device
;
417 anv_compiler_destroy(device
->compiler
);
420 anv_bo_pool_finish(&device
->batch_bo_pool
);
421 anv_block_pool_finish(&device
->dynamic_state_block_pool
);
422 anv_block_pool_finish(&device
->instruction_block_pool
);
423 anv_block_pool_finish(&device
->surface_state_block_pool
);
427 if (device
->aub_writer
)
428 anv_aub_writer_destroy(device
->aub_writer
);
430 anv_device_free(device
, device
);
435 VkResult
anv_GetGlobalExtensionInfo(
436 VkExtensionInfoType infoType
,
437 uint32_t extensionIndex
,
441 static const VkExtensionProperties extensions
[] = {
443 .extName
= "VK_WSI_LunarG",
447 uint32_t count
= ARRAY_SIZE(extensions
);
450 case VK_EXTENSION_INFO_TYPE_COUNT
:
451 memcpy(pData
, &count
, sizeof(count
));
452 *pDataSize
= sizeof(count
);
455 case VK_EXTENSION_INFO_TYPE_PROPERTIES
:
456 if (extensionIndex
>= count
)
457 return vk_error(VK_ERROR_INVALID_EXTENSION
);
459 memcpy(pData
, &extensions
[extensionIndex
], sizeof(extensions
[0]));
460 *pDataSize
= sizeof(extensions
[0]);
464 return VK_UNSUPPORTED
;
468 VkResult
anv_GetPhysicalDeviceExtensionInfo(
469 VkPhysicalDevice physicalDevice
,
470 VkExtensionInfoType infoType
,
471 uint32_t extensionIndex
,
478 case VK_EXTENSION_INFO_TYPE_COUNT
:
487 case VK_EXTENSION_INFO_TYPE_PROPERTIES
:
488 return vk_error(VK_ERROR_INVALID_EXTENSION
);
491 return VK_UNSUPPORTED
;
495 VkResult
anv_EnumerateLayers(
496 VkPhysicalDevice physicalDevice
,
497 size_t maxStringSize
,
499 char* const* pOutLayers
,
507 VkResult
anv_GetDeviceQueue(
509 uint32_t queueNodeIndex
,
513 struct anv_device
*device
= (struct anv_device
*) _device
;
514 struct anv_queue
*queue
;
516 /* FIXME: Should allocate these at device create time. */
518 queue
= anv_device_alloc(device
, sizeof(*queue
), 8,
519 VK_SYSTEM_ALLOC_TYPE_API_OBJECT
);
521 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
523 queue
->device
= device
;
524 queue
->pool
= &device
->surface_state_pool
;
526 queue
->completed_serial
= anv_state_pool_alloc(queue
->pool
, 4, 4);
527 *(uint32_t *)queue
->completed_serial
.map
= 0;
528 queue
->next_serial
= 1;
530 *pQueue
= (VkQueue
) queue
;
536 anv_reloc_list_init(struct anv_reloc_list
*list
, struct anv_device
*device
)
538 list
->num_relocs
= 0;
539 list
->array_length
= 256;
541 anv_device_alloc(device
, list
->array_length
* sizeof(*list
->relocs
), 8,
542 VK_SYSTEM_ALLOC_TYPE_INTERNAL
);
544 if (list
->relocs
== NULL
)
545 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
548 anv_device_alloc(device
, list
->array_length
* sizeof(*list
->reloc_bos
), 8,
549 VK_SYSTEM_ALLOC_TYPE_INTERNAL
);
551 if (list
->relocs
== NULL
) {
552 anv_device_free(device
, list
->relocs
);
553 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
560 anv_reloc_list_finish(struct anv_reloc_list
*list
, struct anv_device
*device
)
562 anv_device_free(device
, list
->relocs
);
563 anv_device_free(device
, list
->reloc_bos
);
567 anv_reloc_list_grow(struct anv_reloc_list
*list
, struct anv_device
*device
,
568 size_t num_additional_relocs
)
570 if (list
->num_relocs
+ num_additional_relocs
<= list
->array_length
)
573 size_t new_length
= list
->array_length
* 2;
574 while (new_length
< list
->num_relocs
+ num_additional_relocs
)
577 struct drm_i915_gem_relocation_entry
*new_relocs
=
578 anv_device_alloc(device
, new_length
* sizeof(*list
->relocs
), 8,
579 VK_SYSTEM_ALLOC_TYPE_INTERNAL
);
580 if (new_relocs
== NULL
)
581 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
583 struct anv_bo
**new_reloc_bos
=
584 anv_device_alloc(device
, new_length
* sizeof(*list
->reloc_bos
), 8,
585 VK_SYSTEM_ALLOC_TYPE_INTERNAL
);
586 if (new_relocs
== NULL
) {
587 anv_device_free(device
, new_relocs
);
588 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
591 memcpy(new_relocs
, list
->relocs
, list
->num_relocs
* sizeof(*list
->relocs
));
592 memcpy(new_reloc_bos
, list
->reloc_bos
,
593 list
->num_relocs
* sizeof(*list
->reloc_bos
));
595 anv_device_free(device
, list
->relocs
);
596 anv_device_free(device
, list
->reloc_bos
);
598 list
->relocs
= new_relocs
;
599 list
->reloc_bos
= new_reloc_bos
;
605 anv_batch_bo_create(struct anv_device
*device
, struct anv_batch_bo
**bbo_out
)
609 struct anv_batch_bo
*bbo
=
610 anv_device_alloc(device
, sizeof(*bbo
), 8, VK_SYSTEM_ALLOC_TYPE_INTERNAL
);
612 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
615 bbo
->prev_batch_bo
= NULL
;
617 result
= anv_bo_pool_alloc(&device
->batch_bo_pool
, &bbo
->bo
);
618 if (result
!= VK_SUCCESS
) {
619 anv_device_free(device
, bbo
);
629 anv_batch_bo_start(struct anv_batch_bo
*bbo
, struct anv_batch
*batch
,
630 size_t batch_padding
)
632 batch
->next
= batch
->start
= bbo
->bo
.map
;
633 batch
->end
= bbo
->bo
.map
+ bbo
->bo
.size
- batch_padding
;
634 bbo
->first_reloc
= batch
->relocs
.num_relocs
;
638 anv_batch_bo_finish(struct anv_batch_bo
*bbo
, struct anv_batch
*batch
)
640 assert(batch
->start
== bbo
->bo
.map
);
641 bbo
->length
= batch
->next
- batch
->start
;
642 bbo
->num_relocs
= batch
->relocs
.num_relocs
- bbo
->first_reloc
;
646 anv_batch_bo_destroy(struct anv_batch_bo
*bbo
, struct anv_device
*device
)
648 anv_bo_pool_free(&device
->batch_bo_pool
, &bbo
->bo
);
649 anv_device_free(device
, bbo
);
653 anv_batch_emit_dwords(struct anv_batch
*batch
, int num_dwords
)
655 if (batch
->next
+ num_dwords
* 4 > batch
->end
)
656 batch
->extend_cb(batch
, batch
->user_data
);
658 void *p
= batch
->next
;
660 batch
->next
+= num_dwords
* 4;
661 assert(batch
->next
<= batch
->end
);
667 anv_reloc_list_append(struct anv_reloc_list
*list
, struct anv_device
*device
,
668 struct anv_reloc_list
*other
, uint32_t offset
)
670 anv_reloc_list_grow(list
, device
, other
->num_relocs
);
671 /* TODO: Handle failure */
673 memcpy(&list
->relocs
[list
->num_relocs
], &other
->relocs
[0],
674 other
->num_relocs
* sizeof(other
->relocs
[0]));
675 memcpy(&list
->reloc_bos
[list
->num_relocs
], &other
->reloc_bos
[0],
676 other
->num_relocs
* sizeof(other
->reloc_bos
[0]));
678 for (uint32_t i
= 0; i
< other
->num_relocs
; i
++)
679 list
->relocs
[i
+ list
->num_relocs
].offset
+= offset
;
681 list
->num_relocs
+= other
->num_relocs
;
685 anv_reloc_list_add(struct anv_reloc_list
*list
, struct anv_device
*device
,
686 uint32_t offset
, struct anv_bo
*target_bo
, uint32_t delta
)
688 struct drm_i915_gem_relocation_entry
*entry
;
691 anv_reloc_list_grow(list
, device
, 1);
692 /* TODO: Handle failure */
694 /* XXX: Can we use I915_EXEC_HANDLE_LUT? */
695 index
= list
->num_relocs
++;
696 list
->reloc_bos
[index
] = target_bo
;
697 entry
= &list
->relocs
[index
];
698 entry
->target_handle
= target_bo
->gem_handle
;
699 entry
->delta
= delta
;
700 entry
->offset
= offset
;
701 entry
->presumed_offset
= target_bo
->offset
;
702 entry
->read_domains
= 0;
703 entry
->write_domain
= 0;
705 return target_bo
->offset
+ delta
;
709 anv_batch_emit_batch(struct anv_batch
*batch
, struct anv_batch
*other
)
711 uint32_t size
, offset
;
713 size
= other
->next
- other
->start
;
714 assert(size
% 4 == 0);
716 if (batch
->next
+ size
> batch
->end
)
717 batch
->extend_cb(batch
, batch
->user_data
);
719 assert(batch
->next
+ size
<= batch
->end
);
721 memcpy(batch
->next
, other
->start
, size
);
723 offset
= batch
->next
- batch
->start
;
724 anv_reloc_list_append(&batch
->relocs
, batch
->device
,
725 &other
->relocs
, offset
);
731 anv_batch_emit_reloc(struct anv_batch
*batch
,
732 void *location
, struct anv_bo
*bo
, uint32_t delta
)
734 return anv_reloc_list_add(&batch
->relocs
, batch
->device
,
735 location
- batch
->start
, bo
, delta
);
738 VkResult
anv_QueueSubmit(
740 uint32_t cmdBufferCount
,
741 const VkCmdBuffer
* pCmdBuffers
,
744 struct anv_queue
*queue
= (struct anv_queue
*) _queue
;
745 struct anv_device
*device
= queue
->device
;
746 struct anv_fence
*fence
= (struct anv_fence
*) _fence
;
749 for (uint32_t i
= 0; i
< cmdBufferCount
; i
++) {
750 struct anv_cmd_buffer
*cmd_buffer
=
751 (struct anv_cmd_buffer
*) pCmdBuffers
[i
];
753 if (device
->dump_aub
)
754 anv_cmd_buffer_dump(cmd_buffer
);
756 if (!device
->no_hw
) {
757 ret
= anv_gem_execbuffer(device
, &cmd_buffer
->execbuf
);
759 return vk_error(VK_ERROR_UNKNOWN
);
762 ret
= anv_gem_execbuffer(device
, &fence
->execbuf
);
764 return vk_error(VK_ERROR_UNKNOWN
);
767 for (uint32_t i
= 0; i
< cmd_buffer
->bo_count
; i
++)
768 cmd_buffer
->exec2_bos
[i
]->offset
= cmd_buffer
->exec2_objects
[i
].offset
;
770 *(uint32_t *)queue
->completed_serial
.map
= cmd_buffer
->serial
;
777 VkResult
anv_QueueAddMemReferences(
780 const VkDeviceMemory
* pMems
)
785 VkResult
anv_QueueRemoveMemReferences(
788 const VkDeviceMemory
* pMems
)
793 VkResult
anv_QueueWaitIdle(
796 struct anv_queue
*queue
= (struct anv_queue
*) _queue
;
798 return vkDeviceWaitIdle((VkDevice
) queue
->device
);
801 VkResult
anv_DeviceWaitIdle(
804 struct anv_device
*device
= (struct anv_device
*) _device
;
805 struct anv_state state
;
806 struct anv_batch batch
;
807 struct drm_i915_gem_execbuffer2 execbuf
;
808 struct drm_i915_gem_exec_object2 exec2_objects
[1];
809 struct anv_bo
*bo
= NULL
;
814 state
= anv_state_pool_alloc(&device
->dynamic_state_pool
, 32, 32);
815 bo
= &device
->dynamic_state_pool
.block_pool
->bo
;
816 batch
.start
= batch
.next
= state
.map
;
817 batch
.end
= state
.map
+ 32;
818 anv_batch_emit(&batch
, GEN8_MI_BATCH_BUFFER_END
);
819 anv_batch_emit(&batch
, GEN8_MI_NOOP
);
821 exec2_objects
[0].handle
= bo
->gem_handle
;
822 exec2_objects
[0].relocation_count
= 0;
823 exec2_objects
[0].relocs_ptr
= 0;
824 exec2_objects
[0].alignment
= 0;
825 exec2_objects
[0].offset
= bo
->offset
;
826 exec2_objects
[0].flags
= 0;
827 exec2_objects
[0].rsvd1
= 0;
828 exec2_objects
[0].rsvd2
= 0;
830 execbuf
.buffers_ptr
= (uintptr_t) exec2_objects
;
831 execbuf
.buffer_count
= 1;
832 execbuf
.batch_start_offset
= state
.offset
;
833 execbuf
.batch_len
= batch
.next
- state
.map
;
834 execbuf
.cliprects_ptr
= 0;
835 execbuf
.num_cliprects
= 0;
840 I915_EXEC_HANDLE_LUT
| I915_EXEC_NO_RELOC
| I915_EXEC_RENDER
;
841 execbuf
.rsvd1
= device
->context_id
;
844 if (!device
->no_hw
) {
845 ret
= anv_gem_execbuffer(device
, &execbuf
);
847 result
= vk_error(VK_ERROR_UNKNOWN
);
852 ret
= anv_gem_wait(device
, bo
->gem_handle
, &timeout
);
854 result
= vk_error(VK_ERROR_UNKNOWN
);
859 anv_state_pool_free(&device
->dynamic_state_pool
, state
);
864 anv_state_pool_free(&device
->dynamic_state_pool
, state
);
870 anv_device_alloc(struct anv_device
* device
,
873 VkSystemAllocType allocType
)
875 return device
->instance
->pfnAlloc(device
->instance
->pAllocUserData
,
882 anv_device_free(struct anv_device
* device
,
885 return device
->instance
->pfnFree(device
->instance
->pAllocUserData
,
890 anv_bo_init_new(struct anv_bo
*bo
, struct anv_device
*device
, uint64_t size
)
892 bo
->gem_handle
= anv_gem_create(device
, size
);
894 return vk_error(VK_ERROR_OUT_OF_DEVICE_MEMORY
);
904 VkResult
anv_AllocMemory(
906 const VkMemoryAllocInfo
* pAllocInfo
,
907 VkDeviceMemory
* pMem
)
909 struct anv_device
*device
= (struct anv_device
*) _device
;
910 struct anv_device_memory
*mem
;
913 assert(pAllocInfo
->sType
== VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO
);
915 mem
= anv_device_alloc(device
, sizeof(*mem
), 8,
916 VK_SYSTEM_ALLOC_TYPE_API_OBJECT
);
918 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
920 result
= anv_bo_init_new(&mem
->bo
, device
, pAllocInfo
->allocationSize
);
921 if (result
!= VK_SUCCESS
)
924 *pMem
= (VkDeviceMemory
) mem
;
929 anv_device_free(device
, mem
);
934 VkResult
anv_FreeMemory(
938 struct anv_device
*device
= (struct anv_device
*) _device
;
939 struct anv_device_memory
*mem
= (struct anv_device_memory
*) _mem
;
942 anv_gem_munmap(mem
->bo
.map
, mem
->bo
.size
);
944 if (mem
->bo
.gem_handle
!= 0)
945 anv_gem_close(device
, mem
->bo
.gem_handle
);
947 anv_device_free(device
, mem
);
952 VkResult
anv_SetMemoryPriority(
955 VkMemoryPriority priority
)
960 VkResult
anv_MapMemory(
965 VkMemoryMapFlags flags
,
968 struct anv_device
*device
= (struct anv_device
*) _device
;
969 struct anv_device_memory
*mem
= (struct anv_device_memory
*) _mem
;
971 /* FIXME: Is this supposed to be thread safe? Since vkUnmapMemory() only
972 * takes a VkDeviceMemory pointer, it seems like only one map of the memory
973 * at a time is valid. We could just mmap up front and return an offset
974 * pointer here, but that may exhaust virtual memory on 32 bit
977 mem
->map
= anv_gem_mmap(device
, mem
->bo
.gem_handle
, offset
, size
);
978 mem
->map_size
= size
;
985 VkResult
anv_UnmapMemory(
989 struct anv_device_memory
*mem
= (struct anv_device_memory
*) _mem
;
991 anv_gem_munmap(mem
->map
, mem
->map_size
);
996 VkResult
anv_FlushMappedMemory(
1002 /* clflush here for !llc platforms */
1007 VkResult
anv_PinSystemMemory(
1009 const void* pSysMem
,
1011 VkDeviceMemory
* pMem
)
1016 VkResult
anv_GetMultiDeviceCompatibility(
1017 VkPhysicalDevice physicalDevice0
,
1018 VkPhysicalDevice physicalDevice1
,
1019 VkPhysicalDeviceCompatibilityInfo
* pInfo
)
1021 return VK_UNSUPPORTED
;
1024 VkResult
anv_OpenSharedMemory(
1026 const VkMemoryOpenInfo
* pOpenInfo
,
1027 VkDeviceMemory
* pMem
)
1029 return VK_UNSUPPORTED
;
1032 VkResult
anv_OpenSharedSemaphore(
1034 const VkSemaphoreOpenInfo
* pOpenInfo
,
1035 VkSemaphore
* pSemaphore
)
1037 return VK_UNSUPPORTED
;
1040 VkResult
anv_OpenPeerMemory(
1042 const VkPeerMemoryOpenInfo
* pOpenInfo
,
1043 VkDeviceMemory
* pMem
)
1045 return VK_UNSUPPORTED
;
1048 VkResult
anv_OpenPeerImage(
1050 const VkPeerImageOpenInfo
* pOpenInfo
,
1052 VkDeviceMemory
* pMem
)
1054 return VK_UNSUPPORTED
;
1057 VkResult
anv_DestroyObject(
1059 VkObjectType objType
,
1062 struct anv_device
*device
= (struct anv_device
*) _device
;
1063 struct anv_object
*object
= (struct anv_object
*) _object
;
1066 case VK_OBJECT_TYPE_INSTANCE
:
1067 return anv_DestroyInstance((VkInstance
) _object
);
1069 case VK_OBJECT_TYPE_PHYSICAL_DEVICE
:
1070 /* We don't want to actually destroy physical devices */
1073 case VK_OBJECT_TYPE_DEVICE
:
1074 assert(_device
== (VkDevice
) _object
);
1075 return anv_DestroyDevice((VkDevice
) _object
);
1077 case VK_OBJECT_TYPE_QUEUE
:
1081 case VK_OBJECT_TYPE_DEVICE_MEMORY
:
1082 return anv_FreeMemory(_device
, (VkDeviceMemory
) _object
);
1084 case VK_OBJECT_TYPE_DESCRIPTOR_POOL
:
1085 /* These are just dummys anyway, so we don't need to destroy them */
1088 case VK_OBJECT_TYPE_BUFFER
:
1089 case VK_OBJECT_TYPE_BUFFER_VIEW
:
1090 case VK_OBJECT_TYPE_IMAGE
:
1091 case VK_OBJECT_TYPE_IMAGE_VIEW
:
1092 case VK_OBJECT_TYPE_COLOR_ATTACHMENT_VIEW
:
1093 case VK_OBJECT_TYPE_DEPTH_STENCIL_VIEW
:
1094 case VK_OBJECT_TYPE_SHADER
:
1095 case VK_OBJECT_TYPE_PIPELINE_LAYOUT
:
1096 case VK_OBJECT_TYPE_SAMPLER
:
1097 case VK_OBJECT_TYPE_DESCRIPTOR_SET
:
1098 case VK_OBJECT_TYPE_DESCRIPTOR_SET_LAYOUT
:
1099 case VK_OBJECT_TYPE_DYNAMIC_RS_STATE
:
1100 case VK_OBJECT_TYPE_DYNAMIC_CB_STATE
:
1101 case VK_OBJECT_TYPE_DYNAMIC_DS_STATE
:
1102 case VK_OBJECT_TYPE_RENDER_PASS
:
1103 /* These are trivially destroyable */
1104 anv_device_free(device
, (void *) _object
);
1107 case VK_OBJECT_TYPE_COMMAND_BUFFER
:
1108 case VK_OBJECT_TYPE_PIPELINE
:
1109 case VK_OBJECT_TYPE_DYNAMIC_VP_STATE
:
1110 case VK_OBJECT_TYPE_FENCE
:
1111 case VK_OBJECT_TYPE_QUERY_POOL
:
1112 case VK_OBJECT_TYPE_FRAMEBUFFER
:
1113 (object
->destructor
)(device
, object
, objType
);
1116 case VK_OBJECT_TYPE_SEMAPHORE
:
1117 case VK_OBJECT_TYPE_EVENT
:
1118 stub_return(VK_UNSUPPORTED
);
1121 unreachable("Invalid object type");
1126 fill_memory_requirements(
1127 VkObjectType objType
,
1129 VkMemoryRequirements
* memory_requirements
)
1131 struct anv_buffer
*buffer
;
1132 struct anv_image
*image
;
1134 memory_requirements
->memPropsAllowed
=
1135 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT
|
1136 VK_MEMORY_PROPERTY_HOST_DEVICE_COHERENT_BIT
|
1137 /* VK_MEMORY_PROPERTY_HOST_UNCACHED_BIT | */
1138 VK_MEMORY_PROPERTY_HOST_WRITE_COMBINED_BIT
|
1139 VK_MEMORY_PROPERTY_PREFER_HOST_LOCAL
|
1140 VK_MEMORY_PROPERTY_SHAREABLE_BIT
;
1142 memory_requirements
->memPropsRequired
= 0;
1145 case VK_OBJECT_TYPE_BUFFER
:
1146 buffer
= (struct anv_buffer
*) object
;
1147 memory_requirements
->size
= buffer
->size
;
1148 memory_requirements
->alignment
= 16;
1150 case VK_OBJECT_TYPE_IMAGE
:
1151 image
= (struct anv_image
*) object
;
1152 memory_requirements
->size
= image
->size
;
1153 memory_requirements
->alignment
= image
->alignment
;
1156 memory_requirements
->size
= 0;
1162 get_allocation_count(VkObjectType objType
)
1165 case VK_OBJECT_TYPE_BUFFER
:
1166 case VK_OBJECT_TYPE_IMAGE
:
1173 VkResult
anv_GetObjectInfo(
1175 VkObjectType objType
,
1177 VkObjectInfoType infoType
,
1181 VkMemoryRequirements memory_requirements
;
1185 case VK_OBJECT_INFO_TYPE_MEMORY_REQUIREMENTS
:
1186 *pDataSize
= sizeof(memory_requirements
);
1190 fill_memory_requirements(objType
, object
, pData
);
1193 case VK_OBJECT_INFO_TYPE_MEMORY_ALLOCATION_COUNT
:
1194 *pDataSize
= sizeof(count
);
1199 *count
= get_allocation_count(objType
);
1203 return VK_UNSUPPORTED
;
1208 VkResult
anv_QueueBindObjectMemory(
1210 VkObjectType objType
,
1212 uint32_t allocationIdx
,
1213 VkDeviceMemory _mem
,
1214 VkDeviceSize memOffset
)
1216 struct anv_buffer
*buffer
;
1217 struct anv_image
*image
;
1218 struct anv_device_memory
*mem
= (struct anv_device_memory
*) _mem
;
1221 case VK_OBJECT_TYPE_BUFFER
:
1222 buffer
= (struct anv_buffer
*) object
;
1223 buffer
->bo
= &mem
->bo
;
1224 buffer
->offset
= memOffset
;
1226 case VK_OBJECT_TYPE_IMAGE
:
1227 image
= (struct anv_image
*) object
;
1228 image
->bo
= &mem
->bo
;
1229 image
->offset
= memOffset
;
1238 VkResult
anv_QueueBindObjectMemoryRange(
1240 VkObjectType objType
,
1242 uint32_t allocationIdx
,
1243 VkDeviceSize rangeOffset
,
1244 VkDeviceSize rangeSize
,
1246 VkDeviceSize memOffset
)
1248 stub_return(VK_UNSUPPORTED
);
1251 VkResult
anv_QueueBindImageMemoryRange(
1254 uint32_t allocationIdx
,
1255 const VkImageMemoryBindInfo
* pBindInfo
,
1257 VkDeviceSize memOffset
)
1259 stub_return(VK_UNSUPPORTED
);
1263 anv_fence_destroy(struct anv_device
*device
,
1264 struct anv_object
*object
,
1265 VkObjectType obj_type
)
1267 struct anv_fence
*fence
= (struct anv_fence
*) object
;
1269 assert(obj_type
== VK_OBJECT_TYPE_FENCE
);
1271 anv_gem_munmap(fence
->bo
.map
, fence
->bo
.size
);
1272 anv_gem_close(device
, fence
->bo
.gem_handle
);
1273 anv_device_free(device
, fence
);
1276 VkResult
anv_CreateFence(
1278 const VkFenceCreateInfo
* pCreateInfo
,
1281 struct anv_device
*device
= (struct anv_device
*) _device
;
1282 struct anv_fence
*fence
;
1283 struct anv_batch batch
;
1286 const uint32_t fence_size
= 128;
1288 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_FENCE_CREATE_INFO
);
1290 fence
= anv_device_alloc(device
, sizeof(*fence
), 8,
1291 VK_SYSTEM_ALLOC_TYPE_API_OBJECT
);
1293 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
1295 result
= anv_bo_init_new(&fence
->bo
, device
, fence_size
);
1296 if (result
!= VK_SUCCESS
)
1299 fence
->base
.destructor
= anv_fence_destroy
;
1302 anv_gem_mmap(device
, fence
->bo
.gem_handle
, 0, fence
->bo
.size
);
1303 batch
.next
= batch
.start
= fence
->bo
.map
;
1304 batch
.end
= fence
->bo
.map
+ fence
->bo
.size
;
1305 anv_batch_emit(&batch
, GEN8_MI_BATCH_BUFFER_END
);
1306 anv_batch_emit(&batch
, GEN8_MI_NOOP
);
1308 fence
->exec2_objects
[0].handle
= fence
->bo
.gem_handle
;
1309 fence
->exec2_objects
[0].relocation_count
= 0;
1310 fence
->exec2_objects
[0].relocs_ptr
= 0;
1311 fence
->exec2_objects
[0].alignment
= 0;
1312 fence
->exec2_objects
[0].offset
= fence
->bo
.offset
;
1313 fence
->exec2_objects
[0].flags
= 0;
1314 fence
->exec2_objects
[0].rsvd1
= 0;
1315 fence
->exec2_objects
[0].rsvd2
= 0;
1317 fence
->execbuf
.buffers_ptr
= (uintptr_t) fence
->exec2_objects
;
1318 fence
->execbuf
.buffer_count
= 1;
1319 fence
->execbuf
.batch_start_offset
= 0;
1320 fence
->execbuf
.batch_len
= batch
.next
- fence
->bo
.map
;
1321 fence
->execbuf
.cliprects_ptr
= 0;
1322 fence
->execbuf
.num_cliprects
= 0;
1323 fence
->execbuf
.DR1
= 0;
1324 fence
->execbuf
.DR4
= 0;
1326 fence
->execbuf
.flags
=
1327 I915_EXEC_HANDLE_LUT
| I915_EXEC_NO_RELOC
| I915_EXEC_RENDER
;
1328 fence
->execbuf
.rsvd1
= device
->context_id
;
1329 fence
->execbuf
.rsvd2
= 0;
1331 *pFence
= (VkQueryPool
) fence
;
1336 anv_device_free(device
, fence
);
1341 VkResult
anv_ResetFences(
1343 uint32_t fenceCount
,
1346 struct anv_fence
**fences
= (struct anv_fence
**) pFences
;
1348 for (uint32_t i
; i
< fenceCount
; i
++)
1349 fences
[i
]->ready
= false;
1354 VkResult
anv_GetFenceStatus(
1358 struct anv_device
*device
= (struct anv_device
*) _device
;
1359 struct anv_fence
*fence
= (struct anv_fence
*) _fence
;
1366 ret
= anv_gem_wait(device
, fence
->bo
.gem_handle
, &t
);
1368 fence
->ready
= true;
1372 return VK_NOT_READY
;
1375 VkResult
anv_WaitForFences(
1377 uint32_t fenceCount
,
1378 const VkFence
* pFences
,
1382 struct anv_device
*device
= (struct anv_device
*) _device
;
1383 struct anv_fence
**fences
= (struct anv_fence
**) pFences
;
1384 int64_t t
= timeout
;
1387 /* FIXME: handle !waitAll */
1389 for (uint32_t i
= 0; i
< fenceCount
; i
++) {
1390 ret
= anv_gem_wait(device
, fences
[i
]->bo
.gem_handle
, &t
);
1391 if (ret
== -1 && errno
== ETIME
)
1394 return vk_error(VK_ERROR_UNKNOWN
);
1400 // Queue semaphore functions
1402 VkResult
anv_CreateSemaphore(
1404 const VkSemaphoreCreateInfo
* pCreateInfo
,
1405 VkSemaphore
* pSemaphore
)
1407 stub_return(VK_UNSUPPORTED
);
1410 VkResult
anv_QueueSignalSemaphore(
1412 VkSemaphore semaphore
)
1414 stub_return(VK_UNSUPPORTED
);
1417 VkResult
anv_QueueWaitSemaphore(
1419 VkSemaphore semaphore
)
1421 stub_return(VK_UNSUPPORTED
);
1426 VkResult
anv_CreateEvent(
1428 const VkEventCreateInfo
* pCreateInfo
,
1431 stub_return(VK_UNSUPPORTED
);
1434 VkResult
anv_GetEventStatus(
1438 stub_return(VK_UNSUPPORTED
);
1441 VkResult
anv_SetEvent(
1445 stub_return(VK_UNSUPPORTED
);
1448 VkResult
anv_ResetEvent(
1452 stub_return(VK_UNSUPPORTED
);
1457 VkResult
anv_CreateBuffer(
1459 const VkBufferCreateInfo
* pCreateInfo
,
1462 struct anv_device
*device
= (struct anv_device
*) _device
;
1463 struct anv_buffer
*buffer
;
1465 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO
);
1467 buffer
= anv_device_alloc(device
, sizeof(*buffer
), 8,
1468 VK_SYSTEM_ALLOC_TYPE_API_OBJECT
);
1470 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
1472 buffer
->size
= pCreateInfo
->size
;
1476 *pBuffer
= (VkBuffer
) buffer
;
1481 // Buffer view functions
1484 fill_buffer_surface_state(void *state
, VkFormat format
,
1485 uint32_t offset
, uint32_t range
)
1487 const struct anv_format
*info
;
1489 info
= anv_format_for_vk_format(format
);
1490 /* This assumes RGBA float format. */
1491 uint32_t stride
= 4;
1492 uint32_t num_elements
= range
/ stride
;
1494 struct GEN8_RENDER_SURFACE_STATE surface_state
= {
1495 .SurfaceType
= SURFTYPE_BUFFER
,
1496 .SurfaceArray
= false,
1497 .SurfaceFormat
= info
->format
,
1498 .SurfaceVerticalAlignment
= VALIGN4
,
1499 .SurfaceHorizontalAlignment
= HALIGN4
,
1501 .VerticalLineStride
= 0,
1502 .VerticalLineStrideOffset
= 0,
1503 .SamplerL2BypassModeDisable
= true,
1504 .RenderCacheReadWriteMode
= WriteOnlyCache
,
1505 .MemoryObjectControlState
= GEN8_MOCS
,
1508 .Height
= (num_elements
>> 7) & 0x3fff,
1509 .Width
= num_elements
& 0x7f,
1510 .Depth
= (num_elements
>> 21) & 0x3f,
1511 .SurfacePitch
= stride
- 1,
1512 .MinimumArrayElement
= 0,
1513 .NumberofMultisamples
= MULTISAMPLECOUNT_1
,
1518 .AuxiliarySurfaceMode
= AUX_NONE
,
1520 .GreenClearColor
= 0,
1521 .BlueClearColor
= 0,
1522 .AlphaClearColor
= 0,
1523 .ShaderChannelSelectRed
= SCS_RED
,
1524 .ShaderChannelSelectGreen
= SCS_GREEN
,
1525 .ShaderChannelSelectBlue
= SCS_BLUE
,
1526 .ShaderChannelSelectAlpha
= SCS_ALPHA
,
1527 .ResourceMinLOD
= 0,
1528 /* FIXME: We assume that the image must be bound at this time. */
1529 .SurfaceBaseAddress
= { NULL
, offset
},
1532 GEN8_RENDER_SURFACE_STATE_pack(NULL
, state
, &surface_state
);
1535 VkResult
anv_CreateBufferView(
1537 const VkBufferViewCreateInfo
* pCreateInfo
,
1538 VkBufferView
* pView
)
1540 struct anv_device
*device
= (struct anv_device
*) _device
;
1541 struct anv_buffer
*buffer
= (struct anv_buffer
*) pCreateInfo
->buffer
;
1542 struct anv_surface_view
*view
;
1544 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO
);
1546 view
= anv_device_alloc(device
, sizeof(*view
), 8,
1547 VK_SYSTEM_ALLOC_TYPE_API_OBJECT
);
1549 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
1551 view
->bo
= buffer
->bo
;
1552 view
->offset
= buffer
->offset
+ pCreateInfo
->offset
;
1553 view
->surface_state
=
1554 anv_state_pool_alloc(&device
->surface_state_pool
, 64, 64);
1555 view
->format
= pCreateInfo
->format
;
1556 view
->range
= pCreateInfo
->range
;
1558 fill_buffer_surface_state(view
->surface_state
.map
,
1559 pCreateInfo
->format
, view
->offset
, pCreateInfo
->range
);
1561 *pView
= (VkImageView
) view
;
1566 // Sampler functions
1568 VkResult
anv_CreateSampler(
1570 const VkSamplerCreateInfo
* pCreateInfo
,
1571 VkSampler
* pSampler
)
1573 struct anv_device
*device
= (struct anv_device
*) _device
;
1574 struct anv_sampler
*sampler
;
1575 uint32_t mag_filter
, min_filter
, max_anisotropy
;
1577 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO
);
1579 sampler
= anv_device_alloc(device
, sizeof(*sampler
), 8,
1580 VK_SYSTEM_ALLOC_TYPE_API_OBJECT
);
1582 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
1584 static const uint32_t vk_to_gen_tex_filter
[] = {
1585 [VK_TEX_FILTER_NEAREST
] = MAPFILTER_NEAREST
,
1586 [VK_TEX_FILTER_LINEAR
] = MAPFILTER_LINEAR
1589 static const uint32_t vk_to_gen_mipmap_mode
[] = {
1590 [VK_TEX_MIPMAP_MODE_BASE
] = MIPFILTER_NONE
,
1591 [VK_TEX_MIPMAP_MODE_NEAREST
] = MIPFILTER_NEAREST
,
1592 [VK_TEX_MIPMAP_MODE_LINEAR
] = MIPFILTER_LINEAR
1595 static const uint32_t vk_to_gen_tex_address
[] = {
1596 [VK_TEX_ADDRESS_WRAP
] = TCM_WRAP
,
1597 [VK_TEX_ADDRESS_MIRROR
] = TCM_MIRROR
,
1598 [VK_TEX_ADDRESS_CLAMP
] = TCM_CLAMP
,
1599 [VK_TEX_ADDRESS_MIRROR_ONCE
] = TCM_MIRROR_ONCE
,
1600 [VK_TEX_ADDRESS_CLAMP_BORDER
] = TCM_CLAMP_BORDER
,
1603 static const uint32_t vk_to_gen_compare_op
[] = {
1604 [VK_COMPARE_OP_NEVER
] = PREFILTEROPNEVER
,
1605 [VK_COMPARE_OP_LESS
] = PREFILTEROPLESS
,
1606 [VK_COMPARE_OP_EQUAL
] = PREFILTEROPEQUAL
,
1607 [VK_COMPARE_OP_LESS_EQUAL
] = PREFILTEROPLEQUAL
,
1608 [VK_COMPARE_OP_GREATER
] = PREFILTEROPGREATER
,
1609 [VK_COMPARE_OP_NOT_EQUAL
] = PREFILTEROPNOTEQUAL
,
1610 [VK_COMPARE_OP_GREATER_EQUAL
] = PREFILTEROPGEQUAL
,
1611 [VK_COMPARE_OP_ALWAYS
] = PREFILTEROPALWAYS
,
1614 if (pCreateInfo
->maxAnisotropy
> 1) {
1615 mag_filter
= MAPFILTER_ANISOTROPIC
;
1616 min_filter
= MAPFILTER_ANISOTROPIC
;
1617 max_anisotropy
= (pCreateInfo
->maxAnisotropy
- 2) / 2;
1619 mag_filter
= vk_to_gen_tex_filter
[pCreateInfo
->magFilter
];
1620 min_filter
= vk_to_gen_tex_filter
[pCreateInfo
->minFilter
];
1621 max_anisotropy
= RATIO21
;
1624 struct GEN8_SAMPLER_STATE sampler_state
= {
1625 .SamplerDisable
= false,
1626 .TextureBorderColorMode
= DX10OGL
,
1627 .LODPreClampMode
= 0,
1629 .MipModeFilter
= vk_to_gen_mipmap_mode
[pCreateInfo
->mipMode
],
1630 .MagModeFilter
= mag_filter
,
1631 .MinModeFilter
= min_filter
,
1632 .TextureLODBias
= pCreateInfo
->mipLodBias
* 256,
1633 .AnisotropicAlgorithm
= EWAApproximation
,
1634 .MinLOD
= pCreateInfo
->minLod
* 256,
1635 .MaxLOD
= pCreateInfo
->maxLod
* 256,
1636 .ChromaKeyEnable
= 0,
1637 .ChromaKeyIndex
= 0,
1639 .ShadowFunction
= vk_to_gen_compare_op
[pCreateInfo
->compareOp
],
1640 .CubeSurfaceControlMode
= 0,
1642 .IndirectStatePointer
=
1643 device
->float_border_colors
.offset
+
1644 pCreateInfo
->borderColor
* sizeof(float) * 4,
1646 .LODClampMagnificationMode
= MIPNONE
,
1647 .MaximumAnisotropy
= max_anisotropy
,
1648 .RAddressMinFilterRoundingEnable
= 0,
1649 .RAddressMagFilterRoundingEnable
= 0,
1650 .VAddressMinFilterRoundingEnable
= 0,
1651 .VAddressMagFilterRoundingEnable
= 0,
1652 .UAddressMinFilterRoundingEnable
= 0,
1653 .UAddressMagFilterRoundingEnable
= 0,
1654 .TrilinearFilterQuality
= 0,
1655 .NonnormalizedCoordinateEnable
= 0,
1656 .TCXAddressControlMode
= vk_to_gen_tex_address
[pCreateInfo
->addressU
],
1657 .TCYAddressControlMode
= vk_to_gen_tex_address
[pCreateInfo
->addressV
],
1658 .TCZAddressControlMode
= vk_to_gen_tex_address
[pCreateInfo
->addressW
],
1661 GEN8_SAMPLER_STATE_pack(NULL
, sampler
->state
, &sampler_state
);
1663 *pSampler
= (VkSampler
) sampler
;
1668 // Descriptor set functions
1670 VkResult
anv_CreateDescriptorSetLayout(
1672 const VkDescriptorSetLayoutCreateInfo
* pCreateInfo
,
1673 VkDescriptorSetLayout
* pSetLayout
)
1675 struct anv_device
*device
= (struct anv_device
*) _device
;
1676 struct anv_descriptor_set_layout
*set_layout
;
1678 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO
);
1680 uint32_t sampler_count
[VK_NUM_SHADER_STAGE
] = { 0, };
1681 uint32_t surface_count
[VK_NUM_SHADER_STAGE
] = { 0, };
1682 uint32_t num_dynamic_buffers
= 0;
1684 uint32_t stages
= 0;
1687 for (uint32_t i
= 0; i
< pCreateInfo
->count
; i
++) {
1688 switch (pCreateInfo
->pBinding
[i
].descriptorType
) {
1689 case VK_DESCRIPTOR_TYPE_SAMPLER
:
1690 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
:
1691 for_each_bit(s
, pCreateInfo
->pBinding
[i
].stageFlags
)
1692 sampler_count
[s
] += pCreateInfo
->pBinding
[i
].count
;
1698 switch (pCreateInfo
->pBinding
[i
].descriptorType
) {
1699 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
:
1700 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE
:
1701 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE
:
1702 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER
:
1703 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER
:
1704 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER
:
1705 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER
:
1706 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
:
1707 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
:
1708 for_each_bit(s
, pCreateInfo
->pBinding
[i
].stageFlags
)
1709 surface_count
[s
] += pCreateInfo
->pBinding
[i
].count
;
1715 switch (pCreateInfo
->pBinding
[i
].descriptorType
) {
1716 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
:
1717 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
:
1718 num_dynamic_buffers
+= pCreateInfo
->pBinding
[i
].count
;
1724 stages
|= pCreateInfo
->pBinding
[i
].stageFlags
;
1725 count
+= pCreateInfo
->pBinding
[i
].count
;
1728 uint32_t sampler_total
= 0;
1729 uint32_t surface_total
= 0;
1730 for (uint32_t s
= 0; s
< VK_NUM_SHADER_STAGE
; s
++) {
1731 sampler_total
+= sampler_count
[s
];
1732 surface_total
+= surface_count
[s
];
1735 size_t size
= sizeof(*set_layout
) +
1736 (sampler_total
+ surface_total
) * sizeof(set_layout
->entries
[0]);
1737 set_layout
= anv_device_alloc(device
, size
, 8,
1738 VK_SYSTEM_ALLOC_TYPE_API_OBJECT
);
1740 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
1742 set_layout
->num_dynamic_buffers
= num_dynamic_buffers
;
1743 set_layout
->count
= count
;
1744 set_layout
->shader_stages
= stages
;
1746 struct anv_descriptor_slot
*p
= set_layout
->entries
;
1747 struct anv_descriptor_slot
*sampler
[VK_NUM_SHADER_STAGE
];
1748 struct anv_descriptor_slot
*surface
[VK_NUM_SHADER_STAGE
];
1749 for (uint32_t s
= 0; s
< VK_NUM_SHADER_STAGE
; s
++) {
1750 set_layout
->stage
[s
].surface_count
= surface_count
[s
];
1751 set_layout
->stage
[s
].surface_start
= surface
[s
] = p
;
1752 p
+= surface_count
[s
];
1753 set_layout
->stage
[s
].sampler_count
= sampler_count
[s
];
1754 set_layout
->stage
[s
].sampler_start
= sampler
[s
] = p
;
1755 p
+= sampler_count
[s
];
1758 uint32_t descriptor
= 0;
1759 int8_t dynamic_slot
= 0;
1761 for (uint32_t i
= 0; i
< pCreateInfo
->count
; i
++) {
1762 switch (pCreateInfo
->pBinding
[i
].descriptorType
) {
1763 case VK_DESCRIPTOR_TYPE_SAMPLER
:
1764 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
:
1765 for_each_bit(s
, pCreateInfo
->pBinding
[i
].stageFlags
)
1766 for (uint32_t j
= 0; j
< pCreateInfo
->pBinding
[i
].count
; j
++) {
1767 sampler
[s
]->index
= descriptor
+ j
;
1768 sampler
[s
]->dynamic_slot
= -1;
1776 switch (pCreateInfo
->pBinding
[i
].descriptorType
) {
1777 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
:
1778 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
:
1786 switch (pCreateInfo
->pBinding
[i
].descriptorType
) {
1787 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
:
1788 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE
:
1789 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE
:
1790 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER
:
1791 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER
:
1792 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER
:
1793 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER
:
1794 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
:
1795 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
:
1796 for_each_bit(s
, pCreateInfo
->pBinding
[i
].stageFlags
)
1797 for (uint32_t j
= 0; j
< pCreateInfo
->pBinding
[i
].count
; j
++) {
1798 surface
[s
]->index
= descriptor
+ j
;
1800 surface
[s
]->dynamic_slot
= dynamic_slot
+ j
;
1802 surface
[s
]->dynamic_slot
= -1;
1811 dynamic_slot
+= pCreateInfo
->pBinding
[i
].count
;
1813 descriptor
+= pCreateInfo
->pBinding
[i
].count
;
1816 *pSetLayout
= (VkDescriptorSetLayout
) set_layout
;
1821 VkResult
anv_BeginDescriptorPoolUpdate(
1823 VkDescriptorUpdateMode updateMode
)
1828 VkResult
anv_EndDescriptorPoolUpdate(
1835 VkResult
anv_CreateDescriptorPool(
1837 VkDescriptorPoolUsage poolUsage
,
1839 const VkDescriptorPoolCreateInfo
* pCreateInfo
,
1840 VkDescriptorPool
* pDescriptorPool
)
1842 *pDescriptorPool
= 1;
1847 VkResult
anv_ResetDescriptorPool(
1849 VkDescriptorPool descriptorPool
)
1854 VkResult
anv_AllocDescriptorSets(
1856 VkDescriptorPool descriptorPool
,
1857 VkDescriptorSetUsage setUsage
,
1859 const VkDescriptorSetLayout
* pSetLayouts
,
1860 VkDescriptorSet
* pDescriptorSets
,
1863 struct anv_device
*device
= (struct anv_device
*) _device
;
1864 const struct anv_descriptor_set_layout
*layout
;
1865 struct anv_descriptor_set
*set
;
1868 for (uint32_t i
= 0; i
< count
; i
++) {
1869 layout
= (struct anv_descriptor_set_layout
*) pSetLayouts
[i
];
1870 size
= sizeof(*set
) + layout
->count
* sizeof(set
->descriptors
[0]);
1871 set
= anv_device_alloc(device
, size
, 8,
1872 VK_SYSTEM_ALLOC_TYPE_API_OBJECT
);
1875 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
1878 /* Descriptor sets may not be 100% filled out so we need to memset to
1879 * ensure that we can properly detect and handle holes.
1881 memset(set
, 0, size
);
1883 pDescriptorSets
[i
] = (VkDescriptorSet
) set
;
1891 void anv_ClearDescriptorSets(
1893 VkDescriptorPool descriptorPool
,
1895 const VkDescriptorSet
* pDescriptorSets
)
1899 void anv_UpdateDescriptors(
1901 VkDescriptorSet descriptorSet
,
1902 uint32_t updateCount
,
1903 const void** ppUpdateArray
)
1905 struct anv_descriptor_set
*set
= (struct anv_descriptor_set
*) descriptorSet
;
1906 VkUpdateSamplers
*update_samplers
;
1907 VkUpdateSamplerTextures
*update_sampler_textures
;
1908 VkUpdateImages
*update_images
;
1909 VkUpdateBuffers
*update_buffers
;
1910 VkUpdateAsCopy
*update_as_copy
;
1912 for (uint32_t i
= 0; i
< updateCount
; i
++) {
1913 const struct anv_common
*common
= ppUpdateArray
[i
];
1915 switch (common
->sType
) {
1916 case VK_STRUCTURE_TYPE_UPDATE_SAMPLERS
:
1917 update_samplers
= (VkUpdateSamplers
*) common
;
1919 for (uint32_t j
= 0; j
< update_samplers
->count
; j
++) {
1920 set
->descriptors
[update_samplers
->binding
+ j
].sampler
=
1921 (struct anv_sampler
*) update_samplers
->pSamplers
[j
];
1925 case VK_STRUCTURE_TYPE_UPDATE_SAMPLER_TEXTURES
:
1926 /* FIXME: Shouldn't this be *_UPDATE_SAMPLER_IMAGES? */
1927 update_sampler_textures
= (VkUpdateSamplerTextures
*) common
;
1929 for (uint32_t j
= 0; j
< update_sampler_textures
->count
; j
++) {
1930 set
->descriptors
[update_sampler_textures
->binding
+ j
].view
=
1931 (struct anv_surface_view
*)
1932 update_sampler_textures
->pSamplerImageViews
[j
].pImageView
->view
;
1933 set
->descriptors
[update_sampler_textures
->binding
+ j
].sampler
=
1934 (struct anv_sampler
*)
1935 update_sampler_textures
->pSamplerImageViews
[j
].sampler
;
1939 case VK_STRUCTURE_TYPE_UPDATE_IMAGES
:
1940 update_images
= (VkUpdateImages
*) common
;
1942 for (uint32_t j
= 0; j
< update_images
->count
; j
++) {
1943 set
->descriptors
[update_images
->binding
+ j
].view
=
1944 (struct anv_surface_view
*) update_images
->pImageViews
[j
].view
;
1948 case VK_STRUCTURE_TYPE_UPDATE_BUFFERS
:
1949 update_buffers
= (VkUpdateBuffers
*) common
;
1951 for (uint32_t j
= 0; j
< update_buffers
->count
; j
++) {
1952 set
->descriptors
[update_buffers
->binding
+ j
].view
=
1953 (struct anv_surface_view
*) update_buffers
->pBufferViews
[j
].view
;
1955 /* FIXME: descriptor arrays? */
1958 case VK_STRUCTURE_TYPE_UPDATE_AS_COPY
:
1959 update_as_copy
= (VkUpdateAsCopy
*) common
;
1960 (void) update_as_copy
;
1969 // State object functions
1971 static inline int64_t
1972 clamp_int64(int64_t x
, int64_t min
, int64_t max
)
1983 anv_dynamic_vp_state_destroy(struct anv_device
*device
,
1984 struct anv_object
*object
,
1985 VkObjectType obj_type
)
1987 struct anv_dynamic_vp_state
*state
= (void *)object
;
1989 assert(obj_type
== VK_OBJECT_TYPE_DYNAMIC_VP_STATE
);
1991 anv_state_pool_free(&device
->dynamic_state_pool
, state
->sf_clip_vp
);
1992 anv_state_pool_free(&device
->dynamic_state_pool
, state
->cc_vp
);
1993 anv_state_pool_free(&device
->dynamic_state_pool
, state
->scissor
);
1995 anv_device_free(device
, state
);
1998 VkResult
anv_CreateDynamicViewportState(
2000 const VkDynamicVpStateCreateInfo
* pCreateInfo
,
2001 VkDynamicVpState
* pState
)
2003 struct anv_device
*device
= (struct anv_device
*) _device
;
2004 struct anv_dynamic_vp_state
*state
;
2006 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_DYNAMIC_VP_STATE_CREATE_INFO
);
2008 state
= anv_device_alloc(device
, sizeof(*state
), 8,
2009 VK_SYSTEM_ALLOC_TYPE_API_OBJECT
);
2011 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
2013 state
->base
.destructor
= anv_dynamic_vp_state_destroy
;
2015 unsigned count
= pCreateInfo
->viewportAndScissorCount
;
2016 state
->sf_clip_vp
= anv_state_pool_alloc(&device
->dynamic_state_pool
,
2018 state
->cc_vp
= anv_state_pool_alloc(&device
->dynamic_state_pool
,
2020 state
->scissor
= anv_state_pool_alloc(&device
->dynamic_state_pool
,
2023 for (uint32_t i
= 0; i
< pCreateInfo
->viewportAndScissorCount
; i
++) {
2024 const VkViewport
*vp
= &pCreateInfo
->pViewports
[i
];
2025 const VkRect
*s
= &pCreateInfo
->pScissors
[i
];
2027 struct GEN8_SF_CLIP_VIEWPORT sf_clip_viewport
= {
2028 .ViewportMatrixElementm00
= vp
->width
/ 2,
2029 .ViewportMatrixElementm11
= vp
->height
/ 2,
2030 .ViewportMatrixElementm22
= (vp
->maxDepth
- vp
->minDepth
) / 2,
2031 .ViewportMatrixElementm30
= vp
->originX
+ vp
->width
/ 2,
2032 .ViewportMatrixElementm31
= vp
->originY
+ vp
->height
/ 2,
2033 .ViewportMatrixElementm32
= (vp
->maxDepth
+ vp
->minDepth
) / 2,
2034 .XMinClipGuardband
= -1.0f
,
2035 .XMaxClipGuardband
= 1.0f
,
2036 .YMinClipGuardband
= -1.0f
,
2037 .YMaxClipGuardband
= 1.0f
,
2038 .XMinViewPort
= vp
->originX
,
2039 .XMaxViewPort
= vp
->originX
+ vp
->width
- 1,
2040 .YMinViewPort
= vp
->originY
,
2041 .YMaxViewPort
= vp
->originY
+ vp
->height
- 1,
2044 struct GEN8_CC_VIEWPORT cc_viewport
= {
2045 .MinimumDepth
= vp
->minDepth
,
2046 .MaximumDepth
= vp
->maxDepth
2049 /* Since xmax and ymax are inclusive, we have to have xmax < xmin or
2050 * ymax < ymin for empty clips. In case clip x, y, width height are all
2051 * 0, the clamps below produce 0 for xmin, ymin, xmax, ymax, which isn't
2052 * what we want. Just special case empty clips and produce a canonical
2054 static const struct GEN8_SCISSOR_RECT empty_scissor
= {
2055 .ScissorRectangleYMin
= 1,
2056 .ScissorRectangleXMin
= 1,
2057 .ScissorRectangleYMax
= 0,
2058 .ScissorRectangleXMax
= 0
2061 const int max
= 0xffff;
2062 struct GEN8_SCISSOR_RECT scissor
= {
2063 /* Do this math using int64_t so overflow gets clamped correctly. */
2064 .ScissorRectangleYMin
= clamp_int64(s
->offset
.y
, 0, max
),
2065 .ScissorRectangleXMin
= clamp_int64(s
->offset
.x
, 0, max
),
2066 .ScissorRectangleYMax
= clamp_int64((uint64_t) s
->offset
.y
+ s
->extent
.height
- 1, 0, max
),
2067 .ScissorRectangleXMax
= clamp_int64((uint64_t) s
->offset
.x
+ s
->extent
.width
- 1, 0, max
)
2070 GEN8_SF_CLIP_VIEWPORT_pack(NULL
, state
->sf_clip_vp
.map
+ i
* 64, &sf_clip_viewport
);
2071 GEN8_CC_VIEWPORT_pack(NULL
, state
->cc_vp
.map
+ i
* 32, &cc_viewport
);
2073 if (s
->extent
.width
<= 0 || s
->extent
.height
<= 0) {
2074 GEN8_SCISSOR_RECT_pack(NULL
, state
->scissor
.map
+ i
* 32, &empty_scissor
);
2076 GEN8_SCISSOR_RECT_pack(NULL
, state
->scissor
.map
+ i
* 32, &scissor
);
2080 *pState
= (VkDynamicVpState
) state
;
2085 VkResult
anv_CreateDynamicRasterState(
2087 const VkDynamicRsStateCreateInfo
* pCreateInfo
,
2088 VkDynamicRsState
* pState
)
2090 struct anv_device
*device
= (struct anv_device
*) _device
;
2091 struct anv_dynamic_rs_state
*state
;
2093 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_DYNAMIC_RS_STATE_CREATE_INFO
);
2095 state
= anv_device_alloc(device
, sizeof(*state
), 8,
2096 VK_SYSTEM_ALLOC_TYPE_API_OBJECT
);
2098 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
2101 * float pointFadeThreshold;
2102 * // optional (GL45) - Size of point fade threshold
2105 struct GEN8_3DSTATE_SF sf
= {
2106 GEN8_3DSTATE_SF_header
,
2107 .LineWidth
= pCreateInfo
->lineWidth
,
2108 .PointWidth
= pCreateInfo
->pointSize
,
2111 GEN8_3DSTATE_SF_pack(NULL
, state
->state_sf
, &sf
);
2113 bool enable_bias
= pCreateInfo
->depthBias
!= 0.0f
||
2114 pCreateInfo
->slopeScaledDepthBias
!= 0.0f
;
2115 struct GEN8_3DSTATE_RASTER raster
= {
2116 .GlobalDepthOffsetEnableSolid
= enable_bias
,
2117 .GlobalDepthOffsetEnableWireframe
= enable_bias
,
2118 .GlobalDepthOffsetEnablePoint
= enable_bias
,
2119 .GlobalDepthOffsetConstant
= pCreateInfo
->depthBias
,
2120 .GlobalDepthOffsetScale
= pCreateInfo
->slopeScaledDepthBias
,
2121 .GlobalDepthOffsetClamp
= pCreateInfo
->depthBiasClamp
2124 GEN8_3DSTATE_RASTER_pack(NULL
, state
->state_raster
, &raster
);
2126 *pState
= (VkDynamicRsState
) state
;
2131 VkResult
anv_CreateDynamicColorBlendState(
2133 const VkDynamicCbStateCreateInfo
* pCreateInfo
,
2134 VkDynamicCbState
* pState
)
2136 struct anv_device
*device
= (struct anv_device
*) _device
;
2137 struct anv_dynamic_cb_state
*state
;
2139 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_DYNAMIC_CB_STATE_CREATE_INFO
);
2141 state
= anv_device_alloc(device
, sizeof(*state
), 8,
2142 VK_SYSTEM_ALLOC_TYPE_API_OBJECT
);
2144 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
2146 struct GEN8_COLOR_CALC_STATE color_calc_state
= {
2147 .BlendConstantColorRed
= pCreateInfo
->blendConst
[0],
2148 .BlendConstantColorGreen
= pCreateInfo
->blendConst
[1],
2149 .BlendConstantColorBlue
= pCreateInfo
->blendConst
[2],
2150 .BlendConstantColorAlpha
= pCreateInfo
->blendConst
[3]
2153 GEN8_COLOR_CALC_STATE_pack(NULL
, state
->state_color_calc
, &color_calc_state
);
2155 *pState
= (VkDynamicCbState
) state
;
2160 VkResult
anv_CreateDynamicDepthStencilState(
2162 const VkDynamicDsStateCreateInfo
* pCreateInfo
,
2163 VkDynamicDsState
* pState
)
2165 struct anv_device
*device
= (struct anv_device
*) _device
;
2166 struct anv_dynamic_ds_state
*state
;
2168 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_DYNAMIC_DS_STATE_CREATE_INFO
);
2170 state
= anv_device_alloc(device
, sizeof(*state
), 8,
2171 VK_SYSTEM_ALLOC_TYPE_API_OBJECT
);
2173 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
2175 struct GEN8_3DSTATE_WM_DEPTH_STENCIL wm_depth_stencil
= {
2176 GEN8_3DSTATE_WM_DEPTH_STENCIL_header
,
2178 /* Is this what we need to do? */
2179 .StencilBufferWriteEnable
= pCreateInfo
->stencilWriteMask
!= 0,
2181 .StencilTestMask
= pCreateInfo
->stencilReadMask
,
2182 .StencilWriteMask
= pCreateInfo
->stencilWriteMask
,
2184 .BackfaceStencilTestMask
= pCreateInfo
->stencilReadMask
,
2185 .BackfaceStencilWriteMask
= pCreateInfo
->stencilWriteMask
,
2188 GEN8_3DSTATE_WM_DEPTH_STENCIL_pack(NULL
, state
->state_wm_depth_stencil
,
2191 struct GEN8_COLOR_CALC_STATE color_calc_state
= {
2192 .StencilReferenceValue
= pCreateInfo
->stencilFrontRef
,
2193 .BackFaceStencilReferenceValue
= pCreateInfo
->stencilBackRef
2196 GEN8_COLOR_CALC_STATE_pack(NULL
, state
->state_color_calc
, &color_calc_state
);
2198 *pState
= (VkDynamicDsState
) state
;
2203 // Command buffer functions
2206 anv_cmd_buffer_destroy(struct anv_device
*device
,
2207 struct anv_object
*object
,
2208 VkObjectType obj_type
)
2210 struct anv_cmd_buffer
*cmd_buffer
= (struct anv_cmd_buffer
*) object
;
2212 assert(obj_type
== VK_OBJECT_TYPE_COMMAND_BUFFER
);
2214 /* Destroy all of the batch buffers */
2215 struct anv_batch_bo
*bbo
= cmd_buffer
->last_batch_bo
;
2216 while (bbo
->prev_batch_bo
) {
2217 struct anv_batch_bo
*prev
= bbo
->prev_batch_bo
;
2218 anv_batch_bo_destroy(bbo
, device
);
2221 anv_reloc_list_finish(&cmd_buffer
->batch
.relocs
, device
);
2223 /* Destroy all of the surface state buffers */
2224 bbo
= cmd_buffer
->surface_batch_bo
;
2225 while (bbo
->prev_batch_bo
) {
2226 struct anv_batch_bo
*prev
= bbo
->prev_batch_bo
;
2227 anv_batch_bo_destroy(bbo
, device
);
2230 anv_reloc_list_finish(&cmd_buffer
->surface_relocs
, device
);
2232 anv_state_stream_finish(&cmd_buffer
->surface_state_stream
);
2233 anv_state_stream_finish(&cmd_buffer
->dynamic_state_stream
);
2234 anv_state_stream_finish(&cmd_buffer
->binding_table_state_stream
);
2235 anv_device_free(device
, cmd_buffer
->exec2_objects
);
2236 anv_device_free(device
, cmd_buffer
->exec2_bos
);
2237 anv_device_free(device
, cmd_buffer
);
2241 anv_cmd_buffer_chain_batch(struct anv_batch
*batch
, void *_data
)
2243 struct anv_cmd_buffer
*cmd_buffer
= _data
;
2245 struct anv_batch_bo
*new_bbo
, *old_bbo
= cmd_buffer
->last_batch_bo
;
2247 VkResult result
= anv_batch_bo_create(cmd_buffer
->device
, &new_bbo
);
2248 if (result
!= VK_SUCCESS
)
2251 /* We set the end of the batch a little short so we would be sure we
2252 * have room for the chaining command. Since we're about to emit the
2253 * chaining command, let's set it back where it should go.
2255 batch
->end
+= GEN8_MI_BATCH_BUFFER_START_length
* 4;
2256 assert(batch
->end
== old_bbo
->bo
.map
+ old_bbo
->bo
.size
);
2258 anv_batch_emit(batch
, GEN8_MI_BATCH_BUFFER_START
,
2259 GEN8_MI_BATCH_BUFFER_START_header
,
2260 ._2ndLevelBatchBuffer
= _1stlevelbatch
,
2261 .AddressSpaceIndicator
= ASI_PPGTT
,
2262 .BatchBufferStartAddress
= { &new_bbo
->bo
, 0 },
2265 /* Pad out to a 2-dword aligned boundary with zeros */
2266 if ((uintptr_t)batch
->next
% 8 != 0) {
2267 *(uint32_t *)batch
->next
= 0;
2271 anv_batch_bo_finish(cmd_buffer
->last_batch_bo
, batch
);
2273 new_bbo
->prev_batch_bo
= old_bbo
;
2274 cmd_buffer
->last_batch_bo
= new_bbo
;
2276 anv_batch_bo_start(new_bbo
, batch
, GEN8_MI_BATCH_BUFFER_START_length
* 4);
2281 VkResult
anv_CreateCommandBuffer(
2283 const VkCmdBufferCreateInfo
* pCreateInfo
,
2284 VkCmdBuffer
* pCmdBuffer
)
2286 struct anv_device
*device
= (struct anv_device
*) _device
;
2287 struct anv_cmd_buffer
*cmd_buffer
;
2290 cmd_buffer
= anv_device_alloc(device
, sizeof(*cmd_buffer
), 8,
2291 VK_SYSTEM_ALLOC_TYPE_API_OBJECT
);
2292 if (cmd_buffer
== NULL
)
2293 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
2295 cmd_buffer
->base
.destructor
= anv_cmd_buffer_destroy
;
2297 cmd_buffer
->device
= device
;
2298 cmd_buffer
->rs_state
= NULL
;
2299 cmd_buffer
->vp_state
= NULL
;
2300 memset(&cmd_buffer
->descriptors
, 0, sizeof(cmd_buffer
->descriptors
));
2302 result
= anv_batch_bo_create(device
, &cmd_buffer
->last_batch_bo
);
2303 if (result
!= VK_SUCCESS
)
2306 result
= anv_reloc_list_init(&cmd_buffer
->batch
.relocs
, device
);
2307 if (result
!= VK_SUCCESS
)
2310 cmd_buffer
->batch
.device
= device
;
2311 cmd_buffer
->batch
.extend_cb
= anv_cmd_buffer_chain_batch
;
2312 cmd_buffer
->batch
.user_data
= cmd_buffer
;
2314 anv_batch_bo_start(cmd_buffer
->last_batch_bo
, &cmd_buffer
->batch
,
2315 GEN8_MI_BATCH_BUFFER_START_length
* 4);
2317 result
= anv_batch_bo_create(device
, &cmd_buffer
->surface_batch_bo
);
2318 if (result
!= VK_SUCCESS
)
2319 goto fail_batch_relocs
;
2320 cmd_buffer
->surface_batch_bo
->first_reloc
= 0;
2322 result
= anv_reloc_list_init(&cmd_buffer
->surface_relocs
, device
);
2323 if (result
!= VK_SUCCESS
)
2324 goto fail_ss_batch_bo
;
2326 /* Start surface_next at 1 so surface offset 0 is invalid. */
2327 cmd_buffer
->surface_next
= 1;
2329 cmd_buffer
->exec2_objects
= NULL
;
2330 cmd_buffer
->exec2_bos
= NULL
;
2331 cmd_buffer
->exec2_array_length
= 0;
2333 anv_state_stream_init(&cmd_buffer
->binding_table_state_stream
,
2334 &device
->binding_table_block_pool
);
2335 anv_state_stream_init(&cmd_buffer
->surface_state_stream
,
2336 &device
->surface_state_block_pool
);
2337 anv_state_stream_init(&cmd_buffer
->dynamic_state_stream
,
2338 &device
->dynamic_state_block_pool
);
2340 cmd_buffer
->dirty
= 0;
2341 cmd_buffer
->vb_dirty
= 0;
2342 cmd_buffer
->descriptors_dirty
= 0;
2343 cmd_buffer
->pipeline
= NULL
;
2344 cmd_buffer
->vp_state
= NULL
;
2345 cmd_buffer
->rs_state
= NULL
;
2346 cmd_buffer
->ds_state
= NULL
;
2348 *pCmdBuffer
= (VkCmdBuffer
) cmd_buffer
;
2353 anv_batch_bo_destroy(cmd_buffer
->surface_batch_bo
, device
);
2355 anv_reloc_list_finish(&cmd_buffer
->batch
.relocs
, device
);
2357 anv_batch_bo_destroy(cmd_buffer
->last_batch_bo
, device
);
2359 anv_device_free(device
, cmd_buffer
);
2365 anv_cmd_buffer_emit_state_base_address(struct anv_cmd_buffer
*cmd_buffer
)
2367 struct anv_device
*device
= cmd_buffer
->device
;
2369 anv_batch_emit(&cmd_buffer
->batch
, GEN8_STATE_BASE_ADDRESS
,
2370 .GeneralStateBaseAddress
= { NULL
, 0 },
2371 .GeneralStateMemoryObjectControlState
= GEN8_MOCS
,
2372 .GeneralStateBaseAddressModifyEnable
= true,
2373 .GeneralStateBufferSize
= 0xfffff,
2374 .GeneralStateBufferSizeModifyEnable
= true,
2376 .SurfaceStateBaseAddress
= { &cmd_buffer
->surface_batch_bo
->bo
, 0 },
2377 .SurfaceStateMemoryObjectControlState
= GEN8_MOCS
,
2378 .SurfaceStateBaseAddressModifyEnable
= true,
2380 .DynamicStateBaseAddress
= { &device
->dynamic_state_block_pool
.bo
, 0 },
2381 .DynamicStateMemoryObjectControlState
= GEN8_MOCS
,
2382 .DynamicStateBaseAddressModifyEnable
= true,
2383 .DynamicStateBufferSize
= 0xfffff,
2384 .DynamicStateBufferSizeModifyEnable
= true,
2386 .IndirectObjectBaseAddress
= { NULL
, 0 },
2387 .IndirectObjectMemoryObjectControlState
= GEN8_MOCS
,
2388 .IndirectObjectBaseAddressModifyEnable
= true,
2389 .IndirectObjectBufferSize
= 0xfffff,
2390 .IndirectObjectBufferSizeModifyEnable
= true,
2392 .InstructionBaseAddress
= { &device
->instruction_block_pool
.bo
, 0 },
2393 .InstructionMemoryObjectControlState
= GEN8_MOCS
,
2394 .InstructionBaseAddressModifyEnable
= true,
2395 .InstructionBufferSize
= 0xfffff,
2396 .InstructionBuffersizeModifyEnable
= true);
2399 VkResult
anv_BeginCommandBuffer(
2400 VkCmdBuffer cmdBuffer
,
2401 const VkCmdBufferBeginInfo
* pBeginInfo
)
2403 struct anv_cmd_buffer
*cmd_buffer
= (struct anv_cmd_buffer
*) cmdBuffer
;
2405 anv_batch_emit(&cmd_buffer
->batch
, GEN8_PIPELINE_SELECT
,
2406 .PipelineSelection
= _3D
);
2407 anv_batch_emit(&cmd_buffer
->batch
, GEN8_STATE_SIP
);
2409 anv_cmd_buffer_emit_state_base_address(cmd_buffer
);
2411 anv_batch_emit(&cmd_buffer
->batch
, GEN8_3DSTATE_VF_STATISTICS
,
2412 .StatisticsEnable
= true);
2413 anv_batch_emit(&cmd_buffer
->batch
, GEN8_3DSTATE_HS
, .Enable
= false);
2414 anv_batch_emit(&cmd_buffer
->batch
, GEN8_3DSTATE_TE
, .TEEnable
= false);
2415 anv_batch_emit(&cmd_buffer
->batch
, GEN8_3DSTATE_DS
, .FunctionEnable
= false);
2416 anv_batch_emit(&cmd_buffer
->batch
, GEN8_3DSTATE_STREAMOUT
, .SOFunctionEnable
= false);
2418 anv_batch_emit(&cmd_buffer
->batch
, GEN8_3DSTATE_PUSH_CONSTANT_ALLOC_VS
,
2419 .ConstantBufferOffset
= 0,
2420 .ConstantBufferSize
= 4);
2421 anv_batch_emit(&cmd_buffer
->batch
, GEN8_3DSTATE_PUSH_CONSTANT_ALLOC_GS
,
2422 .ConstantBufferOffset
= 4,
2423 .ConstantBufferSize
= 4);
2424 anv_batch_emit(&cmd_buffer
->batch
, GEN8_3DSTATE_PUSH_CONSTANT_ALLOC_PS
,
2425 .ConstantBufferOffset
= 8,
2426 .ConstantBufferSize
= 4);
2428 anv_batch_emit(&cmd_buffer
->batch
, GEN8_3DSTATE_WM_CHROMAKEY
,
2429 .ChromaKeyKillEnable
= false);
2430 anv_batch_emit(&cmd_buffer
->batch
, GEN8_3DSTATE_SBE_SWIZ
);
2431 anv_batch_emit(&cmd_buffer
->batch
, GEN8_3DSTATE_AA_LINE_PARAMETERS
);
2437 anv_cmd_buffer_add_bo(struct anv_cmd_buffer
*cmd_buffer
,
2439 struct drm_i915_gem_relocation_entry
*relocs
,
2442 struct drm_i915_gem_exec_object2
*obj
;
2444 if (bo
->index
< cmd_buffer
->bo_count
&&
2445 cmd_buffer
->exec2_bos
[bo
->index
] == bo
)
2448 if (cmd_buffer
->bo_count
>= cmd_buffer
->exec2_array_length
) {
2449 uint32_t new_len
= cmd_buffer
->exec2_objects
?
2450 cmd_buffer
->exec2_array_length
* 2 : 64;
2452 struct drm_i915_gem_exec_object2
*new_objects
=
2453 anv_device_alloc(cmd_buffer
->device
, new_len
* sizeof(*new_objects
),
2454 8, VK_SYSTEM_ALLOC_TYPE_INTERNAL
);
2455 if (new_objects
== NULL
)
2456 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
2458 struct anv_bo
**new_bos
=
2459 anv_device_alloc(cmd_buffer
->device
, new_len
* sizeof(*new_bos
),
2460 8, VK_SYSTEM_ALLOC_TYPE_INTERNAL
);
2461 if (new_objects
== NULL
) {
2462 anv_device_free(cmd_buffer
->device
, new_objects
);
2463 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
2466 if (cmd_buffer
->exec2_objects
) {
2467 memcpy(new_objects
, cmd_buffer
->exec2_objects
,
2468 cmd_buffer
->bo_count
* sizeof(*new_objects
));
2469 memcpy(new_bos
, cmd_buffer
->exec2_bos
,
2470 cmd_buffer
->bo_count
* sizeof(*new_bos
));
2473 cmd_buffer
->exec2_objects
= new_objects
;
2474 cmd_buffer
->exec2_bos
= new_bos
;
2475 cmd_buffer
->exec2_array_length
= new_len
;
2478 assert(cmd_buffer
->bo_count
< cmd_buffer
->exec2_array_length
);
2480 bo
->index
= cmd_buffer
->bo_count
++;
2481 obj
= &cmd_buffer
->exec2_objects
[bo
->index
];
2482 cmd_buffer
->exec2_bos
[bo
->index
] = bo
;
2484 obj
->handle
= bo
->gem_handle
;
2485 obj
->relocation_count
= 0;
2486 obj
->relocs_ptr
= 0;
2488 obj
->offset
= bo
->offset
;
2494 obj
->relocation_count
= num_relocs
;
2495 obj
->relocs_ptr
= (uintptr_t) relocs
;
2502 anv_cmd_buffer_add_validate_bos(struct anv_cmd_buffer
*cmd_buffer
,
2503 struct anv_reloc_list
*list
)
2505 for (size_t i
= 0; i
< list
->num_relocs
; i
++)
2506 anv_cmd_buffer_add_bo(cmd_buffer
, list
->reloc_bos
[i
], NULL
, 0);
2510 anv_cmd_buffer_process_relocs(struct anv_cmd_buffer
*cmd_buffer
,
2511 struct anv_reloc_list
*list
)
2515 /* If the kernel supports I915_EXEC_NO_RELOC, it will compare offset in
2516 * struct drm_i915_gem_exec_object2 against the bos current offset and if
2517 * all bos haven't moved it will skip relocation processing alltogether.
2518 * If I915_EXEC_NO_RELOC is not supported, the kernel ignores the incoming
2519 * value of offset so we can set it either way. For that to work we need
2520 * to make sure all relocs use the same presumed offset.
2523 for (size_t i
= 0; i
< list
->num_relocs
; i
++) {
2524 bo
= list
->reloc_bos
[i
];
2525 if (bo
->offset
!= list
->relocs
[i
].presumed_offset
)
2526 cmd_buffer
->need_reloc
= true;
2528 list
->relocs
[i
].target_handle
= bo
->index
;
2532 VkResult
anv_EndCommandBuffer(
2533 VkCmdBuffer cmdBuffer
)
2535 struct anv_cmd_buffer
*cmd_buffer
= (struct anv_cmd_buffer
*) cmdBuffer
;
2536 struct anv_device
*device
= cmd_buffer
->device
;
2537 struct anv_batch
*batch
= &cmd_buffer
->batch
;
2539 anv_batch_emit(batch
, GEN8_MI_BATCH_BUFFER_END
);
2541 /* Round batch up to an even number of dwords. */
2542 if ((batch
->next
- batch
->start
) & 4)
2543 anv_batch_emit(batch
, GEN8_MI_NOOP
);
2545 anv_batch_bo_finish(cmd_buffer
->last_batch_bo
, &cmd_buffer
->batch
);
2546 cmd_buffer
->surface_batch_bo
->num_relocs
=
2547 cmd_buffer
->surface_relocs
.num_relocs
- cmd_buffer
->surface_batch_bo
->first_reloc
;
2548 cmd_buffer
->surface_batch_bo
->length
= cmd_buffer
->surface_next
;
2550 cmd_buffer
->bo_count
= 0;
2551 cmd_buffer
->need_reloc
= false;
2553 /* Lock for access to bo->index. */
2554 pthread_mutex_lock(&device
->mutex
);
2556 /* Add surface state bos first so we can add them with their relocs. */
2557 for (struct anv_batch_bo
*bbo
= cmd_buffer
->surface_batch_bo
;
2558 bbo
!= NULL
; bbo
= bbo
->prev_batch_bo
) {
2559 anv_cmd_buffer_add_bo(cmd_buffer
, &bbo
->bo
,
2560 &cmd_buffer
->surface_relocs
.relocs
[bbo
->first_reloc
],
2564 /* Add all of the BOs referenced by surface state */
2565 anv_cmd_buffer_add_validate_bos(cmd_buffer
, &cmd_buffer
->surface_relocs
);
2567 /* Add all but the first batch BO */
2568 struct anv_batch_bo
*batch_bo
= cmd_buffer
->last_batch_bo
;
2569 while (batch_bo
->prev_batch_bo
) {
2570 anv_cmd_buffer_add_bo(cmd_buffer
, &batch_bo
->bo
,
2571 &batch
->relocs
.relocs
[batch_bo
->first_reloc
],
2572 batch_bo
->num_relocs
);
2573 batch_bo
= batch_bo
->prev_batch_bo
;
2576 /* Add everything referenced by the batches */
2577 anv_cmd_buffer_add_validate_bos(cmd_buffer
, &batch
->relocs
);
2579 /* Add the first batch bo last */
2580 assert(batch_bo
->prev_batch_bo
== NULL
&& batch_bo
->first_reloc
== 0);
2581 anv_cmd_buffer_add_bo(cmd_buffer
, &batch_bo
->bo
,
2582 &batch
->relocs
.relocs
[batch_bo
->first_reloc
],
2583 batch_bo
->num_relocs
);
2584 assert(batch_bo
->bo
.index
== cmd_buffer
->bo_count
- 1);
2586 anv_cmd_buffer_process_relocs(cmd_buffer
, &cmd_buffer
->surface_relocs
);
2587 anv_cmd_buffer_process_relocs(cmd_buffer
, &batch
->relocs
);
2589 cmd_buffer
->execbuf
.buffers_ptr
= (uintptr_t) cmd_buffer
->exec2_objects
;
2590 cmd_buffer
->execbuf
.buffer_count
= cmd_buffer
->bo_count
;
2591 cmd_buffer
->execbuf
.batch_start_offset
= 0;
2592 cmd_buffer
->execbuf
.batch_len
= batch
->next
- batch
->start
;
2593 cmd_buffer
->execbuf
.cliprects_ptr
= 0;
2594 cmd_buffer
->execbuf
.num_cliprects
= 0;
2595 cmd_buffer
->execbuf
.DR1
= 0;
2596 cmd_buffer
->execbuf
.DR4
= 0;
2598 cmd_buffer
->execbuf
.flags
= I915_EXEC_HANDLE_LUT
;
2599 if (!cmd_buffer
->need_reloc
)
2600 cmd_buffer
->execbuf
.flags
|= I915_EXEC_NO_RELOC
;
2601 cmd_buffer
->execbuf
.flags
|= I915_EXEC_RENDER
;
2602 cmd_buffer
->execbuf
.rsvd1
= device
->context_id
;
2603 cmd_buffer
->execbuf
.rsvd2
= 0;
2605 pthread_mutex_unlock(&device
->mutex
);
2610 VkResult
anv_ResetCommandBuffer(
2611 VkCmdBuffer cmdBuffer
)
2613 struct anv_cmd_buffer
*cmd_buffer
= (struct anv_cmd_buffer
*) cmdBuffer
;
2615 /* Delete all but the first batch bo */
2616 while (cmd_buffer
->last_batch_bo
->prev_batch_bo
) {
2617 struct anv_batch_bo
*prev
= cmd_buffer
->last_batch_bo
->prev_batch_bo
;
2618 anv_batch_bo_destroy(cmd_buffer
->last_batch_bo
, cmd_buffer
->device
);
2619 cmd_buffer
->last_batch_bo
= prev
;
2621 assert(cmd_buffer
->last_batch_bo
->prev_batch_bo
== NULL
);
2623 cmd_buffer
->batch
.relocs
.num_relocs
= 0;
2624 anv_batch_bo_start(cmd_buffer
->last_batch_bo
, &cmd_buffer
->batch
,
2625 GEN8_MI_BATCH_BUFFER_START_length
* 4);
2627 /* Delete all but the first batch bo */
2628 while (cmd_buffer
->surface_batch_bo
->prev_batch_bo
) {
2629 struct anv_batch_bo
*prev
= cmd_buffer
->surface_batch_bo
->prev_batch_bo
;
2630 anv_batch_bo_destroy(cmd_buffer
->surface_batch_bo
, cmd_buffer
->device
);
2631 cmd_buffer
->surface_batch_bo
= prev
;
2633 assert(cmd_buffer
->surface_batch_bo
->prev_batch_bo
== NULL
);
2635 cmd_buffer
->surface_next
= 1;
2636 cmd_buffer
->surface_relocs
.num_relocs
= 0;
2641 // Command buffer building functions
2643 void anv_CmdBindPipeline(
2644 VkCmdBuffer cmdBuffer
,
2645 VkPipelineBindPoint pipelineBindPoint
,
2646 VkPipeline _pipeline
)
2648 struct anv_cmd_buffer
*cmd_buffer
= (struct anv_cmd_buffer
*) cmdBuffer
;
2649 struct anv_pipeline
*pipeline
= (struct anv_pipeline
*) _pipeline
;
2651 cmd_buffer
->pipeline
= pipeline
;
2652 cmd_buffer
->vb_dirty
|= pipeline
->vb_used
;
2653 cmd_buffer
->dirty
|= ANV_CMD_BUFFER_PIPELINE_DIRTY
;
2656 void anv_CmdBindDynamicStateObject(
2657 VkCmdBuffer cmdBuffer
,
2658 VkStateBindPoint stateBindPoint
,
2659 VkDynamicStateObject dynamicState
)
2661 struct anv_cmd_buffer
*cmd_buffer
= (struct anv_cmd_buffer
*) cmdBuffer
;
2662 struct anv_dynamic_vp_state
*vp_state
;
2664 switch (stateBindPoint
) {
2665 case VK_STATE_BIND_POINT_VIEWPORT
:
2666 vp_state
= (struct anv_dynamic_vp_state
*) dynamicState
;
2667 /* We emit state immediately, but set cmd_buffer->vp_state to indicate
2668 * that vp state has been set in this command buffer. */
2669 cmd_buffer
->vp_state
= vp_state
;
2670 anv_batch_emit(&cmd_buffer
->batch
, GEN8_3DSTATE_SCISSOR_STATE_POINTERS
,
2671 .ScissorRectPointer
= vp_state
->scissor
.offset
);
2672 anv_batch_emit(&cmd_buffer
->batch
, GEN8_3DSTATE_VIEWPORT_STATE_POINTERS_CC
,
2673 .CCViewportPointer
= vp_state
->cc_vp
.offset
);
2674 anv_batch_emit(&cmd_buffer
->batch
, GEN8_3DSTATE_VIEWPORT_STATE_POINTERS_SF_CLIP
,
2675 .SFClipViewportPointer
= vp_state
->sf_clip_vp
.offset
);
2677 case VK_STATE_BIND_POINT_RASTER
:
2678 cmd_buffer
->rs_state
= (struct anv_dynamic_rs_state
*) dynamicState
;
2679 cmd_buffer
->dirty
|= ANV_CMD_BUFFER_RS_DIRTY
;
2681 case VK_STATE_BIND_POINT_COLOR_BLEND
:
2682 cmd_buffer
->cb_state
= (struct anv_dynamic_cb_state
*) dynamicState
;
2683 cmd_buffer
->dirty
|= ANV_CMD_BUFFER_CB_DIRTY
;
2685 case VK_STATE_BIND_POINT_DEPTH_STENCIL
:
2686 cmd_buffer
->ds_state
= (struct anv_dynamic_ds_state
*) dynamicState
;
2687 cmd_buffer
->dirty
|= ANV_CMD_BUFFER_DS_DIRTY
;
2694 static struct anv_state
2695 anv_cmd_buffer_alloc_surface_state(struct anv_cmd_buffer
*cmd_buffer
,
2696 uint32_t size
, uint32_t alignment
)
2698 struct anv_state state
;
2700 state
.offset
= ALIGN_U32(cmd_buffer
->surface_next
, alignment
);
2701 if (state
.offset
+ size
> cmd_buffer
->surface_batch_bo
->bo
.size
)
2702 return (struct anv_state
) { 0 };
2704 state
.map
= cmd_buffer
->surface_batch_bo
->bo
.map
+ state
.offset
;
2705 state
.alloc_size
= size
;
2706 cmd_buffer
->surface_next
= state
.offset
+ size
;
2708 assert(state
.offset
+ size
<= cmd_buffer
->surface_batch_bo
->bo
.size
);
2714 anv_cmd_buffer_new_surface_state_bo(struct anv_cmd_buffer
*cmd_buffer
)
2716 struct anv_batch_bo
*new_bbo
, *old_bbo
= cmd_buffer
->surface_batch_bo
;
2718 /* Finish off the old buffer */
2719 old_bbo
->num_relocs
=
2720 cmd_buffer
->surface_relocs
.num_relocs
- old_bbo
->first_reloc
;
2721 old_bbo
->length
= cmd_buffer
->surface_next
;
2723 VkResult result
= anv_batch_bo_create(cmd_buffer
->device
, &new_bbo
);
2724 if (result
!= VK_SUCCESS
)
2727 new_bbo
->first_reloc
= cmd_buffer
->surface_relocs
.num_relocs
;
2728 cmd_buffer
->surface_next
= 1;
2730 new_bbo
->prev_batch_bo
= old_bbo
;
2731 cmd_buffer
->surface_batch_bo
= new_bbo
;
2733 /* Re-emit state base addresses so we get the new surface state base
2734 * address before we start emitting binding tables etc.
2736 anv_cmd_buffer_emit_state_base_address(cmd_buffer
);
2738 /* It seems like just changing the state base addresses isn't enough.
2739 * Invalidating the cache seems to be enough to cause things to
2740 * propagate. However, I'm not 100% sure what we're supposed to do.
2742 anv_batch_emit(&cmd_buffer
->batch
, GEN8_PIPE_CONTROL
,
2743 .TextureCacheInvalidationEnable
= true);
2748 void anv_CmdBindDescriptorSets(
2749 VkCmdBuffer cmdBuffer
,
2750 VkPipelineBindPoint pipelineBindPoint
,
2753 const VkDescriptorSet
* pDescriptorSets
,
2754 uint32_t dynamicOffsetCount
,
2755 const uint32_t* pDynamicOffsets
)
2757 struct anv_cmd_buffer
*cmd_buffer
= (struct anv_cmd_buffer
*) cmdBuffer
;
2758 struct anv_pipeline_layout
*layout
= cmd_buffer
->pipeline
->layout
;
2759 struct anv_descriptor_set
*set
;
2760 struct anv_descriptor_set_layout
*set_layout
;
2762 assert(firstSet
+ setCount
< MAX_SETS
);
2764 uint32_t dynamic_slot
= 0;
2765 for (uint32_t i
= 0; i
< setCount
; i
++) {
2766 set
= (struct anv_descriptor_set
*) pDescriptorSets
[i
];
2767 set_layout
= layout
->set
[firstSet
+ i
].layout
;
2769 cmd_buffer
->descriptors
[firstSet
+ i
].set
= set
;
2771 assert(set_layout
->num_dynamic_buffers
<
2772 ARRAY_SIZE(cmd_buffer
->descriptors
[0].dynamic_offsets
));
2773 memcpy(cmd_buffer
->descriptors
[firstSet
+ i
].dynamic_offsets
,
2774 pDynamicOffsets
+ dynamic_slot
,
2775 set_layout
->num_dynamic_buffers
* sizeof(*pDynamicOffsets
));
2777 cmd_buffer
->descriptors_dirty
|= set_layout
->shader_stages
;
2779 dynamic_slot
+= set_layout
->num_dynamic_buffers
;
2783 void anv_CmdBindIndexBuffer(
2784 VkCmdBuffer cmdBuffer
,
2786 VkDeviceSize offset
,
2787 VkIndexType indexType
)
2789 struct anv_cmd_buffer
*cmd_buffer
= (struct anv_cmd_buffer
*) cmdBuffer
;
2790 struct anv_buffer
*buffer
= (struct anv_buffer
*) _buffer
;
2792 static const uint32_t vk_to_gen_index_type
[] = {
2793 [VK_INDEX_TYPE_UINT8
] = INDEX_BYTE
,
2794 [VK_INDEX_TYPE_UINT16
] = INDEX_WORD
,
2795 [VK_INDEX_TYPE_UINT32
] = INDEX_DWORD
,
2798 anv_batch_emit(&cmd_buffer
->batch
, GEN8_3DSTATE_INDEX_BUFFER
,
2799 .IndexFormat
= vk_to_gen_index_type
[indexType
],
2800 .MemoryObjectControlState
= GEN8_MOCS
,
2801 .BufferStartingAddress
= { buffer
->bo
, buffer
->offset
+ offset
},
2802 .BufferSize
= buffer
->size
- offset
);
2805 void anv_CmdBindVertexBuffers(
2806 VkCmdBuffer cmdBuffer
,
2807 uint32_t startBinding
,
2808 uint32_t bindingCount
,
2809 const VkBuffer
* pBuffers
,
2810 const VkDeviceSize
* pOffsets
)
2812 struct anv_cmd_buffer
*cmd_buffer
= (struct anv_cmd_buffer
*) cmdBuffer
;
2813 struct anv_vertex_binding
*vb
= cmd_buffer
->vertex_bindings
;
2815 /* We have to defer setting up vertex buffer since we need the buffer
2816 * stride from the pipeline. */
2818 assert(startBinding
+ bindingCount
< MAX_VBS
);
2819 for (uint32_t i
= 0; i
< bindingCount
; i
++) {
2820 vb
[startBinding
+ i
].buffer
= (struct anv_buffer
*) pBuffers
[i
];
2821 vb
[startBinding
+ i
].offset
= pOffsets
[i
];
2822 cmd_buffer
->vb_dirty
|= 1 << (startBinding
+ i
);
2827 cmd_buffer_emit_binding_table(struct anv_cmd_buffer
*cmd_buffer
,
2830 struct anv_pipeline_layout
*layout
= cmd_buffer
->pipeline
->layout
;
2831 uint32_t color_attachments
, bias
, size
;
2832 struct anv_state bt_state
;
2834 if (stage
== VK_SHADER_STAGE_FRAGMENT
) {
2836 color_attachments
= cmd_buffer
->framebuffer
->color_attachment_count
;
2839 color_attachments
= 0;
2842 /* This is a little awkward: layout can be NULL but we still have to
2843 * allocate and set a binding table for the PS stage for render
2845 uint32_t surface_count
= layout
? layout
->stage
[stage
].surface_count
: 0;
2847 if (color_attachments
+ surface_count
== 0)
2850 size
= (bias
+ surface_count
) * sizeof(uint32_t);
2851 bt_state
= anv_cmd_buffer_alloc_surface_state(cmd_buffer
, size
, 32);
2852 uint32_t *bt_map
= bt_state
.map
;
2854 if (bt_state
.map
== NULL
)
2855 return VK_ERROR_OUT_OF_DEVICE_MEMORY
;
2857 static const uint32_t binding_table_opcodes
[] = {
2858 [VK_SHADER_STAGE_VERTEX
] = 38,
2859 [VK_SHADER_STAGE_TESS_CONTROL
] = 39,
2860 [VK_SHADER_STAGE_TESS_EVALUATION
] = 40,
2861 [VK_SHADER_STAGE_GEOMETRY
] = 41,
2862 [VK_SHADER_STAGE_FRAGMENT
] = 42,
2863 [VK_SHADER_STAGE_COMPUTE
] = 0,
2866 anv_batch_emit(&cmd_buffer
->batch
,
2867 GEN8_3DSTATE_BINDING_TABLE_POINTERS_VS
,
2868 ._3DCommandSubOpcode
= binding_table_opcodes
[stage
],
2869 .PointertoVSBindingTable
= bt_state
.offset
);
2871 for (uint32_t ca
= 0; ca
< color_attachments
; ca
++) {
2872 const struct anv_surface_view
*view
=
2873 cmd_buffer
->framebuffer
->color_attachments
[ca
];
2875 struct anv_state state
=
2876 anv_cmd_buffer_alloc_surface_state(cmd_buffer
, 64, 64);
2878 if (state
.map
== NULL
)
2879 return VK_ERROR_OUT_OF_DEVICE_MEMORY
;
2881 memcpy(state
.map
, view
->surface_state
.map
, 64);
2883 /* The address goes in dwords 8 and 9 of the SURFACE_STATE */
2884 *(uint64_t *)(state
.map
+ 8 * 4) =
2885 anv_reloc_list_add(&cmd_buffer
->surface_relocs
,
2887 state
.offset
+ 8 * 4,
2888 view
->bo
, view
->offset
);
2890 bt_map
[ca
] = state
.offset
;
2896 for (uint32_t set
= 0; set
< layout
->num_sets
; set
++) {
2897 struct anv_descriptor_set_binding
*d
= &cmd_buffer
->descriptors
[set
];
2898 struct anv_descriptor_set_layout
*set_layout
= layout
->set
[set
].layout
;
2899 struct anv_descriptor_slot
*surface_slots
=
2900 set_layout
->stage
[stage
].surface_start
;
2902 uint32_t start
= bias
+ layout
->set
[set
].surface_start
[stage
];
2904 for (uint32_t b
= 0; b
< set_layout
->stage
[stage
].surface_count
; b
++) {
2905 struct anv_surface_view
*view
=
2906 d
->set
->descriptors
[surface_slots
[b
].index
].view
;
2911 struct anv_state state
=
2912 anv_cmd_buffer_alloc_surface_state(cmd_buffer
, 64, 64);
2914 if (state
.map
== NULL
)
2915 return VK_ERROR_OUT_OF_DEVICE_MEMORY
;
2918 if (surface_slots
[b
].dynamic_slot
>= 0) {
2919 uint32_t dynamic_offset
=
2920 d
->dynamic_offsets
[surface_slots
[b
].dynamic_slot
];
2922 offset
= view
->offset
+ dynamic_offset
;
2923 fill_buffer_surface_state(state
.map
, view
->format
, offset
,
2924 view
->range
- dynamic_offset
);
2926 offset
= view
->offset
;
2927 memcpy(state
.map
, view
->surface_state
.map
, 64);
2930 /* The address goes in dwords 8 and 9 of the SURFACE_STATE */
2931 *(uint64_t *)(state
.map
+ 8 * 4) =
2932 anv_reloc_list_add(&cmd_buffer
->surface_relocs
,
2934 state
.offset
+ 8 * 4,
2937 bt_map
[start
+ b
] = state
.offset
;
2945 cmd_buffer_emit_samplers(struct anv_cmd_buffer
*cmd_buffer
, unsigned stage
)
2947 struct anv_pipeline_layout
*layout
= cmd_buffer
->pipeline
->layout
;
2948 struct anv_state state
;
2953 uint32_t sampler_count
= layout
->stage
[stage
].sampler_count
;
2955 if (sampler_count
== 0)
2958 uint32_t size
= sampler_count
* 16;
2959 state
= anv_state_stream_alloc(&cmd_buffer
->dynamic_state_stream
, size
, 32);
2961 if (state
.map
== NULL
)
2962 return VK_ERROR_OUT_OF_DEVICE_MEMORY
;
2964 static const uint32_t sampler_state_opcodes
[] = {
2965 [VK_SHADER_STAGE_VERTEX
] = 43,
2966 [VK_SHADER_STAGE_TESS_CONTROL
] = 44, /* HS */
2967 [VK_SHADER_STAGE_TESS_EVALUATION
] = 45, /* DS */
2968 [VK_SHADER_STAGE_GEOMETRY
] = 46,
2969 [VK_SHADER_STAGE_FRAGMENT
] = 47,
2970 [VK_SHADER_STAGE_COMPUTE
] = 0,
2973 anv_batch_emit(&cmd_buffer
->batch
,
2974 GEN8_3DSTATE_SAMPLER_STATE_POINTERS_VS
,
2975 ._3DCommandSubOpcode
= sampler_state_opcodes
[stage
],
2976 .PointertoVSSamplerState
= state
.offset
);
2978 for (uint32_t set
= 0; set
< layout
->num_sets
; set
++) {
2979 struct anv_descriptor_set_binding
*d
= &cmd_buffer
->descriptors
[set
];
2980 struct anv_descriptor_set_layout
*set_layout
= layout
->set
[set
].layout
;
2981 struct anv_descriptor_slot
*sampler_slots
=
2982 set_layout
->stage
[stage
].sampler_start
;
2984 uint32_t start
= layout
->set
[set
].sampler_start
[stage
];
2986 for (uint32_t b
= 0; b
< set_layout
->stage
[stage
].sampler_count
; b
++) {
2987 struct anv_sampler
*sampler
=
2988 d
->set
->descriptors
[sampler_slots
[b
].index
].sampler
;
2993 memcpy(state
.map
+ (start
+ b
) * 16,
2994 sampler
->state
, sizeof(sampler
->state
));
3002 flush_descriptor_sets(struct anv_cmd_buffer
*cmd_buffer
)
3004 uint32_t s
, dirty
= cmd_buffer
->descriptors_dirty
&
3005 cmd_buffer
->pipeline
->active_stages
;
3008 for_each_bit(s
, dirty
) {
3009 result
= cmd_buffer_emit_binding_table(cmd_buffer
, s
);
3010 if (result
!= VK_SUCCESS
)
3013 result
= cmd_buffer_emit_samplers(cmd_buffer
, s
);
3014 if (result
!= VK_SUCCESS
)
3018 if (result
!= VK_SUCCESS
) {
3019 assert(result
== VK_ERROR_OUT_OF_DEVICE_MEMORY
);
3021 result
= anv_cmd_buffer_new_surface_state_bo(cmd_buffer
);
3022 assert(result
== VK_SUCCESS
);
3024 /* Re-emit all active binding tables */
3025 for_each_bit(s
, cmd_buffer
->pipeline
->active_stages
) {
3026 result
= cmd_buffer_emit_binding_table(cmd_buffer
, s
);
3027 result
= cmd_buffer_emit_samplers(cmd_buffer
, s
);
3030 /* It had better succeed this time */
3031 assert(result
== VK_SUCCESS
);
3034 cmd_buffer
->descriptors_dirty
&= ~cmd_buffer
->pipeline
->active_stages
;
3037 static struct anv_state
3038 anv_cmd_buffer_emit_dynamic(struct anv_cmd_buffer
*cmd_buffer
,
3039 uint32_t *a
, uint32_t dwords
, uint32_t alignment
)
3041 struct anv_device
*device
= cmd_buffer
->device
;
3042 struct anv_state state
;
3044 state
= anv_state_pool_alloc(&device
->dynamic_state_pool
, dwords
* 4, alignment
);
3045 memcpy(state
.map
, a
, dwords
* 4);
3050 static struct anv_state
3051 anv_cmd_buffer_merge_dynamic(struct anv_cmd_buffer
*cmd_buffer
,
3052 uint32_t *a
, uint32_t *b
, uint32_t dwords
, uint32_t alignment
)
3054 struct anv_device
*device
= cmd_buffer
->device
;
3055 struct anv_state state
;
3058 state
= anv_state_pool_alloc(&device
->dynamic_state_pool
, dwords
* 4, alignment
);
3060 for (uint32_t i
= 0; i
< dwords
; i
++)
3067 anv_cmd_buffer_flush_state(struct anv_cmd_buffer
*cmd_buffer
)
3069 struct anv_pipeline
*pipeline
= cmd_buffer
->pipeline
;
3072 uint32_t vb_emit
= cmd_buffer
->vb_dirty
& pipeline
->vb_used
;
3075 const uint32_t num_buffers
= __builtin_popcount(vb_emit
);
3076 const uint32_t num_dwords
= 1 + num_buffers
* 4;
3078 p
= anv_batch_emitn(&cmd_buffer
->batch
, num_dwords
,
3079 GEN8_3DSTATE_VERTEX_BUFFERS
);
3081 for_each_bit(vb
, vb_emit
) {
3082 struct anv_buffer
*buffer
= cmd_buffer
->vertex_bindings
[vb
].buffer
;
3083 uint32_t offset
= cmd_buffer
->vertex_bindings
[vb
].offset
;
3085 struct GEN8_VERTEX_BUFFER_STATE state
= {
3086 .VertexBufferIndex
= vb
,
3087 .MemoryObjectControlState
= GEN8_MOCS
,
3088 .AddressModifyEnable
= true,
3089 .BufferPitch
= pipeline
->binding_stride
[vb
],
3090 .BufferStartingAddress
= { buffer
->bo
, buffer
->offset
+ offset
},
3091 .BufferSize
= buffer
->size
- offset
3094 GEN8_VERTEX_BUFFER_STATE_pack(&cmd_buffer
->batch
, &p
[1 + i
* 4], &state
);
3099 if (cmd_buffer
->dirty
& ANV_CMD_BUFFER_PIPELINE_DIRTY
)
3100 anv_batch_emit_batch(&cmd_buffer
->batch
, &pipeline
->batch
);
3102 if (cmd_buffer
->descriptors_dirty
)
3103 flush_descriptor_sets(cmd_buffer
);
3105 if (cmd_buffer
->dirty
& (ANV_CMD_BUFFER_PIPELINE_DIRTY
| ANV_CMD_BUFFER_RS_DIRTY
)) {
3106 anv_batch_emit_merge(&cmd_buffer
->batch
,
3107 cmd_buffer
->rs_state
->state_sf
, pipeline
->state_sf
);
3108 anv_batch_emit_merge(&cmd_buffer
->batch
,
3109 cmd_buffer
->rs_state
->state_raster
, pipeline
->state_raster
);
3112 if (cmd_buffer
->ds_state
&&
3113 (cmd_buffer
->dirty
& (ANV_CMD_BUFFER_PIPELINE_DIRTY
| ANV_CMD_BUFFER_DS_DIRTY
)))
3114 anv_batch_emit_merge(&cmd_buffer
->batch
,
3115 cmd_buffer
->ds_state
->state_wm_depth_stencil
,
3116 pipeline
->state_wm_depth_stencil
);
3118 if (cmd_buffer
->dirty
& (ANV_CMD_BUFFER_CB_DIRTY
| ANV_CMD_BUFFER_DS_DIRTY
)) {
3119 struct anv_state state
;
3120 if (cmd_buffer
->ds_state
== NULL
)
3121 state
= anv_cmd_buffer_emit_dynamic(cmd_buffer
,
3122 cmd_buffer
->cb_state
->state_color_calc
,
3123 GEN8_COLOR_CALC_STATE_length
, 32);
3124 else if (cmd_buffer
->cb_state
== NULL
)
3125 state
= anv_cmd_buffer_emit_dynamic(cmd_buffer
,
3126 cmd_buffer
->ds_state
->state_color_calc
,
3127 GEN8_COLOR_CALC_STATE_length
, 32);
3129 state
= anv_cmd_buffer_merge_dynamic(cmd_buffer
,
3130 cmd_buffer
->ds_state
->state_color_calc
,
3131 cmd_buffer
->cb_state
->state_color_calc
,
3132 GEN8_COLOR_CALC_STATE_length
, 32);
3134 anv_batch_emit(&cmd_buffer
->batch
,
3135 GEN8_3DSTATE_CC_STATE_POINTERS
,
3136 .ColorCalcStatePointer
= state
.offset
,
3137 .ColorCalcStatePointerValid
= true);
3140 cmd_buffer
->vb_dirty
&= ~vb_emit
;
3141 cmd_buffer
->dirty
= 0;
3145 VkCmdBuffer cmdBuffer
,
3146 uint32_t firstVertex
,
3147 uint32_t vertexCount
,
3148 uint32_t firstInstance
,
3149 uint32_t instanceCount
)
3151 struct anv_cmd_buffer
*cmd_buffer
= (struct anv_cmd_buffer
*) cmdBuffer
;
3153 anv_cmd_buffer_flush_state(cmd_buffer
);
3155 anv_batch_emit(&cmd_buffer
->batch
, GEN8_3DPRIMITIVE
,
3156 .VertexAccessType
= SEQUENTIAL
,
3157 .VertexCountPerInstance
= vertexCount
,
3158 .StartVertexLocation
= firstVertex
,
3159 .InstanceCount
= instanceCount
,
3160 .StartInstanceLocation
= firstInstance
,
3161 .BaseVertexLocation
= 0);
3164 void anv_CmdDrawIndexed(
3165 VkCmdBuffer cmdBuffer
,
3166 uint32_t firstIndex
,
3167 uint32_t indexCount
,
3168 int32_t vertexOffset
,
3169 uint32_t firstInstance
,
3170 uint32_t instanceCount
)
3172 struct anv_cmd_buffer
*cmd_buffer
= (struct anv_cmd_buffer
*) cmdBuffer
;
3174 anv_cmd_buffer_flush_state(cmd_buffer
);
3176 anv_batch_emit(&cmd_buffer
->batch
, GEN8_3DPRIMITIVE
,
3177 .VertexAccessType
= RANDOM
,
3178 .VertexCountPerInstance
= indexCount
,
3179 .StartVertexLocation
= firstIndex
,
3180 .InstanceCount
= instanceCount
,
3181 .StartInstanceLocation
= firstInstance
,
3182 .BaseVertexLocation
= vertexOffset
);
3186 anv_batch_lrm(struct anv_batch
*batch
,
3187 uint32_t reg
, struct anv_bo
*bo
, uint32_t offset
)
3189 anv_batch_emit(batch
, GEN8_MI_LOAD_REGISTER_MEM
,
3190 .RegisterAddress
= reg
,
3191 .MemoryAddress
= { bo
, offset
});
3195 anv_batch_lri(struct anv_batch
*batch
, uint32_t reg
, uint32_t imm
)
3197 anv_batch_emit(batch
, GEN8_MI_LOAD_REGISTER_IMM
,
3198 .RegisterOffset
= reg
,
3202 /* Auto-Draw / Indirect Registers */
3203 #define GEN7_3DPRIM_END_OFFSET 0x2420
3204 #define GEN7_3DPRIM_START_VERTEX 0x2430
3205 #define GEN7_3DPRIM_VERTEX_COUNT 0x2434
3206 #define GEN7_3DPRIM_INSTANCE_COUNT 0x2438
3207 #define GEN7_3DPRIM_START_INSTANCE 0x243C
3208 #define GEN7_3DPRIM_BASE_VERTEX 0x2440
3210 void anv_CmdDrawIndirect(
3211 VkCmdBuffer cmdBuffer
,
3213 VkDeviceSize offset
,
3217 struct anv_cmd_buffer
*cmd_buffer
= (struct anv_cmd_buffer
*) cmdBuffer
;
3218 struct anv_buffer
*buffer
= (struct anv_buffer
*) _buffer
;
3219 struct anv_bo
*bo
= buffer
->bo
;
3220 uint32_t bo_offset
= buffer
->offset
+ offset
;
3222 anv_cmd_buffer_flush_state(cmd_buffer
);
3224 anv_batch_lrm(&cmd_buffer
->batch
, GEN7_3DPRIM_VERTEX_COUNT
, bo
, bo_offset
);
3225 anv_batch_lrm(&cmd_buffer
->batch
, GEN7_3DPRIM_INSTANCE_COUNT
, bo
, bo_offset
+ 4);
3226 anv_batch_lrm(&cmd_buffer
->batch
, GEN7_3DPRIM_START_VERTEX
, bo
, bo_offset
+ 8);
3227 anv_batch_lrm(&cmd_buffer
->batch
, GEN7_3DPRIM_START_INSTANCE
, bo
, bo_offset
+ 12);
3228 anv_batch_lri(&cmd_buffer
->batch
, GEN7_3DPRIM_BASE_VERTEX
, 0);
3230 anv_batch_emit(&cmd_buffer
->batch
, GEN8_3DPRIMITIVE
,
3231 .IndirectParameterEnable
= true,
3232 .VertexAccessType
= SEQUENTIAL
);
3235 void anv_CmdDrawIndexedIndirect(
3236 VkCmdBuffer cmdBuffer
,
3238 VkDeviceSize offset
,
3242 struct anv_cmd_buffer
*cmd_buffer
= (struct anv_cmd_buffer
*) cmdBuffer
;
3243 struct anv_buffer
*buffer
= (struct anv_buffer
*) _buffer
;
3244 struct anv_bo
*bo
= buffer
->bo
;
3245 uint32_t bo_offset
= buffer
->offset
+ offset
;
3247 anv_cmd_buffer_flush_state(cmd_buffer
);
3249 anv_batch_lrm(&cmd_buffer
->batch
, GEN7_3DPRIM_VERTEX_COUNT
, bo
, bo_offset
);
3250 anv_batch_lrm(&cmd_buffer
->batch
, GEN7_3DPRIM_INSTANCE_COUNT
, bo
, bo_offset
+ 4);
3251 anv_batch_lrm(&cmd_buffer
->batch
, GEN7_3DPRIM_START_VERTEX
, bo
, bo_offset
+ 8);
3252 anv_batch_lrm(&cmd_buffer
->batch
, GEN7_3DPRIM_BASE_VERTEX
, bo
, bo_offset
+ 12);
3253 anv_batch_lrm(&cmd_buffer
->batch
, GEN7_3DPRIM_START_INSTANCE
, bo
, bo_offset
+ 16);
3255 anv_batch_emit(&cmd_buffer
->batch
, GEN8_3DPRIMITIVE
,
3256 .IndirectParameterEnable
= true,
3257 .VertexAccessType
= RANDOM
);
3260 void anv_CmdDispatch(
3261 VkCmdBuffer cmdBuffer
,
3269 void anv_CmdDispatchIndirect(
3270 VkCmdBuffer cmdBuffer
,
3272 VkDeviceSize offset
)
3277 void anv_CmdSetEvent(
3278 VkCmdBuffer cmdBuffer
,
3280 VkPipeEvent pipeEvent
)
3285 void anv_CmdResetEvent(
3286 VkCmdBuffer cmdBuffer
,
3288 VkPipeEvent pipeEvent
)
3293 void anv_CmdWaitEvents(
3294 VkCmdBuffer cmdBuffer
,
3295 VkWaitEvent waitEvent
,
3296 uint32_t eventCount
,
3297 const VkEvent
* pEvents
,
3298 uint32_t memBarrierCount
,
3299 const void** ppMemBarriers
)
3304 void anv_CmdPipelineBarrier(
3305 VkCmdBuffer cmdBuffer
,
3306 VkWaitEvent waitEvent
,
3307 uint32_t pipeEventCount
,
3308 const VkPipeEvent
* pPipeEvents
,
3309 uint32_t memBarrierCount
,
3310 const void** ppMemBarriers
)
3315 void anv_CmdInitAtomicCounters(
3316 VkCmdBuffer cmdBuffer
,
3317 VkPipelineBindPoint pipelineBindPoint
,
3318 uint32_t startCounter
,
3319 uint32_t counterCount
,
3320 const uint32_t* pData
)
3325 void anv_CmdLoadAtomicCounters(
3326 VkCmdBuffer cmdBuffer
,
3327 VkPipelineBindPoint pipelineBindPoint
,
3328 uint32_t startCounter
,
3329 uint32_t counterCount
,
3331 VkDeviceSize srcOffset
)
3336 void anv_CmdSaveAtomicCounters(
3337 VkCmdBuffer cmdBuffer
,
3338 VkPipelineBindPoint pipelineBindPoint
,
3339 uint32_t startCounter
,
3340 uint32_t counterCount
,
3341 VkBuffer destBuffer
,
3342 VkDeviceSize destOffset
)
3348 anv_framebuffer_destroy(struct anv_device
*device
,
3349 struct anv_object
*object
,
3350 VkObjectType obj_type
)
3352 struct anv_framebuffer
*fb
= (struct anv_framebuffer
*)object
;
3354 assert(obj_type
== VK_OBJECT_TYPE_FRAMEBUFFER
);
3356 anv_DestroyObject((VkDevice
) device
,
3357 VK_OBJECT_TYPE_DYNAMIC_VP_STATE
,
3360 anv_device_free(device
, fb
);
3363 VkResult
anv_CreateFramebuffer(
3365 const VkFramebufferCreateInfo
* pCreateInfo
,
3366 VkFramebuffer
* pFramebuffer
)
3368 struct anv_device
*device
= (struct anv_device
*) _device
;
3369 struct anv_framebuffer
*framebuffer
;
3371 static const struct anv_depth_stencil_view null_view
=
3372 { .depth_format
= D16_UNORM
, .depth_stride
= 0, .stencil_stride
= 0 };
3374 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO
);
3376 framebuffer
= anv_device_alloc(device
, sizeof(*framebuffer
), 8,
3377 VK_SYSTEM_ALLOC_TYPE_API_OBJECT
);
3378 if (framebuffer
== NULL
)
3379 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
3381 framebuffer
->base
.destructor
= anv_framebuffer_destroy
;
3383 framebuffer
->color_attachment_count
= pCreateInfo
->colorAttachmentCount
;
3384 for (uint32_t i
= 0; i
< pCreateInfo
->colorAttachmentCount
; i
++) {
3385 framebuffer
->color_attachments
[i
] =
3386 (struct anv_surface_view
*) pCreateInfo
->pColorAttachments
[i
].view
;
3389 if (pCreateInfo
->pDepthStencilAttachment
) {
3390 framebuffer
->depth_stencil
=
3391 (struct anv_depth_stencil_view
*) pCreateInfo
->pDepthStencilAttachment
->view
;
3393 framebuffer
->depth_stencil
= &null_view
;
3396 framebuffer
->sample_count
= pCreateInfo
->sampleCount
;
3397 framebuffer
->width
= pCreateInfo
->width
;
3398 framebuffer
->height
= pCreateInfo
->height
;
3399 framebuffer
->layers
= pCreateInfo
->layers
;
3401 vkCreateDynamicViewportState((VkDevice
) device
,
3402 &(VkDynamicVpStateCreateInfo
) {
3403 .sType
= VK_STRUCTURE_TYPE_DYNAMIC_VP_STATE_CREATE_INFO
,
3404 .viewportAndScissorCount
= 1,
3405 .pViewports
= (VkViewport
[]) {
3409 .width
= pCreateInfo
->width
,
3410 .height
= pCreateInfo
->height
,
3415 .pScissors
= (VkRect
[]) {
3417 { pCreateInfo
->width
, pCreateInfo
->height
} },
3420 &framebuffer
->vp_state
);
3422 *pFramebuffer
= (VkFramebuffer
) framebuffer
;
3427 VkResult
anv_CreateRenderPass(
3429 const VkRenderPassCreateInfo
* pCreateInfo
,
3430 VkRenderPass
* pRenderPass
)
3432 struct anv_device
*device
= (struct anv_device
*) _device
;
3433 struct anv_render_pass
*pass
;
3436 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO
);
3438 size
= sizeof(*pass
) +
3439 pCreateInfo
->layers
* sizeof(struct anv_render_pass_layer
);
3440 pass
= anv_device_alloc(device
, size
, 8,
3441 VK_SYSTEM_ALLOC_TYPE_API_OBJECT
);
3443 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
3445 pass
->render_area
= pCreateInfo
->renderArea
;
3447 pass
->num_layers
= pCreateInfo
->layers
;
3449 pass
->num_clear_layers
= 0;
3450 for (uint32_t i
= 0; i
< pCreateInfo
->layers
; i
++) {
3451 pass
->layers
[i
].color_load_op
= pCreateInfo
->pColorLoadOps
[i
];
3452 pass
->layers
[i
].clear_color
= pCreateInfo
->pColorLoadClearValues
[i
];
3453 if (pass
->layers
[i
].color_load_op
== VK_ATTACHMENT_LOAD_OP_CLEAR
)
3454 pass
->num_clear_layers
++;
3457 *pRenderPass
= (VkRenderPass
) pass
;
3463 anv_cmd_buffer_emit_depth_stencil(struct anv_cmd_buffer
*cmd_buffer
,
3464 struct anv_render_pass
*pass
)
3466 const struct anv_depth_stencil_view
*view
=
3467 cmd_buffer
->framebuffer
->depth_stencil
;
3469 /* FIXME: Implement the PMA stall W/A */
3471 anv_batch_emit(&cmd_buffer
->batch
, GEN8_3DSTATE_DEPTH_BUFFER
,
3472 .SurfaceType
= SURFTYPE_2D
,
3473 .DepthWriteEnable
= view
->depth_stride
> 0,
3474 .StencilWriteEnable
= view
->stencil_stride
> 0,
3475 .HierarchicalDepthBufferEnable
= false,
3476 .SurfaceFormat
= view
->depth_format
,
3477 .SurfacePitch
= view
->depth_stride
> 0 ? view
->depth_stride
- 1 : 0,
3478 .SurfaceBaseAddress
= { view
->bo
, view
->depth_offset
},
3479 .Height
= pass
->render_area
.extent
.height
- 1,
3480 .Width
= pass
->render_area
.extent
.width
- 1,
3483 .MinimumArrayElement
= 0,
3484 .DepthBufferObjectControlState
= GEN8_MOCS
,
3485 .RenderTargetViewExtent
= 1 - 1,
3486 .SurfaceQPitch
= 0);
3488 /* Disable hierarchial depth buffers. */
3489 anv_batch_emit(&cmd_buffer
->batch
, GEN8_3DSTATE_HIER_DEPTH_BUFFER
);
3491 anv_batch_emit(&cmd_buffer
->batch
, GEN8_3DSTATE_STENCIL_BUFFER
,
3492 .StencilBufferEnable
= view
->stencil_stride
> 0,
3493 .StencilBufferObjectControlState
= GEN8_MOCS
,
3494 .SurfacePitch
= view
->stencil_stride
> 0 ? view
->stencil_stride
- 1 : 0,
3495 .SurfaceBaseAddress
= { view
->bo
, view
->stencil_offset
},
3496 .SurfaceQPitch
= 0);
3498 /* Clear the clear params. */
3499 anv_batch_emit(&cmd_buffer
->batch
, GEN8_3DSTATE_CLEAR_PARAMS
);
3502 void anv_CmdBeginRenderPass(
3503 VkCmdBuffer cmdBuffer
,
3504 const VkRenderPassBegin
* pRenderPassBegin
)
3506 struct anv_cmd_buffer
*cmd_buffer
= (struct anv_cmd_buffer
*) cmdBuffer
;
3507 struct anv_render_pass
*pass
= (struct anv_render_pass
*) pRenderPassBegin
->renderPass
;
3508 struct anv_framebuffer
*framebuffer
=
3509 (struct anv_framebuffer
*) pRenderPassBegin
->framebuffer
;
3511 cmd_buffer
->framebuffer
= framebuffer
;
3513 cmd_buffer
->descriptors_dirty
|= VK_SHADER_STAGE_FRAGMENT_BIT
;
3515 anv_batch_emit(&cmd_buffer
->batch
, GEN8_3DSTATE_DRAWING_RECTANGLE
,
3516 .ClippedDrawingRectangleYMin
= pass
->render_area
.offset
.y
,
3517 .ClippedDrawingRectangleXMin
= pass
->render_area
.offset
.x
,
3518 .ClippedDrawingRectangleYMax
=
3519 pass
->render_area
.offset
.y
+ pass
->render_area
.extent
.height
- 1,
3520 .ClippedDrawingRectangleXMax
=
3521 pass
->render_area
.offset
.x
+ pass
->render_area
.extent
.width
- 1,
3522 .DrawingRectangleOriginY
= 0,
3523 .DrawingRectangleOriginX
= 0);
3525 anv_cmd_buffer_emit_depth_stencil(cmd_buffer
, pass
);
3527 anv_cmd_buffer_clear(cmd_buffer
, pass
);
3530 void anv_CmdEndRenderPass(
3531 VkCmdBuffer cmdBuffer
,
3532 VkRenderPass renderPass
)
3534 /* Emit a flushing pipe control at the end of a pass. This is kind of a
3535 * hack but it ensures that render targets always actually get written.
3536 * Eventually, we should do flushing based on image format transitions
3537 * or something of that nature.
3539 struct anv_cmd_buffer
*cmd_buffer
= (struct anv_cmd_buffer
*)cmdBuffer
;
3540 anv_batch_emit(&cmd_buffer
->batch
, GEN8_PIPE_CONTROL
,
3541 .PostSyncOperation
= NoWrite
,
3542 .RenderTargetCacheFlushEnable
= true,
3543 .InstructionCacheInvalidateEnable
= true,
3544 .DepthCacheFlushEnable
= true,
3545 .VFCacheInvalidationEnable
= true,
3546 .TextureCacheInvalidationEnable
= true,
3547 .CommandStreamerStallEnable
= true);
3550 void vkCmdDbgMarkerBegin(
3551 VkCmdBuffer cmdBuffer
,
3552 const char* pMarker
)
3553 __attribute__ ((visibility ("default")));
3555 void vkCmdDbgMarkerEnd(
3556 VkCmdBuffer cmdBuffer
)
3557 __attribute__ ((visibility ("default")));
3559 VkResult
vkDbgSetObjectTag(
3564 __attribute__ ((visibility ("default")));
3567 void vkCmdDbgMarkerBegin(
3568 VkCmdBuffer cmdBuffer
,
3569 const char* pMarker
)
3573 void vkCmdDbgMarkerEnd(
3574 VkCmdBuffer cmdBuffer
)
3578 VkResult
vkDbgSetObjectTag(