glsl2: Rework builtin function generation.
[mesa.git] / src / glsl / builtins / tools / texture_builtins.py
1 #!/usr/bin/python
2
3 import sys
4 import StringIO
5
6 def vec_type(g, size):
7 if size == 1:
8 if g == "i":
9 return "int"
10 elif g == "u":
11 return "uint"
12 return "float"
13 return g + "vec" + str(size)
14
15 # Get the base dimension - i.e. sampler3D gives 3
16 # Array samplers also get +1 here since the layer is really an extra coordinate
17 def get_coord_dim(sampler_type):
18 if sampler_type[0].isdigit():
19 coord_dim = int(sampler_type[0])
20 elif sampler_type.startswith("Cube"):
21 coord_dim = 3
22 else:
23 assert False ("coord_dim: invalid sampler_type: " + sampler_type)
24
25 if sampler_type.find("Array") != -1:
26 coord_dim += 1
27 return coord_dim
28
29 # Get the number of extra vector components (i.e. shadow comparitor)
30 def get_extra_dim(sampler_type, use_proj, unused_fields):
31 extra_dim = unused_fields
32 if sampler_type.find("Shadow") != -1:
33 extra_dim += 1
34 if use_proj:
35 extra_dim += 1
36 return extra_dim
37
38 def generate_sigs(g, tex_inst, sampler_type, use_proj = False, unused_fields = 0):
39 coord_dim = get_coord_dim(sampler_type)
40 extra_dim = get_extra_dim(sampler_type, use_proj, unused_fields)
41
42 # Print parameters
43 print " (signature " + g + "vec4"
44 print " (parameters"
45 print " (declare (in) " + g + "sampler" + sampler_type + " sampler)"
46 print " (declare (in) " + vec_type("i" if tex_inst == "txf" else "", coord_dim + extra_dim) + " P)",
47 if tex_inst == "txb":
48 print "\n (declare (in) float bias)",
49 elif tex_inst == "txl":
50 print "\n (declare (in) float lod)",
51 elif tex_inst == "txf":
52 print "\n (declare (in) int lod)",
53 elif tex_inst == "txd":
54 grad_type = vec_type("", coord_dim)
55 print "\n (declare (in) " + grad_type + " dPdx)",
56 print "\n (declare (in) " + grad_type + " dPdy)",
57
58 print ")\n ((return (" + tex_inst + " (var_ref sampler)",
59
60 # Coordinate
61 if extra_dim > 0:
62 print "(swiz " + "xyzw"[:coord_dim] + " (var_ref P))",
63 else:
64 print "(var_ref P)",
65
66 # Offset
67 print "(0 0 0)",
68
69 if tex_inst != "txf":
70 # Projective divisor
71 if use_proj:
72 print "(swiz " + "xyzw"[coord_dim + extra_dim-1] + " (var_ref P))",
73 else:
74 print "1",
75
76 # Shadow comparitor
77 if sampler_type == "2DArrayShadow": # a special case:
78 print "(swiz w (var_ref P))", # ...array layer is z; shadow is w
79 elif sampler_type.endswith("Shadow"):
80 print "(swiz z (var_ref P))",
81 else:
82 print "()",
83
84 # Bias/explicit LOD/gradient:
85 if tex_inst == "txb":
86 print "(var_ref bias)",
87 elif tex_inst == "txl" or tex_inst == "txf":
88 print "(var_ref lod)",
89 elif tex_inst == "txd":
90 print "((var_ref dPdx) (var_ref dPdy))",
91 print "))))\n"
92
93 def generate_fiu_sigs(tex_inst, sampler_type, use_proj = False, unused_fields = 0):
94 generate_sigs("", tex_inst, sampler_type, use_proj, unused_fields)
95 generate_sigs("i", tex_inst, sampler_type, use_proj, unused_fields)
96 generate_sigs("u", tex_inst, sampler_type, use_proj, unused_fields)
97
98 def start_function(name):
99 sys.stdout = StringIO.StringIO()
100 print "((function " + name
101
102 def end_function(fs, name):
103 print "))"
104 fs[name] = sys.stdout.getvalue();
105 sys.stdout.close()
106
107 # Generate all the functions and store them in the supplied dictionary.
108 # This is better than writing them to actual files since they should never be
109 # edited; it'd also be easy to confuse them with the many hand-generated files.
110 #
111 # Takes a dictionary as an argument.
112 def generate_texture_functions(fs):
113 start_function("texture")
114 generate_fiu_sigs("tex", "1D")
115 generate_fiu_sigs("tex", "2D")
116 generate_fiu_sigs("tex", "3D")
117 generate_fiu_sigs("tex", "Cube")
118 generate_fiu_sigs("tex", "1DArray")
119 generate_fiu_sigs("tex", "2DArray")
120
121 generate_fiu_sigs("txb", "1D")
122 generate_fiu_sigs("txb", "2D")
123 generate_fiu_sigs("txb", "3D")
124 generate_fiu_sigs("txb", "Cube")
125 generate_fiu_sigs("txb", "1DArray")
126 generate_fiu_sigs("txb", "2DArray")
127 end_function(fs, "texture")
128
129 start_function("textureProj")
130 generate_fiu_sigs("tex", "1D", True)
131 generate_fiu_sigs("tex", "1D", True, 2)
132 generate_fiu_sigs("tex", "2D", True)
133 generate_fiu_sigs("tex", "2D", True, 1)
134 generate_fiu_sigs("tex", "3D", True)
135
136 generate_fiu_sigs("txb", "1D", True)
137 generate_fiu_sigs("txb", "1D", True, 2)
138 generate_fiu_sigs("txb", "2D", True)
139 generate_fiu_sigs("txb", "2D", True, 1)
140 generate_fiu_sigs("txb", "3D", True)
141 end_function(fs, "textureProj")
142
143 start_function("textureLod")
144 generate_fiu_sigs("txl", "1D")
145 generate_fiu_sigs("txl", "2D")
146 generate_fiu_sigs("txl", "3D")
147 generate_fiu_sigs("txl", "Cube")
148 generate_fiu_sigs("txl", "1DArray")
149 generate_fiu_sigs("txl", "2DArray")
150 end_function(fs, "textureLod")
151
152 start_function("texelFetch")
153 generate_fiu_sigs("txf", "1D")
154 generate_fiu_sigs("txf", "2D")
155 generate_fiu_sigs("txf", "3D")
156 generate_fiu_sigs("txf", "1DArray")
157 generate_fiu_sigs("txf", "2DArray")
158 end_function(fs, "texelFetch")
159
160 start_function("textureProjLod")
161 generate_fiu_sigs("txl", "1D", True)
162 generate_fiu_sigs("txl", "1D", True, 2)
163 generate_fiu_sigs("txl", "2D", True)
164 generate_fiu_sigs("txl", "2D", True, 1)
165 generate_fiu_sigs("txl", "3D", True)
166 end_function(fs, "textureProjLod")
167
168 start_function("textureGrad")
169 generate_fiu_sigs("txd", "1D")
170 generate_fiu_sigs("txd", "2D")
171 generate_fiu_sigs("txd", "3D")
172 generate_fiu_sigs("txd", "Cube")
173 generate_fiu_sigs("txd", "1DArray")
174 generate_fiu_sigs("txd", "2DArray")
175 end_function(fs, "textureGrad")
176
177 start_function("textureProjGrad")
178 generate_fiu_sigs("txd", "1D", True)
179 generate_fiu_sigs("txd", "1D", True, 2)
180 generate_fiu_sigs("txd", "2D", True)
181 generate_fiu_sigs("txd", "2D", True, 1)
182 generate_fiu_sigs("txd", "3D", True)
183 end_function(fs, "textureProjGrad")
184
185 # ARB_texture_rectangle extension
186 start_function("texture2DRect")
187 generate_sigs("", "tex", "2DRect")
188 end_function(fs, "texture2DRect")
189
190 start_function("texture2DRectProj")
191 generate_sigs("", "tex", "2DRect", True)
192 generate_sigs("", "tex", "2DRect", True, 1)
193 end_function(fs, "texture2DRectProj")
194
195 start_function("shadow2DRect")
196 generate_sigs("", "tex", "2DRectShadow")
197 end_function(fs, "shadow2DRect")
198
199 start_function("shadow2DRectProj")
200 generate_sigs("", "tex", "2DRectShadow", True)
201 end_function(fs, "shadow2DRectProj")
202
203 # EXT_texture_array extension
204 start_function("texture1DArray")
205 generate_sigs("", "tex", "1DArray")
206 generate_sigs("", "txb", "1DArray")
207 end_function(fs, "texture1DArray")
208
209 start_function("texture1DArrayLod")
210 generate_sigs("", "txl", "1DArray")
211 end_function(fs, "texture1DArrayLod")
212
213 start_function("texture2DArray")
214 generate_sigs("", "tex", "2DArray")
215 generate_sigs("", "txb", "2DArray")
216 end_function(fs, "texture2DArray")
217
218 start_function("texture2DArrayLod")
219 generate_sigs("", "txl", "2DArray")
220 end_function(fs, "texture2DArrayLod")
221
222 start_function("shadow1DArray")
223 generate_sigs("", "tex", "1DArrayShadow")
224 generate_sigs("", "txb", "1DArrayShadow")
225 end_function(fs, "shadow1DArray")
226
227 start_function("shadow1DArrayLod")
228 generate_sigs("", "txl", "1DArrayShadow")
229 end_function(fs, "shadow1DArrayLod")
230
231 start_function("shadow2DArray")
232 generate_sigs("", "tex", "2DArrayShadow")
233 end_function(fs, "shadow2DArray")
234
235 # Deprecated (110/120 style) functions with silly names:
236 start_function("texture1D")
237 generate_sigs("", "tex", "1D")
238 generate_sigs("", "txb", "1D")
239 end_function(fs, "texture1D")
240
241 start_function("texture1DLod")
242 generate_sigs("", "txl", "1D")
243 end_function(fs, "texture1DLod")
244
245 start_function("texture1DProj")
246 generate_sigs("", "tex", "1D", True)
247 generate_sigs("", "tex", "1D", True, 2)
248 generate_sigs("", "txb", "1D", True)
249 generate_sigs("", "txb", "1D", True, 2)
250 end_function(fs, "texture1DProj")
251
252 start_function("texture1DProjLod")
253 generate_sigs("", "txl", "1D", True)
254 generate_sigs("", "txl", "1D", True, 2)
255 end_function(fs, "texture1DProjLod")
256
257 start_function("texture2D")
258 generate_sigs("", "tex", "2D")
259 generate_sigs("", "txb", "2D")
260 end_function(fs, "texture2D")
261
262 start_function("texture2DLod")
263 generate_sigs("", "txl", "2D")
264 end_function(fs, "texture2DLod")
265
266 start_function("texture2DProj")
267 generate_sigs("", "tex", "2D", True)
268 generate_sigs("", "tex", "2D", True, 1)
269 generate_sigs("", "txb", "2D", True)
270 generate_sigs("", "txb", "2D", True, 1)
271 end_function(fs, "texture2DProj")
272
273 start_function("texture2DProjLod")
274 generate_sigs("", "txl", "2D", True)
275 generate_sigs("", "txl", "2D", True, 1)
276 end_function(fs, "texture2DProjLod")
277
278 start_function("texture3D")
279 generate_sigs("", "tex", "3D")
280 generate_sigs("", "txb", "3D")
281 end_function(fs, "texture3D")
282
283 start_function("texture3DLod")
284 generate_sigs("", "txl", "3D")
285 end_function(fs, "texture3DLod")
286
287 start_function("texture3DProj")
288 generate_sigs("", "tex", "3D", True)
289 generate_sigs("", "txb", "3D", True)
290 end_function(fs, "texture3DProj")
291
292 start_function("texture3DProjLod")
293 generate_sigs("", "txl", "3D", True)
294 end_function(fs, "texture3DProjLod")
295
296 start_function("textureCube")
297 generate_sigs("", "tex", "Cube")
298 generate_sigs("", "txb", "Cube")
299 end_function(fs, "textureCube")
300
301 start_function("textureCubeLod")
302 generate_sigs("", "txl", "Cube")
303 end_function(fs, "textureCubeLod")
304
305 start_function("shadow1D")
306 generate_sigs("", "tex", "1DShadow", False, 1)
307 generate_sigs("", "txb", "1DShadow", False, 1)
308 end_function(fs, "shadow1D")
309
310 start_function("shadow1DLod")
311 generate_sigs("", "txl", "1DShadow", False, 1)
312 end_function(fs, "shadow1DLod")
313
314 start_function("shadow1DProj")
315 generate_sigs("", "tex", "1DShadow", True, 1)
316 generate_sigs("", "txb", "1DShadow", True, 1)
317 end_function(fs, "shadow1DProj")
318
319 start_function("shadow1DProjLod")
320 generate_sigs("", "txl", "1DShadow", True, 1)
321 end_function(fs, "shadow1DProjLod")
322
323 start_function("shadow2D")
324 generate_sigs("", "tex", "2DShadow")
325 generate_sigs("", "txb", "2DShadow")
326 end_function(fs, "shadow2D")
327
328 start_function("shadow2DLod")
329 generate_sigs("", "txl", "2DShadow")
330 end_function(fs, "shadow2DLod")
331
332 start_function("shadow2DProj")
333 generate_sigs("", "tex", "2DShadow", True)
334 generate_sigs("", "txb", "2DShadow", True)
335 end_function(fs, "shadow2DProj")
336
337 start_function("shadow2DProjLod")
338 generate_sigs("", "txl", "2DShadow", True)
339 end_function(fs, "shadow2DProjLod")
340
341 sys.stdout = sys.__stdout__
342 return fs
343
344 # If you actually run this script, it'll print out all the functions.
345 if __name__ == "__main__":
346 fs = {}
347 generate_texture_functions(fs);
348 for k, v in fs.iteritems():
349 print v