nvc0: expose 4 vertex streams, use stream ids in xfb
[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;
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 }
79
80 void
81 nv50_default_kick_notify(struct nouveau_pushbuf *push)
82 {
83 struct nv50_screen *screen = push->user_priv;
84
85 if (screen) {
86 nouveau_fence_next(&screen->base);
87 nouveau_fence_update(&screen->base, TRUE);
88 if (screen->cur_ctx)
89 screen->cur_ctx->state.flushed = TRUE;
90 }
91 }
92
93 static void
94 nv50_context_unreference_resources(struct nv50_context *nv50)
95 {
96 unsigned s, i;
97
98 nouveau_bufctx_del(&nv50->bufctx_3d);
99 nouveau_bufctx_del(&nv50->bufctx);
100
101 util_unreference_framebuffer_state(&nv50->framebuffer);
102
103 assert(nv50->num_vtxbufs <= PIPE_MAX_ATTRIBS);
104 for (i = 0; i < nv50->num_vtxbufs; ++i)
105 pipe_resource_reference(&nv50->vtxbuf[i].buffer, NULL);
106
107 pipe_resource_reference(&nv50->idxbuf.buffer, NULL);
108
109 for (s = 0; s < 3; ++s) {
110 assert(nv50->num_textures[s] <= PIPE_MAX_SAMPLERS);
111 for (i = 0; i < nv50->num_textures[s]; ++i)
112 pipe_sampler_view_reference(&nv50->textures[s][i], NULL);
113
114 for (i = 0; i < NV50_MAX_PIPE_CONSTBUFS; ++i)
115 if (!nv50->constbuf[s][i].user)
116 pipe_resource_reference(&nv50->constbuf[s][i].u.buf, NULL);
117 }
118 }
119
120 static void
121 nv50_destroy(struct pipe_context *pipe)
122 {
123 struct nv50_context *nv50 = nv50_context(pipe);
124
125 if (nv50_context_screen(nv50)->cur_ctx == nv50)
126 nv50_context_screen(nv50)->cur_ctx = NULL;
127 nouveau_pushbuf_bufctx(nv50->base.pushbuf, NULL);
128 nouveau_pushbuf_kick(nv50->base.pushbuf, nv50->base.pushbuf->channel);
129
130 nv50_context_unreference_resources(nv50);
131
132 #ifdef NV50_WITH_DRAW_MODULE
133 draw_destroy(nv50->draw);
134 #endif
135
136 FREE(nv50->blit);
137
138 nouveau_context_destroy(&nv50->base);
139 }
140
141 static int
142 nv50_invalidate_resource_storage(struct nouveau_context *ctx,
143 struct pipe_resource *res,
144 int ref)
145 {
146 struct nv50_context *nv50 = nv50_context(&ctx->pipe);
147 unsigned s, i;
148
149 if (res->bind & PIPE_BIND_RENDER_TARGET) {
150 assert(nv50->framebuffer.nr_cbufs <= PIPE_MAX_COLOR_BUFS);
151 for (i = 0; i < nv50->framebuffer.nr_cbufs; ++i) {
152 if (nv50->framebuffer.cbufs[i] &&
153 nv50->framebuffer.cbufs[i]->texture == res) {
154 nv50->dirty |= NV50_NEW_FRAMEBUFFER;
155 nouveau_bufctx_reset(nv50->bufctx_3d, NV50_BIND_FB);
156 if (!--ref)
157 return ref;
158 }
159 }
160 }
161 if (res->bind & PIPE_BIND_DEPTH_STENCIL) {
162 if (nv50->framebuffer.zsbuf &&
163 nv50->framebuffer.zsbuf->texture == res) {
164 nv50->dirty |= NV50_NEW_FRAMEBUFFER;
165 nouveau_bufctx_reset(nv50->bufctx_3d, NV50_BIND_FB);
166 if (!--ref)
167 return ref;
168 }
169 }
170
171 if (res->bind & PIPE_BIND_VERTEX_BUFFER) {
172 assert(nv50->num_vtxbufs <= PIPE_MAX_ATTRIBS);
173 for (i = 0; i < nv50->num_vtxbufs; ++i) {
174 if (nv50->vtxbuf[i].buffer == res) {
175 nv50->dirty |= NV50_NEW_ARRAYS;
176 nouveau_bufctx_reset(nv50->bufctx_3d, NV50_BIND_VERTEX);
177 if (!--ref)
178 return ref;
179 }
180 }
181 }
182 if (res->bind & PIPE_BIND_INDEX_BUFFER) {
183 if (nv50->idxbuf.buffer == res)
184 if (!--ref)
185 return ref;
186 }
187
188 if (res->bind & PIPE_BIND_SAMPLER_VIEW) {
189 for (s = 0; s < 3; ++s) {
190 assert(nv50->num_textures[s] <= PIPE_MAX_SAMPLERS);
191 for (i = 0; i < nv50->num_textures[s]; ++i) {
192 if (nv50->textures[s][i] &&
193 nv50->textures[s][i]->texture == res) {
194 nv50->dirty |= NV50_NEW_TEXTURES;
195 nouveau_bufctx_reset(nv50->bufctx_3d, NV50_BIND_TEXTURES);
196 if (!--ref)
197 return ref;
198 }
199 }
200 }
201 }
202
203 if (res->bind & PIPE_BIND_CONSTANT_BUFFER) {
204 for (s = 0; s < 3; ++s) {
205 assert(nv50->num_vtxbufs <= NV50_MAX_PIPE_CONSTBUFS);
206 for (i = 0; i < nv50->num_vtxbufs; ++i) {
207 if (!nv50->constbuf[s][i].user &&
208 nv50->constbuf[s][i].u.buf == res) {
209 nv50->dirty |= NV50_NEW_CONSTBUF;
210 nv50->constbuf_dirty[s] |= 1 << i;
211 nouveau_bufctx_reset(nv50->bufctx_3d, NV50_BIND_CB(s, i));
212 if (!--ref)
213 return ref;
214 }
215 }
216 }
217 }
218
219 return ref;
220 }
221
222 static void
223 nv50_context_get_sample_position(struct pipe_context *, unsigned, unsigned,
224 float *);
225
226 struct pipe_context *
227 nv50_create(struct pipe_screen *pscreen, void *priv)
228 {
229 struct nv50_screen *screen = nv50_screen(pscreen);
230 struct nv50_context *nv50;
231 struct pipe_context *pipe;
232 int ret;
233 uint32_t flags;
234
235 nv50 = CALLOC_STRUCT(nv50_context);
236 if (!nv50)
237 return NULL;
238 pipe = &nv50->base.pipe;
239
240 if (!nv50_blitctx_create(nv50))
241 goto out_err;
242
243 nv50->base.pushbuf = screen->base.pushbuf;
244 nv50->base.client = screen->base.client;
245
246 ret = nouveau_bufctx_new(screen->base.client, NV50_BIND_COUNT,
247 &nv50->bufctx_3d);
248 if (!ret)
249 ret = nouveau_bufctx_new(screen->base.client, 2, &nv50->bufctx);
250 if (ret)
251 goto out_err;
252
253 nv50->base.screen = &screen->base;
254 nv50->base.copy_data = nv50_m2mf_copy_linear;
255 nv50->base.push_data = nv50_sifc_linear_u8;
256 nv50->base.push_cb = nv50_cb_push;
257
258 nv50->screen = screen;
259 pipe->screen = pscreen;
260 pipe->priv = priv;
261
262 pipe->destroy = nv50_destroy;
263
264 pipe->draw_vbo = nv50_draw_vbo;
265 pipe->clear = nv50_clear;
266
267 pipe->flush = nv50_flush;
268 pipe->texture_barrier = nv50_texture_barrier;
269 pipe->memory_barrier = nv50_memory_barrier;
270 pipe->get_sample_position = nv50_context_get_sample_position;
271
272 if (!screen->cur_ctx) {
273 screen->cur_ctx = nv50;
274 nouveau_pushbuf_bufctx(screen->base.pushbuf, nv50->bufctx);
275 }
276 nv50->base.pushbuf->kick_notify = nv50_default_kick_notify;
277
278 nv50_init_query_functions(nv50);
279 nv50_init_surface_functions(nv50);
280 nv50_init_state_functions(nv50);
281 nv50_init_resource_functions(pipe);
282
283 nv50->base.invalidate_resource_storage = nv50_invalidate_resource_storage;
284
285 #ifdef NV50_WITH_DRAW_MODULE
286 /* no software fallbacks implemented */
287 nv50->draw = draw_create(pipe);
288 assert(nv50->draw);
289 draw_set_rasterize_stage(nv50->draw, nv50_draw_render_stage(nv50));
290 #endif
291
292 if (screen->base.device->chipset < 0x84 ||
293 debug_get_bool_option("NOUVEAU_PMPEG", FALSE)) {
294 /* PMPEG */
295 nouveau_context_init_vdec(&nv50->base);
296 } else if (screen->base.device->chipset < 0x98 ||
297 screen->base.device->chipset == 0xa0) {
298 /* VP2 */
299 pipe->create_video_codec = nv84_create_decoder;
300 pipe->create_video_buffer = nv84_video_buffer_create;
301 } else {
302 /* VP3/4 */
303 pipe->create_video_codec = nv98_create_decoder;
304 pipe->create_video_buffer = nv98_video_buffer_create;
305 }
306
307 flags = NOUVEAU_BO_VRAM | NOUVEAU_BO_RD;
308
309 BCTX_REFN_bo(nv50->bufctx_3d, SCREEN, flags, screen->code);
310 BCTX_REFN_bo(nv50->bufctx_3d, SCREEN, flags, screen->uniforms);
311 BCTX_REFN_bo(nv50->bufctx_3d, SCREEN, flags, screen->txc);
312 BCTX_REFN_bo(nv50->bufctx_3d, SCREEN, flags, screen->stack_bo);
313
314 flags = NOUVEAU_BO_GART | NOUVEAU_BO_WR;
315
316 BCTX_REFN_bo(nv50->bufctx_3d, SCREEN, flags, screen->fence.bo);
317 BCTX_REFN_bo(nv50->bufctx, FENCE, flags, screen->fence.bo);
318
319 nv50->base.scratch.bo_size = 2 << 20;
320
321 return pipe;
322
323 out_err:
324 if (nv50->bufctx_3d)
325 nouveau_bufctx_del(&nv50->bufctx_3d);
326 if (nv50->bufctx)
327 nouveau_bufctx_del(&nv50->bufctx);
328 if (nv50->blit)
329 FREE(nv50->blit);
330 FREE(nv50);
331 return NULL;
332 }
333
334 void
335 nv50_bufctx_fence(struct nouveau_bufctx *bufctx, boolean on_flush)
336 {
337 struct nouveau_list *list = on_flush ? &bufctx->current : &bufctx->pending;
338 struct nouveau_list *it;
339
340 for (it = list->next; it != list; it = it->next) {
341 struct nouveau_bufref *ref = (struct nouveau_bufref *)it;
342 struct nv04_resource *res = ref->priv;
343 if (res)
344 nv50_resource_validate(res, (unsigned)ref->priv_data);
345 }
346 }
347
348 static void
349 nv50_context_get_sample_position(struct pipe_context *pipe,
350 unsigned sample_count, unsigned sample_index,
351 float *xy)
352 {
353 static const uint8_t ms1[1][2] = { { 0x8, 0x8 } };
354 static const uint8_t ms2[2][2] = {
355 { 0x4, 0x4 }, { 0xc, 0xc } }; /* surface coords (0,0), (1,0) */
356 static const uint8_t ms4[4][2] = {
357 { 0x6, 0x2 }, { 0xe, 0x6 }, /* (0,0), (1,0) */
358 { 0x2, 0xa }, { 0xa, 0xe } }; /* (0,1), (1,1) */
359 static const uint8_t ms8[8][2] = {
360 { 0x1, 0x7 }, { 0x5, 0x3 }, /* (0,0), (1,0) */
361 { 0x3, 0xd }, { 0x7, 0xb }, /* (0,1), (1,1) */
362 { 0x9, 0x5 }, { 0xf, 0x1 }, /* (2,0), (3,0) */
363 { 0xb, 0xf }, { 0xd, 0x9 } }; /* (2,1), (3,1) */
364 #if 0
365 /* NOTE: there are alternative modes for MS2 and MS8, currently not used */
366 static const uint8_t ms8_alt[8][2] = {
367 { 0x9, 0x5 }, { 0x7, 0xb }, /* (2,0), (1,1) */
368 { 0xd, 0x9 }, { 0x5, 0x3 }, /* (3,1), (1,0) */
369 { 0x3, 0xd }, { 0x1, 0x7 }, /* (0,1), (0,0) */
370 { 0xb, 0xf }, { 0xf, 0x1 } }; /* (2,1), (3,0) */
371 #endif
372
373 const uint8_t (*ptr)[2];
374
375 switch (sample_count) {
376 case 0:
377 case 1: ptr = ms1; break;
378 case 2: ptr = ms2; break;
379 case 4: ptr = ms4; break;
380 case 8: ptr = ms8; break;
381 default:
382 assert(0);
383 return; /* bad sample count -> undefined locations */
384 }
385 xy[0] = ptr[sample_index][0] * 0.0625f;
386 xy[1] = ptr[sample_index][1] * 0.0625f;
387 }