4c5308c8238081534415876946e2d008973630b0
[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 /* TODO: lower GL_CLAMP? */
225 case PIPE_TEX_WRAP_CLAMP:
226 case PIPE_TEX_WRAP_CLAMP_TO_EDGE:
227 return MALI_WRAP_CLAMP_TO_EDGE;
228
229 case PIPE_TEX_WRAP_CLAMP_TO_BORDER:
230 return MALI_WRAP_CLAMP_TO_BORDER;
231
232 case PIPE_TEX_WRAP_MIRROR_REPEAT:
233 return MALI_WRAP_MIRRORED_REPEAT;
234
235 default:
236 unreachable("Invalid wrap");
237 }
238 }
239
240 static unsigned
241 panfrost_translate_compare_func(enum pipe_compare_func in)
242 {
243 switch (in) {
244 case PIPE_FUNC_NEVER:
245 return MALI_FUNC_NEVER;
246
247 case PIPE_FUNC_LESS:
248 return MALI_FUNC_LESS;
249
250 case PIPE_FUNC_EQUAL:
251 return MALI_FUNC_EQUAL;
252
253 case PIPE_FUNC_LEQUAL:
254 return MALI_FUNC_LEQUAL;
255
256 case PIPE_FUNC_GREATER:
257 return MALI_FUNC_GREATER;
258
259 case PIPE_FUNC_NOTEQUAL:
260 return MALI_FUNC_NOTEQUAL;
261
262 case PIPE_FUNC_GEQUAL:
263 return MALI_FUNC_GEQUAL;
264
265 case PIPE_FUNC_ALWAYS:
266 return MALI_FUNC_ALWAYS;
267
268 default:
269 unreachable("Invalid func");
270 }
271 }
272
273 static unsigned
274 panfrost_translate_alt_compare_func(enum pipe_compare_func in)
275 {
276 switch (in) {
277 case PIPE_FUNC_NEVER:
278 return MALI_ALT_FUNC_NEVER;
279
280 case PIPE_FUNC_LESS:
281 return MALI_ALT_FUNC_LESS;
282
283 case PIPE_FUNC_EQUAL:
284 return MALI_ALT_FUNC_EQUAL;
285
286 case PIPE_FUNC_LEQUAL:
287 return MALI_ALT_FUNC_LEQUAL;
288
289 case PIPE_FUNC_GREATER:
290 return MALI_ALT_FUNC_GREATER;
291
292 case PIPE_FUNC_NOTEQUAL:
293 return MALI_ALT_FUNC_NOTEQUAL;
294
295 case PIPE_FUNC_GEQUAL:
296 return MALI_ALT_FUNC_GEQUAL;
297
298 case PIPE_FUNC_ALWAYS:
299 return MALI_ALT_FUNC_ALWAYS;
300
301 default:
302 unreachable("Invalid alt func");
303 }
304 }
305
306 static unsigned
307 panfrost_translate_stencil_op(enum pipe_stencil_op in)
308 {
309 switch (in) {
310 case PIPE_STENCIL_OP_KEEP:
311 return MALI_STENCIL_KEEP;
312
313 case PIPE_STENCIL_OP_ZERO:
314 return MALI_STENCIL_ZERO;
315
316 case PIPE_STENCIL_OP_REPLACE:
317 return MALI_STENCIL_REPLACE;
318
319 case PIPE_STENCIL_OP_INCR:
320 return MALI_STENCIL_INCR;
321
322 case PIPE_STENCIL_OP_DECR:
323 return MALI_STENCIL_DECR;
324
325 case PIPE_STENCIL_OP_INCR_WRAP:
326 return MALI_STENCIL_INCR_WRAP;
327
328 case PIPE_STENCIL_OP_DECR_WRAP:
329 return MALI_STENCIL_DECR_WRAP;
330
331 case PIPE_STENCIL_OP_INVERT:
332 return MALI_STENCIL_INVERT;
333
334 default:
335 unreachable("Invalid stencil op");
336 }
337 }
338
339 static void
340 panfrost_make_stencil_state(const struct pipe_stencil_state *in, struct mali_stencil_test *out)
341 {
342 out->ref = 0; /* Gallium gets it from elsewhere */
343
344 out->mask = in->valuemask;
345 out->func = panfrost_translate_compare_func(in->func);
346 out->sfail = panfrost_translate_stencil_op(in->fail_op);
347 out->dpfail = panfrost_translate_stencil_op(in->zfail_op);
348 out->dppass = panfrost_translate_stencil_op(in->zpass_op);
349 }
350
351 static void
352 panfrost_default_shader_backend(struct panfrost_context *ctx)
353 {
354 struct panfrost_screen *screen = pan_screen(ctx->base.screen);
355 struct mali_shader_meta shader = {
356 .alpha_coverage = ~MALI_ALPHA_COVERAGE(0.000000),
357
358 .unknown2_3 = MALI_DEPTH_FUNC(MALI_FUNC_ALWAYS) | 0x3010,
359 .unknown2_4 = MALI_NO_MSAA | 0x4e0,
360 };
361
362 /* unknown2_4 has 0x10 bit set on T6XX and T720. We don't know why this is
363 * required (independent of 32-bit/64-bit descriptors), or why it's not
364 * used on later GPU revisions. Otherwise, all shader jobs fault on
365 * these earlier chips (perhaps this is a chicken bit of some kind).
366 * More investigation is needed. */
367
368 if (screen->quirks & MIDGARD_SFBD)
369 shader.unknown2_4 |= 0x10;
370
371 struct pipe_stencil_state default_stencil = {
372 .enabled = 0,
373 .func = PIPE_FUNC_ALWAYS,
374 .fail_op = MALI_STENCIL_KEEP,
375 .zfail_op = MALI_STENCIL_KEEP,
376 .zpass_op = MALI_STENCIL_KEEP,
377 .writemask = 0xFF,
378 .valuemask = 0xFF
379 };
380
381 panfrost_make_stencil_state(&default_stencil, &shader.stencil_front);
382 shader.stencil_mask_front = default_stencil.writemask;
383
384 panfrost_make_stencil_state(&default_stencil, &shader.stencil_back);
385 shader.stencil_mask_back = default_stencil.writemask;
386
387 if (default_stencil.enabled)
388 shader.unknown2_4 |= MALI_STENCIL_TEST;
389
390 memcpy(&ctx->fragment_shader_core, &shader, sizeof(shader));
391 }
392
393 /* Generates a vertex/tiler job. This is, in some sense, the heart of the
394 * graphics command stream. It should be called once per draw, accordding to
395 * presentations. Set is_tiler for "tiler" jobs (fragment shader jobs, but in
396 * Mali parlance, "fragment" refers to framebuffer writeout). Clear it for
397 * vertex jobs. */
398
399 struct panfrost_transfer
400 panfrost_vertex_tiler_job(struct panfrost_context *ctx, bool is_tiler)
401 {
402 struct panfrost_batch *batch = panfrost_get_batch_for_fbo(ctx);
403 struct mali_job_descriptor_header job = {
404 .job_type = is_tiler ? JOB_TYPE_TILER : JOB_TYPE_VERTEX,
405 .job_descriptor_size = 1,
406 };
407
408 struct midgard_payload_vertex_tiler *payload = is_tiler ? &ctx->payloads[PIPE_SHADER_FRAGMENT] : &ctx->payloads[PIPE_SHADER_VERTEX];
409
410 struct panfrost_transfer transfer = panfrost_allocate_transient(batch, sizeof(job) + sizeof(*payload));
411 memcpy(transfer.cpu, &job, sizeof(job));
412 memcpy(transfer.cpu + sizeof(job), payload, sizeof(*payload));
413 return transfer;
414 }
415
416 mali_ptr
417 panfrost_vertex_buffer_address(struct panfrost_context *ctx, unsigned i)
418 {
419 struct pipe_vertex_buffer *buf = &ctx->vertex_buffers[i];
420 struct panfrost_resource *rsrc = (struct panfrost_resource *) (buf->buffer.resource);
421
422 return rsrc->bo->gpu + buf->buffer_offset;
423 }
424
425 static bool
426 panfrost_writes_point_size(struct panfrost_context *ctx)
427 {
428 assert(ctx->shader[PIPE_SHADER_VERTEX]);
429 struct panfrost_shader_state *vs = &ctx->shader[PIPE_SHADER_VERTEX]->variants[ctx->shader[PIPE_SHADER_VERTEX]->active_variant];
430
431 return vs->writes_point_size && ctx->payloads[PIPE_SHADER_FRAGMENT].prefix.draw_mode == MALI_POINTS;
432 }
433
434 /* Stage the attribute descriptors so we can adjust src_offset
435 * to let BOs align nicely */
436
437 static void
438 panfrost_stage_attributes(struct panfrost_context *ctx)
439 {
440 struct panfrost_batch *batch = panfrost_get_batch_for_fbo(ctx);
441 struct panfrost_vertex_state *so = ctx->vertex;
442
443 size_t sz = sizeof(struct mali_attr_meta) * PAN_MAX_ATTRIBUTE;
444 struct panfrost_transfer transfer = panfrost_allocate_transient(batch, sz);
445 struct mali_attr_meta *target = (struct mali_attr_meta *) transfer.cpu;
446
447 /* Copy as-is for the first pass */
448 memcpy(target, so->hw, sz);
449
450 /* Fixup offsets for the second pass. Recall that the hardware
451 * calculates attribute addresses as:
452 *
453 * addr = base + (stride * vtx) + src_offset;
454 *
455 * However, on Mali, base must be aligned to 64-bytes, so we
456 * instead let:
457 *
458 * base' = base & ~63 = base - (base & 63)
459 *
460 * To compensate when using base' (see emit_vertex_data), we have
461 * to adjust src_offset by the masked off piece:
462 *
463 * addr' = base' + (stride * vtx) + (src_offset + (base & 63))
464 * = base - (base & 63) + (stride * vtx) + src_offset + (base & 63)
465 * = base + (stride * vtx) + src_offset
466 * = addr;
467 *
468 * QED.
469 */
470
471 unsigned start = ctx->payloads[PIPE_SHADER_VERTEX].offset_start;
472
473 for (unsigned i = 0; i < so->num_elements; ++i) {
474 unsigned vbi = so->pipe[i].vertex_buffer_index;
475 struct pipe_vertex_buffer *buf = &ctx->vertex_buffers[vbi];
476 mali_ptr addr = panfrost_vertex_buffer_address(ctx, vbi);
477
478 /* Adjust by the masked off bits of the offset */
479 target[i].src_offset += (addr & 63);
480
481 /* Also, somewhat obscurely per-instance data needs to be
482 * offset in response to a delayed start in an indexed draw */
483
484 if (so->pipe[i].instance_divisor && ctx->instance_count > 1 && start)
485 target[i].src_offset -= buf->stride * start;
486 }
487
488 /* Let's also include vertex builtins */
489
490 target[PAN_VERTEX_ID].format = MALI_R32UI;
491 target[PAN_VERTEX_ID].swizzle = panfrost_get_default_swizzle(1);
492
493 target[PAN_INSTANCE_ID].format = MALI_R32UI;
494 target[PAN_INSTANCE_ID].swizzle = panfrost_get_default_swizzle(1);
495
496 ctx->payloads[PIPE_SHADER_VERTEX].postfix.attribute_meta = transfer.gpu;
497 }
498
499 static void
500 panfrost_upload_sampler_descriptors(struct panfrost_context *ctx)
501 {
502 struct panfrost_batch *batch = panfrost_get_batch_for_fbo(ctx);
503 size_t desc_size = sizeof(struct mali_sampler_descriptor);
504
505 for (int t = 0; t <= PIPE_SHADER_FRAGMENT; ++t) {
506 mali_ptr upload = 0;
507
508 if (ctx->sampler_count[t]) {
509 size_t transfer_size = desc_size * ctx->sampler_count[t];
510
511 struct panfrost_transfer transfer =
512 panfrost_allocate_transient(batch, transfer_size);
513
514 struct mali_sampler_descriptor *desc =
515 (struct mali_sampler_descriptor *) transfer.cpu;
516
517 for (int i = 0; i < ctx->sampler_count[t]; ++i)
518 desc[i] = ctx->samplers[t][i]->hw;
519
520 upload = transfer.gpu;
521 }
522
523 ctx->payloads[t].postfix.sampler_descriptor = upload;
524 }
525 }
526
527 static enum mali_texture_layout
528 panfrost_layout_for_texture(struct panfrost_resource *rsrc)
529 {
530 /* TODO: other linear depth textures */
531 bool is_depth = rsrc->base.format == PIPE_FORMAT_Z32_UNORM;
532
533 switch (rsrc->layout) {
534 case PAN_AFBC:
535 return MALI_TEXTURE_AFBC;
536 case PAN_TILED:
537 assert(!is_depth);
538 return MALI_TEXTURE_TILED;
539 case PAN_LINEAR:
540 return is_depth ? MALI_TEXTURE_TILED : MALI_TEXTURE_LINEAR;
541 default:
542 unreachable("Invalid texture layout");
543 }
544 }
545
546 static mali_ptr
547 panfrost_upload_tex(
548 struct panfrost_context *ctx,
549 enum pipe_shader_type st,
550 struct panfrost_sampler_view *view)
551 {
552 if (!view)
553 return (mali_ptr) 0;
554
555 struct pipe_sampler_view *pview = &view->base;
556 struct panfrost_resource *rsrc = pan_resource(pview->texture);
557
558 /* Do we interleave an explicit stride with every element? */
559
560 bool has_manual_stride = view->manual_stride;
561
562 /* For easy access */
563
564 bool is_buffer = pview->target == PIPE_BUFFER;
565 unsigned first_level = is_buffer ? 0 : pview->u.tex.first_level;
566 unsigned last_level = is_buffer ? 0 : pview->u.tex.last_level;
567 unsigned first_layer = is_buffer ? 0 : pview->u.tex.first_layer;
568 unsigned last_layer = is_buffer ? 0 : pview->u.tex.last_layer;
569
570 /* Lower-bit is set when sampling from colour AFBC */
571 bool is_afbc = rsrc->layout == PAN_AFBC;
572 bool is_zs = rsrc->base.bind & PIPE_BIND_DEPTH_STENCIL;
573 unsigned afbc_bit = (is_afbc && !is_zs) ? 1 : 0;
574
575 /* Add the BO to the job so it's retained until the job is done. */
576 struct panfrost_batch *batch = panfrost_get_batch_for_fbo(ctx);
577 panfrost_batch_add_bo(batch, rsrc->bo,
578 PAN_BO_ACCESS_SHARED | PAN_BO_ACCESS_READ |
579 panfrost_bo_access_for_stage(st));
580
581 /* Add the usage flags in, since they can change across the CSO
582 * lifetime due to layout switches */
583
584 view->hw.format.layout = panfrost_layout_for_texture(rsrc);
585 view->hw.format.manual_stride = has_manual_stride;
586
587 /* Inject the addresses in, interleaving mip levels, cube faces, and
588 * strides in that order */
589
590 unsigned idx = 0;
591
592 for (unsigned l = first_level; l <= last_level; ++l) {
593 for (unsigned f = first_layer; f <= last_layer; ++f) {
594
595 view->hw.payload[idx++] =
596 panfrost_get_texture_address(rsrc, l, f) + afbc_bit;
597
598 if (has_manual_stride) {
599 view->hw.payload[idx++] =
600 rsrc->slices[l].stride;
601 }
602 }
603 }
604
605 return panfrost_upload_transient(batch, &view->hw,
606 sizeof(struct mali_texture_descriptor));
607 }
608
609 static void
610 panfrost_upload_texture_descriptors(struct panfrost_context *ctx)
611 {
612 struct panfrost_batch *batch = panfrost_get_batch_for_fbo(ctx);
613
614 for (int t = 0; t <= PIPE_SHADER_FRAGMENT; ++t) {
615 mali_ptr trampoline = 0;
616
617 if (ctx->sampler_view_count[t]) {
618 uint64_t trampolines[PIPE_MAX_SHADER_SAMPLER_VIEWS];
619
620 for (int i = 0; i < ctx->sampler_view_count[t]; ++i)
621 trampolines[i] =
622 panfrost_upload_tex(ctx, t, ctx->sampler_views[t][i]);
623
624 trampoline = panfrost_upload_transient(batch, trampolines, sizeof(uint64_t) * ctx->sampler_view_count[t]);
625 }
626
627 ctx->payloads[t].postfix.texture_trampoline = trampoline;
628 }
629 }
630
631 struct sysval_uniform {
632 union {
633 float f[4];
634 int32_t i[4];
635 uint32_t u[4];
636 uint64_t du[2];
637 };
638 };
639
640 static void panfrost_upload_viewport_scale_sysval(struct panfrost_context *ctx,
641 struct sysval_uniform *uniform)
642 {
643 const struct pipe_viewport_state *vp = &ctx->pipe_viewport;
644
645 uniform->f[0] = vp->scale[0];
646 uniform->f[1] = vp->scale[1];
647 uniform->f[2] = vp->scale[2];
648 }
649
650 static void panfrost_upload_viewport_offset_sysval(struct panfrost_context *ctx,
651 struct sysval_uniform *uniform)
652 {
653 const struct pipe_viewport_state *vp = &ctx->pipe_viewport;
654
655 uniform->f[0] = vp->translate[0];
656 uniform->f[1] = vp->translate[1];
657 uniform->f[2] = vp->translate[2];
658 }
659
660 static void panfrost_upload_txs_sysval(struct panfrost_context *ctx,
661 enum pipe_shader_type st,
662 unsigned int sysvalid,
663 struct sysval_uniform *uniform)
664 {
665 unsigned texidx = PAN_SYSVAL_ID_TO_TXS_TEX_IDX(sysvalid);
666 unsigned dim = PAN_SYSVAL_ID_TO_TXS_DIM(sysvalid);
667 bool is_array = PAN_SYSVAL_ID_TO_TXS_IS_ARRAY(sysvalid);
668 struct pipe_sampler_view *tex = &ctx->sampler_views[st][texidx]->base;
669
670 assert(dim);
671 uniform->i[0] = u_minify(tex->texture->width0, tex->u.tex.first_level);
672
673 if (dim > 1)
674 uniform->i[1] = u_minify(tex->texture->height0,
675 tex->u.tex.first_level);
676
677 if (dim > 2)
678 uniform->i[2] = u_minify(tex->texture->depth0,
679 tex->u.tex.first_level);
680
681 if (is_array)
682 uniform->i[dim] = tex->texture->array_size;
683 }
684
685 static void panfrost_upload_ssbo_sysval(
686 struct panfrost_context *ctx,
687 enum pipe_shader_type st,
688 unsigned ssbo_id,
689 struct sysval_uniform *uniform)
690 {
691 assert(ctx->ssbo_mask[st] & (1 << ssbo_id));
692 struct pipe_shader_buffer sb = ctx->ssbo[st][ssbo_id];
693
694 /* Compute address */
695 struct panfrost_batch *batch = panfrost_get_batch_for_fbo(ctx);
696 struct panfrost_bo *bo = pan_resource(sb.buffer)->bo;
697
698 panfrost_batch_add_bo(batch, bo,
699 PAN_BO_ACCESS_SHARED | PAN_BO_ACCESS_RW |
700 panfrost_bo_access_for_stage(st));
701
702 /* Upload address and size as sysval */
703 uniform->du[0] = bo->gpu + sb.buffer_offset;
704 uniform->u[2] = sb.buffer_size;
705 }
706
707 static void
708 panfrost_upload_sampler_sysval(
709 struct panfrost_context *ctx,
710 enum pipe_shader_type st,
711 unsigned sampler_index,
712 struct sysval_uniform *uniform)
713 {
714 struct pipe_sampler_state *sampl =
715 &ctx->samplers[st][sampler_index]->base;
716
717 uniform->f[0] = sampl->min_lod;
718 uniform->f[1] = sampl->max_lod;
719 uniform->f[2] = sampl->lod_bias;
720
721 /* Even without any errata, Midgard represents "no mipmapping" as
722 * fixing the LOD with the clamps; keep behaviour consistent. c.f.
723 * panfrost_create_sampler_state which also explains our choice of
724 * epsilon value (again to keep behaviour consistent) */
725
726 if (sampl->min_mip_filter == PIPE_TEX_MIPFILTER_NONE)
727 uniform->f[1] = uniform->f[0] + (1.0/256.0);
728 }
729
730 static void panfrost_upload_num_work_groups_sysval(struct panfrost_context *ctx,
731 struct sysval_uniform *uniform)
732 {
733 uniform->u[0] = ctx->compute_grid->grid[0];
734 uniform->u[1] = ctx->compute_grid->grid[1];
735 uniform->u[2] = ctx->compute_grid->grid[2];
736 }
737
738 static void panfrost_upload_sysvals(struct panfrost_context *ctx, void *buf,
739 struct panfrost_shader_state *ss,
740 enum pipe_shader_type st)
741 {
742 struct sysval_uniform *uniforms = (void *)buf;
743
744 for (unsigned i = 0; i < ss->sysval_count; ++i) {
745 int sysval = ss->sysval[i];
746
747 switch (PAN_SYSVAL_TYPE(sysval)) {
748 case PAN_SYSVAL_VIEWPORT_SCALE:
749 panfrost_upload_viewport_scale_sysval(ctx, &uniforms[i]);
750 break;
751 case PAN_SYSVAL_VIEWPORT_OFFSET:
752 panfrost_upload_viewport_offset_sysval(ctx, &uniforms[i]);
753 break;
754 case PAN_SYSVAL_TEXTURE_SIZE:
755 panfrost_upload_txs_sysval(ctx, st, PAN_SYSVAL_ID(sysval),
756 &uniforms[i]);
757 break;
758 case PAN_SYSVAL_SSBO:
759 panfrost_upload_ssbo_sysval(ctx, st, PAN_SYSVAL_ID(sysval),
760 &uniforms[i]);
761 break;
762 case PAN_SYSVAL_NUM_WORK_GROUPS:
763 panfrost_upload_num_work_groups_sysval(ctx, &uniforms[i]);
764 break;
765 case PAN_SYSVAL_SAMPLER:
766 panfrost_upload_sampler_sysval(ctx, st, PAN_SYSVAL_ID(sysval),
767 &uniforms[i]);
768 break;
769 default:
770 assert(0);
771 }
772 }
773 }
774
775 static const void *
776 panfrost_map_constant_buffer_cpu(struct panfrost_constant_buffer *buf, unsigned index)
777 {
778 struct pipe_constant_buffer *cb = &buf->cb[index];
779 struct panfrost_resource *rsrc = pan_resource(cb->buffer);
780
781 if (rsrc)
782 return rsrc->bo->cpu;
783 else if (cb->user_buffer)
784 return cb->user_buffer;
785 else
786 unreachable("No constant buffer");
787 }
788
789 static mali_ptr
790 panfrost_map_constant_buffer_gpu(
791 struct panfrost_context *ctx,
792 enum pipe_shader_type st,
793 struct panfrost_constant_buffer *buf,
794 unsigned index)
795 {
796 struct pipe_constant_buffer *cb = &buf->cb[index];
797 struct panfrost_resource *rsrc = pan_resource(cb->buffer);
798 struct panfrost_batch *batch = panfrost_get_batch_for_fbo(ctx);
799
800 if (rsrc) {
801 panfrost_batch_add_bo(batch, rsrc->bo,
802 PAN_BO_ACCESS_SHARED |
803 PAN_BO_ACCESS_READ |
804 panfrost_bo_access_for_stage(st));
805 return rsrc->bo->gpu;
806 } else if (cb->user_buffer) {
807 return panfrost_upload_transient(batch, cb->user_buffer, cb->buffer_size);
808 } else {
809 unreachable("No constant buffer");
810 }
811 }
812
813 /* Compute number of UBOs active (more specifically, compute the highest UBO
814 * number addressable -- if there are gaps, include them in the count anyway).
815 * We always include UBO #0 in the count, since we *need* uniforms enabled for
816 * sysvals. */
817
818 static unsigned
819 panfrost_ubo_count(struct panfrost_context *ctx, enum pipe_shader_type stage)
820 {
821 unsigned mask = ctx->constant_buffer[stage].enabled_mask | 1;
822 return 32 - __builtin_clz(mask);
823 }
824
825 /* Fixes up a shader state with current state */
826
827 static void
828 panfrost_patch_shader_state(struct panfrost_context *ctx,
829 enum pipe_shader_type stage)
830 {
831 struct panfrost_shader_variants *all = ctx->shader[stage];
832
833 if (!all) {
834 ctx->payloads[stage].postfix.shader = 0;
835 return;
836 }
837
838 struct panfrost_shader_state *ss = &all->variants[all->active_variant];
839
840 ss->tripipe->texture_count = ctx->sampler_view_count[stage];
841 ss->tripipe->sampler_count = ctx->sampler_count[stage];
842
843 ss->tripipe->midgard1.flags = 0x220;
844
845 unsigned ubo_count = panfrost_ubo_count(ctx, stage);
846 ss->tripipe->midgard1.uniform_buffer_count = ubo_count;
847
848 struct panfrost_batch *batch = panfrost_get_batch_for_fbo(ctx);
849
850 /* Add the shader BO to the batch. */
851 panfrost_batch_add_bo(batch, ss->bo,
852 PAN_BO_ACCESS_PRIVATE |
853 PAN_BO_ACCESS_READ |
854 panfrost_bo_access_for_stage(stage));
855
856 ctx->payloads[stage].postfix.shader = panfrost_upload_transient(batch,
857 ss->tripipe,
858 sizeof(struct mali_shader_meta));
859 }
860
861 /* Go through dirty flags and actualise them in the cmdstream. */
862
863 void
864 panfrost_emit_for_draw(struct panfrost_context *ctx, bool with_vertex_data)
865 {
866 struct panfrost_batch *batch = panfrost_get_batch_for_fbo(ctx);
867 struct panfrost_screen *screen = pan_screen(ctx->base.screen);
868
869 panfrost_batch_add_fbo_bos(batch);
870 panfrost_attach_vt_framebuffer(ctx);
871
872 if (with_vertex_data) {
873 panfrost_emit_vertex_data(batch);
874
875 /* Varyings emitted for -all- geometry */
876 unsigned total_count = ctx->padded_count * ctx->instance_count;
877 panfrost_emit_varying_descriptor(ctx, total_count);
878 }
879
880 bool msaa = ctx->rasterizer->base.multisample;
881
882 if (ctx->dirty & PAN_DIRTY_RASTERIZER) {
883 ctx->payloads[PIPE_SHADER_FRAGMENT].gl_enables = ctx->rasterizer->tiler_gl_enables;
884
885 /* TODO: Sample size */
886 SET_BIT(ctx->fragment_shader_core.unknown2_3, MALI_HAS_MSAA, msaa);
887 SET_BIT(ctx->fragment_shader_core.unknown2_4, MALI_NO_MSAA, !msaa);
888 }
889
890 panfrost_batch_set_requirements(batch);
891
892 if (ctx->occlusion_query) {
893 ctx->payloads[PIPE_SHADER_FRAGMENT].gl_enables |= MALI_OCCLUSION_QUERY;
894 ctx->payloads[PIPE_SHADER_FRAGMENT].postfix.occlusion_counter = ctx->occlusion_query->bo->gpu;
895 }
896
897 panfrost_patch_shader_state(ctx, PIPE_SHADER_VERTEX);
898 panfrost_patch_shader_state(ctx, PIPE_SHADER_COMPUTE);
899
900 if (ctx->dirty & (PAN_DIRTY_RASTERIZER | PAN_DIRTY_VS)) {
901 /* Check if we need to link the gl_PointSize varying */
902 if (!panfrost_writes_point_size(ctx)) {
903 /* If the size is constant, write it out. Otherwise,
904 * don't touch primitive_size (since we would clobber
905 * the pointer there) */
906
907 ctx->payloads[PIPE_SHADER_FRAGMENT].primitive_size.constant = ctx->rasterizer->base.line_width;
908 }
909 }
910
911 /* TODO: Maybe dirty track FS, maybe not. For now, it's transient. */
912 if (ctx->shader[PIPE_SHADER_FRAGMENT])
913 ctx->dirty |= PAN_DIRTY_FS;
914
915 if (ctx->dirty & PAN_DIRTY_FS) {
916 assert(ctx->shader[PIPE_SHADER_FRAGMENT]);
917 struct panfrost_shader_state *variant = &ctx->shader[PIPE_SHADER_FRAGMENT]->variants[ctx->shader[PIPE_SHADER_FRAGMENT]->active_variant];
918
919 panfrost_patch_shader_state(ctx, PIPE_SHADER_FRAGMENT);
920
921 #define COPY(name) ctx->fragment_shader_core.name = variant->tripipe->name
922
923 COPY(shader);
924 COPY(attribute_count);
925 COPY(varying_count);
926 COPY(texture_count);
927 COPY(sampler_count);
928 COPY(midgard1.uniform_count);
929 COPY(midgard1.uniform_buffer_count);
930 COPY(midgard1.work_count);
931 COPY(midgard1.flags);
932 COPY(midgard1.unknown2);
933
934 #undef COPY
935
936 /* Get blending setup */
937 unsigned rt_count = MAX2(ctx->pipe_framebuffer.nr_cbufs, 1);
938
939 struct panfrost_blend_final blend[PIPE_MAX_COLOR_BUFS];
940
941 for (unsigned c = 0; c < rt_count; ++c)
942 blend[c] = panfrost_get_blend_for_context(ctx, c);
943
944 /* If there is a blend shader, work registers are shared. XXX: opt */
945
946 for (unsigned c = 0; c < rt_count; ++c) {
947 if (blend[c].is_shader)
948 ctx->fragment_shader_core.midgard1.work_count = 16;
949 }
950
951 /* Depending on whether it's legal to in the given shader, we
952 * try to enable early-z testing (or forward-pixel kill?) */
953
954 SET_BIT(ctx->fragment_shader_core.midgard1.flags, MALI_EARLY_Z, !variant->can_discard);
955
956 /* Any time texturing is used, derivatives are implicitly
957 * calculated, so we need to enable helper invocations */
958
959 SET_BIT(ctx->fragment_shader_core.midgard1.flags, MALI_HELPER_INVOCATIONS, variant->helper_invocations);
960
961 /* Assign the stencil refs late */
962
963 unsigned front_ref = ctx->stencil_ref.ref_value[0];
964 unsigned back_ref = ctx->stencil_ref.ref_value[1];
965 bool back_enab = ctx->depth_stencil->stencil[1].enabled;
966
967 ctx->fragment_shader_core.stencil_front.ref = front_ref;
968 ctx->fragment_shader_core.stencil_back.ref = back_enab ? back_ref : front_ref;
969
970 /* CAN_DISCARD should be set if the fragment shader possibly
971 * contains a 'discard' instruction. It is likely this is
972 * related to optimizations related to forward-pixel kill, as
973 * per "Mali Performance 3: Is EGL_BUFFER_PRESERVED a good
974 * thing?" by Peter Harris
975 */
976
977 SET_BIT(ctx->fragment_shader_core.unknown2_3, MALI_CAN_DISCARD, variant->can_discard);
978 SET_BIT(ctx->fragment_shader_core.midgard1.flags, 0x400, variant->can_discard);
979
980 /* Even on MFBD, the shader descriptor gets blend shaders. It's
981 * *also* copied to the blend_meta appended (by convention),
982 * but this is the field actually read by the hardware. (Or
983 * maybe both are read...?) */
984
985 if (blend[0].is_shader) {
986 ctx->fragment_shader_core.blend.shader =
987 blend[0].shader.bo->gpu | blend[0].shader.first_tag;
988 } else {
989 ctx->fragment_shader_core.blend.shader = 0;
990 }
991
992 if (screen->quirks & MIDGARD_SFBD) {
993 /* When only a single render target platform is used, the blend
994 * information is inside the shader meta itself. We
995 * additionally need to signal CAN_DISCARD for nontrivial blend
996 * modes (so we're able to read back the destination buffer) */
997
998 SET_BIT(ctx->fragment_shader_core.unknown2_3, MALI_HAS_BLEND_SHADER, blend[0].is_shader);
999
1000 if (!blend[0].is_shader) {
1001 ctx->fragment_shader_core.blend.equation =
1002 *blend[0].equation.equation;
1003 ctx->fragment_shader_core.blend.constant =
1004 blend[0].equation.constant;
1005 }
1006
1007 SET_BIT(ctx->fragment_shader_core.unknown2_3, MALI_CAN_DISCARD, !blend[0].no_blending);
1008 }
1009
1010 size_t size = sizeof(struct mali_shader_meta) + (sizeof(struct midgard_blend_rt) * rt_count);
1011 struct panfrost_transfer transfer = panfrost_allocate_transient(batch, size);
1012 memcpy(transfer.cpu, &ctx->fragment_shader_core, sizeof(struct mali_shader_meta));
1013
1014 ctx->payloads[PIPE_SHADER_FRAGMENT].postfix.shader = transfer.gpu;
1015
1016 if (!(screen->quirks & MIDGARD_SFBD)) {
1017 /* Additional blend descriptor tacked on for jobs using MFBD */
1018
1019 struct midgard_blend_rt rts[4];
1020
1021 for (unsigned i = 0; i < rt_count; ++i) {
1022 rts[i].flags = 0x200;
1023
1024 bool is_srgb =
1025 (ctx->pipe_framebuffer.nr_cbufs > i) &&
1026 (ctx->pipe_framebuffer.cbufs[i]) &&
1027 util_format_is_srgb(ctx->pipe_framebuffer.cbufs[i]->format);
1028
1029 SET_BIT(rts[i].flags, MALI_BLEND_MRT_SHADER, blend[i].is_shader);
1030 SET_BIT(rts[i].flags, MALI_BLEND_LOAD_TIB, !blend[i].no_blending);
1031 SET_BIT(rts[i].flags, MALI_BLEND_SRGB, is_srgb);
1032 SET_BIT(rts[i].flags, MALI_BLEND_NO_DITHER, !ctx->blend->base.dither);
1033
1034 /* TODO: sRGB in blend shaders is currently
1035 * unimplemented. Contact me (Alyssa) if you're
1036 * interested in working on this. We have
1037 * native Midgard ops for helping here, but
1038 * they're not well-understood yet. */
1039
1040 assert(!(is_srgb && blend[i].is_shader));
1041
1042 if (blend[i].is_shader) {
1043 rts[i].blend.shader = blend[i].shader.bo->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 (mode == PIPE_PRIM_LINE_STRIP) {
1503 draw_flags |= 0x800;
1504 }
1505
1506 panfrost_statistics_record(ctx, info);
1507
1508 if (info->index_size) {
1509 /* Calculate the min/max index used so we can figure out how
1510 * many times to invoke the vertex shader */
1511
1512 /* Fetch / calculate index bounds */
1513 unsigned min_index = 0, max_index = 0;
1514
1515 if (info->max_index == ~0u) {
1516 u_vbuf_get_minmax_index(pipe, info, &min_index, &max_index);
1517 } else {
1518 min_index = info->min_index;
1519 max_index = info->max_index;
1520 }
1521
1522 /* Use the corresponding values */
1523 vertex_count = max_index - min_index + 1;
1524 ctx->payloads[PIPE_SHADER_VERTEX].offset_start = min_index + info->index_bias;
1525 ctx->payloads[PIPE_SHADER_FRAGMENT].offset_start = min_index + info->index_bias;
1526
1527 ctx->payloads[PIPE_SHADER_FRAGMENT].prefix.offset_bias_correction = -min_index;
1528 ctx->payloads[PIPE_SHADER_FRAGMENT].prefix.index_count = MALI_POSITIVE(info->count);
1529
1530 //assert(!info->restart_index); /* TODO: Research */
1531
1532 draw_flags |= panfrost_translate_index_size(info->index_size);
1533 ctx->payloads[PIPE_SHADER_FRAGMENT].prefix.indices = panfrost_get_index_buffer_mapped(ctx, info);
1534 } else {
1535 /* Index count == vertex count, if no indexing is applied, as
1536 * if it is internally indexed in the expected order */
1537
1538 ctx->payloads[PIPE_SHADER_FRAGMENT].prefix.offset_bias_correction = 0;
1539 ctx->payloads[PIPE_SHADER_FRAGMENT].prefix.index_count = MALI_POSITIVE(ctx->vertex_count);
1540
1541 /* Reverse index state */
1542 ctx->payloads[PIPE_SHADER_FRAGMENT].prefix.indices = (u64) NULL;
1543 }
1544
1545 /* Dispatch "compute jobs" for the vertex/tiler pair as (1,
1546 * vertex_count, 1) */
1547
1548 panfrost_pack_work_groups_fused(
1549 &ctx->payloads[PIPE_SHADER_VERTEX].prefix,
1550 &ctx->payloads[PIPE_SHADER_FRAGMENT].prefix,
1551 1, vertex_count, info->instance_count,
1552 1, 1, 1);
1553
1554 ctx->payloads[PIPE_SHADER_FRAGMENT].prefix.unknown_draw = draw_flags;
1555
1556 /* Encode the padded vertex count */
1557
1558 if (info->instance_count > 1) {
1559 ctx->padded_count = panfrost_padded_vertex_count(vertex_count);
1560
1561 unsigned shift = __builtin_ctz(ctx->padded_count);
1562 unsigned k = ctx->padded_count >> (shift + 1);
1563
1564 ctx->payloads[PIPE_SHADER_VERTEX].instance_shift = shift;
1565 ctx->payloads[PIPE_SHADER_FRAGMENT].instance_shift = shift;
1566
1567 ctx->payloads[PIPE_SHADER_VERTEX].instance_odd = k;
1568 ctx->payloads[PIPE_SHADER_FRAGMENT].instance_odd = k;
1569 } else {
1570 ctx->padded_count = vertex_count;
1571
1572 /* Reset instancing state */
1573 ctx->payloads[PIPE_SHADER_VERTEX].instance_shift = 0;
1574 ctx->payloads[PIPE_SHADER_VERTEX].instance_odd = 0;
1575 ctx->payloads[PIPE_SHADER_FRAGMENT].instance_shift = 0;
1576 ctx->payloads[PIPE_SHADER_FRAGMENT].instance_odd = 0;
1577 }
1578
1579 /* Fire off the draw itself */
1580 panfrost_queue_draw(ctx);
1581
1582 /* Increment transform feedback offsets */
1583
1584 for (unsigned i = 0; i < ctx->streamout.num_targets; ++i) {
1585 unsigned output_count = u_stream_outputs_for_vertices(
1586 ctx->active_prim, ctx->vertex_count);
1587
1588 ctx->streamout.offsets[i] += output_count;
1589 }
1590 }
1591
1592 /* CSO state */
1593
1594 static void
1595 panfrost_generic_cso_delete(struct pipe_context *pctx, void *hwcso)
1596 {
1597 free(hwcso);
1598 }
1599
1600 static void *
1601 panfrost_create_rasterizer_state(
1602 struct pipe_context *pctx,
1603 const struct pipe_rasterizer_state *cso)
1604 {
1605 struct panfrost_rasterizer *so = CALLOC_STRUCT(panfrost_rasterizer);
1606
1607 so->base = *cso;
1608
1609 /* Bitmask, unknown meaning of the start value. 0x105 on 32-bit T6XX */
1610 so->tiler_gl_enables = 0x7;
1611
1612 if (cso->front_ccw)
1613 so->tiler_gl_enables |= MALI_FRONT_CCW_TOP;
1614
1615 if (cso->cull_face & PIPE_FACE_FRONT)
1616 so->tiler_gl_enables |= MALI_CULL_FACE_FRONT;
1617
1618 if (cso->cull_face & PIPE_FACE_BACK)
1619 so->tiler_gl_enables |= MALI_CULL_FACE_BACK;
1620
1621 return so;
1622 }
1623
1624 static void
1625 panfrost_bind_rasterizer_state(
1626 struct pipe_context *pctx,
1627 void *hwcso)
1628 {
1629 struct panfrost_context *ctx = pan_context(pctx);
1630
1631 /* TODO: Why can't rasterizer be NULL ever? Other drivers are fine.. */
1632 if (!hwcso)
1633 return;
1634
1635 ctx->rasterizer = hwcso;
1636 ctx->dirty |= PAN_DIRTY_RASTERIZER;
1637
1638 ctx->fragment_shader_core.depth_units = ctx->rasterizer->base.offset_units * 2.0f;
1639 ctx->fragment_shader_core.depth_factor = ctx->rasterizer->base.offset_scale;
1640
1641 /* Gauranteed with the core GL call, so don't expose ARB_polygon_offset */
1642 assert(ctx->rasterizer->base.offset_clamp == 0.0);
1643
1644 /* XXX: Which bit is which? Does this maybe allow offseting not-tri? */
1645
1646 SET_BIT(ctx->fragment_shader_core.unknown2_4, MALI_DEPTH_RANGE_A, ctx->rasterizer->base.offset_tri);
1647 SET_BIT(ctx->fragment_shader_core.unknown2_4, MALI_DEPTH_RANGE_B, ctx->rasterizer->base.offset_tri);
1648
1649 /* Point sprites are emulated */
1650
1651 struct panfrost_shader_state *variant =
1652 ctx->shader[PIPE_SHADER_FRAGMENT] ? &ctx->shader[PIPE_SHADER_FRAGMENT]->variants[ctx->shader[PIPE_SHADER_FRAGMENT]->active_variant] : NULL;
1653
1654 if (ctx->rasterizer->base.sprite_coord_enable || (variant && variant->point_sprite_mask))
1655 ctx->base.bind_fs_state(&ctx->base, ctx->shader[PIPE_SHADER_FRAGMENT]);
1656 }
1657
1658 static void *
1659 panfrost_create_vertex_elements_state(
1660 struct pipe_context *pctx,
1661 unsigned num_elements,
1662 const struct pipe_vertex_element *elements)
1663 {
1664 struct panfrost_vertex_state *so = CALLOC_STRUCT(panfrost_vertex_state);
1665
1666 so->num_elements = num_elements;
1667 memcpy(so->pipe, elements, sizeof(*elements) * num_elements);
1668
1669 for (int i = 0; i < num_elements; ++i) {
1670 so->hw[i].index = i;
1671
1672 enum pipe_format fmt = elements[i].src_format;
1673 const struct util_format_description *desc = util_format_description(fmt);
1674 so->hw[i].unknown1 = 0x2;
1675 so->hw[i].swizzle = panfrost_get_default_swizzle(desc->nr_channels);
1676
1677 so->hw[i].format = panfrost_find_format(desc);
1678
1679 /* The field itself should probably be shifted over */
1680 so->hw[i].src_offset = elements[i].src_offset;
1681 }
1682
1683 return so;
1684 }
1685
1686 static void
1687 panfrost_bind_vertex_elements_state(
1688 struct pipe_context *pctx,
1689 void *hwcso)
1690 {
1691 struct panfrost_context *ctx = pan_context(pctx);
1692
1693 ctx->vertex = hwcso;
1694 ctx->dirty |= PAN_DIRTY_VERTEX;
1695 }
1696
1697 static void *
1698 panfrost_create_shader_state(
1699 struct pipe_context *pctx,
1700 const struct pipe_shader_state *cso,
1701 enum pipe_shader_type stage)
1702 {
1703 struct panfrost_shader_variants *so = CALLOC_STRUCT(panfrost_shader_variants);
1704 so->base = *cso;
1705
1706 /* Token deep copy to prevent memory corruption */
1707
1708 if (cso->type == PIPE_SHADER_IR_TGSI)
1709 so->base.tokens = tgsi_dup_tokens(so->base.tokens);
1710
1711 /* Precompile for shader-db if we need to */
1712 if (unlikely((pan_debug & PAN_DBG_PRECOMPILE) && cso->type == PIPE_SHADER_IR_NIR)) {
1713 struct panfrost_context *ctx = pan_context(pctx);
1714
1715 struct mali_shader_meta meta;
1716 struct panfrost_shader_state state;
1717 uint64_t outputs_written;
1718
1719 panfrost_shader_compile(ctx, &meta,
1720 PIPE_SHADER_IR_NIR,
1721 so->base.ir.nir,
1722 tgsi_processor_to_shader_stage(stage), &state,
1723 &outputs_written);
1724 }
1725
1726 return so;
1727 }
1728
1729 static void
1730 panfrost_delete_shader_state(
1731 struct pipe_context *pctx,
1732 void *so)
1733 {
1734 struct panfrost_shader_variants *cso = (struct panfrost_shader_variants *) so;
1735
1736 if (cso->base.type == PIPE_SHADER_IR_TGSI) {
1737 DBG("Deleting TGSI shader leaks duplicated tokens\n");
1738 }
1739
1740 for (unsigned i = 0; i < cso->variant_count; ++i) {
1741 struct panfrost_shader_state *shader_state = &cso->variants[i];
1742 panfrost_bo_unreference(shader_state->bo);
1743 shader_state->bo = NULL;
1744 }
1745
1746 free(so);
1747 }
1748
1749 static void *
1750 panfrost_create_sampler_state(
1751 struct pipe_context *pctx,
1752 const struct pipe_sampler_state *cso)
1753 {
1754 struct panfrost_sampler_state *so = CALLOC_STRUCT(panfrost_sampler_state);
1755 so->base = *cso;
1756
1757 /* sampler_state corresponds to mali_sampler_descriptor, which we can generate entirely here */
1758
1759 bool min_nearest = cso->min_img_filter == PIPE_TEX_FILTER_NEAREST;
1760 bool mag_nearest = cso->mag_img_filter == PIPE_TEX_FILTER_NEAREST;
1761 bool mip_linear = cso->min_mip_filter == PIPE_TEX_MIPFILTER_LINEAR;
1762
1763 unsigned min_filter = min_nearest ? MALI_SAMP_MIN_NEAREST : 0;
1764 unsigned mag_filter = mag_nearest ? MALI_SAMP_MAG_NEAREST : 0;
1765 unsigned mip_filter = mip_linear ?
1766 (MALI_SAMP_MIP_LINEAR_1 | MALI_SAMP_MIP_LINEAR_2) : 0;
1767 unsigned normalized = cso->normalized_coords ? MALI_SAMP_NORM_COORDS : 0;
1768
1769 struct mali_sampler_descriptor sampler_descriptor = {
1770 .filter_mode = min_filter | mag_filter | mip_filter | normalized,
1771 .wrap_s = translate_tex_wrap(cso->wrap_s),
1772 .wrap_t = translate_tex_wrap(cso->wrap_t),
1773 .wrap_r = translate_tex_wrap(cso->wrap_r),
1774 .compare_func = panfrost_translate_alt_compare_func(cso->compare_func),
1775 .border_color = {
1776 cso->border_color.f[0],
1777 cso->border_color.f[1],
1778 cso->border_color.f[2],
1779 cso->border_color.f[3]
1780 },
1781 .min_lod = FIXED_16(cso->min_lod),
1782 .max_lod = FIXED_16(cso->max_lod),
1783 .lod_bias = FIXED_16(cso->lod_bias),
1784 .seamless_cube_map = cso->seamless_cube_map,
1785 };
1786
1787 /* If necessary, we disable mipmapping in the sampler descriptor by
1788 * clamping the LOD as tight as possible (from 0 to epsilon,
1789 * essentially -- remember these are fixed point numbers, so
1790 * epsilon=1/256) */
1791
1792 if (cso->min_mip_filter == PIPE_TEX_MIPFILTER_NONE)
1793 sampler_descriptor.max_lod = sampler_descriptor.min_lod;
1794
1795 /* Enforce that there is something in the middle by adding epsilon*/
1796
1797 if (sampler_descriptor.min_lod == sampler_descriptor.max_lod)
1798 sampler_descriptor.max_lod++;
1799
1800 /* Sanity check */
1801 assert(sampler_descriptor.max_lod > sampler_descriptor.min_lod);
1802
1803 so->hw = sampler_descriptor;
1804
1805 return so;
1806 }
1807
1808 static void
1809 panfrost_bind_sampler_states(
1810 struct pipe_context *pctx,
1811 enum pipe_shader_type shader,
1812 unsigned start_slot, unsigned num_sampler,
1813 void **sampler)
1814 {
1815 assert(start_slot == 0);
1816
1817 struct panfrost_context *ctx = pan_context(pctx);
1818
1819 /* XXX: Should upload, not just copy? */
1820 ctx->sampler_count[shader] = num_sampler;
1821 memcpy(ctx->samplers[shader], sampler, num_sampler * sizeof (void *));
1822
1823 ctx->dirty |= PAN_DIRTY_SAMPLERS;
1824 }
1825
1826 static bool
1827 panfrost_variant_matches(
1828 struct panfrost_context *ctx,
1829 struct panfrost_shader_state *variant,
1830 enum pipe_shader_type type)
1831 {
1832 struct pipe_rasterizer_state *rasterizer = &ctx->rasterizer->base;
1833 struct pipe_alpha_state *alpha = &ctx->depth_stencil->alpha;
1834
1835 bool is_fragment = (type == PIPE_SHADER_FRAGMENT);
1836
1837 if (is_fragment && (alpha->enabled || variant->alpha_state.enabled)) {
1838 /* Make sure enable state is at least the same */
1839 if (alpha->enabled != variant->alpha_state.enabled) {
1840 return false;
1841 }
1842
1843 /* Check that the contents of the test are the same */
1844 bool same_func = alpha->func == variant->alpha_state.func;
1845 bool same_ref = alpha->ref_value == variant->alpha_state.ref_value;
1846
1847 if (!(same_func && same_ref)) {
1848 return false;
1849 }
1850 }
1851
1852 if (is_fragment && rasterizer && (rasterizer->sprite_coord_enable |
1853 variant->point_sprite_mask)) {
1854 /* Ensure the same varyings are turned to point sprites */
1855 if (rasterizer->sprite_coord_enable != variant->point_sprite_mask)
1856 return false;
1857
1858 /* Ensure the orientation is correct */
1859 bool upper_left =
1860 rasterizer->sprite_coord_mode ==
1861 PIPE_SPRITE_COORD_UPPER_LEFT;
1862
1863 if (variant->point_sprite_upper_left != upper_left)
1864 return false;
1865 }
1866
1867 /* Otherwise, we're good to go */
1868 return true;
1869 }
1870
1871 /**
1872 * Fix an uncompiled shader's stream output info, and produce a bitmask
1873 * of which VARYING_SLOT_* are captured for stream output.
1874 *
1875 * Core Gallium stores output->register_index as a "slot" number, where
1876 * slots are assigned consecutively to all outputs in info->outputs_written.
1877 * This naive packing of outputs doesn't work for us - we too have slots,
1878 * but the layout is defined by the VUE map, which we won't have until we
1879 * compile a specific shader variant. So, we remap these and simply store
1880 * VARYING_SLOT_* in our copy's output->register_index fields.
1881 *
1882 * We then produce a bitmask of outputs which are used for SO.
1883 *
1884 * Implementation from iris.
1885 */
1886
1887 static uint64_t
1888 update_so_info(struct pipe_stream_output_info *so_info,
1889 uint64_t outputs_written)
1890 {
1891 uint64_t so_outputs = 0;
1892 uint8_t reverse_map[64] = {0};
1893 unsigned slot = 0;
1894
1895 while (outputs_written)
1896 reverse_map[slot++] = u_bit_scan64(&outputs_written);
1897
1898 for (unsigned i = 0; i < so_info->num_outputs; i++) {
1899 struct pipe_stream_output *output = &so_info->output[i];
1900
1901 /* Map Gallium's condensed "slots" back to real VARYING_SLOT_* enums */
1902 output->register_index = reverse_map[output->register_index];
1903
1904 so_outputs |= 1ull << output->register_index;
1905 }
1906
1907 return so_outputs;
1908 }
1909
1910 static void
1911 panfrost_bind_shader_state(
1912 struct pipe_context *pctx,
1913 void *hwcso,
1914 enum pipe_shader_type type)
1915 {
1916 struct panfrost_context *ctx = pan_context(pctx);
1917
1918 ctx->shader[type] = hwcso;
1919
1920 if (type == PIPE_SHADER_FRAGMENT)
1921 ctx->dirty |= PAN_DIRTY_FS;
1922 else
1923 ctx->dirty |= PAN_DIRTY_VS;
1924
1925 if (!hwcso) return;
1926
1927 /* Match the appropriate variant */
1928
1929 signed variant = -1;
1930 struct panfrost_shader_variants *variants = (struct panfrost_shader_variants *) hwcso;
1931
1932 for (unsigned i = 0; i < variants->variant_count; ++i) {
1933 if (panfrost_variant_matches(ctx, &variants->variants[i], type)) {
1934 variant = i;
1935 break;
1936 }
1937 }
1938
1939 if (variant == -1) {
1940 /* No variant matched, so create a new one */
1941 variant = variants->variant_count++;
1942 assert(variants->variant_count < MAX_SHADER_VARIANTS);
1943
1944 struct panfrost_shader_state *v =
1945 &variants->variants[variant];
1946
1947 if (type == PIPE_SHADER_FRAGMENT) {
1948 v->alpha_state = ctx->depth_stencil->alpha;
1949
1950 if (ctx->rasterizer) {
1951 v->point_sprite_mask = ctx->rasterizer->base.sprite_coord_enable;
1952 v->point_sprite_upper_left =
1953 ctx->rasterizer->base.sprite_coord_mode ==
1954 PIPE_SPRITE_COORD_UPPER_LEFT;
1955 }
1956 }
1957
1958 variants->variants[variant].tripipe = calloc(1, sizeof(struct mali_shader_meta));
1959
1960 }
1961
1962 /* Select this variant */
1963 variants->active_variant = variant;
1964
1965 struct panfrost_shader_state *shader_state = &variants->variants[variant];
1966 assert(panfrost_variant_matches(ctx, shader_state, type));
1967
1968 /* We finally have a variant, so compile it */
1969
1970 if (!shader_state->compiled) {
1971 uint64_t outputs_written = 0;
1972
1973 panfrost_shader_compile(ctx, shader_state->tripipe,
1974 variants->base.type,
1975 variants->base.type == PIPE_SHADER_IR_NIR ?
1976 variants->base.ir.nir :
1977 variants->base.tokens,
1978 tgsi_processor_to_shader_stage(type), shader_state,
1979 &outputs_written);
1980
1981 shader_state->compiled = true;
1982
1983 /* Fixup the stream out information, since what Gallium returns
1984 * normally is mildly insane */
1985
1986 shader_state->stream_output = variants->base.stream_output;
1987 shader_state->so_mask =
1988 update_so_info(&shader_state->stream_output, outputs_written);
1989 }
1990 }
1991
1992 static void *
1993 panfrost_create_vs_state(struct pipe_context *pctx, const struct pipe_shader_state *hwcso)
1994 {
1995 return panfrost_create_shader_state(pctx, hwcso, PIPE_SHADER_VERTEX);
1996 }
1997
1998 static void *
1999 panfrost_create_fs_state(struct pipe_context *pctx, const struct pipe_shader_state *hwcso)
2000 {
2001 return panfrost_create_shader_state(pctx, hwcso, PIPE_SHADER_FRAGMENT);
2002 }
2003
2004 static void
2005 panfrost_bind_vs_state(struct pipe_context *pctx, void *hwcso)
2006 {
2007 panfrost_bind_shader_state(pctx, hwcso, PIPE_SHADER_VERTEX);
2008 }
2009
2010 static void
2011 panfrost_bind_fs_state(struct pipe_context *pctx, void *hwcso)
2012 {
2013 panfrost_bind_shader_state(pctx, hwcso, PIPE_SHADER_FRAGMENT);
2014 }
2015
2016 static void
2017 panfrost_set_vertex_buffers(
2018 struct pipe_context *pctx,
2019 unsigned start_slot,
2020 unsigned num_buffers,
2021 const struct pipe_vertex_buffer *buffers)
2022 {
2023 struct panfrost_context *ctx = pan_context(pctx);
2024
2025 util_set_vertex_buffers_mask(ctx->vertex_buffers, &ctx->vb_mask, buffers, start_slot, num_buffers);
2026 }
2027
2028 static void
2029 panfrost_set_constant_buffer(
2030 struct pipe_context *pctx,
2031 enum pipe_shader_type shader, uint index,
2032 const struct pipe_constant_buffer *buf)
2033 {
2034 struct panfrost_context *ctx = pan_context(pctx);
2035 struct panfrost_constant_buffer *pbuf = &ctx->constant_buffer[shader];
2036
2037 util_copy_constant_buffer(&pbuf->cb[index], buf);
2038
2039 unsigned mask = (1 << index);
2040
2041 if (unlikely(!buf)) {
2042 pbuf->enabled_mask &= ~mask;
2043 pbuf->dirty_mask &= ~mask;
2044 return;
2045 }
2046
2047 pbuf->enabled_mask |= mask;
2048 pbuf->dirty_mask |= mask;
2049 }
2050
2051 static void
2052 panfrost_set_stencil_ref(
2053 struct pipe_context *pctx,
2054 const struct pipe_stencil_ref *ref)
2055 {
2056 struct panfrost_context *ctx = pan_context(pctx);
2057 ctx->stencil_ref = *ref;
2058
2059 /* Shader core dirty */
2060 ctx->dirty |= PAN_DIRTY_FS;
2061 }
2062
2063 static enum mali_texture_type
2064 panfrost_translate_texture_type(enum pipe_texture_target t) {
2065 switch (t)
2066 {
2067 case PIPE_BUFFER:
2068 case PIPE_TEXTURE_1D:
2069 case PIPE_TEXTURE_1D_ARRAY:
2070 return MALI_TEX_1D;
2071
2072 case PIPE_TEXTURE_2D:
2073 case PIPE_TEXTURE_2D_ARRAY:
2074 case PIPE_TEXTURE_RECT:
2075 return MALI_TEX_2D;
2076
2077 case PIPE_TEXTURE_3D:
2078 return MALI_TEX_3D;
2079
2080 case PIPE_TEXTURE_CUBE:
2081 case PIPE_TEXTURE_CUBE_ARRAY:
2082 return MALI_TEX_CUBE;
2083
2084 default:
2085 unreachable("Unknown target");
2086 }
2087 }
2088
2089 static struct pipe_sampler_view *
2090 panfrost_create_sampler_view(
2091 struct pipe_context *pctx,
2092 struct pipe_resource *texture,
2093 const struct pipe_sampler_view *template)
2094 {
2095 struct panfrost_sampler_view *so = rzalloc(pctx, struct panfrost_sampler_view);
2096 int bytes_per_pixel = util_format_get_blocksize(texture->format);
2097
2098 pipe_reference(NULL, &texture->reference);
2099
2100 struct panfrost_resource *prsrc = (struct panfrost_resource *) texture;
2101 assert(prsrc->bo);
2102
2103 so->base = *template;
2104 so->base.texture = texture;
2105 so->base.reference.count = 1;
2106 so->base.context = pctx;
2107
2108 /* sampler_views correspond to texture descriptors, minus the texture
2109 * (data) itself. So, we serialise the descriptor here and cache it for
2110 * later. */
2111
2112 const struct util_format_description *desc = util_format_description(prsrc->base.format);
2113
2114 unsigned char user_swizzle[4] = {
2115 template->swizzle_r,
2116 template->swizzle_g,
2117 template->swizzle_b,
2118 template->swizzle_a
2119 };
2120
2121 enum mali_format format = panfrost_find_format(desc);
2122
2123 /* Check if we need to set a custom stride by computing the "expected"
2124 * stride and comparing it to what the BO actually wants. Only applies
2125 * to linear textures, since tiled/compressed textures have strict
2126 * alignment requirements for their strides as it is */
2127
2128 unsigned first_level = template->u.tex.first_level;
2129 unsigned last_level = template->u.tex.last_level;
2130
2131 if (prsrc->layout == PAN_LINEAR) {
2132 for (unsigned l = first_level; l <= last_level; ++l) {
2133 unsigned actual_stride = prsrc->slices[l].stride;
2134 unsigned width = u_minify(texture->width0, l);
2135 unsigned comp_stride = width * bytes_per_pixel;
2136
2137 if (comp_stride != actual_stride) {
2138 so->manual_stride = true;
2139 break;
2140 }
2141 }
2142 }
2143
2144 /* In the hardware, array_size refers specifically to array textures,
2145 * whereas in Gallium, it also covers cubemaps */
2146
2147 unsigned array_size = texture->array_size;
2148
2149 if (template->target == PIPE_TEXTURE_CUBE) {
2150 /* TODO: Cubemap arrays */
2151 assert(array_size == 6);
2152 array_size /= 6;
2153 }
2154
2155 struct mali_texture_descriptor texture_descriptor = {
2156 .width = MALI_POSITIVE(u_minify(texture->width0, first_level)),
2157 .height = MALI_POSITIVE(u_minify(texture->height0, first_level)),
2158 .depth = MALI_POSITIVE(u_minify(texture->depth0, first_level)),
2159 .array_size = MALI_POSITIVE(array_size),
2160
2161 .format = {
2162 .swizzle = panfrost_translate_swizzle_4(desc->swizzle),
2163 .format = format,
2164 .srgb = desc->colorspace == UTIL_FORMAT_COLORSPACE_SRGB,
2165 .type = panfrost_translate_texture_type(template->target),
2166 .unknown2 = 0x1,
2167 },
2168
2169 .swizzle = panfrost_translate_swizzle_4(user_swizzle)
2170 };
2171
2172 texture_descriptor.levels = last_level - first_level;
2173
2174 so->hw = texture_descriptor;
2175
2176 return (struct pipe_sampler_view *) so;
2177 }
2178
2179 static void
2180 panfrost_set_sampler_views(
2181 struct pipe_context *pctx,
2182 enum pipe_shader_type shader,
2183 unsigned start_slot, unsigned num_views,
2184 struct pipe_sampler_view **views)
2185 {
2186 struct panfrost_context *ctx = pan_context(pctx);
2187 unsigned new_nr = 0;
2188 unsigned i;
2189
2190 assert(start_slot == 0);
2191
2192 for (i = 0; i < num_views; ++i) {
2193 if (views[i])
2194 new_nr = i + 1;
2195 pipe_sampler_view_reference((struct pipe_sampler_view **)&ctx->sampler_views[shader][i],
2196 views[i]);
2197 }
2198
2199 for (; i < ctx->sampler_view_count[shader]; i++) {
2200 pipe_sampler_view_reference((struct pipe_sampler_view **)&ctx->sampler_views[shader][i],
2201 NULL);
2202 }
2203 ctx->sampler_view_count[shader] = new_nr;
2204
2205 ctx->dirty |= PAN_DIRTY_TEXTURES;
2206 }
2207
2208 static void
2209 panfrost_sampler_view_destroy(
2210 struct pipe_context *pctx,
2211 struct pipe_sampler_view *view)
2212 {
2213 pipe_resource_reference(&view->texture, NULL);
2214 ralloc_free(view);
2215 }
2216
2217 static void
2218 panfrost_set_shader_buffers(
2219 struct pipe_context *pctx,
2220 enum pipe_shader_type shader,
2221 unsigned start, unsigned count,
2222 const struct pipe_shader_buffer *buffers,
2223 unsigned writable_bitmask)
2224 {
2225 struct panfrost_context *ctx = pan_context(pctx);
2226
2227 util_set_shader_buffers_mask(ctx->ssbo[shader], &ctx->ssbo_mask[shader],
2228 buffers, start, count);
2229 }
2230
2231 /* Hints that a framebuffer should use AFBC where possible */
2232
2233 static void
2234 panfrost_hint_afbc(
2235 struct panfrost_screen *screen,
2236 const struct pipe_framebuffer_state *fb)
2237 {
2238 /* AFBC implemenation incomplete; hide it */
2239 if (!(pan_debug & PAN_DBG_AFBC)) return;
2240
2241 /* Hint AFBC to the resources bound to each color buffer */
2242
2243 for (unsigned i = 0; i < fb->nr_cbufs; ++i) {
2244 struct pipe_surface *surf = fb->cbufs[i];
2245 struct panfrost_resource *rsrc = pan_resource(surf->texture);
2246 panfrost_resource_hint_layout(screen, rsrc, PAN_AFBC, 1);
2247 }
2248
2249 /* Also hint it to the depth buffer */
2250
2251 if (fb->zsbuf) {
2252 struct panfrost_resource *rsrc = pan_resource(fb->zsbuf->texture);
2253 panfrost_resource_hint_layout(screen, rsrc, PAN_AFBC, 1);
2254 }
2255 }
2256
2257 static void
2258 panfrost_set_framebuffer_state(struct pipe_context *pctx,
2259 const struct pipe_framebuffer_state *fb)
2260 {
2261 struct panfrost_context *ctx = pan_context(pctx);
2262
2263 panfrost_hint_afbc(pan_screen(pctx->screen), fb);
2264 util_copy_framebuffer_state(&ctx->pipe_framebuffer, fb);
2265 ctx->batch = NULL;
2266 panfrost_invalidate_frame(ctx);
2267 }
2268
2269 static void *
2270 panfrost_create_depth_stencil_state(struct pipe_context *pipe,
2271 const struct pipe_depth_stencil_alpha_state *depth_stencil)
2272 {
2273 return mem_dup(depth_stencil, sizeof(*depth_stencil));
2274 }
2275
2276 static void
2277 panfrost_bind_depth_stencil_state(struct pipe_context *pipe,
2278 void *cso)
2279 {
2280 struct panfrost_context *ctx = pan_context(pipe);
2281 struct pipe_depth_stencil_alpha_state *depth_stencil = cso;
2282 ctx->depth_stencil = depth_stencil;
2283
2284 if (!depth_stencil)
2285 return;
2286
2287 /* Alpha does not exist in the hardware (it's not in ES3), so it's
2288 * emulated in the fragment shader */
2289
2290 if (depth_stencil->alpha.enabled) {
2291 /* We need to trigger a new shader (maybe) */
2292 ctx->base.bind_fs_state(&ctx->base, ctx->shader[PIPE_SHADER_FRAGMENT]);
2293 }
2294
2295 /* Stencil state */
2296 SET_BIT(ctx->fragment_shader_core.unknown2_4, MALI_STENCIL_TEST, depth_stencil->stencil[0].enabled);
2297
2298 panfrost_make_stencil_state(&depth_stencil->stencil[0], &ctx->fragment_shader_core.stencil_front);
2299 ctx->fragment_shader_core.stencil_mask_front = depth_stencil->stencil[0].writemask;
2300
2301 /* If back-stencil is not enabled, use the front values */
2302 bool back_enab = ctx->depth_stencil->stencil[1].enabled;
2303 unsigned back_index = back_enab ? 1 : 0;
2304
2305 panfrost_make_stencil_state(&depth_stencil->stencil[back_index], &ctx->fragment_shader_core.stencil_back);
2306 ctx->fragment_shader_core.stencil_mask_back = depth_stencil->stencil[back_index].writemask;
2307
2308 /* Depth state (TODO: Refactor) */
2309 SET_BIT(ctx->fragment_shader_core.unknown2_3, MALI_DEPTH_WRITEMASK,
2310 depth_stencil->depth.writemask);
2311
2312 int func = depth_stencil->depth.enabled ? depth_stencil->depth.func : PIPE_FUNC_ALWAYS;
2313
2314 ctx->fragment_shader_core.unknown2_3 &= ~MALI_DEPTH_FUNC_MASK;
2315 ctx->fragment_shader_core.unknown2_3 |= MALI_DEPTH_FUNC(panfrost_translate_compare_func(func));
2316
2317 /* Bounds test not implemented */
2318 assert(!depth_stencil->depth.bounds_test);
2319
2320 ctx->dirty |= PAN_DIRTY_FS;
2321 }
2322
2323 static void
2324 panfrost_delete_depth_stencil_state(struct pipe_context *pipe, void *depth)
2325 {
2326 free( depth );
2327 }
2328
2329 static void
2330 panfrost_set_sample_mask(struct pipe_context *pipe,
2331 unsigned sample_mask)
2332 {
2333 }
2334
2335 static void
2336 panfrost_set_clip_state(struct pipe_context *pipe,
2337 const struct pipe_clip_state *clip)
2338 {
2339 //struct panfrost_context *panfrost = pan_context(pipe);
2340 }
2341
2342 static void
2343 panfrost_set_viewport_states(struct pipe_context *pipe,
2344 unsigned start_slot,
2345 unsigned num_viewports,
2346 const struct pipe_viewport_state *viewports)
2347 {
2348 struct panfrost_context *ctx = pan_context(pipe);
2349
2350 assert(start_slot == 0);
2351 assert(num_viewports == 1);
2352
2353 ctx->pipe_viewport = *viewports;
2354 }
2355
2356 static void
2357 panfrost_set_scissor_states(struct pipe_context *pipe,
2358 unsigned start_slot,
2359 unsigned num_scissors,
2360 const struct pipe_scissor_state *scissors)
2361 {
2362 struct panfrost_context *ctx = pan_context(pipe);
2363
2364 assert(start_slot == 0);
2365 assert(num_scissors == 1);
2366
2367 ctx->scissor = *scissors;
2368 }
2369
2370 static void
2371 panfrost_set_polygon_stipple(struct pipe_context *pipe,
2372 const struct pipe_poly_stipple *stipple)
2373 {
2374 //struct panfrost_context *panfrost = pan_context(pipe);
2375 }
2376
2377 static void
2378 panfrost_set_active_query_state(struct pipe_context *pipe,
2379 bool enable)
2380 {
2381 struct panfrost_context *ctx = pan_context(pipe);
2382 ctx->active_queries = enable;
2383 }
2384
2385 static void
2386 panfrost_destroy(struct pipe_context *pipe)
2387 {
2388 struct panfrost_context *panfrost = pan_context(pipe);
2389
2390 if (panfrost->blitter)
2391 util_blitter_destroy(panfrost->blitter);
2392
2393 if (panfrost->blitter_wallpaper)
2394 util_blitter_destroy(panfrost->blitter_wallpaper);
2395
2396 util_unreference_framebuffer_state(&panfrost->pipe_framebuffer);
2397 u_upload_destroy(pipe->stream_uploader);
2398
2399 ralloc_free(pipe);
2400 }
2401
2402 static struct pipe_query *
2403 panfrost_create_query(struct pipe_context *pipe,
2404 unsigned type,
2405 unsigned index)
2406 {
2407 struct panfrost_query *q = rzalloc(pipe, struct panfrost_query);
2408
2409 q->type = type;
2410 q->index = index;
2411
2412 return (struct pipe_query *) q;
2413 }
2414
2415 static void
2416 panfrost_destroy_query(struct pipe_context *pipe, struct pipe_query *q)
2417 {
2418 struct panfrost_query *query = (struct panfrost_query *) q;
2419
2420 if (query->bo) {
2421 panfrost_bo_unreference(query->bo);
2422 query->bo = NULL;
2423 }
2424
2425 ralloc_free(q);
2426 }
2427
2428 static bool
2429 panfrost_begin_query(struct pipe_context *pipe, struct pipe_query *q)
2430 {
2431 struct panfrost_context *ctx = pan_context(pipe);
2432 struct panfrost_query *query = (struct panfrost_query *) q;
2433
2434 switch (query->type) {
2435 case PIPE_QUERY_OCCLUSION_COUNTER:
2436 case PIPE_QUERY_OCCLUSION_PREDICATE:
2437 case PIPE_QUERY_OCCLUSION_PREDICATE_CONSERVATIVE:
2438 /* Allocate a bo for the query results to be stored */
2439 if (!query->bo) {
2440 query->bo = panfrost_bo_create(
2441 pan_screen(ctx->base.screen),
2442 sizeof(unsigned), 0);
2443 }
2444
2445 unsigned *result = (unsigned *)query->bo->cpu;
2446 *result = 0; /* Default to 0 if nothing at all drawn. */
2447 ctx->occlusion_query = query;
2448 break;
2449
2450 /* Geometry statistics are computed in the driver. XXX: geom/tess
2451 * shaders.. */
2452
2453 case PIPE_QUERY_PRIMITIVES_GENERATED:
2454 query->start = ctx->prims_generated;
2455 break;
2456 case PIPE_QUERY_PRIMITIVES_EMITTED:
2457 query->start = ctx->tf_prims_generated;
2458 break;
2459
2460 default:
2461 fprintf(stderr, "Skipping query %u\n", query->type);
2462 break;
2463 }
2464
2465 return true;
2466 }
2467
2468 static bool
2469 panfrost_end_query(struct pipe_context *pipe, struct pipe_query *q)
2470 {
2471 struct panfrost_context *ctx = pan_context(pipe);
2472 struct panfrost_query *query = (struct panfrost_query *) q;
2473
2474 switch (query->type) {
2475 case PIPE_QUERY_OCCLUSION_COUNTER:
2476 case PIPE_QUERY_OCCLUSION_PREDICATE:
2477 case PIPE_QUERY_OCCLUSION_PREDICATE_CONSERVATIVE:
2478 ctx->occlusion_query = NULL;
2479 break;
2480 case PIPE_QUERY_PRIMITIVES_GENERATED:
2481 query->end = ctx->prims_generated;
2482 break;
2483 case PIPE_QUERY_PRIMITIVES_EMITTED:
2484 query->end = ctx->tf_prims_generated;
2485 break;
2486 }
2487
2488 return true;
2489 }
2490
2491 static bool
2492 panfrost_get_query_result(struct pipe_context *pipe,
2493 struct pipe_query *q,
2494 bool wait,
2495 union pipe_query_result *vresult)
2496 {
2497 struct panfrost_query *query = (struct panfrost_query *) q;
2498 struct panfrost_context *ctx = pan_context(pipe);
2499
2500
2501 switch (query->type) {
2502 case PIPE_QUERY_OCCLUSION_COUNTER:
2503 case PIPE_QUERY_OCCLUSION_PREDICATE:
2504 case PIPE_QUERY_OCCLUSION_PREDICATE_CONSERVATIVE:
2505 /* Flush first */
2506 panfrost_flush_all_batches(ctx, true);
2507
2508 /* Read back the query results */
2509 unsigned *result = (unsigned *) query->bo->cpu;
2510 unsigned passed = *result;
2511
2512 if (query->type == PIPE_QUERY_OCCLUSION_COUNTER) {
2513 vresult->u64 = passed;
2514 } else {
2515 vresult->b = !!passed;
2516 }
2517
2518 break;
2519
2520 case PIPE_QUERY_PRIMITIVES_GENERATED:
2521 case PIPE_QUERY_PRIMITIVES_EMITTED:
2522 panfrost_flush_all_batches(ctx, true);
2523 vresult->u64 = query->end - query->start;
2524 break;
2525
2526 default:
2527 DBG("Skipped query get %u\n", query->type);
2528 break;
2529 }
2530
2531 return true;
2532 }
2533
2534 static struct pipe_stream_output_target *
2535 panfrost_create_stream_output_target(struct pipe_context *pctx,
2536 struct pipe_resource *prsc,
2537 unsigned buffer_offset,
2538 unsigned buffer_size)
2539 {
2540 struct pipe_stream_output_target *target;
2541
2542 target = rzalloc(pctx, struct pipe_stream_output_target);
2543
2544 if (!target)
2545 return NULL;
2546
2547 pipe_reference_init(&target->reference, 1);
2548 pipe_resource_reference(&target->buffer, prsc);
2549
2550 target->context = pctx;
2551 target->buffer_offset = buffer_offset;
2552 target->buffer_size = buffer_size;
2553
2554 return target;
2555 }
2556
2557 static void
2558 panfrost_stream_output_target_destroy(struct pipe_context *pctx,
2559 struct pipe_stream_output_target *target)
2560 {
2561 pipe_resource_reference(&target->buffer, NULL);
2562 ralloc_free(target);
2563 }
2564
2565 static void
2566 panfrost_set_stream_output_targets(struct pipe_context *pctx,
2567 unsigned num_targets,
2568 struct pipe_stream_output_target **targets,
2569 const unsigned *offsets)
2570 {
2571 struct panfrost_context *ctx = pan_context(pctx);
2572 struct panfrost_streamout *so = &ctx->streamout;
2573
2574 assert(num_targets <= ARRAY_SIZE(so->targets));
2575
2576 for (unsigned i = 0; i < num_targets; i++) {
2577 if (offsets[i] != -1)
2578 so->offsets[i] = offsets[i];
2579
2580 pipe_so_target_reference(&so->targets[i], targets[i]);
2581 }
2582
2583 for (unsigned i = 0; i < so->num_targets; i++)
2584 pipe_so_target_reference(&so->targets[i], NULL);
2585
2586 so->num_targets = num_targets;
2587 }
2588
2589 struct pipe_context *
2590 panfrost_create_context(struct pipe_screen *screen, void *priv, unsigned flags)
2591 {
2592 struct panfrost_context *ctx = rzalloc(screen, struct panfrost_context);
2593 struct pipe_context *gallium = (struct pipe_context *) ctx;
2594
2595 gallium->screen = screen;
2596
2597 gallium->destroy = panfrost_destroy;
2598
2599 gallium->set_framebuffer_state = panfrost_set_framebuffer_state;
2600
2601 gallium->flush = panfrost_flush;
2602 gallium->clear = panfrost_clear;
2603 gallium->draw_vbo = panfrost_draw_vbo;
2604
2605 gallium->set_vertex_buffers = panfrost_set_vertex_buffers;
2606 gallium->set_constant_buffer = panfrost_set_constant_buffer;
2607 gallium->set_shader_buffers = panfrost_set_shader_buffers;
2608
2609 gallium->set_stencil_ref = panfrost_set_stencil_ref;
2610
2611 gallium->create_sampler_view = panfrost_create_sampler_view;
2612 gallium->set_sampler_views = panfrost_set_sampler_views;
2613 gallium->sampler_view_destroy = panfrost_sampler_view_destroy;
2614
2615 gallium->create_rasterizer_state = panfrost_create_rasterizer_state;
2616 gallium->bind_rasterizer_state = panfrost_bind_rasterizer_state;
2617 gallium->delete_rasterizer_state = panfrost_generic_cso_delete;
2618
2619 gallium->create_vertex_elements_state = panfrost_create_vertex_elements_state;
2620 gallium->bind_vertex_elements_state = panfrost_bind_vertex_elements_state;
2621 gallium->delete_vertex_elements_state = panfrost_generic_cso_delete;
2622
2623 gallium->create_fs_state = panfrost_create_fs_state;
2624 gallium->delete_fs_state = panfrost_delete_shader_state;
2625 gallium->bind_fs_state = panfrost_bind_fs_state;
2626
2627 gallium->create_vs_state = panfrost_create_vs_state;
2628 gallium->delete_vs_state = panfrost_delete_shader_state;
2629 gallium->bind_vs_state = panfrost_bind_vs_state;
2630
2631 gallium->create_sampler_state = panfrost_create_sampler_state;
2632 gallium->delete_sampler_state = panfrost_generic_cso_delete;
2633 gallium->bind_sampler_states = panfrost_bind_sampler_states;
2634
2635 gallium->create_depth_stencil_alpha_state = panfrost_create_depth_stencil_state;
2636 gallium->bind_depth_stencil_alpha_state = panfrost_bind_depth_stencil_state;
2637 gallium->delete_depth_stencil_alpha_state = panfrost_delete_depth_stencil_state;
2638
2639 gallium->set_sample_mask = panfrost_set_sample_mask;
2640
2641 gallium->set_clip_state = panfrost_set_clip_state;
2642 gallium->set_viewport_states = panfrost_set_viewport_states;
2643 gallium->set_scissor_states = panfrost_set_scissor_states;
2644 gallium->set_polygon_stipple = panfrost_set_polygon_stipple;
2645 gallium->set_active_query_state = panfrost_set_active_query_state;
2646
2647 gallium->create_query = panfrost_create_query;
2648 gallium->destroy_query = panfrost_destroy_query;
2649 gallium->begin_query = panfrost_begin_query;
2650 gallium->end_query = panfrost_end_query;
2651 gallium->get_query_result = panfrost_get_query_result;
2652
2653 gallium->create_stream_output_target = panfrost_create_stream_output_target;
2654 gallium->stream_output_target_destroy = panfrost_stream_output_target_destroy;
2655 gallium->set_stream_output_targets = panfrost_set_stream_output_targets;
2656
2657 panfrost_resource_context_init(gallium);
2658 panfrost_blend_context_init(gallium);
2659 panfrost_compute_context_init(gallium);
2660
2661 /* XXX: leaks */
2662 gallium->stream_uploader = u_upload_create_default(gallium);
2663 gallium->const_uploader = gallium->stream_uploader;
2664 assert(gallium->stream_uploader);
2665
2666 /* Midgard supports ES modes, plus QUADS/QUAD_STRIPS/POLYGON */
2667 ctx->draw_modes = (1 << (PIPE_PRIM_POLYGON + 1)) - 1;
2668
2669 ctx->primconvert = util_primconvert_create(gallium, ctx->draw_modes);
2670
2671 ctx->blitter = util_blitter_create(gallium);
2672 ctx->blitter_wallpaper = util_blitter_create(gallium);
2673
2674 assert(ctx->blitter);
2675 assert(ctx->blitter_wallpaper);
2676
2677 /* Prepare for render! */
2678
2679 panfrost_batch_init(ctx);
2680 panfrost_emit_vertex_payload(ctx);
2681 panfrost_emit_tiler_payload(ctx);
2682 panfrost_invalidate_frame(ctx);
2683 panfrost_default_shader_backend(ctx);
2684
2685 return gallium;
2686 }