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