Debian Codegraph @

src/gallium/drivers/nouveau/nv50/nv50_context.c

raw
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 #include "util/u_upload_mgr.h"
26
27 #include "nv50/nv50_context.h"
28 #include "nv50/nv50_screen.h"
29 #include "nv50/nv50_resource.h"
30
31 static void
32 nv50_flush(struct pipe_context *pipe,
33 struct pipe_fence_handle **fence,
34 unsigned flags)
35 {
36 struct nouveau_context *context = nouveau_context(pipe);
37
38 if (fence)
39 nouveau_fence_ref(context->fence, (struct nouveau_fence **)fence);
40
41 PUSH_KICK(context->pushbuf);
42
43 nouveau_context_update_frame_stats(nouveau_context(pipe));
44 }
45
46 static void
47 nv50_texture_barrier(struct pipe_context *pipe, unsigned flags)
48 {
49 struct nouveau_pushbuf *push = nv50_context(pipe)->base.pushbuf;
50
51 BEGIN_NV04(push, SUBC_3D(NV50_GRAPH_SERIALIZE), 1);
52 PUSH_DATA (push, 0);
53 BEGIN_NV04(push, NV50_3D(TEX_CACHE_CTL), 1);
54 PUSH_DATA (push, 0x20);
55 }
56
57 static void
58 nv50_memory_barrier(struct pipe_context *pipe, unsigned flags)
59 {
60 struct nv50_context *nv50 = nv50_context(pipe);
61 struct nouveau_pushbuf *push = nv50->base.pushbuf;
62 int i, s;
63
64 if (flags & PIPE_BARRIER_MAPPED_BUFFER) {
65 for (i = 0; i < nv50->num_vtxbufs; ++i) {
66 if (!nv50->vtxbuf[i].buffer.resource && !nv50->vtxbuf[i].is_user_buffer)
67 continue;
68 if (nv50->vtxbuf[i].buffer.resource->flags & PIPE_RESOURCE_FLAG_MAP_PERSISTENT)
69 nv50->base.vbo_dirty = true;
70 }
71
72 for (s = 0; s < NV50_MAX_3D_SHADER_STAGES && !nv50->cb_dirty; ++s) {
73 uint32_t valid = nv50->constbuf_valid[s];
74
75 while (valid && !nv50->cb_dirty) {
76 const unsigned i = ffs(valid) - 1;
77 struct pipe_resource *res;
78
79 valid &= ~(1 << i);
80 if (nv50->constbuf[s][i].user)
81 continue;
82
83 res = nv50->constbuf[s][i].u.buf;
84 if (!res)
85 continue;
86
87 if (res->flags & PIPE_RESOURCE_FLAG_MAP_PERSISTENT)
88 nv50->cb_dirty = true;
89 }
90 }
91 } else {
92 BEGIN_NV04(push, SUBC_3D(NV50_GRAPH_SERIALIZE), 1);
93 PUSH_DATA (push, 0);
94 }
95
96 /* If we're going to texture from a buffer/image written by a shader, we
97 * must flush the texture cache.
98 */
99 if (flags & PIPE_BARRIER_TEXTURE) {
100 BEGIN_NV04(push, NV50_3D(TEX_CACHE_CTL), 1);
101 PUSH_DATA (push, 0x20);
102 }
103
104 if (flags & PIPE_BARRIER_CONSTANT_BUFFER)
105 nv50->cb_dirty = true;
106 if (flags & (PIPE_BARRIER_VERTEX_BUFFER | PIPE_BARRIER_INDEX_BUFFER))
107 nv50->base.vbo_dirty = true;
108 }
109
110 static void
111 nv50_emit_string_marker(struct pipe_context *pipe, const char *str, int len)
112 {
113 struct nouveau_pushbuf *push = nv50_context(pipe)->base.pushbuf;
114 int string_words = len / 4;
115 int data_words;
116
117 if (len <= 0)
118 return;
119 string_words = MIN2(string_words, NV04_PFIFO_MAX_PACKET_LEN);
120 if (string_words == NV04_PFIFO_MAX_PACKET_LEN)
121 data_words = string_words;
122 else
123 data_words = string_words + !!(len & 3);
124 BEGIN_NI04(push, SUBC_3D(NV04_GRAPH_NOP), data_words);
125 if (string_words)
126 PUSH_DATAp(push, str, string_words);
127 if (string_words != data_words) {
128 int data = 0;
129 memcpy(&data, &str[string_words * 4], len & 3);
130 PUSH_DATA (push, data);
131 }
132 }
133
134 void
135 nv50_default_kick_notify(struct nouveau_context *context)
136 {
137 struct nv50_context *nv50 = nv50_context(&context->pipe);
138
139 _nouveau_fence_next(context);
140 _nouveau_fence_update(context->screen, true);
141 nv50->state.flushed = true;
142 }
143
144 static void
145 nv50_context_unreference_resources(struct nv50_context *nv50)
146 {
147 unsigned s, i;
148
149 nouveau_bufctx_del(&nv50->bufctx_3d);
150 nouveau_bufctx_del(&nv50->bufctx);
151 nouveau_bufctx_del(&nv50->bufctx_cp);
152
153 nv50_framebuffer_init(&nv50->base.pipe, NULL, nv50->fb_cbufs, &nv50->fb_zsbuf);
154 util_unreference_framebuffer_state(&nv50->framebuffer);
155
156 assert(nv50->num_vtxbufs <= PIPE_MAX_ATTRIBS);
157 for (i = 0; i < nv50->num_vtxbufs; ++i)
158 pipe_vertex_buffer_unreference(&nv50->vtxbuf[i]);
159
160 for (s = 0; s < NV50_MAX_SHADER_STAGES; ++s) {
161 assert(nv50->num_textures[s] <= PIPE_MAX_SAMPLERS);
162 for (i = 0; i < nv50->num_textures[s]; ++i)
163 pipe_sampler_view_reference(&nv50->textures[s][i], NULL);
164
165 for (i = 0; i < NV50_MAX_PIPE_CONSTBUFS; ++i)
166 if (!nv50->constbuf[s][i].user)
167 pipe_resource_reference(&nv50->constbuf[s][i].u.buf, NULL);
168 }
169
170 for (i = 0; i < nv50->global_residents.size / sizeof(struct pipe_resource *);
171 ++i) {
172 struct pipe_resource **res = util_dynarray_element(
173 &nv50->global_residents, struct pipe_resource *, i);
174 pipe_resource_reference(res, NULL);
175 }
176 util_dynarray_fini(&nv50->global_residents);
177 }
178
179 static void
180 nv50_destroy(struct pipe_context *pipe)
181 {
182 struct nv50_context *nv50 = nv50_context(pipe);
183
184 simple_mtx_lock(&nv50->screen->state_lock);
185 if (nv50->screen->cur_ctx == nv50) {
186 nv50->screen->cur_ctx = NULL;
187 /* Save off the state in case another context gets created */
188 nv50->screen->save_state = nv50->state;
189 }
190 simple_mtx_unlock(&nv50->screen->state_lock);
191
192 if (nv50->base.pipe.stream_uploader)
193 u_upload_destroy(nv50->base.pipe.stream_uploader);
194
195 nouveau_pushbuf_bufctx(nv50->base.pushbuf, NULL);
196 PUSH_KICK(nv50->base.pushbuf);
197
198 nv50_context_unreference_resources(nv50);
199
200 FREE(nv50->blit);
201
202 nouveau_fence_cleanup(&nv50->base);
203 nouveau_context_destroy(&nv50->base);
204 }
205
206 static int
207 nv50_invalidate_resource_storage(struct nouveau_context *ctx,
208 struct pipe_resource *res,
209 int ref)
210 {
211 struct nv50_context *nv50 = nv50_context(&ctx->pipe);
212 unsigned bind = res->bind ? res->bind : PIPE_BIND_VERTEX_BUFFER;
213 unsigned s, i;
214
215 if (bind & PIPE_BIND_RENDER_TARGET) {
216 assert(nv50->framebuffer.nr_cbufs <= PIPE_MAX_COLOR_BUFS);
217 for (i = 0; i < nv50->framebuffer.nr_cbufs; ++i) {
218 if (nv50->framebuffer.cbufs[i].texture == res) {
219 nv50->dirty_3d |= NV50_NEW_3D_FRAMEBUFFER;
220 nouveau_bufctx_reset(nv50->bufctx_3d, NV50_BIND_3D_FB);
221 if (!--ref)
222 return ref;
223 }
224 }
225 }
226 if (bind & PIPE_BIND_DEPTH_STENCIL) {
227 if (nv50->framebuffer.zsbuf.texture == res) {
228 nv50->dirty_3d |= NV50_NEW_3D_FRAMEBUFFER;
229 nouveau_bufctx_reset(nv50->bufctx_3d, NV50_BIND_3D_FB);
230 if (!--ref)
231 return ref;
232 }
233 }
234
235 if (bind & (PIPE_BIND_VERTEX_BUFFER |
236 PIPE_BIND_INDEX_BUFFER |
237 PIPE_BIND_CONSTANT_BUFFER |
238 PIPE_BIND_STREAM_OUTPUT |
239 PIPE_BIND_SAMPLER_VIEW)) {
240
241 assert(nv50->num_vtxbufs <= PIPE_MAX_ATTRIBS);
242 for (i = 0; i < nv50->num_vtxbufs; ++i) {
243 if (nv50->vtxbuf[i].buffer.resource == res) {
244 nv50->dirty_3d |= NV50_NEW_3D_ARRAYS;
245 nouveau_bufctx_reset(nv50->bufctx_3d, NV50_BIND_3D_VERTEX);
246 if (!--ref)
247 return ref;
248 }
249 }
250
251 for (s = 0; s < NV50_MAX_SHADER_STAGES; ++s) {
252 assert(nv50->num_textures[s] <= PIPE_MAX_SAMPLERS);
253 for (i = 0; i < nv50->num_textures[s]; ++i) {
254 if (nv50->textures[s][i] &&
255 nv50->textures[s][i]->texture == res) {
256 if (unlikely(s == NV50_SHADER_STAGE_COMPUTE)) {
257 nv50->dirty_cp |= NV50_NEW_CP_TEXTURES;
258 nouveau_bufctx_reset(nv50->bufctx_cp, NV50_BIND_CP_TEXTURES);
259 } else {
260 nv50->dirty_3d |= NV50_NEW_3D_TEXTURES;
261 nouveau_bufctx_reset(nv50->bufctx_3d, NV50_BIND_3D_TEXTURES);
262 }
263 if (!--ref)
264 return ref;
265 }
266 }
267 }
268
269 for (s = 0; s < NV50_MAX_SHADER_STAGES; ++s) {
270 for (i = 0; i < NV50_MAX_PIPE_CONSTBUFS; ++i) {
271 if (!(nv50->constbuf_valid[s] & (1 << i)))
272 continue;
273 if (!nv50->constbuf[s][i].user &&
274 nv50->constbuf[s][i].u.buf == res) {
275 nv50->constbuf_dirty[s] |= 1 << i;
276 if (unlikely(s == NV50_SHADER_STAGE_COMPUTE)) {
277 nv50->dirty_cp |= NV50_NEW_CP_CONSTBUF;
278 nouveau_bufctx_reset(nv50->bufctx_cp, NV50_BIND_CP_CB(i));
279 } else {
280 nv50->dirty_3d |= NV50_NEW_3D_CONSTBUF;
281 nouveau_bufctx_reset(nv50->bufctx_3d, NV50_BIND_3D_CB(s, i));
282 }
283 if (!--ref)
284 return ref;
285 }
286 }
287 }
288 }
289
290 return ref;
291 }
292
293 static void
294 nv50_context_get_sample_position(struct pipe_context *, unsigned, unsigned,
295 float *);
296
297 struct pipe_context *
298 nv50_create(struct pipe_screen *pscreen, void *priv, unsigned ctxflags)
299 {
300 struct nv50_screen *screen = nv50_screen(pscreen);
301 struct nv50_context *nv50;
302 struct pipe_context *pipe;
303 int ret;
304 uint32_t flags;
305
306 nv50 = CALLOC_STRUCT(nv50_context);
307 if (!nv50)
308 return NULL;
309 pipe = &nv50->base.pipe;
310
311 if (!nv50_blitctx_create(nv50))
312 goto out_err;
313
314 if (nouveau_context_init(&nv50->base, &screen->base))
315 goto out_err;
316
317 ret = nouveau_bufctx_new(nv50->base.client, 2, &nv50->bufctx);
318 if (!ret)
319 ret = nouveau_bufctx_new(nv50->base.client, NV50_BIND_3D_COUNT,
320 &nv50->bufctx_3d);
321 if (!ret)
322 ret = nouveau_bufctx_new(nv50->base.client, NV50_BIND_CP_COUNT,
323 &nv50->bufctx_cp);
324 if (ret)
325 goto out_err;
326
327 nv50->base.copy_data = nv50_m2mf_copy_linear;
328 nv50->base.push_data = nv50_sifc_linear_u8;
329 nv50->base.push_cb = nv50_cb_push;
330
331 nv50->screen = screen;
332 pipe->screen = pscreen;
333 pipe->priv = priv;
334 pipe->stream_uploader = u_upload_create_default(pipe);
335 if (!pipe->stream_uploader)
336 goto out_err;
337 pipe->const_uploader = pipe->stream_uploader;
338
339 pipe->destroy = nv50_destroy;
340
341 pipe->draw_vbo = nv50_draw_vbo;
342 pipe->clear = nv50_clear;
343 pipe->launch_grid = nv50_launch_grid;
344
345 pipe->flush = nv50_flush;
346 pipe->texture_barrier = nv50_texture_barrier;
347 pipe->memory_barrier = nv50_memory_barrier;
348 pipe->get_sample_position = nv50_context_get_sample_position;
349 pipe->emit_string_marker = nv50_emit_string_marker;
350
351 simple_mtx_lock(&screen->state_lock);
352 if (!screen->cur_ctx) {
353 /* Restore the last context's state here, normally handled during
354 * context switch
355 */
356 nv50->state = screen->save_state;
357 screen->cur_ctx = nv50;
358 }
359 simple_mtx_unlock(&screen->state_lock);
360
361 nouveau_pushbuf_bufctx(nv50->base.pushbuf, nv50->bufctx);
362 nv50->base.kick_notify = nv50_default_kick_notify;
363 nv50->base.pushbuf->rsvd_kick = 5;
364 PUSH_SPACE(nv50->base.pushbuf, 8);
365
366 nv50_init_query_functions(nv50);
367 nv50_init_surface_functions(nv50);
368 nv50_init_state_functions(nv50);
369 nv50_init_resource_functions(pipe);
370
371 nv50->base.invalidate_resource_storage = nv50_invalidate_resource_storage;
372
373 if (screen->base.device->chipset < 0x84 ||
374 debug_get_bool_option("NOUVEAU_PMPEG", false)) {
375 /* PMPEG */
376 nouveau_context_init_vdec(&nv50->base);
377 } else if (screen->base.device->chipset < 0x98 ||
378 screen->base.device->chipset == 0xa0) {
379 /* VP2 */
380 pipe->create_video_codec = nv84_create_decoder;
381 pipe->create_video_buffer = nv84_video_buffer_create;
382 } else {
383 /* VP3/4 */
384 pipe->create_video_codec = nv98_create_decoder;
385 pipe->create_video_buffer = nv98_video_buffer_create;
386 }
387
388 flags = NOUVEAU_BO_VRAM | NOUVEAU_BO_RD;
389
390 BCTX_REFN_bo(nv50->bufctx_3d, 3D_SCREEN, flags, screen->code);
391 BCTX_REFN_bo(nv50->bufctx_3d, 3D_SCREEN, flags, screen->uniforms);
392 BCTX_REFN_bo(nv50->bufctx_3d, 3D_SCREEN, flags, screen->txc);
393 BCTX_REFN_bo(nv50->bufctx_3d, 3D_SCREEN, flags, screen->stack_bo);
394 if (screen->compute) {
395 BCTX_REFN_bo(nv50->bufctx_cp, CP_SCREEN, flags, screen->code);
396 BCTX_REFN_bo(nv50->bufctx_cp, CP_SCREEN, flags, screen->uniforms);
397 BCTX_REFN_bo(nv50->bufctx_cp, CP_SCREEN, flags, screen->txc);
398 BCTX_REFN_bo(nv50->bufctx_cp, CP_SCREEN, flags, screen->stack_bo);
399 }
400
401 flags = NOUVEAU_BO_GART | NOUVEAU_BO_WR;
402
403 BCTX_REFN_bo(nv50->bufctx_3d, 3D_SCREEN, flags, screen->fence.bo);
404 BCTX_REFN_bo(nv50->bufctx, FENCE, flags, screen->fence.bo);
405 if (screen->compute)
406 BCTX_REFN_bo(nv50->bufctx_cp, CP_SCREEN, flags, screen->fence.bo);
407
408 nv50->base.scratch.bo_size = 2 << 20;
409
410 nv50->global_residents = UTIL_DYNARRAY_INIT;
411
412 // Make sure that the first TSC entry has SRGB conversion bit set, since we
413 // use it as a fallback.
414 if (!screen->tsc.entries[0])
415 nv50_upload_tsc0(nv50);
416
417 // And mark samplers as dirty so that the first slot would get bound to the
418 // zero entry if it's not otherwise set.
419 nv50->dirty_3d |= NV50_NEW_3D_SAMPLERS;
420
421 nouveau_fence_new(&nv50->base, &nv50->base.fence);
422
423 return pipe;
424
425 out_err:
426 if (pipe->stream_uploader)
427 u_upload_destroy(pipe->stream_uploader);
428 if (nv50->bufctx_3d)
429 nouveau_bufctx_del(&nv50->bufctx_3d);
430 if (nv50->bufctx_cp)
431 nouveau_bufctx_del(&nv50->bufctx_cp);
432 if (nv50->bufctx)
433 nouveau_bufctx_del(&nv50->bufctx);
434 FREE(nv50->blit);
435 FREE(nv50);
436 return NULL;
437 }
438
439 void
440 nv50_bufctx_fence(struct nv50_context *nv50, struct nouveau_bufctx *bufctx, bool on_flush)
441 {
442 struct list_head *list = on_flush ? &bufctx->current : &bufctx->pending;
443 struct list_head *it;
444
445 for (it = list->next; it != list; it = it->next) {
446 struct nouveau_bufref *ref = (struct nouveau_bufref *)it;
447 struct nv04_resource *res = ref->priv;
448 if (res)
449 nv50_resource_validate(nv50, res, (unsigned)ref->priv_data);
450 }
451 }
452
453 static void
454 nv50_context_get_sample_position(struct pipe_context *pipe,
455 unsigned sample_count, unsigned sample_index,
456 float *xy)
457 {
458 static const uint8_t ms1[1][2] = { { 0x8, 0x8 } };
459 static const uint8_t ms2[2][2] = {
460 { 0x4, 0x4 }, { 0xc, 0xc } }; /* surface coords (0,0), (1,0) */
461 static const uint8_t ms4[4][2] = {
462 { 0x6, 0x2 }, { 0xe, 0x6 }, /* (0,0), (1,0) */
463 { 0x2, 0xa }, { 0xa, 0xe } }; /* (0,1), (1,1) */
464 static const uint8_t ms8[8][2] = {
465 { 0x1, 0x7 }, { 0x5, 0x3 }, /* (0,0), (1,0) */
466 { 0x3, 0xd }, { 0x7, 0xb }, /* (0,1), (1,1) */
467 { 0x9, 0x5 }, { 0xf, 0x1 }, /* (2,0), (3,0) */
468 { 0xb, 0xf }, { 0xd, 0x9 } }; /* (2,1), (3,1) */
469
470 const uint8_t (*ptr)[2];
471
472 switch (sample_count) {
473 case 0:
474 case 1: ptr = ms1; break;
475 case 2: ptr = ms2; break;
476 case 4: ptr = ms4; break;
477 case 8: ptr = ms8; break;
478 default:
479 assert(0);
480 return; /* bad sample count -> undefined locations */
481 }
482 xy[0] = ptr[sample_index][0] * 0.0625f;
483 xy[1] = ptr[sample_index][1] * 0.0625f;
484 }