nvc0: prevent VERTEXID/INSTANCEID from consuming input slots
[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 nouveau_screen *screen = &nvc0_context(pipe)->screen->base;
37
38 if (fence)
39 nouveau_fence_ref(screen->fence.current, (struct nouveau_fence **)fence);
40
41 /* Try to emit before firing to avoid having to flush again right after
42 * in case we have to wait on this fence.
43 */
44 nouveau_fence_emit(screen->fence.current);
45
46 FIRE_RING(screen->channel);
47 }
48
49 static void
50 nvc0_texture_barrier(struct pipe_context *pipe)
51 {
52 struct nouveau_channel *chan = nvc0_context(pipe)->screen->base.channel;
53
54 IMMED_RING(chan, RING_3D(SERIALIZE), 0);
55 IMMED_RING(chan, RING_3D(TEX_CACHE_CTL), 0);
56 }
57
58 static void
59 nvc0_context_unreference_resources(struct nvc0_context *nvc0)
60 {
61 unsigned s, i;
62
63 for (i = 0; i < NVC0_BUFCTX_COUNT; ++i)
64 nvc0_bufctx_reset(nvc0, i);
65
66 for (i = 0; i < nvc0->num_vtxbufs; ++i)
67 pipe_resource_reference(&nvc0->vtxbuf[i].buffer, NULL);
68
69 pipe_resource_reference(&nvc0->idxbuf.buffer, NULL);
70
71 for (s = 0; s < 5; ++s) {
72 for (i = 0; i < nvc0->num_textures[s]; ++i)
73 pipe_sampler_view_reference(&nvc0->textures[s][i], NULL);
74
75 for (i = 0; i < 16; ++i)
76 pipe_resource_reference(&nvc0->constbuf[s][i], NULL);
77 }
78
79 for (i = 0; i < nvc0->num_tfbbufs; ++i)
80 pipe_resource_reference(&nvc0->tfbbuf[i], NULL);
81 }
82
83 static void
84 nvc0_destroy(struct pipe_context *pipe)
85 {
86 struct nvc0_context *nvc0 = nvc0_context(pipe);
87
88 nvc0_context_unreference_resources(nvc0);
89
90 draw_destroy(nvc0->draw);
91
92 if (nvc0->screen->cur_ctx == nvc0)
93 nvc0->screen->cur_ctx = NULL;
94
95 FREE(nvc0);
96 }
97
98 void
99 nvc0_default_flush_notify(struct nouveau_channel *chan)
100 {
101 struct nvc0_screen *screen = chan->user_private;
102
103 if (!screen)
104 return;
105
106 nouveau_fence_update(&screen->base, TRUE);
107 nouveau_fence_next(&screen->base);
108 }
109
110 struct pipe_context *
111 nvc0_create(struct pipe_screen *pscreen, void *priv)
112 {
113 struct pipe_winsys *pipe_winsys = pscreen->winsys;
114 struct nvc0_screen *screen = nvc0_screen(pscreen);
115 struct nvc0_context *nvc0;
116 struct pipe_context *pipe;
117
118 nvc0 = CALLOC_STRUCT(nvc0_context);
119 if (!nvc0)
120 return NULL;
121 pipe = &nvc0->base.pipe;
122
123 nvc0->screen = screen;
124 nvc0->base.screen = &screen->base;
125 nvc0->base.copy_data = nvc0_m2mf_copy_linear;
126 nvc0->base.push_data = nvc0_m2mf_push_linear;
127
128 pipe->winsys = pipe_winsys;
129 pipe->screen = pscreen;
130 pipe->priv = priv;
131
132 pipe->destroy = nvc0_destroy;
133
134 pipe->draw_vbo = nvc0_draw_vbo;
135 pipe->clear = nvc0_clear;
136
137 pipe->flush = nvc0_flush;
138 pipe->texture_barrier = nvc0_texture_barrier;
139
140 if (!screen->cur_ctx)
141 screen->cur_ctx = nvc0;
142 screen->base.channel->flush_notify = nvc0_default_flush_notify;
143
144 nvc0_init_query_functions(nvc0);
145 nvc0_init_surface_functions(nvc0);
146 nvc0_init_state_functions(nvc0);
147 nvc0_init_resource_functions(pipe);
148
149 nvc0->draw = draw_create(pipe);
150 assert(nvc0->draw);
151 draw_set_rasterize_stage(nvc0->draw, nvc0_draw_render_stage(nvc0));
152
153 nouveau_context_init_vdec(&nvc0->base);
154
155 /* shader builtin library is per-screen, but we need a context for m2mf */
156 nvc0_program_library_upload(nvc0);
157
158 return pipe;
159 }
160
161 struct resident {
162 struct nv04_resource *res;
163 uint32_t flags;
164 };
165
166 void
167 nvc0_bufctx_add_resident(struct nvc0_context *nvc0, int ctx,
168 struct nv04_resource *resource, uint32_t flags)
169 {
170 struct resident rsd = { resource, flags };
171
172 if (!resource->bo)
173 return;
174 nvc0->residents_size += sizeof(struct resident);
175
176 /* We don't need to reference the resource here, it will be referenced
177 * in the context/state, and bufctx will be reset when state changes.
178 */
179 util_dynarray_append(&nvc0->residents[ctx], struct resident, rsd);
180 }
181
182 void
183 nvc0_bufctx_del_resident(struct nvc0_context *nvc0, int ctx,
184 struct nv04_resource *resource)
185 {
186 struct resident *rsd, *top;
187 unsigned i;
188
189 for (i = 0; i < nvc0->residents[ctx].size / sizeof(struct resident); ++i) {
190 rsd = util_dynarray_element(&nvc0->residents[ctx], struct resident, i);
191
192 if (rsd->res == resource) {
193 top = util_dynarray_pop_ptr(&nvc0->residents[ctx], struct resident);
194 if (rsd != top)
195 *rsd = *top;
196 nvc0->residents_size -= sizeof(struct resident);
197 break;
198 }
199 }
200 }
201
202 void
203 nvc0_bufctx_emit_relocs(struct nvc0_context *nvc0)
204 {
205 struct resident *rsd;
206 struct util_dynarray *array;
207 unsigned ctx, i, n;
208
209 n = nvc0->residents_size / sizeof(struct resident);
210 n += NVC0_SCREEN_RESIDENT_BO_COUNT;
211
212 MARK_RING(nvc0->screen->base.channel, 0, n);
213
214 for (ctx = 0; ctx < NVC0_BUFCTX_COUNT; ++ctx) {
215 array = &nvc0->residents[ctx];
216
217 n = array->size / sizeof(struct resident);
218 for (i = 0; i < n; ++i) {
219 rsd = util_dynarray_element(array, struct resident, i);
220
221 nvc0_resource_validate(rsd->res, rsd->flags);
222 }
223 }
224
225 nvc0_screen_make_buffers_resident(nvc0->screen);
226 }