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