nvc0: add preliminary support for images
[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 struct nouveau_pushbuf *push = nvc0->base.pushbuf;
60 int i, s;
61
62 if (flags & PIPE_BARRIER_MAPPED_BUFFER) {
63 for (i = 0; i < nvc0->num_vtxbufs; ++i) {
64 if (!nvc0->vtxbuf[i].buffer)
65 continue;
66 if (nvc0->vtxbuf[i].buffer->flags & PIPE_RESOURCE_FLAG_MAP_PERSISTENT)
67 nvc0->base.vbo_dirty = true;
68 }
69
70 if (nvc0->idxbuf.buffer &&
71 nvc0->idxbuf.buffer->flags & PIPE_RESOURCE_FLAG_MAP_PERSISTENT)
72 nvc0->base.vbo_dirty = true;
73
74 for (s = 0; s < 5 && !nvc0->cb_dirty; ++s) {
75 uint32_t valid = nvc0->constbuf_valid[s];
76
77 while (valid && !nvc0->cb_dirty) {
78 const unsigned i = ffs(valid) - 1;
79 struct pipe_resource *res;
80
81 valid &= ~(1 << i);
82 if (nvc0->constbuf[s][i].user)
83 continue;
84
85 res = nvc0->constbuf[s][i].u.buf;
86 if (!res)
87 continue;
88
89 if (res->flags & PIPE_RESOURCE_FLAG_MAP_PERSISTENT)
90 nvc0->cb_dirty = true;
91 }
92 }
93 }
94 if (flags & PIPE_BARRIER_SHADER_BUFFER) {
95 IMMED_NVC0(push, NVC0_3D(MEM_BARRIER), 0x1011);
96 }
97 }
98
99 static void
100 nvc0_emit_string_marker(struct pipe_context *pipe, const char *str, int len)
101 {
102 struct nouveau_pushbuf *push = nvc0_context(pipe)->base.pushbuf;
103 int string_words = len / 4;
104 int data_words;
105
106 if (len <= 0)
107 return;
108 string_words = MIN2(string_words, NV04_PFIFO_MAX_PACKET_LEN);
109 if (string_words == NV04_PFIFO_MAX_PACKET_LEN)
110 data_words = string_words;
111 else
112 data_words = string_words + !!(len & 3);
113 BEGIN_NIC0(push, SUBC_3D(NV04_GRAPH_NOP), data_words);
114 if (string_words)
115 PUSH_DATAp(push, str, string_words);
116 if (string_words != data_words) {
117 int data = 0;
118 memcpy(&data, &str[string_words * 4], len & 3);
119 PUSH_DATA (push, data);
120 }
121 }
122
123 static void
124 nvc0_context_unreference_resources(struct nvc0_context *nvc0)
125 {
126 unsigned s, i;
127
128 nouveau_bufctx_del(&nvc0->bufctx_3d);
129 nouveau_bufctx_del(&nvc0->bufctx);
130 nouveau_bufctx_del(&nvc0->bufctx_cp);
131
132 util_unreference_framebuffer_state(&nvc0->framebuffer);
133
134 for (i = 0; i < nvc0->num_vtxbufs; ++i)
135 pipe_resource_reference(&nvc0->vtxbuf[i].buffer, NULL);
136
137 pipe_resource_reference(&nvc0->idxbuf.buffer, NULL);
138
139 for (s = 0; s < 6; ++s) {
140 for (i = 0; i < nvc0->num_textures[s]; ++i)
141 pipe_sampler_view_reference(&nvc0->textures[s][i], NULL);
142
143 for (i = 0; i < NVC0_MAX_PIPE_CONSTBUFS; ++i)
144 if (!nvc0->constbuf[s][i].user)
145 pipe_resource_reference(&nvc0->constbuf[s][i].u.buf, NULL);
146 }
147
148 for (s = 0; s < 2; ++s) {
149 for (i = 0; i < NVC0_MAX_SURFACE_SLOTS; ++i)
150 pipe_surface_reference(&nvc0->surfaces[s][i], NULL);
151 }
152
153 for (s = 0; s < 6; ++s)
154 for (i = 0; i < NVC0_MAX_BUFFERS; ++i)
155 pipe_resource_reference(&nvc0->buffers[s][i].buffer, NULL);
156
157 for (i = 0; i < nvc0->num_tfbbufs; ++i)
158 pipe_so_target_reference(&nvc0->tfbbuf[i], NULL);
159
160 for (i = 0; i < nvc0->global_residents.size / sizeof(struct pipe_resource *);
161 ++i) {
162 struct pipe_resource **res = util_dynarray_element(
163 &nvc0->global_residents, struct pipe_resource *, i);
164 pipe_resource_reference(res, NULL);
165 }
166 util_dynarray_fini(&nvc0->global_residents);
167
168 if (nvc0->tcp_empty)
169 nvc0->base.pipe.delete_tcs_state(&nvc0->base.pipe, nvc0->tcp_empty);
170 }
171
172 static void
173 nvc0_destroy(struct pipe_context *pipe)
174 {
175 struct nvc0_context *nvc0 = nvc0_context(pipe);
176
177 if (nvc0->screen->cur_ctx == nvc0) {
178 nvc0->screen->cur_ctx = NULL;
179 nvc0->screen->save_state = nvc0->state;
180 nvc0->screen->save_state.tfb = NULL;
181 }
182
183 /* Unset bufctx, we don't want to revalidate any resources after the flush.
184 * Other contexts will always set their bufctx again on action calls.
185 */
186 nouveau_pushbuf_bufctx(nvc0->base.pushbuf, NULL);
187 nouveau_pushbuf_kick(nvc0->base.pushbuf, nvc0->base.pushbuf->channel);
188
189 nvc0_context_unreference_resources(nvc0);
190 nvc0_blitctx_destroy(nvc0);
191
192 nouveau_context_destroy(&nvc0->base);
193 }
194
195 void
196 nvc0_default_kick_notify(struct nouveau_pushbuf *push)
197 {
198 struct nvc0_screen *screen = push->user_priv;
199
200 if (screen) {
201 nouveau_fence_next(&screen->base);
202 nouveau_fence_update(&screen->base, true);
203 if (screen->cur_ctx)
204 screen->cur_ctx->state.flushed = true;
205 NOUVEAU_DRV_STAT(&screen->base, pushbuf_count, 1);
206 }
207 }
208
209 static int
210 nvc0_invalidate_resource_storage(struct nouveau_context *ctx,
211 struct pipe_resource *res,
212 int ref)
213 {
214 struct nvc0_context *nvc0 = nvc0_context(&ctx->pipe);
215 unsigned s, i;
216
217 if (res->bind & PIPE_BIND_RENDER_TARGET) {
218 for (i = 0; i < nvc0->framebuffer.nr_cbufs; ++i) {
219 if (nvc0->framebuffer.cbufs[i] &&
220 nvc0->framebuffer.cbufs[i]->texture == res) {
221 nvc0->dirty_3d |= NVC0_NEW_3D_FRAMEBUFFER;
222 nouveau_bufctx_reset(nvc0->bufctx_3d, NVC0_BIND_3D_FB);
223 if (!--ref)
224 return ref;
225 }
226 }
227 }
228 if (res->bind & PIPE_BIND_DEPTH_STENCIL) {
229 if (nvc0->framebuffer.zsbuf &&
230 nvc0->framebuffer.zsbuf->texture == res) {
231 nvc0->dirty_3d |= NVC0_NEW_3D_FRAMEBUFFER;
232 nouveau_bufctx_reset(nvc0->bufctx_3d, NVC0_BIND_3D_FB);
233 if (!--ref)
234 return ref;
235 }
236 }
237
238 if (res->target == PIPE_BUFFER) {
239 for (i = 0; i < nvc0->num_vtxbufs; ++i) {
240 if (nvc0->vtxbuf[i].buffer == res) {
241 nvc0->dirty_3d |= NVC0_NEW_3D_ARRAYS;
242 nouveau_bufctx_reset(nvc0->bufctx_3d, NVC0_BIND_3D_VTX);
243 if (!--ref)
244 return ref;
245 }
246 }
247
248 if (nvc0->idxbuf.buffer == res) {
249 nvc0->dirty_3d |= NVC0_NEW_3D_IDXBUF;
250 nouveau_bufctx_reset(nvc0->bufctx_3d, NVC0_BIND_3D_IDX);
251 if (!--ref)
252 return ref;
253 }
254
255 for (s = 0; s < 5; ++s) {
256 for (i = 0; i < nvc0->num_textures[s]; ++i) {
257 if (nvc0->textures[s][i] &&
258 nvc0->textures[s][i]->texture == res) {
259 nvc0->textures_dirty[s] |= 1 << i;
260 nvc0->dirty_3d |= NVC0_NEW_3D_TEXTURES;
261 nouveau_bufctx_reset(nvc0->bufctx_3d, NVC0_BIND_3D_TEX(s, i));
262 if (!--ref)
263 return ref;
264 }
265 }
266 }
267
268 for (s = 0; s < 6; ++s) {
269 for (i = 0; i < NVC0_MAX_PIPE_CONSTBUFS; ++i) {
270 if (!(nvc0->constbuf_valid[s] & (1 << i)))
271 continue;
272 if (!nvc0->constbuf[s][i].user &&
273 nvc0->constbuf[s][i].u.buf == res) {
274 nvc0->constbuf_dirty[s] |= 1 << i;
275 if (unlikely(s == 5)) {
276 nvc0->dirty_cp |= NVC0_NEW_CP_CONSTBUF;
277 nouveau_bufctx_reset(nvc0->bufctx_cp, NVC0_BIND_CP_CB(i));
278 } else {
279 nvc0->dirty_3d |= NVC0_NEW_3D_CONSTBUF;
280 nouveau_bufctx_reset(nvc0->bufctx_3d, NVC0_BIND_3D_CB(s, i));
281 }
282 if (!--ref)
283 return ref;
284 }
285 }
286 }
287
288 for (s = 0; s < 6; ++s) {
289 for (i = 0; i < NVC0_MAX_BUFFERS; ++i) {
290 if (nvc0->buffers[s][i].buffer == res) {
291 nvc0->buffers_dirty[s] |= 1 << i;
292 if (unlikely(s == 5)) {
293 nvc0->dirty_cp |= NVC0_NEW_CP_BUFFERS;
294 nouveau_bufctx_reset(nvc0->bufctx_cp, NVC0_BIND_CP_BUF);
295 } else {
296 nvc0->dirty_3d |= NVC0_NEW_3D_BUFFERS;
297 nouveau_bufctx_reset(nvc0->bufctx_3d, NVC0_BIND_3D_BUF);
298 }
299 if (!--ref)
300 return ref;
301 }
302 }
303 }
304
305 for (s = 0; s < 6; ++s) {
306 for (i = 0; i < NVC0_MAX_IMAGES; ++i) {
307 if (nvc0->images[s][i].resource == res) {
308 nvc0->images_dirty[s] |= 1 << i;
309 if (unlikely(s == 5)) {
310 nvc0->dirty_cp |= NVC0_NEW_CP_SURFACES;
311 nouveau_bufctx_reset(nvc0->bufctx_cp, NVC0_BIND_CP_SUF);
312 } else {
313 nvc0->dirty_3d |= NVC0_NEW_3D_SURFACES;
314 nouveau_bufctx_reset(nvc0->bufctx_3d, NVC0_BIND_3D_SUF);
315 }
316 }
317 if (!--ref)
318 return ref;
319 }
320 }
321 }
322
323 return ref;
324 }
325
326 static void
327 nvc0_context_get_sample_position(struct pipe_context *, unsigned, unsigned,
328 float *);
329
330 struct pipe_context *
331 nvc0_create(struct pipe_screen *pscreen, void *priv, unsigned ctxflags)
332 {
333 struct nvc0_screen *screen = nvc0_screen(pscreen);
334 struct nvc0_context *nvc0;
335 struct pipe_context *pipe;
336 int ret;
337 uint32_t flags;
338
339 nvc0 = CALLOC_STRUCT(nvc0_context);
340 if (!nvc0)
341 return NULL;
342 pipe = &nvc0->base.pipe;
343
344 if (!nvc0_blitctx_create(nvc0))
345 goto out_err;
346
347 nvc0->base.pushbuf = screen->base.pushbuf;
348 nvc0->base.client = screen->base.client;
349
350 ret = nouveau_bufctx_new(screen->base.client, 2, &nvc0->bufctx);
351 if (!ret)
352 ret = nouveau_bufctx_new(screen->base.client, NVC0_BIND_3D_COUNT,
353 &nvc0->bufctx_3d);
354 if (!ret)
355 ret = nouveau_bufctx_new(screen->base.client, NVC0_BIND_CP_COUNT,
356 &nvc0->bufctx_cp);
357 if (ret)
358 goto out_err;
359
360 nvc0->screen = screen;
361 nvc0->base.screen = &screen->base;
362
363 pipe->screen = pscreen;
364 pipe->priv = priv;
365
366 pipe->destroy = nvc0_destroy;
367
368 pipe->draw_vbo = nvc0_draw_vbo;
369 pipe->clear = nvc0_clear;
370 pipe->launch_grid = (nvc0->screen->base.class_3d >= NVE4_3D_CLASS) ?
371 nve4_launch_grid : nvc0_launch_grid;
372
373 pipe->flush = nvc0_flush;
374 pipe->texture_barrier = nvc0_texture_barrier;
375 pipe->memory_barrier = nvc0_memory_barrier;
376 pipe->get_sample_position = nvc0_context_get_sample_position;
377 pipe->emit_string_marker = nvc0_emit_string_marker;
378
379 nouveau_context_init(&nvc0->base);
380 nvc0_init_query_functions(nvc0);
381 nvc0_init_surface_functions(nvc0);
382 nvc0_init_state_functions(nvc0);
383 nvc0_init_transfer_functions(nvc0);
384 nvc0_init_resource_functions(pipe);
385
386 nvc0->base.invalidate_resource_storage = nvc0_invalidate_resource_storage;
387
388 pipe->create_video_codec = nvc0_create_decoder;
389 pipe->create_video_buffer = nvc0_video_buffer_create;
390
391 /* shader builtin library is per-screen, but we need a context for m2mf */
392 nvc0_program_library_upload(nvc0);
393 nvc0_program_init_tcp_empty(nvc0);
394 if (!nvc0->tcp_empty)
395 goto out_err;
396 /* set the empty tctl prog on next draw in case one is never set */
397 nvc0->dirty_3d |= NVC0_NEW_3D_TCTLPROG;
398
399 /* Do not bind the COMPUTE driver constbuf at screen initialization because
400 * CBs are aliased between 3D and COMPUTE, but make sure it will be bound if
401 * a grid is launched later. */
402 nvc0->dirty_cp |= NVC0_NEW_CP_DRIVERCONST;
403
404 /* now that there are no more opportunities for errors, set the current
405 * context if there isn't already one.
406 */
407 if (!screen->cur_ctx) {
408 nvc0->state = screen->save_state;
409 screen->cur_ctx = nvc0;
410 nouveau_pushbuf_bufctx(screen->base.pushbuf, nvc0->bufctx);
411 }
412 screen->base.pushbuf->kick_notify = nvc0_default_kick_notify;
413
414 /* add permanently resident buffers to bufctxts */
415
416 flags = NV_VRAM_DOMAIN(&screen->base) | NOUVEAU_BO_RD;
417
418 BCTX_REFN_bo(nvc0->bufctx_3d, 3D_SCREEN, flags, screen->text);
419 BCTX_REFN_bo(nvc0->bufctx_3d, 3D_SCREEN, flags, screen->uniform_bo);
420 BCTX_REFN_bo(nvc0->bufctx_3d, 3D_SCREEN, flags, screen->txc);
421 if (screen->compute) {
422 BCTX_REFN_bo(nvc0->bufctx_cp, CP_SCREEN, flags, screen->text);
423 BCTX_REFN_bo(nvc0->bufctx_cp, CP_SCREEN, flags, screen->uniform_bo);
424 BCTX_REFN_bo(nvc0->bufctx_cp, CP_SCREEN, flags, screen->txc);
425 BCTX_REFN_bo(nvc0->bufctx_cp, CP_SCREEN, flags, screen->parm);
426 }
427
428 flags = NV_VRAM_DOMAIN(&screen->base) | NOUVEAU_BO_RDWR;
429
430 if (screen->poly_cache)
431 BCTX_REFN_bo(nvc0->bufctx_3d, 3D_SCREEN, flags, screen->poly_cache);
432 if (screen->compute)
433 BCTX_REFN_bo(nvc0->bufctx_cp, CP_SCREEN, flags, screen->tls);
434
435 flags = NOUVEAU_BO_GART | NOUVEAU_BO_WR;
436
437 BCTX_REFN_bo(nvc0->bufctx_3d, 3D_SCREEN, flags, screen->fence.bo);
438 BCTX_REFN_bo(nvc0->bufctx, FENCE, flags, screen->fence.bo);
439 if (screen->compute)
440 BCTX_REFN_bo(nvc0->bufctx_cp, CP_SCREEN, flags, screen->fence.bo);
441
442 nvc0->base.scratch.bo_size = 2 << 20;
443
444 memset(nvc0->tex_handles, ~0, sizeof(nvc0->tex_handles));
445
446 util_dynarray_init(&nvc0->global_residents);
447
448 return pipe;
449
450 out_err:
451 if (nvc0) {
452 if (nvc0->bufctx_3d)
453 nouveau_bufctx_del(&nvc0->bufctx_3d);
454 if (nvc0->bufctx_cp)
455 nouveau_bufctx_del(&nvc0->bufctx_cp);
456 if (nvc0->bufctx)
457 nouveau_bufctx_del(&nvc0->bufctx);
458 FREE(nvc0->blit);
459 FREE(nvc0);
460 }
461 return NULL;
462 }
463
464 void
465 nvc0_bufctx_fence(struct nvc0_context *nvc0, struct nouveau_bufctx *bufctx,
466 bool on_flush)
467 {
468 struct nouveau_list *list = on_flush ? &bufctx->current : &bufctx->pending;
469 struct nouveau_list *it;
470 NOUVEAU_DRV_STAT_IFD(unsigned count = 0);
471
472 for (it = list->next; it != list; it = it->next) {
473 struct nouveau_bufref *ref = (struct nouveau_bufref *)it;
474 struct nv04_resource *res = ref->priv;
475 if (res)
476 nvc0_resource_validate(res, (unsigned)ref->priv_data);
477 NOUVEAU_DRV_STAT_IFD(count++);
478 }
479 NOUVEAU_DRV_STAT(&nvc0->screen->base, resource_validate_count, count);
480 }
481
482 static void
483 nvc0_context_get_sample_position(struct pipe_context *pipe,
484 unsigned sample_count, unsigned sample_index,
485 float *xy)
486 {
487 static const uint8_t ms1[1][2] = { { 0x8, 0x8 } };
488 static const uint8_t ms2[2][2] = {
489 { 0x4, 0x4 }, { 0xc, 0xc } }; /* surface coords (0,0), (1,0) */
490 static const uint8_t ms4[4][2] = {
491 { 0x6, 0x2 }, { 0xe, 0x6 }, /* (0,0), (1,0) */
492 { 0x2, 0xa }, { 0xa, 0xe } }; /* (0,1), (1,1) */
493 static const uint8_t ms8[8][2] = {
494 { 0x1, 0x7 }, { 0x5, 0x3 }, /* (0,0), (1,0) */
495 { 0x3, 0xd }, { 0x7, 0xb }, /* (0,1), (1,1) */
496 { 0x9, 0x5 }, { 0xf, 0x1 }, /* (2,0), (3,0) */
497 { 0xb, 0xf }, { 0xd, 0x9 } }; /* (2,1), (3,1) */
498 #if 0
499 /* NOTE: there are alternative modes for MS2 and MS8, currently not used */
500 static const uint8_t ms8_alt[8][2] = {
501 { 0x9, 0x5 }, { 0x7, 0xb }, /* (2,0), (1,1) */
502 { 0xd, 0x9 }, { 0x5, 0x3 }, /* (3,1), (1,0) */
503 { 0x3, 0xd }, { 0x1, 0x7 }, /* (0,1), (0,0) */
504 { 0xb, 0xf }, { 0xf, 0x1 } }; /* (2,1), (3,0) */
505 #endif
506
507 const uint8_t (*ptr)[2];
508
509 switch (sample_count) {
510 case 0:
511 case 1: ptr = ms1; break;
512 case 2: ptr = ms2; break;
513 case 4: ptr = ms4; break;
514 case 8: ptr = ms8; break;
515 default:
516 assert(0);
517 return; /* bad sample count -> undefined locations */
518 }
519 xy[0] = ptr[sample_index][0] * 0.0625f;
520 xy[1] = ptr[sample_index][1] * 0.0625f;
521 }