freedreno_resource.c revision 89b26d5a360ebde11a69f2cdefa66e4d6a2a13fd
1/* -*- mode: C; c-file-style: "k&r"; tab-width 4; indent-tabs-mode: t; -*- */ 2 3/* 4 * Copyright (C) 2012 Rob Clark <robclark@freedesktop.org> 5 * 6 * Permission is hereby granted, free of charge, to any person obtaining a 7 * copy of this software and associated documentation files (the "Software"), 8 * to deal in the Software without restriction, including without limitation 9 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 * and/or sell copies of the Software, and to permit persons to whom the 11 * Software is furnished to do so, subject to the following conditions: 12 * 13 * The above copyright notice and this permission notice (including the next 14 * paragraph) shall be included in all copies or substantial portions of the 15 * Software. 16 * 17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 * SOFTWARE. 24 * 25 * Authors: 26 * Rob Clark <robclark@freedesktop.org> 27 */ 28 29#include "util/u_format.h" 30#include "util/u_inlines.h" 31#include "util/u_transfer.h" 32#include "util/u_string.h" 33#include "util/u_surface.h" 34 35#include "freedreno_resource.h" 36#include "freedreno_screen.h" 37#include "freedreno_surface.h" 38#include "freedreno_context.h" 39#include "freedreno_query_hw.h" 40#include "freedreno_util.h" 41 42#include <errno.h> 43 44static void 45realloc_bo(struct fd_resource *rsc, uint32_t size) 46{ 47 struct fd_screen *screen = fd_screen(rsc->base.b.screen); 48 uint32_t flags = DRM_FREEDRENO_GEM_CACHE_WCOMBINE | 49 DRM_FREEDRENO_GEM_TYPE_KMEM; /* TODO */ 50 51 /* if we start using things other than write-combine, 52 * be sure to check for PIPE_RESOURCE_FLAG_MAP_COHERENT 53 */ 54 55 if (rsc->bo) 56 fd_bo_del(rsc->bo); 57 58 rsc->bo = fd_bo_new(screen->dev, size, flags); 59 rsc->timestamp = 0; 60 rsc->dirty = false; 61} 62 63static void fd_resource_transfer_flush_region(struct pipe_context *pctx, 64 struct pipe_transfer *ptrans, 65 const struct pipe_box *box) 66{ 67 struct fd_context *ctx = fd_context(pctx); 68 struct fd_resource *rsc = fd_resource(ptrans->resource); 69 70 if (rsc->dirty) 71 fd_context_render(pctx); 72 73 if (rsc->timestamp) { 74 fd_pipe_wait(ctx->screen->pipe, rsc->timestamp); 75 rsc->timestamp = 0; 76 } 77} 78 79static void 80fd_resource_transfer_unmap(struct pipe_context *pctx, 81 struct pipe_transfer *ptrans) 82{ 83 struct fd_context *ctx = fd_context(pctx); 84 struct fd_resource *rsc = fd_resource(ptrans->resource); 85 if (!(ptrans->usage & PIPE_TRANSFER_UNSYNCHRONIZED)) 86 fd_bo_cpu_fini(rsc->bo); 87 pipe_resource_reference(&ptrans->resource, NULL); 88 util_slab_free(&ctx->transfer_pool, ptrans); 89} 90 91static void * 92fd_resource_transfer_map(struct pipe_context *pctx, 93 struct pipe_resource *prsc, 94 unsigned level, unsigned usage, 95 const struct pipe_box *box, 96 struct pipe_transfer **pptrans) 97{ 98 struct fd_context *ctx = fd_context(pctx); 99 struct fd_resource *rsc = fd_resource(prsc); 100 struct fd_resource_slice *slice = fd_resource_slice(rsc, level); 101 struct pipe_transfer *ptrans; 102 enum pipe_format format = prsc->format; 103 uint32_t op = 0; 104 uint32_t offset; 105 char *buf; 106 int ret = 0; 107 108 DBG("prsc=%p, level=%u, usage=%x", prsc, level, usage); 109 110 ptrans = util_slab_alloc(&ctx->transfer_pool); 111 if (!ptrans) 112 return NULL; 113 114 /* util_slab_alloc() doesn't zero: */ 115 memset(ptrans, 0, sizeof(*ptrans)); 116 117 pipe_resource_reference(&ptrans->resource, prsc); 118 ptrans->level = level; 119 ptrans->usage = usage; 120 ptrans->box = *box; 121 ptrans->stride = slice->pitch * rsc->cpp; 122 ptrans->layer_stride = slice->size0; 123 124 if (usage & PIPE_TRANSFER_READ) 125 op |= DRM_FREEDRENO_PREP_READ; 126 127 if (usage & PIPE_TRANSFER_WRITE) 128 op |= DRM_FREEDRENO_PREP_WRITE; 129 130 /* some state trackers (at least XA) don't do this.. */ 131 if (!(usage & (PIPE_TRANSFER_FLUSH_EXPLICIT | PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE))) 132 fd_resource_transfer_flush_region(pctx, ptrans, box); 133 134 if (usage & PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE) { 135 realloc_bo(rsc, fd_bo_size(rsc->bo)); 136 } else if (!(usage & PIPE_TRANSFER_UNSYNCHRONIZED)) { 137 ret = fd_bo_cpu_prep(rsc->bo, ctx->screen->pipe, op); 138 if (ret) 139 goto fail; 140 } 141 142 buf = fd_bo_map(rsc->bo); 143 if (!buf) { 144 fd_resource_transfer_unmap(pctx, ptrans); 145 return NULL; 146 } 147 148 *pptrans = ptrans; 149 150 if (rsc->layer_first) { 151 offset = slice->offset + 152 box->y / util_format_get_blockheight(format) * ptrans->stride + 153 box->x / util_format_get_blockwidth(format) * rsc->cpp + 154 box->z * rsc->layer_size; 155 } else { 156 offset = slice->offset + 157 box->y / util_format_get_blockheight(format) * ptrans->stride + 158 box->x / util_format_get_blockwidth(format) * rsc->cpp + 159 box->z * slice->size0; 160 } 161 162 return buf + offset; 163 164fail: 165 fd_resource_transfer_unmap(pctx, ptrans); 166 return NULL; 167} 168 169static void 170fd_resource_destroy(struct pipe_screen *pscreen, 171 struct pipe_resource *prsc) 172{ 173 struct fd_resource *rsc = fd_resource(prsc); 174 if (rsc->bo) 175 fd_bo_del(rsc->bo); 176 FREE(rsc); 177} 178 179static boolean 180fd_resource_get_handle(struct pipe_screen *pscreen, 181 struct pipe_resource *prsc, 182 struct winsys_handle *handle) 183{ 184 struct fd_resource *rsc = fd_resource(prsc); 185 186 return fd_screen_bo_get_handle(pscreen, rsc->bo, 187 rsc->slices[0].pitch * rsc->cpp, handle); 188} 189 190 191static const struct u_resource_vtbl fd_resource_vtbl = { 192 .resource_get_handle = fd_resource_get_handle, 193 .resource_destroy = fd_resource_destroy, 194 .transfer_map = fd_resource_transfer_map, 195 .transfer_flush_region = fd_resource_transfer_flush_region, 196 .transfer_unmap = fd_resource_transfer_unmap, 197 .transfer_inline_write = u_default_transfer_inline_write, 198}; 199 200static uint32_t 201setup_slices(struct fd_resource *rsc, uint32_t alignment) 202{ 203 struct pipe_resource *prsc = &rsc->base.b; 204 uint32_t level, size = 0; 205 uint32_t width = prsc->width0; 206 uint32_t height = prsc->height0; 207 uint32_t depth = prsc->depth0; 208 /* in layer_first layout, the level (slice) contains just one 209 * layer (since in fact the layer contains the slices) 210 */ 211 uint32_t layers_in_level = rsc->layer_first ? 1 : prsc->array_size; 212 213 for (level = 0; level <= prsc->last_level; level++) { 214 struct fd_resource_slice *slice = fd_resource_slice(rsc, level); 215 216 slice->pitch = align(width, 32); 217 slice->offset = size; 218 /* 1d array, 2d array, 3d textures (but not cube!) must all have the 219 * same layer size for each miplevel on a3xx. These are also the 220 * targets that have non-1 alignment. 221 */ 222 if (level == 0 || layers_in_level == 1 || alignment == 1) 223 slice->size0 = align(slice->pitch * height * rsc->cpp, alignment); 224 else 225 slice->size0 = rsc->slices[0].size0; 226 227 size += slice->size0 * depth * layers_in_level; 228 229 width = u_minify(width, 1); 230 height = u_minify(height, 1); 231 depth = u_minify(depth, 1); 232 } 233 234 return size; 235} 236 237static uint32_t 238slice_alignment(struct pipe_screen *pscreen, const struct pipe_resource *tmpl) 239{ 240 /* on a3xx, 2d array and 3d textures seem to want their 241 * layers aligned to page boundaries: 242 */ 243 switch (tmpl->target) { 244 case PIPE_TEXTURE_3D: 245 case PIPE_TEXTURE_1D_ARRAY: 246 case PIPE_TEXTURE_2D_ARRAY: 247 return 4096; 248 default: 249 return 1; 250 } 251} 252 253/** 254 * Create a new texture object, using the given template info. 255 */ 256static struct pipe_resource * 257fd_resource_create(struct pipe_screen *pscreen, 258 const struct pipe_resource *tmpl) 259{ 260 struct fd_resource *rsc = CALLOC_STRUCT(fd_resource); 261 struct pipe_resource *prsc = &rsc->base.b; 262 uint32_t size; 263 264 DBG("target=%d, format=%s, %ux%ux%u, array_size=%u, last_level=%u, " 265 "nr_samples=%u, usage=%u, bind=%x, flags=%x", 266 tmpl->target, util_format_name(tmpl->format), 267 tmpl->width0, tmpl->height0, tmpl->depth0, 268 tmpl->array_size, tmpl->last_level, tmpl->nr_samples, 269 tmpl->usage, tmpl->bind, tmpl->flags); 270 271 if (!rsc) 272 return NULL; 273 274 *prsc = *tmpl; 275 276 pipe_reference_init(&prsc->reference, 1); 277 prsc->screen = pscreen; 278 279 rsc->base.vtbl = &fd_resource_vtbl; 280 rsc->cpp = util_format_get_blocksize(tmpl->format); 281 282 assert(rsc->cpp); 283 284 if (is_a4xx(fd_screen(pscreen))) { 285 switch (tmpl->target) { 286 case PIPE_TEXTURE_3D: 287 /* TODO 3D_ARRAY? */ 288 rsc->layer_first = false; 289 break; 290 default: 291 rsc->layer_first = true; 292 break; 293 } 294 } 295 296 size = setup_slices(rsc, slice_alignment(pscreen, tmpl)); 297 298 if (rsc->layer_first) { 299 rsc->layer_size = align(size, 4096); 300 size = rsc->layer_size * prsc->array_size; 301 } 302 303 realloc_bo(rsc, size); 304 if (!rsc->bo) 305 goto fail; 306 307 return prsc; 308fail: 309 fd_resource_destroy(pscreen, prsc); 310 return NULL; 311} 312 313/** 314 * Create a texture from a winsys_handle. The handle is often created in 315 * another process by first creating a pipe texture and then calling 316 * resource_get_handle. 317 */ 318static struct pipe_resource * 319fd_resource_from_handle(struct pipe_screen *pscreen, 320 const struct pipe_resource *tmpl, 321 struct winsys_handle *handle) 322{ 323 struct fd_resource *rsc = CALLOC_STRUCT(fd_resource); 324 struct fd_resource_slice *slice = &rsc->slices[0]; 325 struct pipe_resource *prsc = &rsc->base.b; 326 327 DBG("target=%d, format=%s, %ux%ux%u, array_size=%u, last_level=%u, " 328 "nr_samples=%u, usage=%u, bind=%x, flags=%x", 329 tmpl->target, util_format_name(tmpl->format), 330 tmpl->width0, tmpl->height0, tmpl->depth0, 331 tmpl->array_size, tmpl->last_level, tmpl->nr_samples, 332 tmpl->usage, tmpl->bind, tmpl->flags); 333 334 if (!rsc) 335 return NULL; 336 337 *prsc = *tmpl; 338 339 pipe_reference_init(&prsc->reference, 1); 340 prsc->screen = pscreen; 341 342 rsc->bo = fd_screen_bo_from_handle(pscreen, handle, &slice->pitch); 343 if (!rsc->bo) 344 goto fail; 345 346 rsc->base.vtbl = &fd_resource_vtbl; 347 rsc->cpp = util_format_get_blocksize(tmpl->format); 348 slice->pitch /= rsc->cpp; 349 350 assert(rsc->cpp); 351 352 return prsc; 353 354fail: 355 fd_resource_destroy(pscreen, prsc); 356 return NULL; 357} 358 359static void fd_blitter_pipe_begin(struct fd_context *ctx); 360static void fd_blitter_pipe_end(struct fd_context *ctx); 361 362/** 363 * _copy_region using pipe (3d engine) 364 */ 365static bool 366fd_blitter_pipe_copy_region(struct fd_context *ctx, 367 struct pipe_resource *dst, 368 unsigned dst_level, 369 unsigned dstx, unsigned dsty, unsigned dstz, 370 struct pipe_resource *src, 371 unsigned src_level, 372 const struct pipe_box *src_box) 373{ 374 /* not until we allow rendertargets to be buffers */ 375 if (dst->target == PIPE_BUFFER || src->target == PIPE_BUFFER) 376 return false; 377 378 if (!util_blitter_is_copy_supported(ctx->blitter, dst, src)) 379 return false; 380 381 fd_blitter_pipe_begin(ctx); 382 util_blitter_copy_texture(ctx->blitter, 383 dst, dst_level, dstx, dsty, dstz, 384 src, src_level, src_box); 385 fd_blitter_pipe_end(ctx); 386 387 return true; 388} 389 390/** 391 * Copy a block of pixels from one resource to another. 392 * The resource must be of the same format. 393 * Resources with nr_samples > 1 are not allowed. 394 */ 395static void 396fd_resource_copy_region(struct pipe_context *pctx, 397 struct pipe_resource *dst, 398 unsigned dst_level, 399 unsigned dstx, unsigned dsty, unsigned dstz, 400 struct pipe_resource *src, 401 unsigned src_level, 402 const struct pipe_box *src_box) 403{ 404 struct fd_context *ctx = fd_context(pctx); 405 406 /* TODO if we have 2d core, or other DMA engine that could be used 407 * for simple copies and reasonably easily synchronized with the 3d 408 * core, this is where we'd plug it in.. 409 */ 410 411 /* try blit on 3d pipe: */ 412 if (fd_blitter_pipe_copy_region(ctx, 413 dst, dst_level, dstx, dsty, dstz, 414 src, src_level, src_box)) 415 return; 416 417 /* else fallback to pure sw: */ 418 util_resource_copy_region(pctx, 419 dst, dst_level, dstx, dsty, dstz, 420 src, src_level, src_box); 421} 422 423/** 424 * Optimal hardware path for blitting pixels. 425 * Scaling, format conversion, up- and downsampling (resolve) are allowed. 426 */ 427static void 428fd_blit(struct pipe_context *pctx, const struct pipe_blit_info *blit_info) 429{ 430 struct fd_context *ctx = fd_context(pctx); 431 struct pipe_blit_info info = *blit_info; 432 433 if (info.src.resource->nr_samples > 1 && 434 info.dst.resource->nr_samples <= 1 && 435 !util_format_is_depth_or_stencil(info.src.resource->format) && 436 !util_format_is_pure_integer(info.src.resource->format)) { 437 DBG("color resolve unimplemented"); 438 return; 439 } 440 441 if (util_try_blit_via_copy_region(pctx, &info)) { 442 return; /* done */ 443 } 444 445 if (info.mask & PIPE_MASK_S) { 446 DBG("cannot blit stencil, skipping"); 447 info.mask &= ~PIPE_MASK_S; 448 } 449 450 if (!util_blitter_is_blit_supported(ctx->blitter, &info)) { 451 DBG("blit unsupported %s -> %s", 452 util_format_short_name(info.src.resource->format), 453 util_format_short_name(info.dst.resource->format)); 454 return; 455 } 456 457 fd_blitter_pipe_begin(ctx); 458 util_blitter_blit(ctx->blitter, &info); 459 fd_blitter_pipe_end(ctx); 460} 461 462static void 463fd_blitter_pipe_begin(struct fd_context *ctx) 464{ 465 util_blitter_save_vertex_buffer_slot(ctx->blitter, ctx->vtx.vertexbuf.vb); 466 util_blitter_save_vertex_elements(ctx->blitter, ctx->vtx.vtx); 467 util_blitter_save_vertex_shader(ctx->blitter, ctx->prog.vp); 468 util_blitter_save_rasterizer(ctx->blitter, ctx->rasterizer); 469 util_blitter_save_viewport(ctx->blitter, &ctx->viewport); 470 util_blitter_save_scissor(ctx->blitter, &ctx->scissor); 471 util_blitter_save_fragment_shader(ctx->blitter, ctx->prog.fp); 472 util_blitter_save_blend(ctx->blitter, ctx->blend); 473 util_blitter_save_depth_stencil_alpha(ctx->blitter, ctx->zsa); 474 util_blitter_save_stencil_ref(ctx->blitter, &ctx->stencil_ref); 475 util_blitter_save_sample_mask(ctx->blitter, ctx->sample_mask); 476 util_blitter_save_framebuffer(ctx->blitter, &ctx->framebuffer); 477 util_blitter_save_fragment_sampler_states(ctx->blitter, 478 ctx->fragtex.num_samplers, 479 (void **)ctx->fragtex.samplers); 480 util_blitter_save_fragment_sampler_views(ctx->blitter, 481 ctx->fragtex.num_textures, ctx->fragtex.textures); 482 483 fd_hw_query_set_stage(ctx, ctx->ring, FD_STAGE_BLIT); 484} 485 486static void 487fd_blitter_pipe_end(struct fd_context *ctx) 488{ 489 fd_hw_query_set_stage(ctx, ctx->ring, FD_STAGE_NULL); 490} 491 492static void 493fd_flush_resource(struct pipe_context *pctx, struct pipe_resource *prsc) 494{ 495 struct fd_resource *rsc = fd_resource(prsc); 496 497 if (rsc->dirty) 498 fd_context_render(pctx); 499} 500 501void 502fd_resource_screen_init(struct pipe_screen *pscreen) 503{ 504 pscreen->resource_create = fd_resource_create; 505 pscreen->resource_from_handle = fd_resource_from_handle; 506 pscreen->resource_get_handle = u_resource_get_handle_vtbl; 507 pscreen->resource_destroy = u_resource_destroy_vtbl; 508} 509 510void 511fd_resource_context_init(struct pipe_context *pctx) 512{ 513 pctx->transfer_map = u_transfer_map_vtbl; 514 pctx->transfer_flush_region = u_transfer_flush_region_vtbl; 515 pctx->transfer_unmap = u_transfer_unmap_vtbl; 516 pctx->transfer_inline_write = u_transfer_inline_write_vtbl; 517 pctx->create_surface = fd_create_surface; 518 pctx->surface_destroy = fd_surface_destroy; 519 pctx->resource_copy_region = fd_resource_copy_region; 520 pctx->blit = fd_blit; 521 pctx->flush_resource = fd_flush_resource; 522} 523