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