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
);
61 if (flags
& PIPE_BARRIER_MAPPED_BUFFER
) {
62 for (i
= 0; i
< nvc0
->num_vtxbufs
; ++i
) {
63 if (!nvc0
->vtxbuf
[i
].buffer
)
65 if (nvc0
->vtxbuf
[i
].buffer
->flags
& PIPE_RESOURCE_FLAG_MAP_PERSISTENT
)
66 nvc0
->base
.vbo_dirty
= true;
69 if (nvc0
->idxbuf
.buffer
&&
70 nvc0
->idxbuf
.buffer
->flags
& PIPE_RESOURCE_FLAG_MAP_PERSISTENT
)
71 nvc0
->base
.vbo_dirty
= true;
73 for (s
= 0; s
< 5 && !nvc0
->cb_dirty
; ++s
) {
74 uint32_t valid
= nvc0
->constbuf_valid
[s
];
76 while (valid
&& !nvc0
->cb_dirty
) {
77 const unsigned i
= ffs(valid
) - 1;
78 struct pipe_resource
*res
;
81 if (nvc0
->constbuf
[s
][i
].user
)
84 res
= nvc0
->constbuf
[s
][i
].u
.buf
;
88 if (res
->flags
& PIPE_RESOURCE_FLAG_MAP_PERSISTENT
)
89 nvc0
->cb_dirty
= true;
96 nvc0_context_unreference_resources(struct nvc0_context
*nvc0
)
100 nouveau_bufctx_del(&nvc0
->bufctx_3d
);
101 nouveau_bufctx_del(&nvc0
->bufctx
);
102 nouveau_bufctx_del(&nvc0
->bufctx_cp
);
104 util_unreference_framebuffer_state(&nvc0
->framebuffer
);
106 for (i
= 0; i
< nvc0
->num_vtxbufs
; ++i
)
107 pipe_resource_reference(&nvc0
->vtxbuf
[i
].buffer
, NULL
);
109 pipe_resource_reference(&nvc0
->idxbuf
.buffer
, NULL
);
111 for (s
= 0; s
< 6; ++s
) {
112 for (i
= 0; i
< nvc0
->num_textures
[s
]; ++i
)
113 pipe_sampler_view_reference(&nvc0
->textures
[s
][i
], NULL
);
115 for (i
= 0; i
< NVC0_MAX_PIPE_CONSTBUFS
; ++i
)
116 if (!nvc0
->constbuf
[s
][i
].user
)
117 pipe_resource_reference(&nvc0
->constbuf
[s
][i
].u
.buf
, NULL
);
120 for (s
= 0; s
< 2; ++s
) {
121 for (i
= 0; i
< NVC0_MAX_SURFACE_SLOTS
; ++i
)
122 pipe_surface_reference(&nvc0
->surfaces
[s
][i
], NULL
);
125 for (i
= 0; i
< nvc0
->num_tfbbufs
; ++i
)
126 pipe_so_target_reference(&nvc0
->tfbbuf
[i
], NULL
);
128 for (i
= 0; i
< nvc0
->global_residents
.size
/ sizeof(struct pipe_resource
*);
130 struct pipe_resource
**res
= util_dynarray_element(
131 &nvc0
->global_residents
, struct pipe_resource
*, i
);
132 pipe_resource_reference(res
, NULL
);
134 util_dynarray_fini(&nvc0
->global_residents
);
137 nvc0
->base
.pipe
.delete_tcs_state(&nvc0
->base
.pipe
, nvc0
->tcp_empty
);
141 nvc0_destroy(struct pipe_context
*pipe
)
143 struct nvc0_context
*nvc0
= nvc0_context(pipe
);
145 if (nvc0
->screen
->cur_ctx
== nvc0
) {
146 nvc0
->screen
->cur_ctx
= NULL
;
147 nvc0
->screen
->save_state
= nvc0
->state
;
148 nvc0
->screen
->save_state
.tfb
= NULL
;
151 /* Unset bufctx, we don't want to revalidate any resources after the flush.
152 * Other contexts will always set their bufctx again on action calls.
154 nouveau_pushbuf_bufctx(nvc0
->base
.pushbuf
, NULL
);
155 nouveau_pushbuf_kick(nvc0
->base
.pushbuf
, nvc0
->base
.pushbuf
->channel
);
157 nvc0_context_unreference_resources(nvc0
);
158 nvc0_blitctx_destroy(nvc0
);
160 nouveau_context_destroy(&nvc0
->base
);
164 nvc0_default_kick_notify(struct nouveau_pushbuf
*push
)
166 struct nvc0_screen
*screen
= push
->user_priv
;
169 nouveau_fence_next(&screen
->base
);
170 nouveau_fence_update(&screen
->base
, true);
172 screen
->cur_ctx
->state
.flushed
= true;
173 NOUVEAU_DRV_STAT(&screen
->base
, pushbuf_count
, 1);
178 nvc0_invalidate_resource_storage(struct nouveau_context
*ctx
,
179 struct pipe_resource
*res
,
182 struct nvc0_context
*nvc0
= nvc0_context(&ctx
->pipe
);
185 if (res
->bind
& PIPE_BIND_RENDER_TARGET
) {
186 for (i
= 0; i
< nvc0
->framebuffer
.nr_cbufs
; ++i
) {
187 if (nvc0
->framebuffer
.cbufs
[i
] &&
188 nvc0
->framebuffer
.cbufs
[i
]->texture
== res
) {
189 nvc0
->dirty
|= NVC0_NEW_FRAMEBUFFER
;
190 nouveau_bufctx_reset(nvc0
->bufctx_3d
, NVC0_BIND_FB
);
196 if (res
->bind
& PIPE_BIND_DEPTH_STENCIL
) {
197 if (nvc0
->framebuffer
.zsbuf
&&
198 nvc0
->framebuffer
.zsbuf
->texture
== res
) {
199 nvc0
->dirty
|= NVC0_NEW_FRAMEBUFFER
;
200 nouveau_bufctx_reset(nvc0
->bufctx_3d
, NVC0_BIND_FB
);
206 if (res
->bind
& (PIPE_BIND_VERTEX_BUFFER
|
207 PIPE_BIND_INDEX_BUFFER
|
208 PIPE_BIND_CONSTANT_BUFFER
|
209 PIPE_BIND_STREAM_OUTPUT
|
210 PIPE_BIND_COMMAND_ARGS_BUFFER
|
211 PIPE_BIND_SAMPLER_VIEW
)) {
212 for (i
= 0; i
< nvc0
->num_vtxbufs
; ++i
) {
213 if (nvc0
->vtxbuf
[i
].buffer
== res
) {
214 nvc0
->dirty
|= NVC0_NEW_ARRAYS
;
215 nouveau_bufctx_reset(nvc0
->bufctx_3d
, NVC0_BIND_VTX
);
221 if (nvc0
->idxbuf
.buffer
== res
) {
222 nvc0
->dirty
|= NVC0_NEW_IDXBUF
;
223 nouveau_bufctx_reset(nvc0
->bufctx_3d
, NVC0_BIND_IDX
);
228 for (s
= 0; s
< 5; ++s
) {
229 for (i
= 0; i
< nvc0
->num_textures
[s
]; ++i
) {
230 if (nvc0
->textures
[s
][i
] &&
231 nvc0
->textures
[s
][i
]->texture
== res
) {
232 nvc0
->textures_dirty
[s
] |= 1 << i
;
233 nvc0
->dirty
|= NVC0_NEW_TEXTURES
;
234 nouveau_bufctx_reset(nvc0
->bufctx_3d
, NVC0_BIND_TEX(s
, i
));
241 for (s
= 0; s
< 5; ++s
) {
242 for (i
= 0; i
< NVC0_MAX_PIPE_CONSTBUFS
; ++i
) {
243 if (!(nvc0
->constbuf_valid
[s
] & (1 << i
)))
245 if (!nvc0
->constbuf
[s
][i
].user
&&
246 nvc0
->constbuf
[s
][i
].u
.buf
== res
) {
247 nvc0
->dirty
|= NVC0_NEW_CONSTBUF
;
248 nvc0
->constbuf_dirty
[s
] |= 1 << i
;
249 nouveau_bufctx_reset(nvc0
->bufctx_3d
, NVC0_BIND_CB(s
, i
));
261 nvc0_context_get_sample_position(struct pipe_context
*, unsigned, unsigned,
264 struct pipe_context
*
265 nvc0_create(struct pipe_screen
*pscreen
, void *priv
, unsigned ctxflags
)
267 struct nvc0_screen
*screen
= nvc0_screen(pscreen
);
268 struct nvc0_context
*nvc0
;
269 struct pipe_context
*pipe
;
273 nvc0
= CALLOC_STRUCT(nvc0_context
);
276 pipe
= &nvc0
->base
.pipe
;
278 if (!nvc0_blitctx_create(nvc0
))
281 nvc0
->base
.pushbuf
= screen
->base
.pushbuf
;
282 nvc0
->base
.client
= screen
->base
.client
;
284 ret
= nouveau_bufctx_new(screen
->base
.client
, 2, &nvc0
->bufctx
);
286 ret
= nouveau_bufctx_new(screen
->base
.client
, NVC0_BIND_3D_COUNT
,
289 ret
= nouveau_bufctx_new(screen
->base
.client
, NVC0_BIND_CP_COUNT
,
294 nvc0
->screen
= screen
;
295 nvc0
->base
.screen
= &screen
->base
;
297 pipe
->screen
= pscreen
;
300 pipe
->destroy
= nvc0_destroy
;
302 pipe
->draw_vbo
= nvc0_draw_vbo
;
303 pipe
->clear
= nvc0_clear
;
304 pipe
->launch_grid
= (nvc0
->screen
->base
.class_3d
>= NVE4_3D_CLASS
) ?
305 nve4_launch_grid
: nvc0_launch_grid
;
307 pipe
->flush
= nvc0_flush
;
308 pipe
->texture_barrier
= nvc0_texture_barrier
;
309 pipe
->memory_barrier
= nvc0_memory_barrier
;
310 pipe
->get_sample_position
= nvc0_context_get_sample_position
;
312 nvc0_init_query_functions(nvc0
);
313 nvc0_init_surface_functions(nvc0
);
314 nvc0_init_state_functions(nvc0
);
315 nvc0_init_transfer_functions(nvc0
);
316 nvc0_init_resource_functions(pipe
);
318 nvc0
->base
.invalidate_resource_storage
= nvc0_invalidate_resource_storage
;
320 pipe
->create_video_codec
= nvc0_create_decoder
;
321 pipe
->create_video_buffer
= nvc0_video_buffer_create
;
323 /* shader builtin library is per-screen, but we need a context for m2mf */
324 nvc0_program_library_upload(nvc0
);
325 nvc0_program_init_tcp_empty(nvc0
);
326 if (!nvc0
->tcp_empty
)
328 /* set the empty tctl prog on next draw in case one is never set */
329 nvc0
->dirty
|= NVC0_NEW_TCTLPROG
;
331 /* now that there are no more opportunities for errors, set the current
332 * context if there isn't already one.
334 if (!screen
->cur_ctx
) {
335 nvc0
->state
= screen
->save_state
;
336 screen
->cur_ctx
= nvc0
;
337 nouveau_pushbuf_bufctx(screen
->base
.pushbuf
, nvc0
->bufctx
);
339 screen
->base
.pushbuf
->kick_notify
= nvc0_default_kick_notify
;
341 /* add permanently resident buffers to bufctxts */
343 flags
= NV_VRAM_DOMAIN(&screen
->base
) | NOUVEAU_BO_RD
;
345 BCTX_REFN_bo(nvc0
->bufctx_3d
, SCREEN
, flags
, screen
->text
);
346 BCTX_REFN_bo(nvc0
->bufctx_3d
, SCREEN
, flags
, screen
->uniform_bo
);
347 BCTX_REFN_bo(nvc0
->bufctx_3d
, SCREEN
, flags
, screen
->txc
);
348 if (screen
->compute
) {
349 BCTX_REFN_bo(nvc0
->bufctx_cp
, CP_SCREEN
, flags
, screen
->text
);
350 BCTX_REFN_bo(nvc0
->bufctx_cp
, CP_SCREEN
, flags
, screen
->txc
);
351 BCTX_REFN_bo(nvc0
->bufctx_cp
, CP_SCREEN
, flags
, screen
->parm
);
354 flags
= NV_VRAM_DOMAIN(&screen
->base
) | NOUVEAU_BO_RDWR
;
356 if (screen
->poly_cache
)
357 BCTX_REFN_bo(nvc0
->bufctx_3d
, SCREEN
, flags
, screen
->poly_cache
);
359 BCTX_REFN_bo(nvc0
->bufctx_cp
, CP_SCREEN
, flags
, screen
->tls
);
361 flags
= NOUVEAU_BO_GART
| NOUVEAU_BO_WR
;
363 BCTX_REFN_bo(nvc0
->bufctx_3d
, SCREEN
, flags
, screen
->fence
.bo
);
364 BCTX_REFN_bo(nvc0
->bufctx
, FENCE
, flags
, screen
->fence
.bo
);
366 BCTX_REFN_bo(nvc0
->bufctx_cp
, CP_SCREEN
, flags
, screen
->fence
.bo
);
368 nvc0
->base
.scratch
.bo_size
= 2 << 20;
370 memset(nvc0
->tex_handles
, ~0, sizeof(nvc0
->tex_handles
));
372 util_dynarray_init(&nvc0
->global_residents
);
379 nouveau_bufctx_del(&nvc0
->bufctx_3d
);
381 nouveau_bufctx_del(&nvc0
->bufctx_cp
);
383 nouveau_bufctx_del(&nvc0
->bufctx
);
391 nvc0_bufctx_fence(struct nvc0_context
*nvc0
, struct nouveau_bufctx
*bufctx
,
394 struct nouveau_list
*list
= on_flush
? &bufctx
->current
: &bufctx
->pending
;
395 struct nouveau_list
*it
;
396 NOUVEAU_DRV_STAT_IFD(unsigned count
= 0);
398 for (it
= list
->next
; it
!= list
; it
= it
->next
) {
399 struct nouveau_bufref
*ref
= (struct nouveau_bufref
*)it
;
400 struct nv04_resource
*res
= ref
->priv
;
402 nvc0_resource_validate(res
, (unsigned)ref
->priv_data
);
403 NOUVEAU_DRV_STAT_IFD(count
++);
405 NOUVEAU_DRV_STAT(&nvc0
->screen
->base
, resource_validate_count
, count
);
409 nvc0_context_get_sample_position(struct pipe_context
*pipe
,
410 unsigned sample_count
, unsigned sample_index
,
413 static const uint8_t ms1
[1][2] = { { 0x8, 0x8 } };
414 static const uint8_t ms2
[2][2] = {
415 { 0x4, 0x4 }, { 0xc, 0xc } }; /* surface coords (0,0), (1,0) */
416 static const uint8_t ms4
[4][2] = {
417 { 0x6, 0x2 }, { 0xe, 0x6 }, /* (0,0), (1,0) */
418 { 0x2, 0xa }, { 0xa, 0xe } }; /* (0,1), (1,1) */
419 static const uint8_t ms8
[8][2] = {
420 { 0x1, 0x7 }, { 0x5, 0x3 }, /* (0,0), (1,0) */
421 { 0x3, 0xd }, { 0x7, 0xb }, /* (0,1), (1,1) */
422 { 0x9, 0x5 }, { 0xf, 0x1 }, /* (2,0), (3,0) */
423 { 0xb, 0xf }, { 0xd, 0x9 } }; /* (2,1), (3,1) */
425 /* NOTE: there are alternative modes for MS2 and MS8, currently not used */
426 static const uint8_t ms8_alt
[8][2] = {
427 { 0x9, 0x5 }, { 0x7, 0xb }, /* (2,0), (1,1) */
428 { 0xd, 0x9 }, { 0x5, 0x3 }, /* (3,1), (1,0) */
429 { 0x3, 0xd }, { 0x1, 0x7 }, /* (0,1), (0,0) */
430 { 0xb, 0xf }, { 0xf, 0x1 } }; /* (2,1), (3,0) */
433 const uint8_t (*ptr
)[2];
435 switch (sample_count
) {
437 case 1: ptr
= ms1
; break;
438 case 2: ptr
= ms2
; break;
439 case 4: ptr
= ms4
; break;
440 case 8: ptr
= ms8
; break;
443 return; /* bad sample count -> undefined locations */
445 xy
[0] = ptr
[sample_index
][0] * 0.0625f
;
446 xy
[1] = ptr
[sample_index
][1] * 0.0625f
;