nv50: do an explicit flush on draw when there are persistent buffers
[mesa.git] / src / gallium / drivers / nouveau / nv50 / nv50_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 NV50_WITH_DRAW_MODULE
27 #include "draw/draw_context.h"
28 #endif
29
30 #include "nv50/nv50_context.h"
31 #include "nv50/nv50_screen.h"
32 #include "nv50/nv50_resource.h"
33
34 static void
35 nv50_flush(struct pipe_context *pipe,
36 struct pipe_fence_handle **fence,
37 unsigned flags)
38 {
39 struct nouveau_screen *screen = nouveau_screen(pipe->screen);
40
41 if (fence)
42 nouveau_fence_ref(screen->fence.current, (struct nouveau_fence **)fence);
43
44 PUSH_KICK(screen->pushbuf);
45
46 nouveau_context_update_frame_stats(nouveau_context(pipe));
47 }
48
49 static void
50 nv50_texture_barrier(struct pipe_context *pipe)
51 {
52 struct nouveau_pushbuf *push = nv50_context(pipe)->base.pushbuf;
53
54 BEGIN_NV04(push, SUBC_3D(NV50_GRAPH_SERIALIZE), 1);
55 PUSH_DATA (push, 0);
56 BEGIN_NV04(push, NV50_3D(TEX_CACHE_CTL), 1);
57 PUSH_DATA (push, 0x20);
58 }
59
60 static void
61 nv50_memory_barrier(struct pipe_context *pipe, unsigned flags)
62 {
63 struct nv50_context *nv50 = nv50_context(pipe);
64 int i, s;
65
66 if (flags & PIPE_BARRIER_MAPPED_BUFFER) {
67 for (i = 0; i < nv50->num_vtxbufs; ++i) {
68 if (!nv50->vtxbuf[i].buffer)
69 continue;
70 if (nv50->vtxbuf[i].buffer->flags & PIPE_RESOURCE_FLAG_MAP_PERSISTENT)
71 nv50->base.vbo_dirty = TRUE;
72 }
73
74 if (nv50->idxbuf.buffer &&
75 nv50->idxbuf.buffer->flags & PIPE_RESOURCE_FLAG_MAP_PERSISTENT)
76 nv50->base.vbo_dirty = TRUE;
77
78 for (s = 0; s < 3 && !nv50->cb_dirty; ++s) {
79 uint32_t valid = nv50->constbuf_valid[s];
80
81 while (valid && !nv50->cb_dirty) {
82 const unsigned i = ffs(valid) - 1;
83 struct pipe_resource *res;
84
85 valid &= ~(1 << i);
86 if (nv50->constbuf[s][i].user)
87 continue;
88
89 res = nv50->constbuf[s][i].u.buf;
90 if (!res)
91 continue;
92
93 if (res->flags & PIPE_RESOURCE_FLAG_MAP_PERSISTENT)
94 nv50->cb_dirty = TRUE;
95 }
96 }
97 }
98 }
99
100 void
101 nv50_default_kick_notify(struct nouveau_pushbuf *push)
102 {
103 struct nv50_screen *screen = push->user_priv;
104
105 if (screen) {
106 nouveau_fence_next(&screen->base);
107 nouveau_fence_update(&screen->base, TRUE);
108 if (screen->cur_ctx)
109 screen->cur_ctx->state.flushed = TRUE;
110 }
111 }
112
113 static void
114 nv50_context_unreference_resources(struct nv50_context *nv50)
115 {
116 unsigned s, i;
117
118 nouveau_bufctx_del(&nv50->bufctx_3d);
119 nouveau_bufctx_del(&nv50->bufctx);
120
121 util_unreference_framebuffer_state(&nv50->framebuffer);
122
123 assert(nv50->num_vtxbufs <= PIPE_MAX_ATTRIBS);
124 for (i = 0; i < nv50->num_vtxbufs; ++i)
125 pipe_resource_reference(&nv50->vtxbuf[i].buffer, NULL);
126
127 pipe_resource_reference(&nv50->idxbuf.buffer, NULL);
128
129 for (s = 0; s < 3; ++s) {
130 assert(nv50->num_textures[s] <= PIPE_MAX_SAMPLERS);
131 for (i = 0; i < nv50->num_textures[s]; ++i)
132 pipe_sampler_view_reference(&nv50->textures[s][i], NULL);
133
134 for (i = 0; i < NV50_MAX_PIPE_CONSTBUFS; ++i)
135 if (!nv50->constbuf[s][i].user)
136 pipe_resource_reference(&nv50->constbuf[s][i].u.buf, NULL);
137 }
138 }
139
140 static void
141 nv50_destroy(struct pipe_context *pipe)
142 {
143 struct nv50_context *nv50 = nv50_context(pipe);
144
145 if (nv50_context_screen(nv50)->cur_ctx == nv50)
146 nv50_context_screen(nv50)->cur_ctx = NULL;
147 nouveau_pushbuf_bufctx(nv50->base.pushbuf, NULL);
148 nouveau_pushbuf_kick(nv50->base.pushbuf, nv50->base.pushbuf->channel);
149
150 nv50_context_unreference_resources(nv50);
151
152 #ifdef NV50_WITH_DRAW_MODULE
153 draw_destroy(nv50->draw);
154 #endif
155
156 FREE(nv50->blit);
157
158 nouveau_context_destroy(&nv50->base);
159 }
160
161 static int
162 nv50_invalidate_resource_storage(struct nouveau_context *ctx,
163 struct pipe_resource *res,
164 int ref)
165 {
166 struct nv50_context *nv50 = nv50_context(&ctx->pipe);
167 unsigned s, i;
168
169 if (res->bind & PIPE_BIND_RENDER_TARGET) {
170 assert(nv50->framebuffer.nr_cbufs <= PIPE_MAX_COLOR_BUFS);
171 for (i = 0; i < nv50->framebuffer.nr_cbufs; ++i) {
172 if (nv50->framebuffer.cbufs[i] &&
173 nv50->framebuffer.cbufs[i]->texture == res) {
174 nv50->dirty |= NV50_NEW_FRAMEBUFFER;
175 nouveau_bufctx_reset(nv50->bufctx_3d, NV50_BIND_FB);
176 if (!--ref)
177 return ref;
178 }
179 }
180 }
181 if (res->bind & PIPE_BIND_DEPTH_STENCIL) {
182 if (nv50->framebuffer.zsbuf &&
183 nv50->framebuffer.zsbuf->texture == res) {
184 nv50->dirty |= NV50_NEW_FRAMEBUFFER;
185 nouveau_bufctx_reset(nv50->bufctx_3d, NV50_BIND_FB);
186 if (!--ref)
187 return ref;
188 }
189 }
190
191 if (res->bind & PIPE_BIND_VERTEX_BUFFER) {
192 assert(nv50->num_vtxbufs <= PIPE_MAX_ATTRIBS);
193 for (i = 0; i < nv50->num_vtxbufs; ++i) {
194 if (nv50->vtxbuf[i].buffer == res) {
195 nv50->dirty |= NV50_NEW_ARRAYS;
196 nouveau_bufctx_reset(nv50->bufctx_3d, NV50_BIND_VERTEX);
197 if (!--ref)
198 return ref;
199 }
200 }
201 }
202 if (res->bind & PIPE_BIND_INDEX_BUFFER) {
203 if (nv50->idxbuf.buffer == res)
204 if (!--ref)
205 return ref;
206 }
207
208 if (res->bind & PIPE_BIND_SAMPLER_VIEW) {
209 for (s = 0; s < 3; ++s) {
210 assert(nv50->num_textures[s] <= PIPE_MAX_SAMPLERS);
211 for (i = 0; i < nv50->num_textures[s]; ++i) {
212 if (nv50->textures[s][i] &&
213 nv50->textures[s][i]->texture == res) {
214 nv50->dirty |= NV50_NEW_TEXTURES;
215 nouveau_bufctx_reset(nv50->bufctx_3d, NV50_BIND_TEXTURES);
216 if (!--ref)
217 return ref;
218 }
219 }
220 }
221 }
222
223 if (res->bind & PIPE_BIND_CONSTANT_BUFFER) {
224 for (s = 0; s < 3; ++s) {
225 assert(nv50->num_vtxbufs <= NV50_MAX_PIPE_CONSTBUFS);
226 for (i = 0; i < nv50->num_vtxbufs; ++i) {
227 if (!nv50->constbuf[s][i].user &&
228 nv50->constbuf[s][i].u.buf == res) {
229 nv50->dirty |= NV50_NEW_CONSTBUF;
230 nv50->constbuf_dirty[s] |= 1 << i;
231 nouveau_bufctx_reset(nv50->bufctx_3d, NV50_BIND_CB(s, i));
232 if (!--ref)
233 return ref;
234 }
235 }
236 }
237 }
238
239 return ref;
240 }
241
242 static void
243 nv50_context_get_sample_position(struct pipe_context *, unsigned, unsigned,
244 float *);
245
246 struct pipe_context *
247 nv50_create(struct pipe_screen *pscreen, void *priv)
248 {
249 struct nv50_screen *screen = nv50_screen(pscreen);
250 struct nv50_context *nv50;
251 struct pipe_context *pipe;
252 int ret;
253 uint32_t flags;
254
255 nv50 = CALLOC_STRUCT(nv50_context);
256 if (!nv50)
257 return NULL;
258 pipe = &nv50->base.pipe;
259
260 if (!nv50_blitctx_create(nv50))
261 goto out_err;
262
263 nv50->base.pushbuf = screen->base.pushbuf;
264 nv50->base.client = screen->base.client;
265
266 ret = nouveau_bufctx_new(screen->base.client, NV50_BIND_COUNT,
267 &nv50->bufctx_3d);
268 if (!ret)
269 ret = nouveau_bufctx_new(screen->base.client, 2, &nv50->bufctx);
270 if (ret)
271 goto out_err;
272
273 nv50->base.screen = &screen->base;
274 nv50->base.copy_data = nv50_m2mf_copy_linear;
275 nv50->base.push_data = nv50_sifc_linear_u8;
276 /* FIXME: Make it possible to use this again. The problem is that there is
277 * some clever logic in the card that allows for multiple renders to happen
278 * when there are only constbuf changes. However that relies on the
279 * constbuf updates happening to the right constbuf slots. Currently
280 * implementation just makes it go through a separate slot which doesn't
281 * properly update the right constbuf data.
282 nv50->base.push_cb = nv50_cb_push;
283 */
284
285 nv50->screen = screen;
286 pipe->screen = pscreen;
287 pipe->priv = priv;
288
289 pipe->destroy = nv50_destroy;
290
291 pipe->draw_vbo = nv50_draw_vbo;
292 pipe->clear = nv50_clear;
293
294 pipe->flush = nv50_flush;
295 pipe->texture_barrier = nv50_texture_barrier;
296 pipe->memory_barrier = nv50_memory_barrier;
297 pipe->get_sample_position = nv50_context_get_sample_position;
298
299 if (!screen->cur_ctx) {
300 screen->cur_ctx = nv50;
301 nouveau_pushbuf_bufctx(screen->base.pushbuf, nv50->bufctx);
302 }
303 nv50->base.pushbuf->kick_notify = nv50_default_kick_notify;
304
305 nv50_init_query_functions(nv50);
306 nv50_init_surface_functions(nv50);
307 nv50_init_state_functions(nv50);
308 nv50_init_resource_functions(pipe);
309
310 nv50->base.invalidate_resource_storage = nv50_invalidate_resource_storage;
311
312 #ifdef NV50_WITH_DRAW_MODULE
313 /* no software fallbacks implemented */
314 nv50->draw = draw_create(pipe);
315 assert(nv50->draw);
316 draw_set_rasterize_stage(nv50->draw, nv50_draw_render_stage(nv50));
317 #endif
318
319 if (screen->base.device->chipset < 0x84 ||
320 debug_get_bool_option("NOUVEAU_PMPEG", FALSE)) {
321 /* PMPEG */
322 nouveau_context_init_vdec(&nv50->base);
323 } else if (screen->base.device->chipset < 0x98 ||
324 screen->base.device->chipset == 0xa0) {
325 /* VP2 */
326 pipe->create_video_codec = nv84_create_decoder;
327 pipe->create_video_buffer = nv84_video_buffer_create;
328 } else {
329 /* VP3/4 */
330 pipe->create_video_codec = nv98_create_decoder;
331 pipe->create_video_buffer = nv98_video_buffer_create;
332 }
333
334 flags = NOUVEAU_BO_VRAM | NOUVEAU_BO_RD;
335
336 BCTX_REFN_bo(nv50->bufctx_3d, SCREEN, flags, screen->code);
337 BCTX_REFN_bo(nv50->bufctx_3d, SCREEN, flags, screen->uniforms);
338 BCTX_REFN_bo(nv50->bufctx_3d, SCREEN, flags, screen->txc);
339 BCTX_REFN_bo(nv50->bufctx_3d, SCREEN, flags, screen->stack_bo);
340
341 flags = NOUVEAU_BO_GART | NOUVEAU_BO_WR;
342
343 BCTX_REFN_bo(nv50->bufctx_3d, SCREEN, flags, screen->fence.bo);
344 BCTX_REFN_bo(nv50->bufctx, FENCE, flags, screen->fence.bo);
345
346 nv50->base.scratch.bo_size = 2 << 20;
347
348 return pipe;
349
350 out_err:
351 if (nv50->bufctx_3d)
352 nouveau_bufctx_del(&nv50->bufctx_3d);
353 if (nv50->bufctx)
354 nouveau_bufctx_del(&nv50->bufctx);
355 if (nv50->blit)
356 FREE(nv50->blit);
357 FREE(nv50);
358 return NULL;
359 }
360
361 void
362 nv50_bufctx_fence(struct nouveau_bufctx *bufctx, boolean on_flush)
363 {
364 struct nouveau_list *list = on_flush ? &bufctx->current : &bufctx->pending;
365 struct nouveau_list *it;
366
367 for (it = list->next; it != list; it = it->next) {
368 struct nouveau_bufref *ref = (struct nouveau_bufref *)it;
369 struct nv04_resource *res = ref->priv;
370 if (res)
371 nv50_resource_validate(res, (unsigned)ref->priv_data);
372 }
373 }
374
375 static void
376 nv50_context_get_sample_position(struct pipe_context *pipe,
377 unsigned sample_count, unsigned sample_index,
378 float *xy)
379 {
380 static const uint8_t ms1[1][2] = { { 0x8, 0x8 } };
381 static const uint8_t ms2[2][2] = {
382 { 0x4, 0x4 }, { 0xc, 0xc } }; /* surface coords (0,0), (1,0) */
383 static const uint8_t ms4[4][2] = {
384 { 0x6, 0x2 }, { 0xe, 0x6 }, /* (0,0), (1,0) */
385 { 0x2, 0xa }, { 0xa, 0xe } }; /* (0,1), (1,1) */
386 static const uint8_t ms8[8][2] = {
387 { 0x1, 0x7 }, { 0x5, 0x3 }, /* (0,0), (1,0) */
388 { 0x3, 0xd }, { 0x7, 0xb }, /* (0,1), (1,1) */
389 { 0x9, 0x5 }, { 0xf, 0x1 }, /* (2,0), (3,0) */
390 { 0xb, 0xf }, { 0xd, 0x9 } }; /* (2,1), (3,1) */
391 #if 0
392 /* NOTE: there are alternative modes for MS2 and MS8, currently not used */
393 static const uint8_t ms8_alt[8][2] = {
394 { 0x9, 0x5 }, { 0x7, 0xb }, /* (2,0), (1,1) */
395 { 0xd, 0x9 }, { 0x5, 0x3 }, /* (3,1), (1,0) */
396 { 0x3, 0xd }, { 0x1, 0x7 }, /* (0,1), (0,0) */
397 { 0xb, 0xf }, { 0xf, 0x1 } }; /* (2,1), (3,0) */
398 #endif
399
400 const uint8_t (*ptr)[2];
401
402 switch (sample_count) {
403 case 0:
404 case 1: ptr = ms1; break;
405 case 2: ptr = ms2; break;
406 case 4: ptr = ms4; break;
407 case 8: ptr = ms8; break;
408 default:
409 assert(0);
410 return; /* bad sample count -> undefined locations */
411 }
412 xy[0] = ptr[sample_index][0] * 0.0625f;
413 xy[1] = ptr[sample_index][1] * 0.0625f;
414 }