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