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