Merge branch '7.8'
[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_BLEND ||
43 ctx->Texture.Unit[0].EnvMode == GL_ADD ||
44 ctx->Texture.Unit[1]._ReallyEnabled ||
45 ctx->Stencil.Enabled)
46 fahrenheit = hw->eng3dm;
47 else
48 fahrenheit = hw->eng3d;
49
50 if (fahrenheit != nctx->eng3d) {
51 nctx->eng3d = fahrenheit;
52
53 if (nv04_mtex_engine(fahrenheit)) {
54 context_dirty_i(ctx, TEX_ENV, 0);
55 context_dirty_i(ctx, TEX_ENV, 1);
56 context_dirty_i(ctx, TEX_OBJ, 0);
57 context_dirty_i(ctx, TEX_OBJ, 1);
58 context_dirty(ctx, CONTROL);
59 context_dirty(ctx, BLEND);
60 } else {
61 context_bctx_i(ctx, TEXTURE, 1);
62 context_dirty_i(ctx, TEX_ENV, 0);
63 context_dirty_i(ctx, TEX_OBJ, 0);
64 context_dirty(ctx, CONTROL);
65 context_dirty(ctx, BLEND);
66 }
67 }
68
69 return fahrenheit;
70 }
71
72 static void
73 nv04_channel_flush_notify(struct nouveau_channel *chan)
74 {
75 struct nouveau_context *nctx = chan->user_private;
76 GLcontext *ctx = &nctx->base;
77
78 if (nctx->fallback < SWRAST) {
79 /* Flushing seems to clobber the engine context. */
80 context_emit(ctx, TEX_OBJ0);
81 context_emit(ctx, TEX_OBJ1);
82 context_emit(ctx, TEX_ENV0);
83 context_emit(ctx, TEX_ENV1);
84 context_emit(ctx, CONTROL);
85 context_emit(ctx, BLEND);
86
87 nouveau_bo_state_emit(ctx);
88 }
89 }
90
91 static void
92 nv04_hwctx_init(GLcontext *ctx)
93 {
94 struct nouveau_channel *chan = context_chan(ctx);
95 struct nouveau_hw_state *hw = &to_nouveau_context(ctx)->hw;
96 struct nouveau_grobj *surf3d = hw->surf3d;
97 struct nouveau_grobj *eng3d = hw->eng3d;
98 struct nouveau_grobj *eng3dm = hw->eng3dm;
99
100 BIND_RING(chan, surf3d, 7);
101 BEGIN_RING(chan, surf3d, NV04_CONTEXT_SURFACES_3D_DMA_NOTIFY, 3);
102 OUT_RING(chan, hw->ntfy->handle);
103 OUT_RING(chan, chan->vram->handle);
104 OUT_RING(chan, chan->vram->handle);
105
106 BEGIN_RING(chan, eng3d, NV04_TEXTURED_TRIANGLE_DMA_NOTIFY, 4);
107 OUT_RING(chan, hw->ntfy->handle);
108 OUT_RING(chan, chan->vram->handle);
109 OUT_RING(chan, chan->gart->handle);
110 OUT_RING(chan, surf3d->handle);
111
112 BEGIN_RING(chan, eng3dm, NV04_MULTITEX_TRIANGLE_DMA_NOTIFY, 4);
113 OUT_RING(chan, hw->ntfy->handle);
114 OUT_RING(chan, chan->vram->handle);
115 OUT_RING(chan, chan->gart->handle);
116 OUT_RING(chan, surf3d->handle);
117
118 FIRE_RING(chan);
119 }
120
121 static void
122 init_dummy_texture(GLcontext *ctx)
123 {
124 struct nouveau_surface *s = &to_nv04_context(ctx)->dummy_texture;
125
126 nouveau_surface_alloc(ctx, s, SWIZZLED,
127 NOUVEAU_BO_MAP | NOUVEAU_BO_VRAM,
128 MESA_FORMAT_ARGB8888, 1, 1);
129
130 nouveau_bo_map(s->bo, NOUVEAU_BO_WR);
131 *(uint32_t *)s->bo->map = 0xffffffff;
132 nouveau_bo_unmap(s->bo);
133 }
134
135 static void
136 nv04_context_destroy(GLcontext *ctx)
137 {
138 struct nouveau_context *nctx = to_nouveau_context(ctx);
139
140 nv04_surface_takedown(ctx);
141 nv04_render_destroy(ctx);
142 nouveau_surface_ref(NULL, &to_nv04_context(ctx)->dummy_texture);
143
144 nouveau_grobj_free(&nctx->hw.eng3d);
145 nouveau_grobj_free(&nctx->hw.eng3dm);
146 nouveau_grobj_free(&nctx->hw.surf3d);
147
148 nouveau_context_deinit(ctx);
149 FREE(ctx);
150 }
151
152 static GLcontext *
153 nv04_context_create(struct nouveau_screen *screen, const GLvisual *visual,
154 GLcontext *share_ctx)
155 {
156 struct nv04_context *nctx;
157 struct nouveau_hw_state *hw;
158 GLcontext *ctx;
159 int ret;
160
161 nctx = CALLOC_STRUCT(nv04_context);
162 if (!nctx)
163 return NULL;
164
165 ctx = &nctx->base.base;
166 hw = &nctx->base.hw;
167
168 if (!nouveau_context_init(ctx, screen, visual, share_ctx))
169 goto fail;
170
171 hw->chan->flush_notify = nv04_channel_flush_notify;
172
173 /* GL constants. */
174 ctx->Const.MaxTextureCoordUnits = NV04_TEXTURE_UNITS;
175 ctx->Const.MaxTextureImageUnits = NV04_TEXTURE_UNITS;
176 ctx->Const.MaxTextureUnits = NV04_TEXTURE_UNITS;
177 ctx->Const.MaxTextureMaxAnisotropy = 2;
178 ctx->Const.MaxTextureLodBias = 15;
179
180 /* 2D engine. */
181 ret = nv04_surface_init(ctx);
182 if (!ret)
183 goto fail;
184
185 /* 3D engine. */
186 ret = nouveau_grobj_alloc(context_chan(ctx), 0xbeef0001,
187 NV04_TEXTURED_TRIANGLE, &hw->eng3d);
188 if (ret)
189 goto fail;
190
191 ret = nouveau_grobj_alloc(context_chan(ctx), 0xbeef0002,
192 NV04_MULTITEX_TRIANGLE, &hw->eng3dm);
193 if (ret)
194 goto fail;
195
196 ret = nouveau_grobj_alloc(context_chan(ctx), 0xbeef0003,
197 NV04_CONTEXT_SURFACES_3D, &hw->surf3d);
198 if (ret)
199 goto fail;
200
201 init_dummy_texture(ctx);
202 nv04_hwctx_init(ctx);
203 nv04_render_init(ctx);
204
205 return ctx;
206
207 fail:
208 nv04_context_destroy(ctx);
209 return NULL;
210 }
211
212 const struct nouveau_driver nv04_driver = {
213 .context_create = nv04_context_create,
214 .context_destroy = nv04_context_destroy,
215 .surface_copy = nv04_surface_copy,
216 .surface_fill = nv04_surface_fill,
217 .emit = (nouveau_state_func[]) {
218 nv04_defer_control,
219 nouveau_emit_nothing,
220 nv04_defer_blend,
221 nv04_defer_blend,
222 nouveau_emit_nothing,
223 nouveau_emit_nothing,
224 nouveau_emit_nothing,
225 nouveau_emit_nothing,
226 nouveau_emit_nothing,
227 nouveau_emit_nothing,
228 nv04_defer_control,
229 nouveau_emit_nothing,
230 nv04_defer_control,
231 nouveau_emit_nothing,
232 nv04_defer_control,
233 nv04_defer_control,
234 nouveau_emit_nothing,
235 nv04_emit_framebuffer,
236 nv04_defer_blend,
237 nouveau_emit_nothing,
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 nv04_emit_scissor,
267 nv04_defer_blend,
268 nv04_defer_control,
269 nv04_defer_control,
270 nv04_defer_control,
271 nv04_emit_tex_env,
272 nv04_emit_tex_env,
273 nouveau_emit_nothing,
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 nv04_emit_tex_obj,
284 nv04_emit_tex_obj,
285 nouveau_emit_nothing,
286 nouveau_emit_nothing,
287 nouveau_emit_nothing,
288 nv04_emit_blend,
289 nv04_emit_control,
290 },
291 .num_emit = NUM_NV04_STATE,
292 };