panfrost: Emit attribute descriptors after patching the templates
[mesa.git] / src / gallium / drivers / panfrost / pan_context.c
1 /*
2 * © Copyright 2018 Alyssa Rosenzweig
3 * Copyright © 2014-2017 Broadcom
4 * Copyright (C) 2017 Intel Corporation
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice (including the next
14 * paragraph) shall be included in all copies or substantial portions of the
15 * Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE.
24 *
25 */
26
27 #include <sys/poll.h>
28 #include <errno.h>
29
30 #include "pan_bo.h"
31 #include "pan_context.h"
32 #include "pan_minmax_cache.h"
33 #include "panfrost-quirks.h"
34
35 #include "util/macros.h"
36 #include "util/format/u_format.h"
37 #include "util/u_inlines.h"
38 #include "util/u_upload_mgr.h"
39 #include "util/u_memory.h"
40 #include "util/u_vbuf.h"
41 #include "util/half_float.h"
42 #include "util/u_helpers.h"
43 #include "util/format/u_format.h"
44 #include "util/u_prim.h"
45 #include "util/u_prim_restart.h"
46 #include "indices/u_primconvert.h"
47 #include "tgsi/tgsi_parse.h"
48 #include "tgsi/tgsi_from_mesa.h"
49 #include "util/u_math.h"
50
51 #include "pan_screen.h"
52 #include "pan_blending.h"
53 #include "pan_blend_shaders.h"
54 #include "pan_cmdstream.h"
55 #include "pan_util.h"
56 #include "pandecode/decode.h"
57
58 struct midgard_tiler_descriptor
59 panfrost_emit_midg_tiler(struct panfrost_batch *batch, unsigned vertex_count)
60 {
61 struct panfrost_screen *screen = pan_screen(batch->ctx->base.screen);
62 bool hierarchy = !(screen->quirks & MIDGARD_NO_HIER_TILING);
63 struct midgard_tiler_descriptor t = {0};
64 unsigned height = batch->key.height;
65 unsigned width = batch->key.width;
66
67 t.hierarchy_mask =
68 panfrost_choose_hierarchy_mask(width, height, vertex_count, hierarchy);
69
70 /* Compute the polygon header size and use that to offset the body */
71
72 unsigned header_size = panfrost_tiler_header_size(
73 width, height, t.hierarchy_mask, hierarchy);
74
75 t.polygon_list_size = panfrost_tiler_full_size(
76 width, height, t.hierarchy_mask, hierarchy);
77
78 /* Sanity check */
79
80 if (vertex_count) {
81 struct panfrost_bo *tiler_heap;
82
83 tiler_heap = panfrost_batch_get_tiler_heap(batch);
84 t.polygon_list = panfrost_batch_get_polygon_list(batch,
85 header_size +
86 t.polygon_list_size);
87
88
89 /* Allow the entire tiler heap */
90 t.heap_start = tiler_heap->gpu;
91 t.heap_end = tiler_heap->gpu + tiler_heap->size;
92 } else {
93 struct panfrost_bo *tiler_dummy;
94
95 tiler_dummy = panfrost_batch_get_tiler_dummy(batch);
96 header_size = MALI_TILER_MINIMUM_HEADER_SIZE;
97
98 /* The tiler is disabled, so don't allow the tiler heap */
99 t.heap_start = tiler_dummy->gpu;
100 t.heap_end = t.heap_start;
101
102 /* Use a dummy polygon list */
103 t.polygon_list = tiler_dummy->gpu;
104
105 /* Disable the tiler */
106 if (hierarchy)
107 t.hierarchy_mask |= MALI_TILER_DISABLED;
108 else {
109 t.hierarchy_mask = MALI_TILER_USER;
110 t.polygon_list_size = MALI_TILER_MINIMUM_HEADER_SIZE + 4;
111
112 /* We don't have a WRITE_VALUE job, so write the polygon list manually */
113 uint32_t *polygon_list_body = (uint32_t *) (tiler_dummy->cpu + header_size);
114 polygon_list_body[0] = 0xa0000000; /* TODO: Just that? */
115 }
116 }
117
118 t.polygon_list_body =
119 t.polygon_list + header_size;
120
121 return t;
122 }
123
124 static void
125 panfrost_clear(
126 struct pipe_context *pipe,
127 unsigned buffers,
128 const union pipe_color_union *color,
129 double depth, unsigned stencil)
130 {
131 struct panfrost_context *ctx = pan_context(pipe);
132
133 /* TODO: panfrost_get_fresh_batch_for_fbo() instantiates a new batch if
134 * the existing batch targeting this FBO has draws. We could probably
135 * avoid that by replacing plain clears by quad-draws with a specific
136 * color/depth/stencil value, thus avoiding the generation of extra
137 * fragment jobs.
138 */
139 struct panfrost_batch *batch = panfrost_get_fresh_batch_for_fbo(ctx);
140
141 panfrost_batch_add_fbo_bos(batch);
142 panfrost_batch_clear(batch, buffers, color, depth, stencil);
143 }
144
145 /* Reset per-frame context, called on context initialisation as well as after
146 * flushing a frame */
147
148 void
149 panfrost_invalidate_frame(struct panfrost_context *ctx)
150 {
151 for (unsigned i = 0; i < PIPE_SHADER_TYPES; ++i)
152 ctx->payloads[i].postfix.shared_memory = 0;
153
154 /* TODO: When does this need to be handled? */
155 ctx->active_queries = true;
156 }
157
158 /* In practice, every field of these payloads should be configurable
159 * arbitrarily, which means these functions are basically catch-all's for
160 * as-of-yet unwavering unknowns */
161
162 static void
163 panfrost_emit_vertex_payload(struct panfrost_context *ctx)
164 {
165 /* 0x2 bit clear on 32-bit T6XX */
166
167 struct midgard_payload_vertex_tiler payload = {
168 .gl_enables = 0x4 | 0x2,
169 };
170
171 /* Vertex and compute are closely coupled, so share a payload */
172
173 memcpy(&ctx->payloads[PIPE_SHADER_VERTEX], &payload, sizeof(payload));
174 memcpy(&ctx->payloads[PIPE_SHADER_COMPUTE], &payload, sizeof(payload));
175 }
176
177 bool
178 panfrost_writes_point_size(struct panfrost_context *ctx)
179 {
180 assert(ctx->shader[PIPE_SHADER_VERTEX]);
181 struct panfrost_shader_state *vs = panfrost_get_shader_state(ctx, PIPE_SHADER_VERTEX);
182
183 return vs->writes_point_size && ctx->payloads[PIPE_SHADER_FRAGMENT].prefix.draw_mode == MALI_POINTS;
184 }
185
186 /* Stage the attribute descriptors so we can adjust src_offset
187 * to let BOs align nicely */
188
189 static void
190 panfrost_stage_attributes(struct panfrost_context *ctx)
191 {
192 struct panfrost_batch *batch = panfrost_get_batch_for_fbo(ctx);
193 struct panfrost_vertex_state *so = ctx->vertex;
194
195 /* Fixup offsets for the second pass. Recall that the hardware
196 * calculates attribute addresses as:
197 *
198 * addr = base + (stride * vtx) + src_offset;
199 *
200 * However, on Mali, base must be aligned to 64-bytes, so we
201 * instead let:
202 *
203 * base' = base & ~63 = base - (base & 63)
204 *
205 * To compensate when using base' (see emit_vertex_data), we have
206 * to adjust src_offset by the masked off piece:
207 *
208 * addr' = base' + (stride * vtx) + (src_offset + (base & 63))
209 * = base - (base & 63) + (stride * vtx) + src_offset + (base & 63)
210 * = base + (stride * vtx) + src_offset
211 * = addr;
212 *
213 * QED.
214 */
215
216 unsigned start = ctx->payloads[PIPE_SHADER_VERTEX].offset_start;
217
218 for (unsigned i = 0; i < so->num_elements; ++i) {
219 unsigned vbi = so->pipe[i].vertex_buffer_index;
220 struct pipe_vertex_buffer *buf = &ctx->vertex_buffers[vbi];
221
222 /* Adjust by the masked off bits of the offset. Make sure we
223 * read src_offset from so->hw (which is not GPU visible)
224 * rather than target (which is) due to caching effects */
225
226 unsigned src_offset = so->pipe[i].src_offset;
227
228 /* BOs aligned to 4k so guaranteed aligned to 64 */
229 src_offset += (buf->buffer_offset & 63);
230
231 /* Also, somewhat obscurely per-instance data needs to be
232 * offset in response to a delayed start in an indexed draw */
233
234 if (so->pipe[i].instance_divisor && ctx->instance_count > 1 && start)
235 src_offset -= buf->stride * start;
236
237 so->hw[i].src_offset = src_offset;
238 }
239
240 mali_ptr out = panfrost_upload_transient(batch, so->hw,
241 sizeof(*so->hw) *
242 PAN_MAX_ATTRIBUTE);
243
244 ctx->payloads[PIPE_SHADER_VERTEX].postfix.attribute_meta = out;
245 }
246
247 /* Compute number of UBOs active (more specifically, compute the highest UBO
248 * number addressable -- if there are gaps, include them in the count anyway).
249 * We always include UBO #0 in the count, since we *need* uniforms enabled for
250 * sysvals. */
251
252 unsigned
253 panfrost_ubo_count(struct panfrost_context *ctx, enum pipe_shader_type stage)
254 {
255 unsigned mask = ctx->constant_buffer[stage].enabled_mask | 1;
256 return 32 - __builtin_clz(mask);
257 }
258
259 /* Go through dirty flags and actualise them in the cmdstream. */
260
261 static void
262 panfrost_emit_for_draw(struct panfrost_context *ctx)
263 {
264 struct panfrost_batch *batch = panfrost_get_batch_for_fbo(ctx);
265
266 panfrost_batch_add_fbo_bos(batch);
267
268 for (int i = 0; i <= PIPE_SHADER_FRAGMENT; ++i)
269 panfrost_vt_attach_framebuffer(ctx, &ctx->payloads[i]);
270
271 panfrost_emit_vertex_data(batch);
272
273 /* Varyings emitted for -all- geometry */
274 unsigned total_count = ctx->padded_count * ctx->instance_count;
275 panfrost_emit_varying_descriptor(ctx, total_count);
276
277 panfrost_batch_set_requirements(batch);
278
279 panfrost_vt_update_rasterizer(ctx, &ctx->payloads[PIPE_SHADER_FRAGMENT]);
280 panfrost_vt_update_occlusion_query(ctx, &ctx->payloads[PIPE_SHADER_FRAGMENT]);
281
282 panfrost_emit_shader_meta(batch, PIPE_SHADER_VERTEX,
283 &ctx->payloads[PIPE_SHADER_VERTEX]);
284 panfrost_emit_shader_meta(batch, PIPE_SHADER_FRAGMENT,
285 &ctx->payloads[PIPE_SHADER_FRAGMENT]);
286
287 /* We stage to transient, so always dirty.. */
288 if (ctx->vertex)
289 panfrost_stage_attributes(ctx);
290
291 for (int i = 0; i <= PIPE_SHADER_FRAGMENT; ++i) {
292 panfrost_emit_sampler_descriptors(batch, i, &ctx->payloads[i]);
293 panfrost_emit_texture_descriptors(batch, i, &ctx->payloads[i]);
294 panfrost_emit_const_buf(batch, i, &ctx->payloads[i]);
295 }
296
297 /* TODO: Upload the viewport somewhere more appropriate */
298
299 panfrost_emit_viewport(batch, &ctx->payloads[PIPE_SHADER_FRAGMENT]);
300 }
301
302 /* Corresponds to exactly one draw, but does not submit anything */
303
304 static void
305 panfrost_queue_draw(struct panfrost_context *ctx)
306 {
307 /* Handle dirty flags now */
308 panfrost_emit_for_draw(ctx);
309
310 struct panfrost_batch *batch = panfrost_get_batch_for_fbo(ctx);
311
312 panfrost_emit_vertex_tiler_jobs(batch,
313 &ctx->payloads[PIPE_SHADER_VERTEX],
314 &ctx->payloads[PIPE_SHADER_FRAGMENT]);
315 panfrost_batch_adjust_stack_size(batch);
316 }
317
318 /* The entire frame is in memory -- send it off to the kernel! */
319
320 void
321 panfrost_flush(
322 struct pipe_context *pipe,
323 struct pipe_fence_handle **fence,
324 unsigned flags)
325 {
326 struct panfrost_context *ctx = pan_context(pipe);
327 struct util_dynarray fences;
328
329 /* We must collect the fences before the flush is done, otherwise we'll
330 * lose track of them.
331 */
332 if (fence) {
333 util_dynarray_init(&fences, NULL);
334 hash_table_foreach(ctx->batches, hentry) {
335 struct panfrost_batch *batch = hentry->data;
336
337 panfrost_batch_fence_reference(batch->out_sync);
338 util_dynarray_append(&fences,
339 struct panfrost_batch_fence *,
340 batch->out_sync);
341 }
342 }
343
344 /* Submit all pending jobs */
345 panfrost_flush_all_batches(ctx, false);
346
347 if (fence) {
348 struct panfrost_fence *f = panfrost_fence_create(ctx, &fences);
349 pipe->screen->fence_reference(pipe->screen, fence, NULL);
350 *fence = (struct pipe_fence_handle *)f;
351
352 util_dynarray_foreach(&fences, struct panfrost_batch_fence *, fence)
353 panfrost_batch_fence_unreference(*fence);
354
355 util_dynarray_fini(&fences);
356 }
357
358 if (pan_debug & PAN_DBG_TRACE)
359 pandecode_next_frame();
360 }
361
362 #define DEFINE_CASE(c) case PIPE_PRIM_##c: return MALI_##c;
363
364 static int
365 g2m_draw_mode(enum pipe_prim_type mode)
366 {
367 switch (mode) {
368 DEFINE_CASE(POINTS);
369 DEFINE_CASE(LINES);
370 DEFINE_CASE(LINE_LOOP);
371 DEFINE_CASE(LINE_STRIP);
372 DEFINE_CASE(TRIANGLES);
373 DEFINE_CASE(TRIANGLE_STRIP);
374 DEFINE_CASE(TRIANGLE_FAN);
375 DEFINE_CASE(QUADS);
376 DEFINE_CASE(QUAD_STRIP);
377 DEFINE_CASE(POLYGON);
378
379 default:
380 unreachable("Invalid draw mode");
381 }
382 }
383
384 #undef DEFINE_CASE
385
386 static unsigned
387 panfrost_translate_index_size(unsigned size)
388 {
389 switch (size) {
390 case 1:
391 return MALI_DRAW_INDEXED_UINT8;
392
393 case 2:
394 return MALI_DRAW_INDEXED_UINT16;
395
396 case 4:
397 return MALI_DRAW_INDEXED_UINT32;
398
399 default:
400 unreachable("Invalid index size");
401 }
402 }
403
404 /* Gets a GPU address for the associated index buffer. Only gauranteed to be
405 * good for the duration of the draw (transient), could last longer. Also get
406 * the bounds on the index buffer for the range accessed by the draw. We do
407 * these operations together because there are natural optimizations which
408 * require them to be together. */
409
410 static mali_ptr
411 panfrost_get_index_buffer_bounded(struct panfrost_context *ctx, const struct pipe_draw_info *info, unsigned *min_index, unsigned *max_index)
412 {
413 struct panfrost_resource *rsrc = (struct panfrost_resource *) (info->index.resource);
414
415 off_t offset = info->start * info->index_size;
416 struct panfrost_batch *batch = panfrost_get_batch_for_fbo(ctx);
417 mali_ptr out = 0;
418
419 bool needs_indices = true;
420
421 if (info->max_index != ~0u) {
422 *min_index = info->min_index;
423 *max_index = info->max_index;
424 needs_indices = false;
425 }
426
427 if (!info->has_user_indices) {
428 /* Only resources can be directly mapped */
429 panfrost_batch_add_bo(batch, rsrc->bo,
430 PAN_BO_ACCESS_SHARED |
431 PAN_BO_ACCESS_READ |
432 PAN_BO_ACCESS_VERTEX_TILER);
433 out = rsrc->bo->gpu + offset;
434
435 /* Check the cache */
436 needs_indices = !panfrost_minmax_cache_get(rsrc->index_cache, info->start, info->count,
437 min_index, max_index);
438 } else {
439 /* Otherwise, we need to upload to transient memory */
440 const uint8_t *ibuf8 = (const uint8_t *) info->index.user;
441 out = panfrost_upload_transient(batch, ibuf8 + offset, info->count * info->index_size);
442 }
443
444 if (needs_indices) {
445 /* Fallback */
446 u_vbuf_get_minmax_index(&ctx->base, info, min_index, max_index);
447
448 if (!info->has_user_indices) {
449 panfrost_minmax_cache_add(rsrc->index_cache, info->start, info->count,
450 *min_index, *max_index);
451 }
452 }
453
454
455 return out;
456 }
457
458 static bool
459 panfrost_scissor_culls_everything(struct panfrost_context *ctx)
460 {
461 const struct pipe_scissor_state *ss = &ctx->scissor;
462
463 /* Check if we're scissoring at all */
464
465 if (!(ctx->rasterizer && ctx->rasterizer->base.scissor))
466 return false;
467
468 return (ss->minx == ss->maxx) || (ss->miny == ss->maxy);
469 }
470
471 /* Count generated primitives (when there is no geom/tess shaders) for
472 * transform feedback */
473
474 static void
475 panfrost_statistics_record(
476 struct panfrost_context *ctx,
477 const struct pipe_draw_info *info)
478 {
479 if (!ctx->active_queries)
480 return;
481
482 uint32_t prims = u_prims_for_vertices(info->mode, info->count);
483 ctx->prims_generated += prims;
484
485 if (!ctx->streamout.num_targets)
486 return;
487
488 ctx->tf_prims_generated += prims;
489 }
490
491 static void
492 panfrost_draw_vbo(
493 struct pipe_context *pipe,
494 const struct pipe_draw_info *info)
495 {
496 struct panfrost_context *ctx = pan_context(pipe);
497
498 /* First of all, check the scissor to see if anything is drawn at all.
499 * If it's not, we drop the draw (mostly a conformance issue;
500 * well-behaved apps shouldn't hit this) */
501
502 if (panfrost_scissor_culls_everything(ctx))
503 return;
504
505 int mode = info->mode;
506
507 /* Fallback unsupported restart index */
508 unsigned primitive_index = (1 << (info->index_size * 8)) - 1;
509
510 if (info->primitive_restart && info->index_size
511 && info->restart_index != primitive_index) {
512 util_draw_vbo_without_prim_restart(pipe, info);
513 return;
514 }
515
516 /* Fallback for unsupported modes */
517
518 assert(ctx->rasterizer != NULL);
519
520 if (!(ctx->draw_modes & (1 << mode))) {
521 if (mode == PIPE_PRIM_QUADS && info->count == 4 && !ctx->rasterizer->base.flatshade) {
522 mode = PIPE_PRIM_TRIANGLE_FAN;
523 } else {
524 if (info->count < 4) {
525 /* Degenerate case? */
526 return;
527 }
528
529 util_primconvert_save_rasterizer_state(ctx->primconvert, &ctx->rasterizer->base);
530 util_primconvert_draw_vbo(ctx->primconvert, info);
531 return;
532 }
533 }
534
535 ctx->payloads[PIPE_SHADER_VERTEX].offset_start = info->start;
536 ctx->payloads[PIPE_SHADER_FRAGMENT].offset_start = info->start;
537
538 /* Now that we have a guaranteed terminating path, find the job.
539 * Assignment commented out to prevent unused warning */
540
541 /* struct panfrost_batch *batch = */ panfrost_get_batch_for_fbo(ctx);
542
543 ctx->payloads[PIPE_SHADER_FRAGMENT].prefix.draw_mode = g2m_draw_mode(mode);
544
545 /* Take into account a negative bias */
546 ctx->vertex_count = info->count + abs(info->index_bias);
547 ctx->instance_count = info->instance_count;
548 ctx->active_prim = info->mode;
549
550 /* For non-indexed draws, they're the same */
551 unsigned vertex_count = ctx->vertex_count;
552
553 unsigned draw_flags = 0;
554
555 /* The draw flags interpret how primitive size is interpreted */
556
557 if (panfrost_writes_point_size(ctx))
558 draw_flags |= MALI_DRAW_VARYING_SIZE;
559
560 if (info->primitive_restart)
561 draw_flags |= MALI_DRAW_PRIMITIVE_RESTART_FIXED_INDEX;
562
563 /* These doesn't make much sense */
564
565 draw_flags |= 0x3000;
566
567 if (ctx->rasterizer && ctx->rasterizer->base.flatshade_first)
568 draw_flags |= MALI_DRAW_FLATSHADE_FIRST;
569
570 panfrost_statistics_record(ctx, info);
571
572 if (info->index_size) {
573 unsigned min_index = 0, max_index = 0;
574 ctx->payloads[PIPE_SHADER_FRAGMENT].prefix.indices =
575 panfrost_get_index_buffer_bounded(ctx, info, &min_index, &max_index);
576
577 /* Use the corresponding values */
578 vertex_count = max_index - min_index + 1;
579 ctx->payloads[PIPE_SHADER_VERTEX].offset_start = min_index + info->index_bias;
580 ctx->payloads[PIPE_SHADER_FRAGMENT].offset_start = min_index + info->index_bias;
581
582 ctx->payloads[PIPE_SHADER_FRAGMENT].prefix.offset_bias_correction = -min_index;
583 ctx->payloads[PIPE_SHADER_FRAGMENT].prefix.index_count = MALI_POSITIVE(info->count);
584
585 draw_flags |= panfrost_translate_index_size(info->index_size);
586 } else {
587 /* Index count == vertex count, if no indexing is applied, as
588 * if it is internally indexed in the expected order */
589
590 ctx->payloads[PIPE_SHADER_FRAGMENT].prefix.offset_bias_correction = 0;
591 ctx->payloads[PIPE_SHADER_FRAGMENT].prefix.index_count = MALI_POSITIVE(ctx->vertex_count);
592
593 /* Reverse index state */
594 ctx->payloads[PIPE_SHADER_FRAGMENT].prefix.indices = (mali_ptr) 0;
595 }
596
597 /* Dispatch "compute jobs" for the vertex/tiler pair as (1,
598 * vertex_count, 1) */
599
600 panfrost_pack_work_groups_fused(
601 &ctx->payloads[PIPE_SHADER_VERTEX].prefix,
602 &ctx->payloads[PIPE_SHADER_FRAGMENT].prefix,
603 1, vertex_count, info->instance_count,
604 1, 1, 1);
605
606 ctx->payloads[PIPE_SHADER_FRAGMENT].prefix.unknown_draw = draw_flags;
607
608 /* Encode the padded vertex count */
609
610 if (info->instance_count > 1) {
611 ctx->padded_count = panfrost_padded_vertex_count(vertex_count);
612
613 unsigned shift = __builtin_ctz(ctx->padded_count);
614 unsigned k = ctx->padded_count >> (shift + 1);
615
616 ctx->payloads[PIPE_SHADER_VERTEX].instance_shift = shift;
617 ctx->payloads[PIPE_SHADER_FRAGMENT].instance_shift = shift;
618
619 ctx->payloads[PIPE_SHADER_VERTEX].instance_odd = k;
620 ctx->payloads[PIPE_SHADER_FRAGMENT].instance_odd = k;
621 } else {
622 ctx->padded_count = vertex_count;
623
624 /* Reset instancing state */
625 ctx->payloads[PIPE_SHADER_VERTEX].instance_shift = 0;
626 ctx->payloads[PIPE_SHADER_VERTEX].instance_odd = 0;
627 ctx->payloads[PIPE_SHADER_FRAGMENT].instance_shift = 0;
628 ctx->payloads[PIPE_SHADER_FRAGMENT].instance_odd = 0;
629 }
630
631 /* Fire off the draw itself */
632 panfrost_queue_draw(ctx);
633
634 /* Increment transform feedback offsets */
635
636 for (unsigned i = 0; i < ctx->streamout.num_targets; ++i) {
637 unsigned output_count = u_stream_outputs_for_vertices(
638 ctx->active_prim, ctx->vertex_count);
639
640 ctx->streamout.offsets[i] += output_count;
641 }
642 }
643
644 /* CSO state */
645
646 static void
647 panfrost_generic_cso_delete(struct pipe_context *pctx, void *hwcso)
648 {
649 free(hwcso);
650 }
651
652 static void *
653 panfrost_create_rasterizer_state(
654 struct pipe_context *pctx,
655 const struct pipe_rasterizer_state *cso)
656 {
657 struct panfrost_rasterizer *so = CALLOC_STRUCT(panfrost_rasterizer);
658
659 so->base = *cso;
660
661 return so;
662 }
663
664 static void
665 panfrost_bind_rasterizer_state(
666 struct pipe_context *pctx,
667 void *hwcso)
668 {
669 struct panfrost_context *ctx = pan_context(pctx);
670
671 ctx->rasterizer = hwcso;
672
673 if (!hwcso)
674 return;
675
676 /* Gauranteed with the core GL call, so don't expose ARB_polygon_offset */
677 assert(ctx->rasterizer->base.offset_clamp == 0.0);
678
679 /* Point sprites are emulated */
680
681 struct panfrost_shader_state *variant = panfrost_get_shader_state(ctx, PIPE_SHADER_FRAGMENT);
682
683 if (ctx->rasterizer->base.sprite_coord_enable || (variant && variant->point_sprite_mask))
684 ctx->base.bind_fs_state(&ctx->base, ctx->shader[PIPE_SHADER_FRAGMENT]);
685 }
686
687 static void *
688 panfrost_create_vertex_elements_state(
689 struct pipe_context *pctx,
690 unsigned num_elements,
691 const struct pipe_vertex_element *elements)
692 {
693 struct panfrost_vertex_state *so = CALLOC_STRUCT(panfrost_vertex_state);
694
695 so->num_elements = num_elements;
696 memcpy(so->pipe, elements, sizeof(*elements) * num_elements);
697
698 for (int i = 0; i < num_elements; ++i) {
699 so->hw[i].index = i;
700
701 enum pipe_format fmt = elements[i].src_format;
702 const struct util_format_description *desc = util_format_description(fmt);
703 so->hw[i].unknown1 = 0x2;
704 so->hw[i].swizzle = panfrost_get_default_swizzle(desc->nr_channels);
705
706 so->hw[i].format = panfrost_find_format(desc);
707 }
708
709 /* Let's also prepare vertex builtins */
710 so->hw[PAN_VERTEX_ID].format = MALI_R32UI;
711 so->hw[PAN_VERTEX_ID].swizzle = panfrost_get_default_swizzle(1);
712 so->hw[PAN_INSTANCE_ID].format = MALI_R32UI;
713 so->hw[PAN_INSTANCE_ID].swizzle = panfrost_get_default_swizzle(1);
714
715 return so;
716 }
717
718 static void
719 panfrost_bind_vertex_elements_state(
720 struct pipe_context *pctx,
721 void *hwcso)
722 {
723 struct panfrost_context *ctx = pan_context(pctx);
724 ctx->vertex = hwcso;
725 }
726
727 static void *
728 panfrost_create_shader_state(
729 struct pipe_context *pctx,
730 const struct pipe_shader_state *cso,
731 enum pipe_shader_type stage)
732 {
733 struct panfrost_shader_variants *so = CALLOC_STRUCT(panfrost_shader_variants);
734 so->base = *cso;
735
736 /* Token deep copy to prevent memory corruption */
737
738 if (cso->type == PIPE_SHADER_IR_TGSI)
739 so->base.tokens = tgsi_dup_tokens(so->base.tokens);
740
741 /* Precompile for shader-db if we need to */
742 if (unlikely((pan_debug & PAN_DBG_PRECOMPILE) && cso->type == PIPE_SHADER_IR_NIR)) {
743 struct panfrost_context *ctx = pan_context(pctx);
744
745 struct panfrost_shader_state state;
746 uint64_t outputs_written;
747
748 panfrost_shader_compile(ctx, PIPE_SHADER_IR_NIR,
749 so->base.ir.nir,
750 tgsi_processor_to_shader_stage(stage),
751 &state, &outputs_written);
752 }
753
754 return so;
755 }
756
757 static void
758 panfrost_delete_shader_state(
759 struct pipe_context *pctx,
760 void *so)
761 {
762 struct panfrost_shader_variants *cso = (struct panfrost_shader_variants *) so;
763
764 if (cso->base.type == PIPE_SHADER_IR_TGSI) {
765 DBG("Deleting TGSI shader leaks duplicated tokens\n");
766 }
767
768 for (unsigned i = 0; i < cso->variant_count; ++i) {
769 struct panfrost_shader_state *shader_state = &cso->variants[i];
770 panfrost_bo_unreference(shader_state->bo);
771 shader_state->bo = NULL;
772 }
773 free(cso->variants);
774
775 free(so);
776 }
777
778 static void *
779 panfrost_create_sampler_state(
780 struct pipe_context *pctx,
781 const struct pipe_sampler_state *cso)
782 {
783 struct panfrost_sampler_state *so = CALLOC_STRUCT(panfrost_sampler_state);
784 so->base = *cso;
785
786 panfrost_sampler_desc_init(cso, &so->hw);
787
788 return so;
789 }
790
791 static void
792 panfrost_bind_sampler_states(
793 struct pipe_context *pctx,
794 enum pipe_shader_type shader,
795 unsigned start_slot, unsigned num_sampler,
796 void **sampler)
797 {
798 assert(start_slot == 0);
799
800 struct panfrost_context *ctx = pan_context(pctx);
801
802 /* XXX: Should upload, not just copy? */
803 ctx->sampler_count[shader] = num_sampler;
804 memcpy(ctx->samplers[shader], sampler, num_sampler * sizeof (void *));
805 }
806
807 static bool
808 panfrost_variant_matches(
809 struct panfrost_context *ctx,
810 struct panfrost_shader_state *variant,
811 enum pipe_shader_type type)
812 {
813 struct pipe_rasterizer_state *rasterizer = &ctx->rasterizer->base;
814 struct pipe_alpha_state *alpha = &ctx->depth_stencil->alpha;
815
816 bool is_fragment = (type == PIPE_SHADER_FRAGMENT);
817
818 if (is_fragment && (alpha->enabled || variant->alpha_state.enabled)) {
819 /* Make sure enable state is at least the same */
820 if (alpha->enabled != variant->alpha_state.enabled) {
821 return false;
822 }
823
824 /* Check that the contents of the test are the same */
825 bool same_func = alpha->func == variant->alpha_state.func;
826 bool same_ref = alpha->ref_value == variant->alpha_state.ref_value;
827
828 if (!(same_func && same_ref)) {
829 return false;
830 }
831 }
832
833 if (is_fragment && rasterizer && (rasterizer->sprite_coord_enable |
834 variant->point_sprite_mask)) {
835 /* Ensure the same varyings are turned to point sprites */
836 if (rasterizer->sprite_coord_enable != variant->point_sprite_mask)
837 return false;
838
839 /* Ensure the orientation is correct */
840 bool upper_left =
841 rasterizer->sprite_coord_mode ==
842 PIPE_SPRITE_COORD_UPPER_LEFT;
843
844 if (variant->point_sprite_upper_left != upper_left)
845 return false;
846 }
847
848 /* Otherwise, we're good to go */
849 return true;
850 }
851
852 /**
853 * Fix an uncompiled shader's stream output info, and produce a bitmask
854 * of which VARYING_SLOT_* are captured for stream output.
855 *
856 * Core Gallium stores output->register_index as a "slot" number, where
857 * slots are assigned consecutively to all outputs in info->outputs_written.
858 * This naive packing of outputs doesn't work for us - we too have slots,
859 * but the layout is defined by the VUE map, which we won't have until we
860 * compile a specific shader variant. So, we remap these and simply store
861 * VARYING_SLOT_* in our copy's output->register_index fields.
862 *
863 * We then produce a bitmask of outputs which are used for SO.
864 *
865 * Implementation from iris.
866 */
867
868 static uint64_t
869 update_so_info(struct pipe_stream_output_info *so_info,
870 uint64_t outputs_written)
871 {
872 uint64_t so_outputs = 0;
873 uint8_t reverse_map[64] = {0};
874 unsigned slot = 0;
875
876 while (outputs_written)
877 reverse_map[slot++] = u_bit_scan64(&outputs_written);
878
879 for (unsigned i = 0; i < so_info->num_outputs; i++) {
880 struct pipe_stream_output *output = &so_info->output[i];
881
882 /* Map Gallium's condensed "slots" back to real VARYING_SLOT_* enums */
883 output->register_index = reverse_map[output->register_index];
884
885 so_outputs |= 1ull << output->register_index;
886 }
887
888 return so_outputs;
889 }
890
891 static void
892 panfrost_bind_shader_state(
893 struct pipe_context *pctx,
894 void *hwcso,
895 enum pipe_shader_type type)
896 {
897 struct panfrost_context *ctx = pan_context(pctx);
898 ctx->shader[type] = hwcso;
899
900 if (!hwcso) return;
901
902 /* Match the appropriate variant */
903
904 signed variant = -1;
905 struct panfrost_shader_variants *variants = (struct panfrost_shader_variants *) hwcso;
906
907 for (unsigned i = 0; i < variants->variant_count; ++i) {
908 if (panfrost_variant_matches(ctx, &variants->variants[i], type)) {
909 variant = i;
910 break;
911 }
912 }
913
914 if (variant == -1) {
915 /* No variant matched, so create a new one */
916 variant = variants->variant_count++;
917
918 if (variants->variant_count > variants->variant_space) {
919 unsigned old_space = variants->variant_space;
920
921 variants->variant_space *= 2;
922 if (variants->variant_space == 0)
923 variants->variant_space = 1;
924
925 /* Arbitrary limit to stop runaway programs from
926 * creating an unbounded number of shader variants. */
927 assert(variants->variant_space < 1024);
928
929 unsigned msize = sizeof(struct panfrost_shader_state);
930 variants->variants = realloc(variants->variants,
931 variants->variant_space * msize);
932
933 memset(&variants->variants[old_space], 0,
934 (variants->variant_space - old_space) * msize);
935 }
936
937 struct panfrost_shader_state *v =
938 &variants->variants[variant];
939
940 if (type == PIPE_SHADER_FRAGMENT) {
941 v->alpha_state = ctx->depth_stencil->alpha;
942
943 if (ctx->rasterizer) {
944 v->point_sprite_mask = ctx->rasterizer->base.sprite_coord_enable;
945 v->point_sprite_upper_left =
946 ctx->rasterizer->base.sprite_coord_mode ==
947 PIPE_SPRITE_COORD_UPPER_LEFT;
948 }
949 }
950 }
951
952 /* Select this variant */
953 variants->active_variant = variant;
954
955 struct panfrost_shader_state *shader_state = &variants->variants[variant];
956 assert(panfrost_variant_matches(ctx, shader_state, type));
957
958 /* We finally have a variant, so compile it */
959
960 if (!shader_state->compiled) {
961 uint64_t outputs_written = 0;
962
963 panfrost_shader_compile(ctx, variants->base.type,
964 variants->base.type == PIPE_SHADER_IR_NIR ?
965 variants->base.ir.nir :
966 variants->base.tokens,
967 tgsi_processor_to_shader_stage(type),
968 shader_state,
969 &outputs_written);
970
971 shader_state->compiled = true;
972
973 /* Fixup the stream out information, since what Gallium returns
974 * normally is mildly insane */
975
976 shader_state->stream_output = variants->base.stream_output;
977 shader_state->so_mask =
978 update_so_info(&shader_state->stream_output, outputs_written);
979 }
980 }
981
982 static void *
983 panfrost_create_vs_state(struct pipe_context *pctx, const struct pipe_shader_state *hwcso)
984 {
985 return panfrost_create_shader_state(pctx, hwcso, PIPE_SHADER_VERTEX);
986 }
987
988 static void *
989 panfrost_create_fs_state(struct pipe_context *pctx, const struct pipe_shader_state *hwcso)
990 {
991 return panfrost_create_shader_state(pctx, hwcso, PIPE_SHADER_FRAGMENT);
992 }
993
994 static void
995 panfrost_bind_vs_state(struct pipe_context *pctx, void *hwcso)
996 {
997 panfrost_bind_shader_state(pctx, hwcso, PIPE_SHADER_VERTEX);
998 }
999
1000 static void
1001 panfrost_bind_fs_state(struct pipe_context *pctx, void *hwcso)
1002 {
1003 panfrost_bind_shader_state(pctx, hwcso, PIPE_SHADER_FRAGMENT);
1004 }
1005
1006 static void
1007 panfrost_set_vertex_buffers(
1008 struct pipe_context *pctx,
1009 unsigned start_slot,
1010 unsigned num_buffers,
1011 const struct pipe_vertex_buffer *buffers)
1012 {
1013 struct panfrost_context *ctx = pan_context(pctx);
1014
1015 util_set_vertex_buffers_mask(ctx->vertex_buffers, &ctx->vb_mask, buffers, start_slot, num_buffers);
1016 }
1017
1018 static void
1019 panfrost_set_constant_buffer(
1020 struct pipe_context *pctx,
1021 enum pipe_shader_type shader, uint index,
1022 const struct pipe_constant_buffer *buf)
1023 {
1024 struct panfrost_context *ctx = pan_context(pctx);
1025 struct panfrost_constant_buffer *pbuf = &ctx->constant_buffer[shader];
1026
1027 util_copy_constant_buffer(&pbuf->cb[index], buf);
1028
1029 unsigned mask = (1 << index);
1030
1031 if (unlikely(!buf)) {
1032 pbuf->enabled_mask &= ~mask;
1033 pbuf->dirty_mask &= ~mask;
1034 return;
1035 }
1036
1037 pbuf->enabled_mask |= mask;
1038 pbuf->dirty_mask |= mask;
1039 }
1040
1041 static void
1042 panfrost_set_stencil_ref(
1043 struct pipe_context *pctx,
1044 const struct pipe_stencil_ref *ref)
1045 {
1046 struct panfrost_context *ctx = pan_context(pctx);
1047 ctx->stencil_ref = *ref;
1048 }
1049
1050 static enum mali_texture_type
1051 panfrost_translate_texture_type(enum pipe_texture_target t) {
1052 switch (t)
1053 {
1054 case PIPE_BUFFER:
1055 case PIPE_TEXTURE_1D:
1056 case PIPE_TEXTURE_1D_ARRAY:
1057 return MALI_TEX_1D;
1058
1059 case PIPE_TEXTURE_2D:
1060 case PIPE_TEXTURE_2D_ARRAY:
1061 case PIPE_TEXTURE_RECT:
1062 return MALI_TEX_2D;
1063
1064 case PIPE_TEXTURE_3D:
1065 return MALI_TEX_3D;
1066
1067 case PIPE_TEXTURE_CUBE:
1068 case PIPE_TEXTURE_CUBE_ARRAY:
1069 return MALI_TEX_CUBE;
1070
1071 default:
1072 unreachable("Unknown target");
1073 }
1074 }
1075
1076 static struct pipe_sampler_view *
1077 panfrost_create_sampler_view(
1078 struct pipe_context *pctx,
1079 struct pipe_resource *texture,
1080 const struct pipe_sampler_view *template)
1081 {
1082 struct panfrost_screen *screen = pan_screen(pctx->screen);
1083 struct panfrost_sampler_view *so = rzalloc(pctx, struct panfrost_sampler_view);
1084
1085 pipe_reference(NULL, &texture->reference);
1086
1087 struct panfrost_resource *prsrc = (struct panfrost_resource *) texture;
1088 assert(prsrc->bo);
1089
1090 so->base = *template;
1091 so->base.texture = texture;
1092 so->base.reference.count = 1;
1093 so->base.context = pctx;
1094
1095 unsigned char user_swizzle[4] = {
1096 template->swizzle_r,
1097 template->swizzle_g,
1098 template->swizzle_b,
1099 template->swizzle_a
1100 };
1101
1102 /* In the hardware, array_size refers specifically to array textures,
1103 * whereas in Gallium, it also covers cubemaps */
1104
1105 unsigned array_size = texture->array_size;
1106
1107 if (template->target == PIPE_TEXTURE_CUBE) {
1108 /* TODO: Cubemap arrays */
1109 assert(array_size == 6);
1110 array_size /= 6;
1111 }
1112
1113 enum mali_texture_type type =
1114 panfrost_translate_texture_type(template->target);
1115
1116 unsigned size = panfrost_estimate_texture_size(
1117 template->u.tex.first_level,
1118 template->u.tex.last_level,
1119 template->u.tex.first_layer,
1120 template->u.tex.last_layer,
1121 type, prsrc->layout);
1122
1123 so->bo = panfrost_bo_create(screen, size, 0);
1124
1125 panfrost_new_texture(
1126 so->bo->cpu,
1127 texture->width0, texture->height0,
1128 texture->depth0, array_size,
1129 template->format,
1130 type, prsrc->layout,
1131 template->u.tex.first_level,
1132 template->u.tex.last_level,
1133 template->u.tex.first_layer,
1134 template->u.tex.last_layer,
1135 prsrc->cubemap_stride,
1136 panfrost_translate_swizzle_4(user_swizzle),
1137 prsrc->bo->gpu,
1138 prsrc->slices);
1139
1140 return (struct pipe_sampler_view *) so;
1141 }
1142
1143 static void
1144 panfrost_set_sampler_views(
1145 struct pipe_context *pctx,
1146 enum pipe_shader_type shader,
1147 unsigned start_slot, unsigned num_views,
1148 struct pipe_sampler_view **views)
1149 {
1150 struct panfrost_context *ctx = pan_context(pctx);
1151 unsigned new_nr = 0;
1152 unsigned i;
1153
1154 assert(start_slot == 0);
1155
1156 for (i = 0; i < num_views; ++i) {
1157 if (views[i])
1158 new_nr = i + 1;
1159 pipe_sampler_view_reference((struct pipe_sampler_view **)&ctx->sampler_views[shader][i],
1160 views[i]);
1161 }
1162
1163 for (; i < ctx->sampler_view_count[shader]; i++) {
1164 pipe_sampler_view_reference((struct pipe_sampler_view **)&ctx->sampler_views[shader][i],
1165 NULL);
1166 }
1167 ctx->sampler_view_count[shader] = new_nr;
1168 }
1169
1170 static void
1171 panfrost_sampler_view_destroy(
1172 struct pipe_context *pctx,
1173 struct pipe_sampler_view *pview)
1174 {
1175 struct panfrost_sampler_view *view = (struct panfrost_sampler_view *) pview;
1176
1177 pipe_resource_reference(&pview->texture, NULL);
1178 panfrost_bo_unreference(view->bo);
1179 ralloc_free(view);
1180 }
1181
1182 static void
1183 panfrost_set_shader_buffers(
1184 struct pipe_context *pctx,
1185 enum pipe_shader_type shader,
1186 unsigned start, unsigned count,
1187 const struct pipe_shader_buffer *buffers,
1188 unsigned writable_bitmask)
1189 {
1190 struct panfrost_context *ctx = pan_context(pctx);
1191
1192 util_set_shader_buffers_mask(ctx->ssbo[shader], &ctx->ssbo_mask[shader],
1193 buffers, start, count);
1194 }
1195
1196 /* Hints that a framebuffer should use AFBC where possible */
1197
1198 static void
1199 panfrost_hint_afbc(
1200 struct panfrost_screen *screen,
1201 const struct pipe_framebuffer_state *fb)
1202 {
1203 /* AFBC implemenation incomplete; hide it */
1204 if (!(pan_debug & PAN_DBG_AFBC)) return;
1205
1206 /* Hint AFBC to the resources bound to each color buffer */
1207
1208 for (unsigned i = 0; i < fb->nr_cbufs; ++i) {
1209 struct pipe_surface *surf = fb->cbufs[i];
1210 struct panfrost_resource *rsrc = pan_resource(surf->texture);
1211 panfrost_resource_hint_layout(screen, rsrc, MALI_TEXTURE_AFBC, 1);
1212 }
1213
1214 /* Also hint it to the depth buffer */
1215
1216 if (fb->zsbuf) {
1217 struct panfrost_resource *rsrc = pan_resource(fb->zsbuf->texture);
1218 panfrost_resource_hint_layout(screen, rsrc, MALI_TEXTURE_AFBC, 1);
1219 }
1220 }
1221
1222 static void
1223 panfrost_set_framebuffer_state(struct pipe_context *pctx,
1224 const struct pipe_framebuffer_state *fb)
1225 {
1226 struct panfrost_context *ctx = pan_context(pctx);
1227
1228 panfrost_hint_afbc(pan_screen(pctx->screen), fb);
1229 util_copy_framebuffer_state(&ctx->pipe_framebuffer, fb);
1230 ctx->batch = NULL;
1231 panfrost_invalidate_frame(ctx);
1232 }
1233
1234 static void *
1235 panfrost_create_depth_stencil_state(struct pipe_context *pipe,
1236 const struct pipe_depth_stencil_alpha_state *depth_stencil)
1237 {
1238 return mem_dup(depth_stencil, sizeof(*depth_stencil));
1239 }
1240
1241 static void
1242 panfrost_bind_depth_stencil_state(struct pipe_context *pipe,
1243 void *cso)
1244 {
1245 struct panfrost_context *ctx = pan_context(pipe);
1246 struct pipe_depth_stencil_alpha_state *depth_stencil = cso;
1247 ctx->depth_stencil = depth_stencil;
1248
1249 if (!depth_stencil)
1250 return;
1251
1252 /* Alpha does not exist in the hardware (it's not in ES3), so it's
1253 * emulated in the fragment shader */
1254
1255 if (depth_stencil->alpha.enabled) {
1256 /* We need to trigger a new shader (maybe) */
1257 ctx->base.bind_fs_state(&ctx->base, ctx->shader[PIPE_SHADER_FRAGMENT]);
1258 }
1259
1260 /* Bounds test not implemented */
1261 assert(!depth_stencil->depth.bounds_test);
1262 }
1263
1264 static void
1265 panfrost_delete_depth_stencil_state(struct pipe_context *pipe, void *depth)
1266 {
1267 free( depth );
1268 }
1269
1270 static void
1271 panfrost_set_sample_mask(struct pipe_context *pipe,
1272 unsigned sample_mask)
1273 {
1274 }
1275
1276 static void
1277 panfrost_set_clip_state(struct pipe_context *pipe,
1278 const struct pipe_clip_state *clip)
1279 {
1280 //struct panfrost_context *panfrost = pan_context(pipe);
1281 }
1282
1283 static void
1284 panfrost_set_viewport_states(struct pipe_context *pipe,
1285 unsigned start_slot,
1286 unsigned num_viewports,
1287 const struct pipe_viewport_state *viewports)
1288 {
1289 struct panfrost_context *ctx = pan_context(pipe);
1290
1291 assert(start_slot == 0);
1292 assert(num_viewports == 1);
1293
1294 ctx->pipe_viewport = *viewports;
1295 }
1296
1297 static void
1298 panfrost_set_scissor_states(struct pipe_context *pipe,
1299 unsigned start_slot,
1300 unsigned num_scissors,
1301 const struct pipe_scissor_state *scissors)
1302 {
1303 struct panfrost_context *ctx = pan_context(pipe);
1304
1305 assert(start_slot == 0);
1306 assert(num_scissors == 1);
1307
1308 ctx->scissor = *scissors;
1309 }
1310
1311 static void
1312 panfrost_set_polygon_stipple(struct pipe_context *pipe,
1313 const struct pipe_poly_stipple *stipple)
1314 {
1315 //struct panfrost_context *panfrost = pan_context(pipe);
1316 }
1317
1318 static void
1319 panfrost_set_active_query_state(struct pipe_context *pipe,
1320 bool enable)
1321 {
1322 struct panfrost_context *ctx = pan_context(pipe);
1323 ctx->active_queries = enable;
1324 }
1325
1326 static void
1327 panfrost_destroy(struct pipe_context *pipe)
1328 {
1329 struct panfrost_context *panfrost = pan_context(pipe);
1330
1331 if (panfrost->blitter)
1332 util_blitter_destroy(panfrost->blitter);
1333
1334 if (panfrost->blitter_wallpaper)
1335 util_blitter_destroy(panfrost->blitter_wallpaper);
1336
1337 util_unreference_framebuffer_state(&panfrost->pipe_framebuffer);
1338 u_upload_destroy(pipe->stream_uploader);
1339
1340 ralloc_free(pipe);
1341 }
1342
1343 static struct pipe_query *
1344 panfrost_create_query(struct pipe_context *pipe,
1345 unsigned type,
1346 unsigned index)
1347 {
1348 struct panfrost_query *q = rzalloc(pipe, struct panfrost_query);
1349
1350 q->type = type;
1351 q->index = index;
1352
1353 return (struct pipe_query *) q;
1354 }
1355
1356 static void
1357 panfrost_destroy_query(struct pipe_context *pipe, struct pipe_query *q)
1358 {
1359 struct panfrost_query *query = (struct panfrost_query *) q;
1360
1361 if (query->bo) {
1362 panfrost_bo_unreference(query->bo);
1363 query->bo = NULL;
1364 }
1365
1366 ralloc_free(q);
1367 }
1368
1369 static bool
1370 panfrost_begin_query(struct pipe_context *pipe, struct pipe_query *q)
1371 {
1372 struct panfrost_context *ctx = pan_context(pipe);
1373 struct panfrost_query *query = (struct panfrost_query *) q;
1374
1375 switch (query->type) {
1376 case PIPE_QUERY_OCCLUSION_COUNTER:
1377 case PIPE_QUERY_OCCLUSION_PREDICATE:
1378 case PIPE_QUERY_OCCLUSION_PREDICATE_CONSERVATIVE:
1379 /* Allocate a bo for the query results to be stored */
1380 if (!query->bo) {
1381 query->bo = panfrost_bo_create(
1382 pan_screen(ctx->base.screen),
1383 sizeof(unsigned), 0);
1384 }
1385
1386 unsigned *result = (unsigned *)query->bo->cpu;
1387 *result = 0; /* Default to 0 if nothing at all drawn. */
1388 ctx->occlusion_query = query;
1389 break;
1390
1391 /* Geometry statistics are computed in the driver. XXX: geom/tess
1392 * shaders.. */
1393
1394 case PIPE_QUERY_PRIMITIVES_GENERATED:
1395 query->start = ctx->prims_generated;
1396 break;
1397 case PIPE_QUERY_PRIMITIVES_EMITTED:
1398 query->start = ctx->tf_prims_generated;
1399 break;
1400
1401 default:
1402 DBG("Skipping query %u\n", query->type);
1403 break;
1404 }
1405
1406 return true;
1407 }
1408
1409 static bool
1410 panfrost_end_query(struct pipe_context *pipe, struct pipe_query *q)
1411 {
1412 struct panfrost_context *ctx = pan_context(pipe);
1413 struct panfrost_query *query = (struct panfrost_query *) q;
1414
1415 switch (query->type) {
1416 case PIPE_QUERY_OCCLUSION_COUNTER:
1417 case PIPE_QUERY_OCCLUSION_PREDICATE:
1418 case PIPE_QUERY_OCCLUSION_PREDICATE_CONSERVATIVE:
1419 ctx->occlusion_query = NULL;
1420 break;
1421 case PIPE_QUERY_PRIMITIVES_GENERATED:
1422 query->end = ctx->prims_generated;
1423 break;
1424 case PIPE_QUERY_PRIMITIVES_EMITTED:
1425 query->end = ctx->tf_prims_generated;
1426 break;
1427 }
1428
1429 return true;
1430 }
1431
1432 static bool
1433 panfrost_get_query_result(struct pipe_context *pipe,
1434 struct pipe_query *q,
1435 bool wait,
1436 union pipe_query_result *vresult)
1437 {
1438 struct panfrost_query *query = (struct panfrost_query *) q;
1439 struct panfrost_context *ctx = pan_context(pipe);
1440
1441
1442 switch (query->type) {
1443 case PIPE_QUERY_OCCLUSION_COUNTER:
1444 case PIPE_QUERY_OCCLUSION_PREDICATE:
1445 case PIPE_QUERY_OCCLUSION_PREDICATE_CONSERVATIVE:
1446 /* Flush first */
1447 panfrost_flush_all_batches(ctx, true);
1448
1449 /* Read back the query results */
1450 unsigned *result = (unsigned *) query->bo->cpu;
1451 unsigned passed = *result;
1452
1453 if (query->type == PIPE_QUERY_OCCLUSION_COUNTER) {
1454 vresult->u64 = passed;
1455 } else {
1456 vresult->b = !!passed;
1457 }
1458
1459 break;
1460
1461 case PIPE_QUERY_PRIMITIVES_GENERATED:
1462 case PIPE_QUERY_PRIMITIVES_EMITTED:
1463 panfrost_flush_all_batches(ctx, true);
1464 vresult->u64 = query->end - query->start;
1465 break;
1466
1467 default:
1468 DBG("Skipped query get %u\n", query->type);
1469 break;
1470 }
1471
1472 return true;
1473 }
1474
1475 static struct pipe_stream_output_target *
1476 panfrost_create_stream_output_target(struct pipe_context *pctx,
1477 struct pipe_resource *prsc,
1478 unsigned buffer_offset,
1479 unsigned buffer_size)
1480 {
1481 struct pipe_stream_output_target *target;
1482
1483 target = rzalloc(pctx, struct pipe_stream_output_target);
1484
1485 if (!target)
1486 return NULL;
1487
1488 pipe_reference_init(&target->reference, 1);
1489 pipe_resource_reference(&target->buffer, prsc);
1490
1491 target->context = pctx;
1492 target->buffer_offset = buffer_offset;
1493 target->buffer_size = buffer_size;
1494
1495 return target;
1496 }
1497
1498 static void
1499 panfrost_stream_output_target_destroy(struct pipe_context *pctx,
1500 struct pipe_stream_output_target *target)
1501 {
1502 pipe_resource_reference(&target->buffer, NULL);
1503 ralloc_free(target);
1504 }
1505
1506 static void
1507 panfrost_set_stream_output_targets(struct pipe_context *pctx,
1508 unsigned num_targets,
1509 struct pipe_stream_output_target **targets,
1510 const unsigned *offsets)
1511 {
1512 struct panfrost_context *ctx = pan_context(pctx);
1513 struct panfrost_streamout *so = &ctx->streamout;
1514
1515 assert(num_targets <= ARRAY_SIZE(so->targets));
1516
1517 for (unsigned i = 0; i < num_targets; i++) {
1518 if (offsets[i] != -1)
1519 so->offsets[i] = offsets[i];
1520
1521 pipe_so_target_reference(&so->targets[i], targets[i]);
1522 }
1523
1524 for (unsigned i = 0; i < so->num_targets; i++)
1525 pipe_so_target_reference(&so->targets[i], NULL);
1526
1527 so->num_targets = num_targets;
1528 }
1529
1530 struct pipe_context *
1531 panfrost_create_context(struct pipe_screen *screen, void *priv, unsigned flags)
1532 {
1533 struct panfrost_context *ctx = rzalloc(screen, struct panfrost_context);
1534 struct pipe_context *gallium = (struct pipe_context *) ctx;
1535
1536 gallium->screen = screen;
1537
1538 gallium->destroy = panfrost_destroy;
1539
1540 gallium->set_framebuffer_state = panfrost_set_framebuffer_state;
1541
1542 gallium->flush = panfrost_flush;
1543 gallium->clear = panfrost_clear;
1544 gallium->draw_vbo = panfrost_draw_vbo;
1545
1546 gallium->set_vertex_buffers = panfrost_set_vertex_buffers;
1547 gallium->set_constant_buffer = panfrost_set_constant_buffer;
1548 gallium->set_shader_buffers = panfrost_set_shader_buffers;
1549
1550 gallium->set_stencil_ref = panfrost_set_stencil_ref;
1551
1552 gallium->create_sampler_view = panfrost_create_sampler_view;
1553 gallium->set_sampler_views = panfrost_set_sampler_views;
1554 gallium->sampler_view_destroy = panfrost_sampler_view_destroy;
1555
1556 gallium->create_rasterizer_state = panfrost_create_rasterizer_state;
1557 gallium->bind_rasterizer_state = panfrost_bind_rasterizer_state;
1558 gallium->delete_rasterizer_state = panfrost_generic_cso_delete;
1559
1560 gallium->create_vertex_elements_state = panfrost_create_vertex_elements_state;
1561 gallium->bind_vertex_elements_state = panfrost_bind_vertex_elements_state;
1562 gallium->delete_vertex_elements_state = panfrost_generic_cso_delete;
1563
1564 gallium->create_fs_state = panfrost_create_fs_state;
1565 gallium->delete_fs_state = panfrost_delete_shader_state;
1566 gallium->bind_fs_state = panfrost_bind_fs_state;
1567
1568 gallium->create_vs_state = panfrost_create_vs_state;
1569 gallium->delete_vs_state = panfrost_delete_shader_state;
1570 gallium->bind_vs_state = panfrost_bind_vs_state;
1571
1572 gallium->create_sampler_state = panfrost_create_sampler_state;
1573 gallium->delete_sampler_state = panfrost_generic_cso_delete;
1574 gallium->bind_sampler_states = panfrost_bind_sampler_states;
1575
1576 gallium->create_depth_stencil_alpha_state = panfrost_create_depth_stencil_state;
1577 gallium->bind_depth_stencil_alpha_state = panfrost_bind_depth_stencil_state;
1578 gallium->delete_depth_stencil_alpha_state = panfrost_delete_depth_stencil_state;
1579
1580 gallium->set_sample_mask = panfrost_set_sample_mask;
1581
1582 gallium->set_clip_state = panfrost_set_clip_state;
1583 gallium->set_viewport_states = panfrost_set_viewport_states;
1584 gallium->set_scissor_states = panfrost_set_scissor_states;
1585 gallium->set_polygon_stipple = panfrost_set_polygon_stipple;
1586 gallium->set_active_query_state = panfrost_set_active_query_state;
1587
1588 gallium->create_query = panfrost_create_query;
1589 gallium->destroy_query = panfrost_destroy_query;
1590 gallium->begin_query = panfrost_begin_query;
1591 gallium->end_query = panfrost_end_query;
1592 gallium->get_query_result = panfrost_get_query_result;
1593
1594 gallium->create_stream_output_target = panfrost_create_stream_output_target;
1595 gallium->stream_output_target_destroy = panfrost_stream_output_target_destroy;
1596 gallium->set_stream_output_targets = panfrost_set_stream_output_targets;
1597
1598 panfrost_resource_context_init(gallium);
1599 panfrost_blend_context_init(gallium);
1600 panfrost_compute_context_init(gallium);
1601
1602 /* XXX: leaks */
1603 gallium->stream_uploader = u_upload_create_default(gallium);
1604 gallium->const_uploader = gallium->stream_uploader;
1605 assert(gallium->stream_uploader);
1606
1607 /* Midgard supports ES modes, plus QUADS/QUAD_STRIPS/POLYGON */
1608 ctx->draw_modes = (1 << (PIPE_PRIM_POLYGON + 1)) - 1;
1609
1610 ctx->primconvert = util_primconvert_create(gallium, ctx->draw_modes);
1611
1612 ctx->blitter = util_blitter_create(gallium);
1613 ctx->blitter_wallpaper = util_blitter_create(gallium);
1614
1615 assert(ctx->blitter);
1616 assert(ctx->blitter_wallpaper);
1617
1618 /* Prepare for render! */
1619
1620 panfrost_batch_init(ctx);
1621 panfrost_emit_vertex_payload(ctx);
1622 panfrost_invalidate_frame(ctx);
1623
1624 return gallium;
1625 }