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