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