Debian Codegraph @

src/gallium/drivers/nouveau/nv30/nv30_context.c

raw
1 /*
2 * Copyright 2012 Red Hat Inc.
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 * Authors: Ben Skeggs
23 *
24 */
25
26 #include "draw/draw_context.h"
27 #include "util/u_upload_mgr.h"
28
29 #include "nv_object.xml.h"
30 #include "nv30/nv30-40_3d.xml.h"
31
32 #include "nouveau_fence.h"
33 #include "nv30/nv30_context.h"
34 #include "nv30/nv30_transfer.h"
35 #include "nv30/nv30_state.h"
36 #include "nv30/nv30_winsys.h"
37
38 static void
39 nv30_context_kick_notify(struct nouveau_pushbuf *push)
40 {
41 struct nouveau_pushbuf_priv *p = push->user_priv;
42 struct nouveau_screen *screen = p->screen;
43
44 _nouveau_fence_next(p->context);
45 _nouveau_fence_update(screen, true);
46
47 if (push->bufctx) {
48 struct nouveau_bufref *bref;
49 LIST_FOR_EACH_ENTRY(bref, &push->bufctx->current, thead) {
50 struct nv04_resource *res = bref->priv;
51 if (res && res->mm) {
52 _nouveau_fence_ref(p->context->fence, &res->fence);
53
54 if (bref->flags & NOUVEAU_BO_RD)
55 res->status |= NOUVEAU_BUFFER_STATUS_GPU_READING;
56
57 if (bref->flags & NOUVEAU_BO_WR) {
58 _nouveau_fence_ref(p->context->fence, &res->fence_wr);
59 res->status |= NOUVEAU_BUFFER_STATUS_GPU_WRITING |
60 NOUVEAU_BUFFER_STATUS_DIRTY;
61 }
62 }
63 }
64 }
65 }
66
67 static void
68 nv30_context_flush(struct pipe_context *pipe, struct pipe_fence_handle **fence,
69 unsigned flags)
70 {
71 struct nv30_context *nv30 = nv30_context(pipe);
72 struct nouveau_pushbuf *push = nv30->base.pushbuf;
73
74 if (fence)
75 nouveau_fence_ref(nv30->base.fence,
76 (struct nouveau_fence **)fence,
77 nv30->base.screen);
78
79 PUSH_KICK(push);
80
81 nouveau_context_update_frame_stats(&nv30->base);
82 }
83
84 static int
85 nv30_invalidate_resource_storage(struct nouveau_context *nv,
86 struct pipe_resource *res,
87 int ref)
88 {
89 struct nv30_context *nv30 = nv30_context(&nv->pipe);
90 unsigned i;
91
92 if (res->bind & PIPE_BIND_RENDER_TARGET) {
93 for (i = 0; i < nv30->framebuffer.nr_cbufs; ++i) {
94 if (nv30->framebuffer.cbufs[i].texture == res) {
95 nv30->dirty |= NV30_NEW_FRAMEBUFFER;
96 nouveau_bufctx_reset(nv30->bufctx, BUFCTX_FB);
97 if (!--ref)
98 return ref;
99 }
100 }
101 }
102 if (res->bind & PIPE_BIND_DEPTH_STENCIL) {
103 if (nv30->framebuffer.zsbuf.texture == res) {
104 nv30->dirty |= NV30_NEW_FRAMEBUFFER;
105 nouveau_bufctx_reset(nv30->bufctx, BUFCTX_FB);
106 if (!--ref)
107 return ref;
108 }
109 }
110
111 if (res->bind & PIPE_BIND_VERTEX_BUFFER) {
112 for (i = 0; i < nv30->num_vtxbufs; ++i) {
113 if (nv30->vtxbuf[i].buffer.resource == res) {
114 nv30->dirty |= NV30_NEW_ARRAYS;
115 nouveau_bufctx_reset(nv30->bufctx, BUFCTX_VTXBUF);
116 if (!--ref)
117 return ref;
118 }
119 }
120 }
121
122 if (res->bind & PIPE_BIND_SAMPLER_VIEW) {
123 for (i = 0; i < nv30->fragprog.num_textures; ++i) {
124 if (nv30->fragprog.textures[i] &&
125 nv30->fragprog.textures[i]->texture == res) {
126 nv30->dirty |= NV30_NEW_FRAGTEX;
127 nouveau_bufctx_reset(nv30->bufctx, BUFCTX_FRAGTEX(i));
128 if (!--ref)
129 return ref;
130 }
131 }
132 for (i = 0; i < nv30->vertprog.num_textures; ++i) {
133 if (nv30->vertprog.textures[i] &&
134 nv30->vertprog.textures[i]->texture == res) {
135 nv30->dirty |= NV30_NEW_VERTTEX;
136 nouveau_bufctx_reset(nv30->bufctx, BUFCTX_VERTTEX(i));
137 if (!--ref)
138 return ref;
139 }
140 }
141 }
142
143 return ref;
144 }
145
146 static void
147 nv30_context_destroy(struct pipe_context *pipe)
148 {
149 struct nv30_context *nv30 = nv30_context(pipe);
150
151 if (nv30->blitter)
152 util_blitter_destroy(nv30->blitter);
153
154 if (nv30->draw)
155 draw_destroy(nv30->draw);
156
157 if (nv30->base.pipe.stream_uploader)
158 u_upload_destroy(nv30->base.pipe.stream_uploader);
159
160 if (nv30->blit_vp)
161 nouveau_heap_free(&nv30->blit_vp);
162
163 if (nv30->blit_fp)
164 pipe_resource_reference(&nv30->blit_fp, NULL);
165
166 nv30_framebuffer_init(pipe, NULL, nv30->fb_cbufs, &nv30->fb_zsbuf);
167 util_unreference_framebuffer_state(&nv30->framebuffer);
168
169 nouveau_bufctx_del(&nv30->bufctx);
170
171 if (nv30->screen->cur_ctx == nv30)
172 nv30->screen->cur_ctx = NULL;
173
174 nouveau_fence_cleanup(&nv30->base);
175 nouveau_context_destroy(&nv30->base);
176 }
177
178 #define FAIL_CONTEXT_INIT(str, err) \
179 do { \
180 NOUVEAU_ERR(str, err); \
181 nv30_context_destroy(pipe); \
182 return NULL; \
183 } while(0)
184
185 struct pipe_context *
186 nv30_context_create(struct pipe_screen *pscreen, void *priv, unsigned ctxflags)
187 {
188 struct nv30_screen *screen = nv30_screen(pscreen);
189 struct nv30_context *nv30 = CALLOC_STRUCT(nv30_context);
190 struct pipe_context *pipe;
191 int ret;
192
193 if (!nv30)
194 return NULL;
195
196 nv30->screen = screen;
197 nv30->base.copy_data = nv30_transfer_copy_data;
198
199 pipe = &nv30->base.pipe;
200 pipe->screen = pscreen;
201 pipe->priv = priv;
202 pipe->destroy = nv30_context_destroy;
203 pipe->flush = nv30_context_flush;
204
205 if (nouveau_context_init(&nv30->base, &screen->base)) {
206 nv30_context_destroy(pipe);
207 return NULL;
208 }
209 nv30->base.pushbuf->kick_notify = nv30_context_kick_notify;
210
211 nv30->base.pipe.stream_uploader = u_upload_create_default(&nv30->base.pipe);
212 if (!nv30->base.pipe.stream_uploader) {
213 nv30_context_destroy(pipe);
214 return NULL;
215 }
216 nv30->base.pipe.const_uploader = nv30->base.pipe.stream_uploader;
217
218 nv30->base.invalidate_resource_storage = nv30_invalidate_resource_storage;
219
220 ret = nouveau_bufctx_new(nv30->base.client, 64, &nv30->bufctx);
221 if (ret) {
222 nv30_context_destroy(pipe);
223 return NULL;
224 }
225
226 /*XXX: make configurable with performance vs quality, these defaults
227 * match the binary driver's defaults
228 */
229 if (screen->eng3d->oclass < NV40_3D_CLASS)
230 nv30->config.filter = 0x00000004;
231 else
232 nv30->config.filter = 0x00002dc4;
233
234 nv30->config.aniso = NV40_3D_TEX_WRAP_ANISO_MIP_FILTER_OPTIMIZATION_OFF;
235
236 if (debug_get_bool_option("NV30_SWTNL", false))
237 nv30->draw_flags |= NV30_NEW_SWTNL;
238
239 nv30->sample_mask = 0xffff;
240 nv30_vbo_init(pipe);
241 nv30_query_init(pipe);
242 nv30_state_init(pipe);
243 nv30_resource_init(pipe);
244 nv30_clear_init(pipe);
245 nv30_fragprog_init(pipe);
246 nv30_vertprog_init(pipe);
247 nv30_texture_init(pipe);
248 nv30_fragtex_init(pipe);
249 nv40_verttex_init(pipe);
250 nv30_draw_init(pipe);
251
252 nv30->blitter = util_blitter_create(pipe);
253 if (!nv30->blitter) {
254 nv30_context_destroy(pipe);
255 return NULL;
256 }
257
258 nouveau_context_init_vdec(&nv30->base);
259 nouveau_fence_new(&nv30->base, &nv30->base.fence);
260
261 return pipe;
262 }