80b9512371b73b4dee7b405b5f52f28a15ac242a
[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 * ALIGN(rsrc->base.width0, 16);
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 static mali_ptr
651 panfrost_emit_varyings(
652 struct panfrost_context *ctx,
653 union mali_attr *slot,
654 unsigned stride,
655 unsigned count)
656 {
657 mali_ptr varying_address = ctx->varying_mem.gpu + ctx->varying_height;
658
659 /* Fill out the descriptor */
660 slot->elements = varying_address | MALI_ATTR_LINEAR;
661 slot->stride = stride;
662 slot->size = stride * count;
663
664 ctx->varying_height += ALIGN(slot->size, 64);
665 assert(ctx->varying_height < ctx->varying_mem.size);
666
667 return varying_address;
668 }
669
670 static void
671 panfrost_emit_point_coord(union mali_attr *slot)
672 {
673 slot->elements = MALI_VARYING_POINT_COORD | MALI_ATTR_LINEAR;
674 slot->stride = slot->size = 0;
675 }
676
677 static void
678 panfrost_emit_varying_descriptor(
679 struct panfrost_context *ctx,
680 unsigned invocation_count)
681 {
682 /* Load the shaders */
683
684 struct panfrost_shader_state *vs = &ctx->vs->variants[ctx->vs->active_variant];
685 struct panfrost_shader_state *fs = &ctx->fs->variants[ctx->fs->active_variant];
686
687 /* Allocate the varying descriptor */
688
689 size_t vs_size = sizeof(struct mali_attr_meta) * vs->tripipe->varying_count;
690 size_t fs_size = sizeof(struct mali_attr_meta) * fs->tripipe->varying_count;
691
692 struct panfrost_transfer trans = panfrost_allocate_transient(ctx,
693 vs_size + fs_size);
694
695 memcpy(trans.cpu, vs->varyings, vs_size);
696 memcpy(trans.cpu + vs_size, fs->varyings, fs_size);
697
698 ctx->payload_vertex.postfix.varying_meta = trans.gpu;
699 ctx->payload_tiler.postfix.varying_meta = trans.gpu + vs_size;
700
701 /* Buffer indices must be in this order per our convention */
702 union mali_attr varyings[PIPE_MAX_ATTRIBS];
703 unsigned idx = 0;
704
705 /* General varyings -- use the VS's, since those are more likely to be
706 * accurate on desktop */
707
708 panfrost_emit_varyings(ctx, &varyings[idx++],
709 vs->general_varying_stride, invocation_count);
710
711 /* fp32 vec4 gl_Position */
712 ctx->payload_tiler.postfix.position_varying =
713 panfrost_emit_varyings(ctx, &varyings[idx++],
714 sizeof(float) * 4, invocation_count);
715
716
717 if (vs->writes_point_size || fs->reads_point_coord) {
718 /* fp16 vec1 gl_PointSize */
719 ctx->payload_tiler.primitive_size.pointer =
720 panfrost_emit_varyings(ctx, &varyings[idx++],
721 2, invocation_count);
722 }
723
724 if (fs->reads_point_coord) {
725 /* Special descriptor */
726 panfrost_emit_point_coord(&varyings[idx++]);
727 }
728
729 mali_ptr varyings_p = panfrost_upload_transient(ctx, &varyings, idx * sizeof(union mali_attr));
730 ctx->payload_vertex.postfix.varyings = varyings_p;
731 ctx->payload_tiler.postfix.varyings = varyings_p;
732 }
733
734 /* Emits attributes and varying descriptors, which should be called every draw,
735 * excepting some obscure circumstances */
736
737 static void
738 panfrost_emit_vertex_data(struct panfrost_context *ctx)
739 {
740 /* TODO: Only update the dirtied buffers */
741 union mali_attr attrs[PIPE_MAX_ATTRIBS];
742
743 unsigned invocation_count = MALI_NEGATIVE(ctx->payload_tiler.prefix.invocation_count);
744
745 for (int i = 0; i < ctx->vertex_buffer_count; ++i) {
746 struct pipe_vertex_buffer *buf = &ctx->vertex_buffers[i];
747 struct panfrost_resource *rsrc = (struct panfrost_resource *) (buf->buffer.resource);
748
749 /* Let's figure out the layout of the attributes in memory so
750 * we can be smart about size computation. The idea is to
751 * figure out the maximum src_offset, which tells us the latest
752 * spot a vertex could start. Meanwhile, we figure out the size
753 * of the attribute memory (assuming interleaved
754 * representation) and tack on the max src_offset for a
755 * reasonably good upper bound on the size.
756 *
757 * Proving correctness is left as an exercise to the reader.
758 */
759
760 unsigned max_src_offset = 0;
761
762 for (unsigned j = 0; j < ctx->vertex->num_elements; ++j) {
763 if (ctx->vertex->pipe[j].vertex_buffer_index != i) continue;
764 max_src_offset = MAX2(max_src_offset, ctx->vertex->pipe[j].src_offset);
765 }
766
767 /* Offset vertex count by draw_start to make sure we upload enough */
768 attrs[i].stride = buf->stride;
769 attrs[i].size = buf->stride * (ctx->payload_vertex.draw_start + invocation_count) + max_src_offset;
770
771 /* Vertex elements are -already- GPU-visible, at
772 * rsrc->gpu. However, attribute buffers must be 64 aligned. If
773 * it is not, for now we have to duplicate the buffer. */
774
775 mali_ptr effective_address = (rsrc->bo->gpu[0] + buf->buffer_offset);
776
777 if (effective_address & 0x3F) {
778 attrs[i].elements = panfrost_upload_transient(ctx, rsrc->bo->cpu[0] + buf->buffer_offset, attrs[i].size) | 1;
779 } else {
780 attrs[i].elements = effective_address | 1;
781 }
782 }
783
784 ctx->payload_vertex.postfix.attributes = panfrost_upload_transient(ctx, attrs, ctx->vertex_buffer_count * sizeof(union mali_attr));
785
786 panfrost_emit_varying_descriptor(ctx, invocation_count);
787 }
788
789 /* Go through dirty flags and actualise them in the cmdstream. */
790
791 void
792 panfrost_emit_for_draw(struct panfrost_context *ctx, bool with_vertex_data)
793 {
794 struct panfrost_job *job = panfrost_get_job_for_fbo(ctx);
795
796 if (with_vertex_data) {
797 panfrost_emit_vertex_data(ctx);
798 }
799
800 bool msaa = ctx->rasterizer->base.multisample;
801
802 if (ctx->dirty & PAN_DIRTY_RASTERIZER) {
803 ctx->payload_tiler.gl_enables = ctx->rasterizer->tiler_gl_enables;
804
805 /* TODO: Sample size */
806 SET_BIT(ctx->fragment_shader_core.unknown2_3, MALI_HAS_MSAA, msaa);
807 SET_BIT(ctx->fragment_shader_core.unknown2_4, MALI_NO_MSAA, !msaa);
808 }
809
810 /* Enable job requirements at draw-time */
811
812 if (msaa)
813 job->requirements |= PAN_REQ_MSAA;
814
815 if (ctx->depth_stencil->depth.writemask)
816 job->requirements |= PAN_REQ_DEPTH_WRITE;
817
818 if (ctx->occlusion_query) {
819 ctx->payload_tiler.gl_enables |= MALI_OCCLUSION_QUERY | MALI_OCCLUSION_PRECISE;
820 ctx->payload_tiler.postfix.occlusion_counter = ctx->occlusion_query->transfer.gpu;
821 }
822
823 if (ctx->dirty & PAN_DIRTY_VS) {
824 assert(ctx->vs);
825
826 struct panfrost_shader_state *vs = &ctx->vs->variants[ctx->vs->active_variant];
827
828 /* Late shader descriptor assignments */
829
830 vs->tripipe->texture_count = ctx->sampler_view_count[PIPE_SHADER_VERTEX];
831 vs->tripipe->sampler_count = ctx->sampler_count[PIPE_SHADER_VERTEX];
832
833 /* Who knows */
834 vs->tripipe->midgard1.unknown1 = 0x2201;
835
836 ctx->payload_vertex.postfix._shader_upper = vs->tripipe_gpu >> 4;
837 }
838
839 if (ctx->dirty & (PAN_DIRTY_RASTERIZER | PAN_DIRTY_VS)) {
840 /* Check if we need to link the gl_PointSize varying */
841 assert(ctx->vs);
842 struct panfrost_shader_state *vs = &ctx->vs->variants[ctx->vs->active_variant];
843
844 bool needs_gl_point_size = vs->writes_point_size && ctx->payload_tiler.prefix.draw_mode == MALI_POINTS;
845
846 if (!needs_gl_point_size) {
847 /* If the size is constant, write it out. Otherwise,
848 * don't touch primitive_size (since we would clobber
849 * the pointer there) */
850
851 ctx->payload_tiler.primitive_size.constant = ctx->rasterizer->base.line_width;
852 }
853
854 /* Set the flag for varying (pointer) point size if the shader needs that */
855 SET_BIT(ctx->payload_tiler.prefix.unknown_draw, MALI_DRAW_VARYING_SIZE, needs_gl_point_size);
856 }
857
858 /* TODO: Maybe dirty track FS, maybe not. For now, it's transient. */
859 if (ctx->fs)
860 ctx->dirty |= PAN_DIRTY_FS;
861
862 if (ctx->dirty & PAN_DIRTY_FS) {
863 assert(ctx->fs);
864 struct panfrost_shader_state *variant = &ctx->fs->variants[ctx->fs->active_variant];
865
866 #define COPY(name) ctx->fragment_shader_core.name = variant->tripipe->name
867
868 COPY(shader);
869 COPY(attribute_count);
870 COPY(varying_count);
871 COPY(midgard1.uniform_count);
872 COPY(midgard1.work_count);
873 COPY(midgard1.unknown2);
874
875 #undef COPY
876 /* If there is a blend shader, work registers are shared */
877
878 if (ctx->blend->has_blend_shader)
879 ctx->fragment_shader_core.midgard1.work_count = /*MAX2(ctx->fragment_shader_core.midgard1.work_count, ctx->blend->blend_work_count)*/16;
880
881 /* Set late due to depending on render state */
882 /* The one at the end seems to mean "1 UBO" */
883 ctx->fragment_shader_core.midgard1.unknown1 = MALI_NO_ALPHA_TO_COVERAGE | 0x200 | 0x2201;
884
885 /* Assign texture/sample count right before upload */
886 ctx->fragment_shader_core.texture_count = ctx->sampler_view_count[PIPE_SHADER_FRAGMENT];
887 ctx->fragment_shader_core.sampler_count = ctx->sampler_count[PIPE_SHADER_FRAGMENT];
888
889 /* Assign the stencil refs late */
890 ctx->fragment_shader_core.stencil_front.ref = ctx->stencil_ref.ref_value[0];
891 ctx->fragment_shader_core.stencil_back.ref = ctx->stencil_ref.ref_value[1];
892
893 /* CAN_DISCARD should be set if the fragment shader possibly
894 * contains a 'discard' instruction. It is likely this is
895 * related to optimizations related to forward-pixel kill, as
896 * per "Mali Performance 3: Is EGL_BUFFER_PRESERVED a good
897 * thing?" by Peter Harris
898 */
899
900 if (variant->can_discard) {
901 ctx->fragment_shader_core.unknown2_3 |= MALI_CAN_DISCARD;
902 ctx->fragment_shader_core.midgard1.unknown1 &= ~MALI_NO_ALPHA_TO_COVERAGE;
903 ctx->fragment_shader_core.midgard1.unknown1 |= 0x4000;
904 ctx->fragment_shader_core.midgard1.unknown1 = 0x4200;
905 }
906
907 /* Check if we're using the default blend descriptor (fast path) */
908
909 bool no_blending =
910 !ctx->blend->has_blend_shader &&
911 (ctx->blend->equation.rgb_mode == 0x122) &&
912 (ctx->blend->equation.alpha_mode == 0x122) &&
913 (ctx->blend->equation.color_mask == 0xf);
914
915 if (ctx->require_sfbd) {
916 /* When only a single render target platform is used, the blend
917 * information is inside the shader meta itself. We
918 * additionally need to signal CAN_DISCARD for nontrivial blend
919 * modes (so we're able to read back the destination buffer) */
920
921 if (ctx->blend->has_blend_shader) {
922 ctx->fragment_shader_core.blend_shader = ctx->blend->blend_shader;
923 } else {
924 memcpy(&ctx->fragment_shader_core.blend_equation, &ctx->blend->equation, sizeof(ctx->blend->equation));
925 }
926
927 if (!no_blending) {
928 ctx->fragment_shader_core.unknown2_3 |= MALI_CAN_DISCARD;
929 }
930 }
931
932 size_t size = sizeof(struct mali_shader_meta) + sizeof(struct mali_blend_meta);
933 struct panfrost_transfer transfer = panfrost_allocate_transient(ctx, size);
934 memcpy(transfer.cpu, &ctx->fragment_shader_core, sizeof(struct mali_shader_meta));
935
936 ctx->payload_tiler.postfix._shader_upper = (transfer.gpu) >> 4;
937
938 if (!ctx->require_sfbd) {
939 /* Additional blend descriptor tacked on for jobs using MFBD */
940
941 unsigned blend_count = 0;
942
943 if (ctx->blend->has_blend_shader) {
944 /* For a blend shader, the bottom nibble corresponds to
945 * the number of work registers used, which signals the
946 * -existence- of a blend shader */
947
948 assert(ctx->blend->blend_work_count >= 2);
949 blend_count |= MIN2(ctx->blend->blend_work_count, 3);
950 } else {
951 /* Otherwise, the bottom bit simply specifies if
952 * blending (anything other than REPLACE) is enabled */
953
954
955 if (!no_blending)
956 blend_count |= 0x1;
957 }
958
959 /* Second blend equation is always a simple replace */
960
961 uint64_t replace_magic = 0xf0122122;
962 struct mali_blend_equation replace_mode;
963 memcpy(&replace_mode, &replace_magic, sizeof(replace_mode));
964
965 struct mali_blend_meta blend_meta[] = {
966 {
967 .unk1 = 0x200 | blend_count,
968 .blend_equation_1 = ctx->blend->equation,
969 .blend_equation_2 = replace_mode
970 },
971 };
972
973 if (ctx->blend->has_blend_shader)
974 memcpy(&blend_meta[0].blend_equation_1, &ctx->blend->blend_shader, sizeof(ctx->blend->blend_shader));
975
976 memcpy(transfer.cpu + sizeof(struct mali_shader_meta), blend_meta, sizeof(blend_meta));
977 }
978 }
979
980 if (ctx->dirty & PAN_DIRTY_VERTEX) {
981 ctx->payload_vertex.postfix.attribute_meta = ctx->vertex->descriptor_ptr;
982 }
983
984 if (ctx->dirty & PAN_DIRTY_SAMPLERS) {
985 /* Upload samplers back to back, no padding */
986
987 for (int t = 0; t <= PIPE_SHADER_FRAGMENT; ++t) {
988 if (!ctx->sampler_count[t]) continue;
989
990 struct panfrost_transfer transfer = panfrost_allocate_transient(ctx, sizeof(struct mali_sampler_descriptor) * ctx->sampler_count[t]);
991 struct mali_sampler_descriptor *desc = (struct mali_sampler_descriptor *) transfer.cpu;
992
993 for (int i = 0; i < ctx->sampler_count[t]; ++i) {
994 desc[i] = ctx->samplers[t][i]->hw;
995 }
996
997 if (t == PIPE_SHADER_FRAGMENT)
998 ctx->payload_tiler.postfix.sampler_descriptor = transfer.gpu;
999 else if (t == PIPE_SHADER_VERTEX)
1000 ctx->payload_vertex.postfix.sampler_descriptor = transfer.gpu;
1001 else
1002 assert(0);
1003 }
1004 }
1005
1006 if (ctx->dirty & PAN_DIRTY_TEXTURES) {
1007 for (int t = 0; t <= PIPE_SHADER_FRAGMENT; ++t) {
1008 /* Shortcircuit */
1009 if (!ctx->sampler_view_count[t]) continue;
1010
1011 uint64_t trampolines[PIPE_MAX_SHADER_SAMPLER_VIEWS];
1012
1013 for (int i = 0; i < ctx->sampler_view_count[t]; ++i) {
1014 if (!ctx->sampler_views[t][i])
1015 continue;
1016
1017 struct pipe_resource *tex_rsrc = ctx->sampler_views[t][i]->base.texture;
1018 struct panfrost_resource *rsrc = (struct panfrost_resource *) tex_rsrc;
1019
1020 /* Inject the address in. */
1021 for (int l = 0; l < (tex_rsrc->last_level + 1); ++l)
1022 ctx->sampler_views[t][i]->hw.swizzled_bitmaps[l] = rsrc->bo->gpu[l];
1023
1024 /* Workaround maybe-errata (?) with non-mipmaps */
1025 int s = ctx->sampler_views[t][i]->hw.nr_mipmap_levels;
1026
1027 if (!rsrc->bo->is_mipmap) {
1028 if (ctx->is_t6xx) {
1029 /* HW ERRATA, not needed after t6XX */
1030 ctx->sampler_views[t][i]->hw.swizzled_bitmaps[1] = rsrc->bo->gpu[0];
1031
1032 ctx->sampler_views[t][i]->hw.unknown3A = 1;
1033 }
1034
1035 ctx->sampler_views[t][i]->hw.nr_mipmap_levels = 0;
1036 }
1037
1038 trampolines[i] = panfrost_upload_transient(ctx, &ctx->sampler_views[t][i]->hw, sizeof(struct mali_texture_descriptor));
1039
1040 /* Restore */
1041 ctx->sampler_views[t][i]->hw.nr_mipmap_levels = s;
1042
1043 if (ctx->is_t6xx) {
1044 ctx->sampler_views[t][i]->hw.unknown3A = 0;
1045 }
1046 }
1047
1048 mali_ptr trampoline = panfrost_upload_transient(ctx, trampolines, sizeof(uint64_t) * ctx->sampler_view_count[t]);
1049
1050 if (t == PIPE_SHADER_FRAGMENT)
1051 ctx->payload_tiler.postfix.texture_trampoline = trampoline;
1052 else if (t == PIPE_SHADER_VERTEX)
1053 ctx->payload_vertex.postfix.texture_trampoline = trampoline;
1054 else
1055 assert(0);
1056 }
1057 }
1058
1059 /* Generate the viewport vector of the form: <width/2, height/2, centerx, centery> */
1060 const struct pipe_viewport_state *vp = &ctx->pipe_viewport;
1061
1062 /* For flipped-Y buffers (signaled by negative scale), the translate is
1063 * flipped as well */
1064
1065 bool invert_y = vp->scale[1] < 0.0;
1066 float translate_y = vp->translate[1];
1067
1068 if (invert_y)
1069 translate_y = ctx->pipe_framebuffer.height - translate_y;
1070
1071 float viewport_vec4[] = {
1072 vp->scale[0],
1073 fabsf(vp->scale[1]),
1074
1075 vp->translate[0],
1076 translate_y
1077 };
1078
1079 for (int i = 0; i < PIPE_SHADER_TYPES; ++i) {
1080 struct panfrost_constant_buffer *buf = &ctx->constant_buffer[i];
1081
1082 if (i == PIPE_SHADER_VERTEX || i == PIPE_SHADER_FRAGMENT) {
1083 /* It doesn't matter if we don't use all the memory;
1084 * we'd need a dummy UBO anyway. Compute the max */
1085
1086 size_t size = sizeof(viewport_vec4) + buf->size;
1087 struct panfrost_transfer transfer = panfrost_allocate_transient(ctx, size);
1088
1089 /* Keep track how much we've uploaded */
1090 off_t offset = 0;
1091
1092 if (i == PIPE_SHADER_VERTEX) {
1093 /* Upload viewport */
1094 memcpy(transfer.cpu + offset, viewport_vec4, sizeof(viewport_vec4));
1095 offset += sizeof(viewport_vec4);
1096 }
1097
1098 /* Upload uniforms */
1099 memcpy(transfer.cpu + offset, buf->buffer, buf->size);
1100
1101 int uniform_count = 0;
1102
1103 struct mali_vertex_tiler_postfix *postfix;
1104
1105 switch (i) {
1106 case PIPE_SHADER_VERTEX:
1107 uniform_count = ctx->vs->variants[ctx->vs->active_variant].uniform_count;
1108 postfix = &ctx->payload_vertex.postfix;
1109 break;
1110
1111 case PIPE_SHADER_FRAGMENT:
1112 uniform_count = ctx->fs->variants[ctx->fs->active_variant].uniform_count;
1113 postfix = &ctx->payload_tiler.postfix;
1114 break;
1115
1116 default:
1117 DBG("Unknown shader stage %d in uniform upload\n", i);
1118 assert(0);
1119 }
1120
1121 /* Also attach the same buffer as a UBO for extended access */
1122
1123 struct mali_uniform_buffer_meta uniform_buffers[] = {
1124 {
1125 .size = MALI_POSITIVE((2 + uniform_count)),
1126 .ptr = transfer.gpu >> 2,
1127 },
1128 };
1129
1130 mali_ptr ubufs = panfrost_upload_transient(ctx, uniform_buffers, sizeof(uniform_buffers));
1131 postfix->uniforms = transfer.gpu;
1132 postfix->uniform_buffers = ubufs;
1133
1134 buf->dirty = 0;
1135 }
1136 }
1137
1138 /* TODO: Upload the viewport somewhere more appropriate */
1139
1140 /* Clip bounds are encoded as floats. The viewport itself is encoded as
1141 * (somewhat) asymmetric ints. */
1142 const struct pipe_scissor_state *ss = &ctx->scissor;
1143
1144 struct mali_viewport view = {
1145 /* By default, do no viewport clipping, i.e. clip to (-inf,
1146 * inf) in each direction. Clipping to the viewport in theory
1147 * should work, but in practice causes issues when we're not
1148 * explicitly trying to scissor */
1149
1150 .clip_minx = -inff,
1151 .clip_miny = -inff,
1152 .clip_maxx = inff,
1153 .clip_maxy = inff,
1154
1155 .clip_minz = 0.0,
1156 .clip_maxz = 1.0,
1157 };
1158
1159 /* Always scissor to the viewport by default. */
1160 view.viewport0[0] = (int) (vp->translate[0] - vp->scale[0]);
1161 view.viewport1[0] = MALI_POSITIVE((int) (vp->translate[0] + vp->scale[0]));
1162
1163 view.viewport0[1] = (int) (translate_y - fabs(vp->scale[1]));
1164 view.viewport1[1] = MALI_POSITIVE((int) (translate_y + fabs(vp->scale[1])));
1165
1166 if (ss && ctx->rasterizer && ctx->rasterizer->base.scissor) {
1167 /* Invert scissor if needed */
1168 unsigned miny = invert_y ?
1169 ctx->pipe_framebuffer.height - ss->maxy : ss->miny;
1170
1171 unsigned maxy = invert_y ?
1172 ctx->pipe_framebuffer.height - ss->miny : ss->maxy;
1173
1174 /* Set the actual scissor */
1175 view.viewport0[0] = ss->minx;
1176 view.viewport0[1] = miny;
1177 view.viewport1[0] = MALI_POSITIVE(ss->maxx);
1178 view.viewport1[1] = MALI_POSITIVE(maxy);
1179 }
1180
1181 ctx->payload_tiler.postfix.viewport =
1182 panfrost_upload_transient(ctx,
1183 &view,
1184 sizeof(struct mali_viewport));
1185
1186 ctx->dirty = 0;
1187 }
1188
1189 /* Corresponds to exactly one draw, but does not submit anything */
1190
1191 static void
1192 panfrost_queue_draw(struct panfrost_context *ctx)
1193 {
1194 /* TODO: Expand the array? */
1195 if (ctx->draw_count >= MAX_DRAW_CALLS) {
1196 DBG("Job buffer overflow, ignoring draw\n");
1197 assert(0);
1198 }
1199
1200 /* Handle dirty flags now */
1201 panfrost_emit_for_draw(ctx, true);
1202
1203 struct panfrost_transfer vertex = panfrost_vertex_tiler_job(ctx, false, false);
1204 struct panfrost_transfer tiler = panfrost_vertex_tiler_job(ctx, true, false);
1205
1206 ctx->u_vertex_jobs[ctx->vertex_job_count] = (struct mali_job_descriptor_header *) vertex.cpu;
1207 ctx->vertex_jobs[ctx->vertex_job_count++] = vertex.gpu;
1208
1209 ctx->u_tiler_jobs[ctx->tiler_job_count] = (struct mali_job_descriptor_header *) tiler.cpu;
1210 ctx->tiler_jobs[ctx->tiler_job_count++] = tiler.gpu;
1211
1212 ctx->draw_count++;
1213 }
1214
1215 /* At the end of the frame, the vertex and tiler jobs are linked together and
1216 * then the fragment job is plonked at the end. Set value job is first for
1217 * unknown reasons. */
1218
1219 static void
1220 panfrost_link_job_pair(struct mali_job_descriptor_header *first, mali_ptr next)
1221 {
1222 if (first->job_descriptor_size)
1223 first->next_job_64 = (u64) (uintptr_t) next;
1224 else
1225 first->next_job_32 = (u32) (uintptr_t) next;
1226 }
1227
1228 static void
1229 panfrost_link_jobs(struct panfrost_context *ctx)
1230 {
1231 if (ctx->draw_count) {
1232 /* Generate the set_value_job */
1233 panfrost_set_value_job(ctx);
1234
1235 /* Have the first vertex job depend on the set value job */
1236 ctx->u_vertex_jobs[0]->job_dependency_index_1 = ctx->u_set_value_job->job_index;
1237
1238 /* SV -> V */
1239 panfrost_link_job_pair(ctx->u_set_value_job, ctx->vertex_jobs[0]);
1240 }
1241
1242 /* V -> V/T ; T -> T/null */
1243 for (int i = 0; i < ctx->vertex_job_count; ++i) {
1244 bool isLast = (i + 1) == ctx->vertex_job_count;
1245
1246 panfrost_link_job_pair(ctx->u_vertex_jobs[i], isLast ? ctx->tiler_jobs[0] : ctx->vertex_jobs[i + 1]);
1247 }
1248
1249 /* T -> T/null */
1250 for (int i = 0; i < ctx->tiler_job_count; ++i) {
1251 bool isLast = (i + 1) == ctx->tiler_job_count;
1252 panfrost_link_job_pair(ctx->u_tiler_jobs[i], isLast ? 0 : ctx->tiler_jobs[i + 1]);
1253 }
1254 }
1255
1256 /* The entire frame is in memory -- send it off to the kernel! */
1257
1258 static void
1259 panfrost_submit_frame(struct panfrost_context *ctx, bool flush_immediate,
1260 struct pipe_fence_handle **fence)
1261 {
1262 struct pipe_context *gallium = (struct pipe_context *) ctx;
1263 struct panfrost_screen *screen = pan_screen(gallium->screen);
1264
1265 /* Edge case if screen is cleared and nothing else */
1266 bool has_draws = ctx->draw_count > 0;
1267
1268 /* Workaround a bizarre lockup (a hardware errata?) */
1269 if (!has_draws)
1270 flush_immediate = true;
1271
1272 /* A number of jobs are batched -- this must be linked and cleared */
1273 panfrost_link_jobs(ctx);
1274
1275 ctx->draw_count = 0;
1276 ctx->vertex_job_count = 0;
1277 ctx->tiler_job_count = 0;
1278
1279 #ifndef DRY_RUN
1280
1281 bool is_scanout = panfrost_is_scanout(ctx);
1282 int fragment_id = screen->driver->submit_vs_fs_job(ctx, has_draws, is_scanout);
1283
1284 /* If visual, we can stall a frame */
1285
1286 if (!flush_immediate)
1287 screen->driver->force_flush_fragment(ctx, fence);
1288
1289 screen->last_fragment_id = fragment_id;
1290 screen->last_fragment_flushed = false;
1291
1292 /* If readback, flush now (hurts the pipelined performance) */
1293 if (flush_immediate)
1294 screen->driver->force_flush_fragment(ctx, fence);
1295
1296 if (screen->driver->dump_counters && pan_counters_base) {
1297 screen->driver->dump_counters(screen);
1298
1299 char filename[128];
1300 snprintf(filename, sizeof(filename), "%s/frame%d.mdgprf", pan_counters_base, ++performance_counter_number);
1301 FILE *fp = fopen(filename, "wb");
1302 fwrite(screen->perf_counters.cpu, 4096, sizeof(uint32_t), fp);
1303 fclose(fp);
1304 }
1305
1306 #endif
1307 }
1308
1309 void
1310 panfrost_flush(
1311 struct pipe_context *pipe,
1312 struct pipe_fence_handle **fence,
1313 unsigned flags)
1314 {
1315 struct panfrost_context *ctx = pan_context(pipe);
1316 struct panfrost_job *job = panfrost_get_job_for_fbo(ctx);
1317
1318 /* Nothing to do! */
1319 if (!ctx->draw_count && !job->clear) return;
1320
1321 /* Whether to stall the pipeline for immediately correct results */
1322 bool flush_immediate = flags & PIPE_FLUSH_END_OF_FRAME;
1323
1324 /* Submit the frame itself */
1325 panfrost_submit_frame(ctx, flush_immediate, fence);
1326
1327 /* Prepare for the next frame */
1328 panfrost_invalidate_frame(ctx);
1329 }
1330
1331 #define DEFINE_CASE(c) case PIPE_PRIM_##c: return MALI_##c;
1332
1333 static int
1334 g2m_draw_mode(enum pipe_prim_type mode)
1335 {
1336 switch (mode) {
1337 DEFINE_CASE(POINTS);
1338 DEFINE_CASE(LINES);
1339 DEFINE_CASE(LINE_LOOP);
1340 DEFINE_CASE(LINE_STRIP);
1341 DEFINE_CASE(TRIANGLES);
1342 DEFINE_CASE(TRIANGLE_STRIP);
1343 DEFINE_CASE(TRIANGLE_FAN);
1344 DEFINE_CASE(QUADS);
1345 DEFINE_CASE(QUAD_STRIP);
1346 DEFINE_CASE(POLYGON);
1347
1348 default:
1349 DBG("Illegal draw mode %d\n", mode);
1350 assert(0);
1351 return MALI_LINE_LOOP;
1352 }
1353 }
1354
1355 #undef DEFINE_CASE
1356
1357 static unsigned
1358 panfrost_translate_index_size(unsigned size)
1359 {
1360 switch (size) {
1361 case 1:
1362 return MALI_DRAW_INDEXED_UINT8;
1363
1364 case 2:
1365 return MALI_DRAW_INDEXED_UINT16;
1366
1367 case 4:
1368 return MALI_DRAW_INDEXED_UINT32;
1369
1370 default:
1371 DBG("Unknown index size %d\n", size);
1372 assert(0);
1373 return 0;
1374 }
1375 }
1376
1377 static const uint8_t *
1378 panfrost_get_index_buffer_raw(const struct pipe_draw_info *info)
1379 {
1380 if (info->has_user_indices) {
1381 return (const uint8_t *) info->index.user;
1382 } else {
1383 struct panfrost_resource *rsrc = (struct panfrost_resource *) (info->index.resource);
1384 return (const uint8_t *) rsrc->bo->cpu[0];
1385 }
1386 }
1387
1388 /* Gets a GPU address for the associated index buffer. Only gauranteed to be
1389 * good for the duration of the draw (transient), could last longer */
1390
1391 static mali_ptr
1392 panfrost_get_index_buffer_mapped(struct panfrost_context *ctx, const struct pipe_draw_info *info)
1393 {
1394 struct panfrost_resource *rsrc = (struct panfrost_resource *) (info->index.resource);
1395
1396 off_t offset = info->start * info->index_size;
1397
1398 if (!info->has_user_indices) {
1399 /* Only resources can be directly mapped */
1400 return rsrc->bo->gpu[0] + offset;
1401 } else {
1402 /* Otherwise, we need to upload to transient memory */
1403 const uint8_t *ibuf8 = panfrost_get_index_buffer_raw(info);
1404 return panfrost_upload_transient(ctx, ibuf8 + offset, info->count * info->index_size);
1405 }
1406 }
1407
1408 #define CALCULATE_MIN_MAX_INDEX(T, buffer, start, count) \
1409 for (unsigned _idx = (start); _idx < (start + count); ++_idx) { \
1410 T idx = buffer[_idx]; \
1411 if (idx > max_index) max_index = idx; \
1412 if (idx < min_index) min_index = idx; \
1413 }
1414
1415 static void
1416 panfrost_draw_vbo(
1417 struct pipe_context *pipe,
1418 const struct pipe_draw_info *info)
1419 {
1420 struct panfrost_context *ctx = pan_context(pipe);
1421
1422 ctx->payload_vertex.draw_start = info->start;
1423 ctx->payload_tiler.draw_start = info->start;
1424
1425 int mode = info->mode;
1426
1427 /* Fallback for unsupported modes */
1428
1429 if (!(ctx->draw_modes & (1 << mode))) {
1430 if (mode == PIPE_PRIM_QUADS && info->count == 4 && ctx->rasterizer && !ctx->rasterizer->base.flatshade) {
1431 mode = PIPE_PRIM_TRIANGLE_FAN;
1432 } else {
1433 if (info->count < 4) {
1434 /* Degenerate case? */
1435 return;
1436 }
1437
1438 util_primconvert_save_rasterizer_state(ctx->primconvert, &ctx->rasterizer->base);
1439 util_primconvert_draw_vbo(ctx->primconvert, info);
1440 return;
1441 }
1442 }
1443
1444 /* Now that we have a guaranteed terminating path, find the job.
1445 * Assignment commented out to prevent unused warning */
1446
1447 /* struct panfrost_job *job = */ panfrost_get_job_for_fbo(ctx);
1448
1449 ctx->payload_tiler.prefix.draw_mode = g2m_draw_mode(mode);
1450
1451 ctx->vertex_count = info->count;
1452
1453 /* For non-indexed draws, they're the same */
1454 unsigned invocation_count = ctx->vertex_count;
1455
1456 /* For higher amounts of vertices (greater than what fits in a 16-bit
1457 * short), the other value is needed, otherwise there will be bizarre
1458 * rendering artefacts. It's not clear what these values mean yet. */
1459
1460 ctx->payload_tiler.prefix.unknown_draw &= ~(0x3000 | 0x18000);
1461 ctx->payload_tiler.prefix.unknown_draw |= (mode == PIPE_PRIM_POINTS || ctx->vertex_count > 65535) ? 0x3000 : 0x18000;
1462
1463 if (info->index_size) {
1464 /* Calculate the min/max index used so we can figure out how
1465 * many times to invoke the vertex shader */
1466
1467 const uint8_t *ibuf8 = panfrost_get_index_buffer_raw(info);
1468
1469 int min_index = INT_MAX;
1470 int max_index = 0;
1471
1472 if (info->index_size == 1) {
1473 CALCULATE_MIN_MAX_INDEX(uint8_t, ibuf8, info->start, info->count);
1474 } else if (info->index_size == 2) {
1475 const uint16_t *ibuf16 = (const uint16_t *) ibuf8;
1476 CALCULATE_MIN_MAX_INDEX(uint16_t, ibuf16, info->start, info->count);
1477 } else if (info->index_size == 4) {
1478 const uint32_t *ibuf32 = (const uint32_t *) ibuf8;
1479 CALCULATE_MIN_MAX_INDEX(uint32_t, ibuf32, info->start, info->count);
1480 } else {
1481 assert(0);
1482 }
1483
1484 /* Make sure we didn't go crazy */
1485 assert(min_index < INT_MAX);
1486 assert(max_index > 0);
1487 assert(max_index > min_index);
1488
1489 /* Use the corresponding values */
1490 invocation_count = max_index - min_index + 1;
1491 ctx->payload_vertex.draw_start = min_index;
1492 ctx->payload_tiler.draw_start = min_index;
1493
1494 ctx->payload_tiler.prefix.negative_start = -min_index;
1495 ctx->payload_tiler.prefix.index_count = MALI_POSITIVE(info->count);
1496
1497 //assert(!info->restart_index); /* TODO: Research */
1498 assert(!info->index_bias);
1499 //assert(!info->min_index); /* TODO: Use value */
1500
1501 ctx->payload_tiler.prefix.unknown_draw |= panfrost_translate_index_size(info->index_size);
1502 ctx->payload_tiler.prefix.indices = panfrost_get_index_buffer_mapped(ctx, info);
1503 } else {
1504 /* Index count == vertex count, if no indexing is applied, as
1505 * if it is internally indexed in the expected order */
1506
1507 ctx->payload_tiler.prefix.negative_start = 0;
1508 ctx->payload_tiler.prefix.index_count = MALI_POSITIVE(ctx->vertex_count);
1509
1510 /* Reverse index state */
1511 ctx->payload_tiler.prefix.unknown_draw &= ~MALI_DRAW_INDEXED_UINT32;
1512 ctx->payload_tiler.prefix.indices = (uintptr_t) NULL;
1513 }
1514
1515 ctx->payload_vertex.prefix.invocation_count = MALI_POSITIVE(invocation_count);
1516 ctx->payload_tiler.prefix.invocation_count = MALI_POSITIVE(invocation_count);
1517
1518 /* Fire off the draw itself */
1519 panfrost_queue_draw(ctx);
1520 }
1521
1522 /* CSO state */
1523
1524 static void
1525 panfrost_generic_cso_delete(struct pipe_context *pctx, void *hwcso)
1526 {
1527 free(hwcso);
1528 }
1529
1530 static void *
1531 panfrost_create_rasterizer_state(
1532 struct pipe_context *pctx,
1533 const struct pipe_rasterizer_state *cso)
1534 {
1535 struct panfrost_context *ctx = pan_context(pctx);
1536 struct panfrost_rasterizer *so = CALLOC_STRUCT(panfrost_rasterizer);
1537
1538 so->base = *cso;
1539
1540 /* Bitmask, unknown meaning of the start value */
1541 so->tiler_gl_enables = ctx->is_t6xx ? 0x105 : 0x7;
1542
1543 so->tiler_gl_enables |= MALI_FRONT_FACE(
1544 cso->front_ccw ? MALI_CCW : MALI_CW);
1545
1546 if (cso->cull_face & PIPE_FACE_FRONT)
1547 so->tiler_gl_enables |= MALI_CULL_FACE_FRONT;
1548
1549 if (cso->cull_face & PIPE_FACE_BACK)
1550 so->tiler_gl_enables |= MALI_CULL_FACE_BACK;
1551
1552 return so;
1553 }
1554
1555 static void
1556 panfrost_bind_rasterizer_state(
1557 struct pipe_context *pctx,
1558 void *hwcso)
1559 {
1560 struct panfrost_context *ctx = pan_context(pctx);
1561
1562 /* TODO: Why can't rasterizer be NULL ever? Other drivers are fine.. */
1563 if (!hwcso)
1564 return;
1565
1566 ctx->rasterizer = hwcso;
1567 ctx->dirty |= PAN_DIRTY_RASTERIZER;
1568 }
1569
1570 static void *
1571 panfrost_create_vertex_elements_state(
1572 struct pipe_context *pctx,
1573 unsigned num_elements,
1574 const struct pipe_vertex_element *elements)
1575 {
1576 struct panfrost_context *ctx = pan_context(pctx);
1577 struct panfrost_vertex_state *so = CALLOC_STRUCT(panfrost_vertex_state);
1578
1579 so->num_elements = num_elements;
1580 memcpy(so->pipe, elements, sizeof(*elements) * num_elements);
1581
1582 struct panfrost_transfer transfer = panfrost_allocate_chunk(ctx, sizeof(struct mali_attr_meta) * num_elements, HEAP_DESCRIPTOR);
1583 so->hw = (struct mali_attr_meta *) transfer.cpu;
1584 so->descriptor_ptr = transfer.gpu;
1585
1586 /* Allocate memory for the descriptor state */
1587
1588 for (int i = 0; i < num_elements; ++i) {
1589 so->hw[i].index = elements[i].vertex_buffer_index;
1590
1591 enum pipe_format fmt = elements[i].src_format;
1592 const struct util_format_description *desc = util_format_description(fmt);
1593 so->hw[i].unknown1 = 0x2;
1594 so->hw[i].swizzle = panfrost_get_default_swizzle(desc->nr_channels);
1595
1596 so->hw[i].format = panfrost_find_format(desc);
1597
1598 /* The field itself should probably be shifted over */
1599 so->hw[i].src_offset = elements[i].src_offset;
1600 }
1601
1602 return so;
1603 }
1604
1605 static void
1606 panfrost_bind_vertex_elements_state(
1607 struct pipe_context *pctx,
1608 void *hwcso)
1609 {
1610 struct panfrost_context *ctx = pan_context(pctx);
1611
1612 ctx->vertex = hwcso;
1613 ctx->dirty |= PAN_DIRTY_VERTEX;
1614 }
1615
1616 static void
1617 panfrost_delete_vertex_elements_state(struct pipe_context *pctx, void *hwcso)
1618 {
1619 struct panfrost_vertex_state *so = (struct panfrost_vertex_state *) hwcso;
1620 unsigned bytes = sizeof(struct mali_attr_meta) * so->num_elements;
1621 DBG("Vertex elements delete leaks descriptor (%d bytes)\n", bytes);
1622 free(hwcso);
1623 }
1624
1625 static void *
1626 panfrost_create_shader_state(
1627 struct pipe_context *pctx,
1628 const struct pipe_shader_state *cso)
1629 {
1630 struct panfrost_shader_variants *so = CALLOC_STRUCT(panfrost_shader_variants);
1631 so->base = *cso;
1632
1633 /* Token deep copy to prevent memory corruption */
1634
1635 if (cso->type == PIPE_SHADER_IR_TGSI)
1636 so->base.tokens = tgsi_dup_tokens(so->base.tokens);
1637
1638 return so;
1639 }
1640
1641 static void
1642 panfrost_delete_shader_state(
1643 struct pipe_context *pctx,
1644 void *so)
1645 {
1646 struct panfrost_shader_variants *cso = (struct panfrost_shader_variants *) so;
1647
1648 if (cso->base.type == PIPE_SHADER_IR_TGSI) {
1649 DBG("Deleting TGSI shader leaks duplicated tokens\n");
1650 }
1651
1652 unsigned leak = cso->variant_count * sizeof(struct mali_shader_meta);
1653 DBG("Deleting shader state leaks descriptors (%d bytes), and shader bytecode\n", leak);
1654
1655 free(so);
1656 }
1657
1658 static void *
1659 panfrost_create_sampler_state(
1660 struct pipe_context *pctx,
1661 const struct pipe_sampler_state *cso)
1662 {
1663 struct panfrost_sampler_state *so = CALLOC_STRUCT(panfrost_sampler_state);
1664 so->base = *cso;
1665
1666 /* sampler_state corresponds to mali_sampler_descriptor, which we can generate entirely here */
1667
1668 struct mali_sampler_descriptor sampler_descriptor = {
1669 .filter_mode = MALI_TEX_MIN(translate_tex_filter(cso->min_img_filter))
1670 | MALI_TEX_MAG(translate_tex_filter(cso->mag_img_filter))
1671 | translate_mip_filter(cso->min_mip_filter)
1672 | 0x20,
1673
1674 .wrap_s = translate_tex_wrap(cso->wrap_s),
1675 .wrap_t = translate_tex_wrap(cso->wrap_t),
1676 .wrap_r = translate_tex_wrap(cso->wrap_r),
1677 .compare_func = panfrost_translate_alt_compare_func(cso->compare_func),
1678 .border_color = {
1679 cso->border_color.f[0],
1680 cso->border_color.f[1],
1681 cso->border_color.f[2],
1682 cso->border_color.f[3]
1683 },
1684 .min_lod = FIXED_16(0.0),
1685 .max_lod = FIXED_16(31.0),
1686 .unknown2 = 1,
1687 };
1688
1689 so->hw = sampler_descriptor;
1690
1691 return so;
1692 }
1693
1694 static void
1695 panfrost_bind_sampler_states(
1696 struct pipe_context *pctx,
1697 enum pipe_shader_type shader,
1698 unsigned start_slot, unsigned num_sampler,
1699 void **sampler)
1700 {
1701 assert(start_slot == 0);
1702
1703 struct panfrost_context *ctx = pan_context(pctx);
1704
1705 /* XXX: Should upload, not just copy? */
1706 ctx->sampler_count[shader] = num_sampler;
1707 memcpy(ctx->samplers[shader], sampler, num_sampler * sizeof (void *));
1708
1709 ctx->dirty |= PAN_DIRTY_SAMPLERS;
1710 }
1711
1712 static bool
1713 panfrost_variant_matches(struct panfrost_context *ctx, struct panfrost_shader_state *variant)
1714 {
1715 struct pipe_alpha_state *alpha = &ctx->depth_stencil->alpha;
1716
1717 if (alpha->enabled || variant->alpha_state.enabled) {
1718 /* Make sure enable state is at least the same */
1719 if (alpha->enabled != variant->alpha_state.enabled) {
1720 return false;
1721 }
1722
1723 /* Check that the contents of the test are the same */
1724 bool same_func = alpha->func == variant->alpha_state.func;
1725 bool same_ref = alpha->ref_value == variant->alpha_state.ref_value;
1726
1727 if (!(same_func && same_ref)) {
1728 return false;
1729 }
1730 }
1731 /* Otherwise, we're good to go */
1732 return true;
1733 }
1734
1735 static void
1736 panfrost_bind_fs_state(
1737 struct pipe_context *pctx,
1738 void *hwcso)
1739 {
1740 struct panfrost_context *ctx = pan_context(pctx);
1741
1742 ctx->fs = hwcso;
1743
1744 if (hwcso) {
1745 /* Match the appropriate variant */
1746
1747 signed variant = -1;
1748
1749 struct panfrost_shader_variants *variants = (struct panfrost_shader_variants *) hwcso;
1750
1751 for (unsigned i = 0; i < variants->variant_count; ++i) {
1752 if (panfrost_variant_matches(ctx, &variants->variants[i])) {
1753 variant = i;
1754 break;
1755 }
1756 }
1757
1758 if (variant == -1) {
1759 /* No variant matched, so create a new one */
1760 variant = variants->variant_count++;
1761 assert(variants->variant_count < MAX_SHADER_VARIANTS);
1762
1763 variants->variants[variant].base = hwcso;
1764 variants->variants[variant].alpha_state = ctx->depth_stencil->alpha;
1765
1766 /* Allocate the mapped descriptor ahead-of-time. TODO: Use for FS as well as VS */
1767 struct panfrost_context *ctx = pan_context(pctx);
1768 struct panfrost_transfer transfer = panfrost_allocate_chunk(ctx, sizeof(struct mali_shader_meta), HEAP_DESCRIPTOR);
1769
1770 variants->variants[variant].tripipe = (struct mali_shader_meta *) transfer.cpu;
1771 variants->variants[variant].tripipe_gpu = transfer.gpu;
1772
1773 }
1774
1775 /* Select this variant */
1776 variants->active_variant = variant;
1777
1778 struct panfrost_shader_state *shader_state = &variants->variants[variant];
1779 assert(panfrost_variant_matches(ctx, shader_state));
1780
1781 /* Now we have a variant selected, so compile and go */
1782
1783 if (!shader_state->compiled) {
1784 panfrost_shader_compile(ctx, shader_state->tripipe, NULL, JOB_TYPE_TILER, shader_state);
1785 shader_state->compiled = true;
1786 }
1787 }
1788
1789 ctx->dirty |= PAN_DIRTY_FS;
1790 }
1791
1792 static void
1793 panfrost_bind_vs_state(
1794 struct pipe_context *pctx,
1795 void *hwcso)
1796 {
1797 struct panfrost_context *ctx = pan_context(pctx);
1798
1799 ctx->vs = hwcso;
1800
1801 if (hwcso) {
1802 if (!ctx->vs->variants[0].compiled) {
1803 ctx->vs->variants[0].base = hwcso;
1804
1805 /* TODO DRY from above */
1806 struct panfrost_transfer transfer = panfrost_allocate_chunk(ctx, sizeof(struct mali_shader_meta), HEAP_DESCRIPTOR);
1807 ctx->vs->variants[0].tripipe = (struct mali_shader_meta *) transfer.cpu;
1808 ctx->vs->variants[0].tripipe_gpu = transfer.gpu;
1809
1810 panfrost_shader_compile(ctx, ctx->vs->variants[0].tripipe, NULL, JOB_TYPE_VERTEX, &ctx->vs->variants[0]);
1811 ctx->vs->variants[0].compiled = true;
1812 }
1813 }
1814
1815 ctx->dirty |= PAN_DIRTY_VS;
1816 }
1817
1818 static void
1819 panfrost_set_vertex_buffers(
1820 struct pipe_context *pctx,
1821 unsigned start_slot,
1822 unsigned num_buffers,
1823 const struct pipe_vertex_buffer *buffers)
1824 {
1825 struct panfrost_context *ctx = pan_context(pctx);
1826 assert(num_buffers <= PIPE_MAX_ATTRIBS);
1827
1828 /* XXX: Dirty tracking? etc */
1829 if (buffers) {
1830 size_t sz = sizeof(buffers[0]) * num_buffers;
1831 ctx->vertex_buffers = malloc(sz);
1832 ctx->vertex_buffer_count = num_buffers;
1833 memcpy(ctx->vertex_buffers, buffers, sz);
1834 } else {
1835 if (ctx->vertex_buffers) {
1836 free(ctx->vertex_buffers);
1837 ctx->vertex_buffers = NULL;
1838 }
1839
1840 ctx->vertex_buffer_count = 0;
1841 }
1842 }
1843
1844 static void
1845 panfrost_set_constant_buffer(
1846 struct pipe_context *pctx,
1847 enum pipe_shader_type shader, uint index,
1848 const struct pipe_constant_buffer *buf)
1849 {
1850 struct panfrost_context *ctx = pan_context(pctx);
1851 struct panfrost_constant_buffer *pbuf = &ctx->constant_buffer[shader];
1852
1853 size_t sz = buf ? buf->buffer_size : 0;
1854
1855 /* Free previous buffer */
1856
1857 pbuf->dirty = true;
1858 pbuf->size = sz;
1859
1860 if (pbuf->buffer) {
1861 free(pbuf->buffer);
1862 pbuf->buffer = NULL;
1863 }
1864
1865 /* If unbinding, we're done */
1866
1867 if (!buf)
1868 return;
1869
1870 /* Multiple constant buffers not yet supported */
1871 assert(index == 0);
1872
1873 const uint8_t *cpu;
1874
1875 struct panfrost_resource *rsrc = (struct panfrost_resource *) (buf->buffer);
1876
1877 if (rsrc) {
1878 cpu = rsrc->bo->cpu[0];
1879 } else if (buf->user_buffer) {
1880 cpu = buf->user_buffer;
1881 } else {
1882 DBG("No constant buffer?\n");
1883 return;
1884 }
1885
1886 /* Copy the constant buffer into the driver context for later upload */
1887
1888 pbuf->buffer = malloc(sz);
1889 memcpy(pbuf->buffer, cpu + buf->buffer_offset, sz);
1890 }
1891
1892 static void
1893 panfrost_set_stencil_ref(
1894 struct pipe_context *pctx,
1895 const struct pipe_stencil_ref *ref)
1896 {
1897 struct panfrost_context *ctx = pan_context(pctx);
1898 ctx->stencil_ref = *ref;
1899
1900 /* Shader core dirty */
1901 ctx->dirty |= PAN_DIRTY_FS;
1902 }
1903
1904 static struct pipe_sampler_view *
1905 panfrost_create_sampler_view(
1906 struct pipe_context *pctx,
1907 struct pipe_resource *texture,
1908 const struct pipe_sampler_view *template)
1909 {
1910 struct panfrost_sampler_view *so = CALLOC_STRUCT(panfrost_sampler_view);
1911 int bytes_per_pixel = util_format_get_blocksize(texture->format);
1912
1913 pipe_reference(NULL, &texture->reference);
1914
1915 struct panfrost_resource *prsrc = (struct panfrost_resource *) texture;
1916
1917 so->base = *template;
1918 so->base.texture = texture;
1919 so->base.reference.count = 1;
1920 so->base.context = pctx;
1921
1922 /* sampler_views correspond to texture descriptors, minus the texture
1923 * (data) itself. So, we serialise the descriptor here and cache it for
1924 * later. */
1925
1926 /* TODO: Other types of textures */
1927 assert(template->target == PIPE_TEXTURE_2D);
1928
1929 /* Make sure it's something with which we're familiar */
1930 assert(bytes_per_pixel >= 1 && bytes_per_pixel <= 4);
1931
1932 /* TODO: Detect from format better */
1933 const struct util_format_description *desc = util_format_description(prsrc->base.format);
1934
1935 unsigned char user_swizzle[4] = {
1936 template->swizzle_r,
1937 template->swizzle_g,
1938 template->swizzle_b,
1939 template->swizzle_a
1940 };
1941
1942 enum mali_format format = panfrost_find_format(desc);
1943
1944 bool is_depth = desc->format == PIPE_FORMAT_Z32_UNORM;
1945
1946 unsigned usage2_layout = 0x10;
1947
1948 switch (prsrc->bo->layout) {
1949 case PAN_AFBC:
1950 usage2_layout |= 0x8 | 0x4;
1951 break;
1952 case PAN_TILED:
1953 usage2_layout |= 0x1;
1954 break;
1955 case PAN_LINEAR:
1956 usage2_layout |= is_depth ? 0x1 : 0x2;
1957 break;
1958 default:
1959 assert(0);
1960 break;
1961 }
1962
1963 struct mali_texture_descriptor texture_descriptor = {
1964 .width = MALI_POSITIVE(texture->width0),
1965 .height = MALI_POSITIVE(texture->height0),
1966 .depth = MALI_POSITIVE(texture->depth0),
1967
1968 /* TODO: Decode */
1969 .format = {
1970 .swizzle = panfrost_translate_swizzle_4(desc->swizzle),
1971 .format = format,
1972
1973 .usage1 = 0x0,
1974 .is_not_cubemap = 1,
1975
1976 .usage2 = usage2_layout
1977 },
1978
1979 .swizzle = panfrost_translate_swizzle_4(user_swizzle)
1980 };
1981
1982 /* TODO: Other base levels require adjusting dimensions / level numbers / etc */
1983 assert (template->u.tex.first_level == 0);
1984
1985 texture_descriptor.nr_mipmap_levels = template->u.tex.last_level - template->u.tex.first_level;
1986
1987 so->hw = texture_descriptor;
1988
1989 return (struct pipe_sampler_view *) so;
1990 }
1991
1992 static void
1993 panfrost_set_sampler_views(
1994 struct pipe_context *pctx,
1995 enum pipe_shader_type shader,
1996 unsigned start_slot, unsigned num_views,
1997 struct pipe_sampler_view **views)
1998 {
1999 struct panfrost_context *ctx = pan_context(pctx);
2000
2001 assert(start_slot == 0);
2002
2003 ctx->sampler_view_count[shader] = num_views;
2004 memcpy(ctx->sampler_views[shader], views, num_views * sizeof (void *));
2005
2006 ctx->dirty |= PAN_DIRTY_TEXTURES;
2007 }
2008
2009 static void
2010 panfrost_sampler_view_destroy(
2011 struct pipe_context *pctx,
2012 struct pipe_sampler_view *views)
2013 {
2014 //struct panfrost_context *ctx = pan_context(pctx);
2015
2016 /* TODO */
2017
2018 free(views);
2019 }
2020
2021 static void
2022 panfrost_set_framebuffer_state(struct pipe_context *pctx,
2023 const struct pipe_framebuffer_state *fb)
2024 {
2025 struct panfrost_context *ctx = pan_context(pctx);
2026
2027 /* Flush when switching away from an FBO */
2028
2029 if (!panfrost_is_scanout(ctx)) {
2030 panfrost_flush(pctx, NULL, 0);
2031 }
2032
2033 ctx->pipe_framebuffer.nr_cbufs = fb->nr_cbufs;
2034 ctx->pipe_framebuffer.samples = fb->samples;
2035 ctx->pipe_framebuffer.layers = fb->layers;
2036 ctx->pipe_framebuffer.width = fb->width;
2037 ctx->pipe_framebuffer.height = fb->height;
2038
2039 for (int i = 0; i < PIPE_MAX_COLOR_BUFS; i++) {
2040 struct pipe_surface *cb = i < fb->nr_cbufs ? fb->cbufs[i] : NULL;
2041
2042 /* check if changing cbuf */
2043 if (ctx->pipe_framebuffer.cbufs[i] == cb) continue;
2044
2045 if (cb && (i != 0)) {
2046 DBG("XXX: Multiple render targets not supported before t7xx!\n");
2047 assert(0);
2048 }
2049
2050 /* assign new */
2051 pipe_surface_reference(&ctx->pipe_framebuffer.cbufs[i], cb);
2052
2053 if (!cb)
2054 continue;
2055
2056 if (ctx->require_sfbd)
2057 ctx->vt_framebuffer_sfbd = panfrost_emit_sfbd(ctx);
2058 else
2059 ctx->vt_framebuffer_mfbd = panfrost_emit_mfbd(ctx);
2060
2061 panfrost_attach_vt_framebuffer(ctx);
2062
2063 struct panfrost_resource *tex = ((struct panfrost_resource *) ctx->pipe_framebuffer.cbufs[i]->texture);
2064 bool is_scanout = panfrost_is_scanout(ctx);
2065
2066 if (!is_scanout && tex->bo->layout != PAN_AFBC) {
2067 /* The blob is aggressive about enabling AFBC. As such,
2068 * it's pretty much necessary to use it here, since we
2069 * have no traces of non-compressed FBO. */
2070
2071 panfrost_enable_afbc(ctx, tex, false);
2072 }
2073
2074 if (!is_scanout && !tex->bo->has_checksum) {
2075 /* Enable transaction elimination if we can */
2076 panfrost_enable_checksum(ctx, tex);
2077 }
2078 }
2079
2080 {
2081 struct pipe_surface *zb = fb->zsbuf;
2082
2083 if (ctx->pipe_framebuffer.zsbuf != zb) {
2084 pipe_surface_reference(&ctx->pipe_framebuffer.zsbuf, zb);
2085
2086 if (zb) {
2087 /* FBO has depth */
2088
2089 if (ctx->require_sfbd)
2090 ctx->vt_framebuffer_sfbd = panfrost_emit_sfbd(ctx);
2091 else
2092 ctx->vt_framebuffer_mfbd = panfrost_emit_mfbd(ctx);
2093
2094 panfrost_attach_vt_framebuffer(ctx);
2095
2096 /* Keep the depth FBO linear */
2097 }
2098 }
2099 }
2100 }
2101
2102 static void *
2103 panfrost_create_blend_state(struct pipe_context *pipe,
2104 const struct pipe_blend_state *blend)
2105 {
2106 struct panfrost_context *ctx = pan_context(pipe);
2107 struct panfrost_blend_state *so = CALLOC_STRUCT(panfrost_blend_state);
2108 so->base = *blend;
2109
2110 /* TODO: The following features are not yet implemented */
2111 assert(!blend->logicop_enable);
2112 assert(!blend->alpha_to_coverage);
2113 assert(!blend->alpha_to_one);
2114
2115 /* Compile the blend state, first as fixed-function if we can */
2116
2117 if (panfrost_make_fixed_blend_mode(&blend->rt[0], &so->equation, blend->rt[0].colormask, &ctx->blend_color))
2118 return so;
2119
2120 /* If we can't, compile a blend shader instead */
2121
2122 panfrost_make_blend_shader(ctx, so, &ctx->blend_color);
2123
2124 return so;
2125 }
2126
2127 static void
2128 panfrost_bind_blend_state(struct pipe_context *pipe,
2129 void *cso)
2130 {
2131 struct panfrost_context *ctx = pan_context(pipe);
2132 struct pipe_blend_state *blend = (struct pipe_blend_state *) cso;
2133 struct panfrost_blend_state *pblend = (struct panfrost_blend_state *) cso;
2134 ctx->blend = pblend;
2135
2136 if (!blend)
2137 return;
2138
2139 SET_BIT(ctx->fragment_shader_core.unknown2_4, MALI_NO_DITHER, !blend->dither);
2140
2141 /* TODO: Attach color */
2142
2143 /* Shader itself is not dirty, but the shader core is */
2144 ctx->dirty |= PAN_DIRTY_FS;
2145 }
2146
2147 static void
2148 panfrost_delete_blend_state(struct pipe_context *pipe,
2149 void *blend)
2150 {
2151 struct panfrost_blend_state *so = (struct panfrost_blend_state *) blend;
2152
2153 if (so->has_blend_shader) {
2154 DBG("Deleting blend state leak blend shaders bytecode\n");
2155 }
2156
2157 free(blend);
2158 }
2159
2160 static void
2161 panfrost_set_blend_color(struct pipe_context *pipe,
2162 const struct pipe_blend_color *blend_color)
2163 {
2164 struct panfrost_context *ctx = pan_context(pipe);
2165
2166 /* If blend_color is we're unbinding, so ctx->blend_color is now undefined -> nothing to do */
2167
2168 if (blend_color) {
2169 ctx->blend_color = *blend_color;
2170
2171 /* The blend mode depends on the blend constant color, due to the
2172 * fixed/programmable split. So, we're forced to regenerate the blend
2173 * equation */
2174
2175 /* TODO: Attach color */
2176 }
2177 }
2178
2179 static void *
2180 panfrost_create_depth_stencil_state(struct pipe_context *pipe,
2181 const struct pipe_depth_stencil_alpha_state *depth_stencil)
2182 {
2183 return mem_dup(depth_stencil, sizeof(*depth_stencil));
2184 }
2185
2186 static void
2187 panfrost_bind_depth_stencil_state(struct pipe_context *pipe,
2188 void *cso)
2189 {
2190 struct panfrost_context *ctx = pan_context(pipe);
2191 struct pipe_depth_stencil_alpha_state *depth_stencil = cso;
2192 ctx->depth_stencil = depth_stencil;
2193
2194 if (!depth_stencil)
2195 return;
2196
2197 /* Alpha does not exist in the hardware (it's not in ES3), so it's
2198 * emulated in the fragment shader */
2199
2200 if (depth_stencil->alpha.enabled) {
2201 /* We need to trigger a new shader (maybe) */
2202 ctx->base.bind_fs_state(&ctx->base, ctx->fs);
2203 }
2204
2205 /* Stencil state */
2206 SET_BIT(ctx->fragment_shader_core.unknown2_4, MALI_STENCIL_TEST, depth_stencil->stencil[0].enabled); /* XXX: which one? */
2207
2208 panfrost_make_stencil_state(&depth_stencil->stencil[0], &ctx->fragment_shader_core.stencil_front);
2209 ctx->fragment_shader_core.stencil_mask_front = depth_stencil->stencil[0].writemask;
2210
2211 panfrost_make_stencil_state(&depth_stencil->stencil[1], &ctx->fragment_shader_core.stencil_back);
2212 ctx->fragment_shader_core.stencil_mask_back = depth_stencil->stencil[1].writemask;
2213
2214 /* Depth state (TODO: Refactor) */
2215 SET_BIT(ctx->fragment_shader_core.unknown2_3, MALI_DEPTH_TEST, depth_stencil->depth.enabled);
2216
2217 int func = depth_stencil->depth.enabled ? depth_stencil->depth.func : PIPE_FUNC_ALWAYS;
2218
2219 ctx->fragment_shader_core.unknown2_3 &= ~MALI_DEPTH_FUNC_MASK;
2220 ctx->fragment_shader_core.unknown2_3 |= MALI_DEPTH_FUNC(panfrost_translate_compare_func(func));
2221
2222 /* Bounds test not implemented */
2223 assert(!depth_stencil->depth.bounds_test);
2224
2225 ctx->dirty |= PAN_DIRTY_FS;
2226 }
2227
2228 static void
2229 panfrost_delete_depth_stencil_state(struct pipe_context *pipe, void *depth)
2230 {
2231 free( depth );
2232 }
2233
2234 static void
2235 panfrost_set_sample_mask(struct pipe_context *pipe,
2236 unsigned sample_mask)
2237 {
2238 }
2239
2240 static void
2241 panfrost_set_clip_state(struct pipe_context *pipe,
2242 const struct pipe_clip_state *clip)
2243 {
2244 //struct panfrost_context *panfrost = pan_context(pipe);
2245 }
2246
2247 static void
2248 panfrost_set_viewport_states(struct pipe_context *pipe,
2249 unsigned start_slot,
2250 unsigned num_viewports,
2251 const struct pipe_viewport_state *viewports)
2252 {
2253 struct panfrost_context *ctx = pan_context(pipe);
2254
2255 assert(start_slot == 0);
2256 assert(num_viewports == 1);
2257
2258 ctx->pipe_viewport = *viewports;
2259
2260 #if 0
2261 /* TODO: What if not centered? */
2262 float w = abs(viewports->scale[0]) * 2.0;
2263 float h = abs(viewports->scale[1]) * 2.0;
2264
2265 ctx->viewport.viewport1[0] = MALI_POSITIVE((int) w);
2266 ctx->viewport.viewport1[1] = MALI_POSITIVE((int) h);
2267 #endif
2268 }
2269
2270 static void
2271 panfrost_set_scissor_states(struct pipe_context *pipe,
2272 unsigned start_slot,
2273 unsigned num_scissors,
2274 const struct pipe_scissor_state *scissors)
2275 {
2276 struct panfrost_context *ctx = pan_context(pipe);
2277
2278 assert(start_slot == 0);
2279 assert(num_scissors == 1);
2280
2281 ctx->scissor = *scissors;
2282 }
2283
2284 static void
2285 panfrost_set_polygon_stipple(struct pipe_context *pipe,
2286 const struct pipe_poly_stipple *stipple)
2287 {
2288 //struct panfrost_context *panfrost = pan_context(pipe);
2289 }
2290
2291 static void
2292 panfrost_set_active_query_state(struct pipe_context *pipe,
2293 boolean enable)
2294 {
2295 //struct panfrost_context *panfrost = pan_context(pipe);
2296 }
2297
2298 static void
2299 panfrost_destroy(struct pipe_context *pipe)
2300 {
2301 struct panfrost_context *panfrost = pan_context(pipe);
2302 struct panfrost_screen *screen = pan_screen(pipe->screen);
2303
2304 if (panfrost->blitter)
2305 util_blitter_destroy(panfrost->blitter);
2306
2307 screen->driver->free_slab(screen, &panfrost->scratchpad);
2308 screen->driver->free_slab(screen, &panfrost->varying_mem);
2309 screen->driver->free_slab(screen, &panfrost->shaders);
2310 screen->driver->free_slab(screen, &panfrost->tiler_heap);
2311 screen->driver->free_slab(screen, &panfrost->misc_0);
2312 }
2313
2314 static struct pipe_query *
2315 panfrost_create_query(struct pipe_context *pipe,
2316 unsigned type,
2317 unsigned index)
2318 {
2319 struct panfrost_query *q = CALLOC_STRUCT(panfrost_query);
2320
2321 q->type = type;
2322 q->index = index;
2323
2324 return (struct pipe_query *) q;
2325 }
2326
2327 static void
2328 panfrost_destroy_query(struct pipe_context *pipe, struct pipe_query *q)
2329 {
2330 FREE(q);
2331 }
2332
2333 static boolean
2334 panfrost_begin_query(struct pipe_context *pipe, struct pipe_query *q)
2335 {
2336 struct panfrost_context *ctx = pan_context(pipe);
2337 struct panfrost_query *query = (struct panfrost_query *) q;
2338
2339 switch (query->type) {
2340 case PIPE_QUERY_OCCLUSION_COUNTER:
2341 case PIPE_QUERY_OCCLUSION_PREDICATE:
2342 case PIPE_QUERY_OCCLUSION_PREDICATE_CONSERVATIVE:
2343 {
2344 /* Allocate a word for the query results to be stored */
2345 query->transfer = panfrost_allocate_chunk(ctx, sizeof(unsigned), HEAP_DESCRIPTOR);
2346
2347 ctx->occlusion_query = query;
2348
2349 break;
2350 }
2351
2352 default:
2353 DBG("Skipping query %d\n", query->type);
2354 break;
2355 }
2356
2357 return true;
2358 }
2359
2360 static bool
2361 panfrost_end_query(struct pipe_context *pipe, struct pipe_query *q)
2362 {
2363 struct panfrost_context *ctx = pan_context(pipe);
2364 ctx->occlusion_query = NULL;
2365 return true;
2366 }
2367
2368 static boolean
2369 panfrost_get_query_result(struct pipe_context *pipe,
2370 struct pipe_query *q,
2371 boolean wait,
2372 union pipe_query_result *vresult)
2373 {
2374 /* STUB */
2375 struct panfrost_query *query = (struct panfrost_query *) q;
2376
2377 /* We need to flush out the jobs to actually run the counter, TODO
2378 * check wait, TODO wallpaper after if needed */
2379
2380 panfrost_flush(pipe, NULL, PIPE_FLUSH_END_OF_FRAME);
2381
2382 switch (query->type) {
2383 case PIPE_QUERY_OCCLUSION_COUNTER:
2384 case PIPE_QUERY_OCCLUSION_PREDICATE:
2385 case PIPE_QUERY_OCCLUSION_PREDICATE_CONSERVATIVE: {
2386 /* Read back the query results */
2387 unsigned *result = (unsigned *) query->transfer.cpu;
2388 unsigned passed = *result;
2389
2390 if (query->type == PIPE_QUERY_OCCLUSION_COUNTER) {
2391 vresult->u64 = passed;
2392 } else {
2393 vresult->b = !!passed;
2394 }
2395
2396 break;
2397 }
2398 default:
2399 DBG("Skipped query get %d\n", query->type);
2400 break;
2401 }
2402
2403 return true;
2404 }
2405
2406 static void
2407 panfrost_setup_hardware(struct panfrost_context *ctx)
2408 {
2409 struct pipe_context *gallium = (struct pipe_context *) ctx;
2410 struct panfrost_screen *screen = pan_screen(gallium->screen);
2411
2412 for (int i = 0; i < ARRAY_SIZE(ctx->transient_pools); ++i) {
2413 /* Allocate the beginning of the transient pool */
2414 int entry_size = (1 << 22); /* 4MB */
2415
2416 ctx->transient_pools[i].entry_size = entry_size;
2417 ctx->transient_pools[i].entry_count = 1;
2418
2419 ctx->transient_pools[i].entries[0] = (struct panfrost_memory_entry *) pb_slab_alloc(&screen->slabs, entry_size, HEAP_TRANSIENT);
2420 }
2421
2422 screen->driver->allocate_slab(screen, &ctx->scratchpad, 64, false, 0, 0, 0);
2423 screen->driver->allocate_slab(screen, &ctx->varying_mem, 16384, false, PAN_ALLOCATE_INVISIBLE | PAN_ALLOCATE_COHERENT_LOCAL, 0, 0);
2424 screen->driver->allocate_slab(screen, &ctx->shaders, 4096, true, PAN_ALLOCATE_EXECUTE, 0, 0);
2425 screen->driver->allocate_slab(screen, &ctx->tiler_heap, 32768, false, PAN_ALLOCATE_INVISIBLE | PAN_ALLOCATE_GROWABLE, 1, 128);
2426 screen->driver->allocate_slab(screen, &ctx->misc_0, 128*128, false, PAN_ALLOCATE_INVISIBLE | PAN_ALLOCATE_GROWABLE, 1, 128);
2427
2428 }
2429
2430 /* New context creation, which also does hardware initialisation since I don't
2431 * know the better way to structure this :smirk: */
2432
2433 struct pipe_context *
2434 panfrost_create_context(struct pipe_screen *screen, void *priv, unsigned flags)
2435 {
2436 struct panfrost_context *ctx = CALLOC_STRUCT(panfrost_context);
2437 struct panfrost_screen *pscreen = pan_screen(screen);
2438 memset(ctx, 0, sizeof(*ctx));
2439 struct pipe_context *gallium = (struct pipe_context *) ctx;
2440 unsigned gpu_id;
2441
2442 gpu_id = pscreen->driver->query_gpu_version(pscreen);
2443
2444 ctx->is_t6xx = gpu_id <= 0x0750; /* For now, this flag means T760 or less */
2445 ctx->require_sfbd = gpu_id < 0x0750; /* T760 is the first to support MFBD */
2446
2447 gallium->screen = screen;
2448
2449 gallium->destroy = panfrost_destroy;
2450
2451 gallium->set_framebuffer_state = panfrost_set_framebuffer_state;
2452
2453 gallium->flush = panfrost_flush;
2454 gallium->clear = panfrost_clear;
2455 gallium->draw_vbo = panfrost_draw_vbo;
2456
2457 gallium->set_vertex_buffers = panfrost_set_vertex_buffers;
2458 gallium->set_constant_buffer = panfrost_set_constant_buffer;
2459
2460 gallium->set_stencil_ref = panfrost_set_stencil_ref;
2461
2462 gallium->create_sampler_view = panfrost_create_sampler_view;
2463 gallium->set_sampler_views = panfrost_set_sampler_views;
2464 gallium->sampler_view_destroy = panfrost_sampler_view_destroy;
2465
2466 gallium->create_rasterizer_state = panfrost_create_rasterizer_state;
2467 gallium->bind_rasterizer_state = panfrost_bind_rasterizer_state;
2468 gallium->delete_rasterizer_state = panfrost_generic_cso_delete;
2469
2470 gallium->create_vertex_elements_state = panfrost_create_vertex_elements_state;
2471 gallium->bind_vertex_elements_state = panfrost_bind_vertex_elements_state;
2472 gallium->delete_vertex_elements_state = panfrost_delete_vertex_elements_state;
2473
2474 gallium->create_fs_state = panfrost_create_shader_state;
2475 gallium->delete_fs_state = panfrost_delete_shader_state;
2476 gallium->bind_fs_state = panfrost_bind_fs_state;
2477
2478 gallium->create_vs_state = panfrost_create_shader_state;
2479 gallium->delete_vs_state = panfrost_delete_shader_state;
2480 gallium->bind_vs_state = panfrost_bind_vs_state;
2481
2482 gallium->create_sampler_state = panfrost_create_sampler_state;
2483 gallium->delete_sampler_state = panfrost_generic_cso_delete;
2484 gallium->bind_sampler_states = panfrost_bind_sampler_states;
2485
2486 gallium->create_blend_state = panfrost_create_blend_state;
2487 gallium->bind_blend_state = panfrost_bind_blend_state;
2488 gallium->delete_blend_state = panfrost_delete_blend_state;
2489
2490 gallium->set_blend_color = panfrost_set_blend_color;
2491
2492 gallium->create_depth_stencil_alpha_state = panfrost_create_depth_stencil_state;
2493 gallium->bind_depth_stencil_alpha_state = panfrost_bind_depth_stencil_state;
2494 gallium->delete_depth_stencil_alpha_state = panfrost_delete_depth_stencil_state;
2495
2496 gallium->set_sample_mask = panfrost_set_sample_mask;
2497
2498 gallium->set_clip_state = panfrost_set_clip_state;
2499 gallium->set_viewport_states = panfrost_set_viewport_states;
2500 gallium->set_scissor_states = panfrost_set_scissor_states;
2501 gallium->set_polygon_stipple = panfrost_set_polygon_stipple;
2502 gallium->set_active_query_state = panfrost_set_active_query_state;
2503
2504 gallium->create_query = panfrost_create_query;
2505 gallium->destroy_query = panfrost_destroy_query;
2506 gallium->begin_query = panfrost_begin_query;
2507 gallium->end_query = panfrost_end_query;
2508 gallium->get_query_result = panfrost_get_query_result;
2509
2510 panfrost_resource_context_init(gallium);
2511
2512 pscreen->driver->init_context(ctx);
2513
2514 panfrost_setup_hardware(ctx);
2515
2516 /* XXX: leaks */
2517 gallium->stream_uploader = u_upload_create_default(gallium);
2518 gallium->const_uploader = gallium->stream_uploader;
2519 assert(gallium->stream_uploader);
2520
2521 /* Midgard supports ES modes, plus QUADS/QUAD_STRIPS/POLYGON */
2522 ctx->draw_modes = (1 << (PIPE_PRIM_POLYGON + 1)) - 1;
2523
2524 ctx->primconvert = util_primconvert_create(gallium, ctx->draw_modes);
2525
2526 ctx->blitter = util_blitter_create(gallium);
2527 assert(ctx->blitter);
2528
2529 /* Prepare for render! */
2530
2531 panfrost_job_init(ctx);
2532 panfrost_emit_vertex_payload(ctx);
2533 panfrost_emit_tiler_payload(ctx);
2534 panfrost_invalidate_frame(ctx);
2535 panfrost_default_shader_backend(ctx);
2536 panfrost_generate_space_filler_indices();
2537
2538 return gallium;
2539 }