turnip: Fix indentation in function signatures
[mesa.git] / src / freedreno / vulkan / tu_image.c
1 /*
2 * Copyright © 2016 Red Hat.
3 * Copyright © 2016 Bas Nieuwenhuizen
4 *
5 * based in part on anv driver which is:
6 * Copyright © 2015 Intel Corporation
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice (including the next
16 * paragraph) shall be included in all copies or substantial portions of the
17 * Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
25 * IN THE SOFTWARE.
26 */
27
28 #include "tu_private.h"
29 #include "util/debug.h"
30 #include "util/u_atomic.h"
31 #include "vk_format.h"
32 #include "vk_util.h"
33
34 VkResult
35 tu_image_create(VkDevice _device,
36 const struct tu_image_create_info *create_info,
37 const VkAllocationCallbacks *alloc,
38 VkImage *pImage)
39 {
40 TU_FROM_HANDLE(tu_device, device, _device);
41 const VkImageCreateInfo *pCreateInfo = create_info->vk_info;
42 struct tu_image *image = NULL;
43 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO);
44
45 tu_assert(pCreateInfo->mipLevels > 0);
46 tu_assert(pCreateInfo->arrayLayers > 0);
47 tu_assert(pCreateInfo->samples > 0);
48 tu_assert(pCreateInfo->extent.width > 0);
49 tu_assert(pCreateInfo->extent.height > 0);
50 tu_assert(pCreateInfo->extent.depth > 0);
51
52 image = vk_zalloc2(&device->alloc,
53 alloc,
54 sizeof(*image),
55 8,
56 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
57 if (!image)
58 return vk_error(device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);
59
60 image->type = pCreateInfo->imageType;
61
62 image->vk_format = pCreateInfo->format;
63 image->tiling = pCreateInfo->tiling;
64 image->usage = pCreateInfo->usage;
65 image->flags = pCreateInfo->flags;
66
67 image->exclusive = pCreateInfo->sharingMode == VK_SHARING_MODE_EXCLUSIVE;
68 if (pCreateInfo->sharingMode == VK_SHARING_MODE_CONCURRENT) {
69 for (uint32_t i = 0; i < pCreateInfo->queueFamilyIndexCount; ++i)
70 if (pCreateInfo->pQueueFamilyIndices[i] ==
71 VK_QUEUE_FAMILY_EXTERNAL_KHR)
72 image->queue_family_mask |= (1u << TU_MAX_QUEUE_FAMILIES) - 1u;
73 else
74 image->queue_family_mask |= 1u
75 << pCreateInfo->pQueueFamilyIndices[i];
76 }
77
78 image->shareable =
79 vk_find_struct_const(pCreateInfo->pNext,
80 EXTERNAL_MEMORY_IMAGE_CREATE_INFO_KHR) != NULL;
81
82 *pImage = tu_image_to_handle(image);
83
84 return VK_SUCCESS;
85 }
86
87 void
88 tu_image_view_init(struct tu_image_view *iview,
89 struct tu_device *device,
90 const VkImageViewCreateInfo *pCreateInfo)
91 {
92 }
93
94 unsigned
95 tu_image_queue_family_mask(const struct tu_image *image,
96 uint32_t family,
97 uint32_t queue_family)
98 {
99 if (!image->exclusive)
100 return image->queue_family_mask;
101 if (family == VK_QUEUE_FAMILY_EXTERNAL_KHR)
102 return (1u << TU_MAX_QUEUE_FAMILIES) - 1u;
103 if (family == VK_QUEUE_FAMILY_IGNORED)
104 return 1u << queue_family;
105 return 1u << family;
106 }
107
108 VkResult
109 tu_CreateImage(VkDevice device,
110 const VkImageCreateInfo *pCreateInfo,
111 const VkAllocationCallbacks *pAllocator,
112 VkImage *pImage)
113 {
114 #ifdef ANDROID
115 const VkNativeBufferANDROID *gralloc_info =
116 vk_find_struct_const(pCreateInfo->pNext, NATIVE_BUFFER_ANDROID);
117
118 if (gralloc_info)
119 return tu_image_from_gralloc(
120 device, pCreateInfo, gralloc_info, pAllocator, pImage);
121 #endif
122
123 return tu_image_create(device,
124 &(struct tu_image_create_info) {
125 .vk_info = pCreateInfo,
126 .scanout = false,
127 },
128 pAllocator,
129 pImage);
130 }
131
132 void
133 tu_DestroyImage(VkDevice _device,
134 VkImage _image,
135 const VkAllocationCallbacks *pAllocator)
136 {
137 TU_FROM_HANDLE(tu_device, device, _device);
138 TU_FROM_HANDLE(tu_image, image, _image);
139
140 if (!image)
141 return;
142
143 if (image->owned_memory != VK_NULL_HANDLE)
144 tu_FreeMemory(_device, image->owned_memory, pAllocator);
145
146 vk_free2(&device->alloc, pAllocator, image);
147 }
148
149 void
150 tu_GetImageSubresourceLayout(VkDevice _device,
151 VkImage _image,
152 const VkImageSubresource *pSubresource,
153 VkSubresourceLayout *pLayout)
154 {
155 }
156
157 VkResult
158 tu_CreateImageView(VkDevice _device,
159 const VkImageViewCreateInfo *pCreateInfo,
160 const VkAllocationCallbacks *pAllocator,
161 VkImageView *pView)
162 {
163 TU_FROM_HANDLE(tu_device, device, _device);
164 struct tu_image_view *view;
165
166 view = vk_alloc2(&device->alloc,
167 pAllocator,
168 sizeof(*view),
169 8,
170 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
171 if (view == NULL)
172 return vk_error(device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);
173
174 tu_image_view_init(view, device, pCreateInfo);
175
176 *pView = tu_image_view_to_handle(view);
177
178 return VK_SUCCESS;
179 }
180
181 void
182 tu_DestroyImageView(VkDevice _device,
183 VkImageView _iview,
184 const VkAllocationCallbacks *pAllocator)
185 {
186 TU_FROM_HANDLE(tu_device, device, _device);
187 TU_FROM_HANDLE(tu_image_view, iview, _iview);
188
189 if (!iview)
190 return;
191 vk_free2(&device->alloc, pAllocator, iview);
192 }
193
194 void
195 tu_buffer_view_init(struct tu_buffer_view *view,
196 struct tu_device *device,
197 const VkBufferViewCreateInfo *pCreateInfo)
198 {
199 TU_FROM_HANDLE(tu_buffer, buffer, pCreateInfo->buffer);
200
201 view->range = pCreateInfo->range == VK_WHOLE_SIZE
202 ? buffer->size - pCreateInfo->offset
203 : pCreateInfo->range;
204 view->vk_format = pCreateInfo->format;
205 }
206
207 VkResult
208 tu_CreateBufferView(VkDevice _device,
209 const VkBufferViewCreateInfo *pCreateInfo,
210 const VkAllocationCallbacks *pAllocator,
211 VkBufferView *pView)
212 {
213 TU_FROM_HANDLE(tu_device, device, _device);
214 struct tu_buffer_view *view;
215
216 view = vk_alloc2(&device->alloc,
217 pAllocator,
218 sizeof(*view),
219 8,
220 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
221 if (!view)
222 return vk_error(device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);
223
224 tu_buffer_view_init(view, device, pCreateInfo);
225
226 *pView = tu_buffer_view_to_handle(view);
227
228 return VK_SUCCESS;
229 }
230
231 void
232 tu_DestroyBufferView(VkDevice _device,
233 VkBufferView bufferView,
234 const VkAllocationCallbacks *pAllocator)
235 {
236 TU_FROM_HANDLE(tu_device, device, _device);
237 TU_FROM_HANDLE(tu_buffer_view, view, bufferView);
238
239 if (!view)
240 return;
241
242 vk_free2(&device->alloc, pAllocator, view);
243 }