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