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