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