anv: Enable clip and cull distance support.
[mesa.git] / src / intel / 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 <sys/mman.h>
28 #include <unistd.h>
29 #include <fcntl.h>
30
31 #include "anv_private.h"
32 #include "anv_timestamp.h"
33 #include "util/strtod.h"
34 #include "util/debug.h"
35
36 #include "genxml/gen7_pack.h"
37
38 struct anv_dispatch_table dtable;
39
40 static void
41 compiler_debug_log(void *data, const char *fmt, ...)
42 { }
43
44 static void
45 compiler_perf_log(void *data, const char *fmt, ...)
46 {
47 va_list args;
48 va_start(args, fmt);
49
50 if (unlikely(INTEL_DEBUG & DEBUG_PERF))
51 vfprintf(stderr, fmt, args);
52
53 va_end(args);
54 }
55
56 static VkResult
57 anv_physical_device_init(struct anv_physical_device *device,
58 struct anv_instance *instance,
59 const char *path)
60 {
61 VkResult result;
62 int fd;
63
64 fd = open(path, O_RDWR | O_CLOEXEC);
65 if (fd < 0)
66 return vk_error(VK_ERROR_INCOMPATIBLE_DRIVER);
67
68 device->_loader_data.loaderMagic = ICD_LOADER_MAGIC;
69 device->instance = instance;
70
71 assert(strlen(path) < ARRAY_SIZE(device->path));
72 strncpy(device->path, path, ARRAY_SIZE(device->path));
73
74 device->chipset_id = anv_gem_get_param(fd, I915_PARAM_CHIPSET_ID);
75 if (!device->chipset_id) {
76 result = vk_error(VK_ERROR_INCOMPATIBLE_DRIVER);
77 goto fail;
78 }
79
80 device->name = gen_get_device_name(device->chipset_id);
81 if (!gen_get_device_info(device->chipset_id, &device->info)) {
82 result = vk_error(VK_ERROR_INCOMPATIBLE_DRIVER);
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 == 7 && device->info.is_baytrail) {
91 fprintf(stderr, "WARNING: Bay Trail Vulkan support is incomplete\n");
92 } else if (device->info.gen >= 8) {
93 /* Broadwell, Cherryview, Skylake, Broxton, Kabylake is as fully
94 * supported as anything */
95 } else {
96 result = vk_errorf(VK_ERROR_INCOMPATIBLE_DRIVER,
97 "Vulkan not yet supported on %s", device->name);
98 goto fail;
99 }
100
101 device->cmd_parser_version = -1;
102 if (device->info.gen == 7) {
103 device->cmd_parser_version =
104 anv_gem_get_param(fd, I915_PARAM_CMD_PARSER_VERSION);
105 if (device->cmd_parser_version == -1) {
106 result = vk_errorf(VK_ERROR_INITIALIZATION_FAILED,
107 "failed to get command parser version");
108 goto fail;
109 }
110 }
111
112 if (anv_gem_get_aperture(fd, &device->aperture_size) == -1) {
113 result = vk_errorf(VK_ERROR_INITIALIZATION_FAILED,
114 "failed to get aperture size: %m");
115 goto fail;
116 }
117
118 if (!anv_gem_get_param(fd, I915_PARAM_HAS_WAIT_TIMEOUT)) {
119 result = vk_errorf(VK_ERROR_INITIALIZATION_FAILED,
120 "kernel missing gem wait");
121 goto fail;
122 }
123
124 if (!anv_gem_get_param(fd, I915_PARAM_HAS_EXECBUF2)) {
125 result = vk_errorf(VK_ERROR_INITIALIZATION_FAILED,
126 "kernel missing execbuf2");
127 goto fail;
128 }
129
130 if (!device->info.has_llc &&
131 anv_gem_get_param(fd, I915_PARAM_MMAP_VERSION) < 1) {
132 result = vk_errorf(VK_ERROR_INITIALIZATION_FAILED,
133 "kernel missing wc mmap");
134 goto fail;
135 }
136
137 bool swizzled = anv_gem_get_bit6_swizzle(fd, I915_TILING_X);
138
139 /* GENs prior to 8 do not support EU/Subslice info */
140 if (device->info.gen >= 8) {
141 device->subslice_total = anv_gem_get_param(fd, I915_PARAM_SUBSLICE_TOTAL);
142 device->eu_total = anv_gem_get_param(fd, I915_PARAM_EU_TOTAL);
143
144 /* Without this information, we cannot get the right Braswell
145 * brandstrings, and we have to use conservative numbers for GPGPU on
146 * many platforms, but otherwise, things will just work.
147 */
148 if (device->subslice_total < 1 || device->eu_total < 1) {
149 fprintf(stderr, "WARNING: Kernel 4.1 required to properly"
150 " query GPU properties.\n");
151 }
152 } else if (device->info.gen == 7) {
153 device->subslice_total = 1 << (device->info.gt - 1);
154 }
155
156 if (device->info.is_cherryview &&
157 device->subslice_total > 0 && device->eu_total > 0) {
158 /* Logical CS threads = EUs per subslice * 7 threads per EU */
159 uint32_t max_cs_threads = device->eu_total / device->subslice_total * 7;
160
161 /* Fuse configurations may give more threads than expected, never less. */
162 if (max_cs_threads > device->info.max_cs_threads)
163 device->info.max_cs_threads = max_cs_threads;
164 }
165
166 close(fd);
167
168 brw_process_intel_debug_variable();
169
170 device->compiler = brw_compiler_create(NULL, &device->info);
171 if (device->compiler == NULL) {
172 result = vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
173 goto fail;
174 }
175 device->compiler->shader_debug_log = compiler_debug_log;
176 device->compiler->shader_perf_log = compiler_perf_log;
177
178 result = anv_init_wsi(device);
179 if (result != VK_SUCCESS)
180 goto fail;
181
182 /* XXX: Actually detect bit6 swizzling */
183 isl_device_init(&device->isl_dev, &device->info, swizzled);
184
185 return VK_SUCCESS;
186
187 fail:
188 close(fd);
189 return result;
190 }
191
192 static void
193 anv_physical_device_finish(struct anv_physical_device *device)
194 {
195 anv_finish_wsi(device);
196 ralloc_free(device->compiler);
197 }
198
199 static const VkExtensionProperties global_extensions[] = {
200 {
201 .extensionName = VK_KHR_SURFACE_EXTENSION_NAME,
202 .specVersion = 25,
203 },
204 #ifdef VK_USE_PLATFORM_XCB_KHR
205 {
206 .extensionName = VK_KHR_XCB_SURFACE_EXTENSION_NAME,
207 .specVersion = 6,
208 },
209 #endif
210 #ifdef VK_USE_PLATFORM_XLIB_KHR
211 {
212 .extensionName = VK_KHR_XLIB_SURFACE_EXTENSION_NAME,
213 .specVersion = 6,
214 },
215 #endif
216 #ifdef VK_USE_PLATFORM_WAYLAND_KHR
217 {
218 .extensionName = VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME,
219 .specVersion = 5,
220 },
221 #endif
222 };
223
224 static const VkExtensionProperties device_extensions[] = {
225 {
226 .extensionName = VK_KHR_SWAPCHAIN_EXTENSION_NAME,
227 .specVersion = 68,
228 },
229 };
230
231 static void *
232 default_alloc_func(void *pUserData, size_t size, size_t align,
233 VkSystemAllocationScope allocationScope)
234 {
235 return malloc(size);
236 }
237
238 static void *
239 default_realloc_func(void *pUserData, void *pOriginal, size_t size,
240 size_t align, VkSystemAllocationScope allocationScope)
241 {
242 return realloc(pOriginal, size);
243 }
244
245 static void
246 default_free_func(void *pUserData, void *pMemory)
247 {
248 free(pMemory);
249 }
250
251 static const VkAllocationCallbacks default_alloc = {
252 .pUserData = NULL,
253 .pfnAllocation = default_alloc_func,
254 .pfnReallocation = default_realloc_func,
255 .pfnFree = default_free_func,
256 };
257
258 VkResult anv_CreateInstance(
259 const VkInstanceCreateInfo* pCreateInfo,
260 const VkAllocationCallbacks* pAllocator,
261 VkInstance* pInstance)
262 {
263 struct anv_instance *instance;
264
265 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO);
266
267 uint32_t client_version;
268 if (pCreateInfo->pApplicationInfo &&
269 pCreateInfo->pApplicationInfo->apiVersion != 0) {
270 client_version = pCreateInfo->pApplicationInfo->apiVersion;
271 } else {
272 client_version = VK_MAKE_VERSION(1, 0, 0);
273 }
274
275 if (VK_MAKE_VERSION(1, 0, 0) > client_version ||
276 client_version > VK_MAKE_VERSION(1, 0, 0xfff)) {
277 return vk_errorf(VK_ERROR_INCOMPATIBLE_DRIVER,
278 "Client requested version %d.%d.%d",
279 VK_VERSION_MAJOR(client_version),
280 VK_VERSION_MINOR(client_version),
281 VK_VERSION_PATCH(client_version));
282 }
283
284 for (uint32_t i = 0; i < pCreateInfo->enabledExtensionCount; i++) {
285 bool found = false;
286 for (uint32_t j = 0; j < ARRAY_SIZE(global_extensions); j++) {
287 if (strcmp(pCreateInfo->ppEnabledExtensionNames[i],
288 global_extensions[j].extensionName) == 0) {
289 found = true;
290 break;
291 }
292 }
293 if (!found)
294 return vk_error(VK_ERROR_EXTENSION_NOT_PRESENT);
295 }
296
297 instance = vk_alloc2(&default_alloc, pAllocator, sizeof(*instance), 8,
298 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
299 if (!instance)
300 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
301
302 instance->_loader_data.loaderMagic = ICD_LOADER_MAGIC;
303
304 if (pAllocator)
305 instance->alloc = *pAllocator;
306 else
307 instance->alloc = default_alloc;
308
309 instance->apiVersion = client_version;
310 instance->physicalDeviceCount = -1;
311
312 _mesa_locale_init();
313
314 VG(VALGRIND_CREATE_MEMPOOL(instance, 0, false));
315
316 *pInstance = anv_instance_to_handle(instance);
317
318 return VK_SUCCESS;
319 }
320
321 void anv_DestroyInstance(
322 VkInstance _instance,
323 const VkAllocationCallbacks* pAllocator)
324 {
325 ANV_FROM_HANDLE(anv_instance, instance, _instance);
326
327 if (instance->physicalDeviceCount > 0) {
328 /* We support at most one physical device. */
329 assert(instance->physicalDeviceCount == 1);
330 anv_physical_device_finish(&instance->physicalDevice);
331 }
332
333 VG(VALGRIND_DESTROY_MEMPOOL(instance));
334
335 _mesa_locale_fini();
336
337 vk_free(&instance->alloc, instance);
338 }
339
340 VkResult anv_EnumeratePhysicalDevices(
341 VkInstance _instance,
342 uint32_t* pPhysicalDeviceCount,
343 VkPhysicalDevice* pPhysicalDevices)
344 {
345 ANV_FROM_HANDLE(anv_instance, instance, _instance);
346 VkResult result;
347
348 if (instance->physicalDeviceCount < 0) {
349 char path[20];
350 for (unsigned i = 0; i < 8; i++) {
351 snprintf(path, sizeof(path), "/dev/dri/renderD%d", 128 + i);
352 result = anv_physical_device_init(&instance->physicalDevice,
353 instance, path);
354 if (result != VK_ERROR_INCOMPATIBLE_DRIVER)
355 break;
356 }
357
358 if (result == VK_ERROR_INCOMPATIBLE_DRIVER) {
359 instance->physicalDeviceCount = 0;
360 } else if (result == VK_SUCCESS) {
361 instance->physicalDeviceCount = 1;
362 } else {
363 return result;
364 }
365 }
366
367 /* pPhysicalDeviceCount is an out parameter if pPhysicalDevices is NULL;
368 * otherwise it's an inout parameter.
369 *
370 * The Vulkan spec (git aaed022) says:
371 *
372 * pPhysicalDeviceCount is a pointer to an unsigned integer variable
373 * that is initialized with the number of devices the application is
374 * prepared to receive handles to. pname:pPhysicalDevices is pointer to
375 * an array of at least this many VkPhysicalDevice handles [...].
376 *
377 * Upon success, if pPhysicalDevices is NULL, vkEnumeratePhysicalDevices
378 * overwrites the contents of the variable pointed to by
379 * pPhysicalDeviceCount with the number of physical devices in in the
380 * instance; otherwise, vkEnumeratePhysicalDevices overwrites
381 * pPhysicalDeviceCount with the number of physical handles written to
382 * pPhysicalDevices.
383 */
384 if (!pPhysicalDevices) {
385 *pPhysicalDeviceCount = instance->physicalDeviceCount;
386 } else if (*pPhysicalDeviceCount >= 1) {
387 pPhysicalDevices[0] = anv_physical_device_to_handle(&instance->physicalDevice);
388 *pPhysicalDeviceCount = 1;
389 } else if (*pPhysicalDeviceCount < instance->physicalDeviceCount) {
390 return VK_INCOMPLETE;
391 } else {
392 *pPhysicalDeviceCount = 0;
393 }
394
395 return VK_SUCCESS;
396 }
397
398 void anv_GetPhysicalDeviceFeatures(
399 VkPhysicalDevice physicalDevice,
400 VkPhysicalDeviceFeatures* pFeatures)
401 {
402 ANV_FROM_HANDLE(anv_physical_device, pdevice, physicalDevice);
403
404 *pFeatures = (VkPhysicalDeviceFeatures) {
405 .robustBufferAccess = true,
406 .fullDrawIndexUint32 = true,
407 .imageCubeArray = false,
408 .independentBlend = true,
409 .geometryShader = true,
410 .tessellationShader = false,
411 .sampleRateShading = true,
412 .dualSrcBlend = true,
413 .logicOp = true,
414 .multiDrawIndirect = false,
415 .drawIndirectFirstInstance = false,
416 .depthClamp = true,
417 .depthBiasClamp = false,
418 .fillModeNonSolid = true,
419 .depthBounds = false,
420 .wideLines = true,
421 .largePoints = true,
422 .alphaToOne = true,
423 .multiViewport = true,
424 .samplerAnisotropy = true,
425 .textureCompressionETC2 = pdevice->info.gen >= 8 ||
426 pdevice->info.is_baytrail,
427 .textureCompressionASTC_LDR = pdevice->info.gen >= 9, /* FINISHME CHV */
428 .textureCompressionBC = true,
429 .occlusionQueryPrecise = true,
430 .pipelineStatisticsQuery = false,
431 .fragmentStoresAndAtomics = true,
432 .shaderTessellationAndGeometryPointSize = true,
433 .shaderImageGatherExtended = false,
434 .shaderStorageImageExtendedFormats = false,
435 .shaderStorageImageMultisample = false,
436 .shaderUniformBufferArrayDynamicIndexing = true,
437 .shaderSampledImageArrayDynamicIndexing = true,
438 .shaderStorageBufferArrayDynamicIndexing = true,
439 .shaderStorageImageArrayDynamicIndexing = true,
440 .shaderStorageImageReadWithoutFormat = false,
441 .shaderStorageImageWriteWithoutFormat = true,
442 .shaderClipDistance = true,
443 .shaderCullDistance = true,
444 .shaderFloat64 = false,
445 .shaderInt64 = false,
446 .shaderInt16 = false,
447 .alphaToOne = true,
448 .variableMultisampleRate = false,
449 .inheritedQueries = false,
450 };
451
452 /* We can't do image stores in vec4 shaders */
453 pFeatures->vertexPipelineStoresAndAtomics =
454 pdevice->compiler->scalar_stage[MESA_SHADER_VERTEX] &&
455 pdevice->compiler->scalar_stage[MESA_SHADER_GEOMETRY];
456 }
457
458 void
459 anv_device_get_cache_uuid(void *uuid)
460 {
461 memset(uuid, 0, VK_UUID_SIZE);
462 snprintf(uuid, VK_UUID_SIZE, "anv-%s", ANV_TIMESTAMP);
463 }
464
465 void anv_GetPhysicalDeviceProperties(
466 VkPhysicalDevice physicalDevice,
467 VkPhysicalDeviceProperties* pProperties)
468 {
469 ANV_FROM_HANDLE(anv_physical_device, pdevice, physicalDevice);
470 const struct gen_device_info *devinfo = &pdevice->info;
471
472 const float time_stamp_base = devinfo->gen >= 9 ? 83.333 : 80.0;
473
474 /* See assertions made when programming the buffer surface state. */
475 const uint32_t max_raw_buffer_sz = devinfo->gen >= 7 ?
476 (1ul << 30) : (1ul << 27);
477
478 VkSampleCountFlags sample_counts =
479 isl_device_get_sample_counts(&pdevice->isl_dev);
480
481 VkPhysicalDeviceLimits limits = {
482 .maxImageDimension1D = (1 << 14),
483 .maxImageDimension2D = (1 << 14),
484 .maxImageDimension3D = (1 << 11),
485 .maxImageDimensionCube = (1 << 14),
486 .maxImageArrayLayers = (1 << 11),
487 .maxTexelBufferElements = 128 * 1024 * 1024,
488 .maxUniformBufferRange = (1ul << 27),
489 .maxStorageBufferRange = max_raw_buffer_sz,
490 .maxPushConstantsSize = MAX_PUSH_CONSTANTS_SIZE,
491 .maxMemoryAllocationCount = UINT32_MAX,
492 .maxSamplerAllocationCount = 64 * 1024,
493 .bufferImageGranularity = 64, /* A cache line */
494 .sparseAddressSpaceSize = 0,
495 .maxBoundDescriptorSets = MAX_SETS,
496 .maxPerStageDescriptorSamplers = 64,
497 .maxPerStageDescriptorUniformBuffers = 64,
498 .maxPerStageDescriptorStorageBuffers = 64,
499 .maxPerStageDescriptorSampledImages = 64,
500 .maxPerStageDescriptorStorageImages = 64,
501 .maxPerStageDescriptorInputAttachments = 64,
502 .maxPerStageResources = 128,
503 .maxDescriptorSetSamplers = 256,
504 .maxDescriptorSetUniformBuffers = 256,
505 .maxDescriptorSetUniformBuffersDynamic = 256,
506 .maxDescriptorSetStorageBuffers = 256,
507 .maxDescriptorSetStorageBuffersDynamic = 256,
508 .maxDescriptorSetSampledImages = 256,
509 .maxDescriptorSetStorageImages = 256,
510 .maxDescriptorSetInputAttachments = 256,
511 .maxVertexInputAttributes = 32,
512 .maxVertexInputBindings = 32,
513 .maxVertexInputAttributeOffset = 2047,
514 .maxVertexInputBindingStride = 2048,
515 .maxVertexOutputComponents = 128,
516 .maxTessellationGenerationLevel = 0,
517 .maxTessellationPatchSize = 0,
518 .maxTessellationControlPerVertexInputComponents = 0,
519 .maxTessellationControlPerVertexOutputComponents = 0,
520 .maxTessellationControlPerPatchOutputComponents = 0,
521 .maxTessellationControlTotalOutputComponents = 0,
522 .maxTessellationEvaluationInputComponents = 0,
523 .maxTessellationEvaluationOutputComponents = 0,
524 .maxGeometryShaderInvocations = 32,
525 .maxGeometryInputComponents = 64,
526 .maxGeometryOutputComponents = 128,
527 .maxGeometryOutputVertices = 256,
528 .maxGeometryTotalOutputComponents = 1024,
529 .maxFragmentInputComponents = 128,
530 .maxFragmentOutputAttachments = 8,
531 .maxFragmentDualSrcAttachments = 2,
532 .maxFragmentCombinedOutputResources = 8,
533 .maxComputeSharedMemorySize = 32768,
534 .maxComputeWorkGroupCount = { 65535, 65535, 65535 },
535 .maxComputeWorkGroupInvocations = 16 * devinfo->max_cs_threads,
536 .maxComputeWorkGroupSize = {
537 16 * devinfo->max_cs_threads,
538 16 * devinfo->max_cs_threads,
539 16 * devinfo->max_cs_threads,
540 },
541 .subPixelPrecisionBits = 4 /* FIXME */,
542 .subTexelPrecisionBits = 4 /* FIXME */,
543 .mipmapPrecisionBits = 4 /* FIXME */,
544 .maxDrawIndexedIndexValue = UINT32_MAX,
545 .maxDrawIndirectCount = UINT32_MAX,
546 .maxSamplerLodBias = 16,
547 .maxSamplerAnisotropy = 16,
548 .maxViewports = MAX_VIEWPORTS,
549 .maxViewportDimensions = { (1 << 14), (1 << 14) },
550 .viewportBoundsRange = { INT16_MIN, INT16_MAX },
551 .viewportSubPixelBits = 13, /* We take a float? */
552 .minMemoryMapAlignment = 4096, /* A page */
553 .minTexelBufferOffsetAlignment = 1,
554 .minUniformBufferOffsetAlignment = 1,
555 .minStorageBufferOffsetAlignment = 1,
556 .minTexelOffset = -8,
557 .maxTexelOffset = 7,
558 .minTexelGatherOffset = -8,
559 .maxTexelGatherOffset = 7,
560 .minInterpolationOffset = -0.5,
561 .maxInterpolationOffset = 0.4375,
562 .subPixelInterpolationOffsetBits = 4,
563 .maxFramebufferWidth = (1 << 14),
564 .maxFramebufferHeight = (1 << 14),
565 .maxFramebufferLayers = (1 << 10),
566 .framebufferColorSampleCounts = sample_counts,
567 .framebufferDepthSampleCounts = sample_counts,
568 .framebufferStencilSampleCounts = sample_counts,
569 .framebufferNoAttachmentsSampleCounts = sample_counts,
570 .maxColorAttachments = MAX_RTS,
571 .sampledImageColorSampleCounts = sample_counts,
572 .sampledImageIntegerSampleCounts = VK_SAMPLE_COUNT_1_BIT,
573 .sampledImageDepthSampleCounts = sample_counts,
574 .sampledImageStencilSampleCounts = sample_counts,
575 .storageImageSampleCounts = VK_SAMPLE_COUNT_1_BIT,
576 .maxSampleMaskWords = 1,
577 .timestampComputeAndGraphics = false,
578 .timestampPeriod = time_stamp_base,
579 .maxClipDistances = 8,
580 .maxCullDistances = 8,
581 .maxCombinedClipAndCullDistances = 8,
582 .discreteQueuePriorities = 1,
583 .pointSizeRange = { 0.125, 255.875 },
584 .lineWidthRange = { 0.0, 7.9921875 },
585 .pointSizeGranularity = (1.0 / 8.0),
586 .lineWidthGranularity = (1.0 / 128.0),
587 .strictLines = false, /* FINISHME */
588 .standardSampleLocations = true,
589 .optimalBufferCopyOffsetAlignment = 128,
590 .optimalBufferCopyRowPitchAlignment = 128,
591 .nonCoherentAtomSize = 64,
592 };
593
594 *pProperties = (VkPhysicalDeviceProperties) {
595 .apiVersion = VK_MAKE_VERSION(1, 0, 5),
596 .driverVersion = 1,
597 .vendorID = 0x8086,
598 .deviceID = pdevice->chipset_id,
599 .deviceType = VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU,
600 .limits = limits,
601 .sparseProperties = {0}, /* Broadwell doesn't do sparse. */
602 };
603
604 strcpy(pProperties->deviceName, pdevice->name);
605 anv_device_get_cache_uuid(pProperties->pipelineCacheUUID);
606 }
607
608 void anv_GetPhysicalDeviceQueueFamilyProperties(
609 VkPhysicalDevice physicalDevice,
610 uint32_t* pCount,
611 VkQueueFamilyProperties* pQueueFamilyProperties)
612 {
613 if (pQueueFamilyProperties == NULL) {
614 *pCount = 1;
615 return;
616 }
617
618 assert(*pCount >= 1);
619
620 *pQueueFamilyProperties = (VkQueueFamilyProperties) {
621 .queueFlags = VK_QUEUE_GRAPHICS_BIT |
622 VK_QUEUE_COMPUTE_BIT |
623 VK_QUEUE_TRANSFER_BIT,
624 .queueCount = 1,
625 .timestampValidBits = 36, /* XXX: Real value here */
626 .minImageTransferGranularity = (VkExtent3D) { 1, 1, 1 },
627 };
628 }
629
630 void anv_GetPhysicalDeviceMemoryProperties(
631 VkPhysicalDevice physicalDevice,
632 VkPhysicalDeviceMemoryProperties* pMemoryProperties)
633 {
634 ANV_FROM_HANDLE(anv_physical_device, physical_device, physicalDevice);
635 VkDeviceSize heap_size;
636
637 /* Reserve some wiggle room for the driver by exposing only 75% of the
638 * aperture to the heap.
639 */
640 heap_size = 3 * physical_device->aperture_size / 4;
641
642 if (physical_device->info.has_llc) {
643 /* Big core GPUs share LLC with the CPU and thus one memory type can be
644 * both cached and coherent at the same time.
645 */
646 pMemoryProperties->memoryTypeCount = 1;
647 pMemoryProperties->memoryTypes[0] = (VkMemoryType) {
648 .propertyFlags = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT |
649 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
650 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT |
651 VK_MEMORY_PROPERTY_HOST_CACHED_BIT,
652 .heapIndex = 0,
653 };
654 } else {
655 /* The spec requires that we expose a host-visible, coherent memory
656 * type, but Atom GPUs don't share LLC. Thus we offer two memory types
657 * to give the application a choice between cached, but not coherent and
658 * coherent but uncached (WC though).
659 */
660 pMemoryProperties->memoryTypeCount = 2;
661 pMemoryProperties->memoryTypes[0] = (VkMemoryType) {
662 .propertyFlags = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT |
663 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
664 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
665 .heapIndex = 0,
666 };
667 pMemoryProperties->memoryTypes[1] = (VkMemoryType) {
668 .propertyFlags = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT |
669 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
670 VK_MEMORY_PROPERTY_HOST_CACHED_BIT,
671 .heapIndex = 0,
672 };
673 }
674
675 pMemoryProperties->memoryHeapCount = 1;
676 pMemoryProperties->memoryHeaps[0] = (VkMemoryHeap) {
677 .size = heap_size,
678 .flags = VK_MEMORY_HEAP_DEVICE_LOCAL_BIT,
679 };
680 }
681
682 PFN_vkVoidFunction anv_GetInstanceProcAddr(
683 VkInstance instance,
684 const char* pName)
685 {
686 return anv_lookup_entrypoint(NULL, pName);
687 }
688
689 /* With version 1+ of the loader interface the ICD should expose
690 * vk_icdGetInstanceProcAddr to work around certain LD_PRELOAD issues seen in apps.
691 */
692 PUBLIC
693 VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL vk_icdGetInstanceProcAddr(
694 VkInstance instance,
695 const char* pName);
696
697 PUBLIC
698 VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL vk_icdGetInstanceProcAddr(
699 VkInstance instance,
700 const char* pName)
701 {
702 return anv_GetInstanceProcAddr(instance, pName);
703 }
704
705 PFN_vkVoidFunction anv_GetDeviceProcAddr(
706 VkDevice _device,
707 const char* pName)
708 {
709 ANV_FROM_HANDLE(anv_device, device, _device);
710 return anv_lookup_entrypoint(&device->info, pName);
711 }
712
713 static VkResult
714 anv_queue_init(struct anv_device *device, struct anv_queue *queue)
715 {
716 queue->_loader_data.loaderMagic = ICD_LOADER_MAGIC;
717 queue->device = device;
718 queue->pool = &device->surface_state_pool;
719
720 return VK_SUCCESS;
721 }
722
723 static void
724 anv_queue_finish(struct anv_queue *queue)
725 {
726 }
727
728 static struct anv_state
729 anv_state_pool_emit_data(struct anv_state_pool *pool, size_t size, size_t align, const void *p)
730 {
731 struct anv_state state;
732
733 state = anv_state_pool_alloc(pool, size, align);
734 memcpy(state.map, p, size);
735
736 if (!pool->block_pool->device->info.has_llc)
737 anv_state_clflush(state);
738
739 return state;
740 }
741
742 struct gen8_border_color {
743 union {
744 float float32[4];
745 uint32_t uint32[4];
746 };
747 /* Pad out to 64 bytes */
748 uint32_t _pad[12];
749 };
750
751 static void
752 anv_device_init_border_colors(struct anv_device *device)
753 {
754 static const struct gen8_border_color border_colors[] = {
755 [VK_BORDER_COLOR_FLOAT_TRANSPARENT_BLACK] = { .float32 = { 0.0, 0.0, 0.0, 0.0 } },
756 [VK_BORDER_COLOR_FLOAT_OPAQUE_BLACK] = { .float32 = { 0.0, 0.0, 0.0, 1.0 } },
757 [VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE] = { .float32 = { 1.0, 1.0, 1.0, 1.0 } },
758 [VK_BORDER_COLOR_INT_TRANSPARENT_BLACK] = { .uint32 = { 0, 0, 0, 0 } },
759 [VK_BORDER_COLOR_INT_OPAQUE_BLACK] = { .uint32 = { 0, 0, 0, 1 } },
760 [VK_BORDER_COLOR_INT_OPAQUE_WHITE] = { .uint32 = { 1, 1, 1, 1 } },
761 };
762
763 device->border_colors = anv_state_pool_emit_data(&device->dynamic_state_pool,
764 sizeof(border_colors), 64,
765 border_colors);
766 }
767
768 VkResult
769 anv_device_submit_simple_batch(struct anv_device *device,
770 struct anv_batch *batch)
771 {
772 struct drm_i915_gem_execbuffer2 execbuf;
773 struct drm_i915_gem_exec_object2 exec2_objects[1];
774 struct anv_bo bo, *exec_bos[1];
775 VkResult result = VK_SUCCESS;
776 uint32_t size;
777 int64_t timeout;
778 int ret;
779
780 /* Kernel driver requires 8 byte aligned batch length */
781 size = align_u32(batch->next - batch->start, 8);
782 result = anv_bo_pool_alloc(&device->batch_bo_pool, &bo, size);
783 if (result != VK_SUCCESS)
784 return result;
785
786 memcpy(bo.map, batch->start, size);
787 if (!device->info.has_llc)
788 anv_clflush_range(bo.map, size);
789
790 exec_bos[0] = &bo;
791 exec2_objects[0].handle = bo.gem_handle;
792 exec2_objects[0].relocation_count = 0;
793 exec2_objects[0].relocs_ptr = 0;
794 exec2_objects[0].alignment = 0;
795 exec2_objects[0].offset = bo.offset;
796 exec2_objects[0].flags = 0;
797 exec2_objects[0].rsvd1 = 0;
798 exec2_objects[0].rsvd2 = 0;
799
800 execbuf.buffers_ptr = (uintptr_t) exec2_objects;
801 execbuf.buffer_count = 1;
802 execbuf.batch_start_offset = 0;
803 execbuf.batch_len = size;
804 execbuf.cliprects_ptr = 0;
805 execbuf.num_cliprects = 0;
806 execbuf.DR1 = 0;
807 execbuf.DR4 = 0;
808
809 execbuf.flags =
810 I915_EXEC_HANDLE_LUT | I915_EXEC_NO_RELOC | I915_EXEC_RENDER;
811 execbuf.rsvd1 = device->context_id;
812 execbuf.rsvd2 = 0;
813
814 result = anv_device_execbuf(device, &execbuf, exec_bos);
815 if (result != VK_SUCCESS)
816 goto fail;
817
818 timeout = INT64_MAX;
819 ret = anv_gem_wait(device, bo.gem_handle, &timeout);
820 if (ret != 0) {
821 /* We don't know the real error. */
822 result = vk_errorf(VK_ERROR_DEVICE_LOST, "execbuf2 failed: %m");
823 goto fail;
824 }
825
826 fail:
827 anv_bo_pool_free(&device->batch_bo_pool, &bo);
828
829 return result;
830 }
831
832 VkResult anv_CreateDevice(
833 VkPhysicalDevice physicalDevice,
834 const VkDeviceCreateInfo* pCreateInfo,
835 const VkAllocationCallbacks* pAllocator,
836 VkDevice* pDevice)
837 {
838 ANV_FROM_HANDLE(anv_physical_device, physical_device, physicalDevice);
839 VkResult result;
840 struct anv_device *device;
841
842 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO);
843
844 for (uint32_t i = 0; i < pCreateInfo->enabledExtensionCount; i++) {
845 bool found = false;
846 for (uint32_t j = 0; j < ARRAY_SIZE(device_extensions); j++) {
847 if (strcmp(pCreateInfo->ppEnabledExtensionNames[i],
848 device_extensions[j].extensionName) == 0) {
849 found = true;
850 break;
851 }
852 }
853 if (!found)
854 return vk_error(VK_ERROR_EXTENSION_NOT_PRESENT);
855 }
856
857 device = vk_alloc2(&physical_device->instance->alloc, pAllocator,
858 sizeof(*device), 8,
859 VK_SYSTEM_ALLOCATION_SCOPE_DEVICE);
860 if (!device)
861 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
862
863 device->_loader_data.loaderMagic = ICD_LOADER_MAGIC;
864 device->instance = physical_device->instance;
865 device->chipset_id = physical_device->chipset_id;
866
867 if (pAllocator)
868 device->alloc = *pAllocator;
869 else
870 device->alloc = physical_device->instance->alloc;
871
872 /* XXX(chadv): Can we dup() physicalDevice->fd here? */
873 device->fd = open(physical_device->path, O_RDWR | O_CLOEXEC);
874 if (device->fd == -1) {
875 result = vk_error(VK_ERROR_INITIALIZATION_FAILED);
876 goto fail_device;
877 }
878
879 device->context_id = anv_gem_create_context(device);
880 if (device->context_id == -1) {
881 result = vk_error(VK_ERROR_INITIALIZATION_FAILED);
882 goto fail_fd;
883 }
884
885 device->info = physical_device->info;
886 device->isl_dev = physical_device->isl_dev;
887
888 /* On Broadwell and later, we can use batch chaining to more efficiently
889 * implement growing command buffers. Prior to Haswell, the kernel
890 * command parser gets in the way and we have to fall back to growing
891 * the batch.
892 */
893 device->can_chain_batches = device->info.gen >= 8;
894
895 device->robust_buffer_access = pCreateInfo->pEnabledFeatures &&
896 pCreateInfo->pEnabledFeatures->robustBufferAccess;
897
898 pthread_mutex_init(&device->mutex, NULL);
899
900 pthread_condattr_t condattr;
901 pthread_condattr_init(&condattr);
902 pthread_condattr_setclock(&condattr, CLOCK_MONOTONIC);
903 pthread_cond_init(&device->queue_submit, NULL);
904 pthread_condattr_destroy(&condattr);
905
906 anv_bo_pool_init(&device->batch_bo_pool, device);
907
908 anv_block_pool_init(&device->dynamic_state_block_pool, device, 16384);
909
910 anv_state_pool_init(&device->dynamic_state_pool,
911 &device->dynamic_state_block_pool);
912
913 anv_block_pool_init(&device->instruction_block_pool, device, 128 * 1024);
914 anv_state_pool_init(&device->instruction_state_pool,
915 &device->instruction_block_pool);
916
917 anv_block_pool_init(&device->surface_state_block_pool, device, 4096);
918
919 anv_state_pool_init(&device->surface_state_pool,
920 &device->surface_state_block_pool);
921
922 anv_bo_init_new(&device->workaround_bo, device, 1024);
923
924 anv_scratch_pool_init(device, &device->scratch_pool);
925
926 anv_queue_init(device, &device->queue);
927
928 switch (device->info.gen) {
929 case 7:
930 if (!device->info.is_haswell)
931 result = gen7_init_device_state(device);
932 else
933 result = gen75_init_device_state(device);
934 break;
935 case 8:
936 result = gen8_init_device_state(device);
937 break;
938 case 9:
939 result = gen9_init_device_state(device);
940 break;
941 default:
942 /* Shouldn't get here as we don't create physical devices for any other
943 * gens. */
944 unreachable("unhandled gen");
945 }
946 if (result != VK_SUCCESS)
947 goto fail_fd;
948
949 anv_device_init_blorp(device);
950
951 anv_device_init_border_colors(device);
952
953 *pDevice = anv_device_to_handle(device);
954
955 return VK_SUCCESS;
956
957 fail_fd:
958 close(device->fd);
959 fail_device:
960 vk_free(&device->alloc, device);
961
962 return result;
963 }
964
965 void anv_DestroyDevice(
966 VkDevice _device,
967 const VkAllocationCallbacks* pAllocator)
968 {
969 ANV_FROM_HANDLE(anv_device, device, _device);
970
971 anv_queue_finish(&device->queue);
972
973 anv_device_finish_blorp(device);
974
975 #ifdef HAVE_VALGRIND
976 /* We only need to free these to prevent valgrind errors. The backing
977 * BO will go away in a couple of lines so we don't actually leak.
978 */
979 anv_state_pool_free(&device->dynamic_state_pool, device->border_colors);
980 #endif
981
982 anv_gem_munmap(device->workaround_bo.map, device->workaround_bo.size);
983 anv_gem_close(device, device->workaround_bo.gem_handle);
984
985 anv_bo_pool_finish(&device->batch_bo_pool);
986 anv_state_pool_finish(&device->dynamic_state_pool);
987 anv_block_pool_finish(&device->dynamic_state_block_pool);
988 anv_state_pool_finish(&device->instruction_state_pool);
989 anv_block_pool_finish(&device->instruction_block_pool);
990 anv_state_pool_finish(&device->surface_state_pool);
991 anv_block_pool_finish(&device->surface_state_block_pool);
992 anv_scratch_pool_finish(device, &device->scratch_pool);
993
994 close(device->fd);
995
996 pthread_mutex_destroy(&device->mutex);
997
998 vk_free(&device->alloc, device);
999 }
1000
1001 VkResult anv_EnumerateInstanceExtensionProperties(
1002 const char* pLayerName,
1003 uint32_t* pPropertyCount,
1004 VkExtensionProperties* pProperties)
1005 {
1006 if (pProperties == NULL) {
1007 *pPropertyCount = ARRAY_SIZE(global_extensions);
1008 return VK_SUCCESS;
1009 }
1010
1011 assert(*pPropertyCount >= ARRAY_SIZE(global_extensions));
1012
1013 *pPropertyCount = ARRAY_SIZE(global_extensions);
1014 memcpy(pProperties, global_extensions, sizeof(global_extensions));
1015
1016 return VK_SUCCESS;
1017 }
1018
1019 VkResult anv_EnumerateDeviceExtensionProperties(
1020 VkPhysicalDevice physicalDevice,
1021 const char* pLayerName,
1022 uint32_t* pPropertyCount,
1023 VkExtensionProperties* pProperties)
1024 {
1025 if (pProperties == NULL) {
1026 *pPropertyCount = ARRAY_SIZE(device_extensions);
1027 return VK_SUCCESS;
1028 }
1029
1030 assert(*pPropertyCount >= ARRAY_SIZE(device_extensions));
1031
1032 *pPropertyCount = ARRAY_SIZE(device_extensions);
1033 memcpy(pProperties, device_extensions, sizeof(device_extensions));
1034
1035 return VK_SUCCESS;
1036 }
1037
1038 VkResult anv_EnumerateInstanceLayerProperties(
1039 uint32_t* pPropertyCount,
1040 VkLayerProperties* pProperties)
1041 {
1042 if (pProperties == NULL) {
1043 *pPropertyCount = 0;
1044 return VK_SUCCESS;
1045 }
1046
1047 /* None supported at this time */
1048 return vk_error(VK_ERROR_LAYER_NOT_PRESENT);
1049 }
1050
1051 VkResult anv_EnumerateDeviceLayerProperties(
1052 VkPhysicalDevice physicalDevice,
1053 uint32_t* pPropertyCount,
1054 VkLayerProperties* pProperties)
1055 {
1056 if (pProperties == NULL) {
1057 *pPropertyCount = 0;
1058 return VK_SUCCESS;
1059 }
1060
1061 /* None supported at this time */
1062 return vk_error(VK_ERROR_LAYER_NOT_PRESENT);
1063 }
1064
1065 void anv_GetDeviceQueue(
1066 VkDevice _device,
1067 uint32_t queueNodeIndex,
1068 uint32_t queueIndex,
1069 VkQueue* pQueue)
1070 {
1071 ANV_FROM_HANDLE(anv_device, device, _device);
1072
1073 assert(queueIndex == 0);
1074
1075 *pQueue = anv_queue_to_handle(&device->queue);
1076 }
1077
1078 VkResult
1079 anv_device_execbuf(struct anv_device *device,
1080 struct drm_i915_gem_execbuffer2 *execbuf,
1081 struct anv_bo **execbuf_bos)
1082 {
1083 int ret = anv_gem_execbuffer(device, execbuf);
1084 if (ret != 0) {
1085 /* We don't know the real error. */
1086 return vk_errorf(VK_ERROR_DEVICE_LOST, "execbuf2 failed: %m");
1087 }
1088
1089 struct drm_i915_gem_exec_object2 *objects =
1090 (void *)(uintptr_t)execbuf->buffers_ptr;
1091 for (uint32_t k = 0; k < execbuf->buffer_count; k++)
1092 execbuf_bos[k]->offset = objects[k].offset;
1093
1094 return VK_SUCCESS;
1095 }
1096
1097 VkResult anv_QueueSubmit(
1098 VkQueue _queue,
1099 uint32_t submitCount,
1100 const VkSubmitInfo* pSubmits,
1101 VkFence _fence)
1102 {
1103 ANV_FROM_HANDLE(anv_queue, queue, _queue);
1104 ANV_FROM_HANDLE(anv_fence, fence, _fence);
1105 struct anv_device *device = queue->device;
1106 VkResult result = VK_SUCCESS;
1107
1108 /* We lock around QueueSubmit for three main reasons:
1109 *
1110 * 1) When a block pool is resized, we create a new gem handle with a
1111 * different size and, in the case of surface states, possibly a
1112 * different center offset but we re-use the same anv_bo struct when
1113 * we do so. If this happens in the middle of setting up an execbuf,
1114 * we could end up with our list of BOs out of sync with our list of
1115 * gem handles.
1116 *
1117 * 2) The algorithm we use for building the list of unique buffers isn't
1118 * thread-safe. While the client is supposed to syncronize around
1119 * QueueSubmit, this would be extremely difficult to debug if it ever
1120 * came up in the wild due to a broken app. It's better to play it
1121 * safe and just lock around QueueSubmit.
1122 *
1123 * 3) The anv_cmd_buffer_execbuf function may perform relocations in
1124 * userspace. Due to the fact that the surface state buffer is shared
1125 * between batches, we can't afford to have that happen from multiple
1126 * threads at the same time. Even though the user is supposed to
1127 * ensure this doesn't happen, we play it safe as in (2) above.
1128 *
1129 * Since the only other things that ever take the device lock such as block
1130 * pool resize only rarely happen, this will almost never be contended so
1131 * taking a lock isn't really an expensive operation in this case.
1132 */
1133 pthread_mutex_lock(&device->mutex);
1134
1135 for (uint32_t i = 0; i < submitCount; i++) {
1136 for (uint32_t j = 0; j < pSubmits[i].commandBufferCount; j++) {
1137 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer,
1138 pSubmits[i].pCommandBuffers[j]);
1139 assert(cmd_buffer->level == VK_COMMAND_BUFFER_LEVEL_PRIMARY);
1140
1141 result = anv_cmd_buffer_execbuf(device, cmd_buffer);
1142 if (result != VK_SUCCESS)
1143 goto out;
1144 }
1145 }
1146
1147 if (fence) {
1148 struct anv_bo *fence_bo = &fence->bo;
1149 result = anv_device_execbuf(device, &fence->execbuf, &fence_bo);
1150 if (result != VK_SUCCESS)
1151 goto out;
1152
1153 /* Update the fence and wake up any waiters */
1154 assert(fence->state == ANV_FENCE_STATE_RESET);
1155 fence->state = ANV_FENCE_STATE_SUBMITTED;
1156 pthread_cond_broadcast(&device->queue_submit);
1157 }
1158
1159 out:
1160 pthread_mutex_unlock(&device->mutex);
1161
1162 return result;
1163 }
1164
1165 VkResult anv_QueueWaitIdle(
1166 VkQueue _queue)
1167 {
1168 ANV_FROM_HANDLE(anv_queue, queue, _queue);
1169
1170 return anv_DeviceWaitIdle(anv_device_to_handle(queue->device));
1171 }
1172
1173 VkResult anv_DeviceWaitIdle(
1174 VkDevice _device)
1175 {
1176 ANV_FROM_HANDLE(anv_device, device, _device);
1177 struct anv_batch batch;
1178
1179 uint32_t cmds[8];
1180 batch.start = batch.next = cmds;
1181 batch.end = (void *) cmds + sizeof(cmds);
1182
1183 anv_batch_emit(&batch, GEN7_MI_BATCH_BUFFER_END, bbe);
1184 anv_batch_emit(&batch, GEN7_MI_NOOP, noop);
1185
1186 return anv_device_submit_simple_batch(device, &batch);
1187 }
1188
1189 VkResult
1190 anv_bo_init_new(struct anv_bo *bo, struct anv_device *device, uint64_t size)
1191 {
1192 uint32_t gem_handle = anv_gem_create(device, size);
1193 if (!gem_handle)
1194 return vk_error(VK_ERROR_OUT_OF_DEVICE_MEMORY);
1195
1196 anv_bo_init(bo, gem_handle, size);
1197
1198 return VK_SUCCESS;
1199 }
1200
1201 VkResult anv_AllocateMemory(
1202 VkDevice _device,
1203 const VkMemoryAllocateInfo* pAllocateInfo,
1204 const VkAllocationCallbacks* pAllocator,
1205 VkDeviceMemory* pMem)
1206 {
1207 ANV_FROM_HANDLE(anv_device, device, _device);
1208 struct anv_device_memory *mem;
1209 VkResult result;
1210
1211 assert(pAllocateInfo->sType == VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO);
1212
1213 if (pAllocateInfo->allocationSize == 0) {
1214 /* Apparently, this is allowed */
1215 *pMem = VK_NULL_HANDLE;
1216 return VK_SUCCESS;
1217 }
1218
1219 /* We support exactly one memory heap. */
1220 assert(pAllocateInfo->memoryTypeIndex == 0 ||
1221 (!device->info.has_llc && pAllocateInfo->memoryTypeIndex < 2));
1222
1223 /* FINISHME: Fail if allocation request exceeds heap size. */
1224
1225 mem = vk_alloc2(&device->alloc, pAllocator, sizeof(*mem), 8,
1226 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
1227 if (mem == NULL)
1228 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
1229
1230 /* The kernel is going to give us whole pages anyway */
1231 uint64_t alloc_size = align_u64(pAllocateInfo->allocationSize, 4096);
1232
1233 result = anv_bo_init_new(&mem->bo, device, alloc_size);
1234 if (result != VK_SUCCESS)
1235 goto fail;
1236
1237 mem->type_index = pAllocateInfo->memoryTypeIndex;
1238
1239 mem->map = NULL;
1240 mem->map_size = 0;
1241
1242 *pMem = anv_device_memory_to_handle(mem);
1243
1244 return VK_SUCCESS;
1245
1246 fail:
1247 vk_free2(&device->alloc, pAllocator, mem);
1248
1249 return result;
1250 }
1251
1252 void anv_FreeMemory(
1253 VkDevice _device,
1254 VkDeviceMemory _mem,
1255 const VkAllocationCallbacks* pAllocator)
1256 {
1257 ANV_FROM_HANDLE(anv_device, device, _device);
1258 ANV_FROM_HANDLE(anv_device_memory, mem, _mem);
1259
1260 if (mem == NULL)
1261 return;
1262
1263 if (mem->map)
1264 anv_UnmapMemory(_device, _mem);
1265
1266 if (mem->bo.map)
1267 anv_gem_munmap(mem->bo.map, mem->bo.size);
1268
1269 if (mem->bo.gem_handle != 0)
1270 anv_gem_close(device, mem->bo.gem_handle);
1271
1272 vk_free2(&device->alloc, pAllocator, mem);
1273 }
1274
1275 VkResult anv_MapMemory(
1276 VkDevice _device,
1277 VkDeviceMemory _memory,
1278 VkDeviceSize offset,
1279 VkDeviceSize size,
1280 VkMemoryMapFlags flags,
1281 void** ppData)
1282 {
1283 ANV_FROM_HANDLE(anv_device, device, _device);
1284 ANV_FROM_HANDLE(anv_device_memory, mem, _memory);
1285
1286 if (mem == NULL) {
1287 *ppData = NULL;
1288 return VK_SUCCESS;
1289 }
1290
1291 if (size == VK_WHOLE_SIZE)
1292 size = mem->bo.size - offset;
1293
1294 /* From the Vulkan spec version 1.0.32 docs for MapMemory:
1295 *
1296 * * If size is not equal to VK_WHOLE_SIZE, size must be greater than 0
1297 * assert(size != 0);
1298 * * If size is not equal to VK_WHOLE_SIZE, size must be less than or
1299 * equal to the size of the memory minus offset
1300 */
1301 assert(size > 0);
1302 assert(offset + size <= mem->bo.size);
1303
1304 /* FIXME: Is this supposed to be thread safe? Since vkUnmapMemory() only
1305 * takes a VkDeviceMemory pointer, it seems like only one map of the memory
1306 * at a time is valid. We could just mmap up front and return an offset
1307 * pointer here, but that may exhaust virtual memory on 32 bit
1308 * userspace. */
1309
1310 uint32_t gem_flags = 0;
1311 if (!device->info.has_llc && mem->type_index == 0)
1312 gem_flags |= I915_MMAP_WC;
1313
1314 /* GEM will fail to map if the offset isn't 4k-aligned. Round down. */
1315 uint64_t map_offset = offset & ~4095ull;
1316 assert(offset >= map_offset);
1317 uint64_t map_size = (offset + size) - map_offset;
1318
1319 /* Let's map whole pages */
1320 map_size = align_u64(map_size, 4096);
1321
1322 void *map = anv_gem_mmap(device, mem->bo.gem_handle,
1323 map_offset, map_size, gem_flags);
1324 if (map == MAP_FAILED)
1325 return vk_error(VK_ERROR_MEMORY_MAP_FAILED);
1326
1327 mem->map = map;
1328 mem->map_size = map_size;
1329
1330 *ppData = mem->map + (offset - map_offset);
1331
1332 return VK_SUCCESS;
1333 }
1334
1335 void anv_UnmapMemory(
1336 VkDevice _device,
1337 VkDeviceMemory _memory)
1338 {
1339 ANV_FROM_HANDLE(anv_device_memory, mem, _memory);
1340
1341 if (mem == NULL)
1342 return;
1343
1344 anv_gem_munmap(mem->map, mem->map_size);
1345
1346 mem->map = NULL;
1347 mem->map_size = 0;
1348 }
1349
1350 static void
1351 clflush_mapped_ranges(struct anv_device *device,
1352 uint32_t count,
1353 const VkMappedMemoryRange *ranges)
1354 {
1355 for (uint32_t i = 0; i < count; i++) {
1356 ANV_FROM_HANDLE(anv_device_memory, mem, ranges[i].memory);
1357 void *p = mem->map + (ranges[i].offset & ~CACHELINE_MASK);
1358 void *end;
1359
1360 if (ranges[i].offset + ranges[i].size > mem->map_size)
1361 end = mem->map + mem->map_size;
1362 else
1363 end = mem->map + ranges[i].offset + ranges[i].size;
1364
1365 while (p < end) {
1366 __builtin_ia32_clflush(p);
1367 p += CACHELINE_SIZE;
1368 }
1369 }
1370 }
1371
1372 VkResult anv_FlushMappedMemoryRanges(
1373 VkDevice _device,
1374 uint32_t memoryRangeCount,
1375 const VkMappedMemoryRange* pMemoryRanges)
1376 {
1377 ANV_FROM_HANDLE(anv_device, device, _device);
1378
1379 if (device->info.has_llc)
1380 return VK_SUCCESS;
1381
1382 /* Make sure the writes we're flushing have landed. */
1383 __builtin_ia32_mfence();
1384
1385 clflush_mapped_ranges(device, memoryRangeCount, pMemoryRanges);
1386
1387 return VK_SUCCESS;
1388 }
1389
1390 VkResult anv_InvalidateMappedMemoryRanges(
1391 VkDevice _device,
1392 uint32_t memoryRangeCount,
1393 const VkMappedMemoryRange* pMemoryRanges)
1394 {
1395 ANV_FROM_HANDLE(anv_device, device, _device);
1396
1397 if (device->info.has_llc)
1398 return VK_SUCCESS;
1399
1400 clflush_mapped_ranges(device, memoryRangeCount, pMemoryRanges);
1401
1402 /* Make sure no reads get moved up above the invalidate. */
1403 __builtin_ia32_mfence();
1404
1405 return VK_SUCCESS;
1406 }
1407
1408 void anv_GetBufferMemoryRequirements(
1409 VkDevice device,
1410 VkBuffer _buffer,
1411 VkMemoryRequirements* pMemoryRequirements)
1412 {
1413 ANV_FROM_HANDLE(anv_buffer, buffer, _buffer);
1414
1415 /* The Vulkan spec (git aaed022) says:
1416 *
1417 * memoryTypeBits is a bitfield and contains one bit set for every
1418 * supported memory type for the resource. The bit `1<<i` is set if and
1419 * only if the memory type `i` in the VkPhysicalDeviceMemoryProperties
1420 * structure for the physical device is supported.
1421 *
1422 * We support exactly one memory type.
1423 */
1424 pMemoryRequirements->memoryTypeBits = 1;
1425
1426 pMemoryRequirements->size = buffer->size;
1427 pMemoryRequirements->alignment = 16;
1428 }
1429
1430 void anv_GetImageMemoryRequirements(
1431 VkDevice device,
1432 VkImage _image,
1433 VkMemoryRequirements* pMemoryRequirements)
1434 {
1435 ANV_FROM_HANDLE(anv_image, image, _image);
1436
1437 /* The Vulkan spec (git aaed022) says:
1438 *
1439 * memoryTypeBits is a bitfield and contains one bit set for every
1440 * supported memory type for the resource. The bit `1<<i` is set if and
1441 * only if the memory type `i` in the VkPhysicalDeviceMemoryProperties
1442 * structure for the physical device is supported.
1443 *
1444 * We support exactly one memory type.
1445 */
1446 pMemoryRequirements->memoryTypeBits = 1;
1447
1448 pMemoryRequirements->size = image->size;
1449 pMemoryRequirements->alignment = image->alignment;
1450 }
1451
1452 void anv_GetImageSparseMemoryRequirements(
1453 VkDevice device,
1454 VkImage image,
1455 uint32_t* pSparseMemoryRequirementCount,
1456 VkSparseImageMemoryRequirements* pSparseMemoryRequirements)
1457 {
1458 stub();
1459 }
1460
1461 void anv_GetDeviceMemoryCommitment(
1462 VkDevice device,
1463 VkDeviceMemory memory,
1464 VkDeviceSize* pCommittedMemoryInBytes)
1465 {
1466 *pCommittedMemoryInBytes = 0;
1467 }
1468
1469 VkResult anv_BindBufferMemory(
1470 VkDevice device,
1471 VkBuffer _buffer,
1472 VkDeviceMemory _memory,
1473 VkDeviceSize memoryOffset)
1474 {
1475 ANV_FROM_HANDLE(anv_device_memory, mem, _memory);
1476 ANV_FROM_HANDLE(anv_buffer, buffer, _buffer);
1477
1478 if (mem) {
1479 buffer->bo = &mem->bo;
1480 buffer->offset = memoryOffset;
1481 } else {
1482 buffer->bo = NULL;
1483 buffer->offset = 0;
1484 }
1485
1486 return VK_SUCCESS;
1487 }
1488
1489 VkResult anv_QueueBindSparse(
1490 VkQueue queue,
1491 uint32_t bindInfoCount,
1492 const VkBindSparseInfo* pBindInfo,
1493 VkFence fence)
1494 {
1495 stub_return(VK_ERROR_INCOMPATIBLE_DRIVER);
1496 }
1497
1498 VkResult anv_CreateFence(
1499 VkDevice _device,
1500 const VkFenceCreateInfo* pCreateInfo,
1501 const VkAllocationCallbacks* pAllocator,
1502 VkFence* pFence)
1503 {
1504 ANV_FROM_HANDLE(anv_device, device, _device);
1505 struct anv_bo fence_bo;
1506 struct anv_fence *fence;
1507 struct anv_batch batch;
1508 VkResult result;
1509
1510 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_FENCE_CREATE_INFO);
1511
1512 result = anv_bo_pool_alloc(&device->batch_bo_pool, &fence_bo, 4096);
1513 if (result != VK_SUCCESS)
1514 return result;
1515
1516 /* Fences are small. Just store the CPU data structure in the BO. */
1517 fence = fence_bo.map;
1518 fence->bo = fence_bo;
1519
1520 /* Place the batch after the CPU data but on its own cache line. */
1521 const uint32_t batch_offset = align_u32(sizeof(*fence), CACHELINE_SIZE);
1522 batch.next = batch.start = fence->bo.map + batch_offset;
1523 batch.end = fence->bo.map + fence->bo.size;
1524 anv_batch_emit(&batch, GEN7_MI_BATCH_BUFFER_END, bbe);
1525 anv_batch_emit(&batch, GEN7_MI_NOOP, noop);
1526
1527 if (!device->info.has_llc) {
1528 assert(((uintptr_t) batch.start & CACHELINE_MASK) == 0);
1529 assert(batch.next - batch.start <= CACHELINE_SIZE);
1530 __builtin_ia32_mfence();
1531 __builtin_ia32_clflush(batch.start);
1532 }
1533
1534 fence->exec2_objects[0].handle = fence->bo.gem_handle;
1535 fence->exec2_objects[0].relocation_count = 0;
1536 fence->exec2_objects[0].relocs_ptr = 0;
1537 fence->exec2_objects[0].alignment = 0;
1538 fence->exec2_objects[0].offset = fence->bo.offset;
1539 fence->exec2_objects[0].flags = 0;
1540 fence->exec2_objects[0].rsvd1 = 0;
1541 fence->exec2_objects[0].rsvd2 = 0;
1542
1543 fence->execbuf.buffers_ptr = (uintptr_t) fence->exec2_objects;
1544 fence->execbuf.buffer_count = 1;
1545 fence->execbuf.batch_start_offset = batch.start - fence->bo.map;
1546 fence->execbuf.batch_len = batch.next - batch.start;
1547 fence->execbuf.cliprects_ptr = 0;
1548 fence->execbuf.num_cliprects = 0;
1549 fence->execbuf.DR1 = 0;
1550 fence->execbuf.DR4 = 0;
1551
1552 fence->execbuf.flags =
1553 I915_EXEC_HANDLE_LUT | I915_EXEC_NO_RELOC | I915_EXEC_RENDER;
1554 fence->execbuf.rsvd1 = device->context_id;
1555 fence->execbuf.rsvd2 = 0;
1556
1557 if (pCreateInfo->flags & VK_FENCE_CREATE_SIGNALED_BIT) {
1558 fence->state = ANV_FENCE_STATE_SIGNALED;
1559 } else {
1560 fence->state = ANV_FENCE_STATE_RESET;
1561 }
1562
1563 *pFence = anv_fence_to_handle(fence);
1564
1565 return VK_SUCCESS;
1566 }
1567
1568 void anv_DestroyFence(
1569 VkDevice _device,
1570 VkFence _fence,
1571 const VkAllocationCallbacks* pAllocator)
1572 {
1573 ANV_FROM_HANDLE(anv_device, device, _device);
1574 ANV_FROM_HANDLE(anv_fence, fence, _fence);
1575
1576 if (!fence)
1577 return;
1578
1579 assert(fence->bo.map == fence);
1580 anv_bo_pool_free(&device->batch_bo_pool, &fence->bo);
1581 }
1582
1583 VkResult anv_ResetFences(
1584 VkDevice _device,
1585 uint32_t fenceCount,
1586 const VkFence* pFences)
1587 {
1588 for (uint32_t i = 0; i < fenceCount; i++) {
1589 ANV_FROM_HANDLE(anv_fence, fence, pFences[i]);
1590 fence->state = ANV_FENCE_STATE_RESET;
1591 }
1592
1593 return VK_SUCCESS;
1594 }
1595
1596 VkResult anv_GetFenceStatus(
1597 VkDevice _device,
1598 VkFence _fence)
1599 {
1600 ANV_FROM_HANDLE(anv_device, device, _device);
1601 ANV_FROM_HANDLE(anv_fence, fence, _fence);
1602 int64_t t = 0;
1603 int ret;
1604
1605 switch (fence->state) {
1606 case ANV_FENCE_STATE_RESET:
1607 /* If it hasn't even been sent off to the GPU yet, it's not ready */
1608 return VK_NOT_READY;
1609
1610 case ANV_FENCE_STATE_SIGNALED:
1611 /* It's been signaled, return success */
1612 return VK_SUCCESS;
1613
1614 case ANV_FENCE_STATE_SUBMITTED:
1615 /* It's been submitted to the GPU but we don't know if it's done yet. */
1616 ret = anv_gem_wait(device, fence->bo.gem_handle, &t);
1617 if (ret == 0) {
1618 fence->state = ANV_FENCE_STATE_SIGNALED;
1619 return VK_SUCCESS;
1620 } else {
1621 return VK_NOT_READY;
1622 }
1623 default:
1624 unreachable("Invalid fence status");
1625 }
1626 }
1627
1628 #define NSEC_PER_SEC 1000000000
1629 #define INT_TYPE_MAX(type) ((1ull << (sizeof(type) * 8 - 1)) - 1)
1630
1631 VkResult anv_WaitForFences(
1632 VkDevice _device,
1633 uint32_t fenceCount,
1634 const VkFence* pFences,
1635 VkBool32 waitAll,
1636 uint64_t _timeout)
1637 {
1638 ANV_FROM_HANDLE(anv_device, device, _device);
1639 int ret;
1640
1641 /* DRM_IOCTL_I915_GEM_WAIT uses a signed 64 bit timeout and is supposed
1642 * to block indefinitely timeouts <= 0. Unfortunately, this was broken
1643 * for a couple of kernel releases. Since there's no way to know
1644 * whether or not the kernel we're using is one of the broken ones, the
1645 * best we can do is to clamp the timeout to INT64_MAX. This limits the
1646 * maximum timeout from 584 years to 292 years - likely not a big deal.
1647 */
1648 int64_t timeout = MIN2(_timeout, INT64_MAX);
1649
1650 uint32_t pending_fences = fenceCount;
1651 while (pending_fences) {
1652 pending_fences = 0;
1653 bool signaled_fences = false;
1654 for (uint32_t i = 0; i < fenceCount; i++) {
1655 ANV_FROM_HANDLE(anv_fence, fence, pFences[i]);
1656 switch (fence->state) {
1657 case ANV_FENCE_STATE_RESET:
1658 /* This fence hasn't been submitted yet, we'll catch it the next
1659 * time around. Yes, this may mean we dead-loop but, short of
1660 * lots of locking and a condition variable, there's not much that
1661 * we can do about that.
1662 */
1663 pending_fences++;
1664 continue;
1665
1666 case ANV_FENCE_STATE_SIGNALED:
1667 /* This fence is not pending. If waitAll isn't set, we can return
1668 * early. Otherwise, we have to keep going.
1669 */
1670 if (!waitAll)
1671 return VK_SUCCESS;
1672 continue;
1673
1674 case ANV_FENCE_STATE_SUBMITTED:
1675 /* These are the fences we really care about. Go ahead and wait
1676 * on it until we hit a timeout.
1677 */
1678 ret = anv_gem_wait(device, fence->bo.gem_handle, &timeout);
1679 if (ret == -1 && errno == ETIME) {
1680 return VK_TIMEOUT;
1681 } else if (ret == -1) {
1682 /* We don't know the real error. */
1683 return vk_errorf(VK_ERROR_DEVICE_LOST, "gem wait failed: %m");
1684 } else {
1685 fence->state = ANV_FENCE_STATE_SIGNALED;
1686 signaled_fences = true;
1687 if (!waitAll)
1688 return VK_SUCCESS;
1689 continue;
1690 }
1691 }
1692 }
1693
1694 if (pending_fences && !signaled_fences) {
1695 /* If we've hit this then someone decided to vkWaitForFences before
1696 * they've actually submitted any of them to a queue. This is a
1697 * fairly pessimal case, so it's ok to lock here and use a standard
1698 * pthreads condition variable.
1699 */
1700 pthread_mutex_lock(&device->mutex);
1701
1702 /* It's possible that some of the fences have changed state since the
1703 * last time we checked. Now that we have the lock, check for
1704 * pending fences again and don't wait if it's changed.
1705 */
1706 uint32_t now_pending_fences = 0;
1707 for (uint32_t i = 0; i < fenceCount; i++) {
1708 ANV_FROM_HANDLE(anv_fence, fence, pFences[i]);
1709 if (fence->state == ANV_FENCE_STATE_RESET)
1710 now_pending_fences++;
1711 }
1712 assert(now_pending_fences <= pending_fences);
1713
1714 if (now_pending_fences == pending_fences) {
1715 struct timespec before;
1716 clock_gettime(CLOCK_MONOTONIC, &before);
1717
1718 uint32_t abs_nsec = before.tv_nsec + timeout % NSEC_PER_SEC;
1719 uint64_t abs_sec = before.tv_sec + (abs_nsec / NSEC_PER_SEC) +
1720 (timeout / NSEC_PER_SEC);
1721 abs_nsec %= NSEC_PER_SEC;
1722
1723 /* Avoid roll-over in tv_sec on 32-bit systems if the user
1724 * provided timeout is UINT64_MAX
1725 */
1726 struct timespec abstime;
1727 abstime.tv_nsec = abs_nsec;
1728 abstime.tv_sec = MIN2(abs_sec, INT_TYPE_MAX(abstime.tv_sec));
1729
1730 ret = pthread_cond_timedwait(&device->queue_submit,
1731 &device->mutex, &abstime);
1732 assert(ret != EINVAL);
1733
1734 struct timespec after;
1735 clock_gettime(CLOCK_MONOTONIC, &after);
1736 uint64_t time_elapsed =
1737 ((uint64_t)after.tv_sec * NSEC_PER_SEC + after.tv_nsec) -
1738 ((uint64_t)before.tv_sec * NSEC_PER_SEC + before.tv_nsec);
1739
1740 if (time_elapsed >= timeout) {
1741 pthread_mutex_unlock(&device->mutex);
1742 return VK_TIMEOUT;
1743 }
1744
1745 timeout -= time_elapsed;
1746 }
1747
1748 pthread_mutex_unlock(&device->mutex);
1749 }
1750 }
1751
1752 return VK_SUCCESS;
1753 }
1754
1755 // Queue semaphore functions
1756
1757 VkResult anv_CreateSemaphore(
1758 VkDevice device,
1759 const VkSemaphoreCreateInfo* pCreateInfo,
1760 const VkAllocationCallbacks* pAllocator,
1761 VkSemaphore* pSemaphore)
1762 {
1763 /* The DRM execbuffer ioctl always execute in-oder, even between different
1764 * rings. As such, there's nothing to do for the user space semaphore.
1765 */
1766
1767 *pSemaphore = (VkSemaphore)1;
1768
1769 return VK_SUCCESS;
1770 }
1771
1772 void anv_DestroySemaphore(
1773 VkDevice device,
1774 VkSemaphore semaphore,
1775 const VkAllocationCallbacks* pAllocator)
1776 {
1777 }
1778
1779 // Event functions
1780
1781 VkResult anv_CreateEvent(
1782 VkDevice _device,
1783 const VkEventCreateInfo* pCreateInfo,
1784 const VkAllocationCallbacks* pAllocator,
1785 VkEvent* pEvent)
1786 {
1787 ANV_FROM_HANDLE(anv_device, device, _device);
1788 struct anv_state state;
1789 struct anv_event *event;
1790
1791 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_EVENT_CREATE_INFO);
1792
1793 state = anv_state_pool_alloc(&device->dynamic_state_pool,
1794 sizeof(*event), 8);
1795 event = state.map;
1796 event->state = state;
1797 event->semaphore = VK_EVENT_RESET;
1798
1799 if (!device->info.has_llc) {
1800 /* Make sure the writes we're flushing have landed. */
1801 __builtin_ia32_mfence();
1802 __builtin_ia32_clflush(event);
1803 }
1804
1805 *pEvent = anv_event_to_handle(event);
1806
1807 return VK_SUCCESS;
1808 }
1809
1810 void anv_DestroyEvent(
1811 VkDevice _device,
1812 VkEvent _event,
1813 const VkAllocationCallbacks* pAllocator)
1814 {
1815 ANV_FROM_HANDLE(anv_device, device, _device);
1816 ANV_FROM_HANDLE(anv_event, event, _event);
1817
1818 if (!event)
1819 return;
1820
1821 anv_state_pool_free(&device->dynamic_state_pool, event->state);
1822 }
1823
1824 VkResult anv_GetEventStatus(
1825 VkDevice _device,
1826 VkEvent _event)
1827 {
1828 ANV_FROM_HANDLE(anv_device, device, _device);
1829 ANV_FROM_HANDLE(anv_event, event, _event);
1830
1831 if (!device->info.has_llc) {
1832 /* Invalidate read cache before reading event written by GPU. */
1833 __builtin_ia32_clflush(event);
1834 __builtin_ia32_mfence();
1835
1836 }
1837
1838 return event->semaphore;
1839 }
1840
1841 VkResult anv_SetEvent(
1842 VkDevice _device,
1843 VkEvent _event)
1844 {
1845 ANV_FROM_HANDLE(anv_device, device, _device);
1846 ANV_FROM_HANDLE(anv_event, event, _event);
1847
1848 event->semaphore = VK_EVENT_SET;
1849
1850 if (!device->info.has_llc) {
1851 /* Make sure the writes we're flushing have landed. */
1852 __builtin_ia32_mfence();
1853 __builtin_ia32_clflush(event);
1854 }
1855
1856 return VK_SUCCESS;
1857 }
1858
1859 VkResult anv_ResetEvent(
1860 VkDevice _device,
1861 VkEvent _event)
1862 {
1863 ANV_FROM_HANDLE(anv_device, device, _device);
1864 ANV_FROM_HANDLE(anv_event, event, _event);
1865
1866 event->semaphore = VK_EVENT_RESET;
1867
1868 if (!device->info.has_llc) {
1869 /* Make sure the writes we're flushing have landed. */
1870 __builtin_ia32_mfence();
1871 __builtin_ia32_clflush(event);
1872 }
1873
1874 return VK_SUCCESS;
1875 }
1876
1877 // Buffer functions
1878
1879 VkResult anv_CreateBuffer(
1880 VkDevice _device,
1881 const VkBufferCreateInfo* pCreateInfo,
1882 const VkAllocationCallbacks* pAllocator,
1883 VkBuffer* pBuffer)
1884 {
1885 ANV_FROM_HANDLE(anv_device, device, _device);
1886 struct anv_buffer *buffer;
1887
1888 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO);
1889
1890 buffer = vk_alloc2(&device->alloc, pAllocator, sizeof(*buffer), 8,
1891 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
1892 if (buffer == NULL)
1893 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
1894
1895 buffer->size = pCreateInfo->size;
1896 buffer->usage = pCreateInfo->usage;
1897 buffer->bo = NULL;
1898 buffer->offset = 0;
1899
1900 *pBuffer = anv_buffer_to_handle(buffer);
1901
1902 return VK_SUCCESS;
1903 }
1904
1905 void anv_DestroyBuffer(
1906 VkDevice _device,
1907 VkBuffer _buffer,
1908 const VkAllocationCallbacks* pAllocator)
1909 {
1910 ANV_FROM_HANDLE(anv_device, device, _device);
1911 ANV_FROM_HANDLE(anv_buffer, buffer, _buffer);
1912
1913 if (!buffer)
1914 return;
1915
1916 vk_free2(&device->alloc, pAllocator, buffer);
1917 }
1918
1919 void
1920 anv_fill_buffer_surface_state(struct anv_device *device, struct anv_state state,
1921 enum isl_format format,
1922 uint32_t offset, uint32_t range, uint32_t stride)
1923 {
1924 isl_buffer_fill_state(&device->isl_dev, state.map,
1925 .address = offset,
1926 .mocs = device->default_mocs,
1927 .size = range,
1928 .format = format,
1929 .stride = stride);
1930
1931 if (!device->info.has_llc)
1932 anv_state_clflush(state);
1933 }
1934
1935 void anv_DestroySampler(
1936 VkDevice _device,
1937 VkSampler _sampler,
1938 const VkAllocationCallbacks* pAllocator)
1939 {
1940 ANV_FROM_HANDLE(anv_device, device, _device);
1941 ANV_FROM_HANDLE(anv_sampler, sampler, _sampler);
1942
1943 if (!sampler)
1944 return;
1945
1946 vk_free2(&device->alloc, pAllocator, sampler);
1947 }
1948
1949 VkResult anv_CreateFramebuffer(
1950 VkDevice _device,
1951 const VkFramebufferCreateInfo* pCreateInfo,
1952 const VkAllocationCallbacks* pAllocator,
1953 VkFramebuffer* pFramebuffer)
1954 {
1955 ANV_FROM_HANDLE(anv_device, device, _device);
1956 struct anv_framebuffer *framebuffer;
1957
1958 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO);
1959
1960 size_t size = sizeof(*framebuffer) +
1961 sizeof(struct anv_image_view *) * pCreateInfo->attachmentCount;
1962 framebuffer = vk_alloc2(&device->alloc, pAllocator, size, 8,
1963 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
1964 if (framebuffer == NULL)
1965 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
1966
1967 framebuffer->attachment_count = pCreateInfo->attachmentCount;
1968 for (uint32_t i = 0; i < pCreateInfo->attachmentCount; i++) {
1969 VkImageView _iview = pCreateInfo->pAttachments[i];
1970 framebuffer->attachments[i] = anv_image_view_from_handle(_iview);
1971 }
1972
1973 framebuffer->width = pCreateInfo->width;
1974 framebuffer->height = pCreateInfo->height;
1975 framebuffer->layers = pCreateInfo->layers;
1976
1977 *pFramebuffer = anv_framebuffer_to_handle(framebuffer);
1978
1979 return VK_SUCCESS;
1980 }
1981
1982 void anv_DestroyFramebuffer(
1983 VkDevice _device,
1984 VkFramebuffer _fb,
1985 const VkAllocationCallbacks* pAllocator)
1986 {
1987 ANV_FROM_HANDLE(anv_device, device, _device);
1988 ANV_FROM_HANDLE(anv_framebuffer, fb, _fb);
1989
1990 if (!fb)
1991 return;
1992
1993 vk_free2(&device->alloc, pAllocator, fb);
1994 }