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