2 * Copyright © 2016 Red Hat.
3 * Copyright © 2016 Bas Nieuwenhuizen
5 * based in part on anv driver which is:
6 * Copyright © 2015 Intel Corporation
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:
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
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
28 #include "tu_private.h"
29 #include "util/debug.h"
30 #include "util/u_atomic.h"
31 #include "vk_format.h"
35 tu_image_create(VkDevice _device
,
36 const struct tu_image_create_info
*create_info
,
37 const VkAllocationCallbacks
*alloc
,
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
);
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);
52 image
= vk_zalloc2(&device
->alloc
,
56 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
58 return vk_error(device
->instance
, VK_ERROR_OUT_OF_HOST_MEMORY
);
60 image
->type
= pCreateInfo
->imageType
;
62 image
->vk_format
= pCreateInfo
->format
;
63 image
->tiling
= pCreateInfo
->tiling
;
64 image
->usage
= pCreateInfo
->usage
;
65 image
->flags
= pCreateInfo
->flags
;
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;
74 image
->queue_family_mask
|= 1u
75 << pCreateInfo
->pQueueFamilyIndices
[i
];
79 vk_find_struct_const(pCreateInfo
->pNext
,
80 EXTERNAL_MEMORY_IMAGE_CREATE_INFO_KHR
) != NULL
;
82 *pImage
= tu_image_to_handle(image
);
88 tu_image_view_init(struct tu_image_view
*iview
,
89 struct tu_device
*device
,
90 const VkImageViewCreateInfo
*pCreateInfo
)
95 tu_image_queue_family_mask(const struct tu_image
*image
,
97 uint32_t queue_family
)
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
;
109 tu_CreateImage(VkDevice device
,
110 const VkImageCreateInfo
*pCreateInfo
,
111 const VkAllocationCallbacks
*pAllocator
,
115 const VkNativeBufferANDROID
*gralloc_info
=
116 vk_find_struct_const(pCreateInfo
->pNext
, NATIVE_BUFFER_ANDROID
);
119 return tu_image_from_gralloc(
120 device
, pCreateInfo
, gralloc_info
, pAllocator
, pImage
);
123 return tu_image_create(device
,
124 &(struct tu_image_create_info
) {
125 .vk_info
= pCreateInfo
,
133 tu_DestroyImage(VkDevice _device
,
135 const VkAllocationCallbacks
*pAllocator
)
137 TU_FROM_HANDLE(tu_device
, device
, _device
);
138 TU_FROM_HANDLE(tu_image
, image
, _image
);
143 if (image
->owned_memory
!= VK_NULL_HANDLE
)
144 tu_FreeMemory(_device
, image
->owned_memory
, pAllocator
);
146 vk_free2(&device
->alloc
, pAllocator
, image
);
150 tu_GetImageSubresourceLayout(VkDevice _device
,
152 const VkImageSubresource
*pSubresource
,
153 VkSubresourceLayout
*pLayout
)
158 tu_CreateImageView(VkDevice _device
,
159 const VkImageViewCreateInfo
*pCreateInfo
,
160 const VkAllocationCallbacks
*pAllocator
,
163 TU_FROM_HANDLE(tu_device
, device
, _device
);
164 struct tu_image_view
*view
;
166 view
= vk_alloc2(&device
->alloc
,
170 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
172 return vk_error(device
->instance
, VK_ERROR_OUT_OF_HOST_MEMORY
);
174 tu_image_view_init(view
, device
, pCreateInfo
);
176 *pView
= tu_image_view_to_handle(view
);
182 tu_DestroyImageView(VkDevice _device
,
184 const VkAllocationCallbacks
*pAllocator
)
186 TU_FROM_HANDLE(tu_device
, device
, _device
);
187 TU_FROM_HANDLE(tu_image_view
, iview
, _iview
);
191 vk_free2(&device
->alloc
, pAllocator
, iview
);
195 tu_buffer_view_init(struct tu_buffer_view
*view
,
196 struct tu_device
*device
,
197 const VkBufferViewCreateInfo
*pCreateInfo
)
199 TU_FROM_HANDLE(tu_buffer
, buffer
, pCreateInfo
->buffer
);
201 view
->range
= pCreateInfo
->range
== VK_WHOLE_SIZE
202 ? buffer
->size
- pCreateInfo
->offset
203 : pCreateInfo
->range
;
204 view
->vk_format
= pCreateInfo
->format
;
208 tu_CreateBufferView(VkDevice _device
,
209 const VkBufferViewCreateInfo
*pCreateInfo
,
210 const VkAllocationCallbacks
*pAllocator
,
213 TU_FROM_HANDLE(tu_device
, device
, _device
);
214 struct tu_buffer_view
*view
;
216 view
= vk_alloc2(&device
->alloc
,
220 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
222 return vk_error(device
->instance
, VK_ERROR_OUT_OF_HOST_MEMORY
);
224 tu_buffer_view_init(view
, device
, pCreateInfo
);
226 *pView
= tu_buffer_view_to_handle(view
);
232 tu_DestroyBufferView(VkDevice _device
,
233 VkBufferView bufferView
,
234 const VkAllocationCallbacks
*pAllocator
)
236 TU_FROM_HANDLE(tu_device
, device
, _device
);
237 TU_FROM_HANDLE(tu_buffer_view
, view
, bufferView
);
242 vk_free2(&device
->alloc
, pAllocator
, view
);