vk/0.210.0: Rework allocation to use the new pAllocator's
[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_UNSUPPORTED,
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_UNSUPPORTED) {
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 .dualSourceBlend = true,
328 .logicOp = true,
329 .multiDrawIndirect = true,
330 .depthClip = false,
331 .depthBiasClamp = false,
332 .fillModeNonSolid = true,
333 .depthBounds = false,
334 .wideLines = true,
335 .largePoints = true,
336 .textureCompressionETC2 = true,
337 .textureCompressionASTC_LDR = true,
338 .textureCompressionBC = true,
339 .occlusionQueryNonConservative = false, /* FINISHME */
340 .pipelineStatisticsQuery = true,
341 .vertexSideEffects = false,
342 .tessellationSideEffects = false,
343 .geometrySideEffects = false,
344 .fragmentSideEffects = false,
345 .shaderTessellationPointSize = false,
346 .shaderGeometryPointSize = true,
347 .shaderImageGatherExtended = true,
348 .shaderStorageImageExtendedFormats = false,
349 .shaderStorageImageMultisample = false,
350 .shaderUniformBufferArrayDynamicIndexing = true,
351 .shaderSampledImageArrayDynamicIndexing = false,
352 .shaderStorageBufferArrayDynamicIndexing = false,
353 .shaderStorageImageArrayDynamicIndexing = false,
354 .shaderClipDistance = false,
355 .shaderCullDistance = false,
356 .shaderFloat64 = false,
357 .shaderInt64 = false,
358 .shaderInt16 = false,
359 .alphaToOne = true,
360 };
361 }
362
363 void anv_GetPhysicalDeviceProperties(
364 VkPhysicalDevice physicalDevice,
365 VkPhysicalDeviceProperties* pProperties)
366 {
367 ANV_FROM_HANDLE(anv_physical_device, pdevice, physicalDevice);
368 const struct brw_device_info *devinfo = pdevice->info;
369
370 anv_finishme("Get correct values for VkPhysicalDeviceLimits");
371
372 VkPhysicalDeviceLimits limits = {
373 .maxImageDimension1D = (1 << 14),
374 .maxImageDimension2D = (1 << 14),
375 .maxImageDimension3D = (1 << 10),
376 .maxImageDimensionCube = (1 << 14),
377 .maxImageArrayLayers = (1 << 10),
378
379 /* Broadwell supports 1, 2, 4, and 8 samples. */
380 .sampleCounts = 4,
381
382 .maxTexelBufferSize = (1 << 14),
383 .maxUniformBufferSize = UINT32_MAX,
384 .maxStorageBufferSize = UINT32_MAX,
385 .maxPushConstantsSize = MAX_PUSH_CONSTANTS_SIZE,
386 .maxMemoryAllocationCount = UINT32_MAX,
387 .bufferImageGranularity = 64, /* A cache line */
388 .sparseAddressSpaceSize = 0,
389 .maxBoundDescriptorSets = MAX_SETS,
390 .maxDescriptorSets = UINT32_MAX,
391 .maxPerStageDescriptorSamplers = 64,
392 .maxPerStageDescriptorUniformBuffers = 64,
393 .maxPerStageDescriptorStorageBuffers = 64,
394 .maxPerStageDescriptorSampledImages = 64,
395 .maxPerStageDescriptorStorageImages = 64,
396 .maxDescriptorSetSamplers = 256,
397 .maxDescriptorSetUniformBuffers = 256,
398 .maxDescriptorSetUniformBuffersDynamic = 256,
399 .maxDescriptorSetStorageBuffers = 256,
400 .maxDescriptorSetStorageBuffersDynamic = 256,
401 .maxDescriptorSetSampledImages = 256,
402 .maxDescriptorSetStorageImages = 256,
403 .maxVertexInputAttributes = 32,
404 .maxVertexInputBindings = 32,
405 .maxVertexInputAttributeOffset = 256,
406 .maxVertexInputBindingStride = 256,
407 .maxVertexOutputComponents = 32,
408 .maxTessGenLevel = 0,
409 .maxTessPatchSize = 0,
410 .maxTessControlPerVertexInputComponents = 0,
411 .maxTessControlPerVertexOutputComponents = 0,
412 .maxTessControlPerPatchOutputComponents = 0,
413 .maxTessControlTotalOutputComponents = 0,
414 .maxTessEvaluationInputComponents = 0,
415 .maxTessEvaluationOutputComponents = 0,
416 .maxGeometryShaderInvocations = 6,
417 .maxGeometryInputComponents = 16,
418 .maxGeometryOutputComponents = 16,
419 .maxGeometryOutputVertices = 16,
420 .maxGeometryTotalOutputComponents = 16,
421 .maxFragmentInputComponents = 16,
422 .maxFragmentOutputBuffers = 8,
423 .maxFragmentDualSourceBuffers = 2,
424 .maxFragmentCombinedOutputResources = 8,
425 .maxComputeSharedMemorySize = 1024,
426 .maxComputeWorkGroupCount = {
427 16 * devinfo->max_cs_threads,
428 16 * devinfo->max_cs_threads,
429 16 * devinfo->max_cs_threads,
430 },
431 .maxComputeWorkGroupInvocations = 16 * devinfo->max_cs_threads,
432 .maxComputeWorkGroupSize = {
433 16 * devinfo->max_cs_threads,
434 16 * devinfo->max_cs_threads,
435 16 * devinfo->max_cs_threads,
436 },
437 .subPixelPrecisionBits = 4 /* FIXME */,
438 .subTexelPrecisionBits = 4 /* FIXME */,
439 .mipmapPrecisionBits = 4 /* FIXME */,
440 .maxDrawIndexedIndexValue = UINT32_MAX,
441 .maxDrawIndirectInstanceCount = UINT32_MAX,
442 .primitiveRestartForPatches = UINT32_MAX,
443 .maxSamplerLodBias = 16,
444 .maxSamplerAnisotropy = 16,
445 .maxViewports = MAX_VIEWPORTS,
446 .maxViewportDimensions = { (1 << 14), (1 << 14) },
447 .viewportBoundsRange = { -1.0, 1.0 }, /* FIXME */
448 .viewportSubPixelBits = 13, /* We take a float? */
449 .minMemoryMapAlignment = 64, /* A cache line */
450 .minTexelBufferOffsetAlignment = 1,
451 .minUniformBufferOffsetAlignment = 1,
452 .minStorageBufferOffsetAlignment = 1,
453 .minTexelOffset = 0, /* FIXME */
454 .maxTexelOffset = 0, /* FIXME */
455 .minTexelGatherOffset = 0, /* FIXME */
456 .maxTexelGatherOffset = 0, /* FIXME */
457 .minInterpolationOffset = 0, /* FIXME */
458 .maxInterpolationOffset = 0, /* FIXME */
459 .subPixelInterpolationOffsetBits = 0, /* FIXME */
460 .maxFramebufferWidth = (1 << 14),
461 .maxFramebufferHeight = (1 << 14),
462 .maxFramebufferLayers = (1 << 10),
463 .maxFramebufferColorSamples = 8,
464 .maxFramebufferDepthSamples = 8,
465 .maxFramebufferStencilSamples = 8,
466 .maxColorAttachments = MAX_RTS,
467 .maxSampledImageColorSamples = 8,
468 .maxSampledImageDepthSamples = 8,
469 .maxSampledImageIntegerSamples = 1,
470 .maxStorageImageSamples = 1,
471 .maxSampleMaskWords = 1,
472 .timestampFrequency = 1000 * 1000 * 1000 / 80,
473 .maxClipDistances = 0 /* FIXME */,
474 .maxCullDistances = 0 /* FIXME */,
475 .maxCombinedClipAndCullDistances = 0 /* FIXME */,
476 .pointSizeRange = { 0.125, 255.875 },
477 .lineWidthRange = { 0.0, 7.9921875 },
478 .pointSizeGranularity = (1.0 / 8.0),
479 .lineWidthGranularity = (1.0 / 128.0),
480 };
481
482 *pProperties = (VkPhysicalDeviceProperties) {
483 .apiVersion = VK_MAKE_VERSION(0, 170, 2),
484 .driverVersion = 1,
485 .vendorID = 0x8086,
486 .deviceID = pdevice->chipset_id,
487 .deviceType = VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU,
488 .limits = limits,
489 .sparseProperties = {0}, /* Broadwell doesn't do sparse. */
490 };
491
492 strcpy(pProperties->deviceName, pdevice->name);
493 snprintf((char *)pProperties->pipelineCacheUUID, VK_UUID_SIZE,
494 "anv-%s", MESA_GIT_SHA1 + 4);
495 }
496
497 void anv_GetPhysicalDeviceQueueFamilyProperties(
498 VkPhysicalDevice physicalDevice,
499 uint32_t* pCount,
500 VkQueueFamilyProperties* pQueueFamilyProperties)
501 {
502 if (pQueueFamilyProperties == NULL) {
503 *pCount = 1;
504 return;
505 }
506
507 assert(*pCount >= 1);
508
509 *pQueueFamilyProperties = (VkQueueFamilyProperties) {
510 .queueFlags = VK_QUEUE_GRAPHICS_BIT |
511 VK_QUEUE_COMPUTE_BIT |
512 VK_QUEUE_TRANSFER_BIT,
513 .queueCount = 1,
514 .supportsTimestamps = true,
515 };
516 }
517
518 void anv_GetPhysicalDeviceMemoryProperties(
519 VkPhysicalDevice physicalDevice,
520 VkPhysicalDeviceMemoryProperties* pMemoryProperties)
521 {
522 ANV_FROM_HANDLE(anv_physical_device, physical_device, physicalDevice);
523 VkDeviceSize heap_size;
524
525 /* Reserve some wiggle room for the driver by exposing only 75% of the
526 * aperture to the heap.
527 */
528 heap_size = 3 * physical_device->aperture_size / 4;
529
530 /* The property flags below are valid only for llc platforms. */
531 pMemoryProperties->memoryTypeCount = 1;
532 pMemoryProperties->memoryTypes[0] = (VkMemoryType) {
533 .propertyFlags = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT,
534 .heapIndex = 1,
535 };
536
537 pMemoryProperties->memoryHeapCount = 1;
538 pMemoryProperties->memoryHeaps[0] = (VkMemoryHeap) {
539 .size = heap_size,
540 .flags = VK_MEMORY_HEAP_HOST_LOCAL_BIT,
541 };
542 }
543
544 PFN_vkVoidFunction anv_GetInstanceProcAddr(
545 VkInstance instance,
546 const char* pName)
547 {
548 return anv_lookup_entrypoint(pName);
549 }
550
551 PFN_vkVoidFunction anv_GetDeviceProcAddr(
552 VkDevice device,
553 const char* pName)
554 {
555 return anv_lookup_entrypoint(pName);
556 }
557
558 static VkResult
559 anv_queue_init(struct anv_device *device, struct anv_queue *queue)
560 {
561 queue->_loader_data.loaderMagic = ICD_LOADER_MAGIC;
562 queue->device = device;
563 queue->pool = &device->surface_state_pool;
564
565 return VK_SUCCESS;
566 }
567
568 static void
569 anv_queue_finish(struct anv_queue *queue)
570 {
571 }
572
573 static void
574 anv_device_init_border_colors(struct anv_device *device)
575 {
576 static const VkClearColorValue border_colors[] = {
577 [VK_BORDER_COLOR_FLOAT_TRANSPARENT_BLACK] = { .float32 = { 0.0, 0.0, 0.0, 0.0 } },
578 [VK_BORDER_COLOR_FLOAT_OPAQUE_BLACK] = { .float32 = { 0.0, 0.0, 0.0, 1.0 } },
579 [VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE] = { .float32 = { 1.0, 1.0, 1.0, 1.0 } },
580 [VK_BORDER_COLOR_INT_TRANSPARENT_BLACK] = { .uint32 = { 0, 0, 0, 0 } },
581 [VK_BORDER_COLOR_INT_OPAQUE_BLACK] = { .uint32 = { 0, 0, 0, 1 } },
582 [VK_BORDER_COLOR_INT_OPAQUE_WHITE] = { .uint32 = { 1, 1, 1, 1 } },
583 };
584
585 device->border_colors =
586 anv_state_pool_alloc(&device->dynamic_state_pool,
587 sizeof(border_colors), 32);
588 memcpy(device->border_colors.map, border_colors, sizeof(border_colors));
589 }
590
591 VkResult anv_CreateDevice(
592 VkPhysicalDevice physicalDevice,
593 const VkDeviceCreateInfo* pCreateInfo,
594 const VkAllocationCallbacks* pAllocator,
595 VkDevice* pDevice)
596 {
597 ANV_FROM_HANDLE(anv_physical_device, physical_device, physicalDevice);
598 struct anv_device *device;
599
600 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO);
601
602 for (uint32_t i = 0; i < pCreateInfo->enabledExtensionNameCount; i++) {
603 bool found = false;
604 for (uint32_t j = 0; j < ARRAY_SIZE(device_extensions); j++) {
605 if (strcmp(pCreateInfo->ppEnabledExtensionNames[i],
606 device_extensions[j].extensionName) == 0) {
607 found = true;
608 break;
609 }
610 }
611 if (!found)
612 return vk_error(VK_ERROR_EXTENSION_NOT_PRESENT);
613 }
614
615 anv_set_dispatch_devinfo(physical_device->info);
616
617 device = anv_alloc2(&physical_device->instance->alloc, pAllocator,
618 sizeof(*device), 8,
619 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
620 if (!device)
621 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
622
623 device->_loader_data.loaderMagic = ICD_LOADER_MAGIC;
624 device->instance = physical_device->instance;
625
626 if (pAllocator)
627 device->alloc = *pAllocator;
628 else
629 device->alloc = physical_device->instance->alloc;
630
631 /* XXX(chadv): Can we dup() physicalDevice->fd here? */
632 device->fd = open(physical_device->path, O_RDWR | O_CLOEXEC);
633 if (device->fd == -1)
634 goto fail_device;
635
636 device->context_id = anv_gem_create_context(device);
637 if (device->context_id == -1)
638 goto fail_fd;
639
640 pthread_mutex_init(&device->mutex, NULL);
641
642 anv_bo_pool_init(&device->batch_bo_pool, device, ANV_CMD_BUFFER_BATCH_SIZE);
643
644 anv_block_pool_init(&device->dynamic_state_block_pool, device, 2048);
645
646 anv_state_pool_init(&device->dynamic_state_pool,
647 &device->dynamic_state_block_pool);
648
649 anv_block_pool_init(&device->instruction_block_pool, device, 4096);
650 anv_block_pool_init(&device->surface_state_block_pool, device, 4096);
651
652 anv_state_pool_init(&device->surface_state_pool,
653 &device->surface_state_block_pool);
654
655 anv_bo_init_new(&device->workaround_bo, device, 1024);
656
657 anv_block_pool_init(&device->scratch_block_pool, device, 0x10000);
658
659 device->info = *physical_device->info;
660 device->isl_dev = physical_device->isl_dev;
661
662 anv_queue_init(device, &device->queue);
663
664 anv_device_init_meta(device);
665
666 anv_device_init_border_colors(device);
667
668 *pDevice = anv_device_to_handle(device);
669
670 return VK_SUCCESS;
671
672 fail_fd:
673 close(device->fd);
674 fail_device:
675 anv_free(&device->alloc, device);
676
677 return vk_error(VK_ERROR_INITIALIZATION_FAILED);
678 }
679
680 void anv_DestroyDevice(
681 VkDevice _device,
682 const VkAllocationCallbacks* pAllocator)
683 {
684 ANV_FROM_HANDLE(anv_device, device, _device);
685
686 anv_queue_finish(&device->queue);
687
688 anv_device_finish_meta(device);
689
690 #ifdef HAVE_VALGRIND
691 /* We only need to free these to prevent valgrind errors. The backing
692 * BO will go away in a couple of lines so we don't actually leak.
693 */
694 anv_state_pool_free(&device->dynamic_state_pool, device->border_colors);
695 #endif
696
697 anv_gem_munmap(device->workaround_bo.map, device->workaround_bo.size);
698 anv_gem_close(device, device->workaround_bo.gem_handle);
699
700 anv_bo_pool_finish(&device->batch_bo_pool);
701 anv_state_pool_finish(&device->dynamic_state_pool);
702 anv_block_pool_finish(&device->dynamic_state_block_pool);
703 anv_block_pool_finish(&device->instruction_block_pool);
704 anv_state_pool_finish(&device->surface_state_pool);
705 anv_block_pool_finish(&device->surface_state_block_pool);
706 anv_block_pool_finish(&device->scratch_block_pool);
707
708 close(device->fd);
709
710 anv_free(&device->alloc, device);
711 }
712
713 VkResult anv_EnumerateInstanceExtensionProperties(
714 const char* pLayerName,
715 uint32_t* pPropertyCount,
716 VkExtensionProperties* pProperties)
717 {
718 if (pProperties == NULL) {
719 *pPropertyCount = ARRAY_SIZE(global_extensions);
720 return VK_SUCCESS;
721 }
722
723 assert(*pPropertyCount >= ARRAY_SIZE(global_extensions));
724
725 *pPropertyCount = ARRAY_SIZE(global_extensions);
726 memcpy(pProperties, global_extensions, sizeof(global_extensions));
727
728 return VK_SUCCESS;
729 }
730
731 VkResult anv_EnumerateDeviceExtensionProperties(
732 VkPhysicalDevice physicalDevice,
733 const char* pLayerName,
734 uint32_t* pPropertyCount,
735 VkExtensionProperties* pProperties)
736 {
737 if (pProperties == NULL) {
738 *pPropertyCount = ARRAY_SIZE(device_extensions);
739 return VK_SUCCESS;
740 }
741
742 assert(*pPropertyCount >= ARRAY_SIZE(device_extensions));
743
744 *pPropertyCount = ARRAY_SIZE(device_extensions);
745 memcpy(pProperties, device_extensions, sizeof(device_extensions));
746
747 return VK_SUCCESS;
748 }
749
750 VkResult anv_EnumerateInstanceLayerProperties(
751 uint32_t* pPropertyCount,
752 VkLayerProperties* pProperties)
753 {
754 if (pProperties == NULL) {
755 *pPropertyCount = 0;
756 return VK_SUCCESS;
757 }
758
759 /* None supported at this time */
760 return vk_error(VK_ERROR_LAYER_NOT_PRESENT);
761 }
762
763 VkResult anv_EnumerateDeviceLayerProperties(
764 VkPhysicalDevice physicalDevice,
765 uint32_t* pPropertyCount,
766 VkLayerProperties* pProperties)
767 {
768 if (pProperties == NULL) {
769 *pPropertyCount = 0;
770 return VK_SUCCESS;
771 }
772
773 /* None supported at this time */
774 return vk_error(VK_ERROR_LAYER_NOT_PRESENT);
775 }
776
777 void anv_GetDeviceQueue(
778 VkDevice _device,
779 uint32_t queueNodeIndex,
780 uint32_t queueIndex,
781 VkQueue* pQueue)
782 {
783 ANV_FROM_HANDLE(anv_device, device, _device);
784
785 assert(queueIndex == 0);
786
787 *pQueue = anv_queue_to_handle(&device->queue);
788 }
789
790 VkResult anv_QueueSubmit(
791 VkQueue _queue,
792 uint32_t commandBufferCount,
793 const VkCommandBuffer* pCommandBuffers,
794 VkFence _fence)
795 {
796 ANV_FROM_HANDLE(anv_queue, queue, _queue);
797 ANV_FROM_HANDLE(anv_fence, fence, _fence);
798 struct anv_device *device = queue->device;
799 int ret;
800
801 for (uint32_t i = 0; i < commandBufferCount; i++) {
802 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, pCommandBuffers[i]);
803
804 assert(cmd_buffer->level == VK_COMMAND_BUFFER_LEVEL_PRIMARY);
805
806 ret = anv_gem_execbuffer(device, &cmd_buffer->execbuf2.execbuf);
807 if (ret != 0) {
808 /* We don't know the real error. */
809 return vk_errorf(VK_ERROR_OUT_OF_DEVICE_MEMORY,
810 "execbuf2 failed: %m");
811 }
812
813 if (fence) {
814 ret = anv_gem_execbuffer(device, &fence->execbuf);
815 if (ret != 0) {
816 /* We don't know the real error. */
817 return vk_errorf(VK_ERROR_OUT_OF_DEVICE_MEMORY,
818 "execbuf2 failed: %m");
819 }
820 }
821
822 for (uint32_t i = 0; i < cmd_buffer->execbuf2.bo_count; i++)
823 cmd_buffer->execbuf2.bos[i]->offset = cmd_buffer->execbuf2.objects[i].offset;
824 }
825
826 return VK_SUCCESS;
827 }
828
829 VkResult anv_QueueWaitIdle(
830 VkQueue _queue)
831 {
832 ANV_FROM_HANDLE(anv_queue, queue, _queue);
833
834 return ANV_CALL(DeviceWaitIdle)(anv_device_to_handle(queue->device));
835 }
836
837 VkResult anv_DeviceWaitIdle(
838 VkDevice _device)
839 {
840 ANV_FROM_HANDLE(anv_device, device, _device);
841 struct anv_state state;
842 struct anv_batch batch;
843 struct drm_i915_gem_execbuffer2 execbuf;
844 struct drm_i915_gem_exec_object2 exec2_objects[1];
845 struct anv_bo *bo = NULL;
846 VkResult result;
847 int64_t timeout;
848 int ret;
849
850 state = anv_state_pool_alloc(&device->dynamic_state_pool, 32, 32);
851 bo = &device->dynamic_state_pool.block_pool->bo;
852 batch.start = batch.next = state.map;
853 batch.end = state.map + 32;
854 anv_batch_emit(&batch, GEN7_MI_BATCH_BUFFER_END);
855 anv_batch_emit(&batch, GEN7_MI_NOOP);
856
857 exec2_objects[0].handle = bo->gem_handle;
858 exec2_objects[0].relocation_count = 0;
859 exec2_objects[0].relocs_ptr = 0;
860 exec2_objects[0].alignment = 0;
861 exec2_objects[0].offset = bo->offset;
862 exec2_objects[0].flags = 0;
863 exec2_objects[0].rsvd1 = 0;
864 exec2_objects[0].rsvd2 = 0;
865
866 execbuf.buffers_ptr = (uintptr_t) exec2_objects;
867 execbuf.buffer_count = 1;
868 execbuf.batch_start_offset = state.offset;
869 execbuf.batch_len = batch.next - state.map;
870 execbuf.cliprects_ptr = 0;
871 execbuf.num_cliprects = 0;
872 execbuf.DR1 = 0;
873 execbuf.DR4 = 0;
874
875 execbuf.flags =
876 I915_EXEC_HANDLE_LUT | I915_EXEC_NO_RELOC | I915_EXEC_RENDER;
877 execbuf.rsvd1 = device->context_id;
878 execbuf.rsvd2 = 0;
879
880 ret = anv_gem_execbuffer(device, &execbuf);
881 if (ret != 0) {
882 /* We don't know the real error. */
883 result = vk_errorf(VK_ERROR_OUT_OF_DEVICE_MEMORY, "execbuf2 failed: %m");
884 goto fail;
885 }
886
887 timeout = INT64_MAX;
888 ret = anv_gem_wait(device, bo->gem_handle, &timeout);
889 if (ret != 0) {
890 /* We don't know the real error. */
891 result = vk_errorf(VK_ERROR_OUT_OF_DEVICE_MEMORY, "execbuf2 failed: %m");
892 goto fail;
893 }
894
895 anv_state_pool_free(&device->dynamic_state_pool, state);
896
897 return VK_SUCCESS;
898
899 fail:
900 anv_state_pool_free(&device->dynamic_state_pool, state);
901
902 return result;
903 }
904
905 VkResult
906 anv_bo_init_new(struct anv_bo *bo, struct anv_device *device, uint64_t size)
907 {
908 bo->gem_handle = anv_gem_create(device, size);
909 if (!bo->gem_handle)
910 return vk_error(VK_ERROR_OUT_OF_DEVICE_MEMORY);
911
912 bo->map = NULL;
913 bo->index = 0;
914 bo->offset = 0;
915 bo->size = size;
916
917 return VK_SUCCESS;
918 }
919
920 VkResult anv_AllocateMemory(
921 VkDevice _device,
922 const VkMemoryAllocateInfo* pAllocateInfo,
923 const VkAllocationCallbacks* pAllocator,
924 VkDeviceMemory* pMem)
925 {
926 ANV_FROM_HANDLE(anv_device, device, _device);
927 struct anv_device_memory *mem;
928 VkResult result;
929
930 assert(pAllocateInfo->sType == VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO);
931
932 /* We support exactly one memory heap. */
933 assert(pAllocateInfo->memoryTypeIndex == 0);
934
935 /* FINISHME: Fail if allocation request exceeds heap size. */
936
937 mem = anv_alloc2(&device->alloc, pAllocator, sizeof(*mem), 8,
938 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
939 if (mem == NULL)
940 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
941
942 result = anv_bo_init_new(&mem->bo, device, pAllocateInfo->allocationSize);
943 if (result != VK_SUCCESS)
944 goto fail;
945
946 *pMem = anv_device_memory_to_handle(mem);
947
948 return VK_SUCCESS;
949
950 fail:
951 anv_free2(&device->alloc, pAllocator, mem);
952
953 return result;
954 }
955
956 void anv_FreeMemory(
957 VkDevice _device,
958 VkDeviceMemory _mem,
959 const VkAllocationCallbacks* pAllocator)
960 {
961 ANV_FROM_HANDLE(anv_device, device, _device);
962 ANV_FROM_HANDLE(anv_device_memory, mem, _mem);
963
964 if (mem->bo.map)
965 anv_gem_munmap(mem->bo.map, mem->bo.size);
966
967 if (mem->bo.gem_handle != 0)
968 anv_gem_close(device, mem->bo.gem_handle);
969
970 anv_free2(&device->alloc, pAllocator, mem);
971 }
972
973 VkResult anv_MapMemory(
974 VkDevice _device,
975 VkDeviceMemory _memory,
976 VkDeviceSize offset,
977 VkDeviceSize size,
978 VkMemoryMapFlags flags,
979 void** ppData)
980 {
981 ANV_FROM_HANDLE(anv_device, device, _device);
982 ANV_FROM_HANDLE(anv_device_memory, mem, _memory);
983
984 /* FIXME: Is this supposed to be thread safe? Since vkUnmapMemory() only
985 * takes a VkDeviceMemory pointer, it seems like only one map of the memory
986 * at a time is valid. We could just mmap up front and return an offset
987 * pointer here, but that may exhaust virtual memory on 32 bit
988 * userspace. */
989
990 mem->map = anv_gem_mmap(device, mem->bo.gem_handle, offset, size);
991 mem->map_size = size;
992
993 *ppData = mem->map;
994
995 return VK_SUCCESS;
996 }
997
998 void anv_UnmapMemory(
999 VkDevice _device,
1000 VkDeviceMemory _memory)
1001 {
1002 ANV_FROM_HANDLE(anv_device_memory, mem, _memory);
1003
1004 anv_gem_munmap(mem->map, mem->map_size);
1005 }
1006
1007 VkResult anv_FlushMappedMemoryRanges(
1008 VkDevice device,
1009 uint32_t memoryRangeCount,
1010 const VkMappedMemoryRange* pMemoryRanges)
1011 {
1012 /* clflush here for !llc platforms */
1013
1014 return VK_SUCCESS;
1015 }
1016
1017 VkResult anv_InvalidateMappedMemoryRanges(
1018 VkDevice device,
1019 uint32_t memoryRangeCount,
1020 const VkMappedMemoryRange* pMemoryRanges)
1021 {
1022 return anv_FlushMappedMemoryRanges(device, memoryRangeCount, pMemoryRanges);
1023 }
1024
1025 void anv_GetBufferMemoryRequirements(
1026 VkDevice device,
1027 VkBuffer _buffer,
1028 VkMemoryRequirements* pMemoryRequirements)
1029 {
1030 ANV_FROM_HANDLE(anv_buffer, buffer, _buffer);
1031
1032 /* The Vulkan spec (git aaed022) says:
1033 *
1034 * memoryTypeBits is a bitfield and contains one bit set for every
1035 * supported memory type for the resource. The bit `1<<i` is set if and
1036 * only if the memory type `i` in the VkPhysicalDeviceMemoryProperties
1037 * structure for the physical device is supported.
1038 *
1039 * We support exactly one memory type.
1040 */
1041 pMemoryRequirements->memoryTypeBits = 1;
1042
1043 pMemoryRequirements->size = buffer->size;
1044 pMemoryRequirements->alignment = 16;
1045 }
1046
1047 void anv_GetImageMemoryRequirements(
1048 VkDevice device,
1049 VkImage _image,
1050 VkMemoryRequirements* pMemoryRequirements)
1051 {
1052 ANV_FROM_HANDLE(anv_image, image, _image);
1053
1054 /* The Vulkan spec (git aaed022) says:
1055 *
1056 * memoryTypeBits is a bitfield and contains one bit set for every
1057 * supported memory type for the resource. The bit `1<<i` is set if and
1058 * only if the memory type `i` in the VkPhysicalDeviceMemoryProperties
1059 * structure for the physical device is supported.
1060 *
1061 * We support exactly one memory type.
1062 */
1063 pMemoryRequirements->memoryTypeBits = 1;
1064
1065 pMemoryRequirements->size = image->size;
1066 pMemoryRequirements->alignment = image->alignment;
1067 }
1068
1069 void anv_GetImageSparseMemoryRequirements(
1070 VkDevice device,
1071 VkImage image,
1072 uint32_t* pNumRequirements,
1073 VkSparseImageMemoryRequirements* pSparseMemoryRequirements)
1074 {
1075 stub();
1076 }
1077
1078 void anv_GetDeviceMemoryCommitment(
1079 VkDevice device,
1080 VkDeviceMemory memory,
1081 VkDeviceSize* pCommittedMemoryInBytes)
1082 {
1083 *pCommittedMemoryInBytes = 0;
1084 }
1085
1086 VkResult anv_BindBufferMemory(
1087 VkDevice device,
1088 VkBuffer _buffer,
1089 VkDeviceMemory _memory,
1090 VkDeviceSize memoryOffset)
1091 {
1092 ANV_FROM_HANDLE(anv_device_memory, mem, _memory);
1093 ANV_FROM_HANDLE(anv_buffer, buffer, _buffer);
1094
1095 buffer->bo = &mem->bo;
1096 buffer->offset = memoryOffset;
1097
1098 return VK_SUCCESS;
1099 }
1100
1101 VkResult anv_BindImageMemory(
1102 VkDevice device,
1103 VkImage _image,
1104 VkDeviceMemory _memory,
1105 VkDeviceSize memoryOffset)
1106 {
1107 ANV_FROM_HANDLE(anv_device_memory, mem, _memory);
1108 ANV_FROM_HANDLE(anv_image, image, _image);
1109
1110 image->bo = &mem->bo;
1111 image->offset = memoryOffset;
1112
1113 return VK_SUCCESS;
1114 }
1115
1116 VkResult anv_QueueBindSparse(
1117 VkQueue queue,
1118 uint32_t bindInfoCount,
1119 const VkBindSparseInfo* pBindInfo,
1120 VkFence fence)
1121 {
1122 stub_return(VK_UNSUPPORTED);
1123 }
1124
1125 VkResult anv_CreateFence(
1126 VkDevice _device,
1127 const VkFenceCreateInfo* pCreateInfo,
1128 const VkAllocationCallbacks* pAllocator,
1129 VkFence* pFence)
1130 {
1131 ANV_FROM_HANDLE(anv_device, device, _device);
1132 struct anv_fence *fence;
1133 struct anv_batch batch;
1134 VkResult result;
1135
1136 const uint32_t fence_size = 128;
1137
1138 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_FENCE_CREATE_INFO);
1139
1140 fence = anv_alloc2(&device->alloc, pAllocator, sizeof(*fence), 8,
1141 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
1142 if (fence == NULL)
1143 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
1144
1145 result = anv_bo_init_new(&fence->bo, device, fence_size);
1146 if (result != VK_SUCCESS)
1147 goto fail;
1148
1149 fence->bo.map =
1150 anv_gem_mmap(device, fence->bo.gem_handle, 0, fence->bo.size);
1151 batch.next = batch.start = fence->bo.map;
1152 batch.end = fence->bo.map + fence->bo.size;
1153 anv_batch_emit(&batch, GEN7_MI_BATCH_BUFFER_END);
1154 anv_batch_emit(&batch, GEN7_MI_NOOP);
1155
1156 fence->exec2_objects[0].handle = fence->bo.gem_handle;
1157 fence->exec2_objects[0].relocation_count = 0;
1158 fence->exec2_objects[0].relocs_ptr = 0;
1159 fence->exec2_objects[0].alignment = 0;
1160 fence->exec2_objects[0].offset = fence->bo.offset;
1161 fence->exec2_objects[0].flags = 0;
1162 fence->exec2_objects[0].rsvd1 = 0;
1163 fence->exec2_objects[0].rsvd2 = 0;
1164
1165 fence->execbuf.buffers_ptr = (uintptr_t) fence->exec2_objects;
1166 fence->execbuf.buffer_count = 1;
1167 fence->execbuf.batch_start_offset = 0;
1168 fence->execbuf.batch_len = batch.next - fence->bo.map;
1169 fence->execbuf.cliprects_ptr = 0;
1170 fence->execbuf.num_cliprects = 0;
1171 fence->execbuf.DR1 = 0;
1172 fence->execbuf.DR4 = 0;
1173
1174 fence->execbuf.flags =
1175 I915_EXEC_HANDLE_LUT | I915_EXEC_NO_RELOC | I915_EXEC_RENDER;
1176 fence->execbuf.rsvd1 = device->context_id;
1177 fence->execbuf.rsvd2 = 0;
1178
1179 *pFence = anv_fence_to_handle(fence);
1180
1181 return VK_SUCCESS;
1182
1183 fail:
1184 anv_free2(&device->alloc, pAllocator, fence);
1185
1186 return result;
1187 }
1188
1189 void anv_DestroyFence(
1190 VkDevice _device,
1191 VkFence _fence,
1192 const VkAllocationCallbacks* pAllocator)
1193 {
1194 ANV_FROM_HANDLE(anv_device, device, _device);
1195 ANV_FROM_HANDLE(anv_fence, fence, _fence);
1196
1197 anv_gem_munmap(fence->bo.map, fence->bo.size);
1198 anv_gem_close(device, fence->bo.gem_handle);
1199 anv_free2(&device->alloc, pAllocator, fence);
1200 }
1201
1202 VkResult anv_ResetFences(
1203 VkDevice _device,
1204 uint32_t fenceCount,
1205 const VkFence* pFences)
1206 {
1207 for (uint32_t i = 0; i < fenceCount; i++) {
1208 ANV_FROM_HANDLE(anv_fence, fence, pFences[i]);
1209 fence->ready = false;
1210 }
1211
1212 return VK_SUCCESS;
1213 }
1214
1215 VkResult anv_GetFenceStatus(
1216 VkDevice _device,
1217 VkFence _fence)
1218 {
1219 ANV_FROM_HANDLE(anv_device, device, _device);
1220 ANV_FROM_HANDLE(anv_fence, fence, _fence);
1221 int64_t t = 0;
1222 int ret;
1223
1224 if (fence->ready)
1225 return VK_SUCCESS;
1226
1227 ret = anv_gem_wait(device, fence->bo.gem_handle, &t);
1228 if (ret == 0) {
1229 fence->ready = true;
1230 return VK_SUCCESS;
1231 }
1232
1233 return VK_NOT_READY;
1234 }
1235
1236 VkResult anv_WaitForFences(
1237 VkDevice _device,
1238 uint32_t fenceCount,
1239 const VkFence* pFences,
1240 VkBool32 waitAll,
1241 uint64_t timeout)
1242 {
1243 ANV_FROM_HANDLE(anv_device, device, _device);
1244
1245 /* DRM_IOCTL_I915_GEM_WAIT uses a signed 64 bit timeout and is supposed
1246 * to block indefinitely timeouts <= 0. Unfortunately, this was broken
1247 * for a couple of kernel releases. Since there's no way to know
1248 * whether or not the kernel we're using is one of the broken ones, the
1249 * best we can do is to clamp the timeout to INT64_MAX. This limits the
1250 * maximum timeout from 584 years to 292 years - likely not a big deal.
1251 */
1252 if (timeout > INT64_MAX)
1253 timeout = INT64_MAX;
1254
1255 int64_t t = timeout;
1256
1257 /* FIXME: handle !waitAll */
1258
1259 for (uint32_t i = 0; i < fenceCount; i++) {
1260 ANV_FROM_HANDLE(anv_fence, fence, pFences[i]);
1261 int ret = anv_gem_wait(device, fence->bo.gem_handle, &t);
1262 if (ret == -1 && errno == ETIME) {
1263 return VK_TIMEOUT;
1264 } else if (ret == -1) {
1265 /* We don't know the real error. */
1266 return vk_errorf(VK_ERROR_OUT_OF_DEVICE_MEMORY,
1267 "gem wait failed: %m");
1268 }
1269 }
1270
1271 return VK_SUCCESS;
1272 }
1273
1274 // Queue semaphore functions
1275
1276 VkResult anv_CreateSemaphore(
1277 VkDevice device,
1278 const VkSemaphoreCreateInfo* pCreateInfo,
1279 const VkAllocationCallbacks* pAllocator,
1280 VkSemaphore* pSemaphore)
1281 {
1282 *pSemaphore = (VkSemaphore)1;
1283 stub_return(VK_SUCCESS);
1284 }
1285
1286 void anv_DestroySemaphore(
1287 VkDevice device,
1288 VkSemaphore semaphore,
1289 const VkAllocationCallbacks* pAllocator)
1290 {
1291 stub();
1292 }
1293
1294 VkResult anv_QueueSignalSemaphore(
1295 VkQueue queue,
1296 VkSemaphore semaphore)
1297 {
1298 stub_return(VK_UNSUPPORTED);
1299 }
1300
1301 VkResult anv_QueueWaitSemaphore(
1302 VkQueue queue,
1303 VkSemaphore semaphore)
1304 {
1305 stub_return(VK_UNSUPPORTED);
1306 }
1307
1308 // Event functions
1309
1310 VkResult anv_CreateEvent(
1311 VkDevice device,
1312 const VkEventCreateInfo* pCreateInfo,
1313 const VkAllocationCallbacks* pAllocator,
1314 VkEvent* pEvent)
1315 {
1316 stub_return(VK_UNSUPPORTED);
1317 }
1318
1319 void anv_DestroyEvent(
1320 VkDevice device,
1321 VkEvent event,
1322 const VkAllocationCallbacks* pAllocator)
1323 {
1324 stub();
1325 }
1326
1327 VkResult anv_GetEventStatus(
1328 VkDevice device,
1329 VkEvent event)
1330 {
1331 stub_return(VK_UNSUPPORTED);
1332 }
1333
1334 VkResult anv_SetEvent(
1335 VkDevice device,
1336 VkEvent event)
1337 {
1338 stub_return(VK_UNSUPPORTED);
1339 }
1340
1341 VkResult anv_ResetEvent(
1342 VkDevice device,
1343 VkEvent event)
1344 {
1345 stub_return(VK_UNSUPPORTED);
1346 }
1347
1348 // Buffer functions
1349
1350 VkResult anv_CreateBuffer(
1351 VkDevice _device,
1352 const VkBufferCreateInfo* pCreateInfo,
1353 const VkAllocationCallbacks* pAllocator,
1354 VkBuffer* pBuffer)
1355 {
1356 ANV_FROM_HANDLE(anv_device, device, _device);
1357 struct anv_buffer *buffer;
1358
1359 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO);
1360
1361 buffer = anv_alloc2(&device->alloc, pAllocator, sizeof(*buffer), 8,
1362 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
1363 if (buffer == NULL)
1364 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
1365
1366 buffer->size = pCreateInfo->size;
1367 buffer->bo = NULL;
1368 buffer->offset = 0;
1369
1370 *pBuffer = anv_buffer_to_handle(buffer);
1371
1372 return VK_SUCCESS;
1373 }
1374
1375 void anv_DestroyBuffer(
1376 VkDevice _device,
1377 VkBuffer _buffer,
1378 const VkAllocationCallbacks* pAllocator)
1379 {
1380 ANV_FROM_HANDLE(anv_device, device, _device);
1381 ANV_FROM_HANDLE(anv_buffer, buffer, _buffer);
1382
1383 anv_free2(&device->alloc, pAllocator, buffer);
1384 }
1385
1386 void
1387 anv_fill_buffer_surface_state(struct anv_device *device, void *state,
1388 const struct anv_format *format,
1389 uint32_t offset, uint32_t range, uint32_t stride)
1390 {
1391 switch (device->info.gen) {
1392 case 7:
1393 if (device->info.is_haswell)
1394 gen75_fill_buffer_surface_state(state, format, offset, range, stride);
1395 else
1396 gen7_fill_buffer_surface_state(state, format, offset, range, stride);
1397 break;
1398 case 8:
1399 gen8_fill_buffer_surface_state(state, format, offset, range, stride);
1400 break;
1401 case 9:
1402 gen9_fill_buffer_surface_state(state, format, offset, range, stride);
1403 break;
1404 default:
1405 unreachable("unsupported gen\n");
1406 }
1407 }
1408
1409 VkResult anv_CreateBufferView(
1410 VkDevice _device,
1411 const VkBufferViewCreateInfo* pCreateInfo,
1412 const VkAllocationCallbacks* pAllocator,
1413 VkBufferView* pView)
1414 {
1415 stub_return(VK_UNSUPPORTED);
1416 }
1417
1418 void anv_DestroyBufferView(
1419 VkDevice _device,
1420 VkBufferView _bview,
1421 const VkAllocationCallbacks* pAllocator)
1422 {
1423 stub();
1424 }
1425
1426 void anv_DestroySampler(
1427 VkDevice _device,
1428 VkSampler _sampler,
1429 const VkAllocationCallbacks* pAllocator)
1430 {
1431 ANV_FROM_HANDLE(anv_device, device, _device);
1432 ANV_FROM_HANDLE(anv_sampler, sampler, _sampler);
1433
1434 anv_free2(&device->alloc, pAllocator, sampler);
1435 }
1436
1437 VkResult anv_CreateFramebuffer(
1438 VkDevice _device,
1439 const VkFramebufferCreateInfo* pCreateInfo,
1440 const VkAllocationCallbacks* pAllocator,
1441 VkFramebuffer* pFramebuffer)
1442 {
1443 ANV_FROM_HANDLE(anv_device, device, _device);
1444 struct anv_framebuffer *framebuffer;
1445
1446 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO);
1447
1448 size_t size = sizeof(*framebuffer) +
1449 sizeof(struct anv_image_view *) * pCreateInfo->attachmentCount;
1450 framebuffer = anv_alloc2(&device->alloc, pAllocator, size, 8,
1451 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
1452 if (framebuffer == NULL)
1453 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
1454
1455 framebuffer->attachment_count = pCreateInfo->attachmentCount;
1456 for (uint32_t i = 0; i < pCreateInfo->attachmentCount; i++) {
1457 VkImageView _iview = pCreateInfo->pAttachments[i];
1458 framebuffer->attachments[i] = anv_image_view_from_handle(_iview);
1459 }
1460
1461 framebuffer->width = pCreateInfo->width;
1462 framebuffer->height = pCreateInfo->height;
1463 framebuffer->layers = pCreateInfo->layers;
1464
1465 *pFramebuffer = anv_framebuffer_to_handle(framebuffer);
1466
1467 return VK_SUCCESS;
1468 }
1469
1470 void anv_DestroyFramebuffer(
1471 VkDevice _device,
1472 VkFramebuffer _fb,
1473 const VkAllocationCallbacks* pAllocator)
1474 {
1475 ANV_FROM_HANDLE(anv_device, device, _device);
1476 ANV_FROM_HANDLE(anv_framebuffer, fb, _fb);
1477
1478 anv_free2(&device->alloc, pAllocator, fb);
1479 }
1480
1481 void vkCmdDbgMarkerBegin(
1482 VkCommandBuffer commandBuffer,
1483 const char* pMarker)
1484 __attribute__ ((visibility ("default")));
1485
1486 void vkCmdDbgMarkerEnd(
1487 VkCommandBuffer commandBuffer)
1488 __attribute__ ((visibility ("default")));
1489
1490 void vkCmdDbgMarkerBegin(
1491 VkCommandBuffer commandBuffer,
1492 const char* pMarker)
1493 {
1494 }
1495
1496 void vkCmdDbgMarkerEnd(
1497 VkCommandBuffer commandBuffer)
1498 {
1499 }