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