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