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