broadcom/vc4: Store job fence in syncobj
[mesa.git] / src / gallium / drivers / vc4 / vc4_context.c
1 /*
2 * Copyright © 2014 Broadcom
3 *
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:
10 *
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
13 * Software.
14 *
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
21 * IN THE SOFTWARE.
22 */
23
24 #include <xf86drm.h>
25 #include <err.h>
26
27 #include "pipe/p_defines.h"
28 #include "util/ralloc.h"
29 #include "util/u_inlines.h"
30 #include "util/u_memory.h"
31 #include "util/u_blitter.h"
32 #include "util/u_upload_mgr.h"
33 #include "indices/u_primconvert.h"
34 #include "pipe/p_screen.h"
35
36 #include "vc4_screen.h"
37 #include "vc4_context.h"
38 #include "vc4_resource.h"
39
40 void
41 vc4_flush(struct pipe_context *pctx)
42 {
43 struct vc4_context *vc4 = vc4_context(pctx);
44
45 struct hash_entry *entry;
46 hash_table_foreach(vc4->jobs, entry) {
47 struct vc4_job *job = entry->data;
48 vc4_job_submit(vc4, job);
49 }
50 }
51
52 static void
53 vc4_pipe_flush(struct pipe_context *pctx, struct pipe_fence_handle **fence,
54 unsigned flags)
55 {
56 struct vc4_context *vc4 = vc4_context(pctx);
57
58 vc4_flush(pctx);
59
60 if (fence) {
61 struct pipe_screen *screen = pctx->screen;
62 struct vc4_fence *f = vc4_fence_create(vc4->screen,
63 vc4->last_emit_seqno);
64 screen->fence_reference(screen, fence, NULL);
65 *fence = (struct pipe_fence_handle *)f;
66 }
67 }
68
69 /* We can't flush the texture cache within rendering a tile, so we have to
70 * flush all rendering to the kernel so that the next job reading from the
71 * tile gets a flushed cache.
72 */
73 static void
74 vc4_texture_barrier(struct pipe_context *pctx, unsigned flags)
75 {
76 vc4_flush(pctx);
77 }
78
79 static void
80 vc4_invalidate_resource(struct pipe_context *pctx, struct pipe_resource *prsc)
81 {
82 struct vc4_context *vc4 = vc4_context(pctx);
83 struct vc4_resource *rsc = vc4_resource(prsc);
84
85 rsc->initialized_buffers = 0;
86
87 struct hash_entry *entry = _mesa_hash_table_search(vc4->write_jobs,
88 prsc);
89 if (!entry)
90 return;
91
92 struct vc4_job *job = entry->data;
93 if (job->key.zsbuf && job->key.zsbuf->texture == prsc)
94 job->resolve &= ~(PIPE_CLEAR_DEPTH | PIPE_CLEAR_STENCIL);
95 }
96
97 static void
98 vc4_context_destroy(struct pipe_context *pctx)
99 {
100 struct vc4_context *vc4 = vc4_context(pctx);
101
102 vc4_flush(pctx);
103
104 if (vc4->blitter)
105 util_blitter_destroy(vc4->blitter);
106
107 if (vc4->primconvert)
108 util_primconvert_destroy(vc4->primconvert);
109
110 if (vc4->uploader)
111 u_upload_destroy(vc4->uploader);
112
113 slab_destroy_child(&vc4->transfer_pool);
114
115 pipe_surface_reference(&vc4->framebuffer.cbufs[0], NULL);
116 pipe_surface_reference(&vc4->framebuffer.zsbuf, NULL);
117
118 if (vc4->yuv_linear_blit_vs)
119 pctx->delete_vs_state(pctx, vc4->yuv_linear_blit_vs);
120 if (vc4->yuv_linear_blit_fs_8bit)
121 pctx->delete_fs_state(pctx, vc4->yuv_linear_blit_fs_8bit);
122 if (vc4->yuv_linear_blit_fs_16bit)
123 pctx->delete_fs_state(pctx, vc4->yuv_linear_blit_fs_16bit);
124
125 vc4_program_fini(pctx);
126
127 if (vc4->screen->has_syncobj)
128 drmSyncobjDestroy(vc4->fd, vc4->job_syncobj);
129
130 ralloc_free(vc4);
131 }
132
133 struct pipe_context *
134 vc4_context_create(struct pipe_screen *pscreen, void *priv, unsigned flags)
135 {
136 struct vc4_screen *screen = vc4_screen(pscreen);
137 struct vc4_context *vc4;
138 int err;
139
140 /* Prevent dumping of the shaders built during context setup. */
141 uint32_t saved_shaderdb_flag = vc4_debug & VC4_DEBUG_SHADERDB;
142 vc4_debug &= ~VC4_DEBUG_SHADERDB;
143
144 vc4 = rzalloc(NULL, struct vc4_context);
145 if (!vc4)
146 return NULL;
147 struct pipe_context *pctx = &vc4->base;
148
149 vc4->screen = screen;
150
151 pctx->screen = pscreen;
152 pctx->priv = priv;
153 pctx->destroy = vc4_context_destroy;
154 pctx->flush = vc4_pipe_flush;
155 pctx->invalidate_resource = vc4_invalidate_resource;
156 pctx->texture_barrier = vc4_texture_barrier;
157
158 vc4_draw_init(pctx);
159 vc4_state_init(pctx);
160 vc4_program_init(pctx);
161 vc4_query_init(pctx);
162 vc4_resource_context_init(pctx);
163
164 vc4->fd = screen->fd;
165
166 err = vc4_job_init(vc4);
167 if (err)
168 goto fail;
169
170 slab_create_child(&vc4->transfer_pool, &screen->transfer_pool);
171
172 vc4->uploader = u_upload_create_default(&vc4->base);
173 vc4->base.stream_uploader = vc4->uploader;
174 vc4->base.const_uploader = vc4->uploader;
175
176 vc4->blitter = util_blitter_create(pctx);
177 if (!vc4->blitter)
178 goto fail;
179
180 vc4->primconvert = util_primconvert_create(pctx,
181 (1 << PIPE_PRIM_QUADS) - 1);
182 if (!vc4->primconvert)
183 goto fail;
184
185 vc4_debug |= saved_shaderdb_flag;
186
187 vc4->sample_mask = (1 << VC4_MAX_SAMPLES) - 1;
188
189 return &vc4->base;
190
191 fail:
192 pctx->destroy(pctx);
193 return NULL;
194 }