anv: Declare/validate the correct API version
[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 struct anv_dispatch_table dtable;
35
36 static VkResult
37 anv_physical_device_init(struct anv_physical_device *device,
38 struct anv_instance *instance,
39 const char *path)
40 {
41 VkResult result;
42 int fd;
43
44 fd = open(path, O_RDWR | O_CLOEXEC);
45 if (fd < 0)
46 return vk_errorf(VK_ERROR_INITIALIZATION_FAILED,
47 "failed to open %s: %m", path);
48
49 device->_loader_data.loaderMagic = ICD_LOADER_MAGIC;
50 device->instance = instance;
51 device->path = path;
52
53 device->chipset_id = anv_gem_get_param(fd, I915_PARAM_CHIPSET_ID);
54 if (!device->chipset_id) {
55 result = vk_errorf(VK_ERROR_INITIALIZATION_FAILED,
56 "failed to get chipset id: %m");
57 goto fail;
58 }
59
60 device->name = brw_get_device_name(device->chipset_id);
61 device->info = brw_get_device_info(device->chipset_id, -1);
62 if (!device->info) {
63 result = vk_errorf(VK_ERROR_INITIALIZATION_FAILED,
64 "failed to get device info");
65 goto fail;
66 }
67
68 if (anv_gem_get_aperture(fd, &device->aperture_size) == -1) {
69 result = vk_errorf(VK_ERROR_INITIALIZATION_FAILED,
70 "failed to get aperture size: %m");
71 goto fail;
72 }
73
74 if (!anv_gem_get_param(fd, I915_PARAM_HAS_WAIT_TIMEOUT)) {
75 result = vk_errorf(VK_ERROR_INITIALIZATION_FAILED,
76 "kernel missing gem wait");
77 goto fail;
78 }
79
80 if (!anv_gem_get_param(fd, I915_PARAM_HAS_EXECBUF2)) {
81 result = vk_errorf(VK_ERROR_INITIALIZATION_FAILED,
82 "kernel missing execbuf2");
83 goto fail;
84 }
85
86 if (!anv_gem_get_param(fd, I915_PARAM_HAS_LLC)) {
87 result = vk_errorf(VK_ERROR_INITIALIZATION_FAILED,
88 "non-llc gpu");
89 goto fail;
90 }
91
92 close(fd);
93
94 return VK_SUCCESS;
95
96 fail:
97 close(fd);
98 return result;
99 }
100
101 static void *default_alloc(
102 void* pUserData,
103 size_t size,
104 size_t alignment,
105 VkSystemAllocType allocType)
106 {
107 return malloc(size);
108 }
109
110 static void default_free(
111 void* pUserData,
112 void* pMem)
113 {
114 free(pMem);
115 }
116
117 static const VkAllocCallbacks default_alloc_callbacks = {
118 .pUserData = NULL,
119 .pfnAlloc = default_alloc,
120 .pfnFree = default_free
121 };
122
123 static const VkExtensionProperties global_extensions[] = {
124 {
125 .extName = VK_EXT_KHR_SWAPCHAIN_EXTENSION_NAME,
126 .specVersion = 17,
127 },
128 };
129
130 static const VkExtensionProperties device_extensions[] = {
131 {
132 .extName = VK_EXT_KHR_DEVICE_SWAPCHAIN_EXTENSION_NAME,
133 .specVersion = 53,
134 },
135 };
136
137
138 VkResult anv_CreateInstance(
139 const VkInstanceCreateInfo* pCreateInfo,
140 VkInstance* pInstance)
141 {
142 struct anv_instance *instance;
143 const VkAllocCallbacks *alloc_callbacks = &default_alloc_callbacks;
144 void *user_data = NULL;
145
146 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO);
147
148 if (pCreateInfo->pAppInfo->apiVersion != VK_MAKE_VERSION(0, 170, 2))
149 return vk_error(VK_ERROR_INCOMPATIBLE_DRIVER);
150
151 for (uint32_t i = 0; i < pCreateInfo->extensionCount; i++) {
152 bool found = false;
153 for (uint32_t j = 0; j < ARRAY_SIZE(global_extensions); j++) {
154 if (strcmp(pCreateInfo->ppEnabledExtensionNames[i],
155 global_extensions[j].extName) == 0) {
156 found = true;
157 break;
158 }
159 }
160 if (!found)
161 return vk_error(VK_ERROR_EXTENSION_NOT_PRESENT);
162 }
163
164 if (pCreateInfo->pAllocCb) {
165 alloc_callbacks = pCreateInfo->pAllocCb;
166 user_data = pCreateInfo->pAllocCb->pUserData;
167 }
168 instance = alloc_callbacks->pfnAlloc(user_data, sizeof(*instance), 8,
169 VK_SYSTEM_ALLOC_TYPE_API_OBJECT);
170 if (!instance)
171 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
172
173 instance->_loader_data.loaderMagic = ICD_LOADER_MAGIC;
174 instance->pAllocUserData = alloc_callbacks->pUserData;
175 instance->pfnAlloc = alloc_callbacks->pfnAlloc;
176 instance->pfnFree = alloc_callbacks->pfnFree;
177 instance->apiVersion = pCreateInfo->pAppInfo->apiVersion;
178 instance->physicalDeviceCount = 0;
179
180 _mesa_locale_init();
181
182 VG(VALGRIND_CREATE_MEMPOOL(instance, 0, false));
183
184 anv_init_wsi(instance);
185
186 *pInstance = anv_instance_to_handle(instance);
187
188 return VK_SUCCESS;
189 }
190
191 void anv_DestroyInstance(
192 VkInstance _instance)
193 {
194 ANV_FROM_HANDLE(anv_instance, instance, _instance);
195
196 anv_finish_wsi(instance);
197
198 VG(VALGRIND_DESTROY_MEMPOOL(instance));
199
200 _mesa_locale_fini();
201
202 instance->pfnFree(instance->pAllocUserData, instance);
203 }
204
205 void *
206 anv_instance_alloc(struct anv_instance *instance, size_t size,
207 size_t alignment, VkSystemAllocType allocType)
208 {
209 void *mem = instance->pfnAlloc(instance->pAllocUserData,
210 size, alignment, allocType);
211 if (mem) {
212 VG(VALGRIND_MEMPOOL_ALLOC(instance, mem, size));
213 VG(VALGRIND_MAKE_MEM_UNDEFINED(mem, size));
214 }
215 return mem;
216 }
217
218 void
219 anv_instance_free(struct anv_instance *instance, void *mem)
220 {
221 if (mem == NULL)
222 return;
223
224 VG(VALGRIND_MEMPOOL_FREE(instance, mem));
225
226 instance->pfnFree(instance->pAllocUserData, mem);
227 }
228
229 VkResult anv_EnumeratePhysicalDevices(
230 VkInstance _instance,
231 uint32_t* pPhysicalDeviceCount,
232 VkPhysicalDevice* pPhysicalDevices)
233 {
234 ANV_FROM_HANDLE(anv_instance, instance, _instance);
235 VkResult result;
236
237 if (instance->physicalDeviceCount == 0) {
238 result = anv_physical_device_init(&instance->physicalDevice,
239 instance, "/dev/dri/renderD128");
240 if (result != VK_SUCCESS)
241 return result;
242
243 instance->physicalDeviceCount = 1;
244 }
245
246 /* pPhysicalDeviceCount is an out parameter if pPhysicalDevices is NULL;
247 * otherwise it's an inout parameter.
248 *
249 * The Vulkan spec (git aaed022) says:
250 *
251 * pPhysicalDeviceCount is a pointer to an unsigned integer variable
252 * that is initialized with the number of devices the application is
253 * prepared to receive handles to. pname:pPhysicalDevices is pointer to
254 * an array of at least this many VkPhysicalDevice handles [...].
255 *
256 * Upon success, if pPhysicalDevices is NULL, vkEnumeratePhysicalDevices
257 * overwrites the contents of the variable pointed to by
258 * pPhysicalDeviceCount with the number of physical devices in in the
259 * instance; otherwise, vkEnumeratePhysicalDevices overwrites
260 * pPhysicalDeviceCount with the number of physical handles written to
261 * pPhysicalDevices.
262 */
263 if (!pPhysicalDevices) {
264 *pPhysicalDeviceCount = instance->physicalDeviceCount;
265 } else if (*pPhysicalDeviceCount >= 1) {
266 pPhysicalDevices[0] = anv_physical_device_to_handle(&instance->physicalDevice);
267 *pPhysicalDeviceCount = 1;
268 } else {
269 *pPhysicalDeviceCount = 0;
270 }
271
272 return VK_SUCCESS;
273 }
274
275 VkResult anv_GetPhysicalDeviceFeatures(
276 VkPhysicalDevice physicalDevice,
277 VkPhysicalDeviceFeatures* pFeatures)
278 {
279 anv_finishme("Get correct values for PhysicalDeviceFeatures");
280
281 *pFeatures = (VkPhysicalDeviceFeatures) {
282 .robustBufferAccess = false,
283 .fullDrawIndexUint32 = false,
284 .imageCubeArray = false,
285 .independentBlend = false,
286 .geometryShader = true,
287 .tessellationShader = false,
288 .sampleRateShading = false,
289 .dualSourceBlend = true,
290 .logicOp = true,
291 .multiDrawIndirect = true,
292 .depthClip = false,
293 .depthBiasClamp = false,
294 .fillModeNonSolid = true,
295 .depthBounds = false,
296 .wideLines = true,
297 .largePoints = true,
298 .textureCompressionETC2 = true,
299 .textureCompressionASTC_LDR = true,
300 .textureCompressionBC = true,
301 .occlusionQueryNonConservative = false, /* FINISHME */
302 .pipelineStatisticsQuery = true,
303 .vertexSideEffects = false,
304 .tessellationSideEffects = false,
305 .geometrySideEffects = false,
306 .fragmentSideEffects = false,
307 .shaderTessellationPointSize = false,
308 .shaderGeometryPointSize = true,
309 .shaderImageGatherExtended = true,
310 .shaderStorageImageExtendedFormats = false,
311 .shaderStorageImageMultisample = false,
312 .shaderUniformBufferArrayDynamicIndexing = true,
313 .shaderSampledImageArrayDynamicIndexing = false,
314 .shaderStorageBufferArrayDynamicIndexing = false,
315 .shaderStorageImageArrayDynamicIndexing = false,
316 .shaderClipDistance = false,
317 .shaderCullDistance = false,
318 .shaderFloat64 = false,
319 .shaderInt64 = false,
320 .shaderInt16 = false,
321 .alphaToOne = true,
322 };
323
324 return VK_SUCCESS;
325 }
326
327 VkResult anv_GetPhysicalDeviceProperties(
328 VkPhysicalDevice physicalDevice,
329 VkPhysicalDeviceProperties* pProperties)
330 {
331 ANV_FROM_HANDLE(anv_physical_device, pdevice, physicalDevice);
332 const struct brw_device_info *devinfo = pdevice->info;
333
334 anv_finishme("Get correct values for VkPhysicalDeviceLimits");
335
336 VkPhysicalDeviceLimits limits = {
337 .maxImageDimension1D = (1 << 14),
338 .maxImageDimension2D = (1 << 14),
339 .maxImageDimension3D = (1 << 10),
340 .maxImageDimensionCube = (1 << 14),
341 .maxImageArrayLayers = (1 << 10),
342
343 /* Broadwell supports 1, 2, 4, and 8 samples. */
344 .sampleCounts = 4,
345
346 .maxTexelBufferSize = (1 << 14),
347 .maxUniformBufferSize = UINT32_MAX,
348 .maxStorageBufferSize = UINT32_MAX,
349 .maxPushConstantsSize = MAX_PUSH_CONSTANTS_SIZE,
350 .maxMemoryAllocationCount = UINT32_MAX,
351 .bufferImageGranularity = 64, /* A cache line */
352 .sparseAddressSpaceSize = 0,
353 .maxBoundDescriptorSets = MAX_SETS,
354 .maxDescriptorSets = UINT32_MAX,
355 .maxPerStageDescriptorSamplers = 64,
356 .maxPerStageDescriptorUniformBuffers = 64,
357 .maxPerStageDescriptorStorageBuffers = 64,
358 .maxPerStageDescriptorSampledImages = 64,
359 .maxPerStageDescriptorStorageImages = 64,
360 .maxDescriptorSetSamplers = 256,
361 .maxDescriptorSetUniformBuffers = 256,
362 .maxDescriptorSetUniformBuffersDynamic = 256,
363 .maxDescriptorSetStorageBuffers = 256,
364 .maxDescriptorSetStorageBuffersDynamic = 256,
365 .maxDescriptorSetSampledImages = 256,
366 .maxDescriptorSetStorageImages = 256,
367 .maxVertexInputAttributes = 32,
368 .maxVertexInputBindings = 32,
369 .maxVertexInputAttributeOffset = 256,
370 .maxVertexInputBindingStride = 256,
371 .maxVertexOutputComponents = 32,
372 .maxTessGenLevel = 0,
373 .maxTessPatchSize = 0,
374 .maxTessControlPerVertexInputComponents = 0,
375 .maxTessControlPerVertexOutputComponents = 0,
376 .maxTessControlPerPatchOutputComponents = 0,
377 .maxTessControlTotalOutputComponents = 0,
378 .maxTessEvaluationInputComponents = 0,
379 .maxTessEvaluationOutputComponents = 0,
380 .maxGeometryShaderInvocations = 6,
381 .maxGeometryInputComponents = 16,
382 .maxGeometryOutputComponents = 16,
383 .maxGeometryOutputVertices = 16,
384 .maxGeometryTotalOutputComponents = 16,
385 .maxFragmentInputComponents = 16,
386 .maxFragmentOutputBuffers = 8,
387 .maxFragmentDualSourceBuffers = 2,
388 .maxFragmentCombinedOutputResources = 8,
389 .maxComputeSharedMemorySize = 1024,
390 .maxComputeWorkGroupCount = {
391 16 * devinfo->max_cs_threads,
392 16 * devinfo->max_cs_threads,
393 16 * devinfo->max_cs_threads,
394 },
395 .maxComputeWorkGroupInvocations = 16 * devinfo->max_cs_threads,
396 .maxComputeWorkGroupSize = {
397 16 * devinfo->max_cs_threads,
398 16 * devinfo->max_cs_threads,
399 16 * devinfo->max_cs_threads,
400 },
401 .subPixelPrecisionBits = 4 /* FIXME */,
402 .subTexelPrecisionBits = 4 /* FIXME */,
403 .mipmapPrecisionBits = 4 /* FIXME */,
404 .maxDrawIndexedIndexValue = UINT32_MAX,
405 .maxDrawIndirectInstanceCount = UINT32_MAX,
406 .primitiveRestartForPatches = UINT32_MAX,
407 .maxSamplerLodBias = 16,
408 .maxSamplerAnisotropy = 16,
409 .maxViewports = MAX_VIEWPORTS,
410 .maxViewportDimensions = { (1 << 14), (1 << 14) },
411 .viewportBoundsRange = { -1.0, 1.0 }, /* FIXME */
412 .viewportSubPixelBits = 13, /* We take a float? */
413 .minMemoryMapAlignment = 64, /* A cache line */
414 .minTexelBufferOffsetAlignment = 1,
415 .minUniformBufferOffsetAlignment = 1,
416 .minStorageBufferOffsetAlignment = 1,
417 .minTexelOffset = 0, /* FIXME */
418 .maxTexelOffset = 0, /* FIXME */
419 .minTexelGatherOffset = 0, /* FIXME */
420 .maxTexelGatherOffset = 0, /* FIXME */
421 .minInterpolationOffset = 0, /* FIXME */
422 .maxInterpolationOffset = 0, /* FIXME */
423 .subPixelInterpolationOffsetBits = 0, /* FIXME */
424 .maxFramebufferWidth = (1 << 14),
425 .maxFramebufferHeight = (1 << 14),
426 .maxFramebufferLayers = (1 << 10),
427 .maxFramebufferColorSamples = 8,
428 .maxFramebufferDepthSamples = 8,
429 .maxFramebufferStencilSamples = 8,
430 .maxColorAttachments = MAX_RTS,
431 .maxSampledImageColorSamples = 8,
432 .maxSampledImageDepthSamples = 8,
433 .maxSampledImageIntegerSamples = 1,
434 .maxStorageImageSamples = 1,
435 .maxSampleMaskWords = 1,
436 .timestampFrequency = 1000 * 1000 * 1000 / 80,
437 .maxClipDistances = 0 /* FIXME */,
438 .maxCullDistances = 0 /* FIXME */,
439 .maxCombinedClipAndCullDistances = 0 /* FIXME */,
440 .pointSizeRange = { 0.125, 255.875 },
441 .lineWidthRange = { 0.0, 7.9921875 },
442 .pointSizeGranularity = (1.0 / 8.0),
443 .lineWidthGranularity = (1.0 / 128.0),
444 };
445
446 *pProperties = (VkPhysicalDeviceProperties) {
447 .apiVersion = VK_MAKE_VERSION(0, 170, 2),
448 .driverVersion = 1,
449 .vendorId = 0x8086,
450 .deviceId = pdevice->chipset_id,
451 .deviceType = VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU,
452 .limits = limits,
453 .sparseProperties = {0}, /* Broadwell doesn't do sparse. */
454 };
455
456 strcpy(pProperties->deviceName, pdevice->name);
457 snprintf((char *)pProperties->pipelineCacheUUID, VK_UUID_LENGTH,
458 "anv-%s", MESA_GIT_SHA1 + 4);
459
460 return VK_SUCCESS;
461 }
462
463 VkResult anv_GetPhysicalDeviceQueueFamilyProperties(
464 VkPhysicalDevice physicalDevice,
465 uint32_t* pCount,
466 VkQueueFamilyProperties* pQueueFamilyProperties)
467 {
468 if (pQueueFamilyProperties == NULL) {
469 *pCount = 1;
470 return VK_SUCCESS;
471 }
472
473 assert(*pCount >= 1);
474
475 *pQueueFamilyProperties = (VkQueueFamilyProperties) {
476 .queueFlags = VK_QUEUE_GRAPHICS_BIT |
477 VK_QUEUE_COMPUTE_BIT |
478 VK_QUEUE_DMA_BIT,
479 .queueCount = 1,
480 .supportsTimestamps = true,
481 };
482
483 return VK_SUCCESS;
484 }
485
486 VkResult anv_GetPhysicalDeviceMemoryProperties(
487 VkPhysicalDevice physicalDevice,
488 VkPhysicalDeviceMemoryProperties* pMemoryProperties)
489 {
490 ANV_FROM_HANDLE(anv_physical_device, physical_device, physicalDevice);
491 VkDeviceSize heap_size;
492
493 /* Reserve some wiggle room for the driver by exposing only 75% of the
494 * aperture to the heap.
495 */
496 heap_size = 3 * physical_device->aperture_size / 4;
497
498 /* The property flags below are valid only for llc platforms. */
499 pMemoryProperties->memoryTypeCount = 1;
500 pMemoryProperties->memoryTypes[0] = (VkMemoryType) {
501 .propertyFlags = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT,
502 .heapIndex = 1,
503 };
504
505 pMemoryProperties->memoryHeapCount = 1;
506 pMemoryProperties->memoryHeaps[0] = (VkMemoryHeap) {
507 .size = heap_size,
508 .flags = VK_MEMORY_HEAP_HOST_LOCAL_BIT,
509 };
510
511 return VK_SUCCESS;
512 }
513
514 PFN_vkVoidFunction anv_GetInstanceProcAddr(
515 VkInstance instance,
516 const char* pName)
517 {
518 return anv_lookup_entrypoint(pName);
519 }
520
521 PFN_vkVoidFunction anv_GetDeviceProcAddr(
522 VkDevice device,
523 const char* pName)
524 {
525 return anv_lookup_entrypoint(pName);
526 }
527
528 static VkResult
529 anv_queue_init(struct anv_device *device, struct anv_queue *queue)
530 {
531 queue->_loader_data.loaderMagic = ICD_LOADER_MAGIC;
532 queue->device = device;
533 queue->pool = &device->surface_state_pool;
534
535 return VK_SUCCESS;
536 }
537
538 static void
539 anv_queue_finish(struct anv_queue *queue)
540 {
541 }
542
543 static void
544 anv_device_init_border_colors(struct anv_device *device)
545 {
546 static const VkClearColorValue border_colors[] = {
547 [VK_BORDER_COLOR_FLOAT_TRANSPARENT_BLACK] = { .float32 = { 0.0, 0.0, 0.0, 0.0 } },
548 [VK_BORDER_COLOR_FLOAT_OPAQUE_BLACK] = { .float32 = { 0.0, 0.0, 0.0, 1.0 } },
549 [VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE] = { .float32 = { 1.0, 1.0, 1.0, 1.0 } },
550 [VK_BORDER_COLOR_INT_TRANSPARENT_BLACK] = { .uint32 = { 0, 0, 0, 0 } },
551 [VK_BORDER_COLOR_INT_OPAQUE_BLACK] = { .uint32 = { 0, 0, 0, 1 } },
552 [VK_BORDER_COLOR_INT_OPAQUE_WHITE] = { .uint32 = { 1, 1, 1, 1 } },
553 };
554
555 device->border_colors =
556 anv_state_pool_alloc(&device->dynamic_state_pool,
557 sizeof(border_colors), 32);
558 memcpy(device->border_colors.map, border_colors, sizeof(border_colors));
559 }
560
561 VkResult anv_CreateDevice(
562 VkPhysicalDevice physicalDevice,
563 const VkDeviceCreateInfo* pCreateInfo,
564 VkDevice* pDevice)
565 {
566 ANV_FROM_HANDLE(anv_physical_device, physical_device, physicalDevice);
567 struct anv_instance *instance = physical_device->instance;
568 struct anv_device *device;
569
570 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO);
571
572 for (uint32_t i = 0; i < pCreateInfo->extensionCount; i++) {
573 bool found = false;
574 for (uint32_t j = 0; j < ARRAY_SIZE(device_extensions); j++) {
575 if (strcmp(pCreateInfo->ppEnabledExtensionNames[i],
576 device_extensions[j].extName) == 0) {
577 found = true;
578 break;
579 }
580 }
581 if (!found)
582 return vk_error(VK_ERROR_EXTENSION_NOT_PRESENT);
583 }
584
585 anv_set_dispatch_gen(physical_device->info->gen);
586
587 device = anv_instance_alloc(instance, sizeof(*device), 8,
588 VK_SYSTEM_ALLOC_TYPE_API_OBJECT);
589 if (!device)
590 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
591
592 device->_loader_data.loaderMagic = ICD_LOADER_MAGIC;
593 device->instance = physical_device->instance;
594
595 /* XXX(chadv): Can we dup() physicalDevice->fd here? */
596 device->fd = open(physical_device->path, O_RDWR | O_CLOEXEC);
597 if (device->fd == -1)
598 goto fail_device;
599
600 device->context_id = anv_gem_create_context(device);
601 if (device->context_id == -1)
602 goto fail_fd;
603
604 pthread_mutex_init(&device->mutex, NULL);
605
606 anv_bo_pool_init(&device->batch_bo_pool, device, ANV_CMD_BUFFER_BATCH_SIZE);
607
608 anv_block_pool_init(&device->dynamic_state_block_pool, device, 2048);
609
610 anv_state_pool_init(&device->dynamic_state_pool,
611 &device->dynamic_state_block_pool);
612
613 anv_block_pool_init(&device->instruction_block_pool, device, 2048);
614 anv_block_pool_init(&device->surface_state_block_pool, device, 4096);
615
616 anv_state_pool_init(&device->surface_state_pool,
617 &device->surface_state_block_pool);
618
619 anv_block_pool_init(&device->scratch_block_pool, device, 0x10000);
620
621 device->info = *physical_device->info;
622
623 device->compiler = anv_compiler_create(device);
624
625 anv_queue_init(device, &device->queue);
626
627 anv_device_init_meta(device);
628
629 anv_device_init_border_colors(device);
630
631 *pDevice = anv_device_to_handle(device);
632
633 return VK_SUCCESS;
634
635 fail_fd:
636 close(device->fd);
637 fail_device:
638 anv_device_free(device, device);
639
640 return vk_error(VK_ERROR_INITIALIZATION_FAILED);
641 }
642
643 void anv_DestroyDevice(
644 VkDevice _device)
645 {
646 ANV_FROM_HANDLE(anv_device, device, _device);
647
648 anv_compiler_destroy(device->compiler);
649
650 anv_queue_finish(&device->queue);
651
652 anv_device_finish_meta(device);
653
654 #ifdef HAVE_VALGRIND
655 /* We only need to free these to prevent valgrind errors. The backing
656 * BO will go away in a couple of lines so we don't actually leak.
657 */
658 anv_state_pool_free(&device->dynamic_state_pool, device->border_colors);
659 #endif
660
661 anv_bo_pool_finish(&device->batch_bo_pool);
662 anv_state_pool_finish(&device->dynamic_state_pool);
663 anv_block_pool_finish(&device->dynamic_state_block_pool);
664 anv_block_pool_finish(&device->instruction_block_pool);
665 anv_state_pool_finish(&device->surface_state_pool);
666 anv_block_pool_finish(&device->surface_state_block_pool);
667 anv_block_pool_finish(&device->scratch_block_pool);
668
669 close(device->fd);
670
671 anv_instance_free(device->instance, device);
672 }
673
674 VkResult anv_EnumerateInstanceExtensionProperties(
675 const char* pLayerName,
676 uint32_t* pCount,
677 VkExtensionProperties* pProperties)
678 {
679 if (pProperties == NULL) {
680 *pCount = ARRAY_SIZE(global_extensions);
681 return VK_SUCCESS;
682 }
683
684 assert(*pCount >= ARRAY_SIZE(global_extensions));
685
686 *pCount = ARRAY_SIZE(global_extensions);
687 memcpy(pProperties, global_extensions, sizeof(global_extensions));
688
689 return VK_SUCCESS;
690 }
691
692 VkResult anv_EnumerateDeviceExtensionProperties(
693 VkPhysicalDevice physicalDevice,
694 const char* pLayerName,
695 uint32_t* pCount,
696 VkExtensionProperties* pProperties)
697 {
698 if (pProperties == NULL) {
699 *pCount = ARRAY_SIZE(device_extensions);
700 return VK_SUCCESS;
701 }
702
703 assert(*pCount >= ARRAY_SIZE(device_extensions));
704
705 *pCount = ARRAY_SIZE(device_extensions);
706 memcpy(pProperties, device_extensions, sizeof(device_extensions));
707
708 return VK_SUCCESS;
709 }
710
711 VkResult anv_EnumerateInstanceLayerProperties(
712 uint32_t* pCount,
713 VkLayerProperties* pProperties)
714 {
715 if (pProperties == NULL) {
716 *pCount = 0;
717 return VK_SUCCESS;
718 }
719
720 /* None supported at this time */
721 return vk_error(VK_ERROR_LAYER_NOT_PRESENT);
722 }
723
724 VkResult anv_EnumerateDeviceLayerProperties(
725 VkPhysicalDevice physicalDevice,
726 uint32_t* pCount,
727 VkLayerProperties* pProperties)
728 {
729 if (pProperties == NULL) {
730 *pCount = 0;
731 return VK_SUCCESS;
732 }
733
734 /* None supported at this time */
735 return vk_error(VK_ERROR_LAYER_NOT_PRESENT);
736 }
737
738 VkResult anv_GetDeviceQueue(
739 VkDevice _device,
740 uint32_t queueNodeIndex,
741 uint32_t queueIndex,
742 VkQueue* pQueue)
743 {
744 ANV_FROM_HANDLE(anv_device, device, _device);
745
746 assert(queueIndex == 0);
747
748 *pQueue = anv_queue_to_handle(&device->queue);
749
750 return VK_SUCCESS;
751 }
752
753 VkResult anv_QueueSubmit(
754 VkQueue _queue,
755 uint32_t cmdBufferCount,
756 const VkCmdBuffer* pCmdBuffers,
757 VkFence _fence)
758 {
759 ANV_FROM_HANDLE(anv_queue, queue, _queue);
760 ANV_FROM_HANDLE(anv_fence, fence, _fence);
761 struct anv_device *device = queue->device;
762 int ret;
763
764 for (uint32_t i = 0; i < cmdBufferCount; i++) {
765 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, pCmdBuffers[i]);
766
767 assert(cmd_buffer->level == VK_CMD_BUFFER_LEVEL_PRIMARY);
768
769 ret = anv_gem_execbuffer(device, &cmd_buffer->execbuf2.execbuf);
770 if (ret != 0) {
771 /* We don't know the real error. */
772 return vk_errorf(VK_ERROR_OUT_OF_DEVICE_MEMORY,
773 "execbuf2 failed: %m");
774 }
775
776 if (fence) {
777 ret = anv_gem_execbuffer(device, &fence->execbuf);
778 if (ret != 0) {
779 /* We don't know the real error. */
780 return vk_errorf(VK_ERROR_OUT_OF_DEVICE_MEMORY,
781 "execbuf2 failed: %m");
782 }
783 }
784
785 for (uint32_t i = 0; i < cmd_buffer->execbuf2.bo_count; i++)
786 cmd_buffer->execbuf2.bos[i]->offset = cmd_buffer->execbuf2.objects[i].offset;
787 }
788
789 return VK_SUCCESS;
790 }
791
792 VkResult anv_QueueWaitIdle(
793 VkQueue _queue)
794 {
795 ANV_FROM_HANDLE(anv_queue, queue, _queue);
796
797 return ANV_CALL(DeviceWaitIdle)(anv_device_to_handle(queue->device));
798 }
799
800 VkResult anv_DeviceWaitIdle(
801 VkDevice _device)
802 {
803 ANV_FROM_HANDLE(anv_device, device, _device);
804 struct anv_state state;
805 struct anv_batch batch;
806 struct drm_i915_gem_execbuffer2 execbuf;
807 struct drm_i915_gem_exec_object2 exec2_objects[1];
808 struct anv_bo *bo = NULL;
809 VkResult result;
810 int64_t timeout;
811 int ret;
812
813 state = anv_state_pool_alloc(&device->dynamic_state_pool, 32, 32);
814 bo = &device->dynamic_state_pool.block_pool->bo;
815 batch.start = batch.next = state.map;
816 batch.end = state.map + 32;
817 anv_batch_emit(&batch, GEN7_MI_BATCH_BUFFER_END);
818 anv_batch_emit(&batch, GEN7_MI_NOOP);
819
820 exec2_objects[0].handle = bo->gem_handle;
821 exec2_objects[0].relocation_count = 0;
822 exec2_objects[0].relocs_ptr = 0;
823 exec2_objects[0].alignment = 0;
824 exec2_objects[0].offset = bo->offset;
825 exec2_objects[0].flags = 0;
826 exec2_objects[0].rsvd1 = 0;
827 exec2_objects[0].rsvd2 = 0;
828
829 execbuf.buffers_ptr = (uintptr_t) exec2_objects;
830 execbuf.buffer_count = 1;
831 execbuf.batch_start_offset = state.offset;
832 execbuf.batch_len = batch.next - state.map;
833 execbuf.cliprects_ptr = 0;
834 execbuf.num_cliprects = 0;
835 execbuf.DR1 = 0;
836 execbuf.DR4 = 0;
837
838 execbuf.flags =
839 I915_EXEC_HANDLE_LUT | I915_EXEC_NO_RELOC | I915_EXEC_RENDER;
840 execbuf.rsvd1 = device->context_id;
841 execbuf.rsvd2 = 0;
842
843 ret = anv_gem_execbuffer(device, &execbuf);
844 if (ret != 0) {
845 /* We don't know the real error. */
846 result = vk_errorf(VK_ERROR_OUT_OF_DEVICE_MEMORY, "execbuf2 failed: %m");
847 goto fail;
848 }
849
850 timeout = INT64_MAX;
851 ret = anv_gem_wait(device, bo->gem_handle, &timeout);
852 if (ret != 0) {
853 /* We don't know the real error. */
854 result = vk_errorf(VK_ERROR_OUT_OF_DEVICE_MEMORY, "execbuf2 failed: %m");
855 goto fail;
856 }
857
858 anv_state_pool_free(&device->dynamic_state_pool, state);
859
860 return VK_SUCCESS;
861
862 fail:
863 anv_state_pool_free(&device->dynamic_state_pool, state);
864
865 return result;
866 }
867
868 void *
869 anv_device_alloc(struct anv_device * device,
870 size_t size,
871 size_t alignment,
872 VkSystemAllocType allocType)
873 {
874 return anv_instance_alloc(device->instance, size, alignment, allocType);
875 }
876
877 void
878 anv_device_free(struct anv_device * device,
879 void * mem)
880 {
881 anv_instance_free(device->instance, mem);
882 }
883
884 VkResult
885 anv_bo_init_new(struct anv_bo *bo, struct anv_device *device, uint64_t size)
886 {
887 bo->gem_handle = anv_gem_create(device, size);
888 if (!bo->gem_handle)
889 return vk_error(VK_ERROR_OUT_OF_DEVICE_MEMORY);
890
891 bo->map = NULL;
892 bo->index = 0;
893 bo->offset = 0;
894 bo->size = size;
895
896 return VK_SUCCESS;
897 }
898
899 VkResult anv_AllocMemory(
900 VkDevice _device,
901 const VkMemoryAllocInfo* pAllocInfo,
902 VkDeviceMemory* pMem)
903 {
904 ANV_FROM_HANDLE(anv_device, device, _device);
905 struct anv_device_memory *mem;
906 VkResult result;
907
908 assert(pAllocInfo->sType == VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO);
909
910 /* We support exactly one memory heap. */
911 assert(pAllocInfo->memoryTypeIndex == 0);
912
913 /* FINISHME: Fail if allocation request exceeds heap size. */
914
915 mem = anv_device_alloc(device, sizeof(*mem), 8,
916 VK_SYSTEM_ALLOC_TYPE_API_OBJECT);
917 if (mem == NULL)
918 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
919
920 result = anv_bo_init_new(&mem->bo, device, pAllocInfo->allocationSize);
921 if (result != VK_SUCCESS)
922 goto fail;
923
924 *pMem = anv_device_memory_to_handle(mem);
925
926 return VK_SUCCESS;
927
928 fail:
929 anv_device_free(device, mem);
930
931 return result;
932 }
933
934 void anv_FreeMemory(
935 VkDevice _device,
936 VkDeviceMemory _mem)
937 {
938 ANV_FROM_HANDLE(anv_device, device, _device);
939 ANV_FROM_HANDLE(anv_device_memory, mem, _mem);
940
941 if (mem->bo.map)
942 anv_gem_munmap(mem->bo.map, mem->bo.size);
943
944 if (mem->bo.gem_handle != 0)
945 anv_gem_close(device, mem->bo.gem_handle);
946
947 anv_device_free(device, mem);
948 }
949
950 VkResult anv_MapMemory(
951 VkDevice _device,
952 VkDeviceMemory _mem,
953 VkDeviceSize offset,
954 VkDeviceSize size,
955 VkMemoryMapFlags flags,
956 void** ppData)
957 {
958 ANV_FROM_HANDLE(anv_device, device, _device);
959 ANV_FROM_HANDLE(anv_device_memory, mem, _mem);
960
961 /* FIXME: Is this supposed to be thread safe? Since vkUnmapMemory() only
962 * takes a VkDeviceMemory pointer, it seems like only one map of the memory
963 * at a time is valid. We could just mmap up front and return an offset
964 * pointer here, but that may exhaust virtual memory on 32 bit
965 * userspace. */
966
967 mem->map = anv_gem_mmap(device, mem->bo.gem_handle, offset, size);
968 mem->map_size = size;
969
970 *ppData = mem->map;
971
972 return VK_SUCCESS;
973 }
974
975 void anv_UnmapMemory(
976 VkDevice _device,
977 VkDeviceMemory _mem)
978 {
979 ANV_FROM_HANDLE(anv_device_memory, mem, _mem);
980
981 anv_gem_munmap(mem->map, mem->map_size);
982 }
983
984 VkResult anv_FlushMappedMemoryRanges(
985 VkDevice device,
986 uint32_t memRangeCount,
987 const VkMappedMemoryRange* pMemRanges)
988 {
989 /* clflush here for !llc platforms */
990
991 return VK_SUCCESS;
992 }
993
994 VkResult anv_InvalidateMappedMemoryRanges(
995 VkDevice device,
996 uint32_t memRangeCount,
997 const VkMappedMemoryRange* pMemRanges)
998 {
999 return anv_FlushMappedMemoryRanges(device, memRangeCount, pMemRanges);
1000 }
1001
1002 VkResult anv_GetBufferMemoryRequirements(
1003 VkDevice device,
1004 VkBuffer _buffer,
1005 VkMemoryRequirements* pMemoryRequirements)
1006 {
1007 ANV_FROM_HANDLE(anv_buffer, buffer, _buffer);
1008
1009 /* The Vulkan spec (git aaed022) says:
1010 *
1011 * memoryTypeBits is a bitfield and contains one bit set for every
1012 * supported memory type for the resource. The bit `1<<i` is set if and
1013 * only if the memory type `i` in the VkPhysicalDeviceMemoryProperties
1014 * structure for the physical device is supported.
1015 *
1016 * We support exactly one memory type.
1017 */
1018 pMemoryRequirements->memoryTypeBits = 1;
1019
1020 pMemoryRequirements->size = buffer->size;
1021 pMemoryRequirements->alignment = 16;
1022
1023 return VK_SUCCESS;
1024 }
1025
1026 VkResult anv_GetImageMemoryRequirements(
1027 VkDevice device,
1028 VkImage _image,
1029 VkMemoryRequirements* pMemoryRequirements)
1030 {
1031 ANV_FROM_HANDLE(anv_image, image, _image);
1032
1033 /* The Vulkan spec (git aaed022) says:
1034 *
1035 * memoryTypeBits is a bitfield and contains one bit set for every
1036 * supported memory type for the resource. The bit `1<<i` is set if and
1037 * only if the memory type `i` in the VkPhysicalDeviceMemoryProperties
1038 * structure for the physical device is supported.
1039 *
1040 * We support exactly one memory type.
1041 */
1042 pMemoryRequirements->memoryTypeBits = 1;
1043
1044 pMemoryRequirements->size = image->size;
1045 pMemoryRequirements->alignment = image->alignment;
1046
1047 return VK_SUCCESS;
1048 }
1049
1050 VkResult anv_GetImageSparseMemoryRequirements(
1051 VkDevice device,
1052 VkImage image,
1053 uint32_t* pNumRequirements,
1054 VkSparseImageMemoryRequirements* pSparseMemoryRequirements)
1055 {
1056 return vk_error(VK_UNSUPPORTED);
1057 }
1058
1059 VkResult anv_GetDeviceMemoryCommitment(
1060 VkDevice device,
1061 VkDeviceMemory memory,
1062 VkDeviceSize* pCommittedMemoryInBytes)
1063 {
1064 *pCommittedMemoryInBytes = 0;
1065 stub_return(VK_SUCCESS);
1066 }
1067
1068 VkResult anv_BindBufferMemory(
1069 VkDevice device,
1070 VkBuffer _buffer,
1071 VkDeviceMemory _mem,
1072 VkDeviceSize memOffset)
1073 {
1074 ANV_FROM_HANDLE(anv_device_memory, mem, _mem);
1075 ANV_FROM_HANDLE(anv_buffer, buffer, _buffer);
1076
1077 buffer->bo = &mem->bo;
1078 buffer->offset = memOffset;
1079
1080 return VK_SUCCESS;
1081 }
1082
1083 VkResult anv_BindImageMemory(
1084 VkDevice device,
1085 VkImage _image,
1086 VkDeviceMemory _mem,
1087 VkDeviceSize memOffset)
1088 {
1089 ANV_FROM_HANDLE(anv_device_memory, mem, _mem);
1090 ANV_FROM_HANDLE(anv_image, image, _image);
1091
1092 image->bo = &mem->bo;
1093 image->offset = memOffset;
1094
1095 return VK_SUCCESS;
1096 }
1097
1098 VkResult anv_QueueBindSparseBufferMemory(
1099 VkQueue queue,
1100 VkBuffer buffer,
1101 uint32_t numBindings,
1102 const VkSparseMemoryBindInfo* pBindInfo)
1103 {
1104 stub_return(VK_UNSUPPORTED);
1105 }
1106
1107 VkResult anv_QueueBindSparseImageOpaqueMemory(
1108 VkQueue queue,
1109 VkImage image,
1110 uint32_t numBindings,
1111 const VkSparseMemoryBindInfo* pBindInfo)
1112 {
1113 stub_return(VK_UNSUPPORTED);
1114 }
1115
1116 VkResult anv_QueueBindSparseImageMemory(
1117 VkQueue queue,
1118 VkImage image,
1119 uint32_t numBindings,
1120 const VkSparseImageMemoryBindInfo* pBindInfo)
1121 {
1122 stub_return(VK_UNSUPPORTED);
1123 }
1124
1125 VkResult anv_CreateFence(
1126 VkDevice _device,
1127 const VkFenceCreateInfo* pCreateInfo,
1128 VkFence* pFence)
1129 {
1130 ANV_FROM_HANDLE(anv_device, device, _device);
1131 struct anv_fence *fence;
1132 struct anv_batch batch;
1133 VkResult result;
1134
1135 const uint32_t fence_size = 128;
1136
1137 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_FENCE_CREATE_INFO);
1138
1139 fence = anv_device_alloc(device, sizeof(*fence), 8,
1140 VK_SYSTEM_ALLOC_TYPE_API_OBJECT);
1141 if (fence == NULL)
1142 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
1143
1144 result = anv_bo_init_new(&fence->bo, device, fence_size);
1145 if (result != VK_SUCCESS)
1146 goto fail;
1147
1148 fence->bo.map =
1149 anv_gem_mmap(device, fence->bo.gem_handle, 0, fence->bo.size);
1150 batch.next = batch.start = fence->bo.map;
1151 batch.end = fence->bo.map + fence->bo.size;
1152 anv_batch_emit(&batch, GEN7_MI_BATCH_BUFFER_END);
1153 anv_batch_emit(&batch, GEN7_MI_NOOP);
1154
1155 fence->exec2_objects[0].handle = fence->bo.gem_handle;
1156 fence->exec2_objects[0].relocation_count = 0;
1157 fence->exec2_objects[0].relocs_ptr = 0;
1158 fence->exec2_objects[0].alignment = 0;
1159 fence->exec2_objects[0].offset = fence->bo.offset;
1160 fence->exec2_objects[0].flags = 0;
1161 fence->exec2_objects[0].rsvd1 = 0;
1162 fence->exec2_objects[0].rsvd2 = 0;
1163
1164 fence->execbuf.buffers_ptr = (uintptr_t) fence->exec2_objects;
1165 fence->execbuf.buffer_count = 1;
1166 fence->execbuf.batch_start_offset = 0;
1167 fence->execbuf.batch_len = batch.next - fence->bo.map;
1168 fence->execbuf.cliprects_ptr = 0;
1169 fence->execbuf.num_cliprects = 0;
1170 fence->execbuf.DR1 = 0;
1171 fence->execbuf.DR4 = 0;
1172
1173 fence->execbuf.flags =
1174 I915_EXEC_HANDLE_LUT | I915_EXEC_NO_RELOC | I915_EXEC_RENDER;
1175 fence->execbuf.rsvd1 = device->context_id;
1176 fence->execbuf.rsvd2 = 0;
1177
1178 *pFence = anv_fence_to_handle(fence);
1179
1180 return VK_SUCCESS;
1181
1182 fail:
1183 anv_device_free(device, fence);
1184
1185 return result;
1186 }
1187
1188 void anv_DestroyFence(
1189 VkDevice _device,
1190 VkFence _fence)
1191 {
1192 ANV_FROM_HANDLE(anv_device, device, _device);
1193 ANV_FROM_HANDLE(anv_fence, fence, _fence);
1194
1195 anv_gem_munmap(fence->bo.map, fence->bo.size);
1196 anv_gem_close(device, fence->bo.gem_handle);
1197 anv_device_free(device, fence);
1198 }
1199
1200 VkResult anv_ResetFences(
1201 VkDevice _device,
1202 uint32_t fenceCount,
1203 const VkFence* pFences)
1204 {
1205 for (uint32_t i = 0; i < fenceCount; i++) {
1206 ANV_FROM_HANDLE(anv_fence, fence, pFences[i]);
1207 fence->ready = false;
1208 }
1209
1210 return VK_SUCCESS;
1211 }
1212
1213 VkResult anv_GetFenceStatus(
1214 VkDevice _device,
1215 VkFence _fence)
1216 {
1217 ANV_FROM_HANDLE(anv_device, device, _device);
1218 ANV_FROM_HANDLE(anv_fence, fence, _fence);
1219 int64_t t = 0;
1220 int ret;
1221
1222 if (fence->ready)
1223 return VK_SUCCESS;
1224
1225 ret = anv_gem_wait(device, fence->bo.gem_handle, &t);
1226 if (ret == 0) {
1227 fence->ready = true;
1228 return VK_SUCCESS;
1229 }
1230
1231 return VK_NOT_READY;
1232 }
1233
1234 VkResult anv_WaitForFences(
1235 VkDevice _device,
1236 uint32_t fenceCount,
1237 const VkFence* pFences,
1238 VkBool32 waitAll,
1239 uint64_t timeout)
1240 {
1241 ANV_FROM_HANDLE(anv_device, device, _device);
1242 int64_t t = timeout;
1243 int ret;
1244
1245 /* FIXME: handle !waitAll */
1246
1247 for (uint32_t i = 0; i < fenceCount; i++) {
1248 ANV_FROM_HANDLE(anv_fence, fence, pFences[i]);
1249 ret = anv_gem_wait(device, fence->bo.gem_handle, &t);
1250 if (ret == -1 && errno == ETIME) {
1251 return VK_TIMEOUT;
1252 } else if (ret == -1) {
1253 /* We don't know the real error. */
1254 return vk_errorf(VK_ERROR_OUT_OF_DEVICE_MEMORY,
1255 "gem wait failed: %m");
1256 }
1257 }
1258
1259 return VK_SUCCESS;
1260 }
1261
1262 // Queue semaphore functions
1263
1264 VkResult anv_CreateSemaphore(
1265 VkDevice device,
1266 const VkSemaphoreCreateInfo* pCreateInfo,
1267 VkSemaphore* pSemaphore)
1268 {
1269 stub_return(VK_UNSUPPORTED);
1270 }
1271
1272 void anv_DestroySemaphore(
1273 VkDevice device,
1274 VkSemaphore semaphore)
1275 {
1276 stub();
1277 }
1278
1279 VkResult anv_QueueSignalSemaphore(
1280 VkQueue queue,
1281 VkSemaphore semaphore)
1282 {
1283 stub_return(VK_UNSUPPORTED);
1284 }
1285
1286 VkResult anv_QueueWaitSemaphore(
1287 VkQueue queue,
1288 VkSemaphore semaphore)
1289 {
1290 stub_return(VK_UNSUPPORTED);
1291 }
1292
1293 // Event functions
1294
1295 VkResult anv_CreateEvent(
1296 VkDevice device,
1297 const VkEventCreateInfo* pCreateInfo,
1298 VkEvent* pEvent)
1299 {
1300 stub_return(VK_UNSUPPORTED);
1301 }
1302
1303 void anv_DestroyEvent(
1304 VkDevice device,
1305 VkEvent event)
1306 {
1307 stub();
1308 }
1309
1310 VkResult anv_GetEventStatus(
1311 VkDevice device,
1312 VkEvent event)
1313 {
1314 stub_return(VK_UNSUPPORTED);
1315 }
1316
1317 VkResult anv_SetEvent(
1318 VkDevice device,
1319 VkEvent event)
1320 {
1321 stub_return(VK_UNSUPPORTED);
1322 }
1323
1324 VkResult anv_ResetEvent(
1325 VkDevice device,
1326 VkEvent event)
1327 {
1328 stub_return(VK_UNSUPPORTED);
1329 }
1330
1331 // Buffer functions
1332
1333 VkResult anv_CreateBuffer(
1334 VkDevice _device,
1335 const VkBufferCreateInfo* pCreateInfo,
1336 VkBuffer* pBuffer)
1337 {
1338 ANV_FROM_HANDLE(anv_device, device, _device);
1339 struct anv_buffer *buffer;
1340
1341 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO);
1342
1343 buffer = anv_device_alloc(device, sizeof(*buffer), 8,
1344 VK_SYSTEM_ALLOC_TYPE_API_OBJECT);
1345 if (buffer == NULL)
1346 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
1347
1348 buffer->size = pCreateInfo->size;
1349 buffer->bo = NULL;
1350 buffer->offset = 0;
1351
1352 *pBuffer = anv_buffer_to_handle(buffer);
1353
1354 return VK_SUCCESS;
1355 }
1356
1357 void anv_DestroyBuffer(
1358 VkDevice _device,
1359 VkBuffer _buffer)
1360 {
1361 ANV_FROM_HANDLE(anv_device, device, _device);
1362 ANV_FROM_HANDLE(anv_buffer, buffer, _buffer);
1363
1364 anv_device_free(device, buffer);
1365 }
1366
1367 void
1368 anv_fill_buffer_surface_state(struct anv_device *device, void *state,
1369 const struct anv_format *format,
1370 uint32_t offset, uint32_t range)
1371 {
1372 switch (device->info.gen) {
1373 case 7:
1374 gen7_fill_buffer_surface_state(state, format, offset, range);
1375 break;
1376 case 8:
1377 gen8_fill_buffer_surface_state(state, format, offset, range);
1378 break;
1379 default:
1380 unreachable("unsupported gen\n");
1381 }
1382 }
1383
1384 VkResult
1385 anv_buffer_view_create(
1386 struct anv_device * device,
1387 const VkBufferViewCreateInfo* pCreateInfo,
1388 struct anv_buffer_view ** bview_out)
1389 {
1390 ANV_FROM_HANDLE(anv_buffer, buffer, pCreateInfo->buffer);
1391 struct anv_buffer_view *bview;
1392
1393 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO);
1394
1395 bview = anv_device_alloc(device, sizeof(*bview), 8,
1396 VK_SYSTEM_ALLOC_TYPE_API_OBJECT);
1397 if (bview == NULL)
1398 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
1399
1400 *bview = (struct anv_buffer_view) {
1401 .bo = buffer->bo,
1402 .offset = buffer->offset + pCreateInfo->offset,
1403 .surface_state = anv_state_pool_alloc(&device->surface_state_pool, 64, 64),
1404 .format = anv_format_for_vk_format(pCreateInfo->format),
1405 .range = pCreateInfo->range,
1406 };
1407
1408 *bview_out = bview;
1409
1410 return VK_SUCCESS;
1411 }
1412
1413 void anv_DestroyBufferView(
1414 VkDevice _device,
1415 VkBufferView _bview)
1416 {
1417 ANV_FROM_HANDLE(anv_device, device, _device);
1418 ANV_FROM_HANDLE(anv_buffer_view, bview, _bview);
1419
1420 anv_state_pool_free(&device->surface_state_pool, bview->surface_state);
1421 anv_device_free(device, bview);
1422 }
1423
1424 void anv_DestroySampler(
1425 VkDevice _device,
1426 VkSampler _sampler)
1427 {
1428 ANV_FROM_HANDLE(anv_device, device, _device);
1429 ANV_FROM_HANDLE(anv_sampler, sampler, _sampler);
1430
1431 anv_device_free(device, sampler);
1432 }
1433
1434 // Descriptor set functions
1435
1436 VkResult anv_CreateDescriptorSetLayout(
1437 VkDevice _device,
1438 const VkDescriptorSetLayoutCreateInfo* pCreateInfo,
1439 VkDescriptorSetLayout* pSetLayout)
1440 {
1441 ANV_FROM_HANDLE(anv_device, device, _device);
1442 struct anv_descriptor_set_layout *set_layout;
1443
1444 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO);
1445
1446 uint32_t sampler_count[VK_SHADER_STAGE_NUM] = { 0, };
1447 uint32_t surface_count[VK_SHADER_STAGE_NUM] = { 0, };
1448 uint32_t num_dynamic_buffers = 0;
1449 uint32_t count = 0;
1450 VkShaderStageFlags stages = 0;
1451 uint32_t s;
1452
1453 for (uint32_t i = 0; i < pCreateInfo->count; i++) {
1454 switch (pCreateInfo->pBinding[i].descriptorType) {
1455 case VK_DESCRIPTOR_TYPE_SAMPLER:
1456 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
1457 for_each_bit(s, pCreateInfo->pBinding[i].stageFlags)
1458 sampler_count[s] += pCreateInfo->pBinding[i].arraySize;
1459 break;
1460 default:
1461 break;
1462 }
1463
1464 switch (pCreateInfo->pBinding[i].descriptorType) {
1465 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
1466 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
1467 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE:
1468 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
1469 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
1470 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
1471 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
1472 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
1473 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
1474 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
1475 for_each_bit(s, pCreateInfo->pBinding[i].stageFlags)
1476 surface_count[s] += pCreateInfo->pBinding[i].arraySize;
1477 break;
1478 default:
1479 break;
1480 }
1481
1482 switch (pCreateInfo->pBinding[i].descriptorType) {
1483 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
1484 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
1485 num_dynamic_buffers += pCreateInfo->pBinding[i].arraySize;
1486 break;
1487 default:
1488 break;
1489 }
1490
1491 stages |= pCreateInfo->pBinding[i].stageFlags;
1492 count += pCreateInfo->pBinding[i].arraySize;
1493 }
1494
1495 uint32_t sampler_total = 0;
1496 uint32_t surface_total = 0;
1497 for (uint32_t s = 0; s < VK_SHADER_STAGE_NUM; s++) {
1498 sampler_total += sampler_count[s];
1499 surface_total += surface_count[s];
1500 }
1501
1502 size_t size = sizeof(*set_layout) +
1503 (sampler_total + surface_total) * sizeof(set_layout->entries[0]);
1504 set_layout = anv_device_alloc(device, size, 8,
1505 VK_SYSTEM_ALLOC_TYPE_API_OBJECT);
1506 if (!set_layout)
1507 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
1508
1509 set_layout->num_dynamic_buffers = num_dynamic_buffers;
1510 set_layout->count = count;
1511 set_layout->shader_stages = stages;
1512
1513 struct anv_descriptor_slot *p = set_layout->entries;
1514 struct anv_descriptor_slot *sampler[VK_SHADER_STAGE_NUM];
1515 struct anv_descriptor_slot *surface[VK_SHADER_STAGE_NUM];
1516 for (uint32_t s = 0; s < VK_SHADER_STAGE_NUM; s++) {
1517 set_layout->stage[s].surface_count = surface_count[s];
1518 set_layout->stage[s].surface_start = surface[s] = p;
1519 p += surface_count[s];
1520 set_layout->stage[s].sampler_count = sampler_count[s];
1521 set_layout->stage[s].sampler_start = sampler[s] = p;
1522 p += sampler_count[s];
1523 }
1524
1525 uint32_t descriptor = 0;
1526 int8_t dynamic_slot = 0;
1527 bool is_dynamic;
1528 for (uint32_t i = 0; i < pCreateInfo->count; i++) {
1529 switch (pCreateInfo->pBinding[i].descriptorType) {
1530 case VK_DESCRIPTOR_TYPE_SAMPLER:
1531 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
1532 for_each_bit(s, pCreateInfo->pBinding[i].stageFlags)
1533 for (uint32_t j = 0; j < pCreateInfo->pBinding[i].arraySize; j++) {
1534 sampler[s]->index = descriptor + j;
1535 sampler[s]->dynamic_slot = -1;
1536 sampler[s]++;
1537 }
1538 break;
1539 default:
1540 break;
1541 }
1542
1543 switch (pCreateInfo->pBinding[i].descriptorType) {
1544 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
1545 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
1546 is_dynamic = true;
1547 break;
1548 default:
1549 is_dynamic = false;
1550 break;
1551 }
1552
1553 switch (pCreateInfo->pBinding[i].descriptorType) {
1554 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
1555 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
1556 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE:
1557 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
1558 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
1559 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
1560 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
1561 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
1562 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
1563 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
1564 for_each_bit(s, pCreateInfo->pBinding[i].stageFlags)
1565 for (uint32_t j = 0; j < pCreateInfo->pBinding[i].arraySize; j++) {
1566 surface[s]->index = descriptor + j;
1567 if (is_dynamic)
1568 surface[s]->dynamic_slot = dynamic_slot + j;
1569 else
1570 surface[s]->dynamic_slot = -1;
1571 surface[s]++;
1572 }
1573 break;
1574 default:
1575 break;
1576 }
1577
1578 if (is_dynamic)
1579 dynamic_slot += pCreateInfo->pBinding[i].arraySize;
1580
1581 descriptor += pCreateInfo->pBinding[i].arraySize;
1582 }
1583
1584 *pSetLayout = anv_descriptor_set_layout_to_handle(set_layout);
1585
1586 return VK_SUCCESS;
1587 }
1588
1589 void anv_DestroyDescriptorSetLayout(
1590 VkDevice _device,
1591 VkDescriptorSetLayout _set_layout)
1592 {
1593 ANV_FROM_HANDLE(anv_device, device, _device);
1594 ANV_FROM_HANDLE(anv_descriptor_set_layout, set_layout, _set_layout);
1595
1596 anv_device_free(device, set_layout);
1597 }
1598
1599 VkResult anv_CreateDescriptorPool(
1600 VkDevice device,
1601 const VkDescriptorPoolCreateInfo* pCreateInfo,
1602 VkDescriptorPool* pDescriptorPool)
1603 {
1604 anv_finishme("VkDescriptorPool is a stub");
1605 pDescriptorPool->handle = 1;
1606 return VK_SUCCESS;
1607 }
1608
1609 void anv_DestroyDescriptorPool(
1610 VkDevice _device,
1611 VkDescriptorPool _pool)
1612 {
1613 anv_finishme("VkDescriptorPool is a stub: free the pool's descriptor sets");
1614 }
1615
1616 VkResult anv_ResetDescriptorPool(
1617 VkDevice device,
1618 VkDescriptorPool descriptorPool)
1619 {
1620 anv_finishme("VkDescriptorPool is a stub: free the pool's descriptor sets");
1621 return VK_SUCCESS;
1622 }
1623
1624 VkResult
1625 anv_descriptor_set_create(struct anv_device *device,
1626 const struct anv_descriptor_set_layout *layout,
1627 struct anv_descriptor_set **out_set)
1628 {
1629 struct anv_descriptor_set *set;
1630 size_t size = sizeof(*set) + layout->count * sizeof(set->descriptors[0]);
1631
1632 set = anv_device_alloc(device, size, 8, VK_SYSTEM_ALLOC_TYPE_API_OBJECT);
1633 if (!set)
1634 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
1635
1636 /* A descriptor set may not be 100% filled. Clear the set so we can can
1637 * later detect holes in it.
1638 */
1639 memset(set, 0, size);
1640
1641 *out_set = set;
1642
1643 return VK_SUCCESS;
1644 }
1645
1646 void
1647 anv_descriptor_set_destroy(struct anv_device *device,
1648 struct anv_descriptor_set *set)
1649 {
1650 anv_device_free(device, set);
1651 }
1652
1653 VkResult anv_AllocDescriptorSets(
1654 VkDevice _device,
1655 VkDescriptorPool descriptorPool,
1656 VkDescriptorSetUsage setUsage,
1657 uint32_t count,
1658 const VkDescriptorSetLayout* pSetLayouts,
1659 VkDescriptorSet* pDescriptorSets)
1660 {
1661 ANV_FROM_HANDLE(anv_device, device, _device);
1662
1663 VkResult result = VK_SUCCESS;
1664 struct anv_descriptor_set *set;
1665 uint32_t i;
1666
1667 for (i = 0; i < count; i++) {
1668 ANV_FROM_HANDLE(anv_descriptor_set_layout, layout, pSetLayouts[i]);
1669
1670 result = anv_descriptor_set_create(device, layout, &set);
1671 if (result != VK_SUCCESS)
1672 break;
1673
1674 pDescriptorSets[i] = anv_descriptor_set_to_handle(set);
1675 }
1676
1677 if (result != VK_SUCCESS)
1678 anv_FreeDescriptorSets(_device, descriptorPool, i, pDescriptorSets);
1679
1680 return result;
1681 }
1682
1683 VkResult anv_FreeDescriptorSets(
1684 VkDevice _device,
1685 VkDescriptorPool descriptorPool,
1686 uint32_t count,
1687 const VkDescriptorSet* pDescriptorSets)
1688 {
1689 ANV_FROM_HANDLE(anv_device, device, _device);
1690
1691 for (uint32_t i = 0; i < count; i++) {
1692 ANV_FROM_HANDLE(anv_descriptor_set, set, pDescriptorSets[i]);
1693
1694 anv_descriptor_set_destroy(device, set);
1695 }
1696
1697 return VK_SUCCESS;
1698 }
1699
1700 void anv_UpdateDescriptorSets(
1701 VkDevice device,
1702 uint32_t writeCount,
1703 const VkWriteDescriptorSet* pDescriptorWrites,
1704 uint32_t copyCount,
1705 const VkCopyDescriptorSet* pDescriptorCopies)
1706 {
1707 for (uint32_t i = 0; i < writeCount; i++) {
1708 const VkWriteDescriptorSet *write = &pDescriptorWrites[i];
1709 ANV_FROM_HANDLE(anv_descriptor_set, set, write->destSet);
1710
1711 for (uint32_t j = 0; j < write->count; ++j) {
1712 const VkDescriptorBufferInfo *binfo
1713 = &write->pDescriptors[j].bufferInfo;
1714
1715 if (binfo->buffer.handle || binfo->offset || binfo->range) {
1716 anv_finishme("VkWriteDesciptorSet::bufferInfo");
1717 break;
1718 }
1719 }
1720
1721 switch (write->descriptorType) {
1722 case VK_DESCRIPTOR_TYPE_SAMPLER:
1723 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
1724 for (uint32_t j = 0; j < write->count; j++) {
1725 ANV_FROM_HANDLE(anv_sampler, sampler,
1726 write->pDescriptors[j].sampler);
1727
1728 set->descriptors[write->destBinding + j] = (struct anv_descriptor) {
1729 .type = ANV_DESCRIPTOR_TYPE_SAMPLER,
1730 .sampler = sampler,
1731 };
1732 }
1733
1734 if (write->descriptorType == VK_DESCRIPTOR_TYPE_SAMPLER)
1735 break;
1736
1737 /* fallthrough */
1738
1739 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
1740 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE:
1741 for (uint32_t j = 0; j < write->count; j++) {
1742 ANV_FROM_HANDLE(anv_image_view, iview,
1743 write->pDescriptors[j].imageView);
1744
1745 set->descriptors[write->destBinding + j] = (struct anv_descriptor) {
1746 .type = ANV_DESCRIPTOR_TYPE_IMAGE_VIEW,
1747 .image_view = iview,
1748 };
1749 }
1750 break;
1751
1752 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
1753 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
1754 anv_finishme("texel buffers not implemented");
1755 break;
1756
1757 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
1758 anv_finishme("input attachments not implemented");
1759 break;
1760
1761 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
1762 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
1763 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
1764 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
1765 for (uint32_t j = 0; j < write->count; j++) {
1766 ANV_FROM_HANDLE(anv_buffer_view, bview,
1767 write->pDescriptors[j].bufferView);
1768
1769 set->descriptors[write->destBinding + j] = (struct anv_descriptor) {
1770 .type = ANV_DESCRIPTOR_TYPE_BUFFER_VIEW,
1771 .buffer_view = bview,
1772 };
1773 }
1774
1775 default:
1776 break;
1777 }
1778 }
1779
1780 for (uint32_t i = 0; i < copyCount; i++) {
1781 const VkCopyDescriptorSet *copy = &pDescriptorCopies[i];
1782 ANV_FROM_HANDLE(anv_descriptor_set, src, copy->destSet);
1783 ANV_FROM_HANDLE(anv_descriptor_set, dest, copy->destSet);
1784 for (uint32_t j = 0; j < copy->count; j++) {
1785 dest->descriptors[copy->destBinding + j] =
1786 src->descriptors[copy->srcBinding + j];
1787 }
1788 }
1789 }
1790
1791 VkResult anv_CreateFramebuffer(
1792 VkDevice _device,
1793 const VkFramebufferCreateInfo* pCreateInfo,
1794 VkFramebuffer* pFramebuffer)
1795 {
1796 ANV_FROM_HANDLE(anv_device, device, _device);
1797 struct anv_framebuffer *framebuffer;
1798
1799 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO);
1800
1801 size_t size = sizeof(*framebuffer) +
1802 sizeof(struct anv_image_view *) * pCreateInfo->attachmentCount;
1803 framebuffer = anv_device_alloc(device, size, 8,
1804 VK_SYSTEM_ALLOC_TYPE_API_OBJECT);
1805 if (framebuffer == NULL)
1806 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
1807
1808 framebuffer->attachment_count = pCreateInfo->attachmentCount;
1809 for (uint32_t i = 0; i < pCreateInfo->attachmentCount; i++) {
1810 VkImageView _iview = pCreateInfo->pAttachments[i];
1811 framebuffer->attachments[i] = anv_image_view_from_handle(_iview);
1812 }
1813
1814 framebuffer->width = pCreateInfo->width;
1815 framebuffer->height = pCreateInfo->height;
1816 framebuffer->layers = pCreateInfo->layers;
1817
1818 *pFramebuffer = anv_framebuffer_to_handle(framebuffer);
1819
1820 return VK_SUCCESS;
1821 }
1822
1823 void anv_DestroyFramebuffer(
1824 VkDevice _device,
1825 VkFramebuffer _fb)
1826 {
1827 ANV_FROM_HANDLE(anv_device, device, _device);
1828 ANV_FROM_HANDLE(anv_framebuffer, fb, _fb);
1829
1830 anv_device_free(device, fb);
1831 }
1832
1833 VkResult anv_CreateRenderPass(
1834 VkDevice _device,
1835 const VkRenderPassCreateInfo* pCreateInfo,
1836 VkRenderPass* pRenderPass)
1837 {
1838 ANV_FROM_HANDLE(anv_device, device, _device);
1839 struct anv_render_pass *pass;
1840 size_t size;
1841 size_t attachments_offset;
1842
1843 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO);
1844
1845 size = sizeof(*pass);
1846 size += pCreateInfo->subpassCount * sizeof(pass->subpasses[0]);
1847 attachments_offset = size;
1848 size += pCreateInfo->attachmentCount * sizeof(pass->attachments[0]);
1849
1850 pass = anv_device_alloc(device, size, 8,
1851 VK_SYSTEM_ALLOC_TYPE_API_OBJECT);
1852 if (pass == NULL)
1853 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
1854
1855 /* Clear the subpasses along with the parent pass. This required because
1856 * each array member of anv_subpass must be a valid pointer if not NULL.
1857 */
1858 memset(pass, 0, size);
1859 pass->attachment_count = pCreateInfo->attachmentCount;
1860 pass->subpass_count = pCreateInfo->subpassCount;
1861 pass->attachments = (void *) pass + attachments_offset;
1862
1863 for (uint32_t i = 0; i < pCreateInfo->attachmentCount; i++) {
1864 struct anv_render_pass_attachment *att = &pass->attachments[i];
1865
1866 att->format = anv_format_for_vk_format(pCreateInfo->pAttachments[i].format);
1867 att->samples = pCreateInfo->pAttachments[i].samples;
1868 att->load_op = pCreateInfo->pAttachments[i].loadOp;
1869 att->stencil_load_op = pCreateInfo->pAttachments[i].stencilLoadOp;
1870 // att->store_op = pCreateInfo->pAttachments[i].storeOp;
1871 // att->stencil_store_op = pCreateInfo->pAttachments[i].stencilStoreOp;
1872
1873 if (att->load_op == VK_ATTACHMENT_LOAD_OP_CLEAR) {
1874 if (anv_format_is_color(att->format)) {
1875 ++pass->num_color_clear_attachments;
1876 } else if (att->format->depth_format) {
1877 pass->has_depth_clear_attachment = true;
1878 }
1879 } else if (att->stencil_load_op == VK_ATTACHMENT_LOAD_OP_CLEAR) {
1880 assert(att->format->has_stencil);
1881 pass->has_stencil_clear_attachment = true;
1882 }
1883 }
1884
1885 for (uint32_t i = 0; i < pCreateInfo->subpassCount; i++) {
1886 const VkSubpassDescription *desc = &pCreateInfo->pSubpasses[i];
1887 struct anv_subpass *subpass = &pass->subpasses[i];
1888
1889 subpass->input_count = desc->inputCount;
1890 subpass->color_count = desc->colorCount;
1891
1892 if (desc->inputCount > 0) {
1893 subpass->input_attachments =
1894 anv_device_alloc(device, desc->inputCount * sizeof(uint32_t),
1895 8, VK_SYSTEM_ALLOC_TYPE_API_OBJECT);
1896
1897 for (uint32_t j = 0; j < desc->inputCount; j++) {
1898 subpass->input_attachments[j]
1899 = desc->pInputAttachments[j].attachment;
1900 }
1901 }
1902
1903 if (desc->colorCount > 0) {
1904 subpass->color_attachments =
1905 anv_device_alloc(device, desc->colorCount * sizeof(uint32_t),
1906 8, VK_SYSTEM_ALLOC_TYPE_API_OBJECT);
1907
1908 for (uint32_t j = 0; j < desc->colorCount; j++) {
1909 subpass->color_attachments[j]
1910 = desc->pColorAttachments[j].attachment;
1911 }
1912 }
1913
1914 if (desc->pResolveAttachments) {
1915 subpass->resolve_attachments =
1916 anv_device_alloc(device, desc->colorCount * sizeof(uint32_t),
1917 8, VK_SYSTEM_ALLOC_TYPE_API_OBJECT);
1918
1919 for (uint32_t j = 0; j < desc->colorCount; j++) {
1920 subpass->resolve_attachments[j]
1921 = desc->pResolveAttachments[j].attachment;
1922 }
1923 }
1924
1925 subpass->depth_stencil_attachment = desc->depthStencilAttachment.attachment;
1926 }
1927
1928 *pRenderPass = anv_render_pass_to_handle(pass);
1929
1930 return VK_SUCCESS;
1931 }
1932
1933 void anv_DestroyRenderPass(
1934 VkDevice _device,
1935 VkRenderPass _pass)
1936 {
1937 ANV_FROM_HANDLE(anv_device, device, _device);
1938 ANV_FROM_HANDLE(anv_render_pass, pass, _pass);
1939
1940 for (uint32_t i = 0; i < pass->subpass_count; i++) {
1941 /* In VkSubpassCreateInfo, each of the attachment arrays may be null.
1942 * Don't free the null arrays.
1943 */
1944 struct anv_subpass *subpass = &pass->subpasses[i];
1945
1946 anv_device_free(device, subpass->input_attachments);
1947 anv_device_free(device, subpass->color_attachments);
1948 anv_device_free(device, subpass->resolve_attachments);
1949 }
1950
1951 anv_device_free(device, pass);
1952 }
1953
1954 VkResult anv_GetRenderAreaGranularity(
1955 VkDevice device,
1956 VkRenderPass renderPass,
1957 VkExtent2D* pGranularity)
1958 {
1959 *pGranularity = (VkExtent2D) { 1, 1 };
1960
1961 return VK_SUCCESS;
1962 }
1963
1964 void vkCmdDbgMarkerBegin(
1965 VkCmdBuffer cmdBuffer,
1966 const char* pMarker)
1967 __attribute__ ((visibility ("default")));
1968
1969 void vkCmdDbgMarkerEnd(
1970 VkCmdBuffer cmdBuffer)
1971 __attribute__ ((visibility ("default")));
1972
1973 void vkCmdDbgMarkerBegin(
1974 VkCmdBuffer cmdBuffer,
1975 const char* pMarker)
1976 {
1977 }
1978
1979 void vkCmdDbgMarkerEnd(
1980 VkCmdBuffer cmdBuffer)
1981 {
1982 }