vk/0.210.0: Rework memory property flags
[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_DEVICE_LOCAL_BIT |
534 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
535 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT |
536 VK_MEMORY_PROPERTY_HOST_CACHED_BIT,
537 .heapIndex = 1,
538 };
539
540 pMemoryProperties->memoryHeapCount = 1;
541 pMemoryProperties->memoryHeaps[0] = (VkMemoryHeap) {
542 .size = heap_size,
543 .flags = VK_MEMORY_HEAP_DEVICE_LOCAL_BIT,
544 };
545 }
546
547 PFN_vkVoidFunction anv_GetInstanceProcAddr(
548 VkInstance instance,
549 const char* pName)
550 {
551 return anv_lookup_entrypoint(pName);
552 }
553
554 PFN_vkVoidFunction anv_GetDeviceProcAddr(
555 VkDevice device,
556 const char* pName)
557 {
558 return anv_lookup_entrypoint(pName);
559 }
560
561 static VkResult
562 anv_queue_init(struct anv_device *device, struct anv_queue *queue)
563 {
564 queue->_loader_data.loaderMagic = ICD_LOADER_MAGIC;
565 queue->device = device;
566 queue->pool = &device->surface_state_pool;
567
568 return VK_SUCCESS;
569 }
570
571 static void
572 anv_queue_finish(struct anv_queue *queue)
573 {
574 }
575
576 static void
577 anv_device_init_border_colors(struct anv_device *device)
578 {
579 static const VkClearColorValue border_colors[] = {
580 [VK_BORDER_COLOR_FLOAT_TRANSPARENT_BLACK] = { .float32 = { 0.0, 0.0, 0.0, 0.0 } },
581 [VK_BORDER_COLOR_FLOAT_OPAQUE_BLACK] = { .float32 = { 0.0, 0.0, 0.0, 1.0 } },
582 [VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE] = { .float32 = { 1.0, 1.0, 1.0, 1.0 } },
583 [VK_BORDER_COLOR_INT_TRANSPARENT_BLACK] = { .uint32 = { 0, 0, 0, 0 } },
584 [VK_BORDER_COLOR_INT_OPAQUE_BLACK] = { .uint32 = { 0, 0, 0, 1 } },
585 [VK_BORDER_COLOR_INT_OPAQUE_WHITE] = { .uint32 = { 1, 1, 1, 1 } },
586 };
587
588 device->border_colors =
589 anv_state_pool_alloc(&device->dynamic_state_pool,
590 sizeof(border_colors), 32);
591 memcpy(device->border_colors.map, border_colors, sizeof(border_colors));
592 }
593
594 VkResult anv_CreateDevice(
595 VkPhysicalDevice physicalDevice,
596 const VkDeviceCreateInfo* pCreateInfo,
597 const VkAllocationCallbacks* pAllocator,
598 VkDevice* pDevice)
599 {
600 ANV_FROM_HANDLE(anv_physical_device, physical_device, physicalDevice);
601 struct anv_device *device;
602
603 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO);
604
605 for (uint32_t i = 0; i < pCreateInfo->enabledExtensionNameCount; i++) {
606 bool found = false;
607 for (uint32_t j = 0; j < ARRAY_SIZE(device_extensions); j++) {
608 if (strcmp(pCreateInfo->ppEnabledExtensionNames[i],
609 device_extensions[j].extensionName) == 0) {
610 found = true;
611 break;
612 }
613 }
614 if (!found)
615 return vk_error(VK_ERROR_EXTENSION_NOT_PRESENT);
616 }
617
618 anv_set_dispatch_devinfo(physical_device->info);
619
620 device = anv_alloc2(&physical_device->instance->alloc, pAllocator,
621 sizeof(*device), 8,
622 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
623 if (!device)
624 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
625
626 device->_loader_data.loaderMagic = ICD_LOADER_MAGIC;
627 device->instance = physical_device->instance;
628
629 if (pAllocator)
630 device->alloc = *pAllocator;
631 else
632 device->alloc = physical_device->instance->alloc;
633
634 /* XXX(chadv): Can we dup() physicalDevice->fd here? */
635 device->fd = open(physical_device->path, O_RDWR | O_CLOEXEC);
636 if (device->fd == -1)
637 goto fail_device;
638
639 device->context_id = anv_gem_create_context(device);
640 if (device->context_id == -1)
641 goto fail_fd;
642
643 pthread_mutex_init(&device->mutex, NULL);
644
645 anv_bo_pool_init(&device->batch_bo_pool, device, ANV_CMD_BUFFER_BATCH_SIZE);
646
647 anv_block_pool_init(&device->dynamic_state_block_pool, device, 2048);
648
649 anv_state_pool_init(&device->dynamic_state_pool,
650 &device->dynamic_state_block_pool);
651
652 anv_block_pool_init(&device->instruction_block_pool, device, 4096);
653 anv_block_pool_init(&device->surface_state_block_pool, device, 4096);
654
655 anv_state_pool_init(&device->surface_state_pool,
656 &device->surface_state_block_pool);
657
658 anv_bo_init_new(&device->workaround_bo, device, 1024);
659
660 anv_block_pool_init(&device->scratch_block_pool, device, 0x10000);
661
662 device->info = *physical_device->info;
663 device->isl_dev = physical_device->isl_dev;
664
665 anv_queue_init(device, &device->queue);
666
667 anv_device_init_meta(device);
668
669 anv_device_init_border_colors(device);
670
671 *pDevice = anv_device_to_handle(device);
672
673 return VK_SUCCESS;
674
675 fail_fd:
676 close(device->fd);
677 fail_device:
678 anv_free(&device->alloc, device);
679
680 return vk_error(VK_ERROR_INITIALIZATION_FAILED);
681 }
682
683 void anv_DestroyDevice(
684 VkDevice _device,
685 const VkAllocationCallbacks* pAllocator)
686 {
687 ANV_FROM_HANDLE(anv_device, device, _device);
688
689 anv_queue_finish(&device->queue);
690
691 anv_device_finish_meta(device);
692
693 #ifdef HAVE_VALGRIND
694 /* We only need to free these to prevent valgrind errors. The backing
695 * BO will go away in a couple of lines so we don't actually leak.
696 */
697 anv_state_pool_free(&device->dynamic_state_pool, device->border_colors);
698 #endif
699
700 anv_gem_munmap(device->workaround_bo.map, device->workaround_bo.size);
701 anv_gem_close(device, device->workaround_bo.gem_handle);
702
703 anv_bo_pool_finish(&device->batch_bo_pool);
704 anv_state_pool_finish(&device->dynamic_state_pool);
705 anv_block_pool_finish(&device->dynamic_state_block_pool);
706 anv_block_pool_finish(&device->instruction_block_pool);
707 anv_state_pool_finish(&device->surface_state_pool);
708 anv_block_pool_finish(&device->surface_state_block_pool);
709 anv_block_pool_finish(&device->scratch_block_pool);
710
711 close(device->fd);
712
713 anv_free(&device->alloc, device);
714 }
715
716 VkResult anv_EnumerateInstanceExtensionProperties(
717 const char* pLayerName,
718 uint32_t* pPropertyCount,
719 VkExtensionProperties* pProperties)
720 {
721 if (pProperties == NULL) {
722 *pPropertyCount = ARRAY_SIZE(global_extensions);
723 return VK_SUCCESS;
724 }
725
726 assert(*pPropertyCount >= ARRAY_SIZE(global_extensions));
727
728 *pPropertyCount = ARRAY_SIZE(global_extensions);
729 memcpy(pProperties, global_extensions, sizeof(global_extensions));
730
731 return VK_SUCCESS;
732 }
733
734 VkResult anv_EnumerateDeviceExtensionProperties(
735 VkPhysicalDevice physicalDevice,
736 const char* pLayerName,
737 uint32_t* pPropertyCount,
738 VkExtensionProperties* pProperties)
739 {
740 if (pProperties == NULL) {
741 *pPropertyCount = ARRAY_SIZE(device_extensions);
742 return VK_SUCCESS;
743 }
744
745 assert(*pPropertyCount >= ARRAY_SIZE(device_extensions));
746
747 *pPropertyCount = ARRAY_SIZE(device_extensions);
748 memcpy(pProperties, device_extensions, sizeof(device_extensions));
749
750 return VK_SUCCESS;
751 }
752
753 VkResult anv_EnumerateInstanceLayerProperties(
754 uint32_t* pPropertyCount,
755 VkLayerProperties* pProperties)
756 {
757 if (pProperties == NULL) {
758 *pPropertyCount = 0;
759 return VK_SUCCESS;
760 }
761
762 /* None supported at this time */
763 return vk_error(VK_ERROR_LAYER_NOT_PRESENT);
764 }
765
766 VkResult anv_EnumerateDeviceLayerProperties(
767 VkPhysicalDevice physicalDevice,
768 uint32_t* pPropertyCount,
769 VkLayerProperties* pProperties)
770 {
771 if (pProperties == NULL) {
772 *pPropertyCount = 0;
773 return VK_SUCCESS;
774 }
775
776 /* None supported at this time */
777 return vk_error(VK_ERROR_LAYER_NOT_PRESENT);
778 }
779
780 void anv_GetDeviceQueue(
781 VkDevice _device,
782 uint32_t queueNodeIndex,
783 uint32_t queueIndex,
784 VkQueue* pQueue)
785 {
786 ANV_FROM_HANDLE(anv_device, device, _device);
787
788 assert(queueIndex == 0);
789
790 *pQueue = anv_queue_to_handle(&device->queue);
791 }
792
793 VkResult anv_QueueSubmit(
794 VkQueue _queue,
795 uint32_t commandBufferCount,
796 const VkCommandBuffer* pCommandBuffers,
797 VkFence _fence)
798 {
799 ANV_FROM_HANDLE(anv_queue, queue, _queue);
800 ANV_FROM_HANDLE(anv_fence, fence, _fence);
801 struct anv_device *device = queue->device;
802 int ret;
803
804 for (uint32_t i = 0; i < commandBufferCount; i++) {
805 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, pCommandBuffers[i]);
806
807 assert(cmd_buffer->level == VK_COMMAND_BUFFER_LEVEL_PRIMARY);
808
809 ret = anv_gem_execbuffer(device, &cmd_buffer->execbuf2.execbuf);
810 if (ret != 0) {
811 /* We don't know the real error. */
812 return vk_errorf(VK_ERROR_OUT_OF_DEVICE_MEMORY,
813 "execbuf2 failed: %m");
814 }
815
816 if (fence) {
817 ret = anv_gem_execbuffer(device, &fence->execbuf);
818 if (ret != 0) {
819 /* We don't know the real error. */
820 return vk_errorf(VK_ERROR_OUT_OF_DEVICE_MEMORY,
821 "execbuf2 failed: %m");
822 }
823 }
824
825 for (uint32_t i = 0; i < cmd_buffer->execbuf2.bo_count; i++)
826 cmd_buffer->execbuf2.bos[i]->offset = cmd_buffer->execbuf2.objects[i].offset;
827 }
828
829 return VK_SUCCESS;
830 }
831
832 VkResult anv_QueueWaitIdle(
833 VkQueue _queue)
834 {
835 ANV_FROM_HANDLE(anv_queue, queue, _queue);
836
837 return ANV_CALL(DeviceWaitIdle)(anv_device_to_handle(queue->device));
838 }
839
840 VkResult anv_DeviceWaitIdle(
841 VkDevice _device)
842 {
843 ANV_FROM_HANDLE(anv_device, device, _device);
844 struct anv_state state;
845 struct anv_batch batch;
846 struct drm_i915_gem_execbuffer2 execbuf;
847 struct drm_i915_gem_exec_object2 exec2_objects[1];
848 struct anv_bo *bo = NULL;
849 VkResult result;
850 int64_t timeout;
851 int ret;
852
853 state = anv_state_pool_alloc(&device->dynamic_state_pool, 32, 32);
854 bo = &device->dynamic_state_pool.block_pool->bo;
855 batch.start = batch.next = state.map;
856 batch.end = state.map + 32;
857 anv_batch_emit(&batch, GEN7_MI_BATCH_BUFFER_END);
858 anv_batch_emit(&batch, GEN7_MI_NOOP);
859
860 exec2_objects[0].handle = bo->gem_handle;
861 exec2_objects[0].relocation_count = 0;
862 exec2_objects[0].relocs_ptr = 0;
863 exec2_objects[0].alignment = 0;
864 exec2_objects[0].offset = bo->offset;
865 exec2_objects[0].flags = 0;
866 exec2_objects[0].rsvd1 = 0;
867 exec2_objects[0].rsvd2 = 0;
868
869 execbuf.buffers_ptr = (uintptr_t) exec2_objects;
870 execbuf.buffer_count = 1;
871 execbuf.batch_start_offset = state.offset;
872 execbuf.batch_len = batch.next - state.map;
873 execbuf.cliprects_ptr = 0;
874 execbuf.num_cliprects = 0;
875 execbuf.DR1 = 0;
876 execbuf.DR4 = 0;
877
878 execbuf.flags =
879 I915_EXEC_HANDLE_LUT | I915_EXEC_NO_RELOC | I915_EXEC_RENDER;
880 execbuf.rsvd1 = device->context_id;
881 execbuf.rsvd2 = 0;
882
883 ret = anv_gem_execbuffer(device, &execbuf);
884 if (ret != 0) {
885 /* We don't know the real error. */
886 result = vk_errorf(VK_ERROR_OUT_OF_DEVICE_MEMORY, "execbuf2 failed: %m");
887 goto fail;
888 }
889
890 timeout = INT64_MAX;
891 ret = anv_gem_wait(device, bo->gem_handle, &timeout);
892 if (ret != 0) {
893 /* We don't know the real error. */
894 result = vk_errorf(VK_ERROR_OUT_OF_DEVICE_MEMORY, "execbuf2 failed: %m");
895 goto fail;
896 }
897
898 anv_state_pool_free(&device->dynamic_state_pool, state);
899
900 return VK_SUCCESS;
901
902 fail:
903 anv_state_pool_free(&device->dynamic_state_pool, state);
904
905 return result;
906 }
907
908 VkResult
909 anv_bo_init_new(struct anv_bo *bo, struct anv_device *device, uint64_t size)
910 {
911 bo->gem_handle = anv_gem_create(device, size);
912 if (!bo->gem_handle)
913 return vk_error(VK_ERROR_OUT_OF_DEVICE_MEMORY);
914
915 bo->map = NULL;
916 bo->index = 0;
917 bo->offset = 0;
918 bo->size = size;
919
920 return VK_SUCCESS;
921 }
922
923 VkResult anv_AllocateMemory(
924 VkDevice _device,
925 const VkMemoryAllocateInfo* pAllocateInfo,
926 const VkAllocationCallbacks* pAllocator,
927 VkDeviceMemory* pMem)
928 {
929 ANV_FROM_HANDLE(anv_device, device, _device);
930 struct anv_device_memory *mem;
931 VkResult result;
932
933 assert(pAllocateInfo->sType == VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO);
934
935 /* We support exactly one memory heap. */
936 assert(pAllocateInfo->memoryTypeIndex == 0);
937
938 /* FINISHME: Fail if allocation request exceeds heap size. */
939
940 mem = anv_alloc2(&device->alloc, pAllocator, sizeof(*mem), 8,
941 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
942 if (mem == NULL)
943 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
944
945 result = anv_bo_init_new(&mem->bo, device, pAllocateInfo->allocationSize);
946 if (result != VK_SUCCESS)
947 goto fail;
948
949 *pMem = anv_device_memory_to_handle(mem);
950
951 return VK_SUCCESS;
952
953 fail:
954 anv_free2(&device->alloc, pAllocator, mem);
955
956 return result;
957 }
958
959 void anv_FreeMemory(
960 VkDevice _device,
961 VkDeviceMemory _mem,
962 const VkAllocationCallbacks* pAllocator)
963 {
964 ANV_FROM_HANDLE(anv_device, device, _device);
965 ANV_FROM_HANDLE(anv_device_memory, mem, _mem);
966
967 if (mem->bo.map)
968 anv_gem_munmap(mem->bo.map, mem->bo.size);
969
970 if (mem->bo.gem_handle != 0)
971 anv_gem_close(device, mem->bo.gem_handle);
972
973 anv_free2(&device->alloc, pAllocator, mem);
974 }
975
976 VkResult anv_MapMemory(
977 VkDevice _device,
978 VkDeviceMemory _memory,
979 VkDeviceSize offset,
980 VkDeviceSize size,
981 VkMemoryMapFlags flags,
982 void** ppData)
983 {
984 ANV_FROM_HANDLE(anv_device, device, _device);
985 ANV_FROM_HANDLE(anv_device_memory, mem, _memory);
986
987 /* FIXME: Is this supposed to be thread safe? Since vkUnmapMemory() only
988 * takes a VkDeviceMemory pointer, it seems like only one map of the memory
989 * at a time is valid. We could just mmap up front and return an offset
990 * pointer here, but that may exhaust virtual memory on 32 bit
991 * userspace. */
992
993 mem->map = anv_gem_mmap(device, mem->bo.gem_handle, offset, size);
994 mem->map_size = size;
995
996 *ppData = mem->map;
997
998 return VK_SUCCESS;
999 }
1000
1001 void anv_UnmapMemory(
1002 VkDevice _device,
1003 VkDeviceMemory _memory)
1004 {
1005 ANV_FROM_HANDLE(anv_device_memory, mem, _memory);
1006
1007 anv_gem_munmap(mem->map, mem->map_size);
1008 }
1009
1010 VkResult anv_FlushMappedMemoryRanges(
1011 VkDevice device,
1012 uint32_t memoryRangeCount,
1013 const VkMappedMemoryRange* pMemoryRanges)
1014 {
1015 /* clflush here for !llc platforms */
1016
1017 return VK_SUCCESS;
1018 }
1019
1020 VkResult anv_InvalidateMappedMemoryRanges(
1021 VkDevice device,
1022 uint32_t memoryRangeCount,
1023 const VkMappedMemoryRange* pMemoryRanges)
1024 {
1025 return anv_FlushMappedMemoryRanges(device, memoryRangeCount, pMemoryRanges);
1026 }
1027
1028 void anv_GetBufferMemoryRequirements(
1029 VkDevice device,
1030 VkBuffer _buffer,
1031 VkMemoryRequirements* pMemoryRequirements)
1032 {
1033 ANV_FROM_HANDLE(anv_buffer, buffer, _buffer);
1034
1035 /* The Vulkan spec (git aaed022) says:
1036 *
1037 * memoryTypeBits is a bitfield and contains one bit set for every
1038 * supported memory type for the resource. The bit `1<<i` is set if and
1039 * only if the memory type `i` in the VkPhysicalDeviceMemoryProperties
1040 * structure for the physical device is supported.
1041 *
1042 * We support exactly one memory type.
1043 */
1044 pMemoryRequirements->memoryTypeBits = 1;
1045
1046 pMemoryRequirements->size = buffer->size;
1047 pMemoryRequirements->alignment = 16;
1048 }
1049
1050 void anv_GetImageMemoryRequirements(
1051 VkDevice device,
1052 VkImage _image,
1053 VkMemoryRequirements* pMemoryRequirements)
1054 {
1055 ANV_FROM_HANDLE(anv_image, image, _image);
1056
1057 /* The Vulkan spec (git aaed022) says:
1058 *
1059 * memoryTypeBits is a bitfield and contains one bit set for every
1060 * supported memory type for the resource. The bit `1<<i` is set if and
1061 * only if the memory type `i` in the VkPhysicalDeviceMemoryProperties
1062 * structure for the physical device is supported.
1063 *
1064 * We support exactly one memory type.
1065 */
1066 pMemoryRequirements->memoryTypeBits = 1;
1067
1068 pMemoryRequirements->size = image->size;
1069 pMemoryRequirements->alignment = image->alignment;
1070 }
1071
1072 void anv_GetImageSparseMemoryRequirements(
1073 VkDevice device,
1074 VkImage image,
1075 uint32_t* pSparseMemoryRequirementCount,
1076 VkSparseImageMemoryRequirements* pSparseMemoryRequirements)
1077 {
1078 stub();
1079 }
1080
1081 void anv_GetDeviceMemoryCommitment(
1082 VkDevice device,
1083 VkDeviceMemory memory,
1084 VkDeviceSize* pCommittedMemoryInBytes)
1085 {
1086 *pCommittedMemoryInBytes = 0;
1087 }
1088
1089 VkResult anv_BindBufferMemory(
1090 VkDevice device,
1091 VkBuffer _buffer,
1092 VkDeviceMemory _memory,
1093 VkDeviceSize memoryOffset)
1094 {
1095 ANV_FROM_HANDLE(anv_device_memory, mem, _memory);
1096 ANV_FROM_HANDLE(anv_buffer, buffer, _buffer);
1097
1098 buffer->bo = &mem->bo;
1099 buffer->offset = memoryOffset;
1100
1101 return VK_SUCCESS;
1102 }
1103
1104 VkResult anv_BindImageMemory(
1105 VkDevice device,
1106 VkImage _image,
1107 VkDeviceMemory _memory,
1108 VkDeviceSize memoryOffset)
1109 {
1110 ANV_FROM_HANDLE(anv_device_memory, mem, _memory);
1111 ANV_FROM_HANDLE(anv_image, image, _image);
1112
1113 image->bo = &mem->bo;
1114 image->offset = memoryOffset;
1115
1116 return VK_SUCCESS;
1117 }
1118
1119 VkResult anv_QueueBindSparse(
1120 VkQueue queue,
1121 uint32_t bindInfoCount,
1122 const VkBindSparseInfo* pBindInfo,
1123 VkFence fence)
1124 {
1125 stub_return(VK_UNSUPPORTED);
1126 }
1127
1128 VkResult anv_CreateFence(
1129 VkDevice _device,
1130 const VkFenceCreateInfo* pCreateInfo,
1131 const VkAllocationCallbacks* pAllocator,
1132 VkFence* pFence)
1133 {
1134 ANV_FROM_HANDLE(anv_device, device, _device);
1135 struct anv_fence *fence;
1136 struct anv_batch batch;
1137 VkResult result;
1138
1139 const uint32_t fence_size = 128;
1140
1141 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_FENCE_CREATE_INFO);
1142
1143 fence = anv_alloc2(&device->alloc, pAllocator, sizeof(*fence), 8,
1144 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
1145 if (fence == NULL)
1146 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
1147
1148 result = anv_bo_init_new(&fence->bo, device, fence_size);
1149 if (result != VK_SUCCESS)
1150 goto fail;
1151
1152 fence->bo.map =
1153 anv_gem_mmap(device, fence->bo.gem_handle, 0, fence->bo.size);
1154 batch.next = batch.start = fence->bo.map;
1155 batch.end = fence->bo.map + fence->bo.size;
1156 anv_batch_emit(&batch, GEN7_MI_BATCH_BUFFER_END);
1157 anv_batch_emit(&batch, GEN7_MI_NOOP);
1158
1159 fence->exec2_objects[0].handle = fence->bo.gem_handle;
1160 fence->exec2_objects[0].relocation_count = 0;
1161 fence->exec2_objects[0].relocs_ptr = 0;
1162 fence->exec2_objects[0].alignment = 0;
1163 fence->exec2_objects[0].offset = fence->bo.offset;
1164 fence->exec2_objects[0].flags = 0;
1165 fence->exec2_objects[0].rsvd1 = 0;
1166 fence->exec2_objects[0].rsvd2 = 0;
1167
1168 fence->execbuf.buffers_ptr = (uintptr_t) fence->exec2_objects;
1169 fence->execbuf.buffer_count = 1;
1170 fence->execbuf.batch_start_offset = 0;
1171 fence->execbuf.batch_len = batch.next - fence->bo.map;
1172 fence->execbuf.cliprects_ptr = 0;
1173 fence->execbuf.num_cliprects = 0;
1174 fence->execbuf.DR1 = 0;
1175 fence->execbuf.DR4 = 0;
1176
1177 fence->execbuf.flags =
1178 I915_EXEC_HANDLE_LUT | I915_EXEC_NO_RELOC | I915_EXEC_RENDER;
1179 fence->execbuf.rsvd1 = device->context_id;
1180 fence->execbuf.rsvd2 = 0;
1181
1182 *pFence = anv_fence_to_handle(fence);
1183
1184 return VK_SUCCESS;
1185
1186 fail:
1187 anv_free2(&device->alloc, pAllocator, fence);
1188
1189 return result;
1190 }
1191
1192 void anv_DestroyFence(
1193 VkDevice _device,
1194 VkFence _fence,
1195 const VkAllocationCallbacks* pAllocator)
1196 {
1197 ANV_FROM_HANDLE(anv_device, device, _device);
1198 ANV_FROM_HANDLE(anv_fence, fence, _fence);
1199
1200 anv_gem_munmap(fence->bo.map, fence->bo.size);
1201 anv_gem_close(device, fence->bo.gem_handle);
1202 anv_free2(&device->alloc, pAllocator, fence);
1203 }
1204
1205 VkResult anv_ResetFences(
1206 VkDevice _device,
1207 uint32_t fenceCount,
1208 const VkFence* pFences)
1209 {
1210 for (uint32_t i = 0; i < fenceCount; i++) {
1211 ANV_FROM_HANDLE(anv_fence, fence, pFences[i]);
1212 fence->ready = false;
1213 }
1214
1215 return VK_SUCCESS;
1216 }
1217
1218 VkResult anv_GetFenceStatus(
1219 VkDevice _device,
1220 VkFence _fence)
1221 {
1222 ANV_FROM_HANDLE(anv_device, device, _device);
1223 ANV_FROM_HANDLE(anv_fence, fence, _fence);
1224 int64_t t = 0;
1225 int ret;
1226
1227 if (fence->ready)
1228 return VK_SUCCESS;
1229
1230 ret = anv_gem_wait(device, fence->bo.gem_handle, &t);
1231 if (ret == 0) {
1232 fence->ready = true;
1233 return VK_SUCCESS;
1234 }
1235
1236 return VK_NOT_READY;
1237 }
1238
1239 VkResult anv_WaitForFences(
1240 VkDevice _device,
1241 uint32_t fenceCount,
1242 const VkFence* pFences,
1243 VkBool32 waitAll,
1244 uint64_t timeout)
1245 {
1246 ANV_FROM_HANDLE(anv_device, device, _device);
1247
1248 /* DRM_IOCTL_I915_GEM_WAIT uses a signed 64 bit timeout and is supposed
1249 * to block indefinitely timeouts <= 0. Unfortunately, this was broken
1250 * for a couple of kernel releases. Since there's no way to know
1251 * whether or not the kernel we're using is one of the broken ones, the
1252 * best we can do is to clamp the timeout to INT64_MAX. This limits the
1253 * maximum timeout from 584 years to 292 years - likely not a big deal.
1254 */
1255 if (timeout > INT64_MAX)
1256 timeout = INT64_MAX;
1257
1258 int64_t t = timeout;
1259
1260 /* FIXME: handle !waitAll */
1261
1262 for (uint32_t i = 0; i < fenceCount; i++) {
1263 ANV_FROM_HANDLE(anv_fence, fence, pFences[i]);
1264 int ret = anv_gem_wait(device, fence->bo.gem_handle, &t);
1265 if (ret == -1 && errno == ETIME) {
1266 return VK_TIMEOUT;
1267 } else if (ret == -1) {
1268 /* We don't know the real error. */
1269 return vk_errorf(VK_ERROR_OUT_OF_DEVICE_MEMORY,
1270 "gem wait failed: %m");
1271 }
1272 }
1273
1274 return VK_SUCCESS;
1275 }
1276
1277 // Queue semaphore functions
1278
1279 VkResult anv_CreateSemaphore(
1280 VkDevice device,
1281 const VkSemaphoreCreateInfo* pCreateInfo,
1282 const VkAllocationCallbacks* pAllocator,
1283 VkSemaphore* pSemaphore)
1284 {
1285 *pSemaphore = (VkSemaphore)1;
1286 stub_return(VK_SUCCESS);
1287 }
1288
1289 void anv_DestroySemaphore(
1290 VkDevice device,
1291 VkSemaphore semaphore,
1292 const VkAllocationCallbacks* pAllocator)
1293 {
1294 stub();
1295 }
1296
1297 // Event functions
1298
1299 VkResult anv_CreateEvent(
1300 VkDevice device,
1301 const VkEventCreateInfo* pCreateInfo,
1302 const VkAllocationCallbacks* pAllocator,
1303 VkEvent* pEvent)
1304 {
1305 stub_return(VK_UNSUPPORTED);
1306 }
1307
1308 void anv_DestroyEvent(
1309 VkDevice device,
1310 VkEvent event,
1311 const VkAllocationCallbacks* pAllocator)
1312 {
1313 stub();
1314 }
1315
1316 VkResult anv_GetEventStatus(
1317 VkDevice device,
1318 VkEvent event)
1319 {
1320 stub_return(VK_UNSUPPORTED);
1321 }
1322
1323 VkResult anv_SetEvent(
1324 VkDevice device,
1325 VkEvent event)
1326 {
1327 stub_return(VK_UNSUPPORTED);
1328 }
1329
1330 VkResult anv_ResetEvent(
1331 VkDevice device,
1332 VkEvent event)
1333 {
1334 stub_return(VK_UNSUPPORTED);
1335 }
1336
1337 // Buffer functions
1338
1339 VkResult anv_CreateBuffer(
1340 VkDevice _device,
1341 const VkBufferCreateInfo* pCreateInfo,
1342 const VkAllocationCallbacks* pAllocator,
1343 VkBuffer* pBuffer)
1344 {
1345 ANV_FROM_HANDLE(anv_device, device, _device);
1346 struct anv_buffer *buffer;
1347
1348 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO);
1349
1350 buffer = anv_alloc2(&device->alloc, pAllocator, sizeof(*buffer), 8,
1351 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
1352 if (buffer == NULL)
1353 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
1354
1355 buffer->size = pCreateInfo->size;
1356 buffer->bo = NULL;
1357 buffer->offset = 0;
1358
1359 *pBuffer = anv_buffer_to_handle(buffer);
1360
1361 return VK_SUCCESS;
1362 }
1363
1364 void anv_DestroyBuffer(
1365 VkDevice _device,
1366 VkBuffer _buffer,
1367 const VkAllocationCallbacks* pAllocator)
1368 {
1369 ANV_FROM_HANDLE(anv_device, device, _device);
1370 ANV_FROM_HANDLE(anv_buffer, buffer, _buffer);
1371
1372 anv_free2(&device->alloc, pAllocator, buffer);
1373 }
1374
1375 void
1376 anv_fill_buffer_surface_state(struct anv_device *device, void *state,
1377 const struct anv_format *format,
1378 uint32_t offset, uint32_t range, uint32_t stride)
1379 {
1380 switch (device->info.gen) {
1381 case 7:
1382 if (device->info.is_haswell)
1383 gen75_fill_buffer_surface_state(state, format, offset, range, stride);
1384 else
1385 gen7_fill_buffer_surface_state(state, format, offset, range, stride);
1386 break;
1387 case 8:
1388 gen8_fill_buffer_surface_state(state, format, offset, range, stride);
1389 break;
1390 case 9:
1391 gen9_fill_buffer_surface_state(state, format, offset, range, stride);
1392 break;
1393 default:
1394 unreachable("unsupported gen\n");
1395 }
1396 }
1397
1398 VkResult anv_CreateBufferView(
1399 VkDevice _device,
1400 const VkBufferViewCreateInfo* pCreateInfo,
1401 const VkAllocationCallbacks* pAllocator,
1402 VkBufferView* pView)
1403 {
1404 stub_return(VK_UNSUPPORTED);
1405 }
1406
1407 void anv_DestroyBufferView(
1408 VkDevice _device,
1409 VkBufferView _bview,
1410 const VkAllocationCallbacks* pAllocator)
1411 {
1412 stub();
1413 }
1414
1415 void anv_DestroySampler(
1416 VkDevice _device,
1417 VkSampler _sampler,
1418 const VkAllocationCallbacks* pAllocator)
1419 {
1420 ANV_FROM_HANDLE(anv_device, device, _device);
1421 ANV_FROM_HANDLE(anv_sampler, sampler, _sampler);
1422
1423 anv_free2(&device->alloc, pAllocator, sampler);
1424 }
1425
1426 VkResult anv_CreateFramebuffer(
1427 VkDevice _device,
1428 const VkFramebufferCreateInfo* pCreateInfo,
1429 const VkAllocationCallbacks* pAllocator,
1430 VkFramebuffer* pFramebuffer)
1431 {
1432 ANV_FROM_HANDLE(anv_device, device, _device);
1433 struct anv_framebuffer *framebuffer;
1434
1435 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO);
1436
1437 size_t size = sizeof(*framebuffer) +
1438 sizeof(struct anv_image_view *) * pCreateInfo->attachmentCount;
1439 framebuffer = anv_alloc2(&device->alloc, pAllocator, size, 8,
1440 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
1441 if (framebuffer == NULL)
1442 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
1443
1444 framebuffer->attachment_count = pCreateInfo->attachmentCount;
1445 for (uint32_t i = 0; i < pCreateInfo->attachmentCount; i++) {
1446 VkImageView _iview = pCreateInfo->pAttachments[i];
1447 framebuffer->attachments[i] = anv_image_view_from_handle(_iview);
1448 }
1449
1450 framebuffer->width = pCreateInfo->width;
1451 framebuffer->height = pCreateInfo->height;
1452 framebuffer->layers = pCreateInfo->layers;
1453
1454 *pFramebuffer = anv_framebuffer_to_handle(framebuffer);
1455
1456 return VK_SUCCESS;
1457 }
1458
1459 void anv_DestroyFramebuffer(
1460 VkDevice _device,
1461 VkFramebuffer _fb,
1462 const VkAllocationCallbacks* pAllocator)
1463 {
1464 ANV_FROM_HANDLE(anv_device, device, _device);
1465 ANV_FROM_HANDLE(anv_framebuffer, fb, _fb);
1466
1467 anv_free2(&device->alloc, pAllocator, fb);
1468 }
1469
1470 void vkCmdDbgMarkerBegin(
1471 VkCommandBuffer commandBuffer,
1472 const char* pMarker)
1473 __attribute__ ((visibility ("default")));
1474
1475 void vkCmdDbgMarkerEnd(
1476 VkCommandBuffer commandBuffer)
1477 __attribute__ ((visibility ("default")));
1478
1479 void vkCmdDbgMarkerBegin(
1480 VkCommandBuffer commandBuffer,
1481 const char* pMarker)
1482 {
1483 }
1484
1485 void vkCmdDbgMarkerEnd(
1486 VkCommandBuffer commandBuffer)
1487 {
1488 }