37bd70cb9a9a8da7134dc0757a73f33df0cf6933
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"
32 panfrost_create_job(struct panfrost_context
*ctx
)
34 struct panfrost_job
*job
= rzalloc(ctx
, struct panfrost_job
);
38 job
->bos
= _mesa_set_create(job
,
40 _mesa_key_pointer_equal
);
42 job
->minx
= job
->miny
= ~0;
43 job
->maxx
= job
->maxy
= 0;
49 panfrost_free_job(struct panfrost_context
*ctx
, struct panfrost_job
*job
)
54 set_foreach(job
->bos
, entry
) {
55 struct panfrost_bo
*bo
= (struct panfrost_bo
*)entry
->key
;
56 panfrost_bo_unreference(ctx
->base
.screen
, bo
);
59 _mesa_hash_table_remove_key(ctx
->jobs
, &job
->key
);
68 panfrost_get_job(struct panfrost_context
*ctx
,
69 struct pipe_surface
**cbufs
, struct pipe_surface
*zsbuf
)
71 /* Lookup the job first */
73 struct panfrost_job_key key
= {
83 struct hash_entry
*entry
= _mesa_hash_table_search(ctx
->jobs
, &key
);
88 /* Otherwise, let's create a job */
90 struct panfrost_job
*job
= panfrost_create_job(ctx
);
92 /* Save the created job */
94 memcpy(&job
->key
, &key
, sizeof(key
));
95 _mesa_hash_table_insert(ctx
->jobs
, &job
->key
, job
);
100 /* Get the job corresponding to the FBO we're currently rendering into */
102 struct panfrost_job
*
103 panfrost_get_job_for_fbo(struct panfrost_context
*ctx
)
105 /* If we already began rendering, use that */
110 /* If not, look up the job */
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
);
120 panfrost_job_add_bo(struct panfrost_job
*job
, struct panfrost_bo
*bo
)
125 if (_mesa_set_search(job
->bos
, bo
))
128 panfrost_bo_reference(bo
);
129 _mesa_set_add(job
->bos
, bo
);
133 panfrost_flush_jobs_writing_resource(struct panfrost_context
*panfrost
,
134 struct pipe_resource
*prsc
)
137 struct hash_entry
*entry
= _mesa_hash_table_search(panfrost
->write_jobs
,
140 struct panfrost_job
*job
= entry
->data
;
141 panfrost_job_submit(panfrost
, job
);
148 panfrost_job_submit(struct panfrost_context
*ctx
, struct panfrost_job
*job
)
150 struct pipe_context
*gallium
= (struct pipe_context
*) ctx
;
151 struct panfrost_screen
*screen
= pan_screen(gallium
->screen
);
154 bool has_draws
= ctx
->draw_count
> 0;
155 bool is_scanout
= panfrost_is_scanout(ctx
);
160 ret
= screen
->driver
->submit_vs_fs_job(ctx
, has_draws
, is_scanout
);
163 fprintf(stderr
, "panfrost_job_submit failed: %d\n", ret
);
165 /* Reset job counters */
167 ctx
->vertex_job_count
= 0;
168 ctx
->tiler_job_count
= 0;
172 panfrost_job_set_requirements(struct panfrost_context
*ctx
,
173 struct panfrost_job
*job
)
175 if (ctx
->rasterizer
&& ctx
->rasterizer
->base
.multisample
)
176 job
->requirements
|= PAN_REQ_MSAA
;
178 if (ctx
->depth_stencil
&& ctx
->depth_stencil
->depth
.writemask
)
179 job
->requirements
|= PAN_REQ_DEPTH_WRITE
;
183 pan_pack_color(const union pipe_color_union
*color
, enum pipe_format format
)
185 /* Alpha magicked to 1.0 if there is no alpha */
187 bool has_alpha
= util_format_has_alpha(format
);
188 float clear_alpha
= has_alpha
? color
->f
[3] : 1.0f
;
190 /* Packed color depends on the framebuffer format */
192 const struct util_format_description
*desc
=
193 util_format_description(format
);
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;
206 /* Then we pack into a sparse u32. TODO: Why these shifts? */
207 return (b5
<< 25) | (g6
<< 14) | (r5
<< 5);
217 panfrost_job_clear(struct panfrost_context
*ctx
,
218 struct panfrost_job
*job
,
220 const union pipe_color_union
*color
,
221 double depth
, unsigned stencil
)
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
);
229 if (buffers
& PIPE_CLEAR_DEPTH
) {
230 job
->clear_depth
= depth
;
233 if (buffers
& PIPE_CLEAR_STENCIL
) {
234 job
->clear_stencil
= stencil
;
237 job
->clear
|= buffers
;
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) */
244 panfrost_job_union_scissor(job
, 0, 0,
245 ctx
->pipe_framebuffer
.width
,
246 ctx
->pipe_framebuffer
.height
);
250 panfrost_flush_jobs_reading_resource(struct panfrost_context
*panfrost
,
251 struct pipe_resource
*prsc
)
253 struct panfrost_resource
*rsc
= pan_resource(prsc
);
255 panfrost_flush_jobs_writing_resource(panfrost
, prsc
);
257 hash_table_foreach(panfrost
->jobs
, entry
) {
258 struct panfrost_job
*job
= entry
->data
;
260 if (_mesa_set_search(job
->bos
, rsc
->bo
)) {
261 printf("TODO: submit job for flush\n");
262 //panfrost_job_submit(panfrost, job);
269 panfrost_job_compare(const void *a
, const void *b
)
271 return memcmp(a
, b
, sizeof(struct panfrost_job_key
)) == 0;
275 panfrost_job_hash(const void *key
)
277 return _mesa_hash_data(key
, sizeof(struct panfrost_job_key
));
280 /* Given a new bounding rectangle (scissor), let the job cover the union of the
281 * new and old bounding rectangles */
284 panfrost_job_union_scissor(struct panfrost_job
*job
,
285 unsigned minx
, unsigned miny
,
286 unsigned maxx
, unsigned maxy
)
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
);
295 panfrost_job_init(struct panfrost_context
*ctx
)
297 ctx
->jobs
= _mesa_hash_table_create(ctx
,
299 panfrost_job_compare
);
301 ctx
->write_jobs
= _mesa_hash_table_create(ctx
,
303 _mesa_key_pointer_equal
);