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
28 #include "pan_context.h"
29 #include "util/hash_table.h"
30 #include "util/ralloc.h"
31 #include "util/u_format.h"
32 #include "util/u_pack_color.h"
34 struct panfrost_batch
*
35 panfrost_create_batch(struct panfrost_context
*ctx
,
36 const struct pipe_framebuffer_state
*key
)
38 struct panfrost_batch
*batch
= rzalloc(ctx
, struct panfrost_batch
);
42 batch
->bos
= _mesa_set_create(batch
,
44 _mesa_key_pointer_equal
);
46 batch
->minx
= batch
->miny
= ~0;
47 batch
->maxx
= batch
->maxy
= 0;
48 batch
->transient_offset
= 0;
50 util_dynarray_init(&batch
->headers
, batch
);
51 util_dynarray_init(&batch
->gpu_headers
, batch
);
52 util_copy_framebuffer_state(&batch
->key
, key
);
58 panfrost_free_batch(struct panfrost_batch
*batch
)
63 struct panfrost_context
*ctx
= batch
->ctx
;
65 set_foreach(batch
->bos
, entry
) {
66 struct panfrost_bo
*bo
= (struct panfrost_bo
*)entry
->key
;
67 panfrost_bo_unreference(ctx
->base
.screen
, bo
);
70 /* Unreference the polygon list */
71 panfrost_bo_unreference(ctx
->base
.screen
, batch
->polygon_list
);
73 _mesa_hash_table_remove_key(ctx
->batches
, &batch
->key
);
75 if (ctx
->batch
== batch
)
78 util_unreference_framebuffer_state(&batch
->key
);
82 struct panfrost_batch
*
83 panfrost_get_batch(struct panfrost_context
*ctx
,
84 const struct pipe_framebuffer_state
*key
)
86 /* Lookup the job first */
87 struct hash_entry
*entry
= _mesa_hash_table_search(ctx
->batches
, key
);
92 /* Otherwise, let's create a job */
94 struct panfrost_batch
*batch
= panfrost_create_batch(ctx
, key
);
96 /* Save the created job */
97 _mesa_hash_table_insert(ctx
->batches
, &batch
->key
, batch
);
102 /* Get the job corresponding to the FBO we're currently rendering into */
104 struct panfrost_batch
*
105 panfrost_get_batch_for_fbo(struct panfrost_context
*ctx
)
107 /* If we're wallpapering, we special case to workaround
110 if (ctx
->wallpaper_batch
)
111 return ctx
->wallpaper_batch
;
113 /* If we already began rendering, use that */
116 assert(util_framebuffer_state_equal(&ctx
->batch
->key
,
117 &ctx
->pipe_framebuffer
));
121 /* If not, look up the job */
122 struct panfrost_batch
*batch
= panfrost_get_batch(ctx
,
123 &ctx
->pipe_framebuffer
);
125 /* Set this job as the current FBO job. Will be reset when updating the
126 * FB state and when submitting or releasing a job.
133 panfrost_batch_add_bo(struct panfrost_batch
*batch
, struct panfrost_bo
*bo
)
138 if (_mesa_set_search(batch
->bos
, bo
))
141 panfrost_bo_reference(bo
);
142 _mesa_set_add(batch
->bos
, bo
);
145 /* Returns the polygon list's GPU address if available, or otherwise allocates
146 * the polygon list. It's perfectly fast to use allocate/free BO directly,
147 * since we'll hit the BO cache and this is one-per-batch anyway. */
150 panfrost_batch_get_polygon_list(struct panfrost_batch
*batch
, unsigned size
)
152 if (batch
->polygon_list
) {
153 assert(batch
->polygon_list
->size
>= size
);
155 struct panfrost_screen
*screen
= pan_screen(batch
->ctx
->base
.screen
);
157 /* Create the BO as invisible, as there's no reason to map */
159 batch
->polygon_list
= panfrost_drm_create_bo(screen
,
160 size
, PAN_ALLOCATE_INVISIBLE
);
163 return batch
->polygon_list
->gpu
;
167 panfrost_batch_submit(struct panfrost_batch
*batch
)
171 struct panfrost_context
*ctx
= batch
->ctx
;
174 panfrost_scoreboard_link_batch(batch
);
176 bool has_draws
= batch
->last_job
.gpu
;
178 ret
= panfrost_drm_submit_vs_fs_batch(batch
, has_draws
);
181 fprintf(stderr
, "panfrost_batch_submit failed: %d\n", ret
);
183 /* The job has been submitted, let's invalidate the current FBO job
186 assert(!ctx
->batch
|| batch
== ctx
->batch
);
189 /* Remove the job from the ctx->batches set so that future
190 * panfrost_get_batch() calls don't see it.
191 * We must reset the job key to avoid removing another valid entry when
194 _mesa_hash_table_remove_key(ctx
->batches
, &batch
->key
);
195 memset(&batch
->key
, 0, sizeof(batch
->key
));
199 panfrost_batch_set_requirements(struct panfrost_batch
*batch
)
201 struct panfrost_context
*ctx
= batch
->ctx
;
203 if (ctx
->rasterizer
&& ctx
->rasterizer
->base
.multisample
)
204 batch
->requirements
|= PAN_REQ_MSAA
;
206 if (ctx
->depth_stencil
&& ctx
->depth_stencil
->depth
.writemask
)
207 batch
->requirements
|= PAN_REQ_DEPTH_WRITE
;
210 /* Helper to smear a 32-bit color across 128-bit components */
213 pan_pack_color_32(uint32_t *packed
, uint32_t v
)
215 for (unsigned i
= 0; i
< 4; ++i
)
220 pan_pack_color_64(uint32_t *packed
, uint32_t lo
, uint32_t hi
)
222 for (unsigned i
= 0; i
< 4; i
+= 2) {
229 pan_pack_color(uint32_t *packed
, const union pipe_color_union
*color
, enum pipe_format format
)
231 /* Alpha magicked to 1.0 if there is no alpha */
233 bool has_alpha
= util_format_has_alpha(format
);
234 float clear_alpha
= has_alpha
? color
->f
[3] : 1.0f
;
236 /* Packed color depends on the framebuffer format */
238 const struct util_format_description
*desc
=
239 util_format_description(format
);
241 if (util_format_is_rgba8_variant(desc
)) {
242 pan_pack_color_32(packed
,
243 (float_to_ubyte(clear_alpha
) << 24) |
244 (float_to_ubyte(color
->f
[2]) << 16) |
245 (float_to_ubyte(color
->f
[1]) << 8) |
246 (float_to_ubyte(color
->f
[0]) << 0));
247 } else if (format
== PIPE_FORMAT_B5G6R5_UNORM
) {
248 /* First, we convert the components to R5, G6, B5 separately */
249 unsigned r5
= CLAMP(color
->f
[0], 0.0, 1.0) * 31.0;
250 unsigned g6
= CLAMP(color
->f
[1], 0.0, 1.0) * 63.0;
251 unsigned b5
= CLAMP(color
->f
[2], 0.0, 1.0) * 31.0;
253 /* Then we pack into a sparse u32. TODO: Why these shifts? */
254 pan_pack_color_32(packed
, (b5
<< 25) | (g6
<< 14) | (r5
<< 5));
255 } else if (format
== PIPE_FORMAT_B4G4R4A4_UNORM
) {
256 /* We scale the components against 0xF0 (=240.0), rather than 0xFF */
257 unsigned r4
= CLAMP(color
->f
[0], 0.0, 1.0) * 240.0;
258 unsigned g4
= CLAMP(color
->f
[1], 0.0, 1.0) * 240.0;
259 unsigned b4
= CLAMP(color
->f
[2], 0.0, 1.0) * 240.0;
260 unsigned a4
= CLAMP(clear_alpha
, 0.0, 1.0) * 240.0;
262 /* Pack on *byte* intervals */
263 pan_pack_color_32(packed
, (a4
<< 24) | (b4
<< 16) | (g4
<< 8) | r4
);
264 } else if (format
== PIPE_FORMAT_B5G5R5A1_UNORM
) {
265 /* Scale as expected but shift oddly */
266 unsigned r5
= round(CLAMP(color
->f
[0], 0.0, 1.0)) * 31.0;
267 unsigned g5
= round(CLAMP(color
->f
[1], 0.0, 1.0)) * 31.0;
268 unsigned b5
= round(CLAMP(color
->f
[2], 0.0, 1.0)) * 31.0;
269 unsigned a1
= round(CLAMP(clear_alpha
, 0.0, 1.0)) * 1.0;
271 pan_pack_color_32(packed
, (a1
<< 31) | (b5
<< 25) | (g5
<< 15) | (r5
<< 5));
273 /* Try Gallium's generic default path. Doesn't work for all
274 * formats but it's a good guess. */
276 union util_color out
;
278 if (util_format_is_pure_integer(format
)) {
279 memcpy(out
.ui
, color
->ui
, 16);
281 util_pack_color(color
->f
, format
, &out
);
284 unsigned size
= util_format_get_blocksize(format
);
287 unsigned b
= out
.ui
[0];
288 unsigned s
= b
| (b
<< 8);
289 pan_pack_color_32(packed
, s
| (s
<< 16));
290 } else if (size
== 2)
291 pan_pack_color_32(packed
, out
.ui
[0] | (out
.ui
[0] << 16));
293 pan_pack_color_32(packed
, out
.ui
[0]);
295 pan_pack_color_64(packed
, out
.ui
[0], out
.ui
[1]);
297 memcpy(packed
, out
.ui
, 16);
299 unreachable("Unknown generic format size packing clear colour");
304 panfrost_batch_clear(struct panfrost_batch
*batch
,
306 const union pipe_color_union
*color
,
307 double depth
, unsigned stencil
)
309 struct panfrost_context
*ctx
= batch
->ctx
;
311 if (buffers
& PIPE_CLEAR_COLOR
) {
312 for (unsigned i
= 0; i
< PIPE_MAX_COLOR_BUFS
; ++i
) {
313 if (!(buffers
& (PIPE_CLEAR_COLOR0
<< i
)))
316 enum pipe_format format
= ctx
->pipe_framebuffer
.cbufs
[i
]->format
;
317 pan_pack_color(batch
->clear_color
[i
], color
, format
);
321 if (buffers
& PIPE_CLEAR_DEPTH
) {
322 batch
->clear_depth
= depth
;
325 if (buffers
& PIPE_CLEAR_STENCIL
) {
326 batch
->clear_stencil
= stencil
;
329 batch
->clear
|= buffers
;
331 /* Clearing affects the entire framebuffer (by definition -- this is
332 * the Gallium clear callback, which clears the whole framebuffer. If
333 * the scissor test were enabled from the GL side, the state tracker
334 * would emit a quad instead and we wouldn't go down this code path) */
336 panfrost_batch_union_scissor(batch
, 0, 0,
337 ctx
->pipe_framebuffer
.width
,
338 ctx
->pipe_framebuffer
.height
);
342 panfrost_batch_compare(const void *a
, const void *b
)
344 return util_framebuffer_state_equal(a
, b
);
348 panfrost_batch_hash(const void *key
)
350 return _mesa_hash_data(key
, sizeof(struct pipe_framebuffer_state
));
353 /* Given a new bounding rectangle (scissor), let the job cover the union of the
354 * new and old bounding rectangles */
357 panfrost_batch_union_scissor(struct panfrost_batch
*batch
,
358 unsigned minx
, unsigned miny
,
359 unsigned maxx
, unsigned maxy
)
361 batch
->minx
= MIN2(batch
->minx
, minx
);
362 batch
->miny
= MIN2(batch
->miny
, miny
);
363 batch
->maxx
= MAX2(batch
->maxx
, maxx
);
364 batch
->maxy
= MAX2(batch
->maxy
, maxy
);
368 panfrost_batch_intersection_scissor(struct panfrost_batch
*batch
,
369 unsigned minx
, unsigned miny
,
370 unsigned maxx
, unsigned maxy
)
372 batch
->minx
= MAX2(batch
->minx
, minx
);
373 batch
->miny
= MAX2(batch
->miny
, miny
);
374 batch
->maxx
= MIN2(batch
->maxx
, maxx
);
375 batch
->maxy
= MIN2(batch
->maxy
, maxy
);
379 panfrost_batch_init(struct panfrost_context
*ctx
)
381 ctx
->batches
= _mesa_hash_table_create(ctx
,
383 panfrost_batch_compare
);