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