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