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