nouveau: switch to libdrm_nouveau-2.0
[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 "draw/draw_context.h"
24 #include "pipe/p_defines.h"
25
26 #include "nvc0_context.h"
27 #include "nvc0_screen.h"
28 #include "nvc0_resource.h"
29
30 #include "nouveau/nouveau_reloc.h"
31
32 static void
33 nvc0_flush(struct pipe_context *pipe,
34 struct pipe_fence_handle **fence)
35 {
36 struct nvc0_context *nvc0 = nvc0_context(pipe);
37 struct nouveau_screen *screen = &nvc0->screen->base;
38
39 if (fence)
40 nouveau_fence_ref(screen->fence.current, (struct nouveau_fence **)fence);
41
42 PUSH_KICK(nvc0->base.pushbuf); /* fencing handled in kick_notify */
43 }
44
45 static void
46 nvc0_texture_barrier(struct pipe_context *pipe)
47 {
48 struct nouveau_pushbuf *push = nvc0_context(pipe)->base.pushbuf;
49
50 IMMED_NVC0(push, NVC0_3D(SERIALIZE), 0);
51 IMMED_NVC0(push, NVC0_3D(TEX_CACHE_CTL), 0);
52 }
53
54 static void
55 nvc0_context_unreference_resources(struct nvc0_context *nvc0)
56 {
57 unsigned s, i;
58
59 nouveau_bufctx_del(&nvc0->bufctx_3d);
60 nouveau_bufctx_del(&nvc0->bufctx);
61
62 for (i = 0; i < nvc0->num_vtxbufs; ++i)
63 pipe_resource_reference(&nvc0->vtxbuf[i].buffer, NULL);
64
65 pipe_resource_reference(&nvc0->idxbuf.buffer, NULL);
66
67 for (s = 0; s < 5; ++s) {
68 for (i = 0; i < nvc0->num_textures[s]; ++i)
69 pipe_sampler_view_reference(&nvc0->textures[s][i], NULL);
70
71 for (i = 0; i < 16; ++i)
72 pipe_resource_reference(&nvc0->constbuf[s][i], NULL);
73 }
74
75 for (i = 0; i < nvc0->num_tfbbufs; ++i)
76 pipe_so_target_reference(&nvc0->tfbbuf[i], NULL);
77 }
78
79 static void
80 nvc0_destroy(struct pipe_context *pipe)
81 {
82 struct nvc0_context *nvc0 = nvc0_context(pipe);
83
84 if (nvc0->screen->cur_ctx == nvc0) {
85 nvc0->base.pushbuf->kick_notify = NULL;
86 nvc0->screen->cur_ctx = NULL;
87 nouveau_pushbuf_bufctx(nvc0->base.pushbuf, NULL);
88 }
89 nouveau_pushbuf_kick(nvc0->base.pushbuf, nvc0->base.pushbuf->channel);
90
91 nvc0_context_unreference_resources(nvc0);
92
93 draw_destroy(nvc0->draw);
94
95 FREE(nvc0);
96 }
97
98 void
99 nvc0_default_kick_notify(struct nouveau_pushbuf *push)
100 {
101 struct nvc0_screen *screen = push->user_priv;
102
103 if (screen) {
104 nouveau_fence_next(&screen->base);
105 nouveau_fence_update(&screen->base, TRUE);
106 if (screen->cur_ctx)
107 screen->cur_ctx->state.flushed = TRUE;
108 }
109 }
110
111 struct pipe_context *
112 nvc0_create(struct pipe_screen *pscreen, void *priv)
113 {
114 struct nvc0_screen *screen = nvc0_screen(pscreen);
115 struct nvc0_context *nvc0;
116 struct pipe_context *pipe;
117 int ret;
118 uint32_t flags;
119
120 nvc0 = CALLOC_STRUCT(nvc0_context);
121 if (!nvc0)
122 return NULL;
123 pipe = &nvc0->base.pipe;
124
125 nvc0->base.pushbuf = screen->base.pushbuf;
126
127 ret = nouveau_bufctx_new(screen->base.client, NVC0_BIND_COUNT,
128 &nvc0->bufctx_3d);
129 if (!ret)
130 nouveau_bufctx_new(screen->base.client, 2, &nvc0->bufctx);
131 if (ret)
132 goto out_err;
133
134 nvc0->screen = screen;
135 nvc0->base.screen = &screen->base;
136 nvc0->base.copy_data = nvc0_m2mf_copy_linear;
137 nvc0->base.push_data = nvc0_m2mf_push_linear;
138 nvc0->base.push_cb = nvc0_cb_push;
139
140 pipe->screen = pscreen;
141 pipe->priv = priv;
142
143 pipe->destroy = nvc0_destroy;
144
145 pipe->draw_vbo = nvc0_draw_vbo;
146 pipe->clear = nvc0_clear;
147
148 pipe->flush = nvc0_flush;
149 pipe->texture_barrier = nvc0_texture_barrier;
150
151 if (!screen->cur_ctx) {
152 screen->cur_ctx = nvc0;
153 nouveau_pushbuf_bufctx(screen->base.pushbuf, nvc0->bufctx);
154 }
155 screen->base.pushbuf->kick_notify = nvc0_default_kick_notify;
156
157 nvc0_init_query_functions(nvc0);
158 nvc0_init_surface_functions(nvc0);
159 nvc0_init_state_functions(nvc0);
160 nvc0_init_resource_functions(pipe);
161
162 nvc0->draw = draw_create(pipe);
163 assert(nvc0->draw);
164 draw_set_rasterize_stage(nvc0->draw, nvc0_draw_render_stage(nvc0));
165
166 nouveau_context_init_vdec(&nvc0->base);
167
168 /* shader builtin library is per-screen, but we need a context for m2mf */
169 nvc0_program_library_upload(nvc0);
170
171 /* add permanently resident buffers to bufctxts */
172
173 flags = NOUVEAU_BO_VRAM | NOUVEAU_BO_RD;
174
175 BCTX_REFN_bo(nvc0->bufctx_3d, SCREEN, flags, screen->text);
176 BCTX_REFN_bo(nvc0->bufctx_3d, SCREEN, flags, screen->uniforms);
177 BCTX_REFN_bo(nvc0->bufctx_3d, SCREEN, flags, screen->txc);
178
179 flags = NOUVEAU_BO_GART | NOUVEAU_BO_WR;
180
181 BCTX_REFN_bo(nvc0->bufctx_3d, SCREEN, flags, screen->fence.bo);
182 BCTX_REFN_bo(nvc0->bufctx, FENCE, flags, screen->fence.bo);
183
184 return pipe;
185
186 out_err:
187 if (nvc0) {
188 if (nvc0->bufctx_3d)
189 nouveau_bufctx_del(&nvc0->bufctx_3d);
190 if (nvc0->bufctx)
191 nouveau_bufctx_del(&nvc0->bufctx);
192 FREE(nvc0);
193 }
194 return NULL;
195 }
196
197 void
198 nvc0_bufctx_fence(struct nvc0_context *nvc0, struct nouveau_bufctx *bufctx,
199 boolean on_flush)
200 {
201 struct nouveau_list *list = on_flush ? &bufctx->current : &bufctx->pending;
202 struct nouveau_list *it;
203
204 for (it = list->next; it != list; it = it->next) {
205 struct nouveau_bufref *ref = (struct nouveau_bufref *)it;
206 struct nv04_resource *res = ref->priv;
207 if (res)
208 nvc0_resource_validate(res, (unsigned)ref->priv_data);
209 }
210 }