glsl: Implement ARB_texture_query_lod
[mesa.git] / src / glsl / ir_print_visitor.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 "ir_print_visitor.h"
25 #include "glsl_types.h"
26 #include "glsl_parser_extras.h"
27 #include "program/hash_table.h"
28
29 static void print_type(const glsl_type *t);
30
31 void
32 ir_instruction::print(void) const
33 {
34 ir_instruction *deconsted = const_cast<ir_instruction *>(this);
35
36 ir_print_visitor v;
37 deconsted->accept(&v);
38 }
39
40 void
41 _mesa_print_ir(exec_list *instructions,
42 struct _mesa_glsl_parse_state *state)
43 {
44 if (state) {
45 for (unsigned i = 0; i < state->num_user_structures; i++) {
46 const glsl_type *const s = state->user_structures[i];
47
48 printf("(structure (%s) (%s@%p) (%u) (\n",
49 s->name, s->name, (void *) s, s->length);
50
51 for (unsigned j = 0; j < s->length; j++) {
52 printf("\t((");
53 print_type(s->fields.structure[j].type);
54 printf(")(%s))\n", s->fields.structure[j].name);
55 }
56
57 printf(")\n");
58 }
59 }
60
61 printf("(\n");
62 foreach_iter(exec_list_iterator, iter, *instructions) {
63 ir_instruction *ir = (ir_instruction *)iter.get();
64 ir->print();
65 if (ir->ir_type != ir_type_function)
66 printf("\n");
67 }
68 printf("\n)");
69 }
70
71 ir_print_visitor::ir_print_visitor()
72 {
73 indentation = 0;
74 printable_names =
75 hash_table_ctor(32, hash_table_pointer_hash, hash_table_pointer_compare);
76 symbols = _mesa_symbol_table_ctor();
77 mem_ctx = ralloc_context(NULL);
78 }
79
80 ir_print_visitor::~ir_print_visitor()
81 {
82 hash_table_dtor(printable_names);
83 _mesa_symbol_table_dtor(symbols);
84 ralloc_free(mem_ctx);
85 }
86
87 void ir_print_visitor::indent(void)
88 {
89 for (int i = 0; i < indentation; i++)
90 printf(" ");
91 }
92
93 const char *
94 ir_print_visitor::unique_name(ir_variable *var)
95 {
96 /* var->name can be NULL in function prototypes when a type is given for a
97 * parameter but no name is given. In that case, just return an empty
98 * string. Don't worry about tracking the generated name in the printable
99 * names hash because this is the only scope where it can ever appear.
100 */
101 if (var->name == NULL) {
102 static unsigned arg = 1;
103 return ralloc_asprintf(this->mem_ctx, "parameter@%u", arg++);
104 }
105
106 /* Do we already have a name for this variable? */
107 const char *name = (const char *) hash_table_find(this->printable_names, var);
108 if (name != NULL)
109 return name;
110
111 /* If there's no conflict, just use the original name */
112 if (_mesa_symbol_table_find_symbol(this->symbols, -1, var->name) == NULL) {
113 name = var->name;
114 } else {
115 static unsigned i = 1;
116 name = ralloc_asprintf(this->mem_ctx, "%s@%u", var->name, ++i);
117 }
118 hash_table_insert(this->printable_names, (void *) name, var);
119 _mesa_symbol_table_add_symbol(this->symbols, -1, name, var);
120 return name;
121 }
122
123 static void
124 print_type(const glsl_type *t)
125 {
126 if (t->base_type == GLSL_TYPE_ARRAY) {
127 printf("(array ");
128 print_type(t->fields.array);
129 printf(" %u)", t->length);
130 } else if ((t->base_type == GLSL_TYPE_STRUCT)
131 && (strncmp("gl_", t->name, 3) != 0)) {
132 printf("%s@%p", t->name, (void *) t);
133 } else {
134 printf("%s", t->name);
135 }
136 }
137
138 void ir_print_visitor::visit(ir_rvalue *ir)
139 {
140 printf("error");
141 }
142
143 void ir_print_visitor::visit(ir_variable *ir)
144 {
145 printf("(declare ");
146
147 const char *const cent = (ir->centroid) ? "centroid " : "";
148 const char *const inv = (ir->invariant) ? "invariant " : "";
149 const char *const mode[] = { "", "uniform ", "shader_in ", "shader_out ",
150 "in ", "out ", "inout ",
151 "const_in ", "sys ", "temporary " };
152 const char *const interp[] = { "", "flat", "noperspective" };
153
154 printf("(%s%s%s%s) ",
155 cent, inv, mode[ir->mode], interp[ir->interpolation]);
156
157 print_type(ir->type);
158 printf(" %s)", unique_name(ir));
159 }
160
161
162 void ir_print_visitor::visit(ir_function_signature *ir)
163 {
164 _mesa_symbol_table_push_scope(symbols);
165 printf("(signature ");
166 indentation++;
167
168 print_type(ir->return_type);
169 printf("\n");
170 indent();
171
172 printf("(parameters\n");
173 indentation++;
174
175 foreach_iter(exec_list_iterator, iter, ir->parameters) {
176 ir_variable *const inst = (ir_variable *) iter.get();
177
178 indent();
179 inst->accept(this);
180 printf("\n");
181 }
182 indentation--;
183
184 indent();
185 printf(")\n");
186
187 indent();
188
189 printf("(\n");
190 indentation++;
191
192 foreach_iter(exec_list_iterator, iter, ir->body) {
193 ir_instruction *const inst = (ir_instruction *) iter.get();
194
195 indent();
196 inst->accept(this);
197 printf("\n");
198 }
199 indentation--;
200 indent();
201 printf("))\n");
202 indentation--;
203 _mesa_symbol_table_pop_scope(symbols);
204 }
205
206
207 void ir_print_visitor::visit(ir_function *ir)
208 {
209 printf("(function %s\n", ir->name);
210 indentation++;
211 foreach_iter(exec_list_iterator, iter, *ir) {
212 ir_function_signature *const sig = (ir_function_signature *) iter.get();
213 indent();
214 sig->accept(this);
215 printf("\n");
216 }
217 indentation--;
218 indent();
219 printf(")\n\n");
220 }
221
222
223 void ir_print_visitor::visit(ir_expression *ir)
224 {
225 printf("(expression ");
226
227 print_type(ir->type);
228
229 printf(" %s ", ir->operator_string());
230
231 for (unsigned i = 0; i < ir->get_num_operands(); i++) {
232 ir->operands[i]->accept(this);
233 }
234
235 printf(") ");
236 }
237
238
239 void ir_print_visitor::visit(ir_texture *ir)
240 {
241 printf("(%s ", ir->opcode_string());
242
243 print_type(ir->type);
244 printf(" ");
245
246 ir->sampler->accept(this);
247 printf(" ");
248
249 if (ir->op != ir_txs) {
250 ir->coordinate->accept(this);
251
252 printf(" ");
253
254 if (ir->offset != NULL) {
255 ir->offset->accept(this);
256 } else {
257 printf("0");
258 }
259
260 printf(" ");
261 }
262
263 if (ir->op != ir_txf && ir->op != ir_txf_ms && ir->op != ir_txs) {
264 if (ir->projector)
265 ir->projector->accept(this);
266 else
267 printf("1");
268
269 if (ir->shadow_comparitor) {
270 printf(" ");
271 ir->shadow_comparitor->accept(this);
272 } else {
273 printf(" ()");
274 }
275 }
276
277 printf(" ");
278 switch (ir->op)
279 {
280 case ir_tex:
281 case ir_lod:
282 break;
283 case ir_txb:
284 ir->lod_info.bias->accept(this);
285 break;
286 case ir_txl:
287 case ir_txf:
288 case ir_txs:
289 ir->lod_info.lod->accept(this);
290 break;
291 case ir_txf_ms:
292 ir->lod_info.sample_index->accept(this);
293 break;
294 case ir_txd:
295 printf("(");
296 ir->lod_info.grad.dPdx->accept(this);
297 printf(" ");
298 ir->lod_info.grad.dPdy->accept(this);
299 printf(")");
300 break;
301 };
302 printf(")");
303 }
304
305
306 void ir_print_visitor::visit(ir_swizzle *ir)
307 {
308 const unsigned swiz[4] = {
309 ir->mask.x,
310 ir->mask.y,
311 ir->mask.z,
312 ir->mask.w,
313 };
314
315 printf("(swiz ");
316 for (unsigned i = 0; i < ir->mask.num_components; i++) {
317 printf("%c", "xyzw"[swiz[i]]);
318 }
319 printf(" ");
320 ir->val->accept(this);
321 printf(")");
322 }
323
324
325 void ir_print_visitor::visit(ir_dereference_variable *ir)
326 {
327 ir_variable *var = ir->variable_referenced();
328 printf("(var_ref %s) ", unique_name(var));
329 }
330
331
332 void ir_print_visitor::visit(ir_dereference_array *ir)
333 {
334 printf("(array_ref ");
335 ir->array->accept(this);
336 ir->array_index->accept(this);
337 printf(") ");
338 }
339
340
341 void ir_print_visitor::visit(ir_dereference_record *ir)
342 {
343 printf("(record_ref ");
344 ir->record->accept(this);
345 printf(" %s) ", ir->field);
346 }
347
348
349 void ir_print_visitor::visit(ir_assignment *ir)
350 {
351 printf("(assign ");
352
353 if (ir->condition)
354 ir->condition->accept(this);
355
356 char mask[5];
357 unsigned j = 0;
358
359 for (unsigned i = 0; i < 4; i++) {
360 if ((ir->write_mask & (1 << i)) != 0) {
361 mask[j] = "xyzw"[i];
362 j++;
363 }
364 }
365 mask[j] = '\0';
366
367 printf(" (%s) ", mask);
368
369 ir->lhs->accept(this);
370
371 printf(" ");
372
373 ir->rhs->accept(this);
374 printf(") ");
375 }
376
377
378 void ir_print_visitor::visit(ir_constant *ir)
379 {
380 printf("(constant ");
381 print_type(ir->type);
382 printf(" (");
383
384 if (ir->type->is_array()) {
385 for (unsigned i = 0; i < ir->type->length; i++)
386 ir->get_array_element(i)->accept(this);
387 } else if (ir->type->is_record()) {
388 ir_constant *value = (ir_constant *) ir->components.get_head();
389 for (unsigned i = 0; i < ir->type->length; i++) {
390 printf("(%s ", ir->type->fields.structure[i].name);
391 value->accept(this);
392 printf(")");
393
394 value = (ir_constant *) value->next;
395 }
396 } else {
397 for (unsigned i = 0; i < ir->type->components(); i++) {
398 if (i != 0)
399 printf(" ");
400 switch (ir->type->base_type) {
401 case GLSL_TYPE_UINT: printf("%u", ir->value.u[i]); break;
402 case GLSL_TYPE_INT: printf("%d", ir->value.i[i]); break;
403 case GLSL_TYPE_FLOAT: printf("%f", ir->value.f[i]); break;
404 case GLSL_TYPE_BOOL: printf("%d", ir->value.b[i]); break;
405 default: assert(0);
406 }
407 }
408 }
409 printf(")) ");
410 }
411
412
413 void
414 ir_print_visitor::visit(ir_call *ir)
415 {
416 printf("(call %s ", ir->callee_name());
417 if (ir->return_deref)
418 ir->return_deref->accept(this);
419 printf(" (");
420 foreach_iter(exec_list_iterator, iter, *ir) {
421 ir_instruction *const inst = (ir_instruction *) iter.get();
422
423 inst->accept(this);
424 }
425 printf("))\n");
426 }
427
428
429 void
430 ir_print_visitor::visit(ir_return *ir)
431 {
432 printf("(return");
433
434 ir_rvalue *const value = ir->get_value();
435 if (value) {
436 printf(" ");
437 value->accept(this);
438 }
439
440 printf(")");
441 }
442
443
444 void
445 ir_print_visitor::visit(ir_discard *ir)
446 {
447 printf("(discard ");
448
449 if (ir->condition != NULL) {
450 printf(" ");
451 ir->condition->accept(this);
452 }
453
454 printf(")");
455 }
456
457
458 void
459 ir_print_visitor::visit(ir_if *ir)
460 {
461 printf("(if ");
462 ir->condition->accept(this);
463
464 printf("(\n");
465 indentation++;
466
467 foreach_iter(exec_list_iterator, iter, ir->then_instructions) {
468 ir_instruction *const inst = (ir_instruction *) iter.get();
469
470 indent();
471 inst->accept(this);
472 printf("\n");
473 }
474
475 indentation--;
476 indent();
477 printf(")\n");
478
479 indent();
480 if (!ir->else_instructions.is_empty()) {
481 printf("(\n");
482 indentation++;
483
484 foreach_iter(exec_list_iterator, iter, ir->else_instructions) {
485 ir_instruction *const inst = (ir_instruction *) iter.get();
486
487 indent();
488 inst->accept(this);
489 printf("\n");
490 }
491 indentation--;
492 indent();
493 printf("))\n");
494 } else {
495 printf("())\n");
496 }
497 }
498
499
500 void
501 ir_print_visitor::visit(ir_loop *ir)
502 {
503 printf("(loop (");
504 if (ir->counter != NULL)
505 ir->counter->accept(this);
506 printf(") (");
507 if (ir->from != NULL)
508 ir->from->accept(this);
509 printf(") (");
510 if (ir->to != NULL)
511 ir->to->accept(this);
512 printf(") (");
513 if (ir->increment != NULL)
514 ir->increment->accept(this);
515 printf(") (\n");
516 indentation++;
517
518 foreach_iter(exec_list_iterator, iter, ir->body_instructions) {
519 ir_instruction *const inst = (ir_instruction *) iter.get();
520
521 indent();
522 inst->accept(this);
523 printf("\n");
524 }
525 indentation--;
526 indent();
527 printf("))\n");
528 }
529
530
531 void
532 ir_print_visitor::visit(ir_loop_jump *ir)
533 {
534 printf("%s", ir->is_break() ? "break" : "continue");
535 }