6838050e57575d0ed636dec999deaeb324948eae
[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
49 return job;
50 }
51
52 void
53 panfrost_free_job(struct panfrost_context *ctx, struct panfrost_job *job)
54 {
55 if (!job)
56 return;
57
58 set_foreach(job->bos, entry) {
59 struct panfrost_bo *bo = (struct panfrost_bo *)entry->key;
60 panfrost_bo_unreference(ctx->base.screen, bo);
61 }
62
63 _mesa_hash_table_remove_key(ctx->jobs, &job->key);
64
65 if (ctx->job == job)
66 ctx->job = NULL;
67
68 ralloc_free(job);
69 }
70
71 struct panfrost_job *
72 panfrost_get_job(struct panfrost_context *ctx,
73 struct pipe_surface **cbufs, struct pipe_surface *zsbuf)
74 {
75 /* Lookup the job first */
76
77 struct panfrost_job_key key = {
78 .cbufs = {
79 cbufs[0],
80 cbufs[1],
81 cbufs[2],
82 cbufs[3],
83 },
84 .zsbuf = zsbuf
85 };
86
87 struct hash_entry *entry = _mesa_hash_table_search(ctx->jobs, &key);
88
89 if (entry)
90 return entry->data;
91
92 /* Otherwise, let's create a job */
93
94 struct panfrost_job *job = panfrost_create_job(ctx);
95
96 /* Save the created job */
97
98 memcpy(&job->key, &key, sizeof(key));
99 _mesa_hash_table_insert(ctx->jobs, &job->key, job);
100
101 return job;
102 }
103
104 /* Get the job corresponding to the FBO we're currently rendering into */
105
106 struct panfrost_job *
107 panfrost_get_job_for_fbo(struct panfrost_context *ctx)
108 {
109 /* If we're wallpapering, we special case to workaround
110 * u_blitter abuse */
111
112 if (ctx->wallpaper_batch)
113 return ctx->wallpaper_batch;
114
115 /* If we already began rendering, use that */
116
117 if (ctx->job)
118 return ctx->job;
119
120 /* If not, look up the job */
121
122 struct pipe_surface **cbufs = ctx->pipe_framebuffer.cbufs;
123 struct pipe_surface *zsbuf = ctx->pipe_framebuffer.zsbuf;
124 struct panfrost_job *job = panfrost_get_job(ctx, cbufs, zsbuf);
125
126 return job;
127 }
128
129 void
130 panfrost_job_add_bo(struct panfrost_job *job, struct panfrost_bo *bo)
131 {
132 if (!bo)
133 return;
134
135 if (_mesa_set_search(job->bos, bo))
136 return;
137
138 panfrost_bo_reference(bo);
139 _mesa_set_add(job->bos, bo);
140 }
141
142 void
143 panfrost_flush_jobs_writing_resource(struct panfrost_context *panfrost,
144 struct pipe_resource *prsc)
145 {
146 #if 0
147 struct hash_entry *entry = _mesa_hash_table_search(panfrost->write_jobs,
148 prsc);
149 if (entry) {
150 struct panfrost_job *job = entry->data;
151 panfrost_job_submit(panfrost, job);
152 }
153 #endif
154 /* TODO stub */
155 }
156
157 void
158 panfrost_job_submit(struct panfrost_context *ctx, struct panfrost_job *job)
159 {
160 int ret;
161
162 panfrost_scoreboard_link_batch(job);
163
164 bool has_draws = job->last_job.gpu;
165 bool is_scanout = panfrost_is_scanout(ctx);
166
167 if (!job)
168 return;
169
170 ret = panfrost_drm_submit_vs_fs_job(ctx, has_draws, is_scanout);
171
172 if (ret)
173 fprintf(stderr, "panfrost_job_submit failed: %d\n", ret);
174 }
175
176 void
177 panfrost_job_set_requirements(struct panfrost_context *ctx,
178 struct panfrost_job *job)
179 {
180 if (ctx->rasterizer && ctx->rasterizer->base.multisample)
181 job->requirements |= PAN_REQ_MSAA;
182
183 if (ctx->depth_stencil && ctx->depth_stencil->depth.writemask)
184 job->requirements |= PAN_REQ_DEPTH_WRITE;
185 }
186
187 /* Helper to smear a 32-bit color across 128-bit components */
188
189 static void
190 pan_pack_color_32(uint32_t *packed, uint32_t v)
191 {
192 for (unsigned i = 0; i < 4; ++i)
193 packed[i] = v;
194 }
195
196 static void
197 pan_pack_color_64(uint32_t *packed, uint32_t lo, uint32_t hi)
198 {
199 for (unsigned i = 0; i < 4; i += 2) {
200 packed[i + 0] = lo;
201 packed[i + 1] = hi;
202 }
203 }
204
205 static void
206 pan_pack_color(uint32_t *packed, const union pipe_color_union *color, enum pipe_format format)
207 {
208 /* Alpha magicked to 1.0 if there is no alpha */
209
210 bool has_alpha = util_format_has_alpha(format);
211 float clear_alpha = has_alpha ? color->f[3] : 1.0f;
212
213 /* Packed color depends on the framebuffer format */
214
215 const struct util_format_description *desc =
216 util_format_description(format);
217
218 if (util_format_is_rgba8_variant(desc)) {
219 pan_pack_color_32(packed,
220 (float_to_ubyte(clear_alpha) << 24) |
221 (float_to_ubyte(color->f[2]) << 16) |
222 (float_to_ubyte(color->f[1]) << 8) |
223 (float_to_ubyte(color->f[0]) << 0));
224 } else if (format == PIPE_FORMAT_B5G6R5_UNORM) {
225 /* First, we convert the components to R5, G6, B5 separately */
226 unsigned r5 = CLAMP(color->f[0], 0.0, 1.0) * 31.0;
227 unsigned g6 = CLAMP(color->f[1], 0.0, 1.0) * 63.0;
228 unsigned b5 = CLAMP(color->f[2], 0.0, 1.0) * 31.0;
229
230 /* Then we pack into a sparse u32. TODO: Why these shifts? */
231 pan_pack_color_32(packed, (b5 << 25) | (g6 << 14) | (r5 << 5));
232 } else if (format == PIPE_FORMAT_B4G4R4A4_UNORM) {
233 /* We scale the components against 0xF0 (=240.0), rather than 0xFF */
234 unsigned r4 = CLAMP(color->f[0], 0.0, 1.0) * 240.0;
235 unsigned g4 = CLAMP(color->f[1], 0.0, 1.0) * 240.0;
236 unsigned b4 = CLAMP(color->f[2], 0.0, 1.0) * 240.0;
237 unsigned a4 = CLAMP(clear_alpha, 0.0, 1.0) * 240.0;
238
239 /* Pack on *byte* intervals */
240 pan_pack_color_32(packed, (a4 << 24) | (b4 << 16) | (g4 << 8) | r4);
241 } else if (format == PIPE_FORMAT_B5G5R5A1_UNORM) {
242 /* Scale as expected but shift oddly */
243 unsigned r5 = round(CLAMP(color->f[0], 0.0, 1.0)) * 31.0;
244 unsigned g5 = round(CLAMP(color->f[1], 0.0, 1.0)) * 31.0;
245 unsigned b5 = round(CLAMP(color->f[2], 0.0, 1.0)) * 31.0;
246 unsigned a1 = round(CLAMP(clear_alpha, 0.0, 1.0)) * 1.0;
247
248 pan_pack_color_32(packed, (a1 << 31) | (b5 << 25) | (g5 << 15) | (r5 << 5));
249 } else {
250 /* Try Gallium's generic default path. Doesn't work for all
251 * formats but it's a good guess. */
252
253 union util_color out;
254
255 if (util_format_is_pure_integer(format)) {
256 memcpy(out.ui, color->ui, 16);
257 } else {
258 util_pack_color(color->f, format, &out);
259 }
260
261 unsigned size = util_format_get_blocksize(format);
262
263 if (size == 1) {
264 unsigned b = out.ui[0];
265 unsigned s = b | (b << 8);
266 pan_pack_color_32(packed, s | (s << 16));
267 } else if (size == 2)
268 pan_pack_color_32(packed, out.ui[0] | (out.ui[0] << 16));
269 else if (size == 4)
270 pan_pack_color_32(packed, out.ui[0]);
271 else if (size == 8)
272 pan_pack_color_64(packed, out.ui[0], out.ui[1]);
273 else if (size == 16)
274 memcpy(packed, out.ui, 16);
275 else
276 unreachable("Unknown generic format size packing clear colour");
277 }
278 }
279
280 void
281 panfrost_job_clear(struct panfrost_context *ctx,
282 struct panfrost_job *job,
283 unsigned buffers,
284 const union pipe_color_union *color,
285 double depth, unsigned stencil)
286
287 {
288 if (buffers & PIPE_CLEAR_COLOR) {
289 for (unsigned i = 0; i < PIPE_MAX_COLOR_BUFS; ++i) {
290 if (!(buffers & (PIPE_CLEAR_COLOR0 << i)))
291 continue;
292
293 enum pipe_format format = ctx->pipe_framebuffer.cbufs[i]->format;
294 pan_pack_color(job->clear_color[i], color, format);
295 }
296 }
297
298 if (buffers & PIPE_CLEAR_DEPTH) {
299 job->clear_depth = depth;
300 }
301
302 if (buffers & PIPE_CLEAR_STENCIL) {
303 job->clear_stencil = stencil;
304 }
305
306 job->clear |= buffers;
307
308 /* Clearing affects the entire framebuffer (by definition -- this is
309 * the Gallium clear callback, which clears the whole framebuffer. If
310 * the scissor test were enabled from the GL side, the state tracker
311 * would emit a quad instead and we wouldn't go down this code path) */
312
313 panfrost_job_union_scissor(job, 0, 0,
314 ctx->pipe_framebuffer.width,
315 ctx->pipe_framebuffer.height);
316 }
317
318 void
319 panfrost_flush_jobs_reading_resource(struct panfrost_context *panfrost,
320 struct pipe_resource *prsc)
321 {
322 struct panfrost_resource *rsc = pan_resource(prsc);
323
324 panfrost_flush_jobs_writing_resource(panfrost, prsc);
325
326 hash_table_foreach(panfrost->jobs, entry) {
327 struct panfrost_job *job = entry->data;
328
329 if (_mesa_set_search(job->bos, rsc->bo)) {
330 printf("TODO: submit job for flush\n");
331 //panfrost_job_submit(panfrost, job);
332 continue;
333 }
334 }
335 }
336
337 static bool
338 panfrost_job_compare(const void *a, const void *b)
339 {
340 return memcmp(a, b, sizeof(struct panfrost_job_key)) == 0;
341 }
342
343 static uint32_t
344 panfrost_job_hash(const void *key)
345 {
346 return _mesa_hash_data(key, sizeof(struct panfrost_job_key));
347 }
348
349 /* Given a new bounding rectangle (scissor), let the job cover the union of the
350 * new and old bounding rectangles */
351
352 void
353 panfrost_job_union_scissor(struct panfrost_job *job,
354 unsigned minx, unsigned miny,
355 unsigned maxx, unsigned maxy)
356 {
357 job->minx = MIN2(job->minx, minx);
358 job->miny = MIN2(job->miny, miny);
359 job->maxx = MAX2(job->maxx, maxx);
360 job->maxy = MAX2(job->maxy, maxy);
361 }
362
363 void
364 panfrost_job_init(struct panfrost_context *ctx)
365 {
366 ctx->jobs = _mesa_hash_table_create(ctx,
367 panfrost_job_hash,
368 panfrost_job_compare);
369
370 ctx->write_jobs = _mesa_hash_table_create(ctx,
371 _mesa_hash_pointer,
372 _mesa_key_pointer_equal);
373 }