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