anv/image_view: Add base mip and base layer fields
[mesa.git] / src / vulkan / anv_image.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 /**
33 * The \a format argument is required and overrides any format found in struct
34 * anv_image_create_info. Exactly one bit must be set in \a aspect.
35 */
36 static isl_surf_usage_flags_t
37 choose_isl_surf_usage(const struct anv_image_create_info *info,
38 VkImageAspectFlags aspect)
39 {
40 const VkImageCreateInfo *vk_info = info->vk_info;
41 isl_surf_usage_flags_t isl_flags = 0;
42
43 /* FINISHME: Support aux surfaces */
44 isl_flags |= ISL_SURF_USAGE_DISABLE_AUX_BIT;
45
46 if (vk_info->usage & VK_IMAGE_USAGE_SAMPLED_BIT)
47 isl_flags |= ISL_SURF_USAGE_TEXTURE_BIT;
48
49 if (vk_info->usage & VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT)
50 isl_flags |= ISL_SURF_USAGE_TEXTURE_BIT;
51
52 if (vk_info->usage & VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT)
53 isl_flags |= ISL_SURF_USAGE_RENDER_TARGET_BIT;
54
55 if (vk_info->flags & VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT)
56 isl_flags |= ISL_SURF_USAGE_CUBE_BIT;
57
58 if (vk_info->usage & VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT) {
59 switch (aspect) {
60 default:
61 unreachable("bad VkImageAspect");
62 case VK_IMAGE_ASPECT_DEPTH_BIT:
63 isl_flags |= ISL_SURF_USAGE_DEPTH_BIT;
64 break;
65 case VK_IMAGE_ASPECT_STENCIL_BIT:
66 isl_flags |= ISL_SURF_USAGE_STENCIL_BIT;
67 break;
68 }
69 }
70
71 if (vk_info->usage & VK_IMAGE_USAGE_TRANSFER_SRC_BIT) {
72 /* Meta implements transfers by sampling from the source image. */
73 isl_flags |= ISL_SURF_USAGE_TEXTURE_BIT;
74 }
75
76 if (vk_info->usage & VK_IMAGE_USAGE_TRANSFER_DST_BIT) {
77 /* Meta implements transfers by rendering into the destination image. */
78 isl_flags |= ISL_SURF_USAGE_RENDER_TARGET_BIT;
79 }
80
81 return isl_flags;
82 }
83
84 /**
85 * Exactly one bit must be set in \a aspect.
86 */
87 static struct anv_surface *
88 get_surface(struct anv_image *image, VkImageAspectFlags aspect)
89 {
90 switch (aspect) {
91 default:
92 unreachable("bad VkImageAspect");
93 case VK_IMAGE_ASPECT_COLOR_BIT:
94 return &image->color_surface;
95 case VK_IMAGE_ASPECT_DEPTH_BIT:
96 return &image->depth_surface;
97 case VK_IMAGE_ASPECT_STENCIL_BIT:
98 return &image->stencil_surface;
99 }
100 }
101
102 /**
103 * Initialize the anv_image::*_surface selected by \a aspect. Then update the
104 * image's memory requirements (that is, the image's size and alignment).
105 *
106 * Exactly one bit must be set in \a aspect.
107 */
108 static VkResult
109 make_surface(const struct anv_device *dev,
110 struct anv_image *image,
111 const struct anv_image_create_info *anv_info,
112 VkImageAspectFlags aspect)
113 {
114 const VkImageCreateInfo *vk_info = anv_info->vk_info;
115 bool ok UNUSED;
116
117 static const enum isl_surf_dim vk_to_isl_surf_dim[] = {
118 [VK_IMAGE_TYPE_1D] = ISL_SURF_DIM_1D,
119 [VK_IMAGE_TYPE_2D] = ISL_SURF_DIM_2D,
120 [VK_IMAGE_TYPE_3D] = ISL_SURF_DIM_3D,
121 };
122
123 isl_tiling_flags_t tiling_flags = anv_info->isl_tiling_flags;
124 if (vk_info->tiling == VK_IMAGE_TILING_LINEAR)
125 tiling_flags &= ISL_TILING_LINEAR_BIT;
126
127 struct anv_surface *anv_surf = get_surface(image, aspect);
128
129 ok = isl_surf_init(&dev->isl_dev, &anv_surf->isl,
130 .dim = vk_to_isl_surf_dim[vk_info->imageType],
131 .format = anv_get_isl_format(vk_info->format, aspect, vk_info->tiling),
132 .width = vk_info->extent.width,
133 .height = vk_info->extent.height,
134 .depth = vk_info->extent.depth,
135 .levels = vk_info->mipLevels,
136 .array_len = vk_info->arrayLayers,
137 .samples = vk_info->samples,
138 .min_alignment = 0,
139 .min_pitch = 0,
140 .usage = choose_isl_surf_usage(anv_info, aspect),
141 .tiling_flags = tiling_flags);
142
143 /* isl_surf_init() will fail only if provided invalid input. Invalid input
144 * is illegal in Vulkan.
145 */
146 assert(ok);
147
148 anv_surf->offset = align_u32(image->size, anv_surf->isl.alignment);
149 image->size = anv_surf->offset + anv_surf->isl.size;
150 image->alignment = MAX(image->alignment, anv_surf->isl.alignment);
151
152 return VK_SUCCESS;
153 }
154
155 static VkImageUsageFlags
156 anv_image_get_full_usage(const VkImageCreateInfo *info)
157 {
158 VkImageUsageFlags usage = info->usage;
159
160 if (usage & VK_IMAGE_USAGE_TRANSFER_SRC_BIT) {
161 /* Meta will transfer from the image by binding it as a texture. */
162 usage |= VK_IMAGE_USAGE_SAMPLED_BIT;
163 }
164
165 if (usage & VK_IMAGE_USAGE_TRANSFER_DST_BIT) {
166 /* Meta will transfer to the image by binding it as a color attachment,
167 * even if the image format is not a color format.
168 */
169 usage |= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
170 }
171
172 return usage;
173 }
174
175 VkResult
176 anv_image_create(VkDevice _device,
177 const struct anv_image_create_info *create_info,
178 const VkAllocationCallbacks* alloc,
179 VkImage *pImage)
180 {
181 ANV_FROM_HANDLE(anv_device, device, _device);
182 const VkImageCreateInfo *pCreateInfo = create_info->vk_info;
183 struct anv_image *image = NULL;
184 VkResult r;
185
186 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO);
187
188 anv_assert(pCreateInfo->mipLevels > 0);
189 anv_assert(pCreateInfo->arrayLayers > 0);
190 anv_assert(pCreateInfo->samples > 0);
191 anv_assert(pCreateInfo->extent.width > 0);
192 anv_assert(pCreateInfo->extent.height > 0);
193 anv_assert(pCreateInfo->extent.depth > 0);
194
195 image = anv_alloc2(&device->alloc, alloc, sizeof(*image), 8,
196 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
197 if (!image)
198 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
199
200 memset(image, 0, sizeof(*image));
201 image->type = pCreateInfo->imageType;
202 image->extent = pCreateInfo->extent;
203 image->vk_format = pCreateInfo->format;
204 image->format = anv_format_for_vk_format(pCreateInfo->format);
205 image->levels = pCreateInfo->mipLevels;
206 image->array_size = pCreateInfo->arrayLayers;
207 image->samples = pCreateInfo->samples;
208 image->usage = anv_image_get_full_usage(pCreateInfo);
209 image->tiling = pCreateInfo->tiling;
210
211 if (image->usage & VK_IMAGE_USAGE_SAMPLED_BIT) {
212 image->needs_nonrt_surface_state = true;
213 }
214
215 if (image->usage & VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT) {
216 image->needs_color_rt_surface_state = true;
217 }
218
219 if (image->usage & VK_IMAGE_USAGE_STORAGE_BIT) {
220 image->needs_storage_surface_state = true;
221 }
222
223 if (likely(anv_format_is_color(image->format))) {
224 r = make_surface(device, image, create_info,
225 VK_IMAGE_ASPECT_COLOR_BIT);
226 if (r != VK_SUCCESS)
227 goto fail;
228 } else {
229 if (image->format->depth_format) {
230 r = make_surface(device, image, create_info,
231 VK_IMAGE_ASPECT_DEPTH_BIT);
232 if (r != VK_SUCCESS)
233 goto fail;
234 }
235
236 if (image->format->has_stencil) {
237 r = make_surface(device, image, create_info,
238 VK_IMAGE_ASPECT_STENCIL_BIT);
239 if (r != VK_SUCCESS)
240 goto fail;
241 }
242 }
243
244 *pImage = anv_image_to_handle(image);
245
246 return VK_SUCCESS;
247
248 fail:
249 if (image)
250 anv_free2(&device->alloc, alloc, image);
251
252 return r;
253 }
254
255 VkResult
256 anv_CreateImage(VkDevice device,
257 const VkImageCreateInfo *pCreateInfo,
258 const VkAllocationCallbacks *pAllocator,
259 VkImage *pImage)
260 {
261 return anv_image_create(device,
262 &(struct anv_image_create_info) {
263 .vk_info = pCreateInfo,
264 .isl_tiling_flags = ISL_TILING_ANY_MASK,
265 },
266 pAllocator,
267 pImage);
268 }
269
270 void
271 anv_DestroyImage(VkDevice _device, VkImage _image,
272 const VkAllocationCallbacks *pAllocator)
273 {
274 ANV_FROM_HANDLE(anv_device, device, _device);
275
276 anv_free2(&device->alloc, pAllocator, anv_image_from_handle(_image));
277 }
278
279 static void
280 anv_surface_get_subresource_layout(struct anv_image *image,
281 struct anv_surface *surface,
282 const VkImageSubresource *subresource,
283 VkSubresourceLayout *layout)
284 {
285 /* If we are on a non-zero mip level or array slice, we need to
286 * calculate a real offset.
287 */
288 anv_assert(subresource->mipLevel == 0);
289 anv_assert(subresource->arrayLayer == 0);
290
291 layout->offset = surface->offset;
292 layout->rowPitch = surface->isl.row_pitch;
293 layout->depthPitch = isl_surf_get_array_pitch(&surface->isl);
294 layout->arrayPitch = isl_surf_get_array_pitch(&surface->isl);
295 layout->size = surface->isl.size;
296 }
297
298 void anv_GetImageSubresourceLayout(
299 VkDevice device,
300 VkImage _image,
301 const VkImageSubresource* pSubresource,
302 VkSubresourceLayout* pLayout)
303 {
304 ANV_FROM_HANDLE(anv_image, image, _image);
305
306 assert(__builtin_popcount(pSubresource->aspectMask) == 1);
307
308 switch (pSubresource->aspectMask) {
309 case VK_IMAGE_ASPECT_COLOR_BIT:
310 anv_surface_get_subresource_layout(image, &image->color_surface,
311 pSubresource, pLayout);
312 break;
313 case VK_IMAGE_ASPECT_DEPTH_BIT:
314 anv_surface_get_subresource_layout(image, &image->depth_surface,
315 pSubresource, pLayout);
316 break;
317 case VK_IMAGE_ASPECT_STENCIL_BIT:
318 anv_surface_get_subresource_layout(image, &image->stencil_surface,
319 pSubresource, pLayout);
320 break;
321 default:
322 assert(!"Invalid image aspect");
323 }
324 }
325
326 VkResult
327 anv_validate_CreateImageView(VkDevice _device,
328 const VkImageViewCreateInfo *pCreateInfo,
329 const VkAllocationCallbacks *pAllocator,
330 VkImageView *pView)
331 {
332 ANV_FROM_HANDLE(anv_image, image, pCreateInfo->image);
333 const VkImageSubresourceRange *subresource;
334 const struct anv_format *view_format_info;
335
336 /* Validate structure type before dereferencing it. */
337 assert(pCreateInfo);
338 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO);
339 subresource = &pCreateInfo->subresourceRange;
340
341 /* Validate viewType is in range before using it. */
342 assert(pCreateInfo->viewType >= VK_IMAGE_VIEW_TYPE_BEGIN_RANGE);
343 assert(pCreateInfo->viewType <= VK_IMAGE_VIEW_TYPE_END_RANGE);
344
345 /* Validate format is in range before using it. */
346 assert(pCreateInfo->format >= VK_FORMAT_BEGIN_RANGE);
347 assert(pCreateInfo->format <= VK_FORMAT_END_RANGE);
348 view_format_info = anv_format_for_vk_format(pCreateInfo->format);
349
350 /* Validate channel swizzles. */
351 assert(pCreateInfo->components.r >= VK_COMPONENT_SWIZZLE_BEGIN_RANGE);
352 assert(pCreateInfo->components.r <= VK_COMPONENT_SWIZZLE_END_RANGE);
353 assert(pCreateInfo->components.g >= VK_COMPONENT_SWIZZLE_BEGIN_RANGE);
354 assert(pCreateInfo->components.g <= VK_COMPONENT_SWIZZLE_END_RANGE);
355 assert(pCreateInfo->components.b >= VK_COMPONENT_SWIZZLE_BEGIN_RANGE);
356 assert(pCreateInfo->components.b <= VK_COMPONENT_SWIZZLE_END_RANGE);
357 assert(pCreateInfo->components.a >= VK_COMPONENT_SWIZZLE_BEGIN_RANGE);
358 assert(pCreateInfo->components.a <= VK_COMPONENT_SWIZZLE_END_RANGE);
359
360 /* Validate subresource. */
361 assert(subresource->aspectMask != 0);
362 assert(subresource->levelCount > 0);
363 assert(subresource->layerCount > 0);
364 assert(subresource->baseMipLevel < image->levels);
365 assert(subresource->baseMipLevel + subresource->levelCount <= image->levels);
366 assert(subresource->baseArrayLayer < image->array_size);
367 assert(subresource->baseArrayLayer + subresource->layerCount <= image->array_size);
368 assert(pView);
369
370 const VkImageAspectFlags ds_flags = VK_IMAGE_ASPECT_DEPTH_BIT
371 | VK_IMAGE_ASPECT_STENCIL_BIT;
372
373 /* Validate format. */
374 if (subresource->aspectMask & VK_IMAGE_ASPECT_COLOR_BIT) {
375 assert(subresource->aspectMask == VK_IMAGE_ASPECT_COLOR_BIT);
376 assert(!image->format->depth_format);
377 assert(!image->format->has_stencil);
378 assert(!view_format_info->depth_format);
379 assert(!view_format_info->has_stencil);
380 assert(view_format_info->isl_layout->bs ==
381 image->format->isl_layout->bs);
382 } else if (subresource->aspectMask & ds_flags) {
383 assert((subresource->aspectMask & ~ds_flags) == 0);
384
385 if (subresource->aspectMask & VK_IMAGE_ASPECT_STENCIL_BIT) {
386 assert(image->format->depth_format);
387 assert(view_format_info->depth_format);
388 assert(view_format_info->isl_layout->bs ==
389 image->format->isl_layout->bs);
390 }
391
392 if (subresource->aspectMask & VK_IMAGE_ASPECT_STENCIL_BIT) {
393 /* FINISHME: Is it legal to have an R8 view of S8? */
394 assert(image->format->has_stencil);
395 assert(view_format_info->has_stencil);
396 }
397 } else {
398 assert(!"bad VkImageSubresourceRange::aspectFlags");
399 }
400
401 return anv_CreateImageView(_device, pCreateInfo, pAllocator, pView);
402 }
403
404 void
405 anv_fill_image_surface_state(struct anv_device *device, void *state_map,
406 struct anv_image_view *iview,
407 const VkImageViewCreateInfo *pCreateInfo,
408 VkImageUsageFlagBits usage)
409 {
410 switch (device->info.gen) {
411 case 7:
412 if (device->info.is_haswell)
413 gen75_fill_image_surface_state(device, state_map, iview,
414 pCreateInfo, usage);
415 else
416 gen7_fill_image_surface_state(device, state_map, iview,
417 pCreateInfo, usage);
418 break;
419 case 8:
420 gen8_fill_image_surface_state(device, state_map, iview,
421 pCreateInfo, usage);
422 break;
423 case 9:
424 gen9_fill_image_surface_state(device, state_map, iview,
425 pCreateInfo, usage);
426 break;
427 default:
428 unreachable("unsupported gen\n");
429 }
430
431 if (!device->info.has_llc)
432 anv_state_clflush(iview->nonrt_surface_state);
433 }
434
435 static struct anv_state
436 alloc_surface_state(struct anv_device *device,
437 struct anv_cmd_buffer *cmd_buffer)
438 {
439 if (cmd_buffer) {
440 return anv_cmd_buffer_alloc_surface_state(cmd_buffer);
441 } else {
442 return anv_state_pool_alloc(&device->surface_state_pool, 64, 64);
443 }
444 }
445
446 void
447 anv_image_view_init(struct anv_image_view *iview,
448 struct anv_device *device,
449 const VkImageViewCreateInfo* pCreateInfo,
450 struct anv_cmd_buffer *cmd_buffer)
451 {
452 ANV_FROM_HANDLE(anv_image, image, pCreateInfo->image);
453 const VkImageSubresourceRange *range = &pCreateInfo->subresourceRange;
454
455 assert(range->layerCount > 0);
456 assert(range->baseMipLevel < image->levels);
457 assert(image->usage & (VK_IMAGE_USAGE_SAMPLED_BIT |
458 VK_IMAGE_USAGE_STORAGE_BIT |
459 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT |
460 VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT));
461
462 switch (image->type) {
463 default:
464 unreachable("bad VkImageType");
465 case VK_IMAGE_TYPE_1D:
466 case VK_IMAGE_TYPE_2D:
467 assert(range->baseArrayLayer + range->layerCount - 1 <= image->array_size);
468 break;
469 case VK_IMAGE_TYPE_3D:
470 assert(range->baseArrayLayer + range->layerCount - 1
471 <= anv_minify(image->extent.depth, range->baseMipLevel));
472 break;
473 }
474
475 struct anv_surface *surface =
476 anv_image_get_surface_for_aspect_mask(image, range->aspectMask);
477
478 iview->image = image;
479 iview->bo = image->bo;
480 iview->offset = image->offset + surface->offset;
481
482 iview->aspect_mask = pCreateInfo->subresourceRange.aspectMask;
483 iview->vk_format = pCreateInfo->format;
484 iview->format = anv_get_isl_format(pCreateInfo->format, iview->aspect_mask,
485 image->tiling);
486
487 iview->base_layer = range->baseArrayLayer;
488 iview->base_mip = range->baseMipLevel;
489 iview->extent = (VkExtent3D) {
490 .width = anv_minify(image->extent.width, range->baseMipLevel),
491 .height = anv_minify(image->extent.height, range->baseMipLevel),
492 .depth = anv_minify(image->extent.depth, range->baseMipLevel),
493 };
494
495 if (image->needs_nonrt_surface_state) {
496 iview->nonrt_surface_state = alloc_surface_state(device, cmd_buffer);
497
498 anv_fill_image_surface_state(device, iview->nonrt_surface_state.map,
499 iview, pCreateInfo,
500 VK_IMAGE_USAGE_SAMPLED_BIT);
501 } else {
502 iview->nonrt_surface_state.alloc_size = 0;
503 }
504
505 if (image->needs_color_rt_surface_state) {
506 iview->color_rt_surface_state = alloc_surface_state(device, cmd_buffer);
507
508 anv_fill_image_surface_state(device, iview->color_rt_surface_state.map,
509 iview, pCreateInfo,
510 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT);
511 } else {
512 iview->color_rt_surface_state.alloc_size = 0;
513 }
514
515 if (image->needs_storage_surface_state) {
516 iview->storage_surface_state = alloc_surface_state(device, cmd_buffer);
517
518 anv_fill_image_surface_state(device, iview->storage_surface_state.map,
519 iview, pCreateInfo,
520 VK_IMAGE_USAGE_STORAGE_BIT);
521 } else {
522 iview->storage_surface_state.alloc_size = 0;
523 }
524 }
525
526 VkResult
527 anv_CreateImageView(VkDevice _device,
528 const VkImageViewCreateInfo *pCreateInfo,
529 const VkAllocationCallbacks *pAllocator,
530 VkImageView *pView)
531 {
532 ANV_FROM_HANDLE(anv_device, device, _device);
533 struct anv_image_view *view;
534
535 view = anv_alloc2(&device->alloc, pAllocator, sizeof(*view), 8,
536 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
537 if (view == NULL)
538 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
539
540 anv_image_view_init(view, device, pCreateInfo, NULL);
541
542 *pView = anv_image_view_to_handle(view);
543
544 return VK_SUCCESS;
545 }
546
547 void
548 anv_DestroyImageView(VkDevice _device, VkImageView _iview,
549 const VkAllocationCallbacks *pAllocator)
550 {
551 ANV_FROM_HANDLE(anv_device, device, _device);
552 ANV_FROM_HANDLE(anv_image_view, iview, _iview);
553
554 if (iview->image->needs_color_rt_surface_state) {
555 anv_state_pool_free(&device->surface_state_pool,
556 iview->color_rt_surface_state);
557 }
558
559 if (iview->image->needs_nonrt_surface_state) {
560 anv_state_pool_free(&device->surface_state_pool,
561 iview->nonrt_surface_state);
562 }
563
564 if (iview->image->needs_storage_surface_state) {
565 anv_state_pool_free(&device->surface_state_pool,
566 iview->storage_surface_state);
567 }
568
569 anv_free2(&device->alloc, pAllocator, iview);
570 }
571
572 VkResult
573 anv_CreateBufferView(VkDevice _device,
574 const VkBufferViewCreateInfo *pCreateInfo,
575 const VkAllocationCallbacks *pAllocator,
576 VkBufferView *pView)
577 {
578 ANV_FROM_HANDLE(anv_device, device, _device);
579 ANV_FROM_HANDLE(anv_buffer, buffer, pCreateInfo->buffer);
580 struct anv_buffer_view *view;
581
582 view = anv_alloc2(&device->alloc, pAllocator, sizeof(*view), 8,
583 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
584 if (!view)
585 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
586
587 const struct anv_format *format =
588 anv_format_for_vk_format(pCreateInfo->format);
589
590 view->format = format->surface_format;
591 view->bo = buffer->bo;
592 view->offset = buffer->offset + pCreateInfo->offset;
593 view->range = pCreateInfo->range == VK_WHOLE_SIZE ?
594 buffer->size - view->offset : pCreateInfo->range;
595
596 if (buffer->usage & VK_BUFFER_USAGE_UNIFORM_TEXEL_BUFFER_BIT) {
597 view->surface_state =
598 anv_state_pool_alloc(&device->surface_state_pool, 64, 64);
599
600 anv_fill_buffer_surface_state(device, view->surface_state.map,
601 view->format,
602 view->offset, view->range,
603 format->isl_layout->bs);
604 } else {
605 view->surface_state = (struct anv_state){ 0 };
606 }
607
608 if (buffer->usage & VK_BUFFER_USAGE_STORAGE_TEXEL_BUFFER_BIT) {
609 view->storage_surface_state =
610 anv_state_pool_alloc(&device->surface_state_pool, 64, 64);
611
612 enum isl_format storage_format =
613 isl_lower_storage_image_format(&device->isl_dev, view->format);
614
615 anv_fill_buffer_surface_state(device, view->storage_surface_state.map,
616 storage_format,
617 view->offset, view->range,
618 format->isl_layout->bs);
619 } else {
620 view->storage_surface_state = (struct anv_state){ 0 };
621 }
622
623 *pView = anv_buffer_view_to_handle(view);
624
625 return VK_SUCCESS;
626 }
627
628 void
629 anv_DestroyBufferView(VkDevice _device, VkBufferView bufferView,
630 const VkAllocationCallbacks *pAllocator)
631 {
632 ANV_FROM_HANDLE(anv_device, device, _device);
633 ANV_FROM_HANDLE(anv_buffer_view, view, bufferView);
634
635 if (view->surface_state.alloc_size > 0)
636 anv_state_pool_free(&device->surface_state_pool,
637 view->surface_state);
638
639 if (view->storage_surface_state.alloc_size > 0)
640 anv_state_pool_free(&device->surface_state_pool,
641 view->storage_surface_state);
642
643 anv_free2(&device->alloc, pAllocator, view);
644 }
645
646 struct anv_surface *
647 anv_image_get_surface_for_aspect_mask(struct anv_image *image, VkImageAspectFlags aspect_mask)
648 {
649 switch (aspect_mask) {
650 case VK_IMAGE_ASPECT_COLOR_BIT:
651 /* Dragons will eat you.
652 *
653 * Meta attaches all destination surfaces as color render targets. Guess
654 * what surface the Meta Dragons really want.
655 */
656 if (image->format->depth_format && image->format->has_stencil) {
657 return &image->depth_surface;
658 } else if (image->format->depth_format) {
659 return &image->depth_surface;
660 } else if (image->format->has_stencil) {
661 return &image->stencil_surface;
662 } else {
663 return &image->color_surface;
664 }
665 break;
666 case VK_IMAGE_ASPECT_DEPTH_BIT:
667 assert(image->format->depth_format);
668 return &image->depth_surface;
669 case VK_IMAGE_ASPECT_STENCIL_BIT:
670 assert(image->format->has_stencil);
671 return &image->stencil_surface;
672 case VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT:
673 if (image->format->depth_format && image->format->has_stencil) {
674 /* FINISHME: The Vulkan spec (git a511ba2) requires support for
675 * combined depth stencil formats. Specifically, it states:
676 *
677 * At least one of ename:VK_FORMAT_D24_UNORM_S8_UINT or
678 * ename:VK_FORMAT_D32_SFLOAT_S8_UINT must be supported.
679 *
680 * Image views with both depth and stencil aspects are only valid for
681 * render target attachments, in which case
682 * cmd_buffer_emit_depth_stencil() will pick out both the depth and
683 * stencil surfaces from the underlying surface.
684 */
685 return &image->depth_surface;
686 } else if (image->format->depth_format) {
687 return &image->depth_surface;
688 } else if (image->format->has_stencil) {
689 return &image->stencil_surface;
690 }
691 /* fallthrough */
692 default:
693 unreachable("image does not have aspect");
694 return NULL;
695 }
696 }
697
698 void
699 anv_image_view_fill_image_param(struct anv_device *device,
700 struct anv_image_view *view,
701 struct brw_image_param *param)
702 {
703 memset(param, 0, sizeof *param);
704 anv_finishme("Actually fill out brw_image_param");
705 }
706
707 void
708 anv_buffer_view_fill_image_param(struct anv_device *device,
709 struct anv_buffer_view *view,
710 struct brw_image_param *param)
711 {
712 /* Set the swizzling shifts to all-ones to effectively disable swizzling --
713 * See emit_address_calculation() in brw_fs_surface_builder.cpp for a more
714 * detailed explanation of these parameters.
715 */
716 param->swizzling[0] = 0xff;
717 param->swizzling[1] = 0xff;
718
719 param->stride[0] = isl_format_layouts[view->format].bs;
720 param->size[0] = view->range / param->stride[0];
721 }