b165abd9b6c5aa43a4e04322a84af37a3b2a2346
[mesa.git] / src / intel / vulkan / anv_meta_blit2d.c
1 /*
2 * Copyright © 2016 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 "anv_meta.h"
25
26 static VkFormat
27 vk_format_for_size(int bs)
28 {
29 /* The choice of UNORM and UINT formats is very intentional here. Most of
30 * the time, we want to use a UINT format to avoid any rounding error in
31 * the blit. For stencil blits, R8_UINT is required by the hardware.
32 * (It's the only format allowed in conjunction with W-tiling.) Also we
33 * intentionally use the 4-channel formats whenever we can. This is so
34 * that, when we do a RGB <-> RGBX copy, the two formats will line up even
35 * though one of them is 3/4 the size of the other. The choice of UNORM
36 * vs. UINT is also very intentional because Haswell doesn't handle 8 or
37 * 16-bit RGB UINT formats at all so we have to use UNORM there.
38 * Fortunately, the only time we should ever use two different formats in
39 * the table below is for RGB -> RGBA blits and so we will never have any
40 * UNORM/UINT mismatch.
41 */
42 switch (bs) {
43 case 1: return VK_FORMAT_R8_UINT;
44 case 2: return VK_FORMAT_R8G8_UINT;
45 case 3: return VK_FORMAT_R8G8B8_UNORM;
46 case 4: return VK_FORMAT_R8G8B8A8_UNORM;
47 case 6: return VK_FORMAT_R16G16B16_UNORM;
48 case 8: return VK_FORMAT_R16G16B16A16_UNORM;
49 case 12: return VK_FORMAT_R32G32B32_UINT;
50 case 16: return VK_FORMAT_R32G32B32A32_UINT;
51 default:
52 unreachable("Invalid format block size");
53 }
54 }
55
56 void
57 anv_meta_end_blit2d(struct anv_cmd_buffer *cmd_buffer,
58 struct anv_meta_saved_state *save)
59 {
60 anv_meta_restore(save, cmd_buffer);
61 }
62
63 void
64 anv_meta_begin_blit2d(struct anv_cmd_buffer *cmd_buffer,
65 struct anv_meta_saved_state *save)
66 {
67 anv_meta_save(save, cmd_buffer,
68 (1 << VK_DYNAMIC_STATE_VIEWPORT));
69 }
70
71 void
72 anv_meta_blit2d(struct anv_cmd_buffer *cmd_buffer,
73 struct anv_meta_blit2d_surf *src,
74 struct anv_meta_blit2d_surf *dst,
75 unsigned num_rects,
76 struct anv_meta_blit2d_rect *rects)
77 {
78 VkDevice vk_device = anv_device_to_handle(cmd_buffer->device);
79 VkFormat src_format = vk_format_for_size(src->bs);
80 VkFormat dst_format = vk_format_for_size(dst->bs);
81 VkImageUsageFlags src_usage = VK_IMAGE_USAGE_SAMPLED_BIT;
82 VkImageUsageFlags dst_usage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
83
84 for (unsigned r = 0; r < num_rects; ++r) {
85
86 /* Create VkImages */
87 VkImageCreateInfo image_info = {
88 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
89 .imageType = VK_IMAGE_TYPE_2D,
90 .format = 0, /* TEMPLATE */
91 .extent = {
92 .width = 0, /* TEMPLATE */
93 .height = 0, /* TEMPLATE */
94 .depth = 1,
95 },
96 .mipLevels = 1,
97 .arrayLayers = 1,
98 .samples = 1,
99 .tiling = 0, /* TEMPLATE */
100 .usage = 0, /* TEMPLATE */
101 };
102 struct anv_image_create_info anv_image_info = {
103 .vk_info = &image_info,
104 .isl_tiling_flags = 0, /* TEMPLATE */
105 };
106
107 /* The image height is the rect height + src/dst y-offset from the
108 * tile-aligned base address.
109 */
110 struct isl_tile_info tile_info;
111
112 anv_image_info.isl_tiling_flags = 1 << src->tiling;
113 image_info.tiling = anv_image_info.isl_tiling_flags ==
114 ISL_TILING_LINEAR_BIT ?
115 VK_IMAGE_TILING_LINEAR : VK_IMAGE_TILING_OPTIMAL;
116 image_info.usage = src_usage;
117 image_info.format = src_format,
118 isl_tiling_get_info(&cmd_buffer->device->isl_dev, src->tiling, src->bs,
119 &tile_info);
120 image_info.extent.height = rects[r].height +
121 rects[r].src_y % tile_info.height;
122 image_info.extent.width = src->pitch / src->bs;
123 VkImage src_image;
124 anv_image_create(vk_device, &anv_image_info,
125 &cmd_buffer->pool->alloc, &src_image);
126
127 anv_image_info.isl_tiling_flags = 1 << dst->tiling;
128 image_info.tiling = anv_image_info.isl_tiling_flags ==
129 ISL_TILING_LINEAR_BIT ?
130 VK_IMAGE_TILING_LINEAR : VK_IMAGE_TILING_OPTIMAL;
131 image_info.usage = dst_usage;
132 image_info.format = dst_format,
133 isl_tiling_get_info(&cmd_buffer->device->isl_dev, dst->tiling, dst->bs,
134 &tile_info);
135 image_info.extent.height = rects[r].height +
136 rects[r].dst_y % tile_info.height;
137 image_info.extent.width = dst->pitch / dst->bs;
138 VkImage dst_image;
139 anv_image_create(vk_device, &anv_image_info,
140 &cmd_buffer->pool->alloc, &dst_image);
141
142 /* We could use a vk call to bind memory, but that would require
143 * creating a dummy memory object etc. so there's really no point.
144 */
145 anv_image_from_handle(src_image)->bo = src->bo;
146 anv_image_from_handle(src_image)->offset = src->base_offset;
147 anv_image_from_handle(dst_image)->bo = dst->bo;
148 anv_image_from_handle(dst_image)->offset = dst->base_offset;
149
150 /* Create VkImageViews */
151 VkImageViewCreateInfo iview_info = {
152 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
153 .image = 0, /* TEMPLATE */
154 .viewType = VK_IMAGE_VIEW_TYPE_2D,
155 .format = 0, /* TEMPLATE */
156 .subresourceRange = {
157 .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
158 .baseMipLevel = 0,
159 .levelCount = 1,
160 .baseArrayLayer = 0,
161 .layerCount = 1
162 },
163 };
164 uint32_t img_o = 0;
165
166 iview_info.image = src_image;
167 iview_info.format = src_format;
168 VkOffset3D src_offset_el = {0};
169 isl_surf_get_image_intratile_offset_el_xy(&cmd_buffer->device->isl_dev,
170 &anv_image_from_handle(src_image)->
171 color_surface.isl,
172 rects[r].src_x,
173 rects[r].src_y,
174 &img_o,
175 (uint32_t*)&src_offset_el.x,
176 (uint32_t*)&src_offset_el.y);
177
178 struct anv_image_view src_iview;
179 anv_image_view_init(&src_iview, cmd_buffer->device,
180 &iview_info, cmd_buffer, img_o, src_usage);
181
182 iview_info.image = dst_image;
183 iview_info.format = dst_format;
184 VkOffset3D dst_offset_el = {0};
185 isl_surf_get_image_intratile_offset_el_xy(&cmd_buffer->device->isl_dev,
186 &anv_image_from_handle(dst_image)->
187 color_surface.isl,
188 rects[r].dst_x,
189 rects[r].dst_y,
190 &img_o,
191 (uint32_t*)&dst_offset_el.x,
192 (uint32_t*)&dst_offset_el.y);
193 struct anv_image_view dst_iview;
194 anv_image_view_init(&dst_iview, cmd_buffer->device,
195 &iview_info, cmd_buffer, img_o, dst_usage);
196
197 /* Perform blit */
198 meta_emit_blit(cmd_buffer,
199 anv_image_from_handle(src_image),
200 &src_iview,
201 src_offset_el,
202 (VkExtent3D){rects[r].width, rects[r].height, 1},
203 anv_image_from_handle(dst_image),
204 &dst_iview,
205 dst_offset_el,
206 (VkExtent3D){rects[r].width, rects[r].height, 1},
207 VK_FILTER_NEAREST);
208
209 anv_DestroyImage(vk_device, src_image, &cmd_buffer->pool->alloc);
210 anv_DestroyImage(vk_device, dst_image, &cmd_buffer->pool->alloc);
211 }
212 }
213