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