freedreno_resource.c revision 575a6e7ec57dc70f96e2618748ed07c7882cf8c1
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_util.h" 40 41#include <errno.h> 42 43static void 44realloc_bo(struct fd_resource *rsc, uint32_t size) 45{ 46 struct fd_screen *screen = fd_screen(rsc->base.b.screen); 47 uint32_t flags = DRM_FREEDRENO_GEM_CACHE_WCOMBINE | 48 DRM_FREEDRENO_GEM_TYPE_KMEM; /* TODO */ 49 50 if (rsc->bo) 51 fd_bo_del(rsc->bo); 52 53 rsc->bo = fd_bo_new(screen->dev, size, flags); 54 rsc->timestamp = 0; 55 rsc->dirty = false; 56} 57 58static void fd_resource_transfer_flush_region(struct pipe_context *pctx, 59 struct pipe_transfer *ptrans, 60 const struct pipe_box *box) 61{ 62 struct fd_context *ctx = fd_context(pctx); 63 struct fd_resource *rsc = fd_resource(ptrans->resource); 64 65 if (rsc->dirty) 66 fd_context_render(pctx); 67 68 if (rsc->timestamp) { 69 fd_pipe_wait(ctx->screen->pipe, rsc->timestamp); 70 rsc->timestamp = 0; 71 } 72} 73 74static void 75fd_resource_transfer_unmap(struct pipe_context *pctx, 76 struct pipe_transfer *ptrans) 77{ 78 struct fd_context *ctx = fd_context(pctx); 79 struct fd_resource *rsc = fd_resource(ptrans->resource); 80 if (!(ptrans->usage & PIPE_TRANSFER_UNSYNCHRONIZED)) 81 fd_bo_cpu_fini(rsc->bo); 82 pipe_resource_reference(&ptrans->resource, NULL); 83 util_slab_free(&ctx->transfer_pool, ptrans); 84} 85 86static void * 87fd_resource_transfer_map(struct pipe_context *pctx, 88 struct pipe_resource *prsc, 89 unsigned level, unsigned usage, 90 const struct pipe_box *box, 91 struct pipe_transfer **pptrans) 92{ 93 struct fd_context *ctx = fd_context(pctx); 94 struct fd_resource *rsc = fd_resource(prsc); 95 struct fd_resource_slice *slice = fd_resource_slice(rsc, level); 96 struct pipe_transfer *ptrans; 97 enum pipe_format format = prsc->format; 98 uint32_t op = 0; 99 char *buf; 100 int ret = 0; 101 102 ptrans = util_slab_alloc(&ctx->transfer_pool); 103 if (!ptrans) 104 return NULL; 105 106 /* util_slab_alloc() doesn't zero: */ 107 memset(ptrans, 0, sizeof(*ptrans)); 108 109 pipe_resource_reference(&ptrans->resource, prsc); 110 ptrans->level = level; 111 ptrans->usage = usage; 112 ptrans->box = *box; 113 ptrans->stride = slice->pitch * rsc->cpp; 114 ptrans->layer_stride = ptrans->stride; 115 116 if (usage & PIPE_TRANSFER_READ) 117 op |= DRM_FREEDRENO_PREP_READ; 118 119 if (usage & PIPE_TRANSFER_WRITE) 120 op |= DRM_FREEDRENO_PREP_WRITE; 121 122 if (usage & PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE) 123 op |= DRM_FREEDRENO_PREP_NOSYNC; 124 125 /* some state trackers (at least XA) don't do this.. */ 126 if (!(usage & (PIPE_TRANSFER_FLUSH_EXPLICIT | PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE))) 127 fd_resource_transfer_flush_region(pctx, ptrans, box); 128 129 if (!(usage & PIPE_TRANSFER_UNSYNCHRONIZED)) { 130 ret = fd_bo_cpu_prep(rsc->bo, ctx->screen->pipe, op); 131 if ((ret == -EBUSY) && (usage & PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE)) 132 realloc_bo(rsc, fd_bo_size(rsc->bo)); 133 else if (ret) 134 goto fail; 135 } 136 137 buf = fd_bo_map(rsc->bo); 138 if (!buf) { 139 fd_resource_transfer_unmap(pctx, ptrans); 140 return NULL; 141 } 142 143 *pptrans = ptrans; 144 145 return buf + slice->offset + 146 box->y / util_format_get_blockheight(format) * ptrans->stride + 147 box->x / util_format_get_blockwidth(format) * rsc->cpp + 148 box->z * slice->size0; 149 150fail: 151 fd_resource_transfer_unmap(pctx, ptrans); 152 return NULL; 153} 154 155static void 156fd_resource_destroy(struct pipe_screen *pscreen, 157 struct pipe_resource *prsc) 158{ 159 struct fd_resource *rsc = fd_resource(prsc); 160 fd_bo_del(rsc->bo); 161 FREE(rsc); 162} 163 164static boolean 165fd_resource_get_handle(struct pipe_screen *pscreen, 166 struct pipe_resource *prsc, 167 struct winsys_handle *handle) 168{ 169 struct fd_resource *rsc = fd_resource(prsc); 170 171 return fd_screen_bo_get_handle(pscreen, rsc->bo, 172 rsc->slices[0].pitch * rsc->cpp, handle); 173} 174 175 176static const struct u_resource_vtbl fd_resource_vtbl = { 177 .resource_get_handle = fd_resource_get_handle, 178 .resource_destroy = fd_resource_destroy, 179 .transfer_map = fd_resource_transfer_map, 180 .transfer_flush_region = fd_resource_transfer_flush_region, 181 .transfer_unmap = fd_resource_transfer_unmap, 182 .transfer_inline_write = u_default_transfer_inline_write, 183}; 184 185static uint32_t 186setup_slices(struct fd_resource *rsc) 187{ 188 struct pipe_resource *prsc = &rsc->base.b; 189 uint32_t level, size = 0; 190 uint32_t width = prsc->width0; 191 uint32_t height = prsc->height0; 192 uint32_t depth = prsc->depth0; 193 194 for (level = 0; level <= prsc->last_level; level++) { 195 struct fd_resource_slice *slice = fd_resource_slice(rsc, level); 196 uint32_t aligned_width = align(width, 32); 197 198 slice->pitch = aligned_width; 199 slice->offset = size; 200 slice->size0 = slice->pitch * height * rsc->cpp; 201 202 size += slice->size0 * depth * prsc->array_size; 203 204 width = u_minify(width, 1); 205 height = u_minify(height, 1); 206 depth = u_minify(depth, 1); 207 } 208 209 return size; 210} 211 212/** 213 * Create a new texture object, using the given template info. 214 */ 215static struct pipe_resource * 216fd_resource_create(struct pipe_screen *pscreen, 217 const struct pipe_resource *tmpl) 218{ 219 struct fd_resource *rsc = CALLOC_STRUCT(fd_resource); 220 struct pipe_resource *prsc = &rsc->base.b; 221 uint32_t size; 222 223 DBG("target=%d, format=%s, %ux%ux%u, array_size=%u, last_level=%u, " 224 "nr_samples=%u, usage=%u, bind=%x, flags=%x", 225 tmpl->target, util_format_name(tmpl->format), 226 tmpl->width0, tmpl->height0, tmpl->depth0, 227 tmpl->array_size, tmpl->last_level, tmpl->nr_samples, 228 tmpl->usage, tmpl->bind, tmpl->flags); 229 230 if (!rsc) 231 return NULL; 232 233 *prsc = *tmpl; 234 235 pipe_reference_init(&prsc->reference, 1); 236 prsc->screen = pscreen; 237 238 rsc->base.vtbl = &fd_resource_vtbl; 239 rsc->cpp = util_format_get_blocksize(tmpl->format); 240 241 assert(rsc->cpp); 242 243 size = setup_slices(rsc); 244 245 realloc_bo(rsc, size); 246 247 return prsc; 248} 249 250/** 251 * Create a texture from a winsys_handle. The handle is often created in 252 * another process by first creating a pipe texture and then calling 253 * resource_get_handle. 254 */ 255static struct pipe_resource * 256fd_resource_from_handle(struct pipe_screen *pscreen, 257 const struct pipe_resource *tmpl, 258 struct winsys_handle *handle) 259{ 260 struct fd_resource *rsc = CALLOC_STRUCT(fd_resource); 261 struct fd_resource_slice *slice = &rsc->slices[0]; 262 struct pipe_resource *prsc = &rsc->base.b; 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->bo = fd_screen_bo_from_handle(pscreen, handle, &slice->pitch); 280 281 rsc->base.vtbl = &fd_resource_vtbl; 282 rsc->cpp = util_format_get_blocksize(tmpl->format); 283 slice->pitch /= rsc->cpp; 284 285 assert(rsc->cpp); 286 287 return prsc; 288} 289 290static bool render_blit(struct pipe_context *pctx, struct pipe_blit_info *info); 291 292/** 293 * Copy a block of pixels from one resource to another. 294 * The resource must be of the same format. 295 * Resources with nr_samples > 1 are not allowed. 296 */ 297static void 298fd_resource_copy_region(struct pipe_context *pctx, 299 struct pipe_resource *dst, 300 unsigned dst_level, 301 unsigned dstx, unsigned dsty, unsigned dstz, 302 struct pipe_resource *src, 303 unsigned src_level, 304 const struct pipe_box *src_box) 305{ 306 /* TODO if we have 2d core, or other DMA engine that could be used 307 * for simple copies and reasonably easily synchronized with the 3d 308 * core, this is where we'd plug it in.. 309 */ 310 struct pipe_blit_info info = { 311 .dst = { 312 .resource = dst, 313 .box = { 314 .x = dstx, 315 .y = dsty, 316 .z = dstz, 317 .width = src_box->width, 318 .height = src_box->height, 319 .depth = src_box->depth, 320 }, 321 .format = util_format_linear(dst->format), 322 }, 323 .src = { 324 .resource = src, 325 .box = *src_box, 326 .format = util_format_linear(src->format), 327 }, 328 .mask = PIPE_MASK_RGBA, 329 .filter = PIPE_TEX_FILTER_NEAREST, 330 }; 331 render_blit(pctx, &info); 332} 333 334/* Optimal hardware path for blitting pixels. 335 * Scaling, format conversion, up- and downsampling (resolve) are allowed. 336 */ 337static void 338fd_blit(struct pipe_context *pctx, const struct pipe_blit_info *blit_info) 339{ 340 struct pipe_blit_info info = *blit_info; 341 342 if (info.src.resource->nr_samples > 1 && 343 info.dst.resource->nr_samples <= 1 && 344 !util_format_is_depth_or_stencil(info.src.resource->format) && 345 !util_format_is_pure_integer(info.src.resource->format)) { 346 DBG("color resolve unimplemented"); 347 return; 348 } 349 350 if (util_try_blit_via_copy_region(pctx, &info)) { 351 return; /* done */ 352 } 353 354 if (info.mask & PIPE_MASK_S) { 355 DBG("cannot blit stencil, skipping"); 356 info.mask &= ~PIPE_MASK_S; 357 } 358 359 render_blit(pctx, &info); 360} 361 362static bool 363render_blit(struct pipe_context *pctx, struct pipe_blit_info *info) 364{ 365 struct fd_context *ctx = fd_context(pctx); 366 367 if (!util_blitter_is_blit_supported(ctx->blitter, info)) { 368 DBG("blit unsupported %s -> %s", 369 util_format_short_name(info->src.resource->format), 370 util_format_short_name(info->dst.resource->format)); 371 return false; 372 } 373 374 util_blitter_save_vertex_buffer_slot(ctx->blitter, ctx->vertexbuf.vb); 375 util_blitter_save_vertex_elements(ctx->blitter, ctx->vtx); 376 util_blitter_save_vertex_shader(ctx->blitter, ctx->prog.vp); 377 util_blitter_save_rasterizer(ctx->blitter, ctx->rasterizer); 378 util_blitter_save_viewport(ctx->blitter, &ctx->viewport); 379 util_blitter_save_scissor(ctx->blitter, &ctx->scissor); 380 util_blitter_save_fragment_shader(ctx->blitter, ctx->prog.fp); 381 util_blitter_save_blend(ctx->blitter, ctx->blend); 382 util_blitter_save_depth_stencil_alpha(ctx->blitter, ctx->zsa); 383 util_blitter_save_stencil_ref(ctx->blitter, &ctx->stencil_ref); 384 util_blitter_save_sample_mask(ctx->blitter, ctx->sample_mask); 385 util_blitter_save_framebuffer(ctx->blitter, &ctx->framebuffer); 386 util_blitter_save_fragment_sampler_states(ctx->blitter, 387 ctx->fragtex.num_samplers, 388 (void **)ctx->fragtex.samplers); 389 util_blitter_save_fragment_sampler_views(ctx->blitter, 390 ctx->fragtex.num_textures, ctx->fragtex.textures); 391 392 util_blitter_blit(ctx->blitter, info); 393 394 return true; 395} 396 397void 398fd_resource_screen_init(struct pipe_screen *pscreen) 399{ 400 pscreen->resource_create = fd_resource_create; 401 pscreen->resource_from_handle = fd_resource_from_handle; 402 pscreen->resource_get_handle = u_resource_get_handle_vtbl; 403 pscreen->resource_destroy = u_resource_destroy_vtbl; 404} 405 406void 407fd_resource_context_init(struct pipe_context *pctx) 408{ 409 pctx->transfer_map = u_transfer_map_vtbl; 410 pctx->transfer_flush_region = u_transfer_flush_region_vtbl; 411 pctx->transfer_unmap = u_transfer_unmap_vtbl; 412 pctx->transfer_inline_write = u_transfer_inline_write_vtbl; 413 pctx->create_surface = fd_create_surface; 414 pctx->surface_destroy = fd_surface_destroy; 415 pctx->resource_copy_region = fd_resource_copy_region; 416 pctx->blit = fd_blit; 417} 418