v3d: Add SSBO/atomic counters support.
[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 "indices/u_primconvert.h"
35 #include "pipe/p_screen.h"
36
37 #include "v3d_screen.h"
38 #include "v3d_context.h"
39 #include "v3d_resource.h"
40
41 void
42 v3d_flush(struct pipe_context *pctx)
43 {
44 struct v3d_context *v3d = v3d_context(pctx);
45
46 hash_table_foreach(v3d->jobs, entry) {
47 struct v3d_job *job = entry->data;
48 v3d_job_submit(v3d, job);
49 }
50 }
51
52 static void
53 v3d_pipe_flush(struct pipe_context *pctx, struct pipe_fence_handle **fence,
54 unsigned flags)
55 {
56 struct v3d_context *v3d = v3d_context(pctx);
57
58 v3d_flush(pctx);
59
60 if (fence) {
61 struct pipe_screen *screen = pctx->screen;
62 struct v3d_fence *f = v3d_fence_create(v3d);
63 screen->fence_reference(screen, fence, NULL);
64 *fence = (struct pipe_fence_handle *)f;
65 }
66 }
67
68 static void
69 v3d_memory_barrier(struct pipe_context *pctx, unsigned int flags)
70 {
71 struct v3d_context *v3d = v3d_context(pctx);
72
73 /* We only need to flush jobs writing to SSBOs/images. */
74 perf_debug("Flushing all jobs for glMemoryBarrier(), could do better");
75 v3d_flush(pctx);
76 }
77
78 static void
79 v3d_set_debug_callback(struct pipe_context *pctx,
80 const struct pipe_debug_callback *cb)
81 {
82 struct v3d_context *v3d = v3d_context(pctx);
83
84 if (cb)
85 v3d->debug = *cb;
86 else
87 memset(&v3d->debug, 0, sizeof(v3d->debug));
88 }
89
90 static void
91 v3d_invalidate_resource(struct pipe_context *pctx, struct pipe_resource *prsc)
92 {
93 struct v3d_context *v3d = v3d_context(pctx);
94 struct v3d_resource *rsc = v3d_resource(prsc);
95
96 rsc->initialized_buffers = 0;
97
98 struct hash_entry *entry = _mesa_hash_table_search(v3d->write_jobs,
99 prsc);
100 if (!entry)
101 return;
102
103 struct v3d_job *job = entry->data;
104 if (job->key.zsbuf && job->key.zsbuf->texture == prsc)
105 job->store &= ~(PIPE_CLEAR_DEPTH | PIPE_CLEAR_STENCIL);
106 }
107
108 static void
109 v3d_context_destroy(struct pipe_context *pctx)
110 {
111 struct v3d_context *v3d = v3d_context(pctx);
112
113 v3d_flush(pctx);
114
115 if (v3d->blitter)
116 util_blitter_destroy(v3d->blitter);
117
118 if (v3d->primconvert)
119 util_primconvert_destroy(v3d->primconvert);
120
121 if (v3d->uploader)
122 u_upload_destroy(v3d->uploader);
123 if (v3d->state_uploader)
124 u_upload_destroy(v3d->state_uploader);
125
126 slab_destroy_child(&v3d->transfer_pool);
127
128 pipe_surface_reference(&v3d->framebuffer.cbufs[0], NULL);
129 pipe_surface_reference(&v3d->framebuffer.zsbuf, NULL);
130
131 v3d_program_fini(pctx);
132
133 ralloc_free(v3d);
134 }
135
136 static void
137 v3d_get_sample_position(struct pipe_context *pctx,
138 unsigned sample_count, unsigned sample_index,
139 float *xy)
140 {
141 struct v3d_context *v3d = v3d_context(pctx);
142
143 if (sample_count <= 1) {
144 xy[0] = 0.5;
145 xy[1] = 0.5;
146 } else {
147 static const int xoffsets_v33[] = { 1, -3, 3, -1 };
148 static const int xoffsets_v42[] = { -1, 3, -3, 1 };
149 const int *xoffsets = (v3d->screen->devinfo.ver >= 42 ?
150 xoffsets_v42 : xoffsets_v33);
151
152 xy[0] = 0.5 + xoffsets[sample_index] * .125;
153 xy[1] = .125 + sample_index * .25;
154 }
155 }
156
157 struct pipe_context *
158 v3d_context_create(struct pipe_screen *pscreen, void *priv, unsigned flags)
159 {
160 struct v3d_screen *screen = v3d_screen(pscreen);
161 struct v3d_context *v3d;
162
163 /* Prevent dumping of the shaders built during context setup. */
164 uint32_t saved_shaderdb_flag = V3D_DEBUG & V3D_DEBUG_SHADERDB;
165 V3D_DEBUG &= ~V3D_DEBUG_SHADERDB;
166
167 v3d = rzalloc(NULL, struct v3d_context);
168 if (!v3d)
169 return NULL;
170 struct pipe_context *pctx = &v3d->base;
171
172 v3d->screen = screen;
173
174 int ret = drmSyncobjCreate(screen->fd, DRM_SYNCOBJ_CREATE_SIGNALED,
175 &v3d->out_sync);
176 if (ret) {
177 ralloc_free(v3d);
178 return NULL;
179 }
180
181 pctx->screen = pscreen;
182 pctx->priv = priv;
183 pctx->destroy = v3d_context_destroy;
184 pctx->flush = v3d_pipe_flush;
185 pctx->memory_barrier = v3d_memory_barrier;
186 pctx->set_debug_callback = v3d_set_debug_callback;
187 pctx->invalidate_resource = v3d_invalidate_resource;
188 pctx->get_sample_position = v3d_get_sample_position;
189
190 if (screen->devinfo.ver >= 41) {
191 v3d41_draw_init(pctx);
192 v3d41_state_init(pctx);
193 } else {
194 v3d33_draw_init(pctx);
195 v3d33_state_init(pctx);
196 }
197 v3d_program_init(pctx);
198 v3d_query_init(pctx);
199 v3d_resource_context_init(pctx);
200
201 v3d_job_init(v3d);
202
203 v3d->fd = screen->fd;
204
205 slab_create_child(&v3d->transfer_pool, &screen->transfer_pool);
206
207 v3d->uploader = u_upload_create_default(&v3d->base);
208 v3d->base.stream_uploader = v3d->uploader;
209 v3d->base.const_uploader = v3d->uploader;
210 v3d->state_uploader = u_upload_create(&v3d->base,
211 4096,
212 PIPE_BIND_CONSTANT_BUFFER,
213 PIPE_USAGE_STREAM, 0);
214
215 v3d->blitter = util_blitter_create(pctx);
216 if (!v3d->blitter)
217 goto fail;
218 v3d->blitter->use_index_buffer = true;
219
220 v3d->primconvert = util_primconvert_create(pctx,
221 (1 << PIPE_PRIM_QUADS) - 1);
222 if (!v3d->primconvert)
223 goto fail;
224
225 V3D_DEBUG |= saved_shaderdb_flag;
226
227 v3d->sample_mask = (1 << VC5_MAX_SAMPLES) - 1;
228 v3d->active_queries = true;
229
230 return &v3d->base;
231
232 fail:
233 pctx->destroy(pctx);
234 return NULL;
235 }