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