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