Merge remote branch 'origin/nv50-compiler'
[mesa.git] / src / mesa / drivers / dri / nouveau / nv04_context.c
1 /*
2 * Copyright (C) 2009-2010 Francisco Jerez.
3 * All Rights Reserved.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial
15 * portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20 * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
21 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 *
25 */
26
27 #include "nouveau_driver.h"
28 #include "nouveau_context.h"
29 #include "nouveau_fbo.h"
30 #include "nouveau_util.h"
31 #include "nouveau_class.h"
32 #include "nv04_driver.h"
33
34 struct nouveau_grobj *
35 nv04_context_engine(GLcontext *ctx)
36 {
37 struct nv04_context *nctx = to_nv04_context(ctx);
38 struct nouveau_hw_state *hw = &to_nouveau_context(ctx)->hw;
39 struct nouveau_grobj *fahrenheit;
40
41 if (ctx->Texture.Unit[0].EnvMode == GL_COMBINE ||
42 ctx->Texture.Unit[0].EnvMode == GL_COMBINE4_NV ||
43 ctx->Texture.Unit[0].EnvMode == GL_BLEND ||
44 ctx->Texture.Unit[0].EnvMode == GL_ADD ||
45 ctx->Texture.Unit[1]._ReallyEnabled ||
46 ctx->Stencil.Enabled)
47 fahrenheit = hw->eng3dm;
48 else
49 fahrenheit = hw->eng3d;
50
51 if (fahrenheit != nctx->eng3d) {
52 nctx->eng3d = fahrenheit;
53
54 if (nv04_mtex_engine(fahrenheit)) {
55 context_dirty_i(ctx, TEX_ENV, 0);
56 context_dirty_i(ctx, TEX_ENV, 1);
57 context_dirty_i(ctx, TEX_OBJ, 0);
58 context_dirty_i(ctx, TEX_OBJ, 1);
59 context_dirty(ctx, CONTROL);
60 context_dirty(ctx, BLEND);
61 } else {
62 context_bctx_i(ctx, TEXTURE, 1);
63 context_dirty_i(ctx, TEX_ENV, 0);
64 context_dirty_i(ctx, TEX_OBJ, 0);
65 context_dirty(ctx, CONTROL);
66 context_dirty(ctx, BLEND);
67 }
68 }
69
70 return fahrenheit;
71 }
72
73 static void
74 nv04_channel_flush_notify(struct nouveau_channel *chan)
75 {
76 struct nouveau_context *nctx = chan->user_private;
77 GLcontext *ctx = &nctx->base;
78
79 if (nctx->fallback < SWRAST) {
80 /* Flushing seems to clobber the engine context. */
81 context_emit(ctx, TEX_OBJ0);
82 context_emit(ctx, TEX_OBJ1);
83 context_emit(ctx, TEX_ENV0);
84 context_emit(ctx, TEX_ENV1);
85 context_emit(ctx, CONTROL);
86 context_emit(ctx, BLEND);
87
88 nouveau_bo_state_emit(ctx);
89 }
90 }
91
92 static void
93 nv04_hwctx_init(GLcontext *ctx)
94 {
95 struct nouveau_channel *chan = context_chan(ctx);
96 struct nouveau_hw_state *hw = &to_nouveau_context(ctx)->hw;
97 struct nouveau_grobj *surf3d = hw->surf3d;
98 struct nouveau_grobj *eng3d = hw->eng3d;
99 struct nouveau_grobj *eng3dm = hw->eng3dm;
100
101 BIND_RING(chan, surf3d, 7);
102 BEGIN_RING(chan, surf3d, NV04_CONTEXT_SURFACES_3D_DMA_NOTIFY, 3);
103 OUT_RING(chan, hw->ntfy->handle);
104 OUT_RING(chan, chan->vram->handle);
105 OUT_RING(chan, chan->vram->handle);
106
107 BEGIN_RING(chan, eng3d, NV04_TEXTURED_TRIANGLE_DMA_NOTIFY, 4);
108 OUT_RING(chan, hw->ntfy->handle);
109 OUT_RING(chan, chan->vram->handle);
110 OUT_RING(chan, chan->gart->handle);
111 OUT_RING(chan, surf3d->handle);
112
113 BEGIN_RING(chan, eng3dm, NV04_MULTITEX_TRIANGLE_DMA_NOTIFY, 4);
114 OUT_RING(chan, hw->ntfy->handle);
115 OUT_RING(chan, chan->vram->handle);
116 OUT_RING(chan, chan->gart->handle);
117 OUT_RING(chan, surf3d->handle);
118
119 FIRE_RING(chan);
120 }
121
122 static void
123 init_dummy_texture(GLcontext *ctx)
124 {
125 struct nouveau_surface *s = &to_nv04_context(ctx)->dummy_texture;
126
127 nouveau_surface_alloc(ctx, s, SWIZZLED,
128 NOUVEAU_BO_MAP | NOUVEAU_BO_VRAM,
129 MESA_FORMAT_ARGB8888, 1, 1);
130
131 nouveau_bo_map(s->bo, NOUVEAU_BO_WR);
132 *(uint32_t *)s->bo->map = 0xffffffff;
133 nouveau_bo_unmap(s->bo);
134 }
135
136 static void
137 nv04_context_destroy(GLcontext *ctx)
138 {
139 struct nouveau_context *nctx = to_nouveau_context(ctx);
140
141 nv04_surface_takedown(ctx);
142 nv04_render_destroy(ctx);
143 nouveau_surface_ref(NULL, &to_nv04_context(ctx)->dummy_texture);
144
145 nouveau_grobj_free(&nctx->hw.eng3d);
146 nouveau_grobj_free(&nctx->hw.eng3dm);
147 nouveau_grobj_free(&nctx->hw.surf3d);
148
149 nouveau_context_deinit(ctx);
150 FREE(ctx);
151 }
152
153 static GLcontext *
154 nv04_context_create(struct nouveau_screen *screen, const GLvisual *visual,
155 GLcontext *share_ctx)
156 {
157 struct nv04_context *nctx;
158 struct nouveau_hw_state *hw;
159 GLcontext *ctx;
160 int ret;
161
162 nctx = CALLOC_STRUCT(nv04_context);
163 if (!nctx)
164 return NULL;
165
166 ctx = &nctx->base.base;
167 hw = &nctx->base.hw;
168
169 if (!nouveau_context_init(ctx, screen, visual, share_ctx))
170 goto fail;
171
172 hw->chan->flush_notify = nv04_channel_flush_notify;
173
174 /* GL constants. */
175 ctx->Const.MaxTextureCoordUnits = NV04_TEXTURE_UNITS;
176 ctx->Const.MaxTextureImageUnits = NV04_TEXTURE_UNITS;
177 ctx->Const.MaxTextureUnits = NV04_TEXTURE_UNITS;
178 ctx->Const.MaxTextureMaxAnisotropy = 2;
179 ctx->Const.MaxTextureLodBias = 15;
180
181 /* 2D engine. */
182 ret = nv04_surface_init(ctx);
183 if (!ret)
184 goto fail;
185
186 /* 3D engine. */
187 ret = nouveau_grobj_alloc(context_chan(ctx), 0xbeef0001,
188 NV04_TEXTURED_TRIANGLE, &hw->eng3d);
189 if (ret)
190 goto fail;
191
192 ret = nouveau_grobj_alloc(context_chan(ctx), 0xbeef0002,
193 NV04_MULTITEX_TRIANGLE, &hw->eng3dm);
194 if (ret)
195 goto fail;
196
197 ret = nouveau_grobj_alloc(context_chan(ctx), 0xbeef0003,
198 NV04_CONTEXT_SURFACES_3D, &hw->surf3d);
199 if (ret)
200 goto fail;
201
202 init_dummy_texture(ctx);
203 nv04_hwctx_init(ctx);
204 nv04_render_init(ctx);
205
206 return ctx;
207
208 fail:
209 nv04_context_destroy(ctx);
210 return NULL;
211 }
212
213 const struct nouveau_driver nv04_driver = {
214 .context_create = nv04_context_create,
215 .context_destroy = nv04_context_destroy,
216 .surface_copy = nv04_surface_copy,
217 .surface_fill = nv04_surface_fill,
218 .emit = (nouveau_state_func[]) {
219 nv04_defer_control,
220 nouveau_emit_nothing,
221 nv04_defer_blend,
222 nv04_defer_blend,
223 nouveau_emit_nothing,
224 nouveau_emit_nothing,
225 nouveau_emit_nothing,
226 nouveau_emit_nothing,
227 nouveau_emit_nothing,
228 nouveau_emit_nothing,
229 nv04_defer_control,
230 nouveau_emit_nothing,
231 nv04_defer_control,
232 nouveau_emit_nothing,
233 nv04_defer_control,
234 nv04_defer_control,
235 nouveau_emit_nothing,
236 nv04_emit_framebuffer,
237 nv04_defer_blend,
238 nouveau_emit_nothing,
239 nouveau_emit_nothing,
240 nouveau_emit_nothing,
241 nouveau_emit_nothing,
242 nouveau_emit_nothing,
243 nouveau_emit_nothing,
244 nouveau_emit_nothing,
245 nouveau_emit_nothing,
246 nouveau_emit_nothing,
247 nouveau_emit_nothing,
248 nouveau_emit_nothing,
249 nouveau_emit_nothing,
250 nouveau_emit_nothing,
251 nouveau_emit_nothing,
252 nouveau_emit_nothing,
253 nouveau_emit_nothing,
254 nouveau_emit_nothing,
255 nouveau_emit_nothing,
256 nouveau_emit_nothing,
257 nouveau_emit_nothing,
258 nouveau_emit_nothing,
259 nouveau_emit_nothing,
260 nouveau_emit_nothing,
261 nouveau_emit_nothing,
262 nouveau_emit_nothing,
263 nouveau_emit_nothing,
264 nouveau_emit_nothing,
265 nouveau_emit_nothing,
266 nouveau_emit_nothing,
267 nv04_emit_scissor,
268 nv04_defer_blend,
269 nv04_defer_control,
270 nv04_defer_control,
271 nv04_defer_control,
272 nv04_emit_tex_env,
273 nv04_emit_tex_env,
274 nouveau_emit_nothing,
275 nouveau_emit_nothing,
276 nouveau_emit_nothing,
277 nouveau_emit_nothing,
278 nouveau_emit_nothing,
279 nouveau_emit_nothing,
280 nouveau_emit_nothing,
281 nouveau_emit_nothing,
282 nouveau_emit_nothing,
283 nouveau_emit_nothing,
284 nv04_emit_tex_obj,
285 nv04_emit_tex_obj,
286 nouveau_emit_nothing,
287 nouveau_emit_nothing,
288 nouveau_emit_nothing,
289 nv04_emit_blend,
290 nv04_emit_control,
291 },
292 .num_emit = NUM_NV04_STATE,
293 };