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