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