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