vk: Add GEN9 pack header
[mesa.git] / src / vulkan / anv_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 "anv_private.h"
31
32 VkResult anv_CreateDmaBufImageINTEL(
33 VkDevice _device,
34 const VkDmaBufImageCreateInfo* pCreateInfo,
35 VkDeviceMemory* pMem,
36 VkImage* pImage)
37 {
38 ANV_FROM_HANDLE(anv_device, device, _device);
39 struct anv_device_memory *mem;
40 struct anv_image *image;
41 VkResult result;
42 VkImage image_h;
43
44 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_DMA_BUF_IMAGE_CREATE_INFO_INTEL);
45
46 mem = anv_device_alloc(device, sizeof(*mem), 8,
47 VK_SYSTEM_ALLOC_TYPE_API_OBJECT);
48 if (mem == NULL)
49 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
50
51 mem->bo.gem_handle = anv_gem_fd_to_handle(device, pCreateInfo->fd);
52 if (!mem->bo.gem_handle) {
53 result = vk_error(VK_ERROR_OUT_OF_DEVICE_MEMORY);
54 goto fail;
55 }
56
57 mem->bo.map = NULL;
58 mem->bo.index = 0;
59 mem->bo.offset = 0;
60 mem->bo.size = pCreateInfo->strideInBytes * pCreateInfo->extent.height;
61
62 image = anv_device_alloc(device, sizeof(*image), 8,
63 VK_SYSTEM_ALLOC_TYPE_API_OBJECT);
64 if (image == NULL) {
65 result = vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
66 goto fail_mem;
67 }
68
69 anv_image_create(_device,
70 &(struct anv_image_create_info) {
71 .force_tiling = true,
72 .tiling = ISL_TILING_X,
73 .stride = pCreateInfo->strideInBytes,
74 .vk_info =
75 &(VkImageCreateInfo) {
76 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
77 .imageType = VK_IMAGE_TYPE_2D,
78 .format = pCreateInfo->format,
79 .extent = pCreateInfo->extent,
80 .mipLevels = 1,
81 .arraySize = 1,
82 .samples = 1,
83 /* FIXME: Need a way to use X tiling to allow scanout */
84 .tiling = VK_IMAGE_TILING_OPTIMAL,
85 .usage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
86 .flags = 0,
87 }},
88 &image_h);
89
90 image = anv_image_from_handle(image_h);
91 image->bo = &mem->bo;
92 image->offset = 0;
93
94 assert(image->extent.width > 0);
95 assert(image->extent.height > 0);
96 assert(image->extent.depth == 1);
97
98 *pMem = anv_device_memory_to_handle(mem);
99 *pImage = anv_image_to_handle(image);
100
101 return VK_SUCCESS;
102
103 fail_mem:
104 anv_gem_close(device, mem->bo.gem_handle);
105 fail:
106 anv_device_free(device, mem);
107
108 return result;
109 }