1696cb8cc9ca75604866e6423f62eee0c5afa4fb
[mesa.git] / src / gallium / drivers / freedreno / freedreno_resource.c
1 /*
2 * Copyright (C) 2012 Rob Clark <robclark@freedesktop.org>
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 *
23 * Authors:
24 * Rob Clark <robclark@freedesktop.org>
25 */
26
27 #include "util/format/u_format.h"
28 #include "util/format/u_format_rgtc.h"
29 #include "util/format/u_format_zs.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 #include "util/set.h"
35 #include "util/u_drm.h"
36
37 #include "freedreno_resource.h"
38 #include "freedreno_batch_cache.h"
39 #include "freedreno_blitter.h"
40 #include "freedreno_fence.h"
41 #include "freedreno_screen.h"
42 #include "freedreno_surface.h"
43 #include "freedreno_context.h"
44 #include "freedreno_query_hw.h"
45 #include "freedreno_util.h"
46
47 #include "drm-uapi/drm_fourcc.h"
48 #include <errno.h>
49
50 /* XXX this should go away, needed for 'struct winsys_handle' */
51 #include "frontend/drm_driver.h"
52
53 /* A private modifier for now, so we have a way to request tiled but not
54 * compressed. It would perhaps be good to get real modifiers for the
55 * tiled formats, but would probably need to do some work to figure out
56 * the layout(s) of the tiled modes, and whether they are the same
57 * across generations.
58 */
59 #define FD_FORMAT_MOD_QCOM_TILED fourcc_mod_code(QCOM, 0xffffffff)
60
61 /**
62 * Go through the entire state and see if the resource is bound
63 * anywhere. If it is, mark the relevant state as dirty. This is
64 * called on realloc_bo to ensure the necessary state is re-
65 * emitted so the GPU looks at the new backing bo.
66 */
67 static void
68 rebind_resource_in_ctx(struct fd_context *ctx, struct fd_resource *rsc)
69 {
70 struct pipe_resource *prsc = &rsc->base;
71
72 if (ctx->rebind_resource)
73 ctx->rebind_resource(ctx, rsc);
74
75 /* VBOs */
76 if (rsc->dirty & FD_DIRTY_VTXBUF) {
77 struct fd_vertexbuf_stateobj *vb = &ctx->vtx.vertexbuf;
78 for (unsigned i = 0; i < vb->count && !(ctx->dirty & FD_DIRTY_VTXBUF); i++) {
79 if (vb->vb[i].buffer.resource == prsc)
80 ctx->dirty |= FD_DIRTY_VTXBUF;
81 }
82 }
83
84 const enum fd_dirty_3d_state per_stage_dirty =
85 FD_DIRTY_CONST | FD_DIRTY_TEX | FD_DIRTY_IMAGE | FD_DIRTY_SSBO;
86
87 if (!(rsc->dirty & per_stage_dirty))
88 return;
89
90 /* per-shader-stage resources: */
91 for (unsigned stage = 0; stage < PIPE_SHADER_TYPES; stage++) {
92 /* Constbufs.. note that constbuf[0] is normal uniforms emitted in
93 * cmdstream rather than by pointer..
94 */
95 if ((rsc->dirty & FD_DIRTY_CONST) &&
96 !(ctx->dirty_shader[stage] & FD_DIRTY_CONST)) {
97 struct fd_constbuf_stateobj *cb = &ctx->constbuf[stage];
98 const unsigned num_ubos = util_last_bit(cb->enabled_mask);
99 for (unsigned i = 1; i < num_ubos; i++) {
100 if (cb->cb[i].buffer == prsc) {
101 ctx->dirty_shader[stage] |= FD_DIRTY_SHADER_CONST;
102 ctx->dirty |= FD_DIRTY_CONST;
103 break;
104 }
105 }
106 }
107
108 /* Textures */
109 if ((rsc->dirty & FD_DIRTY_TEX) &&
110 !(ctx->dirty_shader[stage] & FD_DIRTY_TEX)) {
111 struct fd_texture_stateobj *tex = &ctx->tex[stage];
112 for (unsigned i = 0; i < tex->num_textures; i++) {
113 if (tex->textures[i] && (tex->textures[i]->texture == prsc)) {
114 ctx->dirty_shader[stage] |= FD_DIRTY_SHADER_TEX;
115 ctx->dirty |= FD_DIRTY_TEX;
116 break;
117 }
118 }
119 }
120
121 /* Images */
122 if ((rsc->dirty & FD_DIRTY_IMAGE) &&
123 !(ctx->dirty_shader[stage] & FD_DIRTY_IMAGE)) {
124 struct fd_shaderimg_stateobj *si = &ctx->shaderimg[stage];
125 const unsigned num_images = util_last_bit(si->enabled_mask);
126 for (unsigned i = 0; i < num_images; i++) {
127 if (si->si[i].resource == prsc) {
128 ctx->dirty_shader[stage] |= FD_DIRTY_SHADER_IMAGE;
129 ctx->dirty |= FD_DIRTY_IMAGE;
130 break;
131 }
132 }
133 }
134
135 /* SSBOs */
136 if ((rsc->dirty & FD_DIRTY_SSBO) &&
137 !(ctx->dirty_shader[stage] & FD_DIRTY_SSBO)) {
138 struct fd_shaderbuf_stateobj *sb = &ctx->shaderbuf[stage];
139 const unsigned num_ssbos = util_last_bit(sb->enabled_mask);
140 for (unsigned i = 0; i < num_ssbos; i++) {
141 if (sb->sb[i].buffer == prsc) {
142 ctx->dirty_shader[stage] |= FD_DIRTY_SHADER_SSBO;
143 ctx->dirty |= FD_DIRTY_SSBO;
144 break;
145 }
146 }
147 }
148 }
149 }
150
151 static void
152 rebind_resource(struct fd_resource *rsc)
153 {
154 struct fd_screen *screen = fd_screen(rsc->base.screen);
155
156 fd_screen_lock(screen);
157 fd_resource_lock(rsc);
158
159 if (rsc->dirty)
160 list_for_each_entry (struct fd_context, ctx, &screen->context_list, node)
161 rebind_resource_in_ctx(ctx, rsc);
162
163 fd_resource_unlock(rsc);
164 fd_screen_unlock(screen);
165 }
166
167 static void
168 realloc_bo(struct fd_resource *rsc, uint32_t size)
169 {
170 struct pipe_resource *prsc = &rsc->base;
171 struct fd_screen *screen = fd_screen(rsc->base.screen);
172 uint32_t flags = DRM_FREEDRENO_GEM_CACHE_WCOMBINE |
173 DRM_FREEDRENO_GEM_TYPE_KMEM |
174 COND(prsc->bind & PIPE_BIND_SCANOUT, DRM_FREEDRENO_GEM_SCANOUT);
175 /* TODO other flags? */
176
177 /* if we start using things other than write-combine,
178 * be sure to check for PIPE_RESOURCE_FLAG_MAP_COHERENT
179 */
180
181 if (rsc->bo)
182 fd_bo_del(rsc->bo);
183
184 rsc->bo = fd_bo_new(screen->dev, size, flags, "%ux%ux%u@%u:%x",
185 prsc->width0, prsc->height0, prsc->depth0, rsc->layout.cpp, prsc->bind);
186
187 /* Zero out the UBWC area on allocation. This fixes intermittent failures
188 * with UBWC, which I suspect are due to the HW having a hard time
189 * interpreting arbitrary values populating the flags buffer when the BO
190 * was recycled through the bo cache (instead of fresh allocations from
191 * the kernel, which are zeroed). sleep(1) in this spot didn't work
192 * around the issue, but any memset value seems to.
193 */
194 if (rsc->layout.ubwc) {
195 void *buf = fd_bo_map(rsc->bo);
196 memset(buf, 0, rsc->layout.slices[0].offset);
197 }
198
199 rsc->seqno = p_atomic_inc_return(&screen->rsc_seqno);
200 util_range_set_empty(&rsc->valid_buffer_range);
201 fd_bc_invalidate_resource(rsc, true);
202 }
203
204 static void
205 do_blit(struct fd_context *ctx, const struct pipe_blit_info *blit, bool fallback)
206 {
207 struct pipe_context *pctx = &ctx->base;
208
209 /* TODO size threshold too?? */
210 if (fallback || !fd_blit(pctx, blit)) {
211 /* do blit on cpu: */
212 util_resource_copy_region(pctx,
213 blit->dst.resource, blit->dst.level, blit->dst.box.x,
214 blit->dst.box.y, blit->dst.box.z,
215 blit->src.resource, blit->src.level, &blit->src.box);
216 }
217 }
218
219 /**
220 * @rsc: the resource to shadow
221 * @level: the level to discard (if box != NULL, otherwise ignored)
222 * @box: the box to discard (or NULL if none)
223 * @modifier: the modifier for the new buffer state
224 */
225 static bool
226 fd_try_shadow_resource(struct fd_context *ctx, struct fd_resource *rsc,
227 unsigned level, const struct pipe_box *box, uint64_t modifier)
228 {
229 struct pipe_context *pctx = &ctx->base;
230 struct pipe_resource *prsc = &rsc->base;
231 bool fallback = false;
232
233 if (prsc->next)
234 return false;
235
236 /* TODO: somehow munge dimensions and format to copy unsupported
237 * render target format to something that is supported?
238 */
239 if (!pctx->screen->is_format_supported(pctx->screen,
240 prsc->format, prsc->target, prsc->nr_samples,
241 prsc->nr_storage_samples,
242 PIPE_BIND_RENDER_TARGET))
243 fallback = true;
244
245 /* do shadowing back-blits on the cpu for buffers: */
246 if (prsc->target == PIPE_BUFFER)
247 fallback = true;
248
249 bool discard_whole_level = box && util_texrange_covers_whole_level(prsc, level,
250 box->x, box->y, box->z, box->width, box->height, box->depth);
251
252 /* TODO need to be more clever about current level */
253 if ((prsc->target >= PIPE_TEXTURE_2D) && box && !discard_whole_level)
254 return false;
255
256 struct pipe_resource *pshadow =
257 pctx->screen->resource_create_with_modifiers(pctx->screen,
258 prsc, &modifier, 1);
259
260 if (!pshadow)
261 return false;
262
263 assert(!ctx->in_shadow);
264 ctx->in_shadow = true;
265
266 /* get rid of any references that batch-cache might have to us (which
267 * should empty/destroy rsc->batches hashset)
268 */
269 fd_bc_invalidate_resource(rsc, false);
270 rebind_resource(rsc);
271
272 fd_screen_lock(ctx->screen);
273
274 /* Swap the backing bo's, so shadow becomes the old buffer,
275 * blit from shadow to new buffer. From here on out, we
276 * cannot fail.
277 *
278 * Note that we need to do it in this order, otherwise if
279 * we go down cpu blit path, the recursive transfer_map()
280 * sees the wrong status..
281 */
282 struct fd_resource *shadow = fd_resource(pshadow);
283
284 DBG("shadow: %p (%d) -> %p (%d)\n", rsc, rsc->base.reference.count,
285 shadow, shadow->base.reference.count);
286
287 /* TODO valid_buffer_range?? */
288 swap(rsc->bo, shadow->bo);
289 swap(rsc->write_batch, shadow->write_batch);
290 swap(rsc->layout, shadow->layout);
291 rsc->seqno = p_atomic_inc_return(&ctx->screen->rsc_seqno);
292
293 /* at this point, the newly created shadow buffer is not referenced
294 * by any batches, but the existing rsc (probably) is. We need to
295 * transfer those references over:
296 */
297 debug_assert(shadow->batch_mask == 0);
298 struct fd_batch *batch;
299 foreach_batch(batch, &ctx->screen->batch_cache, rsc->batch_mask) {
300 struct set_entry *entry = _mesa_set_search(batch->resources, rsc);
301 _mesa_set_remove(batch->resources, entry);
302 _mesa_set_add(batch->resources, shadow);
303 }
304 swap(rsc->batch_mask, shadow->batch_mask);
305
306 fd_screen_unlock(ctx->screen);
307
308 struct pipe_blit_info blit = {};
309 blit.dst.resource = prsc;
310 blit.dst.format = prsc->format;
311 blit.src.resource = pshadow;
312 blit.src.format = pshadow->format;
313 blit.mask = util_format_get_mask(prsc->format);
314 blit.filter = PIPE_TEX_FILTER_NEAREST;
315
316 #define set_box(field, val) do { \
317 blit.dst.field = (val); \
318 blit.src.field = (val); \
319 } while (0)
320
321 /* blit the other levels in their entirety: */
322 for (unsigned l = 0; l <= prsc->last_level; l++) {
323 if (box && l == level)
324 continue;
325
326 /* just blit whole level: */
327 set_box(level, l);
328 set_box(box.width, u_minify(prsc->width0, l));
329 set_box(box.height, u_minify(prsc->height0, l));
330 set_box(box.depth, u_minify(prsc->depth0, l));
331
332 for (int i = 0; i < prsc->array_size; i++) {
333 set_box(box.z, i);
334 do_blit(ctx, &blit, fallback);
335 }
336 }
337
338 /* deal w/ current level specially, since we might need to split
339 * it up into a couple blits:
340 */
341 if (box && !discard_whole_level) {
342 set_box(level, level);
343
344 switch (prsc->target) {
345 case PIPE_BUFFER:
346 case PIPE_TEXTURE_1D:
347 set_box(box.y, 0);
348 set_box(box.z, 0);
349 set_box(box.height, 1);
350 set_box(box.depth, 1);
351
352 if (box->x > 0) {
353 set_box(box.x, 0);
354 set_box(box.width, box->x);
355
356 do_blit(ctx, &blit, fallback);
357 }
358 if ((box->x + box->width) < u_minify(prsc->width0, level)) {
359 set_box(box.x, box->x + box->width);
360 set_box(box.width, u_minify(prsc->width0, level) - (box->x + box->width));
361
362 do_blit(ctx, &blit, fallback);
363 }
364 break;
365 case PIPE_TEXTURE_2D:
366 /* TODO */
367 default:
368 unreachable("TODO");
369 }
370 }
371
372 ctx->in_shadow = false;
373
374 pipe_resource_reference(&pshadow, NULL);
375
376 return true;
377 }
378
379 /**
380 * Uncompress an UBWC compressed buffer "in place". This works basically
381 * like resource shadowing, creating a new resource, and doing an uncompress
382 * blit, and swapping the state between shadow and original resource so it
383 * appears to the gallium frontends as if nothing changed.
384 */
385 void
386 fd_resource_uncompress(struct fd_context *ctx, struct fd_resource *rsc)
387 {
388 bool success =
389 fd_try_shadow_resource(ctx, rsc, 0, NULL, FD_FORMAT_MOD_QCOM_TILED);
390
391 /* shadow should not fail in any cases where we need to uncompress: */
392 debug_assert(success);
393 }
394
395 static struct fd_resource *
396 fd_alloc_staging(struct fd_context *ctx, struct fd_resource *rsc,
397 unsigned level, const struct pipe_box *box)
398 {
399 struct pipe_context *pctx = &ctx->base;
400 struct pipe_resource tmpl = rsc->base;
401
402 tmpl.width0 = box->width;
403 tmpl.height0 = box->height;
404 /* for array textures, box->depth is the array_size, otherwise
405 * for 3d textures, it is the depth:
406 */
407 if (tmpl.array_size > 1) {
408 if (tmpl.target == PIPE_TEXTURE_CUBE)
409 tmpl.target = PIPE_TEXTURE_2D_ARRAY;
410 tmpl.array_size = box->depth;
411 tmpl.depth0 = 1;
412 } else {
413 tmpl.array_size = 1;
414 tmpl.depth0 = box->depth;
415 }
416 tmpl.last_level = 0;
417 tmpl.bind |= PIPE_BIND_LINEAR;
418
419 struct pipe_resource *pstaging =
420 pctx->screen->resource_create(pctx->screen, &tmpl);
421 if (!pstaging)
422 return NULL;
423
424 return fd_resource(pstaging);
425 }
426
427 static void
428 fd_blit_from_staging(struct fd_context *ctx, struct fd_transfer *trans)
429 {
430 struct pipe_resource *dst = trans->base.resource;
431 struct pipe_blit_info blit = {};
432
433 blit.dst.resource = dst;
434 blit.dst.format = dst->format;
435 blit.dst.level = trans->base.level;
436 blit.dst.box = trans->base.box;
437 blit.src.resource = trans->staging_prsc;
438 blit.src.format = trans->staging_prsc->format;
439 blit.src.level = 0;
440 blit.src.box = trans->staging_box;
441 blit.mask = util_format_get_mask(trans->staging_prsc->format);
442 blit.filter = PIPE_TEX_FILTER_NEAREST;
443
444 do_blit(ctx, &blit, false);
445 }
446
447 static void
448 fd_blit_to_staging(struct fd_context *ctx, struct fd_transfer *trans)
449 {
450 struct pipe_resource *src = trans->base.resource;
451 struct pipe_blit_info blit = {};
452
453 blit.src.resource = src;
454 blit.src.format = src->format;
455 blit.src.level = trans->base.level;
456 blit.src.box = trans->base.box;
457 blit.dst.resource = trans->staging_prsc;
458 blit.dst.format = trans->staging_prsc->format;
459 blit.dst.level = 0;
460 blit.dst.box = trans->staging_box;
461 blit.mask = util_format_get_mask(trans->staging_prsc->format);
462 blit.filter = PIPE_TEX_FILTER_NEAREST;
463
464 do_blit(ctx, &blit, false);
465 }
466
467 static void fd_resource_transfer_flush_region(struct pipe_context *pctx,
468 struct pipe_transfer *ptrans,
469 const struct pipe_box *box)
470 {
471 struct fd_resource *rsc = fd_resource(ptrans->resource);
472
473 if (ptrans->resource->target == PIPE_BUFFER)
474 util_range_add(&rsc->base, &rsc->valid_buffer_range,
475 ptrans->box.x + box->x,
476 ptrans->box.x + box->x + box->width);
477 }
478
479 static void
480 flush_resource(struct fd_context *ctx, struct fd_resource *rsc, unsigned usage)
481 {
482 struct fd_batch *write_batch = NULL;
483
484 fd_screen_lock(ctx->screen);
485 fd_batch_reference_locked(&write_batch, rsc->write_batch);
486 fd_screen_unlock(ctx->screen);
487
488 if (usage & PIPE_TRANSFER_WRITE) {
489 struct fd_batch *batch, *batches[32] = {};
490 uint32_t batch_mask;
491
492 /* This is a bit awkward, probably a fd_batch_flush_locked()
493 * would make things simpler.. but we need to hold the lock
494 * to iterate the batches which reference this resource. So
495 * we must first grab references under a lock, then flush.
496 */
497 fd_screen_lock(ctx->screen);
498 batch_mask = rsc->batch_mask;
499 foreach_batch(batch, &ctx->screen->batch_cache, batch_mask)
500 fd_batch_reference_locked(&batches[batch->idx], batch);
501 fd_screen_unlock(ctx->screen);
502
503 foreach_batch(batch, &ctx->screen->batch_cache, batch_mask)
504 fd_batch_flush(batch);
505
506 foreach_batch(batch, &ctx->screen->batch_cache, batch_mask) {
507 fd_batch_reference(&batches[batch->idx], NULL);
508 }
509 assert(rsc->batch_mask == 0);
510 } else if (write_batch) {
511 fd_batch_flush(write_batch);
512 }
513
514 fd_batch_reference(&write_batch, NULL);
515
516 assert(!rsc->write_batch);
517 }
518
519 static void
520 fd_flush_resource(struct pipe_context *pctx, struct pipe_resource *prsc)
521 {
522 flush_resource(fd_context(pctx), fd_resource(prsc), PIPE_TRANSFER_READ);
523 }
524
525 static void
526 fd_resource_transfer_unmap(struct pipe_context *pctx,
527 struct pipe_transfer *ptrans)
528 {
529 struct fd_context *ctx = fd_context(pctx);
530 struct fd_resource *rsc = fd_resource(ptrans->resource);
531 struct fd_transfer *trans = fd_transfer(ptrans);
532
533 if (trans->staging_prsc) {
534 if (ptrans->usage & PIPE_TRANSFER_WRITE)
535 fd_blit_from_staging(ctx, trans);
536 pipe_resource_reference(&trans->staging_prsc, NULL);
537 }
538
539 if (!(ptrans->usage & PIPE_TRANSFER_UNSYNCHRONIZED)) {
540 fd_bo_cpu_fini(rsc->bo);
541 }
542
543 util_range_add(&rsc->base, &rsc->valid_buffer_range,
544 ptrans->box.x,
545 ptrans->box.x + ptrans->box.width);
546
547 pipe_resource_reference(&ptrans->resource, NULL);
548 slab_free(&ctx->transfer_pool, ptrans);
549 }
550
551 static void *
552 fd_resource_transfer_map(struct pipe_context *pctx,
553 struct pipe_resource *prsc,
554 unsigned level, unsigned usage,
555 const struct pipe_box *box,
556 struct pipe_transfer **pptrans)
557 {
558 struct fd_context *ctx = fd_context(pctx);
559 struct fd_resource *rsc = fd_resource(prsc);
560 struct fd_transfer *trans;
561 struct pipe_transfer *ptrans;
562 enum pipe_format format = prsc->format;
563 uint32_t op = 0;
564 uint32_t offset;
565 char *buf;
566 int ret = 0;
567
568 DBG("prsc=%p, level=%u, usage=%x, box=%dx%d+%d,%d", prsc, level, usage,
569 box->width, box->height, box->x, box->y);
570
571 if ((usage & PIPE_TRANSFER_MAP_DIRECTLY) && rsc->layout.tile_mode) {
572 DBG("CANNOT MAP DIRECTLY!\n");
573 return NULL;
574 }
575
576 ptrans = slab_alloc(&ctx->transfer_pool);
577 if (!ptrans)
578 return NULL;
579
580 /* slab_alloc_st() doesn't zero: */
581 trans = fd_transfer(ptrans);
582 memset(trans, 0, sizeof(*trans));
583
584 pipe_resource_reference(&ptrans->resource, prsc);
585 ptrans->level = level;
586 ptrans->usage = usage;
587 ptrans->box = *box;
588 ptrans->stride = fd_resource_pitch(rsc, level);
589 ptrans->layer_stride = fd_resource_layer_stride(rsc, level);
590
591 /* we always need a staging texture for tiled buffers:
592 *
593 * TODO we might sometimes want to *also* shadow the resource to avoid
594 * splitting a batch.. for ex, mid-frame texture uploads to a tiled
595 * texture.
596 */
597 if (rsc->layout.tile_mode) {
598 struct fd_resource *staging_rsc;
599
600 staging_rsc = fd_alloc_staging(ctx, rsc, level, box);
601 if (staging_rsc) {
602 // TODO for PIPE_TRANSFER_READ, need to do untiling blit..
603 trans->staging_prsc = &staging_rsc->base;
604 trans->base.stride = fd_resource_pitch(staging_rsc, 0);
605 trans->base.layer_stride = fd_resource_layer_stride(staging_rsc, 0);
606 trans->staging_box = *box;
607 trans->staging_box.x = 0;
608 trans->staging_box.y = 0;
609 trans->staging_box.z = 0;
610
611 if (usage & PIPE_TRANSFER_READ) {
612 fd_blit_to_staging(ctx, trans);
613
614 fd_bo_cpu_prep(staging_rsc->bo, ctx->pipe,
615 DRM_FREEDRENO_PREP_READ);
616 }
617
618 buf = fd_bo_map(staging_rsc->bo);
619 offset = 0;
620
621 *pptrans = ptrans;
622
623 ctx->stats.staging_uploads++;
624
625 return buf;
626 }
627 }
628
629 if (ctx->in_shadow && !(usage & PIPE_TRANSFER_READ))
630 usage |= PIPE_TRANSFER_UNSYNCHRONIZED;
631
632 if (usage & PIPE_TRANSFER_READ)
633 op |= DRM_FREEDRENO_PREP_READ;
634
635 if (usage & PIPE_TRANSFER_WRITE)
636 op |= DRM_FREEDRENO_PREP_WRITE;
637
638 bool needs_flush = pending(rsc, !!(usage & PIPE_TRANSFER_WRITE));
639
640 if (usage & PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE) {
641 if (needs_flush || fd_resource_busy(rsc, op)) {
642 rebind_resource(rsc);
643 realloc_bo(rsc, fd_bo_size(rsc->bo));
644 }
645 } else if ((usage & PIPE_TRANSFER_WRITE) &&
646 prsc->target == PIPE_BUFFER &&
647 !util_ranges_intersect(&rsc->valid_buffer_range,
648 box->x, box->x + box->width)) {
649 /* We are trying to write to a previously uninitialized range. No need
650 * to wait.
651 */
652 } else if (!(usage & PIPE_TRANSFER_UNSYNCHRONIZED)) {
653 struct fd_batch *write_batch = NULL;
654
655 /* hold a reference, so it doesn't disappear under us: */
656 fd_context_lock(ctx);
657 fd_batch_reference_locked(&write_batch, rsc->write_batch);
658 fd_context_unlock(ctx);
659
660 if ((usage & PIPE_TRANSFER_WRITE) && write_batch &&
661 write_batch->back_blit) {
662 /* if only thing pending is a back-blit, we can discard it: */
663 fd_batch_reset(write_batch);
664 }
665
666 /* If the GPU is writing to the resource, or if it is reading from the
667 * resource and we're trying to write to it, flush the renders.
668 */
669 bool busy = needs_flush || fd_resource_busy(rsc, op);
670
671 /* if we need to flush/stall, see if we can make a shadow buffer
672 * to avoid this:
673 *
674 * TODO we could go down this path !reorder && !busy_for_read
675 * ie. we only *don't* want to go down this path if the blit
676 * will trigger a flush!
677 */
678 if (ctx->screen->reorder && busy && !(usage & PIPE_TRANSFER_READ) &&
679 (usage & PIPE_TRANSFER_DISCARD_RANGE)) {
680 /* try shadowing only if it avoids a flush, otherwise staging would
681 * be better:
682 */
683 if (needs_flush && fd_try_shadow_resource(ctx, rsc, level,
684 box, DRM_FORMAT_MOD_LINEAR)) {
685 needs_flush = busy = false;
686 ctx->stats.shadow_uploads++;
687 } else {
688 struct fd_resource *staging_rsc;
689
690 if (needs_flush) {
691 flush_resource(ctx, rsc, usage);
692 needs_flush = false;
693 }
694
695 /* in this case, we don't need to shadow the whole resource,
696 * since any draw that references the previous contents has
697 * already had rendering flushed for all tiles. So we can
698 * use a staging buffer to do the upload.
699 */
700 staging_rsc = fd_alloc_staging(ctx, rsc, level, box);
701 if (staging_rsc) {
702 trans->staging_prsc = &staging_rsc->base;
703 trans->base.stride = fd_resource_pitch(staging_rsc, 0);
704 trans->base.layer_stride =
705 fd_resource_layer_stride(staging_rsc, 0);
706 trans->staging_box = *box;
707 trans->staging_box.x = 0;
708 trans->staging_box.y = 0;
709 trans->staging_box.z = 0;
710 buf = fd_bo_map(staging_rsc->bo);
711 offset = 0;
712
713 *pptrans = ptrans;
714
715 fd_batch_reference(&write_batch, NULL);
716
717 ctx->stats.staging_uploads++;
718
719 return buf;
720 }
721 }
722 }
723
724 if (needs_flush) {
725 flush_resource(ctx, rsc, usage);
726 needs_flush = false;
727 }
728
729 fd_batch_reference(&write_batch, NULL);
730
731 /* The GPU keeps track of how the various bo's are being used, and
732 * will wait if necessary for the proper operation to have
733 * completed.
734 */
735 if (busy) {
736 ret = fd_bo_cpu_prep(rsc->bo, ctx->pipe, op);
737 if (ret)
738 goto fail;
739 }
740 }
741
742 buf = fd_bo_map(rsc->bo);
743 offset =
744 box->y / util_format_get_blockheight(format) * ptrans->stride +
745 box->x / util_format_get_blockwidth(format) * rsc->layout.cpp +
746 fd_resource_offset(rsc, level, box->z);
747
748 if (usage & PIPE_TRANSFER_WRITE)
749 rsc->valid = true;
750
751 *pptrans = ptrans;
752
753 return buf + offset;
754
755 fail:
756 fd_resource_transfer_unmap(pctx, ptrans);
757 return NULL;
758 }
759
760 static void
761 fd_resource_destroy(struct pipe_screen *pscreen,
762 struct pipe_resource *prsc)
763 {
764 struct fd_resource *rsc = fd_resource(prsc);
765 fd_bc_invalidate_resource(rsc, true);
766 if (rsc->bo)
767 fd_bo_del(rsc->bo);
768 if (rsc->scanout)
769 renderonly_scanout_destroy(rsc->scanout, fd_screen(pscreen)->ro);
770
771 util_range_destroy(&rsc->valid_buffer_range);
772 simple_mtx_destroy(&rsc->lock);
773 FREE(rsc);
774 }
775
776 static uint64_t
777 fd_resource_modifier(struct fd_resource *rsc)
778 {
779 if (!rsc->layout.tile_mode)
780 return DRM_FORMAT_MOD_LINEAR;
781
782 if (rsc->layout.ubwc_layer_size)
783 return DRM_FORMAT_MOD_QCOM_COMPRESSED;
784
785 /* TODO invent a modifier for tiled but not UBWC buffers: */
786 return DRM_FORMAT_MOD_INVALID;
787 }
788
789 static bool
790 fd_resource_get_handle(struct pipe_screen *pscreen,
791 struct pipe_context *pctx,
792 struct pipe_resource *prsc,
793 struct winsys_handle *handle,
794 unsigned usage)
795 {
796 struct fd_resource *rsc = fd_resource(prsc);
797
798 handle->modifier = fd_resource_modifier(rsc);
799
800 return fd_screen_bo_get_handle(pscreen, rsc->bo, rsc->scanout,
801 fd_resource_pitch(rsc, 0), handle);
802 }
803
804 /* special case to resize query buf after allocated.. */
805 void
806 fd_resource_resize(struct pipe_resource *prsc, uint32_t sz)
807 {
808 struct fd_resource *rsc = fd_resource(prsc);
809
810 debug_assert(prsc->width0 == 0);
811 debug_assert(prsc->target == PIPE_BUFFER);
812 debug_assert(prsc->bind == PIPE_BIND_QUERY_BUFFER);
813
814 prsc->width0 = sz;
815 realloc_bo(rsc, fd_screen(prsc->screen)->setup_slices(rsc));
816 }
817
818 static void
819 fd_resource_layout_init(struct pipe_resource *prsc)
820 {
821 struct fd_resource *rsc = fd_resource(prsc);
822 struct fdl_layout *layout = &rsc->layout;
823
824 layout->format = prsc->format;
825
826 layout->width0 = prsc->width0;
827 layout->height0 = prsc->height0;
828 layout->depth0 = prsc->depth0;
829
830 layout->cpp = util_format_get_blocksize(prsc->format);
831 layout->cpp *= fd_resource_nr_samples(prsc);
832 layout->cpp_shift = ffs(layout->cpp) - 1;
833 }
834
835 /**
836 * Create a new texture object, using the given template info.
837 */
838 static struct pipe_resource *
839 fd_resource_create_with_modifiers(struct pipe_screen *pscreen,
840 const struct pipe_resource *tmpl,
841 const uint64_t *modifiers, int count)
842 {
843 struct fd_screen *screen = fd_screen(pscreen);
844 struct fd_resource *rsc;
845 struct pipe_resource *prsc;
846 enum pipe_format format = tmpl->format;
847 uint32_t size;
848
849 /* when using kmsro, scanout buffers are allocated on the display device
850 * create_with_modifiers() doesn't give us usage flags, so we have to
851 * assume that all calls with modifiers are scanout-possible
852 */
853 if (screen->ro &&
854 ((tmpl->bind & PIPE_BIND_SCANOUT) ||
855 !(count == 1 && modifiers[0] == DRM_FORMAT_MOD_INVALID))) {
856 struct pipe_resource scanout_templat = *tmpl;
857 struct renderonly_scanout *scanout;
858 struct winsys_handle handle;
859
860 /* note: alignment is wrong for a6xx */
861 scanout_templat.width0 = align(tmpl->width0, screen->gmem_alignw);
862
863 scanout = renderonly_scanout_for_resource(&scanout_templat,
864 screen->ro, &handle);
865 if (!scanout)
866 return NULL;
867
868 renderonly_scanout_destroy(scanout, screen->ro);
869
870 assert(handle.type == WINSYS_HANDLE_TYPE_FD);
871 rsc = fd_resource(pscreen->resource_from_handle(pscreen, tmpl,
872 &handle,
873 PIPE_HANDLE_USAGE_FRAMEBUFFER_WRITE));
874 close(handle.handle);
875 if (!rsc)
876 return NULL;
877
878 return &rsc->base;
879 }
880
881 rsc = CALLOC_STRUCT(fd_resource);
882 prsc = &rsc->base;
883
884 DBG("%p: target=%d, format=%s, %ux%ux%u, array_size=%u, last_level=%u, "
885 "nr_samples=%u, usage=%u, bind=%x, flags=%x", prsc,
886 tmpl->target, util_format_name(format),
887 tmpl->width0, tmpl->height0, tmpl->depth0,
888 tmpl->array_size, tmpl->last_level, tmpl->nr_samples,
889 tmpl->usage, tmpl->bind, tmpl->flags);
890
891 if (!rsc)
892 return NULL;
893
894 *prsc = *tmpl;
895 fd_resource_layout_init(prsc);
896
897 #define LINEAR \
898 (PIPE_BIND_SCANOUT | \
899 PIPE_BIND_LINEAR | \
900 PIPE_BIND_DISPLAY_TARGET)
901
902 bool linear = drm_find_modifier(DRM_FORMAT_MOD_LINEAR, modifiers, count);
903 if (tmpl->bind & LINEAR)
904 linear = true;
905
906 if (fd_mesa_debug & FD_DBG_NOTILE)
907 linear = true;
908
909 /* Normally, for non-shared buffers, allow buffer compression if
910 * not shared, otherwise only allow if QCOM_COMPRESSED modifier
911 * is requested:
912 *
913 * TODO we should probably also limit tiled in a similar way,
914 * except we don't have a format modifier for tiled. (We probably
915 * should.)
916 */
917 bool allow_ubwc = drm_find_modifier(DRM_FORMAT_MOD_INVALID, modifiers, count);
918 if (tmpl->bind & PIPE_BIND_SHARED)
919 allow_ubwc = drm_find_modifier(DRM_FORMAT_MOD_QCOM_COMPRESSED, modifiers, count);
920
921 allow_ubwc &= !(fd_mesa_debug & FD_DBG_NOUBWC);
922
923 pipe_reference_init(&prsc->reference, 1);
924
925 prsc->screen = pscreen;
926
927 if (screen->tile_mode &&
928 (tmpl->target != PIPE_BUFFER) &&
929 !linear) {
930 rsc->layout.tile_mode = screen->tile_mode(prsc);
931 }
932
933 util_range_init(&rsc->valid_buffer_range);
934
935 simple_mtx_init(&rsc->lock, mtx_plain);
936
937 rsc->internal_format = format;
938
939 rsc->layout.ubwc = rsc->layout.tile_mode && is_a6xx(screen) && allow_ubwc;
940
941 if (prsc->target == PIPE_BUFFER) {
942 assert(prsc->format == PIPE_FORMAT_R8_UNORM);
943 size = prsc->width0;
944 fdl_layout_buffer(&rsc->layout, size);
945 } else {
946 size = screen->setup_slices(rsc);
947 }
948
949 /* special case for hw-query buffer, which we need to allocate before we
950 * know the size:
951 */
952 if (size == 0) {
953 /* note, semi-intention == instead of & */
954 debug_assert(prsc->bind == PIPE_BIND_QUERY_BUFFER);
955 return prsc;
956 }
957
958 /* Set the layer size if the (non-a6xx) backend hasn't done so. */
959 if (rsc->layout.layer_first && !rsc->layout.layer_size) {
960 rsc->layout.layer_size = align(size, 4096);
961 size = rsc->layout.layer_size * prsc->array_size;
962 }
963
964 if (fd_mesa_debug & FD_DBG_LAYOUT)
965 fdl_dump_layout(&rsc->layout);
966
967 realloc_bo(rsc, size);
968 if (!rsc->bo)
969 goto fail;
970
971 return prsc;
972 fail:
973 fd_resource_destroy(pscreen, prsc);
974 return NULL;
975 }
976
977 static struct pipe_resource *
978 fd_resource_create(struct pipe_screen *pscreen,
979 const struct pipe_resource *tmpl)
980 {
981 const uint64_t mod = DRM_FORMAT_MOD_INVALID;
982 return fd_resource_create_with_modifiers(pscreen, tmpl, &mod, 1);
983 }
984
985 /**
986 * Create a texture from a winsys_handle. The handle is often created in
987 * another process by first creating a pipe texture and then calling
988 * resource_get_handle.
989 */
990 static struct pipe_resource *
991 fd_resource_from_handle(struct pipe_screen *pscreen,
992 const struct pipe_resource *tmpl,
993 struct winsys_handle *handle, unsigned usage)
994 {
995 struct fd_screen *screen = fd_screen(pscreen);
996 struct fd_resource *rsc = CALLOC_STRUCT(fd_resource);
997 struct fdl_slice *slice = fd_resource_slice(rsc, 0);
998 struct pipe_resource *prsc = &rsc->base;
999
1000 DBG("target=%d, format=%s, %ux%ux%u, array_size=%u, last_level=%u, "
1001 "nr_samples=%u, usage=%u, bind=%x, flags=%x",
1002 tmpl->target, util_format_name(tmpl->format),
1003 tmpl->width0, tmpl->height0, tmpl->depth0,
1004 tmpl->array_size, tmpl->last_level, tmpl->nr_samples,
1005 tmpl->usage, tmpl->bind, tmpl->flags);
1006
1007 if (!rsc)
1008 return NULL;
1009
1010 *prsc = *tmpl;
1011 fd_resource_layout_init(prsc);
1012
1013 pipe_reference_init(&prsc->reference, 1);
1014
1015 prsc->screen = pscreen;
1016
1017 util_range_init(&rsc->valid_buffer_range);
1018
1019 simple_mtx_init(&rsc->lock, mtx_plain);
1020
1021 rsc->bo = fd_screen_bo_from_handle(pscreen, handle);
1022 if (!rsc->bo)
1023 goto fail;
1024
1025 rsc->internal_format = tmpl->format;
1026 rsc->layout.pitch0 = handle->stride;
1027 slice->offset = handle->offset;
1028 slice->size0 = handle->stride * prsc->height0;
1029
1030 /* use a pitchalign of gmem_alignw pixels, because GMEM resolve for
1031 * lower alignments is not implemented (but possible for a6xx at least)
1032 *
1033 * for UBWC-enabled resources, layout_resource_for_modifier will further
1034 * validate the pitch and set the right pitchalign
1035 */
1036 rsc->layout.pitchalign =
1037 fdl_cpp_shift(&rsc->layout) + util_logbase2(screen->gmem_alignw);
1038
1039 /* apply the minimum pitchalign (note: actually 4 for a3xx but doesn't matter) */
1040 if (is_a6xx(screen) || is_a5xx(screen))
1041 rsc->layout.pitchalign = MAX2(rsc->layout.pitchalign, 6);
1042 else
1043 rsc->layout.pitchalign = MAX2(rsc->layout.pitchalign, 5);
1044
1045 if (rsc->layout.pitch0 < (prsc->width0 * rsc->layout.cpp) ||
1046 fd_resource_pitch(rsc, 0) != rsc->layout.pitch0)
1047 goto fail;
1048
1049 assert(rsc->layout.cpp);
1050
1051 if (screen->layout_resource_for_modifier(rsc, handle->modifier) < 0)
1052 goto fail;
1053
1054 if (screen->ro) {
1055 rsc->scanout =
1056 renderonly_create_gpu_import_for_resource(prsc, screen->ro, NULL);
1057 /* failure is expected in some cases.. */
1058 }
1059
1060 rsc->valid = true;
1061
1062 return prsc;
1063
1064 fail:
1065 fd_resource_destroy(pscreen, prsc);
1066 return NULL;
1067 }
1068
1069 bool
1070 fd_render_condition_check(struct pipe_context *pctx)
1071 {
1072 struct fd_context *ctx = fd_context(pctx);
1073
1074 if (!ctx->cond_query)
1075 return true;
1076
1077 union pipe_query_result res = { 0 };
1078 bool wait =
1079 ctx->cond_mode != PIPE_RENDER_COND_NO_WAIT &&
1080 ctx->cond_mode != PIPE_RENDER_COND_BY_REGION_NO_WAIT;
1081
1082 if (pctx->get_query_result(pctx, ctx->cond_query, wait, &res))
1083 return (bool)res.u64 != ctx->cond_cond;
1084
1085 return true;
1086 }
1087
1088 static void
1089 fd_invalidate_resource(struct pipe_context *pctx, struct pipe_resource *prsc)
1090 {
1091 struct fd_context *ctx = fd_context(pctx);
1092 struct fd_resource *rsc = fd_resource(prsc);
1093
1094 /*
1095 * TODO I guess we could track that the resource is invalidated and
1096 * use that as a hint to realloc rather than stall in _transfer_map(),
1097 * even in the non-DISCARD_WHOLE_RESOURCE case?
1098 *
1099 * Note: we set dirty bits to trigger invalidate logic fd_draw_vbo
1100 */
1101
1102 if (rsc->write_batch) {
1103 struct fd_batch *batch = rsc->write_batch;
1104 struct pipe_framebuffer_state *pfb = &batch->framebuffer;
1105
1106 if (pfb->zsbuf && pfb->zsbuf->texture == prsc) {
1107 batch->resolve &= ~(FD_BUFFER_DEPTH | FD_BUFFER_STENCIL);
1108 ctx->dirty |= FD_DIRTY_ZSA;
1109 }
1110
1111 for (unsigned i = 0; i < pfb->nr_cbufs; i++) {
1112 if (pfb->cbufs[i] && pfb->cbufs[i]->texture == prsc) {
1113 batch->resolve &= ~(PIPE_CLEAR_COLOR0 << i);
1114 ctx->dirty |= FD_DIRTY_FRAMEBUFFER;
1115 }
1116 }
1117 }
1118
1119 rsc->valid = false;
1120 }
1121
1122 static enum pipe_format
1123 fd_resource_get_internal_format(struct pipe_resource *prsc)
1124 {
1125 return fd_resource(prsc)->internal_format;
1126 }
1127
1128 static void
1129 fd_resource_set_stencil(struct pipe_resource *prsc,
1130 struct pipe_resource *stencil)
1131 {
1132 fd_resource(prsc)->stencil = fd_resource(stencil);
1133 }
1134
1135 static struct pipe_resource *
1136 fd_resource_get_stencil(struct pipe_resource *prsc)
1137 {
1138 struct fd_resource *rsc = fd_resource(prsc);
1139 if (rsc->stencil)
1140 return &rsc->stencil->base;
1141 return NULL;
1142 }
1143
1144 static const struct u_transfer_vtbl transfer_vtbl = {
1145 .resource_create = fd_resource_create,
1146 .resource_destroy = fd_resource_destroy,
1147 .transfer_map = fd_resource_transfer_map,
1148 .transfer_flush_region = fd_resource_transfer_flush_region,
1149 .transfer_unmap = fd_resource_transfer_unmap,
1150 .get_internal_format = fd_resource_get_internal_format,
1151 .set_stencil = fd_resource_set_stencil,
1152 .get_stencil = fd_resource_get_stencil,
1153 };
1154
1155 static const uint64_t supported_modifiers[] = {
1156 DRM_FORMAT_MOD_LINEAR,
1157 };
1158
1159 static int
1160 fd_layout_resource_for_modifier(struct fd_resource *rsc, uint64_t modifier)
1161 {
1162 switch (modifier) {
1163 case DRM_FORMAT_MOD_LINEAR:
1164 /* The dri gallium frontend will pass DRM_FORMAT_MOD_INVALID to us
1165 * when it's called through any of the non-modifier BO create entry
1166 * points. Other drivers will determine tiling from the kernel or
1167 * other legacy backchannels, but for freedreno it just means
1168 * LINEAR. */
1169 case DRM_FORMAT_MOD_INVALID:
1170 return 0;
1171 default:
1172 return -1;
1173 }
1174 }
1175
1176 static struct pipe_memory_object *
1177 fd_memobj_create_from_handle(struct pipe_screen *pscreen,
1178 struct winsys_handle *whandle,
1179 bool dedicated)
1180 {
1181 struct fd_memory_object *memobj = CALLOC_STRUCT(fd_memory_object);
1182 if (!memobj)
1183 return NULL;
1184
1185 struct fd_bo *bo = fd_screen_bo_from_handle(pscreen, whandle);
1186 if (!bo) {
1187 free(memobj);
1188 return NULL;
1189 }
1190
1191 memobj->b.dedicated = dedicated;
1192 memobj->bo = bo;
1193
1194 return &memobj->b;
1195 }
1196
1197 static void
1198 fd_memobj_destroy(struct pipe_screen *pscreen,
1199 struct pipe_memory_object *pmemobj)
1200 {
1201 struct fd_memory_object *memobj = fd_memory_object(pmemobj);
1202
1203 assert(memobj->bo);
1204 fd_bo_del(memobj->bo);
1205
1206 free(pmemobj);
1207 }
1208
1209 void
1210 fd_resource_screen_init(struct pipe_screen *pscreen)
1211 {
1212 struct fd_screen *screen = fd_screen(pscreen);
1213 bool fake_rgtc = screen->gpu_id < 400;
1214
1215 pscreen->resource_create = u_transfer_helper_resource_create;
1216 /* NOTE: u_transfer_helper does not yet support the _with_modifiers()
1217 * variant:
1218 */
1219 pscreen->resource_create_with_modifiers = fd_resource_create_with_modifiers;
1220 pscreen->resource_from_handle = fd_resource_from_handle;
1221 pscreen->resource_get_handle = fd_resource_get_handle;
1222 pscreen->resource_destroy = u_transfer_helper_resource_destroy;
1223
1224 pscreen->transfer_helper = u_transfer_helper_create(&transfer_vtbl,
1225 true, false, fake_rgtc, true);
1226
1227 if (!screen->layout_resource_for_modifier)
1228 screen->layout_resource_for_modifier = fd_layout_resource_for_modifier;
1229 if (!screen->supported_modifiers) {
1230 screen->supported_modifiers = supported_modifiers;
1231 screen->num_supported_modifiers = ARRAY_SIZE(supported_modifiers);
1232 }
1233
1234 /* GL_EXT_memory_object */
1235 pscreen->memobj_create_from_handle = fd_memobj_create_from_handle;
1236 pscreen->memobj_destroy = fd_memobj_destroy;
1237 }
1238
1239 static void
1240 fd_get_sample_position(struct pipe_context *context,
1241 unsigned sample_count, unsigned sample_index,
1242 float *pos_out)
1243 {
1244 /* The following is copied from nouveau/nv50 except for position
1245 * values, which are taken from blob driver */
1246 static const uint8_t pos1[1][2] = { { 0x8, 0x8 } };
1247 static const uint8_t pos2[2][2] = {
1248 { 0xc, 0xc }, { 0x4, 0x4 } };
1249 static const uint8_t pos4[4][2] = {
1250 { 0x6, 0x2 }, { 0xe, 0x6 },
1251 { 0x2, 0xa }, { 0xa, 0xe } };
1252 /* TODO needs to be verified on supported hw */
1253 static const uint8_t pos8[8][2] = {
1254 { 0x9, 0x5 }, { 0x7, 0xb },
1255 { 0xd, 0x9 }, { 0x5, 0x3 },
1256 { 0x3, 0xd }, { 0x1, 0x7 },
1257 { 0xb, 0xf }, { 0xf, 0x1 } };
1258
1259 const uint8_t (*ptr)[2];
1260
1261 switch (sample_count) {
1262 case 1:
1263 ptr = pos1;
1264 break;
1265 case 2:
1266 ptr = pos2;
1267 break;
1268 case 4:
1269 ptr = pos4;
1270 break;
1271 case 8:
1272 ptr = pos8;
1273 break;
1274 default:
1275 assert(0);
1276 return;
1277 }
1278
1279 pos_out[0] = ptr[sample_index][0] / 16.0f;
1280 pos_out[1] = ptr[sample_index][1] / 16.0f;
1281 }
1282
1283 static void
1284 fd_blit_pipe(struct pipe_context *pctx, const struct pipe_blit_info *blit_info)
1285 {
1286 /* wrap fd_blit to return void */
1287 fd_blit(pctx, blit_info);
1288 }
1289
1290 void
1291 fd_resource_context_init(struct pipe_context *pctx)
1292 {
1293 pctx->transfer_map = u_transfer_helper_transfer_map;
1294 pctx->transfer_flush_region = u_transfer_helper_transfer_flush_region;
1295 pctx->transfer_unmap = u_transfer_helper_transfer_unmap;
1296 pctx->buffer_subdata = u_default_buffer_subdata;
1297 pctx->texture_subdata = u_default_texture_subdata;
1298 pctx->create_surface = fd_create_surface;
1299 pctx->surface_destroy = fd_surface_destroy;
1300 pctx->resource_copy_region = fd_resource_copy_region;
1301 pctx->blit = fd_blit_pipe;
1302 pctx->flush_resource = fd_flush_resource;
1303 pctx->invalidate_resource = fd_invalidate_resource;
1304 pctx->get_sample_position = fd_get_sample_position;
1305 }