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