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