Merge branch '7.8' into master
[mesa.git] / src / mesa / drivers / dri / nouveau / nv10_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
34 void
35 nv10_emit_clip_plane(GLcontext *ctx, int emit)
36 {
37 }
38
39 static inline unsigned
40 get_material_bitmask(unsigned m)
41 {
42 unsigned ret = 0;
43
44 if (m & MAT_BIT_FRONT_EMISSION)
45 ret |= NV10TCL_COLOR_MATERIAL_EMISSION;
46 if (m & MAT_BIT_FRONT_AMBIENT)
47 ret |= NV10TCL_COLOR_MATERIAL_AMBIENT;
48 if (m & MAT_BIT_FRONT_DIFFUSE)
49 ret |= NV10TCL_COLOR_MATERIAL_DIFFUSE;
50 if (m & MAT_BIT_FRONT_SPECULAR)
51 ret |= NV10TCL_COLOR_MATERIAL_SPECULAR;
52
53 return ret;
54 }
55
56 void
57 nv10_emit_color_material(GLcontext *ctx, int emit)
58 {
59 struct nouveau_channel *chan = context_chan(ctx);
60 struct nouveau_grobj *celsius = context_eng3d(ctx);
61 unsigned mask = get_material_bitmask(ctx->Light.ColorMaterialBitmask);
62
63 BEGIN_RING(chan, celsius, NV10TCL_COLOR_MATERIAL, 1);
64 OUT_RING(chan, ctx->Light.ColorMaterialEnabled ? mask : 0);
65 }
66
67 static unsigned
68 get_fog_mode(unsigned mode)
69 {
70 switch (mode) {
71 case GL_LINEAR:
72 return NV10TCL_FOG_MODE_LINEAR;
73 case GL_EXP:
74 return NV10TCL_FOG_MODE_EXP;
75 case GL_EXP2:
76 return NV10TCL_FOG_MODE_EXP2;
77 default:
78 assert(0);
79 }
80 }
81
82 static unsigned
83 get_fog_source(unsigned source)
84 {
85 switch (source) {
86 case GL_FOG_COORDINATE_EXT:
87 return NV10TCL_FOG_COORD_FOG;
88 case GL_FRAGMENT_DEPTH_EXT:
89 return NV10TCL_FOG_COORD_DIST_ORTHOGONAL_ABS;
90 default:
91 assert(0);
92 }
93 }
94
95 void
96 nv10_get_fog_coeff(GLcontext *ctx, float k[3])
97 {
98 struct gl_fog_attrib *f = &ctx->Fog;
99
100 switch (f->Mode) {
101 case GL_LINEAR:
102 k[0] = 2 + f->Start / (f->End - f->Start);
103 k[1] = -1 / (f->End - f->Start);
104 break;
105
106 case GL_EXP:
107 k[0] = 1.5;
108 k[1] = -0.09 * f->Density;
109 break;
110
111 case GL_EXP2:
112 k[0] = 1.5;
113 k[1] = -0.21 * f->Density;
114 break;
115
116 default:
117 assert(0);
118 }
119
120 k[2] = 0;
121 }
122
123 void
124 nv10_emit_fog(GLcontext *ctx, int emit)
125 {
126 struct nouveau_context *nctx = to_nouveau_context(ctx);
127 struct nouveau_channel *chan = context_chan(ctx);
128 struct nouveau_grobj *celsius = context_eng3d(ctx);
129 struct gl_fog_attrib *f = &ctx->Fog;
130 unsigned source = nctx->fallback == HWTNL ?
131 f->FogCoordinateSource : GL_FOG_COORDINATE_EXT;
132 float k[3];
133
134 nv10_get_fog_coeff(ctx, k);
135
136 BEGIN_RING(chan, celsius, NV10TCL_FOG_MODE, 4);
137 OUT_RING(chan, get_fog_mode(f->Mode));
138 OUT_RING(chan, get_fog_source(source));
139 OUT_RING(chan, f->Enabled ? 1 : 0);
140 OUT_RING(chan, pack_rgba_f(MESA_FORMAT_RGBA8888_REV, f->Color));
141
142 BEGIN_RING(chan, celsius, NV10TCL_FOG_EQUATION_CONSTANT, 3);
143 OUT_RINGf(chan, k[0]);
144 OUT_RINGf(chan, k[1]);
145 OUT_RINGf(chan, k[2]);
146
147 context_dirty(ctx, FRAG);
148 }
149
150 static inline unsigned
151 get_light_mode(struct gl_light *l)
152 {
153 if (l->Enabled) {
154 if (l->_Flags & LIGHT_SPOT)
155 return NV10TCL_ENABLED_LIGHTS_0_DIRECTIONAL;
156 else if (l->_Flags & LIGHT_POSITIONAL)
157 return NV10TCL_ENABLED_LIGHTS_0_POSITIONAL;
158 else
159 return NV10TCL_ENABLED_LIGHTS_0_NONPOSITIONAL;
160 } else {
161 return NV10TCL_ENABLED_LIGHTS_0_DISABLED;
162 }
163 }
164
165 void
166 nv10_emit_light_enable(GLcontext *ctx, int emit)
167 {
168 struct nouveau_context *nctx = to_nouveau_context(ctx);
169 struct nouveau_channel *chan = context_chan(ctx);
170 struct nouveau_grobj *celsius = context_eng3d(ctx);
171 uint32_t en_lights = 0;
172 int i;
173
174 if (nctx->fallback != HWTNL) {
175 BEGIN_RING(chan, celsius, NV10TCL_LIGHTING_ENABLE, 1);
176 OUT_RING(chan, 0);
177 return;
178 }
179
180 for (i = 0; i < MAX_LIGHTS; i++)
181 en_lights |= get_light_mode(&ctx->Light.Light[i]) << 2 * i;
182
183 BEGIN_RING(chan, celsius, NV10TCL_ENABLED_LIGHTS, 1);
184 OUT_RING(chan, en_lights);
185 BEGIN_RING(chan, celsius, NV10TCL_LIGHTING_ENABLE, 1);
186 OUT_RING(chan, ctx->Light.Enabled ? 1 : 0);
187 BEGIN_RING(chan, celsius, NV10TCL_NORMALIZE_ENABLE, 1);
188 OUT_RING(chan, ctx->Transform.Normalize ? 1 : 0);
189 }
190
191 void
192 nv10_emit_light_model(GLcontext *ctx, int emit)
193 {
194 struct nouveau_channel *chan = context_chan(ctx);
195 struct nouveau_grobj *celsius = context_eng3d(ctx);
196 struct gl_lightmodel *m = &ctx->Light.Model;
197
198 BEGIN_RING(chan, celsius, NV10TCL_SEPARATE_SPECULAR_ENABLE, 1);
199 OUT_RING(chan, m->ColorControl == GL_SEPARATE_SPECULAR_COLOR ? 1 : 0);
200
201 BEGIN_RING(chan, celsius, NV10TCL_LIGHT_MODEL, 1);
202 OUT_RING(chan, ((m->LocalViewer ?
203 NV10TCL_LIGHT_MODEL_LOCAL_VIEWER : 0) |
204 (NEED_SECONDARY_COLOR(ctx) ?
205 NV10TCL_LIGHT_MODEL_SEPARATE_SPECULAR : 0) |
206 (!ctx->Light.Enabled && ctx->Fog.ColorSumEnabled ?
207 NV10TCL_LIGHT_MODEL_VERTEX_SPECULAR : 0)));
208 }
209
210 static float
211 get_shine(const float p[], float x)
212 {
213 const int n = 15;
214 const float *y = &p[1];
215 float f = (n - 1) * (1 - 1 / (1 + p[0] * x))
216 / (1 - 1 / (1 + p[0] * 1024));
217 int i = f;
218
219 /* Linear interpolation in f-space (Faster and somewhat more
220 * accurate than x-space). */
221 if (x == 0)
222 return y[0];
223 else if (i > n - 2)
224 return y[n - 1];
225 else
226 return y[i] + (y[i + 1] - y[i]) * (f - i);
227 }
228
229 static const float nv10_spot_params[2][16] = {
230 { 0.02, -3.80e-05, -1.77, -2.41, -2.71, -2.88, -2.98, -3.06,
231 -3.11, -3.17, -3.23, -3.28, -3.37, -3.47, -3.83, -5.11 },
232 { 0.02, -0.01, 1.77, 2.39, 2.70, 2.87, 2.98, 3.06,
233 3.10, 3.16, 3.23, 3.27, 3.37, 3.47, 3.83, 5.11 },
234 };
235
236 void
237 nv10_get_spot_coeff(struct gl_light *l, float k[7])
238 {
239 float e = l->SpotExponent;
240 float a0, b0, a1, a2, b2, a3;
241
242 if (e > 0)
243 a0 = -1 - 5.36e-3 / sqrt(e);
244 else
245 a0 = -1;
246 b0 = 1 / (1 + 0.273 * e);
247
248 a1 = get_shine(nv10_spot_params[0], e);
249
250 a2 = get_shine(nv10_spot_params[1], e);
251 b2 = 1 / (1 + 0.273 * e);
252
253 a3 = 0.9 + 0.278 * e;
254
255 if (l->SpotCutoff > 0) {
256 float cutoff = MAX2(a3, 1 / (1 - l->_CosCutoff));
257
258 k[0] = MAX2(0, a0 + b0 * cutoff);
259 k[1] = a1;
260 k[2] = a2 + b2 * cutoff;
261 k[3] = - cutoff * l->_NormSpotDirection[0];
262 k[4] = - cutoff * l->_NormSpotDirection[1];
263 k[5] = - cutoff * l->_NormSpotDirection[2];
264 k[6] = 1 - cutoff;
265
266 } else {
267 k[0] = b0;
268 k[1] = a1;
269 k[2] = a2 + b2;
270 k[3] = - l->_NormSpotDirection[0];
271 k[4] = - l->_NormSpotDirection[1];
272 k[5] = - l->_NormSpotDirection[2];
273 k[6] = -1;
274 }
275 }
276
277 void
278 nv10_emit_light_source(GLcontext *ctx, int emit)
279 {
280 const int i = emit - NOUVEAU_STATE_LIGHT_SOURCE0;
281 struct nouveau_channel *chan = context_chan(ctx);
282 struct nouveau_grobj *celsius = context_eng3d(ctx);
283 struct gl_light *l = &ctx->Light.Light[i];
284
285 if (l->_Flags & LIGHT_POSITIONAL) {
286 BEGIN_RING(chan, celsius, NV10TCL_LIGHT_POSITION_X(i), 3);
287 OUT_RINGf(chan, l->_Position[0]);
288 OUT_RINGf(chan, l->_Position[1]);
289 OUT_RINGf(chan, l->_Position[2]);
290
291 BEGIN_RING(chan, celsius,
292 NV10TCL_LIGHT_ATTENUATION_CONSTANT(i), 3);
293 OUT_RINGf(chan, l->ConstantAttenuation);
294 OUT_RINGf(chan, l->LinearAttenuation);
295 OUT_RINGf(chan, l->QuadraticAttenuation);
296
297 } else {
298 BEGIN_RING(chan, celsius, NV10TCL_LIGHT_DIRECTION_X(i), 3);
299 OUT_RINGf(chan, l->_VP_inf_norm[0]);
300 OUT_RINGf(chan, l->_VP_inf_norm[1]);
301 OUT_RINGf(chan, l->_VP_inf_norm[2]);
302
303 BEGIN_RING(chan, celsius, NV10TCL_LIGHT_HALF_VECTOR_X(i), 3);
304 OUT_RINGf(chan, l->_h_inf_norm[0]);
305 OUT_RINGf(chan, l->_h_inf_norm[1]);
306 OUT_RINGf(chan, l->_h_inf_norm[2]);
307 }
308
309 if (l->_Flags & LIGHT_SPOT) {
310 float k[7];
311
312 nv10_get_spot_coeff(l, k);
313
314 BEGIN_RING(chan, celsius, NV10TCL_LIGHT_SPOT_CUTOFF_A(i), 7);
315 OUT_RINGf(chan, k[0]);
316 OUT_RINGf(chan, k[1]);
317 OUT_RINGf(chan, k[2]);
318 OUT_RINGf(chan, k[3]);
319 OUT_RINGf(chan, k[4]);
320 OUT_RINGf(chan, k[5]);
321 OUT_RINGf(chan, k[6]);
322 }
323 }
324
325 #define USE_COLOR_MATERIAL(attr) \
326 (ctx->Light.ColorMaterialEnabled && \
327 ctx->Light.ColorMaterialBitmask & (1 << MAT_ATTRIB_FRONT_##attr))
328
329 void
330 nv10_emit_material_ambient(GLcontext *ctx, int emit)
331 {
332 struct nouveau_channel *chan = context_chan(ctx);
333 struct nouveau_grobj *celsius = context_eng3d(ctx);
334 float (*mat)[4] = ctx->Light.Material.Attrib;
335 float c_scene[3], c_factor[3];
336 struct gl_light *l;
337
338 if (USE_COLOR_MATERIAL(AMBIENT)) {
339 COPY_3V(c_scene, ctx->Light.Model.Ambient);
340 COPY_3V(c_factor, mat[MAT_ATTRIB_FRONT_EMISSION]);
341
342 } else if (USE_COLOR_MATERIAL(EMISSION)) {
343 SCALE_3V(c_scene, mat[MAT_ATTRIB_FRONT_AMBIENT],
344 ctx->Light.Model.Ambient);
345 ZERO_3V(c_factor);
346
347 } else {
348 COPY_3V(c_scene, ctx->Light._BaseColor[0]);
349 ZERO_3V(c_factor);
350 }
351
352 BEGIN_RING(chan, celsius, NV10TCL_LIGHT_MODEL_AMBIENT_R, 3);
353 OUT_RINGf(chan, c_scene[0]);
354 OUT_RINGf(chan, c_scene[1]);
355 OUT_RINGf(chan, c_scene[2]);
356
357 if (ctx->Light.ColorMaterialEnabled) {
358 BEGIN_RING(chan, celsius, NV10TCL_MATERIAL_FACTOR_R, 3);
359 OUT_RINGf(chan, c_factor[0]);
360 OUT_RINGf(chan, c_factor[1]);
361 OUT_RINGf(chan, c_factor[2]);
362 }
363
364 foreach(l, &ctx->Light.EnabledList) {
365 const int i = l - ctx->Light.Light;
366 float *c_light = (USE_COLOR_MATERIAL(AMBIENT) ?
367 l->Ambient :
368 l->_MatAmbient[0]);
369
370 BEGIN_RING(chan, celsius, NV10TCL_LIGHT_AMBIENT_R(i), 3);
371 OUT_RINGf(chan, c_light[0]);
372 OUT_RINGf(chan, c_light[1]);
373 OUT_RINGf(chan, c_light[2]);
374 }
375 }
376
377 void
378 nv10_emit_material_diffuse(GLcontext *ctx, int emit)
379 {
380 struct nouveau_channel *chan = context_chan(ctx);
381 struct nouveau_grobj *celsius = context_eng3d(ctx);
382 GLfloat (*mat)[4] = ctx->Light.Material.Attrib;
383 struct gl_light *l;
384
385 BEGIN_RING(chan, celsius, NV10TCL_MATERIAL_FACTOR_A, 1);
386 OUT_RINGf(chan, mat[MAT_ATTRIB_FRONT_DIFFUSE][3]);
387
388 foreach(l, &ctx->Light.EnabledList) {
389 const int i = l - ctx->Light.Light;
390 float *c_light = (USE_COLOR_MATERIAL(DIFFUSE) ?
391 l->Diffuse :
392 l->_MatDiffuse[0]);
393
394 BEGIN_RING(chan, celsius, NV10TCL_LIGHT_DIFFUSE_R(i), 3);
395 OUT_RINGf(chan, c_light[0]);
396 OUT_RINGf(chan, c_light[1]);
397 OUT_RINGf(chan, c_light[2]);
398 }
399 }
400
401 void
402 nv10_emit_material_specular(GLcontext *ctx, int emit)
403 {
404 struct nouveau_channel *chan = context_chan(ctx);
405 struct nouveau_grobj *celsius = context_eng3d(ctx);
406 struct gl_light *l;
407
408 foreach(l, &ctx->Light.EnabledList) {
409 const int i = l - ctx->Light.Light;
410 float *c_light = (USE_COLOR_MATERIAL(SPECULAR) ?
411 l->Specular :
412 l->_MatSpecular[0]);
413
414 BEGIN_RING(chan, celsius, NV10TCL_LIGHT_SPECULAR_R(i), 3);
415 OUT_RINGf(chan, c_light[0]);
416 OUT_RINGf(chan, c_light[1]);
417 OUT_RINGf(chan, c_light[2]);
418 }
419 }
420
421 static const float nv10_shininess_param[6][16] = {
422 { 0.70, 0.00, 0.06, 0.06, 0.05, 0.04, 0.02, 0.00,
423 -0.06, -0.13, -0.24, -0.36, -0.51, -0.66, -0.82, -1.00 },
424 { 0.01, 1.00, -2.29, -2.77, -2.96, -3.06, -3.12, -3.18,
425 -3.24, -3.29, -3.36, -3.43, -3.51, -3.75, -4.33, -5.11 },
426 { 0.02, 0.00, 2.28, 2.75, 2.94, 3.04, 3.1, 3.15,
427 3.18, 3.22, 3.27, 3.32, 3.39, 3.48, 3.84, 5.11 },
428 { 0.70, 0.00, 0.05, 0.06, 0.06, 0.06, 0.05, 0.04,
429 0.02, 0.01, -0.03, -0.12, -0.25, -0.43, -0.68, -0.99 },
430 { 0.01, 1.00, -1.61, -2.35, -2.67, -2.84, -2.96, -3.05,
431 -3.08, -3.14, -3.2, -3.26, -3.32, -3.42, -3.54, -4.21 },
432 { 0.01, 0.00, 2.25, 2.73, 2.92, 3.03, 3.09, 3.15,
433 3.16, 3.21, 3.25, 3.29, 3.35, 3.43, 3.56, 4.22 },
434 };
435
436 void
437 nv10_get_shininess_coeff(float s, float k[6])
438 {
439 int i;
440
441 for (i = 0; i < 6; i++)
442 k[i] = get_shine(nv10_shininess_param[i], s);
443 }
444
445 void
446 nv10_emit_material_shininess(GLcontext *ctx, int emit)
447 {
448 struct nouveau_channel *chan = context_chan(ctx);
449 struct nouveau_grobj *celsius = context_eng3d(ctx);
450 float (*mat)[4] = ctx->Light.Material.Attrib;
451 float k[6];
452
453 nv10_get_shininess_coeff(
454 CLAMP(mat[MAT_ATTRIB_FRONT_SHININESS][0], 0, 1024),
455 k);
456
457 BEGIN_RING(chan, celsius, NV10TCL_MATERIAL_SHININESS(0), 6);
458 OUT_RINGf(chan, k[0]);
459 OUT_RINGf(chan, k[1]);
460 OUT_RINGf(chan, k[2]);
461 OUT_RINGf(chan, k[3]);
462 OUT_RINGf(chan, k[4]);
463 OUT_RINGf(chan, k[5]);
464 }
465
466 void
467 nv10_emit_modelview(GLcontext *ctx, int emit)
468 {
469 struct nouveau_context *nctx = to_nouveau_context(ctx);
470 struct nouveau_channel *chan = context_chan(ctx);
471 struct nouveau_grobj *celsius = context_eng3d(ctx);
472 GLmatrix *m = ctx->ModelviewMatrixStack.Top;
473
474 if (nctx->fallback != HWTNL)
475 return;
476
477 if (ctx->Light._NeedEyeCoords || ctx->Fog.Enabled) {
478 BEGIN_RING(chan, celsius, NV10TCL_MODELVIEW0_MATRIX(0), 16);
479 OUT_RINGm(chan, m->m);
480 }
481
482 if (ctx->Light.Enabled) {
483 int i, j;
484
485 BEGIN_RING(chan, celsius,
486 NV10TCL_INVERSE_MODELVIEW0_MATRIX(0), 12);
487 for (i = 0; i < 3; i++)
488 for (j = 0; j < 4; j++)
489 OUT_RINGf(chan, m->inv[4*i + j]);
490 }
491 }
492
493 void
494 nv10_emit_point_parameter(GLcontext *ctx, int emit)
495 {
496 }
497
498 void
499 nv10_emit_projection(GLcontext *ctx, int emit)
500 {
501 struct nouveau_context *nctx = to_nouveau_context(ctx);
502 struct nouveau_channel *chan = context_chan(ctx);
503 struct nouveau_grobj *celsius = context_eng3d(ctx);
504 GLmatrix m;
505
506 _math_matrix_ctr(&m);
507 get_viewport_scale(ctx, m.m);
508
509 if (nctx->fallback == HWTNL)
510 _math_matrix_mul_matrix(&m, &m, &ctx->_ModelProjectMatrix);
511
512 BEGIN_RING(chan, celsius, NV10TCL_PROJECTION_MATRIX(0), 16);
513 OUT_RINGm(chan, m.m);
514
515 _math_matrix_dtr(&m);
516 }