2f7fe9e3cc3d39160ee60e7820ab5208273fbbfb
2 * Copyright (C) 2019 Alyssa Rosenzweig
3 * Copyright (C) 2014-2017 Broadcom
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:
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
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
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"
33 panfrost_create_job(struct panfrost_context
*ctx
)
35 struct panfrost_job
*job
= rzalloc(ctx
, struct panfrost_job
);
39 job
->bos
= _mesa_set_create(job
,
41 _mesa_key_pointer_equal
);
43 job
->minx
= job
->miny
= ~0;
44 job
->maxx
= job
->maxy
= 0;
46 util_dynarray_init(&job
->headers
, job
);
47 util_dynarray_init(&job
->gpu_headers
, job
);
53 panfrost_free_job(struct panfrost_context
*ctx
, struct panfrost_job
*job
)
58 set_foreach(job
->bos
, entry
) {
59 struct panfrost_bo
*bo
= (struct panfrost_bo
*)entry
->key
;
60 panfrost_bo_unreference(ctx
->base
.screen
, bo
);
63 _mesa_hash_table_remove_key(ctx
->jobs
, &job
->key
);
72 panfrost_get_job(struct panfrost_context
*ctx
,
73 struct pipe_surface
**cbufs
, struct pipe_surface
*zsbuf
)
75 /* Lookup the job first */
77 struct panfrost_job_key key
= {
87 struct hash_entry
*entry
= _mesa_hash_table_search(ctx
->jobs
, &key
);
92 /* Otherwise, let's create a job */
94 struct panfrost_job
*job
= panfrost_create_job(ctx
);
96 /* Save the created job */
98 memcpy(&job
->key
, &key
, sizeof(key
));
99 _mesa_hash_table_insert(ctx
->jobs
, &job
->key
, job
);
104 /* Get the job corresponding to the FBO we're currently rendering into */
106 struct panfrost_job
*
107 panfrost_get_job_for_fbo(struct panfrost_context
*ctx
)
109 /* If we're wallpapering, we special case to workaround
112 if (ctx
->wallpaper_batch
)
113 return ctx
->wallpaper_batch
;
115 /* If we already began rendering, use that */
120 /* If not, look up the job */
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
);
130 panfrost_job_add_bo(struct panfrost_job
*job
, struct panfrost_bo
*bo
)
135 if (_mesa_set_search(job
->bos
, bo
))
138 panfrost_bo_reference(bo
);
139 _mesa_set_add(job
->bos
, bo
);
143 panfrost_flush_jobs_writing_resource(struct panfrost_context
*panfrost
,
144 struct pipe_resource
*prsc
)
147 struct hash_entry
*entry
= _mesa_hash_table_search(panfrost
->write_jobs
,
150 struct panfrost_job
*job
= entry
->data
;
151 panfrost_job_submit(panfrost
, job
);
158 panfrost_job_submit(struct panfrost_context
*ctx
, struct panfrost_job
*job
)
162 panfrost_scoreboard_link_batch(job
);
164 bool has_draws
= job
->last_job
.gpu
;
165 bool is_scanout
= panfrost_is_scanout(ctx
);
170 ret
= panfrost_drm_submit_vs_fs_job(ctx
, has_draws
, is_scanout
);
173 fprintf(stderr
, "panfrost_job_submit failed: %d\n", ret
);
177 panfrost_job_set_requirements(struct panfrost_context
*ctx
,
178 struct panfrost_job
*job
)
180 if (ctx
->rasterizer
&& ctx
->rasterizer
->base
.multisample
)
181 job
->requirements
|= PAN_REQ_MSAA
;
183 if (ctx
->depth_stencil
&& ctx
->depth_stencil
->depth
.writemask
)
184 job
->requirements
|= PAN_REQ_DEPTH_WRITE
;
188 pan_pack_color(const union pipe_color_union
*color
, enum pipe_format format
)
190 /* Alpha magicked to 1.0 if there is no alpha */
192 bool has_alpha
= util_format_has_alpha(format
);
193 float clear_alpha
= has_alpha
? color
->f
[3] : 1.0f
;
195 /* Packed color depends on the framebuffer format */
197 const struct util_format_description
*desc
=
198 util_format_description(format
);
200 if (util_format_is_rgba8_variant(desc
)) {
201 return (float_to_ubyte(clear_alpha
) << 24) |
202 (float_to_ubyte(color
->f
[2]) << 16) |
203 (float_to_ubyte(color
->f
[1]) << 8) |
204 (float_to_ubyte(color
->f
[0]) << 0);
205 } else if (format
== PIPE_FORMAT_B5G6R5_UNORM
) {
206 /* First, we convert the components to R5, G6, B5 separately */
207 unsigned r5
= CLAMP(color
->f
[0], 0.0, 1.0) * 31.0;
208 unsigned g6
= CLAMP(color
->f
[1], 0.0, 1.0) * 63.0;
209 unsigned b5
= CLAMP(color
->f
[2], 0.0, 1.0) * 31.0;
211 /* Then we pack into a sparse u32. TODO: Why these shifts? */
212 return (b5
<< 25) | (g6
<< 14) | (r5
<< 5);
214 /* Try Gallium's generic default path. Doesn't work for all
215 * formats but it's a good guess. */
217 union util_color out
;
218 util_pack_color(color
->f
, format
, &out
);
226 panfrost_job_clear(struct panfrost_context
*ctx
,
227 struct panfrost_job
*job
,
229 const union pipe_color_union
*color
,
230 double depth
, unsigned stencil
)
233 if (buffers
& PIPE_CLEAR_COLOR
) {
234 enum pipe_format format
= ctx
->pipe_framebuffer
.cbufs
[0]->format
;
235 job
->clear_color
= pan_pack_color(color
, format
);
238 if (buffers
& PIPE_CLEAR_DEPTH
) {
239 job
->clear_depth
= depth
;
242 if (buffers
& PIPE_CLEAR_STENCIL
) {
243 job
->clear_stencil
= stencil
;
246 job
->clear
|= buffers
;
248 /* Clearing affects the entire framebuffer (by definition -- this is
249 * the Gallium clear callback, which clears the whole framebuffer. If
250 * the scissor test were enabled from the GL side, the state tracker
251 * would emit a quad instead and we wouldn't go down this code path) */
253 panfrost_job_union_scissor(job
, 0, 0,
254 ctx
->pipe_framebuffer
.width
,
255 ctx
->pipe_framebuffer
.height
);
259 panfrost_flush_jobs_reading_resource(struct panfrost_context
*panfrost
,
260 struct pipe_resource
*prsc
)
262 struct panfrost_resource
*rsc
= pan_resource(prsc
);
264 panfrost_flush_jobs_writing_resource(panfrost
, prsc
);
266 hash_table_foreach(panfrost
->jobs
, entry
) {
267 struct panfrost_job
*job
= entry
->data
;
269 if (_mesa_set_search(job
->bos
, rsc
->bo
)) {
270 printf("TODO: submit job for flush\n");
271 //panfrost_job_submit(panfrost, job);
278 panfrost_job_compare(const void *a
, const void *b
)
280 return memcmp(a
, b
, sizeof(struct panfrost_job_key
)) == 0;
284 panfrost_job_hash(const void *key
)
286 return _mesa_hash_data(key
, sizeof(struct panfrost_job_key
));
289 /* Given a new bounding rectangle (scissor), let the job cover the union of the
290 * new and old bounding rectangles */
293 panfrost_job_union_scissor(struct panfrost_job
*job
,
294 unsigned minx
, unsigned miny
,
295 unsigned maxx
, unsigned maxy
)
297 job
->minx
= MIN2(job
->minx
, minx
);
298 job
->miny
= MIN2(job
->miny
, miny
);
299 job
->maxx
= MAX2(job
->maxx
, maxx
);
300 job
->maxy
= MAX2(job
->maxy
, maxy
);
304 panfrost_job_init(struct panfrost_context
*ctx
)
306 ctx
->jobs
= _mesa_hash_table_create(ctx
,
308 panfrost_job_compare
);
310 ctx
->write_jobs
= _mesa_hash_table_create(ctx
,
312 _mesa_key_pointer_equal
);