v3d_blit.c revision 01e04c3f
1/* 2 * Copyright © 2015-2017 Broadcom 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 "util/u_format.h" 25#include "util/u_surface.h" 26#include "util/u_blitter.h" 27#include "v3d_context.h" 28 29#if 0 30static struct pipe_surface * 31v3d_get_blit_surface(struct pipe_context *pctx, 32 struct pipe_resource *prsc, unsigned level) 33{ 34 struct pipe_surface tmpl; 35 36 memset(&tmpl, 0, sizeof(tmpl)); 37 tmpl.format = prsc->format; 38 tmpl.u.tex.level = level; 39 tmpl.u.tex.first_layer = 0; 40 tmpl.u.tex.last_layer = 0; 41 42 return pctx->create_surface(pctx, prsc, &tmpl); 43} 44 45static bool 46is_tile_unaligned(unsigned size, unsigned tile_size) 47{ 48 return size & (tile_size - 1); 49} 50 51static bool 52v3d_tile_blit(struct pipe_context *pctx, const struct pipe_blit_info *info) 53{ 54 struct v3d_context *v3d = v3d_context(pctx); 55 bool msaa = (info->src.resource->nr_samples > 1 || 56 info->dst.resource->nr_samples > 1); 57 int tile_width = msaa ? 32 : 64; 58 int tile_height = msaa ? 32 : 64; 59 60 if (util_format_is_depth_or_stencil(info->dst.resource->format)) 61 return false; 62 63 if (info->scissor_enable) 64 return false; 65 66 if ((info->mask & PIPE_MASK_RGBA) == 0) 67 return false; 68 69 if (info->dst.box.x != info->src.box.x || 70 info->dst.box.y != info->src.box.y || 71 info->dst.box.width != info->src.box.width || 72 info->dst.box.height != info->src.box.height) { 73 return false; 74 } 75 76 int dst_surface_width = u_minify(info->dst.resource->width0, 77 info->dst.level); 78 int dst_surface_height = u_minify(info->dst.resource->height0, 79 info->dst.level); 80 if (is_tile_unaligned(info->dst.box.x, tile_width) || 81 is_tile_unaligned(info->dst.box.y, tile_height) || 82 (is_tile_unaligned(info->dst.box.width, tile_width) && 83 info->dst.box.x + info->dst.box.width != dst_surface_width) || 84 (is_tile_unaligned(info->dst.box.height, tile_height) && 85 info->dst.box.y + info->dst.box.height != dst_surface_height)) { 86 return false; 87 } 88 89 /* VC5_PACKET_LOAD_TILE_BUFFER_GENERAL uses the 90 * VC5_PACKET_TILE_RENDERING_MODE_CONFIG's width (determined by our 91 * destination surface) to determine the stride. This may be wrong 92 * when reading from texture miplevels > 0, which are stored in 93 * POT-sized areas. For MSAA, the tile addresses are computed 94 * explicitly by the RCL, but still use the destination width to 95 * determine the stride (which could be fixed by explicitly supplying 96 * it in the ABI). 97 */ 98 struct v3d_resource *rsc = v3d_resource(info->src.resource); 99 100 uint32_t stride; 101 102 if (info->src.resource->nr_samples > 1) 103 stride = align(dst_surface_width, 32) * 4 * rsc->cpp; 104 /* XXX else if (rsc->slices[info->src.level].tiling == VC5_TILING_FORMAT_T) 105 stride = align(dst_surface_width * rsc->cpp, 128); */ 106 else 107 stride = align(dst_surface_width * rsc->cpp, 16); 108 109 if (stride != rsc->slices[info->src.level].stride) 110 return false; 111 112 if (info->dst.resource->format != info->src.resource->format) 113 return false; 114 115 if (false) { 116 fprintf(stderr, "RCL blit from %d,%d to %d,%d (%d,%d)\n", 117 info->src.box.x, 118 info->src.box.y, 119 info->dst.box.x, 120 info->dst.box.y, 121 info->dst.box.width, 122 info->dst.box.height); 123 } 124 125 struct pipe_surface *dst_surf = 126 v3d_get_blit_surface(pctx, info->dst.resource, info->dst.level); 127 struct pipe_surface *src_surf = 128 v3d_get_blit_surface(pctx, info->src.resource, info->src.level); 129 130 v3d_flush_jobs_reading_resource(v3d, info->src.resource); 131 132 struct v3d_job *job = v3d_get_job(v3d, dst_surf, NULL); 133 pipe_surface_reference(&job->color_read, src_surf); 134 135 /* If we're resolving from MSAA to single sample, we still need to run 136 * the engine in MSAA mode for the load. 137 */ 138 if (!job->msaa && info->src.resource->nr_samples > 1) { 139 job->msaa = true; 140 job->tile_width = 32; 141 job->tile_height = 32; 142 } 143 144 job->draw_min_x = info->dst.box.x; 145 job->draw_min_y = info->dst.box.y; 146 job->draw_max_x = info->dst.box.x + info->dst.box.width; 147 job->draw_max_y = info->dst.box.y + info->dst.box.height; 148 job->draw_width = dst_surf->width; 149 job->draw_height = dst_surf->height; 150 151 job->tile_width = tile_width; 152 job->tile_height = tile_height; 153 job->msaa = msaa; 154 job->needs_flush = true; 155 job->resolve |= PIPE_CLEAR_COLOR; 156 157 v3d_job_submit(v3d, job); 158 159 pipe_surface_reference(&dst_surf, NULL); 160 pipe_surface_reference(&src_surf, NULL); 161 162 return true; 163} 164#endif 165 166void 167v3d_blitter_save(struct v3d_context *v3d) 168{ 169 util_blitter_save_fragment_constant_buffer_slot(v3d->blitter, 170 v3d->constbuf[PIPE_SHADER_FRAGMENT].cb); 171 util_blitter_save_vertex_buffer_slot(v3d->blitter, v3d->vertexbuf.vb); 172 util_blitter_save_vertex_elements(v3d->blitter, v3d->vtx); 173 util_blitter_save_vertex_shader(v3d->blitter, v3d->prog.bind_vs); 174 util_blitter_save_so_targets(v3d->blitter, v3d->streamout.num_targets, 175 v3d->streamout.targets); 176 util_blitter_save_rasterizer(v3d->blitter, v3d->rasterizer); 177 util_blitter_save_viewport(v3d->blitter, &v3d->viewport); 178 util_blitter_save_scissor(v3d->blitter, &v3d->scissor); 179 util_blitter_save_fragment_shader(v3d->blitter, v3d->prog.bind_fs); 180 util_blitter_save_blend(v3d->blitter, v3d->blend); 181 util_blitter_save_depth_stencil_alpha(v3d->blitter, v3d->zsa); 182 util_blitter_save_stencil_ref(v3d->blitter, &v3d->stencil_ref); 183 util_blitter_save_sample_mask(v3d->blitter, v3d->sample_mask); 184 util_blitter_save_framebuffer(v3d->blitter, &v3d->framebuffer); 185 util_blitter_save_fragment_sampler_states(v3d->blitter, 186 v3d->fragtex.num_samplers, 187 (void **)v3d->fragtex.samplers); 188 util_blitter_save_fragment_sampler_views(v3d->blitter, 189 v3d->fragtex.num_textures, v3d->fragtex.textures); 190 util_blitter_save_so_targets(v3d->blitter, v3d->streamout.num_targets, 191 v3d->streamout.targets); 192} 193 194static bool 195v3d_render_blit(struct pipe_context *ctx, struct pipe_blit_info *info) 196{ 197 struct v3d_context *v3d = v3d_context(ctx); 198 struct v3d_resource *src = v3d_resource(info->src.resource); 199 struct pipe_resource *tiled = NULL; 200 201 if (!src->tiled) { 202 struct pipe_box box = { 203 .x = 0, 204 .y = 0, 205 .width = u_minify(info->src.resource->width0, 206 info->src.level), 207 .height = u_minify(info->src.resource->height0, 208 info->src.level), 209 .depth = 1, 210 }; 211 struct pipe_resource tmpl = { 212 .target = info->src.resource->target, 213 .format = info->src.resource->format, 214 .width0 = box.width, 215 .height0 = box.height, 216 .depth0 = 1, 217 .array_size = 1, 218 }; 219 tiled = ctx->screen->resource_create(ctx->screen, &tmpl); 220 if (!tiled) { 221 fprintf(stderr, "Failed to create tiled blit temp\n"); 222 return false; 223 } 224 ctx->resource_copy_region(ctx, 225 tiled, 0, 226 0, 0, 0, 227 info->src.resource, info->src.level, 228 &box); 229 info->src.level = 0; 230 info->src.resource = tiled; 231 } 232 233 if (!util_blitter_is_blit_supported(v3d->blitter, info)) { 234 fprintf(stderr, "blit unsupported %s -> %s\n", 235 util_format_short_name(info->src.resource->format), 236 util_format_short_name(info->dst.resource->format)); 237 return false; 238 } 239 240 v3d_blitter_save(v3d); 241 util_blitter_blit(v3d->blitter, info); 242 243 pipe_resource_reference(&tiled, NULL); 244 245 return true; 246} 247 248/* Implement stencil blits by reinterpreting the stencil data as an RGBA8888 249 * or R8 texture. 250 */ 251static void 252v3d_stencil_blit(struct pipe_context *ctx, const struct pipe_blit_info *info) 253{ 254 struct v3d_context *v3d = v3d_context(ctx); 255 struct v3d_resource *src = v3d_resource(info->src.resource); 256 struct v3d_resource *dst = v3d_resource(info->dst.resource); 257 enum pipe_format src_format, dst_format; 258 259 if (src->separate_stencil) { 260 src = src->separate_stencil; 261 src_format = PIPE_FORMAT_R8_UNORM; 262 } else { 263 src_format = PIPE_FORMAT_RGBA8888_UNORM; 264 } 265 266 if (dst->separate_stencil) { 267 dst = dst->separate_stencil; 268 dst_format = PIPE_FORMAT_R8_UNORM; 269 } else { 270 dst_format = PIPE_FORMAT_RGBA8888_UNORM; 271 } 272 273 /* Initialize the surface. */ 274 struct pipe_surface dst_tmpl = { 275 .u.tex = { 276 .level = info->dst.level, 277 .first_layer = info->dst.box.z, 278 .last_layer = info->dst.box.z, 279 }, 280 .format = dst_format, 281 }; 282 struct pipe_surface *dst_surf = 283 ctx->create_surface(ctx, &dst->base, &dst_tmpl); 284 285 /* Initialize the sampler view. */ 286 struct pipe_sampler_view src_tmpl = { 287 .target = src->base.target, 288 .format = src_format, 289 .u.tex = { 290 .first_level = info->src.level, 291 .last_level = info->src.level, 292 .first_layer = 0, 293 .last_layer = (PIPE_TEXTURE_3D ? 294 u_minify(src->base.depth0, 295 info->src.level) - 1 : 296 src->base.array_size - 1), 297 }, 298 .swizzle_r = PIPE_SWIZZLE_X, 299 .swizzle_g = PIPE_SWIZZLE_Y, 300 .swizzle_b = PIPE_SWIZZLE_Z, 301 .swizzle_a = PIPE_SWIZZLE_W, 302 }; 303 struct pipe_sampler_view *src_view = 304 ctx->create_sampler_view(ctx, &src->base, &src_tmpl); 305 306 v3d_blitter_save(v3d); 307 util_blitter_blit_generic(v3d->blitter, dst_surf, &info->dst.box, 308 src_view, &info->src.box, 309 src->base.width0, src->base.height0, 310 PIPE_MASK_R, 311 PIPE_TEX_FILTER_NEAREST, 312 info->scissor_enable ? &info->scissor : NULL, 313 info->alpha_blend); 314 315 pipe_surface_reference(&dst_surf, NULL); 316 pipe_sampler_view_reference(&src_view, NULL); 317} 318 319/* Optimal hardware path for blitting pixels. 320 * Scaling, format conversion, up- and downsampling (resolve) are allowed. 321 */ 322void 323v3d_blit(struct pipe_context *pctx, const struct pipe_blit_info *blit_info) 324{ 325 struct pipe_blit_info info = *blit_info; 326 327 if (info.mask & PIPE_MASK_S) { 328 v3d_stencil_blit(pctx, blit_info); 329 info.mask &= ~PIPE_MASK_S; 330 } 331 332#if 0 333 if (v3d_tile_blit(pctx, blit_info)) 334 return; 335#endif 336 337 v3d_render_blit(pctx, &info); 338} 339