panfrost: Disable the tiler for clear-only jobs
[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 if (!ctx->sampler_count[t]) continue;
904
905 size_t transfer_size = desc_size * ctx->sampler_count[t];
906
907 struct panfrost_transfer transfer =
908 panfrost_allocate_transient(ctx, transfer_size);
909
910 struct mali_sampler_descriptor *desc =
911 (struct mali_sampler_descriptor *) transfer.cpu;
912
913 for (int i = 0; i < ctx->sampler_count[t]; ++i)
914 desc[i] = ctx->samplers[t][i]->hw;
915
916 if (t == PIPE_SHADER_FRAGMENT)
917 ctx->payload_tiler.postfix.sampler_descriptor = transfer.gpu;
918 else if (t == PIPE_SHADER_VERTEX)
919 ctx->payload_vertex.postfix.sampler_descriptor = transfer.gpu;
920 else
921 assert(0);
922 }
923 }
924
925 /* Computes the address to a texture at a particular slice */
926
927 static mali_ptr
928 panfrost_get_texture_address(
929 struct panfrost_resource *rsrc,
930 unsigned level, unsigned face)
931 {
932 unsigned level_offset = rsrc->bo->slices[level].offset;
933 unsigned face_offset = face * rsrc->bo->cubemap_stride;
934
935 return rsrc->bo->gpu + level_offset + face_offset;
936
937 }
938
939 static mali_ptr
940 panfrost_upload_tex(
941 struct panfrost_context *ctx,
942 struct panfrost_sampler_view *view)
943 {
944 if (!view)
945 return (mali_ptr) NULL;
946
947 struct pipe_resource *tex_rsrc = view->base.texture;
948 struct panfrost_resource *rsrc = (struct panfrost_resource *) tex_rsrc;
949
950 /* Do we interleave an explicit stride with every element? */
951
952 bool has_manual_stride =
953 view->hw.format.usage2 & MALI_TEX_MANUAL_STRIDE;
954
955 /* Inject the addresses in, interleaving mip levels, cube faces, and
956 * strides in that order */
957
958 unsigned idx = 0;
959
960 for (unsigned l = 0; l <= tex_rsrc->last_level; ++l) {
961 for (unsigned f = 0; f < tex_rsrc->array_size; ++f) {
962 view->hw.payload[idx++] =
963 panfrost_get_texture_address(rsrc, l, f);
964
965 if (has_manual_stride) {
966 view->hw.payload[idx++] =
967 rsrc->bo->slices[l].stride;
968 }
969 }
970 }
971
972 return panfrost_upload_transient(ctx, &view->hw,
973 sizeof(struct mali_texture_descriptor));
974 }
975
976 static void
977 panfrost_upload_texture_descriptors(struct panfrost_context *ctx)
978 {
979 for (int t = 0; t <= PIPE_SHADER_FRAGMENT; ++t) {
980 /* Shortcircuit */
981 if (!ctx->sampler_view_count[t]) continue;
982
983 uint64_t trampolines[PIPE_MAX_SHADER_SAMPLER_VIEWS];
984
985 for (int i = 0; i < ctx->sampler_view_count[t]; ++i)
986 trampolines[i] =
987 panfrost_upload_tex(ctx, ctx->sampler_views[t][i]);
988
989 mali_ptr trampoline = panfrost_upload_transient(ctx, trampolines, sizeof(uint64_t) * ctx->sampler_view_count[t]);
990
991 if (t == PIPE_SHADER_FRAGMENT)
992 ctx->payload_tiler.postfix.texture_trampoline = trampoline;
993 else if (t == PIPE_SHADER_VERTEX)
994 ctx->payload_vertex.postfix.texture_trampoline = trampoline;
995 else
996 assert(0);
997 }
998 }
999
1000 /* Go through dirty flags and actualise them in the cmdstream. */
1001
1002 void
1003 panfrost_emit_for_draw(struct panfrost_context *ctx, bool with_vertex_data)
1004 {
1005 struct panfrost_job *job = panfrost_get_job_for_fbo(ctx);
1006
1007 if (with_vertex_data) {
1008 panfrost_emit_vertex_data(ctx, job);
1009 }
1010
1011 bool msaa = ctx->rasterizer->base.multisample;
1012
1013 if (ctx->dirty & PAN_DIRTY_RASTERIZER) {
1014 ctx->payload_tiler.gl_enables = ctx->rasterizer->tiler_gl_enables;
1015
1016 /* TODO: Sample size */
1017 SET_BIT(ctx->fragment_shader_core.unknown2_3, MALI_HAS_MSAA, msaa);
1018 SET_BIT(ctx->fragment_shader_core.unknown2_4, MALI_NO_MSAA, !msaa);
1019 }
1020
1021 /* Enable job requirements at draw-time */
1022
1023 if (msaa)
1024 job->requirements |= PAN_REQ_MSAA;
1025
1026 if (ctx->depth_stencil->depth.writemask)
1027 job->requirements |= PAN_REQ_DEPTH_WRITE;
1028
1029 if (ctx->occlusion_query) {
1030 ctx->payload_tiler.gl_enables |= MALI_OCCLUSION_QUERY | MALI_OCCLUSION_PRECISE;
1031 ctx->payload_tiler.postfix.occlusion_counter = ctx->occlusion_query->transfer.gpu;
1032 }
1033
1034 if (ctx->dirty & PAN_DIRTY_VS) {
1035 assert(ctx->vs);
1036
1037 struct panfrost_shader_state *vs = &ctx->vs->variants[ctx->vs->active_variant];
1038
1039 /* Late shader descriptor assignments */
1040
1041 vs->tripipe->texture_count = ctx->sampler_view_count[PIPE_SHADER_VERTEX];
1042 vs->tripipe->sampler_count = ctx->sampler_count[PIPE_SHADER_VERTEX];
1043
1044 /* Who knows */
1045 vs->tripipe->midgard1.unknown1 = 0x2201;
1046
1047 ctx->payload_vertex.postfix._shader_upper = vs->tripipe_gpu >> 4;
1048 }
1049
1050 if (ctx->dirty & (PAN_DIRTY_RASTERIZER | PAN_DIRTY_VS)) {
1051 /* Check if we need to link the gl_PointSize varying */
1052 if (!panfrost_writes_point_size(ctx)) {
1053 /* If the size is constant, write it out. Otherwise,
1054 * don't touch primitive_size (since we would clobber
1055 * the pointer there) */
1056
1057 ctx->payload_tiler.primitive_size.constant = ctx->rasterizer->base.line_width;
1058 }
1059 }
1060
1061 /* TODO: Maybe dirty track FS, maybe not. For now, it's transient. */
1062 if (ctx->fs)
1063 ctx->dirty |= PAN_DIRTY_FS;
1064
1065 if (ctx->dirty & PAN_DIRTY_FS) {
1066 assert(ctx->fs);
1067 struct panfrost_shader_state *variant = &ctx->fs->variants[ctx->fs->active_variant];
1068
1069 #define COPY(name) ctx->fragment_shader_core.name = variant->tripipe->name
1070
1071 COPY(shader);
1072 COPY(attribute_count);
1073 COPY(varying_count);
1074 COPY(midgard1.uniform_count);
1075 COPY(midgard1.work_count);
1076 COPY(midgard1.unknown2);
1077
1078 #undef COPY
1079 /* If there is a blend shader, work registers are shared */
1080
1081 if (ctx->blend->has_blend_shader)
1082 ctx->fragment_shader_core.midgard1.work_count = /*MAX2(ctx->fragment_shader_core.midgard1.work_count, ctx->blend->blend_work_count)*/16;
1083
1084 /* Set late due to depending on render state */
1085 /* The one at the end seems to mean "1 UBO" */
1086 ctx->fragment_shader_core.midgard1.unknown1 = MALI_NO_ALPHA_TO_COVERAGE | 0x200 | 0x2201;
1087
1088 /* Assign texture/sample count right before upload */
1089 ctx->fragment_shader_core.texture_count = ctx->sampler_view_count[PIPE_SHADER_FRAGMENT];
1090 ctx->fragment_shader_core.sampler_count = ctx->sampler_count[PIPE_SHADER_FRAGMENT];
1091
1092 /* Assign the stencil refs late */
1093 ctx->fragment_shader_core.stencil_front.ref = ctx->stencil_ref.ref_value[0];
1094 ctx->fragment_shader_core.stencil_back.ref = ctx->stencil_ref.ref_value[1];
1095
1096 /* CAN_DISCARD should be set if the fragment shader possibly
1097 * contains a 'discard' instruction. It is likely this is
1098 * related to optimizations related to forward-pixel kill, as
1099 * per "Mali Performance 3: Is EGL_BUFFER_PRESERVED a good
1100 * thing?" by Peter Harris
1101 */
1102
1103 if (variant->can_discard) {
1104 ctx->fragment_shader_core.unknown2_3 |= MALI_CAN_DISCARD;
1105 ctx->fragment_shader_core.midgard1.unknown1 &= ~MALI_NO_ALPHA_TO_COVERAGE;
1106 ctx->fragment_shader_core.midgard1.unknown1 |= 0x4000;
1107 ctx->fragment_shader_core.midgard1.unknown1 = 0x4200;
1108 }
1109
1110 /* Check if we're using the default blend descriptor (fast path) */
1111
1112 bool no_blending =
1113 !ctx->blend->has_blend_shader &&
1114 (ctx->blend->equation.rgb_mode == 0x122) &&
1115 (ctx->blend->equation.alpha_mode == 0x122) &&
1116 (ctx->blend->equation.color_mask == 0xf);
1117
1118 /* Even on MFBD, the shader descriptor gets blend shaders. It's
1119 * *also* copied to the blend_meta appended (by convention),
1120 * but this is the field actually read by the hardware. (Or
1121 * maybe both are read...?) */
1122
1123 if (ctx->blend->has_blend_shader) {
1124 ctx->fragment_shader_core.blend.shader = ctx->blend->blend_shader;
1125 }
1126
1127 if (ctx->require_sfbd) {
1128 /* When only a single render target platform is used, the blend
1129 * information is inside the shader meta itself. We
1130 * additionally need to signal CAN_DISCARD for nontrivial blend
1131 * modes (so we're able to read back the destination buffer) */
1132
1133 if (!ctx->blend->has_blend_shader) {
1134 ctx->fragment_shader_core.blend.equation = ctx->blend->equation;
1135 ctx->fragment_shader_core.blend.constant = ctx->blend->constant;
1136 }
1137
1138 if (!no_blending) {
1139 ctx->fragment_shader_core.unknown2_3 |= MALI_CAN_DISCARD;
1140 }
1141 }
1142
1143 size_t size = sizeof(struct mali_shader_meta) + sizeof(struct midgard_blend_rt);
1144 struct panfrost_transfer transfer = panfrost_allocate_transient(ctx, size);
1145 memcpy(transfer.cpu, &ctx->fragment_shader_core, sizeof(struct mali_shader_meta));
1146
1147 ctx->payload_tiler.postfix._shader_upper = (transfer.gpu) >> 4;
1148
1149 if (!ctx->require_sfbd) {
1150 /* Additional blend descriptor tacked on for jobs using MFBD */
1151
1152 unsigned blend_count = 0x200;
1153
1154 if (ctx->blend->has_blend_shader) {
1155 /* For a blend shader, the bottom nibble corresponds to
1156 * the number of work registers used, which signals the
1157 * -existence- of a blend shader */
1158
1159 assert(ctx->blend->blend_work_count >= 2);
1160 blend_count |= MIN2(ctx->blend->blend_work_count, 3);
1161 } else {
1162 /* Otherwise, the bottom bit simply specifies if
1163 * blending (anything other than REPLACE) is enabled */
1164
1165
1166 if (!no_blending)
1167 blend_count |= 0x1;
1168 }
1169
1170 struct midgard_blend_rt rts[4];
1171
1172 /* TODO: MRT */
1173
1174 for (unsigned i = 0; i < 1; ++i) {
1175 rts[i].flags = blend_count;
1176
1177 if (ctx->blend->has_blend_shader) {
1178 rts[i].blend.shader = ctx->blend->blend_shader;
1179 } else {
1180 rts[i].blend.equation = ctx->blend->equation;
1181 rts[i].blend.constant = ctx->blend->constant;
1182 }
1183 }
1184
1185 memcpy(transfer.cpu + sizeof(struct mali_shader_meta), rts, sizeof(rts[0]) * 1);
1186 }
1187 }
1188
1189 /* We stage to transient, so always dirty.. */
1190 panfrost_stage_attributes(ctx);
1191
1192 if (ctx->dirty & PAN_DIRTY_SAMPLERS)
1193 panfrost_upload_sampler_descriptors(ctx);
1194
1195 if (ctx->dirty & PAN_DIRTY_TEXTURES)
1196 panfrost_upload_texture_descriptors(ctx);
1197
1198 const struct pipe_viewport_state *vp = &ctx->pipe_viewport;
1199
1200 for (int i = 0; i <= PIPE_SHADER_FRAGMENT; ++i) {
1201 struct panfrost_constant_buffer *buf = &ctx->constant_buffer[i];
1202
1203 struct panfrost_shader_state *vs = &ctx->vs->variants[ctx->vs->active_variant];
1204 struct panfrost_shader_state *fs = &ctx->fs->variants[ctx->fs->active_variant];
1205 struct panfrost_shader_state *ss = (i == PIPE_SHADER_FRAGMENT) ? fs : vs;
1206
1207 /* Allocate room for the sysval and the uniforms */
1208 size_t sys_size = sizeof(float) * 4 * ss->sysval_count;
1209 size_t size = sys_size + buf->size;
1210 struct panfrost_transfer transfer = panfrost_allocate_transient(ctx, size);
1211
1212 /* Upload sysvals requested by the shader */
1213 float *uniforms = (float *) transfer.cpu;
1214 for (unsigned i = 0; i < ss->sysval_count; ++i) {
1215 int sysval = ss->sysval[i];
1216
1217 if (sysval == PAN_SYSVAL_VIEWPORT_SCALE) {
1218 uniforms[4*i + 0] = vp->scale[0];
1219 uniforms[4*i + 1] = vp->scale[1];
1220 uniforms[4*i + 2] = vp->scale[2];
1221 } else if (sysval == PAN_SYSVAL_VIEWPORT_OFFSET) {
1222 uniforms[4*i + 0] = vp->translate[0];
1223 uniforms[4*i + 1] = vp->translate[1];
1224 uniforms[4*i + 2] = vp->translate[2];
1225 } else {
1226 assert(0);
1227 }
1228 }
1229
1230 /* Upload uniforms */
1231 memcpy(transfer.cpu + sys_size, buf->buffer, buf->size);
1232
1233 int uniform_count = 0;
1234
1235 struct mali_vertex_tiler_postfix *postfix;
1236
1237 switch (i) {
1238 case PIPE_SHADER_VERTEX:
1239 uniform_count = ctx->vs->variants[ctx->vs->active_variant].uniform_count;
1240 postfix = &ctx->payload_vertex.postfix;
1241 break;
1242
1243 case PIPE_SHADER_FRAGMENT:
1244 uniform_count = ctx->fs->variants[ctx->fs->active_variant].uniform_count;
1245 postfix = &ctx->payload_tiler.postfix;
1246 break;
1247
1248 default:
1249 unreachable("Invalid shader stage\n");
1250 }
1251
1252 /* Also attach the same buffer as a UBO for extended access */
1253
1254 struct mali_uniform_buffer_meta uniform_buffers[] = {
1255 {
1256 .size = MALI_POSITIVE((2 + uniform_count)),
1257 .ptr = transfer.gpu >> 2,
1258 },
1259 };
1260
1261 mali_ptr ubufs = panfrost_upload_transient(ctx, uniform_buffers, sizeof(uniform_buffers));
1262 postfix->uniforms = transfer.gpu;
1263 postfix->uniform_buffers = ubufs;
1264
1265 buf->dirty = 0;
1266 }
1267
1268 /* TODO: Upload the viewport somewhere more appropriate */
1269
1270 /* Clip bounds are encoded as floats. The viewport itself is encoded as
1271 * (somewhat) asymmetric ints. */
1272 const struct pipe_scissor_state *ss = &ctx->scissor;
1273
1274 struct mali_viewport view = {
1275 /* By default, do no viewport clipping, i.e. clip to (-inf,
1276 * inf) in each direction. Clipping to the viewport in theory
1277 * should work, but in practice causes issues when we're not
1278 * explicitly trying to scissor */
1279
1280 .clip_minx = -inff,
1281 .clip_miny = -inff,
1282 .clip_maxx = inff,
1283 .clip_maxy = inff,
1284
1285 .clip_minz = 0.0,
1286 .clip_maxz = 1.0,
1287 };
1288
1289 /* Always scissor to the viewport by default. */
1290 view.viewport0[0] = (int) (vp->translate[0] - vp->scale[0]);
1291 view.viewport1[0] = MALI_POSITIVE((int) (vp->translate[0] + vp->scale[0]));
1292
1293 int miny = (int) (vp->translate[1] - vp->scale[1]);
1294 int maxy = (int) (vp->translate[1] + vp->scale[1]);
1295
1296 if (ss && ctx->rasterizer && ctx->rasterizer->base.scissor) {
1297 view.viewport0[0] = ss->minx;
1298 view.viewport1[0] = MALI_POSITIVE(ss->maxx);
1299
1300 miny = ss->miny;
1301 maxy = ss->maxy;
1302 }
1303
1304 /* Hardware needs the min/max to be strictly ordered, so flip if we
1305 * need to */
1306 if (miny > maxy) {
1307 int temp = miny;
1308 miny = maxy;
1309 maxy = temp;
1310 }
1311
1312 view.viewport0[1] = miny;
1313 view.viewport1[1] = MALI_POSITIVE(maxy);
1314
1315 ctx->payload_tiler.postfix.viewport =
1316 panfrost_upload_transient(ctx,
1317 &view,
1318 sizeof(struct mali_viewport));
1319
1320 ctx->dirty = 0;
1321 }
1322
1323 /* Corresponds to exactly one draw, but does not submit anything */
1324
1325 static void
1326 panfrost_queue_draw(struct panfrost_context *ctx)
1327 {
1328 /* TODO: Expand the array? */
1329 if (ctx->draw_count >= MAX_DRAW_CALLS) {
1330 DBG("Job buffer overflow, ignoring draw\n");
1331 assert(0);
1332 }
1333
1334 /* Handle dirty flags now */
1335 panfrost_emit_for_draw(ctx, true);
1336
1337 /* We need a set_value job before any other draw jobs */
1338 if (ctx->draw_count == 0)
1339 panfrost_set_value_job(ctx);
1340
1341 struct panfrost_transfer vertex = panfrost_vertex_tiler_job(ctx, false);
1342 ctx->u_vertex_jobs[ctx->vertex_job_count] = (struct mali_job_descriptor_header *) vertex.cpu;
1343 ctx->vertex_jobs[ctx->vertex_job_count++] = vertex.gpu;
1344
1345 struct panfrost_transfer tiler = panfrost_vertex_tiler_job(ctx, true);
1346 ctx->u_tiler_jobs[ctx->tiler_job_count] = (struct mali_job_descriptor_header *) tiler.cpu;
1347 ctx->tiler_jobs[ctx->tiler_job_count++] = tiler.gpu;
1348
1349 ctx->draw_count++;
1350 }
1351
1352 /* The entire frame is in memory -- send it off to the kernel! */
1353
1354 static void
1355 panfrost_submit_frame(struct panfrost_context *ctx, bool flush_immediate,
1356 struct pipe_fence_handle **fence,
1357 struct panfrost_job *job)
1358 {
1359 struct pipe_context *gallium = (struct pipe_context *) ctx;
1360 struct panfrost_screen *screen = pan_screen(gallium->screen);
1361
1362 /* Edge case if screen is cleared and nothing else */
1363 bool has_draws = ctx->draw_count > 0;
1364
1365 /* Workaround a bizarre lockup (a hardware errata?) */
1366 if (!has_draws)
1367 flush_immediate = true;
1368
1369 #ifndef DRY_RUN
1370
1371 bool is_scanout = panfrost_is_scanout(ctx);
1372 screen->driver->submit_vs_fs_job(ctx, has_draws, is_scanout);
1373
1374 /* If visual, we can stall a frame */
1375
1376 if (!flush_immediate)
1377 screen->driver->force_flush_fragment(ctx, fence);
1378
1379 screen->last_fragment_flushed = false;
1380 screen->last_job = job;
1381
1382 /* If readback, flush now (hurts the pipelined performance) */
1383 if (flush_immediate)
1384 screen->driver->force_flush_fragment(ctx, fence);
1385
1386 if (screen->driver->dump_counters && pan_counters_base) {
1387 screen->driver->dump_counters(screen);
1388
1389 char filename[128];
1390 snprintf(filename, sizeof(filename), "%s/frame%d.mdgprf", pan_counters_base, ++performance_counter_number);
1391 FILE *fp = fopen(filename, "wb");
1392 fwrite(screen->perf_counters.cpu, 4096, sizeof(uint32_t), fp);
1393 fclose(fp);
1394 }
1395
1396 #endif
1397 }
1398
1399 static void
1400 panfrost_draw_wallpaper(struct pipe_context *pipe)
1401 {
1402 struct panfrost_context *ctx = pan_context(pipe);
1403
1404 /* Nothing to reload? */
1405 if (ctx->pipe_framebuffer.cbufs[0] == NULL)
1406 return;
1407
1408 /* Blit the wallpaper in */
1409 panfrost_blit_wallpaper(ctx);
1410
1411 /* We are flushing all queued draws and we know that no more jobs will
1412 * be added until the next frame.
1413 * We also know that the last jobs are the wallpaper jobs, and they
1414 * need to be linked so they execute right after the set_value job.
1415 */
1416
1417 /* set_value job to wallpaper vertex job */
1418 panfrost_link_job_pair(ctx->u_set_value_job, ctx->vertex_jobs[ctx->vertex_job_count - 1]);
1419 ctx->u_vertex_jobs[ctx->vertex_job_count - 1]->job_dependency_index_1 = ctx->u_set_value_job->job_index;
1420
1421 /* wallpaper vertex job to first vertex job */
1422 panfrost_link_job_pair(ctx->u_vertex_jobs[ctx->vertex_job_count - 1], ctx->vertex_jobs[0]);
1423 ctx->u_vertex_jobs[0]->job_dependency_index_1 = ctx->u_set_value_job->job_index;
1424
1425 /* last vertex job to wallpaper tiler job */
1426 panfrost_link_job_pair(ctx->u_vertex_jobs[ctx->vertex_job_count - 2], ctx->tiler_jobs[ctx->tiler_job_count - 1]);
1427 ctx->u_tiler_jobs[ctx->tiler_job_count - 1]->job_dependency_index_1 = ctx->u_vertex_jobs[ctx->vertex_job_count - 1]->job_index;
1428 ctx->u_tiler_jobs[ctx->tiler_job_count - 1]->job_dependency_index_2 = 0;
1429
1430 /* wallpaper tiler job to first tiler job */
1431 panfrost_link_job_pair(ctx->u_tiler_jobs[ctx->tiler_job_count - 1], ctx->tiler_jobs[0]);
1432 ctx->u_tiler_jobs[0]->job_dependency_index_1 = ctx->u_vertex_jobs[0]->job_index;
1433 ctx->u_tiler_jobs[0]->job_dependency_index_2 = ctx->u_tiler_jobs[ctx->tiler_job_count - 1]->job_index;
1434
1435 /* last tiler job to NULL */
1436 panfrost_link_job_pair(ctx->u_tiler_jobs[ctx->tiler_job_count - 2], 0);
1437 }
1438
1439 void
1440 panfrost_flush(
1441 struct pipe_context *pipe,
1442 struct pipe_fence_handle **fence,
1443 unsigned flags)
1444 {
1445 struct panfrost_context *ctx = pan_context(pipe);
1446 struct panfrost_job *job = panfrost_get_job_for_fbo(ctx);
1447
1448 /* Nothing to do! */
1449 if (!ctx->draw_count && !job->clear) return;
1450
1451 if (!job->clear)
1452 panfrost_draw_wallpaper(&ctx->base);
1453
1454 /* Whether to stall the pipeline for immediately correct results */
1455 bool flush_immediate = flags & PIPE_FLUSH_END_OF_FRAME;
1456
1457 /* Submit the frame itself */
1458 panfrost_submit_frame(ctx, flush_immediate, fence, job);
1459
1460 /* Prepare for the next frame */
1461 panfrost_invalidate_frame(ctx);
1462 }
1463
1464 #define DEFINE_CASE(c) case PIPE_PRIM_##c: return MALI_##c;
1465
1466 static int
1467 g2m_draw_mode(enum pipe_prim_type mode)
1468 {
1469 switch (mode) {
1470 DEFINE_CASE(POINTS);
1471 DEFINE_CASE(LINES);
1472 DEFINE_CASE(LINE_LOOP);
1473 DEFINE_CASE(LINE_STRIP);
1474 DEFINE_CASE(TRIANGLES);
1475 DEFINE_CASE(TRIANGLE_STRIP);
1476 DEFINE_CASE(TRIANGLE_FAN);
1477 DEFINE_CASE(QUADS);
1478 DEFINE_CASE(QUAD_STRIP);
1479 DEFINE_CASE(POLYGON);
1480
1481 default:
1482 unreachable("Invalid draw mode");
1483 }
1484 }
1485
1486 #undef DEFINE_CASE
1487
1488 static unsigned
1489 panfrost_translate_index_size(unsigned size)
1490 {
1491 switch (size) {
1492 case 1:
1493 return MALI_DRAW_INDEXED_UINT8;
1494
1495 case 2:
1496 return MALI_DRAW_INDEXED_UINT16;
1497
1498 case 4:
1499 return MALI_DRAW_INDEXED_UINT32;
1500
1501 default:
1502 unreachable("Invalid index size");
1503 }
1504 }
1505
1506 /* Gets a GPU address for the associated index buffer. Only gauranteed to be
1507 * good for the duration of the draw (transient), could last longer */
1508
1509 static mali_ptr
1510 panfrost_get_index_buffer_mapped(struct panfrost_context *ctx, const struct pipe_draw_info *info)
1511 {
1512 struct panfrost_resource *rsrc = (struct panfrost_resource *) (info->index.resource);
1513
1514 off_t offset = info->start * info->index_size;
1515
1516 if (!info->has_user_indices) {
1517 /* Only resources can be directly mapped */
1518 return rsrc->bo->gpu + offset;
1519 } else {
1520 /* Otherwise, we need to upload to transient memory */
1521 const uint8_t *ibuf8 = (const uint8_t *) info->index.user;
1522 return panfrost_upload_transient(ctx, ibuf8 + offset, info->count * info->index_size);
1523 }
1524 }
1525
1526 static void
1527 panfrost_draw_vbo(
1528 struct pipe_context *pipe,
1529 const struct pipe_draw_info *info)
1530 {
1531 struct panfrost_context *ctx = pan_context(pipe);
1532
1533 ctx->payload_vertex.draw_start = info->start;
1534 ctx->payload_tiler.draw_start = info->start;
1535
1536 int mode = info->mode;
1537
1538 /* Fallback for unsupported modes */
1539
1540 if (!(ctx->draw_modes & (1 << mode))) {
1541 if (mode == PIPE_PRIM_QUADS && info->count == 4 && ctx->rasterizer && !ctx->rasterizer->base.flatshade) {
1542 mode = PIPE_PRIM_TRIANGLE_FAN;
1543 } else {
1544 if (info->count < 4) {
1545 /* Degenerate case? */
1546 return;
1547 }
1548
1549 util_primconvert_save_rasterizer_state(ctx->primconvert, &ctx->rasterizer->base);
1550 util_primconvert_draw_vbo(ctx->primconvert, info);
1551 return;
1552 }
1553 }
1554
1555 /* Now that we have a guaranteed terminating path, find the job.
1556 * Assignment commented out to prevent unused warning */
1557
1558 /* struct panfrost_job *job = */ panfrost_get_job_for_fbo(ctx);
1559
1560 ctx->payload_tiler.prefix.draw_mode = g2m_draw_mode(mode);
1561
1562 ctx->vertex_count = info->count;
1563
1564 /* For non-indexed draws, they're the same */
1565 unsigned invocation_count = ctx->vertex_count;
1566
1567 unsigned draw_flags = 0;
1568
1569 /* The draw flags interpret how primitive size is interpreted */
1570
1571 if (panfrost_writes_point_size(ctx))
1572 draw_flags |= MALI_DRAW_VARYING_SIZE;
1573
1574 /* For higher amounts of vertices (greater than what fits in a 16-bit
1575 * short), the other value is needed, otherwise there will be bizarre
1576 * rendering artefacts. It's not clear what these values mean yet. */
1577
1578 draw_flags |= (mode == PIPE_PRIM_POINTS || ctx->vertex_count > 65535) ? 0x3000 : 0x18000;
1579
1580 if (info->index_size) {
1581 /* Calculate the min/max index used so we can figure out how
1582 * many times to invoke the vertex shader */
1583
1584 /* Fetch / calculate index bounds */
1585 unsigned min_index = 0, max_index = 0;
1586
1587 if (info->max_index == ~0u) {
1588 u_vbuf_get_minmax_index(pipe, info, &min_index, &max_index);
1589 } else {
1590 min_index = info->min_index;
1591 max_index = info->max_index;
1592 }
1593
1594 /* Use the corresponding values */
1595 invocation_count = max_index - min_index + 1;
1596 ctx->payload_vertex.draw_start = min_index;
1597 ctx->payload_tiler.draw_start = min_index;
1598
1599 ctx->payload_tiler.prefix.negative_start = -min_index;
1600 ctx->payload_tiler.prefix.index_count = MALI_POSITIVE(info->count);
1601
1602 //assert(!info->restart_index); /* TODO: Research */
1603 assert(!info->index_bias);
1604
1605 draw_flags |= panfrost_translate_index_size(info->index_size);
1606 ctx->payload_tiler.prefix.indices = panfrost_get_index_buffer_mapped(ctx, info);
1607 } else {
1608 /* Index count == vertex count, if no indexing is applied, as
1609 * if it is internally indexed in the expected order */
1610
1611 ctx->payload_tiler.prefix.negative_start = 0;
1612 ctx->payload_tiler.prefix.index_count = MALI_POSITIVE(ctx->vertex_count);
1613
1614 /* Reverse index state */
1615 ctx->payload_tiler.prefix.indices = (uintptr_t) NULL;
1616 }
1617
1618 ctx->payload_vertex.prefix.invocation_count = MALI_POSITIVE(invocation_count);
1619 ctx->payload_tiler.prefix.invocation_count = MALI_POSITIVE(invocation_count);
1620 ctx->payload_tiler.prefix.unknown_draw = draw_flags;
1621
1622 /* Fire off the draw itself */
1623 panfrost_queue_draw(ctx);
1624 }
1625
1626 /* CSO state */
1627
1628 static void
1629 panfrost_generic_cso_delete(struct pipe_context *pctx, void *hwcso)
1630 {
1631 free(hwcso);
1632 }
1633
1634 static void *
1635 panfrost_create_rasterizer_state(
1636 struct pipe_context *pctx,
1637 const struct pipe_rasterizer_state *cso)
1638 {
1639 struct panfrost_context *ctx = pan_context(pctx);
1640 struct panfrost_rasterizer *so = CALLOC_STRUCT(panfrost_rasterizer);
1641
1642 so->base = *cso;
1643
1644 /* Bitmask, unknown meaning of the start value */
1645 so->tiler_gl_enables = ctx->is_t6xx ? 0x105 : 0x7;
1646
1647 if (cso->front_ccw)
1648 so->tiler_gl_enables |= MALI_FRONT_CCW_TOP;
1649
1650 if (cso->cull_face & PIPE_FACE_FRONT)
1651 so->tiler_gl_enables |= MALI_CULL_FACE_FRONT;
1652
1653 if (cso->cull_face & PIPE_FACE_BACK)
1654 so->tiler_gl_enables |= MALI_CULL_FACE_BACK;
1655
1656 return so;
1657 }
1658
1659 static void
1660 panfrost_bind_rasterizer_state(
1661 struct pipe_context *pctx,
1662 void *hwcso)
1663 {
1664 struct panfrost_context *ctx = pan_context(pctx);
1665
1666 /* TODO: Why can't rasterizer be NULL ever? Other drivers are fine.. */
1667 if (!hwcso)
1668 return;
1669
1670 ctx->rasterizer = hwcso;
1671 ctx->dirty |= PAN_DIRTY_RASTERIZER;
1672 }
1673
1674 static void *
1675 panfrost_create_vertex_elements_state(
1676 struct pipe_context *pctx,
1677 unsigned num_elements,
1678 const struct pipe_vertex_element *elements)
1679 {
1680 struct panfrost_vertex_state *so = CALLOC_STRUCT(panfrost_vertex_state);
1681
1682 so->num_elements = num_elements;
1683 memcpy(so->pipe, elements, sizeof(*elements) * num_elements);
1684
1685 /* XXX: What the cornball? This is totally, 100%, unapologetically
1686 * nonsense. And yet it somehow fixes a regression in -bshadow
1687 * (previously, we allocated the descriptor here... a newer commit
1688 * removed that allocation, and then memory corruption led to
1689 * shader_meta getting overwritten in bad ways and then the whole test
1690 * case falling apart . TODO: LOOK INTO PLEASE XXX XXX BAD XXX XXX XXX
1691 */
1692 panfrost_allocate_chunk(pan_context(pctx), 0, HEAP_DESCRIPTOR);
1693
1694 for (int i = 0; i < num_elements; ++i) {
1695 so->hw[i].index = elements[i].vertex_buffer_index;
1696
1697 enum pipe_format fmt = elements[i].src_format;
1698 const struct util_format_description *desc = util_format_description(fmt);
1699 so->hw[i].unknown1 = 0x2;
1700 so->hw[i].swizzle = panfrost_get_default_swizzle(desc->nr_channels);
1701
1702 so->hw[i].format = panfrost_find_format(desc);
1703
1704 /* The field itself should probably be shifted over */
1705 so->hw[i].src_offset = elements[i].src_offset;
1706 }
1707
1708 return so;
1709 }
1710
1711 static void
1712 panfrost_bind_vertex_elements_state(
1713 struct pipe_context *pctx,
1714 void *hwcso)
1715 {
1716 struct panfrost_context *ctx = pan_context(pctx);
1717
1718 ctx->vertex = hwcso;
1719 ctx->dirty |= PAN_DIRTY_VERTEX;
1720 }
1721
1722 static void *
1723 panfrost_create_shader_state(
1724 struct pipe_context *pctx,
1725 const struct pipe_shader_state *cso)
1726 {
1727 struct panfrost_shader_variants *so = CALLOC_STRUCT(panfrost_shader_variants);
1728 so->base = *cso;
1729
1730 /* Token deep copy to prevent memory corruption */
1731
1732 if (cso->type == PIPE_SHADER_IR_TGSI)
1733 so->base.tokens = tgsi_dup_tokens(so->base.tokens);
1734
1735 return so;
1736 }
1737
1738 static void
1739 panfrost_delete_shader_state(
1740 struct pipe_context *pctx,
1741 void *so)
1742 {
1743 struct panfrost_shader_variants *cso = (struct panfrost_shader_variants *) so;
1744
1745 if (cso->base.type == PIPE_SHADER_IR_TGSI) {
1746 DBG("Deleting TGSI shader leaks duplicated tokens\n");
1747 }
1748
1749 free(so);
1750 }
1751
1752 static void *
1753 panfrost_create_sampler_state(
1754 struct pipe_context *pctx,
1755 const struct pipe_sampler_state *cso)
1756 {
1757 struct panfrost_sampler_state *so = CALLOC_STRUCT(panfrost_sampler_state);
1758 so->base = *cso;
1759
1760 /* sampler_state corresponds to mali_sampler_descriptor, which we can generate entirely here */
1761
1762 struct mali_sampler_descriptor sampler_descriptor = {
1763 .filter_mode = MALI_TEX_MIN(translate_tex_filter(cso->min_img_filter))
1764 | MALI_TEX_MAG(translate_tex_filter(cso->mag_img_filter))
1765 | translate_mip_filter(cso->min_mip_filter)
1766 | 0x20,
1767
1768 .wrap_s = translate_tex_wrap(cso->wrap_s),
1769 .wrap_t = translate_tex_wrap(cso->wrap_t),
1770 .wrap_r = translate_tex_wrap(cso->wrap_r),
1771 .compare_func = panfrost_translate_alt_compare_func(cso->compare_func),
1772 .border_color = {
1773 cso->border_color.f[0],
1774 cso->border_color.f[1],
1775 cso->border_color.f[2],
1776 cso->border_color.f[3]
1777 },
1778 .min_lod = FIXED_16(cso->min_lod),
1779 .max_lod = FIXED_16(cso->max_lod),
1780 .unknown2 = 1,
1781 };
1782
1783 so->hw = sampler_descriptor;
1784
1785 return so;
1786 }
1787
1788 static void
1789 panfrost_bind_sampler_states(
1790 struct pipe_context *pctx,
1791 enum pipe_shader_type shader,
1792 unsigned start_slot, unsigned num_sampler,
1793 void **sampler)
1794 {
1795 assert(start_slot == 0);
1796
1797 struct panfrost_context *ctx = pan_context(pctx);
1798
1799 /* XXX: Should upload, not just copy? */
1800 ctx->sampler_count[shader] = num_sampler;
1801 memcpy(ctx->samplers[shader], sampler, num_sampler * sizeof (void *));
1802
1803 ctx->dirty |= PAN_DIRTY_SAMPLERS;
1804 }
1805
1806 static bool
1807 panfrost_variant_matches(
1808 struct panfrost_context *ctx,
1809 struct panfrost_shader_state *variant,
1810 enum pipe_shader_type type)
1811 {
1812 struct pipe_alpha_state *alpha = &ctx->depth_stencil->alpha;
1813
1814 bool is_fragment = (type == PIPE_SHADER_FRAGMENT);
1815
1816 if (is_fragment && (alpha->enabled || variant->alpha_state.enabled)) {
1817 /* Make sure enable state is at least the same */
1818 if (alpha->enabled != variant->alpha_state.enabled) {
1819 return false;
1820 }
1821
1822 /* Check that the contents of the test are the same */
1823 bool same_func = alpha->func == variant->alpha_state.func;
1824 bool same_ref = alpha->ref_value == variant->alpha_state.ref_value;
1825
1826 if (!(same_func && same_ref)) {
1827 return false;
1828 }
1829 }
1830 /* Otherwise, we're good to go */
1831 return true;
1832 }
1833
1834 static void
1835 panfrost_bind_shader_state(
1836 struct pipe_context *pctx,
1837 void *hwcso,
1838 enum pipe_shader_type type)
1839 {
1840 struct panfrost_context *ctx = pan_context(pctx);
1841
1842 if (type == PIPE_SHADER_FRAGMENT) {
1843 ctx->fs = hwcso;
1844 ctx->dirty |= PAN_DIRTY_FS;
1845 } else {
1846 assert(type == PIPE_SHADER_VERTEX);
1847 ctx->vs = hwcso;
1848 ctx->dirty |= PAN_DIRTY_VS;
1849 }
1850
1851 if (!hwcso) return;
1852
1853 /* Match the appropriate variant */
1854
1855 signed variant = -1;
1856 struct panfrost_shader_variants *variants = (struct panfrost_shader_variants *) hwcso;
1857
1858 for (unsigned i = 0; i < variants->variant_count; ++i) {
1859 if (panfrost_variant_matches(ctx, &variants->variants[i], type)) {
1860 variant = i;
1861 break;
1862 }
1863 }
1864
1865 if (variant == -1) {
1866 /* No variant matched, so create a new one */
1867 variant = variants->variant_count++;
1868 assert(variants->variant_count < MAX_SHADER_VARIANTS);
1869
1870 variants->variants[variant].base = hwcso;
1871
1872 if (type == PIPE_SHADER_FRAGMENT)
1873 variants->variants[variant].alpha_state = ctx->depth_stencil->alpha;
1874
1875 /* Allocate the mapped descriptor ahead-of-time. */
1876 struct panfrost_context *ctx = pan_context(pctx);
1877 struct panfrost_transfer transfer = panfrost_allocate_chunk(ctx, sizeof(struct mali_shader_meta), HEAP_DESCRIPTOR);
1878
1879 variants->variants[variant].tripipe = (struct mali_shader_meta *) transfer.cpu;
1880 variants->variants[variant].tripipe_gpu = transfer.gpu;
1881
1882 }
1883
1884 /* Select this variant */
1885 variants->active_variant = variant;
1886
1887 struct panfrost_shader_state *shader_state = &variants->variants[variant];
1888 assert(panfrost_variant_matches(ctx, shader_state, type));
1889
1890 /* We finally have a variant, so compile it */
1891
1892 if (!shader_state->compiled) {
1893 panfrost_shader_compile(ctx, shader_state->tripipe, NULL,
1894 panfrost_job_type_for_pipe(type), shader_state);
1895
1896 shader_state->compiled = true;
1897 }
1898 }
1899
1900 static void
1901 panfrost_bind_vs_state(struct pipe_context *pctx, void *hwcso)
1902 {
1903 panfrost_bind_shader_state(pctx, hwcso, PIPE_SHADER_VERTEX);
1904 }
1905
1906 static void
1907 panfrost_bind_fs_state(struct pipe_context *pctx, void *hwcso)
1908 {
1909 panfrost_bind_shader_state(pctx, hwcso, PIPE_SHADER_FRAGMENT);
1910 }
1911
1912 static void
1913 panfrost_set_vertex_buffers(
1914 struct pipe_context *pctx,
1915 unsigned start_slot,
1916 unsigned num_buffers,
1917 const struct pipe_vertex_buffer *buffers)
1918 {
1919 struct panfrost_context *ctx = pan_context(pctx);
1920
1921 util_set_vertex_buffers_mask(ctx->vertex_buffers, &ctx->vb_mask, buffers, start_slot, num_buffers);
1922 }
1923
1924 static void
1925 panfrost_set_constant_buffer(
1926 struct pipe_context *pctx,
1927 enum pipe_shader_type shader, uint index,
1928 const struct pipe_constant_buffer *buf)
1929 {
1930 struct panfrost_context *ctx = pan_context(pctx);
1931 struct panfrost_constant_buffer *pbuf = &ctx->constant_buffer[shader];
1932
1933 size_t sz = buf ? buf->buffer_size : 0;
1934
1935 /* Free previous buffer */
1936
1937 pbuf->dirty = true;
1938 pbuf->size = sz;
1939
1940 if (pbuf->buffer) {
1941 free(pbuf->buffer);
1942 pbuf->buffer = NULL;
1943 }
1944
1945 /* If unbinding, we're done */
1946
1947 if (!buf)
1948 return;
1949
1950 /* Multiple constant buffers not yet supported */
1951 assert(index == 0);
1952
1953 const uint8_t *cpu;
1954
1955 struct panfrost_resource *rsrc = (struct panfrost_resource *) (buf->buffer);
1956
1957 if (rsrc) {
1958 cpu = rsrc->bo->cpu;
1959 } else if (buf->user_buffer) {
1960 cpu = buf->user_buffer;
1961 } else {
1962 DBG("No constant buffer?\n");
1963 return;
1964 }
1965
1966 /* Copy the constant buffer into the driver context for later upload */
1967
1968 pbuf->buffer = malloc(sz);
1969 memcpy(pbuf->buffer, cpu + buf->buffer_offset, sz);
1970 }
1971
1972 static void
1973 panfrost_set_stencil_ref(
1974 struct pipe_context *pctx,
1975 const struct pipe_stencil_ref *ref)
1976 {
1977 struct panfrost_context *ctx = pan_context(pctx);
1978 ctx->stencil_ref = *ref;
1979
1980 /* Shader core dirty */
1981 ctx->dirty |= PAN_DIRTY_FS;
1982 }
1983
1984 static struct pipe_sampler_view *
1985 panfrost_create_sampler_view(
1986 struct pipe_context *pctx,
1987 struct pipe_resource *texture,
1988 const struct pipe_sampler_view *template)
1989 {
1990 struct panfrost_sampler_view *so = CALLOC_STRUCT(panfrost_sampler_view);
1991 int bytes_per_pixel = util_format_get_blocksize(texture->format);
1992
1993 pipe_reference(NULL, &texture->reference);
1994
1995 struct panfrost_resource *prsrc = (struct panfrost_resource *) texture;
1996 assert(prsrc->bo);
1997
1998 so->base = *template;
1999 so->base.texture = texture;
2000 so->base.reference.count = 1;
2001 so->base.context = pctx;
2002
2003 /* sampler_views correspond to texture descriptors, minus the texture
2004 * (data) itself. So, we serialise the descriptor here and cache it for
2005 * later. */
2006
2007 /* Make sure it's something with which we're familiar */
2008 assert(bytes_per_pixel >= 1 && bytes_per_pixel <= 4);
2009
2010 /* TODO: Detect from format better */
2011 const struct util_format_description *desc = util_format_description(prsrc->base.format);
2012
2013 unsigned char user_swizzle[4] = {
2014 template->swizzle_r,
2015 template->swizzle_g,
2016 template->swizzle_b,
2017 template->swizzle_a
2018 };
2019
2020 enum mali_format format = panfrost_find_format(desc);
2021
2022 bool is_depth = desc->format == PIPE_FORMAT_Z32_UNORM;
2023
2024 unsigned usage2_layout = 0x10;
2025
2026 switch (prsrc->bo->layout) {
2027 case PAN_AFBC:
2028 usage2_layout |= 0x8 | 0x4;
2029 break;
2030 case PAN_TILED:
2031 usage2_layout |= 0x1;
2032 break;
2033 case PAN_LINEAR:
2034 usage2_layout |= is_depth ? 0x1 : 0x2;
2035 break;
2036 default:
2037 assert(0);
2038 break;
2039 }
2040
2041 /* Check if we need to set a custom stride by computing the "expected"
2042 * stride and comparing it to what the BO actually wants. Only applies
2043 * to linear textures, since tiled/compressed textures have strict
2044 * alignment requirements for their strides as it is */
2045
2046 unsigned first_level = template->u.tex.first_level;
2047 unsigned last_level = template->u.tex.last_level;
2048
2049 if (prsrc->bo->layout == PAN_LINEAR) {
2050 for (unsigned l = first_level; l <= last_level; ++l) {
2051 unsigned actual_stride = prsrc->bo->slices[l].stride;
2052 unsigned width = u_minify(texture->width0, l);
2053 unsigned comp_stride = width * bytes_per_pixel;
2054
2055 if (comp_stride != actual_stride) {
2056 usage2_layout |= MALI_TEX_MANUAL_STRIDE;
2057 break;
2058 }
2059 }
2060 }
2061
2062 struct mali_texture_descriptor texture_descriptor = {
2063 .width = MALI_POSITIVE(texture->width0),
2064 .height = MALI_POSITIVE(texture->height0),
2065 .depth = MALI_POSITIVE(texture->depth0),
2066
2067 /* TODO: Decode */
2068 .format = {
2069 .swizzle = panfrost_translate_swizzle_4(desc->swizzle),
2070 .format = format,
2071
2072 .usage1 = 0x0,
2073 .is_not_cubemap = texture->target != PIPE_TEXTURE_CUBE,
2074
2075 .usage2 = usage2_layout
2076 },
2077
2078 .swizzle = panfrost_translate_swizzle_4(user_swizzle)
2079 };
2080
2081 /* TODO: Other base levels require adjusting dimensions / level numbers / etc */
2082 assert (template->u.tex.first_level == 0);
2083
2084 /* Disable mipmapping for now to avoid regressions while automipmapping
2085 * is being implemented. TODO: Remove me once automipmaps work */
2086
2087 //texture_descriptor.nr_mipmap_levels = template->u.tex.last_level - template->u.tex.first_level;
2088 texture_descriptor.nr_mipmap_levels = 0;
2089
2090 so->hw = texture_descriptor;
2091
2092 return (struct pipe_sampler_view *) so;
2093 }
2094
2095 static void
2096 panfrost_set_sampler_views(
2097 struct pipe_context *pctx,
2098 enum pipe_shader_type shader,
2099 unsigned start_slot, unsigned num_views,
2100 struct pipe_sampler_view **views)
2101 {
2102 struct panfrost_context *ctx = pan_context(pctx);
2103
2104 assert(start_slot == 0);
2105
2106 ctx->sampler_view_count[shader] = num_views;
2107 memcpy(ctx->sampler_views[shader], views, num_views * sizeof (void *));
2108
2109 ctx->dirty |= PAN_DIRTY_TEXTURES;
2110 }
2111
2112 static void
2113 panfrost_sampler_view_destroy(
2114 struct pipe_context *pctx,
2115 struct pipe_sampler_view *view)
2116 {
2117 pipe_resource_reference(&view->texture, NULL);
2118 free(view);
2119 }
2120
2121 static void
2122 panfrost_set_framebuffer_state(struct pipe_context *pctx,
2123 const struct pipe_framebuffer_state *fb)
2124 {
2125 struct panfrost_context *ctx = pan_context(pctx);
2126
2127 /* Flush when switching away from an FBO, but not if the framebuffer
2128 * state is being restored by u_blitter
2129 */
2130
2131 if (!panfrost_is_scanout(ctx) && !ctx->blitter->running) {
2132 panfrost_flush(pctx, NULL, 0);
2133 }
2134
2135 ctx->pipe_framebuffer.nr_cbufs = fb->nr_cbufs;
2136 ctx->pipe_framebuffer.samples = fb->samples;
2137 ctx->pipe_framebuffer.layers = fb->layers;
2138 ctx->pipe_framebuffer.width = fb->width;
2139 ctx->pipe_framebuffer.height = fb->height;
2140
2141 for (int i = 0; i < PIPE_MAX_COLOR_BUFS; i++) {
2142 struct pipe_surface *cb = i < fb->nr_cbufs ? fb->cbufs[i] : NULL;
2143
2144 /* check if changing cbuf */
2145 if (ctx->pipe_framebuffer.cbufs[i] == cb) continue;
2146
2147 if (cb && (i != 0)) {
2148 DBG("XXX: Multiple render targets not supported before t7xx!\n");
2149 assert(0);
2150 }
2151
2152 /* assign new */
2153 pipe_surface_reference(&ctx->pipe_framebuffer.cbufs[i], cb);
2154
2155 if (!cb)
2156 continue;
2157
2158 if (ctx->require_sfbd)
2159 ctx->vt_framebuffer_sfbd = panfrost_emit_sfbd(ctx, ~0);
2160 else
2161 ctx->vt_framebuffer_mfbd = panfrost_emit_mfbd(ctx, ~0);
2162
2163 panfrost_attach_vt_framebuffer(ctx);
2164
2165 struct panfrost_resource *tex = ((struct panfrost_resource *) ctx->pipe_framebuffer.cbufs[i]->texture);
2166 enum pipe_format format = ctx->pipe_framebuffer.cbufs[i]->format;
2167
2168 bool can_afbc = panfrost_format_supports_afbc(format);
2169 bool is_scanout = panfrost_is_scanout(ctx);
2170
2171 if (!is_scanout && tex->bo->layout != PAN_AFBC && can_afbc)
2172 panfrost_enable_afbc(ctx, tex, false);
2173
2174 if (!is_scanout && !tex->bo->has_checksum)
2175 panfrost_enable_checksum(ctx, tex);
2176 }
2177
2178 {
2179 struct pipe_surface *zb = fb->zsbuf;
2180
2181 if (ctx->pipe_framebuffer.zsbuf != zb) {
2182 pipe_surface_reference(&ctx->pipe_framebuffer.zsbuf, zb);
2183
2184 if (zb) {
2185 if (ctx->require_sfbd)
2186 ctx->vt_framebuffer_sfbd = panfrost_emit_sfbd(ctx, ~0);
2187 else
2188 ctx->vt_framebuffer_mfbd = panfrost_emit_mfbd(ctx, ~0);
2189
2190 panfrost_attach_vt_framebuffer(ctx);
2191
2192 struct panfrost_resource *tex = pan_resource(zb->texture);
2193 bool can_afbc = panfrost_format_supports_afbc(zb->format);
2194 bool is_scanout = panfrost_is_scanout(ctx);
2195
2196 if (!is_scanout && tex->bo->layout != PAN_AFBC && can_afbc)
2197 panfrost_enable_afbc(ctx, tex, true);
2198 }
2199 }
2200 }
2201 }
2202
2203 static void *
2204 panfrost_create_blend_state(struct pipe_context *pipe,
2205 const struct pipe_blend_state *blend)
2206 {
2207 struct panfrost_context *ctx = pan_context(pipe);
2208 struct panfrost_blend_state *so = CALLOC_STRUCT(panfrost_blend_state);
2209 so->base = *blend;
2210
2211 /* TODO: The following features are not yet implemented */
2212 assert(!blend->logicop_enable);
2213 assert(!blend->alpha_to_coverage);
2214 assert(!blend->alpha_to_one);
2215
2216 /* Compile the blend state, first as fixed-function if we can */
2217
2218 if (panfrost_make_fixed_blend_mode(&blend->rt[0], so, blend->rt[0].colormask, &ctx->blend_color))
2219 return so;
2220
2221 /* If we can't, compile a blend shader instead */
2222
2223 panfrost_make_blend_shader(ctx, so, &ctx->blend_color);
2224
2225 return so;
2226 }
2227
2228 static void
2229 panfrost_bind_blend_state(struct pipe_context *pipe,
2230 void *cso)
2231 {
2232 struct panfrost_context *ctx = pan_context(pipe);
2233 struct pipe_blend_state *blend = (struct pipe_blend_state *) cso;
2234 struct panfrost_blend_state *pblend = (struct panfrost_blend_state *) cso;
2235 ctx->blend = pblend;
2236
2237 if (!blend)
2238 return;
2239
2240 SET_BIT(ctx->fragment_shader_core.unknown2_4, MALI_NO_DITHER, !blend->dither);
2241
2242 /* TODO: Attach color */
2243
2244 /* Shader itself is not dirty, but the shader core is */
2245 ctx->dirty |= PAN_DIRTY_FS;
2246 }
2247
2248 static void
2249 panfrost_delete_blend_state(struct pipe_context *pipe,
2250 void *blend)
2251 {
2252 struct panfrost_blend_state *so = (struct panfrost_blend_state *) blend;
2253
2254 if (so->has_blend_shader) {
2255 DBG("Deleting blend state leak blend shaders bytecode\n");
2256 }
2257
2258 free(blend);
2259 }
2260
2261 static void
2262 panfrost_set_blend_color(struct pipe_context *pipe,
2263 const struct pipe_blend_color *blend_color)
2264 {
2265 struct panfrost_context *ctx = pan_context(pipe);
2266
2267 /* If blend_color is we're unbinding, so ctx->blend_color is now undefined -> nothing to do */
2268
2269 if (blend_color) {
2270 ctx->blend_color = *blend_color;
2271
2272 /* The blend mode depends on the blend constant color, due to the
2273 * fixed/programmable split. So, we're forced to regenerate the blend
2274 * equation */
2275
2276 /* TODO: Attach color */
2277 }
2278 }
2279
2280 static void *
2281 panfrost_create_depth_stencil_state(struct pipe_context *pipe,
2282 const struct pipe_depth_stencil_alpha_state *depth_stencil)
2283 {
2284 return mem_dup(depth_stencil, sizeof(*depth_stencil));
2285 }
2286
2287 static void
2288 panfrost_bind_depth_stencil_state(struct pipe_context *pipe,
2289 void *cso)
2290 {
2291 struct panfrost_context *ctx = pan_context(pipe);
2292 struct pipe_depth_stencil_alpha_state *depth_stencil = cso;
2293 ctx->depth_stencil = depth_stencil;
2294
2295 if (!depth_stencil)
2296 return;
2297
2298 /* Alpha does not exist in the hardware (it's not in ES3), so it's
2299 * emulated in the fragment shader */
2300
2301 if (depth_stencil->alpha.enabled) {
2302 /* We need to trigger a new shader (maybe) */
2303 ctx->base.bind_fs_state(&ctx->base, ctx->fs);
2304 }
2305
2306 /* Stencil state */
2307 SET_BIT(ctx->fragment_shader_core.unknown2_4, MALI_STENCIL_TEST, depth_stencil->stencil[0].enabled); /* XXX: which one? */
2308
2309 panfrost_make_stencil_state(&depth_stencil->stencil[0], &ctx->fragment_shader_core.stencil_front);
2310 ctx->fragment_shader_core.stencil_mask_front = depth_stencil->stencil[0].writemask;
2311
2312 panfrost_make_stencil_state(&depth_stencil->stencil[1], &ctx->fragment_shader_core.stencil_back);
2313 ctx->fragment_shader_core.stencil_mask_back = depth_stencil->stencil[1].writemask;
2314
2315 /* Depth state (TODO: Refactor) */
2316 SET_BIT(ctx->fragment_shader_core.unknown2_3, MALI_DEPTH_TEST, depth_stencil->depth.enabled);
2317
2318 int func = depth_stencil->depth.enabled ? depth_stencil->depth.func : PIPE_FUNC_ALWAYS;
2319
2320 ctx->fragment_shader_core.unknown2_3 &= ~MALI_DEPTH_FUNC_MASK;
2321 ctx->fragment_shader_core.unknown2_3 |= MALI_DEPTH_FUNC(panfrost_translate_compare_func(func));
2322
2323 /* Bounds test not implemented */
2324 assert(!depth_stencil->depth.bounds_test);
2325
2326 ctx->dirty |= PAN_DIRTY_FS;
2327 }
2328
2329 static void
2330 panfrost_delete_depth_stencil_state(struct pipe_context *pipe, void *depth)
2331 {
2332 free( depth );
2333 }
2334
2335 static void
2336 panfrost_set_sample_mask(struct pipe_context *pipe,
2337 unsigned sample_mask)
2338 {
2339 }
2340
2341 static void
2342 panfrost_set_clip_state(struct pipe_context *pipe,
2343 const struct pipe_clip_state *clip)
2344 {
2345 //struct panfrost_context *panfrost = pan_context(pipe);
2346 }
2347
2348 static void
2349 panfrost_set_viewport_states(struct pipe_context *pipe,
2350 unsigned start_slot,
2351 unsigned num_viewports,
2352 const struct pipe_viewport_state *viewports)
2353 {
2354 struct panfrost_context *ctx = pan_context(pipe);
2355
2356 assert(start_slot == 0);
2357 assert(num_viewports == 1);
2358
2359 ctx->pipe_viewport = *viewports;
2360 }
2361
2362 static void
2363 panfrost_set_scissor_states(struct pipe_context *pipe,
2364 unsigned start_slot,
2365 unsigned num_scissors,
2366 const struct pipe_scissor_state *scissors)
2367 {
2368 struct panfrost_context *ctx = pan_context(pipe);
2369
2370 assert(start_slot == 0);
2371 assert(num_scissors == 1);
2372
2373 ctx->scissor = *scissors;
2374 }
2375
2376 static void
2377 panfrost_set_polygon_stipple(struct pipe_context *pipe,
2378 const struct pipe_poly_stipple *stipple)
2379 {
2380 //struct panfrost_context *panfrost = pan_context(pipe);
2381 }
2382
2383 static void
2384 panfrost_set_active_query_state(struct pipe_context *pipe,
2385 boolean enable)
2386 {
2387 //struct panfrost_context *panfrost = pan_context(pipe);
2388 }
2389
2390 static void
2391 panfrost_destroy(struct pipe_context *pipe)
2392 {
2393 struct panfrost_context *panfrost = pan_context(pipe);
2394 struct panfrost_screen *screen = pan_screen(pipe->screen);
2395
2396 if (panfrost->blitter)
2397 util_blitter_destroy(panfrost->blitter);
2398
2399 screen->driver->free_slab(screen, &panfrost->scratchpad);
2400 screen->driver->free_slab(screen, &panfrost->varying_mem);
2401 screen->driver->free_slab(screen, &panfrost->shaders);
2402 screen->driver->free_slab(screen, &panfrost->tiler_heap);
2403 screen->driver->free_slab(screen, &panfrost->tiler_polygon_list);
2404 }
2405
2406 static struct pipe_query *
2407 panfrost_create_query(struct pipe_context *pipe,
2408 unsigned type,
2409 unsigned index)
2410 {
2411 struct panfrost_query *q = CALLOC_STRUCT(panfrost_query);
2412
2413 q->type = type;
2414 q->index = index;
2415
2416 return (struct pipe_query *) q;
2417 }
2418
2419 static void
2420 panfrost_destroy_query(struct pipe_context *pipe, struct pipe_query *q)
2421 {
2422 FREE(q);
2423 }
2424
2425 static boolean
2426 panfrost_begin_query(struct pipe_context *pipe, struct pipe_query *q)
2427 {
2428 struct panfrost_context *ctx = pan_context(pipe);
2429 struct panfrost_query *query = (struct panfrost_query *) q;
2430
2431 switch (query->type) {
2432 case PIPE_QUERY_OCCLUSION_COUNTER:
2433 case PIPE_QUERY_OCCLUSION_PREDICATE:
2434 case PIPE_QUERY_OCCLUSION_PREDICATE_CONSERVATIVE:
2435 {
2436 /* Allocate a word for the query results to be stored */
2437 query->transfer = panfrost_allocate_chunk(ctx, sizeof(unsigned), HEAP_DESCRIPTOR);
2438
2439 ctx->occlusion_query = query;
2440
2441 break;
2442 }
2443
2444 default:
2445 DBG("Skipping query %d\n", query->type);
2446 break;
2447 }
2448
2449 return true;
2450 }
2451
2452 static bool
2453 panfrost_end_query(struct pipe_context *pipe, struct pipe_query *q)
2454 {
2455 struct panfrost_context *ctx = pan_context(pipe);
2456 ctx->occlusion_query = NULL;
2457 return true;
2458 }
2459
2460 static boolean
2461 panfrost_get_query_result(struct pipe_context *pipe,
2462 struct pipe_query *q,
2463 boolean wait,
2464 union pipe_query_result *vresult)
2465 {
2466 /* STUB */
2467 struct panfrost_query *query = (struct panfrost_query *) q;
2468
2469 /* We need to flush out the jobs to actually run the counter, TODO
2470 * check wait, TODO wallpaper after if needed */
2471
2472 panfrost_flush(pipe, NULL, PIPE_FLUSH_END_OF_FRAME);
2473
2474 switch (query->type) {
2475 case PIPE_QUERY_OCCLUSION_COUNTER:
2476 case PIPE_QUERY_OCCLUSION_PREDICATE:
2477 case PIPE_QUERY_OCCLUSION_PREDICATE_CONSERVATIVE: {
2478 /* Read back the query results */
2479 unsigned *result = (unsigned *) query->transfer.cpu;
2480 unsigned passed = *result;
2481
2482 if (query->type == PIPE_QUERY_OCCLUSION_COUNTER) {
2483 vresult->u64 = passed;
2484 } else {
2485 vresult->b = !!passed;
2486 }
2487
2488 break;
2489 }
2490 default:
2491 DBG("Skipped query get %d\n", query->type);
2492 break;
2493 }
2494
2495 return true;
2496 }
2497
2498 static struct pipe_stream_output_target *
2499 panfrost_create_stream_output_target(struct pipe_context *pctx,
2500 struct pipe_resource *prsc,
2501 unsigned buffer_offset,
2502 unsigned buffer_size)
2503 {
2504 struct pipe_stream_output_target *target;
2505
2506 target = CALLOC_STRUCT(pipe_stream_output_target);
2507
2508 if (!target)
2509 return NULL;
2510
2511 pipe_reference_init(&target->reference, 1);
2512 pipe_resource_reference(&target->buffer, prsc);
2513
2514 target->context = pctx;
2515 target->buffer_offset = buffer_offset;
2516 target->buffer_size = buffer_size;
2517
2518 return target;
2519 }
2520
2521 static void
2522 panfrost_stream_output_target_destroy(struct pipe_context *pctx,
2523 struct pipe_stream_output_target *target)
2524 {
2525 pipe_resource_reference(&target->buffer, NULL);
2526 free(target);
2527 }
2528
2529 static void
2530 panfrost_set_stream_output_targets(struct pipe_context *pctx,
2531 unsigned num_targets,
2532 struct pipe_stream_output_target **targets,
2533 const unsigned *offsets)
2534 {
2535 /* STUB */
2536 }
2537
2538 static void
2539 panfrost_setup_hardware(struct panfrost_context *ctx)
2540 {
2541 struct pipe_context *gallium = (struct pipe_context *) ctx;
2542 struct panfrost_screen *screen = pan_screen(gallium->screen);
2543
2544 for (int i = 0; i < ARRAY_SIZE(ctx->transient_pools); ++i) {
2545 /* Allocate the beginning of the transient pool */
2546 int entry_size = (1 << 22); /* 4MB */
2547
2548 ctx->transient_pools[i].entry_size = entry_size;
2549 ctx->transient_pools[i].entry_count = 1;
2550
2551 ctx->transient_pools[i].entries[0] = (struct panfrost_memory_entry *) pb_slab_alloc(&screen->slabs, entry_size, HEAP_TRANSIENT);
2552 }
2553
2554 screen->driver->allocate_slab(screen, &ctx->scratchpad, 64, false, 0, 0, 0);
2555 screen->driver->allocate_slab(screen, &ctx->varying_mem, 16384, false, PAN_ALLOCATE_INVISIBLE | PAN_ALLOCATE_COHERENT_LOCAL, 0, 0);
2556 screen->driver->allocate_slab(screen, &ctx->shaders, 4096, true, PAN_ALLOCATE_EXECUTE, 0, 0);
2557 screen->driver->allocate_slab(screen, &ctx->tiler_heap, 32768, false, PAN_ALLOCATE_INVISIBLE | PAN_ALLOCATE_GROWABLE, 1, 128);
2558 screen->driver->allocate_slab(screen, &ctx->tiler_polygon_list, 128*128, false, PAN_ALLOCATE_INVISIBLE | PAN_ALLOCATE_GROWABLE, 1, 128);
2559 screen->driver->allocate_slab(screen, &ctx->tiler_dummy, 1, false, PAN_ALLOCATE_INVISIBLE, 0, 0);
2560
2561 }
2562
2563 /* New context creation, which also does hardware initialisation since I don't
2564 * know the better way to structure this :smirk: */
2565
2566 struct pipe_context *
2567 panfrost_create_context(struct pipe_screen *screen, void *priv, unsigned flags)
2568 {
2569 struct panfrost_context *ctx = CALLOC_STRUCT(panfrost_context);
2570 struct panfrost_screen *pscreen = pan_screen(screen);
2571 memset(ctx, 0, sizeof(*ctx));
2572 struct pipe_context *gallium = (struct pipe_context *) ctx;
2573 unsigned gpu_id;
2574
2575 gpu_id = pscreen->driver->query_gpu_version(pscreen);
2576
2577 ctx->is_t6xx = gpu_id <= 0x0750; /* For now, this flag means T760 or less */
2578 ctx->require_sfbd = gpu_id < 0x0750; /* T760 is the first to support MFBD */
2579
2580 gallium->screen = screen;
2581
2582 gallium->destroy = panfrost_destroy;
2583
2584 gallium->set_framebuffer_state = panfrost_set_framebuffer_state;
2585
2586 gallium->flush = panfrost_flush;
2587 gallium->clear = panfrost_clear;
2588 gallium->draw_vbo = panfrost_draw_vbo;
2589
2590 gallium->set_vertex_buffers = panfrost_set_vertex_buffers;
2591 gallium->set_constant_buffer = panfrost_set_constant_buffer;
2592
2593 gallium->set_stencil_ref = panfrost_set_stencil_ref;
2594
2595 gallium->create_sampler_view = panfrost_create_sampler_view;
2596 gallium->set_sampler_views = panfrost_set_sampler_views;
2597 gallium->sampler_view_destroy = panfrost_sampler_view_destroy;
2598
2599 gallium->create_rasterizer_state = panfrost_create_rasterizer_state;
2600 gallium->bind_rasterizer_state = panfrost_bind_rasterizer_state;
2601 gallium->delete_rasterizer_state = panfrost_generic_cso_delete;
2602
2603 gallium->create_vertex_elements_state = panfrost_create_vertex_elements_state;
2604 gallium->bind_vertex_elements_state = panfrost_bind_vertex_elements_state;
2605 gallium->delete_vertex_elements_state = panfrost_generic_cso_delete;
2606
2607 gallium->create_fs_state = panfrost_create_shader_state;
2608 gallium->delete_fs_state = panfrost_delete_shader_state;
2609 gallium->bind_fs_state = panfrost_bind_fs_state;
2610
2611 gallium->create_vs_state = panfrost_create_shader_state;
2612 gallium->delete_vs_state = panfrost_delete_shader_state;
2613 gallium->bind_vs_state = panfrost_bind_vs_state;
2614
2615 gallium->create_sampler_state = panfrost_create_sampler_state;
2616 gallium->delete_sampler_state = panfrost_generic_cso_delete;
2617 gallium->bind_sampler_states = panfrost_bind_sampler_states;
2618
2619 gallium->create_blend_state = panfrost_create_blend_state;
2620 gallium->bind_blend_state = panfrost_bind_blend_state;
2621 gallium->delete_blend_state = panfrost_delete_blend_state;
2622
2623 gallium->set_blend_color = panfrost_set_blend_color;
2624
2625 gallium->create_depth_stencil_alpha_state = panfrost_create_depth_stencil_state;
2626 gallium->bind_depth_stencil_alpha_state = panfrost_bind_depth_stencil_state;
2627 gallium->delete_depth_stencil_alpha_state = panfrost_delete_depth_stencil_state;
2628
2629 gallium->set_sample_mask = panfrost_set_sample_mask;
2630
2631 gallium->set_clip_state = panfrost_set_clip_state;
2632 gallium->set_viewport_states = panfrost_set_viewport_states;
2633 gallium->set_scissor_states = panfrost_set_scissor_states;
2634 gallium->set_polygon_stipple = panfrost_set_polygon_stipple;
2635 gallium->set_active_query_state = panfrost_set_active_query_state;
2636
2637 gallium->create_query = panfrost_create_query;
2638 gallium->destroy_query = panfrost_destroy_query;
2639 gallium->begin_query = panfrost_begin_query;
2640 gallium->end_query = panfrost_end_query;
2641 gallium->get_query_result = panfrost_get_query_result;
2642
2643 gallium->create_stream_output_target = panfrost_create_stream_output_target;
2644 gallium->stream_output_target_destroy = panfrost_stream_output_target_destroy;
2645 gallium->set_stream_output_targets = panfrost_set_stream_output_targets;
2646
2647 panfrost_resource_context_init(gallium);
2648
2649 pscreen->driver->init_context(ctx);
2650
2651 panfrost_setup_hardware(ctx);
2652
2653 /* XXX: leaks */
2654 gallium->stream_uploader = u_upload_create_default(gallium);
2655 gallium->const_uploader = gallium->stream_uploader;
2656 assert(gallium->stream_uploader);
2657
2658 /* Midgard supports ES modes, plus QUADS/QUAD_STRIPS/POLYGON */
2659 ctx->draw_modes = (1 << (PIPE_PRIM_POLYGON + 1)) - 1;
2660
2661 ctx->primconvert = util_primconvert_create(gallium, ctx->draw_modes);
2662
2663 ctx->blitter = util_blitter_create(gallium);
2664 assert(ctx->blitter);
2665
2666 /* Prepare for render! */
2667
2668 panfrost_job_init(ctx);
2669 panfrost_emit_vertex_payload(ctx);
2670 panfrost_emit_tiler_payload(ctx);
2671 panfrost_invalidate_frame(ctx);
2672 panfrost_default_shader_backend(ctx);
2673 panfrost_generate_space_filler_indices();
2674
2675 return gallium;
2676 }