panfrost: Simplify panfrost_emit_for_draw() and make it private
[mesa.git] / src / gallium / drivers / panfrost / pan_context.c
1 /*
2 * © Copyright 2018 Alyssa Rosenzweig
3 * Copyright © 2014-2017 Broadcom
4 * Copyright (C) 2017 Intel Corporation
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice (including the next
14 * paragraph) shall be included in all copies or substantial portions of the
15 * Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE.
24 *
25 */
26
27 #include <sys/poll.h>
28 #include <errno.h>
29
30 #include "pan_bo.h"
31 #include "pan_context.h"
32 #include "pan_minmax_cache.h"
33 #include "panfrost-quirks.h"
34
35 #include "util/macros.h"
36 #include "util/format/u_format.h"
37 #include "util/u_inlines.h"
38 #include "util/u_upload_mgr.h"
39 #include "util/u_memory.h"
40 #include "util/u_vbuf.h"
41 #include "util/half_float.h"
42 #include "util/u_helpers.h"
43 #include "util/format/u_format.h"
44 #include "util/u_prim.h"
45 #include "util/u_prim_restart.h"
46 #include "indices/u_primconvert.h"
47 #include "tgsi/tgsi_parse.h"
48 #include "tgsi/tgsi_from_mesa.h"
49 #include "util/u_math.h"
50
51 #include "pan_screen.h"
52 #include "pan_blending.h"
53 #include "pan_blend_shaders.h"
54 #include "pan_cmdstream.h"
55 #include "pan_util.h"
56 #include "pandecode/decode.h"
57
58 struct midgard_tiler_descriptor
59 panfrost_emit_midg_tiler(struct panfrost_batch *batch, unsigned vertex_count)
60 {
61 struct panfrost_screen *screen = pan_screen(batch->ctx->base.screen);
62 bool hierarchy = !(screen->quirks & MIDGARD_NO_HIER_TILING);
63 struct midgard_tiler_descriptor t = {0};
64 unsigned height = batch->key.height;
65 unsigned width = batch->key.width;
66
67 t.hierarchy_mask =
68 panfrost_choose_hierarchy_mask(width, height, vertex_count, hierarchy);
69
70 /* Compute the polygon header size and use that to offset the body */
71
72 unsigned header_size = panfrost_tiler_header_size(
73 width, height, t.hierarchy_mask, hierarchy);
74
75 t.polygon_list_size = panfrost_tiler_full_size(
76 width, height, t.hierarchy_mask, hierarchy);
77
78 /* Sanity check */
79
80 if (vertex_count) {
81 struct panfrost_bo *tiler_heap;
82
83 tiler_heap = panfrost_batch_get_tiler_heap(batch);
84 t.polygon_list = panfrost_batch_get_polygon_list(batch,
85 header_size +
86 t.polygon_list_size);
87
88
89 /* Allow the entire tiler heap */
90 t.heap_start = tiler_heap->gpu;
91 t.heap_end = tiler_heap->gpu + tiler_heap->size;
92 } else {
93 struct panfrost_bo *tiler_dummy;
94
95 tiler_dummy = panfrost_batch_get_tiler_dummy(batch);
96 header_size = MALI_TILER_MINIMUM_HEADER_SIZE;
97
98 /* The tiler is disabled, so don't allow the tiler heap */
99 t.heap_start = tiler_dummy->gpu;
100 t.heap_end = t.heap_start;
101
102 /* Use a dummy polygon list */
103 t.polygon_list = tiler_dummy->gpu;
104
105 /* Disable the tiler */
106 if (hierarchy)
107 t.hierarchy_mask |= MALI_TILER_DISABLED;
108 else {
109 t.hierarchy_mask = MALI_TILER_USER;
110 t.polygon_list_size = MALI_TILER_MINIMUM_HEADER_SIZE + 4;
111
112 /* We don't have a WRITE_VALUE job, so write the polygon list manually */
113 uint32_t *polygon_list_body = (uint32_t *) (tiler_dummy->cpu + header_size);
114 polygon_list_body[0] = 0xa0000000; /* TODO: Just that? */
115 }
116 }
117
118 t.polygon_list_body =
119 t.polygon_list + header_size;
120
121 return t;
122 }
123
124 static void
125 panfrost_clear(
126 struct pipe_context *pipe,
127 unsigned buffers,
128 const union pipe_color_union *color,
129 double depth, unsigned stencil)
130 {
131 struct panfrost_context *ctx = pan_context(pipe);
132
133 /* TODO: panfrost_get_fresh_batch_for_fbo() instantiates a new batch if
134 * the existing batch targeting this FBO has draws. We could probably
135 * avoid that by replacing plain clears by quad-draws with a specific
136 * color/depth/stencil value, thus avoiding the generation of extra
137 * fragment jobs.
138 */
139 struct panfrost_batch *batch = panfrost_get_fresh_batch_for_fbo(ctx);
140
141 panfrost_batch_add_fbo_bos(batch);
142 panfrost_batch_clear(batch, buffers, color, depth, stencil);
143 }
144
145 /* Reset per-frame context, called on context initialisation as well as after
146 * flushing a frame */
147
148 void
149 panfrost_invalidate_frame(struct panfrost_context *ctx)
150 {
151 for (unsigned i = 0; i < PIPE_SHADER_TYPES; ++i)
152 ctx->payloads[i].postfix.shared_memory = 0;
153
154 /* TODO: When does this need to be handled? */
155 ctx->active_queries = true;
156 }
157
158 /* In practice, every field of these payloads should be configurable
159 * arbitrarily, which means these functions are basically catch-all's for
160 * as-of-yet unwavering unknowns */
161
162 static void
163 panfrost_emit_vertex_payload(struct panfrost_context *ctx)
164 {
165 /* 0x2 bit clear on 32-bit T6XX */
166
167 struct midgard_payload_vertex_tiler payload = {
168 .gl_enables = 0x4 | 0x2,
169 };
170
171 /* Vertex and compute are closely coupled, so share a payload */
172
173 memcpy(&ctx->payloads[PIPE_SHADER_VERTEX], &payload, sizeof(payload));
174 memcpy(&ctx->payloads[PIPE_SHADER_COMPUTE], &payload, sizeof(payload));
175 }
176
177 static unsigned
178 translate_tex_wrap(enum pipe_tex_wrap w)
179 {
180 switch (w) {
181 case PIPE_TEX_WRAP_REPEAT:
182 return MALI_WRAP_REPEAT;
183
184 case PIPE_TEX_WRAP_CLAMP:
185 return MALI_WRAP_CLAMP;
186
187 case PIPE_TEX_WRAP_CLAMP_TO_EDGE:
188 return MALI_WRAP_CLAMP_TO_EDGE;
189
190 case PIPE_TEX_WRAP_CLAMP_TO_BORDER:
191 return MALI_WRAP_CLAMP_TO_BORDER;
192
193 case PIPE_TEX_WRAP_MIRROR_REPEAT:
194 return MALI_WRAP_MIRRORED_REPEAT;
195
196 case PIPE_TEX_WRAP_MIRROR_CLAMP:
197 return MALI_WRAP_MIRRORED_CLAMP;
198
199 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE:
200 return MALI_WRAP_MIRRORED_CLAMP_TO_EDGE;
201
202 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER:
203 return MALI_WRAP_MIRRORED_CLAMP_TO_BORDER;
204
205 default:
206 unreachable("Invalid wrap");
207 }
208 }
209
210 static unsigned
211 panfrost_translate_compare_func(enum pipe_compare_func in)
212 {
213 switch (in) {
214 case PIPE_FUNC_NEVER:
215 return MALI_FUNC_NEVER;
216
217 case PIPE_FUNC_LESS:
218 return MALI_FUNC_LESS;
219
220 case PIPE_FUNC_EQUAL:
221 return MALI_FUNC_EQUAL;
222
223 case PIPE_FUNC_LEQUAL:
224 return MALI_FUNC_LEQUAL;
225
226 case PIPE_FUNC_GREATER:
227 return MALI_FUNC_GREATER;
228
229 case PIPE_FUNC_NOTEQUAL:
230 return MALI_FUNC_NOTEQUAL;
231
232 case PIPE_FUNC_GEQUAL:
233 return MALI_FUNC_GEQUAL;
234
235 case PIPE_FUNC_ALWAYS:
236 return MALI_FUNC_ALWAYS;
237
238 default:
239 unreachable("Invalid func");
240 }
241 }
242
243 static unsigned
244 panfrost_translate_stencil_op(enum pipe_stencil_op in)
245 {
246 switch (in) {
247 case PIPE_STENCIL_OP_KEEP:
248 return MALI_STENCIL_KEEP;
249
250 case PIPE_STENCIL_OP_ZERO:
251 return MALI_STENCIL_ZERO;
252
253 case PIPE_STENCIL_OP_REPLACE:
254 return MALI_STENCIL_REPLACE;
255
256 case PIPE_STENCIL_OP_INCR:
257 return MALI_STENCIL_INCR;
258
259 case PIPE_STENCIL_OP_DECR:
260 return MALI_STENCIL_DECR;
261
262 case PIPE_STENCIL_OP_INCR_WRAP:
263 return MALI_STENCIL_INCR_WRAP;
264
265 case PIPE_STENCIL_OP_DECR_WRAP:
266 return MALI_STENCIL_DECR_WRAP;
267
268 case PIPE_STENCIL_OP_INVERT:
269 return MALI_STENCIL_INVERT;
270
271 default:
272 unreachable("Invalid stencil op");
273 }
274 }
275
276 static void
277 panfrost_make_stencil_state(const struct pipe_stencil_state *in, struct mali_stencil_test *out)
278 {
279 out->ref = 0; /* Gallium gets it from elsewhere */
280
281 out->mask = in->valuemask;
282 out->func = panfrost_translate_compare_func(in->func);
283 out->sfail = panfrost_translate_stencil_op(in->fail_op);
284 out->dpfail = panfrost_translate_stencil_op(in->zfail_op);
285 out->dppass = panfrost_translate_stencil_op(in->zpass_op);
286 }
287
288 static void
289 panfrost_default_shader_backend(struct panfrost_context *ctx)
290 {
291 struct panfrost_screen *screen = pan_screen(ctx->base.screen);
292 struct mali_shader_meta shader = {
293 .alpha_coverage = ~MALI_ALPHA_COVERAGE(0.000000),
294
295 .unknown2_3 = MALI_DEPTH_FUNC(MALI_FUNC_ALWAYS) | 0x3010,
296 .unknown2_4 = MALI_NO_MSAA | 0x4e0,
297 };
298
299 /* unknown2_4 has 0x10 bit set on T6XX and T720. We don't know why this is
300 * required (independent of 32-bit/64-bit descriptors), or why it's not
301 * used on later GPU revisions. Otherwise, all shader jobs fault on
302 * these earlier chips (perhaps this is a chicken bit of some kind).
303 * More investigation is needed. */
304
305 if (screen->quirks & MIDGARD_SFBD)
306 shader.unknown2_4 |= 0x10;
307
308 struct pipe_stencil_state default_stencil = {
309 .enabled = 0,
310 .func = PIPE_FUNC_ALWAYS,
311 .fail_op = MALI_STENCIL_KEEP,
312 .zfail_op = MALI_STENCIL_KEEP,
313 .zpass_op = MALI_STENCIL_KEEP,
314 .writemask = 0xFF,
315 .valuemask = 0xFF
316 };
317
318 panfrost_make_stencil_state(&default_stencil, &shader.stencil_front);
319 shader.stencil_mask_front = default_stencil.writemask;
320
321 panfrost_make_stencil_state(&default_stencil, &shader.stencil_back);
322 shader.stencil_mask_back = default_stencil.writemask;
323
324 if (default_stencil.enabled)
325 shader.unknown2_4 |= MALI_STENCIL_TEST;
326
327 memcpy(&ctx->fragment_shader_core, &shader, sizeof(shader));
328 }
329
330 bool
331 panfrost_writes_point_size(struct panfrost_context *ctx)
332 {
333 assert(ctx->shader[PIPE_SHADER_VERTEX]);
334 struct panfrost_shader_state *vs = panfrost_get_shader_state(ctx, PIPE_SHADER_VERTEX);
335
336 return vs->writes_point_size && ctx->payloads[PIPE_SHADER_FRAGMENT].prefix.draw_mode == MALI_POINTS;
337 }
338
339 /* Stage the attribute descriptors so we can adjust src_offset
340 * to let BOs align nicely */
341
342 static void
343 panfrost_stage_attributes(struct panfrost_context *ctx)
344 {
345 struct panfrost_batch *batch = panfrost_get_batch_for_fbo(ctx);
346 struct panfrost_vertex_state *so = ctx->vertex;
347
348 size_t sz = sizeof(struct mali_attr_meta) * PAN_MAX_ATTRIBUTE;
349 struct panfrost_transfer transfer = panfrost_allocate_transient(batch, sz);
350 struct mali_attr_meta *target = (struct mali_attr_meta *) transfer.cpu;
351
352 /* Copy as-is for the first pass */
353 memcpy(target, so->hw, sz);
354
355 /* Fixup offsets for the second pass. Recall that the hardware
356 * calculates attribute addresses as:
357 *
358 * addr = base + (stride * vtx) + src_offset;
359 *
360 * However, on Mali, base must be aligned to 64-bytes, so we
361 * instead let:
362 *
363 * base' = base & ~63 = base - (base & 63)
364 *
365 * To compensate when using base' (see emit_vertex_data), we have
366 * to adjust src_offset by the masked off piece:
367 *
368 * addr' = base' + (stride * vtx) + (src_offset + (base & 63))
369 * = base - (base & 63) + (stride * vtx) + src_offset + (base & 63)
370 * = base + (stride * vtx) + src_offset
371 * = addr;
372 *
373 * QED.
374 */
375
376 unsigned start = ctx->payloads[PIPE_SHADER_VERTEX].offset_start;
377
378 for (unsigned i = 0; i < so->num_elements; ++i) {
379 unsigned vbi = so->pipe[i].vertex_buffer_index;
380 struct pipe_vertex_buffer *buf = &ctx->vertex_buffers[vbi];
381 struct panfrost_resource *rsrc = (struct panfrost_resource *) (buf->buffer.resource);
382 mali_ptr addr = rsrc->bo->gpu + buf->buffer_offset;
383
384 /* Adjust by the masked off bits of the offset. Make sure we
385 * read src_offset from so->hw (which is not GPU visible)
386 * rather than target (which is) due to caching effects */
387
388 unsigned src_offset = so->hw[i].src_offset;
389 src_offset += (addr & 63);
390
391 /* Also, somewhat obscurely per-instance data needs to be
392 * offset in response to a delayed start in an indexed draw */
393
394 if (so->pipe[i].instance_divisor && ctx->instance_count > 1 && start)
395 src_offset -= buf->stride * start;
396
397 target[i].src_offset = src_offset;
398 }
399
400 /* Let's also include vertex builtins */
401
402 struct mali_attr_meta builtin = {
403 .format = MALI_R32UI,
404 .swizzle = panfrost_get_default_swizzle(1)
405 };
406
407 /* See mali_attr_meta specification for the magic number */
408
409 builtin.index = so->vertexid_index;
410 memcpy(&target[PAN_VERTEX_ID], &builtin, 4);
411
412 builtin.index = so->vertexid_index + 1;
413 memcpy(&target[PAN_INSTANCE_ID], &builtin, 4);
414
415 ctx->payloads[PIPE_SHADER_VERTEX].postfix.attribute_meta = transfer.gpu;
416 }
417
418 static void
419 panfrost_upload_sampler_descriptors(struct panfrost_context *ctx)
420 {
421 struct panfrost_batch *batch = panfrost_get_batch_for_fbo(ctx);
422 size_t desc_size = sizeof(struct mali_sampler_descriptor);
423
424 for (int t = 0; t <= PIPE_SHADER_FRAGMENT; ++t) {
425 mali_ptr upload = 0;
426
427 if (ctx->sampler_count[t]) {
428 size_t transfer_size = desc_size * ctx->sampler_count[t];
429
430 struct panfrost_transfer transfer =
431 panfrost_allocate_transient(batch, transfer_size);
432
433 struct mali_sampler_descriptor *desc =
434 (struct mali_sampler_descriptor *) transfer.cpu;
435
436 for (int i = 0; i < ctx->sampler_count[t]; ++i)
437 desc[i] = ctx->samplers[t][i]->hw;
438
439 upload = transfer.gpu;
440 }
441
442 ctx->payloads[t].postfix.sampler_descriptor = upload;
443 }
444 }
445
446 static mali_ptr
447 panfrost_upload_tex(
448 struct panfrost_context *ctx,
449 enum pipe_shader_type st,
450 struct panfrost_sampler_view *view)
451 {
452 if (!view)
453 return (mali_ptr) 0;
454
455 struct pipe_sampler_view *pview = &view->base;
456 struct panfrost_resource *rsrc = pan_resource(pview->texture);
457
458 /* Add the BO to the job so it's retained until the job is done. */
459 struct panfrost_batch *batch = panfrost_get_batch_for_fbo(ctx);
460
461 panfrost_batch_add_bo(batch, rsrc->bo,
462 PAN_BO_ACCESS_SHARED | PAN_BO_ACCESS_READ |
463 panfrost_bo_access_for_stage(st));
464
465 panfrost_batch_add_bo(batch, view->bo,
466 PAN_BO_ACCESS_SHARED | PAN_BO_ACCESS_READ |
467 panfrost_bo_access_for_stage(st));
468
469 return view->bo->gpu;
470 }
471
472 static void
473 panfrost_upload_texture_descriptors(struct panfrost_context *ctx)
474 {
475 struct panfrost_batch *batch = panfrost_get_batch_for_fbo(ctx);
476
477 for (int t = 0; t <= PIPE_SHADER_FRAGMENT; ++t) {
478 mali_ptr trampoline = 0;
479
480 if (ctx->sampler_view_count[t]) {
481 uint64_t trampolines[PIPE_MAX_SHADER_SAMPLER_VIEWS];
482
483 for (int i = 0; i < ctx->sampler_view_count[t]; ++i)
484 trampolines[i] =
485 panfrost_upload_tex(ctx, t, ctx->sampler_views[t][i]);
486
487 trampoline = panfrost_upload_transient(batch, trampolines, sizeof(uint64_t) * ctx->sampler_view_count[t]);
488 }
489
490 ctx->payloads[t].postfix.texture_trampoline = trampoline;
491 }
492 }
493
494 /* Compute number of UBOs active (more specifically, compute the highest UBO
495 * number addressable -- if there are gaps, include them in the count anyway).
496 * We always include UBO #0 in the count, since we *need* uniforms enabled for
497 * sysvals. */
498
499 unsigned
500 panfrost_ubo_count(struct panfrost_context *ctx, enum pipe_shader_type stage)
501 {
502 unsigned mask = ctx->constant_buffer[stage].enabled_mask | 1;
503 return 32 - __builtin_clz(mask);
504 }
505
506 /* Fixes up a shader state with current state */
507
508 void
509 panfrost_patch_shader_state(struct panfrost_context *ctx,
510 enum pipe_shader_type stage)
511 {
512 struct panfrost_shader_state *ss = panfrost_get_shader_state(ctx, stage);
513
514 if (!ss)
515 return;
516
517 ss->tripipe->texture_count = ctx->sampler_view_count[stage];
518 ss->tripipe->sampler_count = ctx->sampler_count[stage];
519
520 ss->tripipe->midgard1.flags_lo = 0x220;
521
522 unsigned ubo_count = panfrost_ubo_count(ctx, stage);
523 ss->tripipe->midgard1.uniform_buffer_count = ubo_count;
524 }
525
526 /* Go through dirty flags and actualise them in the cmdstream. */
527
528 static void
529 panfrost_emit_for_draw(struct panfrost_context *ctx)
530 {
531 struct panfrost_batch *batch = panfrost_get_batch_for_fbo(ctx);
532 struct panfrost_screen *screen = pan_screen(ctx->base.screen);
533
534 panfrost_batch_add_fbo_bos(batch);
535
536 for (int i = 0; i <= PIPE_SHADER_FRAGMENT; ++i)
537 panfrost_vt_attach_framebuffer(ctx, &ctx->payloads[i]);
538
539 panfrost_emit_vertex_data(batch);
540
541 /* Varyings emitted for -all- geometry */
542 unsigned total_count = ctx->padded_count * ctx->instance_count;
543 panfrost_emit_varying_descriptor(ctx, total_count);
544
545 if (ctx->rasterizer) {
546 bool msaa = ctx->rasterizer->base.multisample;
547 ctx->payloads[PIPE_SHADER_FRAGMENT].gl_enables = ctx->rasterizer->tiler_gl_enables;
548
549 /* TODO: Sample size */
550 SET_BIT(ctx->fragment_shader_core.unknown2_3, MALI_HAS_MSAA, msaa);
551 SET_BIT(ctx->fragment_shader_core.unknown2_4, MALI_NO_MSAA, !msaa);
552 }
553
554 panfrost_batch_set_requirements(batch);
555
556 if (ctx->occlusion_query) {
557 ctx->payloads[PIPE_SHADER_FRAGMENT].gl_enables |= MALI_OCCLUSION_QUERY;
558 ctx->payloads[PIPE_SHADER_FRAGMENT].postfix.occlusion_counter = ctx->occlusion_query->bo->gpu;
559 }
560
561 panfrost_patch_shader_state(ctx, PIPE_SHADER_VERTEX);
562 panfrost_emit_shader_meta(batch, PIPE_SHADER_VERTEX,
563 &ctx->payloads[PIPE_SHADER_VERTEX]);
564
565 if (ctx->shader[PIPE_SHADER_VERTEX] && ctx->shader[PIPE_SHADER_FRAGMENT]) {
566 /* Check if we need to link the gl_PointSize varying */
567 if (!panfrost_writes_point_size(ctx)) {
568 /* If the size is constant, write it out. Otherwise,
569 * don't touch primitive_size (since we would clobber
570 * the pointer there) */
571
572 bool points = ctx->payloads[PIPE_SHADER_FRAGMENT].prefix.draw_mode == MALI_POINTS;
573
574 ctx->payloads[PIPE_SHADER_FRAGMENT].primitive_size.constant = points ?
575 ctx->rasterizer->base.point_size :
576 ctx->rasterizer->base.line_width;
577 }
578 }
579
580 if (ctx->shader[PIPE_SHADER_FRAGMENT]) {
581 struct panfrost_shader_state *variant = panfrost_get_shader_state(ctx, PIPE_SHADER_FRAGMENT);
582
583 panfrost_patch_shader_state(ctx, PIPE_SHADER_FRAGMENT);
584
585 #define COPY(name) ctx->fragment_shader_core.name = variant->tripipe->name
586
587 COPY(shader);
588 COPY(attribute_count);
589 COPY(varying_count);
590 COPY(texture_count);
591 COPY(sampler_count);
592 COPY(midgard1.uniform_count);
593 COPY(midgard1.uniform_buffer_count);
594 COPY(midgard1.work_count);
595 COPY(midgard1.flags_lo);
596 COPY(midgard1.flags_hi);
597
598 #undef COPY
599
600 /* Get blending setup */
601 unsigned rt_count = MAX2(ctx->pipe_framebuffer.nr_cbufs, 1);
602
603 struct panfrost_blend_final blend[PIPE_MAX_COLOR_BUFS];
604 unsigned shader_offset = 0;
605 struct panfrost_bo *shader_bo = NULL;
606
607 for (unsigned c = 0; c < rt_count; ++c) {
608 blend[c] = panfrost_get_blend_for_context(ctx, c, &shader_bo, &shader_offset);
609 }
610
611 /* If there is a blend shader, work registers are shared. XXX: opt */
612
613 for (unsigned c = 0; c < rt_count; ++c) {
614 if (blend[c].is_shader)
615 ctx->fragment_shader_core.midgard1.work_count = 16;
616 }
617
618 /* Depending on whether it's legal to in the given shader, we
619 * try to enable early-z testing (or forward-pixel kill?) */
620
621 SET_BIT(ctx->fragment_shader_core.midgard1.flags_lo, MALI_EARLY_Z,
622 !variant->can_discard && !variant->writes_depth);
623
624 /* Add the writes Z/S flags if needed. */
625 SET_BIT(ctx->fragment_shader_core.midgard1.flags_lo,
626 MALI_WRITES_Z, variant->writes_depth);
627 SET_BIT(ctx->fragment_shader_core.midgard1.flags_hi,
628 MALI_WRITES_S, variant->writes_stencil);
629
630 /* Any time texturing is used, derivatives are implicitly
631 * calculated, so we need to enable helper invocations */
632
633 SET_BIT(ctx->fragment_shader_core.midgard1.flags_lo, MALI_HELPER_INVOCATIONS, variant->helper_invocations);
634
635 /* Assign the stencil refs late */
636
637 unsigned front_ref = ctx->stencil_ref.ref_value[0];
638 unsigned back_ref = ctx->stencil_ref.ref_value[1];
639 bool back_enab = ctx->depth_stencil->stencil[1].enabled;
640
641 ctx->fragment_shader_core.stencil_front.ref = front_ref;
642 ctx->fragment_shader_core.stencil_back.ref = back_enab ? back_ref : front_ref;
643
644 /* CAN_DISCARD should be set if the fragment shader possibly
645 * contains a 'discard' instruction. It is likely this is
646 * related to optimizations related to forward-pixel kill, as
647 * per "Mali Performance 3: Is EGL_BUFFER_PRESERVED a good
648 * thing?" by Peter Harris
649 */
650
651 SET_BIT(ctx->fragment_shader_core.unknown2_3, MALI_CAN_DISCARD, variant->can_discard);
652 SET_BIT(ctx->fragment_shader_core.midgard1.flags_lo, 0x400, variant->can_discard);
653
654 /* Even on MFBD, the shader descriptor gets blend shaders. It's
655 * *also* copied to the blend_meta appended (by convention),
656 * but this is the field actually read by the hardware. (Or
657 * maybe both are read...?). Specify the last RTi with a blend
658 * shader. */
659
660 ctx->fragment_shader_core.blend.shader = 0;
661
662 for (signed rt = (rt_count - 1); rt >= 0; --rt) {
663 if (blend[rt].is_shader) {
664 ctx->fragment_shader_core.blend.shader =
665 blend[rt].shader.gpu | blend[rt].shader.first_tag;
666 break;
667 }
668 }
669
670 if (screen->quirks & MIDGARD_SFBD) {
671 /* When only a single render target platform is used, the blend
672 * information is inside the shader meta itself. We
673 * additionally need to signal CAN_DISCARD for nontrivial blend
674 * modes (so we're able to read back the destination buffer) */
675
676 SET_BIT(ctx->fragment_shader_core.unknown2_3, MALI_HAS_BLEND_SHADER, blend[0].is_shader);
677
678 if (!blend[0].is_shader) {
679 ctx->fragment_shader_core.blend.equation =
680 *blend[0].equation.equation;
681 ctx->fragment_shader_core.blend.constant =
682 blend[0].equation.constant;
683 }
684
685 SET_BIT(ctx->fragment_shader_core.unknown2_3, MALI_CAN_DISCARD, !blend[0].no_blending);
686 }
687
688 size_t size = sizeof(struct mali_shader_meta) + (sizeof(struct midgard_blend_rt) * rt_count);
689 struct panfrost_transfer transfer = panfrost_allocate_transient(batch, size);
690 memcpy(transfer.cpu, &ctx->fragment_shader_core, sizeof(struct mali_shader_meta));
691
692 ctx->payloads[PIPE_SHADER_FRAGMENT].postfix.shader = transfer.gpu;
693
694 if (!(screen->quirks & MIDGARD_SFBD)) {
695 /* Additional blend descriptor tacked on for jobs using MFBD */
696
697 struct midgard_blend_rt rts[4];
698
699 for (unsigned i = 0; i < rt_count; ++i) {
700 rts[i].flags = 0x200;
701
702 bool is_srgb =
703 (ctx->pipe_framebuffer.nr_cbufs > i) &&
704 (ctx->pipe_framebuffer.cbufs[i]) &&
705 util_format_is_srgb(ctx->pipe_framebuffer.cbufs[i]->format);
706
707 SET_BIT(rts[i].flags, MALI_BLEND_MRT_SHADER, blend[i].is_shader);
708 SET_BIT(rts[i].flags, MALI_BLEND_LOAD_TIB, !blend[i].no_blending);
709 SET_BIT(rts[i].flags, MALI_BLEND_SRGB, is_srgb);
710 SET_BIT(rts[i].flags, MALI_BLEND_NO_DITHER, !ctx->blend->base.dither);
711
712 if (blend[i].is_shader) {
713 rts[i].blend.shader = blend[i].shader.gpu | blend[i].shader.first_tag;
714 } else {
715 rts[i].blend.equation = *blend[i].equation.equation;
716 rts[i].blend.constant = blend[i].equation.constant;
717 }
718 }
719
720 memcpy(transfer.cpu + sizeof(struct mali_shader_meta), rts, sizeof(rts[0]) * rt_count);
721 }
722 }
723
724 /* We stage to transient, so always dirty.. */
725 if (ctx->vertex)
726 panfrost_stage_attributes(ctx);
727
728 panfrost_upload_sampler_descriptors(ctx);
729 panfrost_upload_texture_descriptors(ctx);
730
731 for (int i = 0; i <= PIPE_SHADER_FRAGMENT; ++i)
732 panfrost_emit_const_buf(batch, i, &ctx->payloads[i]);
733
734 /* TODO: Upload the viewport somewhere more appropriate */
735
736 panfrost_emit_viewport(batch, &ctx->payloads[PIPE_SHADER_FRAGMENT]);
737 }
738
739 /* Corresponds to exactly one draw, but does not submit anything */
740
741 static void
742 panfrost_queue_draw(struct panfrost_context *ctx)
743 {
744 /* Handle dirty flags now */
745 panfrost_emit_for_draw(ctx);
746
747 /* If rasterizer discard is enable, only submit the vertex */
748
749 bool rasterizer_discard = ctx->rasterizer
750 && ctx->rasterizer->base.rasterizer_discard;
751
752
753 struct midgard_payload_vertex_tiler *vertex_payload = &ctx->payloads[PIPE_SHADER_VERTEX];
754 struct midgard_payload_vertex_tiler *tiler_payload = &ctx->payloads[PIPE_SHADER_FRAGMENT];
755
756 struct panfrost_batch *batch = panfrost_get_batch_for_fbo(ctx);
757 bool wallpapering = ctx->wallpaper_batch && batch->tiler_dep;
758
759 if (wallpapering) {
760 /* Inject in reverse order, with "predicted" job indices. THIS IS A HACK XXX */
761 panfrost_new_job(batch, JOB_TYPE_TILER, false, batch->job_index + 2, tiler_payload, sizeof(*tiler_payload), true);
762 panfrost_new_job(batch, JOB_TYPE_VERTEX, false, 0, vertex_payload, sizeof(*vertex_payload), true);
763 } else {
764 unsigned vertex = panfrost_new_job(batch, JOB_TYPE_VERTEX, false, 0, vertex_payload, sizeof(*vertex_payload), false);
765
766 if (!rasterizer_discard)
767 panfrost_new_job(batch, JOB_TYPE_TILER, false, vertex, tiler_payload, sizeof(*tiler_payload), false);
768 }
769
770 panfrost_batch_adjust_stack_size(batch);
771 }
772
773 /* The entire frame is in memory -- send it off to the kernel! */
774
775 void
776 panfrost_flush(
777 struct pipe_context *pipe,
778 struct pipe_fence_handle **fence,
779 unsigned flags)
780 {
781 struct panfrost_context *ctx = pan_context(pipe);
782 struct util_dynarray fences;
783
784 /* We must collect the fences before the flush is done, otherwise we'll
785 * lose track of them.
786 */
787 if (fence) {
788 util_dynarray_init(&fences, NULL);
789 hash_table_foreach(ctx->batches, hentry) {
790 struct panfrost_batch *batch = hentry->data;
791
792 panfrost_batch_fence_reference(batch->out_sync);
793 util_dynarray_append(&fences,
794 struct panfrost_batch_fence *,
795 batch->out_sync);
796 }
797 }
798
799 /* Submit all pending jobs */
800 panfrost_flush_all_batches(ctx, false);
801
802 if (fence) {
803 struct panfrost_fence *f = panfrost_fence_create(ctx, &fences);
804 pipe->screen->fence_reference(pipe->screen, fence, NULL);
805 *fence = (struct pipe_fence_handle *)f;
806
807 util_dynarray_foreach(&fences, struct panfrost_batch_fence *, fence)
808 panfrost_batch_fence_unreference(*fence);
809
810 util_dynarray_fini(&fences);
811 }
812
813 if (pan_debug & PAN_DBG_TRACE)
814 pandecode_next_frame();
815 }
816
817 #define DEFINE_CASE(c) case PIPE_PRIM_##c: return MALI_##c;
818
819 static int
820 g2m_draw_mode(enum pipe_prim_type mode)
821 {
822 switch (mode) {
823 DEFINE_CASE(POINTS);
824 DEFINE_CASE(LINES);
825 DEFINE_CASE(LINE_LOOP);
826 DEFINE_CASE(LINE_STRIP);
827 DEFINE_CASE(TRIANGLES);
828 DEFINE_CASE(TRIANGLE_STRIP);
829 DEFINE_CASE(TRIANGLE_FAN);
830 DEFINE_CASE(QUADS);
831 DEFINE_CASE(QUAD_STRIP);
832 DEFINE_CASE(POLYGON);
833
834 default:
835 unreachable("Invalid draw mode");
836 }
837 }
838
839 #undef DEFINE_CASE
840
841 static unsigned
842 panfrost_translate_index_size(unsigned size)
843 {
844 switch (size) {
845 case 1:
846 return MALI_DRAW_INDEXED_UINT8;
847
848 case 2:
849 return MALI_DRAW_INDEXED_UINT16;
850
851 case 4:
852 return MALI_DRAW_INDEXED_UINT32;
853
854 default:
855 unreachable("Invalid index size");
856 }
857 }
858
859 /* Gets a GPU address for the associated index buffer. Only gauranteed to be
860 * good for the duration of the draw (transient), could last longer. Also get
861 * the bounds on the index buffer for the range accessed by the draw. We do
862 * these operations together because there are natural optimizations which
863 * require them to be together. */
864
865 static mali_ptr
866 panfrost_get_index_buffer_bounded(struct panfrost_context *ctx, const struct pipe_draw_info *info, unsigned *min_index, unsigned *max_index)
867 {
868 struct panfrost_resource *rsrc = (struct panfrost_resource *) (info->index.resource);
869
870 off_t offset = info->start * info->index_size;
871 struct panfrost_batch *batch = panfrost_get_batch_for_fbo(ctx);
872 mali_ptr out = 0;
873
874 bool needs_indices = true;
875
876 if (info->max_index != ~0u) {
877 *min_index = info->min_index;
878 *max_index = info->max_index;
879 needs_indices = false;
880 }
881
882 if (!info->has_user_indices) {
883 /* Only resources can be directly mapped */
884 panfrost_batch_add_bo(batch, rsrc->bo,
885 PAN_BO_ACCESS_SHARED |
886 PAN_BO_ACCESS_READ |
887 PAN_BO_ACCESS_VERTEX_TILER);
888 out = rsrc->bo->gpu + offset;
889
890 /* Check the cache */
891 needs_indices = !panfrost_minmax_cache_get(rsrc->index_cache, info->start, info->count,
892 min_index, max_index);
893 } else {
894 /* Otherwise, we need to upload to transient memory */
895 const uint8_t *ibuf8 = (const uint8_t *) info->index.user;
896 out = panfrost_upload_transient(batch, ibuf8 + offset, info->count * info->index_size);
897 }
898
899 if (needs_indices) {
900 /* Fallback */
901 u_vbuf_get_minmax_index(&ctx->base, info, min_index, max_index);
902
903 if (!info->has_user_indices) {
904 panfrost_minmax_cache_add(rsrc->index_cache, info->start, info->count,
905 *min_index, *max_index);
906 }
907 }
908
909
910 return out;
911 }
912
913 static bool
914 panfrost_scissor_culls_everything(struct panfrost_context *ctx)
915 {
916 const struct pipe_scissor_state *ss = &ctx->scissor;
917
918 /* Check if we're scissoring at all */
919
920 if (!(ctx->rasterizer && ctx->rasterizer->base.scissor))
921 return false;
922
923 return (ss->minx == ss->maxx) || (ss->miny == ss->maxy);
924 }
925
926 /* Count generated primitives (when there is no geom/tess shaders) for
927 * transform feedback */
928
929 static void
930 panfrost_statistics_record(
931 struct panfrost_context *ctx,
932 const struct pipe_draw_info *info)
933 {
934 if (!ctx->active_queries)
935 return;
936
937 uint32_t prims = u_prims_for_vertices(info->mode, info->count);
938 ctx->prims_generated += prims;
939
940 if (!ctx->streamout.num_targets)
941 return;
942
943 ctx->tf_prims_generated += prims;
944 }
945
946 static void
947 panfrost_draw_vbo(
948 struct pipe_context *pipe,
949 const struct pipe_draw_info *info)
950 {
951 struct panfrost_context *ctx = pan_context(pipe);
952
953 /* First of all, check the scissor to see if anything is drawn at all.
954 * If it's not, we drop the draw (mostly a conformance issue;
955 * well-behaved apps shouldn't hit this) */
956
957 if (panfrost_scissor_culls_everything(ctx))
958 return;
959
960 int mode = info->mode;
961
962 /* Fallback unsupported restart index */
963 unsigned primitive_index = (1 << (info->index_size * 8)) - 1;
964
965 if (info->primitive_restart && info->index_size
966 && info->restart_index != primitive_index) {
967 util_draw_vbo_without_prim_restart(pipe, info);
968 return;
969 }
970
971 /* Fallback for unsupported modes */
972
973 assert(ctx->rasterizer != NULL);
974
975 if (!(ctx->draw_modes & (1 << mode))) {
976 if (mode == PIPE_PRIM_QUADS && info->count == 4 && !ctx->rasterizer->base.flatshade) {
977 mode = PIPE_PRIM_TRIANGLE_FAN;
978 } else {
979 if (info->count < 4) {
980 /* Degenerate case? */
981 return;
982 }
983
984 util_primconvert_save_rasterizer_state(ctx->primconvert, &ctx->rasterizer->base);
985 util_primconvert_draw_vbo(ctx->primconvert, info);
986 return;
987 }
988 }
989
990 ctx->payloads[PIPE_SHADER_VERTEX].offset_start = info->start;
991 ctx->payloads[PIPE_SHADER_FRAGMENT].offset_start = info->start;
992
993 /* Now that we have a guaranteed terminating path, find the job.
994 * Assignment commented out to prevent unused warning */
995
996 /* struct panfrost_batch *batch = */ panfrost_get_batch_for_fbo(ctx);
997
998 ctx->payloads[PIPE_SHADER_FRAGMENT].prefix.draw_mode = g2m_draw_mode(mode);
999
1000 /* Take into account a negative bias */
1001 ctx->vertex_count = info->count + abs(info->index_bias);
1002 ctx->instance_count = info->instance_count;
1003 ctx->active_prim = info->mode;
1004
1005 /* For non-indexed draws, they're the same */
1006 unsigned vertex_count = ctx->vertex_count;
1007
1008 unsigned draw_flags = 0;
1009
1010 /* The draw flags interpret how primitive size is interpreted */
1011
1012 if (panfrost_writes_point_size(ctx))
1013 draw_flags |= MALI_DRAW_VARYING_SIZE;
1014
1015 if (info->primitive_restart)
1016 draw_flags |= MALI_DRAW_PRIMITIVE_RESTART_FIXED_INDEX;
1017
1018 /* These doesn't make much sense */
1019
1020 draw_flags |= 0x3000;
1021
1022 if (ctx->rasterizer && ctx->rasterizer->base.flatshade_first)
1023 draw_flags |= MALI_DRAW_FLATSHADE_FIRST;
1024
1025 panfrost_statistics_record(ctx, info);
1026
1027 if (info->index_size) {
1028 unsigned min_index = 0, max_index = 0;
1029 ctx->payloads[PIPE_SHADER_FRAGMENT].prefix.indices =
1030 panfrost_get_index_buffer_bounded(ctx, info, &min_index, &max_index);
1031
1032 /* Use the corresponding values */
1033 vertex_count = max_index - min_index + 1;
1034 ctx->payloads[PIPE_SHADER_VERTEX].offset_start = min_index + info->index_bias;
1035 ctx->payloads[PIPE_SHADER_FRAGMENT].offset_start = min_index + info->index_bias;
1036
1037 ctx->payloads[PIPE_SHADER_FRAGMENT].prefix.offset_bias_correction = -min_index;
1038 ctx->payloads[PIPE_SHADER_FRAGMENT].prefix.index_count = MALI_POSITIVE(info->count);
1039
1040 draw_flags |= panfrost_translate_index_size(info->index_size);
1041 } else {
1042 /* Index count == vertex count, if no indexing is applied, as
1043 * if it is internally indexed in the expected order */
1044
1045 ctx->payloads[PIPE_SHADER_FRAGMENT].prefix.offset_bias_correction = 0;
1046 ctx->payloads[PIPE_SHADER_FRAGMENT].prefix.index_count = MALI_POSITIVE(ctx->vertex_count);
1047
1048 /* Reverse index state */
1049 ctx->payloads[PIPE_SHADER_FRAGMENT].prefix.indices = (mali_ptr) 0;
1050 }
1051
1052 /* Dispatch "compute jobs" for the vertex/tiler pair as (1,
1053 * vertex_count, 1) */
1054
1055 panfrost_pack_work_groups_fused(
1056 &ctx->payloads[PIPE_SHADER_VERTEX].prefix,
1057 &ctx->payloads[PIPE_SHADER_FRAGMENT].prefix,
1058 1, vertex_count, info->instance_count,
1059 1, 1, 1);
1060
1061 ctx->payloads[PIPE_SHADER_FRAGMENT].prefix.unknown_draw = draw_flags;
1062
1063 /* Encode the padded vertex count */
1064
1065 if (info->instance_count > 1) {
1066 ctx->padded_count = panfrost_padded_vertex_count(vertex_count);
1067
1068 unsigned shift = __builtin_ctz(ctx->padded_count);
1069 unsigned k = ctx->padded_count >> (shift + 1);
1070
1071 ctx->payloads[PIPE_SHADER_VERTEX].instance_shift = shift;
1072 ctx->payloads[PIPE_SHADER_FRAGMENT].instance_shift = shift;
1073
1074 ctx->payloads[PIPE_SHADER_VERTEX].instance_odd = k;
1075 ctx->payloads[PIPE_SHADER_FRAGMENT].instance_odd = k;
1076 } else {
1077 ctx->padded_count = vertex_count;
1078
1079 /* Reset instancing state */
1080 ctx->payloads[PIPE_SHADER_VERTEX].instance_shift = 0;
1081 ctx->payloads[PIPE_SHADER_VERTEX].instance_odd = 0;
1082 ctx->payloads[PIPE_SHADER_FRAGMENT].instance_shift = 0;
1083 ctx->payloads[PIPE_SHADER_FRAGMENT].instance_odd = 0;
1084 }
1085
1086 /* Fire off the draw itself */
1087 panfrost_queue_draw(ctx);
1088
1089 /* Increment transform feedback offsets */
1090
1091 for (unsigned i = 0; i < ctx->streamout.num_targets; ++i) {
1092 unsigned output_count = u_stream_outputs_for_vertices(
1093 ctx->active_prim, ctx->vertex_count);
1094
1095 ctx->streamout.offsets[i] += output_count;
1096 }
1097 }
1098
1099 /* CSO state */
1100
1101 static void
1102 panfrost_generic_cso_delete(struct pipe_context *pctx, void *hwcso)
1103 {
1104 free(hwcso);
1105 }
1106
1107 static void *
1108 panfrost_create_rasterizer_state(
1109 struct pipe_context *pctx,
1110 const struct pipe_rasterizer_state *cso)
1111 {
1112 struct panfrost_rasterizer *so = CALLOC_STRUCT(panfrost_rasterizer);
1113
1114 so->base = *cso;
1115
1116 /* Bitmask, unknown meaning of the start value. 0x105 on 32-bit T6XX */
1117 so->tiler_gl_enables = 0x7;
1118
1119 if (cso->front_ccw)
1120 so->tiler_gl_enables |= MALI_FRONT_CCW_TOP;
1121
1122 if (cso->cull_face & PIPE_FACE_FRONT)
1123 so->tiler_gl_enables |= MALI_CULL_FACE_FRONT;
1124
1125 if (cso->cull_face & PIPE_FACE_BACK)
1126 so->tiler_gl_enables |= MALI_CULL_FACE_BACK;
1127
1128 return so;
1129 }
1130
1131 static void
1132 panfrost_bind_rasterizer_state(
1133 struct pipe_context *pctx,
1134 void *hwcso)
1135 {
1136 struct panfrost_context *ctx = pan_context(pctx);
1137
1138 ctx->rasterizer = hwcso;
1139
1140 if (!hwcso)
1141 return;
1142
1143 ctx->fragment_shader_core.depth_units = ctx->rasterizer->base.offset_units * 2.0f;
1144 ctx->fragment_shader_core.depth_factor = ctx->rasterizer->base.offset_scale;
1145
1146 /* Gauranteed with the core GL call, so don't expose ARB_polygon_offset */
1147 assert(ctx->rasterizer->base.offset_clamp == 0.0);
1148
1149 /* XXX: Which bit is which? Does this maybe allow offseting not-tri? */
1150
1151 SET_BIT(ctx->fragment_shader_core.unknown2_4, MALI_DEPTH_RANGE_A, ctx->rasterizer->base.offset_tri);
1152 SET_BIT(ctx->fragment_shader_core.unknown2_4, MALI_DEPTH_RANGE_B, ctx->rasterizer->base.offset_tri);
1153
1154 /* Point sprites are emulated */
1155
1156 struct panfrost_shader_state *variant = panfrost_get_shader_state(ctx, PIPE_SHADER_FRAGMENT);
1157
1158 if (ctx->rasterizer->base.sprite_coord_enable || (variant && variant->point_sprite_mask))
1159 ctx->base.bind_fs_state(&ctx->base, ctx->shader[PIPE_SHADER_FRAGMENT]);
1160 }
1161
1162 static void *
1163 panfrost_create_vertex_elements_state(
1164 struct pipe_context *pctx,
1165 unsigned num_elements,
1166 const struct pipe_vertex_element *elements)
1167 {
1168 struct panfrost_vertex_state *so = CALLOC_STRUCT(panfrost_vertex_state);
1169
1170 so->num_elements = num_elements;
1171 memcpy(so->pipe, elements, sizeof(*elements) * num_elements);
1172
1173 for (int i = 0; i < num_elements; ++i) {
1174 so->hw[i].index = i;
1175
1176 enum pipe_format fmt = elements[i].src_format;
1177 const struct util_format_description *desc = util_format_description(fmt);
1178 so->hw[i].unknown1 = 0x2;
1179 so->hw[i].swizzle = panfrost_get_default_swizzle(desc->nr_channels);
1180
1181 so->hw[i].format = panfrost_find_format(desc);
1182
1183 /* The field itself should probably be shifted over */
1184 so->hw[i].src_offset = elements[i].src_offset;
1185 }
1186
1187 return so;
1188 }
1189
1190 static void
1191 panfrost_bind_vertex_elements_state(
1192 struct pipe_context *pctx,
1193 void *hwcso)
1194 {
1195 struct panfrost_context *ctx = pan_context(pctx);
1196 ctx->vertex = hwcso;
1197 }
1198
1199 static void *
1200 panfrost_create_shader_state(
1201 struct pipe_context *pctx,
1202 const struct pipe_shader_state *cso,
1203 enum pipe_shader_type stage)
1204 {
1205 struct panfrost_shader_variants *so = CALLOC_STRUCT(panfrost_shader_variants);
1206 so->base = *cso;
1207
1208 /* Token deep copy to prevent memory corruption */
1209
1210 if (cso->type == PIPE_SHADER_IR_TGSI)
1211 so->base.tokens = tgsi_dup_tokens(so->base.tokens);
1212
1213 /* Precompile for shader-db if we need to */
1214 if (unlikely((pan_debug & PAN_DBG_PRECOMPILE) && cso->type == PIPE_SHADER_IR_NIR)) {
1215 struct panfrost_context *ctx = pan_context(pctx);
1216
1217 struct mali_shader_meta meta;
1218 struct panfrost_shader_state state;
1219 uint64_t outputs_written;
1220
1221 panfrost_shader_compile(ctx, &meta,
1222 PIPE_SHADER_IR_NIR,
1223 so->base.ir.nir,
1224 tgsi_processor_to_shader_stage(stage), &state,
1225 &outputs_written);
1226 }
1227
1228 return so;
1229 }
1230
1231 static void
1232 panfrost_delete_shader_state(
1233 struct pipe_context *pctx,
1234 void *so)
1235 {
1236 struct panfrost_shader_variants *cso = (struct panfrost_shader_variants *) so;
1237
1238 if (cso->base.type == PIPE_SHADER_IR_TGSI) {
1239 DBG("Deleting TGSI shader leaks duplicated tokens\n");
1240 }
1241
1242 for (unsigned i = 0; i < cso->variant_count; ++i) {
1243 struct panfrost_shader_state *shader_state = &cso->variants[i];
1244 panfrost_bo_unreference(shader_state->bo);
1245 shader_state->bo = NULL;
1246 }
1247 free(cso->variants);
1248
1249 free(so);
1250 }
1251
1252 static void *
1253 panfrost_create_sampler_state(
1254 struct pipe_context *pctx,
1255 const struct pipe_sampler_state *cso)
1256 {
1257 struct panfrost_sampler_state *so = CALLOC_STRUCT(panfrost_sampler_state);
1258 so->base = *cso;
1259
1260 /* sampler_state corresponds to mali_sampler_descriptor, which we can generate entirely here */
1261
1262 bool min_nearest = cso->min_img_filter == PIPE_TEX_FILTER_NEAREST;
1263 bool mag_nearest = cso->mag_img_filter == PIPE_TEX_FILTER_NEAREST;
1264 bool mip_linear = cso->min_mip_filter == PIPE_TEX_MIPFILTER_LINEAR;
1265
1266 unsigned min_filter = min_nearest ? MALI_SAMP_MIN_NEAREST : 0;
1267 unsigned mag_filter = mag_nearest ? MALI_SAMP_MAG_NEAREST : 0;
1268 unsigned mip_filter = mip_linear ?
1269 (MALI_SAMP_MIP_LINEAR_1 | MALI_SAMP_MIP_LINEAR_2) : 0;
1270 unsigned normalized = cso->normalized_coords ? MALI_SAMP_NORM_COORDS : 0;
1271
1272 struct mali_sampler_descriptor sampler_descriptor = {
1273 .filter_mode = min_filter | mag_filter | mip_filter | normalized,
1274 .wrap_s = translate_tex_wrap(cso->wrap_s),
1275 .wrap_t = translate_tex_wrap(cso->wrap_t),
1276 .wrap_r = translate_tex_wrap(cso->wrap_r),
1277 .compare_func = panfrost_flip_compare_func(
1278 panfrost_translate_compare_func(
1279 cso->compare_func)),
1280 .border_color = {
1281 cso->border_color.f[0],
1282 cso->border_color.f[1],
1283 cso->border_color.f[2],
1284 cso->border_color.f[3]
1285 },
1286 .min_lod = FIXED_16(cso->min_lod, false), /* clamp at 0 */
1287 .max_lod = FIXED_16(cso->max_lod, false),
1288 .lod_bias = FIXED_16(cso->lod_bias, true), /* can be negative */
1289 .seamless_cube_map = cso->seamless_cube_map,
1290 };
1291
1292 /* If necessary, we disable mipmapping in the sampler descriptor by
1293 * clamping the LOD as tight as possible (from 0 to epsilon,
1294 * essentially -- remember these are fixed point numbers, so
1295 * epsilon=1/256) */
1296
1297 if (cso->min_mip_filter == PIPE_TEX_MIPFILTER_NONE) {
1298 sampler_descriptor.max_lod = sampler_descriptor.min_lod;
1299
1300 /* Enforce that there is something in the middle by adding epsilon*/
1301
1302 if (sampler_descriptor.min_lod == sampler_descriptor.max_lod)
1303 sampler_descriptor.max_lod++;
1304
1305 /* Sanity check */
1306 assert(sampler_descriptor.max_lod > sampler_descriptor.min_lod);
1307 }
1308
1309 so->hw = sampler_descriptor;
1310
1311 return so;
1312 }
1313
1314 static void
1315 panfrost_bind_sampler_states(
1316 struct pipe_context *pctx,
1317 enum pipe_shader_type shader,
1318 unsigned start_slot, unsigned num_sampler,
1319 void **sampler)
1320 {
1321 assert(start_slot == 0);
1322
1323 struct panfrost_context *ctx = pan_context(pctx);
1324
1325 /* XXX: Should upload, not just copy? */
1326 ctx->sampler_count[shader] = num_sampler;
1327 memcpy(ctx->samplers[shader], sampler, num_sampler * sizeof (void *));
1328 }
1329
1330 static bool
1331 panfrost_variant_matches(
1332 struct panfrost_context *ctx,
1333 struct panfrost_shader_state *variant,
1334 enum pipe_shader_type type)
1335 {
1336 struct pipe_rasterizer_state *rasterizer = &ctx->rasterizer->base;
1337 struct pipe_alpha_state *alpha = &ctx->depth_stencil->alpha;
1338
1339 bool is_fragment = (type == PIPE_SHADER_FRAGMENT);
1340
1341 if (is_fragment && (alpha->enabled || variant->alpha_state.enabled)) {
1342 /* Make sure enable state is at least the same */
1343 if (alpha->enabled != variant->alpha_state.enabled) {
1344 return false;
1345 }
1346
1347 /* Check that the contents of the test are the same */
1348 bool same_func = alpha->func == variant->alpha_state.func;
1349 bool same_ref = alpha->ref_value == variant->alpha_state.ref_value;
1350
1351 if (!(same_func && same_ref)) {
1352 return false;
1353 }
1354 }
1355
1356 if (is_fragment && rasterizer && (rasterizer->sprite_coord_enable |
1357 variant->point_sprite_mask)) {
1358 /* Ensure the same varyings are turned to point sprites */
1359 if (rasterizer->sprite_coord_enable != variant->point_sprite_mask)
1360 return false;
1361
1362 /* Ensure the orientation is correct */
1363 bool upper_left =
1364 rasterizer->sprite_coord_mode ==
1365 PIPE_SPRITE_COORD_UPPER_LEFT;
1366
1367 if (variant->point_sprite_upper_left != upper_left)
1368 return false;
1369 }
1370
1371 /* Otherwise, we're good to go */
1372 return true;
1373 }
1374
1375 /**
1376 * Fix an uncompiled shader's stream output info, and produce a bitmask
1377 * of which VARYING_SLOT_* are captured for stream output.
1378 *
1379 * Core Gallium stores output->register_index as a "slot" number, where
1380 * slots are assigned consecutively to all outputs in info->outputs_written.
1381 * This naive packing of outputs doesn't work for us - we too have slots,
1382 * but the layout is defined by the VUE map, which we won't have until we
1383 * compile a specific shader variant. So, we remap these and simply store
1384 * VARYING_SLOT_* in our copy's output->register_index fields.
1385 *
1386 * We then produce a bitmask of outputs which are used for SO.
1387 *
1388 * Implementation from iris.
1389 */
1390
1391 static uint64_t
1392 update_so_info(struct pipe_stream_output_info *so_info,
1393 uint64_t outputs_written)
1394 {
1395 uint64_t so_outputs = 0;
1396 uint8_t reverse_map[64] = {0};
1397 unsigned slot = 0;
1398
1399 while (outputs_written)
1400 reverse_map[slot++] = u_bit_scan64(&outputs_written);
1401
1402 for (unsigned i = 0; i < so_info->num_outputs; i++) {
1403 struct pipe_stream_output *output = &so_info->output[i];
1404
1405 /* Map Gallium's condensed "slots" back to real VARYING_SLOT_* enums */
1406 output->register_index = reverse_map[output->register_index];
1407
1408 so_outputs |= 1ull << output->register_index;
1409 }
1410
1411 return so_outputs;
1412 }
1413
1414 static void
1415 panfrost_bind_shader_state(
1416 struct pipe_context *pctx,
1417 void *hwcso,
1418 enum pipe_shader_type type)
1419 {
1420 struct panfrost_context *ctx = pan_context(pctx);
1421 ctx->shader[type] = hwcso;
1422
1423 if (!hwcso) return;
1424
1425 /* Match the appropriate variant */
1426
1427 signed variant = -1;
1428 struct panfrost_shader_variants *variants = (struct panfrost_shader_variants *) hwcso;
1429
1430 for (unsigned i = 0; i < variants->variant_count; ++i) {
1431 if (panfrost_variant_matches(ctx, &variants->variants[i], type)) {
1432 variant = i;
1433 break;
1434 }
1435 }
1436
1437 if (variant == -1) {
1438 /* No variant matched, so create a new one */
1439 variant = variants->variant_count++;
1440
1441 if (variants->variant_count > variants->variant_space) {
1442 unsigned old_space = variants->variant_space;
1443
1444 variants->variant_space *= 2;
1445 if (variants->variant_space == 0)
1446 variants->variant_space = 1;
1447
1448 /* Arbitrary limit to stop runaway programs from
1449 * creating an unbounded number of shader variants. */
1450 assert(variants->variant_space < 1024);
1451
1452 unsigned msize = sizeof(struct panfrost_shader_state);
1453 variants->variants = realloc(variants->variants,
1454 variants->variant_space * msize);
1455
1456 memset(&variants->variants[old_space], 0,
1457 (variants->variant_space - old_space) * msize);
1458 }
1459
1460 struct panfrost_shader_state *v =
1461 &variants->variants[variant];
1462
1463 if (type == PIPE_SHADER_FRAGMENT) {
1464 v->alpha_state = ctx->depth_stencil->alpha;
1465
1466 if (ctx->rasterizer) {
1467 v->point_sprite_mask = ctx->rasterizer->base.sprite_coord_enable;
1468 v->point_sprite_upper_left =
1469 ctx->rasterizer->base.sprite_coord_mode ==
1470 PIPE_SPRITE_COORD_UPPER_LEFT;
1471 }
1472 }
1473
1474 variants->variants[variant].tripipe = calloc(1, sizeof(struct mali_shader_meta));
1475
1476 }
1477
1478 /* Select this variant */
1479 variants->active_variant = variant;
1480
1481 struct panfrost_shader_state *shader_state = &variants->variants[variant];
1482 assert(panfrost_variant_matches(ctx, shader_state, type));
1483
1484 /* We finally have a variant, so compile it */
1485
1486 if (!shader_state->compiled) {
1487 uint64_t outputs_written = 0;
1488
1489 panfrost_shader_compile(ctx, shader_state->tripipe,
1490 variants->base.type,
1491 variants->base.type == PIPE_SHADER_IR_NIR ?
1492 variants->base.ir.nir :
1493 variants->base.tokens,
1494 tgsi_processor_to_shader_stage(type), shader_state,
1495 &outputs_written);
1496
1497 shader_state->compiled = true;
1498
1499 /* Fixup the stream out information, since what Gallium returns
1500 * normally is mildly insane */
1501
1502 shader_state->stream_output = variants->base.stream_output;
1503 shader_state->so_mask =
1504 update_so_info(&shader_state->stream_output, outputs_written);
1505 }
1506 }
1507
1508 static void *
1509 panfrost_create_vs_state(struct pipe_context *pctx, const struct pipe_shader_state *hwcso)
1510 {
1511 return panfrost_create_shader_state(pctx, hwcso, PIPE_SHADER_VERTEX);
1512 }
1513
1514 static void *
1515 panfrost_create_fs_state(struct pipe_context *pctx, const struct pipe_shader_state *hwcso)
1516 {
1517 return panfrost_create_shader_state(pctx, hwcso, PIPE_SHADER_FRAGMENT);
1518 }
1519
1520 static void
1521 panfrost_bind_vs_state(struct pipe_context *pctx, void *hwcso)
1522 {
1523 panfrost_bind_shader_state(pctx, hwcso, PIPE_SHADER_VERTEX);
1524 }
1525
1526 static void
1527 panfrost_bind_fs_state(struct pipe_context *pctx, void *hwcso)
1528 {
1529 panfrost_bind_shader_state(pctx, hwcso, PIPE_SHADER_FRAGMENT);
1530 }
1531
1532 static void
1533 panfrost_set_vertex_buffers(
1534 struct pipe_context *pctx,
1535 unsigned start_slot,
1536 unsigned num_buffers,
1537 const struct pipe_vertex_buffer *buffers)
1538 {
1539 struct panfrost_context *ctx = pan_context(pctx);
1540
1541 util_set_vertex_buffers_mask(ctx->vertex_buffers, &ctx->vb_mask, buffers, start_slot, num_buffers);
1542 }
1543
1544 static void
1545 panfrost_set_constant_buffer(
1546 struct pipe_context *pctx,
1547 enum pipe_shader_type shader, uint index,
1548 const struct pipe_constant_buffer *buf)
1549 {
1550 struct panfrost_context *ctx = pan_context(pctx);
1551 struct panfrost_constant_buffer *pbuf = &ctx->constant_buffer[shader];
1552
1553 util_copy_constant_buffer(&pbuf->cb[index], buf);
1554
1555 unsigned mask = (1 << index);
1556
1557 if (unlikely(!buf)) {
1558 pbuf->enabled_mask &= ~mask;
1559 pbuf->dirty_mask &= ~mask;
1560 return;
1561 }
1562
1563 pbuf->enabled_mask |= mask;
1564 pbuf->dirty_mask |= mask;
1565 }
1566
1567 static void
1568 panfrost_set_stencil_ref(
1569 struct pipe_context *pctx,
1570 const struct pipe_stencil_ref *ref)
1571 {
1572 struct panfrost_context *ctx = pan_context(pctx);
1573 ctx->stencil_ref = *ref;
1574 }
1575
1576 static enum mali_texture_type
1577 panfrost_translate_texture_type(enum pipe_texture_target t) {
1578 switch (t)
1579 {
1580 case PIPE_BUFFER:
1581 case PIPE_TEXTURE_1D:
1582 case PIPE_TEXTURE_1D_ARRAY:
1583 return MALI_TEX_1D;
1584
1585 case PIPE_TEXTURE_2D:
1586 case PIPE_TEXTURE_2D_ARRAY:
1587 case PIPE_TEXTURE_RECT:
1588 return MALI_TEX_2D;
1589
1590 case PIPE_TEXTURE_3D:
1591 return MALI_TEX_3D;
1592
1593 case PIPE_TEXTURE_CUBE:
1594 case PIPE_TEXTURE_CUBE_ARRAY:
1595 return MALI_TEX_CUBE;
1596
1597 default:
1598 unreachable("Unknown target");
1599 }
1600 }
1601
1602 static struct pipe_sampler_view *
1603 panfrost_create_sampler_view(
1604 struct pipe_context *pctx,
1605 struct pipe_resource *texture,
1606 const struct pipe_sampler_view *template)
1607 {
1608 struct panfrost_screen *screen = pan_screen(pctx->screen);
1609 struct panfrost_sampler_view *so = rzalloc(pctx, struct panfrost_sampler_view);
1610
1611 pipe_reference(NULL, &texture->reference);
1612
1613 struct panfrost_resource *prsrc = (struct panfrost_resource *) texture;
1614 assert(prsrc->bo);
1615
1616 so->base = *template;
1617 so->base.texture = texture;
1618 so->base.reference.count = 1;
1619 so->base.context = pctx;
1620
1621 unsigned char user_swizzle[4] = {
1622 template->swizzle_r,
1623 template->swizzle_g,
1624 template->swizzle_b,
1625 template->swizzle_a
1626 };
1627
1628 /* In the hardware, array_size refers specifically to array textures,
1629 * whereas in Gallium, it also covers cubemaps */
1630
1631 unsigned array_size = texture->array_size;
1632
1633 if (template->target == PIPE_TEXTURE_CUBE) {
1634 /* TODO: Cubemap arrays */
1635 assert(array_size == 6);
1636 array_size /= 6;
1637 }
1638
1639 enum mali_texture_type type =
1640 panfrost_translate_texture_type(template->target);
1641
1642 unsigned size = panfrost_estimate_texture_size(
1643 template->u.tex.first_level,
1644 template->u.tex.last_level,
1645 template->u.tex.first_layer,
1646 template->u.tex.last_layer,
1647 type, prsrc->layout);
1648
1649 so->bo = panfrost_bo_create(screen, size, 0);
1650
1651 panfrost_new_texture(
1652 so->bo->cpu,
1653 texture->width0, texture->height0,
1654 texture->depth0, array_size,
1655 template->format,
1656 type, prsrc->layout,
1657 template->u.tex.first_level,
1658 template->u.tex.last_level,
1659 template->u.tex.first_layer,
1660 template->u.tex.last_layer,
1661 prsrc->cubemap_stride,
1662 panfrost_translate_swizzle_4(user_swizzle),
1663 prsrc->bo->gpu,
1664 prsrc->slices);
1665
1666 return (struct pipe_sampler_view *) so;
1667 }
1668
1669 static void
1670 panfrost_set_sampler_views(
1671 struct pipe_context *pctx,
1672 enum pipe_shader_type shader,
1673 unsigned start_slot, unsigned num_views,
1674 struct pipe_sampler_view **views)
1675 {
1676 struct panfrost_context *ctx = pan_context(pctx);
1677 unsigned new_nr = 0;
1678 unsigned i;
1679
1680 assert(start_slot == 0);
1681
1682 for (i = 0; i < num_views; ++i) {
1683 if (views[i])
1684 new_nr = i + 1;
1685 pipe_sampler_view_reference((struct pipe_sampler_view **)&ctx->sampler_views[shader][i],
1686 views[i]);
1687 }
1688
1689 for (; i < ctx->sampler_view_count[shader]; i++) {
1690 pipe_sampler_view_reference((struct pipe_sampler_view **)&ctx->sampler_views[shader][i],
1691 NULL);
1692 }
1693 ctx->sampler_view_count[shader] = new_nr;
1694 }
1695
1696 static void
1697 panfrost_sampler_view_destroy(
1698 struct pipe_context *pctx,
1699 struct pipe_sampler_view *pview)
1700 {
1701 struct panfrost_sampler_view *view = (struct panfrost_sampler_view *) pview;
1702
1703 pipe_resource_reference(&pview->texture, NULL);
1704 panfrost_bo_unreference(view->bo);
1705 ralloc_free(view);
1706 }
1707
1708 static void
1709 panfrost_set_shader_buffers(
1710 struct pipe_context *pctx,
1711 enum pipe_shader_type shader,
1712 unsigned start, unsigned count,
1713 const struct pipe_shader_buffer *buffers,
1714 unsigned writable_bitmask)
1715 {
1716 struct panfrost_context *ctx = pan_context(pctx);
1717
1718 util_set_shader_buffers_mask(ctx->ssbo[shader], &ctx->ssbo_mask[shader],
1719 buffers, start, count);
1720 }
1721
1722 /* Hints that a framebuffer should use AFBC where possible */
1723
1724 static void
1725 panfrost_hint_afbc(
1726 struct panfrost_screen *screen,
1727 const struct pipe_framebuffer_state *fb)
1728 {
1729 /* AFBC implemenation incomplete; hide it */
1730 if (!(pan_debug & PAN_DBG_AFBC)) return;
1731
1732 /* Hint AFBC to the resources bound to each color buffer */
1733
1734 for (unsigned i = 0; i < fb->nr_cbufs; ++i) {
1735 struct pipe_surface *surf = fb->cbufs[i];
1736 struct panfrost_resource *rsrc = pan_resource(surf->texture);
1737 panfrost_resource_hint_layout(screen, rsrc, MALI_TEXTURE_AFBC, 1);
1738 }
1739
1740 /* Also hint it to the depth buffer */
1741
1742 if (fb->zsbuf) {
1743 struct panfrost_resource *rsrc = pan_resource(fb->zsbuf->texture);
1744 panfrost_resource_hint_layout(screen, rsrc, MALI_TEXTURE_AFBC, 1);
1745 }
1746 }
1747
1748 static void
1749 panfrost_set_framebuffer_state(struct pipe_context *pctx,
1750 const struct pipe_framebuffer_state *fb)
1751 {
1752 struct panfrost_context *ctx = pan_context(pctx);
1753
1754 panfrost_hint_afbc(pan_screen(pctx->screen), fb);
1755 util_copy_framebuffer_state(&ctx->pipe_framebuffer, fb);
1756 ctx->batch = NULL;
1757 panfrost_invalidate_frame(ctx);
1758 }
1759
1760 static void *
1761 panfrost_create_depth_stencil_state(struct pipe_context *pipe,
1762 const struct pipe_depth_stencil_alpha_state *depth_stencil)
1763 {
1764 return mem_dup(depth_stencil, sizeof(*depth_stencil));
1765 }
1766
1767 static void
1768 panfrost_bind_depth_stencil_state(struct pipe_context *pipe,
1769 void *cso)
1770 {
1771 struct panfrost_context *ctx = pan_context(pipe);
1772 struct pipe_depth_stencil_alpha_state *depth_stencil = cso;
1773 ctx->depth_stencil = depth_stencil;
1774
1775 if (!depth_stencil)
1776 return;
1777
1778 /* Alpha does not exist in the hardware (it's not in ES3), so it's
1779 * emulated in the fragment shader */
1780
1781 if (depth_stencil->alpha.enabled) {
1782 /* We need to trigger a new shader (maybe) */
1783 ctx->base.bind_fs_state(&ctx->base, ctx->shader[PIPE_SHADER_FRAGMENT]);
1784 }
1785
1786 /* Stencil state */
1787 SET_BIT(ctx->fragment_shader_core.unknown2_4, MALI_STENCIL_TEST, depth_stencil->stencil[0].enabled);
1788
1789 panfrost_make_stencil_state(&depth_stencil->stencil[0], &ctx->fragment_shader_core.stencil_front);
1790 ctx->fragment_shader_core.stencil_mask_front = depth_stencil->stencil[0].writemask;
1791
1792 /* If back-stencil is not enabled, use the front values */
1793 bool back_enab = ctx->depth_stencil->stencil[1].enabled;
1794 unsigned back_index = back_enab ? 1 : 0;
1795
1796 panfrost_make_stencil_state(&depth_stencil->stencil[back_index], &ctx->fragment_shader_core.stencil_back);
1797 ctx->fragment_shader_core.stencil_mask_back = depth_stencil->stencil[back_index].writemask;
1798
1799 /* Depth state (TODO: Refactor) */
1800 SET_BIT(ctx->fragment_shader_core.unknown2_3, MALI_DEPTH_WRITEMASK,
1801 depth_stencil->depth.writemask);
1802
1803 int func = depth_stencil->depth.enabled ? depth_stencil->depth.func : PIPE_FUNC_ALWAYS;
1804
1805 ctx->fragment_shader_core.unknown2_3 &= ~MALI_DEPTH_FUNC_MASK;
1806 ctx->fragment_shader_core.unknown2_3 |= MALI_DEPTH_FUNC(panfrost_translate_compare_func(func));
1807
1808 /* Bounds test not implemented */
1809 assert(!depth_stencil->depth.bounds_test);
1810 }
1811
1812 static void
1813 panfrost_delete_depth_stencil_state(struct pipe_context *pipe, void *depth)
1814 {
1815 free( depth );
1816 }
1817
1818 static void
1819 panfrost_set_sample_mask(struct pipe_context *pipe,
1820 unsigned sample_mask)
1821 {
1822 }
1823
1824 static void
1825 panfrost_set_clip_state(struct pipe_context *pipe,
1826 const struct pipe_clip_state *clip)
1827 {
1828 //struct panfrost_context *panfrost = pan_context(pipe);
1829 }
1830
1831 static void
1832 panfrost_set_viewport_states(struct pipe_context *pipe,
1833 unsigned start_slot,
1834 unsigned num_viewports,
1835 const struct pipe_viewport_state *viewports)
1836 {
1837 struct panfrost_context *ctx = pan_context(pipe);
1838
1839 assert(start_slot == 0);
1840 assert(num_viewports == 1);
1841
1842 ctx->pipe_viewport = *viewports;
1843 }
1844
1845 static void
1846 panfrost_set_scissor_states(struct pipe_context *pipe,
1847 unsigned start_slot,
1848 unsigned num_scissors,
1849 const struct pipe_scissor_state *scissors)
1850 {
1851 struct panfrost_context *ctx = pan_context(pipe);
1852
1853 assert(start_slot == 0);
1854 assert(num_scissors == 1);
1855
1856 ctx->scissor = *scissors;
1857 }
1858
1859 static void
1860 panfrost_set_polygon_stipple(struct pipe_context *pipe,
1861 const struct pipe_poly_stipple *stipple)
1862 {
1863 //struct panfrost_context *panfrost = pan_context(pipe);
1864 }
1865
1866 static void
1867 panfrost_set_active_query_state(struct pipe_context *pipe,
1868 bool enable)
1869 {
1870 struct panfrost_context *ctx = pan_context(pipe);
1871 ctx->active_queries = enable;
1872 }
1873
1874 static void
1875 panfrost_destroy(struct pipe_context *pipe)
1876 {
1877 struct panfrost_context *panfrost = pan_context(pipe);
1878
1879 if (panfrost->blitter)
1880 util_blitter_destroy(panfrost->blitter);
1881
1882 if (panfrost->blitter_wallpaper)
1883 util_blitter_destroy(panfrost->blitter_wallpaper);
1884
1885 util_unreference_framebuffer_state(&panfrost->pipe_framebuffer);
1886 u_upload_destroy(pipe->stream_uploader);
1887
1888 ralloc_free(pipe);
1889 }
1890
1891 static struct pipe_query *
1892 panfrost_create_query(struct pipe_context *pipe,
1893 unsigned type,
1894 unsigned index)
1895 {
1896 struct panfrost_query *q = rzalloc(pipe, struct panfrost_query);
1897
1898 q->type = type;
1899 q->index = index;
1900
1901 return (struct pipe_query *) q;
1902 }
1903
1904 static void
1905 panfrost_destroy_query(struct pipe_context *pipe, struct pipe_query *q)
1906 {
1907 struct panfrost_query *query = (struct panfrost_query *) q;
1908
1909 if (query->bo) {
1910 panfrost_bo_unreference(query->bo);
1911 query->bo = NULL;
1912 }
1913
1914 ralloc_free(q);
1915 }
1916
1917 static bool
1918 panfrost_begin_query(struct pipe_context *pipe, struct pipe_query *q)
1919 {
1920 struct panfrost_context *ctx = pan_context(pipe);
1921 struct panfrost_query *query = (struct panfrost_query *) q;
1922
1923 switch (query->type) {
1924 case PIPE_QUERY_OCCLUSION_COUNTER:
1925 case PIPE_QUERY_OCCLUSION_PREDICATE:
1926 case PIPE_QUERY_OCCLUSION_PREDICATE_CONSERVATIVE:
1927 /* Allocate a bo for the query results to be stored */
1928 if (!query->bo) {
1929 query->bo = panfrost_bo_create(
1930 pan_screen(ctx->base.screen),
1931 sizeof(unsigned), 0);
1932 }
1933
1934 unsigned *result = (unsigned *)query->bo->cpu;
1935 *result = 0; /* Default to 0 if nothing at all drawn. */
1936 ctx->occlusion_query = query;
1937 break;
1938
1939 /* Geometry statistics are computed in the driver. XXX: geom/tess
1940 * shaders.. */
1941
1942 case PIPE_QUERY_PRIMITIVES_GENERATED:
1943 query->start = ctx->prims_generated;
1944 break;
1945 case PIPE_QUERY_PRIMITIVES_EMITTED:
1946 query->start = ctx->tf_prims_generated;
1947 break;
1948
1949 default:
1950 DBG("Skipping query %u\n", query->type);
1951 break;
1952 }
1953
1954 return true;
1955 }
1956
1957 static bool
1958 panfrost_end_query(struct pipe_context *pipe, struct pipe_query *q)
1959 {
1960 struct panfrost_context *ctx = pan_context(pipe);
1961 struct panfrost_query *query = (struct panfrost_query *) q;
1962
1963 switch (query->type) {
1964 case PIPE_QUERY_OCCLUSION_COUNTER:
1965 case PIPE_QUERY_OCCLUSION_PREDICATE:
1966 case PIPE_QUERY_OCCLUSION_PREDICATE_CONSERVATIVE:
1967 ctx->occlusion_query = NULL;
1968 break;
1969 case PIPE_QUERY_PRIMITIVES_GENERATED:
1970 query->end = ctx->prims_generated;
1971 break;
1972 case PIPE_QUERY_PRIMITIVES_EMITTED:
1973 query->end = ctx->tf_prims_generated;
1974 break;
1975 }
1976
1977 return true;
1978 }
1979
1980 static bool
1981 panfrost_get_query_result(struct pipe_context *pipe,
1982 struct pipe_query *q,
1983 bool wait,
1984 union pipe_query_result *vresult)
1985 {
1986 struct panfrost_query *query = (struct panfrost_query *) q;
1987 struct panfrost_context *ctx = pan_context(pipe);
1988
1989
1990 switch (query->type) {
1991 case PIPE_QUERY_OCCLUSION_COUNTER:
1992 case PIPE_QUERY_OCCLUSION_PREDICATE:
1993 case PIPE_QUERY_OCCLUSION_PREDICATE_CONSERVATIVE:
1994 /* Flush first */
1995 panfrost_flush_all_batches(ctx, true);
1996
1997 /* Read back the query results */
1998 unsigned *result = (unsigned *) query->bo->cpu;
1999 unsigned passed = *result;
2000
2001 if (query->type == PIPE_QUERY_OCCLUSION_COUNTER) {
2002 vresult->u64 = passed;
2003 } else {
2004 vresult->b = !!passed;
2005 }
2006
2007 break;
2008
2009 case PIPE_QUERY_PRIMITIVES_GENERATED:
2010 case PIPE_QUERY_PRIMITIVES_EMITTED:
2011 panfrost_flush_all_batches(ctx, true);
2012 vresult->u64 = query->end - query->start;
2013 break;
2014
2015 default:
2016 DBG("Skipped query get %u\n", query->type);
2017 break;
2018 }
2019
2020 return true;
2021 }
2022
2023 static struct pipe_stream_output_target *
2024 panfrost_create_stream_output_target(struct pipe_context *pctx,
2025 struct pipe_resource *prsc,
2026 unsigned buffer_offset,
2027 unsigned buffer_size)
2028 {
2029 struct pipe_stream_output_target *target;
2030
2031 target = rzalloc(pctx, struct pipe_stream_output_target);
2032
2033 if (!target)
2034 return NULL;
2035
2036 pipe_reference_init(&target->reference, 1);
2037 pipe_resource_reference(&target->buffer, prsc);
2038
2039 target->context = pctx;
2040 target->buffer_offset = buffer_offset;
2041 target->buffer_size = buffer_size;
2042
2043 return target;
2044 }
2045
2046 static void
2047 panfrost_stream_output_target_destroy(struct pipe_context *pctx,
2048 struct pipe_stream_output_target *target)
2049 {
2050 pipe_resource_reference(&target->buffer, NULL);
2051 ralloc_free(target);
2052 }
2053
2054 static void
2055 panfrost_set_stream_output_targets(struct pipe_context *pctx,
2056 unsigned num_targets,
2057 struct pipe_stream_output_target **targets,
2058 const unsigned *offsets)
2059 {
2060 struct panfrost_context *ctx = pan_context(pctx);
2061 struct panfrost_streamout *so = &ctx->streamout;
2062
2063 assert(num_targets <= ARRAY_SIZE(so->targets));
2064
2065 for (unsigned i = 0; i < num_targets; i++) {
2066 if (offsets[i] != -1)
2067 so->offsets[i] = offsets[i];
2068
2069 pipe_so_target_reference(&so->targets[i], targets[i]);
2070 }
2071
2072 for (unsigned i = 0; i < so->num_targets; i++)
2073 pipe_so_target_reference(&so->targets[i], NULL);
2074
2075 so->num_targets = num_targets;
2076 }
2077
2078 struct pipe_context *
2079 panfrost_create_context(struct pipe_screen *screen, void *priv, unsigned flags)
2080 {
2081 struct panfrost_context *ctx = rzalloc(screen, struct panfrost_context);
2082 struct pipe_context *gallium = (struct pipe_context *) ctx;
2083
2084 gallium->screen = screen;
2085
2086 gallium->destroy = panfrost_destroy;
2087
2088 gallium->set_framebuffer_state = panfrost_set_framebuffer_state;
2089
2090 gallium->flush = panfrost_flush;
2091 gallium->clear = panfrost_clear;
2092 gallium->draw_vbo = panfrost_draw_vbo;
2093
2094 gallium->set_vertex_buffers = panfrost_set_vertex_buffers;
2095 gallium->set_constant_buffer = panfrost_set_constant_buffer;
2096 gallium->set_shader_buffers = panfrost_set_shader_buffers;
2097
2098 gallium->set_stencil_ref = panfrost_set_stencil_ref;
2099
2100 gallium->create_sampler_view = panfrost_create_sampler_view;
2101 gallium->set_sampler_views = panfrost_set_sampler_views;
2102 gallium->sampler_view_destroy = panfrost_sampler_view_destroy;
2103
2104 gallium->create_rasterizer_state = panfrost_create_rasterizer_state;
2105 gallium->bind_rasterizer_state = panfrost_bind_rasterizer_state;
2106 gallium->delete_rasterizer_state = panfrost_generic_cso_delete;
2107
2108 gallium->create_vertex_elements_state = panfrost_create_vertex_elements_state;
2109 gallium->bind_vertex_elements_state = panfrost_bind_vertex_elements_state;
2110 gallium->delete_vertex_elements_state = panfrost_generic_cso_delete;
2111
2112 gallium->create_fs_state = panfrost_create_fs_state;
2113 gallium->delete_fs_state = panfrost_delete_shader_state;
2114 gallium->bind_fs_state = panfrost_bind_fs_state;
2115
2116 gallium->create_vs_state = panfrost_create_vs_state;
2117 gallium->delete_vs_state = panfrost_delete_shader_state;
2118 gallium->bind_vs_state = panfrost_bind_vs_state;
2119
2120 gallium->create_sampler_state = panfrost_create_sampler_state;
2121 gallium->delete_sampler_state = panfrost_generic_cso_delete;
2122 gallium->bind_sampler_states = panfrost_bind_sampler_states;
2123
2124 gallium->create_depth_stencil_alpha_state = panfrost_create_depth_stencil_state;
2125 gallium->bind_depth_stencil_alpha_state = panfrost_bind_depth_stencil_state;
2126 gallium->delete_depth_stencil_alpha_state = panfrost_delete_depth_stencil_state;
2127
2128 gallium->set_sample_mask = panfrost_set_sample_mask;
2129
2130 gallium->set_clip_state = panfrost_set_clip_state;
2131 gallium->set_viewport_states = panfrost_set_viewport_states;
2132 gallium->set_scissor_states = panfrost_set_scissor_states;
2133 gallium->set_polygon_stipple = panfrost_set_polygon_stipple;
2134 gallium->set_active_query_state = panfrost_set_active_query_state;
2135
2136 gallium->create_query = panfrost_create_query;
2137 gallium->destroy_query = panfrost_destroy_query;
2138 gallium->begin_query = panfrost_begin_query;
2139 gallium->end_query = panfrost_end_query;
2140 gallium->get_query_result = panfrost_get_query_result;
2141
2142 gallium->create_stream_output_target = panfrost_create_stream_output_target;
2143 gallium->stream_output_target_destroy = panfrost_stream_output_target_destroy;
2144 gallium->set_stream_output_targets = panfrost_set_stream_output_targets;
2145
2146 panfrost_resource_context_init(gallium);
2147 panfrost_blend_context_init(gallium);
2148 panfrost_compute_context_init(gallium);
2149
2150 /* XXX: leaks */
2151 gallium->stream_uploader = u_upload_create_default(gallium);
2152 gallium->const_uploader = gallium->stream_uploader;
2153 assert(gallium->stream_uploader);
2154
2155 /* Midgard supports ES modes, plus QUADS/QUAD_STRIPS/POLYGON */
2156 ctx->draw_modes = (1 << (PIPE_PRIM_POLYGON + 1)) - 1;
2157
2158 ctx->primconvert = util_primconvert_create(gallium, ctx->draw_modes);
2159
2160 ctx->blitter = util_blitter_create(gallium);
2161 ctx->blitter_wallpaper = util_blitter_create(gallium);
2162
2163 assert(ctx->blitter);
2164 assert(ctx->blitter_wallpaper);
2165
2166 /* Prepare for render! */
2167
2168 panfrost_batch_init(ctx);
2169 panfrost_emit_vertex_payload(ctx);
2170 panfrost_invalidate_frame(ctx);
2171 panfrost_default_shader_backend(ctx);
2172
2173 return gallium;
2174 }