freedreno: add a reading flag to indicate gpu is reading rsc
[mesa.git] / src / gallium / drivers / freedreno / freedreno_resource.c
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
44 static void
45 realloc_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
63 static void fd_resource_transfer_flush_region(struct pipe_context *pctx,
64 struct pipe_transfer *ptrans,
65 const struct pipe_box *box)
66 {
67 }
68
69 static void
70 fd_resource_transfer_unmap(struct pipe_context *pctx,
71 struct pipe_transfer *ptrans)
72 {
73 struct fd_context *ctx = fd_context(pctx);
74 struct fd_resource *rsc = fd_resource(ptrans->resource);
75 if (!(ptrans->usage & PIPE_TRANSFER_UNSYNCHRONIZED))
76 fd_bo_cpu_fini(rsc->bo);
77 pipe_resource_reference(&ptrans->resource, NULL);
78 util_slab_free(&ctx->transfer_pool, ptrans);
79 }
80
81 static void *
82 fd_resource_transfer_map(struct pipe_context *pctx,
83 struct pipe_resource *prsc,
84 unsigned level, unsigned usage,
85 const struct pipe_box *box,
86 struct pipe_transfer **pptrans)
87 {
88 struct fd_context *ctx = fd_context(pctx);
89 struct fd_resource *rsc = fd_resource(prsc);
90 struct fd_resource_slice *slice = fd_resource_slice(rsc, level);
91 struct pipe_transfer *ptrans;
92 enum pipe_format format = prsc->format;
93 uint32_t op = 0;
94 uint32_t offset;
95 char *buf;
96 int ret = 0;
97
98 DBG("prsc=%p, level=%u, usage=%x", prsc, level, usage);
99
100 ptrans = util_slab_alloc(&ctx->transfer_pool);
101 if (!ptrans)
102 return NULL;
103
104 /* util_slab_alloc() doesn't zero: */
105 memset(ptrans, 0, sizeof(*ptrans));
106
107 pipe_resource_reference(&ptrans->resource, prsc);
108 ptrans->level = level;
109 ptrans->usage = usage;
110 ptrans->box = *box;
111 ptrans->stride = slice->pitch * rsc->cpp;
112 ptrans->layer_stride = slice->size0;
113
114 if (usage & PIPE_TRANSFER_READ)
115 op |= DRM_FREEDRENO_PREP_READ;
116
117 if (usage & PIPE_TRANSFER_WRITE)
118 op |= DRM_FREEDRENO_PREP_WRITE;
119
120 if (usage & PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE) {
121 realloc_bo(rsc, fd_bo_size(rsc->bo));
122 } else if (!(usage & PIPE_TRANSFER_UNSYNCHRONIZED)) {
123 /* If the GPU is writing to the resource, or if it is reading from the
124 * resource and we're trying to write to it, flush the renders.
125 */
126 if (rsc->dirty ||
127 ((ptrans->usage & PIPE_TRANSFER_WRITE) && rsc->reading))
128 fd_context_render(pctx);
129
130 /* The GPU keeps track of how the various bo's are being used, and
131 * will wait if necessary for the proper operation to have
132 * completed.
133 */
134 ret = fd_bo_cpu_prep(rsc->bo, ctx->screen->pipe, op);
135 if (ret)
136 goto fail;
137 }
138
139 buf = fd_bo_map(rsc->bo);
140 if (!buf) {
141 fd_resource_transfer_unmap(pctx, ptrans);
142 return NULL;
143 }
144
145 *pptrans = ptrans;
146
147 if (rsc->layer_first) {
148 offset = slice->offset +
149 box->y / util_format_get_blockheight(format) * ptrans->stride +
150 box->x / util_format_get_blockwidth(format) * rsc->cpp +
151 box->z * rsc->layer_size;
152 } else {
153 offset = slice->offset +
154 box->y / util_format_get_blockheight(format) * ptrans->stride +
155 box->x / util_format_get_blockwidth(format) * rsc->cpp +
156 box->z * slice->size0;
157 }
158
159 return buf + offset;
160
161 fail:
162 fd_resource_transfer_unmap(pctx, ptrans);
163 return NULL;
164 }
165
166 static void
167 fd_resource_destroy(struct pipe_screen *pscreen,
168 struct pipe_resource *prsc)
169 {
170 struct fd_resource *rsc = fd_resource(prsc);
171 if (rsc->bo)
172 fd_bo_del(rsc->bo);
173 FREE(rsc);
174 }
175
176 static boolean
177 fd_resource_get_handle(struct pipe_screen *pscreen,
178 struct pipe_resource *prsc,
179 struct winsys_handle *handle)
180 {
181 struct fd_resource *rsc = fd_resource(prsc);
182
183 return fd_screen_bo_get_handle(pscreen, rsc->bo,
184 rsc->slices[0].pitch * rsc->cpp, handle);
185 }
186
187
188 static const struct u_resource_vtbl fd_resource_vtbl = {
189 .resource_get_handle = fd_resource_get_handle,
190 .resource_destroy = fd_resource_destroy,
191 .transfer_map = fd_resource_transfer_map,
192 .transfer_flush_region = fd_resource_transfer_flush_region,
193 .transfer_unmap = fd_resource_transfer_unmap,
194 .transfer_inline_write = u_default_transfer_inline_write,
195 };
196
197 static uint32_t
198 setup_slices(struct fd_resource *rsc, uint32_t alignment)
199 {
200 struct pipe_resource *prsc = &rsc->base.b;
201 uint32_t level, size = 0;
202 uint32_t width = prsc->width0;
203 uint32_t height = prsc->height0;
204 uint32_t depth = prsc->depth0;
205 /* in layer_first layout, the level (slice) contains just one
206 * layer (since in fact the layer contains the slices)
207 */
208 uint32_t layers_in_level = rsc->layer_first ? 1 : prsc->array_size;
209
210 for (level = 0; level <= prsc->last_level; level++) {
211 struct fd_resource_slice *slice = fd_resource_slice(rsc, level);
212
213 slice->pitch = width = align(width, 32);
214 slice->offset = size;
215 /* 1d array and 2d array textures must all have the same layer size
216 * for each miplevel on a3xx. 3d textures can have different layer
217 * sizes for high levels, but the hw auto-sizer is buggy (or at least
218 * different than what this code does), so as soon as the layer size
219 * range gets into range, we stop reducing it.
220 */
221 if (prsc->target == PIPE_TEXTURE_3D && (
222 level == 1 ||
223 (level > 1 && rsc->slices[level - 1].size0 > 0xf000)))
224 slice->size0 = align(slice->pitch * height * rsc->cpp, alignment);
225 else if (level == 0 || rsc->layer_first || alignment == 1)
226 slice->size0 = align(slice->pitch * height * rsc->cpp, alignment);
227 else
228 slice->size0 = rsc->slices[level - 1].size0;
229
230 size += slice->size0 * depth * layers_in_level;
231
232 width = u_minify(width, 1);
233 height = u_minify(height, 1);
234 depth = u_minify(depth, 1);
235 }
236
237 return size;
238 }
239
240 static uint32_t
241 slice_alignment(struct pipe_screen *pscreen, const struct pipe_resource *tmpl)
242 {
243 /* on a3xx, 2d array and 3d textures seem to want their
244 * layers aligned to page boundaries:
245 */
246 switch (tmpl->target) {
247 case PIPE_TEXTURE_3D:
248 case PIPE_TEXTURE_1D_ARRAY:
249 case PIPE_TEXTURE_2D_ARRAY:
250 return 4096;
251 default:
252 return 1;
253 }
254 }
255
256 /**
257 * Create a new texture object, using the given template info.
258 */
259 static struct pipe_resource *
260 fd_resource_create(struct pipe_screen *pscreen,
261 const struct pipe_resource *tmpl)
262 {
263 struct fd_resource *rsc = CALLOC_STRUCT(fd_resource);
264 struct pipe_resource *prsc = &rsc->base.b;
265 uint32_t size;
266
267 DBG("target=%d, format=%s, %ux%ux%u, array_size=%u, last_level=%u, "
268 "nr_samples=%u, usage=%u, bind=%x, flags=%x",
269 tmpl->target, util_format_name(tmpl->format),
270 tmpl->width0, tmpl->height0, tmpl->depth0,
271 tmpl->array_size, tmpl->last_level, tmpl->nr_samples,
272 tmpl->usage, tmpl->bind, tmpl->flags);
273
274 if (!rsc)
275 return NULL;
276
277 *prsc = *tmpl;
278
279 pipe_reference_init(&prsc->reference, 1);
280 prsc->screen = pscreen;
281
282 rsc->base.vtbl = &fd_resource_vtbl;
283 rsc->cpp = util_format_get_blocksize(tmpl->format);
284
285 assert(rsc->cpp);
286
287 if (is_a4xx(fd_screen(pscreen))) {
288 switch (tmpl->target) {
289 case PIPE_TEXTURE_3D:
290 /* TODO 3D_ARRAY? */
291 rsc->layer_first = false;
292 break;
293 default:
294 rsc->layer_first = true;
295 break;
296 }
297 }
298
299 size = setup_slices(rsc, slice_alignment(pscreen, tmpl));
300
301 if (rsc->layer_first) {
302 rsc->layer_size = align(size, 4096);
303 size = rsc->layer_size * prsc->array_size;
304 }
305
306 realloc_bo(rsc, size);
307 if (!rsc->bo)
308 goto fail;
309
310 return prsc;
311 fail:
312 fd_resource_destroy(pscreen, prsc);
313 return NULL;
314 }
315
316 /**
317 * Create a texture from a winsys_handle. The handle is often created in
318 * another process by first creating a pipe texture and then calling
319 * resource_get_handle.
320 */
321 static struct pipe_resource *
322 fd_resource_from_handle(struct pipe_screen *pscreen,
323 const struct pipe_resource *tmpl,
324 struct winsys_handle *handle)
325 {
326 struct fd_resource *rsc = CALLOC_STRUCT(fd_resource);
327 struct fd_resource_slice *slice = &rsc->slices[0];
328 struct pipe_resource *prsc = &rsc->base.b;
329
330 DBG("target=%d, format=%s, %ux%ux%u, array_size=%u, last_level=%u, "
331 "nr_samples=%u, usage=%u, bind=%x, flags=%x",
332 tmpl->target, util_format_name(tmpl->format),
333 tmpl->width0, tmpl->height0, tmpl->depth0,
334 tmpl->array_size, tmpl->last_level, tmpl->nr_samples,
335 tmpl->usage, tmpl->bind, tmpl->flags);
336
337 if (!rsc)
338 return NULL;
339
340 *prsc = *tmpl;
341
342 pipe_reference_init(&prsc->reference, 1);
343 prsc->screen = pscreen;
344
345 rsc->bo = fd_screen_bo_from_handle(pscreen, handle, &slice->pitch);
346 if (!rsc->bo)
347 goto fail;
348
349 rsc->base.vtbl = &fd_resource_vtbl;
350 rsc->cpp = util_format_get_blocksize(tmpl->format);
351 slice->pitch /= rsc->cpp;
352
353 assert(rsc->cpp);
354
355 return prsc;
356
357 fail:
358 fd_resource_destroy(pscreen, prsc);
359 return NULL;
360 }
361
362 static void fd_blitter_pipe_begin(struct fd_context *ctx);
363 static void fd_blitter_pipe_end(struct fd_context *ctx);
364
365 /**
366 * _copy_region using pipe (3d engine)
367 */
368 static bool
369 fd_blitter_pipe_copy_region(struct fd_context *ctx,
370 struct pipe_resource *dst,
371 unsigned dst_level,
372 unsigned dstx, unsigned dsty, unsigned dstz,
373 struct pipe_resource *src,
374 unsigned src_level,
375 const struct pipe_box *src_box)
376 {
377 /* not until we allow rendertargets to be buffers */
378 if (dst->target == PIPE_BUFFER || src->target == PIPE_BUFFER)
379 return false;
380
381 if (!util_blitter_is_copy_supported(ctx->blitter, dst, src))
382 return false;
383
384 fd_blitter_pipe_begin(ctx);
385 util_blitter_copy_texture(ctx->blitter,
386 dst, dst_level, dstx, dsty, dstz,
387 src, src_level, src_box);
388 fd_blitter_pipe_end(ctx);
389
390 return true;
391 }
392
393 /**
394 * Copy a block of pixels from one resource to another.
395 * The resource must be of the same format.
396 * Resources with nr_samples > 1 are not allowed.
397 */
398 static void
399 fd_resource_copy_region(struct pipe_context *pctx,
400 struct pipe_resource *dst,
401 unsigned dst_level,
402 unsigned dstx, unsigned dsty, unsigned dstz,
403 struct pipe_resource *src,
404 unsigned src_level,
405 const struct pipe_box *src_box)
406 {
407 struct fd_context *ctx = fd_context(pctx);
408
409 /* TODO if we have 2d core, or other DMA engine that could be used
410 * for simple copies and reasonably easily synchronized with the 3d
411 * core, this is where we'd plug it in..
412 */
413
414 /* try blit on 3d pipe: */
415 if (fd_blitter_pipe_copy_region(ctx,
416 dst, dst_level, dstx, dsty, dstz,
417 src, src_level, src_box))
418 return;
419
420 /* else fallback to pure sw: */
421 util_resource_copy_region(pctx,
422 dst, dst_level, dstx, dsty, dstz,
423 src, src_level, src_box);
424 }
425
426 /**
427 * Optimal hardware path for blitting pixels.
428 * Scaling, format conversion, up- and downsampling (resolve) are allowed.
429 */
430 static void
431 fd_blit(struct pipe_context *pctx, const struct pipe_blit_info *blit_info)
432 {
433 struct fd_context *ctx = fd_context(pctx);
434 struct pipe_blit_info info = *blit_info;
435
436 if (info.src.resource->nr_samples > 1 &&
437 info.dst.resource->nr_samples <= 1 &&
438 !util_format_is_depth_or_stencil(info.src.resource->format) &&
439 !util_format_is_pure_integer(info.src.resource->format)) {
440 DBG("color resolve unimplemented");
441 return;
442 }
443
444 if (util_try_blit_via_copy_region(pctx, &info)) {
445 return; /* done */
446 }
447
448 if (info.mask & PIPE_MASK_S) {
449 DBG("cannot blit stencil, skipping");
450 info.mask &= ~PIPE_MASK_S;
451 }
452
453 if (!util_blitter_is_blit_supported(ctx->blitter, &info)) {
454 DBG("blit unsupported %s -> %s",
455 util_format_short_name(info.src.resource->format),
456 util_format_short_name(info.dst.resource->format));
457 return;
458 }
459
460 fd_blitter_pipe_begin(ctx);
461 util_blitter_blit(ctx->blitter, &info);
462 fd_blitter_pipe_end(ctx);
463 }
464
465 static void
466 fd_blitter_pipe_begin(struct fd_context *ctx)
467 {
468 util_blitter_save_vertex_buffer_slot(ctx->blitter, ctx->vtx.vertexbuf.vb);
469 util_blitter_save_vertex_elements(ctx->blitter, ctx->vtx.vtx);
470 util_blitter_save_vertex_shader(ctx->blitter, ctx->prog.vp);
471 util_blitter_save_rasterizer(ctx->blitter, ctx->rasterizer);
472 util_blitter_save_viewport(ctx->blitter, &ctx->viewport);
473 util_blitter_save_scissor(ctx->blitter, &ctx->scissor);
474 util_blitter_save_fragment_shader(ctx->blitter, ctx->prog.fp);
475 util_blitter_save_blend(ctx->blitter, ctx->blend);
476 util_blitter_save_depth_stencil_alpha(ctx->blitter, ctx->zsa);
477 util_blitter_save_stencil_ref(ctx->blitter, &ctx->stencil_ref);
478 util_blitter_save_sample_mask(ctx->blitter, ctx->sample_mask);
479 util_blitter_save_framebuffer(ctx->blitter, &ctx->framebuffer);
480 util_blitter_save_fragment_sampler_states(ctx->blitter,
481 ctx->fragtex.num_samplers,
482 (void **)ctx->fragtex.samplers);
483 util_blitter_save_fragment_sampler_views(ctx->blitter,
484 ctx->fragtex.num_textures, ctx->fragtex.textures);
485
486 fd_hw_query_set_stage(ctx, ctx->ring, FD_STAGE_BLIT);
487 }
488
489 static void
490 fd_blitter_pipe_end(struct fd_context *ctx)
491 {
492 fd_hw_query_set_stage(ctx, ctx->ring, FD_STAGE_NULL);
493 }
494
495 static void
496 fd_flush_resource(struct pipe_context *pctx, struct pipe_resource *prsc)
497 {
498 struct fd_resource *rsc = fd_resource(prsc);
499
500 if (rsc->dirty)
501 fd_context_render(pctx);
502 }
503
504 void
505 fd_resource_screen_init(struct pipe_screen *pscreen)
506 {
507 pscreen->resource_create = fd_resource_create;
508 pscreen->resource_from_handle = fd_resource_from_handle;
509 pscreen->resource_get_handle = u_resource_get_handle_vtbl;
510 pscreen->resource_destroy = u_resource_destroy_vtbl;
511 }
512
513 void
514 fd_resource_context_init(struct pipe_context *pctx)
515 {
516 pctx->transfer_map = u_transfer_map_vtbl;
517 pctx->transfer_flush_region = u_transfer_flush_region_vtbl;
518 pctx->transfer_unmap = u_transfer_unmap_vtbl;
519 pctx->transfer_inline_write = u_transfer_inline_write_vtbl;
520 pctx->create_surface = fd_create_surface;
521 pctx->surface_destroy = fd_surface_destroy;
522 pctx->resource_copy_region = fd_resource_copy_region;
523 pctx->blit = fd_blit;
524 pctx->flush_resource = fd_flush_resource;
525 }