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