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