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