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