broadcom/vc5: Update the UABI for in/out syncobjs
[mesa.git] / src / gallium / drivers / vc5 / vc5_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 "vc5_screen.h"
38 #include "vc5_context.h"
39 #include "vc5_resource.h"
40
41 void
42 vc5_flush(struct pipe_context *pctx)
43 {
44 struct vc5_context *vc5 = vc5_context(pctx);
45
46 struct hash_entry *entry;
47 hash_table_foreach(vc5->jobs, entry) {
48 struct vc5_job *job = entry->data;
49 vc5_job_submit(vc5, job);
50 }
51 }
52
53 static void
54 vc5_pipe_flush(struct pipe_context *pctx, struct pipe_fence_handle **fence,
55 unsigned flags)
56 {
57 struct vc5_context *vc5 = vc5_context(pctx);
58
59 vc5_flush(pctx);
60
61 if (fence) {
62 struct pipe_screen *screen = pctx->screen;
63 struct vc5_fence *f = vc5_fence_create(vc5);
64 screen->fence_reference(screen, fence, NULL);
65 *fence = (struct pipe_fence_handle *)f;
66 }
67 }
68
69 static void
70 vc5_invalidate_resource(struct pipe_context *pctx, struct pipe_resource *prsc)
71 {
72 struct vc5_context *vc5 = vc5_context(pctx);
73 struct vc5_resource *rsc = vc5_resource(prsc);
74
75 rsc->initialized_buffers = 0;
76
77 struct hash_entry *entry = _mesa_hash_table_search(vc5->write_jobs,
78 prsc);
79 if (!entry)
80 return;
81
82 struct vc5_job *job = entry->data;
83 if (job->key.zsbuf && job->key.zsbuf->texture == prsc)
84 job->resolve &= ~(PIPE_CLEAR_DEPTH | PIPE_CLEAR_STENCIL);
85 }
86
87 static void
88 vc5_context_destroy(struct pipe_context *pctx)
89 {
90 struct vc5_context *vc5 = vc5_context(pctx);
91
92 vc5_flush(pctx);
93
94 if (vc5->blitter)
95 util_blitter_destroy(vc5->blitter);
96
97 if (vc5->primconvert)
98 util_primconvert_destroy(vc5->primconvert);
99
100 if (vc5->uploader)
101 u_upload_destroy(vc5->uploader);
102
103 slab_destroy_child(&vc5->transfer_pool);
104
105 pipe_surface_reference(&vc5->framebuffer.cbufs[0], NULL);
106 pipe_surface_reference(&vc5->framebuffer.zsbuf, NULL);
107
108 vc5_program_fini(pctx);
109
110 ralloc_free(vc5);
111 }
112
113 struct pipe_context *
114 vc5_context_create(struct pipe_screen *pscreen, void *priv, unsigned flags)
115 {
116 struct vc5_screen *screen = vc5_screen(pscreen);
117 struct vc5_context *vc5;
118
119 /* Prevent dumping of the shaders built during context setup. */
120 uint32_t saved_shaderdb_flag = V3D_DEBUG & V3D_DEBUG_SHADERDB;
121 V3D_DEBUG &= ~V3D_DEBUG_SHADERDB;
122
123 vc5 = rzalloc(NULL, struct vc5_context);
124 if (!vc5)
125 return NULL;
126 struct pipe_context *pctx = &vc5->base;
127
128 vc5->screen = screen;
129
130 int ret = drmSyncobjCreate(screen->fd, DRM_SYNCOBJ_CREATE_SIGNALED,
131 &vc5->out_sync);
132 if (ret) {
133 ralloc_free(vc5);
134 return NULL;
135 }
136
137 pctx->screen = pscreen;
138 pctx->priv = priv;
139 pctx->destroy = vc5_context_destroy;
140 pctx->flush = vc5_pipe_flush;
141 pctx->invalidate_resource = vc5_invalidate_resource;
142
143 if (screen->devinfo.ver >= 41) {
144 v3d41_draw_init(pctx);
145 v3d41_state_init(pctx);
146 } else {
147 v3d33_draw_init(pctx);
148 v3d33_state_init(pctx);
149 }
150 vc5_program_init(pctx);
151 vc5_query_init(pctx);
152 vc5_resource_context_init(pctx);
153
154 vc5_job_init(vc5);
155
156 vc5->fd = screen->fd;
157
158 slab_create_child(&vc5->transfer_pool, &screen->transfer_pool);
159
160 vc5->uploader = u_upload_create_default(&vc5->base);
161 vc5->base.stream_uploader = vc5->uploader;
162 vc5->base.const_uploader = vc5->uploader;
163
164 vc5->blitter = util_blitter_create(pctx);
165 if (!vc5->blitter)
166 goto fail;
167
168 vc5->primconvert = util_primconvert_create(pctx,
169 (1 << PIPE_PRIM_QUADS) - 1);
170 if (!vc5->primconvert)
171 goto fail;
172
173 V3D_DEBUG |= saved_shaderdb_flag;
174
175 vc5->sample_mask = (1 << VC5_MAX_SAMPLES) - 1;
176 vc5->active_queries = true;
177
178 return &vc5->base;
179
180 fail:
181 pctx->destroy(pctx);
182 return NULL;
183 }