panfrost: Bookkeep transient indices
[mesa.git] / src / gallium / drivers / panfrost / pan_job.c
1 /*
2 * Copyright (C) 2019 Alyssa Rosenzweig
3 * Copyright (C) 2014-2017 Broadcom
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 *
24 */
25
26 #include "pan_context.h"
27 #include "util/hash_table.h"
28 #include "util/ralloc.h"
29 #include "util/u_format.h"
30 #include "util/u_pack_color.h"
31
32 struct panfrost_job *
33 panfrost_create_job(struct panfrost_context *ctx)
34 {
35 struct panfrost_job *job = rzalloc(ctx, struct panfrost_job);
36
37 job->ctx = ctx;
38
39 job->bos = _mesa_set_create(job,
40 _mesa_hash_pointer,
41 _mesa_key_pointer_equal);
42
43 job->minx = job->miny = ~0;
44 job->maxx = job->maxy = 0;
45
46 util_dynarray_init(&job->headers, job);
47 util_dynarray_init(&job->gpu_headers, job);
48 util_dynarray_init(&job->transient_indices, job);
49
50 return job;
51 }
52
53 void
54 panfrost_free_job(struct panfrost_context *ctx, struct panfrost_job *job)
55 {
56 if (!job)
57 return;
58
59 set_foreach(job->bos, entry) {
60 struct panfrost_bo *bo = (struct panfrost_bo *)entry->key;
61 panfrost_bo_unreference(ctx->base.screen, bo);
62 }
63
64 _mesa_hash_table_remove_key(ctx->jobs, &job->key);
65
66 if (ctx->job == job)
67 ctx->job = NULL;
68
69 ralloc_free(job);
70 }
71
72 struct panfrost_job *
73 panfrost_get_job(struct panfrost_context *ctx,
74 struct pipe_surface **cbufs, struct pipe_surface *zsbuf)
75 {
76 /* Lookup the job first */
77
78 struct panfrost_job_key key = {
79 .cbufs = {
80 cbufs[0],
81 cbufs[1],
82 cbufs[2],
83 cbufs[3],
84 },
85 .zsbuf = zsbuf
86 };
87
88 struct hash_entry *entry = _mesa_hash_table_search(ctx->jobs, &key);
89
90 if (entry)
91 return entry->data;
92
93 /* Otherwise, let's create a job */
94
95 struct panfrost_job *job = panfrost_create_job(ctx);
96
97 /* Save the created job */
98
99 memcpy(&job->key, &key, sizeof(key));
100 _mesa_hash_table_insert(ctx->jobs, &job->key, job);
101
102 return job;
103 }
104
105 /* Get the job corresponding to the FBO we're currently rendering into */
106
107 struct panfrost_job *
108 panfrost_get_job_for_fbo(struct panfrost_context *ctx)
109 {
110 /* If we're wallpapering, we special case to workaround
111 * u_blitter abuse */
112
113 if (ctx->wallpaper_batch)
114 return ctx->wallpaper_batch;
115
116 /* If we already began rendering, use that */
117
118 if (ctx->job)
119 return ctx->job;
120
121 /* If not, look up the job */
122
123 struct pipe_surface **cbufs = ctx->pipe_framebuffer.cbufs;
124 struct pipe_surface *zsbuf = ctx->pipe_framebuffer.zsbuf;
125 struct panfrost_job *job = panfrost_get_job(ctx, cbufs, zsbuf);
126
127 return job;
128 }
129
130 void
131 panfrost_job_add_bo(struct panfrost_job *job, struct panfrost_bo *bo)
132 {
133 if (!bo)
134 return;
135
136 if (_mesa_set_search(job->bos, bo))
137 return;
138
139 panfrost_bo_reference(bo);
140 _mesa_set_add(job->bos, bo);
141 }
142
143 void
144 panfrost_flush_jobs_writing_resource(struct panfrost_context *panfrost,
145 struct pipe_resource *prsc)
146 {
147 #if 0
148 struct hash_entry *entry = _mesa_hash_table_search(panfrost->write_jobs,
149 prsc);
150 if (entry) {
151 struct panfrost_job *job = entry->data;
152 panfrost_job_submit(panfrost, job);
153 }
154 #endif
155 /* TODO stub */
156 }
157
158 void
159 panfrost_job_submit(struct panfrost_context *ctx, struct panfrost_job *job)
160 {
161 int ret;
162
163 panfrost_scoreboard_link_batch(job);
164
165 bool has_draws = job->last_job.gpu;
166 bool is_scanout = panfrost_is_scanout(ctx);
167
168 if (!job)
169 return;
170
171 ret = panfrost_drm_submit_vs_fs_job(ctx, has_draws, is_scanout);
172
173 if (ret)
174 fprintf(stderr, "panfrost_job_submit failed: %d\n", ret);
175 }
176
177 void
178 panfrost_job_set_requirements(struct panfrost_context *ctx,
179 struct panfrost_job *job)
180 {
181 if (ctx->rasterizer && ctx->rasterizer->base.multisample)
182 job->requirements |= PAN_REQ_MSAA;
183
184 if (ctx->depth_stencil && ctx->depth_stencil->depth.writemask)
185 job->requirements |= PAN_REQ_DEPTH_WRITE;
186 }
187
188 /* Helper to smear a 32-bit color across 128-bit components */
189
190 static void
191 pan_pack_color_32(uint32_t *packed, uint32_t v)
192 {
193 for (unsigned i = 0; i < 4; ++i)
194 packed[i] = v;
195 }
196
197 static void
198 pan_pack_color_64(uint32_t *packed, uint32_t lo, uint32_t hi)
199 {
200 for (unsigned i = 0; i < 4; i += 2) {
201 packed[i + 0] = lo;
202 packed[i + 1] = hi;
203 }
204 }
205
206 static void
207 pan_pack_color(uint32_t *packed, const union pipe_color_union *color, enum pipe_format format)
208 {
209 /* Alpha magicked to 1.0 if there is no alpha */
210
211 bool has_alpha = util_format_has_alpha(format);
212 float clear_alpha = has_alpha ? color->f[3] : 1.0f;
213
214 /* Packed color depends on the framebuffer format */
215
216 const struct util_format_description *desc =
217 util_format_description(format);
218
219 if (util_format_is_rgba8_variant(desc)) {
220 pan_pack_color_32(packed,
221 (float_to_ubyte(clear_alpha) << 24) |
222 (float_to_ubyte(color->f[2]) << 16) |
223 (float_to_ubyte(color->f[1]) << 8) |
224 (float_to_ubyte(color->f[0]) << 0));
225 } else if (format == PIPE_FORMAT_B5G6R5_UNORM) {
226 /* First, we convert the components to R5, G6, B5 separately */
227 unsigned r5 = CLAMP(color->f[0], 0.0, 1.0) * 31.0;
228 unsigned g6 = CLAMP(color->f[1], 0.0, 1.0) * 63.0;
229 unsigned b5 = CLAMP(color->f[2], 0.0, 1.0) * 31.0;
230
231 /* Then we pack into a sparse u32. TODO: Why these shifts? */
232 pan_pack_color_32(packed, (b5 << 25) | (g6 << 14) | (r5 << 5));
233 } else if (format == PIPE_FORMAT_B4G4R4A4_UNORM) {
234 /* We scale the components against 0xF0 (=240.0), rather than 0xFF */
235 unsigned r4 = CLAMP(color->f[0], 0.0, 1.0) * 240.0;
236 unsigned g4 = CLAMP(color->f[1], 0.0, 1.0) * 240.0;
237 unsigned b4 = CLAMP(color->f[2], 0.0, 1.0) * 240.0;
238 unsigned a4 = CLAMP(clear_alpha, 0.0, 1.0) * 240.0;
239
240 /* Pack on *byte* intervals */
241 pan_pack_color_32(packed, (a4 << 24) | (b4 << 16) | (g4 << 8) | r4);
242 } else if (format == PIPE_FORMAT_B5G5R5A1_UNORM) {
243 /* Scale as expected but shift oddly */
244 unsigned r5 = round(CLAMP(color->f[0], 0.0, 1.0)) * 31.0;
245 unsigned g5 = round(CLAMP(color->f[1], 0.0, 1.0)) * 31.0;
246 unsigned b5 = round(CLAMP(color->f[2], 0.0, 1.0)) * 31.0;
247 unsigned a1 = round(CLAMP(clear_alpha, 0.0, 1.0)) * 1.0;
248
249 pan_pack_color_32(packed, (a1 << 31) | (b5 << 25) | (g5 << 15) | (r5 << 5));
250 } else {
251 /* Try Gallium's generic default path. Doesn't work for all
252 * formats but it's a good guess. */
253
254 union util_color out;
255
256 if (util_format_is_pure_integer(format)) {
257 memcpy(out.ui, color->ui, 16);
258 } else {
259 util_pack_color(color->f, format, &out);
260 }
261
262 unsigned size = util_format_get_blocksize(format);
263
264 if (size == 1) {
265 unsigned b = out.ui[0];
266 unsigned s = b | (b << 8);
267 pan_pack_color_32(packed, s | (s << 16));
268 } else if (size == 2)
269 pan_pack_color_32(packed, out.ui[0] | (out.ui[0] << 16));
270 else if (size == 4)
271 pan_pack_color_32(packed, out.ui[0]);
272 else if (size == 8)
273 pan_pack_color_64(packed, out.ui[0], out.ui[1]);
274 else if (size == 16)
275 memcpy(packed, out.ui, 16);
276 else
277 unreachable("Unknown generic format size packing clear colour");
278 }
279 }
280
281 void
282 panfrost_job_clear(struct panfrost_context *ctx,
283 struct panfrost_job *job,
284 unsigned buffers,
285 const union pipe_color_union *color,
286 double depth, unsigned stencil)
287
288 {
289 if (buffers & PIPE_CLEAR_COLOR) {
290 for (unsigned i = 0; i < PIPE_MAX_COLOR_BUFS; ++i) {
291 if (!(buffers & (PIPE_CLEAR_COLOR0 << i)))
292 continue;
293
294 enum pipe_format format = ctx->pipe_framebuffer.cbufs[i]->format;
295 pan_pack_color(job->clear_color[i], color, format);
296 }
297 }
298
299 if (buffers & PIPE_CLEAR_DEPTH) {
300 job->clear_depth = depth;
301 }
302
303 if (buffers & PIPE_CLEAR_STENCIL) {
304 job->clear_stencil = stencil;
305 }
306
307 job->clear |= buffers;
308
309 /* Clearing affects the entire framebuffer (by definition -- this is
310 * the Gallium clear callback, which clears the whole framebuffer. If
311 * the scissor test were enabled from the GL side, the state tracker
312 * would emit a quad instead and we wouldn't go down this code path) */
313
314 panfrost_job_union_scissor(job, 0, 0,
315 ctx->pipe_framebuffer.width,
316 ctx->pipe_framebuffer.height);
317 }
318
319 void
320 panfrost_flush_jobs_reading_resource(struct panfrost_context *panfrost,
321 struct pipe_resource *prsc)
322 {
323 struct panfrost_resource *rsc = pan_resource(prsc);
324
325 panfrost_flush_jobs_writing_resource(panfrost, prsc);
326
327 hash_table_foreach(panfrost->jobs, entry) {
328 struct panfrost_job *job = entry->data;
329
330 if (_mesa_set_search(job->bos, rsc->bo)) {
331 printf("TODO: submit job for flush\n");
332 //panfrost_job_submit(panfrost, job);
333 continue;
334 }
335 }
336 }
337
338 static bool
339 panfrost_job_compare(const void *a, const void *b)
340 {
341 return memcmp(a, b, sizeof(struct panfrost_job_key)) == 0;
342 }
343
344 static uint32_t
345 panfrost_job_hash(const void *key)
346 {
347 return _mesa_hash_data(key, sizeof(struct panfrost_job_key));
348 }
349
350 /* Given a new bounding rectangle (scissor), let the job cover the union of the
351 * new and old bounding rectangles */
352
353 void
354 panfrost_job_union_scissor(struct panfrost_job *job,
355 unsigned minx, unsigned miny,
356 unsigned maxx, unsigned maxy)
357 {
358 job->minx = MIN2(job->minx, minx);
359 job->miny = MIN2(job->miny, miny);
360 job->maxx = MAX2(job->maxx, maxx);
361 job->maxy = MAX2(job->maxy, maxy);
362 }
363
364 void
365 panfrost_job_init(struct panfrost_context *ctx)
366 {
367 ctx->jobs = _mesa_hash_table_create(ctx,
368 panfrost_job_hash,
369 panfrost_job_compare);
370
371 ctx->write_jobs = _mesa_hash_table_create(ctx,
372 _mesa_hash_pointer,
373 _mesa_key_pointer_equal);
374 }