panfrost: Improve logging and patch memory leaks
[mesa.git] / src / gallium / drivers / panfrost / pan_context.c
1 /*
2 * © Copyright 2018 Alyssa Rosenzweig
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 */
24
25 #include <sys/poll.h>
26 #include <errno.h>
27
28 #include "pan_context.h"
29 #include "pan_swizzle.h"
30 #include "pan_format.h"
31
32 #include "util/macros.h"
33 #include "util/u_format.h"
34 #include "util/u_inlines.h"
35 #include "util/u_upload_mgr.h"
36 #include "util/u_memory.h"
37 #include "util/half_float.h"
38 #include "indices/u_primconvert.h"
39 #include "tgsi/tgsi_parse.h"
40
41 #include "pan_screen.h"
42 #include "pan_blending.h"
43 #include "pan_blend_shaders.h"
44 #include "pan_wallpaper.h"
45
46 #ifdef DUMP_PERFORMANCE_COUNTERS
47 static int performance_counter_number = 0;
48 #endif
49
50 /* Do not actually send anything to the GPU; merely generate the cmdstream as fast as possible. Disables framebuffer writes */
51 //#define DRY_RUN
52
53 #define SET_BIT(lval, bit, cond) \
54 if (cond) \
55 lval |= (bit); \
56 else \
57 lval &= ~(bit);
58
59 /* TODO: Sample size, etc */
60
61 /* True for t6XX, false for t8xx. TODO: Run-time settable for automatic
62 * hardware configuration. */
63
64 static bool is_t6xx = false;
65
66 /* If set, we'll require the use of single render-target framebuffer
67 * descriptors (SFBD), for older hardware -- specifically, <T760 hardware, If
68 * false, we'll use the MFBD no matter what. New hardware -does- retain support
69 * for SFBD, and in theory we could flip between them on a per-RT basis, but
70 * there's no real advantage to doing so */
71
72 static bool require_sfbd = false;
73
74 static void
75 panfrost_set_framebuffer_msaa(struct panfrost_context *ctx, bool enabled)
76 {
77 SET_BIT(ctx->fragment_shader_core.unknown2_3, MALI_HAS_MSAA, enabled);
78 SET_BIT(ctx->fragment_shader_core.unknown2_4, MALI_NO_MSAA, !enabled);
79
80 if (require_sfbd) {
81 SET_BIT(ctx->fragment_sfbd.format, MALI_FRAMEBUFFER_MSAA_A | MALI_FRAMEBUFFER_MSAA_B, enabled);
82 } else {
83 SET_BIT(ctx->fragment_rts[0].format, MALI_MFBD_FORMAT_MSAA, enabled);
84
85 SET_BIT(ctx->fragment_mfbd.unk1, (1 << 4) | (1 << 1), enabled);
86
87 /* XXX */
88 ctx->fragment_mfbd.rt_count_2 = enabled ? 4 : 1;
89 }
90 }
91
92 /* AFBC is enabled on a per-resource basis (AFBC enabling is theoretically
93 * indepdent between color buffers and depth/stencil). To enable, we allocate
94 * the AFBC metadata buffer and mark that it is enabled. We do -not- actually
95 * edit the fragment job here. This routine should be called ONCE per
96 * AFBC-compressed buffer, rather than on every frame. */
97
98 static void
99 panfrost_enable_afbc(struct panfrost_context *ctx, struct panfrost_resource *rsrc, bool ds)
100 {
101 if (require_sfbd) {
102 printf("AFBC not supported yet on SFBD\n");
103 assert(0);
104 }
105
106 struct pipe_context *gallium = (struct pipe_context *) ctx;
107 struct panfrost_screen *screen = pan_screen(gallium->screen);
108 /* AFBC metadata is 16 bytes per tile */
109 int tile_w = (rsrc->base.width0 + (MALI_TILE_LENGTH - 1)) >> MALI_TILE_SHIFT;
110 int tile_h = (rsrc->base.height0 + (MALI_TILE_LENGTH - 1)) >> MALI_TILE_SHIFT;
111 int bytes_per_pixel = util_format_get_blocksize(rsrc->base.format);
112 int stride = bytes_per_pixel * rsrc->base.width0; /* TODO: Alignment? */
113
114 stride *= 2; /* TODO: Should this be carried over? */
115 int main_size = stride * rsrc->base.height0;
116 rsrc->bo->afbc_metadata_size = tile_w * tile_h * 16;
117
118 /* Allocate the AFBC slab itself, large enough to hold the above */
119 screen->driver->allocate_slab(screen, &rsrc->bo->afbc_slab,
120 (rsrc->bo->afbc_metadata_size + main_size + 4095) / 4096,
121 true, 0, 0, 0);
122
123 rsrc->bo->has_afbc = true;
124
125 /* Compressed textured reads use a tagged pointer to the metadata */
126
127 rsrc->bo->gpu[0] = rsrc->bo->afbc_slab.gpu | (ds ? 0 : 1);
128 rsrc->bo->cpu[0] = rsrc->bo->afbc_slab.cpu;
129 }
130
131 static void
132 panfrost_enable_checksum(struct panfrost_context *ctx, struct panfrost_resource *rsrc)
133 {
134 struct pipe_context *gallium = (struct pipe_context *) ctx;
135 struct panfrost_screen *screen = pan_screen(gallium->screen);
136 int tile_w = (rsrc->base.width0 + (MALI_TILE_LENGTH - 1)) >> MALI_TILE_SHIFT;
137 int tile_h = (rsrc->base.height0 + (MALI_TILE_LENGTH - 1)) >> MALI_TILE_SHIFT;
138
139 /* 8 byte checksum per tile */
140 rsrc->bo->checksum_stride = tile_w * 8;
141 int pages = (((rsrc->bo->checksum_stride * tile_h) + 4095) / 4096);
142 screen->driver->allocate_slab(screen, &rsrc->bo->checksum_slab, pages, false, 0, 0, 0);
143
144 rsrc->bo->has_checksum = true;
145 }
146
147 /* ..by contrast, this routine runs for every FRAGMENT job, but does no
148 * allocation. AFBC is enabled on a per-surface basis */
149
150 static void
151 panfrost_set_fragment_afbc(struct panfrost_context *ctx)
152 {
153 for (int cb = 0; cb < ctx->pipe_framebuffer.nr_cbufs; ++cb) {
154 struct panfrost_resource *rsrc = (struct panfrost_resource *) ctx->pipe_framebuffer.cbufs[cb]->texture;
155
156 /* Non-AFBC is the default */
157 if (!rsrc->bo->has_afbc)
158 continue;
159
160 if (require_sfbd) {
161 fprintf(stderr, "Color AFBC not supported on SFBD\n");
162 assert(0);
163 }
164
165 /* Enable AFBC for the render target */
166 ctx->fragment_rts[0].afbc.metadata = rsrc->bo->afbc_slab.gpu;
167 ctx->fragment_rts[0].afbc.stride = 0;
168 ctx->fragment_rts[0].afbc.unk = 0x30009;
169
170 ctx->fragment_rts[0].format |= MALI_MFBD_FORMAT_AFBC;
171
172 /* Point rendering to our special framebuffer */
173 ctx->fragment_rts[0].framebuffer = rsrc->bo->afbc_slab.gpu + rsrc->bo->afbc_metadata_size;
174
175 /* WAT? Stride is diff from the scanout case */
176 ctx->fragment_rts[0].framebuffer_stride = ctx->pipe_framebuffer.width * 2 * 4;
177 }
178
179 /* Enable depth/stencil AFBC for the framebuffer (not the render target) */
180 if (ctx->pipe_framebuffer.zsbuf) {
181 struct panfrost_resource *rsrc = (struct panfrost_resource *) ctx->pipe_framebuffer.zsbuf->texture;
182
183 if (rsrc->bo->has_afbc) {
184 if (require_sfbd) {
185 fprintf(stderr, "Depth AFBC not supported on SFBD\n");
186 assert(0);
187 }
188
189 ctx->fragment_mfbd.unk3 |= MALI_MFBD_EXTRA;
190
191 ctx->fragment_extra.ds_afbc.depth_stencil_afbc_metadata = rsrc->bo->afbc_slab.gpu;
192 ctx->fragment_extra.ds_afbc.depth_stencil_afbc_stride = 0;
193
194 ctx->fragment_extra.ds_afbc.depth_stencil = rsrc->bo->afbc_slab.gpu + rsrc->bo->afbc_metadata_size;
195
196 ctx->fragment_extra.ds_afbc.zero1 = 0x10009;
197 ctx->fragment_extra.ds_afbc.padding = 0x1000;
198
199 ctx->fragment_extra.unk = 0x435; /* General 0x400 in all unks. 0x5 for depth/stencil. 0x10 for AFBC encoded depth stencil. Unclear where the 0x20 is from */
200
201 ctx->fragment_mfbd.unk3 |= 0x400;
202 }
203 }
204
205 /* For the special case of a depth-only FBO, we need to attach a dummy render target */
206
207 if (ctx->pipe_framebuffer.nr_cbufs == 0) {
208 if (require_sfbd) {
209 fprintf(stderr, "Depth-only FBO not supported on SFBD\n");
210 assert(0);
211 }
212
213 ctx->fragment_rts[0].format = 0x80008000;
214 ctx->fragment_rts[0].framebuffer = 0;
215 ctx->fragment_rts[0].framebuffer_stride = 0;
216 }
217 }
218
219 /* Framebuffer descriptor */
220
221 static void
222 panfrost_set_framebuffer_resolution(struct mali_single_framebuffer *fb, int w, int h)
223 {
224 fb->width = MALI_POSITIVE(w);
225 fb->height = MALI_POSITIVE(h);
226
227 /* No idea why this is needed, but it's how resolution_check is
228 * calculated. It's not clear to us yet why the hardware wants this.
229 * The formula itself was discovered mostly by manual bruteforce and
230 * aggressive algebraic simplification. */
231
232 fb->resolution_check = ((w + h) / 3) << 4;
233 }
234
235 static struct mali_single_framebuffer
236 panfrost_emit_sfbd(struct panfrost_context *ctx)
237 {
238 struct mali_single_framebuffer framebuffer = {
239 .unknown2 = 0x1f,
240 .format = 0x30000000,
241 .clear_flags = 0x1000,
242 .unknown_address_0 = ctx->scratchpad.gpu,
243 .unknown_address_1 = ctx->misc_0.gpu,
244 .unknown_address_2 = ctx->misc_0.gpu + 40960,
245 .tiler_flags = 0xf0,
246 .tiler_heap_free = ctx->tiler_heap.gpu,
247 .tiler_heap_end = ctx->tiler_heap.gpu + ctx->tiler_heap.size,
248 };
249
250 panfrost_set_framebuffer_resolution(&framebuffer, ctx->pipe_framebuffer.width, ctx->pipe_framebuffer.height);
251
252 return framebuffer;
253 }
254
255 static struct bifrost_framebuffer
256 panfrost_emit_mfbd(struct panfrost_context *ctx)
257 {
258 struct bifrost_framebuffer framebuffer = {
259 .tiler_meta = 0xf00000c600,
260
261 .width1 = MALI_POSITIVE(ctx->pipe_framebuffer.width),
262 .height1 = MALI_POSITIVE(ctx->pipe_framebuffer.height),
263 .width2 = MALI_POSITIVE(ctx->pipe_framebuffer.width),
264 .height2 = MALI_POSITIVE(ctx->pipe_framebuffer.height),
265
266 .unk1 = 0x1080,
267
268 /* TODO: MRT */
269 .rt_count_1 = MALI_POSITIVE(1),
270 .rt_count_2 = 4,
271
272 .unknown2 = 0x1f,
273
274 /* Presumably corresponds to unknown_address_X of SFBD */
275 .scratchpad = ctx->scratchpad.gpu,
276 .tiler_scratch_start = ctx->misc_0.gpu,
277 .tiler_scratch_middle = ctx->misc_0.gpu + /*ctx->misc_0.size*/40960, /* Size depends on the size of the framebuffer and the number of vertices */
278
279 .tiler_heap_start = ctx->tiler_heap.gpu,
280 .tiler_heap_end = ctx->tiler_heap.gpu + ctx->tiler_heap.size,
281 };
282
283 return framebuffer;
284 }
285
286 /* Are we currently rendering to the screen (rather than an FBO)? */
287
288 static bool
289 panfrost_is_scanout(struct panfrost_context *ctx)
290 {
291 /* If there is no color buffer, it's an FBO */
292 if (!ctx->pipe_framebuffer.nr_cbufs)
293 return false;
294
295 /* If we're too early that no framebuffer was sent, it's scanout */
296 if (!ctx->pipe_framebuffer.cbufs[0])
297 return true;
298
299 return ctx->pipe_framebuffer.cbufs[0]->texture->bind & PIPE_BIND_DISPLAY_TARGET ||
300 ctx->pipe_framebuffer.cbufs[0]->texture->bind & PIPE_BIND_SCANOUT ||
301 ctx->pipe_framebuffer.cbufs[0]->texture->bind & PIPE_BIND_SHARED;
302 }
303
304 /* The above function is for generalised fbd emission, used in both fragment as
305 * well as vertex/tiler payloads. This payload is specific to fragment
306 * payloads. */
307
308 static void
309 panfrost_new_frag_framebuffer(struct panfrost_context *ctx)
310 {
311 mali_ptr framebuffer;
312 int stride;
313
314 if (ctx->pipe_framebuffer.nr_cbufs > 0) {
315 framebuffer = ((struct panfrost_resource *) ctx->pipe_framebuffer.cbufs[0]->texture)->bo->gpu[0];
316 stride = util_format_get_stride(ctx->pipe_framebuffer.cbufs[0]->format, ctx->pipe_framebuffer.width);
317 } else {
318 /* Depth-only framebuffer -> dummy RT */
319 framebuffer = 0;
320 stride = 0;
321 }
322
323 /* The default is upside down from OpenGL's perspective. */
324 if (panfrost_is_scanout(ctx)) {
325 framebuffer += stride * (ctx->pipe_framebuffer.height - 1);
326 stride = -stride;
327 }
328
329 if (require_sfbd) {
330 struct mali_single_framebuffer fb = panfrost_emit_sfbd(ctx);
331
332 fb.framebuffer = framebuffer;
333 fb.stride = stride;
334
335 fb.format = 0xb84e0281; /* RGB32, no MSAA */
336 memcpy(&ctx->fragment_sfbd, &fb, sizeof(fb));
337 } else {
338 struct bifrost_framebuffer fb = panfrost_emit_mfbd(ctx);
339
340 /* XXX: MRT case */
341 fb.rt_count_2 = 1;
342 fb.unk3 = 0x100;
343
344 struct bifrost_render_target rt = {
345 .unk1 = 0x4000000,
346 .format = 0x860a8899, /* RGBA32, no MSAA */
347 .framebuffer = framebuffer,
348 .framebuffer_stride = (stride / 16) & 0xfffffff,
349 };
350
351 memcpy(&ctx->fragment_rts[0], &rt, sizeof(rt));
352
353 memset(&ctx->fragment_extra, 0, sizeof(ctx->fragment_extra));
354 memcpy(&ctx->fragment_mfbd, &fb, sizeof(fb));
355 }
356 }
357
358 /* Maps float 0.0-1.0 to int 0x00-0xFF */
359 static uint8_t
360 normalised_float_to_u8(float f)
361 {
362 return (uint8_t) (int) (f * 255.0f);
363 }
364
365 static void
366 panfrost_clear_sfbd(struct panfrost_context *ctx,
367 bool clear_color,
368 bool clear_depth,
369 bool clear_stencil,
370 uint32_t packed_color,
371 double depth, unsigned stencil
372 )
373 {
374 struct mali_single_framebuffer *sfbd = &ctx->fragment_sfbd;
375
376 if (clear_color) {
377 sfbd->clear_color_1 = packed_color;
378 sfbd->clear_color_2 = packed_color;
379 sfbd->clear_color_3 = packed_color;
380 sfbd->clear_color_4 = packed_color;
381 }
382
383 if (clear_depth) {
384 sfbd->clear_depth_1 = depth;
385 sfbd->clear_depth_2 = depth;
386 sfbd->clear_depth_3 = depth;
387 sfbd->clear_depth_4 = depth;
388 }
389
390 if (clear_stencil) {
391 sfbd->clear_stencil = stencil;
392 }
393
394 /* Setup buffers */
395
396 if (clear_depth) {
397 sfbd->depth_buffer = ctx->depth_stencil_buffer.gpu;
398 sfbd->depth_buffer_enable = MALI_DEPTH_STENCIL_ENABLE;
399 }
400
401 if (clear_stencil) {
402 sfbd->stencil_buffer = ctx->depth_stencil_buffer.gpu;
403 sfbd->stencil_buffer_enable = MALI_DEPTH_STENCIL_ENABLE;
404 }
405
406 /* Set flags based on what has been cleared, for the SFBD case */
407 /* XXX: What do these flags mean? */
408 int clear_flags = 0x101100;
409
410 if (clear_color && clear_depth && clear_stencil) {
411 /* On a tiler like this, it's fastest to clear all three buffers at once */
412
413 clear_flags |= MALI_CLEAR_FAST;
414 } else {
415 clear_flags |= MALI_CLEAR_SLOW;
416
417 if (clear_stencil)
418 clear_flags |= MALI_CLEAR_SLOW_STENCIL;
419 }
420
421 sfbd->clear_flags = clear_flags;
422 }
423
424 static void
425 panfrost_clear_mfbd(struct panfrost_context *ctx,
426 bool clear_color,
427 bool clear_depth,
428 bool clear_stencil,
429 uint32_t packed_color,
430 double depth, unsigned stencil
431 )
432 {
433 struct bifrost_render_target *buffer_color = &ctx->fragment_rts[0];
434 struct bifrost_framebuffer *buffer_ds = &ctx->fragment_mfbd;
435
436 if (clear_color) {
437 buffer_color->clear_color_1 = packed_color;
438 buffer_color->clear_color_2 = packed_color;
439 buffer_color->clear_color_3 = packed_color;
440 buffer_color->clear_color_4 = packed_color;
441 }
442
443 if (clear_depth) {
444 buffer_ds->clear_depth = depth;
445 }
446
447 if (clear_stencil) {
448 buffer_ds->clear_stencil = stencil;
449 }
450
451 if (clear_depth || clear_stencil) {
452 /* Setup combined 24/8 depth/stencil */
453 ctx->fragment_mfbd.unk3 |= MALI_MFBD_EXTRA;
454 //ctx->fragment_extra.unk = /*0x405*/0x404;
455 ctx->fragment_extra.unk = 0x405;
456 ctx->fragment_extra.ds_linear.depth = ctx->depth_stencil_buffer.gpu;
457 ctx->fragment_extra.ds_linear.depth_stride = ctx->pipe_framebuffer.width * 4;
458 }
459 }
460
461 static void
462 panfrost_clear(
463 struct pipe_context *pipe,
464 unsigned buffers,
465 const union pipe_color_union *color,
466 double depth, unsigned stencil)
467 {
468 struct panfrost_context *ctx = pan_context(pipe);
469
470 if (!color) {
471 printf("Warning: clear color null?\n");
472 return;
473 }
474
475 /* Save settings for FBO switch */
476 ctx->last_clear.buffers = buffers;
477 ctx->last_clear.color = color;
478 ctx->last_clear.depth = depth;
479 ctx->last_clear.depth = depth;
480
481 bool clear_color = buffers & PIPE_CLEAR_COLOR;
482 bool clear_depth = buffers & PIPE_CLEAR_DEPTH;
483 bool clear_stencil = buffers & PIPE_CLEAR_STENCIL;
484
485 /* Remember that we've done something */
486 ctx->frame_cleared = true;
487
488 /* Alpha clear only meaningful without alpha channel */
489 bool has_alpha = ctx->pipe_framebuffer.nr_cbufs && util_format_has_alpha(ctx->pipe_framebuffer.cbufs[0]->format);
490 float clear_alpha = has_alpha ? color->f[3] : 1.0f;
491
492 uint32_t packed_color =
493 (normalised_float_to_u8(clear_alpha) << 24) |
494 (normalised_float_to_u8(color->f[2]) << 16) |
495 (normalised_float_to_u8(color->f[1]) << 8) |
496 (normalised_float_to_u8(color->f[0]) << 0);
497
498 if (require_sfbd) {
499 panfrost_clear_sfbd(ctx, clear_color, clear_depth, clear_stencil, packed_color, depth, stencil);
500 } else {
501 panfrost_clear_mfbd(ctx, clear_color, clear_depth, clear_stencil, packed_color, depth, stencil);
502 }
503 }
504
505 static mali_ptr
506 panfrost_attach_vt_mfbd(struct panfrost_context *ctx)
507 {
508 /* MFBD needs a sequential semi-render target upload, but what exactly this is, is beyond me for now */
509 struct bifrost_render_target rts_list[] = {
510 {
511 .chunknown = {
512 .unk = 0x30005,
513 },
514 .framebuffer = ctx->misc_0.gpu,
515 .zero2 = 0x3,
516 },
517 };
518
519 /* Allocate memory for the three components */
520 int size = 1024 + sizeof(ctx->vt_framebuffer_mfbd) + sizeof(rts_list);
521 struct panfrost_transfer transfer = panfrost_allocate_transient(ctx, size);
522
523 /* Opaque 1024-block */
524 rts_list[0].chunknown.pointer = transfer.gpu;
525
526 memcpy(transfer.cpu + 1024, &ctx->vt_framebuffer_mfbd, sizeof(ctx->vt_framebuffer_mfbd));
527 memcpy(transfer.cpu + 1024 + sizeof(ctx->vt_framebuffer_mfbd), rts_list, sizeof(rts_list));
528
529 return (transfer.gpu + 1024) | MALI_MFBD;
530 }
531
532 static mali_ptr
533 panfrost_attach_vt_sfbd(struct panfrost_context *ctx)
534 {
535 return panfrost_upload_transient(ctx, &ctx->vt_framebuffer_sfbd, sizeof(ctx->vt_framebuffer_sfbd)) | MALI_SFBD;
536 }
537
538 static void
539 panfrost_attach_vt_framebuffer(struct panfrost_context *ctx)
540 {
541 mali_ptr framebuffer = require_sfbd ?
542 panfrost_attach_vt_sfbd(ctx) :
543 panfrost_attach_vt_mfbd(ctx);
544
545 ctx->payload_vertex.postfix.framebuffer = framebuffer;
546 ctx->payload_tiler.postfix.framebuffer = framebuffer;
547 }
548
549 static void
550 panfrost_viewport(struct panfrost_context *ctx,
551 float depth_range_n,
552 float depth_range_f,
553 int viewport_x0, int viewport_y0,
554 int viewport_x1, int viewport_y1)
555 {
556 /* Viewport encoding is asymmetric. Purpose of the floats is unknown? */
557
558 struct mali_viewport ret = {
559 .floats = {
560 #if 0
561 -inff, -inff,
562 inff, inff,
563 #endif
564 0.0, 0.0,
565 2048.0, 1600.0,
566 },
567
568 .depth_range_n = depth_range_n,
569 .depth_range_f = depth_range_f,
570
571 .viewport0 = { viewport_x0, viewport_y0 },
572 .viewport1 = { MALI_POSITIVE(viewport_x1), MALI_POSITIVE(viewport_y1) },
573 };
574
575 memcpy(ctx->viewport, &ret, sizeof(ret));
576 }
577
578 /* Reset per-frame context, called on context initialisation as well as after
579 * flushing a frame */
580
581 static void
582 panfrost_invalidate_frame(struct panfrost_context *ctx)
583 {
584 unsigned transient_count = ctx->transient_pools[ctx->cmdstream_i].entry_index*ctx->transient_pools[0].entry_size + ctx->transient_pools[ctx->cmdstream_i].entry_offset;
585 printf("Uploaded transient %d bytes\n", transient_count);
586
587 /* Rotate cmdstream */
588 if ((++ctx->cmdstream_i) == (sizeof(ctx->transient_pools) / sizeof(ctx->transient_pools[0])))
589 ctx->cmdstream_i = 0;
590
591 if (require_sfbd)
592 ctx->vt_framebuffer_sfbd = panfrost_emit_sfbd(ctx);
593 else
594 ctx->vt_framebuffer_mfbd = panfrost_emit_mfbd(ctx);
595
596 panfrost_new_frag_framebuffer(ctx);
597
598 /* Reset varyings allocated */
599 ctx->varying_height = 0;
600
601 /* The transient cmdstream is dirty every frame; the only bits worth preserving
602 * (textures, shaders, etc) are in other buffers anyways */
603
604 ctx->transient_pools[ctx->cmdstream_i].entry_index = 0;
605 ctx->transient_pools[ctx->cmdstream_i].entry_offset = 0;
606
607 /* Regenerate payloads */
608 panfrost_attach_vt_framebuffer(ctx);
609
610 if (ctx->rasterizer)
611 ctx->dirty |= PAN_DIRTY_RASTERIZER;
612
613 /* XXX */
614 ctx->dirty |= PAN_DIRTY_SAMPLERS | PAN_DIRTY_TEXTURES;
615 }
616
617 /* In practice, every field of these payloads should be configurable
618 * arbitrarily, which means these functions are basically catch-all's for
619 * as-of-yet unwavering unknowns */
620
621 static void
622 panfrost_emit_vertex_payload(struct panfrost_context *ctx)
623 {
624 struct midgard_payload_vertex_tiler payload = {
625 .prefix = {
626 .workgroups_z_shift = 32,
627 .workgroups_x_shift_2 = 0x2,
628 .workgroups_x_shift_3 = 0x5,
629 },
630 .gl_enables = 0x4 | (is_t6xx ? 0 : 0x2),
631 };
632
633 memcpy(&ctx->payload_vertex, &payload, sizeof(payload));
634 }
635
636 static void
637 panfrost_emit_tiler_payload(struct panfrost_context *ctx)
638 {
639 struct midgard_payload_vertex_tiler payload = {
640 .prefix = {
641 .workgroups_z_shift = 32,
642 .workgroups_x_shift_2 = 0x2,
643 .workgroups_x_shift_3 = 0x6,
644
645 .zero1 = 0xffff, /* Why is this only seen on test-quad-textured? */
646 },
647 };
648
649 /* Reserve the viewport */
650 struct panfrost_transfer t = panfrost_allocate_chunk(ctx, sizeof(struct mali_viewport), HEAP_DESCRIPTOR);
651 ctx->viewport = (struct mali_viewport *) t.cpu;
652 payload.postfix.viewport = t.gpu;
653
654 memcpy(&ctx->payload_tiler, &payload, sizeof(payload));
655 }
656
657 static unsigned
658 translate_tex_wrap(enum pipe_tex_wrap w)
659 {
660 switch (w) {
661 case PIPE_TEX_WRAP_REPEAT:
662 return MALI_WRAP_REPEAT;
663
664 case PIPE_TEX_WRAP_CLAMP_TO_EDGE:
665 return MALI_WRAP_CLAMP_TO_EDGE;
666
667 case PIPE_TEX_WRAP_CLAMP_TO_BORDER:
668 return MALI_WRAP_CLAMP_TO_BORDER;
669
670 case PIPE_TEX_WRAP_MIRROR_REPEAT:
671 return MALI_WRAP_MIRRORED_REPEAT;
672
673 default:
674 assert(0);
675 return 0;
676 }
677 }
678
679 static unsigned
680 translate_tex_filter(enum pipe_tex_filter f)
681 {
682 switch (f) {
683 case PIPE_TEX_FILTER_NEAREST:
684 return MALI_NEAREST;
685
686 case PIPE_TEX_FILTER_LINEAR:
687 return MALI_LINEAR;
688
689 default:
690 assert(0);
691 return 0;
692 }
693 }
694
695 static unsigned
696 translate_mip_filter(enum pipe_tex_mipfilter f)
697 {
698 return (f == PIPE_TEX_MIPFILTER_LINEAR) ? MALI_MIP_LINEAR : 0;
699 }
700
701 static unsigned
702 panfrost_translate_compare_func(enum pipe_compare_func in)
703 {
704 switch (in) {
705 case PIPE_FUNC_NEVER:
706 return MALI_FUNC_NEVER;
707
708 case PIPE_FUNC_LESS:
709 return MALI_FUNC_LESS;
710
711 case PIPE_FUNC_EQUAL:
712 return MALI_FUNC_EQUAL;
713
714 case PIPE_FUNC_LEQUAL:
715 return MALI_FUNC_LEQUAL;
716
717 case PIPE_FUNC_GREATER:
718 return MALI_FUNC_GREATER;
719
720 case PIPE_FUNC_NOTEQUAL:
721 return MALI_FUNC_NOTEQUAL;
722
723 case PIPE_FUNC_GEQUAL:
724 return MALI_FUNC_GEQUAL;
725
726 case PIPE_FUNC_ALWAYS:
727 return MALI_FUNC_ALWAYS;
728 }
729
730 assert (0);
731 return 0; /* Unreachable */
732 }
733
734 static unsigned
735 panfrost_translate_alt_compare_func(enum pipe_compare_func in)
736 {
737 switch (in) {
738 case PIPE_FUNC_NEVER:
739 return MALI_ALT_FUNC_NEVER;
740
741 case PIPE_FUNC_LESS:
742 return MALI_ALT_FUNC_LESS;
743
744 case PIPE_FUNC_EQUAL:
745 return MALI_ALT_FUNC_EQUAL;
746
747 case PIPE_FUNC_LEQUAL:
748 return MALI_ALT_FUNC_LEQUAL;
749
750 case PIPE_FUNC_GREATER:
751 return MALI_ALT_FUNC_GREATER;
752
753 case PIPE_FUNC_NOTEQUAL:
754 return MALI_ALT_FUNC_NOTEQUAL;
755
756 case PIPE_FUNC_GEQUAL:
757 return MALI_ALT_FUNC_GEQUAL;
758
759 case PIPE_FUNC_ALWAYS:
760 return MALI_ALT_FUNC_ALWAYS;
761 }
762
763 assert (0);
764 return 0; /* Unreachable */
765 }
766
767 static unsigned
768 panfrost_translate_stencil_op(enum pipe_stencil_op in)
769 {
770 switch (in) {
771 case PIPE_STENCIL_OP_KEEP:
772 return MALI_STENCIL_KEEP;
773
774 case PIPE_STENCIL_OP_ZERO:
775 return MALI_STENCIL_ZERO;
776
777 case PIPE_STENCIL_OP_REPLACE:
778 return MALI_STENCIL_REPLACE;
779
780 case PIPE_STENCIL_OP_INCR:
781 return MALI_STENCIL_INCR;
782
783 case PIPE_STENCIL_OP_DECR:
784 return MALI_STENCIL_DECR;
785
786 case PIPE_STENCIL_OP_INCR_WRAP:
787 return MALI_STENCIL_INCR_WRAP;
788
789 case PIPE_STENCIL_OP_DECR_WRAP:
790 return MALI_STENCIL_DECR_WRAP;
791
792 case PIPE_STENCIL_OP_INVERT:
793 return MALI_STENCIL_INVERT;
794 }
795
796 assert (0);
797 return 0; /* Unreachable */
798 }
799
800 static void
801 panfrost_make_stencil_state(const struct pipe_stencil_state *in, struct mali_stencil_test *out)
802 {
803 out->ref = 0; /* Gallium gets it from elsewhere */
804
805 out->mask = in->valuemask;
806 out->func = panfrost_translate_compare_func(in->func);
807 out->sfail = panfrost_translate_stencil_op(in->fail_op);
808 out->dpfail = panfrost_translate_stencil_op(in->zfail_op);
809 out->dppass = panfrost_translate_stencil_op(in->zpass_op);
810 }
811
812 static void
813 panfrost_default_shader_backend(struct panfrost_context *ctx)
814 {
815 struct mali_shader_meta shader = {
816 .alpha_coverage = ~MALI_ALPHA_COVERAGE(0.000000),
817
818 .unknown2_3 = MALI_DEPTH_FUNC(MALI_FUNC_ALWAYS) | 0x3010,
819 .unknown2_4 = MALI_NO_MSAA | 0x4e0,
820 };
821
822 if (is_t6xx) {
823 shader.unknown2_4 |= 0x10;
824 }
825
826 struct pipe_stencil_state default_stencil = {
827 .enabled = 0,
828 .func = PIPE_FUNC_ALWAYS,
829 .fail_op = MALI_STENCIL_KEEP,
830 .zfail_op = MALI_STENCIL_KEEP,
831 .zpass_op = MALI_STENCIL_KEEP,
832 .writemask = 0xFF,
833 .valuemask = 0xFF
834 };
835
836 panfrost_make_stencil_state(&default_stencil, &shader.stencil_front);
837 shader.stencil_mask_front = default_stencil.writemask;
838
839 panfrost_make_stencil_state(&default_stencil, &shader.stencil_back);
840 shader.stencil_mask_back = default_stencil.writemask;
841
842 if (default_stencil.enabled)
843 shader.unknown2_4 |= MALI_STENCIL_TEST;
844
845 memcpy(&ctx->fragment_shader_core, &shader, sizeof(shader));
846 }
847
848 /* Generates a vertex/tiler job. This is, in some sense, the heart of the
849 * graphics command stream. It should be called once per draw, accordding to
850 * presentations. Set is_tiler for "tiler" jobs (fragment shader jobs, but in
851 * Mali parlance, "fragment" refers to framebuffer writeout). Clear it for
852 * vertex jobs. */
853
854 struct panfrost_transfer
855 panfrost_vertex_tiler_job(struct panfrost_context *ctx, bool is_tiler, bool is_elided_tiler)
856 {
857 /* Each draw call corresponds to two jobs, and we want to offset to leave room for the set-value job */
858 int draw_job_index = 1 + (2 * ctx->draw_count);
859
860 struct mali_job_descriptor_header job = {
861 .job_type = is_tiler ? JOB_TYPE_TILER : JOB_TYPE_VERTEX,
862 .job_index = draw_job_index + (is_tiler ? 1 : 0),
863 #ifdef __LP64__
864 .job_descriptor_size = 1,
865 #endif
866 };
867
868 /* Only non-elided tiler jobs have dependencies which are known at this point */
869
870 if (is_tiler && !is_elided_tiler) {
871 /* Tiler jobs depend on vertex jobs */
872
873 job.job_dependency_index_1 = draw_job_index;
874
875 /* Tiler jobs also depend on the previous tiler job */
876
877 if (ctx->draw_count)
878 job.job_dependency_index_2 = draw_job_index - 1;
879 }
880
881 struct midgard_payload_vertex_tiler *payload = is_tiler ? &ctx->payload_tiler : &ctx->payload_vertex;
882
883 /* There's some padding hacks on 32-bit */
884
885 #ifdef __LP64__
886 int offset = 0;
887 #else
888 int offset = 4;
889 #endif
890 struct panfrost_transfer transfer = panfrost_allocate_transient(ctx, sizeof(job) + sizeof(*payload));
891 memcpy(transfer.cpu, &job, sizeof(job));
892 memcpy(transfer.cpu + sizeof(job) - offset, payload, sizeof(*payload));
893 return transfer;
894 }
895
896 /* Generates a set value job. It's unclear what exactly this does, why it's
897 * necessary, and when to call it. */
898
899 static void
900 panfrost_set_value_job(struct panfrost_context *ctx)
901 {
902 struct mali_job_descriptor_header job = {
903 .job_type = JOB_TYPE_SET_VALUE,
904 .job_descriptor_size = 1,
905 .job_index = 1 + (2 * ctx->draw_count),
906 };
907
908 struct mali_payload_set_value payload = {
909 .out = ctx->misc_0.gpu,
910 .unknown = 0x3,
911 };
912
913 struct panfrost_transfer transfer = panfrost_allocate_transient(ctx, sizeof(job) + sizeof(payload));
914 memcpy(transfer.cpu, &job, sizeof(job));
915 memcpy(transfer.cpu + sizeof(job), &payload, sizeof(payload));
916
917 ctx->u_set_value_job = (struct mali_job_descriptor_header *) transfer.cpu;
918 ctx->set_value_job = transfer.gpu;
919 }
920
921 /* Generate a fragment job. This should be called once per frame. (According to
922 * presentations, this is supposed to correspond to eglSwapBuffers) */
923
924 mali_ptr
925 panfrost_fragment_job(struct panfrost_context *ctx)
926 {
927 /* Update fragment FBD */
928 panfrost_set_fragment_afbc(ctx);
929
930 if (ctx->pipe_framebuffer.nr_cbufs == 1) {
931 struct panfrost_resource *rsrc = (struct panfrost_resource *) ctx->pipe_framebuffer.cbufs[0]->texture;
932
933 if (rsrc->bo->has_checksum) {
934 if (require_sfbd) {
935 fprintf(stderr, "Checksumming not supported on SFBD\n");
936 assert(0);
937 }
938
939 int stride = util_format_get_stride(rsrc->base.format, rsrc->base.width0);
940
941 ctx->fragment_mfbd.unk3 |= MALI_MFBD_EXTRA;
942 ctx->fragment_extra.unk |= 0x420;
943 ctx->fragment_extra.checksum_stride = rsrc->bo->checksum_stride;
944 ctx->fragment_extra.checksum = rsrc->bo->gpu[0] + stride * rsrc->base.height0;
945 }
946 }
947
948 /* The frame is complete and therefore the framebuffer descriptor is
949 * ready for linkage and upload */
950
951 size_t sz = require_sfbd ? sizeof(struct mali_single_framebuffer) : (sizeof(struct bifrost_framebuffer) + sizeof(struct bifrost_fb_extra) + sizeof(struct bifrost_render_target) * 1);
952 struct panfrost_transfer fbd_t = panfrost_allocate_transient(ctx, sz);
953 off_t offset = 0;
954
955 if (require_sfbd) {
956 /* Upload just the SFBD all at once */
957 memcpy(fbd_t.cpu, &ctx->fragment_sfbd, sizeof(ctx->fragment_sfbd));
958 offset += sizeof(ctx->fragment_sfbd);
959 } else {
960 /* Upload the MFBD header */
961 memcpy(fbd_t.cpu, &ctx->fragment_mfbd, sizeof(ctx->fragment_mfbd));
962 offset += sizeof(ctx->fragment_mfbd);
963
964 /* Upload extra framebuffer info if necessary */
965 if (ctx->fragment_mfbd.unk3 & MALI_MFBD_EXTRA) {
966 memcpy(fbd_t.cpu + offset, &ctx->fragment_extra, sizeof(struct bifrost_fb_extra));
967 offset += sizeof(struct bifrost_fb_extra);
968 }
969
970 /* Upload (single) render target */
971 memcpy(fbd_t.cpu + offset, &ctx->fragment_rts[0], sizeof(struct bifrost_render_target) * 1);
972 }
973
974 /* Generate the fragment (frame) job */
975
976 struct mali_job_descriptor_header header = {
977 .job_type = JOB_TYPE_FRAGMENT,
978 .job_index = 1,
979 #ifdef __LP64__
980 .job_descriptor_size = 1
981 #endif
982 };
983
984 struct mali_payload_fragment payload = {
985 .min_tile_coord = MALI_COORDINATE_TO_TILE_MIN(0, 0),
986 .max_tile_coord = MALI_COORDINATE_TO_TILE_MAX(ctx->pipe_framebuffer.width, ctx->pipe_framebuffer.height),
987 .framebuffer = fbd_t.gpu | (require_sfbd ? MALI_SFBD : MALI_MFBD),
988 };
989
990 if (!require_sfbd && ctx->fragment_mfbd.unk3 & MALI_MFBD_EXTRA) {
991 /* Signal that there is an extra portion of the framebuffer
992 * descriptor */
993
994 payload.framebuffer |= 2;
995 }
996
997 /* Normally, there should be no padding. However, fragment jobs are
998 * shared with 64-bit Bifrost systems, and accordingly there is 4-bytes
999 * of zero padding in between. */
1000
1001 struct panfrost_transfer transfer = panfrost_allocate_transient(ctx, sizeof(header) + sizeof(payload));
1002 memcpy(transfer.cpu, &header, sizeof(header));
1003 memcpy(transfer.cpu + sizeof(header), &payload, sizeof(payload));
1004 return transfer.gpu;
1005 }
1006
1007 /* Emits attributes and varying descriptors, which should be called every draw,
1008 * excepting some obscure circumstances */
1009
1010 static void
1011 panfrost_emit_vertex_data(struct panfrost_context *ctx)
1012 {
1013 /* TODO: Only update the dirtied buffers */
1014 union mali_attr attrs[PIPE_MAX_ATTRIBS];
1015 union mali_attr varyings[PIPE_MAX_ATTRIBS];
1016
1017 unsigned invocation_count = MALI_NEGATIVE(ctx->payload_tiler.prefix.invocation_count);
1018
1019 for (int i = 0; i < ctx->vertex_buffer_count; ++i) {
1020 struct pipe_vertex_buffer *buf = &ctx->vertex_buffers[i];
1021 struct panfrost_resource *rsrc = (struct panfrost_resource *) (buf->buffer.resource);
1022
1023 /* Let's figure out the layout of the attributes in memory so
1024 * we can be smart about size computation. The idea is to
1025 * figure out the maximum src_offset, which tells us the latest
1026 * spot a vertex could start. Meanwhile, we figure out the size
1027 * of the attribute memory (assuming interleaved
1028 * representation) and tack on the max src_offset for a
1029 * reasonably good upper bound on the size.
1030 *
1031 * Proving correctness is left as an exercise to the reader.
1032 */
1033
1034 unsigned max_src_offset = 0;
1035
1036 for (unsigned j = 0; j < ctx->vertex->num_elements; ++j) {
1037 if (ctx->vertex->pipe[j].vertex_buffer_index != i) continue;
1038 max_src_offset = MAX2(max_src_offset, ctx->vertex->pipe[j].src_offset);
1039 }
1040
1041 /* Offset vertex count by draw_start to make sure we upload enough */
1042 attrs[i].stride = buf->stride;
1043 attrs[i].size = buf->stride * (ctx->payload_vertex.draw_start + invocation_count) + max_src_offset;
1044
1045 /* Vertex elements are -already- GPU-visible, at
1046 * rsrc->gpu. However, attribute buffers must be 64 aligned. If
1047 * it is not, for now we have to duplicate the buffer. */
1048
1049 mali_ptr effective_address = (rsrc->bo->gpu[0] + buf->buffer_offset);
1050
1051 if (effective_address & 0x3F) {
1052 attrs[i].elements = panfrost_upload_transient(ctx, rsrc->bo->cpu[0] + buf->buffer_offset, attrs[i].size) | 1;
1053 } else {
1054 attrs[i].elements = effective_address | 1;
1055 }
1056 }
1057
1058 struct panfrost_varyings *vars = &ctx->vs->variants[ctx->vs->active_variant].varyings;
1059
1060 for (int i = 0; i < vars->varying_buffer_count; ++i) {
1061 mali_ptr varying_address = ctx->varying_mem.gpu + ctx->varying_height;
1062
1063 varyings[i].elements = varying_address | 1;
1064 varyings[i].stride = vars->varyings_stride[i];
1065 varyings[i].size = vars->varyings_stride[i] * invocation_count;
1066
1067 /* If this varying has to be linked somewhere, do it now. See
1068 * pan_assemble.c for the indices. TODO: Use a more generic
1069 * linking interface */
1070
1071 if (i == 1) {
1072 /* gl_Position */
1073 ctx->payload_tiler.postfix.position_varying = varying_address;
1074 } else if (i == 2) {
1075 /* gl_PointSize */
1076 ctx->payload_tiler.primitive_size.pointer = varying_address;
1077 }
1078
1079 /* Varyings appear to need 64-byte alignment */
1080 ctx->varying_height += ALIGN(varyings[i].size, 64);
1081
1082 /* Ensure that we fit */
1083 assert(ctx->varying_height < ctx->varying_mem.size);
1084 }
1085
1086 ctx->payload_vertex.postfix.attributes = panfrost_upload_transient(ctx, attrs, ctx->vertex_buffer_count * sizeof(union mali_attr));
1087
1088 mali_ptr varyings_p = panfrost_upload_transient(ctx, &varyings, vars->varying_buffer_count * sizeof(union mali_attr));
1089 ctx->payload_vertex.postfix.varyings = varyings_p;
1090 ctx->payload_tiler.postfix.varyings = varyings_p;
1091 }
1092
1093 /* Go through dirty flags and actualise them in the cmdstream. */
1094
1095 void
1096 panfrost_emit_for_draw(struct panfrost_context *ctx, bool with_vertex_data)
1097 {
1098 if (with_vertex_data) {
1099 panfrost_emit_vertex_data(ctx);
1100 }
1101
1102 if (ctx->dirty & PAN_DIRTY_RASTERIZER) {
1103 ctx->payload_tiler.gl_enables = ctx->rasterizer->tiler_gl_enables;
1104 panfrost_set_framebuffer_msaa(ctx, ctx->rasterizer->base.multisample);
1105 }
1106
1107 if (ctx->occlusion_query) {
1108 ctx->payload_tiler.gl_enables |= MALI_OCCLUSION_QUERY | MALI_OCCLUSION_PRECISE;
1109 ctx->payload_tiler.postfix.occlusion_counter = ctx->occlusion_query->transfer.gpu;
1110 }
1111
1112 if (ctx->dirty & PAN_DIRTY_VS) {
1113 assert(ctx->vs);
1114
1115 struct panfrost_shader_state *vs = &ctx->vs->variants[ctx->vs->active_variant];
1116
1117 /* Late shader descriptor assignments */
1118 vs->tripipe->texture_count = ctx->sampler_view_count[PIPE_SHADER_VERTEX];
1119 vs->tripipe->sampler_count = ctx->sampler_count[PIPE_SHADER_VERTEX];
1120
1121 /* Who knows */
1122 vs->tripipe->midgard1.unknown1 = 0x2201;
1123
1124 ctx->payload_vertex.postfix._shader_upper = vs->tripipe_gpu >> 4;
1125
1126 /* Varying descriptor is tied to the vertex shader. Also the
1127 * fragment shader, I suppose, but it's generated with the
1128 * vertex shader so */
1129
1130 struct panfrost_varyings *varyings = &ctx->vs->variants[ctx->vs->active_variant].varyings;
1131
1132 ctx->payload_vertex.postfix.varying_meta = varyings->varyings_descriptor;
1133 ctx->payload_tiler.postfix.varying_meta = varyings->varyings_descriptor_fragment;
1134 }
1135
1136 if (ctx->dirty & (PAN_DIRTY_RASTERIZER | PAN_DIRTY_VS)) {
1137 /* Check if we need to link the gl_PointSize varying */
1138 assert(ctx->vs);
1139 struct panfrost_shader_state *vs = &ctx->vs->variants[ctx->vs->active_variant];
1140
1141 bool needs_gl_point_size = vs->writes_point_size && ctx->payload_tiler.prefix.draw_mode == MALI_POINTS;
1142
1143 if (!needs_gl_point_size) {
1144 /* If the size is constant, write it out. Otherwise,
1145 * don't touch primitive_size (since we would clobber
1146 * the pointer there) */
1147
1148 ctx->payload_tiler.primitive_size.constant = ctx->rasterizer->base.line_width;
1149 }
1150
1151 /* Set the flag for varying (pointer) point size if the shader needs that */
1152 SET_BIT(ctx->payload_tiler.prefix.unknown_draw, MALI_DRAW_VARYING_SIZE, needs_gl_point_size);
1153 }
1154
1155 /* TODO: Maybe dirty track FS, maybe not. For now, it's transient. */
1156 if (ctx->fs)
1157 ctx->dirty |= PAN_DIRTY_FS;
1158
1159 if (ctx->dirty & PAN_DIRTY_FS) {
1160 assert(ctx->fs);
1161 struct panfrost_shader_state *variant = &ctx->fs->variants[ctx->fs->active_variant];
1162
1163 #define COPY(name) ctx->fragment_shader_core.name = variant->tripipe->name
1164
1165 COPY(shader);
1166 COPY(attribute_count);
1167 COPY(varying_count);
1168 COPY(midgard1.uniform_count);
1169 COPY(midgard1.work_count);
1170 COPY(midgard1.unknown2);
1171
1172 #undef COPY
1173 /* If there is a blend shader, work registers are shared */
1174
1175 if (ctx->blend->has_blend_shader)
1176 ctx->fragment_shader_core.midgard1.work_count = /*MAX2(ctx->fragment_shader_core.midgard1.work_count, ctx->blend->blend_work_count)*/16;
1177
1178 /* Set late due to depending on render state */
1179 /* The one at the end seems to mean "1 UBO" */
1180 ctx->fragment_shader_core.midgard1.unknown1 = MALI_NO_ALPHA_TO_COVERAGE | 0x200 | 0x2201;
1181
1182 /* Assign texture/sample count right before upload */
1183 ctx->fragment_shader_core.texture_count = ctx->sampler_view_count[PIPE_SHADER_FRAGMENT];
1184 ctx->fragment_shader_core.sampler_count = ctx->sampler_count[PIPE_SHADER_FRAGMENT];
1185
1186 /* Assign the stencil refs late */
1187 ctx->fragment_shader_core.stencil_front.ref = ctx->stencil_ref.ref_value[0];
1188 ctx->fragment_shader_core.stencil_back.ref = ctx->stencil_ref.ref_value[1];
1189
1190 /* CAN_DISCARD should be set if the fragment shader possibly
1191 * contains a 'discard' instruction. It is likely this is
1192 * related to optimizations related to forward-pixel kill, as
1193 * per "Mali Performance 3: Is EGL_BUFFER_PRESERVED a good
1194 * thing?" by Peter Harris
1195 */
1196
1197 if (variant->can_discard) {
1198 ctx->fragment_shader_core.unknown2_3 |= MALI_CAN_DISCARD;
1199 ctx->fragment_shader_core.midgard1.unknown1 &= ~MALI_NO_ALPHA_TO_COVERAGE;
1200 ctx->fragment_shader_core.midgard1.unknown1 |= 0x4000;
1201 ctx->fragment_shader_core.midgard1.unknown1 = 0x4200;
1202 }
1203
1204 /* Check if we're using the default blend descriptor (fast path) */
1205
1206 bool no_blending =
1207 !ctx->blend->has_blend_shader &&
1208 (ctx->blend->equation.rgb_mode == 0x122) &&
1209 (ctx->blend->equation.alpha_mode == 0x122) &&
1210 (ctx->blend->equation.color_mask == 0xf);
1211
1212 if (require_sfbd) {
1213 /* When only a single render target platform is used, the blend
1214 * information is inside the shader meta itself. We
1215 * additionally need to signal CAN_DISCARD for nontrivial blend
1216 * modes (so we're able to read back the destination buffer) */
1217
1218 if (ctx->blend->has_blend_shader) {
1219 ctx->fragment_shader_core.blend_shader = ctx->blend->blend_shader;
1220 } else {
1221 memcpy(&ctx->fragment_shader_core.blend_equation, &ctx->blend->equation, sizeof(ctx->blend->equation));
1222 }
1223
1224 if (!no_blending) {
1225 ctx->fragment_shader_core.unknown2_3 |= MALI_CAN_DISCARD;
1226 }
1227 }
1228
1229 size_t size = sizeof(struct mali_shader_meta) + sizeof(struct mali_blend_meta);
1230 struct panfrost_transfer transfer = panfrost_allocate_transient(ctx, size);
1231 memcpy(transfer.cpu, &ctx->fragment_shader_core, sizeof(struct mali_shader_meta));
1232
1233 ctx->payload_tiler.postfix._shader_upper = (transfer.gpu) >> 4;
1234
1235 if (!require_sfbd) {
1236 /* Additional blend descriptor tacked on for jobs using MFBD */
1237
1238 unsigned blend_count = 0;
1239
1240 if (ctx->blend->has_blend_shader) {
1241 /* For a blend shader, the bottom nibble corresponds to
1242 * the number of work registers used, which signals the
1243 * -existence- of a blend shader */
1244
1245 assert(ctx->blend->blend_work_count >= 2);
1246 blend_count |= MIN2(ctx->blend->blend_work_count, 3);
1247 } else {
1248 /* Otherwise, the bottom bit simply specifies if
1249 * blending (anything other than REPLACE) is enabled */
1250
1251
1252 if (!no_blending)
1253 blend_count |= 0x1;
1254 }
1255
1256 /* Second blend equation is always a simple replace */
1257
1258 uint64_t replace_magic = 0xf0122122;
1259 struct mali_blend_equation replace_mode;
1260 memcpy(&replace_mode, &replace_magic, sizeof(replace_mode));
1261
1262 struct mali_blend_meta blend_meta[] = {
1263 {
1264 .unk1 = 0x200 | blend_count,
1265 .blend_equation_1 = ctx->blend->equation,
1266 .blend_equation_2 = replace_mode
1267 },
1268 };
1269
1270 if (ctx->blend->has_blend_shader)
1271 memcpy(&blend_meta[0].blend_equation_1, &ctx->blend->blend_shader, sizeof(ctx->blend->blend_shader));
1272
1273 memcpy(transfer.cpu + sizeof(struct mali_shader_meta), blend_meta, sizeof(blend_meta));
1274 }
1275 }
1276
1277 if (ctx->dirty & PAN_DIRTY_VERTEX) {
1278 ctx->payload_vertex.postfix.attribute_meta = ctx->vertex->descriptor_ptr;
1279 }
1280
1281 if (ctx->dirty & PAN_DIRTY_SAMPLERS) {
1282 /* Upload samplers back to back, no padding */
1283
1284 for (int t = 0; t <= PIPE_SHADER_FRAGMENT; ++t) {
1285 if (!ctx->sampler_count[t]) continue;
1286
1287 struct panfrost_transfer transfer = panfrost_allocate_transient(ctx, sizeof(struct mali_sampler_descriptor) * ctx->sampler_count[t]);
1288 struct mali_sampler_descriptor *desc = (struct mali_sampler_descriptor *) transfer.cpu;
1289
1290 for (int i = 0; i < ctx->sampler_count[t]; ++i) {
1291 desc[i] = ctx->samplers[t][i]->hw;
1292 }
1293
1294 if (t == PIPE_SHADER_FRAGMENT)
1295 ctx->payload_tiler.postfix.sampler_descriptor = transfer.gpu;
1296 else if (t == PIPE_SHADER_VERTEX)
1297 ctx->payload_vertex.postfix.sampler_descriptor = transfer.gpu;
1298 else
1299 assert(0);
1300 }
1301 }
1302
1303 if (ctx->dirty & PAN_DIRTY_TEXTURES) {
1304 for (int t = 0; t <= PIPE_SHADER_FRAGMENT; ++t) {
1305 /* Shortcircuit */
1306 if (!ctx->sampler_view_count[t]) continue;
1307
1308 uint64_t trampolines[PIPE_MAX_SHADER_SAMPLER_VIEWS];
1309
1310 for (int i = 0; i < ctx->sampler_view_count[t]; ++i) {
1311 if (!ctx->sampler_views[t][i])
1312 continue;
1313
1314 struct pipe_resource *tex_rsrc = ctx->sampler_views[t][i]->base.texture;
1315 struct panfrost_resource *rsrc = (struct panfrost_resource *) tex_rsrc;
1316
1317 /* Inject the address in. */
1318 for (int l = 0; l < (tex_rsrc->last_level + 1); ++l)
1319 ctx->sampler_views[t][i]->hw.swizzled_bitmaps[l] = rsrc->bo->gpu[l];
1320
1321 /* Workaround maybe-errata (?) with non-mipmaps */
1322 int s = ctx->sampler_views[t][i]->hw.nr_mipmap_levels;
1323
1324 if (!rsrc->bo->is_mipmap) {
1325 if (is_t6xx) {
1326 /* HW ERRATA, not needed after t6XX */
1327 ctx->sampler_views[t][i]->hw.swizzled_bitmaps[1] = rsrc->bo->gpu[0];
1328
1329 ctx->sampler_views[t][i]->hw.unknown3A = 1;
1330 }
1331
1332 ctx->sampler_views[t][i]->hw.nr_mipmap_levels = 0;
1333 }
1334
1335 trampolines[i] = panfrost_upload_transient(ctx, &ctx->sampler_views[t][i]->hw, sizeof(struct mali_texture_descriptor));
1336
1337 /* Restore */
1338 ctx->sampler_views[t][i]->hw.nr_mipmap_levels = s;
1339
1340 if (is_t6xx) {
1341 ctx->sampler_views[t][i]->hw.unknown3A = 0;
1342 }
1343 }
1344
1345 mali_ptr trampoline = panfrost_upload_transient(ctx, trampolines, sizeof(uint64_t) * ctx->sampler_view_count[t]);
1346
1347 if (t == PIPE_SHADER_FRAGMENT)
1348 ctx->payload_tiler.postfix.texture_trampoline = trampoline;
1349 else if (t == PIPE_SHADER_VERTEX)
1350 ctx->payload_vertex.postfix.texture_trampoline = trampoline;
1351 else
1352 assert(0);
1353 }
1354 }
1355
1356 /* Generate the viewport vector of the form: <width/2, height/2, centerx, centery> */
1357 const struct pipe_viewport_state *vp = &ctx->pipe_viewport;
1358
1359 float viewport_vec4[] = {
1360 vp->scale[0],
1361 fabsf(vp->scale[1]),
1362
1363 vp->translate[0],
1364 /* -1.0 * vp->translate[1] */ fabs(1.0 * vp->scale[1]) /* XXX */
1365 };
1366
1367 for (int i = 0; i < PIPE_SHADER_TYPES; ++i) {
1368 struct panfrost_constant_buffer *buf = &ctx->constant_buffer[i];
1369
1370 if (i == PIPE_SHADER_VERTEX || i == PIPE_SHADER_FRAGMENT) {
1371 /* It doesn't matter if we don't use all the memory;
1372 * we'd need a dummy UBO anyway. Compute the max */
1373
1374 size_t size = sizeof(viewport_vec4) + buf->size;
1375 struct panfrost_transfer transfer = panfrost_allocate_transient(ctx, size);
1376
1377 /* Keep track how much we've uploaded */
1378 off_t offset = 0;
1379
1380 if (i == PIPE_SHADER_VERTEX) {
1381 /* Upload viewport */
1382 memcpy(transfer.cpu + offset, viewport_vec4, sizeof(viewport_vec4));
1383 offset += sizeof(viewport_vec4);
1384 }
1385
1386 /* Upload uniforms */
1387 memcpy(transfer.cpu + offset, buf->buffer, buf->size);
1388
1389 int uniform_count = 0;
1390
1391 struct mali_vertex_tiler_postfix *postfix;
1392
1393 switch (i) {
1394 case PIPE_SHADER_VERTEX:
1395 uniform_count = ctx->vs->variants[ctx->vs->active_variant].uniform_count;
1396 postfix = &ctx->payload_vertex.postfix;
1397 break;
1398
1399 case PIPE_SHADER_FRAGMENT:
1400 uniform_count = ctx->fs->variants[ctx->fs->active_variant].uniform_count;
1401 postfix = &ctx->payload_tiler.postfix;
1402 break;
1403
1404 default:
1405 printf("Unknown shader stage %d in uniform upload\n", i);
1406 assert(0);
1407 }
1408
1409 /* Also attach the same buffer as a UBO for extended access */
1410
1411 struct mali_uniform_buffer_meta uniform_buffers[] = {
1412 {
1413 .size = MALI_POSITIVE((2 + uniform_count)),
1414 .ptr = transfer.gpu >> 2,
1415 },
1416 };
1417
1418 mali_ptr ubufs = panfrost_upload_transient(ctx, uniform_buffers, sizeof(uniform_buffers));
1419 postfix->uniforms = transfer.gpu;
1420 postfix->uniform_buffers = ubufs;
1421
1422 buf->dirty = 0;
1423 }
1424 }
1425
1426 ctx->dirty = 0;
1427 }
1428
1429 /* Corresponds to exactly one draw, but does not submit anything */
1430
1431 static void
1432 panfrost_queue_draw(struct panfrost_context *ctx)
1433 {
1434 /* TODO: Expand the array? */
1435 if (ctx->draw_count >= MAX_DRAW_CALLS) {
1436 printf("Job buffer overflow, ignoring draw\n");
1437 assert(0);
1438 }
1439
1440 /* Handle dirty flags now */
1441 panfrost_emit_for_draw(ctx, true);
1442
1443 struct panfrost_transfer vertex = panfrost_vertex_tiler_job(ctx, false, false);
1444 struct panfrost_transfer tiler = panfrost_vertex_tiler_job(ctx, true, false);
1445
1446 ctx->u_vertex_jobs[ctx->vertex_job_count] = (struct mali_job_descriptor_header *) vertex.cpu;
1447 ctx->vertex_jobs[ctx->vertex_job_count++] = vertex.gpu;
1448
1449 ctx->u_tiler_jobs[ctx->tiler_job_count] = (struct mali_job_descriptor_header *) tiler.cpu;
1450 ctx->tiler_jobs[ctx->tiler_job_count++] = tiler.gpu;
1451
1452 ctx->draw_count++;
1453 }
1454
1455 /* At the end of the frame, the vertex and tiler jobs are linked together and
1456 * then the fragment job is plonked at the end. Set value job is first for
1457 * unknown reasons. */
1458
1459 static void
1460 panfrost_link_job_pair(struct mali_job_descriptor_header *first, mali_ptr next)
1461 {
1462 if (first->job_descriptor_size)
1463 first->next_job_64 = (u64) (uintptr_t) next;
1464 else
1465 first->next_job_32 = (u32) (uintptr_t) next;
1466 }
1467
1468 static void
1469 panfrost_link_jobs(struct panfrost_context *ctx)
1470 {
1471 if (ctx->draw_count) {
1472 /* Generate the set_value_job */
1473 panfrost_set_value_job(ctx);
1474
1475 /* Have the first vertex job depend on the set value job */
1476 ctx->u_vertex_jobs[0]->job_dependency_index_1 = ctx->u_set_value_job->job_index;
1477
1478 /* SV -> V */
1479 panfrost_link_job_pair(ctx->u_set_value_job, ctx->vertex_jobs[0]);
1480 }
1481
1482 /* V -> V/T ; T -> T/null */
1483 for (int i = 0; i < ctx->vertex_job_count; ++i) {
1484 bool isLast = (i + 1) == ctx->vertex_job_count;
1485
1486 panfrost_link_job_pair(ctx->u_vertex_jobs[i], isLast ? ctx->tiler_jobs[0] : ctx->vertex_jobs[i + 1]);
1487 }
1488
1489 /* T -> T/null */
1490 for (int i = 0; i < ctx->tiler_job_count; ++i) {
1491 bool isLast = (i + 1) == ctx->tiler_job_count;
1492 panfrost_link_job_pair(ctx->u_tiler_jobs[i], isLast ? 0 : ctx->tiler_jobs[i + 1]);
1493 }
1494 }
1495
1496 /* The entire frame is in memory -- send it off to the kernel! */
1497
1498 static void
1499 panfrost_submit_frame(struct panfrost_context *ctx, bool flush_immediate)
1500 {
1501 struct pipe_context *gallium = (struct pipe_context *) ctx;
1502 struct panfrost_screen *screen = pan_screen(gallium->screen);
1503
1504 /* Edge case if screen is cleared and nothing else */
1505 bool has_draws = ctx->draw_count > 0;
1506
1507 /* Workaround a bizarre lockup (a hardware errata?) */
1508 if (!has_draws)
1509 flush_immediate = true;
1510
1511 /* A number of jobs are batched -- this must be linked and cleared */
1512 panfrost_link_jobs(ctx);
1513
1514 ctx->draw_count = 0;
1515 ctx->vertex_job_count = 0;
1516 ctx->tiler_job_count = 0;
1517
1518 #ifndef DRY_RUN
1519
1520 bool is_scanout = panfrost_is_scanout(ctx);
1521 int fragment_id = screen->driver->submit_vs_fs_job(ctx, has_draws, is_scanout);
1522
1523 /* If visual, we can stall a frame */
1524
1525 if (panfrost_is_scanout(ctx) && !flush_immediate)
1526 screen->driver->force_flush_fragment(ctx);
1527
1528 screen->last_fragment_id = fragment_id;
1529 screen->last_fragment_flushed = false;
1530
1531 /* If readback, flush now (hurts the pipelined performance) */
1532 if (panfrost_is_scanout(ctx) && flush_immediate)
1533 screen->driver->force_flush_fragment(ctx);
1534
1535 #ifdef DUMP_PERFORMANCE_COUNTERS
1536 char filename[128];
1537 snprintf(filename, sizeof(filename), "/dev/shm/frame%d.mdgprf", ++performance_counter_number);
1538 FILE *fp = fopen(filename, "wb");
1539 fwrite(screen->perf_counters.cpu, 4096, sizeof(uint32_t), fp);
1540 fclose(fp);
1541 #endif
1542
1543 #endif
1544 }
1545
1546 bool dont_scanout = false;
1547
1548 void
1549 panfrost_flush(
1550 struct pipe_context *pipe,
1551 struct pipe_fence_handle **fence,
1552 unsigned flags)
1553 {
1554 struct panfrost_context *ctx = pan_context(pipe);
1555
1556 /* If there is nothing drawn, skip the frame */
1557 if (!ctx->draw_count && !ctx->frame_cleared) return;
1558
1559 if (!ctx->frame_cleared) {
1560 /* While there are draws, there was no clear. This is a partial
1561 * update, which needs to be handled via the "wallpaper"
1562 * method. We also need to fake a clear, just to get the
1563 * FRAGMENT job correct. */
1564
1565 panfrost_clear(&ctx->base, ctx->last_clear.buffers, ctx->last_clear.color, ctx->last_clear.depth, ctx->last_clear.stencil);
1566
1567 panfrost_draw_wallpaper(pipe);
1568 }
1569
1570 /* Frame clear handled, reset */
1571 ctx->frame_cleared = false;
1572
1573 /* Whether to stall the pipeline for immediately correct results */
1574 bool flush_immediate = flags & PIPE_FLUSH_END_OF_FRAME;
1575
1576 /* Submit the frame itself */
1577 panfrost_submit_frame(ctx, flush_immediate);
1578
1579 /* Prepare for the next frame */
1580 panfrost_invalidate_frame(ctx);
1581 }
1582
1583 #define DEFINE_CASE(c) case PIPE_PRIM_##c: return MALI_##c;
1584
1585 static int
1586 g2m_draw_mode(enum pipe_prim_type mode)
1587 {
1588 switch (mode) {
1589 DEFINE_CASE(POINTS);
1590 DEFINE_CASE(LINES);
1591 DEFINE_CASE(LINE_LOOP);
1592 DEFINE_CASE(LINE_STRIP);
1593 DEFINE_CASE(TRIANGLES);
1594 DEFINE_CASE(TRIANGLE_STRIP);
1595 DEFINE_CASE(TRIANGLE_FAN);
1596 DEFINE_CASE(QUADS);
1597 DEFINE_CASE(QUAD_STRIP);
1598 DEFINE_CASE(POLYGON);
1599
1600 default:
1601 printf("Illegal draw mode %d\n", mode);
1602 assert(0);
1603 return MALI_LINE_LOOP;
1604 }
1605 }
1606
1607 #undef DEFINE_CASE
1608
1609 static unsigned
1610 panfrost_translate_index_size(unsigned size)
1611 {
1612 switch (size) {
1613 case 1:
1614 return MALI_DRAW_INDEXED_UINT8;
1615
1616 case 2:
1617 return MALI_DRAW_INDEXED_UINT16;
1618
1619 case 4:
1620 return MALI_DRAW_INDEXED_UINT32;
1621
1622 default:
1623 printf("Unknown index size %d\n", size);
1624 assert(0);
1625 return 0;
1626 }
1627 }
1628
1629 static const uint8_t *
1630 panfrost_get_index_buffer_raw(const struct pipe_draw_info *info)
1631 {
1632 if (info->has_user_indices) {
1633 return (const uint8_t *) info->index.user;
1634 } else {
1635 struct panfrost_resource *rsrc = (struct panfrost_resource *) (info->index.resource);
1636 return (const uint8_t *) rsrc->bo->cpu[0];
1637 }
1638 }
1639
1640 /* Gets a GPU address for the associated index buffer. Only gauranteed to be
1641 * good for the duration of the draw (transient), could last longer */
1642
1643 static mali_ptr
1644 panfrost_get_index_buffer_mapped(struct panfrost_context *ctx, const struct pipe_draw_info *info)
1645 {
1646 struct panfrost_resource *rsrc = (struct panfrost_resource *) (info->index.resource);
1647
1648 off_t offset = info->start * info->index_size;
1649
1650 if (!info->has_user_indices) {
1651 /* Only resources can be directly mapped */
1652 return rsrc->bo->gpu[0] + offset;
1653 } else {
1654 /* Otherwise, we need to upload to transient memory */
1655 const uint8_t *ibuf8 = panfrost_get_index_buffer_raw(info);
1656 return panfrost_upload_transient(ctx, ibuf8 + offset, info->count * info->index_size);
1657 }
1658 }
1659
1660 static void
1661 panfrost_draw_vbo(
1662 struct pipe_context *pipe,
1663 const struct pipe_draw_info *info);
1664
1665 #define CALCULATE_MIN_MAX_INDEX(T, buffer, start, count) \
1666 for (unsigned _idx = (start); _idx < (start + count); ++_idx) { \
1667 T idx = buffer[_idx]; \
1668 if (idx > max_index) max_index = idx; \
1669 if (idx < min_index) min_index = idx; \
1670 }
1671
1672 static void
1673 panfrost_draw_vbo(
1674 struct pipe_context *pipe,
1675 const struct pipe_draw_info *info)
1676 {
1677 struct panfrost_context *ctx = pan_context(pipe);
1678
1679 ctx->payload_vertex.draw_start = info->start;
1680 ctx->payload_tiler.draw_start = info->start;
1681
1682 int mode = info->mode;
1683
1684 /* Fallback for unsupported modes */
1685
1686 if (!(ctx->draw_modes & mode)) {
1687 if (mode == PIPE_PRIM_QUADS && info->count == 4 && ctx->rasterizer && !ctx->rasterizer->base.flatshade) {
1688 mode = PIPE_PRIM_TRIANGLE_FAN;
1689 } else {
1690 if (info->count < 4) {
1691 /* Degenerate case? */
1692 return;
1693 }
1694
1695 util_primconvert_save_rasterizer_state(ctx->primconvert, &ctx->rasterizer->base);
1696 util_primconvert_draw_vbo(ctx->primconvert, info);
1697 return;
1698 }
1699 }
1700
1701 ctx->payload_tiler.prefix.draw_mode = g2m_draw_mode(mode);
1702
1703 ctx->vertex_count = info->count;
1704
1705 /* For non-indexed draws, they're the same */
1706 unsigned invocation_count = ctx->vertex_count;
1707
1708 /* For higher amounts of vertices (greater than what fits in a 16-bit
1709 * short), the other value is needed, otherwise there will be bizarre
1710 * rendering artefacts. It's not clear what these values mean yet. */
1711
1712 ctx->payload_tiler.prefix.unknown_draw &= ~(0x3000 | 0x18000);
1713 ctx->payload_tiler.prefix.unknown_draw |= (mode == PIPE_PRIM_POINTS || ctx->vertex_count > 65535) ? 0x3000 : 0x18000;
1714
1715 if (info->index_size) {
1716 /* Calculate the min/max index used so we can figure out how
1717 * many times to invoke the vertex shader */
1718
1719 const uint8_t *ibuf8 = panfrost_get_index_buffer_raw(info);
1720
1721 int min_index = INT_MAX;
1722 int max_index = 0;
1723
1724 if (info->index_size == 1) {
1725 CALCULATE_MIN_MAX_INDEX(uint8_t, ibuf8, info->start, info->count);
1726 } else if (info->index_size == 2) {
1727 const uint16_t *ibuf16 = (const uint16_t *) ibuf8;
1728 CALCULATE_MIN_MAX_INDEX(uint16_t, ibuf16, info->start, info->count);
1729 } else if (info->index_size == 4) {
1730 const uint32_t *ibuf32 = (const uint32_t *) ibuf8;
1731 CALCULATE_MIN_MAX_INDEX(uint32_t, ibuf32, info->start, info->count);
1732 } else {
1733 assert(0);
1734 }
1735
1736 /* Make sure we didn't go crazy */
1737 assert(min_index < INT_MAX);
1738 assert(max_index > 0);
1739 assert(max_index > min_index);
1740
1741 /* Use the corresponding values */
1742 invocation_count = max_index - min_index + 1;
1743 ctx->payload_vertex.draw_start = min_index;
1744 ctx->payload_tiler.draw_start = min_index;
1745
1746 ctx->payload_tiler.prefix.negative_start = -min_index;
1747 ctx->payload_tiler.prefix.index_count = MALI_POSITIVE(info->count);
1748
1749 //assert(!info->restart_index); /* TODO: Research */
1750 assert(!info->index_bias);
1751 //assert(!info->min_index); /* TODO: Use value */
1752
1753 ctx->payload_tiler.prefix.unknown_draw |= panfrost_translate_index_size(info->index_size);
1754 ctx->payload_tiler.prefix.indices = panfrost_get_index_buffer_mapped(ctx, info);
1755 } else {
1756 /* Index count == vertex count, if no indexing is applied, as
1757 * if it is internally indexed in the expected order */
1758
1759 ctx->payload_tiler.prefix.negative_start = 0;
1760 ctx->payload_tiler.prefix.index_count = MALI_POSITIVE(ctx->vertex_count);
1761
1762 /* Reverse index state */
1763 ctx->payload_tiler.prefix.unknown_draw &= ~MALI_DRAW_INDEXED_UINT32;
1764 ctx->payload_tiler.prefix.indices = (uintptr_t) NULL;
1765 }
1766
1767 ctx->payload_vertex.prefix.invocation_count = MALI_POSITIVE(invocation_count);
1768 ctx->payload_tiler.prefix.invocation_count = MALI_POSITIVE(invocation_count);
1769
1770 /* Fire off the draw itself */
1771 panfrost_queue_draw(ctx);
1772 }
1773
1774 /* CSO state */
1775
1776 static void
1777 panfrost_generic_cso_delete(struct pipe_context *pctx, void *hwcso)
1778 {
1779 free(hwcso);
1780 }
1781
1782 static void
1783 panfrost_set_scissor(struct panfrost_context *ctx)
1784 {
1785 const struct pipe_scissor_state *ss = &ctx->scissor;
1786
1787 if (ss && ctx->rasterizer && ctx->rasterizer->base.scissor && 0) {
1788 ctx->viewport->viewport0[0] = ss->minx;
1789 ctx->viewport->viewport0[1] = ss->miny;
1790 ctx->viewport->viewport1[0] = MALI_POSITIVE(ss->maxx);
1791 ctx->viewport->viewport1[1] = MALI_POSITIVE(ss->maxy);
1792 } else {
1793 ctx->viewport->viewport0[0] = 0;
1794 ctx->viewport->viewport0[1] = 0;
1795 ctx->viewport->viewport1[0] = MALI_POSITIVE(ctx->pipe_framebuffer.width);
1796 ctx->viewport->viewport1[1] = MALI_POSITIVE(ctx->pipe_framebuffer.height);
1797 }
1798 }
1799
1800 static void *
1801 panfrost_create_rasterizer_state(
1802 struct pipe_context *pctx,
1803 const struct pipe_rasterizer_state *cso)
1804 {
1805 struct panfrost_rasterizer *so = CALLOC_STRUCT(panfrost_rasterizer);
1806
1807 so->base = *cso;
1808
1809 /* Bitmask, unknown meaning of the start value */
1810 so->tiler_gl_enables = is_t6xx ? 0x105 : 0x7;
1811
1812 so->tiler_gl_enables |= MALI_FRONT_FACE(
1813 cso->front_ccw ? MALI_CCW : MALI_CW);
1814
1815 if (cso->cull_face & PIPE_FACE_FRONT)
1816 so->tiler_gl_enables |= MALI_CULL_FACE_FRONT;
1817
1818 if (cso->cull_face & PIPE_FACE_BACK)
1819 so->tiler_gl_enables |= MALI_CULL_FACE_BACK;
1820
1821 return so;
1822 }
1823
1824 static void
1825 panfrost_bind_rasterizer_state(
1826 struct pipe_context *pctx,
1827 void *hwcso)
1828 {
1829 struct panfrost_context *ctx = pan_context(pctx);
1830 struct pipe_rasterizer_state *cso = hwcso;
1831
1832 /* TODO: Why can't rasterizer be NULL ever? Other drivers are fine.. */
1833 if (!hwcso)
1834 return;
1835
1836 /* If scissor test has changed, we'll need to update that now */
1837 bool update_scissor = !ctx->rasterizer || ctx->rasterizer->base.scissor != cso->scissor;
1838
1839 ctx->rasterizer = hwcso;
1840
1841 /* Actualise late changes */
1842 if (update_scissor)
1843 panfrost_set_scissor(ctx);
1844
1845 ctx->dirty |= PAN_DIRTY_RASTERIZER;
1846 }
1847
1848 static void *
1849 panfrost_create_vertex_elements_state(
1850 struct pipe_context *pctx,
1851 unsigned num_elements,
1852 const struct pipe_vertex_element *elements)
1853 {
1854 struct panfrost_context *ctx = pan_context(pctx);
1855 struct panfrost_vertex_state *so = CALLOC_STRUCT(panfrost_vertex_state);
1856
1857 so->num_elements = num_elements;
1858 memcpy(so->pipe, elements, sizeof(*elements) * num_elements);
1859
1860 struct panfrost_transfer transfer = panfrost_allocate_chunk(ctx, sizeof(struct mali_attr_meta) * num_elements, HEAP_DESCRIPTOR);
1861 so->hw = (struct mali_attr_meta *) transfer.cpu;
1862 so->descriptor_ptr = transfer.gpu;
1863
1864 /* Allocate memory for the descriptor state */
1865
1866 for (int i = 0; i < num_elements; ++i) {
1867 so->hw[i].index = elements[i].vertex_buffer_index;
1868
1869 enum pipe_format fmt = elements[i].src_format;
1870 const struct util_format_description *desc = util_format_description(fmt);
1871 so->hw[i].unknown1 = 0x2;
1872 so->hw[i].swizzle = panfrost_get_default_swizzle(desc->nr_channels);
1873
1874 so->hw[i].format = panfrost_find_format(desc);
1875
1876 /* The field itself should probably be shifted over */
1877 so->hw[i].src_offset = elements[i].src_offset;
1878 }
1879
1880 return so;
1881 }
1882
1883 static void
1884 panfrost_bind_vertex_elements_state(
1885 struct pipe_context *pctx,
1886 void *hwcso)
1887 {
1888 struct panfrost_context *ctx = pan_context(pctx);
1889
1890 ctx->vertex = hwcso;
1891 ctx->dirty |= PAN_DIRTY_VERTEX;
1892 }
1893
1894 static void
1895 panfrost_delete_vertex_elements_state(struct pipe_context *pctx, void *hwcso)
1896 {
1897 struct panfrost_vertex_state *so = (struct panfrost_vertex_state *) hwcso;
1898 unsigned bytes = sizeof(struct mali_attr_meta) * so->num_elements;
1899 printf("Vertex elements delete leaks descriptor (%d bytes)\n", bytes);
1900 free(hwcso);
1901 }
1902
1903 static void *
1904 panfrost_create_shader_state(
1905 struct pipe_context *pctx,
1906 const struct pipe_shader_state *cso)
1907 {
1908 struct panfrost_shader_variants *so = CALLOC_STRUCT(panfrost_shader_variants);
1909 so->base = *cso;
1910
1911 /* Token deep copy to prevent memory corruption */
1912
1913 if (cso->type == PIPE_SHADER_IR_TGSI)
1914 so->base.tokens = tgsi_dup_tokens(so->base.tokens);
1915
1916 return so;
1917 }
1918
1919 static void
1920 panfrost_delete_shader_state(
1921 struct pipe_context *pctx,
1922 void *so)
1923 {
1924 struct panfrost_shader_variants *cso = (struct panfrost_shader_variants *) so;
1925
1926 if (cso->base.type == PIPE_SHADER_IR_TGSI) {
1927 printf("Deleting TGSI shader leaks duplicated tokens\n");
1928 }
1929
1930 unsigned leak = cso->variant_count * sizeof(struct mali_shader_meta);
1931 printf("Deleting shader state leaks descriptors (%d bytes), and shader bytecode\n", leak);
1932
1933 free(so);
1934 }
1935
1936 static void *
1937 panfrost_create_sampler_state(
1938 struct pipe_context *pctx,
1939 const struct pipe_sampler_state *cso)
1940 {
1941 struct panfrost_sampler_state *so = CALLOC_STRUCT(panfrost_sampler_state);
1942 so->base = *cso;
1943
1944 /* sampler_state corresponds to mali_sampler_descriptor, which we can generate entirely here */
1945
1946 struct mali_sampler_descriptor sampler_descriptor = {
1947 .filter_mode = MALI_TEX_MIN(translate_tex_filter(cso->min_img_filter))
1948 | MALI_TEX_MAG(translate_tex_filter(cso->mag_img_filter))
1949 | translate_mip_filter(cso->min_mip_filter)
1950 | 0x20,
1951
1952 .wrap_s = translate_tex_wrap(cso->wrap_s),
1953 .wrap_t = translate_tex_wrap(cso->wrap_t),
1954 .wrap_r = translate_tex_wrap(cso->wrap_r),
1955 .compare_func = panfrost_translate_alt_compare_func(cso->compare_func),
1956 .border_color = {
1957 cso->border_color.f[0],
1958 cso->border_color.f[1],
1959 cso->border_color.f[2],
1960 cso->border_color.f[3]
1961 },
1962 .min_lod = FIXED_16(0.0),
1963 .max_lod = FIXED_16(31.0),
1964 .unknown2 = 1,
1965 };
1966
1967 so->hw = sampler_descriptor;
1968
1969 return so;
1970 }
1971
1972 static void
1973 panfrost_bind_sampler_states(
1974 struct pipe_context *pctx,
1975 enum pipe_shader_type shader,
1976 unsigned start_slot, unsigned num_sampler,
1977 void **sampler)
1978 {
1979 assert(start_slot == 0);
1980
1981 struct panfrost_context *ctx = pan_context(pctx);
1982
1983 /* XXX: Should upload, not just copy? */
1984 ctx->sampler_count[shader] = num_sampler;
1985 memcpy(ctx->samplers[shader], sampler, num_sampler * sizeof (void *));
1986
1987 ctx->dirty |= PAN_DIRTY_SAMPLERS;
1988 }
1989
1990 static bool
1991 panfrost_variant_matches(struct panfrost_context *ctx, struct panfrost_shader_state *variant)
1992 {
1993 struct pipe_alpha_state *alpha = &ctx->depth_stencil->alpha;
1994
1995 if (alpha->enabled || variant->alpha_state.enabled) {
1996 /* Make sure enable state is at least the same */
1997 if (alpha->enabled != variant->alpha_state.enabled) {
1998 return false;
1999 }
2000
2001 /* Check that the contents of the test are the same */
2002 bool same_func = alpha->func == variant->alpha_state.func;
2003 bool same_ref = alpha->ref_value == variant->alpha_state.ref_value;
2004
2005 if (!(same_func && same_ref)) {
2006 return false;
2007 }
2008 }
2009 /* Otherwise, we're good to go */
2010 return true;
2011 }
2012
2013 static void
2014 panfrost_bind_fs_state(
2015 struct pipe_context *pctx,
2016 void *hwcso)
2017 {
2018 struct panfrost_context *ctx = pan_context(pctx);
2019
2020 ctx->fs = hwcso;
2021
2022 if (hwcso) {
2023 /* Match the appropriate variant */
2024
2025 signed variant = -1;
2026
2027 struct panfrost_shader_variants *variants = (struct panfrost_shader_variants *) hwcso;
2028
2029 for (unsigned i = 0; i < variants->variant_count; ++i) {
2030 if (panfrost_variant_matches(ctx, &variants->variants[i])) {
2031 variant = i;
2032 break;
2033 }
2034 }
2035
2036 if (variant == -1) {
2037 /* No variant matched, so create a new one */
2038 variant = variants->variant_count++;
2039 assert(variants->variant_count < MAX_SHADER_VARIANTS);
2040
2041 variants->variants[variant].base = hwcso;
2042 variants->variants[variant].alpha_state = ctx->depth_stencil->alpha;
2043
2044 /* Allocate the mapped descriptor ahead-of-time. TODO: Use for FS as well as VS */
2045 struct panfrost_context *ctx = pan_context(pctx);
2046 struct panfrost_transfer transfer = panfrost_allocate_chunk(ctx, sizeof(struct mali_shader_meta), HEAP_DESCRIPTOR);
2047
2048 variants->variants[variant].tripipe = (struct mali_shader_meta *) transfer.cpu;
2049 variants->variants[variant].tripipe_gpu = transfer.gpu;
2050
2051 }
2052
2053 /* Select this variant */
2054 variants->active_variant = variant;
2055
2056 struct panfrost_shader_state *shader_state = &variants->variants[variant];
2057 assert(panfrost_variant_matches(ctx, shader_state));
2058
2059 /* Now we have a variant selected, so compile and go */
2060
2061 if (!shader_state->compiled) {
2062 panfrost_shader_compile(ctx, shader_state->tripipe, NULL, JOB_TYPE_TILER, shader_state);
2063 shader_state->compiled = true;
2064 }
2065 }
2066
2067 ctx->dirty |= PAN_DIRTY_FS;
2068 }
2069
2070 static void
2071 panfrost_bind_vs_state(
2072 struct pipe_context *pctx,
2073 void *hwcso)
2074 {
2075 struct panfrost_context *ctx = pan_context(pctx);
2076
2077 ctx->vs = hwcso;
2078
2079 if (hwcso) {
2080 if (!ctx->vs->variants[0].compiled) {
2081 ctx->vs->variants[0].base = hwcso;
2082
2083 /* TODO DRY from above */
2084 struct panfrost_transfer transfer = panfrost_allocate_chunk(ctx, sizeof(struct mali_shader_meta), HEAP_DESCRIPTOR);
2085 ctx->vs->variants[0].tripipe = (struct mali_shader_meta *) transfer.cpu;
2086 ctx->vs->variants[0].tripipe_gpu = transfer.gpu;
2087
2088 panfrost_shader_compile(ctx, ctx->vs->variants[0].tripipe, NULL, JOB_TYPE_VERTEX, &ctx->vs->variants[0]);
2089 ctx->vs->variants[0].compiled = true;
2090 }
2091 }
2092
2093 ctx->dirty |= PAN_DIRTY_VS;
2094 }
2095
2096 static void
2097 panfrost_set_vertex_buffers(
2098 struct pipe_context *pctx,
2099 unsigned start_slot,
2100 unsigned num_buffers,
2101 const struct pipe_vertex_buffer *buffers)
2102 {
2103 struct panfrost_context *ctx = pan_context(pctx);
2104 assert(num_buffers <= PIPE_MAX_ATTRIBS);
2105
2106 /* XXX: Dirty tracking? etc */
2107 if (buffers) {
2108 size_t sz = sizeof(buffers[0]) * num_buffers;
2109 ctx->vertex_buffers = malloc(sz);
2110 ctx->vertex_buffer_count = num_buffers;
2111 memcpy(ctx->vertex_buffers, buffers, sz);
2112 } else {
2113 if (ctx->vertex_buffers) {
2114 free(ctx->vertex_buffers);
2115 ctx->vertex_buffers = NULL;
2116 }
2117
2118 ctx->vertex_buffer_count = 0;
2119 }
2120 }
2121
2122 static void
2123 panfrost_set_constant_buffer(
2124 struct pipe_context *pctx,
2125 enum pipe_shader_type shader, uint index,
2126 const struct pipe_constant_buffer *buf)
2127 {
2128 struct panfrost_context *ctx = pan_context(pctx);
2129 struct panfrost_constant_buffer *pbuf = &ctx->constant_buffer[shader];
2130
2131 size_t sz = buf ? buf->buffer_size : 0;
2132
2133 /* Free previous buffer */
2134
2135 pbuf->dirty = true;
2136 pbuf->size = sz;
2137
2138 if (pbuf->buffer) {
2139 free(pbuf->buffer);
2140 pbuf->buffer = NULL;
2141 }
2142
2143 /* If unbinding, we're done */
2144
2145 if (!buf)
2146 return;
2147
2148 /* Multiple constant buffers not yet supported */
2149 assert(index == 0);
2150
2151 const uint8_t *cpu;
2152
2153 struct panfrost_resource *rsrc = (struct panfrost_resource *) (buf->buffer);
2154
2155 if (rsrc) {
2156 cpu = rsrc->bo->cpu[0];
2157 } else if (buf->user_buffer) {
2158 cpu = buf->user_buffer;
2159 } else {
2160 printf("No constant buffer?\n");
2161 return;
2162 }
2163
2164 /* Copy the constant buffer into the driver context for later upload */
2165
2166 pbuf->buffer = malloc(sz);
2167 memcpy(pbuf->buffer, cpu + buf->buffer_offset, sz);
2168 }
2169
2170 static void
2171 panfrost_set_stencil_ref(
2172 struct pipe_context *pctx,
2173 const struct pipe_stencil_ref *ref)
2174 {
2175 struct panfrost_context *ctx = pan_context(pctx);
2176 ctx->stencil_ref = *ref;
2177
2178 /* Shader core dirty */
2179 ctx->dirty |= PAN_DIRTY_FS;
2180 }
2181
2182 static struct pipe_sampler_view *
2183 panfrost_create_sampler_view(
2184 struct pipe_context *pctx,
2185 struct pipe_resource *texture,
2186 const struct pipe_sampler_view *template)
2187 {
2188 struct panfrost_sampler_view *so = CALLOC_STRUCT(panfrost_sampler_view);
2189 int bytes_per_pixel = util_format_get_blocksize(texture->format);
2190
2191 pipe_reference(NULL, &texture->reference);
2192
2193 struct panfrost_resource *prsrc = (struct panfrost_resource *) texture;
2194
2195 so->base = *template;
2196 so->base.texture = texture;
2197 so->base.reference.count = 1;
2198 so->base.context = pctx;
2199
2200 /* sampler_views correspond to texture descriptors, minus the texture
2201 * (data) itself. So, we serialise the descriptor here and cache it for
2202 * later. */
2203
2204 /* TODO: Other types of textures */
2205 assert(template->target == PIPE_TEXTURE_2D);
2206
2207 /* Make sure it's something with which we're familiar */
2208 assert(bytes_per_pixel >= 1 && bytes_per_pixel <= 4);
2209
2210 /* TODO: Detect from format better */
2211 const struct util_format_description *desc = util_format_description(prsrc->base.format);
2212
2213 unsigned char user_swizzle[4] = {
2214 template->swizzle_r,
2215 template->swizzle_g,
2216 template->swizzle_b,
2217 template->swizzle_a
2218 };
2219
2220 enum mali_format format = panfrost_find_format(desc);
2221
2222 struct mali_texture_descriptor texture_descriptor = {
2223 .width = MALI_POSITIVE(texture->width0),
2224 .height = MALI_POSITIVE(texture->height0),
2225 .depth = MALI_POSITIVE(texture->depth0),
2226
2227 /* TODO: Decode */
2228 .format = {
2229 .swizzle = panfrost_translate_swizzle_4(desc->swizzle),
2230 .format = format,
2231
2232 .usage1 = 0x0,
2233 .is_not_cubemap = 1,
2234
2235 /* 0x11 - regular texture 2d, uncompressed tiled */
2236 /* 0x12 - regular texture 2d, uncompressed linear */
2237 /* 0x1c - AFBC compressed (internally tiled, probably) texture 2D */
2238
2239 .usage2 = prsrc->bo->has_afbc ? 0x1c : (prsrc->bo->tiled ? 0x11 : 0x12),
2240 },
2241
2242 .swizzle = panfrost_translate_swizzle_4(user_swizzle)
2243 };
2244
2245 /* TODO: Other base levels require adjusting dimensions / level numbers / etc */
2246 assert (template->u.tex.first_level == 0);
2247
2248 texture_descriptor.nr_mipmap_levels = template->u.tex.last_level - template->u.tex.first_level;
2249
2250 so->hw = texture_descriptor;
2251
2252 return (struct pipe_sampler_view *) so;
2253 }
2254
2255 static void
2256 panfrost_set_sampler_views(
2257 struct pipe_context *pctx,
2258 enum pipe_shader_type shader,
2259 unsigned start_slot, unsigned num_views,
2260 struct pipe_sampler_view **views)
2261 {
2262 struct panfrost_context *ctx = pan_context(pctx);
2263
2264 assert(start_slot == 0);
2265
2266 ctx->sampler_view_count[shader] = num_views;
2267 memcpy(ctx->sampler_views[shader], views, num_views * sizeof (void *));
2268
2269 ctx->dirty |= PAN_DIRTY_TEXTURES;
2270 }
2271
2272 static void
2273 panfrost_sampler_view_destroy(
2274 struct pipe_context *pctx,
2275 struct pipe_sampler_view *views)
2276 {
2277 //struct panfrost_context *ctx = pan_context(pctx);
2278
2279 /* TODO */
2280
2281 free(views);
2282 }
2283
2284 static void
2285 panfrost_set_framebuffer_state(struct pipe_context *pctx,
2286 const struct pipe_framebuffer_state *fb)
2287 {
2288 struct panfrost_context *ctx = pan_context(pctx);
2289
2290 /* Flush when switching away from an FBO */
2291
2292 if (!panfrost_is_scanout(ctx)) {
2293 panfrost_flush(pctx, NULL, 0);
2294 }
2295
2296 ctx->pipe_framebuffer.nr_cbufs = fb->nr_cbufs;
2297 ctx->pipe_framebuffer.samples = fb->samples;
2298 ctx->pipe_framebuffer.layers = fb->layers;
2299 ctx->pipe_framebuffer.width = fb->width;
2300 ctx->pipe_framebuffer.height = fb->height;
2301
2302 for (int i = 0; i < PIPE_MAX_COLOR_BUFS; i++) {
2303 struct pipe_surface *cb = i < fb->nr_cbufs ? fb->cbufs[i] : NULL;
2304
2305 /* check if changing cbuf */
2306 if (ctx->pipe_framebuffer.cbufs[i] == cb) continue;
2307
2308 if (cb && (i != 0)) {
2309 printf("XXX: Multiple render targets not supported before t7xx!\n");
2310 assert(0);
2311 }
2312
2313 /* assign new */
2314 pipe_surface_reference(&ctx->pipe_framebuffer.cbufs[i], cb);
2315
2316 if (!cb)
2317 continue;
2318
2319 if (require_sfbd)
2320 ctx->vt_framebuffer_sfbd = panfrost_emit_sfbd(ctx);
2321 else
2322 ctx->vt_framebuffer_mfbd = panfrost_emit_mfbd(ctx);
2323
2324 panfrost_attach_vt_framebuffer(ctx);
2325 panfrost_new_frag_framebuffer(ctx);
2326 panfrost_set_scissor(ctx);
2327
2328 struct panfrost_resource *tex = ((struct panfrost_resource *) ctx->pipe_framebuffer.cbufs[i]->texture);
2329 bool is_scanout = panfrost_is_scanout(ctx);
2330
2331 if (!is_scanout && !tex->bo->has_afbc) {
2332 /* The blob is aggressive about enabling AFBC. As such,
2333 * it's pretty much necessary to use it here, since we
2334 * have no traces of non-compressed FBO. */
2335
2336 panfrost_enable_afbc(ctx, tex, false);
2337 }
2338
2339 if (!is_scanout && !tex->bo->has_checksum) {
2340 /* Enable transaction elimination if we can */
2341 panfrost_enable_checksum(ctx, tex);
2342 }
2343 }
2344
2345 {
2346 struct pipe_surface *zb = fb->zsbuf;
2347
2348 if (ctx->pipe_framebuffer.zsbuf != zb) {
2349 pipe_surface_reference(&ctx->pipe_framebuffer.zsbuf, zb);
2350
2351 if (zb) {
2352 /* FBO has depth */
2353
2354 if (require_sfbd)
2355 ctx->vt_framebuffer_sfbd = panfrost_emit_sfbd(ctx);
2356 else
2357 ctx->vt_framebuffer_mfbd = panfrost_emit_mfbd(ctx);
2358
2359 panfrost_attach_vt_framebuffer(ctx);
2360 panfrost_new_frag_framebuffer(ctx);
2361 panfrost_set_scissor(ctx);
2362
2363 struct panfrost_resource *tex = ((struct panfrost_resource *) ctx->pipe_framebuffer.zsbuf->texture);
2364
2365 if (!tex->bo->has_afbc && !panfrost_is_scanout(ctx))
2366 panfrost_enable_afbc(ctx, tex, true);
2367 }
2368 }
2369 }
2370
2371 /* Force a clear XXX wrong? */
2372 if (ctx->last_clear.color)
2373 panfrost_clear(&ctx->base, ctx->last_clear.buffers, ctx->last_clear.color, ctx->last_clear.depth, ctx->last_clear.stencil);
2374 }
2375
2376 static void *
2377 panfrost_create_blend_state(struct pipe_context *pipe,
2378 const struct pipe_blend_state *blend)
2379 {
2380 struct panfrost_context *ctx = pan_context(pipe);
2381 struct panfrost_blend_state *so = CALLOC_STRUCT(panfrost_blend_state);
2382 so->base = *blend;
2383
2384 /* TODO: The following features are not yet implemented */
2385 assert(!blend->logicop_enable);
2386 assert(!blend->alpha_to_coverage);
2387 assert(!blend->alpha_to_one);
2388
2389 /* Compile the blend state, first as fixed-function if we can */
2390
2391 if (panfrost_make_fixed_blend_mode(&blend->rt[0], &so->equation, blend->rt[0].colormask, &ctx->blend_color))
2392 return so;
2393
2394 /* If we can't, compile a blend shader instead */
2395
2396 panfrost_make_blend_shader(ctx, so, &ctx->blend_color);
2397
2398 return so;
2399 }
2400
2401 static void
2402 panfrost_bind_blend_state(struct pipe_context *pipe,
2403 void *cso)
2404 {
2405 struct panfrost_context *ctx = pan_context(pipe);
2406 struct pipe_blend_state *blend = (struct pipe_blend_state *) cso;
2407 struct panfrost_blend_state *pblend = (struct panfrost_blend_state *) cso;
2408 ctx->blend = pblend;
2409
2410 if (!blend)
2411 return;
2412
2413 SET_BIT(ctx->fragment_shader_core.unknown2_4, MALI_NO_DITHER, !blend->dither);
2414
2415 /* TODO: Attach color */
2416
2417 /* Shader itself is not dirty, but the shader core is */
2418 ctx->dirty |= PAN_DIRTY_FS;
2419 }
2420
2421 static void
2422 panfrost_delete_blend_state(struct pipe_context *pipe,
2423 void *blend)
2424 {
2425 struct panfrost_blend_state *so = (struct panfrost_blend_state *) blend;
2426
2427 if (so->has_blend_shader) {
2428 printf("Deleting blend state leak blend shaders bytecode\n");
2429 }
2430
2431 free(blend);
2432 }
2433
2434 static void
2435 panfrost_set_blend_color(struct pipe_context *pipe,
2436 const struct pipe_blend_color *blend_color)
2437 {
2438 struct panfrost_context *ctx = pan_context(pipe);
2439
2440 /* If blend_color is we're unbinding, so ctx->blend_color is now undefined -> nothing to do */
2441
2442 if (blend_color) {
2443 ctx->blend_color = *blend_color;
2444
2445 /* The blend mode depends on the blend constant color, due to the
2446 * fixed/programmable split. So, we're forced to regenerate the blend
2447 * equation */
2448
2449 /* TODO: Attach color */
2450 }
2451 }
2452
2453 static void *
2454 panfrost_create_depth_stencil_state(struct pipe_context *pipe,
2455 const struct pipe_depth_stencil_alpha_state *depth_stencil)
2456 {
2457 return mem_dup(depth_stencil, sizeof(*depth_stencil));
2458 }
2459
2460 static void
2461 panfrost_bind_depth_stencil_state(struct pipe_context *pipe,
2462 void *cso)
2463 {
2464 struct panfrost_context *ctx = pan_context(pipe);
2465 struct pipe_depth_stencil_alpha_state *depth_stencil = cso;
2466 ctx->depth_stencil = depth_stencil;
2467
2468 if (!depth_stencil)
2469 return;
2470
2471 /* Alpha does not exist in the hardware (it's not in ES3), so it's
2472 * emulated in the fragment shader */
2473
2474 if (depth_stencil->alpha.enabled) {
2475 /* We need to trigger a new shader (maybe) */
2476 ctx->base.bind_fs_state(&ctx->base, ctx->fs);
2477 }
2478
2479 /* Stencil state */
2480 SET_BIT(ctx->fragment_shader_core.unknown2_4, MALI_STENCIL_TEST, depth_stencil->stencil[0].enabled); /* XXX: which one? */
2481
2482 panfrost_make_stencil_state(&depth_stencil->stencil[0], &ctx->fragment_shader_core.stencil_front);
2483 ctx->fragment_shader_core.stencil_mask_front = depth_stencil->stencil[0].writemask;
2484
2485 panfrost_make_stencil_state(&depth_stencil->stencil[1], &ctx->fragment_shader_core.stencil_back);
2486 ctx->fragment_shader_core.stencil_mask_back = depth_stencil->stencil[1].writemask;
2487
2488 /* Depth state (TODO: Refactor) */
2489 SET_BIT(ctx->fragment_shader_core.unknown2_3, MALI_DEPTH_TEST, depth_stencil->depth.enabled);
2490
2491 int func = depth_stencil->depth.enabled ? depth_stencil->depth.func : PIPE_FUNC_ALWAYS;
2492
2493 ctx->fragment_shader_core.unknown2_3 &= ~MALI_DEPTH_FUNC_MASK;
2494 ctx->fragment_shader_core.unknown2_3 |= MALI_DEPTH_FUNC(panfrost_translate_compare_func(func));
2495
2496 /* Bounds test not implemented */
2497 assert(!depth_stencil->depth.bounds_test);
2498
2499 ctx->dirty |= PAN_DIRTY_FS;
2500 }
2501
2502 static void
2503 panfrost_delete_depth_stencil_state(struct pipe_context *pipe, void *depth)
2504 {
2505 free( depth );
2506 }
2507
2508 static void
2509 panfrost_set_sample_mask(struct pipe_context *pipe,
2510 unsigned sample_mask)
2511 {
2512 }
2513
2514 static void
2515 panfrost_set_clip_state(struct pipe_context *pipe,
2516 const struct pipe_clip_state *clip)
2517 {
2518 //struct panfrost_context *panfrost = pan_context(pipe);
2519 }
2520
2521 static void
2522 panfrost_set_viewport_states(struct pipe_context *pipe,
2523 unsigned start_slot,
2524 unsigned num_viewports,
2525 const struct pipe_viewport_state *viewports)
2526 {
2527 struct panfrost_context *ctx = pan_context(pipe);
2528
2529 assert(start_slot == 0);
2530 assert(num_viewports == 1);
2531
2532 ctx->pipe_viewport = *viewports;
2533
2534 #if 0
2535 /* TODO: What if not centered? */
2536 float w = abs(viewports->scale[0]) * 2.0;
2537 float h = abs(viewports->scale[1]) * 2.0;
2538
2539 ctx->viewport.viewport1[0] = MALI_POSITIVE((int) w);
2540 ctx->viewport.viewport1[1] = MALI_POSITIVE((int) h);
2541 #endif
2542 }
2543
2544 static void
2545 panfrost_set_scissor_states(struct pipe_context *pipe,
2546 unsigned start_slot,
2547 unsigned num_scissors,
2548 const struct pipe_scissor_state *scissors)
2549 {
2550 struct panfrost_context *ctx = pan_context(pipe);
2551
2552 assert(start_slot == 0);
2553 assert(num_scissors == 1);
2554
2555 ctx->scissor = *scissors;
2556
2557 panfrost_set_scissor(ctx);
2558 }
2559
2560 static void
2561 panfrost_set_polygon_stipple(struct pipe_context *pipe,
2562 const struct pipe_poly_stipple *stipple)
2563 {
2564 //struct panfrost_context *panfrost = pan_context(pipe);
2565 }
2566
2567 static void
2568 panfrost_set_active_query_state(struct pipe_context *pipe,
2569 boolean enable)
2570 {
2571 //struct panfrost_context *panfrost = pan_context(pipe);
2572 }
2573
2574 static void
2575 panfrost_destroy(struct pipe_context *pipe)
2576 {
2577 struct panfrost_context *panfrost = pan_context(pipe);
2578
2579 if (panfrost->blitter)
2580 util_blitter_destroy(panfrost->blitter);
2581 }
2582
2583 static struct pipe_query *
2584 panfrost_create_query(struct pipe_context *pipe,
2585 unsigned type,
2586 unsigned index)
2587 {
2588 struct panfrost_query *q = CALLOC_STRUCT(panfrost_query);
2589
2590 q->type = type;
2591 q->index = index;
2592
2593 return (struct pipe_query *) q;
2594 }
2595
2596 static void
2597 panfrost_destroy_query(struct pipe_context *pipe, struct pipe_query *q)
2598 {
2599 FREE(q);
2600 }
2601
2602 static boolean
2603 panfrost_begin_query(struct pipe_context *pipe, struct pipe_query *q)
2604 {
2605 struct panfrost_context *ctx = pan_context(pipe);
2606 struct panfrost_query *query = (struct panfrost_query *) q;
2607
2608 switch (query->type) {
2609 case PIPE_QUERY_OCCLUSION_COUNTER:
2610 case PIPE_QUERY_OCCLUSION_PREDICATE:
2611 case PIPE_QUERY_OCCLUSION_PREDICATE_CONSERVATIVE:
2612 {
2613 /* Allocate a word for the query results to be stored */
2614 query->transfer = panfrost_allocate_chunk(ctx, sizeof(unsigned), HEAP_DESCRIPTOR);
2615
2616 ctx->occlusion_query = query;
2617
2618 break;
2619 }
2620
2621 default:
2622 fprintf(stderr, "Skipping query %d\n", query->type);
2623 break;
2624 }
2625
2626 return true;
2627 }
2628
2629 static bool
2630 panfrost_end_query(struct pipe_context *pipe, struct pipe_query *q)
2631 {
2632 struct panfrost_context *ctx = pan_context(pipe);
2633 ctx->occlusion_query = NULL;
2634 return true;
2635 }
2636
2637 static boolean
2638 panfrost_get_query_result(struct pipe_context *pipe,
2639 struct pipe_query *q,
2640 boolean wait,
2641 union pipe_query_result *vresult)
2642 {
2643 /* STUB */
2644 struct panfrost_query *query = (struct panfrost_query *) q;
2645
2646 /* We need to flush out the jobs to actually run the counter, TODO
2647 * check wait, TODO wallpaper after if needed */
2648
2649 panfrost_flush(pipe, NULL, PIPE_FLUSH_END_OF_FRAME);
2650
2651 switch (query->type) {
2652 case PIPE_QUERY_OCCLUSION_COUNTER:
2653 case PIPE_QUERY_OCCLUSION_PREDICATE:
2654 case PIPE_QUERY_OCCLUSION_PREDICATE_CONSERVATIVE: {
2655 /* Read back the query results */
2656 unsigned *result = (unsigned *) query->transfer.cpu;
2657 unsigned passed = *result;
2658
2659 if (query->type == PIPE_QUERY_OCCLUSION_COUNTER) {
2660 vresult->u64 = passed;
2661 } else {
2662 vresult->b = !!passed;
2663 }
2664
2665 break;
2666 }
2667 default:
2668 fprintf(stderr, "Skipped query get %d\n", query->type);
2669 break;
2670 }
2671
2672 return true;
2673 }
2674
2675 static void
2676 panfrost_setup_hardware(struct panfrost_context *ctx)
2677 {
2678 struct pipe_context *gallium = (struct pipe_context *) ctx;
2679 struct panfrost_screen *screen = pan_screen(gallium->screen);
2680
2681 for (int i = 0; i < ARRAY_SIZE(ctx->transient_pools); ++i) {
2682 /* Allocate the beginning of the transient pool */
2683 int entry_size = (1 << 22); /* 4MB */
2684
2685 ctx->transient_pools[i].entry_size = entry_size;
2686 ctx->transient_pools[i].entry_count = 1;
2687
2688 ctx->transient_pools[i].entries[0] = (struct panfrost_memory_entry *) pb_slab_alloc(&screen->slabs, entry_size, HEAP_TRANSIENT);
2689 }
2690
2691 screen->driver->allocate_slab(screen, &ctx->scratchpad, 64, false, 0, 0, 0);
2692 screen->driver->allocate_slab(screen, &ctx->varying_mem, 16384, false, 0, 0, 0);
2693 screen->driver->allocate_slab(screen, &ctx->shaders, 4096, true, PAN_ALLOCATE_EXECUTE, 0, 0);
2694 screen->driver->allocate_slab(screen, &ctx->tiler_heap, 32768, false, PAN_ALLOCATE_GROWABLE, 1, 128);
2695 screen->driver->allocate_slab(screen, &ctx->misc_0, 128, false, PAN_ALLOCATE_GROWABLE, 1, 128);
2696
2697 }
2698
2699 /* New context creation, which also does hardware initialisation since I don't
2700 * know the better way to structure this :smirk: */
2701
2702 struct pipe_context *
2703 panfrost_create_context(struct pipe_screen *screen, void *priv, unsigned flags)
2704 {
2705 struct panfrost_context *ctx = CALLOC_STRUCT(panfrost_context);
2706 memset(ctx, 0, sizeof(*ctx));
2707 struct pipe_context *gallium = (struct pipe_context *) ctx;
2708
2709 gallium->screen = screen;
2710
2711 gallium->destroy = panfrost_destroy;
2712
2713 gallium->set_framebuffer_state = panfrost_set_framebuffer_state;
2714
2715 gallium->flush = panfrost_flush;
2716 gallium->clear = panfrost_clear;
2717 gallium->draw_vbo = panfrost_draw_vbo;
2718
2719 gallium->set_vertex_buffers = panfrost_set_vertex_buffers;
2720 gallium->set_constant_buffer = panfrost_set_constant_buffer;
2721
2722 gallium->set_stencil_ref = panfrost_set_stencil_ref;
2723
2724 gallium->create_sampler_view = panfrost_create_sampler_view;
2725 gallium->set_sampler_views = panfrost_set_sampler_views;
2726 gallium->sampler_view_destroy = panfrost_sampler_view_destroy;
2727
2728 gallium->create_rasterizer_state = panfrost_create_rasterizer_state;
2729 gallium->bind_rasterizer_state = panfrost_bind_rasterizer_state;
2730 gallium->delete_rasterizer_state = panfrost_generic_cso_delete;
2731
2732 gallium->create_vertex_elements_state = panfrost_create_vertex_elements_state;
2733 gallium->bind_vertex_elements_state = panfrost_bind_vertex_elements_state;
2734 gallium->delete_vertex_elements_state = panfrost_delete_vertex_elements_state;
2735
2736 gallium->create_fs_state = panfrost_create_shader_state;
2737 gallium->delete_fs_state = panfrost_delete_shader_state;
2738 gallium->bind_fs_state = panfrost_bind_fs_state;
2739
2740 gallium->create_vs_state = panfrost_create_shader_state;
2741 gallium->delete_vs_state = panfrost_delete_shader_state;
2742 gallium->bind_vs_state = panfrost_bind_vs_state;
2743
2744 gallium->create_sampler_state = panfrost_create_sampler_state;
2745 gallium->delete_sampler_state = panfrost_generic_cso_delete;
2746 gallium->bind_sampler_states = panfrost_bind_sampler_states;
2747
2748 gallium->create_blend_state = panfrost_create_blend_state;
2749 gallium->bind_blend_state = panfrost_bind_blend_state;
2750 gallium->delete_blend_state = panfrost_delete_blend_state;
2751
2752 gallium->set_blend_color = panfrost_set_blend_color;
2753
2754 gallium->create_depth_stencil_alpha_state = panfrost_create_depth_stencil_state;
2755 gallium->bind_depth_stencil_alpha_state = panfrost_bind_depth_stencil_state;
2756 gallium->delete_depth_stencil_alpha_state = panfrost_delete_depth_stencil_state;
2757
2758 gallium->set_sample_mask = panfrost_set_sample_mask;
2759
2760 gallium->set_clip_state = panfrost_set_clip_state;
2761 gallium->set_viewport_states = panfrost_set_viewport_states;
2762 gallium->set_scissor_states = panfrost_set_scissor_states;
2763 gallium->set_polygon_stipple = panfrost_set_polygon_stipple;
2764 gallium->set_active_query_state = panfrost_set_active_query_state;
2765
2766 gallium->create_query = panfrost_create_query;
2767 gallium->destroy_query = panfrost_destroy_query;
2768 gallium->begin_query = panfrost_begin_query;
2769 gallium->end_query = panfrost_end_query;
2770 gallium->get_query_result = panfrost_get_query_result;
2771
2772 panfrost_resource_context_init(gallium);
2773
2774 panfrost_setup_hardware(ctx);
2775
2776 /* XXX: leaks */
2777 gallium->stream_uploader = u_upload_create_default(gallium);
2778 gallium->const_uploader = gallium->stream_uploader;
2779 assert(gallium->stream_uploader);
2780
2781 /* Midgard supports ES modes, plus QUADS/QUAD_STRIPS/POLYGON */
2782 ctx->draw_modes = (1 << (PIPE_PRIM_POLYGON + 1)) - 1;
2783
2784 ctx->primconvert = util_primconvert_create(gallium, ctx->draw_modes);
2785
2786 ctx->blitter = util_blitter_create(gallium);
2787 assert(ctx->blitter);
2788
2789 /* Prepare for render! */
2790
2791 panfrost_emit_vertex_payload(ctx);
2792 panfrost_emit_tiler_payload(ctx);
2793 panfrost_invalidate_frame(ctx);
2794 panfrost_viewport(ctx, 0.0, 1.0, 0, 0, ctx->pipe_framebuffer.width, ctx->pipe_framebuffer.height);
2795 panfrost_default_shader_backend(ctx);
2796 panfrost_generate_space_filler_indices();
2797
2798 return gallium;
2799 }