gallium: extend pipe_context::flush for it to accept an END_OF_FRAME flag
[mesa.git] / src / gallium / drivers / nvc0 / nvc0_context.c
1 /*
2 * Copyright 2010 Christoph Bumiller
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 shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
18 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
19 * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 * SOFTWARE.
21 */
22
23 #include "pipe/p_defines.h"
24 #include "util/u_framebuffer.h"
25
26 #ifdef NVC0_WITH_DRAW_MODULE
27 #include "draw/draw_context.h"
28 #endif
29
30 #include "nvc0_context.h"
31 #include "nvc0_screen.h"
32 #include "nvc0_resource.h"
33
34 static void
35 nvc0_flush(struct pipe_context *pipe,
36 struct pipe_fence_handle **fence,
37 enum pipe_flush_flags flags)
38 {
39 struct nvc0_context *nvc0 = nvc0_context(pipe);
40 struct nouveau_screen *screen = &nvc0->screen->base;
41
42 if (fence)
43 nouveau_fence_ref(screen->fence.current, (struct nouveau_fence **)fence);
44
45 PUSH_KICK(nvc0->base.pushbuf); /* fencing handled in kick_notify */
46 }
47
48 static void
49 nvc0_texture_barrier(struct pipe_context *pipe)
50 {
51 struct nouveau_pushbuf *push = nvc0_context(pipe)->base.pushbuf;
52
53 IMMED_NVC0(push, NVC0_3D(SERIALIZE), 0);
54 IMMED_NVC0(push, NVC0_3D(TEX_CACHE_CTL), 0);
55 }
56
57 static void
58 nvc0_context_unreference_resources(struct nvc0_context *nvc0)
59 {
60 unsigned s, i;
61
62 nouveau_bufctx_del(&nvc0->bufctx_3d);
63 nouveau_bufctx_del(&nvc0->bufctx);
64
65 util_unreference_framebuffer_state(&nvc0->framebuffer);
66
67 for (i = 0; i < nvc0->num_vtxbufs; ++i)
68 pipe_resource_reference(&nvc0->vtxbuf[i].buffer, NULL);
69
70 pipe_resource_reference(&nvc0->idxbuf.buffer, NULL);
71
72 for (s = 0; s < 5; ++s) {
73 for (i = 0; i < nvc0->num_textures[s]; ++i)
74 pipe_sampler_view_reference(&nvc0->textures[s][i], NULL);
75
76 for (i = 0; i < NVC0_MAX_PIPE_CONSTBUFS; ++i)
77 if (!nvc0->constbuf[s][i].user)
78 pipe_resource_reference(&nvc0->constbuf[s][i].u.buf, NULL);
79 }
80
81 for (i = 0; i < nvc0->num_tfbbufs; ++i)
82 pipe_so_target_reference(&nvc0->tfbbuf[i], NULL);
83 }
84
85 static void
86 nvc0_destroy(struct pipe_context *pipe)
87 {
88 struct nvc0_context *nvc0 = nvc0_context(pipe);
89
90 if (nvc0->screen->cur_ctx == nvc0) {
91 nvc0->base.pushbuf->kick_notify = NULL;
92 nvc0->screen->cur_ctx = NULL;
93 nouveau_pushbuf_bufctx(nvc0->base.pushbuf, NULL);
94 }
95 nouveau_pushbuf_kick(nvc0->base.pushbuf, nvc0->base.pushbuf->channel);
96
97 nvc0_context_unreference_resources(nvc0);
98
99 #ifdef NVC0_WITH_DRAW_MODULE
100 draw_destroy(nvc0->draw);
101 #endif
102
103 nouveau_context_destroy(&nvc0->base);
104 }
105
106 void
107 nvc0_default_kick_notify(struct nouveau_pushbuf *push)
108 {
109 struct nvc0_screen *screen = push->user_priv;
110
111 if (screen) {
112 nouveau_fence_next(&screen->base);
113 nouveau_fence_update(&screen->base, TRUE);
114 if (screen->cur_ctx)
115 screen->cur_ctx->state.flushed = TRUE;
116 }
117 }
118
119 struct pipe_context *
120 nvc0_create(struct pipe_screen *pscreen, void *priv)
121 {
122 struct nvc0_screen *screen = nvc0_screen(pscreen);
123 struct nvc0_context *nvc0;
124 struct pipe_context *pipe;
125 int ret;
126 uint32_t flags;
127
128 nvc0 = CALLOC_STRUCT(nvc0_context);
129 if (!nvc0)
130 return NULL;
131 pipe = &nvc0->base.pipe;
132
133 if (!nvc0_blitctx_create(nvc0))
134 goto out_err;
135
136 nvc0->base.pushbuf = screen->base.pushbuf;
137
138 ret = nouveau_bufctx_new(screen->base.client, NVC0_BIND_COUNT,
139 &nvc0->bufctx_3d);
140 if (!ret)
141 nouveau_bufctx_new(screen->base.client, 2, &nvc0->bufctx);
142 if (ret)
143 goto out_err;
144
145 nvc0->screen = screen;
146 nvc0->base.screen = &screen->base;
147
148 pipe->screen = pscreen;
149 pipe->priv = priv;
150
151 pipe->destroy = nvc0_destroy;
152
153 pipe->draw_vbo = nvc0_draw_vbo;
154 pipe->clear = nvc0_clear;
155
156 pipe->flush = nvc0_flush;
157 pipe->texture_barrier = nvc0_texture_barrier;
158
159 if (!screen->cur_ctx) {
160 screen->cur_ctx = nvc0;
161 nouveau_pushbuf_bufctx(screen->base.pushbuf, nvc0->bufctx);
162 }
163 screen->base.pushbuf->kick_notify = nvc0_default_kick_notify;
164
165 nvc0_init_query_functions(nvc0);
166 nvc0_init_surface_functions(nvc0);
167 nvc0_init_state_functions(nvc0);
168 nvc0_init_transfer_functions(nvc0);
169 nvc0_init_resource_functions(pipe);
170
171 #ifdef NVC0_WITH_DRAW_MODULE
172 /* no software fallbacks implemented */
173 nvc0->draw = draw_create(pipe);
174 assert(nvc0->draw);
175 draw_set_rasterize_stage(nvc0->draw, nvc0_draw_render_stage(nvc0));
176 #endif
177
178 nouveau_context_init_vdec(&nvc0->base);
179
180 /* shader builtin library is per-screen, but we need a context for m2mf */
181 nvc0_program_library_upload(nvc0);
182
183 /* add permanently resident buffers to bufctxts */
184
185 flags = NOUVEAU_BO_VRAM | NOUVEAU_BO_RD;
186
187 BCTX_REFN_bo(nvc0->bufctx_3d, SCREEN, flags, screen->text);
188 BCTX_REFN_bo(nvc0->bufctx_3d, SCREEN, flags, screen->uniform_bo);
189 BCTX_REFN_bo(nvc0->bufctx_3d, SCREEN, flags, screen->txc);
190 BCTX_REFN_bo(nvc0->bufctx_3d, SCREEN, flags, screen->poly_cache);
191
192 flags = NOUVEAU_BO_GART | NOUVEAU_BO_WR;
193
194 BCTX_REFN_bo(nvc0->bufctx_3d, SCREEN, flags, screen->fence.bo);
195 BCTX_REFN_bo(nvc0->bufctx, FENCE, flags, screen->fence.bo);
196
197 nvc0->base.scratch.bo_size = 2 << 20;
198
199 memset(nvc0->tex_handles, ~0, sizeof(nvc0->tex_handles));
200
201 return pipe;
202
203 out_err:
204 if (nvc0) {
205 if (nvc0->bufctx_3d)
206 nouveau_bufctx_del(&nvc0->bufctx_3d);
207 if (nvc0->bufctx)
208 nouveau_bufctx_del(&nvc0->bufctx);
209 if (nvc0->blit)
210 FREE(nvc0->blit);
211 FREE(nvc0);
212 }
213 return NULL;
214 }
215
216 void
217 nvc0_bufctx_fence(struct nvc0_context *nvc0, struct nouveau_bufctx *bufctx,
218 boolean on_flush)
219 {
220 struct nouveau_list *list = on_flush ? &bufctx->current : &bufctx->pending;
221 struct nouveau_list *it;
222
223 for (it = list->next; it != list; it = it->next) {
224 struct nouveau_bufref *ref = (struct nouveau_bufref *)it;
225 struct nv04_resource *res = ref->priv;
226 if (res)
227 nvc0_resource_validate(res, (unsigned)ref->priv_data);
228 }
229 }