5928c994c5de35afff4fb97d05559a592aa57945
[mesa.git] / src / gallium / drivers / nouveau / 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 OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE 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/nvc0_context.h"
31 #include "nvc0/nvc0_screen.h"
32 #include "nvc0/nvc0_resource.h"
33
34 static void
35 nvc0_flush(struct pipe_context *pipe,
36 struct pipe_fence_handle **fence,
37 unsigned 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_memory_barrier(struct pipe_context *pipe, unsigned flags)
61 {
62 struct nvc0_context *nvc0 = nvc0_context(pipe);
63 int i, s;
64
65 if (flags & PIPE_BARRIER_MAPPED_BUFFER) {
66 for (i = 0; i < nvc0->num_vtxbufs; ++i) {
67 if (!nvc0->vtxbuf[i].buffer)
68 continue;
69 if (nvc0->vtxbuf[i].buffer->flags & PIPE_RESOURCE_FLAG_MAP_PERSISTENT)
70 nvc0->base.vbo_dirty = TRUE;
71 }
72
73 if (nvc0->idxbuf.buffer &&
74 nvc0->idxbuf.buffer->flags & PIPE_RESOURCE_FLAG_MAP_PERSISTENT)
75 nvc0->base.vbo_dirty = TRUE;
76
77 for (s = 0; s < 5 && !nvc0->cb_dirty; ++s) {
78 uint32_t valid = nvc0->constbuf_valid[s];
79
80 while (valid && !nvc0->cb_dirty) {
81 const unsigned i = ffs(valid) - 1;
82 struct pipe_resource *res;
83
84 valid &= ~(1 << i);
85 if (nvc0->constbuf[s][i].user)
86 continue;
87
88 res = nvc0->constbuf[s][i].u.buf;
89 if (!res)
90 continue;
91
92 if (res->flags & PIPE_RESOURCE_FLAG_MAP_PERSISTENT)
93 nvc0->cb_dirty = TRUE;
94 }
95 }
96 }
97 }
98
99 static void
100 nvc0_context_unreference_resources(struct nvc0_context *nvc0)
101 {
102 unsigned s, i;
103
104 nouveau_bufctx_del(&nvc0->bufctx_3d);
105 nouveau_bufctx_del(&nvc0->bufctx);
106 nouveau_bufctx_del(&nvc0->bufctx_cp);
107
108 util_unreference_framebuffer_state(&nvc0->framebuffer);
109
110 for (i = 0; i < nvc0->num_vtxbufs; ++i)
111 pipe_resource_reference(&nvc0->vtxbuf[i].buffer, NULL);
112
113 pipe_resource_reference(&nvc0->idxbuf.buffer, NULL);
114
115 for (s = 0; s < 6; ++s) {
116 for (i = 0; i < nvc0->num_textures[s]; ++i)
117 pipe_sampler_view_reference(&nvc0->textures[s][i], NULL);
118
119 for (i = 0; i < NVC0_MAX_PIPE_CONSTBUFS; ++i)
120 if (!nvc0->constbuf[s][i].user)
121 pipe_resource_reference(&nvc0->constbuf[s][i].u.buf, NULL);
122 }
123
124 for (s = 0; s < 2; ++s) {
125 for (i = 0; i < NVC0_MAX_SURFACE_SLOTS; ++i)
126 pipe_surface_reference(&nvc0->surfaces[s][i], NULL);
127 }
128
129 for (i = 0; i < nvc0->num_tfbbufs; ++i)
130 pipe_so_target_reference(&nvc0->tfbbuf[i], NULL);
131
132 for (i = 0; i < nvc0->global_residents.size / sizeof(struct pipe_resource *);
133 ++i) {
134 struct pipe_resource **res = util_dynarray_element(
135 &nvc0->global_residents, struct pipe_resource *, i);
136 pipe_resource_reference(res, NULL);
137 }
138 util_dynarray_fini(&nvc0->global_residents);
139 }
140
141 static void
142 nvc0_destroy(struct pipe_context *pipe)
143 {
144 struct nvc0_context *nvc0 = nvc0_context(pipe);
145
146 if (nvc0->screen->cur_ctx == nvc0)
147 nvc0->screen->cur_ctx = NULL;
148 /* Unset bufctx, we don't want to revalidate any resources after the flush.
149 * Other contexts will always set their bufctx again on action calls.
150 */
151 nouveau_pushbuf_bufctx(nvc0->base.pushbuf, NULL);
152 nouveau_pushbuf_kick(nvc0->base.pushbuf, nvc0->base.pushbuf->channel);
153
154 nvc0_context_unreference_resources(nvc0);
155 nvc0_blitctx_destroy(nvc0);
156
157 #ifdef NVC0_WITH_DRAW_MODULE
158 draw_destroy(nvc0->draw);
159 #endif
160
161 nouveau_context_destroy(&nvc0->base);
162 }
163
164 void
165 nvc0_default_kick_notify(struct nouveau_pushbuf *push)
166 {
167 struct nvc0_screen *screen = push->user_priv;
168
169 if (screen) {
170 nouveau_fence_next(&screen->base);
171 nouveau_fence_update(&screen->base, TRUE);
172 if (screen->cur_ctx)
173 screen->cur_ctx->state.flushed = TRUE;
174 }
175 NOUVEAU_DRV_STAT(&screen->base, pushbuf_count, 1);
176 }
177
178 static int
179 nvc0_invalidate_resource_storage(struct nouveau_context *ctx,
180 struct pipe_resource *res,
181 int ref)
182 {
183 struct nvc0_context *nvc0 = nvc0_context(&ctx->pipe);
184 unsigned s, i;
185
186 if (res->bind & PIPE_BIND_RENDER_TARGET) {
187 for (i = 0; i < nvc0->framebuffer.nr_cbufs; ++i) {
188 if (nvc0->framebuffer.cbufs[i] &&
189 nvc0->framebuffer.cbufs[i]->texture == res) {
190 nvc0->dirty |= NVC0_NEW_FRAMEBUFFER;
191 nouveau_bufctx_reset(nvc0->bufctx_3d, NVC0_BIND_FB);
192 if (!--ref)
193 return ref;
194 }
195 }
196 }
197 if (res->bind & PIPE_BIND_DEPTH_STENCIL) {
198 if (nvc0->framebuffer.zsbuf &&
199 nvc0->framebuffer.zsbuf->texture == res) {
200 nvc0->dirty |= NVC0_NEW_FRAMEBUFFER;
201 nouveau_bufctx_reset(nvc0->bufctx_3d, NVC0_BIND_FB);
202 if (!--ref)
203 return ref;
204 }
205 }
206
207 if (res->bind & PIPE_BIND_VERTEX_BUFFER) {
208 for (i = 0; i < nvc0->num_vtxbufs; ++i) {
209 if (nvc0->vtxbuf[i].buffer == res) {
210 nvc0->dirty |= NVC0_NEW_ARRAYS;
211 nouveau_bufctx_reset(nvc0->bufctx_3d, NVC0_BIND_VTX);
212 if (!--ref)
213 return ref;
214 }
215 }
216 }
217 if (res->bind & PIPE_BIND_INDEX_BUFFER) {
218 if (nvc0->idxbuf.buffer == res) {
219 nvc0->dirty |= NVC0_NEW_IDXBUF;
220 nouveau_bufctx_reset(nvc0->bufctx_3d, NVC0_BIND_IDX);
221 if (!--ref)
222 return ref;
223 }
224 }
225
226 if (res->bind & PIPE_BIND_SAMPLER_VIEW) {
227 for (s = 0; s < 5; ++s) {
228 for (i = 0; i < nvc0->num_textures[s]; ++i) {
229 if (nvc0->textures[s][i] &&
230 nvc0->textures[s][i]->texture == res) {
231 nvc0->textures_dirty[s] |= 1 << i;
232 nvc0->dirty |= NVC0_NEW_TEXTURES;
233 nouveau_bufctx_reset(nvc0->bufctx_3d, NVC0_BIND_TEX(s, i));
234 if (!--ref)
235 return ref;
236 }
237 }
238 }
239 }
240
241 if (res->bind & PIPE_BIND_CONSTANT_BUFFER) {
242 for (s = 0; s < 5; ++s) {
243 for (i = 0; i < nvc0->num_vtxbufs; ++i) {
244 if (!nvc0->constbuf[s][i].user &&
245 nvc0->constbuf[s][i].u.buf == res) {
246 nvc0->dirty |= NVC0_NEW_CONSTBUF;
247 nvc0->constbuf_dirty[s] |= 1 << i;
248 nouveau_bufctx_reset(nvc0->bufctx_3d, NVC0_BIND_CB(s, i));
249 if (!--ref)
250 return ref;
251 }
252 }
253 }
254 }
255
256 return ref;
257 }
258
259 static void
260 nvc0_context_get_sample_position(struct pipe_context *, unsigned, unsigned,
261 float *);
262
263 struct pipe_context *
264 nvc0_create(struct pipe_screen *pscreen, void *priv)
265 {
266 struct nvc0_screen *screen = nvc0_screen(pscreen);
267 struct nvc0_context *nvc0;
268 struct pipe_context *pipe;
269 int ret;
270 uint32_t flags;
271
272 nvc0 = CALLOC_STRUCT(nvc0_context);
273 if (!nvc0)
274 return NULL;
275 pipe = &nvc0->base.pipe;
276
277 if (!nvc0_blitctx_create(nvc0))
278 goto out_err;
279
280 nvc0->base.pushbuf = screen->base.pushbuf;
281 nvc0->base.client = screen->base.client;
282
283 ret = nouveau_bufctx_new(screen->base.client, 2, &nvc0->bufctx);
284 if (!ret)
285 ret = nouveau_bufctx_new(screen->base.client, NVC0_BIND_3D_COUNT,
286 &nvc0->bufctx_3d);
287 if (!ret)
288 ret = nouveau_bufctx_new(screen->base.client, NVC0_BIND_CP_COUNT,
289 &nvc0->bufctx_cp);
290 if (ret)
291 goto out_err;
292
293 nvc0->screen = screen;
294 nvc0->base.screen = &screen->base;
295
296 pipe->screen = pscreen;
297 pipe->priv = priv;
298
299 pipe->destroy = nvc0_destroy;
300
301 pipe->draw_vbo = nvc0_draw_vbo;
302 pipe->clear = nvc0_clear;
303 pipe->launch_grid = (nvc0->screen->base.class_3d >= NVE4_3D_CLASS) ?
304 nve4_launch_grid : nvc0_launch_grid;
305
306 pipe->flush = nvc0_flush;
307 pipe->texture_barrier = nvc0_texture_barrier;
308 pipe->memory_barrier = nvc0_memory_barrier;
309 pipe->get_sample_position = nvc0_context_get_sample_position;
310
311 if (!screen->cur_ctx) {
312 screen->cur_ctx = nvc0;
313 nouveau_pushbuf_bufctx(screen->base.pushbuf, nvc0->bufctx);
314 }
315 screen->base.pushbuf->kick_notify = nvc0_default_kick_notify;
316
317 nvc0_init_query_functions(nvc0);
318 nvc0_init_surface_functions(nvc0);
319 nvc0_init_state_functions(nvc0);
320 nvc0_init_transfer_functions(nvc0);
321 nvc0_init_resource_functions(pipe);
322
323 nvc0->base.invalidate_resource_storage = nvc0_invalidate_resource_storage;
324
325 #ifdef NVC0_WITH_DRAW_MODULE
326 /* no software fallbacks implemented */
327 nvc0->draw = draw_create(pipe);
328 assert(nvc0->draw);
329 draw_set_rasterize_stage(nvc0->draw, nvc0_draw_render_stage(nvc0));
330 #endif
331
332 pipe->create_video_codec = nvc0_create_decoder;
333 pipe->create_video_buffer = nvc0_video_buffer_create;
334
335 /* shader builtin library is per-screen, but we need a context for m2mf */
336 nvc0_program_library_upload(nvc0);
337
338 /* add permanently resident buffers to bufctxts */
339
340 flags = NOUVEAU_BO_VRAM | NOUVEAU_BO_RD;
341
342 BCTX_REFN_bo(nvc0->bufctx_3d, SCREEN, flags, screen->text);
343 BCTX_REFN_bo(nvc0->bufctx_3d, SCREEN, flags, screen->uniform_bo);
344 BCTX_REFN_bo(nvc0->bufctx_3d, SCREEN, flags, screen->txc);
345 if (screen->compute) {
346 BCTX_REFN_bo(nvc0->bufctx_cp, CP_SCREEN, flags, screen->text);
347 BCTX_REFN_bo(nvc0->bufctx_cp, CP_SCREEN, flags, screen->txc);
348 BCTX_REFN_bo(nvc0->bufctx_cp, CP_SCREEN, flags, screen->parm);
349 }
350
351 flags = NOUVEAU_BO_VRAM | NOUVEAU_BO_RDWR;
352
353 if (screen->poly_cache)
354 BCTX_REFN_bo(nvc0->bufctx_3d, SCREEN, flags, screen->poly_cache);
355 if (screen->compute)
356 BCTX_REFN_bo(nvc0->bufctx_cp, CP_SCREEN, flags, screen->tls);
357
358 flags = NOUVEAU_BO_GART | NOUVEAU_BO_WR;
359
360 BCTX_REFN_bo(nvc0->bufctx_3d, SCREEN, flags, screen->fence.bo);
361 BCTX_REFN_bo(nvc0->bufctx, FENCE, flags, screen->fence.bo);
362 if (screen->compute)
363 BCTX_REFN_bo(nvc0->bufctx_cp, CP_SCREEN, flags, screen->fence.bo);
364
365 nvc0->base.scratch.bo_size = 2 << 20;
366
367 memset(nvc0->tex_handles, ~0, sizeof(nvc0->tex_handles));
368
369 util_dynarray_init(&nvc0->global_residents);
370
371 return pipe;
372
373 out_err:
374 if (nvc0) {
375 if (nvc0->bufctx_3d)
376 nouveau_bufctx_del(&nvc0->bufctx_3d);
377 if (nvc0->bufctx_cp)
378 nouveau_bufctx_del(&nvc0->bufctx_cp);
379 if (nvc0->bufctx)
380 nouveau_bufctx_del(&nvc0->bufctx);
381 if (nvc0->blit)
382 FREE(nvc0->blit);
383 FREE(nvc0);
384 }
385 return NULL;
386 }
387
388 void
389 nvc0_bufctx_fence(struct nvc0_context *nvc0, struct nouveau_bufctx *bufctx,
390 boolean on_flush)
391 {
392 struct nouveau_list *list = on_flush ? &bufctx->current : &bufctx->pending;
393 struct nouveau_list *it;
394 NOUVEAU_DRV_STAT_IFD(unsigned count = 0);
395
396 for (it = list->next; it != list; it = it->next) {
397 struct nouveau_bufref *ref = (struct nouveau_bufref *)it;
398 struct nv04_resource *res = ref->priv;
399 if (res)
400 nvc0_resource_validate(res, (unsigned)ref->priv_data);
401 NOUVEAU_DRV_STAT_IFD(count++);
402 }
403 NOUVEAU_DRV_STAT(&nvc0->screen->base, resource_validate_count, count);
404 }
405
406 static void
407 nvc0_context_get_sample_position(struct pipe_context *pipe,
408 unsigned sample_count, unsigned sample_index,
409 float *xy)
410 {
411 static const uint8_t ms1[1][2] = { { 0x8, 0x8 } };
412 static const uint8_t ms2[2][2] = {
413 { 0x4, 0x4 }, { 0xc, 0xc } }; /* surface coords (0,0), (1,0) */
414 static const uint8_t ms4[4][2] = {
415 { 0x6, 0x2 }, { 0xe, 0x6 }, /* (0,0), (1,0) */
416 { 0x2, 0xa }, { 0xa, 0xe } }; /* (0,1), (1,1) */
417 static const uint8_t ms8[8][2] = {
418 { 0x1, 0x7 }, { 0x5, 0x3 }, /* (0,0), (1,0) */
419 { 0x3, 0xd }, { 0x7, 0xb }, /* (0,1), (1,1) */
420 { 0x9, 0x5 }, { 0xf, 0x1 }, /* (2,0), (3,0) */
421 { 0xb, 0xf }, { 0xd, 0x9 } }; /* (2,1), (3,1) */
422 #if 0
423 /* NOTE: there are alternative modes for MS2 and MS8, currently not used */
424 static const uint8_t ms8_alt[8][2] = {
425 { 0x9, 0x5 }, { 0x7, 0xb }, /* (2,0), (1,1) */
426 { 0xd, 0x9 }, { 0x5, 0x3 }, /* (3,1), (1,0) */
427 { 0x3, 0xd }, { 0x1, 0x7 }, /* (0,1), (0,0) */
428 { 0xb, 0xf }, { 0xf, 0x1 } }; /* (2,1), (3,0) */
429 #endif
430
431 const uint8_t (*ptr)[2];
432
433 switch (sample_count) {
434 case 0:
435 case 1: ptr = ms1; break;
436 case 2: ptr = ms2; break;
437 case 4: ptr = ms4; break;
438 case 8: ptr = ms8; break;
439 default:
440 assert(0);
441 return; /* bad sample count -> undefined locations */
442 }
443 xy[0] = ptr[sample_index][0] * 0.0625f;
444 xy[1] = ptr[sample_index][1] * 0.0625f;
445 }