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 #ifdef NV50_WITH_DRAW_MODULE
27 #include "draw/draw_context.h"
30 #include "nv50_context.h"
31 #include "nv50_screen.h"
32 #include "nv50_resource.h"
35 nv50_flush(struct pipe_context
*pipe
,
36 struct pipe_fence_handle
**fence
,
37 enum pipe_flush_flags flags
)
39 struct nouveau_screen
*screen
= nouveau_screen(pipe
->screen
);
42 nouveau_fence_ref(screen
->fence
.current
, (struct nouveau_fence
**)fence
);
44 PUSH_KICK(screen
->pushbuf
);
46 nouveau_context_update_frame_stats(nouveau_context(pipe
));
50 nv50_texture_barrier(struct pipe_context
*pipe
)
52 struct nouveau_pushbuf
*push
= nv50_context(pipe
)->base
.pushbuf
;
54 BEGIN_NV04(push
, SUBC_3D(NV50_GRAPH_SERIALIZE
), 1);
56 BEGIN_NV04(push
, NV50_3D(TEX_CACHE_CTL
), 1);
57 PUSH_DATA (push
, 0x20);
61 nv50_default_kick_notify(struct nouveau_pushbuf
*push
)
63 struct nv50_screen
*screen
= push
->user_priv
;
66 nouveau_fence_next(&screen
->base
);
67 nouveau_fence_update(&screen
->base
, TRUE
);
69 screen
->cur_ctx
->state
.flushed
= TRUE
;
74 nv50_context_unreference_resources(struct nv50_context
*nv50
)
78 nouveau_bufctx_del(&nv50
->bufctx_3d
);
79 nouveau_bufctx_del(&nv50
->bufctx
);
81 util_unreference_framebuffer_state(&nv50
->framebuffer
);
83 for (i
= 0; i
< nv50
->num_vtxbufs
; ++i
)
84 pipe_resource_reference(&nv50
->vtxbuf
[i
].buffer
, NULL
);
86 pipe_resource_reference(&nv50
->idxbuf
.buffer
, NULL
);
88 for (s
= 0; s
< 3; ++s
) {
89 for (i
= 0; i
< nv50
->num_textures
[s
]; ++i
)
90 pipe_sampler_view_reference(&nv50
->textures
[s
][i
], NULL
);
92 for (i
= 0; i
< NV50_MAX_PIPE_CONSTBUFS
; ++i
)
93 if (!nv50
->constbuf
[s
][i
].user
)
94 pipe_resource_reference(&nv50
->constbuf
[s
][i
].u
.buf
, NULL
);
99 nv50_destroy(struct pipe_context
*pipe
)
101 struct nv50_context
*nv50
= nv50_context(pipe
);
103 if (nv50_context_screen(nv50
)->cur_ctx
== nv50
) {
104 nv50
->base
.pushbuf
->kick_notify
= NULL
;
105 nv50_context_screen(nv50
)->cur_ctx
= NULL
;
106 nouveau_pushbuf_bufctx(nv50
->base
.pushbuf
, NULL
);
108 /* need to flush before destroying the bufctx */
109 nouveau_pushbuf_kick(nv50
->base
.pushbuf
, nv50
->base
.pushbuf
->channel
);
111 nv50_context_unreference_resources(nv50
);
113 #ifdef NV50_WITH_DRAW_MODULE
114 draw_destroy(nv50
->draw
);
117 nouveau_context_destroy(&nv50
->base
);
121 nv50_invalidate_resource_storage(struct nouveau_context
*ctx
,
122 struct pipe_resource
*res
,
125 struct nv50_context
*nv50
= nv50_context(&ctx
->pipe
);
128 if (res
->bind
& PIPE_BIND_RENDER_TARGET
) {
129 for (i
= 0; i
< nv50
->framebuffer
.nr_cbufs
; ++i
) {
130 if (nv50
->framebuffer
.cbufs
[i
] &&
131 nv50
->framebuffer
.cbufs
[i
]->texture
== res
) {
132 nv50
->dirty
|= NV50_NEW_FRAMEBUFFER
;
133 nouveau_bufctx_reset(nv50
->bufctx_3d
, NV50_BIND_FB
);
139 if (res
->bind
& PIPE_BIND_DEPTH_STENCIL
) {
140 if (nv50
->framebuffer
.zsbuf
&&
141 nv50
->framebuffer
.zsbuf
->texture
== res
) {
142 nv50
->dirty
|= NV50_NEW_FRAMEBUFFER
;
143 nouveau_bufctx_reset(nv50
->bufctx_3d
, NV50_BIND_FB
);
149 if (res
->bind
& PIPE_BIND_VERTEX_BUFFER
) {
150 for (i
= 0; i
< nv50
->num_vtxbufs
; ++i
) {
151 if (nv50
->vtxbuf
[i
].buffer
== res
) {
152 nv50
->dirty
|= NV50_NEW_ARRAYS
;
153 nouveau_bufctx_reset(nv50
->bufctx_3d
, NV50_BIND_VERTEX
);
159 if (res
->bind
& PIPE_BIND_INDEX_BUFFER
) {
160 if (nv50
->idxbuf
.buffer
== res
)
165 if (res
->bind
& PIPE_BIND_SAMPLER_VIEW
) {
166 for (s
= 0; s
< 5; ++s
) {
167 for (i
= 0; i
< nv50
->num_textures
[s
]; ++i
) {
168 if (nv50
->textures
[s
][i
] &&
169 nv50
->textures
[s
][i
]->texture
== res
) {
170 nv50
->dirty
|= NV50_NEW_TEXTURES
;
171 nouveau_bufctx_reset(nv50
->bufctx_3d
, NV50_BIND_TEXTURES
);
179 if (res
->bind
& PIPE_BIND_CONSTANT_BUFFER
) {
180 for (s
= 0; s
< 5; ++s
) {
181 for (i
= 0; i
< nv50
->num_vtxbufs
; ++i
) {
182 if (!nv50
->constbuf
[s
][i
].user
&&
183 nv50
->constbuf
[s
][i
].u
.buf
== res
) {
184 nv50
->dirty
|= NV50_NEW_CONSTBUF
;
185 nv50
->constbuf_dirty
[s
] |= 1 << i
;
186 nouveau_bufctx_reset(nv50
->bufctx_3d
, NV50_BIND_CB(s
, i
));
197 struct pipe_context
*
198 nv50_create(struct pipe_screen
*pscreen
, void *priv
)
200 struct nv50_screen
*screen
= nv50_screen(pscreen
);
201 struct nv50_context
*nv50
;
202 struct pipe_context
*pipe
;
206 nv50
= CALLOC_STRUCT(nv50_context
);
209 pipe
= &nv50
->base
.pipe
;
211 if (!nv50_blitctx_create(nv50
))
214 nv50
->base
.pushbuf
= screen
->base
.pushbuf
;
215 nv50
->base
.client
= screen
->base
.client
;
217 ret
= nouveau_bufctx_new(screen
->base
.client
, NV50_BIND_COUNT
,
220 ret
= nouveau_bufctx_new(screen
->base
.client
, 2, &nv50
->bufctx
);
224 nv50
->base
.screen
= &screen
->base
;
225 nv50
->base
.copy_data
= nv50_m2mf_copy_linear
;
226 nv50
->base
.push_data
= nv50_sifc_linear_u8
;
227 nv50
->base
.push_cb
= nv50_cb_push
;
229 nv50
->screen
= screen
;
230 pipe
->screen
= pscreen
;
233 pipe
->destroy
= nv50_destroy
;
235 pipe
->draw_vbo
= nv50_draw_vbo
;
236 pipe
->clear
= nv50_clear
;
238 pipe
->flush
= nv50_flush
;
239 pipe
->texture_barrier
= nv50_texture_barrier
;
241 if (!screen
->cur_ctx
) {
242 screen
->cur_ctx
= nv50
;
243 nouveau_pushbuf_bufctx(screen
->base
.pushbuf
, nv50
->bufctx
);
246 nv50_init_query_functions(nv50
);
247 nv50_init_surface_functions(nv50
);
248 nv50_init_state_functions(nv50
);
249 nv50_init_resource_functions(pipe
);
251 nv50
->base
.invalidate_resource_storage
= nv50_invalidate_resource_storage
;
253 #ifdef NV50_WITH_DRAW_MODULE
254 /* no software fallbacks implemented */
255 nv50
->draw
= draw_create(pipe
);
257 draw_set_rasterize_stage(nv50
->draw
, nv50_draw_render_stage(nv50
));
260 nouveau_context_init_vdec(&nv50
->base
);
262 flags
= NOUVEAU_BO_VRAM
| NOUVEAU_BO_RD
;
264 BCTX_REFN_bo(nv50
->bufctx_3d
, SCREEN
, flags
, screen
->code
);
265 BCTX_REFN_bo(nv50
->bufctx_3d
, SCREEN
, flags
, screen
->uniforms
);
266 BCTX_REFN_bo(nv50
->bufctx_3d
, SCREEN
, flags
, screen
->txc
);
267 BCTX_REFN_bo(nv50
->bufctx_3d
, SCREEN
, flags
, screen
->stack_bo
);
269 flags
= NOUVEAU_BO_GART
| NOUVEAU_BO_WR
;
271 BCTX_REFN_bo(nv50
->bufctx_3d
, SCREEN
, flags
, screen
->fence
.bo
);
272 BCTX_REFN_bo(nv50
->bufctx
, FENCE
, flags
, screen
->fence
.bo
);
274 nv50
->base
.scratch
.bo_size
= 2 << 20;
281 nouveau_bufctx_del(&nv50
->bufctx_3d
);
283 nouveau_bufctx_del(&nv50
->bufctx
);
292 nv50_bufctx_fence(struct nouveau_bufctx
*bufctx
, boolean on_flush
)
294 struct nouveau_list
*list
= on_flush
? &bufctx
->current
: &bufctx
->pending
;
295 struct nouveau_list
*it
;
297 for (it
= list
->next
; it
!= list
; it
= it
->next
) {
298 struct nouveau_bufref
*ref
= (struct nouveau_bufref
*)it
;
299 struct nv04_resource
*res
= ref
->priv
;
301 nv50_resource_validate(res
, (unsigned)ref
->priv_data
);