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