nvc0: add some driver statistics queries
[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 nouveau_context_update_frame_stats(&nvc0->base);
48 }
49
50 static void
51 nvc0_texture_barrier(struct pipe_context *pipe)
52 {
53 struct nouveau_pushbuf *push = nvc0_context(pipe)->base.pushbuf;
54
55 IMMED_NVC0(push, NVC0_3D(SERIALIZE), 0);
56 IMMED_NVC0(push, NVC0_3D(TEX_CACHE_CTL), 0);
57 }
58
59 static void
60 nvc0_context_unreference_resources(struct nvc0_context *nvc0)
61 {
62 unsigned s, i;
63
64 nouveau_bufctx_del(&nvc0->bufctx_3d);
65 nouveau_bufctx_del(&nvc0->bufctx);
66 nouveau_bufctx_del(&nvc0->bufctx_cp);
67
68 util_unreference_framebuffer_state(&nvc0->framebuffer);
69
70 for (i = 0; i < nvc0->num_vtxbufs; ++i)
71 pipe_resource_reference(&nvc0->vtxbuf[i].buffer, NULL);
72
73 pipe_resource_reference(&nvc0->idxbuf.buffer, NULL);
74
75 for (s = 0; s < 6; ++s) {
76 for (i = 0; i < nvc0->num_textures[s]; ++i)
77 pipe_sampler_view_reference(&nvc0->textures[s][i], NULL);
78
79 for (i = 0; i < NVC0_MAX_PIPE_CONSTBUFS; ++i)
80 if (!nvc0->constbuf[s][i].user)
81 pipe_resource_reference(&nvc0->constbuf[s][i].u.buf, NULL);
82 }
83
84 for (s = 0; s < 2; ++s) {
85 for (i = 0; i < NVC0_MAX_SURFACE_SLOTS; ++i)
86 pipe_surface_reference(&nvc0->surfaces[s][i], NULL);
87 }
88
89 for (i = 0; i < nvc0->num_tfbbufs; ++i)
90 pipe_so_target_reference(&nvc0->tfbbuf[i], NULL);
91
92 for (i = 0; i < nvc0->global_residents.size / sizeof(struct pipe_resource *);
93 ++i) {
94 struct pipe_resource **res = util_dynarray_element(
95 &nvc0->global_residents, struct pipe_resource *, i);
96 pipe_resource_reference(res, NULL);
97 }
98 util_dynarray_fini(&nvc0->global_residents);
99 }
100
101 static void
102 nvc0_destroy(struct pipe_context *pipe)
103 {
104 struct nvc0_context *nvc0 = nvc0_context(pipe);
105
106 if (nvc0->screen->cur_ctx == nvc0) {
107 nvc0->base.pushbuf->kick_notify = NULL;
108 nvc0->screen->cur_ctx = NULL;
109 nouveau_pushbuf_bufctx(nvc0->base.pushbuf, NULL);
110 }
111 nouveau_pushbuf_kick(nvc0->base.pushbuf, nvc0->base.pushbuf->channel);
112
113 nvc0_context_unreference_resources(nvc0);
114
115 #ifdef NVC0_WITH_DRAW_MODULE
116 draw_destroy(nvc0->draw);
117 #endif
118
119 nouveau_context_destroy(&nvc0->base);
120 }
121
122 void
123 nvc0_default_kick_notify(struct nouveau_pushbuf *push)
124 {
125 struct nvc0_screen *screen = push->user_priv;
126
127 if (screen) {
128 nouveau_fence_next(&screen->base);
129 nouveau_fence_update(&screen->base, TRUE);
130 if (screen->cur_ctx)
131 screen->cur_ctx->state.flushed = TRUE;
132 }
133 NOUVEAU_DRV_STAT(&screen->base, pushbuf_count, 1);
134 }
135
136 static int
137 nvc0_invalidate_resource_storage(struct nouveau_context *ctx,
138 struct pipe_resource *res,
139 int ref)
140 {
141 struct nvc0_context *nvc0 = nvc0_context(&ctx->pipe);
142 unsigned s, i;
143
144 if (res->bind & PIPE_BIND_RENDER_TARGET) {
145 for (i = 0; i < nvc0->framebuffer.nr_cbufs; ++i) {
146 if (nvc0->framebuffer.cbufs[i] &&
147 nvc0->framebuffer.cbufs[i]->texture == res) {
148 nvc0->dirty |= NVC0_NEW_FRAMEBUFFER;
149 nouveau_bufctx_reset(nvc0->bufctx_3d, NVC0_BIND_FB);
150 if (!--ref)
151 return ref;
152 }
153 }
154 }
155 if (res->bind & PIPE_BIND_DEPTH_STENCIL) {
156 if (nvc0->framebuffer.zsbuf &&
157 nvc0->framebuffer.zsbuf->texture == res) {
158 nvc0->dirty |= NVC0_NEW_FRAMEBUFFER;
159 nouveau_bufctx_reset(nvc0->bufctx_3d, NVC0_BIND_FB);
160 if (!--ref)
161 return ref;
162 }
163 }
164
165 if (res->bind & PIPE_BIND_VERTEX_BUFFER) {
166 for (i = 0; i < nvc0->num_vtxbufs; ++i) {
167 if (nvc0->vtxbuf[i].buffer == res) {
168 nvc0->dirty |= NVC0_NEW_ARRAYS;
169 nouveau_bufctx_reset(nvc0->bufctx_3d, NVC0_BIND_VTX);
170 if (!--ref)
171 return ref;
172 }
173 }
174 }
175 if (res->bind & PIPE_BIND_INDEX_BUFFER) {
176 if (nvc0->idxbuf.buffer == res) {
177 nvc0->dirty |= NVC0_NEW_IDXBUF;
178 nouveau_bufctx_reset(nvc0->bufctx_3d, NVC0_BIND_IDX);
179 if (!--ref)
180 return ref;
181 }
182 }
183
184 if (res->bind & PIPE_BIND_SAMPLER_VIEW) {
185 for (s = 0; s < 5; ++s) {
186 for (i = 0; i < nvc0->num_textures[s]; ++i) {
187 if (nvc0->textures[s][i] &&
188 nvc0->textures[s][i]->texture == res) {
189 nvc0->textures_dirty[s] |= 1 << i;
190 nvc0->dirty |= NVC0_NEW_TEXTURES;
191 nouveau_bufctx_reset(nvc0->bufctx_3d, NVC0_BIND_TEX(s, i));
192 if (!--ref)
193 return ref;
194 }
195 }
196 }
197 }
198
199 if (res->bind & PIPE_BIND_CONSTANT_BUFFER) {
200 for (s = 0; s < 5; ++s) {
201 for (i = 0; i < nvc0->num_vtxbufs; ++i) {
202 if (!nvc0->constbuf[s][i].user &&
203 nvc0->constbuf[s][i].u.buf == res) {
204 nvc0->dirty |= NVC0_NEW_CONSTBUF;
205 nvc0->constbuf_dirty[s] |= 1 << i;
206 nouveau_bufctx_reset(nvc0->bufctx_3d, NVC0_BIND_CB(s, i));
207 if (!--ref)
208 return ref;
209 }
210 }
211 }
212 }
213
214 return ref;
215 }
216
217 struct pipe_context *
218 nvc0_create(struct pipe_screen *pscreen, void *priv)
219 {
220 struct nvc0_screen *screen = nvc0_screen(pscreen);
221 struct nvc0_context *nvc0;
222 struct pipe_context *pipe;
223 int ret;
224 uint32_t flags;
225
226 nvc0 = CALLOC_STRUCT(nvc0_context);
227 if (!nvc0)
228 return NULL;
229 pipe = &nvc0->base.pipe;
230
231 if (!nvc0_blitctx_create(nvc0))
232 goto out_err;
233
234 nvc0->base.pushbuf = screen->base.pushbuf;
235 nvc0->base.client = screen->base.client;
236
237 ret = nouveau_bufctx_new(screen->base.client, 2, &nvc0->bufctx);
238 if (!ret)
239 ret = nouveau_bufctx_new(screen->base.client, NVC0_BIND_3D_COUNT,
240 &nvc0->bufctx_3d);
241 if (!ret)
242 ret = nouveau_bufctx_new(screen->base.client, NVC0_BIND_CP_COUNT,
243 &nvc0->bufctx_cp);
244 if (ret)
245 goto out_err;
246
247 nvc0->screen = screen;
248 nvc0->base.screen = &screen->base;
249
250 pipe->screen = pscreen;
251 pipe->priv = priv;
252
253 pipe->destroy = nvc0_destroy;
254
255 pipe->draw_vbo = nvc0_draw_vbo;
256 pipe->clear = nvc0_clear;
257 if (nvc0->screen->base.class_3d >= NVE4_3D_CLASS)
258 pipe->launch_grid = nve4_launch_grid;
259
260 pipe->flush = nvc0_flush;
261 pipe->texture_barrier = nvc0_texture_barrier;
262
263 if (!screen->cur_ctx) {
264 screen->cur_ctx = nvc0;
265 nouveau_pushbuf_bufctx(screen->base.pushbuf, nvc0->bufctx);
266 }
267 screen->base.pushbuf->kick_notify = nvc0_default_kick_notify;
268
269 nvc0_init_query_functions(nvc0);
270 nvc0_init_surface_functions(nvc0);
271 nvc0_init_state_functions(nvc0);
272 nvc0_init_transfer_functions(nvc0);
273 nvc0_init_resource_functions(pipe);
274
275 nvc0->base.invalidate_resource_storage = nvc0_invalidate_resource_storage;
276
277 #ifdef NVC0_WITH_DRAW_MODULE
278 /* no software fallbacks implemented */
279 nvc0->draw = draw_create(pipe);
280 assert(nvc0->draw);
281 draw_set_rasterize_stage(nvc0->draw, nvc0_draw_render_stage(nvc0));
282 #endif
283
284 pipe->create_video_decoder = nvc0_create_decoder;
285 pipe->create_video_buffer = nvc0_video_buffer_create;
286
287 /* shader builtin library is per-screen, but we need a context for m2mf */
288 nvc0_program_library_upload(nvc0);
289
290 /* add permanently resident buffers to bufctxts */
291
292 flags = NOUVEAU_BO_VRAM | NOUVEAU_BO_RD;
293
294 BCTX_REFN_bo(nvc0->bufctx_3d, SCREEN, flags, screen->text);
295 BCTX_REFN_bo(nvc0->bufctx_3d, SCREEN, flags, screen->uniform_bo);
296 BCTX_REFN_bo(nvc0->bufctx_3d, SCREEN, flags, screen->txc);
297 if (screen->compute) {
298 BCTX_REFN_bo(nvc0->bufctx_cp, CP_SCREEN, flags, screen->text);
299 BCTX_REFN_bo(nvc0->bufctx_cp, CP_SCREEN, flags, screen->txc);
300 BCTX_REFN_bo(nvc0->bufctx_cp, CP_SCREEN, flags, screen->parm);
301 }
302
303 flags = NOUVEAU_BO_VRAM | NOUVEAU_BO_RDWR;
304
305 BCTX_REFN_bo(nvc0->bufctx_3d, SCREEN, flags, screen->poly_cache);
306 if (screen->compute)
307 BCTX_REFN_bo(nvc0->bufctx_cp, CP_SCREEN, flags, screen->tls);
308
309 flags = NOUVEAU_BO_GART | NOUVEAU_BO_WR;
310
311 BCTX_REFN_bo(nvc0->bufctx_3d, SCREEN, flags, screen->fence.bo);
312 BCTX_REFN_bo(nvc0->bufctx, FENCE, flags, screen->fence.bo);
313 if (screen->compute)
314 BCTX_REFN_bo(nvc0->bufctx_cp, CP_SCREEN, flags, screen->fence.bo);
315
316 nvc0->base.scratch.bo_size = 2 << 20;
317
318 memset(nvc0->tex_handles, ~0, sizeof(nvc0->tex_handles));
319
320 util_dynarray_init(&nvc0->global_residents);
321
322 return pipe;
323
324 out_err:
325 if (nvc0) {
326 if (nvc0->bufctx_3d)
327 nouveau_bufctx_del(&nvc0->bufctx_3d);
328 if (nvc0->bufctx_cp)
329 nouveau_bufctx_del(&nvc0->bufctx_cp);
330 if (nvc0->bufctx)
331 nouveau_bufctx_del(&nvc0->bufctx);
332 if (nvc0->blit)
333 FREE(nvc0->blit);
334 FREE(nvc0);
335 }
336 return NULL;
337 }
338
339 void
340 nvc0_bufctx_fence(struct nvc0_context *nvc0, struct nouveau_bufctx *bufctx,
341 boolean on_flush)
342 {
343 struct nouveau_list *list = on_flush ? &bufctx->current : &bufctx->pending;
344 struct nouveau_list *it;
345 NOUVEAU_DRV_STAT_IFD(unsigned count = 0);
346
347 for (it = list->next; it != list; it = it->next) {
348 struct nouveau_bufref *ref = (struct nouveau_bufref *)it;
349 struct nv04_resource *res = ref->priv;
350 if (res)
351 nvc0_resource_validate(res, (unsigned)ref->priv_data);
352 NOUVEAU_DRV_STAT_IFD(count++);
353 }
354 NOUVEAU_DRV_STAT(&nvc0->screen->base, resource_validate_count, count);
355 }