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