2 * Copyright 2010 Christoph Bumiller
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:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
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.
23 #include "pipe/p_defines.h"
24 #include "util/u_framebuffer.h"
26 #include "nvc0/nvc0_context.h"
27 #include "nvc0/nvc0_screen.h"
28 #include "nvc0/nvc0_resource.h"
31 nvc0_flush(struct pipe_context
*pipe
,
32 struct pipe_fence_handle
**fence
,
35 struct nvc0_context
*nvc0
= nvc0_context(pipe
);
36 struct nouveau_screen
*screen
= &nvc0
->screen
->base
;
39 nouveau_fence_ref(screen
->fence
.current
, (struct nouveau_fence
**)fence
);
41 PUSH_KICK(nvc0
->base
.pushbuf
); /* fencing handled in kick_notify */
43 nouveau_context_update_frame_stats(&nvc0
->base
);
47 nvc0_texture_barrier(struct pipe_context
*pipe
)
49 struct nouveau_pushbuf
*push
= nvc0_context(pipe
)->base
.pushbuf
;
51 IMMED_NVC0(push
, NVC0_3D(SERIALIZE
), 0);
52 IMMED_NVC0(push
, NVC0_3D(TEX_CACHE_CTL
), 0);
56 nvc0_memory_barrier(struct pipe_context
*pipe
, unsigned flags
)
58 struct nvc0_context
*nvc0
= nvc0_context(pipe
);
59 struct nouveau_pushbuf
*push
= nvc0
->base
.pushbuf
;
62 if (flags
& PIPE_BARRIER_MAPPED_BUFFER
) {
63 for (i
= 0; i
< nvc0
->num_vtxbufs
; ++i
) {
64 if (!nvc0
->vtxbuf
[i
].buffer
)
66 if (nvc0
->vtxbuf
[i
].buffer
->flags
& PIPE_RESOURCE_FLAG_MAP_PERSISTENT
)
67 nvc0
->base
.vbo_dirty
= true;
70 if (nvc0
->idxbuf
.buffer
&&
71 nvc0
->idxbuf
.buffer
->flags
& PIPE_RESOURCE_FLAG_MAP_PERSISTENT
)
72 nvc0
->base
.vbo_dirty
= true;
74 for (s
= 0; s
< 5 && !nvc0
->cb_dirty
; ++s
) {
75 uint32_t valid
= nvc0
->constbuf_valid
[s
];
77 while (valid
&& !nvc0
->cb_dirty
) {
78 const unsigned i
= ffs(valid
) - 1;
79 struct pipe_resource
*res
;
82 if (nvc0
->constbuf
[s
][i
].user
)
85 res
= nvc0
->constbuf
[s
][i
].u
.buf
;
89 if (res
->flags
& PIPE_RESOURCE_FLAG_MAP_PERSISTENT
)
90 nvc0
->cb_dirty
= true;
94 /* Pretty much any writing by shaders needs a serialize after
95 * it. Especially when moving between 3d and compute pipelines, but even
98 IMMED_NVC0(push
, NVC0_3D(SERIALIZE
), 0);
101 /* If we're going to texture from a buffer/image written by a shader, we
102 * must flush the texture cache.
104 if (flags
& PIPE_BARRIER_TEXTURE
)
105 IMMED_NVC0(push
, NVC0_3D(TEX_CACHE_CTL
), 0);
107 if (flags
& PIPE_BARRIER_CONSTANT_BUFFER
)
108 nvc0
->cb_dirty
= true;
109 if (flags
& (PIPE_BARRIER_VERTEX_BUFFER
| PIPE_BARRIER_INDEX_BUFFER
))
110 nvc0
->base
.vbo_dirty
= true;
114 nvc0_emit_string_marker(struct pipe_context
*pipe
, const char *str
, int len
)
116 struct nouveau_pushbuf
*push
= nvc0_context(pipe
)->base
.pushbuf
;
117 int string_words
= len
/ 4;
122 string_words
= MIN2(string_words
, NV04_PFIFO_MAX_PACKET_LEN
);
123 if (string_words
== NV04_PFIFO_MAX_PACKET_LEN
)
124 data_words
= string_words
;
126 data_words
= string_words
+ !!(len
& 3);
127 BEGIN_NIC0(push
, SUBC_3D(NV04_GRAPH_NOP
), data_words
);
129 PUSH_DATAp(push
, str
, string_words
);
130 if (string_words
!= data_words
) {
132 memcpy(&data
, &str
[string_words
* 4], len
& 3);
133 PUSH_DATA (push
, data
);
138 nvc0_context_unreference_resources(struct nvc0_context
*nvc0
)
142 nouveau_bufctx_del(&nvc0
->bufctx_3d
);
143 nouveau_bufctx_del(&nvc0
->bufctx
);
144 nouveau_bufctx_del(&nvc0
->bufctx_cp
);
146 util_unreference_framebuffer_state(&nvc0
->framebuffer
);
148 for (i
= 0; i
< nvc0
->num_vtxbufs
; ++i
)
149 pipe_resource_reference(&nvc0
->vtxbuf
[i
].buffer
, NULL
);
151 pipe_resource_reference(&nvc0
->idxbuf
.buffer
, NULL
);
153 for (s
= 0; s
< 6; ++s
) {
154 for (i
= 0; i
< nvc0
->num_textures
[s
]; ++i
)
155 pipe_sampler_view_reference(&nvc0
->textures
[s
][i
], NULL
);
157 for (i
= 0; i
< NVC0_MAX_PIPE_CONSTBUFS
; ++i
)
158 if (!nvc0
->constbuf
[s
][i
].user
)
159 pipe_resource_reference(&nvc0
->constbuf
[s
][i
].u
.buf
, NULL
);
161 for (i
= 0; i
< NVC0_MAX_BUFFERS
; ++i
)
162 pipe_resource_reference(&nvc0
->buffers
[s
][i
].buffer
, NULL
);
164 for (i
= 0; i
< NVC0_MAX_IMAGES
; ++i
)
165 pipe_resource_reference(&nvc0
->images
[s
][i
].resource
, NULL
);
168 for (s
= 0; s
< 2; ++s
) {
169 for (i
= 0; i
< NVC0_MAX_SURFACE_SLOTS
; ++i
)
170 pipe_surface_reference(&nvc0
->surfaces
[s
][i
], NULL
);
173 for (i
= 0; i
< nvc0
->num_tfbbufs
; ++i
)
174 pipe_so_target_reference(&nvc0
->tfbbuf
[i
], NULL
);
176 for (i
= 0; i
< nvc0
->global_residents
.size
/ sizeof(struct pipe_resource
*);
178 struct pipe_resource
**res
= util_dynarray_element(
179 &nvc0
->global_residents
, struct pipe_resource
*, i
);
180 pipe_resource_reference(res
, NULL
);
182 util_dynarray_fini(&nvc0
->global_residents
);
185 nvc0
->base
.pipe
.delete_tcs_state(&nvc0
->base
.pipe
, nvc0
->tcp_empty
);
189 nvc0_destroy(struct pipe_context
*pipe
)
191 struct nvc0_context
*nvc0
= nvc0_context(pipe
);
193 if (nvc0
->screen
->cur_ctx
== nvc0
) {
194 nvc0
->screen
->cur_ctx
= NULL
;
195 nvc0
->screen
->save_state
= nvc0
->state
;
196 nvc0
->screen
->save_state
.tfb
= NULL
;
199 /* Unset bufctx, we don't want to revalidate any resources after the flush.
200 * Other contexts will always set their bufctx again on action calls.
202 nouveau_pushbuf_bufctx(nvc0
->base
.pushbuf
, NULL
);
203 nouveau_pushbuf_kick(nvc0
->base
.pushbuf
, nvc0
->base
.pushbuf
->channel
);
205 nvc0_context_unreference_resources(nvc0
);
206 nvc0_blitctx_destroy(nvc0
);
208 nouveau_context_destroy(&nvc0
->base
);
212 nvc0_default_kick_notify(struct nouveau_pushbuf
*push
)
214 struct nvc0_screen
*screen
= push
->user_priv
;
217 nouveau_fence_next(&screen
->base
);
218 nouveau_fence_update(&screen
->base
, true);
220 screen
->cur_ctx
->state
.flushed
= true;
221 NOUVEAU_DRV_STAT(&screen
->base
, pushbuf_count
, 1);
226 nvc0_invalidate_resource_storage(struct nouveau_context
*ctx
,
227 struct pipe_resource
*res
,
230 struct nvc0_context
*nvc0
= nvc0_context(&ctx
->pipe
);
233 if (res
->bind
& PIPE_BIND_RENDER_TARGET
) {
234 for (i
= 0; i
< nvc0
->framebuffer
.nr_cbufs
; ++i
) {
235 if (nvc0
->framebuffer
.cbufs
[i
] &&
236 nvc0
->framebuffer
.cbufs
[i
]->texture
== res
) {
237 nvc0
->dirty_3d
|= NVC0_NEW_3D_FRAMEBUFFER
;
238 nouveau_bufctx_reset(nvc0
->bufctx_3d
, NVC0_BIND_3D_FB
);
244 if (res
->bind
& PIPE_BIND_DEPTH_STENCIL
) {
245 if (nvc0
->framebuffer
.zsbuf
&&
246 nvc0
->framebuffer
.zsbuf
->texture
== res
) {
247 nvc0
->dirty_3d
|= NVC0_NEW_3D_FRAMEBUFFER
;
248 nouveau_bufctx_reset(nvc0
->bufctx_3d
, NVC0_BIND_3D_FB
);
254 if (res
->target
== PIPE_BUFFER
) {
255 for (i
= 0; i
< nvc0
->num_vtxbufs
; ++i
) {
256 if (nvc0
->vtxbuf
[i
].buffer
== res
) {
257 nvc0
->dirty_3d
|= NVC0_NEW_3D_ARRAYS
;
258 nouveau_bufctx_reset(nvc0
->bufctx_3d
, NVC0_BIND_3D_VTX
);
264 if (nvc0
->idxbuf
.buffer
== res
) {
265 nvc0
->dirty_3d
|= NVC0_NEW_3D_IDXBUF
;
266 nouveau_bufctx_reset(nvc0
->bufctx_3d
, NVC0_BIND_3D_IDX
);
271 for (s
= 0; s
< 6; ++s
) {
272 for (i
= 0; i
< nvc0
->num_textures
[s
]; ++i
) {
273 if (nvc0
->textures
[s
][i
] &&
274 nvc0
->textures
[s
][i
]->texture
== res
) {
275 nvc0
->textures_dirty
[s
] |= 1 << i
;
276 if (unlikely(s
== 5)) {
277 nvc0
->dirty_cp
|= NVC0_NEW_CP_TEXTURES
;
278 nouveau_bufctx_reset(nvc0
->bufctx_cp
, NVC0_BIND_CP_TEX(i
));
280 nvc0
->dirty_3d
|= NVC0_NEW_3D_TEXTURES
;
281 nouveau_bufctx_reset(nvc0
->bufctx_3d
, NVC0_BIND_3D_TEX(s
, i
));
289 for (s
= 0; s
< 6; ++s
) {
290 for (i
= 0; i
< NVC0_MAX_PIPE_CONSTBUFS
; ++i
) {
291 if (!(nvc0
->constbuf_valid
[s
] & (1 << i
)))
293 if (!nvc0
->constbuf
[s
][i
].user
&&
294 nvc0
->constbuf
[s
][i
].u
.buf
== res
) {
295 nvc0
->constbuf_dirty
[s
] |= 1 << i
;
296 if (unlikely(s
== 5)) {
297 nvc0
->dirty_cp
|= NVC0_NEW_CP_CONSTBUF
;
298 nouveau_bufctx_reset(nvc0
->bufctx_cp
, NVC0_BIND_CP_CB(i
));
300 nvc0
->dirty_3d
|= NVC0_NEW_3D_CONSTBUF
;
301 nouveau_bufctx_reset(nvc0
->bufctx_3d
, NVC0_BIND_3D_CB(s
, i
));
309 for (s
= 0; s
< 6; ++s
) {
310 for (i
= 0; i
< NVC0_MAX_BUFFERS
; ++i
) {
311 if (nvc0
->buffers
[s
][i
].buffer
== res
) {
312 nvc0
->buffers_dirty
[s
] |= 1 << i
;
313 if (unlikely(s
== 5)) {
314 nvc0
->dirty_cp
|= NVC0_NEW_CP_BUFFERS
;
315 nouveau_bufctx_reset(nvc0
->bufctx_cp
, NVC0_BIND_CP_BUF
);
317 nvc0
->dirty_3d
|= NVC0_NEW_3D_BUFFERS
;
318 nouveau_bufctx_reset(nvc0
->bufctx_3d
, NVC0_BIND_3D_BUF
);
326 for (s
= 0; s
< 6; ++s
) {
327 for (i
= 0; i
< NVC0_MAX_IMAGES
; ++i
) {
328 if (nvc0
->images
[s
][i
].resource
== res
) {
329 nvc0
->images_dirty
[s
] |= 1 << i
;
330 if (unlikely(s
== 5)) {
331 nvc0
->dirty_cp
|= NVC0_NEW_CP_SURFACES
;
332 nouveau_bufctx_reset(nvc0
->bufctx_cp
, NVC0_BIND_CP_SUF
);
334 nvc0
->dirty_3d
|= NVC0_NEW_3D_SURFACES
;
335 nouveau_bufctx_reset(nvc0
->bufctx_3d
, NVC0_BIND_3D_SUF
);
348 nvc0_context_get_sample_position(struct pipe_context
*, unsigned, unsigned,
351 struct pipe_context
*
352 nvc0_create(struct pipe_screen
*pscreen
, void *priv
, unsigned ctxflags
)
354 struct nvc0_screen
*screen
= nvc0_screen(pscreen
);
355 struct nvc0_context
*nvc0
;
356 struct pipe_context
*pipe
;
360 nvc0
= CALLOC_STRUCT(nvc0_context
);
363 pipe
= &nvc0
->base
.pipe
;
365 if (!nvc0_blitctx_create(nvc0
))
368 nvc0
->base
.pushbuf
= screen
->base
.pushbuf
;
369 nvc0
->base
.client
= screen
->base
.client
;
371 ret
= nouveau_bufctx_new(screen
->base
.client
, 2, &nvc0
->bufctx
);
373 ret
= nouveau_bufctx_new(screen
->base
.client
, NVC0_BIND_3D_COUNT
,
376 ret
= nouveau_bufctx_new(screen
->base
.client
, NVC0_BIND_CP_COUNT
,
381 nvc0
->screen
= screen
;
382 nvc0
->base
.screen
= &screen
->base
;
384 pipe
->screen
= pscreen
;
387 pipe
->destroy
= nvc0_destroy
;
389 pipe
->draw_vbo
= nvc0_draw_vbo
;
390 pipe
->clear
= nvc0_clear
;
391 pipe
->launch_grid
= (nvc0
->screen
->base
.class_3d
>= NVE4_3D_CLASS
) ?
392 nve4_launch_grid
: nvc0_launch_grid
;
394 pipe
->flush
= nvc0_flush
;
395 pipe
->texture_barrier
= nvc0_texture_barrier
;
396 pipe
->memory_barrier
= nvc0_memory_barrier
;
397 pipe
->get_sample_position
= nvc0_context_get_sample_position
;
398 pipe
->emit_string_marker
= nvc0_emit_string_marker
;
400 nouveau_context_init(&nvc0
->base
);
401 nvc0_init_query_functions(nvc0
);
402 nvc0_init_surface_functions(nvc0
);
403 nvc0_init_state_functions(nvc0
);
404 nvc0_init_transfer_functions(nvc0
);
405 nvc0_init_resource_functions(pipe
);
407 nvc0
->base
.invalidate_resource_storage
= nvc0_invalidate_resource_storage
;
409 pipe
->create_video_codec
= nvc0_create_decoder
;
410 pipe
->create_video_buffer
= nvc0_video_buffer_create
;
412 /* shader builtin library is per-screen, but we need a context for m2mf */
413 nvc0_program_library_upload(nvc0
);
414 nvc0_program_init_tcp_empty(nvc0
);
415 if (!nvc0
->tcp_empty
)
417 /* set the empty tctl prog on next draw in case one is never set */
418 nvc0
->dirty_3d
|= NVC0_NEW_3D_TCTLPROG
;
420 /* Do not bind the COMPUTE driver constbuf at screen initialization because
421 * CBs are aliased between 3D and COMPUTE, but make sure it will be bound if
422 * a grid is launched later. */
423 nvc0
->dirty_cp
|= NVC0_NEW_CP_DRIVERCONST
;
425 /* now that there are no more opportunities for errors, set the current
426 * context if there isn't already one.
428 if (!screen
->cur_ctx
) {
429 nvc0
->state
= screen
->save_state
;
430 screen
->cur_ctx
= nvc0
;
431 nouveau_pushbuf_bufctx(screen
->base
.pushbuf
, nvc0
->bufctx
);
433 screen
->base
.pushbuf
->kick_notify
= nvc0_default_kick_notify
;
435 /* add permanently resident buffers to bufctxts */
437 flags
= NV_VRAM_DOMAIN(&screen
->base
) | NOUVEAU_BO_RD
;
439 BCTX_REFN_bo(nvc0
->bufctx_3d
, 3D_SCREEN
, flags
, screen
->text
);
440 BCTX_REFN_bo(nvc0
->bufctx_3d
, 3D_SCREEN
, flags
, screen
->uniform_bo
);
441 BCTX_REFN_bo(nvc0
->bufctx_3d
, 3D_SCREEN
, flags
, screen
->txc
);
442 if (screen
->compute
) {
443 BCTX_REFN_bo(nvc0
->bufctx_cp
, CP_SCREEN
, flags
, screen
->text
);
444 BCTX_REFN_bo(nvc0
->bufctx_cp
, CP_SCREEN
, flags
, screen
->uniform_bo
);
445 BCTX_REFN_bo(nvc0
->bufctx_cp
, CP_SCREEN
, flags
, screen
->txc
);
448 flags
= NV_VRAM_DOMAIN(&screen
->base
) | NOUVEAU_BO_RDWR
;
450 if (screen
->poly_cache
)
451 BCTX_REFN_bo(nvc0
->bufctx_3d
, 3D_SCREEN
, flags
, screen
->poly_cache
);
453 BCTX_REFN_bo(nvc0
->bufctx_cp
, CP_SCREEN
, flags
, screen
->tls
);
455 flags
= NOUVEAU_BO_GART
| NOUVEAU_BO_WR
;
457 BCTX_REFN_bo(nvc0
->bufctx_3d
, 3D_SCREEN
, flags
, screen
->fence
.bo
);
458 BCTX_REFN_bo(nvc0
->bufctx
, FENCE
, flags
, screen
->fence
.bo
);
460 BCTX_REFN_bo(nvc0
->bufctx_cp
, CP_SCREEN
, flags
, screen
->fence
.bo
);
462 nvc0
->base
.scratch
.bo_size
= 2 << 20;
464 memset(nvc0
->tex_handles
, ~0, sizeof(nvc0
->tex_handles
));
466 util_dynarray_init(&nvc0
->global_residents
);
473 nouveau_bufctx_del(&nvc0
->bufctx_3d
);
475 nouveau_bufctx_del(&nvc0
->bufctx_cp
);
477 nouveau_bufctx_del(&nvc0
->bufctx
);
485 nvc0_bufctx_fence(struct nvc0_context
*nvc0
, struct nouveau_bufctx
*bufctx
,
488 struct nouveau_list
*list
= on_flush
? &bufctx
->current
: &bufctx
->pending
;
489 struct nouveau_list
*it
;
490 NOUVEAU_DRV_STAT_IFD(unsigned count
= 0);
492 for (it
= list
->next
; it
!= list
; it
= it
->next
) {
493 struct nouveau_bufref
*ref
= (struct nouveau_bufref
*)it
;
494 struct nv04_resource
*res
= ref
->priv
;
496 nvc0_resource_validate(res
, (unsigned)ref
->priv_data
);
497 NOUVEAU_DRV_STAT_IFD(count
++);
499 NOUVEAU_DRV_STAT(&nvc0
->screen
->base
, resource_validate_count
, count
);
503 nvc0_context_get_sample_position(struct pipe_context
*pipe
,
504 unsigned sample_count
, unsigned sample_index
,
507 static const uint8_t ms1
[1][2] = { { 0x8, 0x8 } };
508 static const uint8_t ms2
[2][2] = {
509 { 0x4, 0x4 }, { 0xc, 0xc } }; /* surface coords (0,0), (1,0) */
510 static const uint8_t ms4
[4][2] = {
511 { 0x6, 0x2 }, { 0xe, 0x6 }, /* (0,0), (1,0) */
512 { 0x2, 0xa }, { 0xa, 0xe } }; /* (0,1), (1,1) */
513 static const uint8_t ms8
[8][2] = {
514 { 0x1, 0x7 }, { 0x5, 0x3 }, /* (0,0), (1,0) */
515 { 0x3, 0xd }, { 0x7, 0xb }, /* (0,1), (1,1) */
516 { 0x9, 0x5 }, { 0xf, 0x1 }, /* (2,0), (3,0) */
517 { 0xb, 0xf }, { 0xd, 0x9 } }; /* (2,1), (3,1) */
519 /* NOTE: there are alternative modes for MS2 and MS8, currently not used */
520 static const uint8_t ms8_alt
[8][2] = {
521 { 0x9, 0x5 }, { 0x7, 0xb }, /* (2,0), (1,1) */
522 { 0xd, 0x9 }, { 0x5, 0x3 }, /* (3,1), (1,0) */
523 { 0x3, 0xd }, { 0x1, 0x7 }, /* (0,1), (0,0) */
524 { 0xb, 0xf }, { 0xf, 0x1 } }; /* (2,1), (3,0) */
527 const uint8_t (*ptr
)[2];
529 switch (sample_count
) {
531 case 1: ptr
= ms1
; break;
532 case 2: ptr
= ms2
; break;
533 case 4: ptr
= ms4
; break;
534 case 8: ptr
= ms8
; break;
537 return; /* bad sample count -> undefined locations */
539 xy
[0] = ptr
[sample_index
][0] * 0.0625f
;
540 xy
[1] = ptr
[sample_index
][1] * 0.0625f
;