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 "nv04_3d.xml.h"
32 #include "nv04_driver.h"
33 #include "util/u_memory.h"
34
35 static GLboolean
texunit_needs_combiners(struct gl_texture_unit * u,struct gl_fixedfunc_texture_unit * f)36 texunit_needs_combiners(struct gl_texture_unit *u,
37 struct gl_fixedfunc_texture_unit *f)
38 {
39 struct gl_texture_object *t = u->_Current;
40 struct gl_texture_image *ti = t->Image[0][t->Attrib.BaseLevel];
41
42 return ti->TexFormat == MESA_FORMAT_A_UNORM8 ||
43 ti->TexFormat == MESA_FORMAT_L_UNORM8 ||
44 f->EnvMode == GL_COMBINE ||
45 f->EnvMode == GL_COMBINE4_NV ||
46 f->EnvMode == GL_BLEND ||
47 f->EnvMode == GL_ADD;
48 }
49
50 struct nouveau_object *
nv04_context_engine(struct gl_context * ctx)51 nv04_context_engine(struct gl_context *ctx)
52 {
53 struct nv04_context *nctx = to_nv04_context(ctx);
54 struct nouveau_hw_state *hw = &to_nouveau_context(ctx)->hw;
55 struct nouveau_pushbuf *push = context_push(ctx);
56 struct nouveau_object *fahrenheit;
57
58 if ((ctx->Texture.Unit[0]._Current &&
59 texunit_needs_combiners(&ctx->Texture.Unit[0],
60 &ctx->Texture.FixedFuncUnit[0])) ||
61 ctx->Texture.Unit[1]._Current ||
62 ctx->Stencil.Enabled ||
63 GET_COLORMASK(ctx->Color.ColorMask, 0) != 0xf)
64 fahrenheit = hw->eng3dm;
65 else
66 fahrenheit = hw->eng3d;
67
68 if (fahrenheit != nctx->eng3d) {
69 BEGIN_NV04(push, NV01_SUBC(3D, OBJECT), 1);
70 PUSH_DATA (push, fahrenheit->handle);
71 nctx->eng3d = fahrenheit;
72 }
73
74 return fahrenheit;
75 }
76
77 static void
nv04_hwctx_init(struct gl_context * ctx)78 nv04_hwctx_init(struct gl_context *ctx)
79 {
80 struct nouveau_hw_state *hw = &to_nouveau_context(ctx)->hw;
81 struct nouveau_pushbuf *push = context_push(ctx);
82 struct nv04_fifo *fifo = hw->chan->data;
83
84 BEGIN_NV04(push, NV01_SUBC(SURF, OBJECT), 1);
85 PUSH_DATA (push, hw->surf3d->handle);
86 BEGIN_NV04(push, NV04_SF3D(DMA_NOTIFY), 3);
87 PUSH_DATA (push, hw->ntfy->handle);
88 PUSH_DATA (push, fifo->vram);
89 PUSH_DATA (push, fifo->vram);
90
91 BEGIN_NV04(push, NV01_SUBC(3D, OBJECT), 1);
92 PUSH_DATA (push, hw->eng3d->handle);
93 BEGIN_NV04(push, NV04_TTRI(DMA_NOTIFY), 4);
94 PUSH_DATA (push, hw->ntfy->handle);
95 PUSH_DATA (push, fifo->vram);
96 PUSH_DATA (push, fifo->gart);
97 PUSH_DATA (push, hw->surf3d->handle);
98
99 BEGIN_NV04(push, NV01_SUBC(3D, OBJECT), 1);
100 PUSH_DATA (push, hw->eng3dm->handle);
101 BEGIN_NV04(push, NV04_MTRI(DMA_NOTIFY), 4);
102 PUSH_DATA (push, hw->ntfy->handle);
103 PUSH_DATA (push, fifo->vram);
104 PUSH_DATA (push, fifo->gart);
105 PUSH_DATA (push, hw->surf3d->handle);
106
107 PUSH_KICK (push);
108 }
109
110 static void
init_dummy_texture(struct gl_context * ctx)111 init_dummy_texture(struct gl_context *ctx)
112 {
113 struct nouveau_surface *s = &to_nv04_context(ctx)->dummy_texture;
114
115 nouveau_surface_alloc(ctx, s, SWIZZLED,
116 NOUVEAU_BO_MAP | NOUVEAU_BO_VRAM,
117 MESA_FORMAT_B8G8R8A8_UNORM, 1, 1);
118
119 nouveau_bo_map(s->bo, NOUVEAU_BO_WR, context_client(ctx));
120 *(uint32_t *)s->bo->map = 0xffffffff;
121 }
122
123 static void
nv04_context_destroy(struct gl_context * ctx)124 nv04_context_destroy(struct gl_context *ctx)
125 {
126 struct nouveau_context *nctx = to_nouveau_context(ctx);
127
128 nv04_surface_takedown(ctx);
129 nv04_render_destroy(ctx);
130 nouveau_surface_ref(NULL, &to_nv04_context(ctx)->dummy_texture);
131
132 nouveau_object_del(&nctx->hw.eng3d);
133 nouveau_object_del(&nctx->hw.eng3dm);
134 nouveau_object_del(&nctx->hw.surf3d);
135
136 nouveau_context_deinit(ctx);
137 align_free(ctx);
138 }
139
140 static struct gl_context *
nv04_context_create(struct nouveau_screen * screen,gl_api api,const struct gl_config * visual,struct gl_context * share_ctx)141 nv04_context_create(struct nouveau_screen *screen, gl_api api,
142 const struct gl_config *visual,
143 struct gl_context *share_ctx)
144 {
145 struct nv04_context *nctx;
146 struct nouveau_hw_state *hw;
147 struct gl_context *ctx;
148 int ret;
149
150 nctx = align_calloc(sizeof(struct nv04_context), 16);
151 if (!nctx)
152 return NULL;
153
154 ctx = &nctx->base.base;
155 hw = &nctx->base.hw;
156
157 if (!nouveau_context_init(ctx, api, screen, visual, share_ctx))
158 goto fail;
159
160 /* GL constants. */
161 ctx->Const.MaxTextureSize = 1024;
162 ctx->Const.MaxTextureCoordUnits = NV04_TEXTURE_UNITS;
163 ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxTextureImageUnits = NV04_TEXTURE_UNITS;
164 ctx->Const.MaxTextureUnits = NV04_TEXTURE_UNITS;
165 ctx->Const.MaxTextureMaxAnisotropy = 2;
166 ctx->Const.MaxTextureLodBias = 15;
167
168 /* 2D engine. */
169 ret = nv04_surface_init(ctx);
170 if (!ret)
171 goto fail;
172
173 /* 3D engine. */
174 ret = nouveau_object_new(context_chan(ctx), 0xbeef0001,
175 NV04_TEXTURED_TRIANGLE_CLASS, NULL, 0,
176 &hw->eng3d);
177 if (ret)
178 goto fail;
179
180 ret = nouveau_object_new(context_chan(ctx), 0xbeef0002,
181 NV04_MULTITEX_TRIANGLE_CLASS, NULL, 0,
182 &hw->eng3dm);
183 if (ret)
184 goto fail;
185
186 ret = nouveau_object_new(context_chan(ctx), 0xbeef0003,
187 NV04_SURFACE_3D_CLASS, NULL, 0,
188 &hw->surf3d);
189 if (ret)
190 goto fail;
191
192 init_dummy_texture(ctx);
193 nv04_hwctx_init(ctx);
194 nv04_render_init(ctx);
195
196 return ctx;
197
198 fail:
199 nv04_context_destroy(ctx);
200 return NULL;
201 }
202
203 const struct nouveau_driver nv04_driver = {
204 .context_create = nv04_context_create,
205 .context_destroy = nv04_context_destroy,
206 .surface_copy = nv04_surface_copy,
207 .surface_fill = nv04_surface_fill,
208 .emit = (nouveau_state_func[]) {
209 nv04_defer_control,
210 nouveau_emit_nothing,
211 nv04_defer_blend,
212 nv04_defer_blend,
213 nouveau_emit_nothing,
214 nouveau_emit_nothing,
215 nouveau_emit_nothing,
216 nouveau_emit_nothing,
217 nouveau_emit_nothing,
218 nouveau_emit_nothing,
219 nv04_defer_control,
220 nouveau_emit_nothing,
221 nv04_defer_control,
222 nouveau_emit_nothing,
223 nv04_defer_control,
224 nv04_defer_control,
225 nouveau_emit_nothing,
226 nv04_emit_framebuffer,
227 nv04_defer_blend,
228 nouveau_emit_nothing,
229 nouveau_emit_nothing,
230 nouveau_emit_nothing,
231 nouveau_emit_nothing,
232 nouveau_emit_nothing,
233 nouveau_emit_nothing,
234 nouveau_emit_nothing,
235 nouveau_emit_nothing,
236 nouveau_emit_nothing,
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 nv04_emit_scissor,
258 nv04_defer_blend,
259 nv04_defer_control,
260 nv04_defer_control,
261 nv04_defer_control,
262 nv04_emit_tex_env,
263 nv04_emit_tex_env,
264 nouveau_emit_nothing,
265 nouveau_emit_nothing,
266 nouveau_emit_nothing,
267 nouveau_emit_nothing,
268 nouveau_emit_nothing,
269 nouveau_emit_nothing,
270 nouveau_emit_nothing,
271 nouveau_emit_nothing,
272 nouveau_emit_nothing,
273 nouveau_emit_nothing,
274 nv04_emit_tex_obj,
275 nv04_emit_tex_obj,
276 nouveau_emit_nothing,
277 nouveau_emit_nothing,
278 nouveau_emit_nothing,
279 nv04_emit_blend,
280 nv04_emit_control,
281 },
282 .num_emit = NUM_NV04_STATE,
283 };
284