implement support for pre-defined uniform structs (state vars)
[mesa.git] / src / mesa / shader / slang / slang_builtin.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 6.5.3
4 *
5 * Copyright (C) 2005-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 slang_builtin.c
27 * Resolve built-in uniform vars.
28 * \author Brian Paul
29 */
30
31 #include "imports.h"
32 #include "macros.h"
33 #include "slang_builtin.h"
34 #include "slang_typeinfo.h"
35 #include "slang_codegen.h"
36 #include "slang_compile.h"
37 #include "slang_ir.h"
38 #include "mtypes.h"
39 #include "program.h"
40 #include "prog_instruction.h"
41 #include "prog_parameter.h"
42 #include "prog_statevars.h"
43 #include "slang_print.h"
44
45
46
47 /**
48 * XXX we might consider moving much of this into the prog_statevars.c file
49 */
50
51
52 /**
53 * Determine if 'name' is a state variable (pre-defined uniform).
54 * If so, create a new program parameter for it, and return the
55 * param's index.
56 *
57 * \param swizzleOut returns the swizzle needed to access 'float' values
58 * \return the state value's position in the parameter list, or -1 if error
59 */
60 GLint
61 _slang_lookup_statevar(const char *name, GLint index,
62 struct gl_program_parameter_list *paramList,
63 GLuint *swizzleOut)
64 {
65 struct state_info {
66 const char *Name;
67 const GLuint NumRows; /** for matrices */
68 const GLuint Swizzle;
69 const GLint Indexes[STATE_LENGTH];
70 };
71 static const struct state_info state[] = {
72 { "gl_ModelViewMatrix", 4, SWIZZLE_NOOP,
73 { STATE_MATRIX, STATE_MODELVIEW, 0, 0, 0, 0 } },
74 { "gl_NormalMatrix", 3, SWIZZLE_NOOP,
75 { STATE_MATRIX, STATE_MODELVIEW, 0, 0, 0, 0 } },
76 { "gl_ProjectionMatrix", 4, SWIZZLE_NOOP,
77 { STATE_MATRIX, STATE_PROJECTION, 0, 0, 0, 0 } },
78 { "gl_ModelViewProjectionMatrix", 4, SWIZZLE_NOOP,
79 { STATE_MATRIX, STATE_MVP, 0, 0, 0, 0 } },
80 { "gl_TextureMatrix", 4, SWIZZLE_NOOP,
81 { STATE_MATRIX, STATE_TEXTURE, 0, 0, 0, 0 } },
82 { "gl_NormalScale", 1, SWIZZLE_NOOP,
83 { STATE_INTERNAL, STATE_NORMAL_SCALE, 0, 0, 0, 0} },
84
85 /* For aggregate/structs we need entries for both the base name
86 * and base.field.
87 */
88 { "gl_DepthRange", 1, SWIZZLE_NOOP,
89 { STATE_DEPTH_RANGE, 0, 0, 0, 0, 0 } },
90 { "gl_DepthRange.near", 1, SWIZZLE_XXXX,
91 { STATE_DEPTH_RANGE, 0, 0, 0, 0, 0 } },
92 { "gl_DepthRange.far", 1, SWIZZLE_YYYY,
93 { STATE_DEPTH_RANGE, 0, 0, 0, 0, 0 } },
94 { "gl_DepthRange.diff", 1, SWIZZLE_ZZZZ,
95 { STATE_DEPTH_RANGE, 0, 0, 0, 0, 0 } },
96
97 { "gl_Point", 1, SWIZZLE_NOOP,
98 { STATE_POINT_SIZE, 0, 0, 0, 0, 0 } },
99 { "gl_Point.size", 1, SWIZZLE_XXXX,
100 { STATE_POINT_SIZE, 0, 0, 0, 0, 0 } },
101 { "gl_Point.sizeMin", 1, SWIZZLE_YYYY,
102 { STATE_POINT_SIZE, 0, 0, 0, 0, 0 } },
103 { "gl_Point.sizeMax", 1, SWIZZLE_ZZZZ,
104 { STATE_POINT_SIZE, 0, 0, 0, 0, 0 } },
105 { "gl_Point.fadeThresholdSize", 1, SWIZZLE_WWWW,
106 { STATE_POINT_SIZE, 0, 0, 0, 0, 0 } },
107 { "gl_Point.distanceConstantAttenuation", 1, SWIZZLE_XXXX,
108 { STATE_POINT_ATTENUATION, 0, 0, 0, 0, 0 } },
109 { "gl_Point.distanceLinearAttenuation", 1, SWIZZLE_YYYY,
110 { STATE_POINT_ATTENUATION, 0, 0, 0, 0, 0 } },
111 { "gl_Point.distanceQuadraticAttenuation", 1, SWIZZLE_ZZZZ,
112 { STATE_POINT_ATTENUATION, 0, 0, 0, 0, 0 } },
113
114 { "gl_FrontMaterial", 1, SWIZZLE_NOOP,
115 { STATE_MATERIAL, 0, STATE_EMISSION, 0, 0, 0 } },
116 { "gl_FrontMaterial.emission", 1, SWIZZLE_NOOP,
117 { STATE_MATERIAL, 0, STATE_EMISSION, 0, 0, 0 } },
118 { "gl_FrontMaterial.ambient", 1, SWIZZLE_NOOP,
119 { STATE_MATERIAL, 0, STATE_AMBIENT, 0, 0, 0 } },
120 { "gl_FrontMaterial.diffuse", 1, SWIZZLE_NOOP,
121 { STATE_MATERIAL, 0, STATE_DIFFUSE, 0, 0, 0 } },
122 { "gl_FrontMaterial.specular", 1, SWIZZLE_NOOP,
123 { STATE_MATERIAL, 0, STATE_SPECULAR, 0, 0, 0 } },
124 { "gl_FrontMaterial.shininess", 1, SWIZZLE_XXXX,
125 { STATE_MATERIAL, 0, STATE_SHININESS, 0, 0, 0 } },
126
127 { "gl_BackMaterial", 1, SWIZZLE_NOOP,
128 { STATE_MATERIAL, 1, STATE_EMISSION, 0, 0, 0 } },
129 { "gl_BackMaterial.emission", 1, SWIZZLE_NOOP,
130 { STATE_MATERIAL, 1, STATE_EMISSION, 0, 0, 0 } },
131 { "gl_BackMaterial.ambient", 1, SWIZZLE_NOOP,
132 { STATE_MATERIAL, 1, STATE_AMBIENT, 0, 0, 0 } },
133 { "gl_BackMaterial.diffuse", 1, SWIZZLE_NOOP,
134 { STATE_MATERIAL, 1, STATE_DIFFUSE, 0, 0, 0 } },
135 { "gl_BackMaterial.specular", 1, SWIZZLE_NOOP,
136 { STATE_MATERIAL, 1, STATE_SPECULAR, 0, 0, 0 } },
137 { "gl_BackMaterial.shininess", 1, SWIZZLE_XXXX,
138 { STATE_MATERIAL, 1, STATE_SHININESS, 0, 0, 0 } },
139
140 { "gl_LightModel", 1, SWIZZLE_NOOP,
141 { STATE_LIGHTMODEL_AMBIENT, 0, 0, 0, 0, 0 } },
142 { "gl_LightModel.ambient", 1, SWIZZLE_NOOP,
143 { STATE_LIGHTMODEL_AMBIENT, 0, 0, 0, 0, 0 } },
144
145 { "gl_FrontLightModelProduct", 1, SWIZZLE_NOOP,
146 { STATE_LIGHTMODEL_SCENECOLOR, 0, 0, 0, 0, 0 } },
147 { "gl_FrontLightModelProduct.sceneColor", 1, SWIZZLE_NOOP,
148 { STATE_LIGHTMODEL_SCENECOLOR, 0, 0, 0, 0, 0 } },
149
150 { "gl_BackLightModelProduct", 1, SWIZZLE_NOOP,
151 { STATE_LIGHTMODEL_SCENECOLOR, 1, 0, 0, 0, 0 } },
152 { "gl_BackLightModelProduct.sceneColor", 1, SWIZZLE_NOOP,
153 { STATE_LIGHTMODEL_SCENECOLOR, 1, 0, 0, 0, 0 } },
154
155 { "gl_Fog", 1, SWIZZLE_NOOP,
156 { STATE_FOG_COLOR, 0, 0, 0, 0, 0 } },
157 { "gl_Fog.color", 1, SWIZZLE_NOOP,
158 { STATE_FOG_COLOR, 0, 0, 0, 0, 0 } },
159 { "gl_Fog.density", 1, SWIZZLE_XXXX,
160 { STATE_FOG_PARAMS, 0, 0, 0, 0, 0 } },
161 { "gl_Fog.start", 1, SWIZZLE_YYYY,
162 { STATE_FOG_PARAMS, 0, 0, 0, 0, 0 } },
163 { "gl_Fog.end", 1, SWIZZLE_ZZZZ,
164 { STATE_FOG_PARAMS, 0, 0, 0, 0, 0 } },
165 { "gl_Fog.scale", 1, SWIZZLE_WWWW,
166 { STATE_FOG_PARAMS, 0, 0, 0, 0, 0 } },
167
168
169 { NULL, 0, 0, {0, 0, 0, 0, 0, 0} }
170 };
171 GLuint i;
172
173 for (i = 0; state[i].Name; i++) {
174 if (strcmp(state[i].Name, name) == 0) {
175 /* found */
176 *swizzleOut = state[i].Swizzle;
177 if (paramList) {
178 if (state[i].NumRows > 1) {
179 /* a matrix */
180 GLuint j;
181 GLint pos[4], indexesCopy[STATE_LENGTH];
182 /* make copy of state tokens */
183 for (j = 0; j < STATE_LENGTH; j++)
184 indexesCopy[j] = state[i].Indexes[j];
185 /* load rows */
186 for (j = 0; j < state[i].NumRows; j++) {
187 indexesCopy[3] = indexesCopy[4] = j; /* jth row of matrix */
188 pos[j] = _mesa_add_state_reference(paramList, indexesCopy);
189 assert(pos[j] >= 0);
190 }
191 return pos[0];
192 }
193 else {
194 /* non-matrix state */
195 GLint pos
196 = _mesa_add_state_reference(paramList, state[i].Indexes);
197 assert(pos >= 0);
198 return pos;
199 }
200 }
201 }
202 }
203 return -1;
204 }
205
206
207 GLint
208 _slang_lookup_statevar_field(const char *base, const char *field,
209 struct gl_program_parameter_list *paramList,
210 GLuint *swizzleOut)
211 {
212 GLint pos = -1;
213 const GLint len = _mesa_strlen(base)
214 + _mesa_strlen(field) + 2;
215 char *name = (char *) _mesa_malloc(len);
216
217 if (!name)
218 return -1;
219
220 _mesa_strcpy(name, base);
221 /*_mesa_*/strcat(name, ".");
222 /*_mesa_*/strcat(name, field);
223 printf("FULL NAME: %s\n", name);
224
225 pos = _slang_lookup_statevar(name, 0, paramList, swizzleOut);
226
227 _mesa_free(name);
228
229 return pos;
230 }
231
232