Debian Codegraph @

third_party/rust/glslopt/glsl-optimizer/src/compiler/glsl/glsl_optimizer.cpp

raw
1 #include "glsl_optimizer.h"
2 #include "ast.h"
3 #include "glsl_parser_extras.h"
4 #include "glsl_parser.h"
5 #include "ir_optimization.h"
6 // FIXME: metal
7 // #include "ir_print_metal_visitor.h"
8 #include "ir_print_glsl_visitor.h"
9 #include "ir_print_visitor.h"
10 // FIXME: stats
11 // #include "ir_stats.h"
12 #include "loop_analysis.h"
13 #include "program.h"
14 #include "linker.h"
15 #include "main/mtypes.h"
16 #include "standalone_scaffolding.h"
17 #include "builtin_functions.h"
18 #include "program/program.h"
19
20 static void
21 init_gl_program(struct gl_program *prog, bool is_arb_asm, gl_shader_stage stage)
22 {
23 prog->RefCount = 1;
24 prog->Format = GL_PROGRAM_FORMAT_ASCII_ARB;
25 prog->is_arb_asm = is_arb_asm;
26 prog->info.stage = stage;
27 }
28
29 static struct gl_program *
30 new_program(UNUSED struct gl_context *ctx, gl_shader_stage stage,
31 UNUSED GLuint id, bool is_arb_asm)
32 {
33 struct gl_program *prog = rzalloc(NULL, struct gl_program);
34 init_gl_program(prog, is_arb_asm, stage);
35 return prog;
36 }
37
38 static void
39 initialize_mesa_context(struct gl_context *ctx, glslopt_target api)
40 {
41 gl_api mesaAPI;
42 switch(api)
43 {
44 default:
45 case kGlslTargetOpenGL:
46 mesaAPI = API_OPENGL_COMPAT;
47 break;
48 case kGlslTargetOpenGLES20:
49 mesaAPI = API_OPENGLES2;
50 break;
51 case kGlslTargetOpenGLES30:
52 mesaAPI = API_OPENGL_CORE;
53 break;
54 case kGlslTargetMetal:
55 mesaAPI = API_OPENGL_CORE;
56 break;
57 }
58 initialize_context_to_defaults (ctx, mesaAPI);
59 _mesa_glsl_builtin_functions_init_or_ref();
60
61 switch(api)
62 {
63 default:
64 case kGlslTargetOpenGL:
65 ctx->Const.GLSLVersion = 150;
66 break;
67 case kGlslTargetOpenGLES20:
68 ctx->Extensions.OES_standard_derivatives = true;
69 // FIXME: extensions
70 // ctx->Extensions.EXT_shadow_samplers = true;
71 // ctx->Extensions.EXT_frag_depth = true;
72 ctx->Extensions.EXT_shader_framebuffer_fetch = true;
73 break;
74 case kGlslTargetOpenGLES30:
75 ctx->Extensions.ARB_ES3_1_compatibility = true;
76 ctx->Extensions.EXT_shader_framebuffer_fetch = true;
77 break;
78 case kGlslTargetMetal:
79 ctx->Extensions.ARB_ES3_compatibility = true;
80 ctx->Extensions.EXT_shader_framebuffer_fetch = true;
81 break;
82 }
83
84
85 // allow high amount of texcoords
86 ctx->Const.MaxTextureCoordUnits = 16;
87
88 ctx->Const.Program[MESA_SHADER_VERTEX].MaxTextureImageUnits = 16;
89 ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxTextureImageUnits = 16;
90 ctx->Const.Program[MESA_SHADER_GEOMETRY].MaxTextureImageUnits = 16;
91
92 // For GLES2.0 this would be 1, but we do support GL_EXT_draw_buffers
93 ctx->Const.MaxDrawBuffers = 4;
94
95 ctx->Driver.NewProgram = new_program;
96 }
97
98
99 struct glslopt_ctx {
100 glslopt_ctx (glslopt_target target) {
101 this->target = target;
102 mem_ctx = ralloc_context (NULL);
103 initialize_mesa_context (&mesa_ctx, target);
104 }
105 ~glslopt_ctx() {
106 ralloc_free (mem_ctx);
107 }
108 struct gl_context mesa_ctx;
109 void* mem_ctx;
110 glslopt_target target;
111 };
112
113 glslopt_ctx* glslopt_initialize (glslopt_target target)
114 {
115 return new glslopt_ctx(target);
116 }
117
118 void glslopt_cleanup (glslopt_ctx* ctx)
119 {
120 delete ctx;
121 }
122
123 void glslopt_set_max_unroll_iterations (glslopt_ctx* ctx, unsigned iterations)
124 {
125 for (int i = 0; i < MESA_SHADER_STAGES; ++i)
126 ctx->mesa_ctx.Const.ShaderCompilerOptions[i].MaxUnrollIterations = iterations;
127 }
128
129 struct glslopt_shader_var
130 {
131 const char* name;
132 glslopt_basic_type type;
133 glslopt_precision prec;
134 int vectorSize;
135 int matrixSize;
136 int arraySize;
137 int location;
138 };
139
140 struct glslopt_shader
141 {
142 static void* operator new(size_t size, void *ctx)
143 {
144 void *node;
145 node = ralloc_size(ctx, size);
146 assert(node != NULL);
147 return node;
148 }
149 static void operator delete(void *node)
150 {
151 ralloc_free(node);
152 }
153
154 glslopt_shader ()
155 : rawOutput(0)
156 , optimizedOutput(0)
157 , status(false)
158 , uniformCount(0)
159 , uniformsSize(0)
160 , inputCount(0)
161 , textureCount(0)
162 , statsMath(0)
163 , statsTex(0)
164 , statsFlow(0)
165 {
166 infoLog = "Shader not compiled yet";
167
168 whole_program = rzalloc (NULL, struct gl_shader_program);
169 assert(whole_program != NULL);
170 whole_program->data = rzalloc(whole_program, struct gl_shader_program_data);
171 assert(whole_program->data != NULL);
172 whole_program->data->InfoLog = ralloc_strdup(whole_program->data, "");
173
174 whole_program->Shaders = reralloc(whole_program, whole_program->Shaders, struct gl_shader *, whole_program->NumShaders + 1);
175 assert(whole_program->Shaders != NULL);
176
177 shader = rzalloc(whole_program, gl_shader);
178 whole_program->Shaders[whole_program->NumShaders] = shader;
179 whole_program->NumShaders++;
180
181 whole_program->data->LinkStatus = LINKING_SUCCESS;
182 }
183
184 ~glslopt_shader()
185 {
186 for (unsigned i = 0; i < MESA_SHADER_STAGES; i++)
187 ralloc_free(whole_program->_LinkedShaders[i]);
188 ralloc_free(whole_program);
189 ralloc_free(rawOutput);
190 ralloc_free(optimizedOutput);
191 }
192
193 struct gl_shader_program* whole_program;
194 struct gl_shader* shader;
195
196 static const int kMaxShaderUniforms = 1024;
197 static const int kMaxShaderInputs = 128;
198 static const int kMaxShaderTextures = 128;
199 glslopt_shader_var uniforms[kMaxShaderUniforms];
200 glslopt_shader_var inputs[kMaxShaderInputs];
201 glslopt_shader_var textures[kMaxShaderInputs];
202 int uniformCount, uniformsSize;
203 int inputCount;
204 int textureCount;
205 int statsMath, statsTex, statsFlow;
206
207 char* rawOutput;
208 char* optimizedOutput;
209 const char* infoLog;
210 bool status;
211 };
212
213 static inline void debug_print_ir (const char* name, exec_list* ir, _mesa_glsl_parse_state* state, void* memctx)
214 {
215 #if 0
216 printf("**** %s:\n", name);
217 // _mesa_print_ir (ir, state);
218 char* foobar = _mesa_print_ir_glsl(ir, state, ralloc_strdup(memctx, ""), kPrintGlslFragment);
219 printf("%s\n", foobar);
220 validate_ir_tree(ir);
221 #endif
222 }
223
224
225 // FIXME: precision
226 // struct precision_ctx
227 // {
228 // exec_list* root_ir;
229 // bool res;
230 // };
231
232
233 // static void propagate_precision_deref(ir_instruction *ir, void *data)
234 // {
235 // // variable deref with undefined precision: take from variable itself
236 // ir_dereference_variable* der = ir->as_dereference_variable();
237 // if (der && der->get_precision() == glsl_precision_undefined && der->var->data.precision != glsl_precision_undefined)
238 // {
239 // der->set_precision ((glsl_precision)der->var->data.precision);
240 // ((precision_ctx*)data)->res = true;
241 // }
242
243 // // array deref with undefined precision: take from array itself
244 // ir_dereference_array* der_arr = ir->as_dereference_array();
245 // if (der_arr && der_arr->get_precision() == glsl_precision_undefined && der_arr->array->get_precision() != glsl_precision_undefined)
246 // {
247 // der_arr->set_precision (der_arr->array->get_precision());
248 // ((precision_ctx*)data)->res = true;
249 // }
250
251 // // swizzle with undefined precision: take from swizzle argument
252 // ir_swizzle* swz = ir->as_swizzle();
253 // if (swz && swz->get_precision() == glsl_precision_undefined && swz->val->get_precision() != glsl_precision_undefined)
254 // {
255 // swz->set_precision (swz->val->get_precision());
256 // ((precision_ctx*)data)->res = true;
257 // }
258
259 // }
260
261 // static void propagate_precision_expr(ir_instruction *ir, void *data)
262 // {
263 // ir_expression* expr = ir->as_expression();
264 // if (!expr)
265 // return;
266 // if (expr->get_precision() != glsl_precision_undefined)
267 // return;
268
269 // glsl_precision prec_params_max = glsl_precision_undefined;
270 // for (int i = 0; i < (int)expr->get_num_operands(); ++i)
271 // {
272 // ir_rvalue* op = expr->operands[i];
273 // if (op && op->get_precision() != glsl_precision_undefined)
274 // prec_params_max = higher_precision (prec_params_max, op->get_precision());
275 // }
276 // if (expr->get_precision() != prec_params_max)
277 // {
278 // expr->set_precision (prec_params_max);
279 // ((precision_ctx*)data)->res = true;
280 // }
281
282 // }
283
284 // static void propagate_precision_texture(ir_instruction *ir, void *data)
285 // {
286 // ir_texture* tex = ir->as_texture();
287 // if (!tex)
288 // return;
289
290 // glsl_precision sampler_prec = tex->sampler->get_precision();
291 // if (tex->get_precision() == sampler_prec || sampler_prec == glsl_precision_undefined)
292 // return;
293
294 // // set precision of ir_texture node to that of the sampler itself
295 // tex->set_precision(sampler_prec);
296 // ((precision_ctx*)data)->res = true;
297 // }
298
299 // struct undefined_ass_ctx
300 // {
301 // ir_variable* var;
302 // bool res;
303 // };
304
305 // static void has_only_undefined_precision_assignments(ir_instruction *ir, void *data)
306 // {
307 // ir_assignment* ass = ir->as_assignment();
308 // if (!ass)
309 // return;
310 // undefined_ass_ctx* ctx = (undefined_ass_ctx*)data;
311 // if (ass->whole_variable_written() != ctx->var)
312 // return;
313 // glsl_precision prec = ass->rhs->get_precision();
314 // if (prec == glsl_precision_undefined)
315 // return;
316 // ctx->res = false;
317 // }
318
319
320 // static void propagate_precision_assign(ir_instruction *ir, void *data)
321 // {
322 // ir_assignment* ass = ir->as_assignment();
323 // if (!ass || !ass->lhs || !ass->rhs)
324 // return;
325
326 // glsl_precision lp = ass->lhs->get_precision();
327 // glsl_precision rp = ass->rhs->get_precision();
328
329 // // for assignments with LHS having undefined precision, take it from RHS
330 // if (rp != glsl_precision_undefined)
331 // {
332 // ir_variable* lhs_var = ass->lhs->variable_referenced();
333 // if (lp == glsl_precision_undefined)
334 // {
335 // if (lhs_var)
336 // lhs_var->data.precision = rp;
337 // ass->lhs->set_precision (rp);
338 // ((precision_ctx*)data)->res = true;
339 // }
340 // return;
341 // }
342
343 // // for assignments where LHS has precision, but RHS is a temporary variable
344 // // with undefined precision that's only assigned from other undefined precision
345 // // sources -> make the RHS variable take LHS precision
346 // if (lp != glsl_precision_undefined && rp == glsl_precision_undefined)
347 // {
348 // ir_dereference* deref = ass->rhs->as_dereference();
349 // if (deref)
350 // {
351 // ir_variable* rhs_var = deref->variable_referenced();
352 // if (rhs_var && rhs_var->data.mode == ir_var_temporary && rhs_var->data.precision == glsl_precision_undefined)
353 // {
354 // undefined_ass_ctx ctx;
355 // ctx.var = rhs_var;
356 // // find if we only assign to it from undefined precision sources
357 // ctx.res = true;
358 // exec_list* root_ir = ((precision_ctx*)data)->root_ir;
359 // foreach_in_list(ir_instruction, inst, root_ir)
360 // {
361 // visit_tree (ir, has_only_undefined_precision_assignments, &ctx);
362 // }
363 // if (ctx.res)
364 // {
365 // rhs_var->data.precision = lp;
366 // ass->rhs->set_precision(lp);
367 // ((precision_ctx*)data)->res = true;
368 // }
369 // }
370 // }
371 // return;
372 // }
373 // }
374
375
376 // static void propagate_precision_call(ir_instruction *ir, void *data)
377 // {
378 // ir_call* call = ir->as_call();
379 // if (!call)
380 // return;
381 // if (!call->return_deref)
382 // return;
383 // if (call->return_deref->get_precision() == glsl_precision_undefined /*&& call->callee->precision == glsl_precision_undefined*/)
384 // {
385 // glsl_precision prec_params_max = glsl_precision_undefined;
386 // foreach_two_lists(formal_node, &call->callee->parameters,
387 // actual_node, &call->actual_parameters) {
388 // ir_variable* sig_param = (ir_variable*)formal_node;
389 // ir_rvalue* param = (ir_rvalue*)actual_node;
390
391 // glsl_precision p = (glsl_precision)sig_param->data.precision;
392 // if (p == glsl_precision_undefined)
393 // p = param->get_precision();
394
395 // prec_params_max = higher_precision (prec_params_max, p);
396 // }
397 // if (call->return_deref->get_precision() != prec_params_max)
398 // {
399 // call->return_deref->set_precision (prec_params_max);
400 // ((precision_ctx*)data)->res = true;
401 // }
402 // }
403 // }
404
405 // static bool propagate_precision(exec_list* list, bool assign_high_to_undefined)
406 // {
407 // bool anyProgress = false;
408 // precision_ctx ctx;
409
410 // do {
411 // ctx.res = false;
412 // ctx.root_ir = list;
413 // foreach_in_list(ir_instruction, ir, list)
414 // {
415 // visit_tree (ir, propagate_precision_texture, &ctx);
416 // visit_tree (ir, propagate_precision_deref, &ctx);
417 // bool hadProgress = ctx.res;
418 // ctx.res = false;
419 // visit_tree (ir, propagate_precision_assign, &ctx);
420 // if (ctx.res)
421 // {
422 // // assignment precision propagation might have added precision
423 // // to some variables; need to propagate dereference precision right
424 // // after that too.
425 // visit_tree (ir, propagate_precision_deref, &ctx);
426 // }
427 // ctx.res |= hadProgress;
428 // visit_tree (ir, propagate_precision_call, &ctx);
429 // visit_tree (ir, propagate_precision_expr, &ctx);
430 // }
431 // anyProgress |= ctx.res;
432 // } while (ctx.res);
433 // anyProgress |= ctx.res;
434
435 // // for globals that have undefined precision, set it to highp
436 // if (assign_high_to_undefined)
437 // {
438 // foreach_in_list(ir_instruction, ir, list)
439 // {
440 // ir_variable* var = ir->as_variable();
441 // if (var)
442 // {
443 // if (var->data.precision == glsl_precision_undefined)
444 // {
445 // var->data.precision = glsl_precision_high;
446 // anyProgress = true;
447 // }
448 // }
449 // }
450 // }
451
452 // return anyProgress;
453 // }
454
455
456 static void do_optimization_passes(exec_list* ir, bool linked, _mesa_glsl_parse_state* state, void* mem_ctx)
457 {
458 bool progress;
459 // FIXME: Shouldn't need to bound the number of passes
460 int passes = 0,
461 kMaximumPasses = 1000;
462 do {
463 progress = false;
464 ++passes;
465 bool progress2;
466 debug_print_ir ("Initial", ir, state, mem_ctx);
467 if (linked) {
468 progress2 = do_function_inlining(ir); progress |= progress2; if (progress2) debug_print_ir ("After inlining", ir, state, mem_ctx);
469 progress2 = do_dead_functions(ir); progress |= progress2; if (progress2) debug_print_ir ("After dead functions", ir, state, mem_ctx);
470 progress2 = do_structure_splitting(ir); progress |= progress2; if (progress2) debug_print_ir ("After struct splitting", ir, state, mem_ctx);
471 }
472 progress2 = do_if_simplification(ir); progress |= progress2; if (progress2) debug_print_ir ("After if simpl", ir, state, mem_ctx);
473 progress2 = opt_flatten_nested_if_blocks(ir); progress |= progress2; if (progress2) debug_print_ir ("After if flatten", ir, state, mem_ctx);
474 // progress2 = propagate_precision (ir, state->metal_target); progress |= progress2; if (progress2) debug_print_ir ("After prec propagation", ir, state, mem_ctx);
475 progress2 = do_copy_propagation_elements(ir); progress |= progress2; if (progress2) debug_print_ir ("After copy propagation elems", ir, state, mem_ctx);
476
477 if (linked)
478 {
479 progress2 = do_vectorize(ir); progress |= progress2; if (progress2) debug_print_ir ("After vectorize", ir, state, mem_ctx);
480 }
481 if (linked) {
482 progress2 = do_dead_code(ir,false); progress |= progress2; if (progress2) debug_print_ir ("After dead code", ir, state, mem_ctx);
483 } else {
484 progress2 = do_dead_code_unlinked(ir); progress |= progress2; if (progress2) debug_print_ir ("After dead code unlinked", ir, state, mem_ctx);
485 }
486 progress2 = do_dead_code_local(ir); progress |= progress2; if (progress2) debug_print_ir ("After dead code local", ir, state, mem_ctx);
487 // progress2 = propagate_precision (ir, state->metal_target); progress |= progress2; if (progress2) debug_print_ir ("After prec propagation", ir, state, mem_ctx);
488 progress2 = do_tree_grafting(ir); progress |= progress2; if (progress2) debug_print_ir ("After tree grafting", ir, state, mem_ctx);
489 progress2 = do_constant_propagation(ir); progress |= progress2; if (progress2) debug_print_ir ("After const propagation", ir, state, mem_ctx);
490 if (linked) {
491 progress2 = do_constant_variable(ir); progress |= progress2; if (progress2) debug_print_ir ("After const variable", ir, state, mem_ctx);
492 } else {
493 progress2 = do_constant_variable_unlinked(ir); progress |= progress2; if (progress2) debug_print_ir ("After const variable unlinked", ir, state, mem_ctx);
494 }
495 progress2 = do_constant_folding(ir); progress |= progress2; if (progress2) debug_print_ir ("After const folding", ir, state, mem_ctx);
496 progress2 = do_minmax_prune(ir); progress |= progress2; if (progress2) debug_print_ir ("After minmax prune", ir, state, mem_ctx);
497 progress2 = do_rebalance_tree(ir); progress |= progress2; if (progress2) debug_print_ir ("After rebalance tree", ir, state, mem_ctx);
498 progress2 = do_algebraic(ir, state->ctx->Const.NativeIntegers, &state->ctx->Const.ShaderCompilerOptions[state->stage]); progress |= progress2; if (progress2) debug_print_ir ("After algebraic", ir, state, mem_ctx);
499 progress2 = do_lower_jumps(ir); progress |= progress2; if (progress2) debug_print_ir ("After lower jumps", ir, state, mem_ctx);
500 progress2 = do_vec_index_to_swizzle(ir); progress |= progress2; if (progress2) debug_print_ir ("After vec index to swizzle", ir, state, mem_ctx);
501 progress2 = lower_vector_insert(ir, false); progress |= progress2; if (progress2) debug_print_ir ("After lower vector insert", ir, state, mem_ctx);
502 progress2 = optimize_swizzles(ir); progress |= progress2; if (progress2) debug_print_ir ("After optimize swizzles", ir, state, mem_ctx);
503 progress2 = optimize_split_arrays(ir, linked); progress |= progress2; if (progress2) debug_print_ir ("After split arrays", ir, state, mem_ctx);
504 progress2 = optimize_redundant_jumps(ir); progress |= progress2; if (progress2) debug_print_ir ("After redundant jumps", ir, state, mem_ctx);
505
506 // do loop stuff only when linked; otherwise causes duplicate loop induction variable
507 // problems (ast-in.txt test)
508 if (linked)
509 {
510 loop_state *ls = analyze_loop_variables(ir);
511 if (ls->loop_found) {
512 progress2 = unroll_loops(ir, ls, &state->ctx->Const.ShaderCompilerOptions[state->stage]); progress |= progress2; if (progress2) debug_print_ir ("After unroll", ir, state, mem_ctx);
513 }
514 delete ls;
515 }
516 } while (progress && passes < kMaximumPasses);
517
518 // GLSL/ES does not have saturate, so lower it
519 lower_instructions(ir, SAT_TO_CLAMP);
520 }
521
522 // FIXME
523 // static void glsl_type_to_optimizer_desc(const glsl_type* type, glsl_precision prec, glslopt_shader_var* out)
524 // {
525 // out->arraySize = type->array_size();
526
527 // // type; use element type when in array
528 // if (type->is_array())
529 // type = type->element_type();
530
531 // if (type->is_float())
532 // out->type = kGlslTypeFloat;
533 // else if (type->is_integer())
534 // out->type = kGlslTypeInt;
535 // else if (type->is_boolean())
536 // out->type = kGlslTypeBool;
537 // else if (type->is_sampler())
538 // {
539 // if (type->sampler_dimensionality == GLSL_SAMPLER_DIM_2D)
540 // {
541 // if (type->sampler_shadow)
542 // out->type = kGlslTypeTex2DShadow;
543 // else if (type->sampler_array)
544 // out->type = kGlslTypeTex2DArray;
545 // else
546 // out->type = kGlslTypeTex2D;
547 // }
548 // else if (type->sampler_dimensionality == GLSL_SAMPLER_DIM_3D)
549 // out->type = kGlslTypeTex3D;
550 // else if (type->sampler_dimensionality == GLSL_SAMPLER_DIM_CUBE)
551 // out->type = kGlslTypeTexCube;
552 // else
553 // out->type = kGlslTypeOther;
554 // }
555 // else
556 // out->type = kGlslTypeOther;
557
558 // // sizes
559 // out->vectorSize = type->vector_elements;
560 // out->matrixSize = type->matrix_columns;
561
562 // // precision
563 // switch (prec)
564 // {
565 // case glsl_precision_high: out->prec = kGlslPrecHigh; break;
566 // case glsl_precision_medium: out->prec = kGlslPrecMedium; break;
567 // case glsl_precision_low: out->prec = kGlslPrecLow; break;
568 // default: out->prec = kGlslPrecHigh; break;
569 // }
570 // }
571
572 static void find_shader_variables(glslopt_shader* sh, exec_list* ir)
573 {
574 foreach_in_list(ir_instruction, node, ir)
575 {
576 ir_variable* const var = node->as_variable();
577 if (var == NULL)
578 continue;
579 if (var->data.mode == ir_var_shader_in)
580 {
581 if (sh->inputCount >= glslopt_shader::kMaxShaderInputs)
582 continue;
583
584 glslopt_shader_var& v = sh->inputs[sh->inputCount];
585 v.name = ralloc_strdup(sh, var->name);
586 // glsl_type_to_optimizer_desc(var->type, (glsl_precision)var->data.precision, &v);
587 v.location = var->data.explicit_location ? var->data.location : -1;
588 ++sh->inputCount;
589 }
590 if (var->data.mode == ir_var_uniform && !var->type->is_sampler())
591 {
592 if (sh->uniformCount >= glslopt_shader::kMaxShaderUniforms)
593 continue;
594
595 glslopt_shader_var& v = sh->uniforms[sh->uniformCount];
596 v.name = ralloc_strdup(sh, var->name);
597 // glsl_type_to_optimizer_desc(var->type, (glsl_precision)var->data.precision, &v);
598 v.location = var->data.explicit_location ? var->data.location : -1;
599 ++sh->uniformCount;
600 }
601 if (var->data.mode == ir_var_uniform && var->type->is_sampler())
602 {
603 if (sh->textureCount >= glslopt_shader::kMaxShaderTextures)
604 continue;
605
606 glslopt_shader_var& v = sh->textures[sh->textureCount];
607 v.name = ralloc_strdup(sh, var->name);
608 // glsl_type_to_optimizer_desc(var->type, (glsl_precision)var->data.precision, &v);
609 v.location = var->data.explicit_location ? var->data.location : -1;
610 ++sh->textureCount;
611 }
612 }
613 }
614
615 glslopt_shader* glslopt_optimize (glslopt_ctx* ctx, glslopt_shader_type type, const char* shaderSource, unsigned options)
616 {
617 glslopt_shader* shader = new (ctx->mem_ctx) glslopt_shader ();
618
619 PrintGlslMode printMode = kPrintGlslVertex;
620 switch (type) {
621 case kGlslOptShaderVertex:
622 shader->shader->Type = GL_VERTEX_SHADER;
623 shader->shader->Stage = MESA_SHADER_VERTEX;
624 printMode = kPrintGlslVertex;
625 break;
626 case kGlslOptShaderFragment:
627 shader->shader->Type = GL_FRAGMENT_SHADER;
628 shader->shader->Stage = MESA_SHADER_FRAGMENT;
629 printMode = kPrintGlslFragment;
630 break;
631 }
632 if (!shader->shader->Type)
633 {
634 shader->infoLog = ralloc_asprintf (shader, "Unknown shader type %d", (int)type);
635 shader->status = false;
636 return shader;
637 }
638
639 _mesa_glsl_parse_state* state = new (shader) _mesa_glsl_parse_state (&ctx->mesa_ctx, shader->shader->Stage, shader);
640 state->error = 0;
641
642 if (!(options & kGlslOptionSkipPreprocessor))
643 {
644 state->error = !!glcpp_preprocess (state, &shaderSource, &state->info_log, add_builtin_defines, state, &ctx->mesa_ctx);
645 if (state->error)
646 {
647 shader->status = !state->error;
648 shader->infoLog = state->info_log;
649 return shader;
650 }
651 }
652
653 _mesa_glsl_lexer_ctor (state, shaderSource);
654 _mesa_glsl_parse (state);
655 _mesa_glsl_lexer_dtor (state);
656
657 exec_list* ir = new (shader) exec_list();
658 shader->shader->ir = ir;
659
660 if (!state->error && !state->translation_unit.is_empty())
661 _mesa_ast_to_hir (ir, state);
662
663 // Un-optimized output
664 if (!state->error) {
665 validate_ir_tree(ir);
666 shader->rawOutput = _mesa_print_ir_glsl(ir, state, ralloc_strdup(shader, ""), printMode);
667 }
668
669 // Lower builtin functions prior to linking.
670 lower_builtins(ir);
671
672 // Link built-in functions
673 shader->shader->symbols = state->symbols;
674
675 struct gl_linked_shader* linked_shader = NULL;
676
677 if (!state->error && !ir->is_empty() && !(options & kGlslOptionNotFullShader))
678 {
679 linked_shader = link_intrastage_shaders(shader,
680 &ctx->mesa_ctx,
681 shader->whole_program,
682 shader->whole_program->Shaders,
683 shader->whole_program->NumShaders,
684 true);
685 if (!linked_shader)
686 {
687 shader->status = false;
688 shader->infoLog = shader->whole_program->data->InfoLog;
689 return shader;
690 }
691 ir = linked_shader->ir;
692
693 debug_print_ir ("==== After link ====", ir, state, shader);
694 }
695
696 // Do optimization post-link
697 if (!state->error && !ir->is_empty())
698 {
699 const bool linked = !(options & kGlslOptionNotFullShader);
700 do_optimization_passes(ir, linked, state, shader);
701 validate_ir_tree(ir);
702 }
703
704 // Final optimized output
705 if (!state->error)
706 {
707 shader->optimizedOutput = _mesa_print_ir_glsl(ir, state, ralloc_strdup(shader, ""), printMode);
708 }
709
710 shader->status = !state->error;
711 shader->infoLog = state->info_log;
712
713 find_shader_variables (shader, ir);
714 // FIXME: stats
715 // if (!state->error)
716 // calculate_shader_stats (ir, &shader->statsMath, &shader->statsTex, &shader->statsFlow);
717
718 ralloc_free (ir);
719 ralloc_free (state);
720
721 if (linked_shader)
722 ralloc_free(linked_shader);
723
724 return shader;
725 }
726
727 void glslopt_shader_delete (glslopt_shader* shader)
728 {
729 delete shader;
730 }
731
732 bool glslopt_get_status (glslopt_shader* shader)
733 {
734 return shader->status;
735 }
736
737 const char* glslopt_get_output (glslopt_shader* shader)
738 {
739 return shader->optimizedOutput;
740 }
741
742 const char* glslopt_get_raw_output (glslopt_shader* shader)
743 {
744 return shader->rawOutput;
745 }
746
747 const char* glslopt_get_log (glslopt_shader* shader)
748 {
749 return shader->infoLog;
750 }
751
752 int glslopt_shader_get_input_count (glslopt_shader* shader)
753 {
754 return shader->inputCount;
755 }
756
757 int glslopt_shader_get_uniform_count (glslopt_shader* shader)
758 {
759 return shader->uniformCount;
760 }
761
762 int glslopt_shader_get_uniform_total_size (glslopt_shader* shader)
763 {
764 return shader->uniformsSize;
765 }
766
767 int glslopt_shader_get_texture_count (glslopt_shader* shader)
768 {
769 return shader->textureCount;
770 }
771
772 void glslopt_shader_get_input_desc (glslopt_shader* shader, int index, const char** outName, glslopt_basic_type* outType, glslopt_precision* outPrec, int* outVecSize, int* outMatSize, int* outArraySize, int* outLocation)
773 {
774 const glslopt_shader_var& v = shader->inputs[index];
775 *outName = v.name;
776 *outType = v.type;
777 *outPrec = v.prec;
778 *outVecSize = v.vectorSize;
779 *outMatSize = v.matrixSize;
780 *outArraySize = v.arraySize;
781 *outLocation = v.location;
782 }
783
784 void glslopt_shader_get_uniform_desc (glslopt_shader* shader, int index, const char** outName, glslopt_basic_type* outType, glslopt_precision* outPrec, int* outVecSize, int* outMatSize, int* outArraySize, int* outLocation)
785 {
786 const glslopt_shader_var& v = shader->uniforms[index];
787 *outName = v.name;
788 *outType = v.type;
789 *outPrec = v.prec;
790 *outVecSize = v.vectorSize;
791 *outMatSize = v.matrixSize;
792 *outArraySize = v.arraySize;
793 *outLocation = v.location;
794 }
795
796 void glslopt_shader_get_texture_desc (glslopt_shader* shader, int index, const char** outName, glslopt_basic_type* outType, glslopt_precision* outPrec, int* outVecSize, int* outMatSize, int* outArraySize, int* outLocation)
797 {
798 const glslopt_shader_var& v = shader->textures[index];
799 *outName = v.name;
800 *outType = v.type;
801 *outPrec = v.prec;
802 *outVecSize = v.vectorSize;
803 *outMatSize = v.matrixSize;
804 *outArraySize = v.arraySize;
805 *outLocation = v.location;
806 }
807
808 void glslopt_shader_get_stats (glslopt_shader* shader, int* approxMath, int* approxTex, int* approxFlow)
809 {
810 *approxMath = shader->statsMath;
811 *approxTex = shader->statsTex;
812 *approxFlow = shader->statsFlow;
813 }