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