blob: bb4ba3b8f458aa76211d754c27c1a60ae79d9bbe [file] [log] [blame]
Alyssa Rosenzweig7da251f2019-02-05 04:32:27 +00001/*
2 * © Copyright 2018 Alyssa Rosenzweig
Alyssa Rosenzweig5ff79732019-08-06 16:43:28 -07003 * Copyright © 2014-2017 Broadcom
Alyssa Rosenzweigf714eab2019-08-07 10:11:28 -07004 * Copyright (C) 2017 Intel Corporation
Alyssa Rosenzweig7da251f2019-02-05 04:32:27 +00005 *
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
Boris Brezillon154cb722019-09-14 09:58:55 +020030#include "pan_bo.h"
Alyssa Rosenzweig7da251f2019-02-05 04:32:27 +000031#include "pan_context.h"
Vasily Khoruzhick53d6bb92020-03-03 21:31:51 -080032#include "pan_minmax_cache.h"
Tomeu Vizoso6887ff42019-11-28 10:21:06 +010033#include "panfrost-quirks.h"
Alyssa Rosenzweig7da251f2019-02-05 04:32:27 +000034
35#include "util/macros.h"
Eric Anholt882ca6d2019-06-27 15:05:31 -070036#include "util/format/u_format.h"
Alyssa Rosenzweig7da251f2019-02-05 04:32:27 +000037#include "util/u_inlines.h"
38#include "util/u_upload_mgr.h"
39#include "util/u_memory.h"
Alyssa Rosenzweig89b02bf2019-04-13 00:10:20 +000040#include "util/u_vbuf.h"
Alyssa Rosenzweig7da251f2019-02-05 04:32:27 +000041#include "util/half_float.h"
Alyssa Rosenzweige008d4f2019-04-14 22:42:44 +000042#include "util/u_helpers.h"
Eric Anholt882ca6d2019-06-27 15:05:31 -070043#include "util/format/u_format.h"
Alyssa Rosenzweig72fc06d2019-08-08 07:10:24 -070044#include "util/u_prim.h"
Alyssa Rosenzweig7f548122019-06-26 15:59:29 -070045#include "util/u_prim_restart.h"
Alyssa Rosenzweig7da251f2019-02-05 04:32:27 +000046#include "indices/u_primconvert.h"
47#include "tgsi/tgsi_parse.h"
Alyssa Rosenzweig46479992019-07-31 15:49:13 -070048#include "tgsi/tgsi_from_mesa.h"
Alyssa Rosenzweig31f5a432019-05-02 02:27:04 +000049#include "util/u_math.h"
Alyssa Rosenzweig7da251f2019-02-05 04:32:27 +000050
51#include "pan_screen.h"
52#include "pan_blending.h"
53#include "pan_blend_shaders.h"
Boris Brezillona72bab12020-03-05 09:30:58 +010054#include "pan_cmdstream.h"
Tomeu Vizoso97f2d042019-03-08 15:24:57 +010055#include "pan_util.h"
Alyssa Rosenzweig1c62b552020-08-05 16:16:00 -040056#include "decode.h"
Icecream95d16d1362020-07-07 15:06:08 +120057#include "util/pan_lower_framebuffer.h"
Alyssa Rosenzweig7da251f2019-02-05 04:32:27 +000058
Alyssa Rosenzweigb0e915b2019-12-09 11:00:42 -050059struct midgard_tiler_descriptor
Boris Brezillonaa851a62019-09-01 10:30:39 +020060panfrost_emit_midg_tiler(struct panfrost_batch *batch, unsigned vertex_count)
Alyssa Rosenzweig7da251f2019-02-05 04:32:27 +000061{
Alyssa Rosenzweigca8c6252020-03-23 18:44:21 -040062 struct panfrost_device *device = pan_device(batch->ctx->base.screen);
63 bool hierarchy = !(device->quirks & MIDGARD_NO_HIER_TILING);
Vinson Leede2e5f62019-11-27 23:37:00 -080064 struct midgard_tiler_descriptor t = {0};
Boris Brezillonaa851a62019-09-01 10:30:39 +020065 unsigned height = batch->key.height;
66 unsigned width = batch->key.width;
Alyssa Rosenzweig7da251f2019-02-05 04:32:27 +000067
Alyssa Rosenzweig31fc52a2019-07-10 07:22:19 -070068 t.hierarchy_mask =
Alyssa Rosenzweig9fb09042019-11-27 08:31:16 -050069 panfrost_choose_hierarchy_mask(width, height, vertex_count, hierarchy);
Alyssa Rosenzweig7da251f2019-02-05 04:32:27 +000070
Alyssa Rosenzweig31fc52a2019-07-10 07:22:19 -070071 /* Compute the polygon header size and use that to offset the body */
72
73 unsigned header_size = panfrost_tiler_header_size(
Alyssa Rosenzweig9fb09042019-11-27 08:31:16 -050074 width, height, t.hierarchy_mask, hierarchy);
Alyssa Rosenzweig31fc52a2019-07-10 07:22:19 -070075
Alyssa Rosenzweigf5c29342019-08-19 14:30:53 -070076 t.polygon_list_size = panfrost_tiler_full_size(
Alyssa Rosenzweig9fb09042019-11-27 08:31:16 -050077 width, height, t.hierarchy_mask, hierarchy);
Alyssa Rosenzweig31fc52a2019-07-10 07:22:19 -070078
79 /* Sanity check */
80
Alyssa Rosenzweig9fb09042019-11-27 08:31:16 -050081 if (vertex_count) {
Boris Brezillon1e483a82019-09-14 19:18:51 +020082 struct panfrost_bo *tiler_heap;
83
84 tiler_heap = panfrost_batch_get_tiler_heap(batch);
Boris Brezillon2c526992019-09-05 21:41:26 +020085 t.polygon_list = panfrost_batch_get_polygon_list(batch,
86 header_size +
87 t.polygon_list_size);
Alyssa Rosenzweigf5c29342019-08-19 14:30:53 -070088
Alyssa Rosenzweig31fc52a2019-07-10 07:22:19 -070089
90 /* Allow the entire tiler heap */
Boris Brezillon1e483a82019-09-14 19:18:51 +020091 t.heap_start = tiler_heap->gpu;
92 t.heap_end = tiler_heap->gpu + tiler_heap->size;
Alyssa Rosenzweig31fc52a2019-07-10 07:22:19 -070093 } else {
Boris Brezillon1e483a82019-09-14 19:18:51 +020094 struct panfrost_bo *tiler_dummy;
95
96 tiler_dummy = panfrost_batch_get_tiler_dummy(batch);
Alyssa Rosenzweig9fb09042019-11-27 08:31:16 -050097 header_size = MALI_TILER_MINIMUM_HEADER_SIZE;
Boris Brezillon1e483a82019-09-14 19:18:51 +020098
Alyssa Rosenzweig31fc52a2019-07-10 07:22:19 -070099 /* The tiler is disabled, so don't allow the tiler heap */
Boris Brezillon1e483a82019-09-14 19:18:51 +0200100 t.heap_start = tiler_dummy->gpu;
Alyssa Rosenzweig31fc52a2019-07-10 07:22:19 -0700101 t.heap_end = t.heap_start;
102
103 /* Use a dummy polygon list */
Boris Brezillon1e483a82019-09-14 19:18:51 +0200104 t.polygon_list = tiler_dummy->gpu;
Alyssa Rosenzweig31fc52a2019-07-10 07:22:19 -0700105
Alyssa Rosenzweig897110a2019-08-19 14:47:50 -0700106 /* Disable the tiler */
Alyssa Rosenzweig9fb09042019-11-27 08:31:16 -0500107 if (hierarchy)
108 t.hierarchy_mask |= MALI_TILER_DISABLED;
109 else {
110 t.hierarchy_mask = MALI_TILER_USER;
111 t.polygon_list_size = MALI_TILER_MINIMUM_HEADER_SIZE + 4;
Tomeu Vizoso6469c1a2019-10-29 15:42:03 +0100112
Alyssa Rosenzweigadf716d2019-12-05 09:06:53 -0500113 /* We don't have a WRITE_VALUE job, so write the polygon list manually */
Tomeu Vizoso6469c1a2019-10-29 15:42:03 +0100114 uint32_t *polygon_list_body = (uint32_t *) (tiler_dummy->cpu + header_size);
115 polygon_list_body[0] = 0xa0000000; /* TODO: Just that? */
116 }
Alyssa Rosenzweig31fc52a2019-07-10 07:22:19 -0700117 }
118
119 t.polygon_list_body =
120 t.polygon_list + header_size;
121
Alyssa Rosenzweig31fc52a2019-07-10 07:22:19 -0700122 return t;
Alyssa Rosenzweig7da251f2019-02-05 04:32:27 +0000123}
Alyssa Rosenzweig7da251f2019-02-05 04:32:27 +0000124
Alyssa Rosenzweig7da251f2019-02-05 04:32:27 +0000125static void
126panfrost_clear(
127 struct pipe_context *pipe,
128 unsigned buffers,
Mike Blumenkrantz1c8bcad2020-03-24 12:02:51 -0400129 const struct pipe_scissor_state *scissor_state,
Alyssa Rosenzweig7da251f2019-02-05 04:32:27 +0000130 const union pipe_color_union *color,
131 double depth, unsigned stencil)
132{
133 struct panfrost_context *ctx = pan_context(pipe);
Boris Brezillonc138ca82019-09-19 15:52:02 +0200134
135 /* TODO: panfrost_get_fresh_batch_for_fbo() instantiates a new batch if
136 * the existing batch targeting this FBO has draws. We could probably
137 * avoid that by replacing plain clears by quad-draws with a specific
138 * color/depth/stencil value, thus avoiding the generation of extra
Alyssa Rosenzweigadf716d2019-12-05 09:06:53 -0500139 * fragment jobs.
Boris Brezillonc138ca82019-09-19 15:52:02 +0200140 */
141 struct panfrost_batch *batch = panfrost_get_fresh_batch_for_fbo(ctx);
Alyssa Rosenzweig7da251f2019-02-05 04:32:27 +0000142
Boris Brezillon0eec73a2019-09-14 18:40:23 +0200143 panfrost_batch_add_fbo_bos(batch);
Boris Brezillon12d8a172019-09-05 21:41:28 +0200144 panfrost_batch_clear(batch, buffers, color, depth, stencil);
Alyssa Rosenzweig7da251f2019-02-05 04:32:27 +0000145}
146
Alyssa Rosenzweig7da251f2019-02-05 04:32:27 +0000147/* Reset per-frame context, called on context initialisation as well as after
148 * flushing a frame */
149
Boris Brezillon6ddfd372019-09-05 20:47:45 +0200150void
Alyssa Rosenzweig7da251f2019-02-05 04:32:27 +0000151panfrost_invalidate_frame(struct panfrost_context *ctx)
152{
Alyssa Rosenzweig72fc06d2019-08-08 07:10:24 -0700153 /* TODO: When does this need to be handled? */
154 ctx->active_queries = true;
Alyssa Rosenzweig7da251f2019-02-05 04:32:27 +0000155}
156
Boris Brezillona0402f72020-03-05 17:24:39 +0100157bool
Alyssa Rosenzweig89b02bf2019-04-13 00:10:20 +0000158panfrost_writes_point_size(struct panfrost_context *ctx)
159{
Alyssa Rosenzweigac6aa932019-07-31 14:13:30 -0700160 assert(ctx->shader[PIPE_SHADER_VERTEX]);
Boris Brezillonb28f4bb2020-03-05 10:46:39 +0100161 struct panfrost_shader_state *vs = panfrost_get_shader_state(ctx, PIPE_SHADER_VERTEX);
Alyssa Rosenzweig89b02bf2019-04-13 00:10:20 +0000162
Boris Brezillon093da772020-03-06 11:43:38 +0100163 return vs->writes_point_size && ctx->active_prim == PIPE_PRIM_POINTS;
Alyssa Rosenzweig89b02bf2019-04-13 00:10:20 +0000164}
165
Boris Brezillon046c1542020-03-05 21:48:09 +0100166void
Boris Brezillondcc0b1f2020-03-05 19:32:01 +0100167panfrost_vertex_state_upd_attr_offs(struct panfrost_context *ctx,
Tomeu Vizoso7b10d4e2020-04-08 10:55:28 +0200168 struct mali_vertex_tiler_postfix *vertex_postfix)
Alyssa Rosenzweig31d9caa2019-04-15 04:08:46 +0000169{
Boris Brezillondcc0b1f2020-03-05 19:32:01 +0100170 if (!ctx->vertex)
171 return;
172
Alyssa Rosenzweig31d9caa2019-04-15 04:08:46 +0000173 struct panfrost_vertex_state *so = ctx->vertex;
174
Alyssa Rosenzweig31d9caa2019-04-15 04:08:46 +0000175 /* Fixup offsets for the second pass. Recall that the hardware
176 * calculates attribute addresses as:
177 *
178 * addr = base + (stride * vtx) + src_offset;
179 *
180 * However, on Mali, base must be aligned to 64-bytes, so we
181 * instead let:
182 *
183 * base' = base & ~63 = base - (base & 63)
Alyssa Rosenzweiga2d0ea92019-07-10 10:10:31 -0700184 *
Alyssa Rosenzweig31d9caa2019-04-15 04:08:46 +0000185 * To compensate when using base' (see emit_vertex_data), we have
186 * to adjust src_offset by the masked off piece:
187 *
188 * addr' = base' + (stride * vtx) + (src_offset + (base & 63))
189 * = base - (base & 63) + (stride * vtx) + src_offset + (base & 63)
190 * = base + (stride * vtx) + src_offset
191 * = addr;
192 *
193 * QED.
194 */
195
Tomeu Vizoso7b10d4e2020-04-08 10:55:28 +0200196 unsigned start = vertex_postfix->offset_start;
Alyssa Rosenzweig8d747492019-06-27 14:13:10 -0700197
Alyssa Rosenzweig31d9caa2019-04-15 04:08:46 +0000198 for (unsigned i = 0; i < so->num_elements; ++i) {
199 unsigned vbi = so->pipe[i].vertex_buffer_index;
Alyssa Rosenzweig8d747492019-06-27 14:13:10 -0700200 struct pipe_vertex_buffer *buf = &ctx->vertex_buffers[vbi];
Alyssa Rosenzweig31d9caa2019-04-15 04:08:46 +0000201
Alyssa Rosenzweig027944c2020-02-16 16:41:29 -0500202 /* Adjust by the masked off bits of the offset. Make sure we
203 * read src_offset from so->hw (which is not GPU visible)
204 * rather than target (which is) due to caching effects */
205
Boris Brezillon128820b2020-03-05 19:22:26 +0100206 unsigned src_offset = so->pipe[i].src_offset;
Boris Brezillonb692ab02020-03-05 19:26:03 +0100207
208 /* BOs aligned to 4k so guaranteed aligned to 64 */
209 src_offset += (buf->buffer_offset & 63);
Alyssa Rosenzweig8d747492019-06-27 14:13:10 -0700210
211 /* Also, somewhat obscurely per-instance data needs to be
212 * offset in response to a delayed start in an indexed draw */
213
Alyssa Rosenzweigbe691ca2019-12-19 14:00:24 -0500214 if (so->pipe[i].instance_divisor && ctx->instance_count > 1 && start)
Alyssa Rosenzweig027944c2020-02-16 16:41:29 -0500215 src_offset -= buf->stride * start;
216
Boris Brezillon575f62e2020-03-05 19:48:30 +0100217 so->hw[i].src_offset = src_offset;
Alyssa Rosenzweig31d9caa2019-04-15 04:08:46 +0000218 }
Boris Brezillondcc0b1f2020-03-05 19:32:01 +0100219}
220
Alyssa Rosenzweig5d60be42019-06-20 16:16:07 -0700221/* Compute number of UBOs active (more specifically, compute the highest UBO
Alyssa Rosenzweig4c6d7512019-06-20 16:21:48 -0700222 * number addressable -- if there are gaps, include them in the count anyway).
223 * We always include UBO #0 in the count, since we *need* uniforms enabled for
224 * sysvals. */
Alyssa Rosenzweig5d60be42019-06-20 16:16:07 -0700225
Boris Brezillon0b735a22020-03-05 09:46:42 +0100226unsigned
Alyssa Rosenzweig5d60be42019-06-20 16:16:07 -0700227panfrost_ubo_count(struct panfrost_context *ctx, enum pipe_shader_type stage)
228{
Alyssa Rosenzweig4c6d7512019-06-20 16:21:48 -0700229 unsigned mask = ctx->constant_buffer[stage].enabled_mask | 1;
Alyssa Rosenzweig5d60be42019-06-20 16:16:07 -0700230 return 32 - __builtin_clz(mask);
231}
232
Alyssa Rosenzweig7da251f2019-02-05 04:32:27 +0000233/* The entire frame is in memory -- send it off to the kernel! */
234
Alyssa Rosenzweig7da251f2019-02-05 04:32:27 +0000235void
236panfrost_flush(
237 struct pipe_context *pipe,
238 struct pipe_fence_handle **fence,
239 unsigned flags)
240{
241 struct panfrost_context *ctx = pan_context(pipe);
Alyssa Rosenzweiged1910d2020-07-07 16:15:45 -0400242 struct panfrost_device *dev = pan_device(pipe->screen);
Alyssa Rosenzweig64d6f562020-07-20 13:34:42 -0400243 uint32_t syncobj = 0;
Boris Brezillonb5d8f9b2019-09-15 18:23:10 +0200244
Alyssa Rosenzweig64d6f562020-07-20 13:34:42 -0400245 if (fence)
246 drmSyncobjCreate(dev->fd, 0, &syncobj);
Alyssa Rosenzweig7da251f2019-02-05 04:32:27 +0000247
Boris Brezillona45984b2019-09-15 19:15:16 +0200248 /* Submit all pending jobs */
Alyssa Rosenzweig64d6f562020-07-20 13:34:42 -0400249 panfrost_flush_all_batches(ctx, syncobj);
Boris Brezillon2fc91a12019-09-05 19:14:25 +0200250
251 if (fence) {
Alyssa Rosenzweig64d6f562020-07-20 13:34:42 -0400252 struct panfrost_fence *f = panfrost_fence_create(ctx, syncobj);
Boris Brezillon2fc91a12019-09-05 19:14:25 +0200253 pipe->screen->fence_reference(pipe->screen, fence, NULL);
254 *fence = (struct pipe_fence_handle *)f;
255 }
Icecream95cf2c5a52020-01-23 10:32:18 +1300256
Alyssa Rosenzweiged1910d2020-07-07 16:15:45 -0400257 if (dev->debug & PAN_DBG_TRACE)
Icecream95cf2c5a52020-01-23 10:32:18 +1300258 pandecode_next_frame();
Alyssa Rosenzweig7da251f2019-02-05 04:32:27 +0000259}
260
Icecream95391ad722020-07-10 11:19:07 +1200261static void
262panfrost_texture_barrier(struct pipe_context *pipe, unsigned flags)
263{
264 struct panfrost_context *ctx = pan_context(pipe);
Alyssa Rosenzweig64d6f562020-07-20 13:34:42 -0400265 panfrost_flush_all_batches(ctx, 0);
Icecream95391ad722020-07-10 11:19:07 +1200266}
267
Alyssa Rosenzweig32dbc802020-08-05 18:44:36 -0400268#define DEFINE_CASE(c) case PIPE_PRIM_##c: return MALI_DRAW_MODE_##c;
Alyssa Rosenzweig7da251f2019-02-05 04:32:27 +0000269
270static int
271g2m_draw_mode(enum pipe_prim_type mode)
272{
273 switch (mode) {
274 DEFINE_CASE(POINTS);
275 DEFINE_CASE(LINES);
276 DEFINE_CASE(LINE_LOOP);
277 DEFINE_CASE(LINE_STRIP);
278 DEFINE_CASE(TRIANGLES);
279 DEFINE_CASE(TRIANGLE_STRIP);
280 DEFINE_CASE(TRIANGLE_FAN);
281 DEFINE_CASE(QUADS);
282 DEFINE_CASE(QUAD_STRIP);
283 DEFINE_CASE(POLYGON);
284
285 default:
Alyssa Rosenzweigc65271c2019-05-16 23:42:33 +0000286 unreachable("Invalid draw mode");
Alyssa Rosenzweig7da251f2019-02-05 04:32:27 +0000287 }
288}
289
290#undef DEFINE_CASE
291
Alyssa Rosenzweig9865b792019-06-17 09:26:34 -0700292static bool
293panfrost_scissor_culls_everything(struct panfrost_context *ctx)
294{
295 const struct pipe_scissor_state *ss = &ctx->scissor;
296
297 /* Check if we're scissoring at all */
298
Boris Brezillon443e5302019-06-26 11:16:31 +0200299 if (!(ctx->rasterizer && ctx->rasterizer->base.scissor))
Alyssa Rosenzweig9865b792019-06-17 09:26:34 -0700300 return false;
301
Alyssa Rosenzweig124f6b52019-06-24 14:13:20 -0700302 return (ss->minx == ss->maxx) || (ss->miny == ss->maxy);
Alyssa Rosenzweig9865b792019-06-17 09:26:34 -0700303}
304
Alyssa Rosenzweig72fc06d2019-08-08 07:10:24 -0700305/* Count generated primitives (when there is no geom/tess shaders) for
306 * transform feedback */
307
308static void
309panfrost_statistics_record(
310 struct panfrost_context *ctx,
311 const struct pipe_draw_info *info)
312{
313 if (!ctx->active_queries)
314 return;
315
316 uint32_t prims = u_prims_for_vertices(info->mode, info->count);
317 ctx->prims_generated += prims;
318
Alyssa Rosenzweig42f0aae2019-08-30 17:37:22 -0700319 if (!ctx->streamout.num_targets)
Alyssa Rosenzweig72fc06d2019-08-08 07:10:24 -0700320 return;
321
322 ctx->tf_prims_generated += prims;
323}
324
Alyssa Rosenzweig7da251f2019-02-05 04:32:27 +0000325static void
Boris Brezillon13881a42020-03-05 21:55:01 +0100326panfrost_update_streamout_offsets(struct panfrost_context *ctx)
327{
328 for (unsigned i = 0; i < ctx->streamout.num_targets; ++i) {
329 unsigned count;
330
331 count = u_stream_outputs_for_vertices(ctx->active_prim,
332 ctx->vertex_count);
333 ctx->streamout.offsets[i] += count;
334 }
335}
336
337static void
Alyssa Rosenzweig7da251f2019-02-05 04:32:27 +0000338panfrost_draw_vbo(
339 struct pipe_context *pipe,
340 const struct pipe_draw_info *info)
341{
342 struct panfrost_context *ctx = pan_context(pipe);
343
Alyssa Rosenzweig9865b792019-06-17 09:26:34 -0700344 /* First of all, check the scissor to see if anything is drawn at all.
345 * If it's not, we drop the draw (mostly a conformance issue;
346 * well-behaved apps shouldn't hit this) */
347
348 if (panfrost_scissor_culls_everything(ctx))
349 return;
350
Alyssa Rosenzweig7da251f2019-02-05 04:32:27 +0000351 int mode = info->mode;
352
Alyssa Rosenzweig7f548122019-06-26 15:59:29 -0700353 /* Fallback unsupported restart index */
354 unsigned primitive_index = (1 << (info->index_size * 8)) - 1;
355
356 if (info->primitive_restart && info->index_size
Alyssa Rosenzweiga2d0ea92019-07-10 10:10:31 -0700357 && info->restart_index != primitive_index) {
Alyssa Rosenzweig7f548122019-06-26 15:59:29 -0700358 util_draw_vbo_without_prim_restart(pipe, info);
359 return;
360 }
361
Alyssa Rosenzweig85e2bb52019-02-08 02:28:12 +0000362 /* Fallback for unsupported modes */
Alyssa Rosenzweig7da251f2019-02-05 04:32:27 +0000363
Alyssa Rosenzweig27b62642019-08-21 09:40:11 -0700364 assert(ctx->rasterizer != NULL);
365
Alyssa Rosenzweig7c02c4f2019-03-15 02:13:34 +0000366 if (!(ctx->draw_modes & (1 << mode))) {
Alyssa Rosenzweig27b62642019-08-21 09:40:11 -0700367 if (mode == PIPE_PRIM_QUADS && info->count == 4 && !ctx->rasterizer->base.flatshade) {
Alyssa Rosenzweig7da251f2019-02-05 04:32:27 +0000368 mode = PIPE_PRIM_TRIANGLE_FAN;
369 } else {
370 if (info->count < 4) {
371 /* Degenerate case? */
372 return;
373 }
374
375 util_primconvert_save_rasterizer_state(ctx->primconvert, &ctx->rasterizer->base);
376 util_primconvert_draw_vbo(ctx->primconvert, info);
377 return;
378 }
379 }
Alyssa Rosenzweig7da251f2019-02-05 04:32:27 +0000380
Alyssa Rosenzweig59c96232019-02-25 05:32:16 +0000381 /* Now that we have a guaranteed terminating path, find the job.
382 * Assignment commented out to prevent unused warning */
383
Boris Brezillon251e6852020-03-06 08:57:31 +0100384 struct panfrost_batch *batch = panfrost_get_batch_for_fbo(ctx);
385
386 panfrost_batch_add_fbo_bos(batch);
387 panfrost_batch_set_requirements(batch);
Alyssa Rosenzweig59c96232019-02-25 05:32:16 +0000388
Rohan Garg16edd562019-07-17 18:50:13 +0200389 /* Take into account a negative bias */
390 ctx->vertex_count = info->count + abs(info->index_bias);
Alyssa Rosenzweig8d747492019-06-27 14:13:10 -0700391 ctx->instance_count = info->instance_count;
Alyssa Rosenzweig7c295882019-08-08 08:16:09 -0700392 ctx->active_prim = info->mode;
Alyssa Rosenzweig7da251f2019-02-05 04:32:27 +0000393
Tomeu Vizoso7b10d4e2020-04-08 10:55:28 +0200394 struct mali_vertex_tiler_prefix vertex_prefix, tiler_prefix;
395 struct mali_vertex_tiler_postfix vertex_postfix, tiler_postfix;
396 union midgard_primitive_size primitive_size;
Boris Brezillon5d9995e2020-03-06 08:02:14 +0100397 unsigned vertex_count;
Alyssa Rosenzweig7da251f2019-02-05 04:32:27 +0000398
Tomeu Vizoso7b10d4e2020-04-08 10:55:28 +0200399 panfrost_vt_init(ctx, PIPE_SHADER_VERTEX, &vertex_prefix, &vertex_postfix);
400 panfrost_vt_init(ctx, PIPE_SHADER_FRAGMENT, &tiler_prefix, &tiler_postfix);
Boris Brezillond66ef692020-03-06 11:31:06 +0100401
Tomeu Vizoso7b10d4e2020-04-08 10:55:28 +0200402 panfrost_vt_set_draw_info(ctx, info, g2m_draw_mode(mode),
403 &vertex_postfix, &tiler_prefix,
404 &tiler_postfix, &vertex_count,
405 &ctx->padded_count);
Alyssa Rosenzweig0e4c3212019-03-31 04:26:48 +0000406
Alyssa Rosenzweig72fc06d2019-08-08 07:10:24 -0700407 panfrost_statistics_record(ctx, info);
408
Alyssa Rosenzweigc9b164f2019-06-27 08:29:06 -0700409 /* Dispatch "compute jobs" for the vertex/tiler pair as (1,
410 * vertex_count, 1) */
411
Tomeu Vizoso7b10d4e2020-04-08 10:55:28 +0200412 panfrost_pack_work_groups_fused(&vertex_prefix, &tiler_prefix,
Boris Brezillonb1a6a152020-03-06 11:46:39 +0100413 1, vertex_count, info->instance_count,
414 1, 1, 1);
Alyssa Rosenzweigc9b164f2019-06-27 08:29:06 -0700415
Boris Brezillon251e6852020-03-06 08:57:31 +0100416 /* Emit all sort of descriptors. */
Tomeu Vizoso7b10d4e2020-04-08 10:55:28 +0200417 panfrost_emit_vertex_data(batch, &vertex_postfix);
Boris Brezillon836686d2020-03-06 09:45:31 +0100418 panfrost_emit_varying_descriptor(batch,
Boris Brezillon251e6852020-03-06 08:57:31 +0100419 ctx->padded_count *
Boris Brezillon836686d2020-03-06 09:45:31 +0100420 ctx->instance_count,
Tomeu Vizoso7b10d4e2020-04-08 10:55:28 +0200421 &vertex_postfix, &tiler_postfix,
422 &primitive_size);
423 panfrost_emit_shader_meta(batch, PIPE_SHADER_VERTEX, &vertex_postfix);
424 panfrost_emit_shader_meta(batch, PIPE_SHADER_FRAGMENT, &tiler_postfix);
425 panfrost_emit_vertex_attr_meta(batch, &vertex_postfix);
426 panfrost_emit_sampler_descriptors(batch, PIPE_SHADER_VERTEX, &vertex_postfix);
427 panfrost_emit_sampler_descriptors(batch, PIPE_SHADER_FRAGMENT, &tiler_postfix);
428 panfrost_emit_texture_descriptors(batch, PIPE_SHADER_VERTEX, &vertex_postfix);
429 panfrost_emit_texture_descriptors(batch, PIPE_SHADER_FRAGMENT, &tiler_postfix);
430 panfrost_emit_const_buf(batch, PIPE_SHADER_VERTEX, &vertex_postfix);
431 panfrost_emit_const_buf(batch, PIPE_SHADER_FRAGMENT, &tiler_postfix);
432 panfrost_emit_viewport(batch, &tiler_postfix);
433
434 panfrost_vt_update_primitive_size(ctx, &tiler_prefix, &primitive_size);
Boris Brezillon251e6852020-03-06 08:57:31 +0100435
Alyssa Rosenzweig7da251f2019-02-05 04:32:27 +0000436 /* Fire off the draw itself */
Tomeu Vizoso7b10d4e2020-04-08 10:55:28 +0200437 panfrost_emit_vertex_tiler_jobs(batch, &vertex_prefix, &vertex_postfix,
438 &tiler_prefix, &tiler_postfix,
439 &primitive_size);
Boris Brezillon251e6852020-03-06 08:57:31 +0100440
441 /* Adjust the batch stack size based on the new shader stack sizes. */
442 panfrost_batch_adjust_stack_size(batch);
Alyssa Rosenzweig7c295882019-08-08 08:16:09 -0700443
444 /* Increment transform feedback offsets */
Boris Brezillon13881a42020-03-05 21:55:01 +0100445 panfrost_update_streamout_offsets(ctx);
Alyssa Rosenzweig7da251f2019-02-05 04:32:27 +0000446}
447
448/* CSO state */
449
450static void
451panfrost_generic_cso_delete(struct pipe_context *pctx, void *hwcso)
452{
453 free(hwcso);
454}
455
Alyssa Rosenzweig7da251f2019-02-05 04:32:27 +0000456static void *
457panfrost_create_rasterizer_state(
458 struct pipe_context *pctx,
459 const struct pipe_rasterizer_state *cso)
460{
461 struct panfrost_rasterizer *so = CALLOC_STRUCT(panfrost_rasterizer);
462
463 so->base = *cso;
464
Alyssa Rosenzweig7da251f2019-02-05 04:32:27 +0000465 return so;
466}
467
468static void
469panfrost_bind_rasterizer_state(
470 struct pipe_context *pctx,
471 void *hwcso)
472{
473 struct panfrost_context *ctx = pan_context(pctx);
Alyssa Rosenzweig7da251f2019-02-05 04:32:27 +0000474
Alyssa Rosenzweigd3160a62020-02-18 10:08:51 -0500475 ctx->rasterizer = hwcso;
476
Alyssa Rosenzweig7da251f2019-02-05 04:32:27 +0000477 if (!hwcso)
478 return;
479
Alyssa Rosenzweig83057662019-07-11 07:02:26 -0700480 /* Gauranteed with the core GL call, so don't expose ARB_polygon_offset */
481 assert(ctx->rasterizer->base.offset_clamp == 0.0);
482
Alyssa Rosenzweigbe030602019-06-24 11:01:05 -0700483 /* Point sprites are emulated */
484
Boris Brezillonb28f4bb2020-03-05 10:46:39 +0100485 struct panfrost_shader_state *variant = panfrost_get_shader_state(ctx, PIPE_SHADER_FRAGMENT);
Alyssa Rosenzweigf35f3732019-06-24 11:53:58 -0700486
487 if (ctx->rasterizer->base.sprite_coord_enable || (variant && variant->point_sprite_mask))
Alyssa Rosenzweigac6aa932019-07-31 14:13:30 -0700488 ctx->base.bind_fs_state(&ctx->base, ctx->shader[PIPE_SHADER_FRAGMENT]);
Alyssa Rosenzweig7da251f2019-02-05 04:32:27 +0000489}
490
491static void *
492panfrost_create_vertex_elements_state(
493 struct pipe_context *pctx,
494 unsigned num_elements,
495 const struct pipe_vertex_element *elements)
496{
Alyssa Rosenzweig7da251f2019-02-05 04:32:27 +0000497 struct panfrost_vertex_state *so = CALLOC_STRUCT(panfrost_vertex_state);
Tomeu Vizosoc4400b02020-05-01 07:36:31 +0200498 struct panfrost_device *dev = pan_device(pctx->screen);
Alyssa Rosenzweig7da251f2019-02-05 04:32:27 +0000499
500 so->num_elements = num_elements;
501 memcpy(so->pipe, elements, sizeof(*elements) * num_elements);
502
Alyssa Rosenzweig7da251f2019-02-05 04:32:27 +0000503 for (int i = 0; i < num_elements; ++i) {
Alyssa Rosenzweig8d747492019-06-27 14:13:10 -0700504 so->hw[i].index = i;
Alyssa Rosenzweig7da251f2019-02-05 04:32:27 +0000505
506 enum pipe_format fmt = elements[i].src_format;
507 const struct util_format_description *desc = util_format_description(fmt);
508 so->hw[i].unknown1 = 0x2;
Tomeu Vizosoc4400b02020-05-01 07:36:31 +0200509
510 if (dev->quirks & HAS_SWIZZLES)
511 so->hw[i].swizzle = panfrost_translate_swizzle_4(desc->swizzle);
512 else
513 so->hw[i].swizzle = panfrost_bifrost_swizzle(desc->nr_channels);
Alyssa Rosenzweig7da251f2019-02-05 04:32:27 +0000514
Alyssa Rosenzweig861e7dc2020-05-15 18:43:41 -0400515 enum mali_format hw_format = panfrost_pipe_format_table[desc->format].hw;
516 so->hw[i].format = hw_format;
517 assert(hw_format);
Alyssa Rosenzweig7da251f2019-02-05 04:32:27 +0000518 }
519
Boris Brezillon4a2ee612020-03-05 19:40:15 +0100520 /* Let's also prepare vertex builtins */
521 so->hw[PAN_VERTEX_ID].format = MALI_R32UI;
Tomeu Vizosoc4400b02020-05-01 07:36:31 +0200522 if (dev->quirks & HAS_SWIZZLES)
523 so->hw[PAN_VERTEX_ID].swizzle = panfrost_get_default_swizzle(1);
524 else
525 so->hw[PAN_VERTEX_ID].swizzle = panfrost_bifrost_swizzle(1);
526
Boris Brezillon4a2ee612020-03-05 19:40:15 +0100527 so->hw[PAN_INSTANCE_ID].format = MALI_R32UI;
Tomeu Vizosoc4400b02020-05-01 07:36:31 +0200528 if (dev->quirks & HAS_SWIZZLES)
529 so->hw[PAN_INSTANCE_ID].swizzle = panfrost_get_default_swizzle(1);
530 else
531 so->hw[PAN_INSTANCE_ID].swizzle = panfrost_bifrost_swizzle(1);
Boris Brezillon4a2ee612020-03-05 19:40:15 +0100532
Alyssa Rosenzweig7da251f2019-02-05 04:32:27 +0000533 return so;
534}
535
536static void
537panfrost_bind_vertex_elements_state(
538 struct pipe_context *pctx,
539 void *hwcso)
540{
541 struct panfrost_context *ctx = pan_context(pctx);
Alyssa Rosenzweig7da251f2019-02-05 04:32:27 +0000542 ctx->vertex = hwcso;
Alyssa Rosenzweig7da251f2019-02-05 04:32:27 +0000543}
544
Alyssa Rosenzweig7da251f2019-02-05 04:32:27 +0000545static void *
546panfrost_create_shader_state(
547 struct pipe_context *pctx,
Alyssa Rosenzweig271726e2019-12-13 15:13:02 -0500548 const struct pipe_shader_state *cso,
549 enum pipe_shader_type stage)
Alyssa Rosenzweig7da251f2019-02-05 04:32:27 +0000550{
551 struct panfrost_shader_variants *so = CALLOC_STRUCT(panfrost_shader_variants);
Alyssa Rosenzweiged1910d2020-07-07 16:15:45 -0400552 struct panfrost_device *dev = pan_device(pctx->screen);
Alyssa Rosenzweig7da251f2019-02-05 04:32:27 +0000553 so->base = *cso;
554
555 /* Token deep copy to prevent memory corruption */
556
557 if (cso->type == PIPE_SHADER_IR_TGSI)
558 so->base.tokens = tgsi_dup_tokens(so->base.tokens);
559
Alyssa Rosenzweig271726e2019-12-13 15:13:02 -0500560 /* Precompile for shader-db if we need to */
Alyssa Rosenzweiged1910d2020-07-07 16:15:45 -0400561 if (unlikely((dev->debug & PAN_DBG_PRECOMPILE) && cso->type == PIPE_SHADER_IR_NIR)) {
Alyssa Rosenzweig271726e2019-12-13 15:13:02 -0500562 struct panfrost_context *ctx = pan_context(pctx);
563
Alyssa Rosenzweig271726e2019-12-13 15:13:02 -0500564 struct panfrost_shader_state state;
565 uint64_t outputs_written;
566
Boris Brezillonb02f97c2020-03-05 16:20:18 +0100567 panfrost_shader_compile(ctx, PIPE_SHADER_IR_NIR,
568 so->base.ir.nir,
569 tgsi_processor_to_shader_stage(stage),
570 &state, &outputs_written);
Alyssa Rosenzweig271726e2019-12-13 15:13:02 -0500571 }
572
Alyssa Rosenzweig7da251f2019-02-05 04:32:27 +0000573 return so;
574}
575
576static void
577panfrost_delete_shader_state(
578 struct pipe_context *pctx,
579 void *so)
580{
Alyssa Rosenzweigacc52ff2019-02-14 04:00:19 +0000581 struct panfrost_shader_variants *cso = (struct panfrost_shader_variants *) so;
582
583 if (cso->base.type == PIPE_SHADER_IR_TGSI) {
Alyssa Rosenzweiged1910d2020-07-07 16:15:45 -0400584 /* TODO: leaks TGSI tokens! */
Alyssa Rosenzweigacc52ff2019-02-14 04:00:19 +0000585 }
586
Tomeu Vizoso950b5fc52019-08-01 16:45:50 +0200587 for (unsigned i = 0; i < cso->variant_count; ++i) {
588 struct panfrost_shader_state *shader_state = &cso->variants[i];
Boris Brezillone15ab932019-09-14 10:35:47 +0200589 panfrost_bo_unreference(shader_state->bo);
Tomeu Vizoso950b5fc52019-08-01 16:45:50 +0200590 shader_state->bo = NULL;
591 }
Icecream95d8a35012020-01-12 14:19:25 +1300592 free(cso->variants);
Tomeu Vizoso950b5fc52019-08-01 16:45:50 +0200593
Alyssa Rosenzweig7da251f2019-02-05 04:32:27 +0000594 free(so);
595}
596
597static void *
598panfrost_create_sampler_state(
599 struct pipe_context *pctx,
600 const struct pipe_sampler_state *cso)
601{
602 struct panfrost_sampler_state *so = CALLOC_STRUCT(panfrost_sampler_state);
Tomeu Vizosod3eb23a2020-04-17 14:23:39 +0200603 struct panfrost_device *device = pan_device(pctx->screen);
604
Alyssa Rosenzweig7da251f2019-02-05 04:32:27 +0000605 so->base = *cso;
606
Tomeu Vizosod3eb23a2020-04-17 14:23:39 +0200607 if (device->quirks & IS_BIFROST)
608 panfrost_sampler_desc_init_bifrost(cso, &so->bifrost_hw);
609 else
610 panfrost_sampler_desc_init(cso, &so->midgard_hw);
Alyssa Rosenzweig7da251f2019-02-05 04:32:27 +0000611
612 return so;
613}
614
615static void
616panfrost_bind_sampler_states(
617 struct pipe_context *pctx,
618 enum pipe_shader_type shader,
619 unsigned start_slot, unsigned num_sampler,
620 void **sampler)
621{
622 assert(start_slot == 0);
623
624 struct panfrost_context *ctx = pan_context(pctx);
625
626 /* XXX: Should upload, not just copy? */
627 ctx->sampler_count[shader] = num_sampler;
628 memcpy(ctx->samplers[shader], sampler, num_sampler * sizeof (void *));
Alyssa Rosenzweig7da251f2019-02-05 04:32:27 +0000629}
630
631static bool
Alyssa Rosenzweig9ab8d312019-06-14 10:12:38 -0700632panfrost_variant_matches(
Alyssa Rosenzweiga2d0ea92019-07-10 10:10:31 -0700633 struct panfrost_context *ctx,
634 struct panfrost_shader_state *variant,
635 enum pipe_shader_type type)
Alyssa Rosenzweig7da251f2019-02-05 04:32:27 +0000636{
Alyssa Rosenzweigb73b3392020-06-01 20:52:59 -0400637 struct panfrost_device *dev = pan_device(ctx->base.screen);
Alyssa Rosenzweigbe030602019-06-24 11:01:05 -0700638 struct pipe_rasterizer_state *rasterizer = &ctx->rasterizer->base;
Alyssa Rosenzweig7da251f2019-02-05 04:32:27 +0000639 struct pipe_alpha_state *alpha = &ctx->depth_stencil->alpha;
640
Alyssa Rosenzweig9ab8d312019-06-14 10:12:38 -0700641 bool is_fragment = (type == PIPE_SHADER_FRAGMENT);
642
643 if (is_fragment && (alpha->enabled || variant->alpha_state.enabled)) {
Alyssa Rosenzweig7da251f2019-02-05 04:32:27 +0000644 /* Make sure enable state is at least the same */
645 if (alpha->enabled != variant->alpha_state.enabled) {
646 return false;
647 }
648
649 /* Check that the contents of the test are the same */
650 bool same_func = alpha->func == variant->alpha_state.func;
651 bool same_ref = alpha->ref_value == variant->alpha_state.ref_value;
652
653 if (!(same_func && same_ref)) {
654 return false;
655 }
656 }
Alyssa Rosenzweigbe030602019-06-24 11:01:05 -0700657
Icecream95e6032482020-07-06 19:41:28 +1200658 if (variant->outputs_read) {
659 struct pipe_framebuffer_state *fb = &ctx->pipe_framebuffer;
660
661 unsigned i;
662 BITSET_FOREACH_SET(i, &variant->outputs_read, 8) {
663 enum pipe_format fmt = PIPE_FORMAT_R8G8B8A8_UNORM;
664
665 if ((fb->nr_cbufs > i) && fb->cbufs[i])
666 fmt = fb->cbufs[i]->format;
667
Icecream95d16d1362020-07-07 15:06:08 +1200668 const struct util_format_description *desc =
669 util_format_description(fmt);
670
671 if (pan_format_class_load(desc, dev->quirks) == PAN_FORMAT_NATIVE)
672 fmt = PIPE_FORMAT_NONE;
673
Icecream95e6032482020-07-06 19:41:28 +1200674 if (variant->rt_formats[i] != fmt)
675 return false;
676 }
677 }
678
Alyssa Rosenzweigb73b3392020-06-01 20:52:59 -0400679 /* Point sprites TODO on bifrost, always pass */
Alyssa Rosenzweigbe030602019-06-24 11:01:05 -0700680 if (is_fragment && rasterizer && (rasterizer->sprite_coord_enable |
Alyssa Rosenzweigb73b3392020-06-01 20:52:59 -0400681 variant->point_sprite_mask)
682 && !(dev->quirks & IS_BIFROST)) {
Alyssa Rosenzweigbe030602019-06-24 11:01:05 -0700683 /* Ensure the same varyings are turned to point sprites */
684 if (rasterizer->sprite_coord_enable != variant->point_sprite_mask)
685 return false;
686
687 /* Ensure the orientation is correct */
688 bool upper_left =
689 rasterizer->sprite_coord_mode ==
690 PIPE_SPRITE_COORD_UPPER_LEFT;
691
692 if (variant->point_sprite_upper_left != upper_left)
693 return false;
694 }
695
Alyssa Rosenzweig7da251f2019-02-05 04:32:27 +0000696 /* Otherwise, we're good to go */
697 return true;
698}
699
Alyssa Rosenzweigf714eab2019-08-07 10:11:28 -0700700/**
701 * Fix an uncompiled shader's stream output info, and produce a bitmask
702 * of which VARYING_SLOT_* are captured for stream output.
703 *
704 * Core Gallium stores output->register_index as a "slot" number, where
705 * slots are assigned consecutively to all outputs in info->outputs_written.
706 * This naive packing of outputs doesn't work for us - we too have slots,
707 * but the layout is defined by the VUE map, which we won't have until we
708 * compile a specific shader variant. So, we remap these and simply store
709 * VARYING_SLOT_* in our copy's output->register_index fields.
710 *
711 * We then produce a bitmask of outputs which are used for SO.
712 *
713 * Implementation from iris.
714 */
715
716static uint64_t
717update_so_info(struct pipe_stream_output_info *so_info,
718 uint64_t outputs_written)
719{
720 uint64_t so_outputs = 0;
Vinson Leede2e5f62019-11-27 23:37:00 -0800721 uint8_t reverse_map[64] = {0};
Alyssa Rosenzweigf714eab2019-08-07 10:11:28 -0700722 unsigned slot = 0;
723
724 while (outputs_written)
725 reverse_map[slot++] = u_bit_scan64(&outputs_written);
726
727 for (unsigned i = 0; i < so_info->num_outputs; i++) {
728 struct pipe_stream_output *output = &so_info->output[i];
729
730 /* Map Gallium's condensed "slots" back to real VARYING_SLOT_* enums */
731 output->register_index = reverse_map[output->register_index];
732
733 so_outputs |= 1ull << output->register_index;
734 }
735
736 return so_outputs;
737}
738
Alyssa Rosenzweig7da251f2019-02-05 04:32:27 +0000739static void
Alyssa Rosenzweig1acffb52019-06-04 23:48:17 +0000740panfrost_bind_shader_state(
Alyssa Rosenzweig7da251f2019-02-05 04:32:27 +0000741 struct pipe_context *pctx,
Alyssa Rosenzweig1acffb52019-06-04 23:48:17 +0000742 void *hwcso,
743 enum pipe_shader_type type)
Alyssa Rosenzweig7da251f2019-02-05 04:32:27 +0000744{
745 struct panfrost_context *ctx = pan_context(pctx);
Alyssa Rosenzweigb73b3392020-06-01 20:52:59 -0400746 struct panfrost_device *dev = pan_device(ctx->base.screen);
Alyssa Rosenzweig3113be32019-07-31 14:15:19 -0700747 ctx->shader[type] = hwcso;
748
Alyssa Rosenzweig1acffb52019-06-04 23:48:17 +0000749 if (!hwcso) return;
Alyssa Rosenzweig7da251f2019-02-05 04:32:27 +0000750
Alyssa Rosenzweig1acffb52019-06-04 23:48:17 +0000751 /* Match the appropriate variant */
Alyssa Rosenzweig7da251f2019-02-05 04:32:27 +0000752
Alyssa Rosenzweig1acffb52019-06-04 23:48:17 +0000753 signed variant = -1;
754 struct panfrost_shader_variants *variants = (struct panfrost_shader_variants *) hwcso;
Alyssa Rosenzweig7da251f2019-02-05 04:32:27 +0000755
Alyssa Rosenzweig1acffb52019-06-04 23:48:17 +0000756 for (unsigned i = 0; i < variants->variant_count; ++i) {
Alyssa Rosenzweig9ab8d312019-06-14 10:12:38 -0700757 if (panfrost_variant_matches(ctx, &variants->variants[i], type)) {
Alyssa Rosenzweig1acffb52019-06-04 23:48:17 +0000758 variant = i;
759 break;
Alyssa Rosenzweig7da251f2019-02-05 04:32:27 +0000760 }
761 }
762
Alyssa Rosenzweig1acffb52019-06-04 23:48:17 +0000763 if (variant == -1) {
764 /* No variant matched, so create a new one */
765 variant = variants->variant_count++;
Icecream95d8a35012020-01-12 14:19:25 +1300766
767 if (variants->variant_count > variants->variant_space) {
768 unsigned old_space = variants->variant_space;
769
770 variants->variant_space *= 2;
771 if (variants->variant_space == 0)
772 variants->variant_space = 1;
773
774 /* Arbitrary limit to stop runaway programs from
775 * creating an unbounded number of shader variants. */
776 assert(variants->variant_space < 1024);
777
778 unsigned msize = sizeof(struct panfrost_shader_state);
779 variants->variants = realloc(variants->variants,
780 variants->variant_space * msize);
781
782 memset(&variants->variants[old_space], 0,
783 (variants->variant_space - old_space) * msize);
784 }
Alyssa Rosenzweig1acffb52019-06-04 23:48:17 +0000785
Alyssa Rosenzweigbe030602019-06-24 11:01:05 -0700786 struct panfrost_shader_state *v =
Alyssa Rosenzweiga2d0ea92019-07-10 10:10:31 -0700787 &variants->variants[variant];
Alyssa Rosenzweig1acffb52019-06-04 23:48:17 +0000788
Alyssa Rosenzweigbe030602019-06-24 11:01:05 -0700789 if (type == PIPE_SHADER_FRAGMENT) {
790 v->alpha_state = ctx->depth_stencil->alpha;
791
Icecream9575018f62020-07-06 19:40:05 +1200792 struct pipe_framebuffer_state *fb = &ctx->pipe_framebuffer;
793 for (unsigned i = 0; i < fb->nr_cbufs; ++i) {
794 enum pipe_format fmt = PIPE_FORMAT_R8G8B8A8_UNORM;
795
796 if ((fb->nr_cbufs > i) && fb->cbufs[i])
797 fmt = fb->cbufs[i]->format;
798
Icecream95d16d1362020-07-07 15:06:08 +1200799 const struct util_format_description *desc =
800 util_format_description(fmt);
801
802 if (pan_format_class_load(desc, dev->quirks) == PAN_FORMAT_NATIVE)
803 fmt = PIPE_FORMAT_NONE;
804
Icecream9575018f62020-07-06 19:40:05 +1200805 v->rt_formats[i] = fmt;
806 }
807
Alyssa Rosenzweigb73b3392020-06-01 20:52:59 -0400808 /* Point sprites are TODO on Bifrost */
809 if (ctx->rasterizer && !(dev->quirks & IS_BIFROST)) {
Alyssa Rosenzweigbe030602019-06-24 11:01:05 -0700810 v->point_sprite_mask = ctx->rasterizer->base.sprite_coord_enable;
811 v->point_sprite_upper_left =
812 ctx->rasterizer->base.sprite_coord_mode ==
813 PIPE_SPRITE_COORD_UPPER_LEFT;
814 }
815 }
Alyssa Rosenzweig1acffb52019-06-04 23:48:17 +0000816 }
817
818 /* Select this variant */
819 variants->active_variant = variant;
820
821 struct panfrost_shader_state *shader_state = &variants->variants[variant];
Alyssa Rosenzweig9ab8d312019-06-14 10:12:38 -0700822 assert(panfrost_variant_matches(ctx, shader_state, type));
Alyssa Rosenzweig1acffb52019-06-04 23:48:17 +0000823
824 /* We finally have a variant, so compile it */
825
826 if (!shader_state->compiled) {
Alyssa Rosenzweig5b0a1a42019-08-07 10:26:12 -0700827 uint64_t outputs_written = 0;
828
Boris Brezillonb02f97c2020-03-05 16:20:18 +0100829 panfrost_shader_compile(ctx, variants->base.type,
830 variants->base.type == PIPE_SHADER_IR_NIR ?
831 variants->base.ir.nir :
832 variants->base.tokens,
833 tgsi_processor_to_shader_stage(type),
834 shader_state,
Alyssa Rosenzweig5b0a1a42019-08-07 10:26:12 -0700835 &outputs_written);
Alyssa Rosenzweig1acffb52019-06-04 23:48:17 +0000836
837 shader_state->compiled = true;
Alyssa Rosenzweige7a05a62019-08-07 10:33:15 -0700838
839 /* Fixup the stream out information, since what Gallium returns
840 * normally is mildly insane */
841
842 shader_state->stream_output = variants->base.stream_output;
843 shader_state->so_mask =
844 update_so_info(&shader_state->stream_output, outputs_written);
Alyssa Rosenzweig1acffb52019-06-04 23:48:17 +0000845 }
Alyssa Rosenzweig7da251f2019-02-05 04:32:27 +0000846}
847
Alyssa Rosenzweig271726e2019-12-13 15:13:02 -0500848static void *
849panfrost_create_vs_state(struct pipe_context *pctx, const struct pipe_shader_state *hwcso)
850{
851 return panfrost_create_shader_state(pctx, hwcso, PIPE_SHADER_VERTEX);
852}
853
854static void *
855panfrost_create_fs_state(struct pipe_context *pctx, const struct pipe_shader_state *hwcso)
856{
857 return panfrost_create_shader_state(pctx, hwcso, PIPE_SHADER_FRAGMENT);
858}
859
Alyssa Rosenzweig7da251f2019-02-05 04:32:27 +0000860static void
Alyssa Rosenzweig1acffb52019-06-04 23:48:17 +0000861panfrost_bind_vs_state(struct pipe_context *pctx, void *hwcso)
Alyssa Rosenzweig7da251f2019-02-05 04:32:27 +0000862{
Alyssa Rosenzweig1acffb52019-06-04 23:48:17 +0000863 panfrost_bind_shader_state(pctx, hwcso, PIPE_SHADER_VERTEX);
864}
Alyssa Rosenzweig7da251f2019-02-05 04:32:27 +0000865
Alyssa Rosenzweig1acffb52019-06-04 23:48:17 +0000866static void
867panfrost_bind_fs_state(struct pipe_context *pctx, void *hwcso)
868{
869 panfrost_bind_shader_state(pctx, hwcso, PIPE_SHADER_FRAGMENT);
Alyssa Rosenzweig7da251f2019-02-05 04:32:27 +0000870}
871
872static void
873panfrost_set_vertex_buffers(
874 struct pipe_context *pctx,
875 unsigned start_slot,
876 unsigned num_buffers,
877 const struct pipe_vertex_buffer *buffers)
878{
879 struct panfrost_context *ctx = pan_context(pctx);
Alyssa Rosenzweig7da251f2019-02-05 04:32:27 +0000880
Alyssa Rosenzweige008d4f2019-04-14 22:42:44 +0000881 util_set_vertex_buffers_mask(ctx->vertex_buffers, &ctx->vb_mask, buffers, start_slot, num_buffers);
Alyssa Rosenzweig7da251f2019-02-05 04:32:27 +0000882}
883
884static void
885panfrost_set_constant_buffer(
886 struct pipe_context *pctx,
887 enum pipe_shader_type shader, uint index,
888 const struct pipe_constant_buffer *buf)
889{
890 struct panfrost_context *ctx = pan_context(pctx);
891 struct panfrost_constant_buffer *pbuf = &ctx->constant_buffer[shader];
892
Alyssa Rosenzweigca2caf02019-06-20 16:07:57 -0700893 util_copy_constant_buffer(&pbuf->cb[index], buf);
Alyssa Rosenzweig7da251f2019-02-05 04:32:27 +0000894
Alyssa Rosenzweigca2caf02019-06-20 16:07:57 -0700895 unsigned mask = (1 << index);
Alyssa Rosenzweig7da251f2019-02-05 04:32:27 +0000896
Alyssa Rosenzweigca2caf02019-06-20 16:07:57 -0700897 if (unlikely(!buf)) {
898 pbuf->enabled_mask &= ~mask;
899 pbuf->dirty_mask &= ~mask;
Alyssa Rosenzweig7da251f2019-02-05 04:32:27 +0000900 return;
901 }
902
Alyssa Rosenzweigca2caf02019-06-20 16:07:57 -0700903 pbuf->enabled_mask |= mask;
904 pbuf->dirty_mask |= mask;
Alyssa Rosenzweig7da251f2019-02-05 04:32:27 +0000905}
906
907static void
908panfrost_set_stencil_ref(
909 struct pipe_context *pctx,
910 const struct pipe_stencil_ref *ref)
911{
912 struct panfrost_context *ctx = pan_context(pctx);
913 ctx->stencil_ref = *ref;
Alyssa Rosenzweig7da251f2019-02-05 04:32:27 +0000914}
915
Icecream953baf10a2020-04-30 15:21:17 +1200916void
917panfrost_create_sampler_view_bo(struct panfrost_sampler_view *so,
918 struct pipe_context *pctx,
919 struct pipe_resource *texture)
Alyssa Rosenzweig7da251f2019-02-05 04:32:27 +0000920{
Alyssa Rosenzweigca8c6252020-03-23 18:44:21 -0400921 struct panfrost_device *device = pan_device(pctx->screen);
Icecream953baf10a2020-04-30 15:21:17 +1200922 struct panfrost_resource *prsrc = (struct panfrost_resource *)texture;
Alyssa Rosenzweigcce3d922020-07-09 16:34:00 -0400923 enum pipe_format format = so->base.format;
Alyssa Rosenzweig81b10532019-05-14 23:18:18 +0000924 assert(prsrc->bo);
Alyssa Rosenzweig7da251f2019-02-05 04:32:27 +0000925
Alyssa Rosenzweigcce3d922020-07-09 16:34:00 -0400926 /* Format to access the stencil portion of a Z32_S8 texture */
Icecream95c1d3d392020-07-13 22:45:51 +1200927 if (format == PIPE_FORMAT_X32_S8X24_UINT) {
Alyssa Rosenzweigcce3d922020-07-09 16:34:00 -0400928 assert(prsrc->separate_stencil);
929 texture = &prsrc->separate_stencil->base;
930 prsrc = (struct panfrost_resource *)texture;
931 format = texture->format;
932 }
933
Icecream95c1d3d392020-07-13 22:45:51 +1200934 const struct util_format_description *desc = util_format_description(format);
935
936 bool fake_rgtc = !panfrost_supports_compressed_format(device, MALI_BC4_UNORM);
937
938 if (desc->layout == UTIL_FORMAT_LAYOUT_RGTC && fake_rgtc) {
939 if (desc->is_snorm)
940 format = PIPE_FORMAT_R8G8B8A8_SNORM;
941 else
942 format = PIPE_FORMAT_R8G8B8A8_UNORM;
943 desc = util_format_description(format);
944 }
945
Icecream9565b3b082020-06-20 19:09:03 +1200946 so->texture_bo = prsrc->bo->gpu;
Alyssa Rosenzweig965537df2020-07-22 10:23:50 -0400947 so->modifier = prsrc->modifier;
Icecream95fafc3052020-06-12 20:14:02 +1200948
Alyssa Rosenzweig7da251f2019-02-05 04:32:27 +0000949 unsigned char user_swizzle[4] = {
Icecream953baf10a2020-04-30 15:21:17 +1200950 so->base.swizzle_r,
951 so->base.swizzle_g,
952 so->base.swizzle_b,
953 so->base.swizzle_a
Alyssa Rosenzweig7da251f2019-02-05 04:32:27 +0000954 };
955
Alyssa Rosenzweig67a34ac2019-06-14 16:26:49 -0700956 /* In the hardware, array_size refers specifically to array textures,
957 * whereas in Gallium, it also covers cubemaps */
958
959 unsigned array_size = texture->array_size;
Alyssa Rosenzweig0b5bc6e2020-06-30 15:41:24 -0400960 unsigned depth = texture->depth0;
Alyssa Rosenzweig67a34ac2019-06-14 16:26:49 -0700961
Icecream953baf10a2020-04-30 15:21:17 +1200962 if (so->base.target == PIPE_TEXTURE_CUBE) {
Alyssa Rosenzweig67a34ac2019-06-14 16:26:49 -0700963 /* TODO: Cubemap arrays */
964 assert(array_size == 6);
Alyssa Rosenzweigeb3c0972019-06-21 17:27:05 -0700965 array_size /= 6;
Alyssa Rosenzweig67a34ac2019-06-14 16:26:49 -0700966 }
967
Alyssa Rosenzweig0b5bc6e2020-06-30 15:41:24 -0400968 /* MSAA only supported for 2D textures (and 2D texture arrays via an
969 * extension currently unimplemented */
970
971 if (so->base.target == PIPE_TEXTURE_2D) {
972 assert(depth == 1);
973 depth = texture->nr_samples;
974 } else {
975 /* MSAA only supported for 2D textures */
976 assert(texture->nr_samples <= 1);
977 }
978
Alyssa Rosenzweigb9295652020-02-18 14:20:16 -0500979 enum mali_texture_type type =
Icecream953baf10a2020-04-30 15:21:17 +1200980 panfrost_translate_texture_type(so->base.target);
Alyssa Rosenzweig7da251f2019-02-05 04:32:27 +0000981
Tomeu Vizosoe41894b2020-04-17 14:23:49 +0200982 if (device->quirks & IS_BIFROST) {
Alyssa Rosenzweiga3d29362020-04-21 16:08:07 -0400983 unsigned char composed_swizzle[4];
984 util_format_compose_swizzles(desc->swizzle, user_swizzle, composed_swizzle);
985
986 unsigned size = panfrost_estimate_texture_payload_size(
Icecream953baf10a2020-04-30 15:21:17 +1200987 so->base.u.tex.first_level,
988 so->base.u.tex.last_level,
989 so->base.u.tex.first_layer,
990 so->base.u.tex.last_layer,
Alyssa Rosenzweig60888912020-07-15 11:39:08 -0400991 texture->nr_samples,
Alyssa Rosenzweig965537df2020-07-22 10:23:50 -0400992 type, prsrc->modifier);
Alyssa Rosenzweiga3d29362020-04-21 16:08:07 -0400993
Alyssa Rosenzweig1d88f072020-07-07 16:19:39 -0400994 so->bo = panfrost_bo_create(device, size, 0);
Alyssa Rosenzweiga3d29362020-04-21 16:08:07 -0400995
Tomeu Vizosoe41894b2020-04-17 14:23:49 +0200996 so->bifrost_descriptor = rzalloc(pctx, struct bifrost_texture_descriptor);
997 panfrost_new_texture_bifrost(
998 so->bifrost_descriptor,
999 texture->width0, texture->height0,
Alyssa Rosenzweig0b5bc6e2020-06-30 15:41:24 -04001000 depth, array_size,
Alyssa Rosenzweigcce3d922020-07-09 16:34:00 -04001001 format,
Alyssa Rosenzweig965537df2020-07-22 10:23:50 -04001002 type, prsrc->modifier,
Icecream953baf10a2020-04-30 15:21:17 +12001003 so->base.u.tex.first_level,
1004 so->base.u.tex.last_level,
1005 so->base.u.tex.first_layer,
1006 so->base.u.tex.last_layer,
Alyssa Rosenzweig41c06de2020-06-30 16:43:32 -04001007 texture->nr_samples,
Tomeu Vizosoe41894b2020-04-17 14:23:49 +02001008 prsrc->cubemap_stride,
Alyssa Rosenzweiga3d29362020-04-21 16:08:07 -04001009 panfrost_translate_swizzle_4(composed_swizzle),
Tomeu Vizosoe41894b2020-04-17 14:23:49 +02001010 prsrc->bo->gpu,
Alyssa Rosenzweiga3d29362020-04-21 16:08:07 -04001011 prsrc->slices,
Alyssa Rosenzweig32b171d2020-06-15 09:20:39 -04001012 so->bo);
Tomeu Vizosoe41894b2020-04-17 14:23:49 +02001013 } else {
Alyssa Rosenzweiga3d29362020-04-21 16:08:07 -04001014 unsigned size = panfrost_estimate_texture_payload_size(
Icecream953baf10a2020-04-30 15:21:17 +12001015 so->base.u.tex.first_level,
1016 so->base.u.tex.last_level,
1017 so->base.u.tex.first_layer,
1018 so->base.u.tex.last_layer,
Alyssa Rosenzweig60888912020-07-15 11:39:08 -04001019 texture->nr_samples,
Alyssa Rosenzweig965537df2020-07-22 10:23:50 -04001020 type, prsrc->modifier);
Alyssa Rosenzweiga3d29362020-04-21 16:08:07 -04001021 size += sizeof(struct mali_texture_descriptor);
Alyssa Rosenzweig7da251f2019-02-05 04:32:27 +00001022
Alyssa Rosenzweig1d88f072020-07-07 16:19:39 -04001023 so->bo = panfrost_bo_create(device, size, 0);
Alyssa Rosenzweig7da251f2019-02-05 04:32:27 +00001024
Tomeu Vizosoe41894b2020-04-17 14:23:49 +02001025 panfrost_new_texture(
Alyssa Rosenzweig32b171d2020-06-15 09:20:39 -04001026 so->bo->cpu,
Tomeu Vizosoe41894b2020-04-17 14:23:49 +02001027 texture->width0, texture->height0,
Alyssa Rosenzweig0b5bc6e2020-06-30 15:41:24 -04001028 depth, array_size,
Alyssa Rosenzweigcce3d922020-07-09 16:34:00 -04001029 format,
Alyssa Rosenzweig965537df2020-07-22 10:23:50 -04001030 type, prsrc->modifier,
Icecream953baf10a2020-04-30 15:21:17 +12001031 so->base.u.tex.first_level,
1032 so->base.u.tex.last_level,
1033 so->base.u.tex.first_layer,
1034 so->base.u.tex.last_layer,
Alyssa Rosenzweig41c06de2020-06-30 16:43:32 -04001035 texture->nr_samples,
Tomeu Vizosoe41894b2020-04-17 14:23:49 +02001036 prsrc->cubemap_stride,
1037 panfrost_translate_swizzle_4(user_swizzle),
1038 prsrc->bo->gpu,
1039 prsrc->slices);
1040 }
Icecream953baf10a2020-04-30 15:21:17 +12001041}
1042
1043static struct pipe_sampler_view *
1044panfrost_create_sampler_view(
1045 struct pipe_context *pctx,
1046 struct pipe_resource *texture,
1047 const struct pipe_sampler_view *template)
1048{
1049 struct panfrost_sampler_view *so = rzalloc(pctx, struct panfrost_sampler_view);
1050
1051 pipe_reference(NULL, &texture->reference);
1052
1053 so->base = *template;
1054 so->base.texture = texture;
1055 so->base.reference.count = 1;
1056 so->base.context = pctx;
1057
1058 panfrost_create_sampler_view_bo(so, pctx, texture);
Alyssa Rosenzweig7da251f2019-02-05 04:32:27 +00001059
1060 return (struct pipe_sampler_view *) so;
1061}
1062
1063static void
1064panfrost_set_sampler_views(
1065 struct pipe_context *pctx,
1066 enum pipe_shader_type shader,
1067 unsigned start_slot, unsigned num_views,
1068 struct pipe_sampler_view **views)
1069{
1070 struct panfrost_context *ctx = pan_context(pctx);
Tomeu Vizoso5dfe4122019-12-12 08:43:12 +01001071 unsigned new_nr = 0;
1072 unsigned i;
Alyssa Rosenzweig7da251f2019-02-05 04:32:27 +00001073
1074 assert(start_slot == 0);
1075
Tomeu Vizoso5dfe4122019-12-12 08:43:12 +01001076 for (i = 0; i < num_views; ++i) {
Alyssa Rosenzweig0219b992019-06-11 14:21:14 -07001077 if (views[i])
1078 new_nr = i + 1;
Tomeu Vizoso5dfe4122019-12-12 08:43:12 +01001079 pipe_sampler_view_reference((struct pipe_sampler_view **)&ctx->sampler_views[shader][i],
1080 views[i]);
Alyssa Rosenzweig0219b992019-06-11 14:21:14 -07001081 }
1082
Tomeu Vizoso5dfe4122019-12-12 08:43:12 +01001083 for (; i < ctx->sampler_view_count[shader]; i++) {
1084 pipe_sampler_view_reference((struct pipe_sampler_view **)&ctx->sampler_views[shader][i],
1085 NULL);
1086 }
Alyssa Rosenzweig0219b992019-06-11 14:21:14 -07001087 ctx->sampler_view_count[shader] = new_nr;
Alyssa Rosenzweig7da251f2019-02-05 04:32:27 +00001088}
1089
1090static void
1091panfrost_sampler_view_destroy(
1092 struct pipe_context *pctx,
Alyssa Rosenzweigb9295652020-02-18 14:20:16 -05001093 struct pipe_sampler_view *pview)
Alyssa Rosenzweig7da251f2019-02-05 04:32:27 +00001094{
Alyssa Rosenzweigb9295652020-02-18 14:20:16 -05001095 struct panfrost_sampler_view *view = (struct panfrost_sampler_view *) pview;
1096
1097 pipe_resource_reference(&pview->texture, NULL);
Alyssa Rosenzweig32b171d2020-06-15 09:20:39 -04001098 panfrost_bo_unreference(view->bo);
Tomeu Vizosod3eb23a2020-04-17 14:23:39 +02001099 if (view->bifrost_descriptor)
1100 ralloc_free(view->bifrost_descriptor);
Tomeu Vizoso0fcf73b2019-06-18 14:24:57 +02001101 ralloc_free(view);
Alyssa Rosenzweig7da251f2019-02-05 04:32:27 +00001102}
1103
Alyssa Rosenzweig4e736b82019-08-01 10:31:35 -07001104static void
1105panfrost_set_shader_buffers(
1106 struct pipe_context *pctx,
1107 enum pipe_shader_type shader,
1108 unsigned start, unsigned count,
1109 const struct pipe_shader_buffer *buffers,
1110 unsigned writable_bitmask)
1111{
1112 struct panfrost_context *ctx = pan_context(pctx);
1113
1114 util_set_shader_buffers_mask(ctx->ssbo[shader], &ctx->ssbo_mask[shader],
1115 buffers, start, count);
1116}
1117
Alyssa Rosenzweig7da251f2019-02-05 04:32:27 +00001118static void
1119panfrost_set_framebuffer_state(struct pipe_context *pctx,
1120 const struct pipe_framebuffer_state *fb)
1121{
1122 struct panfrost_context *ctx = pan_context(pctx);
1123
Alyssa Rosenzweig629c7362019-07-18 11:05:01 -07001124 util_copy_framebuffer_state(&ctx->pipe_framebuffer, fb);
Boris Brezillon1ac33aa2019-09-15 20:33:13 +02001125 ctx->batch = NULL;
1126 panfrost_invalidate_frame(ctx);
Alyssa Rosenzweigb9869e02020-07-10 12:12:14 -04001127
1128 /* We may need to generate a new variant if the fragment shader is
1129 * keyed to the framebuffer format (due to EXT_framebuffer_fetch) */
1130 struct panfrost_shader_variants *fs = ctx->shader[PIPE_SHADER_FRAGMENT];
1131
1132 if (fs && fs->variant_count && fs->variants[fs->active_variant].outputs_read)
1133 ctx->base.bind_fs_state(&ctx->base, fs);
Alyssa Rosenzweig7da251f2019-02-05 04:32:27 +00001134}
1135
1136static void *
Alyssa Rosenzweig7da251f2019-02-05 04:32:27 +00001137panfrost_create_depth_stencil_state(struct pipe_context *pipe,
1138 const struct pipe_depth_stencil_alpha_state *depth_stencil)
1139{
1140 return mem_dup(depth_stencil, sizeof(*depth_stencil));
1141}
1142
1143static void
1144panfrost_bind_depth_stencil_state(struct pipe_context *pipe,
1145 void *cso)
1146{
1147 struct panfrost_context *ctx = pan_context(pipe);
1148 struct pipe_depth_stencil_alpha_state *depth_stencil = cso;
1149 ctx->depth_stencil = depth_stencil;
1150
1151 if (!depth_stencil)
1152 return;
1153
1154 /* Alpha does not exist in the hardware (it's not in ES3), so it's
1155 * emulated in the fragment shader */
1156
1157 if (depth_stencil->alpha.enabled) {
1158 /* We need to trigger a new shader (maybe) */
Alyssa Rosenzweigac6aa932019-07-31 14:13:30 -07001159 ctx->base.bind_fs_state(&ctx->base, ctx->shader[PIPE_SHADER_FRAGMENT]);
Alyssa Rosenzweig7da251f2019-02-05 04:32:27 +00001160 }
1161
Alyssa Rosenzweig7da251f2019-02-05 04:32:27 +00001162 /* Bounds test not implemented */
1163 assert(!depth_stencil->depth.bounds_test);
Alyssa Rosenzweig7da251f2019-02-05 04:32:27 +00001164}
1165
1166static void
1167panfrost_delete_depth_stencil_state(struct pipe_context *pipe, void *depth)
1168{
1169 free( depth );
1170}
1171
1172static void
1173panfrost_set_sample_mask(struct pipe_context *pipe,
1174 unsigned sample_mask)
1175{
Alyssa Rosenzweigf23cdd42020-07-02 10:07:08 -04001176 struct panfrost_context *ctx = pan_context(pipe);
1177 ctx->sample_mask = sample_mask;
Alyssa Rosenzweig7da251f2019-02-05 04:32:27 +00001178}
1179
1180static void
Alyssa Rosenzweig82256042020-07-15 11:38:39 -04001181panfrost_set_min_samples(struct pipe_context *pipe,
1182 unsigned min_samples)
1183{
1184 struct panfrost_context *ctx = pan_context(pipe);
1185 ctx->min_samples = min_samples;
1186}
1187
1188
1189static void
Alyssa Rosenzweig7da251f2019-02-05 04:32:27 +00001190panfrost_set_clip_state(struct pipe_context *pipe,
1191 const struct pipe_clip_state *clip)
1192{
1193 //struct panfrost_context *panfrost = pan_context(pipe);
1194}
1195
1196static void
1197panfrost_set_viewport_states(struct pipe_context *pipe,
1198 unsigned start_slot,
1199 unsigned num_viewports,
1200 const struct pipe_viewport_state *viewports)
1201{
1202 struct panfrost_context *ctx = pan_context(pipe);
1203
1204 assert(start_slot == 0);
1205 assert(num_viewports == 1);
1206
1207 ctx->pipe_viewport = *viewports;
Alyssa Rosenzweig7da251f2019-02-05 04:32:27 +00001208}
1209
1210static void
1211panfrost_set_scissor_states(struct pipe_context *pipe,
1212 unsigned start_slot,
1213 unsigned num_scissors,
1214 const struct pipe_scissor_state *scissors)
1215{
1216 struct panfrost_context *ctx = pan_context(pipe);
1217
1218 assert(start_slot == 0);
1219 assert(num_scissors == 1);
1220
1221 ctx->scissor = *scissors;
Alyssa Rosenzweig7da251f2019-02-05 04:32:27 +00001222}
1223
1224static void
1225panfrost_set_polygon_stipple(struct pipe_context *pipe,
1226 const struct pipe_poly_stipple *stipple)
1227{
1228 //struct panfrost_context *panfrost = pan_context(pipe);
1229}
1230
1231static void
1232panfrost_set_active_query_state(struct pipe_context *pipe,
Ilia Mirkin0e30c6b2019-07-04 11:41:41 -04001233 bool enable)
Alyssa Rosenzweig7da251f2019-02-05 04:32:27 +00001234{
Alyssa Rosenzweig7c224c12019-08-08 07:01:12 -07001235 struct panfrost_context *ctx = pan_context(pipe);
1236 ctx->active_queries = enable;
Alyssa Rosenzweig7da251f2019-02-05 04:32:27 +00001237}
1238
1239static void
1240panfrost_destroy(struct pipe_context *pipe)
1241{
1242 struct panfrost_context *panfrost = pan_context(pipe);
1243
1244 if (panfrost->blitter)
1245 util_blitter_destroy(panfrost->blitter);
Tomeu Vizoso756f7b92019-03-08 10:27:07 +01001246
Alyssa Rosenzweig7005c0d2019-06-23 11:05:10 -07001247 if (panfrost->blitter_wallpaper)
1248 util_blitter_destroy(panfrost->blitter_wallpaper);
1249
Boris Brezillonb60ed3c2019-11-06 15:52:45 +01001250 util_unreference_framebuffer_state(&panfrost->pipe_framebuffer);
Boris Brezillon8c8e4fd2019-11-06 15:49:43 +01001251 u_upload_destroy(pipe->stream_uploader);
1252
Tomeu Vizoso0fcf73b2019-06-18 14:24:57 +02001253 ralloc_free(pipe);
Alyssa Rosenzweig7da251f2019-02-05 04:32:27 +00001254}
1255
1256static struct pipe_query *
Alyssa Rosenzweiga2d0ea92019-07-10 10:10:31 -07001257panfrost_create_query(struct pipe_context *pipe,
1258 unsigned type,
1259 unsigned index)
Alyssa Rosenzweig7da251f2019-02-05 04:32:27 +00001260{
Tomeu Vizoso0fcf73b2019-06-18 14:24:57 +02001261 struct panfrost_query *q = rzalloc(pipe, struct panfrost_query);
Alyssa Rosenzweig7da251f2019-02-05 04:32:27 +00001262
1263 q->type = type;
1264 q->index = index;
1265
1266 return (struct pipe_query *) q;
1267}
1268
1269static void
1270panfrost_destroy_query(struct pipe_context *pipe, struct pipe_query *q)
1271{
Urja Rannikkodff99ce2019-10-22 12:05:07 +00001272 struct panfrost_query *query = (struct panfrost_query *) q;
1273
1274 if (query->bo) {
1275 panfrost_bo_unreference(query->bo);
1276 query->bo = NULL;
1277 }
1278
Tomeu Vizoso0fcf73b2019-06-18 14:24:57 +02001279 ralloc_free(q);
Alyssa Rosenzweig7da251f2019-02-05 04:32:27 +00001280}
1281
Ilia Mirkin0e30c6b2019-07-04 11:41:41 -04001282static bool
Alyssa Rosenzweig7da251f2019-02-05 04:32:27 +00001283panfrost_begin_query(struct pipe_context *pipe, struct pipe_query *q)
1284{
1285 struct panfrost_context *ctx = pan_context(pipe);
1286 struct panfrost_query *query = (struct panfrost_query *) q;
1287
1288 switch (query->type) {
Alyssa Rosenzweiga2d0ea92019-07-10 10:10:31 -07001289 case PIPE_QUERY_OCCLUSION_COUNTER:
1290 case PIPE_QUERY_OCCLUSION_PREDICATE:
Alyssa Rosenzweig7c224c12019-08-08 07:01:12 -07001291 case PIPE_QUERY_OCCLUSION_PREDICATE_CONSERVATIVE:
Urja Rannikkodff99ce2019-10-22 12:05:07 +00001292 /* Allocate a bo for the query results to be stored */
1293 if (!query->bo) {
Alyssa Rosenzweig1d88f072020-07-07 16:19:39 -04001294 query->bo = panfrost_bo_create(
Alyssa Rosenzweigca8c6252020-03-23 18:44:21 -04001295 pan_device(ctx->base.screen),
Urja Rannikkodff99ce2019-10-22 12:05:07 +00001296 sizeof(unsigned), 0);
1297 }
1298
1299 unsigned *result = (unsigned *)query->bo->cpu;
1300 *result = 0; /* Default to 0 if nothing at all drawn. */
Alyssa Rosenzweiga2d0ea92019-07-10 10:10:31 -07001301 ctx->occlusion_query = query;
Alyssa Rosenzweiga2d0ea92019-07-10 10:10:31 -07001302 break;
Alyssa Rosenzweig7c224c12019-08-08 07:01:12 -07001303
1304 /* Geometry statistics are computed in the driver. XXX: geom/tess
1305 * shaders.. */
1306
1307 case PIPE_QUERY_PRIMITIVES_GENERATED:
1308 query->start = ctx->prims_generated;
1309 break;
1310 case PIPE_QUERY_PRIMITIVES_EMITTED:
1311 query->start = ctx->tf_prims_generated;
1312 break;
Alyssa Rosenzweig7da251f2019-02-05 04:32:27 +00001313
Alyssa Rosenzweiga2d0ea92019-07-10 10:10:31 -07001314 default:
Alyssa Rosenzweiged1910d2020-07-07 16:15:45 -04001315 /* TODO: timestamp queries, etc? */
Alyssa Rosenzweiga2d0ea92019-07-10 10:10:31 -07001316 break;
Alyssa Rosenzweig7da251f2019-02-05 04:32:27 +00001317 }
1318
1319 return true;
1320}
1321
1322static bool
1323panfrost_end_query(struct pipe_context *pipe, struct pipe_query *q)
1324{
1325 struct panfrost_context *ctx = pan_context(pipe);
Alyssa Rosenzweig7c224c12019-08-08 07:01:12 -07001326 struct panfrost_query *query = (struct panfrost_query *) q;
1327
1328 switch (query->type) {
1329 case PIPE_QUERY_OCCLUSION_COUNTER:
1330 case PIPE_QUERY_OCCLUSION_PREDICATE:
1331 case PIPE_QUERY_OCCLUSION_PREDICATE_CONSERVATIVE:
1332 ctx->occlusion_query = NULL;
1333 break;
1334 case PIPE_QUERY_PRIMITIVES_GENERATED:
1335 query->end = ctx->prims_generated;
1336 break;
1337 case PIPE_QUERY_PRIMITIVES_EMITTED:
1338 query->end = ctx->tf_prims_generated;
1339 break;
1340 }
1341
Alyssa Rosenzweig7da251f2019-02-05 04:32:27 +00001342 return true;
1343}
1344
Ilia Mirkin0e30c6b2019-07-04 11:41:41 -04001345static bool
Alyssa Rosenzweiga2d0ea92019-07-10 10:10:31 -07001346panfrost_get_query_result(struct pipe_context *pipe,
Alyssa Rosenzweig7da251f2019-02-05 04:32:27 +00001347 struct pipe_query *q,
Ilia Mirkin0e30c6b2019-07-04 11:41:41 -04001348 bool wait,
Alyssa Rosenzweig7da251f2019-02-05 04:32:27 +00001349 union pipe_query_result *vresult)
1350{
Alyssa Rosenzweig7da251f2019-02-05 04:32:27 +00001351 struct panfrost_query *query = (struct panfrost_query *) q;
Boris Brezillona45984b2019-09-15 19:15:16 +02001352 struct panfrost_context *ctx = pan_context(pipe);
Alyssa Rosenzweig7da251f2019-02-05 04:32:27 +00001353
Alyssa Rosenzweig7da251f2019-02-05 04:32:27 +00001354
1355 switch (query->type) {
Alyssa Rosenzweiga2d0ea92019-07-10 10:10:31 -07001356 case PIPE_QUERY_OCCLUSION_COUNTER:
1357 case PIPE_QUERY_OCCLUSION_PREDICATE:
Alyssa Rosenzweig7c224c12019-08-08 07:01:12 -07001358 case PIPE_QUERY_OCCLUSION_PREDICATE_CONSERVATIVE:
Icecream950ef168d2020-07-18 11:39:45 +12001359 panfrost_flush_batches_accessing_bo(ctx, query->bo, false);
Icecream95858cc132020-07-18 11:36:36 +12001360 panfrost_bo_wait(query->bo, INT64_MAX, false);
Alyssa Rosenzweig7c224c12019-08-08 07:01:12 -07001361
Alyssa Rosenzweiga2d0ea92019-07-10 10:10:31 -07001362 /* Read back the query results */
Urja Rannikkodff99ce2019-10-22 12:05:07 +00001363 unsigned *result = (unsigned *) query->bo->cpu;
Alyssa Rosenzweiga2d0ea92019-07-10 10:10:31 -07001364 unsigned passed = *result;
Alyssa Rosenzweig7da251f2019-02-05 04:32:27 +00001365
Alyssa Rosenzweiga2d0ea92019-07-10 10:10:31 -07001366 if (query->type == PIPE_QUERY_OCCLUSION_COUNTER) {
1367 vresult->u64 = passed;
1368 } else {
1369 vresult->b = !!passed;
Alyssa Rosenzweig7da251f2019-02-05 04:32:27 +00001370 }
Alyssa Rosenzweiga2d0ea92019-07-10 10:10:31 -07001371
1372 break;
Alyssa Rosenzweig7c224c12019-08-08 07:01:12 -07001373
1374 case PIPE_QUERY_PRIMITIVES_GENERATED:
1375 case PIPE_QUERY_PRIMITIVES_EMITTED:
Alyssa Rosenzweig64d6f562020-07-20 13:34:42 -04001376 panfrost_flush_all_batches(ctx, 0);
Alyssa Rosenzweig7c224c12019-08-08 07:01:12 -07001377 vresult->u64 = query->end - query->start;
1378 break;
1379
Alyssa Rosenzweiga2d0ea92019-07-10 10:10:31 -07001380 default:
Alyssa Rosenzweiged1910d2020-07-07 16:15:45 -04001381 /* TODO: more queries */
Alyssa Rosenzweiga2d0ea92019-07-10 10:10:31 -07001382 break;
Alyssa Rosenzweig7da251f2019-02-05 04:32:27 +00001383 }
1384
1385 return true;
1386}
1387
Alyssa Rosenzweigf277bd32019-03-25 04:57:27 +00001388static struct pipe_stream_output_target *
1389panfrost_create_stream_output_target(struct pipe_context *pctx,
Alyssa Rosenzweiga2d0ea92019-07-10 10:10:31 -07001390 struct pipe_resource *prsc,
1391 unsigned buffer_offset,
1392 unsigned buffer_size)
Alyssa Rosenzweigf277bd32019-03-25 04:57:27 +00001393{
1394 struct pipe_stream_output_target *target;
1395
Tomeu Vizoso0fcf73b2019-06-18 14:24:57 +02001396 target = rzalloc(pctx, struct pipe_stream_output_target);
Alyssa Rosenzweigf277bd32019-03-25 04:57:27 +00001397
1398 if (!target)
1399 return NULL;
1400
1401 pipe_reference_init(&target->reference, 1);
1402 pipe_resource_reference(&target->buffer, prsc);
1403
1404 target->context = pctx;
1405 target->buffer_offset = buffer_offset;
1406 target->buffer_size = buffer_size;
1407
1408 return target;
1409}
1410
1411static void
1412panfrost_stream_output_target_destroy(struct pipe_context *pctx,
Alyssa Rosenzweiga2d0ea92019-07-10 10:10:31 -07001413 struct pipe_stream_output_target *target)
Alyssa Rosenzweigf277bd32019-03-25 04:57:27 +00001414{
1415 pipe_resource_reference(&target->buffer, NULL);
Tomeu Vizoso0fcf73b2019-06-18 14:24:57 +02001416 ralloc_free(target);
Alyssa Rosenzweigf277bd32019-03-25 04:57:27 +00001417}
1418
1419static void
1420panfrost_set_stream_output_targets(struct pipe_context *pctx,
Alyssa Rosenzweiga2d0ea92019-07-10 10:10:31 -07001421 unsigned num_targets,
1422 struct pipe_stream_output_target **targets,
1423 const unsigned *offsets)
Alyssa Rosenzweigf277bd32019-03-25 04:57:27 +00001424{
Alyssa Rosenzweig5ff79732019-08-06 16:43:28 -07001425 struct panfrost_context *ctx = pan_context(pctx);
1426 struct panfrost_streamout *so = &ctx->streamout;
1427
1428 assert(num_targets <= ARRAY_SIZE(so->targets));
1429
1430 for (unsigned i = 0; i < num_targets; i++) {
1431 if (offsets[i] != -1)
1432 so->offsets[i] = offsets[i];
1433
1434 pipe_so_target_reference(&so->targets[i], targets[i]);
1435 }
1436
1437 for (unsigned i = 0; i < so->num_targets; i++)
1438 pipe_so_target_reference(&so->targets[i], NULL);
1439
1440 so->num_targets = num_targets;
Alyssa Rosenzweigf277bd32019-03-25 04:57:27 +00001441}
1442
Alyssa Rosenzweig7da251f2019-02-05 04:32:27 +00001443struct pipe_context *
1444panfrost_create_context(struct pipe_screen *screen, void *priv, unsigned flags)
1445{
Tomeu Vizoso0fcf73b2019-06-18 14:24:57 +02001446 struct panfrost_context *ctx = rzalloc(screen, struct panfrost_context);
Alyssa Rosenzweig7da251f2019-02-05 04:32:27 +00001447 struct pipe_context *gallium = (struct pipe_context *) ctx;
Alyssa Rosenzweig229084f2020-05-29 14:02:43 -04001448 struct panfrost_device *dev = pan_device(screen);
Tomeu Vizoso756f7b92019-03-08 10:27:07 +01001449
Alyssa Rosenzweig7da251f2019-02-05 04:32:27 +00001450 gallium->screen = screen;
1451
1452 gallium->destroy = panfrost_destroy;
1453
1454 gallium->set_framebuffer_state = panfrost_set_framebuffer_state;
1455
1456 gallium->flush = panfrost_flush;
1457 gallium->clear = panfrost_clear;
1458 gallium->draw_vbo = panfrost_draw_vbo;
Icecream95391ad722020-07-10 11:19:07 +12001459 gallium->texture_barrier = panfrost_texture_barrier;
Alyssa Rosenzweig7da251f2019-02-05 04:32:27 +00001460
1461 gallium->set_vertex_buffers = panfrost_set_vertex_buffers;
1462 gallium->set_constant_buffer = panfrost_set_constant_buffer;
Alyssa Rosenzweig4e736b82019-08-01 10:31:35 -07001463 gallium->set_shader_buffers = panfrost_set_shader_buffers;
Alyssa Rosenzweig7da251f2019-02-05 04:32:27 +00001464
1465 gallium->set_stencil_ref = panfrost_set_stencil_ref;
1466
1467 gallium->create_sampler_view = panfrost_create_sampler_view;
1468 gallium->set_sampler_views = panfrost_set_sampler_views;
1469 gallium->sampler_view_destroy = panfrost_sampler_view_destroy;
1470
1471 gallium->create_rasterizer_state = panfrost_create_rasterizer_state;
1472 gallium->bind_rasterizer_state = panfrost_bind_rasterizer_state;
1473 gallium->delete_rasterizer_state = panfrost_generic_cso_delete;
1474
1475 gallium->create_vertex_elements_state = panfrost_create_vertex_elements_state;
1476 gallium->bind_vertex_elements_state = panfrost_bind_vertex_elements_state;
Alyssa Rosenzweig81d32622019-05-17 00:14:49 +00001477 gallium->delete_vertex_elements_state = panfrost_generic_cso_delete;
Alyssa Rosenzweig7da251f2019-02-05 04:32:27 +00001478
Alyssa Rosenzweig271726e2019-12-13 15:13:02 -05001479 gallium->create_fs_state = panfrost_create_fs_state;
Alyssa Rosenzweig7da251f2019-02-05 04:32:27 +00001480 gallium->delete_fs_state = panfrost_delete_shader_state;
1481 gallium->bind_fs_state = panfrost_bind_fs_state;
1482
Alyssa Rosenzweig271726e2019-12-13 15:13:02 -05001483 gallium->create_vs_state = panfrost_create_vs_state;
Alyssa Rosenzweig7da251f2019-02-05 04:32:27 +00001484 gallium->delete_vs_state = panfrost_delete_shader_state;
1485 gallium->bind_vs_state = panfrost_bind_vs_state;
1486
1487 gallium->create_sampler_state = panfrost_create_sampler_state;
1488 gallium->delete_sampler_state = panfrost_generic_cso_delete;
1489 gallium->bind_sampler_states = panfrost_bind_sampler_states;
1490
Alyssa Rosenzweig7da251f2019-02-05 04:32:27 +00001491 gallium->create_depth_stencil_alpha_state = panfrost_create_depth_stencil_state;
1492 gallium->bind_depth_stencil_alpha_state = panfrost_bind_depth_stencil_state;
1493 gallium->delete_depth_stencil_alpha_state = panfrost_delete_depth_stencil_state;
1494
1495 gallium->set_sample_mask = panfrost_set_sample_mask;
Alyssa Rosenzweig82256042020-07-15 11:38:39 -04001496 gallium->set_min_samples = panfrost_set_min_samples;
Alyssa Rosenzweig7da251f2019-02-05 04:32:27 +00001497
1498 gallium->set_clip_state = panfrost_set_clip_state;
1499 gallium->set_viewport_states = panfrost_set_viewport_states;
1500 gallium->set_scissor_states = panfrost_set_scissor_states;
1501 gallium->set_polygon_stipple = panfrost_set_polygon_stipple;
1502 gallium->set_active_query_state = panfrost_set_active_query_state;
1503
1504 gallium->create_query = panfrost_create_query;
1505 gallium->destroy_query = panfrost_destroy_query;
1506 gallium->begin_query = panfrost_begin_query;
1507 gallium->end_query = panfrost_end_query;
1508 gallium->get_query_result = panfrost_get_query_result;
1509
Alyssa Rosenzweigf277bd32019-03-25 04:57:27 +00001510 gallium->create_stream_output_target = panfrost_create_stream_output_target;
1511 gallium->stream_output_target_destroy = panfrost_stream_output_target_destroy;
1512 gallium->set_stream_output_targets = panfrost_set_stream_output_targets;
1513
Alyssa Rosenzweig7da251f2019-02-05 04:32:27 +00001514 panfrost_resource_context_init(gallium);
Alyssa Rosenzweig46396af2019-07-05 15:40:08 -07001515 panfrost_blend_context_init(gallium);
Alyssa Rosenzweiga8fc40a2019-07-23 08:28:23 -07001516 panfrost_compute_context_init(gallium);
Alyssa Rosenzweig7da251f2019-02-05 04:32:27 +00001517
Alyssa Rosenzweig7da251f2019-02-05 04:32:27 +00001518 gallium->stream_uploader = u_upload_create_default(gallium);
1519 gallium->const_uploader = gallium->stream_uploader;
1520 assert(gallium->stream_uploader);
1521
Alyssa Rosenzweig229084f2020-05-29 14:02:43 -04001522 /* All of our GPUs support ES mode. Midgard supports additionally
1523 * QUADS/QUAD_STRIPS/POLYGON. Bifrost supports just QUADS. */
1524
1525 ctx->draw_modes = (1 << (PIPE_PRIM_QUADS + 1)) - 1;
1526
1527 if (!(dev->quirks & IS_BIFROST)) {
1528 ctx->draw_modes |= (1 << PIPE_PRIM_QUAD_STRIP);
1529 ctx->draw_modes |= (1 << PIPE_PRIM_POLYGON);
1530 }
Alyssa Rosenzweig85e2bb52019-02-08 02:28:12 +00001531
1532 ctx->primconvert = util_primconvert_create(gallium, ctx->draw_modes);
Alyssa Rosenzweig7da251f2019-02-05 04:32:27 +00001533
1534 ctx->blitter = util_blitter_create(gallium);
Alyssa Rosenzweig7005c0d2019-06-23 11:05:10 -07001535 ctx->blitter_wallpaper = util_blitter_create(gallium);
1536
Alyssa Rosenzweig7da251f2019-02-05 04:32:27 +00001537 assert(ctx->blitter);
Alyssa Rosenzweig7005c0d2019-06-23 11:05:10 -07001538 assert(ctx->blitter_wallpaper);
Alyssa Rosenzweig7da251f2019-02-05 04:32:27 +00001539
1540 /* Prepare for render! */
1541
Boris Brezillon2c526992019-09-05 21:41:26 +02001542 panfrost_batch_init(ctx);
Alyssa Rosenzweig7da251f2019-02-05 04:32:27 +00001543 panfrost_invalidate_frame(ctx);
Alyssa Rosenzweig7da251f2019-02-05 04:32:27 +00001544
Alyssa Rosenzweig293f2512020-07-09 13:42:25 -04001545 if (!(dev->quirks & IS_BIFROST)) {
1546 for (unsigned c = 0; c < PIPE_MAX_COLOR_BUFS; ++c)
1547 ctx->blit_blend.rt[c].shaders = _mesa_hash_table_u64_create(ctx);
1548 }
1549
Alyssa Rosenzweigf23cdd42020-07-02 10:07:08 -04001550 /* By default mask everything on */
1551 ctx->sample_mask = ~0;
1552
Alyssa Rosenzweig7da251f2019-02-05 04:32:27 +00001553 return gallium;
1554}