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_ir.h"
39 #include "slang/slang_builtin.h"
42 /** special state token (see below) */
43 #define STATE_ARRAY ((gl_state_index) 0xfffff)
47 * Lookup GL state given a variable name, 0, 1 or 2 indexes and a field.
48 * Allocate room for the state in the given param list and return position
50 * Yes, this is kind of ugly, but it works.
53 lookup_statevar(const char *var
, GLint index1
, GLint index2
, const char *field
,
55 struct gl_program_parameter_list
*paramList
)
58 * NOTE: The ARB_vertex_program extension specified that matrices get
59 * loaded in registers in row-major order. With GLSL, we want column-
60 * major order. So, we need to transpose all matrices here...
64 gl_state_index matrix
;
65 gl_state_index modifier
;
67 { "gl_ModelViewMatrix", STATE_MODELVIEW_MATRIX
, STATE_MATRIX_TRANSPOSE
},
68 { "gl_ModelViewMatrixInverse", STATE_MODELVIEW_MATRIX
, STATE_MATRIX_INVTRANS
},
69 { "gl_ModelViewMatrixTranspose", STATE_MODELVIEW_MATRIX
, 0 },
70 { "gl_ModelViewMatrixInverseTranspose", STATE_MODELVIEW_MATRIX
, STATE_MATRIX_INVERSE
},
72 { "gl_ProjectionMatrix", STATE_PROJECTION_MATRIX
, STATE_MATRIX_TRANSPOSE
},
73 { "gl_ProjectionMatrixInverse", STATE_PROJECTION_MATRIX
, STATE_MATRIX_INVTRANS
},
74 { "gl_ProjectionMatrixTranspose", STATE_PROJECTION_MATRIX
, 0 },
75 { "gl_ProjectionMatrixInverseTranspose", STATE_PROJECTION_MATRIX
, STATE_MATRIX_INVERSE
},
77 { "gl_ModelViewProjectionMatrix", STATE_MVP_MATRIX
, STATE_MATRIX_TRANSPOSE
},
78 { "gl_ModelViewProjectionMatrixInverse", STATE_MVP_MATRIX
, STATE_MATRIX_INVTRANS
},
79 { "gl_ModelViewProjectionMatrixTranspose", STATE_MVP_MATRIX
, 0 },
80 { "gl_ModelViewProjectionMatrixInverseTranspose", STATE_MVP_MATRIX
, STATE_MATRIX_INVERSE
},
82 { "gl_TextureMatrix", STATE_TEXTURE_MATRIX
, STATE_MATRIX_TRANSPOSE
},
83 { "gl_TextureMatrixInverse", STATE_TEXTURE_MATRIX
, STATE_MATRIX_INVTRANS
},
84 { "gl_TextureMatrixTranspose", STATE_TEXTURE_MATRIX
, 0 },
85 { "gl_TextureMatrixInverseTranspose", STATE_TEXTURE_MATRIX
, STATE_MATRIX_INVERSE
},
87 { "gl_NormalMatrix", STATE_MODELVIEW_MATRIX
, STATE_MATRIX_INVERSE
},
91 gl_state_index tokens
[STATE_LENGTH
];
93 GLboolean isMatrix
= GL_FALSE
;
95 for (i
= 0; i
< STATE_LENGTH
; i
++) {
98 *swizzleOut
= SWIZZLE_NOOP
;
100 /* first, look if var is a pre-defined matrix */
101 for (i
= 0; matrices
[i
].name
; i
++) {
102 if (strcmp(var
, matrices
[i
].name
) == 0) {
103 tokens
[0] = matrices
[i
].matrix
;
104 /* tokens[1], [2] and [3] filled below */
105 tokens
[4] = matrices
[i
].modifier
;
112 if (tokens
[0] == STATE_TEXTURE_MATRIX
) {
113 /* texture_matrix[index1][index2] */
114 tokens
[1] = index1
>= 0 ? index1
: 0; /* which texture matrix */
115 index1
= index2
; /* move matrix row value to index1 */
118 /* index1 is unused: prevent extra addition at end of function */
122 else if (strcmp(var
, "gl_DepthRange") == 0) {
123 tokens
[0] = STATE_DEPTH_RANGE
;
125 if (strcmp(field
, "near") == 0) {
126 *swizzleOut
= SWIZZLE_XXXX
;
128 else if (strcmp(field
, "far") == 0) {
129 *swizzleOut
= SWIZZLE_YYYY
;
131 else if (strcmp(field
, "diff") == 0) {
132 *swizzleOut
= SWIZZLE_ZZZZ
;
138 else if (strcmp(var
, "gl_ClipPlane") == 0) {
141 tokens
[0] = STATE_CLIPPLANE
;
144 else if (strcmp(var
, "gl_Point") == 0) {
146 if (strcmp(field
, "size") == 0) {
147 tokens
[0] = STATE_POINT_SIZE
;
148 *swizzleOut
= SWIZZLE_XXXX
;
150 else if (strcmp(field
, "sizeMin") == 0) {
151 tokens
[0] = STATE_POINT_SIZE
;
152 *swizzleOut
= SWIZZLE_YYYY
;
154 else if (strcmp(field
, "sizeMax") == 0) {
155 tokens
[0] = STATE_POINT_SIZE
;
156 *swizzleOut
= SWIZZLE_ZZZZ
;
158 else if (strcmp(field
, "fadeThresholdSize") == 0) {
159 tokens
[0] = STATE_POINT_SIZE
;
160 *swizzleOut
= SWIZZLE_WWWW
;
162 else if (strcmp(field
, "distanceConstantAttenuation") == 0) {
163 tokens
[0] = STATE_POINT_ATTENUATION
;
164 *swizzleOut
= SWIZZLE_XXXX
;
166 else if (strcmp(field
, "distanceLinearAttenuation") == 0) {
167 tokens
[0] = STATE_POINT_ATTENUATION
;
168 *swizzleOut
= SWIZZLE_YYYY
;
170 else if (strcmp(field
, "distanceQuadraticAttenuation") == 0) {
171 tokens
[0] = STATE_POINT_ATTENUATION
;
172 *swizzleOut
= SWIZZLE_ZZZZ
;
178 else if (strcmp(var
, "gl_FrontMaterial") == 0 ||
179 strcmp(var
, "gl_BackMaterial") == 0) {
180 tokens
[0] = STATE_MATERIAL
;
181 if (strcmp(var
, "gl_FrontMaterial") == 0)
186 if (strcmp(field
, "emission") == 0) {
187 tokens
[2] = STATE_EMISSION
;
189 else if (strcmp(field
, "ambient") == 0) {
190 tokens
[2] = STATE_AMBIENT
;
192 else if (strcmp(field
, "diffuse") == 0) {
193 tokens
[2] = STATE_DIFFUSE
;
195 else if (strcmp(field
, "specular") == 0) {
196 tokens
[2] = STATE_SPECULAR
;
198 else if (strcmp(field
, "shininess") == 0) {
199 tokens
[2] = STATE_SHININESS
;
200 *swizzleOut
= SWIZZLE_XXXX
;
206 else if (strcmp(var
, "gl_LightSource") == 0) {
207 if (!field
|| index1
< 0)
210 tokens
[0] = STATE_LIGHT
;
213 if (strcmp(field
, "ambient") == 0) {
214 tokens
[2] = STATE_AMBIENT
;
216 else if (strcmp(field
, "diffuse") == 0) {
217 tokens
[2] = STATE_DIFFUSE
;
219 else if (strcmp(field
, "specular") == 0) {
220 tokens
[2] = STATE_SPECULAR
;
222 else if (strcmp(field
, "position") == 0) {
223 tokens
[2] = STATE_POSITION
;
225 else if (strcmp(field
, "halfVector") == 0) {
226 tokens
[2] = STATE_HALF_VECTOR
;
228 else if (strcmp(field
, "spotDirection") == 0) {
229 tokens
[2] = STATE_SPOT_DIRECTION
;
231 else if (strcmp(field
, "spotCosCutoff") == 0) {
232 tokens
[2] = STATE_SPOT_DIRECTION
;
233 *swizzleOut
= SWIZZLE_WWWW
;
235 else if (strcmp(field
, "spotCutoff") == 0) {
236 tokens
[2] = STATE_SPOT_CUTOFF
;
237 *swizzleOut
= SWIZZLE_XXXX
;
239 else if (strcmp(field
, "spotExponent") == 0) {
240 tokens
[2] = STATE_ATTENUATION
;
241 *swizzleOut
= SWIZZLE_WWWW
;
243 else if (strcmp(field
, "constantAttenuation") == 0) {
244 tokens
[2] = STATE_ATTENUATION
;
245 *swizzleOut
= SWIZZLE_XXXX
;
247 else if (strcmp(field
, "linearAttenuation") == 0) {
248 tokens
[2] = STATE_ATTENUATION
;
249 *swizzleOut
= SWIZZLE_YYYY
;
251 else if (strcmp(field
, "quadraticAttenuation") == 0) {
252 tokens
[2] = STATE_ATTENUATION
;
253 *swizzleOut
= SWIZZLE_ZZZZ
;
259 else if (strcmp(var
, "gl_LightModel") == 0) {
260 if (strcmp(field
, "ambient") == 0) {
261 tokens
[0] = STATE_LIGHTMODEL_AMBIENT
;
267 else if (strcmp(var
, "gl_FrontLightModelProduct") == 0) {
268 if (strcmp(field
, "sceneColor") == 0) {
269 tokens
[0] = STATE_LIGHTMODEL_SCENECOLOR
;
276 else if (strcmp(var
, "gl_BackLightModelProduct") == 0) {
277 if (strcmp(field
, "sceneColor") == 0) {
278 tokens
[0] = STATE_LIGHTMODEL_SCENECOLOR
;
285 else if (strcmp(var
, "gl_FrontLightProduct") == 0 ||
286 strcmp(var
, "gl_BackLightProduct") == 0) {
287 if (index1
< 0 || !field
)
290 tokens
[0] = STATE_LIGHTPROD
;
291 tokens
[1] = index1
; /* light number */
292 if (strcmp(var
, "gl_FrontLightProduct") == 0) {
293 tokens
[2] = 0; /* front */
296 tokens
[2] = 1; /* back */
298 if (strcmp(field
, "ambient") == 0) {
299 tokens
[3] = STATE_AMBIENT
;
301 else if (strcmp(field
, "diffuse") == 0) {
302 tokens
[3] = STATE_DIFFUSE
;
304 else if (strcmp(field
, "specular") == 0) {
305 tokens
[3] = STATE_SPECULAR
;
311 else if (strcmp(var
, "gl_TextureEnvColor") == 0) {
314 tokens
[0] = STATE_TEXENV_COLOR
;
317 else if (strcmp(var
, "gl_EyePlaneS") == 0) {
320 tokens
[0] = STATE_TEXGEN
;
321 tokens
[1] = index1
; /* tex unit */
322 tokens
[2] = STATE_TEXGEN_EYE_S
;
324 else if (strcmp(var
, "gl_EyePlaneT") == 0) {
327 tokens
[0] = STATE_TEXGEN
;
328 tokens
[1] = index1
; /* tex unit */
329 tokens
[2] = STATE_TEXGEN_EYE_T
;
331 else if (strcmp(var
, "gl_EyePlaneR") == 0) {
334 tokens
[0] = STATE_TEXGEN
;
335 tokens
[1] = index1
; /* tex unit */
336 tokens
[2] = STATE_TEXGEN_EYE_R
;
338 else if (strcmp(var
, "gl_EyePlaneQ") == 0) {
341 tokens
[0] = STATE_TEXGEN
;
342 tokens
[1] = index1
; /* tex unit */
343 tokens
[2] = STATE_TEXGEN_EYE_Q
;
345 else if (strcmp(var
, "gl_ObjectPlaneS") == 0) {
348 tokens
[0] = STATE_TEXGEN
;
349 tokens
[1] = index1
; /* tex unit */
350 tokens
[2] = STATE_TEXGEN_OBJECT_S
;
352 else if (strcmp(var
, "gl_ObjectPlaneT") == 0) {
355 tokens
[0] = STATE_TEXGEN
;
356 tokens
[1] = index1
; /* tex unit */
357 tokens
[2] = STATE_TEXGEN_OBJECT_T
;
359 else if (strcmp(var
, "gl_ObjectPlaneR") == 0) {
362 tokens
[0] = STATE_TEXGEN
;
363 tokens
[1] = index1
; /* tex unit */
364 tokens
[2] = STATE_TEXGEN_OBJECT_R
;
366 else if (strcmp(var
, "gl_ObjectPlaneQ") == 0) {
369 tokens
[0] = STATE_TEXGEN
;
370 tokens
[1] = index1
; /* tex unit */
371 tokens
[2] = STATE_TEXGEN_OBJECT_Q
;
373 else if (strcmp(var
, "gl_Fog") == 0) {
374 if (strcmp(field
, "color") == 0) {
375 tokens
[0] = STATE_FOG_COLOR
;
377 else if (strcmp(field
, "density") == 0) {
378 tokens
[0] = STATE_FOG_PARAMS
;
379 *swizzleOut
= SWIZZLE_XXXX
;
381 else if (strcmp(field
, "start") == 0) {
382 tokens
[0] = STATE_FOG_PARAMS
;
383 *swizzleOut
= SWIZZLE_YYYY
;
385 else if (strcmp(field
, "end") == 0) {
386 tokens
[0] = STATE_FOG_PARAMS
;
387 *swizzleOut
= SWIZZLE_ZZZZ
;
389 else if (strcmp(field
, "scale") == 0) {
390 tokens
[0] = STATE_FOG_PARAMS
;
391 *swizzleOut
= SWIZZLE_WWWW
;
402 /* load all four rows (or columns) of matrix */
405 for (j
= 0; j
< 4; j
++) {
406 tokens
[2] = tokens
[3] = j
; /* jth row of matrix */
407 pos
[j
] = _mesa_add_state_reference(paramList
, tokens
);
411 return pos
[0] + index1
;
414 /* allocate a single register */
415 GLint pos
= _mesa_add_state_reference(paramList
, tokens
);
424 * Given a variable name and datatype, emit uniform/constant buffer
425 * entries which will store that state variable.
426 * For example, if name="gl_LightSource" we'll emit 64 state variable
427 * vectors/references and return position where that data starts. This will
428 * allow run-time array indexing into the light source array.
430 * Note that this is a recursive function.
432 * \return -1 if error, else index of start of data in the program parameter list
435 emit_statevars(const char *name
, int array_len
,
436 const slang_type_specifier
*type
,
437 gl_state_index tokens
[STATE_LENGTH
],
438 struct gl_program_parameter_list
*paramList
)
440 if (type
->type
== SLANG_SPEC_ARRAY
) {
442 assert(array_len
> 0);
443 if (strcmp(name
, "gl_ClipPlane") == 0) {
444 tokens
[0] = STATE_CLIPPLANE
;
446 else if (strcmp(name
, "gl_LightSource") == 0) {
447 tokens
[0] = STATE_LIGHT
;
449 else if (strcmp(name
, "gl_FrontLightProduct") == 0) {
450 tokens
[0] = STATE_LIGHTPROD
;
451 tokens
[2] = 0; /* front */
453 else if (strcmp(name
, "gl_BackLightProduct") == 0) {
454 tokens
[0] = STATE_LIGHTPROD
;
455 tokens
[2] = 1; /* back */
457 else if (strcmp(name
, "gl_TextureEnvColor") == 0) {
458 tokens
[0] = STATE_TEXENV_COLOR
;
460 else if (strcmp(name
, "gl_EyePlaneS") == 0) {
461 tokens
[0] = STATE_TEXGEN
;
462 tokens
[2] = STATE_TEXGEN_EYE_S
;
464 else if (strcmp(name
, "gl_EyePlaneT") == 0) {
465 tokens
[0] = STATE_TEXGEN
;
466 tokens
[2] = STATE_TEXGEN_EYE_T
;
468 else if (strcmp(name
, "gl_EyePlaneR") == 0) {
469 tokens
[0] = STATE_TEXGEN
;
470 tokens
[2] = STATE_TEXGEN_EYE_R
;
472 else if (strcmp(name
, "gl_EyePlaneQ") == 0) {
473 tokens
[0] = STATE_TEXGEN
;
474 tokens
[2] = STATE_TEXGEN_EYE_Q
;
476 else if (strcmp(name
, "gl_ObjectPlaneS") == 0) {
477 tokens
[0] = STATE_TEXGEN
;
478 tokens
[2] = STATE_TEXGEN_OBJECT_S
;
480 else if (strcmp(name
, "gl_ObjectPlaneT") == 0) {
481 tokens
[0] = STATE_TEXGEN
;
482 tokens
[2] = STATE_TEXGEN_OBJECT_T
;
484 else if (strcmp(name
, "gl_ObjectPlaneR") == 0) {
485 tokens
[0] = STATE_TEXGEN
;
486 tokens
[2] = STATE_TEXGEN_OBJECT_R
;
488 else if (strcmp(name
, "gl_ObjectPlaneQ") == 0) {
489 tokens
[0] = STATE_TEXGEN
;
490 tokens
[2] = STATE_TEXGEN_OBJECT_Q
;
492 else if (strcmp(name
, "gl_TextureMatrix") == 0) {
493 tokens
[0] = STATE_TEXTURE_MATRIX
;
494 tokens
[4] = STATE_MATRIX_TRANSPOSE
;
496 else if (strcmp(name
, "gl_TextureMatrixInverse") == 0) {
497 tokens
[0] = STATE_TEXTURE_MATRIX
;
498 tokens
[4] = STATE_MATRIX_INVTRANS
;
500 else if (strcmp(name
, "gl_TextureMatrixTranspose") == 0) {
501 tokens
[0] = STATE_TEXTURE_MATRIX
;
504 else if (strcmp(name
, "gl_TextureMatrixInverseTranspose") == 0) {
505 tokens
[0] = STATE_TEXTURE_MATRIX
;
506 tokens
[4] = STATE_MATRIX_INVERSE
;
509 return -1; /* invalid array name */
511 /* emit state vars for each array element */
512 for (i
= 0; i
< array_len
; i
++) {
515 p
= emit_statevars(NULL
, 0, type
->_array
, tokens
, paramList
);
521 else if (type
->type
== SLANG_SPEC_STRUCT
) {
522 const slang_variable_scope
*fields
= type
->_struct
->fields
;
524 for (i
= 0; i
< fields
->num_variables
; i
++) {
525 const slang_variable
*var
= fields
->variables
[i
];
526 GLint p
= emit_statevars(var
->a_name
, 0, &var
->type
.specifier
,
533 else if (type
->type
== SLANG_SPEC_MAT4
) {
534 /* unroll/emit 4 array rows (or columns) */
535 slang_type_specifier vec4
;
536 GLint i
, p
, pos
= -1;
537 vec4
.type
= SLANG_SPEC_VEC4
;
538 for (i
= 0; i
< 4; i
++) {
539 tokens
[2] = tokens
[3] = i
; /* row[i] (or column[i]) of matrix */
540 p
= emit_statevars(NULL
, 0, &vec4
, tokens
, paramList
);
548 assert(type
->type
== SLANG_SPEC_VEC4
||
549 type
->type
== SLANG_SPEC_VEC3
||
550 type
->type
== SLANG_SPEC_VEC2
||
551 type
->type
== SLANG_SPEC_FLOAT
||
552 type
->type
== SLANG_SPEC_IVEC4
||
553 type
->type
== SLANG_SPEC_IVEC3
||
554 type
->type
== SLANG_SPEC_IVEC2
||
555 type
->type
== SLANG_SPEC_INT
);
559 if (tokens
[0] == STATE_LIGHT
)
561 else if (tokens
[0] == STATE_LIGHTPROD
)
564 return -1; /* invalid array name */
566 if (strcmp(name
, "ambient") == 0) {
567 tokens
[t
] = STATE_AMBIENT
;
569 else if (strcmp(name
, "diffuse") == 0) {
570 tokens
[t
] = STATE_DIFFUSE
;
572 else if (strcmp(name
, "specular") == 0) {
573 tokens
[t
] = STATE_SPECULAR
;
575 else if (strcmp(name
, "position") == 0) {
576 tokens
[t
] = STATE_POSITION
;
578 else if (strcmp(name
, "halfVector") == 0) {
579 tokens
[t
] = STATE_HALF_VECTOR
;
581 else if (strcmp(name
, "spotDirection") == 0) {
582 tokens
[t
] = STATE_SPOT_DIRECTION
; /* xyz components */
584 else if (strcmp(name
, "spotCosCutoff") == 0) {
585 tokens
[t
] = STATE_SPOT_DIRECTION
; /* w component */
588 else if (strcmp(name
, "constantAttenuation") == 0) {
589 tokens
[t
] = STATE_ATTENUATION
; /* x component */
591 else if (strcmp(name
, "linearAttenuation") == 0) {
592 tokens
[t
] = STATE_ATTENUATION
; /* y component */
594 else if (strcmp(name
, "quadraticAttenuation") == 0) {
595 tokens
[t
] = STATE_ATTENUATION
; /* z component */
597 else if (strcmp(name
, "spotExponent") == 0) {
598 tokens
[t
] = STATE_ATTENUATION
; /* w = spot exponent */
601 else if (strcmp(name
, "spotCutoff") == 0) {
602 tokens
[t
] = STATE_SPOT_CUTOFF
; /* x component */
606 return -1; /* invalid field name */
610 pos
= _mesa_add_state_reference(paramList
, tokens
);
619 * Unroll the named built-in uniform variable into a sequence of state
620 * vars in the given parameter list.
623 alloc_state_var_array(const slang_variable
*var
,
624 struct gl_program_parameter_list
*paramList
)
626 gl_state_index tokens
[STATE_LENGTH
];
630 /* Initialize the state tokens array. This is very important.
631 * When we call _mesa_add_state_reference() it'll searches the parameter
632 * list to see if the given statevar token sequence is already present.
633 * This is normally a good thing since it prevents redundant values in the
636 * But when we're building arrays of state this can be bad. For example,
637 * consider this fragment of GLSL code:
638 * foo = gl_LightSource[3].diffuse;
640 * bar = gl_LightSource[i].diffuse;
642 * When we unroll the gl_LightSource array (for "bar") we want to re-emit
643 * gl_LightSource[3].diffuse and not re-use the first instance (from "foo")
644 * since that would upset the array layout. We handle this situation by
645 * setting the last token in the state var token array to the special
647 * This token will only be set for array state. We can hijack the last
648 * element in the array for this since it's never used for light, clipplane
649 * or texture env array state.
651 for (i
= 0; i
< STATE_LENGTH
; i
++)
653 tokens
[STATE_LENGTH
- 1] = STATE_ARRAY
;
655 pos
= emit_statevars(var
->a_name
, var
->array_len
, &var
->type
.specifier
,
664 * Allocate storage for a pre-defined uniform (a GL state variable).
665 * As a memory-saving optimization, we try to only allocate storage for
666 * state vars that are actually used.
668 * Arrays such as gl_LightSource are handled specially. For an expression
669 * like "gl_LightSource[2].diffuse", we can allocate a single uniform/constant
670 * slot and return the index. In this case, we return direct=TRUE.
672 * Buf for something like "gl_LightSource[i].diffuse" we don't know the value
673 * of 'i' at compile time so we need to "unroll" the gl_LightSource array
674 * into a consecutive sequence of uniform/constant slots so it can be indexed
675 * at runtime. In this case, we return direct=FALSE.
677 * Currently, all pre-defined uniforms are in one of these forms:
684 * \return -1 upon error, else position in paramList of the state variable/data
687 _slang_alloc_statevar(slang_ir_node
*n
,
688 struct gl_program_parameter_list
*paramList
,
691 slang_ir_node
*n0
= n
;
692 const char *field
= NULL
;
693 GLint index1
= -1, index2
= -1;
698 if (n
->Opcode
== IR_FIELD
) {
703 if (n
->Opcode
== IR_ELEMENT
) {
704 if (n
->Children
[1]->Opcode
== IR_FLOAT
) {
705 index1
= (GLint
) n
->Children
[1]->Value
[0];
713 if (n
->Opcode
== IR_ELEMENT
) {
714 /* XXX can only handle constant indexes for now */
715 if (n
->Children
[1]->Opcode
== IR_FLOAT
) {
716 /* two-dimensional array index: mat[i][j] */
718 index1
= (GLint
) n
->Children
[1]->Value
[0];
726 assert(n
->Opcode
== IR_VAR
);
729 const char *var
= (const char *) n
->Var
->a_name
;
731 lookup_statevar(var
, index1
, index2
, field
, &swizzle
, paramList
);
733 /* newly resolved storage for the statevar/constant/uniform */
734 n0
->Store
->File
= PROGRAM_STATE_VAR
;
735 n0
->Store
->Index
= pos
;
736 n0
->Store
->Swizzle
= swizzle
;
737 n0
->Store
->Parent
= NULL
;
743 return alloc_state_var_array(n
->Var
, paramList
);
749 #define SWIZZLE_ZWWW MAKE_SWIZZLE4(SWIZZLE_Z, SWIZZLE_W, SWIZZLE_W, SWIZZLE_W)
752 /** Predefined shader inputs */
761 /** Predefined vertex shader inputs/attributes */
762 static const struct input_info vertInputs
[] = {
763 { "gl_Vertex", VERT_ATTRIB_POS
, GL_FLOAT_VEC4
, SWIZZLE_NOOP
},
764 { "gl_Normal", VERT_ATTRIB_NORMAL
, GL_FLOAT_VEC3
, SWIZZLE_NOOP
},
765 { "gl_Color", VERT_ATTRIB_COLOR0
, GL_FLOAT_VEC4
, SWIZZLE_NOOP
},
766 { "gl_SecondaryColor", VERT_ATTRIB_COLOR1
, GL_FLOAT_VEC4
, SWIZZLE_NOOP
},
767 { "gl_FogCoord", VERT_ATTRIB_FOG
, GL_FLOAT
, SWIZZLE_XXXX
},
768 { "gl_MultiTexCoord0", VERT_ATTRIB_TEX0
, GL_FLOAT_VEC4
, SWIZZLE_NOOP
},
769 { "gl_MultiTexCoord1", VERT_ATTRIB_TEX1
, GL_FLOAT_VEC4
, SWIZZLE_NOOP
},
770 { "gl_MultiTexCoord2", VERT_ATTRIB_TEX2
, GL_FLOAT_VEC4
, SWIZZLE_NOOP
},
771 { "gl_MultiTexCoord3", VERT_ATTRIB_TEX3
, GL_FLOAT_VEC4
, SWIZZLE_NOOP
},
772 { "gl_MultiTexCoord4", VERT_ATTRIB_TEX4
, GL_FLOAT_VEC4
, SWIZZLE_NOOP
},
773 { "gl_MultiTexCoord5", VERT_ATTRIB_TEX5
, GL_FLOAT_VEC4
, SWIZZLE_NOOP
},
774 { "gl_MultiTexCoord6", VERT_ATTRIB_TEX6
, GL_FLOAT_VEC4
, SWIZZLE_NOOP
},
775 { "gl_MultiTexCoord7", VERT_ATTRIB_TEX7
, GL_FLOAT_VEC4
, SWIZZLE_NOOP
}
778 static const struct input_info geomInputs
[] = {
779 { "gl_PrimitiveIDIn", GEOM_ATTRIB_PRIMITIVE_ID
, GL_FLOAT
, SWIZZLE_NOOP
},
780 { "gl_FrontColorIn", GEOM_ATTRIB_COLOR0
, GL_FLOAT_VEC4
, SWIZZLE_NOOP
},
781 { "gl_BackColorIn", GEOM_ATTRIB_COLOR1
, GL_FLOAT_VEC4
, SWIZZLE_NOOP
},
782 { "gl_FrontSecondaryColorIn", GEOM_ATTRIB_SECONDARY_COLOR0
, GL_FLOAT_VEC4
, SWIZZLE_NOOP
},
783 { "gl_BackSecondaryColorIn", GEOM_ATTRIB_SECONDARY_COLOR1
, GL_FLOAT_VEC4
, SWIZZLE_NOOP
},
784 { "gl_TexCoordIn", GEOM_ATTRIB_TEX_COORD
, GL_FLOAT_VEC4
, SWIZZLE_NOOP
},
785 { "gl_FogFragCoordIn", GEOM_ATTRIB_FOG_FRAG_COORD
, GL_FLOAT
, SWIZZLE_NOOP
},
786 { "gl_PositionIn", GEOM_ATTRIB_POSITION
, GL_FLOAT_VEC4
, SWIZZLE_NOOP
},
787 { "gl_ClipVertexIn", GEOM_ATTRIB_CLIP_VERTEX
, GL_FLOAT_VEC4
, SWIZZLE_NOOP
},
788 { "gl_PointSizeIn", GEOM_ATTRIB_POINT_SIZE
, GL_FLOAT
, SWIZZLE_NOOP
}
791 /** Predefined fragment shader inputs */
792 static const struct input_info fragInputs
[] = {
793 { "gl_FragCoord", FRAG_ATTRIB_WPOS
, GL_FLOAT_VEC4
, SWIZZLE_NOOP
},
794 { "gl_Color", FRAG_ATTRIB_COL0
, GL_FLOAT_VEC4
, SWIZZLE_NOOP
},
795 { "gl_SecondaryColor", FRAG_ATTRIB_COL1
, GL_FLOAT_VEC4
, SWIZZLE_NOOP
},
796 { "gl_TexCoord", FRAG_ATTRIB_TEX0
, GL_FLOAT_VEC4
, SWIZZLE_NOOP
},
797 { "gl_FogFragCoord", FRAG_ATTRIB_FOGC
, GL_FLOAT
, SWIZZLE_XXXX
},
798 { "gl_FrontFacing", FRAG_ATTRIB_FACE
, GL_FLOAT
, SWIZZLE_XXXX
},
799 { "gl_PointCoord", FRAG_ATTRIB_PNTC
, GL_FLOAT_VEC2
, SWIZZLE_XYZW
}
804 * Return the VERT_ATTRIB_* or FRAG_ATTRIB_* value that corresponds to
805 * a vertex or fragment program input variable. Return -1 if the input
807 * XXX return size too
810 _slang_input_index(const char *name
, GLenum target
, GLuint
*swizzleOut
)
812 const struct input_info
*inputs
;
816 case GL_VERTEX_PROGRAM_ARB
:
818 n
= Elements(vertInputs
);
820 case GL_FRAGMENT_PROGRAM_ARB
:
822 n
= Elements(fragInputs
);
824 case MESA_GEOMETRY_PROGRAM
:
826 n
= Elements(geomInputs
);
829 _mesa_problem(NULL
, "bad target in _slang_input_index");
833 ASSERT(MAX_TEXTURE_COORD_UNITS
== 8); /* if this fails, fix vertInputs above */
835 for (i
= 0; i
< n
; i
++) {
836 if (strcmp(inputs
[i
].Name
, name
) == 0) {
838 *swizzleOut
= inputs
[i
].Swizzle
;
839 return inputs
[i
].Attrib
;
847 * Return name of the given vertex attribute (VERT_ATTRIB_x).
850 _slang_vert_attrib_name(GLuint attrib
)
853 assert(attrib
< VERT_ATTRIB_GENERIC0
);
854 for (i
= 0; Elements(vertInputs
); i
++) {
855 if (vertInputs
[i
].Attrib
== attrib
)
856 return vertInputs
[i
].Name
;
863 * Return type (GL_FLOAT, GL_FLOAT_VEC2, etc) of the given vertex
864 * attribute (VERT_ATTRIB_x).
867 _slang_vert_attrib_type(GLuint attrib
)
870 assert(attrib
< VERT_ATTRIB_GENERIC0
);
871 for (i
= 0; Elements(vertInputs
); i
++) {
872 if (vertInputs
[i
].Attrib
== attrib
)
873 return vertInputs
[i
].Type
;
882 /** Predefined shader output info */
890 /** Predefined vertex shader outputs */
891 static const struct output_info vertOutputs
[] = {
892 { "gl_Position", VERT_RESULT_HPOS
, GL_FLOAT_VEC4
},
893 { "gl_FrontColor", VERT_RESULT_COL0
, GL_FLOAT_VEC4
},
894 { "gl_BackColor", VERT_RESULT_BFC0
, GL_FLOAT_VEC4
},
895 { "gl_FrontSecondaryColor", VERT_RESULT_COL1
, GL_FLOAT_VEC4
},
896 { "gl_BackSecondaryColor", VERT_RESULT_BFC1
, GL_FLOAT_VEC4
},
897 { "gl_TexCoord", VERT_RESULT_TEX0
, GL_FLOAT_VEC4
},
898 { "gl_FogFragCoord", VERT_RESULT_FOGC
, GL_FLOAT
},
899 { "gl_PointSize", VERT_RESULT_PSIZ
, GL_FLOAT
}
902 /** Predefined geometry shader outputs */
903 static const struct output_info geomOutputs
[] = {
904 { "gl_Position", GEOM_RESULT_POS
, GL_FLOAT_VEC4
},
905 { "gl_FrontColor", GEOM_RESULT_COL0
, GL_FLOAT_VEC4
},
906 { "gl_BackColor", GEOM_RESULT_COL1
, GL_FLOAT_VEC4
},
907 { "gl_FrontSecondaryColor", GEOM_RESULT_SCOL0
, GL_FLOAT_VEC4
},
908 { "gl_BackSecondaryColor", GEOM_RESULT_SCOL1
, GL_FLOAT_VEC4
},
909 { "gl_TexCoord", GEOM_RESULT_TEX0
, GL_FLOAT_VEC4
},
910 { "gl_FogFragCoord", GEOM_RESULT_FOGC
, GL_FLOAT
},
911 { "gl_ClipVertex", GEOM_RESULT_CLPV
, GL_FLOAT_VEC4
},
912 { "gl_PointSize", GEOM_RESULT_PSIZ
, GL_FLOAT
},
913 { "gl_PrimitiveID", GEOM_RESULT_PRID
, GL_FLOAT
},
914 { "gl_Layer", GEOM_RESULT_LAYR
, GL_FLOAT
}
917 /** Predefined fragment shader outputs */
918 static const struct output_info fragOutputs
[] = {
919 { "gl_FragColor", FRAG_RESULT_COLOR
, GL_FLOAT_VEC4
},
920 { "gl_FragDepth", FRAG_RESULT_DEPTH
, GL_FLOAT
},
921 { "gl_FragData", FRAG_RESULT_DATA0
, GL_FLOAT_VEC4
}
926 * Return the VERT_RESULT_*, GEOM_RESULT_* or FRAG_RESULT_* value that corresponds
927 * to a vertex or fragment program output variable. Return -1 for an invalid
931 _slang_output_index(const char *name
, GLenum target
)
933 const struct output_info
*outputs
;
937 case GL_VERTEX_PROGRAM_ARB
:
938 outputs
= vertOutputs
;
939 n
= Elements(vertOutputs
);
941 case GL_FRAGMENT_PROGRAM_ARB
:
942 outputs
= fragOutputs
;
943 n
= Elements(fragOutputs
);
945 case MESA_GEOMETRY_PROGRAM
:
946 outputs
= geomOutputs
;
947 n
= Elements(geomOutputs
);
950 _mesa_problem(NULL
, "bad target in _slang_output_index");
954 for (i
= 0; i
< n
; i
++) {
955 if (strcmp(outputs
[i
].Name
, name
) == 0) {
957 return outputs
[i
].Attrib
;
965 * Given a VERT_RESULT_x index, return the corresponding string name.
968 _slang_vertex_output_name(gl_vert_result index
)
970 if (index
< Elements(vertOutputs
))
971 return vertOutputs
[index
].Name
;
978 * Given a GEOM_RESULT_x index, return the corresponding string name.
981 _slang_geometry_output_name(gl_geom_result index
)
983 if (index
< Elements(geomOutputs
))
984 return geomOutputs
[index
].Name
;
991 * Given a FRAG_RESULT_x index, return the corresponding string name.
994 _slang_fragment_output_name(gl_frag_result index
)
996 if (index
< Elements(fragOutputs
))
997 return fragOutputs
[index
].Name
;
1004 * Given a VERT_RESULT_x index, return the corresponding varying
1008 _slang_vertex_output_type(gl_vert_result index
)
1010 if (index
< Elements(vertOutputs
))
1011 return vertOutputs
[index
].Type
;