dri/nouveau: Fix nv[12]x color sum.
[mesa.git] / src / mesa / drivers / dri / nouveau / nv20_state_tnl.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_gldefs.h"
30 #include "nouveau_util.h"
31 #include "nouveau_class.h"
32 #include "nv10_driver.h"
33 #include "nv20_driver.h"
34
35 void
36 nv20_emit_clip_plane(GLcontext *ctx, int emit)
37 {
38 }
39
40 static inline unsigned
41 get_material_bitmask(unsigned m)
42 {
43 unsigned ret = 0;
44
45 if (m & MAT_BIT_FRONT_EMISSION)
46 ret |= NV20TCL_COLOR_MATERIAL_FRONT_EMISSION_COL1;
47 if (m & MAT_BIT_FRONT_AMBIENT)
48 ret |= NV20TCL_COLOR_MATERIAL_FRONT_AMBIENT_COL1;
49 if (m & MAT_BIT_FRONT_DIFFUSE)
50 ret |= NV20TCL_COLOR_MATERIAL_FRONT_DIFFUSE_COL1;
51 if (m & MAT_BIT_FRONT_SPECULAR)
52 ret |= NV20TCL_COLOR_MATERIAL_FRONT_SPECULAR_COL1;
53
54 if (m & MAT_BIT_BACK_EMISSION)
55 ret |= NV20TCL_COLOR_MATERIAL_BACK_EMISSION_COL1;
56 if (m & MAT_BIT_BACK_AMBIENT)
57 ret |= NV20TCL_COLOR_MATERIAL_BACK_AMBIENT_COL1;
58 if (m & MAT_BIT_BACK_DIFFUSE)
59 ret |= NV20TCL_COLOR_MATERIAL_BACK_DIFFUSE_COL1;
60 if (m & MAT_BIT_BACK_SPECULAR)
61 ret |= NV20TCL_COLOR_MATERIAL_BACK_SPECULAR_COL1;
62
63 return ret;
64 }
65
66 void
67 nv20_emit_color_material(GLcontext *ctx, int emit)
68 {
69 struct nouveau_channel *chan = context_chan(ctx);
70 struct nouveau_grobj *kelvin = context_eng3d(ctx);
71 unsigned mask = get_material_bitmask(ctx->Light.ColorMaterialBitmask);
72
73 BEGIN_RING(chan, kelvin, NV20TCL_COLOR_MATERIAL, 1);
74 OUT_RING(chan, ctx->Light.ColorMaterialEnabled ? mask : 0);
75 }
76
77 static unsigned
78 get_fog_mode_signed(unsigned mode)
79 {
80 switch (mode) {
81 case GL_LINEAR:
82 return NV20TCL_FOG_MODE_LINEAR_SIGNED;
83 case GL_EXP:
84 return NV20TCL_FOG_MODE_EXP_SIGNED;
85 case GL_EXP2:
86 return NV20TCL_FOG_MODE_EXP2_SIGNED;
87 default:
88 assert(0);
89 }
90 }
91
92 static unsigned
93 get_fog_mode_unsigned(unsigned mode)
94 {
95 switch (mode) {
96 case GL_LINEAR:
97 return NV20TCL_FOG_MODE_LINEAR_UNSIGNED;
98 case GL_EXP:
99 return NV20TCL_FOG_MODE_EXP_UNSIGNED;
100 case GL_EXP2:
101 return NV20TCL_FOG_MODE_EXP2_UNSIGNED;
102 default:
103 assert(0);
104 }
105 }
106
107 static unsigned
108 get_fog_source(unsigned source)
109 {
110 switch (source) {
111 case GL_FOG_COORDINATE_EXT:
112 return NV20TCL_FOG_COORD_FOG;
113 case GL_FRAGMENT_DEPTH_EXT:
114 return NV20TCL_FOG_COORD_DIST_ORTHOGONAL_ABS;
115 default:
116 assert(0);
117 }
118 }
119
120 void
121 nv20_emit_fog(GLcontext *ctx, int emit)
122 {
123 struct nouveau_context *nctx = to_nouveau_context(ctx);
124 struct nouveau_channel *chan = context_chan(ctx);
125 struct nouveau_grobj *kelvin = context_eng3d(ctx);
126 struct gl_fog_attrib *f = &ctx->Fog;
127 unsigned source = nctx->fallback == HWTNL ?
128 f->FogCoordinateSource : GL_FOG_COORDINATE_EXT;
129 float k[3];
130
131 nv10_get_fog_coeff(ctx, k);
132
133 BEGIN_RING(chan, kelvin, NV20TCL_FOG_MODE, 4);
134 OUT_RING(chan, (source == GL_FOG_COORDINATE_EXT ?
135 get_fog_mode_signed(f->Mode) :
136 get_fog_mode_unsigned(f->Mode)));
137 OUT_RING(chan, get_fog_source(source));
138 OUT_RING(chan, f->Enabled ? 1 : 0);
139 OUT_RING(chan, pack_rgba_f(MESA_FORMAT_RGBA8888_REV, f->Color));
140
141 BEGIN_RING(chan, kelvin, NV20TCL_FOG_EQUATION_CONSTANT, 3);
142 OUT_RINGf(chan, k[0]);
143 OUT_RINGf(chan, k[1]);
144 OUT_RINGf(chan, k[2]);
145 }
146
147 void
148 nv20_emit_light_model(GLcontext *ctx, int emit)
149 {
150 struct nouveau_channel *chan = context_chan(ctx);
151 struct nouveau_grobj *kelvin = context_eng3d(ctx);
152 struct gl_lightmodel *m = &ctx->Light.Model;
153
154 BEGIN_RING(chan, kelvin, NV20TCL_SEPARATE_SPECULAR_ENABLE, 1);
155 OUT_RING(chan, m->ColorControl == GL_SEPARATE_SPECULAR_COLOR ? 1 : 0);
156
157 BEGIN_RING(chan, kelvin, NV20TCL_LIGHT_MODEL, 1);
158 OUT_RING(chan, ((m->LocalViewer ?
159 NV20TCL_LIGHT_MODEL_VIEWER_LOCAL :
160 NV20TCL_LIGHT_MODEL_VIEWER_NONLOCAL) |
161 (NEED_SECONDARY_COLOR(ctx) ?
162 NV20TCL_LIGHT_MODEL_SEPARATE_SPECULAR :
163 0)));
164
165 BEGIN_RING(chan, kelvin, NV20TCL_LIGHT_MODEL_TWO_SIDE_ENABLE, 1);
166 OUT_RING(chan, ctx->Light.Model.TwoSide ? 1 : 0);
167 }
168
169 void
170 nv20_emit_light_source(GLcontext *ctx, int emit)
171 {
172 const int i = emit - NOUVEAU_STATE_LIGHT_SOURCE0;
173 struct nouveau_channel *chan = context_chan(ctx);
174 struct nouveau_grobj *kelvin = context_eng3d(ctx);
175 struct gl_light *l = &ctx->Light.Light[i];
176
177 if (l->_Flags & LIGHT_POSITIONAL) {
178 BEGIN_RING(chan, kelvin, NV20TCL_LIGHT_POSITION_X(i), 3);
179 OUT_RINGf(chan, l->_Position[0]);
180 OUT_RINGf(chan, l->_Position[1]);
181 OUT_RINGf(chan, l->_Position[2]);
182
183 BEGIN_RING(chan, kelvin, NV20TCL_LIGHT_ATTENUATION_CONSTANT(i), 3);
184 OUT_RINGf(chan, l->ConstantAttenuation);
185 OUT_RINGf(chan, l->LinearAttenuation);
186 OUT_RINGf(chan, l->QuadraticAttenuation);
187
188 } else {
189 BEGIN_RING(chan, kelvin, NV20TCL_LIGHT_DIRECTION_X(i), 3);
190 OUT_RINGf(chan, l->_VP_inf_norm[0]);
191 OUT_RINGf(chan, l->_VP_inf_norm[1]);
192 OUT_RINGf(chan, l->_VP_inf_norm[2]);
193
194 BEGIN_RING(chan, kelvin, NV20TCL_LIGHT_HALF_VECTOR_X(i), 3);
195 OUT_RINGf(chan, l->_h_inf_norm[0]);
196 OUT_RINGf(chan, l->_h_inf_norm[1]);
197 OUT_RINGf(chan, l->_h_inf_norm[2]);
198 }
199
200 if (l->_Flags & LIGHT_SPOT) {
201 float k[7];
202
203 nv10_get_spot_coeff(l, k);
204
205 BEGIN_RING(chan, kelvin, NV20TCL_LIGHT_SPOT_CUTOFF_A(i), 7);
206 OUT_RINGf(chan, k[0]);
207 OUT_RINGf(chan, k[1]);
208 OUT_RINGf(chan, k[2]);
209 OUT_RINGf(chan, k[3]);
210 OUT_RINGf(chan, k[4]);
211 OUT_RINGf(chan, k[5]);
212 OUT_RINGf(chan, k[6]);
213 }
214 }
215
216 #define USE_COLOR_MATERIAL(attr, side) \
217 (ctx->Light.ColorMaterialEnabled && \
218 ctx->Light.ColorMaterialBitmask & (1 << MAT_ATTRIB_##attr(side)))
219
220 void
221 nv20_emit_material_ambient(GLcontext *ctx, int emit)
222 {
223 const int side = emit - NOUVEAU_STATE_MATERIAL_FRONT_AMBIENT;
224 struct nouveau_channel *chan = context_chan(ctx);
225 struct nouveau_grobj *kelvin = context_eng3d(ctx);
226 float (*mat)[4] = ctx->Light.Material.Attrib;
227 uint32_t m_scene[] = { NV20TCL_LIGHT_MODEL_FRONT_AMBIENT_R,
228 NV20TCL_LIGHT_MODEL_BACK_AMBIENT_R };
229 uint32_t m_factor[] = { NV20TCL_MATERIAL_FACTOR_FRONT_R,
230 NV20TCL_MATERIAL_FACTOR_BACK_R };
231 float c_scene[3], c_factor[3];
232 struct gl_light *l;
233
234 if (USE_COLOR_MATERIAL(AMBIENT, side)) {
235 COPY_3V(c_scene, mat[MAT_ATTRIB_EMISSION(side)]);
236 COPY_3V(c_factor, ctx->Light.Model.Ambient);
237
238 } else if (USE_COLOR_MATERIAL(EMISSION, side)) {
239 SCALE_3V(c_scene, mat[MAT_ATTRIB_AMBIENT(side)],
240 ctx->Light.Model.Ambient);
241 ASSIGN_3V(c_factor, 1, 1, 1);
242
243 } else {
244 COPY_3V(c_scene, ctx->Light._BaseColor[side]);
245 ZERO_3V(c_factor);
246 }
247
248 BEGIN_RING(chan, kelvin, m_scene[side], 3);
249 OUT_RINGf(chan, c_scene[0]);
250 OUT_RINGf(chan, c_scene[1]);
251 OUT_RINGf(chan, c_scene[2]);
252
253 if (ctx->Light.ColorMaterialEnabled) {
254 BEGIN_RING(chan, kelvin, m_factor[side], 3);
255 OUT_RINGf(chan, c_factor[0]);
256 OUT_RINGf(chan, c_factor[1]);
257 OUT_RINGf(chan, c_factor[2]);
258 }
259
260 foreach(l, &ctx->Light.EnabledList) {
261 const int i = l - ctx->Light.Light;
262 uint32_t m_light[] = { NV20TCL_LIGHT_FRONT_AMBIENT_R(i),
263 NV20TCL_LIGHT_BACK_AMBIENT_R(i) };
264 float *c_light = (USE_COLOR_MATERIAL(AMBIENT, side) ?
265 l->Ambient :
266 l->_MatAmbient[side]);
267
268 BEGIN_RING(chan, kelvin, m_light[side], 3);
269 OUT_RINGf(chan, c_light[0]);
270 OUT_RINGf(chan, c_light[1]);
271 OUT_RINGf(chan, c_light[2]);
272 }
273 }
274
275 void
276 nv20_emit_material_diffuse(GLcontext *ctx, int emit)
277 {
278 const int side = emit - NOUVEAU_STATE_MATERIAL_FRONT_DIFFUSE;
279 struct nouveau_channel *chan = context_chan(ctx);
280 struct nouveau_grobj *kelvin = context_eng3d(ctx);
281 GLfloat (*mat)[4] = ctx->Light.Material.Attrib;
282 uint32_t m_factor[] = { NV20TCL_MATERIAL_FACTOR_FRONT_A,
283 NV20TCL_MATERIAL_FACTOR_BACK_A };
284 struct gl_light *l;
285
286 BEGIN_RING(chan, kelvin, m_factor[side], 1);
287 OUT_RINGf(chan, mat[MAT_ATTRIB_DIFFUSE(side)][3]);
288
289 foreach(l, &ctx->Light.EnabledList) {
290 const int i = l - ctx->Light.Light;
291 uint32_t m_light[] = { NV20TCL_LIGHT_FRONT_DIFFUSE_R(i),
292 NV20TCL_LIGHT_BACK_DIFFUSE_R(i) };
293 float *c_light = (USE_COLOR_MATERIAL(DIFFUSE, side) ?
294 l->Diffuse :
295 l->_MatDiffuse[side]);
296
297 BEGIN_RING(chan, kelvin, m_light[side], 3);
298 OUT_RINGf(chan, c_light[0]);
299 OUT_RINGf(chan, c_light[1]);
300 OUT_RINGf(chan, c_light[2]);
301 }
302 }
303
304 void
305 nv20_emit_material_specular(GLcontext *ctx, int emit)
306 {
307 const int side = emit - NOUVEAU_STATE_MATERIAL_FRONT_SPECULAR;
308 struct nouveau_channel *chan = context_chan(ctx);
309 struct nouveau_grobj *kelvin = context_eng3d(ctx);
310 struct gl_light *l;
311
312 foreach(l, &ctx->Light.EnabledList) {
313 const int i = l - ctx->Light.Light;
314 uint32_t m_light[] = { NV20TCL_LIGHT_FRONT_SPECULAR_R(i),
315 NV20TCL_LIGHT_BACK_SPECULAR_R(i) };
316 float *c_light = (USE_COLOR_MATERIAL(SPECULAR, side) ?
317 l->Specular :
318 l->_MatSpecular[side]);
319
320 BEGIN_RING(chan, kelvin, m_light[side], 3);
321 OUT_RINGf(chan, c_light[0]);
322 OUT_RINGf(chan, c_light[1]);
323 OUT_RINGf(chan, c_light[2]);
324 }
325 }
326
327 void
328 nv20_emit_material_shininess(GLcontext *ctx, int emit)
329 {
330 const int side = emit - NOUVEAU_STATE_MATERIAL_FRONT_SHININESS;
331 struct nouveau_channel *chan = context_chan(ctx);
332 struct nouveau_grobj *kelvin = context_eng3d(ctx);
333 float (*mat)[4] = ctx->Light.Material.Attrib;
334 uint32_t mthd[] = { NV20TCL_FRONT_MATERIAL_SHININESS(0),
335 NV20TCL_BACK_MATERIAL_SHININESS(0) };
336 float k[6];
337
338 nv10_get_shininess_coeff(
339 CLAMP(mat[MAT_ATTRIB_SHININESS(side)][0], 0, 1024),
340 k);
341
342 BEGIN_RING(chan, kelvin, mthd[side], 6);
343 OUT_RINGf(chan, k[0]);
344 OUT_RINGf(chan, k[1]);
345 OUT_RINGf(chan, k[2]);
346 OUT_RINGf(chan, k[3]);
347 OUT_RINGf(chan, k[4]);
348 OUT_RINGf(chan, k[5]);
349 }
350
351 void
352 nv20_emit_modelview(GLcontext *ctx, int emit)
353 {
354 struct nouveau_context *nctx = to_nouveau_context(ctx);
355 struct nouveau_channel *chan = context_chan(ctx);
356 struct nouveau_grobj *kelvin = context_eng3d(ctx);
357 GLmatrix *m = ctx->ModelviewMatrixStack.Top;
358
359 if (nctx->fallback != HWTNL)
360 return;
361
362 if (ctx->Light._NeedEyeCoords || ctx->Fog.Enabled) {
363 BEGIN_RING(chan, kelvin, NV20TCL_MODELVIEW0_MATRIX(0), 16);
364 OUT_RINGm(chan, m->m);
365 }
366
367 if (ctx->Light.Enabled) {
368 int i, j;
369
370 BEGIN_RING(chan, kelvin,
371 NV20TCL_INVERSE_MODELVIEW0_MATRIX(0), 12);
372 for (i = 0; i < 3; i++)
373 for (j = 0; j < 4; j++)
374 OUT_RINGf(chan, m->inv[4*i + j]);
375 }
376 }
377
378 void
379 nv20_emit_projection(GLcontext *ctx, int emit)
380 {
381 struct nouveau_context *nctx = to_nouveau_context(ctx);
382 struct nouveau_channel *chan = context_chan(ctx);
383 struct nouveau_grobj *kelvin = context_eng3d(ctx);
384 GLmatrix m;
385
386 _math_matrix_ctr(&m);
387 get_viewport_scale(ctx, m.m);
388
389 if (nctx->fallback == HWTNL)
390 _math_matrix_mul_matrix(&m, &m, &ctx->_ModelProjectMatrix);
391
392 BEGIN_RING(chan, kelvin, NV20TCL_PROJECTION_MATRIX(0), 16);
393 OUT_RINGm(chan, m.m);
394
395 _math_matrix_dtr(&m);
396 }