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