2 * Copyright © 2014-2017 Broadcom
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
26 * Functions for submitting VC5 render jobs to the kernel.
30 #include "vc5_context.h"
31 /* The OQ/semaphore packets are the same across V3D versions. */
32 #define V3D_VERSION 33
33 #include "broadcom/cle/v3dx_pack.h"
34 #include "broadcom/common/v3d_macros.h"
35 #include "util/hash_table.h"
36 #include "util/ralloc.h"
38 #include "broadcom/clif/clif_dump.h"
41 remove_from_ht(struct hash_table
*ht
, void *key
)
43 struct hash_entry
*entry
= _mesa_hash_table_search(ht
, key
);
44 _mesa_hash_table_remove(ht
, entry
);
48 vc5_job_free(struct vc5_context
*vc5
, struct vc5_job
*job
)
50 struct set_entry
*entry
;
52 set_foreach(job
->bos
, entry
) {
53 struct vc5_bo
*bo
= (struct vc5_bo
*)entry
->key
;
54 vc5_bo_unreference(&bo
);
57 remove_from_ht(vc5
->jobs
, &job
->key
);
59 if (job
->write_prscs
) {
60 struct set_entry
*entry
;
62 set_foreach(job
->write_prscs
, entry
) {
63 const struct pipe_resource
*prsc
= entry
->key
;
65 remove_from_ht(vc5
->write_jobs
, (void *)prsc
);
69 for (int i
= 0; i
< VC5_MAX_DRAW_BUFFERS
; i
++) {
71 remove_from_ht(vc5
->write_jobs
, job
->cbufs
[i
]->texture
);
72 pipe_surface_reference(&job
->cbufs
[i
], NULL
);
76 remove_from_ht(vc5
->write_jobs
, job
->zsbuf
->texture
);
77 pipe_surface_reference(&job
->zsbuf
, NULL
);
83 vc5_destroy_cl(&job
->bcl
);
84 vc5_destroy_cl(&job
->rcl
);
85 vc5_destroy_cl(&job
->indirect
);
86 vc5_bo_unreference(&job
->tile_alloc
);
91 static struct vc5_job
*
92 vc5_job_create(struct vc5_context
*vc5
)
94 struct vc5_job
*job
= rzalloc(vc5
, struct vc5_job
);
98 vc5_init_cl(job
, &job
->bcl
);
99 vc5_init_cl(job
, &job
->rcl
);
100 vc5_init_cl(job
, &job
->indirect
);
102 job
->draw_min_x
= ~0;
103 job
->draw_min_y
= ~0;
107 job
->bos
= _mesa_set_create(job
,
109 _mesa_key_pointer_equal
);
114 vc5_job_add_bo(struct vc5_job
*job
, struct vc5_bo
*bo
)
119 if (_mesa_set_search(job
->bos
, bo
))
122 vc5_bo_reference(bo
);
123 _mesa_set_add(job
->bos
, bo
);
124 job
->referenced_size
+= bo
->size
;
126 uint32_t *bo_handles
= (void *)(uintptr_t)job
->submit
.bo_handles
;
128 if (job
->submit
.bo_handle_count
>= job
->bo_handles_size
) {
129 job
->bo_handles_size
= MAX2(4, job
->bo_handles_size
* 2);
130 bo_handles
= reralloc(job
, bo_handles
,
131 uint32_t, job
->bo_handles_size
);
132 job
->submit
.bo_handles
= (uintptr_t)(void *)bo_handles
;
134 bo_handles
[job
->submit
.bo_handle_count
++] = bo
->handle
;
138 vc5_job_add_write_resource(struct vc5_job
*job
, struct pipe_resource
*prsc
)
140 struct vc5_context
*vc5
= job
->vc5
;
142 if (!job
->write_prscs
) {
143 job
->write_prscs
= _mesa_set_create(job
,
145 _mesa_key_pointer_equal
);
148 _mesa_set_add(job
->write_prscs
, prsc
);
149 _mesa_hash_table_insert(vc5
->write_jobs
, prsc
, job
);
153 vc5_flush_jobs_writing_resource(struct vc5_context
*vc5
,
154 struct pipe_resource
*prsc
)
156 struct hash_entry
*entry
= _mesa_hash_table_search(vc5
->write_jobs
,
159 struct vc5_job
*job
= entry
->data
;
160 vc5_job_submit(vc5
, job
);
165 vc5_flush_jobs_reading_resource(struct vc5_context
*vc5
,
166 struct pipe_resource
*prsc
)
168 struct vc5_resource
*rsc
= vc5_resource(prsc
);
170 vc5_flush_jobs_writing_resource(vc5
, prsc
);
172 struct hash_entry
*entry
;
173 hash_table_foreach(vc5
->jobs
, entry
) {
174 struct vc5_job
*job
= entry
->data
;
176 if (_mesa_set_search(job
->bos
, rsc
->bo
)) {
177 vc5_job_submit(vc5
, job
);
178 /* Reminder: vc5->jobs is safe to keep iterating even
179 * after deletion of an entry.
187 vc5_job_set_tile_buffer_size(struct vc5_job
*job
)
189 static const uint8_t tile_sizes
[] = {
196 int tile_size_index
= 0;
198 tile_size_index
+= 2;
200 if (job
->cbufs
[3] || job
->cbufs
[2])
201 tile_size_index
+= 2;
202 else if (job
->cbufs
[1])
205 int max_bpp
= RENDER_TARGET_MAXIMUM_32BPP
;
206 for (int i
= 0; i
< VC5_MAX_DRAW_BUFFERS
; i
++) {
208 struct vc5_surface
*surf
= vc5_surface(job
->cbufs
[i
]);
209 max_bpp
= MAX2(max_bpp
, surf
->internal_bpp
);
212 job
->internal_bpp
= max_bpp
;
213 STATIC_ASSERT(RENDER_TARGET_MAXIMUM_32BPP
== 0);
214 tile_size_index
+= max_bpp
;
216 assert(tile_size_index
< ARRAY_SIZE(tile_sizes
));
217 job
->tile_width
= tile_sizes
[tile_size_index
* 2 + 0];
218 job
->tile_height
= tile_sizes
[tile_size_index
* 2 + 1];
222 * Returns a vc5_job struture for tracking V3D rendering to a particular FBO.
224 * If we've already started rendering to this FBO, then return old same job,
225 * otherwise make a new one. If we're beginning rendering to an FBO, make
226 * sure that any previous reads of the FBO (or writes to its color/Z surfaces)
230 vc5_get_job(struct vc5_context
*vc5
,
231 struct pipe_surface
**cbufs
, struct pipe_surface
*zsbuf
)
233 /* Return the existing job for this FBO if we have one */
234 struct vc5_job_key local_key
= {
243 struct hash_entry
*entry
= _mesa_hash_table_search(vc5
->jobs
,
248 /* Creating a new job. Make sure that any previous jobs reading or
249 * writing these buffers are flushed.
251 struct vc5_job
*job
= vc5_job_create(vc5
);
253 for (int i
= 0; i
< VC5_MAX_DRAW_BUFFERS
; i
++) {
255 vc5_flush_jobs_reading_resource(vc5
, cbufs
[i
]->texture
);
256 pipe_surface_reference(&job
->cbufs
[i
], cbufs
[i
]);
258 if (cbufs
[i
]->texture
->nr_samples
> 1)
263 vc5_flush_jobs_reading_resource(vc5
, zsbuf
->texture
);
264 pipe_surface_reference(&job
->zsbuf
, zsbuf
);
265 if (zsbuf
->texture
->nr_samples
> 1)
269 vc5_job_set_tile_buffer_size(job
);
271 for (int i
= 0; i
< VC5_MAX_DRAW_BUFFERS
; i
++) {
273 _mesa_hash_table_insert(vc5
->write_jobs
,
274 cbufs
[i
]->texture
, job
);
277 _mesa_hash_table_insert(vc5
->write_jobs
, zsbuf
->texture
, job
);
279 memcpy(&job
->key
, &local_key
, sizeof(local_key
));
280 _mesa_hash_table_insert(vc5
->jobs
, &job
->key
, job
);
286 vc5_get_job_for_fbo(struct vc5_context
*vc5
)
291 struct pipe_surface
**cbufs
= vc5
->framebuffer
.cbufs
;
292 struct pipe_surface
*zsbuf
= vc5
->framebuffer
.zsbuf
;
293 struct vc5_job
*job
= vc5_get_job(vc5
, cbufs
, zsbuf
);
295 /* The dirty flags are tracking what's been updated while vc5->job has
296 * been bound, so set them all to ~0 when switching between jobs. We
297 * also need to reset all state at the start of rendering.
301 /* If we're binding to uninitialized buffers, no need to load their
302 * contents before drawing.
304 for (int i
= 0; i
< 4; i
++) {
306 struct vc5_resource
*rsc
= vc5_resource(cbufs
[i
]->texture
);
308 job
->cleared
|= PIPE_CLEAR_COLOR0
<< i
;
313 struct vc5_resource
*rsc
= vc5_resource(zsbuf
->texture
);
315 job
->cleared
|= PIPE_CLEAR_DEPTH
| PIPE_CLEAR_STENCIL
;
318 job
->draw_tiles_x
= DIV_ROUND_UP(vc5
->framebuffer
.width
,
320 job
->draw_tiles_y
= DIV_ROUND_UP(vc5
->framebuffer
.height
,
329 vc5_clif_dump_lookup(void *data
, uint32_t addr
, void **vaddr
)
331 struct vc5_job
*job
= data
;
332 struct set_entry
*entry
;
334 set_foreach(job
->bos
, entry
) {
335 struct vc5_bo
*bo
= (void *)entry
->key
;
337 if (addr
>= bo
->offset
&&
338 addr
< bo
->offset
+ bo
->size
) {
340 *vaddr
= bo
->map
+ addr
- bo
->offset
;
349 vc5_clif_dump(struct vc5_context
*vc5
, struct vc5_job
*job
)
351 if (!(V3D_DEBUG
& V3D_DEBUG_CL
))
354 struct clif_dump
*clif
= clif_dump_init(&vc5
->screen
->devinfo
,
355 stderr
, vc5_clif_dump_lookup
,
358 fprintf(stderr
, "BCL: 0x%08x..0x%08x\n",
359 job
->submit
.bcl_start
, job
->submit
.bcl_end
);
361 clif_dump_add_cl(clif
, job
->submit
.bcl_start
, job
->submit
.bcl_end
);
363 fprintf(stderr
, "RCL: 0x%08x..0x%08x\n",
364 job
->submit
.rcl_start
, job
->submit
.rcl_end
);
365 clif_dump_add_cl(clif
, job
->submit
.rcl_start
, job
->submit
.rcl_end
);
369 * Submits the job to the kernel and then reinitializes it.
372 vc5_job_submit(struct vc5_context
*vc5
, struct vc5_job
*job
)
374 if (!job
->needs_flush
)
377 /* The RCL setup would choke if the draw bounds cause no drawing, so
378 * just drop the drawing if that's the case.
380 if (job
->draw_max_x
<= job
->draw_min_x
||
381 job
->draw_max_y
<= job
->draw_min_y
) {
387 if (cl_offset(&job
->bcl
) > 0) {
388 vc5_cl_ensure_space_with_branch(&job
->bcl
,
390 cl_packet_length(OCCLUSION_QUERY_COUNTER
));
392 if (job
->oq_enabled
) {
393 /* Disable the OQ at the end of the CL, so that the
394 * draw calls at the start of the CL don't inherit the
397 cl_emit(&job
->bcl
, OCCLUSION_QUERY_COUNTER
, counter
);
400 /* Increment the semaphore indicating that binning is done and
401 * unblocking the render thread. Note that this doesn't act
402 * until the FLUSH completes.
404 cl_emit(&job
->bcl
, INCREMENT_SEMAPHORE
, incr
);
406 /* The FLUSH_ALL emits any unwritten state changes in each
407 * tile. We can use this to reset any state that needs to be
408 * present at the start of the next tile, as we do with
409 * OCCLUSION_QUERY_COUNTER above.
411 cl_emit(&job
->bcl
, FLUSH_ALL_STATE
, flush
);
414 job
->submit
.bcl_end
= job
->bcl
.bo
->offset
+ cl_offset(&job
->bcl
);
415 job
->submit
.rcl_end
= job
->rcl
.bo
->offset
+ cl_offset(&job
->rcl
);
417 vc5_clif_dump(vc5
, job
);
419 if (!(V3D_DEBUG
& V3D_DEBUG_NORAST
)) {
422 #ifndef USE_VC5_SIMULATOR
423 ret
= drmIoctl(vc5
->fd
, DRM_IOCTL_VC5_SUBMIT_CL
, &job
->submit
);
425 ret
= vc5_simulator_flush(vc5
, &job
->submit
, job
);
427 static bool warned
= false;
428 if (ret
&& !warned
) {
429 fprintf(stderr
, "Draw call returned %s. "
430 "Expect corruption.\n", strerror(errno
));
435 if (vc5
->last_emit_seqno
- vc5
->screen
->finished_seqno
> 5) {
436 if (!vc5_wait_seqno(vc5
->screen
,
437 vc5
->last_emit_seqno
- 5,
438 PIPE_TIMEOUT_INFINITE
,
440 fprintf(stderr
, "Job throttling failed\n");
445 vc5_job_free(vc5
, job
);
449 vc5_job_compare(const void *a
, const void *b
)
451 return memcmp(a
, b
, sizeof(struct vc5_job_key
)) == 0;
455 vc5_job_hash(const void *key
)
457 return _mesa_hash_data(key
, sizeof(struct vc5_job_key
));
461 vc5_job_init(struct vc5_context
*vc5
)
463 vc5
->jobs
= _mesa_hash_table_create(vc5
,
466 vc5
->write_jobs
= _mesa_hash_table_create(vc5
,
468 _mesa_key_pointer_equal
);