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