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