2 * Mesa 3-D graphics library
5 * Copyright (C) 2005-2007 Brian Paul All Rights Reserved.
6 * Copyright (C) 2008 VMware, Inc. All Rights Reserved.
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice shall be included
16 * in all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
22 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 * \file slang_builtin.c
28 * Resolve built-in uniform vars.
32 #include "main/imports.h"
33 #include "main/mtypes.h"
34 #include "program/program.h"
35 #include "program/prog_instruction.h"
36 #include "program/prog_parameter.h"
37 #include "program/prog_statevars.h"
38 #include "slang/slang_builtin.h"
39 #include "slang/slang_compile_struct.h"
40 #include "slang/slang_ir.h"
41 #include "slang/slang_typeinfo.h"
44 /** special state token (see below) */
45 #define STATE_ARRAY ((gl_state_index) 0xfffff)
49 * Lookup GL state given a variable name, 0, 1 or 2 indexes and a field.
50 * Allocate room for the state in the given param list and return position
52 * Yes, this is kind of ugly, but it works.
55 lookup_statevar(const char *var
, GLint index1
, GLint index2
, const char *field
,
57 struct gl_program_parameter_list
*paramList
)
60 * NOTE: The ARB_vertex_program extension specified that matrices get
61 * loaded in registers in row-major order. With GLSL, we want column-
62 * major order. So, we need to transpose all matrices here...
66 gl_state_index matrix
;
67 gl_state_index modifier
;
69 { "gl_ModelViewMatrix", STATE_MODELVIEW_MATRIX
, STATE_MATRIX_TRANSPOSE
},
70 { "gl_ModelViewMatrixInverse", STATE_MODELVIEW_MATRIX
, STATE_MATRIX_INVTRANS
},
71 { "gl_ModelViewMatrixTranspose", STATE_MODELVIEW_MATRIX
, 0 },
72 { "gl_ModelViewMatrixInverseTranspose", STATE_MODELVIEW_MATRIX
, STATE_MATRIX_INVERSE
},
74 { "gl_ProjectionMatrix", STATE_PROJECTION_MATRIX
, STATE_MATRIX_TRANSPOSE
},
75 { "gl_ProjectionMatrixInverse", STATE_PROJECTION_MATRIX
, STATE_MATRIX_INVTRANS
},
76 { "gl_ProjectionMatrixTranspose", STATE_PROJECTION_MATRIX
, 0 },
77 { "gl_ProjectionMatrixInverseTranspose", STATE_PROJECTION_MATRIX
, STATE_MATRIX_INVERSE
},
79 { "gl_ModelViewProjectionMatrix", STATE_MVP_MATRIX
, STATE_MATRIX_TRANSPOSE
},
80 { "gl_ModelViewProjectionMatrixInverse", STATE_MVP_MATRIX
, STATE_MATRIX_INVTRANS
},
81 { "gl_ModelViewProjectionMatrixTranspose", STATE_MVP_MATRIX
, 0 },
82 { "gl_ModelViewProjectionMatrixInverseTranspose", STATE_MVP_MATRIX
, STATE_MATRIX_INVERSE
},
84 { "gl_TextureMatrix", STATE_TEXTURE_MATRIX
, STATE_MATRIX_TRANSPOSE
},
85 { "gl_TextureMatrixInverse", STATE_TEXTURE_MATRIX
, STATE_MATRIX_INVTRANS
},
86 { "gl_TextureMatrixTranspose", STATE_TEXTURE_MATRIX
, 0 },
87 { "gl_TextureMatrixInverseTranspose", STATE_TEXTURE_MATRIX
, STATE_MATRIX_INVERSE
},
89 { "gl_NormalMatrix", STATE_MODELVIEW_MATRIX
, STATE_MATRIX_INVERSE
},
93 gl_state_index tokens
[STATE_LENGTH
];
95 GLboolean isMatrix
= GL_FALSE
;
97 for (i
= 0; i
< STATE_LENGTH
; i
++) {
100 *swizzleOut
= SWIZZLE_NOOP
;
102 /* first, look if var is a pre-defined matrix */
103 for (i
= 0; matrices
[i
].name
; i
++) {
104 if (strcmp(var
, matrices
[i
].name
) == 0) {
105 tokens
[0] = matrices
[i
].matrix
;
106 /* tokens[1], [2] and [3] filled below */
107 tokens
[4] = matrices
[i
].modifier
;
114 if (tokens
[0] == STATE_TEXTURE_MATRIX
) {
115 /* texture_matrix[index1][index2] */
116 tokens
[1] = index1
>= 0 ? index1
: 0; /* which texture matrix */
117 index1
= index2
; /* move matrix row value to index1 */
120 /* index1 is unused: prevent extra addition at end of function */
124 else if (strcmp(var
, "gl_DepthRange") == 0) {
125 tokens
[0] = STATE_DEPTH_RANGE
;
127 if (strcmp(field
, "near") == 0) {
128 *swizzleOut
= SWIZZLE_XXXX
;
130 else if (strcmp(field
, "far") == 0) {
131 *swizzleOut
= SWIZZLE_YYYY
;
133 else if (strcmp(field
, "diff") == 0) {
134 *swizzleOut
= SWIZZLE_ZZZZ
;
140 else if (strcmp(var
, "gl_ClipPlane") == 0) {
143 tokens
[0] = STATE_CLIPPLANE
;
146 else if (strcmp(var
, "gl_Point") == 0) {
148 if (strcmp(field
, "size") == 0) {
149 tokens
[0] = STATE_POINT_SIZE
;
150 *swizzleOut
= SWIZZLE_XXXX
;
152 else if (strcmp(field
, "sizeMin") == 0) {
153 tokens
[0] = STATE_POINT_SIZE
;
154 *swizzleOut
= SWIZZLE_YYYY
;
156 else if (strcmp(field
, "sizeMax") == 0) {
157 tokens
[0] = STATE_POINT_SIZE
;
158 *swizzleOut
= SWIZZLE_ZZZZ
;
160 else if (strcmp(field
, "fadeThresholdSize") == 0) {
161 tokens
[0] = STATE_POINT_SIZE
;
162 *swizzleOut
= SWIZZLE_WWWW
;
164 else if (strcmp(field
, "distanceConstantAttenuation") == 0) {
165 tokens
[0] = STATE_POINT_ATTENUATION
;
166 *swizzleOut
= SWIZZLE_XXXX
;
168 else if (strcmp(field
, "distanceLinearAttenuation") == 0) {
169 tokens
[0] = STATE_POINT_ATTENUATION
;
170 *swizzleOut
= SWIZZLE_YYYY
;
172 else if (strcmp(field
, "distanceQuadraticAttenuation") == 0) {
173 tokens
[0] = STATE_POINT_ATTENUATION
;
174 *swizzleOut
= SWIZZLE_ZZZZ
;
180 else if (strcmp(var
, "gl_FrontMaterial") == 0 ||
181 strcmp(var
, "gl_BackMaterial") == 0) {
182 tokens
[0] = STATE_MATERIAL
;
183 if (strcmp(var
, "gl_FrontMaterial") == 0)
188 if (strcmp(field
, "emission") == 0) {
189 tokens
[2] = STATE_EMISSION
;
191 else if (strcmp(field
, "ambient") == 0) {
192 tokens
[2] = STATE_AMBIENT
;
194 else if (strcmp(field
, "diffuse") == 0) {
195 tokens
[2] = STATE_DIFFUSE
;
197 else if (strcmp(field
, "specular") == 0) {
198 tokens
[2] = STATE_SPECULAR
;
200 else if (strcmp(field
, "shininess") == 0) {
201 tokens
[2] = STATE_SHININESS
;
202 *swizzleOut
= SWIZZLE_XXXX
;
208 else if (strcmp(var
, "gl_LightSource") == 0) {
209 if (!field
|| index1
< 0)
212 tokens
[0] = STATE_LIGHT
;
215 if (strcmp(field
, "ambient") == 0) {
216 tokens
[2] = STATE_AMBIENT
;
218 else if (strcmp(field
, "diffuse") == 0) {
219 tokens
[2] = STATE_DIFFUSE
;
221 else if (strcmp(field
, "specular") == 0) {
222 tokens
[2] = STATE_SPECULAR
;
224 else if (strcmp(field
, "position") == 0) {
225 tokens
[2] = STATE_POSITION
;
227 else if (strcmp(field
, "halfVector") == 0) {
228 tokens
[2] = STATE_HALF_VECTOR
;
230 else if (strcmp(field
, "spotDirection") == 0) {
231 tokens
[2] = STATE_SPOT_DIRECTION
;
233 else if (strcmp(field
, "spotCosCutoff") == 0) {
234 tokens
[2] = STATE_SPOT_DIRECTION
;
235 *swizzleOut
= SWIZZLE_WWWW
;
237 else if (strcmp(field
, "spotCutoff") == 0) {
238 tokens
[2] = STATE_SPOT_CUTOFF
;
239 *swizzleOut
= SWIZZLE_XXXX
;
241 else if (strcmp(field
, "spotExponent") == 0) {
242 tokens
[2] = STATE_ATTENUATION
;
243 *swizzleOut
= SWIZZLE_WWWW
;
245 else if (strcmp(field
, "constantAttenuation") == 0) {
246 tokens
[2] = STATE_ATTENUATION
;
247 *swizzleOut
= SWIZZLE_XXXX
;
249 else if (strcmp(field
, "linearAttenuation") == 0) {
250 tokens
[2] = STATE_ATTENUATION
;
251 *swizzleOut
= SWIZZLE_YYYY
;
253 else if (strcmp(field
, "quadraticAttenuation") == 0) {
254 tokens
[2] = STATE_ATTENUATION
;
255 *swizzleOut
= SWIZZLE_ZZZZ
;
261 else if (strcmp(var
, "gl_LightModel") == 0) {
262 if (strcmp(field
, "ambient") == 0) {
263 tokens
[0] = STATE_LIGHTMODEL_AMBIENT
;
269 else if (strcmp(var
, "gl_FrontLightModelProduct") == 0) {
270 if (strcmp(field
, "sceneColor") == 0) {
271 tokens
[0] = STATE_LIGHTMODEL_SCENECOLOR
;
278 else if (strcmp(var
, "gl_BackLightModelProduct") == 0) {
279 if (strcmp(field
, "sceneColor") == 0) {
280 tokens
[0] = STATE_LIGHTMODEL_SCENECOLOR
;
287 else if (strcmp(var
, "gl_FrontLightProduct") == 0 ||
288 strcmp(var
, "gl_BackLightProduct") == 0) {
289 if (index1
< 0 || !field
)
292 tokens
[0] = STATE_LIGHTPROD
;
293 tokens
[1] = index1
; /* light number */
294 if (strcmp(var
, "gl_FrontLightProduct") == 0) {
295 tokens
[2] = 0; /* front */
298 tokens
[2] = 1; /* back */
300 if (strcmp(field
, "ambient") == 0) {
301 tokens
[3] = STATE_AMBIENT
;
303 else if (strcmp(field
, "diffuse") == 0) {
304 tokens
[3] = STATE_DIFFUSE
;
306 else if (strcmp(field
, "specular") == 0) {
307 tokens
[3] = STATE_SPECULAR
;
313 else if (strcmp(var
, "gl_TextureEnvColor") == 0) {
316 tokens
[0] = STATE_TEXENV_COLOR
;
319 else if (strcmp(var
, "gl_EyePlaneS") == 0) {
322 tokens
[0] = STATE_TEXGEN
;
323 tokens
[1] = index1
; /* tex unit */
324 tokens
[2] = STATE_TEXGEN_EYE_S
;
326 else if (strcmp(var
, "gl_EyePlaneT") == 0) {
329 tokens
[0] = STATE_TEXGEN
;
330 tokens
[1] = index1
; /* tex unit */
331 tokens
[2] = STATE_TEXGEN_EYE_T
;
333 else if (strcmp(var
, "gl_EyePlaneR") == 0) {
336 tokens
[0] = STATE_TEXGEN
;
337 tokens
[1] = index1
; /* tex unit */
338 tokens
[2] = STATE_TEXGEN_EYE_R
;
340 else if (strcmp(var
, "gl_EyePlaneQ") == 0) {
343 tokens
[0] = STATE_TEXGEN
;
344 tokens
[1] = index1
; /* tex unit */
345 tokens
[2] = STATE_TEXGEN_EYE_Q
;
347 else if (strcmp(var
, "gl_ObjectPlaneS") == 0) {
350 tokens
[0] = STATE_TEXGEN
;
351 tokens
[1] = index1
; /* tex unit */
352 tokens
[2] = STATE_TEXGEN_OBJECT_S
;
354 else if (strcmp(var
, "gl_ObjectPlaneT") == 0) {
357 tokens
[0] = STATE_TEXGEN
;
358 tokens
[1] = index1
; /* tex unit */
359 tokens
[2] = STATE_TEXGEN_OBJECT_T
;
361 else if (strcmp(var
, "gl_ObjectPlaneR") == 0) {
364 tokens
[0] = STATE_TEXGEN
;
365 tokens
[1] = index1
; /* tex unit */
366 tokens
[2] = STATE_TEXGEN_OBJECT_R
;
368 else if (strcmp(var
, "gl_ObjectPlaneQ") == 0) {
371 tokens
[0] = STATE_TEXGEN
;
372 tokens
[1] = index1
; /* tex unit */
373 tokens
[2] = STATE_TEXGEN_OBJECT_Q
;
375 else if (strcmp(var
, "gl_Fog") == 0) {
376 if (strcmp(field
, "color") == 0) {
377 tokens
[0] = STATE_FOG_COLOR
;
379 else if (strcmp(field
, "density") == 0) {
380 tokens
[0] = STATE_FOG_PARAMS
;
381 *swizzleOut
= SWIZZLE_XXXX
;
383 else if (strcmp(field
, "start") == 0) {
384 tokens
[0] = STATE_FOG_PARAMS
;
385 *swizzleOut
= SWIZZLE_YYYY
;
387 else if (strcmp(field
, "end") == 0) {
388 tokens
[0] = STATE_FOG_PARAMS
;
389 *swizzleOut
= SWIZZLE_ZZZZ
;
391 else if (strcmp(field
, "scale") == 0) {
392 tokens
[0] = STATE_FOG_PARAMS
;
393 *swizzleOut
= SWIZZLE_WWWW
;
404 /* load all four rows (or columns) of matrix */
407 for (j
= 0; j
< 4; j
++) {
408 tokens
[2] = tokens
[3] = j
; /* jth row of matrix */
409 pos
[j
] = _mesa_add_state_reference(paramList
, tokens
);
413 return pos
[0] + index1
;
416 /* allocate a single register */
417 GLint pos
= _mesa_add_state_reference(paramList
, tokens
);
426 * Given a variable name and datatype, emit uniform/constant buffer
427 * entries which will store that state variable.
428 * For example, if name="gl_LightSource" we'll emit 64 state variable
429 * vectors/references and return position where that data starts. This will
430 * allow run-time array indexing into the light source array.
432 * Note that this is a recursive function.
434 * \return -1 if error, else index of start of data in the program parameter list
437 emit_statevars(const char *name
, int array_len
,
438 const slang_type_specifier
*type
,
439 gl_state_index tokens
[STATE_LENGTH
],
440 struct gl_program_parameter_list
*paramList
)
442 if (type
->type
== SLANG_SPEC_ARRAY
) {
444 assert(array_len
> 0);
445 if (strcmp(name
, "gl_ClipPlane") == 0) {
446 tokens
[0] = STATE_CLIPPLANE
;
448 else if (strcmp(name
, "gl_LightSource") == 0) {
449 tokens
[0] = STATE_LIGHT
;
451 else if (strcmp(name
, "gl_FrontLightProduct") == 0) {
452 tokens
[0] = STATE_LIGHTPROD
;
453 tokens
[2] = 0; /* front */
455 else if (strcmp(name
, "gl_BackLightProduct") == 0) {
456 tokens
[0] = STATE_LIGHTPROD
;
457 tokens
[2] = 1; /* back */
459 else if (strcmp(name
, "gl_TextureEnvColor") == 0) {
460 tokens
[0] = STATE_TEXENV_COLOR
;
462 else if (strcmp(name
, "gl_EyePlaneS") == 0) {
463 tokens
[0] = STATE_TEXGEN
;
464 tokens
[2] = STATE_TEXGEN_EYE_S
;
466 else if (strcmp(name
, "gl_EyePlaneT") == 0) {
467 tokens
[0] = STATE_TEXGEN
;
468 tokens
[2] = STATE_TEXGEN_EYE_T
;
470 else if (strcmp(name
, "gl_EyePlaneR") == 0) {
471 tokens
[0] = STATE_TEXGEN
;
472 tokens
[2] = STATE_TEXGEN_EYE_R
;
474 else if (strcmp(name
, "gl_EyePlaneQ") == 0) {
475 tokens
[0] = STATE_TEXGEN
;
476 tokens
[2] = STATE_TEXGEN_EYE_Q
;
478 else if (strcmp(name
, "gl_ObjectPlaneS") == 0) {
479 tokens
[0] = STATE_TEXGEN
;
480 tokens
[2] = STATE_TEXGEN_OBJECT_S
;
482 else if (strcmp(name
, "gl_ObjectPlaneT") == 0) {
483 tokens
[0] = STATE_TEXGEN
;
484 tokens
[2] = STATE_TEXGEN_OBJECT_T
;
486 else if (strcmp(name
, "gl_ObjectPlaneR") == 0) {
487 tokens
[0] = STATE_TEXGEN
;
488 tokens
[2] = STATE_TEXGEN_OBJECT_R
;
490 else if (strcmp(name
, "gl_ObjectPlaneQ") == 0) {
491 tokens
[0] = STATE_TEXGEN
;
492 tokens
[2] = STATE_TEXGEN_OBJECT_Q
;
494 else if (strcmp(name
, "gl_TextureMatrix") == 0) {
495 tokens
[0] = STATE_TEXTURE_MATRIX
;
496 tokens
[4] = STATE_MATRIX_TRANSPOSE
;
498 else if (strcmp(name
, "gl_TextureMatrixInverse") == 0) {
499 tokens
[0] = STATE_TEXTURE_MATRIX
;
500 tokens
[4] = STATE_MATRIX_INVTRANS
;
502 else if (strcmp(name
, "gl_TextureMatrixTranspose") == 0) {
503 tokens
[0] = STATE_TEXTURE_MATRIX
;
506 else if (strcmp(name
, "gl_TextureMatrixInverseTranspose") == 0) {
507 tokens
[0] = STATE_TEXTURE_MATRIX
;
508 tokens
[4] = STATE_MATRIX_INVERSE
;
511 return -1; /* invalid array name */
513 /* emit state vars for each array element */
514 for (i
= 0; i
< array_len
; i
++) {
517 p
= emit_statevars(NULL
, 0, type
->_array
, tokens
, paramList
);
523 else if (type
->type
== SLANG_SPEC_STRUCT
) {
524 const slang_variable_scope
*fields
= type
->_struct
->fields
;
526 for (i
= 0; i
< fields
->num_variables
; i
++) {
527 const slang_variable
*var
= fields
->variables
[i
];
528 GLint p
= emit_statevars(var
->a_name
, 0, &var
->type
.specifier
,
535 else if (type
->type
== SLANG_SPEC_MAT4
) {
536 /* unroll/emit 4 array rows (or columns) */
537 slang_type_specifier vec4
;
538 GLint i
, p
, pos
= -1;
539 vec4
.type
= SLANG_SPEC_VEC4
;
540 for (i
= 0; i
< 4; i
++) {
541 tokens
[2] = tokens
[3] = i
; /* row[i] (or column[i]) of matrix */
542 p
= emit_statevars(NULL
, 0, &vec4
, tokens
, paramList
);
550 assert(type
->type
== SLANG_SPEC_VEC4
||
551 type
->type
== SLANG_SPEC_VEC3
||
552 type
->type
== SLANG_SPEC_VEC2
||
553 type
->type
== SLANG_SPEC_FLOAT
||
554 type
->type
== SLANG_SPEC_IVEC4
||
555 type
->type
== SLANG_SPEC_IVEC3
||
556 type
->type
== SLANG_SPEC_IVEC2
||
557 type
->type
== SLANG_SPEC_INT
);
561 if (tokens
[0] == STATE_LIGHT
)
563 else if (tokens
[0] == STATE_LIGHTPROD
)
566 return -1; /* invalid array name */
568 if (strcmp(name
, "ambient") == 0) {
569 tokens
[t
] = STATE_AMBIENT
;
571 else if (strcmp(name
, "diffuse") == 0) {
572 tokens
[t
] = STATE_DIFFUSE
;
574 else if (strcmp(name
, "specular") == 0) {
575 tokens
[t
] = STATE_SPECULAR
;
577 else if (strcmp(name
, "position") == 0) {
578 tokens
[t
] = STATE_POSITION
;
580 else if (strcmp(name
, "halfVector") == 0) {
581 tokens
[t
] = STATE_HALF_VECTOR
;
583 else if (strcmp(name
, "spotDirection") == 0) {
584 tokens
[t
] = STATE_SPOT_DIRECTION
; /* xyz components */
586 else if (strcmp(name
, "spotCosCutoff") == 0) {
587 tokens
[t
] = STATE_SPOT_DIRECTION
; /* w component */
590 else if (strcmp(name
, "constantAttenuation") == 0) {
591 tokens
[t
] = STATE_ATTENUATION
; /* x component */
593 else if (strcmp(name
, "linearAttenuation") == 0) {
594 tokens
[t
] = STATE_ATTENUATION
; /* y component */
596 else if (strcmp(name
, "quadraticAttenuation") == 0) {
597 tokens
[t
] = STATE_ATTENUATION
; /* z component */
599 else if (strcmp(name
, "spotExponent") == 0) {
600 tokens
[t
] = STATE_ATTENUATION
; /* w = spot exponent */
603 else if (strcmp(name
, "spotCutoff") == 0) {
604 tokens
[t
] = STATE_SPOT_CUTOFF
; /* x component */
608 return -1; /* invalid field name */
612 pos
= _mesa_add_state_reference(paramList
, tokens
);
621 * Unroll the named built-in uniform variable into a sequence of state
622 * vars in the given parameter list.
625 alloc_state_var_array(const slang_variable
*var
,
626 struct gl_program_parameter_list
*paramList
)
628 gl_state_index tokens
[STATE_LENGTH
];
632 /* Initialize the state tokens array. This is very important.
633 * When we call _mesa_add_state_reference() it'll searches the parameter
634 * list to see if the given statevar token sequence is already present.
635 * This is normally a good thing since it prevents redundant values in the
638 * But when we're building arrays of state this can be bad. For example,
639 * consider this fragment of GLSL code:
640 * foo = gl_LightSource[3].diffuse;
642 * bar = gl_LightSource[i].diffuse;
644 * When we unroll the gl_LightSource array (for "bar") we want to re-emit
645 * gl_LightSource[3].diffuse and not re-use the first instance (from "foo")
646 * since that would upset the array layout. We handle this situation by
647 * setting the last token in the state var token array to the special
649 * This token will only be set for array state. We can hijack the last
650 * element in the array for this since it's never used for light, clipplane
651 * or texture env array state.
653 for (i
= 0; i
< STATE_LENGTH
; i
++)
655 tokens
[STATE_LENGTH
- 1] = STATE_ARRAY
;
657 pos
= emit_statevars(var
->a_name
, var
->array_len
, &var
->type
.specifier
,
666 * Allocate storage for a pre-defined uniform (a GL state variable).
667 * As a memory-saving optimization, we try to only allocate storage for
668 * state vars that are actually used.
670 * Arrays such as gl_LightSource are handled specially. For an expression
671 * like "gl_LightSource[2].diffuse", we can allocate a single uniform/constant
672 * slot and return the index. In this case, we return direct=TRUE.
674 * Buf for something like "gl_LightSource[i].diffuse" we don't know the value
675 * of 'i' at compile time so we need to "unroll" the gl_LightSource array
676 * into a consecutive sequence of uniform/constant slots so it can be indexed
677 * at runtime. In this case, we return direct=FALSE.
679 * Currently, all pre-defined uniforms are in one of these forms:
686 * \return -1 upon error, else position in paramList of the state variable/data
689 _slang_alloc_statevar(slang_ir_node
*n
,
690 struct gl_program_parameter_list
*paramList
,
693 slang_ir_node
*n0
= n
;
694 const char *field
= NULL
;
695 GLint index1
= -1, index2
= -1;
700 if (n
->Opcode
== IR_FIELD
) {
705 if (n
->Opcode
== IR_ELEMENT
) {
706 if (n
->Children
[1]->Opcode
== IR_FLOAT
) {
707 index1
= (GLint
) n
->Children
[1]->Value
[0];
715 if (n
->Opcode
== IR_ELEMENT
) {
716 /* XXX can only handle constant indexes for now */
717 if (n
->Children
[1]->Opcode
== IR_FLOAT
) {
718 /* two-dimensional array index: mat[i][j] */
720 index1
= (GLint
) n
->Children
[1]->Value
[0];
728 assert(n
->Opcode
== IR_VAR
);
731 const char *var
= (const char *) n
->Var
->a_name
;
733 lookup_statevar(var
, index1
, index2
, field
, &swizzle
, paramList
);
735 /* newly resolved storage for the statevar/constant/uniform */
736 n0
->Store
->File
= PROGRAM_STATE_VAR
;
737 n0
->Store
->Index
= pos
;
738 n0
->Store
->Swizzle
= swizzle
;
739 n0
->Store
->Parent
= NULL
;
745 return alloc_state_var_array(n
->Var
, paramList
);
751 #define SWIZZLE_ZWWW MAKE_SWIZZLE4(SWIZZLE_Z, SWIZZLE_W, SWIZZLE_W, SWIZZLE_W)
754 /** Predefined shader inputs */
764 /** Predefined vertex shader inputs/attributes */
765 static const struct input_info vertInputs
[] = {
766 { "gl_Vertex", VERT_ATTRIB_POS
, GL_FLOAT_VEC4
, SWIZZLE_NOOP
, GL_FALSE
},
767 { "gl_Normal", VERT_ATTRIB_NORMAL
, GL_FLOAT_VEC3
, SWIZZLE_NOOP
, GL_FALSE
},
768 { "gl_Color", VERT_ATTRIB_COLOR0
, GL_FLOAT_VEC4
, SWIZZLE_NOOP
, GL_FALSE
},
769 { "gl_SecondaryColor", VERT_ATTRIB_COLOR1
, GL_FLOAT_VEC4
, SWIZZLE_NOOP
, GL_FALSE
},
770 { "gl_FogCoord", VERT_ATTRIB_FOG
, GL_FLOAT
, SWIZZLE_XXXX
, GL_FALSE
},
771 { "gl_MultiTexCoord0", VERT_ATTRIB_TEX0
, GL_FLOAT_VEC4
, SWIZZLE_NOOP
, GL_FALSE
},
772 { "gl_MultiTexCoord1", VERT_ATTRIB_TEX1
, GL_FLOAT_VEC4
, SWIZZLE_NOOP
, GL_FALSE
},
773 { "gl_MultiTexCoord2", VERT_ATTRIB_TEX2
, GL_FLOAT_VEC4
, SWIZZLE_NOOP
, GL_FALSE
},
774 { "gl_MultiTexCoord3", VERT_ATTRIB_TEX3
, GL_FLOAT_VEC4
, SWIZZLE_NOOP
, GL_FALSE
},
775 { "gl_MultiTexCoord4", VERT_ATTRIB_TEX4
, GL_FLOAT_VEC4
, SWIZZLE_NOOP
, GL_FALSE
},
776 { "gl_MultiTexCoord5", VERT_ATTRIB_TEX5
, GL_FLOAT_VEC4
, SWIZZLE_NOOP
, GL_FALSE
},
777 { "gl_MultiTexCoord6", VERT_ATTRIB_TEX6
, GL_FLOAT_VEC4
, SWIZZLE_NOOP
, GL_FALSE
},
778 { "gl_MultiTexCoord7", VERT_ATTRIB_TEX7
, GL_FLOAT_VEC4
, SWIZZLE_NOOP
, GL_FALSE
}
781 static const struct input_info geomInputs
[] = {
782 { "gl_PrimitiveIDIn", GEOM_ATTRIB_PRIMITIVE_ID
, GL_FLOAT
, SWIZZLE_NOOP
, GL_FALSE
},
783 { "gl_FrontColorIn", GEOM_ATTRIB_COLOR0
, GL_FLOAT_VEC4
, SWIZZLE_NOOP
, GL_TRUE
},
784 { "gl_BackColorIn", GEOM_ATTRIB_COLOR1
, GL_FLOAT_VEC4
, SWIZZLE_NOOP
, GL_TRUE
},
785 { "gl_FrontSecondaryColorIn", GEOM_ATTRIB_SECONDARY_COLOR0
, GL_FLOAT_VEC4
, SWIZZLE_NOOP
, GL_TRUE
},
786 { "gl_BackSecondaryColorIn", GEOM_ATTRIB_SECONDARY_COLOR1
, GL_FLOAT_VEC4
, SWIZZLE_NOOP
, GL_TRUE
},
787 { "gl_TexCoordIn", GEOM_ATTRIB_TEX_COORD
, GL_FLOAT_VEC4
, SWIZZLE_NOOP
, GL_TRUE
},
788 { "gl_FogFragCoordIn", GEOM_ATTRIB_FOG_FRAG_COORD
, GL_FLOAT
, SWIZZLE_NOOP
, GL_TRUE
},
789 { "gl_PositionIn", GEOM_ATTRIB_POSITION
, GL_FLOAT_VEC4
, SWIZZLE_NOOP
, GL_TRUE
},
790 { "gl_ClipVertexIn", GEOM_ATTRIB_CLIP_VERTEX
, GL_FLOAT_VEC4
, SWIZZLE_NOOP
, GL_TRUE
},
791 { "gl_PointSizeIn", GEOM_ATTRIB_POINT_SIZE
, GL_FLOAT
, SWIZZLE_NOOP
, GL_TRUE
}
794 /** Predefined fragment shader inputs */
795 static const struct input_info fragInputs
[] = {
796 { "gl_FragCoord", FRAG_ATTRIB_WPOS
, GL_FLOAT_VEC4
, SWIZZLE_NOOP
, GL_FALSE
},
797 { "gl_Color", FRAG_ATTRIB_COL0
, GL_FLOAT_VEC4
, SWIZZLE_NOOP
, GL_FALSE
},
798 { "gl_SecondaryColor", FRAG_ATTRIB_COL1
, GL_FLOAT_VEC4
, SWIZZLE_NOOP
, GL_FALSE
},
799 { "gl_TexCoord", FRAG_ATTRIB_TEX0
, GL_FLOAT_VEC4
, SWIZZLE_NOOP
, GL_FALSE
},
800 { "gl_FogFragCoord", FRAG_ATTRIB_FOGC
, GL_FLOAT
, SWIZZLE_XXXX
, GL_FALSE
},
801 { "gl_FrontFacing", FRAG_ATTRIB_FACE
, GL_FLOAT
, SWIZZLE_XXXX
, GL_FALSE
},
802 { "gl_PointCoord", FRAG_ATTRIB_PNTC
, GL_FLOAT_VEC2
, SWIZZLE_XYZW
, GL_FALSE
}
807 * Return the VERT_ATTRIB_* or FRAG_ATTRIB_* value that corresponds to
808 * a vertex or fragment program input variable. Return -1 if the input
810 * XXX return size too
813 _slang_input_index(const char *name
, GLenum target
, GLuint
*swizzleOut
,
816 const struct input_info
*inputs
;
820 case GL_VERTEX_PROGRAM_ARB
:
822 n
= Elements(vertInputs
);
824 case GL_FRAGMENT_PROGRAM_ARB
:
826 n
= Elements(fragInputs
);
828 case MESA_GEOMETRY_PROGRAM
:
830 n
= Elements(geomInputs
);
833 _mesa_problem(NULL
, "bad target in _slang_input_index");
837 ASSERT(MAX_TEXTURE_COORD_UNITS
== 8); /* if this fails, fix vertInputs above */
839 for (i
= 0; i
< n
; i
++) {
840 if (strcmp(inputs
[i
].Name
, name
) == 0) {
842 *swizzleOut
= inputs
[i
].Swizzle
;
844 *is_array
= inputs
[i
].Array
;
845 return inputs
[i
].Attrib
;
853 * Return name of the given vertex attribute (VERT_ATTRIB_x).
856 _slang_vert_attrib_name(GLuint attrib
)
859 assert(attrib
< VERT_ATTRIB_GENERIC0
);
860 for (i
= 0; Elements(vertInputs
); i
++) {
861 if (vertInputs
[i
].Attrib
== attrib
)
862 return vertInputs
[i
].Name
;
869 * Return type (GL_FLOAT, GL_FLOAT_VEC2, etc) of the given vertex
870 * attribute (VERT_ATTRIB_x).
873 _slang_vert_attrib_type(GLuint attrib
)
876 assert(attrib
< VERT_ATTRIB_GENERIC0
);
877 for (i
= 0; Elements(vertInputs
); i
++) {
878 if (vertInputs
[i
].Attrib
== attrib
)
879 return vertInputs
[i
].Type
;
888 /** Predefined shader output info */
896 /** Predefined vertex shader outputs */
897 static const struct output_info vertOutputs
[] = {
898 { "gl_Position", VERT_RESULT_HPOS
, GL_FLOAT_VEC4
},
899 { "gl_FrontColor", VERT_RESULT_COL0
, GL_FLOAT_VEC4
},
900 { "gl_BackColor", VERT_RESULT_BFC0
, GL_FLOAT_VEC4
},
901 { "gl_FrontSecondaryColor", VERT_RESULT_COL1
, GL_FLOAT_VEC4
},
902 { "gl_BackSecondaryColor", VERT_RESULT_BFC1
, GL_FLOAT_VEC4
},
903 { "gl_TexCoord", VERT_RESULT_TEX0
, GL_FLOAT_VEC4
},
904 { "gl_FogFragCoord", VERT_RESULT_FOGC
, GL_FLOAT
},
905 { "gl_PointSize", VERT_RESULT_PSIZ
, GL_FLOAT
}
908 /** Predefined geometry shader outputs */
909 static const struct output_info geomOutputs
[] = {
910 { "gl_Position", GEOM_RESULT_POS
, GL_FLOAT_VEC4
},
911 { "gl_FrontColor", GEOM_RESULT_COL0
, GL_FLOAT_VEC4
},
912 { "gl_BackColor", GEOM_RESULT_COL1
, GL_FLOAT_VEC4
},
913 { "gl_FrontSecondaryColor", GEOM_RESULT_SCOL0
, GL_FLOAT_VEC4
},
914 { "gl_BackSecondaryColor", GEOM_RESULT_SCOL1
, GL_FLOAT_VEC4
},
915 { "gl_TexCoord", GEOM_RESULT_TEX0
, GL_FLOAT_VEC4
},
916 { "gl_FogFragCoord", GEOM_RESULT_FOGC
, GL_FLOAT
},
917 { "gl_ClipVertex", GEOM_RESULT_CLPV
, GL_FLOAT_VEC4
},
918 { "gl_PointSize", GEOM_RESULT_PSIZ
, GL_FLOAT
},
919 { "gl_PrimitiveID", GEOM_RESULT_PRID
, GL_FLOAT
},
920 { "gl_Layer", GEOM_RESULT_LAYR
, GL_FLOAT
}
923 /** Predefined fragment shader outputs */
924 static const struct output_info fragOutputs
[] = {
925 { "gl_FragColor", FRAG_RESULT_COLOR
, GL_FLOAT_VEC4
},
926 { "gl_FragDepth", FRAG_RESULT_DEPTH
, GL_FLOAT
},
927 { "gl_FragData", FRAG_RESULT_DATA0
, GL_FLOAT_VEC4
}
932 * Return the VERT_RESULT_*, GEOM_RESULT_* or FRAG_RESULT_* value that corresponds
933 * to a vertex or fragment program output variable. Return -1 for an invalid
937 _slang_output_index(const char *name
, GLenum target
)
939 const struct output_info
*outputs
;
943 case GL_VERTEX_PROGRAM_ARB
:
944 outputs
= vertOutputs
;
945 n
= Elements(vertOutputs
);
947 case GL_FRAGMENT_PROGRAM_ARB
:
948 outputs
= fragOutputs
;
949 n
= Elements(fragOutputs
);
951 case MESA_GEOMETRY_PROGRAM
:
952 outputs
= geomOutputs
;
953 n
= Elements(geomOutputs
);
956 _mesa_problem(NULL
, "bad target in _slang_output_index");
960 for (i
= 0; i
< n
; i
++) {
961 if (strcmp(outputs
[i
].Name
, name
) == 0) {
963 return outputs
[i
].Attrib
;
971 * Given a VERT_RESULT_x index, return the corresponding string name.
974 _slang_vertex_output_name(gl_vert_result index
)
976 if (index
< Elements(vertOutputs
))
977 return vertOutputs
[index
].Name
;
984 * Given a GEOM_RESULT_x index, return the corresponding string name.
987 _slang_geometry_output_name(gl_geom_result index
)
989 if (index
< Elements(geomOutputs
))
990 return geomOutputs
[index
].Name
;
997 * Given a FRAG_RESULT_x index, return the corresponding string name.
1000 _slang_fragment_output_name(gl_frag_result index
)
1002 if (index
< Elements(fragOutputs
))
1003 return fragOutputs
[index
].Name
;
1010 * Given a VERT_RESULT_x index, return the corresponding varying
1014 _slang_vertex_output_type(gl_vert_result index
)
1016 if (index
< Elements(vertOutputs
))
1017 return vertOutputs
[index
].Type
;