zink: try copy_region hook for blits where we can't do a regular blit or resolve
[mesa.git] / src / gallium / drivers / zink / zink_blit.c
1 #include "zink_context.h"
2 #include "zink_helpers.h"
3 #include "zink_resource.h"
4 #include "zink_screen.h"
5
6 #include "util/u_blitter.h"
7 #include "util/u_surface.h"
8 #include "util/format/u_format.h"
9
10 static bool
11 blit_resolve(struct zink_context *ctx, const struct pipe_blit_info *info)
12 {
13 if (util_format_get_mask(info->dst.format) != info->mask ||
14 util_format_get_mask(info->src.format) != info->mask ||
15 info->scissor_enable ||
16 info->alpha_blend ||
17 info->render_condition_enable)
18 return false;
19
20 struct zink_resource *src = zink_resource(info->src.resource);
21 struct zink_resource *dst = zink_resource(info->dst.resource);
22
23 struct zink_screen *screen = zink_screen(ctx->base.screen);
24 if (src->format != zink_get_format(screen, info->src.format) ||
25 dst->format != zink_get_format(screen, info->dst.format))
26 return false;
27
28 struct zink_batch *batch = zink_batch_no_rp(ctx);
29
30 zink_batch_reference_resoure(batch, src);
31 zink_batch_reference_resoure(batch, dst);
32
33 if (src->layout != VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL)
34 zink_resource_barrier(batch->cmdbuf, src, src->aspect,
35 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL);
36
37 if (dst->layout != VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL)
38 zink_resource_barrier(batch->cmdbuf, dst, dst->aspect,
39 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL);
40
41 VkImageResolve region = {};
42
43 region.srcSubresource.aspectMask = src->aspect;
44 region.srcSubresource.mipLevel = info->src.level;
45 region.srcSubresource.baseArrayLayer = 0; // no clue
46 region.srcSubresource.layerCount = 1; // no clue
47 region.srcOffset.x = info->src.box.x;
48 region.srcOffset.y = info->src.box.y;
49 region.srcOffset.z = info->src.box.z;
50
51 region.dstSubresource.aspectMask = dst->aspect;
52 region.dstSubresource.mipLevel = info->dst.level;
53 region.dstSubresource.baseArrayLayer = 0; // no clue
54 region.dstSubresource.layerCount = 1; // no clue
55 region.dstOffset.x = info->dst.box.x;
56 region.dstOffset.y = info->dst.box.y;
57 region.dstOffset.z = info->dst.box.z;
58
59 region.extent.width = info->dst.box.width;
60 region.extent.height = info->dst.box.height;
61 region.extent.depth = info->dst.box.depth;
62 vkCmdResolveImage(batch->cmdbuf, src->image, src->layout,
63 dst->image, dst->layout,
64 1, &region);
65
66 return true;
67 }
68
69 static bool
70 blit_native(struct zink_context *ctx, const struct pipe_blit_info *info)
71 {
72 if (util_format_get_mask(info->dst.format) != info->mask ||
73 util_format_get_mask(info->src.format) != info->mask ||
74 info->scissor_enable ||
75 info->alpha_blend ||
76 info->render_condition_enable)
77 return false;
78
79 if (util_format_is_depth_or_stencil(info->dst.format) &&
80 info->dst.format != info->src.format)
81 return false;
82
83 struct zink_resource *src = zink_resource(info->src.resource);
84 struct zink_resource *dst = zink_resource(info->dst.resource);
85
86 struct zink_screen *screen = zink_screen(ctx->base.screen);
87 if (src->format != zink_get_format(screen, info->src.format) ||
88 dst->format != zink_get_format(screen, info->dst.format))
89 return false;
90
91 struct zink_batch *batch = zink_batch_no_rp(ctx);
92 zink_batch_reference_resoure(batch, src);
93 zink_batch_reference_resoure(batch, dst);
94
95 if (src == dst) {
96 /* The Vulkan 1.1 specification says the following about valid usage
97 * of vkCmdBlitImage:
98 *
99 * "srcImageLayout must be VK_IMAGE_LAYOUT_SHARED_PRESENT_KHR,
100 * VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL or VK_IMAGE_LAYOUT_GENERAL"
101 *
102 * and:
103 *
104 * "dstImageLayout must be VK_IMAGE_LAYOUT_SHARED_PRESENT_KHR,
105 * VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL or VK_IMAGE_LAYOUT_GENERAL"
106 *
107 * Since we cant have the same image in two states at the same time,
108 * we're effectively left with VK_IMAGE_LAYOUT_SHARED_PRESENT_KHR or
109 * VK_IMAGE_LAYOUT_GENERAL. And since this isn't a present-related
110 * operation, VK_IMAGE_LAYOUT_GENERAL seems most appropriate.
111 */
112 if (src->layout != VK_IMAGE_LAYOUT_GENERAL)
113 zink_resource_barrier(batch->cmdbuf, src, src->aspect,
114 VK_IMAGE_LAYOUT_GENERAL);
115 } else {
116 if (src->layout != VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL)
117 zink_resource_barrier(batch->cmdbuf, src, src->aspect,
118 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL);
119
120 if (dst->layout != VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL)
121 zink_resource_barrier(batch->cmdbuf, dst, dst->aspect,
122 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL);
123 }
124
125 VkImageBlit region = {};
126 region.srcSubresource.aspectMask = src->aspect;
127 region.srcSubresource.mipLevel = info->src.level;
128 region.srcOffsets[0].x = info->src.box.x;
129 region.srcOffsets[0].y = info->src.box.y;
130 region.srcOffsets[1].x = info->src.box.x + info->src.box.width;
131 region.srcOffsets[1].y = info->src.box.y + info->src.box.height;
132
133 if (src->base.array_size > 1) {
134 region.srcOffsets[0].z = 0;
135 region.srcOffsets[1].z = 1;
136 region.srcSubresource.baseArrayLayer = info->src.box.z;
137 region.srcSubresource.layerCount = info->src.box.depth;
138 } else {
139 region.srcOffsets[0].z = info->src.box.z;
140 region.srcOffsets[1].z = info->src.box.z + info->src.box.depth;
141 region.srcSubresource.baseArrayLayer = 0;
142 region.srcSubresource.layerCount = 1;
143 }
144
145 region.dstSubresource.aspectMask = dst->aspect;
146 region.dstSubresource.mipLevel = info->dst.level;
147 region.dstOffsets[0].x = info->dst.box.x;
148 region.dstOffsets[0].y = info->dst.box.y;
149 region.dstOffsets[1].x = info->dst.box.x + info->dst.box.width;
150 region.dstOffsets[1].y = info->dst.box.y + info->dst.box.height;
151
152 if (dst->base.array_size > 1) {
153 region.dstOffsets[0].z = 0;
154 region.dstOffsets[1].z = 1;
155 region.dstSubresource.baseArrayLayer = info->dst.box.z;
156 region.dstSubresource.layerCount = info->dst.box.depth;
157 } else {
158 region.dstOffsets[0].z = info->dst.box.z;
159 region.dstOffsets[1].z = info->dst.box.z + info->dst.box.depth;
160 region.dstSubresource.baseArrayLayer = 0;
161 region.dstSubresource.layerCount = 1;
162 }
163
164 vkCmdBlitImage(batch->cmdbuf, src->image, src->layout,
165 dst->image, dst->layout,
166 1, &region,
167 zink_filter(info->filter));
168
169 return true;
170 }
171
172 void
173 zink_blit(struct pipe_context *pctx,
174 const struct pipe_blit_info *info)
175 {
176 struct zink_context *ctx = zink_context(pctx);
177 if (info->src.resource->nr_samples > 1 &&
178 info->dst.resource->nr_samples <= 1) {
179 if (blit_resolve(ctx, info))
180 return;
181 } else {
182 if (blit_native(ctx, info))
183 return;
184 }
185
186 struct zink_resource *src = zink_resource(info->src.resource);
187 struct zink_resource *dst = zink_resource(info->dst.resource);
188 /* if we're copying between resources with matching aspects then we can probably just copy_region */
189 if (src->aspect == dst->aspect && util_try_blit_via_copy_region(pctx, info))
190 return;
191
192 if (!util_blitter_is_blit_supported(ctx->blitter, info)) {
193 debug_printf("blit unsupported %s -> %s\n",
194 util_format_short_name(info->src.resource->format),
195 util_format_short_name(info->dst.resource->format));
196 return;
197 }
198
199 util_blitter_save_blend(ctx->blitter, ctx->gfx_pipeline_state.blend_state);
200 util_blitter_save_depth_stencil_alpha(ctx->blitter, ctx->gfx_pipeline_state.depth_stencil_alpha_state);
201 util_blitter_save_vertex_elements(ctx->blitter, ctx->element_state);
202 util_blitter_save_stencil_ref(ctx->blitter, &ctx->stencil_ref);
203 util_blitter_save_rasterizer(ctx->blitter, ctx->rast_state);
204 util_blitter_save_fragment_shader(ctx->blitter, ctx->gfx_stages[PIPE_SHADER_FRAGMENT]);
205 util_blitter_save_vertex_shader(ctx->blitter, ctx->gfx_stages[PIPE_SHADER_VERTEX]);
206 util_blitter_save_framebuffer(ctx->blitter, &ctx->fb_state);
207 util_blitter_save_viewport(ctx->blitter, ctx->viewport_states);
208 util_blitter_save_scissor(ctx->blitter, ctx->scissor_states);
209 util_blitter_save_fragment_sampler_states(ctx->blitter,
210 ctx->num_samplers[PIPE_SHADER_FRAGMENT],
211 ctx->sampler_states[PIPE_SHADER_FRAGMENT]);
212 util_blitter_save_fragment_sampler_views(ctx->blitter,
213 ctx->num_image_views[PIPE_SHADER_FRAGMENT],
214 ctx->image_views[PIPE_SHADER_FRAGMENT]);
215 util_blitter_save_fragment_constant_buffer_slot(ctx->blitter, ctx->ubos[PIPE_SHADER_FRAGMENT]);
216 util_blitter_save_vertex_buffer_slot(ctx->blitter, ctx->buffers);
217 util_blitter_save_sample_mask(ctx->blitter, ctx->gfx_pipeline_state.sample_mask);
218 util_blitter_save_so_targets(ctx->blitter, ctx->num_so_targets, ctx->so_targets);
219
220 util_blitter_blit(ctx->blitter, info);
221 }