panfrost: Bail out early when doing a wallpaper blit
[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_format.h"
30
31 #include "util/macros.h"
32 #include "util/u_format.h"
33 #include "util/u_inlines.h"
34 #include "util/u_upload_mgr.h"
35 #include "util/u_memory.h"
36 #include "util/u_vbuf.h"
37 #include "util/half_float.h"
38 #include "util/u_helpers.h"
39 #include "util/u_format.h"
40 #include "util/u_prim_restart.h"
41 #include "indices/u_primconvert.h"
42 #include "tgsi/tgsi_parse.h"
43 #include "tgsi/tgsi_from_mesa.h"
44 #include "util/u_math.h"
45
46 #include "pan_screen.h"
47 #include "pan_blending.h"
48 #include "pan_blend_shaders.h"
49 #include "pan_util.h"
50 #include "pan_tiler.h"
51
52 /* Do not actually send anything to the GPU; merely generate the cmdstream as fast as possible. Disables framebuffer writes */
53 //#define DRY_RUN
54
55 /* Framebuffer descriptor */
56
57 static struct midgard_tiler_descriptor
58 panfrost_emit_midg_tiler(
59 struct panfrost_context *ctx,
60 unsigned width,
61 unsigned height,
62 unsigned vertex_count)
63 {
64 struct midgard_tiler_descriptor t = {};
65
66 t.hierarchy_mask =
67 panfrost_choose_hierarchy_mask(width, height, vertex_count);
68
69 /* Compute the polygon header size and use that to offset the body */
70
71 unsigned header_size = panfrost_tiler_header_size(
72 width, height, t.hierarchy_mask);
73
74 unsigned body_size = panfrost_tiler_body_size(
75 width, height, t.hierarchy_mask);
76
77 /* Sanity check */
78
79 if (t.hierarchy_mask) {
80 assert(ctx->tiler_polygon_list.bo->size >= (header_size + body_size));
81
82 /* Specify allocated tiler structures */
83 t.polygon_list = ctx->tiler_polygon_list.bo->gpu;
84
85 /* Allow the entire tiler heap */
86 t.heap_start = ctx->tiler_heap.bo->gpu;
87 t.heap_end =
88 ctx->tiler_heap.bo->gpu + ctx->tiler_heap.bo->size;
89 } else {
90 /* The tiler is disabled, so don't allow the tiler heap */
91 t.heap_start = ctx->tiler_heap.bo->gpu;
92 t.heap_end = t.heap_start;
93
94 /* Use a dummy polygon list */
95 t.polygon_list = ctx->tiler_dummy.bo->gpu;
96
97 /* Also, set a "tiler disabled?" flag? */
98 t.hierarchy_mask |= 0x1000;
99 }
100
101 t.polygon_list_body =
102 t.polygon_list + header_size;
103
104 t.polygon_list_size =
105 header_size + body_size;
106
107 return t;
108 }
109
110 struct mali_single_framebuffer
111 panfrost_emit_sfbd(struct panfrost_context *ctx, unsigned vertex_count)
112 {
113 unsigned width = ctx->pipe_framebuffer.width;
114 unsigned height = ctx->pipe_framebuffer.height;
115
116 struct mali_single_framebuffer framebuffer = {
117 .width = MALI_POSITIVE(width),
118 .height = MALI_POSITIVE(height),
119 .unknown2 = 0x1f,
120 .format = 0x30000000,
121 .clear_flags = 0x1000,
122 .unknown_address_0 = ctx->scratchpad.bo->gpu,
123 .tiler = panfrost_emit_midg_tiler(ctx,
124 width, height, vertex_count),
125 };
126
127 return framebuffer;
128 }
129
130 struct bifrost_framebuffer
131 panfrost_emit_mfbd(struct panfrost_context *ctx, unsigned vertex_count)
132 {
133 unsigned width = ctx->pipe_framebuffer.width;
134 unsigned height = ctx->pipe_framebuffer.height;
135
136 struct bifrost_framebuffer framebuffer = {
137 .unk0 = 0x1e5, /* 1e4 if no spill */
138 .width1 = MALI_POSITIVE(width),
139 .height1 = MALI_POSITIVE(height),
140 .width2 = MALI_POSITIVE(width),
141 .height2 = MALI_POSITIVE(height),
142
143 .unk1 = 0x1080,
144
145 .rt_count_1 = MALI_POSITIVE(ctx->pipe_framebuffer.nr_cbufs),
146 .rt_count_2 = 4,
147
148 .unknown2 = 0x1f,
149
150 .scratchpad = ctx->scratchpad.bo->gpu,
151 .tiler = panfrost_emit_midg_tiler(ctx,
152 width, height, vertex_count)
153 };
154
155 return framebuffer;
156 }
157
158 /* Are we currently rendering to the screen (rather than an FBO)? */
159
160 bool
161 panfrost_is_scanout(struct panfrost_context *ctx)
162 {
163 /* If there is no color buffer, it's an FBO */
164 if (ctx->pipe_framebuffer.nr_cbufs != 1)
165 return false;
166
167 /* If we're too early that no framebuffer was sent, it's scanout */
168 if (!ctx->pipe_framebuffer.cbufs[0])
169 return true;
170
171 return ctx->pipe_framebuffer.cbufs[0]->texture->bind & PIPE_BIND_DISPLAY_TARGET ||
172 ctx->pipe_framebuffer.cbufs[0]->texture->bind & PIPE_BIND_SCANOUT ||
173 ctx->pipe_framebuffer.cbufs[0]->texture->bind & PIPE_BIND_SHARED;
174 }
175
176 static void
177 panfrost_clear(
178 struct pipe_context *pipe,
179 unsigned buffers,
180 const union pipe_color_union *color,
181 double depth, unsigned stencil)
182 {
183 struct panfrost_context *ctx = pan_context(pipe);
184 struct panfrost_job *job = panfrost_get_job_for_fbo(ctx);
185
186 panfrost_job_clear(ctx, job, buffers, color, depth, stencil);
187 }
188
189 static mali_ptr
190 panfrost_attach_vt_mfbd(struct panfrost_context *ctx)
191 {
192 struct bifrost_framebuffer mfbd = panfrost_emit_mfbd(ctx, ~0);
193
194 return panfrost_upload_transient(ctx, &mfbd, sizeof(mfbd)) | MALI_MFBD;
195 }
196
197 static mali_ptr
198 panfrost_attach_vt_sfbd(struct panfrost_context *ctx)
199 {
200 struct mali_single_framebuffer sfbd = panfrost_emit_sfbd(ctx, ~0);
201
202 return panfrost_upload_transient(ctx, &sfbd, sizeof(sfbd)) | MALI_SFBD;
203 }
204
205 static void
206 panfrost_attach_vt_framebuffer(struct panfrost_context *ctx, bool skippable)
207 {
208 /* Skip the attach if we can */
209
210 if (skippable && ctx->payloads[PIPE_SHADER_VERTEX].postfix.framebuffer) {
211 assert(ctx->payloads[PIPE_SHADER_FRAGMENT].postfix.framebuffer);
212 return;
213 }
214
215 struct panfrost_screen *screen = pan_screen(ctx->base.screen);
216 mali_ptr framebuffer = screen->require_sfbd ?
217 panfrost_attach_vt_sfbd(ctx) :
218 panfrost_attach_vt_mfbd(ctx);
219
220 for (unsigned i = 0; i < PIPE_SHADER_TYPES; ++i)
221 ctx->payloads[i].postfix.framebuffer = framebuffer;
222 }
223
224 /* Reset per-frame context, called on context initialisation as well as after
225 * flushing a frame */
226
227 static void
228 panfrost_invalidate_frame(struct panfrost_context *ctx)
229 {
230 for (unsigned i = 0; i < PIPE_SHADER_TYPES; ++i)
231 ctx->payloads[i].postfix.framebuffer = 0;
232
233 if (ctx->rasterizer)
234 ctx->dirty |= PAN_DIRTY_RASTERIZER;
235
236 /* XXX */
237 ctx->dirty |= PAN_DIRTY_SAMPLERS | PAN_DIRTY_TEXTURES;
238 }
239
240 /* In practice, every field of these payloads should be configurable
241 * arbitrarily, which means these functions are basically catch-all's for
242 * as-of-yet unwavering unknowns */
243
244 static void
245 panfrost_emit_vertex_payload(struct panfrost_context *ctx)
246 {
247 /* 0x2 bit clear on 32-bit T6XX */
248
249 struct midgard_payload_vertex_tiler payload = {
250 .gl_enables = 0x4 | 0x2,
251 };
252
253 /* Vertex and compute are closely coupled, so share a payload */
254
255 memcpy(&ctx->payloads[PIPE_SHADER_VERTEX], &payload, sizeof(payload));
256 memcpy(&ctx->payloads[PIPE_SHADER_COMPUTE], &payload, sizeof(payload));
257 }
258
259 static void
260 panfrost_emit_tiler_payload(struct panfrost_context *ctx)
261 {
262 struct midgard_payload_vertex_tiler payload = {
263 .prefix = {
264 .zero1 = 0xffff, /* Why is this only seen on test-quad-textured? */
265 },
266 };
267
268 memcpy(&ctx->payloads[PIPE_SHADER_FRAGMENT], &payload, sizeof(payload));
269 }
270
271 static unsigned
272 translate_tex_wrap(enum pipe_tex_wrap w)
273 {
274 switch (w) {
275 case PIPE_TEX_WRAP_REPEAT:
276 return MALI_WRAP_REPEAT;
277
278 case PIPE_TEX_WRAP_CLAMP_TO_EDGE:
279 return MALI_WRAP_CLAMP_TO_EDGE;
280
281 case PIPE_TEX_WRAP_CLAMP_TO_BORDER:
282 return MALI_WRAP_CLAMP_TO_BORDER;
283
284 case PIPE_TEX_WRAP_MIRROR_REPEAT:
285 return MALI_WRAP_MIRRORED_REPEAT;
286
287 default:
288 unreachable("Invalid wrap");
289 }
290 }
291
292 static unsigned
293 panfrost_translate_compare_func(enum pipe_compare_func in)
294 {
295 switch (in) {
296 case PIPE_FUNC_NEVER:
297 return MALI_FUNC_NEVER;
298
299 case PIPE_FUNC_LESS:
300 return MALI_FUNC_LESS;
301
302 case PIPE_FUNC_EQUAL:
303 return MALI_FUNC_EQUAL;
304
305 case PIPE_FUNC_LEQUAL:
306 return MALI_FUNC_LEQUAL;
307
308 case PIPE_FUNC_GREATER:
309 return MALI_FUNC_GREATER;
310
311 case PIPE_FUNC_NOTEQUAL:
312 return MALI_FUNC_NOTEQUAL;
313
314 case PIPE_FUNC_GEQUAL:
315 return MALI_FUNC_GEQUAL;
316
317 case PIPE_FUNC_ALWAYS:
318 return MALI_FUNC_ALWAYS;
319
320 default:
321 unreachable("Invalid func");
322 }
323 }
324
325 static unsigned
326 panfrost_translate_alt_compare_func(enum pipe_compare_func in)
327 {
328 switch (in) {
329 case PIPE_FUNC_NEVER:
330 return MALI_ALT_FUNC_NEVER;
331
332 case PIPE_FUNC_LESS:
333 return MALI_ALT_FUNC_LESS;
334
335 case PIPE_FUNC_EQUAL:
336 return MALI_ALT_FUNC_EQUAL;
337
338 case PIPE_FUNC_LEQUAL:
339 return MALI_ALT_FUNC_LEQUAL;
340
341 case PIPE_FUNC_GREATER:
342 return MALI_ALT_FUNC_GREATER;
343
344 case PIPE_FUNC_NOTEQUAL:
345 return MALI_ALT_FUNC_NOTEQUAL;
346
347 case PIPE_FUNC_GEQUAL:
348 return MALI_ALT_FUNC_GEQUAL;
349
350 case PIPE_FUNC_ALWAYS:
351 return MALI_ALT_FUNC_ALWAYS;
352
353 default:
354 unreachable("Invalid alt func");
355 }
356 }
357
358 static unsigned
359 panfrost_translate_stencil_op(enum pipe_stencil_op in)
360 {
361 switch (in) {
362 case PIPE_STENCIL_OP_KEEP:
363 return MALI_STENCIL_KEEP;
364
365 case PIPE_STENCIL_OP_ZERO:
366 return MALI_STENCIL_ZERO;
367
368 case PIPE_STENCIL_OP_REPLACE:
369 return MALI_STENCIL_REPLACE;
370
371 case PIPE_STENCIL_OP_INCR:
372 return MALI_STENCIL_INCR;
373
374 case PIPE_STENCIL_OP_DECR:
375 return MALI_STENCIL_DECR;
376
377 case PIPE_STENCIL_OP_INCR_WRAP:
378 return MALI_STENCIL_INCR_WRAP;
379
380 case PIPE_STENCIL_OP_DECR_WRAP:
381 return MALI_STENCIL_DECR_WRAP;
382
383 case PIPE_STENCIL_OP_INVERT:
384 return MALI_STENCIL_INVERT;
385
386 default:
387 unreachable("Invalid stencil op");
388 }
389 }
390
391 static void
392 panfrost_make_stencil_state(const struct pipe_stencil_state *in, struct mali_stencil_test *out)
393 {
394 out->ref = 0; /* Gallium gets it from elsewhere */
395
396 out->mask = in->valuemask;
397 out->func = panfrost_translate_compare_func(in->func);
398 out->sfail = panfrost_translate_stencil_op(in->fail_op);
399 out->dpfail = panfrost_translate_stencil_op(in->zfail_op);
400 out->dppass = panfrost_translate_stencil_op(in->zpass_op);
401 }
402
403 static void
404 panfrost_default_shader_backend(struct panfrost_context *ctx)
405 {
406 struct mali_shader_meta shader = {
407 .alpha_coverage = ~MALI_ALPHA_COVERAGE(0.000000),
408
409 .unknown2_3 = MALI_DEPTH_FUNC(MALI_FUNC_ALWAYS) | 0x3010,
410 .unknown2_4 = MALI_NO_MSAA | 0x4e0,
411 };
412
413 /* unknown2_4 has 0x10 bit set on T6XX. We don't know why this is
414 * required (independent of 32-bit/64-bit descriptors), or why it's not
415 * used on later GPU revisions. Otherwise, all shader jobs fault on
416 * these earlier chips (perhaps this is a chicken bit of some kind).
417 * More investigation is needed. */
418
419 if (ctx->is_t6xx) {
420 shader.unknown2_4 |= 0x10;
421 }
422
423 struct pipe_stencil_state default_stencil = {
424 .enabled = 0,
425 .func = PIPE_FUNC_ALWAYS,
426 .fail_op = MALI_STENCIL_KEEP,
427 .zfail_op = MALI_STENCIL_KEEP,
428 .zpass_op = MALI_STENCIL_KEEP,
429 .writemask = 0xFF,
430 .valuemask = 0xFF
431 };
432
433 panfrost_make_stencil_state(&default_stencil, &shader.stencil_front);
434 shader.stencil_mask_front = default_stencil.writemask;
435
436 panfrost_make_stencil_state(&default_stencil, &shader.stencil_back);
437 shader.stencil_mask_back = default_stencil.writemask;
438
439 if (default_stencil.enabled)
440 shader.unknown2_4 |= MALI_STENCIL_TEST;
441
442 memcpy(&ctx->fragment_shader_core, &shader, sizeof(shader));
443 }
444
445 /* Generates a vertex/tiler job. This is, in some sense, the heart of the
446 * graphics command stream. It should be called once per draw, accordding to
447 * presentations. Set is_tiler for "tiler" jobs (fragment shader jobs, but in
448 * Mali parlance, "fragment" refers to framebuffer writeout). Clear it for
449 * vertex jobs. */
450
451 struct panfrost_transfer
452 panfrost_vertex_tiler_job(struct panfrost_context *ctx, bool is_tiler)
453 {
454 struct mali_job_descriptor_header job = {
455 .job_type = is_tiler ? JOB_TYPE_TILER : JOB_TYPE_VERTEX,
456 .job_descriptor_size = 1,
457 };
458
459 struct midgard_payload_vertex_tiler *payload = is_tiler ? &ctx->payloads[PIPE_SHADER_FRAGMENT] : &ctx->payloads[PIPE_SHADER_VERTEX];
460
461 struct panfrost_transfer transfer = panfrost_allocate_transient(ctx, sizeof(job) + sizeof(*payload));
462 memcpy(transfer.cpu, &job, sizeof(job));
463 memcpy(transfer.cpu + sizeof(job), payload, sizeof(*payload));
464 return transfer;
465 }
466
467 static mali_ptr
468 panfrost_emit_varyings(
469 struct panfrost_context *ctx,
470 union mali_attr *slot,
471 unsigned stride,
472 unsigned count)
473 {
474 /* Fill out the descriptor */
475 slot->stride = stride;
476 slot->size = stride * count;
477 slot->shift = slot->extra_flags = 0;
478
479 struct panfrost_transfer transfer =
480 panfrost_allocate_transient(ctx, slot->size);
481
482 slot->elements = transfer.gpu | MALI_ATTR_LINEAR;
483
484 return transfer.gpu;
485 }
486
487 static void
488 panfrost_emit_point_coord(union mali_attr *slot)
489 {
490 slot->elements = MALI_VARYING_POINT_COORD | MALI_ATTR_LINEAR;
491 slot->stride = slot->size = slot->shift = slot->extra_flags = 0;
492 }
493
494 static void
495 panfrost_emit_front_face(union mali_attr *slot)
496 {
497 slot->elements = MALI_VARYING_FRONT_FACING | MALI_ATTR_INTERNAL;
498 }
499
500 static void
501 panfrost_emit_varying_descriptor(
502 struct panfrost_context *ctx,
503 unsigned vertex_count)
504 {
505 /* Load the shaders */
506
507 struct panfrost_shader_state *vs = &ctx->shader[PIPE_SHADER_VERTEX]->variants[ctx->shader[PIPE_SHADER_VERTEX]->active_variant];
508 struct panfrost_shader_state *fs = &ctx->shader[PIPE_SHADER_FRAGMENT]->variants[ctx->shader[PIPE_SHADER_FRAGMENT]->active_variant];
509 unsigned int num_gen_varyings = 0;
510
511 /* Allocate the varying descriptor */
512
513 size_t vs_size = sizeof(struct mali_attr_meta) * vs->tripipe->varying_count;
514 size_t fs_size = sizeof(struct mali_attr_meta) * fs->tripipe->varying_count;
515
516 struct panfrost_transfer trans = panfrost_allocate_transient(ctx,
517 vs_size + fs_size);
518
519 /*
520 * Assign ->src_offset now that we know about all the general purpose
521 * varyings that will be used by the fragment and vertex shaders.
522 */
523 for (unsigned i = 0; i < vs->tripipe->varying_count; i++) {
524 /*
525 * General purpose varyings have ->index set to 0, skip other
526 * entries.
527 */
528 if (vs->varyings[i].index)
529 continue;
530
531 vs->varyings[i].src_offset = 16 * (num_gen_varyings++);
532 }
533
534 for (unsigned i = 0; i < fs->tripipe->varying_count; i++) {
535 unsigned j;
536
537 /* If we have a point sprite replacement, handle that here. We
538 * have to translate location first. TODO: Flip y in shader.
539 * We're already keying ... just time crunch .. */
540
541 unsigned loc = fs->varyings_loc[i];
542 unsigned pnt_loc =
543 (loc >= VARYING_SLOT_VAR0) ? (loc - VARYING_SLOT_VAR0) :
544 (loc == VARYING_SLOT_PNTC) ? 8 :
545 ~0;
546
547 if (~pnt_loc && fs->point_sprite_mask & (1 << pnt_loc)) {
548 /* gl_PointCoord index by convention */
549 fs->varyings[i].index = 3;
550 fs->reads_point_coord = true;
551
552 /* Swizzle out the z/w to 0/1 */
553 fs->varyings[i].format = MALI_RG16F;
554 fs->varyings[i].swizzle =
555 panfrost_get_default_swizzle(2);
556
557 continue;
558 }
559
560 if (fs->varyings[i].index)
561 continue;
562
563 /*
564 * Re-use the VS general purpose varying pos if it exists,
565 * create a new one otherwise.
566 */
567 for (j = 0; j < vs->tripipe->varying_count; j++) {
568 if (fs->varyings_loc[i] == vs->varyings_loc[j])
569 break;
570 }
571
572 if (j < vs->tripipe->varying_count)
573 fs->varyings[i].src_offset = vs->varyings[j].src_offset;
574 else
575 fs->varyings[i].src_offset = 16 * (num_gen_varyings++);
576 }
577
578 memcpy(trans.cpu, vs->varyings, vs_size);
579 memcpy(trans.cpu + vs_size, fs->varyings, fs_size);
580
581 ctx->payloads[PIPE_SHADER_VERTEX].postfix.varying_meta = trans.gpu;
582 ctx->payloads[PIPE_SHADER_FRAGMENT].postfix.varying_meta = trans.gpu + vs_size;
583
584 /* Buffer indices must be in this order per our convention */
585 union mali_attr varyings[PIPE_MAX_ATTRIBS];
586 unsigned idx = 0;
587
588 panfrost_emit_varyings(ctx, &varyings[idx++], num_gen_varyings * 16,
589 vertex_count);
590
591 /* fp32 vec4 gl_Position */
592 ctx->payloads[PIPE_SHADER_FRAGMENT].postfix.position_varying =
593 panfrost_emit_varyings(ctx, &varyings[idx++],
594 sizeof(float) * 4, vertex_count);
595
596
597 if (vs->writes_point_size || fs->reads_point_coord) {
598 /* fp16 vec1 gl_PointSize */
599 ctx->payloads[PIPE_SHADER_FRAGMENT].primitive_size.pointer =
600 panfrost_emit_varyings(ctx, &varyings[idx++],
601 2, vertex_count);
602 } else if (fs->reads_face) {
603 /* Dummy to advance index */
604 ++idx;
605 }
606
607 if (fs->reads_point_coord) {
608 /* Special descriptor */
609 panfrost_emit_point_coord(&varyings[idx++]);
610 } else if (fs->reads_face) {
611 ++idx;
612 }
613
614 if (fs->reads_face) {
615 panfrost_emit_front_face(&varyings[idx++]);
616 }
617
618 mali_ptr varyings_p = panfrost_upload_transient(ctx, &varyings, idx * sizeof(union mali_attr));
619 ctx->payloads[PIPE_SHADER_VERTEX].postfix.varyings = varyings_p;
620 ctx->payloads[PIPE_SHADER_FRAGMENT].postfix.varyings = varyings_p;
621 }
622
623 mali_ptr
624 panfrost_vertex_buffer_address(struct panfrost_context *ctx, unsigned i)
625 {
626 struct pipe_vertex_buffer *buf = &ctx->vertex_buffers[i];
627 struct panfrost_resource *rsrc = (struct panfrost_resource *) (buf->buffer.resource);
628
629 return rsrc->bo->gpu + buf->buffer_offset;
630 }
631
632 static bool
633 panfrost_writes_point_size(struct panfrost_context *ctx)
634 {
635 assert(ctx->shader[PIPE_SHADER_VERTEX]);
636 struct panfrost_shader_state *vs = &ctx->shader[PIPE_SHADER_VERTEX]->variants[ctx->shader[PIPE_SHADER_VERTEX]->active_variant];
637
638 return vs->writes_point_size && ctx->payloads[PIPE_SHADER_FRAGMENT].prefix.draw_mode == MALI_POINTS;
639 }
640
641 /* Stage the attribute descriptors so we can adjust src_offset
642 * to let BOs align nicely */
643
644 static void
645 panfrost_stage_attributes(struct panfrost_context *ctx)
646 {
647 struct panfrost_vertex_state *so = ctx->vertex;
648
649 size_t sz = sizeof(struct mali_attr_meta) * so->num_elements;
650 struct panfrost_transfer transfer = panfrost_allocate_transient(ctx, sz);
651 struct mali_attr_meta *target = (struct mali_attr_meta *) transfer.cpu;
652
653 /* Copy as-is for the first pass */
654 memcpy(target, so->hw, sz);
655
656 /* Fixup offsets for the second pass. Recall that the hardware
657 * calculates attribute addresses as:
658 *
659 * addr = base + (stride * vtx) + src_offset;
660 *
661 * However, on Mali, base must be aligned to 64-bytes, so we
662 * instead let:
663 *
664 * base' = base & ~63 = base - (base & 63)
665 *
666 * To compensate when using base' (see emit_vertex_data), we have
667 * to adjust src_offset by the masked off piece:
668 *
669 * addr' = base' + (stride * vtx) + (src_offset + (base & 63))
670 * = base - (base & 63) + (stride * vtx) + src_offset + (base & 63)
671 * = base + (stride * vtx) + src_offset
672 * = addr;
673 *
674 * QED.
675 */
676
677 unsigned start = ctx->payloads[PIPE_SHADER_VERTEX].draw_start;
678
679 for (unsigned i = 0; i < so->num_elements; ++i) {
680 unsigned vbi = so->pipe[i].vertex_buffer_index;
681 struct pipe_vertex_buffer *buf = &ctx->vertex_buffers[vbi];
682 mali_ptr addr = panfrost_vertex_buffer_address(ctx, vbi);
683
684 /* Adjust by the masked off bits of the offset */
685 target[i].src_offset += (addr & 63);
686
687 /* Also, somewhat obscurely per-instance data needs to be
688 * offset in response to a delayed start in an indexed draw */
689
690 if (so->pipe[i].instance_divisor && ctx->instance_count > 1 && start) {
691 target[i].src_offset -= buf->stride * start;
692 }
693
694
695 }
696
697 ctx->payloads[PIPE_SHADER_VERTEX].postfix.attribute_meta = transfer.gpu;
698 }
699
700 static void
701 panfrost_upload_sampler_descriptors(struct panfrost_context *ctx)
702 {
703 size_t desc_size = sizeof(struct mali_sampler_descriptor);
704
705 for (int t = 0; t <= PIPE_SHADER_FRAGMENT; ++t) {
706 mali_ptr upload = 0;
707
708 if (ctx->sampler_count[t] && ctx->sampler_view_count[t]) {
709 size_t transfer_size = desc_size * ctx->sampler_count[t];
710
711 struct panfrost_transfer transfer =
712 panfrost_allocate_transient(ctx, transfer_size);
713
714 struct mali_sampler_descriptor *desc =
715 (struct mali_sampler_descriptor *) transfer.cpu;
716
717 for (int i = 0; i < ctx->sampler_count[t]; ++i)
718 desc[i] = ctx->samplers[t][i]->hw;
719
720 upload = transfer.gpu;
721 }
722
723 ctx->payloads[t].postfix.sampler_descriptor = upload;
724 }
725 }
726
727 static unsigned
728 panfrost_layout_for_texture(struct panfrost_resource *rsrc, bool manual_stride)
729 {
730 /* TODO: other linear depth textures */
731 bool is_depth = rsrc->base.format == PIPE_FORMAT_Z32_UNORM;
732
733 unsigned usage2_layout = 0x10;
734
735 switch (rsrc->layout) {
736 case PAN_AFBC:
737 usage2_layout |= 0x8 | 0x4;
738 break;
739 case PAN_TILED:
740 usage2_layout |= 0x1;
741 break;
742 case PAN_LINEAR:
743 usage2_layout |= is_depth ? 0x1 : 0x2;
744 break;
745 default:
746 assert(0);
747 break;
748 }
749
750 if (manual_stride)
751 usage2_layout |= MALI_TEX_MANUAL_STRIDE;
752
753 return usage2_layout;
754 }
755
756 static mali_ptr
757 panfrost_upload_tex(
758 struct panfrost_context *ctx,
759 struct panfrost_sampler_view *view)
760 {
761 if (!view)
762 return (mali_ptr) 0;
763
764 struct pipe_sampler_view *pview = &view->base;
765 struct panfrost_resource *rsrc = pan_resource(pview->texture);
766
767 /* Do we interleave an explicit stride with every element? */
768
769 bool has_manual_stride = view->manual_stride;
770
771 /* For easy access */
772
773 assert(pview->target != PIPE_BUFFER);
774 unsigned first_level = pview->u.tex.first_level;
775 unsigned last_level = pview->u.tex.last_level;
776 unsigned first_layer = pview->u.tex.first_layer;
777 unsigned last_layer = pview->u.tex.last_layer;
778
779 /* Lower-bit is set when sampling from colour AFBC */
780 bool is_afbc = rsrc->layout == PAN_AFBC;
781 bool is_zs = rsrc->base.bind & PIPE_BIND_DEPTH_STENCIL;
782 unsigned afbc_bit = (is_afbc && !is_zs) ? 1 : 0;
783
784 /* Add the BO to the job so it's retained until the job is done. */
785 struct panfrost_job *job = panfrost_get_job_for_fbo(ctx);
786 panfrost_job_add_bo(job, rsrc->bo);
787
788 /* Add the usage flags in, since they can change across the CSO
789 * lifetime due to layout switches */
790
791 view->hw.format.usage2 = panfrost_layout_for_texture(rsrc, has_manual_stride);
792
793 /* Inject the addresses in, interleaving mip levels, cube faces, and
794 * strides in that order */
795
796 unsigned idx = 0;
797
798 for (unsigned l = first_level; l <= last_level; ++l) {
799 for (unsigned f = first_layer; f <= last_layer; ++f) {
800
801 view->hw.payload[idx++] =
802 panfrost_get_texture_address(rsrc, l, f) + afbc_bit;
803
804 if (has_manual_stride) {
805 view->hw.payload[idx++] =
806 rsrc->slices[l].stride;
807 }
808 }
809 }
810
811 return panfrost_upload_transient(ctx, &view->hw,
812 sizeof(struct mali_texture_descriptor));
813 }
814
815 static void
816 panfrost_upload_texture_descriptors(struct panfrost_context *ctx)
817 {
818 for (int t = 0; t <= PIPE_SHADER_FRAGMENT; ++t) {
819 mali_ptr trampoline = 0;
820
821 if (ctx->sampler_view_count[t]) {
822 uint64_t trampolines[PIPE_MAX_SHADER_SAMPLER_VIEWS];
823
824 for (int i = 0; i < ctx->sampler_view_count[t]; ++i)
825 trampolines[i] =
826 panfrost_upload_tex(ctx, ctx->sampler_views[t][i]);
827
828 trampoline = panfrost_upload_transient(ctx, trampolines, sizeof(uint64_t) * ctx->sampler_view_count[t]);
829 }
830
831 ctx->payloads[t].postfix.texture_trampoline = trampoline;
832 }
833 }
834
835 struct sysval_uniform {
836 union {
837 float f[4];
838 int32_t i[4];
839 uint32_t u[4];
840 };
841 };
842
843 static void panfrost_upload_viewport_scale_sysval(struct panfrost_context *ctx,
844 struct sysval_uniform *uniform)
845 {
846 const struct pipe_viewport_state *vp = &ctx->pipe_viewport;
847
848 uniform->f[0] = vp->scale[0];
849 uniform->f[1] = vp->scale[1];
850 uniform->f[2] = vp->scale[2];
851 }
852
853 static void panfrost_upload_viewport_offset_sysval(struct panfrost_context *ctx,
854 struct sysval_uniform *uniform)
855 {
856 const struct pipe_viewport_state *vp = &ctx->pipe_viewport;
857
858 uniform->f[0] = vp->translate[0];
859 uniform->f[1] = vp->translate[1];
860 uniform->f[2] = vp->translate[2];
861 }
862
863 static void panfrost_upload_txs_sysval(struct panfrost_context *ctx,
864 enum pipe_shader_type st,
865 unsigned int sysvalid,
866 struct sysval_uniform *uniform)
867 {
868 unsigned texidx = PAN_SYSVAL_ID_TO_TXS_TEX_IDX(sysvalid);
869 unsigned dim = PAN_SYSVAL_ID_TO_TXS_DIM(sysvalid);
870 bool is_array = PAN_SYSVAL_ID_TO_TXS_IS_ARRAY(sysvalid);
871 struct pipe_sampler_view *tex = &ctx->sampler_views[st][texidx]->base;
872
873 assert(dim);
874 uniform->i[0] = u_minify(tex->texture->width0, tex->u.tex.first_level);
875
876 if (dim > 1)
877 uniform->i[1] = u_minify(tex->texture->height0,
878 tex->u.tex.first_level);
879
880 if (dim > 2)
881 uniform->i[2] = u_minify(tex->texture->depth0,
882 tex->u.tex.first_level);
883
884 if (is_array)
885 uniform->i[dim] = tex->texture->array_size;
886 }
887
888 static void panfrost_upload_sysvals(struct panfrost_context *ctx, void *buf,
889 struct panfrost_shader_state *ss,
890 enum pipe_shader_type st)
891 {
892 struct sysval_uniform *uniforms = (void *)buf;
893
894 for (unsigned i = 0; i < ss->sysval_count; ++i) {
895 int sysval = ss->sysval[i];
896
897 switch (PAN_SYSVAL_TYPE(sysval)) {
898 case PAN_SYSVAL_VIEWPORT_SCALE:
899 panfrost_upload_viewport_scale_sysval(ctx, &uniforms[i]);
900 break;
901 case PAN_SYSVAL_VIEWPORT_OFFSET:
902 panfrost_upload_viewport_offset_sysval(ctx, &uniforms[i]);
903 break;
904 case PAN_SYSVAL_TEXTURE_SIZE:
905 panfrost_upload_txs_sysval(ctx, st, PAN_SYSVAL_ID(sysval),
906 &uniforms[i]);
907 break;
908 default:
909 assert(0);
910 }
911 }
912 }
913
914 static const void *
915 panfrost_map_constant_buffer_cpu(struct panfrost_constant_buffer *buf, unsigned index)
916 {
917 struct pipe_constant_buffer *cb = &buf->cb[index];
918 struct panfrost_resource *rsrc = pan_resource(cb->buffer);
919
920 if (rsrc)
921 return rsrc->bo->cpu;
922 else if (cb->user_buffer)
923 return cb->user_buffer;
924 else
925 unreachable("No constant buffer");
926 }
927
928 static mali_ptr
929 panfrost_map_constant_buffer_gpu(
930 struct panfrost_context *ctx,
931 struct panfrost_constant_buffer *buf,
932 unsigned index)
933 {
934 struct pipe_constant_buffer *cb = &buf->cb[index];
935 struct panfrost_resource *rsrc = pan_resource(cb->buffer);
936
937 if (rsrc)
938 return rsrc->bo->gpu;
939 else if (cb->user_buffer)
940 return panfrost_upload_transient(ctx, cb->user_buffer, cb->buffer_size);
941 else
942 unreachable("No constant buffer");
943 }
944
945 /* Compute number of UBOs active (more specifically, compute the highest UBO
946 * number addressable -- if there are gaps, include them in the count anyway).
947 * We always include UBO #0 in the count, since we *need* uniforms enabled for
948 * sysvals. */
949
950 static unsigned
951 panfrost_ubo_count(struct panfrost_context *ctx, enum pipe_shader_type stage)
952 {
953 unsigned mask = ctx->constant_buffer[stage].enabled_mask | 1;
954 return 32 - __builtin_clz(mask);
955 }
956
957 /* Fixes up a shader state with current state, returning a GPU address to the
958 * patched shader */
959
960 static mali_ptr
961 panfrost_patch_shader_state(
962 struct panfrost_context *ctx,
963 struct panfrost_shader_state *ss,
964 enum pipe_shader_type stage,
965 bool should_upload)
966 {
967 ss->tripipe->texture_count = ctx->sampler_view_count[stage];
968 ss->tripipe->sampler_count = ctx->sampler_count[stage];
969
970 ss->tripipe->midgard1.flags = 0x220;
971
972 unsigned ubo_count = panfrost_ubo_count(ctx, stage);
973 ss->tripipe->midgard1.uniform_buffer_count = ubo_count;
974
975 /* We can't reuse over frames; that's not safe. The descriptor must be
976 * transient uploaded */
977
978 if (should_upload) {
979 return panfrost_upload_transient(ctx,
980 ss->tripipe,
981 sizeof(struct mali_shader_meta));
982 }
983
984 /* If we don't need an upload, don't bother */
985 return 0;
986
987 }
988
989 static void
990 panfrost_patch_shader_state_compute(
991 struct panfrost_context *ctx,
992 enum pipe_shader_type stage,
993 bool should_upload)
994 {
995 struct panfrost_shader_variants *all = ctx->shader[stage];
996
997 if (!all) {
998 ctx->payloads[stage].postfix._shader_upper = 0;
999 return;
1000 }
1001
1002 struct panfrost_shader_state *s = &all->variants[all->active_variant];
1003
1004 ctx->payloads[stage].postfix._shader_upper =
1005 panfrost_patch_shader_state(ctx, s, stage, should_upload) >> 4;
1006 }
1007
1008 /* Go through dirty flags and actualise them in the cmdstream. */
1009
1010 void
1011 panfrost_emit_for_draw(struct panfrost_context *ctx, bool with_vertex_data)
1012 {
1013 struct panfrost_job *job = panfrost_get_job_for_fbo(ctx);
1014 struct panfrost_screen *screen = pan_screen(ctx->base.screen);
1015
1016 panfrost_attach_vt_framebuffer(ctx, true);
1017
1018 if (with_vertex_data) {
1019 panfrost_emit_vertex_data(job);
1020
1021 /* Varyings emitted for -all- geometry */
1022 unsigned total_count = ctx->padded_count * ctx->instance_count;
1023 panfrost_emit_varying_descriptor(ctx, total_count);
1024 }
1025
1026 bool msaa = ctx->rasterizer->base.multisample;
1027
1028 if (ctx->dirty & PAN_DIRTY_RASTERIZER) {
1029 ctx->payloads[PIPE_SHADER_FRAGMENT].gl_enables = ctx->rasterizer->tiler_gl_enables;
1030
1031 /* TODO: Sample size */
1032 SET_BIT(ctx->fragment_shader_core.unknown2_3, MALI_HAS_MSAA, msaa);
1033 SET_BIT(ctx->fragment_shader_core.unknown2_4, MALI_NO_MSAA, !msaa);
1034 }
1035
1036 panfrost_job_set_requirements(ctx, job);
1037
1038 if (ctx->occlusion_query) {
1039 ctx->payloads[PIPE_SHADER_FRAGMENT].gl_enables |= MALI_OCCLUSION_QUERY | MALI_OCCLUSION_PRECISE;
1040 ctx->payloads[PIPE_SHADER_FRAGMENT].postfix.occlusion_counter = ctx->occlusion_query->transfer.gpu;
1041 }
1042
1043 panfrost_patch_shader_state_compute(ctx, PIPE_SHADER_VERTEX, true);
1044 panfrost_patch_shader_state_compute(ctx, PIPE_SHADER_COMPUTE, true);
1045
1046 if (ctx->dirty & (PAN_DIRTY_RASTERIZER | PAN_DIRTY_VS)) {
1047 /* Check if we need to link the gl_PointSize varying */
1048 if (!panfrost_writes_point_size(ctx)) {
1049 /* If the size is constant, write it out. Otherwise,
1050 * don't touch primitive_size (since we would clobber
1051 * the pointer there) */
1052
1053 ctx->payloads[PIPE_SHADER_FRAGMENT].primitive_size.constant = ctx->rasterizer->base.line_width;
1054 }
1055 }
1056
1057 /* TODO: Maybe dirty track FS, maybe not. For now, it's transient. */
1058 if (ctx->shader[PIPE_SHADER_FRAGMENT])
1059 ctx->dirty |= PAN_DIRTY_FS;
1060
1061 if (ctx->dirty & PAN_DIRTY_FS) {
1062 assert(ctx->shader[PIPE_SHADER_FRAGMENT]);
1063 struct panfrost_shader_state *variant = &ctx->shader[PIPE_SHADER_FRAGMENT]->variants[ctx->shader[PIPE_SHADER_FRAGMENT]->active_variant];
1064
1065 panfrost_patch_shader_state(ctx, variant, PIPE_SHADER_FRAGMENT, false);
1066
1067 #define COPY(name) ctx->fragment_shader_core.name = variant->tripipe->name
1068
1069 COPY(shader);
1070 COPY(attribute_count);
1071 COPY(varying_count);
1072 COPY(texture_count);
1073 COPY(sampler_count);
1074 COPY(sampler_count);
1075 COPY(midgard1.uniform_count);
1076 COPY(midgard1.uniform_buffer_count);
1077 COPY(midgard1.work_count);
1078 COPY(midgard1.flags);
1079 COPY(midgard1.unknown2);
1080
1081 #undef COPY
1082
1083 /* Get blending setup */
1084 struct panfrost_blend_final blend =
1085 panfrost_get_blend_for_context(ctx, 0);
1086
1087 /* If there is a blend shader, work registers are shared */
1088
1089 if (blend.is_shader)
1090 ctx->fragment_shader_core.midgard1.work_count = /*MAX2(ctx->fragment_shader_core.midgard1.work_count, ctx->blend->blend_work_count)*/16;
1091
1092 /* Set late due to depending on render state */
1093 unsigned flags = ctx->fragment_shader_core.midgard1.flags;
1094
1095 /* Depending on whether it's legal to in the given shader, we
1096 * try to enable early-z testing (or forward-pixel kill?) */
1097
1098 if (!variant->can_discard)
1099 flags |= MALI_EARLY_Z;
1100
1101 /* Any time texturing is used, derivatives are implicitly
1102 * calculated, so we need to enable helper invocations */
1103
1104 if (variant->helper_invocations)
1105 flags |= MALI_HELPER_INVOCATIONS;
1106
1107 ctx->fragment_shader_core.midgard1.flags = flags;
1108
1109 /* Assign the stencil refs late */
1110
1111 unsigned front_ref = ctx->stencil_ref.ref_value[0];
1112 unsigned back_ref = ctx->stencil_ref.ref_value[1];
1113 bool back_enab = ctx->depth_stencil->stencil[1].enabled;
1114
1115 ctx->fragment_shader_core.stencil_front.ref = front_ref;
1116 ctx->fragment_shader_core.stencil_back.ref = back_enab ? back_ref : front_ref;
1117
1118 /* CAN_DISCARD should be set if the fragment shader possibly
1119 * contains a 'discard' instruction. It is likely this is
1120 * related to optimizations related to forward-pixel kill, as
1121 * per "Mali Performance 3: Is EGL_BUFFER_PRESERVED a good
1122 * thing?" by Peter Harris
1123 */
1124
1125 if (variant->can_discard) {
1126 ctx->fragment_shader_core.unknown2_3 |= MALI_CAN_DISCARD;
1127 ctx->fragment_shader_core.midgard1.flags |= 0x400;
1128 }
1129
1130 /* Check if we're using the default blend descriptor (fast path) */
1131
1132 bool no_blending =
1133 !blend.is_shader &&
1134 (blend.equation.equation->rgb_mode == 0x122) &&
1135 (blend.equation.equation->alpha_mode == 0x122) &&
1136 (blend.equation.equation->color_mask == 0xf);
1137
1138 /* Even on MFBD, the shader descriptor gets blend shaders. It's
1139 * *also* copied to the blend_meta appended (by convention),
1140 * but this is the field actually read by the hardware. (Or
1141 * maybe both are read...?) */
1142
1143 if (blend.is_shader) {
1144 ctx->fragment_shader_core.blend.shader =
1145 blend.shader.gpu;
1146 } else {
1147 ctx->fragment_shader_core.blend.shader = 0;
1148 }
1149
1150 if (screen->require_sfbd) {
1151 /* When only a single render target platform is used, the blend
1152 * information is inside the shader meta itself. We
1153 * additionally need to signal CAN_DISCARD for nontrivial blend
1154 * modes (so we're able to read back the destination buffer) */
1155
1156 if (!blend.is_shader) {
1157 ctx->fragment_shader_core.blend.equation =
1158 *blend.equation.equation;
1159 ctx->fragment_shader_core.blend.constant =
1160 blend.equation.constant;
1161 }
1162
1163 if (!no_blending) {
1164 ctx->fragment_shader_core.unknown2_3 |= MALI_CAN_DISCARD;
1165 }
1166 }
1167
1168 size_t size = sizeof(struct mali_shader_meta) + sizeof(struct midgard_blend_rt);
1169 struct panfrost_transfer transfer = panfrost_allocate_transient(ctx, size);
1170 memcpy(transfer.cpu, &ctx->fragment_shader_core, sizeof(struct mali_shader_meta));
1171
1172 ctx->payloads[PIPE_SHADER_FRAGMENT].postfix._shader_upper = (transfer.gpu) >> 4;
1173
1174 if (!screen->require_sfbd) {
1175 /* Additional blend descriptor tacked on for jobs using MFBD */
1176
1177 unsigned blend_count = 0x200;
1178
1179 if (blend.is_shader) {
1180 /* For a blend shader, the bottom nibble corresponds to
1181 * the number of work registers used, which signals the
1182 * -existence- of a blend shader */
1183
1184 assert(blend.shader.work_count >= 2);
1185 blend_count |= MIN2(blend.shader.work_count, 3);
1186 } else {
1187 /* Otherwise, the bottom bit simply specifies if
1188 * blending (anything other than REPLACE) is enabled */
1189
1190
1191 if (!no_blending)
1192 blend_count |= 0x1;
1193 }
1194
1195 struct midgard_blend_rt rts[4];
1196
1197 for (unsigned i = 0; i < ctx->pipe_framebuffer.nr_cbufs; ++i) {
1198 bool is_srgb =
1199 (ctx->pipe_framebuffer.nr_cbufs > i) &&
1200 (ctx->pipe_framebuffer.cbufs[i]) &&
1201 util_format_is_srgb(ctx->pipe_framebuffer.cbufs[i]->format);
1202
1203 rts[i].flags = blend_count;
1204
1205 if (is_srgb)
1206 rts[i].flags |= MALI_BLEND_SRGB;
1207
1208 if (!ctx->blend->base.dither)
1209 rts[i].flags |= MALI_BLEND_NO_DITHER;
1210
1211 /* TODO: sRGB in blend shaders is currently
1212 * unimplemented. Contact me (Alyssa) if you're
1213 * interested in working on this. We have
1214 * native Midgard ops for helping here, but
1215 * they're not well-understood yet. */
1216
1217 assert(!(is_srgb && blend.is_shader));
1218
1219 if (blend.is_shader) {
1220 rts[i].blend.shader = blend.shader.gpu;
1221 } else {
1222 rts[i].blend.equation = *blend.equation.equation;
1223 rts[i].blend.constant = blend.equation.constant;
1224 }
1225 }
1226
1227 memcpy(transfer.cpu + sizeof(struct mali_shader_meta), rts, sizeof(rts[0]) * 1);
1228 }
1229 }
1230
1231 /* We stage to transient, so always dirty.. */
1232 if (ctx->vertex)
1233 panfrost_stage_attributes(ctx);
1234
1235 if (ctx->dirty & PAN_DIRTY_SAMPLERS)
1236 panfrost_upload_sampler_descriptors(ctx);
1237
1238 if (ctx->dirty & PAN_DIRTY_TEXTURES)
1239 panfrost_upload_texture_descriptors(ctx);
1240
1241 const struct pipe_viewport_state *vp = &ctx->pipe_viewport;
1242
1243 for (int i = 0; i < PIPE_SHADER_TYPES; ++i) {
1244 struct panfrost_shader_variants *all = ctx->shader[i];
1245
1246 if (!all)
1247 continue;
1248
1249 struct panfrost_constant_buffer *buf = &ctx->constant_buffer[i];
1250
1251 struct panfrost_shader_state *ss = &all->variants[all->active_variant];
1252
1253 /* Uniforms are implicitly UBO #0 */
1254 bool has_uniforms = buf->enabled_mask & (1 << 0);
1255
1256 /* Allocate room for the sysval and the uniforms */
1257 size_t sys_size = sizeof(float) * 4 * ss->sysval_count;
1258 size_t uniform_size = has_uniforms ? (buf->cb[0].buffer_size) : 0;
1259 size_t size = sys_size + uniform_size;
1260 struct panfrost_transfer transfer = panfrost_allocate_transient(ctx, size);
1261
1262 /* Upload sysvals requested by the shader */
1263 panfrost_upload_sysvals(ctx, transfer.cpu, ss, i);
1264
1265 /* Upload uniforms */
1266 if (has_uniforms) {
1267 const void *cpu = panfrost_map_constant_buffer_cpu(buf, 0);
1268 memcpy(transfer.cpu + sys_size, cpu, uniform_size);
1269 }
1270
1271 int uniform_count =
1272 ctx->shader[i]->variants[ctx->shader[i]->active_variant].uniform_count;
1273
1274 struct mali_vertex_tiler_postfix *postfix =
1275 &ctx->payloads[i].postfix;
1276
1277 /* Next up, attach UBOs. UBO #0 is the uniforms we just
1278 * uploaded */
1279
1280 unsigned ubo_count = panfrost_ubo_count(ctx, i);
1281 assert(ubo_count >= 1);
1282
1283 size_t sz = sizeof(struct mali_uniform_buffer_meta) * ubo_count;
1284 struct mali_uniform_buffer_meta ubos[PAN_MAX_CONST_BUFFERS];
1285
1286 /* Upload uniforms as a UBO */
1287 ubos[0].size = MALI_POSITIVE((2 + uniform_count));
1288 ubos[0].ptr = transfer.gpu >> 2;
1289
1290 /* The rest are honest-to-goodness UBOs */
1291
1292 for (unsigned ubo = 1; ubo < ubo_count; ++ubo) {
1293 size_t sz = buf->cb[ubo].buffer_size;
1294
1295 bool enabled = buf->enabled_mask & (1 << ubo);
1296 bool empty = sz == 0;
1297
1298 if (!enabled || empty) {
1299 /* Stub out disabled UBOs to catch accesses */
1300
1301 ubos[ubo].size = 0;
1302 ubos[ubo].ptr = 0xDEAD0000;
1303 continue;
1304 }
1305
1306 mali_ptr gpu = panfrost_map_constant_buffer_gpu(ctx, buf, ubo);
1307
1308 unsigned bytes_per_field = 16;
1309 unsigned aligned = ALIGN_POT(sz, bytes_per_field);
1310 unsigned fields = aligned / bytes_per_field;
1311
1312 ubos[ubo].size = MALI_POSITIVE(fields);
1313 ubos[ubo].ptr = gpu >> 2;
1314 }
1315
1316 mali_ptr ubufs = panfrost_upload_transient(ctx, ubos, sz);
1317 postfix->uniforms = transfer.gpu;
1318 postfix->uniform_buffers = ubufs;
1319
1320 buf->dirty_mask = 0;
1321 }
1322
1323 /* TODO: Upload the viewport somewhere more appropriate */
1324
1325 /* Clip bounds are encoded as floats. The viewport itself is encoded as
1326 * (somewhat) asymmetric ints. */
1327 const struct pipe_scissor_state *ss = &ctx->scissor;
1328
1329 struct mali_viewport view = {
1330 /* By default, do no viewport clipping, i.e. clip to (-inf,
1331 * inf) in each direction. Clipping to the viewport in theory
1332 * should work, but in practice causes issues when we're not
1333 * explicitly trying to scissor */
1334
1335 .clip_minx = -INFINITY,
1336 .clip_miny = -INFINITY,
1337 .clip_maxx = INFINITY,
1338 .clip_maxy = INFINITY,
1339
1340 .clip_minz = 0.0,
1341 .clip_maxz = 1.0,
1342 };
1343
1344 /* Always scissor to the viewport by default. */
1345 float vp_minx = (int) (vp->translate[0] - fabsf(vp->scale[0]));
1346 float vp_maxx = (int) (vp->translate[0] + fabsf(vp->scale[0]));
1347
1348 float vp_miny = (int) (vp->translate[1] - fabsf(vp->scale[1]));
1349 float vp_maxy = (int) (vp->translate[1] + fabsf(vp->scale[1]));
1350
1351 /* Apply the scissor test */
1352
1353 unsigned minx, miny, maxx, maxy;
1354
1355 if (ss && ctx->rasterizer && ctx->rasterizer->base.scissor) {
1356 minx = MAX2(ss->minx, vp_minx);
1357 miny = MAX2(ss->miny, vp_miny);
1358 maxx = MIN2(ss->maxx, vp_maxx);
1359 maxy = MIN2(ss->maxy, vp_maxy);
1360 } else {
1361 minx = vp_minx;
1362 miny = vp_miny;
1363 maxx = vp_maxx;
1364 maxy = vp_maxy;
1365 }
1366
1367 /* Hardware needs the min/max to be strictly ordered, so flip if we
1368 * need to. The viewport transformation in the vertex shader will
1369 * handle the negatives if we don't */
1370
1371 if (miny > maxy) {
1372 int temp = miny;
1373 miny = maxy;
1374 maxy = temp;
1375 }
1376
1377 if (minx > maxx) {
1378 int temp = minx;
1379 minx = maxx;
1380 maxx = temp;
1381 }
1382
1383 /* Clamp everything positive, just in case */
1384
1385 maxx = MAX2(0, maxx);
1386 maxy = MAX2(0, maxy);
1387 minx = MAX2(0, minx);
1388 miny = MAX2(0, miny);
1389
1390 /* Clamp to the framebuffer size as a last check */
1391
1392 minx = MIN2(ctx->pipe_framebuffer.width, minx);
1393 maxx = MIN2(ctx->pipe_framebuffer.width, maxx);
1394
1395 miny = MIN2(ctx->pipe_framebuffer.height, miny);
1396 maxy = MIN2(ctx->pipe_framebuffer.height, maxy);
1397
1398 /* Update the job, unless we're doing wallpapering (whose lack of
1399 * scissor we can ignore, since if we "miss" a tile of wallpaper, it'll
1400 * just... be faster :) */
1401
1402 if (!ctx->wallpaper_batch)
1403 panfrost_job_union_scissor(job, minx, miny, maxx, maxy);
1404
1405 /* Upload */
1406
1407 view.viewport0[0] = minx;
1408 view.viewport1[0] = MALI_POSITIVE(maxx);
1409
1410 view.viewport0[1] = miny;
1411 view.viewport1[1] = MALI_POSITIVE(maxy);
1412
1413 ctx->payloads[PIPE_SHADER_FRAGMENT].postfix.viewport =
1414 panfrost_upload_transient(ctx,
1415 &view,
1416 sizeof(struct mali_viewport));
1417
1418 ctx->dirty = 0;
1419 }
1420
1421 /* Corresponds to exactly one draw, but does not submit anything */
1422
1423 static void
1424 panfrost_queue_draw(struct panfrost_context *ctx)
1425 {
1426 /* Handle dirty flags now */
1427 panfrost_emit_for_draw(ctx, true);
1428
1429 /* If rasterizer discard is enable, only submit the vertex */
1430
1431 bool rasterizer_discard = ctx->rasterizer
1432 && ctx->rasterizer->base.rasterizer_discard;
1433
1434 struct panfrost_transfer vertex = panfrost_vertex_tiler_job(ctx, false);
1435 struct panfrost_transfer tiler;
1436
1437 if (!rasterizer_discard)
1438 tiler = panfrost_vertex_tiler_job(ctx, true);
1439
1440 struct panfrost_job *batch = panfrost_get_job_for_fbo(ctx);
1441
1442 if (rasterizer_discard)
1443 panfrost_scoreboard_queue_vertex_job(batch, vertex, FALSE);
1444 else if (ctx->wallpaper_batch)
1445 panfrost_scoreboard_queue_fused_job_prepend(batch, vertex, tiler);
1446 else
1447 panfrost_scoreboard_queue_fused_job(batch, vertex, tiler);
1448 }
1449
1450 /* The entire frame is in memory -- send it off to the kernel! */
1451
1452 static void
1453 panfrost_submit_frame(struct panfrost_context *ctx, bool flush_immediate,
1454 struct pipe_fence_handle **fence,
1455 struct panfrost_job *job)
1456 {
1457 struct pipe_context *gallium = (struct pipe_context *) ctx;
1458 struct panfrost_screen *screen = pan_screen(gallium->screen);
1459
1460 #ifndef DRY_RUN
1461
1462 panfrost_job_submit(ctx, job);
1463
1464 /* If visual, we can stall a frame */
1465
1466 if (!flush_immediate)
1467 panfrost_drm_force_flush_fragment(ctx, fence);
1468
1469 screen->last_fragment_flushed = false;
1470 screen->last_job = job;
1471
1472 /* If readback, flush now (hurts the pipelined performance) */
1473 if (flush_immediate)
1474 panfrost_drm_force_flush_fragment(ctx, fence);
1475 #endif
1476 }
1477
1478 static void
1479 panfrost_draw_wallpaper(struct pipe_context *pipe)
1480 {
1481 struct panfrost_context *ctx = pan_context(pipe);
1482
1483 /* Nothing to reload? TODO: MRT wallpapers */
1484 if (ctx->pipe_framebuffer.cbufs[0] == NULL)
1485 return;
1486
1487 /* Check if the buffer has any content on it worth preserving */
1488
1489 struct pipe_surface *surf = ctx->pipe_framebuffer.cbufs[0];
1490 struct panfrost_resource *rsrc = pan_resource(surf->texture);
1491 unsigned level = surf->u.tex.level;
1492
1493 if (!rsrc->slices[level].initialized)
1494 return;
1495
1496 /* Save the batch */
1497 struct panfrost_job *batch = panfrost_get_job_for_fbo(ctx);
1498
1499 ctx->wallpaper_batch = batch;
1500 panfrost_blit_wallpaper(ctx);
1501 ctx->wallpaper_batch = NULL;
1502 }
1503
1504 void
1505 panfrost_flush(
1506 struct pipe_context *pipe,
1507 struct pipe_fence_handle **fence,
1508 unsigned flags)
1509 {
1510 struct panfrost_context *ctx = pan_context(pipe);
1511 struct panfrost_job *job = panfrost_get_job_for_fbo(ctx);
1512
1513 /* Nothing to do! */
1514 if (!job->last_job.gpu && !job->clear) return;
1515
1516 if (!job->clear && job->last_tiler.gpu)
1517 panfrost_draw_wallpaper(&ctx->base);
1518
1519 /* Whether to stall the pipeline for immediately correct results. Since
1520 * pipelined rendering is quite broken right now (to be fixed by the
1521 * panfrost_job refactor, just take the perf hit for correctness) */
1522 bool flush_immediate = /*flags & PIPE_FLUSH_END_OF_FRAME*/true;
1523
1524 /* Submit the frame itself */
1525 panfrost_submit_frame(ctx, flush_immediate, fence, job);
1526
1527 /* Prepare for the next frame */
1528 panfrost_invalidate_frame(ctx);
1529 }
1530
1531 #define DEFINE_CASE(c) case PIPE_PRIM_##c: return MALI_##c;
1532
1533 static int
1534 g2m_draw_mode(enum pipe_prim_type mode)
1535 {
1536 switch (mode) {
1537 DEFINE_CASE(POINTS);
1538 DEFINE_CASE(LINES);
1539 DEFINE_CASE(LINE_LOOP);
1540 DEFINE_CASE(LINE_STRIP);
1541 DEFINE_CASE(TRIANGLES);
1542 DEFINE_CASE(TRIANGLE_STRIP);
1543 DEFINE_CASE(TRIANGLE_FAN);
1544 DEFINE_CASE(QUADS);
1545 DEFINE_CASE(QUAD_STRIP);
1546 DEFINE_CASE(POLYGON);
1547
1548 default:
1549 unreachable("Invalid draw mode");
1550 }
1551 }
1552
1553 #undef DEFINE_CASE
1554
1555 static unsigned
1556 panfrost_translate_index_size(unsigned size)
1557 {
1558 switch (size) {
1559 case 1:
1560 return MALI_DRAW_INDEXED_UINT8;
1561
1562 case 2:
1563 return MALI_DRAW_INDEXED_UINT16;
1564
1565 case 4:
1566 return MALI_DRAW_INDEXED_UINT32;
1567
1568 default:
1569 unreachable("Invalid index size");
1570 }
1571 }
1572
1573 /* Gets a GPU address for the associated index buffer. Only gauranteed to be
1574 * good for the duration of the draw (transient), could last longer */
1575
1576 static mali_ptr
1577 panfrost_get_index_buffer_mapped(struct panfrost_context *ctx, const struct pipe_draw_info *info)
1578 {
1579 struct panfrost_resource *rsrc = (struct panfrost_resource *) (info->index.resource);
1580
1581 off_t offset = info->start * info->index_size;
1582 struct panfrost_job *batch = panfrost_get_job_for_fbo(ctx);
1583
1584 if (!info->has_user_indices) {
1585 /* Only resources can be directly mapped */
1586 panfrost_job_add_bo(batch, rsrc->bo);
1587 return rsrc->bo->gpu + offset;
1588 } else {
1589 /* Otherwise, we need to upload to transient memory */
1590 const uint8_t *ibuf8 = (const uint8_t *) info->index.user;
1591 return panfrost_upload_transient(ctx, ibuf8 + offset, info->count * info->index_size);
1592 }
1593 }
1594
1595 static bool
1596 panfrost_scissor_culls_everything(struct panfrost_context *ctx)
1597 {
1598 const struct pipe_scissor_state *ss = &ctx->scissor;
1599
1600 /* Check if we're scissoring at all */
1601
1602 if (!(ctx->rasterizer && ctx->rasterizer->base.scissor))
1603 return false;
1604
1605 return (ss->minx == ss->maxx) || (ss->miny == ss->maxy);
1606 }
1607
1608 static void
1609 panfrost_draw_vbo(
1610 struct pipe_context *pipe,
1611 const struct pipe_draw_info *info)
1612 {
1613 struct panfrost_context *ctx = pan_context(pipe);
1614
1615 /* First of all, check the scissor to see if anything is drawn at all.
1616 * If it's not, we drop the draw (mostly a conformance issue;
1617 * well-behaved apps shouldn't hit this) */
1618
1619 if (panfrost_scissor_culls_everything(ctx))
1620 return;
1621
1622 ctx->payloads[PIPE_SHADER_VERTEX].draw_start = info->start;
1623 ctx->payloads[PIPE_SHADER_FRAGMENT].draw_start = info->start;
1624
1625 int mode = info->mode;
1626
1627 /* Fallback unsupported restart index */
1628 unsigned primitive_index = (1 << (info->index_size * 8)) - 1;
1629
1630 if (info->primitive_restart && info->index_size
1631 && info->restart_index != primitive_index) {
1632 util_draw_vbo_without_prim_restart(pipe, info);
1633 return;
1634 }
1635
1636 /* Fallback for unsupported modes */
1637
1638 if (!(ctx->draw_modes & (1 << mode))) {
1639 if (mode == PIPE_PRIM_QUADS && info->count == 4 && ctx->rasterizer && !ctx->rasterizer->base.flatshade) {
1640 mode = PIPE_PRIM_TRIANGLE_FAN;
1641 } else {
1642 if (info->count < 4) {
1643 /* Degenerate case? */
1644 return;
1645 }
1646
1647 util_primconvert_save_rasterizer_state(ctx->primconvert, &ctx->rasterizer->base);
1648 util_primconvert_draw_vbo(ctx->primconvert, info);
1649 return;
1650 }
1651 }
1652
1653 /* Now that we have a guaranteed terminating path, find the job.
1654 * Assignment commented out to prevent unused warning */
1655
1656 /* struct panfrost_job *job = */ panfrost_get_job_for_fbo(ctx);
1657
1658 ctx->payloads[PIPE_SHADER_FRAGMENT].prefix.draw_mode = g2m_draw_mode(mode);
1659
1660 ctx->vertex_count = info->count;
1661 ctx->instance_count = info->instance_count;
1662
1663 /* For non-indexed draws, they're the same */
1664 unsigned vertex_count = ctx->vertex_count;
1665
1666 unsigned draw_flags = 0;
1667
1668 /* The draw flags interpret how primitive size is interpreted */
1669
1670 if (panfrost_writes_point_size(ctx))
1671 draw_flags |= MALI_DRAW_VARYING_SIZE;
1672
1673 if (info->primitive_restart)
1674 draw_flags |= MALI_DRAW_PRIMITIVE_RESTART_FIXED_INDEX;
1675
1676 /* For higher amounts of vertices (greater than what fits in a 16-bit
1677 * short), the other value is needed, otherwise there will be bizarre
1678 * rendering artefacts. It's not clear what these values mean yet. This
1679 * change is also needed for instancing and sometimes points (perhaps
1680 * related to dynamically setting gl_PointSize) */
1681
1682 bool is_points = mode == PIPE_PRIM_POINTS;
1683 bool many_verts = ctx->vertex_count > 0xFFFF;
1684 bool instanced = ctx->instance_count > 1;
1685
1686 draw_flags |= (is_points || many_verts || instanced) ? 0x3000 : 0x18000;
1687
1688 /* This doesn't make much sense */
1689 if (mode == PIPE_PRIM_LINE_STRIP) {
1690 draw_flags |= 0x800;
1691 }
1692
1693 if (info->index_size) {
1694 /* Calculate the min/max index used so we can figure out how
1695 * many times to invoke the vertex shader */
1696
1697 /* Fetch / calculate index bounds */
1698 unsigned min_index = 0, max_index = 0;
1699
1700 if (info->max_index == ~0u) {
1701 u_vbuf_get_minmax_index(pipe, info, &min_index, &max_index);
1702 } else {
1703 min_index = info->min_index;
1704 max_index = info->max_index;
1705 }
1706
1707 /* Use the corresponding values */
1708 vertex_count = max_index - min_index + 1;
1709 ctx->payloads[PIPE_SHADER_VERTEX].draw_start = min_index;
1710 ctx->payloads[PIPE_SHADER_FRAGMENT].draw_start = min_index;
1711
1712 ctx->payloads[PIPE_SHADER_FRAGMENT].prefix.negative_start = -min_index;
1713 ctx->payloads[PIPE_SHADER_FRAGMENT].prefix.index_count = MALI_POSITIVE(info->count);
1714
1715 //assert(!info->restart_index); /* TODO: Research */
1716 assert(!info->index_bias);
1717
1718 draw_flags |= panfrost_translate_index_size(info->index_size);
1719 ctx->payloads[PIPE_SHADER_FRAGMENT].prefix.indices = panfrost_get_index_buffer_mapped(ctx, info);
1720 } else {
1721 /* Index count == vertex count, if no indexing is applied, as
1722 * if it is internally indexed in the expected order */
1723
1724 ctx->payloads[PIPE_SHADER_FRAGMENT].prefix.negative_start = 0;
1725 ctx->payloads[PIPE_SHADER_FRAGMENT].prefix.index_count = MALI_POSITIVE(ctx->vertex_count);
1726
1727 /* Reverse index state */
1728 ctx->payloads[PIPE_SHADER_FRAGMENT].prefix.indices = (u64) NULL;
1729 }
1730
1731 /* Dispatch "compute jobs" for the vertex/tiler pair as (1,
1732 * vertex_count, 1) */
1733
1734 panfrost_pack_work_groups_fused(
1735 &ctx->payloads[PIPE_SHADER_VERTEX].prefix,
1736 &ctx->payloads[PIPE_SHADER_FRAGMENT].prefix,
1737 1, vertex_count, info->instance_count,
1738 1, 1, 1);
1739
1740 ctx->payloads[PIPE_SHADER_FRAGMENT].prefix.unknown_draw = draw_flags;
1741
1742 /* Encode the padded vertex count */
1743
1744 if (info->instance_count > 1) {
1745 /* Triangles have non-even vertex counts so they change how
1746 * padding works internally */
1747
1748 bool is_triangle =
1749 mode == PIPE_PRIM_TRIANGLES ||
1750 mode == PIPE_PRIM_TRIANGLE_STRIP ||
1751 mode == PIPE_PRIM_TRIANGLE_FAN;
1752
1753 struct pan_shift_odd so =
1754 panfrost_padded_vertex_count(vertex_count, !is_triangle);
1755
1756 ctx->payloads[PIPE_SHADER_VERTEX].instance_shift = so.shift;
1757 ctx->payloads[PIPE_SHADER_FRAGMENT].instance_shift = so.shift;
1758
1759 ctx->payloads[PIPE_SHADER_VERTEX].instance_odd = so.odd;
1760 ctx->payloads[PIPE_SHADER_FRAGMENT].instance_odd = so.odd;
1761
1762 ctx->padded_count = pan_expand_shift_odd(so);
1763 } else {
1764 ctx->padded_count = ctx->vertex_count;
1765
1766 /* Reset instancing state */
1767 ctx->payloads[PIPE_SHADER_VERTEX].instance_shift = 0;
1768 ctx->payloads[PIPE_SHADER_VERTEX].instance_odd = 0;
1769 ctx->payloads[PIPE_SHADER_FRAGMENT].instance_shift = 0;
1770 ctx->payloads[PIPE_SHADER_FRAGMENT].instance_odd = 0;
1771 }
1772
1773 /* Fire off the draw itself */
1774 panfrost_queue_draw(ctx);
1775 }
1776
1777 /* CSO state */
1778
1779 static void
1780 panfrost_generic_cso_delete(struct pipe_context *pctx, void *hwcso)
1781 {
1782 free(hwcso);
1783 }
1784
1785 static void *
1786 panfrost_create_rasterizer_state(
1787 struct pipe_context *pctx,
1788 const struct pipe_rasterizer_state *cso)
1789 {
1790 struct panfrost_rasterizer *so = CALLOC_STRUCT(panfrost_rasterizer);
1791
1792 so->base = *cso;
1793
1794 /* Bitmask, unknown meaning of the start value. 0x105 on 32-bit T6XX */
1795 so->tiler_gl_enables = 0x7;
1796
1797 if (cso->front_ccw)
1798 so->tiler_gl_enables |= MALI_FRONT_CCW_TOP;
1799
1800 if (cso->cull_face & PIPE_FACE_FRONT)
1801 so->tiler_gl_enables |= MALI_CULL_FACE_FRONT;
1802
1803 if (cso->cull_face & PIPE_FACE_BACK)
1804 so->tiler_gl_enables |= MALI_CULL_FACE_BACK;
1805
1806 return so;
1807 }
1808
1809 static void
1810 panfrost_bind_rasterizer_state(
1811 struct pipe_context *pctx,
1812 void *hwcso)
1813 {
1814 struct panfrost_context *ctx = pan_context(pctx);
1815
1816 /* TODO: Why can't rasterizer be NULL ever? Other drivers are fine.. */
1817 if (!hwcso)
1818 return;
1819
1820 ctx->rasterizer = hwcso;
1821 ctx->dirty |= PAN_DIRTY_RASTERIZER;
1822
1823 ctx->fragment_shader_core.depth_units = ctx->rasterizer->base.offset_units;
1824 ctx->fragment_shader_core.depth_factor = ctx->rasterizer->base.offset_scale;
1825
1826 /* Gauranteed with the core GL call, so don't expose ARB_polygon_offset */
1827 assert(ctx->rasterizer->base.offset_clamp == 0.0);
1828
1829 /* XXX: Which bit is which? Does this maybe allow offseting not-tri? */
1830
1831 SET_BIT(ctx->fragment_shader_core.unknown2_4, MALI_DEPTH_RANGE_A, ctx->rasterizer->base.offset_tri);
1832 SET_BIT(ctx->fragment_shader_core.unknown2_4, MALI_DEPTH_RANGE_B, ctx->rasterizer->base.offset_tri);
1833
1834 /* Point sprites are emulated */
1835
1836 struct panfrost_shader_state *variant =
1837 ctx->shader[PIPE_SHADER_FRAGMENT] ? &ctx->shader[PIPE_SHADER_FRAGMENT]->variants[ctx->shader[PIPE_SHADER_FRAGMENT]->active_variant] : NULL;
1838
1839 if (ctx->rasterizer->base.sprite_coord_enable || (variant && variant->point_sprite_mask))
1840 ctx->base.bind_fs_state(&ctx->base, ctx->shader[PIPE_SHADER_FRAGMENT]);
1841 }
1842
1843 static void *
1844 panfrost_create_vertex_elements_state(
1845 struct pipe_context *pctx,
1846 unsigned num_elements,
1847 const struct pipe_vertex_element *elements)
1848 {
1849 struct panfrost_vertex_state *so = CALLOC_STRUCT(panfrost_vertex_state);
1850
1851 so->num_elements = num_elements;
1852 memcpy(so->pipe, elements, sizeof(*elements) * num_elements);
1853
1854 for (int i = 0; i < num_elements; ++i) {
1855 so->hw[i].index = i;
1856
1857 enum pipe_format fmt = elements[i].src_format;
1858 const struct util_format_description *desc = util_format_description(fmt);
1859 so->hw[i].unknown1 = 0x2;
1860 so->hw[i].swizzle = panfrost_get_default_swizzle(desc->nr_channels);
1861
1862 so->hw[i].format = panfrost_find_format(desc);
1863
1864 /* The field itself should probably be shifted over */
1865 so->hw[i].src_offset = elements[i].src_offset;
1866 }
1867
1868 return so;
1869 }
1870
1871 static void
1872 panfrost_bind_vertex_elements_state(
1873 struct pipe_context *pctx,
1874 void *hwcso)
1875 {
1876 struct panfrost_context *ctx = pan_context(pctx);
1877
1878 ctx->vertex = hwcso;
1879 ctx->dirty |= PAN_DIRTY_VERTEX;
1880 }
1881
1882 static void *
1883 panfrost_create_shader_state(
1884 struct pipe_context *pctx,
1885 const struct pipe_shader_state *cso)
1886 {
1887 struct panfrost_shader_variants *so = CALLOC_STRUCT(panfrost_shader_variants);
1888 so->base = *cso;
1889
1890 /* Token deep copy to prevent memory corruption */
1891
1892 if (cso->type == PIPE_SHADER_IR_TGSI)
1893 so->base.tokens = tgsi_dup_tokens(so->base.tokens);
1894
1895 return so;
1896 }
1897
1898 static void
1899 panfrost_delete_shader_state(
1900 struct pipe_context *pctx,
1901 void *so)
1902 {
1903 struct panfrost_shader_variants *cso = (struct panfrost_shader_variants *) so;
1904
1905 if (cso->base.type == PIPE_SHADER_IR_TGSI) {
1906 DBG("Deleting TGSI shader leaks duplicated tokens\n");
1907 }
1908
1909 free(so);
1910 }
1911
1912 static void *
1913 panfrost_create_sampler_state(
1914 struct pipe_context *pctx,
1915 const struct pipe_sampler_state *cso)
1916 {
1917 struct panfrost_sampler_state *so = CALLOC_STRUCT(panfrost_sampler_state);
1918 so->base = *cso;
1919
1920 /* sampler_state corresponds to mali_sampler_descriptor, which we can generate entirely here */
1921
1922 bool min_nearest = cso->min_img_filter == PIPE_TEX_FILTER_NEAREST;
1923 bool mag_nearest = cso->mag_img_filter == PIPE_TEX_FILTER_NEAREST;
1924 bool mip_linear = cso->min_mip_filter == PIPE_TEX_MIPFILTER_LINEAR;
1925
1926 unsigned min_filter = min_nearest ? MALI_SAMP_MIN_NEAREST : 0;
1927 unsigned mag_filter = mag_nearest ? MALI_SAMP_MAG_NEAREST : 0;
1928 unsigned mip_filter = mip_linear ?
1929 (MALI_SAMP_MIP_LINEAR_1 | MALI_SAMP_MIP_LINEAR_2) : 0;
1930 unsigned normalized = cso->normalized_coords ? MALI_SAMP_NORM_COORDS : 0;
1931
1932 struct mali_sampler_descriptor sampler_descriptor = {
1933 .filter_mode = min_filter | mag_filter | mip_filter | normalized,
1934 .wrap_s = translate_tex_wrap(cso->wrap_s),
1935 .wrap_t = translate_tex_wrap(cso->wrap_t),
1936 .wrap_r = translate_tex_wrap(cso->wrap_r),
1937 .compare_func = panfrost_translate_alt_compare_func(cso->compare_func),
1938 .border_color = {
1939 cso->border_color.f[0],
1940 cso->border_color.f[1],
1941 cso->border_color.f[2],
1942 cso->border_color.f[3]
1943 },
1944 .min_lod = FIXED_16(cso->min_lod),
1945 .max_lod = FIXED_16(cso->max_lod),
1946 .seamless_cube_map = cso->seamless_cube_map,
1947 };
1948
1949 /* If necessary, we disable mipmapping in the sampler descriptor by
1950 * clamping the LOD as tight as possible (from 0 to epsilon,
1951 * essentially -- remember these are fixed point numbers, so
1952 * epsilon=1/256) */
1953
1954 if (cso->min_mip_filter == PIPE_TEX_MIPFILTER_NONE)
1955 sampler_descriptor.max_lod = sampler_descriptor.min_lod;
1956
1957 /* Enforce that there is something in the middle by adding epsilon*/
1958
1959 if (sampler_descriptor.min_lod == sampler_descriptor.max_lod)
1960 sampler_descriptor.max_lod++;
1961
1962 /* Sanity check */
1963 assert(sampler_descriptor.max_lod > sampler_descriptor.min_lod);
1964
1965 so->hw = sampler_descriptor;
1966
1967 return so;
1968 }
1969
1970 static void
1971 panfrost_bind_sampler_states(
1972 struct pipe_context *pctx,
1973 enum pipe_shader_type shader,
1974 unsigned start_slot, unsigned num_sampler,
1975 void **sampler)
1976 {
1977 assert(start_slot == 0);
1978
1979 struct panfrost_context *ctx = pan_context(pctx);
1980
1981 /* XXX: Should upload, not just copy? */
1982 ctx->sampler_count[shader] = num_sampler;
1983 memcpy(ctx->samplers[shader], sampler, num_sampler * sizeof (void *));
1984
1985 ctx->dirty |= PAN_DIRTY_SAMPLERS;
1986 }
1987
1988 static bool
1989 panfrost_variant_matches(
1990 struct panfrost_context *ctx,
1991 struct panfrost_shader_state *variant,
1992 enum pipe_shader_type type)
1993 {
1994 struct pipe_rasterizer_state *rasterizer = &ctx->rasterizer->base;
1995 struct pipe_alpha_state *alpha = &ctx->depth_stencil->alpha;
1996
1997 bool is_fragment = (type == PIPE_SHADER_FRAGMENT);
1998
1999 if (is_fragment && (alpha->enabled || variant->alpha_state.enabled)) {
2000 /* Make sure enable state is at least the same */
2001 if (alpha->enabled != variant->alpha_state.enabled) {
2002 return false;
2003 }
2004
2005 /* Check that the contents of the test are the same */
2006 bool same_func = alpha->func == variant->alpha_state.func;
2007 bool same_ref = alpha->ref_value == variant->alpha_state.ref_value;
2008
2009 if (!(same_func && same_ref)) {
2010 return false;
2011 }
2012 }
2013
2014 if (is_fragment && rasterizer && (rasterizer->sprite_coord_enable |
2015 variant->point_sprite_mask)) {
2016 /* Ensure the same varyings are turned to point sprites */
2017 if (rasterizer->sprite_coord_enable != variant->point_sprite_mask)
2018 return false;
2019
2020 /* Ensure the orientation is correct */
2021 bool upper_left =
2022 rasterizer->sprite_coord_mode ==
2023 PIPE_SPRITE_COORD_UPPER_LEFT;
2024
2025 if (variant->point_sprite_upper_left != upper_left)
2026 return false;
2027 }
2028
2029 /* Otherwise, we're good to go */
2030 return true;
2031 }
2032
2033 static void
2034 panfrost_bind_shader_state(
2035 struct pipe_context *pctx,
2036 void *hwcso,
2037 enum pipe_shader_type type)
2038 {
2039 struct panfrost_context *ctx = pan_context(pctx);
2040
2041 ctx->shader[type] = hwcso;
2042
2043 if (type == PIPE_SHADER_FRAGMENT)
2044 ctx->dirty |= PAN_DIRTY_FS;
2045 else
2046 ctx->dirty |= PAN_DIRTY_VS;
2047
2048 if (!hwcso) return;
2049
2050 /* Match the appropriate variant */
2051
2052 signed variant = -1;
2053 struct panfrost_shader_variants *variants = (struct panfrost_shader_variants *) hwcso;
2054
2055 for (unsigned i = 0; i < variants->variant_count; ++i) {
2056 if (panfrost_variant_matches(ctx, &variants->variants[i], type)) {
2057 variant = i;
2058 break;
2059 }
2060 }
2061
2062 if (variant == -1) {
2063 /* No variant matched, so create a new one */
2064 variant = variants->variant_count++;
2065 assert(variants->variant_count < MAX_SHADER_VARIANTS);
2066
2067 struct panfrost_shader_state *v =
2068 &variants->variants[variant];
2069
2070 if (type == PIPE_SHADER_FRAGMENT) {
2071 v->alpha_state = ctx->depth_stencil->alpha;
2072
2073 if (ctx->rasterizer) {
2074 v->point_sprite_mask = ctx->rasterizer->base.sprite_coord_enable;
2075 v->point_sprite_upper_left =
2076 ctx->rasterizer->base.sprite_coord_mode ==
2077 PIPE_SPRITE_COORD_UPPER_LEFT;
2078 }
2079 }
2080
2081 variants->variants[variant].tripipe = malloc(sizeof(struct mali_shader_meta));
2082
2083 }
2084
2085 /* Select this variant */
2086 variants->active_variant = variant;
2087
2088 struct panfrost_shader_state *shader_state = &variants->variants[variant];
2089 assert(panfrost_variant_matches(ctx, shader_state, type));
2090
2091 /* We finally have a variant, so compile it */
2092
2093 if (!shader_state->compiled) {
2094 panfrost_shader_compile(ctx, shader_state->tripipe,
2095 variants->base.type,
2096 variants->base.type == PIPE_SHADER_IR_NIR ?
2097 variants->base.ir.nir :
2098 variants->base.tokens,
2099 tgsi_processor_to_shader_stage(type), shader_state);
2100
2101 shader_state->compiled = true;
2102 }
2103 }
2104
2105 static void
2106 panfrost_bind_vs_state(struct pipe_context *pctx, void *hwcso)
2107 {
2108 panfrost_bind_shader_state(pctx, hwcso, PIPE_SHADER_VERTEX);
2109 }
2110
2111 static void
2112 panfrost_bind_fs_state(struct pipe_context *pctx, void *hwcso)
2113 {
2114 panfrost_bind_shader_state(pctx, hwcso, PIPE_SHADER_FRAGMENT);
2115 }
2116
2117 static void
2118 panfrost_set_vertex_buffers(
2119 struct pipe_context *pctx,
2120 unsigned start_slot,
2121 unsigned num_buffers,
2122 const struct pipe_vertex_buffer *buffers)
2123 {
2124 struct panfrost_context *ctx = pan_context(pctx);
2125
2126 util_set_vertex_buffers_mask(ctx->vertex_buffers, &ctx->vb_mask, buffers, start_slot, num_buffers);
2127 }
2128
2129 static void
2130 panfrost_set_constant_buffer(
2131 struct pipe_context *pctx,
2132 enum pipe_shader_type shader, uint index,
2133 const struct pipe_constant_buffer *buf)
2134 {
2135 struct panfrost_context *ctx = pan_context(pctx);
2136 struct panfrost_constant_buffer *pbuf = &ctx->constant_buffer[shader];
2137
2138 util_copy_constant_buffer(&pbuf->cb[index], buf);
2139
2140 unsigned mask = (1 << index);
2141
2142 if (unlikely(!buf)) {
2143 pbuf->enabled_mask &= ~mask;
2144 pbuf->dirty_mask &= ~mask;
2145 return;
2146 }
2147
2148 pbuf->enabled_mask |= mask;
2149 pbuf->dirty_mask |= mask;
2150 }
2151
2152 static void
2153 panfrost_set_stencil_ref(
2154 struct pipe_context *pctx,
2155 const struct pipe_stencil_ref *ref)
2156 {
2157 struct panfrost_context *ctx = pan_context(pctx);
2158 ctx->stencil_ref = *ref;
2159
2160 /* Shader core dirty */
2161 ctx->dirty |= PAN_DIRTY_FS;
2162 }
2163
2164 static enum mali_texture_type
2165 panfrost_translate_texture_type(enum pipe_texture_target t) {
2166 switch (t)
2167 {
2168 case PIPE_BUFFER:
2169 case PIPE_TEXTURE_1D:
2170 case PIPE_TEXTURE_1D_ARRAY:
2171 return MALI_TEX_1D;
2172
2173 case PIPE_TEXTURE_2D:
2174 case PIPE_TEXTURE_2D_ARRAY:
2175 case PIPE_TEXTURE_RECT:
2176 return MALI_TEX_2D;
2177
2178 case PIPE_TEXTURE_3D:
2179 return MALI_TEX_3D;
2180
2181 case PIPE_TEXTURE_CUBE:
2182 case PIPE_TEXTURE_CUBE_ARRAY:
2183 return MALI_TEX_CUBE;
2184
2185 default:
2186 unreachable("Unknown target");
2187 }
2188 }
2189
2190 static struct pipe_sampler_view *
2191 panfrost_create_sampler_view(
2192 struct pipe_context *pctx,
2193 struct pipe_resource *texture,
2194 const struct pipe_sampler_view *template)
2195 {
2196 struct panfrost_sampler_view *so = rzalloc(pctx, struct panfrost_sampler_view);
2197 int bytes_per_pixel = util_format_get_blocksize(texture->format);
2198
2199 pipe_reference(NULL, &texture->reference);
2200
2201 struct panfrost_resource *prsrc = (struct panfrost_resource *) texture;
2202 assert(prsrc->bo);
2203
2204 so->base = *template;
2205 so->base.texture = texture;
2206 so->base.reference.count = 1;
2207 so->base.context = pctx;
2208
2209 /* sampler_views correspond to texture descriptors, minus the texture
2210 * (data) itself. So, we serialise the descriptor here and cache it for
2211 * later. */
2212
2213 /* TODO: Detect from format better */
2214 const struct util_format_description *desc = util_format_description(prsrc->base.format);
2215
2216 unsigned char user_swizzle[4] = {
2217 template->swizzle_r,
2218 template->swizzle_g,
2219 template->swizzle_b,
2220 template->swizzle_a
2221 };
2222
2223 enum mali_format format = panfrost_find_format(desc);
2224
2225 /* Check if we need to set a custom stride by computing the "expected"
2226 * stride and comparing it to what the BO actually wants. Only applies
2227 * to linear textures, since tiled/compressed textures have strict
2228 * alignment requirements for their strides as it is */
2229
2230 unsigned first_level = template->u.tex.first_level;
2231 unsigned last_level = template->u.tex.last_level;
2232
2233 if (prsrc->layout == PAN_LINEAR) {
2234 for (unsigned l = first_level; l <= last_level; ++l) {
2235 unsigned actual_stride = prsrc->slices[l].stride;
2236 unsigned width = u_minify(texture->width0, l);
2237 unsigned comp_stride = width * bytes_per_pixel;
2238
2239 if (comp_stride != actual_stride) {
2240 so->manual_stride = true;
2241 break;
2242 }
2243 }
2244 }
2245
2246 /* In the hardware, array_size refers specifically to array textures,
2247 * whereas in Gallium, it also covers cubemaps */
2248
2249 unsigned array_size = texture->array_size;
2250
2251 if (template->target == PIPE_TEXTURE_CUBE) {
2252 /* TODO: Cubemap arrays */
2253 assert(array_size == 6);
2254 array_size /= 6;
2255 }
2256
2257 struct mali_texture_descriptor texture_descriptor = {
2258 .width = MALI_POSITIVE(u_minify(texture->width0, first_level)),
2259 .height = MALI_POSITIVE(u_minify(texture->height0, first_level)),
2260 .depth = MALI_POSITIVE(u_minify(texture->depth0, first_level)),
2261 .array_size = MALI_POSITIVE(array_size),
2262
2263 /* TODO: Decode */
2264 .format = {
2265 .swizzle = panfrost_translate_swizzle_4(desc->swizzle),
2266 .format = format,
2267
2268 .srgb = desc->colorspace == UTIL_FORMAT_COLORSPACE_SRGB,
2269 .type = panfrost_translate_texture_type(template->target),
2270 },
2271
2272 .swizzle = panfrost_translate_swizzle_4(user_swizzle)
2273 };
2274
2275 texture_descriptor.nr_mipmap_levels = last_level - first_level;
2276
2277 so->hw = texture_descriptor;
2278
2279 return (struct pipe_sampler_view *) so;
2280 }
2281
2282 static void
2283 panfrost_set_sampler_views(
2284 struct pipe_context *pctx,
2285 enum pipe_shader_type shader,
2286 unsigned start_slot, unsigned num_views,
2287 struct pipe_sampler_view **views)
2288 {
2289 struct panfrost_context *ctx = pan_context(pctx);
2290
2291 assert(start_slot == 0);
2292
2293 unsigned new_nr = 0;
2294 for (unsigned i = 0; i < num_views; ++i) {
2295 if (views[i])
2296 new_nr = i + 1;
2297 }
2298
2299 ctx->sampler_view_count[shader] = new_nr;
2300 memcpy(ctx->sampler_views[shader], views, num_views * sizeof (void *));
2301
2302 ctx->dirty |= PAN_DIRTY_TEXTURES;
2303 }
2304
2305 static void
2306 panfrost_sampler_view_destroy(
2307 struct pipe_context *pctx,
2308 struct pipe_sampler_view *view)
2309 {
2310 pipe_resource_reference(&view->texture, NULL);
2311 ralloc_free(view);
2312 }
2313
2314 static void
2315 panfrost_set_shader_buffers(
2316 struct pipe_context *pctx,
2317 enum pipe_shader_type shader,
2318 unsigned start, unsigned count,
2319 const struct pipe_shader_buffer *buffers,
2320 unsigned writable_bitmask)
2321 {
2322 struct panfrost_context *ctx = pan_context(pctx);
2323
2324 util_set_shader_buffers_mask(ctx->ssbo[shader], &ctx->ssbo_mask[shader],
2325 buffers, start, count);
2326 }
2327
2328 /* Hints that a framebuffer should use AFBC where possible */
2329
2330 static void
2331 panfrost_hint_afbc(
2332 struct panfrost_screen *screen,
2333 const struct pipe_framebuffer_state *fb)
2334 {
2335 /* AFBC implemenation incomplete; hide it */
2336 if (!(pan_debug & PAN_DBG_AFBC)) return;
2337
2338 /* Hint AFBC to the resources bound to each color buffer */
2339
2340 for (unsigned i = 0; i < fb->nr_cbufs; ++i) {
2341 struct pipe_surface *surf = fb->cbufs[i];
2342 struct panfrost_resource *rsrc = pan_resource(surf->texture);
2343 panfrost_resource_hint_layout(screen, rsrc, PAN_AFBC, 1);
2344 }
2345
2346 /* Also hint it to the depth buffer */
2347
2348 if (fb->zsbuf) {
2349 struct panfrost_resource *rsrc = pan_resource(fb->zsbuf->texture);
2350 panfrost_resource_hint_layout(screen, rsrc, PAN_AFBC, 1);
2351 }
2352 }
2353
2354 static void
2355 panfrost_set_framebuffer_state(struct pipe_context *pctx,
2356 const struct pipe_framebuffer_state *fb)
2357 {
2358 struct panfrost_context *ctx = pan_context(pctx);
2359
2360 /* Flush when switching framebuffers, but not if the framebuffer
2361 * state is being restored by u_blitter
2362 */
2363
2364 struct panfrost_job *job = panfrost_get_job_for_fbo(ctx);
2365 bool is_scanout = panfrost_is_scanout(ctx);
2366 bool has_draws = job->last_job.gpu;
2367
2368 /* Bail out early when the current and new states are the same. */
2369 if (util_framebuffer_state_equal(&ctx->pipe_framebuffer, fb))
2370 return;
2371
2372 /* The wallpaper logic sets a new FB state before doing the blit and
2373 * restore the old one when it's done. Those FB states are reported to
2374 * be different because the surface they are pointing to are different,
2375 * but those surfaces actually point to the same cbufs/zbufs. In that
2376 * case we definitely don't want new FB descs to be emitted/attached
2377 * since the job is expected to be flushed just after the blit is done,
2378 * so let's just copy the new state and return here.
2379 */
2380 if (ctx->wallpaper_batch) {
2381 util_copy_framebuffer_state(&ctx->pipe_framebuffer, fb);
2382 return;
2383 }
2384
2385 if (!is_scanout || has_draws)
2386 panfrost_flush(pctx, NULL, PIPE_FLUSH_END_OF_FRAME);
2387
2388 /* Invalidate the FBO job cache since we've just been assigned a new
2389 * FB state.
2390 */
2391 ctx->job = NULL;
2392
2393 util_copy_framebuffer_state(&ctx->pipe_framebuffer, fb);
2394
2395 /* Given that we're rendering, we'd love to have compression */
2396 struct panfrost_screen *screen = pan_screen(ctx->base.screen);
2397
2398 panfrost_hint_afbc(screen, &ctx->pipe_framebuffer);
2399 panfrost_attach_vt_framebuffer(ctx, false);
2400 }
2401
2402 static void *
2403 panfrost_create_depth_stencil_state(struct pipe_context *pipe,
2404 const struct pipe_depth_stencil_alpha_state *depth_stencil)
2405 {
2406 return mem_dup(depth_stencil, sizeof(*depth_stencil));
2407 }
2408
2409 static void
2410 panfrost_bind_depth_stencil_state(struct pipe_context *pipe,
2411 void *cso)
2412 {
2413 struct panfrost_context *ctx = pan_context(pipe);
2414 struct pipe_depth_stencil_alpha_state *depth_stencil = cso;
2415 ctx->depth_stencil = depth_stencil;
2416
2417 if (!depth_stencil)
2418 return;
2419
2420 /* Alpha does not exist in the hardware (it's not in ES3), so it's
2421 * emulated in the fragment shader */
2422
2423 if (depth_stencil->alpha.enabled) {
2424 /* We need to trigger a new shader (maybe) */
2425 ctx->base.bind_fs_state(&ctx->base, ctx->shader[PIPE_SHADER_FRAGMENT]);
2426 }
2427
2428 /* Stencil state */
2429 SET_BIT(ctx->fragment_shader_core.unknown2_4, MALI_STENCIL_TEST, depth_stencil->stencil[0].enabled);
2430
2431 panfrost_make_stencil_state(&depth_stencil->stencil[0], &ctx->fragment_shader_core.stencil_front);
2432 ctx->fragment_shader_core.stencil_mask_front = depth_stencil->stencil[0].writemask;
2433
2434 /* If back-stencil is not enabled, use the front values */
2435 bool back_enab = ctx->depth_stencil->stencil[1].enabled;
2436 unsigned back_index = back_enab ? 1 : 0;
2437
2438 panfrost_make_stencil_state(&depth_stencil->stencil[back_index], &ctx->fragment_shader_core.stencil_back);
2439 ctx->fragment_shader_core.stencil_mask_back = depth_stencil->stencil[back_index].writemask;
2440
2441 /* Depth state (TODO: Refactor) */
2442 SET_BIT(ctx->fragment_shader_core.unknown2_3, MALI_DEPTH_TEST, depth_stencil->depth.enabled);
2443
2444 int func = depth_stencil->depth.enabled ? depth_stencil->depth.func : PIPE_FUNC_ALWAYS;
2445
2446 ctx->fragment_shader_core.unknown2_3 &= ~MALI_DEPTH_FUNC_MASK;
2447 ctx->fragment_shader_core.unknown2_3 |= MALI_DEPTH_FUNC(panfrost_translate_compare_func(func));
2448
2449 /* Bounds test not implemented */
2450 assert(!depth_stencil->depth.bounds_test);
2451
2452 ctx->dirty |= PAN_DIRTY_FS;
2453 }
2454
2455 static void
2456 panfrost_delete_depth_stencil_state(struct pipe_context *pipe, void *depth)
2457 {
2458 free( depth );
2459 }
2460
2461 static void
2462 panfrost_set_sample_mask(struct pipe_context *pipe,
2463 unsigned sample_mask)
2464 {
2465 }
2466
2467 static void
2468 panfrost_set_clip_state(struct pipe_context *pipe,
2469 const struct pipe_clip_state *clip)
2470 {
2471 //struct panfrost_context *panfrost = pan_context(pipe);
2472 }
2473
2474 static void
2475 panfrost_set_viewport_states(struct pipe_context *pipe,
2476 unsigned start_slot,
2477 unsigned num_viewports,
2478 const struct pipe_viewport_state *viewports)
2479 {
2480 struct panfrost_context *ctx = pan_context(pipe);
2481
2482 assert(start_slot == 0);
2483 assert(num_viewports == 1);
2484
2485 ctx->pipe_viewport = *viewports;
2486 }
2487
2488 static void
2489 panfrost_set_scissor_states(struct pipe_context *pipe,
2490 unsigned start_slot,
2491 unsigned num_scissors,
2492 const struct pipe_scissor_state *scissors)
2493 {
2494 struct panfrost_context *ctx = pan_context(pipe);
2495
2496 assert(start_slot == 0);
2497 assert(num_scissors == 1);
2498
2499 ctx->scissor = *scissors;
2500 }
2501
2502 static void
2503 panfrost_set_polygon_stipple(struct pipe_context *pipe,
2504 const struct pipe_poly_stipple *stipple)
2505 {
2506 //struct panfrost_context *panfrost = pan_context(pipe);
2507 }
2508
2509 static void
2510 panfrost_set_active_query_state(struct pipe_context *pipe,
2511 bool enable)
2512 {
2513 //struct panfrost_context *panfrost = pan_context(pipe);
2514 }
2515
2516 static void
2517 panfrost_destroy(struct pipe_context *pipe)
2518 {
2519 struct panfrost_context *panfrost = pan_context(pipe);
2520 struct panfrost_screen *screen = pan_screen(pipe->screen);
2521
2522 if (panfrost->blitter)
2523 util_blitter_destroy(panfrost->blitter);
2524
2525 if (panfrost->blitter_wallpaper)
2526 util_blitter_destroy(panfrost->blitter_wallpaper);
2527
2528 panfrost_drm_free_slab(screen, &panfrost->scratchpad);
2529 panfrost_drm_free_slab(screen, &panfrost->shaders);
2530 panfrost_drm_free_slab(screen, &panfrost->tiler_heap);
2531 panfrost_drm_free_slab(screen, &panfrost->tiler_polygon_list);
2532 panfrost_drm_free_slab(screen, &panfrost->tiler_dummy);
2533
2534 ralloc_free(pipe);
2535 }
2536
2537 static struct pipe_query *
2538 panfrost_create_query(struct pipe_context *pipe,
2539 unsigned type,
2540 unsigned index)
2541 {
2542 struct panfrost_query *q = rzalloc(pipe, struct panfrost_query);
2543
2544 q->type = type;
2545 q->index = index;
2546
2547 return (struct pipe_query *) q;
2548 }
2549
2550 static void
2551 panfrost_destroy_query(struct pipe_context *pipe, struct pipe_query *q)
2552 {
2553 ralloc_free(q);
2554 }
2555
2556 static bool
2557 panfrost_begin_query(struct pipe_context *pipe, struct pipe_query *q)
2558 {
2559 struct panfrost_context *ctx = pan_context(pipe);
2560 struct panfrost_query *query = (struct panfrost_query *) q;
2561
2562 switch (query->type) {
2563 case PIPE_QUERY_OCCLUSION_COUNTER:
2564 case PIPE_QUERY_OCCLUSION_PREDICATE:
2565 case PIPE_QUERY_OCCLUSION_PREDICATE_CONSERVATIVE: {
2566 /* Allocate a word for the query results to be stored */
2567 query->transfer = panfrost_allocate_transient(ctx, sizeof(unsigned));
2568
2569 ctx->occlusion_query = query;
2570
2571 break;
2572 }
2573
2574 default:
2575 DBG("Skipping query %d\n", query->type);
2576 break;
2577 }
2578
2579 return true;
2580 }
2581
2582 static bool
2583 panfrost_end_query(struct pipe_context *pipe, struct pipe_query *q)
2584 {
2585 struct panfrost_context *ctx = pan_context(pipe);
2586 ctx->occlusion_query = NULL;
2587 return true;
2588 }
2589
2590 static bool
2591 panfrost_get_query_result(struct pipe_context *pipe,
2592 struct pipe_query *q,
2593 bool wait,
2594 union pipe_query_result *vresult)
2595 {
2596 /* STUB */
2597 struct panfrost_query *query = (struct panfrost_query *) q;
2598
2599 /* We need to flush out the jobs to actually run the counter, TODO
2600 * check wait, TODO wallpaper after if needed */
2601
2602 panfrost_flush(pipe, NULL, PIPE_FLUSH_END_OF_FRAME);
2603
2604 switch (query->type) {
2605 case PIPE_QUERY_OCCLUSION_COUNTER:
2606 case PIPE_QUERY_OCCLUSION_PREDICATE:
2607 case PIPE_QUERY_OCCLUSION_PREDICATE_CONSERVATIVE: {
2608 /* Read back the query results */
2609 unsigned *result = (unsigned *) query->transfer.cpu;
2610 unsigned passed = *result;
2611
2612 if (query->type == PIPE_QUERY_OCCLUSION_COUNTER) {
2613 vresult->u64 = passed;
2614 } else {
2615 vresult->b = !!passed;
2616 }
2617
2618 break;
2619 }
2620 default:
2621 DBG("Skipped query get %d\n", query->type);
2622 break;
2623 }
2624
2625 return true;
2626 }
2627
2628 static struct pipe_stream_output_target *
2629 panfrost_create_stream_output_target(struct pipe_context *pctx,
2630 struct pipe_resource *prsc,
2631 unsigned buffer_offset,
2632 unsigned buffer_size)
2633 {
2634 struct pipe_stream_output_target *target;
2635
2636 target = rzalloc(pctx, struct pipe_stream_output_target);
2637
2638 if (!target)
2639 return NULL;
2640
2641 pipe_reference_init(&target->reference, 1);
2642 pipe_resource_reference(&target->buffer, prsc);
2643
2644 target->context = pctx;
2645 target->buffer_offset = buffer_offset;
2646 target->buffer_size = buffer_size;
2647
2648 return target;
2649 }
2650
2651 static void
2652 panfrost_stream_output_target_destroy(struct pipe_context *pctx,
2653 struct pipe_stream_output_target *target)
2654 {
2655 pipe_resource_reference(&target->buffer, NULL);
2656 ralloc_free(target);
2657 }
2658
2659 static void
2660 panfrost_set_stream_output_targets(struct pipe_context *pctx,
2661 unsigned num_targets,
2662 struct pipe_stream_output_target **targets,
2663 const unsigned *offsets)
2664 {
2665 /* STUB */
2666 }
2667
2668 static void
2669 panfrost_setup_hardware(struct panfrost_context *ctx)
2670 {
2671 struct pipe_context *gallium = (struct pipe_context *) ctx;
2672 struct panfrost_screen *screen = pan_screen(gallium->screen);
2673
2674 panfrost_drm_allocate_slab(screen, &ctx->scratchpad, 64*4, false, 0, 0, 0);
2675 panfrost_drm_allocate_slab(screen, &ctx->shaders, 4096, true, PAN_ALLOCATE_EXECUTE, 0, 0);
2676 panfrost_drm_allocate_slab(screen, &ctx->tiler_heap, 4096, false, PAN_ALLOCATE_INVISIBLE | PAN_ALLOCATE_GROWABLE, 1, 128);
2677 panfrost_drm_allocate_slab(screen, &ctx->tiler_polygon_list, 128*128, false, PAN_ALLOCATE_INVISIBLE | PAN_ALLOCATE_GROWABLE, 1, 128);
2678 panfrost_drm_allocate_slab(screen, &ctx->tiler_dummy, 1, false, PAN_ALLOCATE_INVISIBLE, 0, 0);
2679 }
2680
2681 /* New context creation, which also does hardware initialisation since I don't
2682 * know the better way to structure this :smirk: */
2683
2684 struct pipe_context *
2685 panfrost_create_context(struct pipe_screen *screen, void *priv, unsigned flags)
2686 {
2687 struct panfrost_context *ctx = rzalloc(screen, struct panfrost_context);
2688 struct panfrost_screen *pscreen = pan_screen(screen);
2689 memset(ctx, 0, sizeof(*ctx));
2690 struct pipe_context *gallium = (struct pipe_context *) ctx;
2691
2692 ctx->is_t6xx = pscreen->gpu_id < 0x0700; /* Literally, "earlier than T700" */
2693
2694 gallium->screen = screen;
2695
2696 gallium->destroy = panfrost_destroy;
2697
2698 gallium->set_framebuffer_state = panfrost_set_framebuffer_state;
2699
2700 gallium->flush = panfrost_flush;
2701 gallium->clear = panfrost_clear;
2702 gallium->draw_vbo = panfrost_draw_vbo;
2703
2704 gallium->set_vertex_buffers = panfrost_set_vertex_buffers;
2705 gallium->set_constant_buffer = panfrost_set_constant_buffer;
2706 gallium->set_shader_buffers = panfrost_set_shader_buffers;
2707
2708 gallium->set_stencil_ref = panfrost_set_stencil_ref;
2709
2710 gallium->create_sampler_view = panfrost_create_sampler_view;
2711 gallium->set_sampler_views = panfrost_set_sampler_views;
2712 gallium->sampler_view_destroy = panfrost_sampler_view_destroy;
2713
2714 gallium->create_rasterizer_state = panfrost_create_rasterizer_state;
2715 gallium->bind_rasterizer_state = panfrost_bind_rasterizer_state;
2716 gallium->delete_rasterizer_state = panfrost_generic_cso_delete;
2717
2718 gallium->create_vertex_elements_state = panfrost_create_vertex_elements_state;
2719 gallium->bind_vertex_elements_state = panfrost_bind_vertex_elements_state;
2720 gallium->delete_vertex_elements_state = panfrost_generic_cso_delete;
2721
2722 gallium->create_fs_state = panfrost_create_shader_state;
2723 gallium->delete_fs_state = panfrost_delete_shader_state;
2724 gallium->bind_fs_state = panfrost_bind_fs_state;
2725
2726 gallium->create_vs_state = panfrost_create_shader_state;
2727 gallium->delete_vs_state = panfrost_delete_shader_state;
2728 gallium->bind_vs_state = panfrost_bind_vs_state;
2729
2730 gallium->create_sampler_state = panfrost_create_sampler_state;
2731 gallium->delete_sampler_state = panfrost_generic_cso_delete;
2732 gallium->bind_sampler_states = panfrost_bind_sampler_states;
2733
2734 gallium->create_depth_stencil_alpha_state = panfrost_create_depth_stencil_state;
2735 gallium->bind_depth_stencil_alpha_state = panfrost_bind_depth_stencil_state;
2736 gallium->delete_depth_stencil_alpha_state = panfrost_delete_depth_stencil_state;
2737
2738 gallium->set_sample_mask = panfrost_set_sample_mask;
2739
2740 gallium->set_clip_state = panfrost_set_clip_state;
2741 gallium->set_viewport_states = panfrost_set_viewport_states;
2742 gallium->set_scissor_states = panfrost_set_scissor_states;
2743 gallium->set_polygon_stipple = panfrost_set_polygon_stipple;
2744 gallium->set_active_query_state = panfrost_set_active_query_state;
2745
2746 gallium->create_query = panfrost_create_query;
2747 gallium->destroy_query = panfrost_destroy_query;
2748 gallium->begin_query = panfrost_begin_query;
2749 gallium->end_query = panfrost_end_query;
2750 gallium->get_query_result = panfrost_get_query_result;
2751
2752 gallium->create_stream_output_target = panfrost_create_stream_output_target;
2753 gallium->stream_output_target_destroy = panfrost_stream_output_target_destroy;
2754 gallium->set_stream_output_targets = panfrost_set_stream_output_targets;
2755
2756 panfrost_resource_context_init(gallium);
2757 panfrost_blend_context_init(gallium);
2758 panfrost_compute_context_init(gallium);
2759
2760 panfrost_drm_init_context(ctx);
2761
2762 panfrost_setup_hardware(ctx);
2763
2764 /* XXX: leaks */
2765 gallium->stream_uploader = u_upload_create_default(gallium);
2766 gallium->const_uploader = gallium->stream_uploader;
2767 assert(gallium->stream_uploader);
2768
2769 /* Midgard supports ES modes, plus QUADS/QUAD_STRIPS/POLYGON */
2770 ctx->draw_modes = (1 << (PIPE_PRIM_POLYGON + 1)) - 1;
2771
2772 ctx->primconvert = util_primconvert_create(gallium, ctx->draw_modes);
2773
2774 ctx->blitter = util_blitter_create(gallium);
2775 ctx->blitter_wallpaper = util_blitter_create(gallium);
2776
2777 assert(ctx->blitter);
2778 assert(ctx->blitter_wallpaper);
2779
2780 /* Prepare for render! */
2781
2782 panfrost_job_init(ctx);
2783 panfrost_emit_vertex_payload(ctx);
2784 panfrost_emit_tiler_payload(ctx);
2785 panfrost_invalidate_frame(ctx);
2786 panfrost_default_shader_backend(ctx);
2787
2788 return gallium;
2789 }