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