cd8fe7cd02337534d263778d20592cfd4c4b1178
[mesa.git] / src / libre-soc / vulkan / libresoc_image.c
1
2 /*
3 * Copyright © 2016 Red Hat.
4 * Copyright © 2016 Bas Nieuwenhuizen
5 *
6 * based in part on anv driver which is:
7 * Copyright © 2015 Intel Corporation
8 *
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
15 *
16 * The above copyright notice and this permission notice (including the next
17 * paragraph) shall be included in all copies or substantial portions of the
18 * Software.
19 *
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
25 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
26 * IN THE SOFTWARE.
27 */
28
29 #include "vk_util.h"
30 #include "libresoc_private.h"
31
32
33 VkResult
34 libresoc_image_create(VkDevice _device,
35 const struct libresoc_image_create_info *create_info,
36 const VkAllocationCallbacks* alloc,
37 VkImage *pImage)
38 {
39 LIBRESOC_FROM_HANDLE(libresoc_device, device, _device);
40 const VkImageCreateInfo *pCreateInfo = create_info->vk_info;
41 struct libresoc_image *image = NULL;
42 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO);
43
44 // const unsigned plane_count = vk_format_get_plane_count(format);
45 // const size_t image_struct_size = sizeof(*image) + sizeof(struct libresoc_image_plane) * plane_count;
46
47 const size_t image_struct_size = sizeof(*image);
48 // libresoc_assert(pCreateInfo->mipLevels > 0);
49 // libresoc_assert(pCreateInfo->arrayLayers > 0);
50 // libresoc_assert(pCreateInfo->samples > 0);
51 // libresoc_assert(pCreateInfo->extent.width > 0);
52 // libresoc_assert(pCreateInfo->extent.height > 0);
53 // libresoc_assert(pCreateInfo->extent.depth > 0);
54
55 image = vk_zalloc2(&device->vk.alloc, alloc, image_struct_size, 8,
56 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
57 if (!image)
58 return vk_error(device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);
59
60 vk_object_base_init(&device->vk, &image->base, VK_OBJECT_TYPE_IMAGE);
61
62 image->type = pCreateInfo->imageType;
63 image->size = pCreateInfo->extent.width * pCreateInfo->extent.height * 4;
64 image->width = pCreateInfo->extent.width;
65 image->height = pCreateInfo->extent.height;
66 // image->info.depth = pCreateInfo->extent.depth;
67 // image->info.samples = pCreateInfo->samples;
68 // image->info.storage_samples = pCreateInfo->samples;
69 // image->info.array_size = pCreateInfo->arrayLayers;
70 // image->info.levels = pCreateInfo->mipLevels;
71 // image->info.num_channels = vk_format_get_nr_components(format);
72
73 //image->vk_format = format;
74 image->tiling = pCreateInfo->tiling;
75 image->usage = pCreateInfo->usage;
76 image->flags = pCreateInfo->flags;
77 //image->plane_count = plane_count;
78
79 image->exclusive = pCreateInfo->sharingMode == VK_SHARING_MODE_EXCLUSIVE;
80 // if (pCreateInfo->sharingMode == VK_SHARING_MODE_CONCURRENT) {
81 // for (uint32_t i = 0; i < pCreateInfo->queueFamilyIndexCount; ++i)
82 // if (pCreateInfo->pQueueFamilyIndices[i] == VK_QUEUE_FAMILY_EXTERNAL ||
83 // pCreateInfo->pQueueFamilyIndices[i] == VK_QUEUE_FAMILY_FOREIGN_EXT)
84 // image->queue_family_mask |= (1u << LIBRESOC_MAX_QUEUE_FAMILIES) - 1u;
85 // else
86 // image->queue_family_mask |= 1u << pCreateInfo->pQueueFamilyIndices[i];
87 // }
88
89 // const VkExternalMemoryImageCreateInfo *external_info =
90 // vk_find_struct_const(pCreateInfo->pNext,
91 // EXTERNAL_MEMORY_IMAGE_CREATE_INFO) ;
92
93 // image->shareable = external_info;
94 // if (!vk_format_is_depth_or_stencil(format) && !image->shareable) {
95 // image->info.surf_index = &device->image_mrt_offset_counter;
96 // }
97
98 // for (unsigned plane = 0; plane < image->plane_count; ++plane) {
99 // image->planes[plane].surface.flags =
100 // libresoc_get_surface_flags(device, image, plane, pCreateInfo, format);
101 // }
102
103 // bool delay_layout = external_info &&
104 // (external_info->handleTypes & VK_EXTERNAL_MEMORY_HANDLE_TYPE_ANDROID_HARDWARE_BUFFER_BIT_ANDROID);
105
106 // if (delay_layout) {
107 // *pImage = libresoc_image_to_handle(image);
108 // assert (!(image->flags & VK_IMAGE_CREATE_SPARSE_BINDING_BIT));
109 // return VK_SUCCESS;
110 // }
111
112 // ASSERTED VkResult result = libresoc_image_create_layout(device, *create_info, image);
113 // assert(result == VK_SUCCESS);
114
115 // if (image->flags & VK_IMAGE_CREATE_SPARSE_BINDING_BIT) {
116 // image->alignment = MAX2(image->alignment, 4096);
117 // image->size = align64(image->size, image->alignment);
118 // image->offset = 0;
119
120 // image->bo = device->ws->buffer_create(device->ws, image->size, image->alignment,
121 // 0, RADEON_FLAG_VIRTUAL, LIBRESOC_BO_PRIORITY_VIRTUAL);
122 // if (!image->bo) {
123 // libresoc_destroy_image(device, alloc, image);
124 // return vk_error(device->instance, VK_ERROR_OUT_OF_DEVICE_MEMORY);
125 // }
126 // }
127
128 *pImage = libresoc_image_to_handle(image);
129
130 return VK_SUCCESS;
131 }
132
133
134 VkResult
135 libresoc_CreateImage(VkDevice device,
136 const VkImageCreateInfo *pCreateInfo,
137 const VkAllocationCallbacks *pAllocator,
138 VkImage *pImage)
139 {
140
141 const struct wsi_image_create_info *wsi_info =
142 vk_find_struct_const(pCreateInfo->pNext, WSI_IMAGE_CREATE_INFO_MESA);
143 bool scanout = wsi_info && wsi_info->scanout;
144
145 return libresoc_image_create(device,
146 &(struct libresoc_image_create_info) {
147 .vk_info = pCreateInfo,
148 .scanout = scanout,
149 },
150 pAllocator,
151 pImage);
152 }
153 void libresoc_GetImageSubresourceLayout(
154 VkDevice _device,
155 VkImage _image,
156 const VkImageSubresource* pSubresource,
157 VkSubresourceLayout* pLayout)
158 {
159 LIBRESOC_FROM_HANDLE(libresoc_image, image, _image);
160
161 pLayout->size = image->size;
162 pLayout->rowPitch = image->width;
163 }
164
165 VkResult
166 libresoc_CreateImageView(VkDevice _device,
167 const VkImageViewCreateInfo *pCreateInfo,
168 const VkAllocationCallbacks *pAllocator,
169 VkImageView *pView)
170 {
171 //TODO: stub
172 return VK_SUCCESS;
173 // LIBRESOC_FROM_HANDLE(libresoc_device, device, _device);
174 // struct libresoc_image_view *view;
175
176 // view = vk_alloc2(&device->vk.alloc, pAllocator, sizeof(*view), 8,
177 // VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
178 // if (view == NULL)
179 // return vk_error(device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);
180
181 // vk_object_base_init(&device->vk, &view->base,
182 // VK_OBJECT_TYPE_IMAGE_VIEW);
183
184 // libresoc_image_view_init(view, device, pCreateInfo, NULL);
185
186 // *pView = libresoc_image_view_to_handle(view);
187
188 // return VK_SUCCESS;
189 }
190 void
191 libresoc_DestroyImage(VkDevice _device, VkImage _image,
192 const VkAllocationCallbacks *pAllocator)
193 {
194
195 }
196
197 void
198 libresoc_DestroyImageView(VkDevice _device, VkImageView _iview,
199 const VkAllocationCallbacks *pAllocator)
200 {}