mesa/program: Use sampler object state if present
[mesa.git] / src / mesa / program / prog_statevars.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 7.1
4 *
5 * Copyright (C) 1999-2007 Brian Paul All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25 /**
26 * \file prog_statevars.c
27 * Program state variable management.
28 * \author Brian Paul
29 */
30
31
32 #include "main/glheader.h"
33 #include "main/context.h"
34 #include "main/imports.h"
35 #include "main/macros.h"
36 #include "main/mtypes.h"
37 #include "main/fbobject.h"
38 #include "prog_statevars.h"
39 #include "prog_parameter.h"
40 #include "main/samplerobj.h"
41
42
43 /**
44 * Use the list of tokens in the state[] array to find global GL state
45 * and return it in <value>. Usually, four values are returned in <value>
46 * but matrix queries may return as many as 16 values.
47 * This function is used for ARB vertex/fragment programs.
48 * The program parser will produce the state[] values.
49 */
50 static void
51 _mesa_fetch_state(struct gl_context *ctx, const gl_state_index state[],
52 GLfloat *value)
53 {
54 switch (state[0]) {
55 case STATE_MATERIAL:
56 {
57 /* state[1] is either 0=front or 1=back side */
58 const GLuint face = (GLuint) state[1];
59 const struct gl_material *mat = &ctx->Light.Material;
60 ASSERT(face == 0 || face == 1);
61 /* we rely on tokens numbered so that _BACK_ == _FRONT_+ 1 */
62 ASSERT(MAT_ATTRIB_FRONT_AMBIENT + 1 == MAT_ATTRIB_BACK_AMBIENT);
63 /* XXX we could get rid of this switch entirely with a little
64 * work in arbprogparse.c's parse_state_single_item().
65 */
66 /* state[2] is the material attribute */
67 switch (state[2]) {
68 case STATE_AMBIENT:
69 COPY_4V(value, mat->Attrib[MAT_ATTRIB_FRONT_AMBIENT + face]);
70 return;
71 case STATE_DIFFUSE:
72 COPY_4V(value, mat->Attrib[MAT_ATTRIB_FRONT_DIFFUSE + face]);
73 return;
74 case STATE_SPECULAR:
75 COPY_4V(value, mat->Attrib[MAT_ATTRIB_FRONT_SPECULAR + face]);
76 return;
77 case STATE_EMISSION:
78 COPY_4V(value, mat->Attrib[MAT_ATTRIB_FRONT_EMISSION + face]);
79 return;
80 case STATE_SHININESS:
81 value[0] = mat->Attrib[MAT_ATTRIB_FRONT_SHININESS + face][0];
82 value[1] = 0.0F;
83 value[2] = 0.0F;
84 value[3] = 1.0F;
85 return;
86 default:
87 _mesa_problem(ctx, "Invalid material state in fetch_state");
88 return;
89 }
90 }
91 case STATE_LIGHT:
92 {
93 /* state[1] is the light number */
94 const GLuint ln = (GLuint) state[1];
95 /* state[2] is the light attribute */
96 switch (state[2]) {
97 case STATE_AMBIENT:
98 COPY_4V(value, ctx->Light.Light[ln].Ambient);
99 return;
100 case STATE_DIFFUSE:
101 COPY_4V(value, ctx->Light.Light[ln].Diffuse);
102 return;
103 case STATE_SPECULAR:
104 COPY_4V(value, ctx->Light.Light[ln].Specular);
105 return;
106 case STATE_POSITION:
107 COPY_4V(value, ctx->Light.Light[ln].EyePosition);
108 return;
109 case STATE_ATTENUATION:
110 value[0] = ctx->Light.Light[ln].ConstantAttenuation;
111 value[1] = ctx->Light.Light[ln].LinearAttenuation;
112 value[2] = ctx->Light.Light[ln].QuadraticAttenuation;
113 value[3] = ctx->Light.Light[ln].SpotExponent;
114 return;
115 case STATE_SPOT_DIRECTION:
116 COPY_3V(value, ctx->Light.Light[ln].SpotDirection);
117 value[3] = ctx->Light.Light[ln]._CosCutoff;
118 return;
119 case STATE_SPOT_CUTOFF:
120 value[0] = ctx->Light.Light[ln].SpotCutoff;
121 return;
122 case STATE_HALF_VECTOR:
123 {
124 static const GLfloat eye_z[] = {0, 0, 1};
125 GLfloat p[3];
126 /* Compute infinite half angle vector:
127 * halfVector = normalize(normalize(lightPos) + (0, 0, 1))
128 * light.EyePosition.w should be 0 for infinite lights.
129 */
130 COPY_3V(p, ctx->Light.Light[ln].EyePosition);
131 NORMALIZE_3FV(p);
132 ADD_3V(value, p, eye_z);
133 NORMALIZE_3FV(value);
134 value[3] = 1.0;
135 }
136 return;
137 default:
138 _mesa_problem(ctx, "Invalid light state in fetch_state");
139 return;
140 }
141 }
142 case STATE_LIGHTMODEL_AMBIENT:
143 COPY_4V(value, ctx->Light.Model.Ambient);
144 return;
145 case STATE_LIGHTMODEL_SCENECOLOR:
146 if (state[1] == 0) {
147 /* front */
148 GLint i;
149 for (i = 0; i < 3; i++) {
150 value[i] = ctx->Light.Model.Ambient[i]
151 * ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_AMBIENT][i]
152 + ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_EMISSION][i];
153 }
154 value[3] = ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_DIFFUSE][3];
155 }
156 else {
157 /* back */
158 GLint i;
159 for (i = 0; i < 3; i++) {
160 value[i] = ctx->Light.Model.Ambient[i]
161 * ctx->Light.Material.Attrib[MAT_ATTRIB_BACK_AMBIENT][i]
162 + ctx->Light.Material.Attrib[MAT_ATTRIB_BACK_EMISSION][i];
163 }
164 value[3] = ctx->Light.Material.Attrib[MAT_ATTRIB_BACK_DIFFUSE][3];
165 }
166 return;
167 case STATE_LIGHTPROD:
168 {
169 const GLuint ln = (GLuint) state[1];
170 const GLuint face = (GLuint) state[2];
171 GLint i;
172 ASSERT(face == 0 || face == 1);
173 switch (state[3]) {
174 case STATE_AMBIENT:
175 for (i = 0; i < 3; i++) {
176 value[i] = ctx->Light.Light[ln].Ambient[i] *
177 ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_AMBIENT+face][i];
178 }
179 /* [3] = material alpha */
180 value[3] = ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_AMBIENT+face][3];
181 return;
182 case STATE_DIFFUSE:
183 for (i = 0; i < 3; i++) {
184 value[i] = ctx->Light.Light[ln].Diffuse[i] *
185 ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_DIFFUSE+face][i];
186 }
187 /* [3] = material alpha */
188 value[3] = ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_DIFFUSE+face][3];
189 return;
190 case STATE_SPECULAR:
191 for (i = 0; i < 3; i++) {
192 value[i] = ctx->Light.Light[ln].Specular[i] *
193 ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_SPECULAR+face][i];
194 }
195 /* [3] = material alpha */
196 value[3] = ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_SPECULAR+face][3];
197 return;
198 default:
199 _mesa_problem(ctx, "Invalid lightprod state in fetch_state");
200 return;
201 }
202 }
203 case STATE_TEXGEN:
204 {
205 /* state[1] is the texture unit */
206 const GLuint unit = (GLuint) state[1];
207 /* state[2] is the texgen attribute */
208 switch (state[2]) {
209 case STATE_TEXGEN_EYE_S:
210 COPY_4V(value, ctx->Texture.Unit[unit].GenS.EyePlane);
211 return;
212 case STATE_TEXGEN_EYE_T:
213 COPY_4V(value, ctx->Texture.Unit[unit].GenT.EyePlane);
214 return;
215 case STATE_TEXGEN_EYE_R:
216 COPY_4V(value, ctx->Texture.Unit[unit].GenR.EyePlane);
217 return;
218 case STATE_TEXGEN_EYE_Q:
219 COPY_4V(value, ctx->Texture.Unit[unit].GenQ.EyePlane);
220 return;
221 case STATE_TEXGEN_OBJECT_S:
222 COPY_4V(value, ctx->Texture.Unit[unit].GenS.ObjectPlane);
223 return;
224 case STATE_TEXGEN_OBJECT_T:
225 COPY_4V(value, ctx->Texture.Unit[unit].GenT.ObjectPlane);
226 return;
227 case STATE_TEXGEN_OBJECT_R:
228 COPY_4V(value, ctx->Texture.Unit[unit].GenR.ObjectPlane);
229 return;
230 case STATE_TEXGEN_OBJECT_Q:
231 COPY_4V(value, ctx->Texture.Unit[unit].GenQ.ObjectPlane);
232 return;
233 default:
234 _mesa_problem(ctx, "Invalid texgen state in fetch_state");
235 return;
236 }
237 }
238 case STATE_TEXENV_COLOR:
239 {
240 /* state[1] is the texture unit */
241 const GLuint unit = (GLuint) state[1];
242 if(ctx->Color._ClampFragmentColor)
243 COPY_4V(value, ctx->Texture.Unit[unit].EnvColor);
244 else
245 COPY_4V(value, ctx->Texture.Unit[unit].EnvColorUnclamped);
246 }
247 return;
248 case STATE_FOG_COLOR:
249 if(ctx->Color._ClampFragmentColor)
250 COPY_4V(value, ctx->Fog.Color);
251 else
252 COPY_4V(value, ctx->Fog.ColorUnclamped);
253 return;
254 case STATE_FOG_PARAMS:
255 value[0] = ctx->Fog.Density;
256 value[1] = ctx->Fog.Start;
257 value[2] = ctx->Fog.End;
258 value[3] = (ctx->Fog.End == ctx->Fog.Start)
259 ? 1.0f : (GLfloat)(1.0 / (ctx->Fog.End - ctx->Fog.Start));
260 return;
261 case STATE_CLIPPLANE:
262 {
263 const GLuint plane = (GLuint) state[1];
264 COPY_4V(value, ctx->Transform.EyeUserPlane[plane]);
265 }
266 return;
267 case STATE_POINT_SIZE:
268 value[0] = ctx->Point.Size;
269 value[1] = ctx->Point.MinSize;
270 value[2] = ctx->Point.MaxSize;
271 value[3] = ctx->Point.Threshold;
272 return;
273 case STATE_POINT_ATTENUATION:
274 value[0] = ctx->Point.Params[0];
275 value[1] = ctx->Point.Params[1];
276 value[2] = ctx->Point.Params[2];
277 value[3] = 1.0F;
278 return;
279 case STATE_MODELVIEW_MATRIX:
280 case STATE_PROJECTION_MATRIX:
281 case STATE_MVP_MATRIX:
282 case STATE_TEXTURE_MATRIX:
283 case STATE_PROGRAM_MATRIX:
284 {
285 /* state[0] = modelview, projection, texture, etc. */
286 /* state[1] = which texture matrix or program matrix */
287 /* state[2] = first row to fetch */
288 /* state[3] = last row to fetch */
289 /* state[4] = transpose, inverse or invtrans */
290 const GLmatrix *matrix;
291 const gl_state_index mat = state[0];
292 const GLuint index = (GLuint) state[1];
293 const GLuint firstRow = (GLuint) state[2];
294 const GLuint lastRow = (GLuint) state[3];
295 const gl_state_index modifier = state[4];
296 const GLfloat *m;
297 GLuint row, i;
298 ASSERT(firstRow >= 0);
299 ASSERT(firstRow < 4);
300 ASSERT(lastRow >= 0);
301 ASSERT(lastRow < 4);
302 if (mat == STATE_MODELVIEW_MATRIX) {
303 matrix = ctx->ModelviewMatrixStack.Top;
304 }
305 else if (mat == STATE_PROJECTION_MATRIX) {
306 matrix = ctx->ProjectionMatrixStack.Top;
307 }
308 else if (mat == STATE_MVP_MATRIX) {
309 matrix = &ctx->_ModelProjectMatrix;
310 }
311 else if (mat == STATE_TEXTURE_MATRIX) {
312 ASSERT(index < Elements(ctx->TextureMatrixStack));
313 matrix = ctx->TextureMatrixStack[index].Top;
314 }
315 else if (mat == STATE_PROGRAM_MATRIX) {
316 ASSERT(index < Elements(ctx->ProgramMatrixStack));
317 matrix = ctx->ProgramMatrixStack[index].Top;
318 }
319 else {
320 _mesa_problem(ctx, "Bad matrix name in _mesa_fetch_state()");
321 return;
322 }
323 if (modifier == STATE_MATRIX_INVERSE ||
324 modifier == STATE_MATRIX_INVTRANS) {
325 /* Be sure inverse is up to date:
326 */
327 _math_matrix_analyse( (GLmatrix*) matrix );
328 m = matrix->inv;
329 }
330 else {
331 m = matrix->m;
332 }
333 if (modifier == STATE_MATRIX_TRANSPOSE ||
334 modifier == STATE_MATRIX_INVTRANS) {
335 for (i = 0, row = firstRow; row <= lastRow; row++) {
336 value[i++] = m[row * 4 + 0];
337 value[i++] = m[row * 4 + 1];
338 value[i++] = m[row * 4 + 2];
339 value[i++] = m[row * 4 + 3];
340 }
341 }
342 else {
343 for (i = 0, row = firstRow; row <= lastRow; row++) {
344 value[i++] = m[row + 0];
345 value[i++] = m[row + 4];
346 value[i++] = m[row + 8];
347 value[i++] = m[row + 12];
348 }
349 }
350 }
351 return;
352 case STATE_DEPTH_RANGE:
353 value[0] = ctx->Viewport.Near; /* near */
354 value[1] = ctx->Viewport.Far; /* far */
355 value[2] = ctx->Viewport.Far - ctx->Viewport.Near; /* far - near */
356 value[3] = 1.0;
357 return;
358 case STATE_FRAGMENT_PROGRAM:
359 {
360 /* state[1] = {STATE_ENV, STATE_LOCAL} */
361 /* state[2] = parameter index */
362 const int idx = (int) state[2];
363 switch (state[1]) {
364 case STATE_ENV:
365 COPY_4V(value, ctx->FragmentProgram.Parameters[idx]);
366 return;
367 case STATE_LOCAL:
368 COPY_4V(value, ctx->FragmentProgram.Current->Base.LocalParams[idx]);
369 return;
370 default:
371 _mesa_problem(ctx, "Bad state switch in _mesa_fetch_state()");
372 return;
373 }
374 }
375 return;
376
377 case STATE_VERTEX_PROGRAM:
378 {
379 /* state[1] = {STATE_ENV, STATE_LOCAL} */
380 /* state[2] = parameter index */
381 const int idx = (int) state[2];
382 switch (state[1]) {
383 case STATE_ENV:
384 COPY_4V(value, ctx->VertexProgram.Parameters[idx]);
385 return;
386 case STATE_LOCAL:
387 COPY_4V(value, ctx->VertexProgram.Current->Base.LocalParams[idx]);
388 return;
389 default:
390 _mesa_problem(ctx, "Bad state switch in _mesa_fetch_state()");
391 return;
392 }
393 }
394 return;
395
396 case STATE_NORMAL_SCALE:
397 ASSIGN_4V(value, ctx->_ModelViewInvScale, 0, 0, 1);
398 return;
399
400 case STATE_INTERNAL:
401 switch (state[1]) {
402 case STATE_CURRENT_ATTRIB:
403 {
404 const GLuint idx = (GLuint) state[2];
405 COPY_4V(value, ctx->Current.Attrib[idx]);
406 }
407 return;
408
409 case STATE_CURRENT_ATTRIB_MAYBE_VP_CLAMPED:
410 {
411 const GLuint idx = (GLuint) state[2];
412 if(ctx->Light._ClampVertexColor &&
413 (idx == VERT_ATTRIB_COLOR0 ||
414 idx == VERT_ATTRIB_COLOR1)) {
415 value[0] = CLAMP(ctx->Current.Attrib[idx][0], 0.0f, 1.0f);
416 value[1] = CLAMP(ctx->Current.Attrib[idx][1], 0.0f, 1.0f);
417 value[2] = CLAMP(ctx->Current.Attrib[idx][2], 0.0f, 1.0f);
418 value[3] = CLAMP(ctx->Current.Attrib[idx][3], 0.0f, 1.0f);
419 }
420 else
421 COPY_4V(value, ctx->Current.Attrib[idx]);
422 }
423 return;
424
425 case STATE_NORMAL_SCALE:
426 ASSIGN_4V(value,
427 ctx->_ModelViewInvScale,
428 ctx->_ModelViewInvScale,
429 ctx->_ModelViewInvScale,
430 1);
431 return;
432
433 case STATE_TEXRECT_SCALE:
434 /* Value = { 1/texWidth, 1/texHeight, 0, 1 }.
435 * Used to convert unnormalized texcoords to normalized texcoords.
436 */
437 {
438 const int unit = (int) state[2];
439 const struct gl_texture_object *texObj
440 = ctx->Texture.Unit[unit]._Current;
441 if (texObj) {
442 struct gl_texture_image *texImage = texObj->Image[0][0];
443 ASSIGN_4V(value,
444 (GLfloat) (1.0 / texImage->Width),
445 (GLfloat) (1.0 / texImage->Height),
446 0.0f, 1.0f);
447 }
448 }
449 return;
450
451 case STATE_FOG_PARAMS_OPTIMIZED:
452 /* for simpler per-vertex/pixel fog calcs. POW (for EXP/EXP2 fog)
453 * might be more expensive than EX2 on some hw, plus it needs
454 * another constant (e) anyway. Linear fog can now be done with a
455 * single MAD.
456 * linear: fogcoord * -1/(end-start) + end/(end-start)
457 * exp: 2^-(density/ln(2) * fogcoord)
458 * exp2: 2^-((density/(ln(2)^2) * fogcoord)^2)
459 */
460 value[0] = (ctx->Fog.End == ctx->Fog.Start)
461 ? 1.0f : (GLfloat)(-1.0F / (ctx->Fog.End - ctx->Fog.Start));
462 value[1] = ctx->Fog.End * -value[0];
463 value[2] = (GLfloat)(ctx->Fog.Density * M_LOG2E); /* M_LOG2E == 1/ln(2) */
464 value[3] = (GLfloat)(ctx->Fog.Density * ONE_DIV_SQRT_LN2);
465 return;
466
467 case STATE_POINT_SIZE_CLAMPED:
468 {
469 /* this includes implementation dependent limits, to avoid
470 * another potentially necessary clamp.
471 * Note: for sprites, point smooth (point AA) is ignored
472 * and we'll clamp to MinPointSizeAA and MaxPointSize, because we
473 * expect drivers will want to say their minimum for AA size is 0.0
474 * but for non-AA it's 1.0 (because normal points with size below 1.0
475 * need to get rounded up to 1.0, hence never disappear). GL does
476 * not specify max clamp size for sprites, other than it needs to be
477 * at least as large as max AA size, hence use non-AA size there.
478 */
479 GLfloat minImplSize;
480 GLfloat maxImplSize;
481 if (ctx->Point.PointSprite) {
482 minImplSize = ctx->Const.MinPointSizeAA;
483 maxImplSize = ctx->Const.MaxPointSize;
484 }
485 else if (ctx->Point.SmoothFlag || ctx->Multisample._Enabled) {
486 minImplSize = ctx->Const.MinPointSizeAA;
487 maxImplSize = ctx->Const.MaxPointSizeAA;
488 }
489 else {
490 minImplSize = ctx->Const.MinPointSize;
491 maxImplSize = ctx->Const.MaxPointSize;
492 }
493 value[0] = ctx->Point.Size;
494 value[1] = ctx->Point.MinSize >= minImplSize ? ctx->Point.MinSize : minImplSize;
495 value[2] = ctx->Point.MaxSize <= maxImplSize ? ctx->Point.MaxSize : maxImplSize;
496 value[3] = ctx->Point.Threshold;
497 }
498 return;
499 case STATE_LIGHT_SPOT_DIR_NORMALIZED:
500 {
501 /* here, state[2] is the light number */
502 /* pre-normalize spot dir */
503 const GLuint ln = (GLuint) state[2];
504 COPY_3V(value, ctx->Light.Light[ln]._NormSpotDirection);
505 value[3] = ctx->Light.Light[ln]._CosCutoff;
506 }
507 return;
508
509 case STATE_LIGHT_POSITION:
510 {
511 const GLuint ln = (GLuint) state[2];
512 COPY_4V(value, ctx->Light.Light[ln]._Position);
513 }
514 return;
515
516 case STATE_LIGHT_POSITION_NORMALIZED:
517 {
518 const GLuint ln = (GLuint) state[2];
519 COPY_4V(value, ctx->Light.Light[ln]._Position);
520 NORMALIZE_3FV( value );
521 }
522 return;
523
524 case STATE_LIGHT_HALF_VECTOR:
525 {
526 const GLuint ln = (GLuint) state[2];
527 GLfloat p[3];
528 /* Compute infinite half angle vector:
529 * halfVector = normalize(normalize(lightPos) + (0, 0, 1))
530 * light.EyePosition.w should be 0 for infinite lights.
531 */
532 COPY_3V(p, ctx->Light.Light[ln]._Position);
533 NORMALIZE_3FV(p);
534 ADD_3V(value, p, ctx->_EyeZDir);
535 NORMALIZE_3FV(value);
536 value[3] = 1.0;
537 }
538 return;
539
540 case STATE_PT_SCALE:
541 value[0] = ctx->Pixel.RedScale;
542 value[1] = ctx->Pixel.GreenScale;
543 value[2] = ctx->Pixel.BlueScale;
544 value[3] = ctx->Pixel.AlphaScale;
545 return;
546
547 case STATE_PT_BIAS:
548 value[0] = ctx->Pixel.RedBias;
549 value[1] = ctx->Pixel.GreenBias;
550 value[2] = ctx->Pixel.BlueBias;
551 value[3] = ctx->Pixel.AlphaBias;
552 return;
553
554 case STATE_SHADOW_AMBIENT:
555 {
556 const int unit = (int) state[2];
557 const struct gl_texture_object *texObj
558 = ctx->Texture.Unit[unit]._Current;
559 const struct gl_sampler_object *samp =
560 _mesa_get_samplerobj(ctx, unit);
561 if (texObj) {
562 value[0] =
563 value[1] =
564 value[2] =
565 value[3] = samp->CompareFailValue;
566 }
567 }
568 return;
569
570 case STATE_FB_SIZE:
571 value[0] = (GLfloat) (ctx->DrawBuffer->Width - 1);
572 value[1] = (GLfloat) (ctx->DrawBuffer->Height - 1);
573 value[2] = 0.0F;
574 value[3] = 0.0F;
575 return;
576
577 case STATE_FB_WPOS_Y_TRANSFORM:
578 /* A driver may negate this conditional by using ZW swizzle
579 * instead of XY (based on e.g. some other state). */
580 if (_mesa_is_user_fbo(ctx->DrawBuffer)) {
581 /* Identity (XY) followed by flipping Y upside down (ZW). */
582 value[0] = 1.0F;
583 value[1] = 0.0F;
584 value[2] = -1.0F;
585 value[3] = (GLfloat) ctx->DrawBuffer->Height;
586 } else {
587 /* Flipping Y upside down (XY) followed by identity (ZW). */
588 value[0] = -1.0F;
589 value[1] = (GLfloat) ctx->DrawBuffer->Height;
590 value[2] = 1.0F;
591 value[3] = 0.0F;
592 }
593 return;
594
595 case STATE_ROT_MATRIX_0:
596 {
597 const int unit = (int) state[2];
598 GLfloat *rotMat22 = ctx->Texture.Unit[unit].RotMatrix;
599 value[0] = rotMat22[0];
600 value[1] = rotMat22[2];
601 value[2] = 0.0;
602 value[3] = 0.0;
603 }
604 return;
605
606 case STATE_ROT_MATRIX_1:
607 {
608 const int unit = (int) state[2];
609 GLfloat *rotMat22 = ctx->Texture.Unit[unit].RotMatrix;
610 value[0] = rotMat22[1];
611 value[1] = rotMat22[3];
612 value[2] = 0.0;
613 value[3] = 0.0;
614 }
615 return;
616
617 /* XXX: make sure new tokens added here are also handled in the
618 * _mesa_program_state_flags() switch, below.
619 */
620 default:
621 /* Unknown state indexes are silently ignored here.
622 * Drivers may do something special.
623 */
624 return;
625 }
626 return;
627
628 default:
629 _mesa_problem(ctx, "Invalid state in _mesa_fetch_state");
630 return;
631 }
632 }
633
634
635 /**
636 * Return a bitmask of the Mesa state flags (_NEW_* values) which would
637 * indicate that the given context state may have changed.
638 * The bitmask is used during validation to determine if we need to update
639 * vertex/fragment program parameters (like "state.material.color") when
640 * some GL state has changed.
641 */
642 GLbitfield
643 _mesa_program_state_flags(const gl_state_index state[STATE_LENGTH])
644 {
645 switch (state[0]) {
646 case STATE_MATERIAL:
647 case STATE_LIGHTPROD:
648 case STATE_LIGHTMODEL_SCENECOLOR:
649 /* these can be effected by glColor when colormaterial mode is used */
650 return _NEW_LIGHT | _NEW_CURRENT_ATTRIB;
651
652 case STATE_LIGHT:
653 case STATE_LIGHTMODEL_AMBIENT:
654 return _NEW_LIGHT;
655
656 case STATE_TEXGEN:
657 return _NEW_TEXTURE;
658 case STATE_TEXENV_COLOR:
659 return _NEW_TEXTURE | _NEW_BUFFERS | _NEW_FRAG_CLAMP;
660
661 case STATE_FOG_COLOR:
662 return _NEW_FOG | _NEW_BUFFERS | _NEW_FRAG_CLAMP;
663 case STATE_FOG_PARAMS:
664 return _NEW_FOG;
665
666 case STATE_CLIPPLANE:
667 return _NEW_TRANSFORM;
668
669 case STATE_POINT_SIZE:
670 case STATE_POINT_ATTENUATION:
671 return _NEW_POINT;
672
673 case STATE_MODELVIEW_MATRIX:
674 return _NEW_MODELVIEW;
675 case STATE_PROJECTION_MATRIX:
676 return _NEW_PROJECTION;
677 case STATE_MVP_MATRIX:
678 return _NEW_MODELVIEW | _NEW_PROJECTION;
679 case STATE_TEXTURE_MATRIX:
680 return _NEW_TEXTURE_MATRIX;
681 case STATE_PROGRAM_MATRIX:
682 return _NEW_TRACK_MATRIX;
683
684 case STATE_DEPTH_RANGE:
685 return _NEW_VIEWPORT;
686
687 case STATE_FRAGMENT_PROGRAM:
688 case STATE_VERTEX_PROGRAM:
689 return _NEW_PROGRAM;
690
691 case STATE_NORMAL_SCALE:
692 return _NEW_MODELVIEW;
693
694 case STATE_INTERNAL:
695 switch (state[1]) {
696 case STATE_CURRENT_ATTRIB:
697 return _NEW_CURRENT_ATTRIB;
698 case STATE_CURRENT_ATTRIB_MAYBE_VP_CLAMPED:
699 return _NEW_CURRENT_ATTRIB | _NEW_LIGHT | _NEW_BUFFERS;
700
701 case STATE_NORMAL_SCALE:
702 return _NEW_MODELVIEW;
703
704 case STATE_TEXRECT_SCALE:
705 case STATE_SHADOW_AMBIENT:
706 case STATE_ROT_MATRIX_0:
707 case STATE_ROT_MATRIX_1:
708 return _NEW_TEXTURE;
709 case STATE_FOG_PARAMS_OPTIMIZED:
710 return _NEW_FOG;
711 case STATE_POINT_SIZE_CLAMPED:
712 return _NEW_POINT | _NEW_MULTISAMPLE;
713 case STATE_LIGHT_SPOT_DIR_NORMALIZED:
714 case STATE_LIGHT_POSITION:
715 case STATE_LIGHT_POSITION_NORMALIZED:
716 case STATE_LIGHT_HALF_VECTOR:
717 return _NEW_LIGHT;
718
719 case STATE_PT_SCALE:
720 case STATE_PT_BIAS:
721 return _NEW_PIXEL;
722
723 case STATE_FB_SIZE:
724 case STATE_FB_WPOS_Y_TRANSFORM:
725 return _NEW_BUFFERS;
726
727 default:
728 /* unknown state indexes are silently ignored and
729 * no flag set, since it is handled by the driver.
730 */
731 return 0;
732 }
733
734 default:
735 _mesa_problem(NULL, "unexpected state[0] in make_state_flags()");
736 return 0;
737 }
738 }
739
740
741 static void
742 append(char *dst, const char *src)
743 {
744 while (*dst)
745 dst++;
746 while (*src)
747 *dst++ = *src++;
748 *dst = 0;
749 }
750
751
752 /**
753 * Convert token 'k' to a string, append it onto 'dst' string.
754 */
755 static void
756 append_token(char *dst, gl_state_index k)
757 {
758 switch (k) {
759 case STATE_MATERIAL:
760 append(dst, "material");
761 break;
762 case STATE_LIGHT:
763 append(dst, "light");
764 break;
765 case STATE_LIGHTMODEL_AMBIENT:
766 append(dst, "lightmodel.ambient");
767 break;
768 case STATE_LIGHTMODEL_SCENECOLOR:
769 break;
770 case STATE_LIGHTPROD:
771 append(dst, "lightprod");
772 break;
773 case STATE_TEXGEN:
774 append(dst, "texgen");
775 break;
776 case STATE_FOG_COLOR:
777 append(dst, "fog.color");
778 break;
779 case STATE_FOG_PARAMS:
780 append(dst, "fog.params");
781 break;
782 case STATE_CLIPPLANE:
783 append(dst, "clip");
784 break;
785 case STATE_POINT_SIZE:
786 append(dst, "point.size");
787 break;
788 case STATE_POINT_ATTENUATION:
789 append(dst, "point.attenuation");
790 break;
791 case STATE_MODELVIEW_MATRIX:
792 append(dst, "matrix.modelview");
793 break;
794 case STATE_PROJECTION_MATRIX:
795 append(dst, "matrix.projection");
796 break;
797 case STATE_MVP_MATRIX:
798 append(dst, "matrix.mvp");
799 break;
800 case STATE_TEXTURE_MATRIX:
801 append(dst, "matrix.texture");
802 break;
803 case STATE_PROGRAM_MATRIX:
804 append(dst, "matrix.program");
805 break;
806 case STATE_MATRIX_INVERSE:
807 append(dst, ".inverse");
808 break;
809 case STATE_MATRIX_TRANSPOSE:
810 append(dst, ".transpose");
811 break;
812 case STATE_MATRIX_INVTRANS:
813 append(dst, ".invtrans");
814 break;
815 case STATE_AMBIENT:
816 append(dst, ".ambient");
817 break;
818 case STATE_DIFFUSE:
819 append(dst, ".diffuse");
820 break;
821 case STATE_SPECULAR:
822 append(dst, ".specular");
823 break;
824 case STATE_EMISSION:
825 append(dst, ".emission");
826 break;
827 case STATE_SHININESS:
828 append(dst, "lshininess");
829 break;
830 case STATE_HALF_VECTOR:
831 append(dst, ".half");
832 break;
833 case STATE_POSITION:
834 append(dst, ".position");
835 break;
836 case STATE_ATTENUATION:
837 append(dst, ".attenuation");
838 break;
839 case STATE_SPOT_DIRECTION:
840 append(dst, ".spot.direction");
841 break;
842 case STATE_SPOT_CUTOFF:
843 append(dst, ".spot.cutoff");
844 break;
845 case STATE_TEXGEN_EYE_S:
846 append(dst, ".eye.s");
847 break;
848 case STATE_TEXGEN_EYE_T:
849 append(dst, ".eye.t");
850 break;
851 case STATE_TEXGEN_EYE_R:
852 append(dst, ".eye.r");
853 break;
854 case STATE_TEXGEN_EYE_Q:
855 append(dst, ".eye.q");
856 break;
857 case STATE_TEXGEN_OBJECT_S:
858 append(dst, ".object.s");
859 break;
860 case STATE_TEXGEN_OBJECT_T:
861 append(dst, ".object.t");
862 break;
863 case STATE_TEXGEN_OBJECT_R:
864 append(dst, ".object.r");
865 break;
866 case STATE_TEXGEN_OBJECT_Q:
867 append(dst, ".object.q");
868 break;
869 case STATE_TEXENV_COLOR:
870 append(dst, "texenv");
871 break;
872 case STATE_DEPTH_RANGE:
873 append(dst, "depth.range");
874 break;
875 case STATE_VERTEX_PROGRAM:
876 case STATE_FRAGMENT_PROGRAM:
877 break;
878 case STATE_ENV:
879 append(dst, "env");
880 break;
881 case STATE_LOCAL:
882 append(dst, "local");
883 break;
884 /* BEGIN internal state vars */
885 case STATE_INTERNAL:
886 append(dst, ".internal.");
887 break;
888 case STATE_CURRENT_ATTRIB:
889 append(dst, "current");
890 break;
891 case STATE_NORMAL_SCALE:
892 append(dst, "normalScale");
893 break;
894 case STATE_TEXRECT_SCALE:
895 append(dst, "texrectScale");
896 break;
897 case STATE_FOG_PARAMS_OPTIMIZED:
898 append(dst, "fogParamsOptimized");
899 break;
900 case STATE_POINT_SIZE_CLAMPED:
901 append(dst, "pointSizeClamped");
902 break;
903 case STATE_LIGHT_SPOT_DIR_NORMALIZED:
904 append(dst, "lightSpotDirNormalized");
905 break;
906 case STATE_LIGHT_POSITION:
907 append(dst, "lightPosition");
908 break;
909 case STATE_LIGHT_POSITION_NORMALIZED:
910 append(dst, "light.position.normalized");
911 break;
912 case STATE_LIGHT_HALF_VECTOR:
913 append(dst, "lightHalfVector");
914 break;
915 case STATE_PT_SCALE:
916 append(dst, "PTscale");
917 break;
918 case STATE_PT_BIAS:
919 append(dst, "PTbias");
920 break;
921 case STATE_SHADOW_AMBIENT:
922 append(dst, "CompareFailValue");
923 break;
924 case STATE_FB_SIZE:
925 append(dst, "FbSize");
926 break;
927 case STATE_FB_WPOS_Y_TRANSFORM:
928 append(dst, "FbWposYTransform");
929 break;
930 case STATE_ROT_MATRIX_0:
931 append(dst, "rotMatrixRow0");
932 break;
933 case STATE_ROT_MATRIX_1:
934 append(dst, "rotMatrixRow1");
935 break;
936 default:
937 /* probably STATE_INTERNAL_DRIVER+i (driver private state) */
938 append(dst, "driverState");
939 }
940 }
941
942 static void
943 append_face(char *dst, GLint face)
944 {
945 if (face == 0)
946 append(dst, "front.");
947 else
948 append(dst, "back.");
949 }
950
951 static void
952 append_index(char *dst, GLint index)
953 {
954 char s[20];
955 sprintf(s, "[%d]", index);
956 append(dst, s);
957 }
958
959 /**
960 * Make a string from the given state vector.
961 * For example, return "state.matrix.texture[2].inverse".
962 * Use free() to deallocate the string.
963 */
964 char *
965 _mesa_program_state_string(const gl_state_index state[STATE_LENGTH])
966 {
967 char str[1000] = "";
968 char tmp[30];
969
970 append(str, "state.");
971 append_token(str, state[0]);
972
973 switch (state[0]) {
974 case STATE_MATERIAL:
975 append_face(str, state[1]);
976 append_token(str, state[2]);
977 break;
978 case STATE_LIGHT:
979 append_index(str, state[1]); /* light number [i]. */
980 append_token(str, state[2]); /* coefficients */
981 break;
982 case STATE_LIGHTMODEL_AMBIENT:
983 append(str, "lightmodel.ambient");
984 break;
985 case STATE_LIGHTMODEL_SCENECOLOR:
986 if (state[1] == 0) {
987 append(str, "lightmodel.front.scenecolor");
988 }
989 else {
990 append(str, "lightmodel.back.scenecolor");
991 }
992 break;
993 case STATE_LIGHTPROD:
994 append_index(str, state[1]); /* light number [i]. */
995 append_face(str, state[2]);
996 append_token(str, state[3]);
997 break;
998 case STATE_TEXGEN:
999 append_index(str, state[1]); /* tex unit [i] */
1000 append_token(str, state[2]); /* plane coef */
1001 break;
1002 case STATE_TEXENV_COLOR:
1003 append_index(str, state[1]); /* tex unit [i] */
1004 append(str, "color");
1005 break;
1006 case STATE_CLIPPLANE:
1007 append_index(str, state[1]); /* plane [i] */
1008 append(str, ".plane");
1009 break;
1010 case STATE_MODELVIEW_MATRIX:
1011 case STATE_PROJECTION_MATRIX:
1012 case STATE_MVP_MATRIX:
1013 case STATE_TEXTURE_MATRIX:
1014 case STATE_PROGRAM_MATRIX:
1015 {
1016 /* state[0] = modelview, projection, texture, etc. */
1017 /* state[1] = which texture matrix or program matrix */
1018 /* state[2] = first row to fetch */
1019 /* state[3] = last row to fetch */
1020 /* state[4] = transpose, inverse or invtrans */
1021 const gl_state_index mat = state[0];
1022 const GLuint index = (GLuint) state[1];
1023 const GLuint firstRow = (GLuint) state[2];
1024 const GLuint lastRow = (GLuint) state[3];
1025 const gl_state_index modifier = state[4];
1026 if (index ||
1027 mat == STATE_TEXTURE_MATRIX ||
1028 mat == STATE_PROGRAM_MATRIX)
1029 append_index(str, index);
1030 if (modifier)
1031 append_token(str, modifier);
1032 if (firstRow == lastRow)
1033 sprintf(tmp, ".row[%d]", firstRow);
1034 else
1035 sprintf(tmp, ".row[%d..%d]", firstRow, lastRow);
1036 append(str, tmp);
1037 }
1038 break;
1039 case STATE_POINT_SIZE:
1040 break;
1041 case STATE_POINT_ATTENUATION:
1042 break;
1043 case STATE_FOG_PARAMS:
1044 break;
1045 case STATE_FOG_COLOR:
1046 break;
1047 case STATE_DEPTH_RANGE:
1048 break;
1049 case STATE_FRAGMENT_PROGRAM:
1050 case STATE_VERTEX_PROGRAM:
1051 /* state[1] = {STATE_ENV, STATE_LOCAL} */
1052 /* state[2] = parameter index */
1053 append_token(str, state[1]);
1054 append_index(str, state[2]);
1055 break;
1056 case STATE_NORMAL_SCALE:
1057 break;
1058 case STATE_INTERNAL:
1059 append_token(str, state[1]);
1060 if (state[1] == STATE_CURRENT_ATTRIB)
1061 append_index(str, state[2]);
1062 break;
1063 default:
1064 _mesa_problem(NULL, "Invalid state in _mesa_program_state_string");
1065 break;
1066 }
1067
1068 return _mesa_strdup(str);
1069 }
1070
1071
1072 /**
1073 * Loop over all the parameters in a parameter list. If the parameter
1074 * is a GL state reference, look up the current value of that state
1075 * variable and put it into the parameter's Value[4] array.
1076 * Other parameter types never change or are explicitly set by the user
1077 * with glUniform() or glProgramParameter(), etc.
1078 * This would be called at glBegin time.
1079 */
1080 void
1081 _mesa_load_state_parameters(struct gl_context *ctx,
1082 struct gl_program_parameter_list *paramList)
1083 {
1084 GLuint i;
1085
1086 if (!paramList)
1087 return;
1088
1089 for (i = 0; i < paramList->NumParameters; i++) {
1090 if (paramList->Parameters[i].Type == PROGRAM_STATE_VAR) {
1091 _mesa_fetch_state(ctx,
1092 paramList->Parameters[i].StateIndexes,
1093 &paramList->ParameterValues[i][0].f);
1094 }
1095 }
1096 }
1097
1098
1099 /**
1100 * Copy the 16 elements of a matrix into four consecutive program
1101 * registers starting at 'pos'.
1102 */
1103 static void
1104 load_matrix(GLfloat registers[][4], GLuint pos, const GLfloat mat[16])
1105 {
1106 GLuint i;
1107 for (i = 0; i < 4; i++) {
1108 registers[pos + i][0] = mat[0 + i];
1109 registers[pos + i][1] = mat[4 + i];
1110 registers[pos + i][2] = mat[8 + i];
1111 registers[pos + i][3] = mat[12 + i];
1112 }
1113 }
1114
1115
1116 /**
1117 * As above, but transpose the matrix.
1118 */
1119 static void
1120 load_transpose_matrix(GLfloat registers[][4], GLuint pos,
1121 const GLfloat mat[16])
1122 {
1123 memcpy(registers[pos], mat, 16 * sizeof(GLfloat));
1124 }
1125
1126
1127 /**
1128 * Load current vertex program's parameter registers with tracked
1129 * matrices (if NV program). This only needs to be done per
1130 * glBegin/glEnd, not per-vertex.
1131 */
1132 void
1133 _mesa_load_tracked_matrices(struct gl_context *ctx)
1134 {
1135 GLuint i;
1136
1137 for (i = 0; i < MAX_NV_VERTEX_PROGRAM_PARAMS / 4; i++) {
1138 /* point 'mat' at source matrix */
1139 GLmatrix *mat;
1140 if (ctx->VertexProgram.TrackMatrix[i] == GL_MODELVIEW) {
1141 mat = ctx->ModelviewMatrixStack.Top;
1142 }
1143 else if (ctx->VertexProgram.TrackMatrix[i] == GL_PROJECTION) {
1144 mat = ctx->ProjectionMatrixStack.Top;
1145 }
1146 else if (ctx->VertexProgram.TrackMatrix[i] == GL_TEXTURE) {
1147 GLuint unit = MIN2(ctx->Texture.CurrentUnit,
1148 Elements(ctx->TextureMatrixStack) - 1);
1149 mat = ctx->TextureMatrixStack[unit].Top;
1150 }
1151 else if (ctx->VertexProgram.TrackMatrix[i]==GL_MODELVIEW_PROJECTION_NV) {
1152 /* XXX verify the combined matrix is up to date */
1153 mat = &ctx->_ModelProjectMatrix;
1154 }
1155 else if (ctx->VertexProgram.TrackMatrix[i] >= GL_MATRIX0_NV &&
1156 ctx->VertexProgram.TrackMatrix[i] <= GL_MATRIX7_NV) {
1157 GLuint n = ctx->VertexProgram.TrackMatrix[i] - GL_MATRIX0_NV;
1158 ASSERT(n < Elements(ctx->ProgramMatrixStack));
1159 mat = ctx->ProgramMatrixStack[n].Top;
1160 }
1161 else {
1162 /* no matrix is tracked, but we leave the register values as-is */
1163 assert(ctx->VertexProgram.TrackMatrix[i] == GL_NONE);
1164 continue;
1165 }
1166
1167 /* load the matrix values into sequential registers */
1168 if (ctx->VertexProgram.TrackMatrixTransform[i] == GL_IDENTITY_NV) {
1169 load_matrix(ctx->VertexProgram.Parameters, i*4, mat->m);
1170 }
1171 else if (ctx->VertexProgram.TrackMatrixTransform[i] == GL_INVERSE_NV) {
1172 _math_matrix_analyse(mat); /* update the inverse */
1173 ASSERT(!_math_matrix_is_dirty(mat));
1174 load_matrix(ctx->VertexProgram.Parameters, i*4, mat->inv);
1175 }
1176 else if (ctx->VertexProgram.TrackMatrixTransform[i] == GL_TRANSPOSE_NV) {
1177 load_transpose_matrix(ctx->VertexProgram.Parameters, i*4, mat->m);
1178 }
1179 else {
1180 assert(ctx->VertexProgram.TrackMatrixTransform[i]
1181 == GL_INVERSE_TRANSPOSE_NV);
1182 _math_matrix_analyse(mat); /* update the inverse */
1183 ASSERT(!_math_matrix_is_dirty(mat));
1184 load_transpose_matrix(ctx->VertexProgram.Parameters, i*4, mat->inv);
1185 }
1186 }
1187 }