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