Merge remote-tracking branch 'fdo-personal/wip/nir-vtn' into vulkan
[mesa.git] / src / vulkan / intel.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 "private.h"
31
32 #include <vulkan/vulkan_intel.h>
33
34 VkResult VKAPI vkCreateDmaBufImageINTEL(
35 VkDevice _device,
36 const VkDmaBufImageCreateInfo* pCreateInfo,
37 VkDeviceMemory* pMem,
38 VkImage* pImage)
39 {
40 struct anv_device *device = (struct anv_device *) _device;
41 struct anv_device_memory *mem;
42 struct anv_image *image;
43 VkResult result;
44
45 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_DMA_BUF_IMAGE_CREATE_INFO_INTEL);
46
47 mem = anv_device_alloc(device, sizeof(*mem), 8,
48 VK_SYSTEM_ALLOC_TYPE_API_OBJECT);
49 if (mem == NULL)
50 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
51
52 mem->bo.gem_handle = anv_gem_fd_to_handle(device, pCreateInfo->fd);
53 if (!mem->bo.gem_handle) {
54 result = vk_error(VK_ERROR_OUT_OF_DEVICE_MEMORY);
55 goto fail;
56 }
57
58 mem->bo.map = NULL;
59 mem->bo.index = 0;
60 mem->bo.offset = 0;
61 mem->bo.size = pCreateInfo->strideInBytes * pCreateInfo->extent.height;
62
63 image = anv_device_alloc(device, sizeof(*image), 8,
64 VK_SYSTEM_ALLOC_TYPE_API_OBJECT);
65 if (image == NULL) {
66 result = vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
67 goto fail_mem;
68 }
69
70 image->bo = &mem->bo;
71 image->offset = 0;
72 image->type = VK_IMAGE_TYPE_2D;
73 image->extent = pCreateInfo->extent;
74 image->tile_mode = XMAJOR;
75 image->stride = pCreateInfo->strideInBytes;
76 image->size = mem->bo.size;
77
78 assert(image->extent.width > 0);
79 assert(image->extent.height > 0);
80 assert(image->extent.depth == 1);
81
82 *pMem = (VkDeviceMemory) mem;
83 *pImage = (VkImage) image;
84
85 return VK_SUCCESS;
86
87 fail_mem:
88 anv_gem_close(device, mem->bo.gem_handle);
89 fail:
90 anv_device_free(device, mem);
91
92 return result;
93 }