v3d: fix flushing of SSBOs and shader images
[mesa.git] / src / gallium / drivers / v3d / v3d_context.c
1 /*
2 * Copyright © 2014-2017 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/hash_table.h"
29 #include "util/ralloc.h"
30 #include "util/u_inlines.h"
31 #include "util/u_memory.h"
32 #include "util/u_blitter.h"
33 #include "util/u_upload_mgr.h"
34 #include "util/u_prim.h"
35 #include "indices/u_primconvert.h"
36 #include "pipe/p_screen.h"
37
38 #include "v3d_screen.h"
39 #include "v3d_context.h"
40 #include "v3d_resource.h"
41 #include "broadcom/compiler/v3d_compiler.h"
42
43 void
44 v3d_flush(struct pipe_context *pctx)
45 {
46 struct v3d_context *v3d = v3d_context(pctx);
47
48 hash_table_foreach(v3d->jobs, entry) {
49 struct v3d_job *job = entry->data;
50 v3d_job_submit(v3d, job);
51 }
52 }
53
54 static void
55 v3d_pipe_flush(struct pipe_context *pctx, struct pipe_fence_handle **fence,
56 unsigned flags)
57 {
58 struct v3d_context *v3d = v3d_context(pctx);
59
60 v3d_flush(pctx);
61
62 if (fence) {
63 struct pipe_screen *screen = pctx->screen;
64 struct v3d_fence *f = v3d_fence_create(v3d);
65 screen->fence_reference(screen, fence, NULL);
66 *fence = (struct pipe_fence_handle *)f;
67 }
68 }
69
70 static void
71 v3d_memory_barrier(struct pipe_context *pctx, unsigned int flags)
72 {
73 struct v3d_context *v3d = v3d_context(pctx);
74
75 if (!(flags & ~PIPE_BARRIER_UPDATE))
76 return;
77
78 /* We only need to flush jobs writing to SSBOs/images. */
79 perf_debug("Flushing all jobs for glMemoryBarrier(), could do better");
80 v3d_flush(pctx);
81 }
82
83 static void
84 v3d_set_debug_callback(struct pipe_context *pctx,
85 const struct pipe_debug_callback *cb)
86 {
87 struct v3d_context *v3d = v3d_context(pctx);
88
89 if (cb)
90 v3d->debug = *cb;
91 else
92 memset(&v3d->debug, 0, sizeof(v3d->debug));
93 }
94
95 static void
96 v3d_invalidate_resource(struct pipe_context *pctx, struct pipe_resource *prsc)
97 {
98 struct v3d_context *v3d = v3d_context(pctx);
99 struct v3d_resource *rsc = v3d_resource(prsc);
100
101 rsc->initialized_buffers = 0;
102
103 struct hash_entry *entry = _mesa_hash_table_search(v3d->write_jobs,
104 prsc);
105 if (!entry)
106 return;
107
108 struct v3d_job *job = entry->data;
109 if (job->key.zsbuf && job->key.zsbuf->texture == prsc)
110 job->store &= ~(PIPE_CLEAR_DEPTH | PIPE_CLEAR_STENCIL);
111 }
112
113 /**
114 * Flushes the current job to get up-to-date primive counts written to the
115 * primitive counts BO, then accumulates the transform feedback primitive count
116 * in the context and the corresponding vertex counts in the bound stream
117 * output targets.
118 */
119 void
120 v3d_tf_update_counters(struct v3d_context *v3d)
121 {
122 struct v3d_job *job = v3d_get_job_for_fbo(v3d);
123 if (job->draw_calls_queued == 0)
124 return;
125
126 /* In order to get up-to-date primitive counts we need to submit
127 * the job for execution so we get the counts written to memory.
128 * Notice that this will require a sync wait for the buffer write.
129 */
130 uint32_t prims_before = v3d->tf_prims_generated;
131 v3d_job_submit(v3d, job);
132 uint32_t prims_after = v3d->tf_prims_generated;
133 if (prims_before == prims_after)
134 return;
135
136 enum pipe_prim_type prim_type = u_base_prim_type(v3d->prim_mode);
137 uint32_t num_verts = u_vertices_for_prims(prim_type,
138 prims_after - prims_before);
139 for (int i = 0; i < v3d->streamout.num_targets; i++) {
140 struct v3d_stream_output_target *so =
141 v3d_stream_output_target(v3d->streamout.targets[i]);
142 so->recorded_vertex_count += num_verts;
143 }
144 }
145
146 static void
147 v3d_context_destroy(struct pipe_context *pctx)
148 {
149 struct v3d_context *v3d = v3d_context(pctx);
150
151 v3d_flush(pctx);
152
153 if (v3d->blitter)
154 util_blitter_destroy(v3d->blitter);
155
156 if (v3d->primconvert)
157 util_primconvert_destroy(v3d->primconvert);
158
159 if (v3d->uploader)
160 u_upload_destroy(v3d->uploader);
161 if (v3d->state_uploader)
162 u_upload_destroy(v3d->state_uploader);
163
164 if (v3d->prim_counts)
165 pipe_resource_reference(&v3d->prim_counts, NULL);
166
167 slab_destroy_child(&v3d->transfer_pool);
168
169 pipe_surface_reference(&v3d->framebuffer.cbufs[0], NULL);
170 pipe_surface_reference(&v3d->framebuffer.zsbuf, NULL);
171
172 v3d_program_fini(pctx);
173
174 ralloc_free(v3d);
175 }
176
177 static void
178 v3d_get_sample_position(struct pipe_context *pctx,
179 unsigned sample_count, unsigned sample_index,
180 float *xy)
181 {
182 struct v3d_context *v3d = v3d_context(pctx);
183
184 if (sample_count <= 1) {
185 xy[0] = 0.5;
186 xy[1] = 0.5;
187 } else {
188 static const int xoffsets_v33[] = { 1, -3, 3, -1 };
189 static const int xoffsets_v42[] = { -1, 3, -3, 1 };
190 const int *xoffsets = (v3d->screen->devinfo.ver >= 42 ?
191 xoffsets_v42 : xoffsets_v33);
192
193 xy[0] = 0.5 + xoffsets[sample_index] * .125;
194 xy[1] = .125 + sample_index * .25;
195 }
196 }
197
198 struct pipe_context *
199 v3d_context_create(struct pipe_screen *pscreen, void *priv, unsigned flags)
200 {
201 struct v3d_screen *screen = v3d_screen(pscreen);
202 struct v3d_context *v3d;
203
204 /* Prevent dumping of the shaders built during context setup. */
205 uint32_t saved_shaderdb_flag = V3D_DEBUG & V3D_DEBUG_SHADERDB;
206 V3D_DEBUG &= ~V3D_DEBUG_SHADERDB;
207
208 v3d = rzalloc(NULL, struct v3d_context);
209 if (!v3d)
210 return NULL;
211 struct pipe_context *pctx = &v3d->base;
212
213 v3d->screen = screen;
214
215 int ret = drmSyncobjCreate(screen->fd, DRM_SYNCOBJ_CREATE_SIGNALED,
216 &v3d->out_sync);
217 if (ret) {
218 ralloc_free(v3d);
219 return NULL;
220 }
221
222 pctx->screen = pscreen;
223 pctx->priv = priv;
224 pctx->destroy = v3d_context_destroy;
225 pctx->flush = v3d_pipe_flush;
226 pctx->memory_barrier = v3d_memory_barrier;
227 pctx->set_debug_callback = v3d_set_debug_callback;
228 pctx->invalidate_resource = v3d_invalidate_resource;
229 pctx->get_sample_position = v3d_get_sample_position;
230
231 if (screen->devinfo.ver >= 41) {
232 v3d41_draw_init(pctx);
233 v3d41_state_init(pctx);
234 } else {
235 v3d33_draw_init(pctx);
236 v3d33_state_init(pctx);
237 }
238 v3d_program_init(pctx);
239 v3d_query_init(pctx);
240 v3d_resource_context_init(pctx);
241
242 v3d_job_init(v3d);
243
244 v3d->fd = screen->fd;
245
246 slab_create_child(&v3d->transfer_pool, &screen->transfer_pool);
247
248 v3d->uploader = u_upload_create_default(&v3d->base);
249 v3d->base.stream_uploader = v3d->uploader;
250 v3d->base.const_uploader = v3d->uploader;
251 v3d->state_uploader = u_upload_create(&v3d->base,
252 4096,
253 PIPE_BIND_CONSTANT_BUFFER,
254 PIPE_USAGE_STREAM, 0);
255
256 v3d->blitter = util_blitter_create(pctx);
257 if (!v3d->blitter)
258 goto fail;
259 v3d->blitter->use_index_buffer = true;
260
261 v3d->primconvert = util_primconvert_create(pctx,
262 (1 << PIPE_PRIM_QUADS) - 1);
263 if (!v3d->primconvert)
264 goto fail;
265
266 V3D_DEBUG |= saved_shaderdb_flag;
267
268 v3d->sample_mask = (1 << V3D_MAX_SAMPLES) - 1;
269 v3d->active_queries = true;
270
271 return &v3d->base;
272
273 fail:
274 pctx->destroy(pctx);
275 return NULL;
276 }