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