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