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