vk/0.210.0: Rework vkQueueSubmit
[mesa.git] / src / vulkan / anv_device.c
1 /*
2 * Copyright © 2015 Intel Corporation
3 *
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:
10 *
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
13 * Software.
14 *
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
21 * IN THE SOFTWARE.
22 */
23
24 #include <assert.h>
25 #include <stdbool.h>
26 #include <string.h>
27 #include <unistd.h>
28 #include <fcntl.h>
29
30 #include "anv_private.h"
31 #include "mesa/main/git_sha1.h"
32 #include "util/strtod.h"
33
34 #include "gen7_pack.h"
35
36 struct anv_dispatch_table dtable;
37
38 static void
39 compiler_debug_log(void *data, const char *fmt, ...)
40 { }
41
42 static void
43 compiler_perf_log(void *data, const char *fmt, ...)
44 {
45 va_list args;
46 va_start(args, fmt);
47
48 if (unlikely(INTEL_DEBUG & DEBUG_PERF))
49 vfprintf(stderr, fmt, args);
50
51 va_end(args);
52 }
53
54 static VkResult
55 anv_physical_device_init(struct anv_physical_device *device,
56 struct anv_instance *instance,
57 const char *path)
58 {
59 VkResult result;
60 int fd;
61
62 fd = open(path, O_RDWR | O_CLOEXEC);
63 if (fd < 0)
64 return vk_errorf(VK_ERROR_INITIALIZATION_FAILED,
65 "failed to open %s: %m", path);
66
67 device->_loader_data.loaderMagic = ICD_LOADER_MAGIC;
68 device->instance = instance;
69 device->path = path;
70
71 device->chipset_id = anv_gem_get_param(fd, I915_PARAM_CHIPSET_ID);
72 if (!device->chipset_id) {
73 result = vk_errorf(VK_ERROR_INITIALIZATION_FAILED,
74 "failed to get chipset id: %m");
75 goto fail;
76 }
77
78 device->name = brw_get_device_name(device->chipset_id);
79 device->info = brw_get_device_info(device->chipset_id);
80 if (!device->info) {
81 result = vk_errorf(VK_ERROR_INITIALIZATION_FAILED,
82 "failed to get device info");
83 goto fail;
84 }
85
86 if (device->info->is_haswell) {
87 fprintf(stderr, "WARNING: Haswell Vulkan support is incomplete\n");
88 } else if (device->info->gen == 7 && !device->info->is_baytrail) {
89 fprintf(stderr, "WARNING: Ivy Bridge Vulkan support is incomplete\n");
90 } else if (device->info->gen == 9) {
91 fprintf(stderr, "WARNING: Skylake Vulkan support is incomplete\n");
92 } else if (device->info->gen == 8 && !device->info->is_cherryview) {
93 /* Broadwell is as fully supported as anything */
94 } else {
95 result = vk_errorf(VK_ERROR_INCOMPATIBLE_DRIVER,
96 "Vulkan not yet supported on %s", device->name);
97 goto fail;
98 }
99
100 if (anv_gem_get_aperture(fd, &device->aperture_size) == -1) {
101 result = vk_errorf(VK_ERROR_INITIALIZATION_FAILED,
102 "failed to get aperture size: %m");
103 goto fail;
104 }
105
106 if (!anv_gem_get_param(fd, I915_PARAM_HAS_WAIT_TIMEOUT)) {
107 result = vk_errorf(VK_ERROR_INITIALIZATION_FAILED,
108 "kernel missing gem wait");
109 goto fail;
110 }
111
112 if (!anv_gem_get_param(fd, I915_PARAM_HAS_EXECBUF2)) {
113 result = vk_errorf(VK_ERROR_INITIALIZATION_FAILED,
114 "kernel missing execbuf2");
115 goto fail;
116 }
117
118 if (!anv_gem_get_param(fd, I915_PARAM_HAS_LLC)) {
119 result = vk_errorf(VK_ERROR_INITIALIZATION_FAILED,
120 "non-llc gpu");
121 goto fail;
122 }
123
124 close(fd);
125
126 brw_process_intel_debug_variable();
127
128 device->compiler = brw_compiler_create(NULL, device->info);
129 if (device->compiler == NULL) {
130 result = vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
131 goto fail;
132 }
133 device->compiler->shader_debug_log = compiler_debug_log;
134 device->compiler->shader_perf_log = compiler_perf_log;
135
136 isl_device_init(&device->isl_dev, device->info);
137
138 return VK_SUCCESS;
139
140 fail:
141 close(fd);
142 return result;
143 }
144
145 static void
146 anv_physical_device_finish(struct anv_physical_device *device)
147 {
148 ralloc_free(device->compiler);
149 }
150
151 static const VkExtensionProperties global_extensions[] = {
152 {
153 .extensionName = VK_EXT_KHR_SWAPCHAIN_EXTENSION_NAME,
154 .specVersion = 17,
155 },
156 };
157
158 static const VkExtensionProperties device_extensions[] = {
159 {
160 .extensionName = VK_EXT_KHR_DEVICE_SWAPCHAIN_EXTENSION_NAME,
161 .specVersion = 53,
162 },
163 };
164
165 static void *
166 default_alloc_func(void *pUserData, size_t size, size_t align,
167 VkSystemAllocationScope allocationScope)
168 {
169 return malloc(size);
170 }
171
172 static void *
173 default_realloc_func(void *pUserData, void *pOriginal, size_t size,
174 size_t align, VkSystemAllocationScope allocationScope)
175 {
176 return realloc(pOriginal, size);
177 }
178
179 static void
180 default_free_func(void *pUserData, void *pMemory)
181 {
182 free(pMemory);
183 }
184
185 static const VkAllocationCallbacks default_alloc = {
186 .pUserData = NULL,
187 .pfnAllocation = default_alloc_func,
188 .pfnReallocation = default_realloc_func,
189 .pfnFree = default_free_func,
190 };
191
192 VkResult anv_CreateInstance(
193 const VkInstanceCreateInfo* pCreateInfo,
194 const VkAllocationCallbacks* pAllocator,
195 VkInstance* pInstance)
196 {
197 struct anv_instance *instance;
198
199 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO);
200
201 if (pCreateInfo->pApplicationInfo->apiVersion != VK_MAKE_VERSION(0, 170, 2))
202 return vk_error(VK_ERROR_INCOMPATIBLE_DRIVER);
203
204 for (uint32_t i = 0; i < pCreateInfo->enabledExtensionNameCount; i++) {
205 bool found = false;
206 for (uint32_t j = 0; j < ARRAY_SIZE(global_extensions); j++) {
207 if (strcmp(pCreateInfo->ppEnabledExtensionNames[i],
208 global_extensions[j].extensionName) == 0) {
209 found = true;
210 break;
211 }
212 }
213 if (!found)
214 return vk_error(VK_ERROR_EXTENSION_NOT_PRESENT);
215 }
216
217 instance = anv_alloc2(&default_alloc, pAllocator, sizeof(*instance), 8,
218 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
219 if (!instance)
220 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
221
222 instance->_loader_data.loaderMagic = ICD_LOADER_MAGIC;
223
224 if (pAllocator)
225 instance->alloc = *pAllocator;
226 else
227 instance->alloc = default_alloc;
228
229 instance->apiVersion = pCreateInfo->pApplicationInfo->apiVersion;
230 instance->physicalDeviceCount = -1;
231
232 _mesa_locale_init();
233
234 VG(VALGRIND_CREATE_MEMPOOL(instance, 0, false));
235
236 anv_init_wsi(instance);
237
238 *pInstance = anv_instance_to_handle(instance);
239
240 return VK_SUCCESS;
241 }
242
243 void anv_DestroyInstance(
244 VkInstance _instance,
245 const VkAllocationCallbacks* pAllocator)
246 {
247 ANV_FROM_HANDLE(anv_instance, instance, _instance);
248
249 if (instance->physicalDeviceCount > 0) {
250 /* We support at most one physical device. */
251 assert(instance->physicalDeviceCount == 1);
252 anv_physical_device_finish(&instance->physicalDevice);
253 }
254
255 anv_finish_wsi(instance);
256
257 VG(VALGRIND_DESTROY_MEMPOOL(instance));
258
259 _mesa_locale_fini();
260
261 anv_free(&instance->alloc, instance);
262 }
263
264 VkResult anv_EnumeratePhysicalDevices(
265 VkInstance _instance,
266 uint32_t* pPhysicalDeviceCount,
267 VkPhysicalDevice* pPhysicalDevices)
268 {
269 ANV_FROM_HANDLE(anv_instance, instance, _instance);
270 VkResult result;
271
272 if (instance->physicalDeviceCount < 0) {
273 result = anv_physical_device_init(&instance->physicalDevice,
274 instance, "/dev/dri/renderD128");
275 if (result == VK_ERROR_INCOMPATIBLE_DRIVER) {
276 instance->physicalDeviceCount = 0;
277 } else if (result == VK_SUCCESS) {
278 instance->physicalDeviceCount = 1;
279 } else {
280 return result;
281 }
282 }
283
284 /* pPhysicalDeviceCount is an out parameter if pPhysicalDevices is NULL;
285 * otherwise it's an inout parameter.
286 *
287 * The Vulkan spec (git aaed022) says:
288 *
289 * pPhysicalDeviceCount is a pointer to an unsigned integer variable
290 * that is initialized with the number of devices the application is
291 * prepared to receive handles to. pname:pPhysicalDevices is pointer to
292 * an array of at least this many VkPhysicalDevice handles [...].
293 *
294 * Upon success, if pPhysicalDevices is NULL, vkEnumeratePhysicalDevices
295 * overwrites the contents of the variable pointed to by
296 * pPhysicalDeviceCount with the number of physical devices in in the
297 * instance; otherwise, vkEnumeratePhysicalDevices overwrites
298 * pPhysicalDeviceCount with the number of physical handles written to
299 * pPhysicalDevices.
300 */
301 if (!pPhysicalDevices) {
302 *pPhysicalDeviceCount = instance->physicalDeviceCount;
303 } else if (*pPhysicalDeviceCount >= 1) {
304 pPhysicalDevices[0] = anv_physical_device_to_handle(&instance->physicalDevice);
305 *pPhysicalDeviceCount = 1;
306 } else {
307 *pPhysicalDeviceCount = 0;
308 }
309
310 return VK_SUCCESS;
311 }
312
313 void anv_GetPhysicalDeviceFeatures(
314 VkPhysicalDevice physicalDevice,
315 VkPhysicalDeviceFeatures* pFeatures)
316 {
317 anv_finishme("Get correct values for PhysicalDeviceFeatures");
318
319 *pFeatures = (VkPhysicalDeviceFeatures) {
320 .robustBufferAccess = false,
321 .fullDrawIndexUint32 = false,
322 .imageCubeArray = false,
323 .independentBlend = false,
324 .geometryShader = true,
325 .tessellationShader = false,
326 .sampleRateShading = false,
327 .dualSrcBlend = true,
328 .logicOp = true,
329 .multiDrawIndirect = true,
330 .depthClamp = false,
331 .depthBiasClamp = false,
332 .fillModeNonSolid = true,
333 .depthBounds = false,
334 .wideLines = true,
335 .largePoints = true,
336 .alphaToOne = true,
337 .multiViewport = true,
338 .samplerAnisotropy = false, /* FINISHME */
339 .textureCompressionETC2 = true,
340 .textureCompressionASTC_LDR = true,
341 .textureCompressionBC = true,
342 .occlusionQueryPrecise = false, /* FINISHME */
343 .pipelineStatisticsQuery = true,
344 .vertexPipelineStoresAndAtomics = false,
345 .fragmentStoresAndAtomics = true,
346 .shaderTessellationAndGeometryPointSize = true,
347 .shaderImageGatherExtended = true,
348 .shaderStorageImageExtendedFormats = false,
349 .shaderStorageImageMultisample = false,
350 .shaderUniformBufferArrayDynamicIndexing = true,
351 .shaderSampledImageArrayDynamicIndexing = false,
352 .shaderStorageBufferArrayDynamicIndexing = false,
353 .shaderStorageImageArrayDynamicIndexing = false,
354 .shaderStorageImageReadWithoutFormat = false,
355 .shaderStorageImageWriteWithoutFormat = true,
356 .shaderClipDistance = false,
357 .shaderCullDistance = false,
358 .shaderFloat64 = false,
359 .shaderInt64 = false,
360 .shaderInt16 = false,
361 .alphaToOne = true,
362 .variableMultisampleRate = false,
363 };
364 }
365
366 void anv_GetPhysicalDeviceProperties(
367 VkPhysicalDevice physicalDevice,
368 VkPhysicalDeviceProperties* pProperties)
369 {
370 ANV_FROM_HANDLE(anv_physical_device, pdevice, physicalDevice);
371 const struct brw_device_info *devinfo = pdevice->info;
372
373 anv_finishme("Get correct values for VkPhysicalDeviceLimits");
374
375 VkSampleCountFlags sample_counts =
376 VK_SAMPLE_COUNT_1_BIT |
377 VK_SAMPLE_COUNT_2_BIT |
378 VK_SAMPLE_COUNT_4_BIT |
379 VK_SAMPLE_COUNT_8_BIT;
380
381 VkPhysicalDeviceLimits limits = {
382 .maxImageDimension1D = (1 << 14),
383 .maxImageDimension2D = (1 << 14),
384 .maxImageDimension3D = (1 << 10),
385 .maxImageDimensionCube = (1 << 14),
386 .maxImageArrayLayers = (1 << 10),
387 .maxTexelBufferElements = (1 << 14),
388 .maxUniformBufferRange = UINT32_MAX,
389 .maxStorageBufferRange = UINT32_MAX,
390 .maxPushConstantsSize = MAX_PUSH_CONSTANTS_SIZE,
391 .maxMemoryAllocationCount = UINT32_MAX,
392 .maxSamplerAllocationCount = UINT32_MAX,
393 .bufferImageGranularity = 64, /* A cache line */
394 .sparseAddressSpaceSize = 0,
395 .maxBoundDescriptorSets = MAX_SETS,
396 .maxPerStageDescriptorSamplers = 64,
397 .maxPerStageDescriptorUniformBuffers = 64,
398 .maxPerStageDescriptorStorageBuffers = 64,
399 .maxPerStageDescriptorSampledImages = 64,
400 .maxPerStageDescriptorStorageImages = 64,
401 .maxPerStageDescriptorInputAttachments = 64,
402 .maxPerStageResources = 128,
403 .maxDescriptorSetSamplers = 256,
404 .maxDescriptorSetUniformBuffers = 256,
405 .maxDescriptorSetUniformBuffersDynamic = 256,
406 .maxDescriptorSetStorageBuffers = 256,
407 .maxDescriptorSetStorageBuffersDynamic = 256,
408 .maxDescriptorSetSampledImages = 256,
409 .maxDescriptorSetStorageImages = 256,
410 .maxDescriptorSetInputAttachments = 256,
411 .maxVertexInputAttributes = 32,
412 .maxVertexInputBindings = 32,
413 .maxVertexInputAttributeOffset = 256,
414 .maxVertexInputBindingStride = 256,
415 .maxVertexOutputComponents = 32,
416 .maxTessellationGenerationLevel = 0,
417 .maxTessellationPatchSize = 0,
418 .maxTessellationControlPerVertexInputComponents = 0,
419 .maxTessellationControlPerVertexOutputComponents = 0,
420 .maxTessellationControlPerPatchOutputComponents = 0,
421 .maxTessellationControlTotalOutputComponents = 0,
422 .maxTessellationEvaluationInputComponents = 0,
423 .maxTessellationEvaluationOutputComponents = 0,
424 .maxGeometryShaderInvocations = 6,
425 .maxGeometryInputComponents = 16,
426 .maxGeometryOutputComponents = 16,
427 .maxGeometryOutputVertices = 16,
428 .maxGeometryTotalOutputComponents = 16,
429 .maxFragmentInputComponents = 16,
430 .maxFragmentOutputAttachments = 8,
431 .maxFragmentDualSrcAttachments = 2,
432 .maxFragmentCombinedOutputResources = 8,
433 .maxComputeSharedMemorySize = 1024,
434 .maxComputeWorkGroupCount = {
435 16 * devinfo->max_cs_threads,
436 16 * devinfo->max_cs_threads,
437 16 * devinfo->max_cs_threads,
438 },
439 .maxComputeWorkGroupInvocations = 16 * devinfo->max_cs_threads,
440 .maxComputeWorkGroupSize = {
441 16 * devinfo->max_cs_threads,
442 16 * devinfo->max_cs_threads,
443 16 * devinfo->max_cs_threads,
444 },
445 .subPixelPrecisionBits = 4 /* FIXME */,
446 .subTexelPrecisionBits = 4 /* FIXME */,
447 .mipmapPrecisionBits = 4 /* FIXME */,
448 .maxDrawIndexedIndexValue = UINT32_MAX,
449 .maxDrawIndirectCount = UINT32_MAX,
450 .maxSamplerLodBias = 16,
451 .maxSamplerAnisotropy = 16,
452 .maxViewports = MAX_VIEWPORTS,
453 .maxViewportDimensions = { (1 << 14), (1 << 14) },
454 .viewportBoundsRange = { -1.0, 1.0 }, /* FIXME */
455 .viewportSubPixelBits = 13, /* We take a float? */
456 .minMemoryMapAlignment = 64, /* A cache line */
457 .minTexelBufferOffsetAlignment = 1,
458 .minUniformBufferOffsetAlignment = 1,
459 .minStorageBufferOffsetAlignment = 1,
460 .minTexelOffset = 0, /* FIXME */
461 .maxTexelOffset = 0, /* FIXME */
462 .minTexelGatherOffset = 0, /* FIXME */
463 .maxTexelGatherOffset = 0, /* FIXME */
464 .minInterpolationOffset = 0, /* FIXME */
465 .maxInterpolationOffset = 0, /* FIXME */
466 .subPixelInterpolationOffsetBits = 0, /* FIXME */
467 .maxFramebufferWidth = (1 << 14),
468 .maxFramebufferHeight = (1 << 14),
469 .maxFramebufferLayers = (1 << 10),
470 .framebufferColorSampleCounts = sample_counts,
471 .framebufferDepthSampleCounts = sample_counts,
472 .framebufferStencilSampleCounts = sample_counts,
473 .framebufferNoAttachmentsSampleCounts = sample_counts,
474 .maxColorAttachments = MAX_RTS,
475 .sampledImageColorSampleCounts = sample_counts,
476 .sampledImageIntegerSampleCounts = VK_SAMPLE_COUNT_1_BIT,
477 .sampledImageDepthSampleCounts = sample_counts,
478 .sampledImageStencilSampleCounts = sample_counts,
479 .storageImageSampleCounts = VK_SAMPLE_COUNT_1_BIT,
480 .maxSampleMaskWords = 1,
481 .timestampPeriod = 80.0 / (1000 * 1000 * 1000),
482 .maxClipDistances = 0 /* FIXME */,
483 .maxCullDistances = 0 /* FIXME */,
484 .maxCombinedClipAndCullDistances = 0 /* FIXME */,
485 .discreteQueuePriorities = 1,
486 .pointSizeRange = { 0.125, 255.875 },
487 .lineWidthRange = { 0.0, 7.9921875 },
488 .pointSizeGranularity = (1.0 / 8.0),
489 .lineWidthGranularity = (1.0 / 128.0),
490 .strictLines = false, /* FINISHME */
491 .standardSampleLocations = true, /* FINISHME */
492 .optimalBufferCopyOffsetAlignment = 128,
493 .optimalBufferCopyRowPitchAlignment = 128,
494 .nonCoherentAtomSize = 64,
495 };
496
497 *pProperties = (VkPhysicalDeviceProperties) {
498 .apiVersion = VK_MAKE_VERSION(0, 170, 2),
499 .driverVersion = 1,
500 .vendorID = 0x8086,
501 .deviceID = pdevice->chipset_id,
502 .deviceType = VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU,
503 .limits = limits,
504 .sparseProperties = {0}, /* Broadwell doesn't do sparse. */
505 };
506
507 strcpy(pProperties->deviceName, pdevice->name);
508 snprintf((char *)pProperties->pipelineCacheUUID, VK_UUID_SIZE,
509 "anv-%s", MESA_GIT_SHA1 + 4);
510 }
511
512 void anv_GetPhysicalDeviceQueueFamilyProperties(
513 VkPhysicalDevice physicalDevice,
514 uint32_t* pCount,
515 VkQueueFamilyProperties* pQueueFamilyProperties)
516 {
517 if (pQueueFamilyProperties == NULL) {
518 *pCount = 1;
519 return;
520 }
521
522 assert(*pCount >= 1);
523
524 *pQueueFamilyProperties = (VkQueueFamilyProperties) {
525 .queueFlags = VK_QUEUE_GRAPHICS_BIT |
526 VK_QUEUE_COMPUTE_BIT |
527 VK_QUEUE_TRANSFER_BIT,
528 .queueCount = 1,
529 .timestampValidBits = 0, /* XXX: Real value here */
530 .minImageTransferGranularity = (VkExtent3D) { 1, 1, 1 },
531 };
532 }
533
534 void anv_GetPhysicalDeviceMemoryProperties(
535 VkPhysicalDevice physicalDevice,
536 VkPhysicalDeviceMemoryProperties* pMemoryProperties)
537 {
538 ANV_FROM_HANDLE(anv_physical_device, physical_device, physicalDevice);
539 VkDeviceSize heap_size;
540
541 /* Reserve some wiggle room for the driver by exposing only 75% of the
542 * aperture to the heap.
543 */
544 heap_size = 3 * physical_device->aperture_size / 4;
545
546 /* The property flags below are valid only for llc platforms. */
547 pMemoryProperties->memoryTypeCount = 1;
548 pMemoryProperties->memoryTypes[0] = (VkMemoryType) {
549 .propertyFlags = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT |
550 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
551 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT |
552 VK_MEMORY_PROPERTY_HOST_CACHED_BIT,
553 .heapIndex = 1,
554 };
555
556 pMemoryProperties->memoryHeapCount = 1;
557 pMemoryProperties->memoryHeaps[0] = (VkMemoryHeap) {
558 .size = heap_size,
559 .flags = VK_MEMORY_HEAP_DEVICE_LOCAL_BIT,
560 };
561 }
562
563 PFN_vkVoidFunction anv_GetInstanceProcAddr(
564 VkInstance instance,
565 const char* pName)
566 {
567 return anv_lookup_entrypoint(pName);
568 }
569
570 PFN_vkVoidFunction anv_GetDeviceProcAddr(
571 VkDevice device,
572 const char* pName)
573 {
574 return anv_lookup_entrypoint(pName);
575 }
576
577 static VkResult
578 anv_queue_init(struct anv_device *device, struct anv_queue *queue)
579 {
580 queue->_loader_data.loaderMagic = ICD_LOADER_MAGIC;
581 queue->device = device;
582 queue->pool = &device->surface_state_pool;
583
584 return VK_SUCCESS;
585 }
586
587 static void
588 anv_queue_finish(struct anv_queue *queue)
589 {
590 }
591
592 static void
593 anv_device_init_border_colors(struct anv_device *device)
594 {
595 static const VkClearColorValue border_colors[] = {
596 [VK_BORDER_COLOR_FLOAT_TRANSPARENT_BLACK] = { .float32 = { 0.0, 0.0, 0.0, 0.0 } },
597 [VK_BORDER_COLOR_FLOAT_OPAQUE_BLACK] = { .float32 = { 0.0, 0.0, 0.0, 1.0 } },
598 [VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE] = { .float32 = { 1.0, 1.0, 1.0, 1.0 } },
599 [VK_BORDER_COLOR_INT_TRANSPARENT_BLACK] = { .uint32 = { 0, 0, 0, 0 } },
600 [VK_BORDER_COLOR_INT_OPAQUE_BLACK] = { .uint32 = { 0, 0, 0, 1 } },
601 [VK_BORDER_COLOR_INT_OPAQUE_WHITE] = { .uint32 = { 1, 1, 1, 1 } },
602 };
603
604 device->border_colors =
605 anv_state_pool_alloc(&device->dynamic_state_pool,
606 sizeof(border_colors), 32);
607 memcpy(device->border_colors.map, border_colors, sizeof(border_colors));
608 }
609
610 VkResult anv_CreateDevice(
611 VkPhysicalDevice physicalDevice,
612 const VkDeviceCreateInfo* pCreateInfo,
613 const VkAllocationCallbacks* pAllocator,
614 VkDevice* pDevice)
615 {
616 ANV_FROM_HANDLE(anv_physical_device, physical_device, physicalDevice);
617 struct anv_device *device;
618
619 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO);
620
621 for (uint32_t i = 0; i < pCreateInfo->enabledExtensionNameCount; i++) {
622 bool found = false;
623 for (uint32_t j = 0; j < ARRAY_SIZE(device_extensions); j++) {
624 if (strcmp(pCreateInfo->ppEnabledExtensionNames[i],
625 device_extensions[j].extensionName) == 0) {
626 found = true;
627 break;
628 }
629 }
630 if (!found)
631 return vk_error(VK_ERROR_EXTENSION_NOT_PRESENT);
632 }
633
634 anv_set_dispatch_devinfo(physical_device->info);
635
636 device = anv_alloc2(&physical_device->instance->alloc, pAllocator,
637 sizeof(*device), 8,
638 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
639 if (!device)
640 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
641
642 device->_loader_data.loaderMagic = ICD_LOADER_MAGIC;
643 device->instance = physical_device->instance;
644
645 if (pAllocator)
646 device->alloc = *pAllocator;
647 else
648 device->alloc = physical_device->instance->alloc;
649
650 /* XXX(chadv): Can we dup() physicalDevice->fd here? */
651 device->fd = open(physical_device->path, O_RDWR | O_CLOEXEC);
652 if (device->fd == -1)
653 goto fail_device;
654
655 device->context_id = anv_gem_create_context(device);
656 if (device->context_id == -1)
657 goto fail_fd;
658
659 pthread_mutex_init(&device->mutex, NULL);
660
661 anv_bo_pool_init(&device->batch_bo_pool, device, ANV_CMD_BUFFER_BATCH_SIZE);
662
663 anv_block_pool_init(&device->dynamic_state_block_pool, device, 2048);
664
665 anv_state_pool_init(&device->dynamic_state_pool,
666 &device->dynamic_state_block_pool);
667
668 anv_block_pool_init(&device->instruction_block_pool, device, 4096);
669 anv_block_pool_init(&device->surface_state_block_pool, device, 4096);
670
671 anv_state_pool_init(&device->surface_state_pool,
672 &device->surface_state_block_pool);
673
674 anv_bo_init_new(&device->workaround_bo, device, 1024);
675
676 anv_block_pool_init(&device->scratch_block_pool, device, 0x10000);
677
678 device->info = *physical_device->info;
679 device->isl_dev = physical_device->isl_dev;
680
681 anv_queue_init(device, &device->queue);
682
683 anv_device_init_meta(device);
684
685 anv_device_init_border_colors(device);
686
687 *pDevice = anv_device_to_handle(device);
688
689 return VK_SUCCESS;
690
691 fail_fd:
692 close(device->fd);
693 fail_device:
694 anv_free(&device->alloc, device);
695
696 return vk_error(VK_ERROR_INITIALIZATION_FAILED);
697 }
698
699 void anv_DestroyDevice(
700 VkDevice _device,
701 const VkAllocationCallbacks* pAllocator)
702 {
703 ANV_FROM_HANDLE(anv_device, device, _device);
704
705 anv_queue_finish(&device->queue);
706
707 anv_device_finish_meta(device);
708
709 #ifdef HAVE_VALGRIND
710 /* We only need to free these to prevent valgrind errors. The backing
711 * BO will go away in a couple of lines so we don't actually leak.
712 */
713 anv_state_pool_free(&device->dynamic_state_pool, device->border_colors);
714 #endif
715
716 anv_gem_munmap(device->workaround_bo.map, device->workaround_bo.size);
717 anv_gem_close(device, device->workaround_bo.gem_handle);
718
719 anv_bo_pool_finish(&device->batch_bo_pool);
720 anv_state_pool_finish(&device->dynamic_state_pool);
721 anv_block_pool_finish(&device->dynamic_state_block_pool);
722 anv_block_pool_finish(&device->instruction_block_pool);
723 anv_state_pool_finish(&device->surface_state_pool);
724 anv_block_pool_finish(&device->surface_state_block_pool);
725 anv_block_pool_finish(&device->scratch_block_pool);
726
727 close(device->fd);
728
729 anv_free(&device->alloc, device);
730 }
731
732 VkResult anv_EnumerateInstanceExtensionProperties(
733 const char* pLayerName,
734 uint32_t* pPropertyCount,
735 VkExtensionProperties* pProperties)
736 {
737 if (pProperties == NULL) {
738 *pPropertyCount = ARRAY_SIZE(global_extensions);
739 return VK_SUCCESS;
740 }
741
742 assert(*pPropertyCount >= ARRAY_SIZE(global_extensions));
743
744 *pPropertyCount = ARRAY_SIZE(global_extensions);
745 memcpy(pProperties, global_extensions, sizeof(global_extensions));
746
747 return VK_SUCCESS;
748 }
749
750 VkResult anv_EnumerateDeviceExtensionProperties(
751 VkPhysicalDevice physicalDevice,
752 const char* pLayerName,
753 uint32_t* pPropertyCount,
754 VkExtensionProperties* pProperties)
755 {
756 if (pProperties == NULL) {
757 *pPropertyCount = ARRAY_SIZE(device_extensions);
758 return VK_SUCCESS;
759 }
760
761 assert(*pPropertyCount >= ARRAY_SIZE(device_extensions));
762
763 *pPropertyCount = ARRAY_SIZE(device_extensions);
764 memcpy(pProperties, device_extensions, sizeof(device_extensions));
765
766 return VK_SUCCESS;
767 }
768
769 VkResult anv_EnumerateInstanceLayerProperties(
770 uint32_t* pPropertyCount,
771 VkLayerProperties* pProperties)
772 {
773 if (pProperties == NULL) {
774 *pPropertyCount = 0;
775 return VK_SUCCESS;
776 }
777
778 /* None supported at this time */
779 return vk_error(VK_ERROR_LAYER_NOT_PRESENT);
780 }
781
782 VkResult anv_EnumerateDeviceLayerProperties(
783 VkPhysicalDevice physicalDevice,
784 uint32_t* pPropertyCount,
785 VkLayerProperties* pProperties)
786 {
787 if (pProperties == NULL) {
788 *pPropertyCount = 0;
789 return VK_SUCCESS;
790 }
791
792 /* None supported at this time */
793 return vk_error(VK_ERROR_LAYER_NOT_PRESENT);
794 }
795
796 void anv_GetDeviceQueue(
797 VkDevice _device,
798 uint32_t queueNodeIndex,
799 uint32_t queueIndex,
800 VkQueue* pQueue)
801 {
802 ANV_FROM_HANDLE(anv_device, device, _device);
803
804 assert(queueIndex == 0);
805
806 *pQueue = anv_queue_to_handle(&device->queue);
807 }
808
809 VkResult anv_QueueSubmit(
810 VkQueue _queue,
811 uint32_t submitCount,
812 const VkSubmitInfo* pSubmits,
813 VkFence _fence)
814 {
815 ANV_FROM_HANDLE(anv_queue, queue, _queue);
816 ANV_FROM_HANDLE(anv_fence, fence, _fence);
817 struct anv_device *device = queue->device;
818 int ret;
819
820 for (uint32_t i = 0; i < submitCount; i++) {
821 for (uint32_t j = 0; j < pSubmits[i].commandBufferCount; j++) {
822 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer,
823 pSubmits[i].pCommandBuffers[j]);
824 assert(cmd_buffer->level == VK_COMMAND_BUFFER_LEVEL_PRIMARY);
825
826 ret = anv_gem_execbuffer(device, &cmd_buffer->execbuf2.execbuf);
827 if (ret != 0) {
828 /* We don't know the real error. */
829 return vk_errorf(VK_ERROR_OUT_OF_DEVICE_MEMORY,
830 "execbuf2 failed: %m");
831 }
832
833 if (fence) {
834 ret = anv_gem_execbuffer(device, &fence->execbuf);
835 if (ret != 0) {
836 /* We don't know the real error. */
837 return vk_errorf(VK_ERROR_OUT_OF_DEVICE_MEMORY,
838 "execbuf2 failed: %m");
839 }
840 }
841
842 for (uint32_t k = 0; k < cmd_buffer->execbuf2.bo_count; k++)
843 cmd_buffer->execbuf2.bos[k]->offset = cmd_buffer->execbuf2.objects[k].offset;
844 }
845 }
846
847 return VK_SUCCESS;
848 }
849
850 VkResult anv_QueueWaitIdle(
851 VkQueue _queue)
852 {
853 ANV_FROM_HANDLE(anv_queue, queue, _queue);
854
855 return ANV_CALL(DeviceWaitIdle)(anv_device_to_handle(queue->device));
856 }
857
858 VkResult anv_DeviceWaitIdle(
859 VkDevice _device)
860 {
861 ANV_FROM_HANDLE(anv_device, device, _device);
862 struct anv_state state;
863 struct anv_batch batch;
864 struct drm_i915_gem_execbuffer2 execbuf;
865 struct drm_i915_gem_exec_object2 exec2_objects[1];
866 struct anv_bo *bo = NULL;
867 VkResult result;
868 int64_t timeout;
869 int ret;
870
871 state = anv_state_pool_alloc(&device->dynamic_state_pool, 32, 32);
872 bo = &device->dynamic_state_pool.block_pool->bo;
873 batch.start = batch.next = state.map;
874 batch.end = state.map + 32;
875 anv_batch_emit(&batch, GEN7_MI_BATCH_BUFFER_END);
876 anv_batch_emit(&batch, GEN7_MI_NOOP);
877
878 exec2_objects[0].handle = bo->gem_handle;
879 exec2_objects[0].relocation_count = 0;
880 exec2_objects[0].relocs_ptr = 0;
881 exec2_objects[0].alignment = 0;
882 exec2_objects[0].offset = bo->offset;
883 exec2_objects[0].flags = 0;
884 exec2_objects[0].rsvd1 = 0;
885 exec2_objects[0].rsvd2 = 0;
886
887 execbuf.buffers_ptr = (uintptr_t) exec2_objects;
888 execbuf.buffer_count = 1;
889 execbuf.batch_start_offset = state.offset;
890 execbuf.batch_len = batch.next - state.map;
891 execbuf.cliprects_ptr = 0;
892 execbuf.num_cliprects = 0;
893 execbuf.DR1 = 0;
894 execbuf.DR4 = 0;
895
896 execbuf.flags =
897 I915_EXEC_HANDLE_LUT | I915_EXEC_NO_RELOC | I915_EXEC_RENDER;
898 execbuf.rsvd1 = device->context_id;
899 execbuf.rsvd2 = 0;
900
901 ret = anv_gem_execbuffer(device, &execbuf);
902 if (ret != 0) {
903 /* We don't know the real error. */
904 result = vk_errorf(VK_ERROR_OUT_OF_DEVICE_MEMORY, "execbuf2 failed: %m");
905 goto fail;
906 }
907
908 timeout = INT64_MAX;
909 ret = anv_gem_wait(device, bo->gem_handle, &timeout);
910 if (ret != 0) {
911 /* We don't know the real error. */
912 result = vk_errorf(VK_ERROR_OUT_OF_DEVICE_MEMORY, "execbuf2 failed: %m");
913 goto fail;
914 }
915
916 anv_state_pool_free(&device->dynamic_state_pool, state);
917
918 return VK_SUCCESS;
919
920 fail:
921 anv_state_pool_free(&device->dynamic_state_pool, state);
922
923 return result;
924 }
925
926 VkResult
927 anv_bo_init_new(struct anv_bo *bo, struct anv_device *device, uint64_t size)
928 {
929 bo->gem_handle = anv_gem_create(device, size);
930 if (!bo->gem_handle)
931 return vk_error(VK_ERROR_OUT_OF_DEVICE_MEMORY);
932
933 bo->map = NULL;
934 bo->index = 0;
935 bo->offset = 0;
936 bo->size = size;
937
938 return VK_SUCCESS;
939 }
940
941 VkResult anv_AllocateMemory(
942 VkDevice _device,
943 const VkMemoryAllocateInfo* pAllocateInfo,
944 const VkAllocationCallbacks* pAllocator,
945 VkDeviceMemory* pMem)
946 {
947 ANV_FROM_HANDLE(anv_device, device, _device);
948 struct anv_device_memory *mem;
949 VkResult result;
950
951 assert(pAllocateInfo->sType == VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO);
952
953 /* We support exactly one memory heap. */
954 assert(pAllocateInfo->memoryTypeIndex == 0);
955
956 /* FINISHME: Fail if allocation request exceeds heap size. */
957
958 mem = anv_alloc2(&device->alloc, pAllocator, sizeof(*mem), 8,
959 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
960 if (mem == NULL)
961 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
962
963 result = anv_bo_init_new(&mem->bo, device, pAllocateInfo->allocationSize);
964 if (result != VK_SUCCESS)
965 goto fail;
966
967 *pMem = anv_device_memory_to_handle(mem);
968
969 return VK_SUCCESS;
970
971 fail:
972 anv_free2(&device->alloc, pAllocator, mem);
973
974 return result;
975 }
976
977 void anv_FreeMemory(
978 VkDevice _device,
979 VkDeviceMemory _mem,
980 const VkAllocationCallbacks* pAllocator)
981 {
982 ANV_FROM_HANDLE(anv_device, device, _device);
983 ANV_FROM_HANDLE(anv_device_memory, mem, _mem);
984
985 if (mem->bo.map)
986 anv_gem_munmap(mem->bo.map, mem->bo.size);
987
988 if (mem->bo.gem_handle != 0)
989 anv_gem_close(device, mem->bo.gem_handle);
990
991 anv_free2(&device->alloc, pAllocator, mem);
992 }
993
994 VkResult anv_MapMemory(
995 VkDevice _device,
996 VkDeviceMemory _memory,
997 VkDeviceSize offset,
998 VkDeviceSize size,
999 VkMemoryMapFlags flags,
1000 void** ppData)
1001 {
1002 ANV_FROM_HANDLE(anv_device, device, _device);
1003 ANV_FROM_HANDLE(anv_device_memory, mem, _memory);
1004
1005 /* FIXME: Is this supposed to be thread safe? Since vkUnmapMemory() only
1006 * takes a VkDeviceMemory pointer, it seems like only one map of the memory
1007 * at a time is valid. We could just mmap up front and return an offset
1008 * pointer here, but that may exhaust virtual memory on 32 bit
1009 * userspace. */
1010
1011 mem->map = anv_gem_mmap(device, mem->bo.gem_handle, offset, size);
1012 mem->map_size = size;
1013
1014 *ppData = mem->map;
1015
1016 return VK_SUCCESS;
1017 }
1018
1019 void anv_UnmapMemory(
1020 VkDevice _device,
1021 VkDeviceMemory _memory)
1022 {
1023 ANV_FROM_HANDLE(anv_device_memory, mem, _memory);
1024
1025 anv_gem_munmap(mem->map, mem->map_size);
1026 }
1027
1028 VkResult anv_FlushMappedMemoryRanges(
1029 VkDevice device,
1030 uint32_t memoryRangeCount,
1031 const VkMappedMemoryRange* pMemoryRanges)
1032 {
1033 /* clflush here for !llc platforms */
1034
1035 return VK_SUCCESS;
1036 }
1037
1038 VkResult anv_InvalidateMappedMemoryRanges(
1039 VkDevice device,
1040 uint32_t memoryRangeCount,
1041 const VkMappedMemoryRange* pMemoryRanges)
1042 {
1043 return anv_FlushMappedMemoryRanges(device, memoryRangeCount, pMemoryRanges);
1044 }
1045
1046 void anv_GetBufferMemoryRequirements(
1047 VkDevice device,
1048 VkBuffer _buffer,
1049 VkMemoryRequirements* pMemoryRequirements)
1050 {
1051 ANV_FROM_HANDLE(anv_buffer, buffer, _buffer);
1052
1053 /* The Vulkan spec (git aaed022) says:
1054 *
1055 * memoryTypeBits is a bitfield and contains one bit set for every
1056 * supported memory type for the resource. The bit `1<<i` is set if and
1057 * only if the memory type `i` in the VkPhysicalDeviceMemoryProperties
1058 * structure for the physical device is supported.
1059 *
1060 * We support exactly one memory type.
1061 */
1062 pMemoryRequirements->memoryTypeBits = 1;
1063
1064 pMemoryRequirements->size = buffer->size;
1065 pMemoryRequirements->alignment = 16;
1066 }
1067
1068 void anv_GetImageMemoryRequirements(
1069 VkDevice device,
1070 VkImage _image,
1071 VkMemoryRequirements* pMemoryRequirements)
1072 {
1073 ANV_FROM_HANDLE(anv_image, image, _image);
1074
1075 /* The Vulkan spec (git aaed022) says:
1076 *
1077 * memoryTypeBits is a bitfield and contains one bit set for every
1078 * supported memory type for the resource. The bit `1<<i` is set if and
1079 * only if the memory type `i` in the VkPhysicalDeviceMemoryProperties
1080 * structure for the physical device is supported.
1081 *
1082 * We support exactly one memory type.
1083 */
1084 pMemoryRequirements->memoryTypeBits = 1;
1085
1086 pMemoryRequirements->size = image->size;
1087 pMemoryRequirements->alignment = image->alignment;
1088 }
1089
1090 void anv_GetImageSparseMemoryRequirements(
1091 VkDevice device,
1092 VkImage image,
1093 uint32_t* pSparseMemoryRequirementCount,
1094 VkSparseImageMemoryRequirements* pSparseMemoryRequirements)
1095 {
1096 stub();
1097 }
1098
1099 void anv_GetDeviceMemoryCommitment(
1100 VkDevice device,
1101 VkDeviceMemory memory,
1102 VkDeviceSize* pCommittedMemoryInBytes)
1103 {
1104 *pCommittedMemoryInBytes = 0;
1105 }
1106
1107 VkResult anv_BindBufferMemory(
1108 VkDevice device,
1109 VkBuffer _buffer,
1110 VkDeviceMemory _memory,
1111 VkDeviceSize memoryOffset)
1112 {
1113 ANV_FROM_HANDLE(anv_device_memory, mem, _memory);
1114 ANV_FROM_HANDLE(anv_buffer, buffer, _buffer);
1115
1116 buffer->bo = &mem->bo;
1117 buffer->offset = memoryOffset;
1118
1119 return VK_SUCCESS;
1120 }
1121
1122 VkResult anv_BindImageMemory(
1123 VkDevice device,
1124 VkImage _image,
1125 VkDeviceMemory _memory,
1126 VkDeviceSize memoryOffset)
1127 {
1128 ANV_FROM_HANDLE(anv_device_memory, mem, _memory);
1129 ANV_FROM_HANDLE(anv_image, image, _image);
1130
1131 image->bo = &mem->bo;
1132 image->offset = memoryOffset;
1133
1134 return VK_SUCCESS;
1135 }
1136
1137 VkResult anv_QueueBindSparse(
1138 VkQueue queue,
1139 uint32_t bindInfoCount,
1140 const VkBindSparseInfo* pBindInfo,
1141 VkFence fence)
1142 {
1143 stub_return(VK_ERROR_INCOMPATIBLE_DRIVER);
1144 }
1145
1146 VkResult anv_CreateFence(
1147 VkDevice _device,
1148 const VkFenceCreateInfo* pCreateInfo,
1149 const VkAllocationCallbacks* pAllocator,
1150 VkFence* pFence)
1151 {
1152 ANV_FROM_HANDLE(anv_device, device, _device);
1153 struct anv_fence *fence;
1154 struct anv_batch batch;
1155 VkResult result;
1156
1157 const uint32_t fence_size = 128;
1158
1159 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_FENCE_CREATE_INFO);
1160
1161 fence = anv_alloc2(&device->alloc, pAllocator, sizeof(*fence), 8,
1162 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
1163 if (fence == NULL)
1164 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
1165
1166 result = anv_bo_init_new(&fence->bo, device, fence_size);
1167 if (result != VK_SUCCESS)
1168 goto fail;
1169
1170 fence->bo.map =
1171 anv_gem_mmap(device, fence->bo.gem_handle, 0, fence->bo.size);
1172 batch.next = batch.start = fence->bo.map;
1173 batch.end = fence->bo.map + fence->bo.size;
1174 anv_batch_emit(&batch, GEN7_MI_BATCH_BUFFER_END);
1175 anv_batch_emit(&batch, GEN7_MI_NOOP);
1176
1177 fence->exec2_objects[0].handle = fence->bo.gem_handle;
1178 fence->exec2_objects[0].relocation_count = 0;
1179 fence->exec2_objects[0].relocs_ptr = 0;
1180 fence->exec2_objects[0].alignment = 0;
1181 fence->exec2_objects[0].offset = fence->bo.offset;
1182 fence->exec2_objects[0].flags = 0;
1183 fence->exec2_objects[0].rsvd1 = 0;
1184 fence->exec2_objects[0].rsvd2 = 0;
1185
1186 fence->execbuf.buffers_ptr = (uintptr_t) fence->exec2_objects;
1187 fence->execbuf.buffer_count = 1;
1188 fence->execbuf.batch_start_offset = 0;
1189 fence->execbuf.batch_len = batch.next - fence->bo.map;
1190 fence->execbuf.cliprects_ptr = 0;
1191 fence->execbuf.num_cliprects = 0;
1192 fence->execbuf.DR1 = 0;
1193 fence->execbuf.DR4 = 0;
1194
1195 fence->execbuf.flags =
1196 I915_EXEC_HANDLE_LUT | I915_EXEC_NO_RELOC | I915_EXEC_RENDER;
1197 fence->execbuf.rsvd1 = device->context_id;
1198 fence->execbuf.rsvd2 = 0;
1199
1200 *pFence = anv_fence_to_handle(fence);
1201
1202 return VK_SUCCESS;
1203
1204 fail:
1205 anv_free2(&device->alloc, pAllocator, fence);
1206
1207 return result;
1208 }
1209
1210 void anv_DestroyFence(
1211 VkDevice _device,
1212 VkFence _fence,
1213 const VkAllocationCallbacks* pAllocator)
1214 {
1215 ANV_FROM_HANDLE(anv_device, device, _device);
1216 ANV_FROM_HANDLE(anv_fence, fence, _fence);
1217
1218 anv_gem_munmap(fence->bo.map, fence->bo.size);
1219 anv_gem_close(device, fence->bo.gem_handle);
1220 anv_free2(&device->alloc, pAllocator, fence);
1221 }
1222
1223 VkResult anv_ResetFences(
1224 VkDevice _device,
1225 uint32_t fenceCount,
1226 const VkFence* pFences)
1227 {
1228 for (uint32_t i = 0; i < fenceCount; i++) {
1229 ANV_FROM_HANDLE(anv_fence, fence, pFences[i]);
1230 fence->ready = false;
1231 }
1232
1233 return VK_SUCCESS;
1234 }
1235
1236 VkResult anv_GetFenceStatus(
1237 VkDevice _device,
1238 VkFence _fence)
1239 {
1240 ANV_FROM_HANDLE(anv_device, device, _device);
1241 ANV_FROM_HANDLE(anv_fence, fence, _fence);
1242 int64_t t = 0;
1243 int ret;
1244
1245 if (fence->ready)
1246 return VK_SUCCESS;
1247
1248 ret = anv_gem_wait(device, fence->bo.gem_handle, &t);
1249 if (ret == 0) {
1250 fence->ready = true;
1251 return VK_SUCCESS;
1252 }
1253
1254 return VK_NOT_READY;
1255 }
1256
1257 VkResult anv_WaitForFences(
1258 VkDevice _device,
1259 uint32_t fenceCount,
1260 const VkFence* pFences,
1261 VkBool32 waitAll,
1262 uint64_t timeout)
1263 {
1264 ANV_FROM_HANDLE(anv_device, device, _device);
1265
1266 /* DRM_IOCTL_I915_GEM_WAIT uses a signed 64 bit timeout and is supposed
1267 * to block indefinitely timeouts <= 0. Unfortunately, this was broken
1268 * for a couple of kernel releases. Since there's no way to know
1269 * whether or not the kernel we're using is one of the broken ones, the
1270 * best we can do is to clamp the timeout to INT64_MAX. This limits the
1271 * maximum timeout from 584 years to 292 years - likely not a big deal.
1272 */
1273 if (timeout > INT64_MAX)
1274 timeout = INT64_MAX;
1275
1276 int64_t t = timeout;
1277
1278 /* FIXME: handle !waitAll */
1279
1280 for (uint32_t i = 0; i < fenceCount; i++) {
1281 ANV_FROM_HANDLE(anv_fence, fence, pFences[i]);
1282 int ret = anv_gem_wait(device, fence->bo.gem_handle, &t);
1283 if (ret == -1 && errno == ETIME) {
1284 return VK_TIMEOUT;
1285 } else if (ret == -1) {
1286 /* We don't know the real error. */
1287 return vk_errorf(VK_ERROR_OUT_OF_DEVICE_MEMORY,
1288 "gem wait failed: %m");
1289 }
1290 }
1291
1292 return VK_SUCCESS;
1293 }
1294
1295 // Queue semaphore functions
1296
1297 VkResult anv_CreateSemaphore(
1298 VkDevice device,
1299 const VkSemaphoreCreateInfo* pCreateInfo,
1300 const VkAllocationCallbacks* pAllocator,
1301 VkSemaphore* pSemaphore)
1302 {
1303 *pSemaphore = (VkSemaphore)1;
1304 stub_return(VK_SUCCESS);
1305 }
1306
1307 void anv_DestroySemaphore(
1308 VkDevice device,
1309 VkSemaphore semaphore,
1310 const VkAllocationCallbacks* pAllocator)
1311 {
1312 stub();
1313 }
1314
1315 // Event functions
1316
1317 VkResult anv_CreateEvent(
1318 VkDevice device,
1319 const VkEventCreateInfo* pCreateInfo,
1320 const VkAllocationCallbacks* pAllocator,
1321 VkEvent* pEvent)
1322 {
1323 stub_return(VK_ERROR_INCOMPATIBLE_DRIVER);
1324 }
1325
1326 void anv_DestroyEvent(
1327 VkDevice device,
1328 VkEvent event,
1329 const VkAllocationCallbacks* pAllocator)
1330 {
1331 stub();
1332 }
1333
1334 VkResult anv_GetEventStatus(
1335 VkDevice device,
1336 VkEvent event)
1337 {
1338 stub_return(VK_ERROR_INCOMPATIBLE_DRIVER);
1339 }
1340
1341 VkResult anv_SetEvent(
1342 VkDevice device,
1343 VkEvent event)
1344 {
1345 stub_return(VK_ERROR_INCOMPATIBLE_DRIVER);
1346 }
1347
1348 VkResult anv_ResetEvent(
1349 VkDevice device,
1350 VkEvent event)
1351 {
1352 stub_return(VK_ERROR_INCOMPATIBLE_DRIVER);
1353 }
1354
1355 // Buffer functions
1356
1357 VkResult anv_CreateBuffer(
1358 VkDevice _device,
1359 const VkBufferCreateInfo* pCreateInfo,
1360 const VkAllocationCallbacks* pAllocator,
1361 VkBuffer* pBuffer)
1362 {
1363 ANV_FROM_HANDLE(anv_device, device, _device);
1364 struct anv_buffer *buffer;
1365
1366 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO);
1367
1368 buffer = anv_alloc2(&device->alloc, pAllocator, sizeof(*buffer), 8,
1369 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
1370 if (buffer == NULL)
1371 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
1372
1373 buffer->size = pCreateInfo->size;
1374 buffer->bo = NULL;
1375 buffer->offset = 0;
1376
1377 *pBuffer = anv_buffer_to_handle(buffer);
1378
1379 return VK_SUCCESS;
1380 }
1381
1382 void anv_DestroyBuffer(
1383 VkDevice _device,
1384 VkBuffer _buffer,
1385 const VkAllocationCallbacks* pAllocator)
1386 {
1387 ANV_FROM_HANDLE(anv_device, device, _device);
1388 ANV_FROM_HANDLE(anv_buffer, buffer, _buffer);
1389
1390 anv_free2(&device->alloc, pAllocator, buffer);
1391 }
1392
1393 void
1394 anv_fill_buffer_surface_state(struct anv_device *device, void *state,
1395 const struct anv_format *format,
1396 uint32_t offset, uint32_t range, uint32_t stride)
1397 {
1398 switch (device->info.gen) {
1399 case 7:
1400 if (device->info.is_haswell)
1401 gen75_fill_buffer_surface_state(state, format, offset, range, stride);
1402 else
1403 gen7_fill_buffer_surface_state(state, format, offset, range, stride);
1404 break;
1405 case 8:
1406 gen8_fill_buffer_surface_state(state, format, offset, range, stride);
1407 break;
1408 case 9:
1409 gen9_fill_buffer_surface_state(state, format, offset, range, stride);
1410 break;
1411 default:
1412 unreachable("unsupported gen\n");
1413 }
1414 }
1415
1416 VkResult anv_CreateBufferView(
1417 VkDevice _device,
1418 const VkBufferViewCreateInfo* pCreateInfo,
1419 const VkAllocationCallbacks* pAllocator,
1420 VkBufferView* pView)
1421 {
1422 stub_return(VK_ERROR_INCOMPATIBLE_DRIVER);
1423 }
1424
1425 void anv_DestroyBufferView(
1426 VkDevice _device,
1427 VkBufferView _bview,
1428 const VkAllocationCallbacks* pAllocator)
1429 {
1430 stub();
1431 }
1432
1433 void anv_DestroySampler(
1434 VkDevice _device,
1435 VkSampler _sampler,
1436 const VkAllocationCallbacks* pAllocator)
1437 {
1438 ANV_FROM_HANDLE(anv_device, device, _device);
1439 ANV_FROM_HANDLE(anv_sampler, sampler, _sampler);
1440
1441 anv_free2(&device->alloc, pAllocator, sampler);
1442 }
1443
1444 VkResult anv_CreateFramebuffer(
1445 VkDevice _device,
1446 const VkFramebufferCreateInfo* pCreateInfo,
1447 const VkAllocationCallbacks* pAllocator,
1448 VkFramebuffer* pFramebuffer)
1449 {
1450 ANV_FROM_HANDLE(anv_device, device, _device);
1451 struct anv_framebuffer *framebuffer;
1452
1453 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO);
1454
1455 size_t size = sizeof(*framebuffer) +
1456 sizeof(struct anv_image_view *) * pCreateInfo->attachmentCount;
1457 framebuffer = anv_alloc2(&device->alloc, pAllocator, size, 8,
1458 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
1459 if (framebuffer == NULL)
1460 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
1461
1462 framebuffer->attachment_count = pCreateInfo->attachmentCount;
1463 for (uint32_t i = 0; i < pCreateInfo->attachmentCount; i++) {
1464 VkImageView _iview = pCreateInfo->pAttachments[i];
1465 framebuffer->attachments[i] = anv_image_view_from_handle(_iview);
1466 }
1467
1468 framebuffer->width = pCreateInfo->width;
1469 framebuffer->height = pCreateInfo->height;
1470 framebuffer->layers = pCreateInfo->layers;
1471
1472 *pFramebuffer = anv_framebuffer_to_handle(framebuffer);
1473
1474 return VK_SUCCESS;
1475 }
1476
1477 void anv_DestroyFramebuffer(
1478 VkDevice _device,
1479 VkFramebuffer _fb,
1480 const VkAllocationCallbacks* pAllocator)
1481 {
1482 ANV_FROM_HANDLE(anv_device, device, _device);
1483 ANV_FROM_HANDLE(anv_framebuffer, fb, _fb);
1484
1485 anv_free2(&device->alloc, pAllocator, fb);
1486 }
1487
1488 void vkCmdDbgMarkerBegin(
1489 VkCommandBuffer commandBuffer,
1490 const char* pMarker)
1491 __attribute__ ((visibility ("default")));
1492
1493 void vkCmdDbgMarkerEnd(
1494 VkCommandBuffer commandBuffer)
1495 __attribute__ ((visibility ("default")));
1496
1497 void vkCmdDbgMarkerBegin(
1498 VkCommandBuffer commandBuffer,
1499 const char* pMarker)
1500 {
1501 }
1502
1503 void vkCmdDbgMarkerEnd(
1504 VkCommandBuffer commandBuffer)
1505 {
1506 }