Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mesa
Path: blob/21.2-virgl/src/gallium/drivers/vc4/vc4_resource.c
4570 views
1
/*
2
* Copyright © 2014 Broadcom
3
* Copyright (C) 2012 Rob Clark <[email protected]>
4
*
5
* Permission is hereby granted, free of charge, to any person obtaining a
6
* copy of this software and associated documentation files (the "Software"),
7
* to deal in the Software without restriction, including without limitation
8
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
9
* and/or sell copies of the Software, and to permit persons to whom the
10
* Software is furnished to do so, subject to the following conditions:
11
*
12
* The above copyright notice and this permission notice (including the next
13
* paragraph) shall be included in all copies or substantial portions of the
14
* Software.
15
*
16
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22
* IN THE SOFTWARE.
23
*/
24
25
#include "pipe/p_defines.h"
26
#include "util/u_memory.h"
27
#include "util/format/u_format.h"
28
#include "util/u_inlines.h"
29
#include "util/u_surface.h"
30
#include "util/u_transfer_helper.h"
31
#include "util/u_upload_mgr.h"
32
#include "util/u_drm.h"
33
34
#include "drm-uapi/drm_fourcc.h"
35
#include "drm-uapi/vc4_drm.h"
36
#include "vc4_screen.h"
37
#include "vc4_context.h"
38
#include "vc4_resource.h"
39
#include "vc4_tiling.h"
40
41
static bool
42
vc4_resource_bo_alloc(struct vc4_resource *rsc)
43
{
44
struct pipe_resource *prsc = &rsc->base;
45
struct pipe_screen *pscreen = prsc->screen;
46
struct vc4_bo *bo;
47
48
if (vc4_debug & VC4_DEBUG_SURFACE) {
49
fprintf(stderr, "alloc %p: size %d + offset %d -> %d\n",
50
rsc,
51
rsc->slices[0].size,
52
rsc->slices[0].offset,
53
rsc->slices[0].offset +
54
rsc->slices[0].size +
55
rsc->cube_map_stride * (prsc->array_size - 1));
56
}
57
58
bo = vc4_bo_alloc(vc4_screen(pscreen),
59
rsc->slices[0].offset +
60
rsc->slices[0].size +
61
rsc->cube_map_stride * (prsc->array_size - 1),
62
"resource");
63
if (bo) {
64
vc4_bo_unreference(&rsc->bo);
65
rsc->bo = bo;
66
return true;
67
} else {
68
return false;
69
}
70
}
71
72
static void
73
vc4_resource_transfer_unmap(struct pipe_context *pctx,
74
struct pipe_transfer *ptrans)
75
{
76
struct vc4_context *vc4 = vc4_context(pctx);
77
struct vc4_transfer *trans = vc4_transfer(ptrans);
78
79
if (trans->map) {
80
struct vc4_resource *rsc = vc4_resource(ptrans->resource);
81
struct vc4_resource_slice *slice = &rsc->slices[ptrans->level];
82
83
if (ptrans->usage & PIPE_MAP_WRITE) {
84
vc4_store_tiled_image(rsc->bo->map + slice->offset +
85
ptrans->box.z * rsc->cube_map_stride,
86
slice->stride,
87
trans->map, ptrans->stride,
88
slice->tiling, rsc->cpp,
89
&ptrans->box);
90
}
91
free(trans->map);
92
}
93
94
pipe_resource_reference(&ptrans->resource, NULL);
95
slab_free(&vc4->transfer_pool, ptrans);
96
}
97
98
static void *
99
vc4_resource_transfer_map(struct pipe_context *pctx,
100
struct pipe_resource *prsc,
101
unsigned level, unsigned usage,
102
const struct pipe_box *box,
103
struct pipe_transfer **pptrans)
104
{
105
struct vc4_context *vc4 = vc4_context(pctx);
106
struct vc4_resource *rsc = vc4_resource(prsc);
107
struct vc4_transfer *trans;
108
struct pipe_transfer *ptrans;
109
enum pipe_format format = prsc->format;
110
char *buf;
111
112
/* Upgrade DISCARD_RANGE to WHOLE_RESOURCE if the whole resource is
113
* being mapped.
114
*/
115
if ((usage & PIPE_MAP_DISCARD_RANGE) &&
116
!(usage & PIPE_MAP_UNSYNCHRONIZED) &&
117
!(prsc->flags & PIPE_RESOURCE_FLAG_MAP_PERSISTENT) &&
118
prsc->last_level == 0 &&
119
prsc->width0 == box->width &&
120
prsc->height0 == box->height &&
121
prsc->depth0 == box->depth &&
122
prsc->array_size == 1 &&
123
rsc->bo->private) {
124
usage |= PIPE_MAP_DISCARD_WHOLE_RESOURCE;
125
}
126
127
if (usage & PIPE_MAP_DISCARD_WHOLE_RESOURCE) {
128
if (vc4_resource_bo_alloc(rsc)) {
129
/* If it might be bound as one of our vertex buffers,
130
* make sure we re-emit vertex buffer state.
131
*/
132
if (prsc->bind & PIPE_BIND_VERTEX_BUFFER)
133
vc4->dirty |= VC4_DIRTY_VTXBUF;
134
} else {
135
/* If we failed to reallocate, flush users so that we
136
* don't violate any syncing requirements.
137
*/
138
vc4_flush_jobs_reading_resource(vc4, prsc);
139
}
140
} else if (!(usage & PIPE_MAP_UNSYNCHRONIZED)) {
141
/* If we're writing and the buffer is being used by the CL, we
142
* have to flush the CL first. If we're only reading, we need
143
* to flush if the CL has written our buffer.
144
*/
145
if (usage & PIPE_MAP_WRITE)
146
vc4_flush_jobs_reading_resource(vc4, prsc);
147
else
148
vc4_flush_jobs_writing_resource(vc4, prsc);
149
}
150
151
if (usage & PIPE_MAP_WRITE) {
152
rsc->writes++;
153
rsc->initialized_buffers = ~0;
154
}
155
156
trans = slab_alloc(&vc4->transfer_pool);
157
if (!trans)
158
return NULL;
159
160
/* XXX: Handle DONTBLOCK, DISCARD_RANGE, PERSISTENT, COHERENT. */
161
162
/* slab_alloc_st() doesn't zero: */
163
memset(trans, 0, sizeof(*trans));
164
ptrans = &trans->base;
165
166
pipe_resource_reference(&ptrans->resource, prsc);
167
ptrans->level = level;
168
ptrans->usage = usage;
169
ptrans->box = *box;
170
171
if (usage & PIPE_MAP_UNSYNCHRONIZED)
172
buf = vc4_bo_map_unsynchronized(rsc->bo);
173
else
174
buf = vc4_bo_map(rsc->bo);
175
if (!buf) {
176
fprintf(stderr, "Failed to map bo\n");
177
goto fail;
178
}
179
180
*pptrans = ptrans;
181
182
struct vc4_resource_slice *slice = &rsc->slices[level];
183
if (rsc->tiled) {
184
/* No direct mappings of tiled, since we need to manually
185
* tile/untile.
186
*/
187
if (usage & PIPE_MAP_DIRECTLY)
188
return NULL;
189
190
if (format == PIPE_FORMAT_ETC1_RGB8) {
191
/* ETC1 is arranged as 64-bit blocks, where each block
192
* is 4x4 pixels. Texture tiling operates on the
193
* 64-bit block the way it would an uncompressed
194
* pixels.
195
*/
196
assert(!(ptrans->box.x & 3));
197
assert(!(ptrans->box.y & 3));
198
ptrans->box.x >>= 2;
199
ptrans->box.y >>= 2;
200
ptrans->box.width = (ptrans->box.width + 3) >> 2;
201
ptrans->box.height = (ptrans->box.height + 3) >> 2;
202
}
203
204
ptrans->stride = ptrans->box.width * rsc->cpp;
205
ptrans->layer_stride = ptrans->stride * ptrans->box.height;
206
207
trans->map = malloc(ptrans->layer_stride * ptrans->box.depth);
208
209
if (usage & PIPE_MAP_READ) {
210
vc4_load_tiled_image(trans->map, ptrans->stride,
211
buf + slice->offset +
212
ptrans->box.z * rsc->cube_map_stride,
213
slice->stride,
214
slice->tiling, rsc->cpp,
215
&ptrans->box);
216
}
217
return trans->map;
218
} else {
219
ptrans->stride = slice->stride;
220
ptrans->layer_stride = ptrans->stride;
221
222
return buf + slice->offset +
223
ptrans->box.y / util_format_get_blockheight(format) * ptrans->stride +
224
ptrans->box.x / util_format_get_blockwidth(format) * rsc->cpp +
225
ptrans->box.z * rsc->cube_map_stride;
226
}
227
228
229
fail:
230
vc4_resource_transfer_unmap(pctx, ptrans);
231
return NULL;
232
}
233
234
static void
235
vc4_texture_subdata(struct pipe_context *pctx,
236
struct pipe_resource *prsc,
237
unsigned level,
238
unsigned usage,
239
const struct pipe_box *box,
240
const void *data,
241
unsigned stride,
242
unsigned layer_stride)
243
{
244
struct vc4_resource *rsc = vc4_resource(prsc);
245
struct vc4_resource_slice *slice = &rsc->slices[level];
246
247
/* For a direct mapping, we can just take the u_transfer path. */
248
if (!rsc->tiled ||
249
box->depth != 1 ||
250
(usage & PIPE_MAP_DISCARD_WHOLE_RESOURCE)) {
251
return u_default_texture_subdata(pctx, prsc, level, usage, box,
252
data, stride, layer_stride);
253
}
254
255
/* Otherwise, map and store the texture data directly into the tiled
256
* texture.
257
*/
258
void *buf;
259
if (usage & PIPE_MAP_UNSYNCHRONIZED)
260
buf = vc4_bo_map_unsynchronized(rsc->bo);
261
else
262
buf = vc4_bo_map(rsc->bo);
263
264
vc4_store_tiled_image(buf + slice->offset +
265
box->z * rsc->cube_map_stride,
266
slice->stride,
267
(void *)data, stride,
268
slice->tiling, rsc->cpp,
269
box);
270
}
271
272
static void
273
vc4_resource_destroy(struct pipe_screen *pscreen,
274
struct pipe_resource *prsc)
275
{
276
struct vc4_screen *screen = vc4_screen(pscreen);
277
struct vc4_resource *rsc = vc4_resource(prsc);
278
vc4_bo_unreference(&rsc->bo);
279
280
if (rsc->scanout)
281
renderonly_scanout_destroy(rsc->scanout, screen->ro);
282
283
free(rsc);
284
}
285
286
static bool
287
vc4_resource_get_handle(struct pipe_screen *pscreen,
288
struct pipe_context *pctx,
289
struct pipe_resource *prsc,
290
struct winsys_handle *whandle,
291
unsigned usage)
292
{
293
struct vc4_screen *screen = vc4_screen(pscreen);
294
struct vc4_resource *rsc = vc4_resource(prsc);
295
296
whandle->stride = rsc->slices[0].stride;
297
whandle->offset = 0;
298
299
/* If we're passing some reference to our BO out to some other part of
300
* the system, then we can't do any optimizations about only us being
301
* the ones seeing it (like BO caching or shadow update avoidance).
302
*/
303
rsc->bo->private = false;
304
305
if (rsc->tiled)
306
whandle->modifier = DRM_FORMAT_MOD_BROADCOM_VC4_T_TILED;
307
else
308
whandle->modifier = DRM_FORMAT_MOD_LINEAR;
309
310
switch (whandle->type) {
311
case WINSYS_HANDLE_TYPE_SHARED:
312
if (screen->ro) {
313
/* This could probably be supported, assuming that a
314
* control node was used for pl111.
315
*/
316
fprintf(stderr, "flink unsupported with pl111\n");
317
return false;
318
}
319
320
return vc4_bo_flink(rsc->bo, &whandle->handle);
321
case WINSYS_HANDLE_TYPE_KMS:
322
if (screen->ro) {
323
return renderonly_get_handle(rsc->scanout, whandle);
324
}
325
whandle->handle = rsc->bo->handle;
326
return true;
327
case WINSYS_HANDLE_TYPE_FD:
328
/* FDs are cross-device, so we can export directly from vc4.
329
*/
330
whandle->handle = vc4_bo_get_dmabuf(rsc->bo);
331
return whandle->handle != -1;
332
}
333
334
return false;
335
}
336
337
static void
338
vc4_setup_slices(struct vc4_resource *rsc, const char *caller)
339
{
340
struct pipe_resource *prsc = &rsc->base;
341
uint32_t width = prsc->width0;
342
uint32_t height = prsc->height0;
343
if (prsc->format == PIPE_FORMAT_ETC1_RGB8) {
344
width = (width + 3) >> 2;
345
height = (height + 3) >> 2;
346
}
347
348
uint32_t pot_width = util_next_power_of_two(width);
349
uint32_t pot_height = util_next_power_of_two(height);
350
uint32_t offset = 0;
351
uint32_t utile_w = vc4_utile_width(rsc->cpp);
352
uint32_t utile_h = vc4_utile_height(rsc->cpp);
353
354
for (int i = prsc->last_level; i >= 0; i--) {
355
struct vc4_resource_slice *slice = &rsc->slices[i];
356
357
uint32_t level_width, level_height;
358
if (i == 0) {
359
level_width = width;
360
level_height = height;
361
} else {
362
level_width = u_minify(pot_width, i);
363
level_height = u_minify(pot_height, i);
364
}
365
366
if (!rsc->tiled) {
367
slice->tiling = VC4_TILING_FORMAT_LINEAR;
368
if (prsc->nr_samples > 1) {
369
/* MSAA (4x) surfaces are stored as raw tile buffer contents. */
370
level_width = align(level_width, 32);
371
level_height = align(level_height, 32);
372
} else {
373
level_width = align(level_width, utile_w);
374
}
375
} else {
376
if (vc4_size_is_lt(level_width, level_height,
377
rsc->cpp)) {
378
slice->tiling = VC4_TILING_FORMAT_LT;
379
level_width = align(level_width, utile_w);
380
level_height = align(level_height, utile_h);
381
} else {
382
slice->tiling = VC4_TILING_FORMAT_T;
383
level_width = align(level_width,
384
4 * 2 * utile_w);
385
level_height = align(level_height,
386
4 * 2 * utile_h);
387
}
388
}
389
390
slice->offset = offset;
391
slice->stride = (level_width * rsc->cpp *
392
MAX2(prsc->nr_samples, 1));
393
slice->size = level_height * slice->stride;
394
395
offset += slice->size;
396
397
if (vc4_debug & VC4_DEBUG_SURFACE) {
398
static const char tiling_chars[] = {
399
[VC4_TILING_FORMAT_LINEAR] = 'R',
400
[VC4_TILING_FORMAT_LT] = 'L',
401
[VC4_TILING_FORMAT_T] = 'T'
402
};
403
fprintf(stderr,
404
"rsc %s %p (format %s: vc4 %d), %dx%d: "
405
"level %d (%c) -> %dx%d, stride %d@0x%08x\n",
406
caller, rsc,
407
util_format_short_name(prsc->format),
408
rsc->vc4_format,
409
prsc->width0, prsc->height0,
410
i, tiling_chars[slice->tiling],
411
level_width, level_height,
412
slice->stride, slice->offset);
413
}
414
}
415
416
/* The texture base pointer that has to point to level 0 doesn't have
417
* intra-page bits, so we have to align it, and thus shift up all the
418
* smaller slices.
419
*/
420
uint32_t page_align_offset = (align(rsc->slices[0].offset, 4096) -
421
rsc->slices[0].offset);
422
if (page_align_offset) {
423
for (int i = 0; i <= prsc->last_level; i++)
424
rsc->slices[i].offset += page_align_offset;
425
}
426
427
/* Cube map faces appear as whole miptrees at a page-aligned offset
428
* from the first face's miptree.
429
*/
430
if (prsc->target == PIPE_TEXTURE_CUBE) {
431
rsc->cube_map_stride = align(rsc->slices[0].offset +
432
rsc->slices[0].size, 4096);
433
}
434
}
435
436
static struct vc4_resource *
437
vc4_resource_setup(struct pipe_screen *pscreen,
438
const struct pipe_resource *tmpl)
439
{
440
struct vc4_resource *rsc = CALLOC_STRUCT(vc4_resource);
441
if (!rsc)
442
return NULL;
443
struct pipe_resource *prsc = &rsc->base;
444
445
*prsc = *tmpl;
446
447
pipe_reference_init(&prsc->reference, 1);
448
prsc->screen = pscreen;
449
450
if (prsc->nr_samples <= 1)
451
rsc->cpp = util_format_get_blocksize(tmpl->format);
452
else
453
rsc->cpp = sizeof(uint32_t);
454
455
assert(rsc->cpp);
456
457
return rsc;
458
}
459
460
static enum vc4_texture_data_type
461
get_resource_texture_format(struct pipe_resource *prsc)
462
{
463
struct vc4_resource *rsc = vc4_resource(prsc);
464
uint8_t format = vc4_get_tex_format(prsc->format);
465
466
if (!rsc->tiled) {
467
if (prsc->nr_samples > 1) {
468
return ~0;
469
} else {
470
if (format == VC4_TEXTURE_TYPE_RGBA8888)
471
return VC4_TEXTURE_TYPE_RGBA32R;
472
else
473
return ~0;
474
}
475
}
476
477
return format;
478
}
479
480
static struct pipe_resource *
481
vc4_resource_create_with_modifiers(struct pipe_screen *pscreen,
482
const struct pipe_resource *tmpl,
483
const uint64_t *modifiers,
484
int count)
485
{
486
struct vc4_screen *screen = vc4_screen(pscreen);
487
struct vc4_resource *rsc = vc4_resource_setup(pscreen, tmpl);
488
struct pipe_resource *prsc = &rsc->base;
489
bool linear_ok = drm_find_modifier(DRM_FORMAT_MOD_LINEAR, modifiers, count);
490
/* Use a tiled layout if we can, for better 3D performance. */
491
bool should_tile = true;
492
493
/* VBOs/PBOs are untiled (and 1 height). */
494
if (tmpl->target == PIPE_BUFFER)
495
should_tile = false;
496
497
/* MSAA buffers are linear. */
498
if (tmpl->nr_samples > 1)
499
should_tile = false;
500
501
/* No tiling when we're sharing with another device (pl111). */
502
if (screen->ro && (tmpl->bind & PIPE_BIND_SCANOUT))
503
should_tile = false;
504
505
/* Cursors are always linear, and the user can request linear as well.
506
*/
507
if (tmpl->bind & (PIPE_BIND_LINEAR | PIPE_BIND_CURSOR))
508
should_tile = false;
509
510
/* No shared objects with LT format -- the kernel only has T-format
511
* metadata. LT objects are small enough it's not worth the trouble to
512
* give them metadata to tile.
513
*/
514
if ((tmpl->bind & (PIPE_BIND_SHARED | PIPE_BIND_SCANOUT)) &&
515
vc4_size_is_lt(prsc->width0, prsc->height0, rsc->cpp))
516
should_tile = false;
517
518
/* If we're sharing or scanning out, we need the ioctl present to
519
* inform the kernel or the other side.
520
*/
521
if ((tmpl->bind & (PIPE_BIND_SHARED |
522
PIPE_BIND_SCANOUT)) && !screen->has_tiling_ioctl)
523
should_tile = false;
524
525
/* No user-specified modifier; determine our own. */
526
if (count == 1 && modifiers[0] == DRM_FORMAT_MOD_INVALID) {
527
linear_ok = true;
528
rsc->tiled = should_tile;
529
} else if (should_tile &&
530
drm_find_modifier(DRM_FORMAT_MOD_BROADCOM_VC4_T_TILED,
531
modifiers, count)) {
532
rsc->tiled = true;
533
} else if (linear_ok) {
534
rsc->tiled = false;
535
} else {
536
fprintf(stderr, "Unsupported modifier requested\n");
537
return NULL;
538
}
539
540
if (tmpl->target != PIPE_BUFFER)
541
rsc->vc4_format = get_resource_texture_format(prsc);
542
543
vc4_setup_slices(rsc, "create");
544
if (!vc4_resource_bo_alloc(rsc))
545
goto fail;
546
547
if (screen->has_tiling_ioctl) {
548
uint64_t modifier;
549
if (rsc->tiled)
550
modifier = DRM_FORMAT_MOD_BROADCOM_VC4_T_TILED;
551
else
552
modifier = DRM_FORMAT_MOD_LINEAR;
553
struct drm_vc4_set_tiling set_tiling = {
554
.handle = rsc->bo->handle,
555
.modifier = modifier,
556
};
557
int ret = vc4_ioctl(screen->fd, DRM_IOCTL_VC4_SET_TILING,
558
&set_tiling);
559
if (ret != 0)
560
goto fail;
561
}
562
563
/* Set up the "scanout resource" (the dmabuf export of our buffer to
564
* the KMS handle) if the buffer might ever have
565
* resource_get_handle(WINSYS_HANDLE_TYPE_KMS) called on it.
566
* create_with_modifiers() doesn't give us usage flags, so we have to
567
* assume that all calls with modifiers are scanout-possible.
568
*/
569
if (screen->ro &&
570
((tmpl->bind & PIPE_BIND_SCANOUT) ||
571
!(count == 1 && modifiers[0] == DRM_FORMAT_MOD_INVALID))) {
572
rsc->scanout =
573
renderonly_scanout_for_resource(prsc, screen->ro, NULL);
574
if (!rsc->scanout)
575
goto fail;
576
}
577
578
vc4_bo_label(screen, rsc->bo, "%sresource %dx%d@%d/%d",
579
(tmpl->bind & PIPE_BIND_SCANOUT) ? "scanout " : "",
580
tmpl->width0, tmpl->height0,
581
rsc->cpp * 8, prsc->last_level);
582
583
return prsc;
584
fail:
585
vc4_resource_destroy(pscreen, prsc);
586
return NULL;
587
}
588
589
struct pipe_resource *
590
vc4_resource_create(struct pipe_screen *pscreen,
591
const struct pipe_resource *tmpl)
592
{
593
const uint64_t mod = DRM_FORMAT_MOD_INVALID;
594
return vc4_resource_create_with_modifiers(pscreen, tmpl, &mod, 1);
595
}
596
597
static struct pipe_resource *
598
vc4_resource_from_handle(struct pipe_screen *pscreen,
599
const struct pipe_resource *tmpl,
600
struct winsys_handle *whandle,
601
unsigned usage)
602
{
603
struct vc4_screen *screen = vc4_screen(pscreen);
604
struct vc4_resource *rsc = vc4_resource_setup(pscreen, tmpl);
605
struct pipe_resource *prsc = &rsc->base;
606
struct vc4_resource_slice *slice = &rsc->slices[0];
607
608
if (!rsc)
609
return NULL;
610
611
switch (whandle->type) {
612
case WINSYS_HANDLE_TYPE_SHARED:
613
rsc->bo = vc4_bo_open_name(screen, whandle->handle);
614
break;
615
case WINSYS_HANDLE_TYPE_FD:
616
rsc->bo = vc4_bo_open_dmabuf(screen, whandle->handle);
617
break;
618
default:
619
fprintf(stderr,
620
"Attempt to import unsupported handle type %d\n",
621
whandle->type);
622
}
623
624
if (!rsc->bo)
625
goto fail;
626
627
struct drm_vc4_get_tiling get_tiling = {
628
.handle = rsc->bo->handle,
629
};
630
int ret = vc4_ioctl(screen->fd, DRM_IOCTL_VC4_GET_TILING, &get_tiling);
631
632
if (ret != 0) {
633
whandle->modifier = DRM_FORMAT_MOD_LINEAR;
634
} else if (whandle->modifier == DRM_FORMAT_MOD_INVALID) {
635
whandle->modifier = get_tiling.modifier;
636
} else if (whandle->modifier != get_tiling.modifier) {
637
fprintf(stderr,
638
"Modifier 0x%llx vs. tiling (0x%llx) mismatch\n",
639
(long long)whandle->modifier, get_tiling.modifier);
640
goto fail;
641
}
642
643
switch (whandle->modifier) {
644
case DRM_FORMAT_MOD_LINEAR:
645
rsc->tiled = false;
646
break;
647
case DRM_FORMAT_MOD_BROADCOM_VC4_T_TILED:
648
rsc->tiled = true;
649
break;
650
default:
651
fprintf(stderr,
652
"Attempt to import unsupported modifier 0x%llx\n",
653
(long long)whandle->modifier);
654
goto fail;
655
}
656
657
rsc->vc4_format = get_resource_texture_format(prsc);
658
vc4_setup_slices(rsc, "import");
659
660
if (whandle->offset != 0) {
661
if (rsc->tiled) {
662
fprintf(stderr,
663
"Attempt to import unsupported "
664
"winsys offset %u\n",
665
whandle->offset);
666
goto fail;
667
}
668
669
rsc->slices[0].offset += whandle->offset;
670
671
if (rsc->slices[0].offset + rsc->slices[0].size >
672
rsc->bo->size) {
673
fprintf(stderr, "Attempt to import "
674
"with overflowing offset (%d + %d > %d)\n",
675
whandle->offset,
676
rsc->slices[0].size,
677
rsc->bo->size);
678
goto fail;
679
}
680
}
681
682
if (screen->ro) {
683
/* Make sure that renderonly has a handle to our buffer in the
684
* display's fd, so that a later renderonly_get_handle()
685
* returns correct handles or GEM names.
686
*/
687
rsc->scanout =
688
renderonly_create_gpu_import_for_resource(prsc,
689
screen->ro,
690
NULL);
691
}
692
693
if (rsc->tiled && whandle->stride != slice->stride) {
694
static bool warned = false;
695
if (!warned) {
696
warned = true;
697
fprintf(stderr,
698
"Attempting to import %dx%d %s with "
699
"unsupported stride %d instead of %d\n",
700
prsc->width0, prsc->height0,
701
util_format_short_name(prsc->format),
702
whandle->stride,
703
slice->stride);
704
}
705
goto fail;
706
} else if (!rsc->tiled) {
707
slice->stride = whandle->stride;
708
}
709
710
return prsc;
711
712
fail:
713
vc4_resource_destroy(pscreen, prsc);
714
return NULL;
715
}
716
717
static struct pipe_surface *
718
vc4_create_surface(struct pipe_context *pctx,
719
struct pipe_resource *ptex,
720
const struct pipe_surface *surf_tmpl)
721
{
722
struct vc4_surface *surface = CALLOC_STRUCT(vc4_surface);
723
struct vc4_resource *rsc = vc4_resource(ptex);
724
725
if (!surface)
726
return NULL;
727
728
assert(surf_tmpl->u.tex.first_layer == surf_tmpl->u.tex.last_layer);
729
730
struct pipe_surface *psurf = &surface->base;
731
unsigned level = surf_tmpl->u.tex.level;
732
733
pipe_reference_init(&psurf->reference, 1);
734
pipe_resource_reference(&psurf->texture, ptex);
735
736
psurf->context = pctx;
737
psurf->format = surf_tmpl->format;
738
psurf->width = u_minify(ptex->width0, level);
739
psurf->height = u_minify(ptex->height0, level);
740
psurf->u.tex.level = level;
741
psurf->u.tex.first_layer = surf_tmpl->u.tex.first_layer;
742
psurf->u.tex.last_layer = surf_tmpl->u.tex.last_layer;
743
surface->offset = (rsc->slices[level].offset +
744
psurf->u.tex.first_layer * rsc->cube_map_stride);
745
surface->tiling = rsc->slices[level].tiling;
746
747
return &surface->base;
748
}
749
750
static void
751
vc4_surface_destroy(struct pipe_context *pctx, struct pipe_surface *psurf)
752
{
753
pipe_resource_reference(&psurf->texture, NULL);
754
FREE(psurf);
755
}
756
757
static void
758
vc4_dump_surface_non_msaa(struct pipe_surface *psurf)
759
{
760
struct pipe_resource *prsc = psurf->texture;
761
struct vc4_resource *rsc = vc4_resource(prsc);
762
uint32_t *map = vc4_bo_map(rsc->bo);
763
uint32_t stride = rsc->slices[0].stride / 4;
764
uint32_t width = psurf->width;
765
uint32_t height = psurf->height;
766
uint32_t chunk_w = width / 79;
767
uint32_t chunk_h = height / 40;
768
uint32_t found_colors[10] = { 0 };
769
uint32_t num_found_colors = 0;
770
771
if (rsc->vc4_format != VC4_TEXTURE_TYPE_RGBA32R) {
772
fprintf(stderr, "%s: Unsupported format %s\n",
773
__func__, util_format_short_name(psurf->format));
774
return;
775
}
776
777
for (int by = 0; by < height; by += chunk_h) {
778
for (int bx = 0; bx < width; bx += chunk_w) {
779
int all_found_color = -1; /* nothing found */
780
781
for (int y = by; y < MIN2(height, by + chunk_h); y++) {
782
for (int x = bx; x < MIN2(width, bx + chunk_w); x++) {
783
uint32_t pix = map[y * stride + x];
784
785
int i;
786
for (i = 0; i < num_found_colors; i++) {
787
if (pix == found_colors[i])
788
break;
789
}
790
if (i == num_found_colors &&
791
num_found_colors <
792
ARRAY_SIZE(found_colors)) {
793
found_colors[num_found_colors++] = pix;
794
}
795
796
if (i < num_found_colors) {
797
if (all_found_color == -1)
798
all_found_color = i;
799
else if (i != all_found_color)
800
all_found_color = ARRAY_SIZE(found_colors);
801
}
802
}
803
}
804
/* If all pixels for this chunk have a consistent
805
* value, then print a character for it. Either a
806
* fixed name (particularly common for piglit tests),
807
* or a runtime-generated number.
808
*/
809
if (all_found_color >= 0 &&
810
all_found_color < ARRAY_SIZE(found_colors)) {
811
static const struct {
812
uint32_t val;
813
const char *c;
814
} named_colors[] = {
815
{ 0xff000000, "█" },
816
{ 0x00000000, "█" },
817
{ 0xffff0000, "r" },
818
{ 0xff00ff00, "g" },
819
{ 0xff0000ff, "b" },
820
{ 0xffffffff, "w" },
821
};
822
int i;
823
for (i = 0; i < ARRAY_SIZE(named_colors); i++) {
824
if (named_colors[i].val ==
825
found_colors[all_found_color]) {
826
fprintf(stderr, "%s",
827
named_colors[i].c);
828
break;
829
}
830
}
831
/* For unnamed colors, print a number and the
832
* numbers will have values printed at the
833
* end.
834
*/
835
if (i == ARRAY_SIZE(named_colors)) {
836
fprintf(stderr, "%c",
837
'0' + all_found_color);
838
}
839
} else {
840
/* If there's no consistent color, print this.
841
*/
842
fprintf(stderr, ".");
843
}
844
}
845
fprintf(stderr, "\n");
846
}
847
848
for (int i = 0; i < num_found_colors; i++) {
849
fprintf(stderr, "color %d: 0x%08x\n", i, found_colors[i]);
850
}
851
}
852
853
static uint32_t
854
vc4_surface_msaa_get_sample(struct pipe_surface *psurf,
855
uint32_t x, uint32_t y, uint32_t sample)
856
{
857
struct pipe_resource *prsc = psurf->texture;
858
struct vc4_resource *rsc = vc4_resource(prsc);
859
uint32_t tile_w = 32, tile_h = 32;
860
uint32_t tiles_w = DIV_ROUND_UP(psurf->width, 32);
861
862
uint32_t tile_x = x / tile_w;
863
uint32_t tile_y = y / tile_h;
864
uint32_t *tile = (vc4_bo_map(rsc->bo) +
865
VC4_TILE_BUFFER_SIZE * (tile_y * tiles_w + tile_x));
866
uint32_t subtile_x = x % tile_w;
867
uint32_t subtile_y = y % tile_h;
868
869
uint32_t quad_samples = VC4_MAX_SAMPLES * 4;
870
uint32_t tile_stride = quad_samples * tile_w / 2;
871
872
return *((uint32_t *)tile +
873
(subtile_y >> 1) * tile_stride +
874
(subtile_x >> 1) * quad_samples +
875
((subtile_y & 1) << 1) +
876
(subtile_x & 1) +
877
sample);
878
}
879
880
static void
881
vc4_dump_surface_msaa_char(struct pipe_surface *psurf,
882
uint32_t start_x, uint32_t start_y,
883
uint32_t w, uint32_t h)
884
{
885
bool all_same_color = true;
886
uint32_t all_pix = 0;
887
888
for (int y = start_y; y < start_y + h; y++) {
889
for (int x = start_x; x < start_x + w; x++) {
890
for (int s = 0; s < VC4_MAX_SAMPLES; s++) {
891
uint32_t pix = vc4_surface_msaa_get_sample(psurf,
892
x, y,
893
s);
894
if (x == start_x && y == start_y)
895
all_pix = pix;
896
else if (all_pix != pix)
897
all_same_color = false;
898
}
899
}
900
}
901
if (all_same_color) {
902
static const struct {
903
uint32_t val;
904
const char *c;
905
} named_colors[] = {
906
{ 0xff000000, "█" },
907
{ 0x00000000, "█" },
908
{ 0xffff0000, "r" },
909
{ 0xff00ff00, "g" },
910
{ 0xff0000ff, "b" },
911
{ 0xffffffff, "w" },
912
};
913
int i;
914
for (i = 0; i < ARRAY_SIZE(named_colors); i++) {
915
if (named_colors[i].val == all_pix) {
916
fprintf(stderr, "%s",
917
named_colors[i].c);
918
return;
919
}
920
}
921
fprintf(stderr, "x");
922
} else {
923
fprintf(stderr, ".");
924
}
925
}
926
927
static void
928
vc4_dump_surface_msaa(struct pipe_surface *psurf)
929
{
930
uint32_t tile_w = 32, tile_h = 32;
931
uint32_t tiles_w = DIV_ROUND_UP(psurf->width, tile_w);
932
uint32_t tiles_h = DIV_ROUND_UP(psurf->height, tile_h);
933
uint32_t char_w = 140, char_h = 60;
934
uint32_t char_w_per_tile = char_w / tiles_w - 1;
935
uint32_t char_h_per_tile = char_h / tiles_h - 1;
936
937
fprintf(stderr, "Surface: %dx%d (%dx MSAA)\n",
938
psurf->width, psurf->height, psurf->texture->nr_samples);
939
940
for (int x = 0; x < (char_w_per_tile + 1) * tiles_w; x++)
941
fprintf(stderr, "-");
942
fprintf(stderr, "\n");
943
944
for (int ty = 0; ty < psurf->height; ty += tile_h) {
945
for (int y = 0; y < char_h_per_tile; y++) {
946
947
for (int tx = 0; tx < psurf->width; tx += tile_w) {
948
for (int x = 0; x < char_w_per_tile; x++) {
949
uint32_t bx1 = (x * tile_w /
950
char_w_per_tile);
951
uint32_t bx2 = ((x + 1) * tile_w /
952
char_w_per_tile);
953
uint32_t by1 = (y * tile_h /
954
char_h_per_tile);
955
uint32_t by2 = ((y + 1) * tile_h /
956
char_h_per_tile);
957
958
vc4_dump_surface_msaa_char(psurf,
959
tx + bx1,
960
ty + by1,
961
bx2 - bx1,
962
by2 - by1);
963
}
964
fprintf(stderr, "|");
965
}
966
fprintf(stderr, "\n");
967
}
968
969
for (int x = 0; x < (char_w_per_tile + 1) * tiles_w; x++)
970
fprintf(stderr, "-");
971
fprintf(stderr, "\n");
972
}
973
}
974
975
/** Debug routine to dump the contents of an 8888 surface to the console */
976
void
977
vc4_dump_surface(struct pipe_surface *psurf)
978
{
979
if (!psurf)
980
return;
981
982
if (psurf->texture->nr_samples > 1)
983
vc4_dump_surface_msaa(psurf);
984
else
985
vc4_dump_surface_non_msaa(psurf);
986
}
987
988
static void
989
vc4_flush_resource(struct pipe_context *pctx, struct pipe_resource *resource)
990
{
991
/* All calls to flush_resource are followed by a flush of the context,
992
* so there's nothing to do.
993
*/
994
}
995
996
void
997
vc4_update_shadow_baselevel_texture(struct pipe_context *pctx,
998
struct pipe_sampler_view *pview)
999
{
1000
struct vc4_context *vc4 = vc4_context(pctx);
1001
struct vc4_sampler_view *view = vc4_sampler_view(pview);
1002
struct vc4_resource *shadow = vc4_resource(view->texture);
1003
struct vc4_resource *orig = vc4_resource(pview->texture);
1004
1005
assert(view->texture != pview->texture);
1006
1007
if (shadow->writes == orig->writes && orig->bo->private)
1008
return;
1009
1010
perf_debug("Updating %dx%d@%d shadow texture due to %s\n",
1011
orig->base.width0, orig->base.height0,
1012
pview->u.tex.first_level,
1013
pview->u.tex.first_level ? "base level" : "raster layout");
1014
1015
for (int i = 0; i <= shadow->base.last_level; i++) {
1016
unsigned width = u_minify(shadow->base.width0, i);
1017
unsigned height = u_minify(shadow->base.height0, i);
1018
struct pipe_blit_info info = {
1019
.dst = {
1020
.resource = &shadow->base,
1021
.level = i,
1022
.box = {
1023
.x = 0,
1024
.y = 0,
1025
.z = 0,
1026
.width = width,
1027
.height = height,
1028
.depth = 1,
1029
},
1030
.format = shadow->base.format,
1031
},
1032
.src = {
1033
.resource = &orig->base,
1034
.level = pview->u.tex.first_level + i,
1035
.box = {
1036
.x = 0,
1037
.y = 0,
1038
.z = 0,
1039
.width = width,
1040
.height = height,
1041
.depth = 1,
1042
},
1043
.format = orig->base.format,
1044
},
1045
.mask = ~0,
1046
};
1047
pctx->blit(pctx, &info);
1048
}
1049
1050
shadow->writes = orig->writes;
1051
}
1052
1053
/**
1054
* Converts a 4-byte index buffer to 2 bytes.
1055
*
1056
* Since GLES2 only has support for 1 and 2-byte indices, the hardware doesn't
1057
* include 4-byte index support, and we have to shrink it down.
1058
*
1059
* There's no fallback support for when indices end up being larger than 2^16,
1060
* though it will at least assertion fail. Also, if the original index data
1061
* was in user memory, it would be nice to not have uploaded it to a VBO
1062
* before translating.
1063
*/
1064
struct pipe_resource *
1065
vc4_get_shadow_index_buffer(struct pipe_context *pctx,
1066
const struct pipe_draw_info *info,
1067
uint32_t offset,
1068
uint32_t count,
1069
uint32_t *shadow_offset)
1070
{
1071
struct vc4_context *vc4 = vc4_context(pctx);
1072
struct vc4_resource *orig = vc4_resource(info->index.resource);
1073
perf_debug("Fallback conversion for %d uint indices\n", count);
1074
1075
void *data;
1076
struct pipe_resource *shadow_rsc = NULL;
1077
u_upload_alloc(vc4->uploader, 0, count * 2, 4,
1078
shadow_offset, &shadow_rsc, &data);
1079
uint16_t *dst = data;
1080
1081
struct pipe_transfer *src_transfer = NULL;
1082
const uint32_t *src;
1083
if (info->has_user_indices) {
1084
src = (uint32_t*)((char*)info->index.user + offset);
1085
} else {
1086
src = pipe_buffer_map_range(pctx, &orig->base,
1087
offset,
1088
count * 4,
1089
PIPE_MAP_READ, &src_transfer);
1090
}
1091
1092
for (int i = 0; i < count; i++) {
1093
uint32_t src_index = src[i];
1094
assert(src_index <= 0xffff);
1095
dst[i] = src_index;
1096
}
1097
1098
if (src_transfer)
1099
pctx->buffer_unmap(pctx, src_transfer);
1100
1101
return shadow_rsc;
1102
}
1103
1104
static const struct u_transfer_vtbl transfer_vtbl = {
1105
.resource_create = vc4_resource_create,
1106
.resource_destroy = vc4_resource_destroy,
1107
.transfer_map = vc4_resource_transfer_map,
1108
.transfer_unmap = vc4_resource_transfer_unmap,
1109
.transfer_flush_region = u_default_transfer_flush_region,
1110
};
1111
1112
void
1113
vc4_resource_screen_init(struct pipe_screen *pscreen)
1114
{
1115
struct vc4_screen *screen = vc4_screen(pscreen);
1116
1117
pscreen->resource_create = vc4_resource_create;
1118
pscreen->resource_create_with_modifiers =
1119
vc4_resource_create_with_modifiers;
1120
pscreen->resource_from_handle = vc4_resource_from_handle;
1121
pscreen->resource_get_handle = vc4_resource_get_handle;
1122
pscreen->resource_destroy = vc4_resource_destroy;
1123
pscreen->transfer_helper = u_transfer_helper_create(&transfer_vtbl,
1124
false, false,
1125
false, true);
1126
1127
/* Test if the kernel has GET_TILING; it will return -EINVAL if the
1128
* ioctl does not exist, but -ENOENT if we pass an impossible handle.
1129
* 0 cannot be a valid GEM object, so use that.
1130
*/
1131
struct drm_vc4_get_tiling get_tiling = {
1132
.handle = 0x0,
1133
};
1134
int ret = vc4_ioctl(screen->fd, DRM_IOCTL_VC4_GET_TILING, &get_tiling);
1135
if (ret == -1 && errno == ENOENT)
1136
screen->has_tiling_ioctl = true;
1137
}
1138
1139
void
1140
vc4_resource_context_init(struct pipe_context *pctx)
1141
{
1142
pctx->buffer_map = u_transfer_helper_transfer_map;
1143
pctx->texture_map = u_transfer_helper_transfer_map;
1144
pctx->transfer_flush_region = u_transfer_helper_transfer_flush_region;
1145
pctx->buffer_unmap = u_transfer_helper_transfer_unmap;
1146
pctx->texture_unmap = u_transfer_helper_transfer_unmap;
1147
pctx->buffer_subdata = u_default_buffer_subdata;
1148
pctx->texture_subdata = vc4_texture_subdata;
1149
pctx->create_surface = vc4_create_surface;
1150
pctx->surface_destroy = vc4_surface_destroy;
1151
pctx->resource_copy_region = util_resource_copy_region;
1152
pctx->blit = vc4_blit;
1153
pctx->flush_resource = vc4_flush_resource;
1154
}
1155
1156