panfrost: Implement command stream scoreboarding
[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
31 struct panfrost_job *
32 panfrost_create_job(struct panfrost_context *ctx)
33 {
34 struct panfrost_job *job = rzalloc(ctx, struct panfrost_job);
35
36 job->ctx = ctx;
37
38 job->bos = _mesa_set_create(job,
39 _mesa_hash_pointer,
40 _mesa_key_pointer_equal);
41
42 job->minx = job->miny = ~0;
43 job->maxx = job->maxy = 0;
44
45 util_dynarray_init(&job->headers, job);
46 util_dynarray_init(&job->gpu_headers, job);
47
48 return job;
49 }
50
51 void
52 panfrost_free_job(struct panfrost_context *ctx, struct panfrost_job *job)
53 {
54 if (!job)
55 return;
56
57 set_foreach(job->bos, entry) {
58 struct panfrost_bo *bo = (struct panfrost_bo *)entry->key;
59 panfrost_bo_unreference(ctx->base.screen, bo);
60 }
61
62 _mesa_hash_table_remove_key(ctx->jobs, &job->key);
63
64 if (ctx->job == job)
65 ctx->job = NULL;
66
67 ralloc_free(job);
68 }
69
70 struct panfrost_job *
71 panfrost_get_job(struct panfrost_context *ctx,
72 struct pipe_surface **cbufs, struct pipe_surface *zsbuf)
73 {
74 /* Lookup the job first */
75
76 struct panfrost_job_key key = {
77 .cbufs = {
78 cbufs[0],
79 cbufs[1],
80 cbufs[2],
81 cbufs[3],
82 },
83 .zsbuf = zsbuf
84 };
85
86 struct hash_entry *entry = _mesa_hash_table_search(ctx->jobs, &key);
87
88 if (entry)
89 return entry->data;
90
91 /* Otherwise, let's create a job */
92
93 struct panfrost_job *job = panfrost_create_job(ctx);
94
95 /* Save the created job */
96
97 memcpy(&job->key, &key, sizeof(key));
98 _mesa_hash_table_insert(ctx->jobs, &job->key, job);
99
100 return job;
101 }
102
103 /* Get the job corresponding to the FBO we're currently rendering into */
104
105 struct panfrost_job *
106 panfrost_get_job_for_fbo(struct panfrost_context *ctx)
107 {
108 /* If we're wallpapering, we special case to workaround
109 * u_blitter abuse */
110
111 if (ctx->wallpaper_batch)
112 return ctx->wallpaper_batch;
113
114 /* If we already began rendering, use that */
115
116 if (ctx->job)
117 return ctx->job;
118
119 /* If not, look up the job */
120
121 struct pipe_surface **cbufs = ctx->pipe_framebuffer.cbufs;
122 struct pipe_surface *zsbuf = ctx->pipe_framebuffer.zsbuf;
123 struct panfrost_job *job = panfrost_get_job(ctx, cbufs, zsbuf);
124
125 return job;
126 }
127
128 void
129 panfrost_job_add_bo(struct panfrost_job *job, struct panfrost_bo *bo)
130 {
131 if (!bo)
132 return;
133
134 if (_mesa_set_search(job->bos, bo))
135 return;
136
137 panfrost_bo_reference(bo);
138 _mesa_set_add(job->bos, bo);
139 }
140
141 void
142 panfrost_flush_jobs_writing_resource(struct panfrost_context *panfrost,
143 struct pipe_resource *prsc)
144 {
145 #if 0
146 struct hash_entry *entry = _mesa_hash_table_search(panfrost->write_jobs,
147 prsc);
148 if (entry) {
149 struct panfrost_job *job = entry->data;
150 panfrost_job_submit(panfrost, job);
151 }
152 #endif
153 /* TODO stub */
154 }
155
156 void
157 panfrost_job_submit(struct panfrost_context *ctx, struct panfrost_job *job)
158 {
159 struct pipe_context *gallium = (struct pipe_context *) ctx;
160 struct panfrost_screen *screen = pan_screen(gallium->screen);
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 = screen->driver->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 static uint32_t
189 pan_pack_color(const union pipe_color_union *color, enum pipe_format format)
190 {
191 /* Alpha magicked to 1.0 if there is no alpha */
192
193 bool has_alpha = util_format_has_alpha(format);
194 float clear_alpha = has_alpha ? color->f[3] : 1.0f;
195
196 /* Packed color depends on the framebuffer format */
197
198 const struct util_format_description *desc =
199 util_format_description(format);
200
201 if (util_format_is_rgba8_variant(desc)) {
202 return (float_to_ubyte(clear_alpha) << 24) |
203 (float_to_ubyte(color->f[2]) << 16) |
204 (float_to_ubyte(color->f[1]) << 8) |
205 (float_to_ubyte(color->f[0]) << 0);
206 } else if (format == PIPE_FORMAT_B5G6R5_UNORM) {
207 /* First, we convert the components to R5, G6, B5 separately */
208 unsigned r5 = CLAMP(color->f[0], 0.0, 1.0) * 31.0;
209 unsigned g6 = CLAMP(color->f[1], 0.0, 1.0) * 63.0;
210 unsigned b5 = CLAMP(color->f[2], 0.0, 1.0) * 31.0;
211
212 /* Then we pack into a sparse u32. TODO: Why these shifts? */
213 return (b5 << 25) | (g6 << 14) | (r5 << 5);
214 } else {
215 /* Unknown format */
216 assert(0);
217 }
218
219 return 0;
220 }
221
222 void
223 panfrost_job_clear(struct panfrost_context *ctx,
224 struct panfrost_job *job,
225 unsigned buffers,
226 const union pipe_color_union *color,
227 double depth, unsigned stencil)
228
229 {
230 if (buffers & PIPE_CLEAR_COLOR) {
231 enum pipe_format format = ctx->pipe_framebuffer.cbufs[0]->format;
232 job->clear_color = pan_pack_color(color, format);
233 }
234
235 if (buffers & PIPE_CLEAR_DEPTH) {
236 job->clear_depth = depth;
237 }
238
239 if (buffers & PIPE_CLEAR_STENCIL) {
240 job->clear_stencil = stencil;
241 }
242
243 job->clear |= buffers;
244
245 /* Clearing affects the entire framebuffer (by definition -- this is
246 * the Gallium clear callback, which clears the whole framebuffer. If
247 * the scissor test were enabled from the GL side, the state tracker
248 * would emit a quad instead and we wouldn't go down this code path) */
249
250 panfrost_job_union_scissor(job, 0, 0,
251 ctx->pipe_framebuffer.width,
252 ctx->pipe_framebuffer.height);
253 }
254
255 void
256 panfrost_flush_jobs_reading_resource(struct panfrost_context *panfrost,
257 struct pipe_resource *prsc)
258 {
259 struct panfrost_resource *rsc = pan_resource(prsc);
260
261 panfrost_flush_jobs_writing_resource(panfrost, prsc);
262
263 hash_table_foreach(panfrost->jobs, entry) {
264 struct panfrost_job *job = entry->data;
265
266 if (_mesa_set_search(job->bos, rsc->bo)) {
267 printf("TODO: submit job for flush\n");
268 //panfrost_job_submit(panfrost, job);
269 continue;
270 }
271 }
272 }
273
274 static bool
275 panfrost_job_compare(const void *a, const void *b)
276 {
277 return memcmp(a, b, sizeof(struct panfrost_job_key)) == 0;
278 }
279
280 static uint32_t
281 panfrost_job_hash(const void *key)
282 {
283 return _mesa_hash_data(key, sizeof(struct panfrost_job_key));
284 }
285
286 /* Given a new bounding rectangle (scissor), let the job cover the union of the
287 * new and old bounding rectangles */
288
289 void
290 panfrost_job_union_scissor(struct panfrost_job *job,
291 unsigned minx, unsigned miny,
292 unsigned maxx, unsigned maxy)
293 {
294 job->minx = MIN2(job->minx, minx);
295 job->miny = MIN2(job->miny, miny);
296 job->maxx = MAX2(job->maxx, maxx);
297 job->maxy = MAX2(job->maxy, maxy);
298 }
299
300 void
301 panfrost_job_init(struct panfrost_context *ctx)
302 {
303 ctx->jobs = _mesa_hash_table_create(ctx,
304 panfrost_job_hash,
305 panfrost_job_compare);
306
307 ctx->write_jobs = _mesa_hash_table_create(ctx,
308 _mesa_hash_pointer,
309 _mesa_key_pointer_equal);
310 }