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