Debian Codegraph @

src/imagination/vulkan/pvr_arch_image.c

raw
1 /*
2 * Copyright © 2022 Imagination Technologies Ltd.
3 *
4 * SPDX-License-Identifier: MIT
5 */
6
7 #include "pvr_image.h"
8
9 #include <assert.h>
10 #include <stdint.h>
11
12 #include "vk_log.h"
13 #include "vk_ycbcr_conversion.h"
14
15 #include "pvr_buffer.h"
16 #include "pvr_csb.h"
17 #include "pvr_device.h"
18 #include "pvr_entrypoints.h"
19 #include "pvr_macros.h"
20 #include "pvr_physical_device.h"
21 #include "pvr_tex_state.h"
22 #include "pvr_ycbcr.h"
23
24 static void pvr_adjust_non_compressed_view(const struct pvr_image *image,
25 const struct pvr_image_plane *plane,
26 struct pvr_texture_state_info *info)
27 {
28 const uint32_t base_level = info->base_level;
29
30 if (!vk_format_is_compressed(image->vk.format) ||
31 vk_format_is_compressed(info->format)) {
32 return;
33 }
34
35 /* Cannot use the image state, as the miplevel sizes for an
36 * uncompressed chain view may not decrease by 2 each time compared to the
37 * compressed one e.g. (22x22,11x11,5x5) -> (6x6,3x3,2x2)
38 * Instead manually apply an offset and patch the size
39 */
40 info->extent.width = u_minify(info->extent.width, base_level);
41 info->extent.height = u_minify(info->extent.height, base_level);
42 info->extent.depth = u_minify(info->extent.depth, base_level);
43 info->extent = vk_image_extent_to_elements(&image->vk, info->extent);
44 info->offset += plane->mip_levels[base_level].offset;
45 info->base_level = 0;
46 }
47
48 static unsigned int pvr_ycbcr_csc_index(struct vk_ycbcr_conversion *conversion)
49 {
50 #define MAPPING(vk, slot_full, slot_narrow) \
51 [VK_SAMPLER_YCBCR_MODEL_CONVERSION_##vk] = { \
52 PVR_YCBCR_SLOT_##slot_full, \
53 PVR_YCBCR_SLOT_##slot_narrow \
54 }
55
56 static unsigned int mapping[][2] = {
57 MAPPING(RGB_IDENTITY, RGB_IDENTITY, RGB_IDENTITY),
58 MAPPING(YCBCR_IDENTITY, YCBCR_IDENTITY_FULL, YCBCR_IDENTITY),
59 MAPPING(YCBCR_709, YCBCR_709_FULL, YCBCR_709),
60 MAPPING(YCBCR_601, YCBCR_601_FULL, YCBCR_601),
61 MAPPING(YCBCR_2020, YCBCR_2020_FULL, YCBCR_2020),
62 };
63 return mapping[conversion->state.ycbcr_model][conversion->state.ycbcr_range];
64
65 #undef MAPPING
66 }
67
68 VkResult PVR_PER_ARCH(CreateImageView)(VkDevice _device,
69 const VkImageViewCreateInfo *pCreateInfo,
70 const VkAllocationCallbacks *pAllocator,
71 VkImageView *pView)
72 {
73 VK_FROM_HANDLE(pvr_device, device, _device);
74 struct pvr_texture_state_info info = { 0 };
75 unsigned char input_swizzle[4];
76 const uint8_t *format_swizzle;
77 const struct pvr_image *image;
78 struct pvr_image_view *iview;
79 VkResult result;
80
81 iview = vk_image_view_create(&device->vk,
82 pCreateInfo,
83 pAllocator,
84 sizeof(*iview));
85 if (!iview)
86 return vk_error(device, VK_ERROR_OUT_OF_HOST_MEMORY);
87
88 image = pvr_image_view_get_image(iview);
89 const struct pvr_image_plane *plane;
90
91 const struct vk_format_ycbcr_info *ycbcr_image =
92 vk_format_get_ycbcr_info(image->vk.format);
93 const struct vk_format_ycbcr_info *ycbcr_iview =
94 vk_format_get_ycbcr_info(iview->vk.format);
95 if (ycbcr_image && !ycbcr_iview) {
96 plane = pvr_plane_from_aspect_const(image, iview->vk.aspects);
97 } else {
98 plane = &image->planes[0];
99 }
100
101 if (image->vk.image_type == VK_IMAGE_TYPE_3D &&
102 (iview->vk.view_type == VK_IMAGE_VIEW_TYPE_2D_ARRAY ||
103 iview->vk.view_type == VK_IMAGE_VIEW_TYPE_2D)) {
104 iview->vk.layer_count = image->vk.extent.depth;
105 }
106
107 info.type = iview->vk.view_type;
108 info.base_level = iview->vk.base_mip_level;
109 info.mip_levels = iview->vk.level_count;
110 info.extent = image->vk.extent;
111 info.aspect_mask = iview->vk.aspects;
112 info.is_cube = (info.type == VK_IMAGE_VIEW_TYPE_CUBE ||
113 info.type == VK_IMAGE_VIEW_TYPE_CUBE_ARRAY);
114 info.array_size = iview->vk.layer_count;
115 info.offset = iview->vk.base_array_layer * plane->layer_size;
116 info.mipmaps_present = (image->vk.mip_levels > 1) ? true : false;
117 info.stride = plane->physical_extent.width;
118 info.tex_state_type = PVR_TEXTURE_STATE_SAMPLE;
119 info.mem_layout = image->memlayout;
120 info.flags = 0;
121 info.sample_count = image->vk.samples;
122 info.addr = image->dev_addr;
123
124 info.format = pCreateInfo->format;
125 info.layer_size = plane->layer_size;
126
127 if (image->vk.create_flags & VK_IMAGE_CREATE_2D_VIEW_COMPATIBLE_BIT_EXT) {
128 info.offset = 0;
129 info.z_slice = iview->vk.base_array_layer;
130 }
131
132 pvr_adjust_non_compressed_view(image, plane, &info);
133
134 if (ycbcr_iview) {
135 const VkSamplerYcbcrConversionInfo *pConversion =
136 vk_find_struct_const(pCreateInfo->pNext,
137 SAMPLER_YCBCR_CONVERSION_INFO);
138 VK_FROM_HANDLE(vk_ycbcr_conversion, conversion, pConversion->conversion);
139 assert(conversion);
140
141 /* UV swizzle is handled by chroma swap instead */
142 input_swizzle[0] = PIPE_SWIZZLE_X;
143 input_swizzle[1] = PIPE_SWIZZLE_Y;
144 input_swizzle[2] = PIPE_SWIZZLE_Z;
145 input_swizzle[3] = PIPE_SWIZZLE_1;
146
147 if (conversion->state.mapping[3] == VK_COMPONENT_SWIZZLE_ZERO) {
148 input_swizzle[3] = PIPE_SWIZZLE_0;
149 }
150
151 info.swap_chroma = conversion->state.mapping[0] == VK_COMPONENT_SWIZZLE_B;
152
153 info.csc_coeff_index = pvr_ycbcr_csc_index(conversion);
154
155 pvr_csb_pack (&iview->sampler_words[0], TEXSTATE_SAMPLER_WORD0, cfg) {
156 cfg.texaddr_plane2_lo =
157 PVR_DEV_ADDR_OFFSET(image->dev_addr, image->planes[1].offset);
158 }
159 pvr_csb_pack (&iview->sampler_words[1], TEXSTATE_SAMPLER_WORD1, cfg) {
160 cfg.texaddr_plane2_hi =
161 PVR_DEV_ADDR_OFFSET(image->dev_addr, image->planes[1].offset);
162 cfg.texaddr_plane3 =
163 PVR_DEV_ADDR_OFFSET(image->dev_addr, image->planes[2].offset);
164 }
165 } else {
166 vk_component_mapping_to_pipe_swizzle(iview->vk.swizzle, input_swizzle);
167 }
168
169 enum pipe_format pipe_format =
170 vk_format_to_pipe_format(iview->vk.view_format);
171 if (util_format_is_depth_or_stencil(pipe_format)) {
172 switch (pipe_format) {
173 case PIPE_FORMAT_S8_UINT:
174 pipe_format = PIPE_FORMAT_R8_UINT;
175 break;
176
177 case PIPE_FORMAT_Z16_UNORM:
178 pipe_format = PIPE_FORMAT_R16_UINT;
179 break;
180
181 case PIPE_FORMAT_Z32_FLOAT:
182 pipe_format = PIPE_FORMAT_R32_FLOAT;
183 break;
184
185 default:
186 break;
187 }
188 }
189 format_swizzle = util_format_description(pipe_format)->swizzle;
190
191 util_format_compose_swizzles(format_swizzle, input_swizzle, info.swizzle);
192
193 result = pvr_arch_pack_tex_state(device,
194 &info,
195 &iview->image_state[info.tex_state_type]);
196 if (result != VK_SUCCESS)
197 goto err_vk_image_view_destroy;
198
199 /* Create an additional texture state for cube type if storage
200 * usage flag is set.
201 */
202 if (info.is_cube && image->vk.usage & VK_IMAGE_USAGE_STORAGE_BIT) {
203 info.tex_state_type = PVR_TEXTURE_STATE_STORAGE;
204
205 result =
206 pvr_arch_pack_tex_state(device,
207 &info,
208 &iview->image_state[info.tex_state_type]);
209 if (result != VK_SUCCESS)
210 goto err_vk_image_view_destroy;
211 }
212
213 if (image->vk.usage & VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT) {
214 /* Attachment state is created as if the mipmaps are not supported, so the
215 * baselevel is set to zero and num_mip_levels is set to 1. Which gives an
216 * impression that this is the only level in the image. This also requires
217 * that width, height and depth be adjusted as well. Given
218 * iview->vk.extent is already adjusted for base mip map level we use it
219 * here.
220 */
221 /* TODO: Investigate and document the reason for above approach. */
222 info.extent = iview->vk.extent;
223
224 info.mip_levels = 1;
225 info.mipmaps_present = false;
226 info.stride = u_minify(plane->physical_extent.width, info.base_level);
227 info.base_level = 0;
228 info.tex_state_type = PVR_TEXTURE_STATE_ATTACHMENT;
229
230 if (image->vk.image_type == VK_IMAGE_TYPE_3D &&
231 iview->vk.view_type == VK_IMAGE_VIEW_TYPE_2D) {
232 info.type = VK_IMAGE_VIEW_TYPE_3D;
233 } else {
234 info.type = iview->vk.view_type;
235 }
236
237 result =
238 pvr_arch_pack_tex_state(device,
239 &info,
240 &iview->image_state[info.tex_state_type]);
241 if (result != VK_SUCCESS)
242 goto err_vk_image_view_destroy;
243 }
244
245 *pView = pvr_image_view_to_handle(iview);
246
247 return VK_SUCCESS;
248
249 err_vk_image_view_destroy:
250 vk_image_view_destroy(&device->vk, pAllocator, &iview->vk);
251
252 return result;
253 }
254
255 void PVR_PER_ARCH(DestroyImageView)(VkDevice _device,
256 VkImageView _iview,
257 const VkAllocationCallbacks *pAllocator)
258 {
259 VK_FROM_HANDLE(pvr_device, device, _device);
260 VK_FROM_HANDLE(pvr_image_view, iview, _iview);
261
262 if (!iview)
263 return;
264
265 vk_image_view_destroy(&device->vk, pAllocator, &iview->vk);
266 }
267
268 VkResult
269 PVR_PER_ARCH(CreateBufferView)(VkDevice _device,
270 const VkBufferViewCreateInfo *pCreateInfo,
271 const VkAllocationCallbacks *pAllocator,
272 VkBufferView *pView)
273 {
274 VK_FROM_HANDLE(pvr_buffer, buffer, pCreateInfo->buffer);
275 VK_FROM_HANDLE(pvr_device, device, _device);
276 struct pvr_texture_state_info info = { 0 };
277 const uint8_t *format_swizzle;
278 struct pvr_buffer_view *bview;
279 VkResult result;
280
281 bview = vk_buffer_view_create(&device->vk,
282 pCreateInfo,
283 pAllocator,
284 sizeof(*bview));
285 if (!bview)
286 return vk_error(device, VK_ERROR_OUT_OF_HOST_MEMORY);
287
288 /* If the remaining size of the buffer is not a multiple of the element
289 * size of the format, the nearest smaller multiple is used.
290 */
291 bview->vk.range -=
292 bview->vk.range % vk_format_get_blocksize(bview->vk.format);
293
294 /* The range of the buffer view shouldn't be smaller than one texel. */
295 assert(bview->vk.range >= vk_format_get_blocksize(bview->vk.format));
296
297 bview->num_rows = DIV_ROUND_UP(bview->vk.elements, PVR_BUFFER_VIEW_WIDTH);
298
299 info.base_level = 0U;
300 info.mip_levels = 1U;
301 info.mipmaps_present = false;
302 info.extent.width = PVR_BUFFER_VIEW_WIDTH;
303 info.extent.height = bview->num_rows;
304 info.extent.depth = 0U;
305 info.sample_count = 1U;
306 info.stride = PVR_BUFFER_VIEW_WIDTH;
307 info.offset = 0U;
308 info.addr = PVR_DEV_ADDR_OFFSET(buffer->dev_addr, pCreateInfo->offset);
309 info.mem_layout = PVR_MEMLAYOUT_LINEAR;
310 info.is_cube = false;
311 info.type = VK_IMAGE_VIEW_TYPE_2D;
312 info.tex_state_type = PVR_TEXTURE_STATE_SAMPLE;
313 info.format = bview->vk.format;
314 info.flags = PVR_TEXFLAGS_INDEX_LOOKUP;
315 info.aspect_mask = VK_IMAGE_ASPECT_COLOR_BIT;
316 info.buffer_elems = bview->vk.elements;
317
318 if (PVR_HAS_FEATURE(&device->pdevice->dev_info, tpu_array_textures))
319 info.array_size = 1U;
320
321 format_swizzle = pvr_get_format_swizzle(info.format);
322 memcpy(info.swizzle, format_swizzle, sizeof(info.swizzle));
323
324 result = pvr_arch_pack_tex_state(device, &info, &bview->image_state);
325 if (result != VK_SUCCESS)
326 goto err_vk_buffer_view_destroy;
327
328 *pView = pvr_buffer_view_to_handle(bview);
329
330 return VK_SUCCESS;
331
332 err_vk_buffer_view_destroy:
333 vk_object_free(&device->vk, pAllocator, bview);
334
335 return result;
336 }
337
338 void PVR_PER_ARCH(DestroyBufferView)(VkDevice _device,
339 VkBufferView bufferView,
340 const VkAllocationCallbacks *pAllocator)
341 {
342 VK_FROM_HANDLE(pvr_buffer_view, bview, bufferView);
343 VK_FROM_HANDLE(pvr_device, device, _device);
344
345 if (!bview)
346 return;
347
348 vk_buffer_view_destroy(&device->vk, pAllocator, &bview->vk);
349 }