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