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