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