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"
25 #include "util/u_upload_mgr.h"
27 #include "nvc0/nvc0_context.h"
28 #include "nvc0/nvc0_screen.h"
29 #include "nvc0/nvc0_resource.h"
32 nvc0_flush(struct pipe_context
*pipe
,
33 struct pipe_fence_handle
**fence
,
36 struct nvc0_context
*nvc0
= nvc0_context(pipe
);
37 struct nouveau_screen
*screen
= &nvc0
->screen
->base
;
40 nouveau_fence_ref(screen
->fence
.current
, (struct nouveau_fence
**)fence
);
42 PUSH_KICK(nvc0
->base
.pushbuf
); /* fencing handled in kick_notify */
44 nouveau_context_update_frame_stats(&nvc0
->base
);
48 nvc0_texture_barrier(struct pipe_context
*pipe
, unsigned flags
)
50 struct nouveau_pushbuf
*push
= nvc0_context(pipe
)->base
.pushbuf
;
52 IMMED_NVC0(push
, NVC0_3D(SERIALIZE
), 0);
53 IMMED_NVC0(push
, NVC0_3D(TEX_CACHE_CTL
), 0);
57 nvc0_memory_barrier(struct pipe_context
*pipe
, unsigned flags
)
59 struct nvc0_context
*nvc0
= nvc0_context(pipe
);
60 struct nouveau_pushbuf
*push
= nvc0
->base
.pushbuf
;
63 if (flags
& PIPE_BARRIER_MAPPED_BUFFER
) {
64 for (i
= 0; i
< nvc0
->num_vtxbufs
; ++i
) {
65 if (!nvc0
->vtxbuf
[i
].buffer
.resource
&& !nvc0
->vtxbuf
[i
].is_user_buffer
)
67 if (nvc0
->vtxbuf
[i
].buffer
.resource
->flags
& PIPE_RESOURCE_FLAG_MAP_PERSISTENT
)
68 nvc0
->base
.vbo_dirty
= true;
71 for (s
= 0; s
< 5 && !nvc0
->cb_dirty
; ++s
) {
72 uint32_t valid
= nvc0
->constbuf_valid
[s
];
74 while (valid
&& !nvc0
->cb_dirty
) {
75 const unsigned i
= ffs(valid
) - 1;
76 struct pipe_resource
*res
;
79 if (nvc0
->constbuf
[s
][i
].user
)
82 res
= nvc0
->constbuf
[s
][i
].u
.buf
;
86 if (res
->flags
& PIPE_RESOURCE_FLAG_MAP_PERSISTENT
)
87 nvc0
->cb_dirty
= true;
91 /* Pretty much any writing by shaders needs a serialize after
92 * it. Especially when moving between 3d and compute pipelines, but even
95 IMMED_NVC0(push
, NVC0_3D(SERIALIZE
), 0);
98 /* If we're going to texture from a buffer/image written by a shader, we
99 * must flush the texture cache.
101 if (flags
& PIPE_BARRIER_TEXTURE
)
102 IMMED_NVC0(push
, NVC0_3D(TEX_CACHE_CTL
), 0);
104 if (flags
& PIPE_BARRIER_CONSTANT_BUFFER
)
105 nvc0
->cb_dirty
= true;
106 if (flags
& (PIPE_BARRIER_VERTEX_BUFFER
| PIPE_BARRIER_INDEX_BUFFER
))
107 nvc0
->base
.vbo_dirty
= true;
111 nvc0_emit_string_marker(struct pipe_context
*pipe
, const char *str
, int len
)
113 struct nouveau_pushbuf
*push
= nvc0_context(pipe
)->base
.pushbuf
;
114 int string_words
= len
/ 4;
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
;
123 data_words
= string_words
+ !!(len
& 3);
124 BEGIN_NIC0(push
, SUBC_3D(NV04_GRAPH_NOP
), data_words
);
126 PUSH_DATAp(push
, str
, string_words
);
127 if (string_words
!= data_words
) {
129 memcpy(&data
, &str
[string_words
* 4], len
& 3);
130 PUSH_DATA (push
, data
);
135 nvc0_context_unreference_resources(struct nvc0_context
*nvc0
)
139 nouveau_bufctx_del(&nvc0
->bufctx_3d
);
140 nouveau_bufctx_del(&nvc0
->bufctx
);
141 nouveau_bufctx_del(&nvc0
->bufctx_cp
);
143 util_unreference_framebuffer_state(&nvc0
->framebuffer
);
145 for (i
= 0; i
< nvc0
->num_vtxbufs
; ++i
)
146 pipe_vertex_buffer_unreference(&nvc0
->vtxbuf
[i
]);
148 for (s
= 0; s
< 6; ++s
) {
149 for (i
= 0; i
< nvc0
->num_textures
[s
]; ++i
)
150 pipe_sampler_view_reference(&nvc0
->textures
[s
][i
], NULL
);
152 for (i
= 0; i
< NVC0_MAX_PIPE_CONSTBUFS
; ++i
)
153 if (!nvc0
->constbuf
[s
][i
].user
)
154 pipe_resource_reference(&nvc0
->constbuf
[s
][i
].u
.buf
, NULL
);
156 for (i
= 0; i
< NVC0_MAX_BUFFERS
; ++i
)
157 pipe_resource_reference(&nvc0
->buffers
[s
][i
].buffer
, NULL
);
159 for (i
= 0; i
< NVC0_MAX_IMAGES
; ++i
) {
160 pipe_resource_reference(&nvc0
->images
[s
][i
].resource
, NULL
);
161 if (nvc0
->screen
->base
.class_3d
>= GM107_3D_CLASS
)
162 pipe_sampler_view_reference(&nvc0
->images_tic
[s
][i
], NULL
);
166 for (s
= 0; s
< 2; ++s
) {
167 for (i
= 0; i
< NVC0_MAX_SURFACE_SLOTS
; ++i
)
168 pipe_surface_reference(&nvc0
->surfaces
[s
][i
], NULL
);
171 for (i
= 0; i
< nvc0
->num_tfbbufs
; ++i
)
172 pipe_so_target_reference(&nvc0
->tfbbuf
[i
], NULL
);
174 for (i
= 0; i
< nvc0
->global_residents
.size
/ sizeof(struct pipe_resource
*);
176 struct pipe_resource
**res
= util_dynarray_element(
177 &nvc0
->global_residents
, struct pipe_resource
*, i
);
178 pipe_resource_reference(res
, NULL
);
180 util_dynarray_fini(&nvc0
->global_residents
);
183 nvc0
->base
.pipe
.delete_tcs_state(&nvc0
->base
.pipe
, nvc0
->tcp_empty
);
187 nvc0_destroy(struct pipe_context
*pipe
)
189 struct nvc0_context
*nvc0
= nvc0_context(pipe
);
191 if (nvc0
->screen
->cur_ctx
== nvc0
) {
192 nvc0
->screen
->cur_ctx
= NULL
;
193 nvc0
->screen
->save_state
= nvc0
->state
;
194 nvc0
->screen
->save_state
.tfb
= NULL
;
197 if (nvc0
->base
.pipe
.stream_uploader
)
198 u_upload_destroy(nvc0
->base
.pipe
.stream_uploader
);
200 /* Unset bufctx, we don't want to revalidate any resources after the flush.
201 * Other contexts will always set their bufctx again on action calls.
203 nouveau_pushbuf_bufctx(nvc0
->base
.pushbuf
, NULL
);
204 nouveau_pushbuf_kick(nvc0
->base
.pushbuf
, nvc0
->base
.pushbuf
->channel
);
206 nvc0_context_unreference_resources(nvc0
);
207 nvc0_blitctx_destroy(nvc0
);
209 list_for_each_entry_safe(struct nvc0_resident
, pos
, &nvc0
->tex_head
, list
) {
210 list_del(&pos
->list
);
214 list_for_each_entry_safe(struct nvc0_resident
, pos
, &nvc0
->img_head
, list
) {
215 list_del(&pos
->list
);
219 nouveau_context_destroy(&nvc0
->base
);
223 nvc0_default_kick_notify(struct nouveau_pushbuf
*push
)
225 struct nvc0_screen
*screen
= push
->user_priv
;
228 nouveau_fence_next(&screen
->base
);
229 nouveau_fence_update(&screen
->base
, true);
231 screen
->cur_ctx
->state
.flushed
= true;
232 NOUVEAU_DRV_STAT(&screen
->base
, pushbuf_count
, 1);
237 nvc0_invalidate_resource_storage(struct nouveau_context
*ctx
,
238 struct pipe_resource
*res
,
241 struct nvc0_context
*nvc0
= nvc0_context(&ctx
->pipe
);
244 if (res
->bind
& PIPE_BIND_RENDER_TARGET
) {
245 for (i
= 0; i
< nvc0
->framebuffer
.nr_cbufs
; ++i
) {
246 if (nvc0
->framebuffer
.cbufs
[i
] &&
247 nvc0
->framebuffer
.cbufs
[i
]->texture
== res
) {
248 nvc0
->dirty_3d
|= NVC0_NEW_3D_FRAMEBUFFER
;
249 nouveau_bufctx_reset(nvc0
->bufctx_3d
, NVC0_BIND_3D_FB
);
255 if (res
->bind
& PIPE_BIND_DEPTH_STENCIL
) {
256 if (nvc0
->framebuffer
.zsbuf
&&
257 nvc0
->framebuffer
.zsbuf
->texture
== res
) {
258 nvc0
->dirty_3d
|= NVC0_NEW_3D_FRAMEBUFFER
;
259 nouveau_bufctx_reset(nvc0
->bufctx_3d
, NVC0_BIND_3D_FB
);
265 if (res
->target
== PIPE_BUFFER
) {
266 for (i
= 0; i
< nvc0
->num_vtxbufs
; ++i
) {
267 if (nvc0
->vtxbuf
[i
].buffer
.resource
== res
) {
268 nvc0
->dirty_3d
|= NVC0_NEW_3D_ARRAYS
;
269 nouveau_bufctx_reset(nvc0
->bufctx_3d
, NVC0_BIND_3D_VTX
);
275 for (s
= 0; s
< 6; ++s
) {
276 for (i
= 0; i
< nvc0
->num_textures
[s
]; ++i
) {
277 if (nvc0
->textures
[s
][i
] &&
278 nvc0
->textures
[s
][i
]->texture
== res
) {
279 nvc0
->textures_dirty
[s
] |= 1 << i
;
280 if (unlikely(s
== 5)) {
281 nvc0
->dirty_cp
|= NVC0_NEW_CP_TEXTURES
;
282 nouveau_bufctx_reset(nvc0
->bufctx_cp
, NVC0_BIND_CP_TEX(i
));
284 nvc0
->dirty_3d
|= NVC0_NEW_3D_TEXTURES
;
285 nouveau_bufctx_reset(nvc0
->bufctx_3d
, NVC0_BIND_3D_TEX(s
, i
));
293 for (s
= 0; s
< 6; ++s
) {
294 for (i
= 0; i
< NVC0_MAX_PIPE_CONSTBUFS
; ++i
) {
295 if (!(nvc0
->constbuf_valid
[s
] & (1 << i
)))
297 if (!nvc0
->constbuf
[s
][i
].user
&&
298 nvc0
->constbuf
[s
][i
].u
.buf
== res
) {
299 nvc0
->constbuf_dirty
[s
] |= 1 << i
;
300 if (unlikely(s
== 5)) {
301 nvc0
->dirty_cp
|= NVC0_NEW_CP_CONSTBUF
;
302 nouveau_bufctx_reset(nvc0
->bufctx_cp
, NVC0_BIND_CP_CB(i
));
304 nvc0
->dirty_3d
|= NVC0_NEW_3D_CONSTBUF
;
305 nouveau_bufctx_reset(nvc0
->bufctx_3d
, NVC0_BIND_3D_CB(s
, i
));
313 for (s
= 0; s
< 6; ++s
) {
314 for (i
= 0; i
< NVC0_MAX_BUFFERS
; ++i
) {
315 if (nvc0
->buffers
[s
][i
].buffer
== res
) {
316 nvc0
->buffers_dirty
[s
] |= 1 << i
;
317 if (unlikely(s
== 5)) {
318 nvc0
->dirty_cp
|= NVC0_NEW_CP_BUFFERS
;
319 nouveau_bufctx_reset(nvc0
->bufctx_cp
, NVC0_BIND_CP_BUF
);
321 nvc0
->dirty_3d
|= NVC0_NEW_3D_BUFFERS
;
322 nouveau_bufctx_reset(nvc0
->bufctx_3d
, NVC0_BIND_3D_BUF
);
330 for (s
= 0; s
< 6; ++s
) {
331 for (i
= 0; i
< NVC0_MAX_IMAGES
; ++i
) {
332 if (nvc0
->images
[s
][i
].resource
== res
) {
333 nvc0
->images_dirty
[s
] |= 1 << i
;
334 if (unlikely(s
== 5)) {
335 nvc0
->dirty_cp
|= NVC0_NEW_CP_SURFACES
;
336 nouveau_bufctx_reset(nvc0
->bufctx_cp
, NVC0_BIND_CP_SUF
);
338 nvc0
->dirty_3d
|= NVC0_NEW_3D_SURFACES
;
339 nouveau_bufctx_reset(nvc0
->bufctx_3d
, NVC0_BIND_3D_SUF
);
352 nvc0_context_get_sample_position(struct pipe_context
*, unsigned, unsigned,
355 struct pipe_context
*
356 nvc0_create(struct pipe_screen
*pscreen
, void *priv
, unsigned ctxflags
)
358 struct nvc0_screen
*screen
= nvc0_screen(pscreen
);
359 struct nvc0_context
*nvc0
;
360 struct pipe_context
*pipe
;
364 nvc0
= CALLOC_STRUCT(nvc0_context
);
367 pipe
= &nvc0
->base
.pipe
;
369 if (!nvc0_blitctx_create(nvc0
))
372 nvc0
->base
.pushbuf
= screen
->base
.pushbuf
;
373 nvc0
->base
.client
= screen
->base
.client
;
375 ret
= nouveau_bufctx_new(screen
->base
.client
, 2, &nvc0
->bufctx
);
377 ret
= nouveau_bufctx_new(screen
->base
.client
, NVC0_BIND_3D_COUNT
,
380 ret
= nouveau_bufctx_new(screen
->base
.client
, NVC0_BIND_CP_COUNT
,
385 nvc0
->screen
= screen
;
386 nvc0
->base
.screen
= &screen
->base
;
388 pipe
->screen
= pscreen
;
390 pipe
->stream_uploader
= u_upload_create_default(pipe
);
391 if (!pipe
->stream_uploader
)
393 pipe
->const_uploader
= pipe
->stream_uploader
;
395 pipe
->destroy
= nvc0_destroy
;
397 pipe
->draw_vbo
= nvc0_draw_vbo
;
398 pipe
->clear
= nvc0_clear
;
399 pipe
->launch_grid
= (nvc0
->screen
->base
.class_3d
>= NVE4_3D_CLASS
) ?
400 nve4_launch_grid
: nvc0_launch_grid
;
402 pipe
->flush
= nvc0_flush
;
403 pipe
->texture_barrier
= nvc0_texture_barrier
;
404 pipe
->memory_barrier
= nvc0_memory_barrier
;
405 pipe
->get_sample_position
= nvc0_context_get_sample_position
;
406 pipe
->emit_string_marker
= nvc0_emit_string_marker
;
408 nouveau_context_init(&nvc0
->base
);
409 nvc0_init_query_functions(nvc0
);
410 nvc0_init_surface_functions(nvc0
);
411 nvc0_init_state_functions(nvc0
);
412 nvc0_init_transfer_functions(nvc0
);
413 nvc0_init_resource_functions(pipe
);
414 if (nvc0
->screen
->base
.class_3d
>= NVE4_3D_CLASS
)
415 nvc0_init_bindless_functions(pipe
);
417 list_inithead(&nvc0
->tex_head
);
418 list_inithead(&nvc0
->img_head
);
420 nvc0
->base
.invalidate_resource_storage
= nvc0_invalidate_resource_storage
;
422 pipe
->create_video_codec
= nvc0_create_decoder
;
423 pipe
->create_video_buffer
= nvc0_video_buffer_create
;
425 /* shader builtin library is per-screen, but we need a context for m2mf */
426 nvc0_program_library_upload(nvc0
);
427 nvc0_program_init_tcp_empty(nvc0
);
428 if (!nvc0
->tcp_empty
)
430 /* set the empty tctl prog on next draw in case one is never set */
431 nvc0
->dirty_3d
|= NVC0_NEW_3D_TCTLPROG
;
433 /* Do not bind the COMPUTE driver constbuf at screen initialization because
434 * CBs are aliased between 3D and COMPUTE, but make sure it will be bound if
435 * a grid is launched later. */
436 nvc0
->dirty_cp
|= NVC0_NEW_CP_DRIVERCONST
;
438 /* now that there are no more opportunities for errors, set the current
439 * context if there isn't already one.
441 if (!screen
->cur_ctx
) {
442 nvc0
->state
= screen
->save_state
;
443 screen
->cur_ctx
= nvc0
;
444 nouveau_pushbuf_bufctx(screen
->base
.pushbuf
, nvc0
->bufctx
);
446 screen
->base
.pushbuf
->kick_notify
= nvc0_default_kick_notify
;
448 /* add permanently resident buffers to bufctxts */
450 flags
= NV_VRAM_DOMAIN(&screen
->base
) | NOUVEAU_BO_RD
;
452 BCTX_REFN_bo(nvc0
->bufctx_3d
, 3D_TEXT
, flags
, screen
->text
);
453 BCTX_REFN_bo(nvc0
->bufctx_3d
, 3D_SCREEN
, flags
, screen
->uniform_bo
);
454 BCTX_REFN_bo(nvc0
->bufctx_3d
, 3D_SCREEN
, flags
, screen
->txc
);
455 if (screen
->compute
) {
456 BCTX_REFN_bo(nvc0
->bufctx_cp
, CP_TEXT
, flags
, screen
->text
);
457 BCTX_REFN_bo(nvc0
->bufctx_cp
, CP_SCREEN
, flags
, screen
->uniform_bo
);
458 BCTX_REFN_bo(nvc0
->bufctx_cp
, CP_SCREEN
, flags
, screen
->txc
);
461 flags
= NV_VRAM_DOMAIN(&screen
->base
) | NOUVEAU_BO_RDWR
;
463 if (screen
->poly_cache
)
464 BCTX_REFN_bo(nvc0
->bufctx_3d
, 3D_SCREEN
, flags
, screen
->poly_cache
);
466 BCTX_REFN_bo(nvc0
->bufctx_cp
, CP_SCREEN
, flags
, screen
->tls
);
468 flags
= NOUVEAU_BO_GART
| NOUVEAU_BO_WR
;
470 BCTX_REFN_bo(nvc0
->bufctx_3d
, 3D_SCREEN
, flags
, screen
->fence
.bo
);
471 BCTX_REFN_bo(nvc0
->bufctx
, FENCE
, flags
, screen
->fence
.bo
);
473 BCTX_REFN_bo(nvc0
->bufctx_cp
, CP_SCREEN
, flags
, screen
->fence
.bo
);
475 nvc0
->base
.scratch
.bo_size
= 2 << 20;
477 memset(nvc0
->tex_handles
, ~0, sizeof(nvc0
->tex_handles
));
479 util_dynarray_init(&nvc0
->global_residents
, NULL
);
485 if (pipe
->stream_uploader
)
486 u_upload_destroy(pipe
->stream_uploader
);
488 nouveau_bufctx_del(&nvc0
->bufctx_3d
);
490 nouveau_bufctx_del(&nvc0
->bufctx_cp
);
492 nouveau_bufctx_del(&nvc0
->bufctx
);
500 nvc0_bufctx_fence(struct nvc0_context
*nvc0
, struct nouveau_bufctx
*bufctx
,
503 struct nouveau_list
*list
= on_flush
? &bufctx
->current
: &bufctx
->pending
;
504 struct nouveau_list
*it
;
505 NOUVEAU_DRV_STAT_IFD(unsigned count
= 0);
507 for (it
= list
->next
; it
!= list
; it
= it
->next
) {
508 struct nouveau_bufref
*ref
= (struct nouveau_bufref
*)it
;
509 struct nv04_resource
*res
= ref
->priv
;
511 nvc0_resource_validate(res
, (unsigned)ref
->priv_data
);
512 NOUVEAU_DRV_STAT_IFD(count
++);
514 NOUVEAU_DRV_STAT(&nvc0
->screen
->base
, resource_validate_count
, count
);
518 nvc0_get_sample_locations(unsigned sample_count
)
520 static const uint8_t ms1
[1][2] = { { 0x8, 0x8 } };
521 static const uint8_t ms2
[2][2] = {
522 { 0x4, 0x4 }, { 0xc, 0xc } }; /* surface coords (0,0), (1,0) */
523 static const uint8_t ms4
[4][2] = {
524 { 0x6, 0x2 }, { 0xe, 0x6 }, /* (0,0), (1,0) */
525 { 0x2, 0xa }, { 0xa, 0xe } }; /* (0,1), (1,1) */
526 static const uint8_t ms8
[8][2] = {
527 { 0x1, 0x7 }, { 0x5, 0x3 }, /* (0,0), (1,0) */
528 { 0x3, 0xd }, { 0x7, 0xb }, /* (0,1), (1,1) */
529 { 0x9, 0x5 }, { 0xf, 0x1 }, /* (2,0), (3,0) */
530 { 0xb, 0xf }, { 0xd, 0x9 } }; /* (2,1), (3,1) */
532 /* NOTE: there are alternative modes for MS2 and MS8, currently not used */
533 static const uint8_t ms8_alt
[8][2] = {
534 { 0x9, 0x5 }, { 0x7, 0xb }, /* (2,0), (1,1) */
535 { 0xd, 0x9 }, { 0x5, 0x3 }, /* (3,1), (1,0) */
536 { 0x3, 0xd }, { 0x1, 0x7 }, /* (0,1), (0,0) */
537 { 0xb, 0xf }, { 0xf, 0x1 } }; /* (2,1), (3,0) */
540 const uint8_t (*ptr
)[2];
542 switch (sample_count
) {
544 case 1: ptr
= ms1
; break;
545 case 2: ptr
= ms2
; break;
546 case 4: ptr
= ms4
; break;
547 case 8: ptr
= ms8
; break;
550 return NULL
; /* bad sample count -> undefined locations */
556 nvc0_context_get_sample_position(struct pipe_context
*pipe
,
557 unsigned sample_count
, unsigned sample_index
,
560 const uint8_t (*ptr
)[2];
562 ptr
= nvc0_get_sample_locations(sample_count
);
566 xy
[0] = ptr
[sample_index
][0] * 0.0625f
;
567 xy
[1] = ptr
[sample_index
][1] * 0.0625f
;