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