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