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