nvc0: identify VERTEX_QUARANTINE
[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_channel *chan = nvc0->screen->base.channel;
38
39 /* XXX This flag wasn't set by the state tracker anyway. */
40 /*if (flags & PIPE_FLUSH_TEXTURE_CACHE) {
41 BEGIN_RING(chan, RING_3D(SERIALIZE), 1);
42 OUT_RING (chan, 0);
43 BEGIN_RING(chan, RING_3D(TEX_CACHE_CTL), 1);
44 OUT_RING (chan, 0x00);
45 } else*/
46 /* XXX FLUSH_FRAME is now implicit. */
47 /*if ((flags & PIPE_FLUSH_RENDER_CACHE) && !(flags & PIPE_FLUSH_FRAME)) {
48 BEGIN_RING(chan, RING_3D(SERIALIZE), 1);
49 OUT_RING (chan, 0);
50 }*/
51
52 if (fence)
53 nouveau_fence_ref(nvc0->screen->base.fence.current,
54 (struct nouveau_fence **)fence);
55
56 FIRE_RING(chan);
57 }
58
59 static void
60 nvc0_destroy(struct pipe_context *pipe)
61 {
62 struct nvc0_context *nvc0 = nvc0_context(pipe);
63
64 draw_destroy(nvc0->draw);
65
66 if (nvc0->screen->cur_ctx == nvc0) {
67 nvc0->screen->base.channel->user_private = NULL;
68 nvc0->screen->cur_ctx = NULL;
69 }
70
71 FREE(nvc0);
72 }
73
74 void
75 nvc0_default_flush_notify(struct nouveau_channel *chan)
76 {
77 struct nvc0_context *nvc0 = chan->user_private;
78
79 if (!nvc0)
80 return;
81
82 nouveau_fence_update(&nvc0->screen->base, TRUE);
83 nouveau_fence_next(&nvc0->screen->base);
84 }
85
86 struct pipe_context *
87 nvc0_create(struct pipe_screen *pscreen, void *priv)
88 {
89 struct pipe_winsys *pipe_winsys = pscreen->winsys;
90 struct nvc0_screen *screen = nvc0_screen(pscreen);
91 struct nvc0_context *nvc0;
92 struct pipe_context *pipe;
93
94 nvc0 = CALLOC_STRUCT(nvc0_context);
95 if (!nvc0)
96 return NULL;
97 pipe = &nvc0->base.pipe;
98
99 nvc0->screen = screen;
100 nvc0->base.screen = &screen->base;
101 nvc0->base.copy_data = nvc0_m2mf_copy_linear;
102 nvc0->base.push_data = nvc0_m2mf_push_linear;
103
104 pipe->winsys = pipe_winsys;
105 pipe->screen = pscreen;
106 pipe->priv = priv;
107
108 pipe->destroy = nvc0_destroy;
109
110 pipe->draw_vbo = nvc0_draw_vbo;
111 pipe->clear = nvc0_clear;
112
113 pipe->flush = nvc0_flush;
114
115 if (!screen->cur_ctx)
116 screen->cur_ctx = nvc0;
117 screen->base.channel->user_private = nvc0;
118 screen->base.channel->flush_notify = nvc0_default_flush_notify;
119
120 nvc0_init_query_functions(nvc0);
121 nvc0_init_surface_functions(nvc0);
122 nvc0_init_state_functions(nvc0);
123 nvc0_init_resource_functions(pipe);
124
125 nvc0->draw = draw_create(pipe);
126 assert(nvc0->draw);
127 draw_set_rasterize_stage(nvc0->draw, nvc0_draw_render_stage(nvc0));
128
129 return pipe;
130 }
131
132 struct resident {
133 struct nv04_resource *res;
134 uint32_t flags;
135 };
136
137 void
138 nvc0_bufctx_add_resident(struct nvc0_context *nvc0, int ctx,
139 struct nv04_resource *resource, uint32_t flags)
140 {
141 struct resident rsd = { resource, flags };
142
143 if (!resource->bo)
144 return;
145
146 /* We don't need to reference the resource here, it will be referenced
147 * in the context/state, and bufctx will be reset when state changes.
148 */
149 util_dynarray_append(&nvc0->residents[ctx], struct resident, rsd);
150 }
151
152 void
153 nvc0_bufctx_del_resident(struct nvc0_context *nvc0, int ctx,
154 struct nv04_resource *resource)
155 {
156 struct resident *rsd, *top;
157 unsigned i;
158
159 for (i = 0; i < nvc0->residents[ctx].size / sizeof(struct resident); ++i) {
160 rsd = util_dynarray_element(&nvc0->residents[ctx], struct resident, i);
161
162 if (rsd->res == resource) {
163 top = util_dynarray_pop_ptr(&nvc0->residents[ctx], struct resident);
164 if (rsd != top)
165 *rsd = *top;
166 break;
167 }
168 }
169 }
170
171 void
172 nvc0_bufctx_emit_relocs(struct nvc0_context *nvc0)
173 {
174 struct resident *rsd;
175 struct util_dynarray *array;
176 unsigned ctx, i, n;
177
178 for (ctx = 0; ctx < NVC0_BUFCTX_COUNT; ++ctx) {
179 array = &nvc0->residents[ctx];
180
181 n = array->size / sizeof(struct resident);
182 MARK_RING(nvc0->screen->base.channel, n, n);
183 for (i = 0; i < n; ++i) {
184 rsd = util_dynarray_element(array, struct resident, i);
185
186 nvc0_resource_validate(rsd->res, rsd->flags);
187 }
188 }
189
190 nvc0_screen_make_buffers_resident(nvc0->screen);
191 }