panfrost: Implement index buffer cache
[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 uint64_t ht_key = 0;
1278
1279 if (!info->has_user_indices) {
1280 /* Only resources can be directly mapped */
1281 panfrost_batch_add_bo(batch, rsrc->bo,
1282 PAN_BO_ACCESS_SHARED |
1283 PAN_BO_ACCESS_READ |
1284 PAN_BO_ACCESS_VERTEX_TILER);
1285 out = rsrc->bo->gpu + offset;
1286
1287 /* Check the cache */
1288 if (rsrc->index_cache) {
1289 ht_key = (((uint64_t) info->count) << 32) | info->start;
1290
1291 struct panfrost_minmax_cache *cache = rsrc->index_cache;
1292
1293 for (unsigned i = 0; i < cache->size; ++i) {
1294 if (cache->keys[i] == ht_key) {
1295 uint64_t hit = cache->values[i];
1296
1297 *min_index = hit & 0xffffffff;
1298 *max_index = hit >> 32;
1299 needs_indices = false;
1300 break;
1301 }
1302 }
1303 }
1304 } else {
1305 /* Otherwise, we need to upload to transient memory */
1306 const uint8_t *ibuf8 = (const uint8_t *) info->index.user;
1307 out = panfrost_upload_transient(batch, ibuf8 + offset, info->count * info->index_size);
1308 }
1309
1310 if (needs_indices) {
1311 /* Fallback */
1312 u_vbuf_get_minmax_index(&ctx->base, info, min_index, max_index);
1313
1314 if (!info->has_user_indices && rsrc->index_cache) {
1315 struct panfrost_minmax_cache *cache = rsrc->index_cache;
1316 uint64_t value = (*min_index) | (((uint64_t) *max_index) << 32);
1317 unsigned index = 0;
1318
1319 if (cache->size == PANFROST_MINMAX_SIZE) {
1320 index = cache->index++;
1321 cache->index = cache->index % PANFROST_MINMAX_SIZE;
1322 } else {
1323 index = cache->size++;
1324 }
1325
1326 cache->keys[index] = ht_key;
1327 cache->values[index] = value;
1328 }
1329 }
1330
1331
1332 return out;
1333 }
1334
1335 static bool
1336 panfrost_scissor_culls_everything(struct panfrost_context *ctx)
1337 {
1338 const struct pipe_scissor_state *ss = &ctx->scissor;
1339
1340 /* Check if we're scissoring at all */
1341
1342 if (!(ctx->rasterizer && ctx->rasterizer->base.scissor))
1343 return false;
1344
1345 return (ss->minx == ss->maxx) || (ss->miny == ss->maxy);
1346 }
1347
1348 /* Count generated primitives (when there is no geom/tess shaders) for
1349 * transform feedback */
1350
1351 static void
1352 panfrost_statistics_record(
1353 struct panfrost_context *ctx,
1354 const struct pipe_draw_info *info)
1355 {
1356 if (!ctx->active_queries)
1357 return;
1358
1359 uint32_t prims = u_prims_for_vertices(info->mode, info->count);
1360 ctx->prims_generated += prims;
1361
1362 if (!ctx->streamout.num_targets)
1363 return;
1364
1365 ctx->tf_prims_generated += prims;
1366 }
1367
1368 static void
1369 panfrost_draw_vbo(
1370 struct pipe_context *pipe,
1371 const struct pipe_draw_info *info)
1372 {
1373 struct panfrost_context *ctx = pan_context(pipe);
1374
1375 /* First of all, check the scissor to see if anything is drawn at all.
1376 * If it's not, we drop the draw (mostly a conformance issue;
1377 * well-behaved apps shouldn't hit this) */
1378
1379 if (panfrost_scissor_culls_everything(ctx))
1380 return;
1381
1382 int mode = info->mode;
1383
1384 /* Fallback unsupported restart index */
1385 unsigned primitive_index = (1 << (info->index_size * 8)) - 1;
1386
1387 if (info->primitive_restart && info->index_size
1388 && info->restart_index != primitive_index) {
1389 util_draw_vbo_without_prim_restart(pipe, info);
1390 return;
1391 }
1392
1393 /* Fallback for unsupported modes */
1394
1395 assert(ctx->rasterizer != NULL);
1396
1397 if (!(ctx->draw_modes & (1 << mode))) {
1398 if (mode == PIPE_PRIM_QUADS && info->count == 4 && !ctx->rasterizer->base.flatshade) {
1399 mode = PIPE_PRIM_TRIANGLE_FAN;
1400 } else {
1401 if (info->count < 4) {
1402 /* Degenerate case? */
1403 return;
1404 }
1405
1406 util_primconvert_save_rasterizer_state(ctx->primconvert, &ctx->rasterizer->base);
1407 util_primconvert_draw_vbo(ctx->primconvert, info);
1408 return;
1409 }
1410 }
1411
1412 ctx->payloads[PIPE_SHADER_VERTEX].offset_start = info->start;
1413 ctx->payloads[PIPE_SHADER_FRAGMENT].offset_start = info->start;
1414
1415 /* Now that we have a guaranteed terminating path, find the job.
1416 * Assignment commented out to prevent unused warning */
1417
1418 /* struct panfrost_batch *batch = */ panfrost_get_batch_for_fbo(ctx);
1419
1420 ctx->payloads[PIPE_SHADER_FRAGMENT].prefix.draw_mode = g2m_draw_mode(mode);
1421
1422 /* Take into account a negative bias */
1423 ctx->vertex_count = info->count + abs(info->index_bias);
1424 ctx->instance_count = info->instance_count;
1425 ctx->active_prim = info->mode;
1426
1427 /* For non-indexed draws, they're the same */
1428 unsigned vertex_count = ctx->vertex_count;
1429
1430 unsigned draw_flags = 0;
1431
1432 /* The draw flags interpret how primitive size is interpreted */
1433
1434 if (panfrost_writes_point_size(ctx))
1435 draw_flags |= MALI_DRAW_VARYING_SIZE;
1436
1437 if (info->primitive_restart)
1438 draw_flags |= MALI_DRAW_PRIMITIVE_RESTART_FIXED_INDEX;
1439
1440 /* These doesn't make much sense */
1441
1442 draw_flags |= 0x3000;
1443
1444 if (ctx->rasterizer && ctx->rasterizer->base.flatshade_first)
1445 draw_flags |= MALI_DRAW_FLATSHADE_FIRST;
1446
1447 panfrost_statistics_record(ctx, info);
1448
1449 if (info->index_size) {
1450 unsigned min_index = 0, max_index = 0;
1451 ctx->payloads[PIPE_SHADER_FRAGMENT].prefix.indices =
1452 panfrost_get_index_buffer_bounded(ctx, info, &min_index, &max_index);
1453
1454 /* Use the corresponding values */
1455 vertex_count = max_index - min_index + 1;
1456 ctx->payloads[PIPE_SHADER_VERTEX].offset_start = min_index + info->index_bias;
1457 ctx->payloads[PIPE_SHADER_FRAGMENT].offset_start = min_index + info->index_bias;
1458
1459 ctx->payloads[PIPE_SHADER_FRAGMENT].prefix.offset_bias_correction = -min_index;
1460 ctx->payloads[PIPE_SHADER_FRAGMENT].prefix.index_count = MALI_POSITIVE(info->count);
1461
1462 draw_flags |= panfrost_translate_index_size(info->index_size);
1463 } else {
1464 /* Index count == vertex count, if no indexing is applied, as
1465 * if it is internally indexed in the expected order */
1466
1467 ctx->payloads[PIPE_SHADER_FRAGMENT].prefix.offset_bias_correction = 0;
1468 ctx->payloads[PIPE_SHADER_FRAGMENT].prefix.index_count = MALI_POSITIVE(ctx->vertex_count);
1469
1470 /* Reverse index state */
1471 ctx->payloads[PIPE_SHADER_FRAGMENT].prefix.indices = (mali_ptr) 0;
1472 }
1473
1474 /* Dispatch "compute jobs" for the vertex/tiler pair as (1,
1475 * vertex_count, 1) */
1476
1477 panfrost_pack_work_groups_fused(
1478 &ctx->payloads[PIPE_SHADER_VERTEX].prefix,
1479 &ctx->payloads[PIPE_SHADER_FRAGMENT].prefix,
1480 1, vertex_count, info->instance_count,
1481 1, 1, 1);
1482
1483 ctx->payloads[PIPE_SHADER_FRAGMENT].prefix.unknown_draw = draw_flags;
1484
1485 /* Encode the padded vertex count */
1486
1487 if (info->instance_count > 1) {
1488 ctx->padded_count = panfrost_padded_vertex_count(vertex_count);
1489
1490 unsigned shift = __builtin_ctz(ctx->padded_count);
1491 unsigned k = ctx->padded_count >> (shift + 1);
1492
1493 ctx->payloads[PIPE_SHADER_VERTEX].instance_shift = shift;
1494 ctx->payloads[PIPE_SHADER_FRAGMENT].instance_shift = shift;
1495
1496 ctx->payloads[PIPE_SHADER_VERTEX].instance_odd = k;
1497 ctx->payloads[PIPE_SHADER_FRAGMENT].instance_odd = k;
1498 } else {
1499 ctx->padded_count = vertex_count;
1500
1501 /* Reset instancing state */
1502 ctx->payloads[PIPE_SHADER_VERTEX].instance_shift = 0;
1503 ctx->payloads[PIPE_SHADER_VERTEX].instance_odd = 0;
1504 ctx->payloads[PIPE_SHADER_FRAGMENT].instance_shift = 0;
1505 ctx->payloads[PIPE_SHADER_FRAGMENT].instance_odd = 0;
1506 }
1507
1508 /* Fire off the draw itself */
1509 panfrost_queue_draw(ctx);
1510
1511 /* Increment transform feedback offsets */
1512
1513 for (unsigned i = 0; i < ctx->streamout.num_targets; ++i) {
1514 unsigned output_count = u_stream_outputs_for_vertices(
1515 ctx->active_prim, ctx->vertex_count);
1516
1517 ctx->streamout.offsets[i] += output_count;
1518 }
1519 }
1520
1521 /* CSO state */
1522
1523 static void
1524 panfrost_generic_cso_delete(struct pipe_context *pctx, void *hwcso)
1525 {
1526 free(hwcso);
1527 }
1528
1529 static void *
1530 panfrost_create_rasterizer_state(
1531 struct pipe_context *pctx,
1532 const struct pipe_rasterizer_state *cso)
1533 {
1534 struct panfrost_rasterizer *so = CALLOC_STRUCT(panfrost_rasterizer);
1535
1536 so->base = *cso;
1537
1538 /* Bitmask, unknown meaning of the start value. 0x105 on 32-bit T6XX */
1539 so->tiler_gl_enables = 0x7;
1540
1541 if (cso->front_ccw)
1542 so->tiler_gl_enables |= MALI_FRONT_CCW_TOP;
1543
1544 if (cso->cull_face & PIPE_FACE_FRONT)
1545 so->tiler_gl_enables |= MALI_CULL_FACE_FRONT;
1546
1547 if (cso->cull_face & PIPE_FACE_BACK)
1548 so->tiler_gl_enables |= MALI_CULL_FACE_BACK;
1549
1550 return so;
1551 }
1552
1553 static void
1554 panfrost_bind_rasterizer_state(
1555 struct pipe_context *pctx,
1556 void *hwcso)
1557 {
1558 struct panfrost_context *ctx = pan_context(pctx);
1559
1560 ctx->rasterizer = hwcso;
1561
1562 if (!hwcso)
1563 return;
1564
1565 ctx->fragment_shader_core.depth_units = ctx->rasterizer->base.offset_units * 2.0f;
1566 ctx->fragment_shader_core.depth_factor = ctx->rasterizer->base.offset_scale;
1567
1568 /* Gauranteed with the core GL call, so don't expose ARB_polygon_offset */
1569 assert(ctx->rasterizer->base.offset_clamp == 0.0);
1570
1571 /* XXX: Which bit is which? Does this maybe allow offseting not-tri? */
1572
1573 SET_BIT(ctx->fragment_shader_core.unknown2_4, MALI_DEPTH_RANGE_A, ctx->rasterizer->base.offset_tri);
1574 SET_BIT(ctx->fragment_shader_core.unknown2_4, MALI_DEPTH_RANGE_B, ctx->rasterizer->base.offset_tri);
1575
1576 /* Point sprites are emulated */
1577
1578 struct panfrost_shader_state *variant =
1579 ctx->shader[PIPE_SHADER_FRAGMENT] ? &ctx->shader[PIPE_SHADER_FRAGMENT]->variants[ctx->shader[PIPE_SHADER_FRAGMENT]->active_variant] : NULL;
1580
1581 if (ctx->rasterizer->base.sprite_coord_enable || (variant && variant->point_sprite_mask))
1582 ctx->base.bind_fs_state(&ctx->base, ctx->shader[PIPE_SHADER_FRAGMENT]);
1583 }
1584
1585 static void *
1586 panfrost_create_vertex_elements_state(
1587 struct pipe_context *pctx,
1588 unsigned num_elements,
1589 const struct pipe_vertex_element *elements)
1590 {
1591 struct panfrost_vertex_state *so = CALLOC_STRUCT(panfrost_vertex_state);
1592
1593 so->num_elements = num_elements;
1594 memcpy(so->pipe, elements, sizeof(*elements) * num_elements);
1595
1596 for (int i = 0; i < num_elements; ++i) {
1597 so->hw[i].index = i;
1598
1599 enum pipe_format fmt = elements[i].src_format;
1600 const struct util_format_description *desc = util_format_description(fmt);
1601 so->hw[i].unknown1 = 0x2;
1602 so->hw[i].swizzle = panfrost_get_default_swizzle(desc->nr_channels);
1603
1604 so->hw[i].format = panfrost_find_format(desc);
1605
1606 /* The field itself should probably be shifted over */
1607 so->hw[i].src_offset = elements[i].src_offset;
1608 }
1609
1610 return so;
1611 }
1612
1613 static void
1614 panfrost_bind_vertex_elements_state(
1615 struct pipe_context *pctx,
1616 void *hwcso)
1617 {
1618 struct panfrost_context *ctx = pan_context(pctx);
1619 ctx->vertex = hwcso;
1620 }
1621
1622 static void *
1623 panfrost_create_shader_state(
1624 struct pipe_context *pctx,
1625 const struct pipe_shader_state *cso,
1626 enum pipe_shader_type stage)
1627 {
1628 struct panfrost_shader_variants *so = CALLOC_STRUCT(panfrost_shader_variants);
1629 so->base = *cso;
1630
1631 /* Token deep copy to prevent memory corruption */
1632
1633 if (cso->type == PIPE_SHADER_IR_TGSI)
1634 so->base.tokens = tgsi_dup_tokens(so->base.tokens);
1635
1636 /* Precompile for shader-db if we need to */
1637 if (unlikely((pan_debug & PAN_DBG_PRECOMPILE) && cso->type == PIPE_SHADER_IR_NIR)) {
1638 struct panfrost_context *ctx = pan_context(pctx);
1639
1640 struct mali_shader_meta meta;
1641 struct panfrost_shader_state state;
1642 uint64_t outputs_written;
1643
1644 panfrost_shader_compile(ctx, &meta,
1645 PIPE_SHADER_IR_NIR,
1646 so->base.ir.nir,
1647 tgsi_processor_to_shader_stage(stage), &state,
1648 &outputs_written);
1649 }
1650
1651 return so;
1652 }
1653
1654 static void
1655 panfrost_delete_shader_state(
1656 struct pipe_context *pctx,
1657 void *so)
1658 {
1659 struct panfrost_shader_variants *cso = (struct panfrost_shader_variants *) so;
1660
1661 if (cso->base.type == PIPE_SHADER_IR_TGSI) {
1662 DBG("Deleting TGSI shader leaks duplicated tokens\n");
1663 }
1664
1665 for (unsigned i = 0; i < cso->variant_count; ++i) {
1666 struct panfrost_shader_state *shader_state = &cso->variants[i];
1667 panfrost_bo_unreference(shader_state->bo);
1668 shader_state->bo = NULL;
1669 }
1670 free(cso->variants);
1671
1672 free(so);
1673 }
1674
1675 static void *
1676 panfrost_create_sampler_state(
1677 struct pipe_context *pctx,
1678 const struct pipe_sampler_state *cso)
1679 {
1680 struct panfrost_sampler_state *so = CALLOC_STRUCT(panfrost_sampler_state);
1681 so->base = *cso;
1682
1683 /* sampler_state corresponds to mali_sampler_descriptor, which we can generate entirely here */
1684
1685 bool min_nearest = cso->min_img_filter == PIPE_TEX_FILTER_NEAREST;
1686 bool mag_nearest = cso->mag_img_filter == PIPE_TEX_FILTER_NEAREST;
1687 bool mip_linear = cso->min_mip_filter == PIPE_TEX_MIPFILTER_LINEAR;
1688
1689 unsigned min_filter = min_nearest ? MALI_SAMP_MIN_NEAREST : 0;
1690 unsigned mag_filter = mag_nearest ? MALI_SAMP_MAG_NEAREST : 0;
1691 unsigned mip_filter = mip_linear ?
1692 (MALI_SAMP_MIP_LINEAR_1 | MALI_SAMP_MIP_LINEAR_2) : 0;
1693 unsigned normalized = cso->normalized_coords ? MALI_SAMP_NORM_COORDS : 0;
1694
1695 struct mali_sampler_descriptor sampler_descriptor = {
1696 .filter_mode = min_filter | mag_filter | mip_filter | normalized,
1697 .wrap_s = translate_tex_wrap(cso->wrap_s),
1698 .wrap_t = translate_tex_wrap(cso->wrap_t),
1699 .wrap_r = translate_tex_wrap(cso->wrap_r),
1700 .compare_func = panfrost_flip_compare_func(
1701 panfrost_translate_compare_func(
1702 cso->compare_func)),
1703 .border_color = {
1704 cso->border_color.f[0],
1705 cso->border_color.f[1],
1706 cso->border_color.f[2],
1707 cso->border_color.f[3]
1708 },
1709 .min_lod = FIXED_16(cso->min_lod, false), /* clamp at 0 */
1710 .max_lod = FIXED_16(cso->max_lod, false),
1711 .lod_bias = FIXED_16(cso->lod_bias, true), /* can be negative */
1712 .seamless_cube_map = cso->seamless_cube_map,
1713 };
1714
1715 /* If necessary, we disable mipmapping in the sampler descriptor by
1716 * clamping the LOD as tight as possible (from 0 to epsilon,
1717 * essentially -- remember these are fixed point numbers, so
1718 * epsilon=1/256) */
1719
1720 if (cso->min_mip_filter == PIPE_TEX_MIPFILTER_NONE) {
1721 sampler_descriptor.max_lod = sampler_descriptor.min_lod;
1722
1723 /* Enforce that there is something in the middle by adding epsilon*/
1724
1725 if (sampler_descriptor.min_lod == sampler_descriptor.max_lod)
1726 sampler_descriptor.max_lod++;
1727
1728 /* Sanity check */
1729 assert(sampler_descriptor.max_lod > sampler_descriptor.min_lod);
1730 }
1731
1732 so->hw = sampler_descriptor;
1733
1734 return so;
1735 }
1736
1737 static void
1738 panfrost_bind_sampler_states(
1739 struct pipe_context *pctx,
1740 enum pipe_shader_type shader,
1741 unsigned start_slot, unsigned num_sampler,
1742 void **sampler)
1743 {
1744 assert(start_slot == 0);
1745
1746 struct panfrost_context *ctx = pan_context(pctx);
1747
1748 /* XXX: Should upload, not just copy? */
1749 ctx->sampler_count[shader] = num_sampler;
1750 memcpy(ctx->samplers[shader], sampler, num_sampler * sizeof (void *));
1751 }
1752
1753 static bool
1754 panfrost_variant_matches(
1755 struct panfrost_context *ctx,
1756 struct panfrost_shader_state *variant,
1757 enum pipe_shader_type type)
1758 {
1759 struct pipe_rasterizer_state *rasterizer = &ctx->rasterizer->base;
1760 struct pipe_alpha_state *alpha = &ctx->depth_stencil->alpha;
1761
1762 bool is_fragment = (type == PIPE_SHADER_FRAGMENT);
1763
1764 if (is_fragment && (alpha->enabled || variant->alpha_state.enabled)) {
1765 /* Make sure enable state is at least the same */
1766 if (alpha->enabled != variant->alpha_state.enabled) {
1767 return false;
1768 }
1769
1770 /* Check that the contents of the test are the same */
1771 bool same_func = alpha->func == variant->alpha_state.func;
1772 bool same_ref = alpha->ref_value == variant->alpha_state.ref_value;
1773
1774 if (!(same_func && same_ref)) {
1775 return false;
1776 }
1777 }
1778
1779 if (is_fragment && rasterizer && (rasterizer->sprite_coord_enable |
1780 variant->point_sprite_mask)) {
1781 /* Ensure the same varyings are turned to point sprites */
1782 if (rasterizer->sprite_coord_enable != variant->point_sprite_mask)
1783 return false;
1784
1785 /* Ensure the orientation is correct */
1786 bool upper_left =
1787 rasterizer->sprite_coord_mode ==
1788 PIPE_SPRITE_COORD_UPPER_LEFT;
1789
1790 if (variant->point_sprite_upper_left != upper_left)
1791 return false;
1792 }
1793
1794 /* Otherwise, we're good to go */
1795 return true;
1796 }
1797
1798 /**
1799 * Fix an uncompiled shader's stream output info, and produce a bitmask
1800 * of which VARYING_SLOT_* are captured for stream output.
1801 *
1802 * Core Gallium stores output->register_index as a "slot" number, where
1803 * slots are assigned consecutively to all outputs in info->outputs_written.
1804 * This naive packing of outputs doesn't work for us - we too have slots,
1805 * but the layout is defined by the VUE map, which we won't have until we
1806 * compile a specific shader variant. So, we remap these and simply store
1807 * VARYING_SLOT_* in our copy's output->register_index fields.
1808 *
1809 * We then produce a bitmask of outputs which are used for SO.
1810 *
1811 * Implementation from iris.
1812 */
1813
1814 static uint64_t
1815 update_so_info(struct pipe_stream_output_info *so_info,
1816 uint64_t outputs_written)
1817 {
1818 uint64_t so_outputs = 0;
1819 uint8_t reverse_map[64] = {0};
1820 unsigned slot = 0;
1821
1822 while (outputs_written)
1823 reverse_map[slot++] = u_bit_scan64(&outputs_written);
1824
1825 for (unsigned i = 0; i < so_info->num_outputs; i++) {
1826 struct pipe_stream_output *output = &so_info->output[i];
1827
1828 /* Map Gallium's condensed "slots" back to real VARYING_SLOT_* enums */
1829 output->register_index = reverse_map[output->register_index];
1830
1831 so_outputs |= 1ull << output->register_index;
1832 }
1833
1834 return so_outputs;
1835 }
1836
1837 static void
1838 panfrost_bind_shader_state(
1839 struct pipe_context *pctx,
1840 void *hwcso,
1841 enum pipe_shader_type type)
1842 {
1843 struct panfrost_context *ctx = pan_context(pctx);
1844 ctx->shader[type] = hwcso;
1845
1846 if (!hwcso) return;
1847
1848 /* Match the appropriate variant */
1849
1850 signed variant = -1;
1851 struct panfrost_shader_variants *variants = (struct panfrost_shader_variants *) hwcso;
1852
1853 for (unsigned i = 0; i < variants->variant_count; ++i) {
1854 if (panfrost_variant_matches(ctx, &variants->variants[i], type)) {
1855 variant = i;
1856 break;
1857 }
1858 }
1859
1860 if (variant == -1) {
1861 /* No variant matched, so create a new one */
1862 variant = variants->variant_count++;
1863
1864 if (variants->variant_count > variants->variant_space) {
1865 unsigned old_space = variants->variant_space;
1866
1867 variants->variant_space *= 2;
1868 if (variants->variant_space == 0)
1869 variants->variant_space = 1;
1870
1871 /* Arbitrary limit to stop runaway programs from
1872 * creating an unbounded number of shader variants. */
1873 assert(variants->variant_space < 1024);
1874
1875 unsigned msize = sizeof(struct panfrost_shader_state);
1876 variants->variants = realloc(variants->variants,
1877 variants->variant_space * msize);
1878
1879 memset(&variants->variants[old_space], 0,
1880 (variants->variant_space - old_space) * msize);
1881 }
1882
1883 struct panfrost_shader_state *v =
1884 &variants->variants[variant];
1885
1886 if (type == PIPE_SHADER_FRAGMENT) {
1887 v->alpha_state = ctx->depth_stencil->alpha;
1888
1889 if (ctx->rasterizer) {
1890 v->point_sprite_mask = ctx->rasterizer->base.sprite_coord_enable;
1891 v->point_sprite_upper_left =
1892 ctx->rasterizer->base.sprite_coord_mode ==
1893 PIPE_SPRITE_COORD_UPPER_LEFT;
1894 }
1895 }
1896
1897 variants->variants[variant].tripipe = calloc(1, sizeof(struct mali_shader_meta));
1898
1899 }
1900
1901 /* Select this variant */
1902 variants->active_variant = variant;
1903
1904 struct panfrost_shader_state *shader_state = &variants->variants[variant];
1905 assert(panfrost_variant_matches(ctx, shader_state, type));
1906
1907 /* We finally have a variant, so compile it */
1908
1909 if (!shader_state->compiled) {
1910 uint64_t outputs_written = 0;
1911
1912 panfrost_shader_compile(ctx, shader_state->tripipe,
1913 variants->base.type,
1914 variants->base.type == PIPE_SHADER_IR_NIR ?
1915 variants->base.ir.nir :
1916 variants->base.tokens,
1917 tgsi_processor_to_shader_stage(type), shader_state,
1918 &outputs_written);
1919
1920 shader_state->compiled = true;
1921
1922 /* Fixup the stream out information, since what Gallium returns
1923 * normally is mildly insane */
1924
1925 shader_state->stream_output = variants->base.stream_output;
1926 shader_state->so_mask =
1927 update_so_info(&shader_state->stream_output, outputs_written);
1928 }
1929 }
1930
1931 static void *
1932 panfrost_create_vs_state(struct pipe_context *pctx, const struct pipe_shader_state *hwcso)
1933 {
1934 return panfrost_create_shader_state(pctx, hwcso, PIPE_SHADER_VERTEX);
1935 }
1936
1937 static void *
1938 panfrost_create_fs_state(struct pipe_context *pctx, const struct pipe_shader_state *hwcso)
1939 {
1940 return panfrost_create_shader_state(pctx, hwcso, PIPE_SHADER_FRAGMENT);
1941 }
1942
1943 static void
1944 panfrost_bind_vs_state(struct pipe_context *pctx, void *hwcso)
1945 {
1946 panfrost_bind_shader_state(pctx, hwcso, PIPE_SHADER_VERTEX);
1947 }
1948
1949 static void
1950 panfrost_bind_fs_state(struct pipe_context *pctx, void *hwcso)
1951 {
1952 panfrost_bind_shader_state(pctx, hwcso, PIPE_SHADER_FRAGMENT);
1953 }
1954
1955 static void
1956 panfrost_set_vertex_buffers(
1957 struct pipe_context *pctx,
1958 unsigned start_slot,
1959 unsigned num_buffers,
1960 const struct pipe_vertex_buffer *buffers)
1961 {
1962 struct panfrost_context *ctx = pan_context(pctx);
1963
1964 util_set_vertex_buffers_mask(ctx->vertex_buffers, &ctx->vb_mask, buffers, start_slot, num_buffers);
1965 }
1966
1967 static void
1968 panfrost_set_constant_buffer(
1969 struct pipe_context *pctx,
1970 enum pipe_shader_type shader, uint index,
1971 const struct pipe_constant_buffer *buf)
1972 {
1973 struct panfrost_context *ctx = pan_context(pctx);
1974 struct panfrost_constant_buffer *pbuf = &ctx->constant_buffer[shader];
1975
1976 util_copy_constant_buffer(&pbuf->cb[index], buf);
1977
1978 unsigned mask = (1 << index);
1979
1980 if (unlikely(!buf)) {
1981 pbuf->enabled_mask &= ~mask;
1982 pbuf->dirty_mask &= ~mask;
1983 return;
1984 }
1985
1986 pbuf->enabled_mask |= mask;
1987 pbuf->dirty_mask |= mask;
1988 }
1989
1990 static void
1991 panfrost_set_stencil_ref(
1992 struct pipe_context *pctx,
1993 const struct pipe_stencil_ref *ref)
1994 {
1995 struct panfrost_context *ctx = pan_context(pctx);
1996 ctx->stencil_ref = *ref;
1997 }
1998
1999 static enum mali_texture_type
2000 panfrost_translate_texture_type(enum pipe_texture_target t) {
2001 switch (t)
2002 {
2003 case PIPE_BUFFER:
2004 case PIPE_TEXTURE_1D:
2005 case PIPE_TEXTURE_1D_ARRAY:
2006 return MALI_TEX_1D;
2007
2008 case PIPE_TEXTURE_2D:
2009 case PIPE_TEXTURE_2D_ARRAY:
2010 case PIPE_TEXTURE_RECT:
2011 return MALI_TEX_2D;
2012
2013 case PIPE_TEXTURE_3D:
2014 return MALI_TEX_3D;
2015
2016 case PIPE_TEXTURE_CUBE:
2017 case PIPE_TEXTURE_CUBE_ARRAY:
2018 return MALI_TEX_CUBE;
2019
2020 default:
2021 unreachable("Unknown target");
2022 }
2023 }
2024
2025 static struct pipe_sampler_view *
2026 panfrost_create_sampler_view(
2027 struct pipe_context *pctx,
2028 struct pipe_resource *texture,
2029 const struct pipe_sampler_view *template)
2030 {
2031 struct panfrost_screen *screen = pan_screen(pctx->screen);
2032 struct panfrost_sampler_view *so = rzalloc(pctx, struct panfrost_sampler_view);
2033
2034 pipe_reference(NULL, &texture->reference);
2035
2036 struct panfrost_resource *prsrc = (struct panfrost_resource *) texture;
2037 assert(prsrc->bo);
2038
2039 so->base = *template;
2040 so->base.texture = texture;
2041 so->base.reference.count = 1;
2042 so->base.context = pctx;
2043
2044 unsigned char user_swizzle[4] = {
2045 template->swizzle_r,
2046 template->swizzle_g,
2047 template->swizzle_b,
2048 template->swizzle_a
2049 };
2050
2051 /* In the hardware, array_size refers specifically to array textures,
2052 * whereas in Gallium, it also covers cubemaps */
2053
2054 unsigned array_size = texture->array_size;
2055
2056 if (template->target == PIPE_TEXTURE_CUBE) {
2057 /* TODO: Cubemap arrays */
2058 assert(array_size == 6);
2059 array_size /= 6;
2060 }
2061
2062 enum mali_texture_type type =
2063 panfrost_translate_texture_type(template->target);
2064
2065 unsigned size = panfrost_estimate_texture_size(
2066 template->u.tex.first_level,
2067 template->u.tex.last_level,
2068 template->u.tex.first_layer,
2069 template->u.tex.last_layer,
2070 type, prsrc->layout);
2071
2072 so->bo = panfrost_bo_create(screen, size, 0);
2073
2074 panfrost_new_texture(
2075 so->bo->cpu,
2076 texture->width0, texture->height0,
2077 texture->depth0, array_size,
2078 texture->format,
2079 type, prsrc->layout,
2080 template->u.tex.first_level,
2081 template->u.tex.last_level,
2082 template->u.tex.first_layer,
2083 template->u.tex.last_layer,
2084 prsrc->cubemap_stride,
2085 panfrost_translate_swizzle_4(user_swizzle),
2086 prsrc->bo->gpu,
2087 prsrc->slices);
2088
2089 return (struct pipe_sampler_view *) so;
2090 }
2091
2092 static void
2093 panfrost_set_sampler_views(
2094 struct pipe_context *pctx,
2095 enum pipe_shader_type shader,
2096 unsigned start_slot, unsigned num_views,
2097 struct pipe_sampler_view **views)
2098 {
2099 struct panfrost_context *ctx = pan_context(pctx);
2100 unsigned new_nr = 0;
2101 unsigned i;
2102
2103 assert(start_slot == 0);
2104
2105 for (i = 0; i < num_views; ++i) {
2106 if (views[i])
2107 new_nr = i + 1;
2108 pipe_sampler_view_reference((struct pipe_sampler_view **)&ctx->sampler_views[shader][i],
2109 views[i]);
2110 }
2111
2112 for (; i < ctx->sampler_view_count[shader]; i++) {
2113 pipe_sampler_view_reference((struct pipe_sampler_view **)&ctx->sampler_views[shader][i],
2114 NULL);
2115 }
2116 ctx->sampler_view_count[shader] = new_nr;
2117 }
2118
2119 static void
2120 panfrost_sampler_view_destroy(
2121 struct pipe_context *pctx,
2122 struct pipe_sampler_view *pview)
2123 {
2124 struct panfrost_sampler_view *view = (struct panfrost_sampler_view *) pview;
2125
2126 pipe_resource_reference(&pview->texture, NULL);
2127 panfrost_bo_unreference(view->bo);
2128 ralloc_free(view);
2129 }
2130
2131 static void
2132 panfrost_set_shader_buffers(
2133 struct pipe_context *pctx,
2134 enum pipe_shader_type shader,
2135 unsigned start, unsigned count,
2136 const struct pipe_shader_buffer *buffers,
2137 unsigned writable_bitmask)
2138 {
2139 struct panfrost_context *ctx = pan_context(pctx);
2140
2141 util_set_shader_buffers_mask(ctx->ssbo[shader], &ctx->ssbo_mask[shader],
2142 buffers, start, count);
2143 }
2144
2145 /* Hints that a framebuffer should use AFBC where possible */
2146
2147 static void
2148 panfrost_hint_afbc(
2149 struct panfrost_screen *screen,
2150 const struct pipe_framebuffer_state *fb)
2151 {
2152 /* AFBC implemenation incomplete; hide it */
2153 if (!(pan_debug & PAN_DBG_AFBC)) return;
2154
2155 /* Hint AFBC to the resources bound to each color buffer */
2156
2157 for (unsigned i = 0; i < fb->nr_cbufs; ++i) {
2158 struct pipe_surface *surf = fb->cbufs[i];
2159 struct panfrost_resource *rsrc = pan_resource(surf->texture);
2160 panfrost_resource_hint_layout(screen, rsrc, MALI_TEXTURE_AFBC, 1);
2161 }
2162
2163 /* Also hint it to the depth buffer */
2164
2165 if (fb->zsbuf) {
2166 struct panfrost_resource *rsrc = pan_resource(fb->zsbuf->texture);
2167 panfrost_resource_hint_layout(screen, rsrc, MALI_TEXTURE_AFBC, 1);
2168 }
2169 }
2170
2171 static void
2172 panfrost_set_framebuffer_state(struct pipe_context *pctx,
2173 const struct pipe_framebuffer_state *fb)
2174 {
2175 struct panfrost_context *ctx = pan_context(pctx);
2176
2177 panfrost_hint_afbc(pan_screen(pctx->screen), fb);
2178 util_copy_framebuffer_state(&ctx->pipe_framebuffer, fb);
2179 ctx->batch = NULL;
2180 panfrost_invalidate_frame(ctx);
2181 }
2182
2183 static void *
2184 panfrost_create_depth_stencil_state(struct pipe_context *pipe,
2185 const struct pipe_depth_stencil_alpha_state *depth_stencil)
2186 {
2187 return mem_dup(depth_stencil, sizeof(*depth_stencil));
2188 }
2189
2190 static void
2191 panfrost_bind_depth_stencil_state(struct pipe_context *pipe,
2192 void *cso)
2193 {
2194 struct panfrost_context *ctx = pan_context(pipe);
2195 struct pipe_depth_stencil_alpha_state *depth_stencil = cso;
2196 ctx->depth_stencil = depth_stencil;
2197
2198 if (!depth_stencil)
2199 return;
2200
2201 /* Alpha does not exist in the hardware (it's not in ES3), so it's
2202 * emulated in the fragment shader */
2203
2204 if (depth_stencil->alpha.enabled) {
2205 /* We need to trigger a new shader (maybe) */
2206 ctx->base.bind_fs_state(&ctx->base, ctx->shader[PIPE_SHADER_FRAGMENT]);
2207 }
2208
2209 /* Stencil state */
2210 SET_BIT(ctx->fragment_shader_core.unknown2_4, MALI_STENCIL_TEST, depth_stencil->stencil[0].enabled);
2211
2212 panfrost_make_stencil_state(&depth_stencil->stencil[0], &ctx->fragment_shader_core.stencil_front);
2213 ctx->fragment_shader_core.stencil_mask_front = depth_stencil->stencil[0].writemask;
2214
2215 /* If back-stencil is not enabled, use the front values */
2216 bool back_enab = ctx->depth_stencil->stencil[1].enabled;
2217 unsigned back_index = back_enab ? 1 : 0;
2218
2219 panfrost_make_stencil_state(&depth_stencil->stencil[back_index], &ctx->fragment_shader_core.stencil_back);
2220 ctx->fragment_shader_core.stencil_mask_back = depth_stencil->stencil[back_index].writemask;
2221
2222 /* Depth state (TODO: Refactor) */
2223 SET_BIT(ctx->fragment_shader_core.unknown2_3, MALI_DEPTH_WRITEMASK,
2224 depth_stencil->depth.writemask);
2225
2226 int func = depth_stencil->depth.enabled ? depth_stencil->depth.func : PIPE_FUNC_ALWAYS;
2227
2228 ctx->fragment_shader_core.unknown2_3 &= ~MALI_DEPTH_FUNC_MASK;
2229 ctx->fragment_shader_core.unknown2_3 |= MALI_DEPTH_FUNC(panfrost_translate_compare_func(func));
2230
2231 /* Bounds test not implemented */
2232 assert(!depth_stencil->depth.bounds_test);
2233 }
2234
2235 static void
2236 panfrost_delete_depth_stencil_state(struct pipe_context *pipe, void *depth)
2237 {
2238 free( depth );
2239 }
2240
2241 static void
2242 panfrost_set_sample_mask(struct pipe_context *pipe,
2243 unsigned sample_mask)
2244 {
2245 }
2246
2247 static void
2248 panfrost_set_clip_state(struct pipe_context *pipe,
2249 const struct pipe_clip_state *clip)
2250 {
2251 //struct panfrost_context *panfrost = pan_context(pipe);
2252 }
2253
2254 static void
2255 panfrost_set_viewport_states(struct pipe_context *pipe,
2256 unsigned start_slot,
2257 unsigned num_viewports,
2258 const struct pipe_viewport_state *viewports)
2259 {
2260 struct panfrost_context *ctx = pan_context(pipe);
2261
2262 assert(start_slot == 0);
2263 assert(num_viewports == 1);
2264
2265 ctx->pipe_viewport = *viewports;
2266 }
2267
2268 static void
2269 panfrost_set_scissor_states(struct pipe_context *pipe,
2270 unsigned start_slot,
2271 unsigned num_scissors,
2272 const struct pipe_scissor_state *scissors)
2273 {
2274 struct panfrost_context *ctx = pan_context(pipe);
2275
2276 assert(start_slot == 0);
2277 assert(num_scissors == 1);
2278
2279 ctx->scissor = *scissors;
2280 }
2281
2282 static void
2283 panfrost_set_polygon_stipple(struct pipe_context *pipe,
2284 const struct pipe_poly_stipple *stipple)
2285 {
2286 //struct panfrost_context *panfrost = pan_context(pipe);
2287 }
2288
2289 static void
2290 panfrost_set_active_query_state(struct pipe_context *pipe,
2291 bool enable)
2292 {
2293 struct panfrost_context *ctx = pan_context(pipe);
2294 ctx->active_queries = enable;
2295 }
2296
2297 static void
2298 panfrost_destroy(struct pipe_context *pipe)
2299 {
2300 struct panfrost_context *panfrost = pan_context(pipe);
2301
2302 if (panfrost->blitter)
2303 util_blitter_destroy(panfrost->blitter);
2304
2305 if (panfrost->blitter_wallpaper)
2306 util_blitter_destroy(panfrost->blitter_wallpaper);
2307
2308 util_unreference_framebuffer_state(&panfrost->pipe_framebuffer);
2309 u_upload_destroy(pipe->stream_uploader);
2310
2311 ralloc_free(pipe);
2312 }
2313
2314 static struct pipe_query *
2315 panfrost_create_query(struct pipe_context *pipe,
2316 unsigned type,
2317 unsigned index)
2318 {
2319 struct panfrost_query *q = rzalloc(pipe, struct panfrost_query);
2320
2321 q->type = type;
2322 q->index = index;
2323
2324 return (struct pipe_query *) q;
2325 }
2326
2327 static void
2328 panfrost_destroy_query(struct pipe_context *pipe, struct pipe_query *q)
2329 {
2330 struct panfrost_query *query = (struct panfrost_query *) q;
2331
2332 if (query->bo) {
2333 panfrost_bo_unreference(query->bo);
2334 query->bo = NULL;
2335 }
2336
2337 ralloc_free(q);
2338 }
2339
2340 static bool
2341 panfrost_begin_query(struct pipe_context *pipe, struct pipe_query *q)
2342 {
2343 struct panfrost_context *ctx = pan_context(pipe);
2344 struct panfrost_query *query = (struct panfrost_query *) q;
2345
2346 switch (query->type) {
2347 case PIPE_QUERY_OCCLUSION_COUNTER:
2348 case PIPE_QUERY_OCCLUSION_PREDICATE:
2349 case PIPE_QUERY_OCCLUSION_PREDICATE_CONSERVATIVE:
2350 /* Allocate a bo for the query results to be stored */
2351 if (!query->bo) {
2352 query->bo = panfrost_bo_create(
2353 pan_screen(ctx->base.screen),
2354 sizeof(unsigned), 0);
2355 }
2356
2357 unsigned *result = (unsigned *)query->bo->cpu;
2358 *result = 0; /* Default to 0 if nothing at all drawn. */
2359 ctx->occlusion_query = query;
2360 break;
2361
2362 /* Geometry statistics are computed in the driver. XXX: geom/tess
2363 * shaders.. */
2364
2365 case PIPE_QUERY_PRIMITIVES_GENERATED:
2366 query->start = ctx->prims_generated;
2367 break;
2368 case PIPE_QUERY_PRIMITIVES_EMITTED:
2369 query->start = ctx->tf_prims_generated;
2370 break;
2371
2372 default:
2373 DBG("Skipping query %u\n", query->type);
2374 break;
2375 }
2376
2377 return true;
2378 }
2379
2380 static bool
2381 panfrost_end_query(struct pipe_context *pipe, struct pipe_query *q)
2382 {
2383 struct panfrost_context *ctx = pan_context(pipe);
2384 struct panfrost_query *query = (struct panfrost_query *) q;
2385
2386 switch (query->type) {
2387 case PIPE_QUERY_OCCLUSION_COUNTER:
2388 case PIPE_QUERY_OCCLUSION_PREDICATE:
2389 case PIPE_QUERY_OCCLUSION_PREDICATE_CONSERVATIVE:
2390 ctx->occlusion_query = NULL;
2391 break;
2392 case PIPE_QUERY_PRIMITIVES_GENERATED:
2393 query->end = ctx->prims_generated;
2394 break;
2395 case PIPE_QUERY_PRIMITIVES_EMITTED:
2396 query->end = ctx->tf_prims_generated;
2397 break;
2398 }
2399
2400 return true;
2401 }
2402
2403 static bool
2404 panfrost_get_query_result(struct pipe_context *pipe,
2405 struct pipe_query *q,
2406 bool wait,
2407 union pipe_query_result *vresult)
2408 {
2409 struct panfrost_query *query = (struct panfrost_query *) q;
2410 struct panfrost_context *ctx = pan_context(pipe);
2411
2412
2413 switch (query->type) {
2414 case PIPE_QUERY_OCCLUSION_COUNTER:
2415 case PIPE_QUERY_OCCLUSION_PREDICATE:
2416 case PIPE_QUERY_OCCLUSION_PREDICATE_CONSERVATIVE:
2417 /* Flush first */
2418 panfrost_flush_all_batches(ctx, true);
2419
2420 /* Read back the query results */
2421 unsigned *result = (unsigned *) query->bo->cpu;
2422 unsigned passed = *result;
2423
2424 if (query->type == PIPE_QUERY_OCCLUSION_COUNTER) {
2425 vresult->u64 = passed;
2426 } else {
2427 vresult->b = !!passed;
2428 }
2429
2430 break;
2431
2432 case PIPE_QUERY_PRIMITIVES_GENERATED:
2433 case PIPE_QUERY_PRIMITIVES_EMITTED:
2434 panfrost_flush_all_batches(ctx, true);
2435 vresult->u64 = query->end - query->start;
2436 break;
2437
2438 default:
2439 DBG("Skipped query get %u\n", query->type);
2440 break;
2441 }
2442
2443 return true;
2444 }
2445
2446 static struct pipe_stream_output_target *
2447 panfrost_create_stream_output_target(struct pipe_context *pctx,
2448 struct pipe_resource *prsc,
2449 unsigned buffer_offset,
2450 unsigned buffer_size)
2451 {
2452 struct pipe_stream_output_target *target;
2453
2454 target = rzalloc(pctx, struct pipe_stream_output_target);
2455
2456 if (!target)
2457 return NULL;
2458
2459 pipe_reference_init(&target->reference, 1);
2460 pipe_resource_reference(&target->buffer, prsc);
2461
2462 target->context = pctx;
2463 target->buffer_offset = buffer_offset;
2464 target->buffer_size = buffer_size;
2465
2466 return target;
2467 }
2468
2469 static void
2470 panfrost_stream_output_target_destroy(struct pipe_context *pctx,
2471 struct pipe_stream_output_target *target)
2472 {
2473 pipe_resource_reference(&target->buffer, NULL);
2474 ralloc_free(target);
2475 }
2476
2477 static void
2478 panfrost_set_stream_output_targets(struct pipe_context *pctx,
2479 unsigned num_targets,
2480 struct pipe_stream_output_target **targets,
2481 const unsigned *offsets)
2482 {
2483 struct panfrost_context *ctx = pan_context(pctx);
2484 struct panfrost_streamout *so = &ctx->streamout;
2485
2486 assert(num_targets <= ARRAY_SIZE(so->targets));
2487
2488 for (unsigned i = 0; i < num_targets; i++) {
2489 if (offsets[i] != -1)
2490 so->offsets[i] = offsets[i];
2491
2492 pipe_so_target_reference(&so->targets[i], targets[i]);
2493 }
2494
2495 for (unsigned i = 0; i < so->num_targets; i++)
2496 pipe_so_target_reference(&so->targets[i], NULL);
2497
2498 so->num_targets = num_targets;
2499 }
2500
2501 struct pipe_context *
2502 panfrost_create_context(struct pipe_screen *screen, void *priv, unsigned flags)
2503 {
2504 struct panfrost_context *ctx = rzalloc(screen, struct panfrost_context);
2505 struct pipe_context *gallium = (struct pipe_context *) ctx;
2506
2507 gallium->screen = screen;
2508
2509 gallium->destroy = panfrost_destroy;
2510
2511 gallium->set_framebuffer_state = panfrost_set_framebuffer_state;
2512
2513 gallium->flush = panfrost_flush;
2514 gallium->clear = panfrost_clear;
2515 gallium->draw_vbo = panfrost_draw_vbo;
2516
2517 gallium->set_vertex_buffers = panfrost_set_vertex_buffers;
2518 gallium->set_constant_buffer = panfrost_set_constant_buffer;
2519 gallium->set_shader_buffers = panfrost_set_shader_buffers;
2520
2521 gallium->set_stencil_ref = panfrost_set_stencil_ref;
2522
2523 gallium->create_sampler_view = panfrost_create_sampler_view;
2524 gallium->set_sampler_views = panfrost_set_sampler_views;
2525 gallium->sampler_view_destroy = panfrost_sampler_view_destroy;
2526
2527 gallium->create_rasterizer_state = panfrost_create_rasterizer_state;
2528 gallium->bind_rasterizer_state = panfrost_bind_rasterizer_state;
2529 gallium->delete_rasterizer_state = panfrost_generic_cso_delete;
2530
2531 gallium->create_vertex_elements_state = panfrost_create_vertex_elements_state;
2532 gallium->bind_vertex_elements_state = panfrost_bind_vertex_elements_state;
2533 gallium->delete_vertex_elements_state = panfrost_generic_cso_delete;
2534
2535 gallium->create_fs_state = panfrost_create_fs_state;
2536 gallium->delete_fs_state = panfrost_delete_shader_state;
2537 gallium->bind_fs_state = panfrost_bind_fs_state;
2538
2539 gallium->create_vs_state = panfrost_create_vs_state;
2540 gallium->delete_vs_state = panfrost_delete_shader_state;
2541 gallium->bind_vs_state = panfrost_bind_vs_state;
2542
2543 gallium->create_sampler_state = panfrost_create_sampler_state;
2544 gallium->delete_sampler_state = panfrost_generic_cso_delete;
2545 gallium->bind_sampler_states = panfrost_bind_sampler_states;
2546
2547 gallium->create_depth_stencil_alpha_state = panfrost_create_depth_stencil_state;
2548 gallium->bind_depth_stencil_alpha_state = panfrost_bind_depth_stencil_state;
2549 gallium->delete_depth_stencil_alpha_state = panfrost_delete_depth_stencil_state;
2550
2551 gallium->set_sample_mask = panfrost_set_sample_mask;
2552
2553 gallium->set_clip_state = panfrost_set_clip_state;
2554 gallium->set_viewport_states = panfrost_set_viewport_states;
2555 gallium->set_scissor_states = panfrost_set_scissor_states;
2556 gallium->set_polygon_stipple = panfrost_set_polygon_stipple;
2557 gallium->set_active_query_state = panfrost_set_active_query_state;
2558
2559 gallium->create_query = panfrost_create_query;
2560 gallium->destroy_query = panfrost_destroy_query;
2561 gallium->begin_query = panfrost_begin_query;
2562 gallium->end_query = panfrost_end_query;
2563 gallium->get_query_result = panfrost_get_query_result;
2564
2565 gallium->create_stream_output_target = panfrost_create_stream_output_target;
2566 gallium->stream_output_target_destroy = panfrost_stream_output_target_destroy;
2567 gallium->set_stream_output_targets = panfrost_set_stream_output_targets;
2568
2569 panfrost_resource_context_init(gallium);
2570 panfrost_blend_context_init(gallium);
2571 panfrost_compute_context_init(gallium);
2572
2573 /* XXX: leaks */
2574 gallium->stream_uploader = u_upload_create_default(gallium);
2575 gallium->const_uploader = gallium->stream_uploader;
2576 assert(gallium->stream_uploader);
2577
2578 /* Midgard supports ES modes, plus QUADS/QUAD_STRIPS/POLYGON */
2579 ctx->draw_modes = (1 << (PIPE_PRIM_POLYGON + 1)) - 1;
2580
2581 ctx->primconvert = util_primconvert_create(gallium, ctx->draw_modes);
2582
2583 ctx->blitter = util_blitter_create(gallium);
2584 ctx->blitter_wallpaper = util_blitter_create(gallium);
2585
2586 assert(ctx->blitter);
2587 assert(ctx->blitter_wallpaper);
2588
2589 /* Prepare for render! */
2590
2591 panfrost_batch_init(ctx);
2592 panfrost_emit_vertex_payload(ctx);
2593 panfrost_invalidate_frame(ctx);
2594 panfrost_default_shader_backend(ctx);
2595
2596 return gallium;
2597 }