vk/image: Check extent does not exceed surface type limits
[mesa.git] / src / glsl / ir_function.cpp
1 /*
2 * Copyright © 2010 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 */
23
24 #include "glsl_types.h"
25 #include "ir.h"
26 #include "glsl_parser_extras.h"
27 #include "main/errors.h"
28
29 typedef enum {
30 PARAMETER_LIST_NO_MATCH,
31 PARAMETER_LIST_EXACT_MATCH,
32 PARAMETER_LIST_INEXACT_MATCH /*< Match requires implicit conversion. */
33 } parameter_list_match_t;
34
35 /**
36 * \brief Check if two parameter lists match.
37 *
38 * \param list_a Parameters of the function definition.
39 * \param list_b Actual parameters passed to the function.
40 * \see matching_signature()
41 */
42 static parameter_list_match_t
43 parameter_lists_match(_mesa_glsl_parse_state *state,
44 const exec_list *list_a, const exec_list *list_b)
45 {
46 const exec_node *node_a = list_a->head;
47 const exec_node *node_b = list_b->head;
48
49 /* This is set to true if there is an inexact match requiring an implicit
50 * conversion. */
51 bool inexact_match = false;
52
53 for (/* empty */
54 ; !node_a->is_tail_sentinel()
55 ; node_a = node_a->next, node_b = node_b->next) {
56 /* If all of the parameters from the other parameter list have been
57 * exhausted, the lists have different length and, by definition,
58 * do not match.
59 */
60 if (node_b->is_tail_sentinel())
61 return PARAMETER_LIST_NO_MATCH;
62
63
64 const ir_variable *const param = (ir_variable *) node_a;
65 const ir_rvalue *const actual = (ir_rvalue *) node_b;
66
67 if (param->type == actual->type)
68 continue;
69
70 /* Try to find an implicit conversion from actual to param. */
71 inexact_match = true;
72 switch ((enum ir_variable_mode)(param->data.mode)) {
73 case ir_var_auto:
74 case ir_var_uniform:
75 case ir_var_temporary:
76 /* These are all error conditions. It is invalid for a parameter to
77 * a function to be declared as auto (not in, out, or inout) or
78 * as uniform.
79 */
80 assert(0);
81 return PARAMETER_LIST_NO_MATCH;
82
83 case ir_var_const_in:
84 case ir_var_function_in:
85 if (!actual->type->can_implicitly_convert_to(param->type, state))
86 return PARAMETER_LIST_NO_MATCH;
87 break;
88
89 case ir_var_function_out:
90 if (!param->type->can_implicitly_convert_to(actual->type, state))
91 return PARAMETER_LIST_NO_MATCH;
92 break;
93
94 case ir_var_function_inout:
95 /* Since there are no bi-directional automatic conversions (e.g.,
96 * there is int -> float but no float -> int), inout parameters must
97 * be exact matches.
98 */
99 return PARAMETER_LIST_NO_MATCH;
100
101 default:
102 assert(false);
103 return PARAMETER_LIST_NO_MATCH;
104 }
105 }
106
107 /* If all of the parameters from the other parameter list have been
108 * exhausted, the lists have different length and, by definition, do not
109 * match.
110 */
111 if (!node_b->is_tail_sentinel())
112 return PARAMETER_LIST_NO_MATCH;
113
114 if (inexact_match)
115 return PARAMETER_LIST_INEXACT_MATCH;
116 else
117 return PARAMETER_LIST_EXACT_MATCH;
118 }
119
120
121 /* Classes of parameter match, sorted (mostly) best matches first.
122 * See is_better_parameter_match() below for the exceptions.
123 * */
124 typedef enum {
125 PARAMETER_EXACT_MATCH,
126 PARAMETER_FLOAT_TO_DOUBLE,
127 PARAMETER_INT_TO_FLOAT,
128 PARAMETER_INT_TO_DOUBLE,
129 PARAMETER_OTHER_CONVERSION,
130 } parameter_match_t;
131
132
133 static parameter_match_t
134 get_parameter_match_type(const ir_variable *param,
135 const ir_rvalue *actual)
136 {
137 const glsl_type *from_type;
138 const glsl_type *to_type;
139
140 if (param->data.mode == ir_var_function_out) {
141 from_type = param->type;
142 to_type = actual->type;
143 } else {
144 from_type = actual->type;
145 to_type = param->type;
146 }
147
148 if (from_type == to_type)
149 return PARAMETER_EXACT_MATCH;
150
151 if (to_type->base_type == GLSL_TYPE_DOUBLE) {
152 if (from_type->base_type == GLSL_TYPE_FLOAT)
153 return PARAMETER_FLOAT_TO_DOUBLE;
154 return PARAMETER_INT_TO_DOUBLE;
155 }
156
157 if (to_type->base_type == GLSL_TYPE_FLOAT)
158 return PARAMETER_INT_TO_FLOAT;
159
160 /* int -> uint and any other oddball conversions */
161 return PARAMETER_OTHER_CONVERSION;
162 }
163
164
165 static bool
166 is_better_parameter_match(parameter_match_t a_match,
167 parameter_match_t b_match)
168 {
169 /* From section 6.1 of the GLSL 4.00 spec (and the ARB_gpu_shader5 spec):
170 *
171 * 1. An exact match is better than a match involving any implicit
172 * conversion.
173 *
174 * 2. A match involving an implicit conversion from float to double
175 * is better than match involving any other implicit conversion.
176 *
177 * [XXX: Not in GLSL 4.0: Only in ARB_gpu_shader5:
178 * 3. A match involving an implicit conversion from either int or uint
179 * to float is better than a match involving an implicit conversion
180 * from either int or uint to double.]
181 *
182 * If none of the rules above apply to a particular pair of conversions,
183 * neither conversion is considered better than the other.
184 *
185 * --
186 *
187 * Notably, the int->uint conversion is *not* considered to be better
188 * or worse than int/uint->float or int/uint->double.
189 */
190
191 if (a_match >= PARAMETER_INT_TO_FLOAT && b_match == PARAMETER_OTHER_CONVERSION)
192 return false;
193
194 return a_match < b_match;
195 }
196
197
198 static bool
199 is_best_inexact_overload(const exec_list *actual_parameters,
200 ir_function_signature **matches,
201 int num_matches,
202 ir_function_signature *sig)
203 {
204 /* From section 6.1 of the GLSL 4.00 spec (and the ARB_gpu_shader5 spec):
205 *
206 * "A function definition A is considered a better
207 * match than function definition B if:
208 *
209 * * for at least one function argument, the conversion for that argument
210 * in A is better than the corresponding conversion in B; and
211 *
212 * * there is no function argument for which the conversion in B is better
213 * than the corresponding conversion in A.
214 *
215 * If a single function definition is considered a better match than every
216 * other matching function definition, it will be used. Otherwise, a
217 * semantic error occurs and the shader will fail to compile."
218 */
219 for (ir_function_signature **other = matches;
220 other < matches + num_matches; other++) {
221 if (*other == sig)
222 continue;
223
224 const exec_node *node_a = sig->parameters.head;
225 const exec_node *node_b = (*other)->parameters.head;
226 const exec_node *node_p = actual_parameters->head;
227
228 bool better_for_some_parameter = false;
229
230 for (/* empty */
231 ; !node_a->is_tail_sentinel()
232 ; node_a = node_a->next,
233 node_b = node_b->next,
234 node_p = node_p->next) {
235 parameter_match_t a_match = get_parameter_match_type(
236 (const ir_variable *)node_a,
237 (const ir_rvalue *)node_p);
238 parameter_match_t b_match = get_parameter_match_type(
239 (const ir_variable *)node_b,
240 (const ir_rvalue *)node_p);
241
242 if (is_better_parameter_match(a_match, b_match))
243 better_for_some_parameter = true;
244
245 if (is_better_parameter_match(b_match, a_match))
246 return false; /* B is better for this parameter */
247 }
248
249 if (!better_for_some_parameter)
250 return false; /* A must be better than B for some parameter */
251
252 }
253
254 return true;
255 }
256
257
258 static ir_function_signature *
259 choose_best_inexact_overload(_mesa_glsl_parse_state *state,
260 const exec_list *actual_parameters,
261 ir_function_signature **matches,
262 int num_matches)
263 {
264 if (num_matches == 0)
265 return NULL;
266
267 if (num_matches == 1)
268 return *matches;
269
270 /* Without GLSL 4.0 / ARB_gpu_shader5, there is no overload resolution
271 * among multiple inexact matches. Note that state may be NULL here if
272 * called from the linker; in that case we assume everything supported in
273 * any GLSL version is available. */
274 if (!state || state->is_version(400, 0) || state->ARB_gpu_shader5_enable) {
275 for (ir_function_signature **sig = matches; sig < matches + num_matches; sig++) {
276 if (is_best_inexact_overload(actual_parameters, matches, num_matches, *sig))
277 return *sig;
278 }
279 }
280
281 return NULL; /* no best candidate */
282 }
283
284
285 ir_function_signature *
286 ir_function::matching_signature(_mesa_glsl_parse_state *state,
287 const exec_list *actual_parameters,
288 bool allow_builtins)
289 {
290 bool is_exact;
291 return matching_signature(state, actual_parameters, allow_builtins,
292 &is_exact);
293 }
294
295 ir_function_signature *
296 ir_function::matching_signature(_mesa_glsl_parse_state *state,
297 const exec_list *actual_parameters,
298 bool allow_builtins,
299 bool *is_exact)
300 {
301 ir_function_signature **inexact_matches = NULL;
302 ir_function_signature **inexact_matches_temp;
303 ir_function_signature *match = NULL;
304 int num_inexact_matches = 0;
305
306 /* From page 42 (page 49 of the PDF) of the GLSL 1.20 spec:
307 *
308 * "If an exact match is found, the other signatures are ignored, and
309 * the exact match is used. Otherwise, if no exact match is found, then
310 * the implicit conversions in Section 4.1.10 "Implicit Conversions" will
311 * be applied to the calling arguments if this can make their types match
312 * a signature. In this case, it is a semantic error if there are
313 * multiple ways to apply these conversions to the actual arguments of a
314 * call such that the call can be made to match multiple signatures."
315 */
316 foreach_in_list(ir_function_signature, sig, &this->signatures) {
317 /* Skip over any built-ins that aren't available in this shader. */
318 if (sig->is_builtin() && (!allow_builtins ||
319 !sig->is_builtin_available(state)))
320 continue;
321
322 switch (parameter_lists_match(state, & sig->parameters, actual_parameters)) {
323 case PARAMETER_LIST_EXACT_MATCH:
324 *is_exact = true;
325 free(inexact_matches);
326 return sig;
327 case PARAMETER_LIST_INEXACT_MATCH:
328 inexact_matches_temp = (ir_function_signature **)
329 realloc(inexact_matches,
330 sizeof(*inexact_matches) *
331 (num_inexact_matches + 1));
332 if (inexact_matches_temp == NULL) {
333 _mesa_error_no_memory(__func__);
334 free(inexact_matches);
335 return NULL;
336 }
337 inexact_matches = inexact_matches_temp;
338 inexact_matches[num_inexact_matches++] = sig;
339 continue;
340 case PARAMETER_LIST_NO_MATCH:
341 continue;
342 default:
343 assert(false);
344 return NULL;
345 }
346 }
347
348 /* There is no exact match (we would have returned it by now). If there
349 * are multiple inexact matches, the call is ambiguous, which is an error.
350 *
351 * FINISHME: Report a decent error. Returning NULL will likely result in
352 * FINISHME: a "no matching signature" error; it should report that the
353 * FINISHME: call is ambiguous. But reporting errors from here is hard.
354 */
355 *is_exact = false;
356
357 match = choose_best_inexact_overload(state, actual_parameters,
358 inexact_matches, num_inexact_matches);
359
360 free(inexact_matches);
361 return match;
362 }
363
364
365 static bool
366 parameter_lists_match_exact(const exec_list *list_a, const exec_list *list_b)
367 {
368 const exec_node *node_a = list_a->head;
369 const exec_node *node_b = list_b->head;
370
371 for (/* empty */
372 ; !node_a->is_tail_sentinel() && !node_b->is_tail_sentinel()
373 ; node_a = node_a->next, node_b = node_b->next) {
374 ir_variable *a = (ir_variable *) node_a;
375 ir_variable *b = (ir_variable *) node_b;
376
377 /* If the types of the parameters do not match, the parameters lists
378 * are different.
379 */
380 if (a->type != b->type)
381 return false;
382 }
383
384 /* Unless both lists are exhausted, they differ in length and, by
385 * definition, do not match.
386 */
387 return (node_a->is_tail_sentinel() == node_b->is_tail_sentinel());
388 }
389
390 ir_function_signature *
391 ir_function::exact_matching_signature(_mesa_glsl_parse_state *state,
392 const exec_list *actual_parameters)
393 {
394 foreach_in_list(ir_function_signature, sig, &this->signatures) {
395 /* Skip over any built-ins that aren't available in this shader. */
396 if (sig->is_builtin() && !sig->is_builtin_available(state))
397 continue;
398
399 if (parameter_lists_match_exact(&sig->parameters, actual_parameters))
400 return sig;
401 }
402 return NULL;
403 }