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