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