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