mesa/src/glsl
Vinson Lee 56176f00f5 glsl: Silence uninitialized variable warning.
i686-apple-darwin10-gcc-4.2.1 generated the following warning.
warning: 'score' may be used uninitialized in this function

GCC 4.4.3 on Linux didn't generate the above warning.
2010-08-21 20:42:47 -07:00
..
builtins generate_builtins.py: Remove unused import sys. 2010-08-21 01:13:16 -07:00
glcpp glcpp: Refresh autogenerated lexer. 2010-08-18 17:38:47 -07:00
tests glsl2: Move the compiler to the subdirectory it will live in in Mesa. 2010-06-24 15:36:00 -07:00
.dir-locals.el glsl2: Move the compiler to the subdirectory it will live in in Mesa. 2010-06-24 15:36:00 -07:00
.gitignore glsl: Ignore glsl_compiler and glsl_parser.output files. 2010-07-28 13:48:32 -07:00
ast.h glsl2: Remove unnecessary use of 'struct' before type names 2010-08-13 16:46:43 -07:00
ast_expr.cpp glsl2: Move the compiler to the subdirectory it will live in in Mesa. 2010-06-24 15:36:00 -07:00
ast_function.cpp glsl2: Fix transpose of rows and cols 2010-08-17 16:06:30 -07:00
ast_to_hir.cpp glsl: Silence unused variable warning. 2010-08-21 16:22:18 -07:00
ast_type.cpp glsl2: Give the path within src/mesa/ for headers instead of relying on -I. 2010-08-02 10:59:46 -07:00
autogen.sh glsl2: Move the compiler to the subdirectory it will live in in Mesa. 2010-06-24 15:36:00 -07:00
builtin_function.cpp glsl: Refresh autogenerated file builtin_function.cpp. 2010-08-18 13:20:58 -07:00
builtin_types.h glsl2: Use Elements from main/compiler.h instead of open-coding 2010-08-12 15:05:39 -07:00
builtin_variables.h glsl2: add gl_LightModel built-in uniform. 2010-08-04 14:41:01 -07:00
configure.ac glsl2: Move the compiler to the subdirectory it will live in in Mesa. 2010-06-24 15:36:00 -07:00
glsl_lexer.cpp glsl2: Commit generated file change by commit ab18be74 2010-08-13 19:05:54 -07:00
glsl_lexer.lpp glsl2: Avoid token name collisions with names used by Windows header files 2010-08-13 09:30:56 -07:00
glsl_parser.cpp glsl2: Remove unnecessary use of 'struct' before type names 2010-08-13 16:46:43 -07:00
glsl_parser.h glsl2: Remove unnecessary use of 'struct' before type names 2010-08-13 16:46:43 -07:00
glsl_parser.ypp glsl2: Remove unnecessary use of 'struct' before type names 2010-08-13 16:46:43 -07:00
glsl_parser_extras.cpp glsl2: Free the shader compiler at dri screen destruction. 2010-08-18 17:10:48 -07:00
glsl_parser_extras.h glsl2: Free the shader compiler at dri screen destruction. 2010-08-18 17:10:48 -07:00
glsl_symbol_table.h glsl: Standardize a few more uses of struct vs class keyword. 2010-08-14 15:35:57 +01:00
glsl_types.cpp glsl2: Use Elements from main/compiler.h instead of open-coding 2010-08-12 15:05:39 -07:00
glsl_types.h glsl: Standardize a few more uses of struct vs class keyword. 2010-08-14 15:35:57 +01:00
hir_field_selection.cpp glsl2: Remove unnecessary use of 'struct' before type names 2010-08-13 16:46:43 -07:00
ir.cpp ir_constant: Don't assert on out-of-bounds array accesses 2010-08-17 13:00:03 -07:00
ir.h glsl: Standardize a few more uses of struct vs class keyword. 2010-08-14 15:35:57 +01:00
ir_algebraic.cpp glsl: When doing algebraic simplification, make sure the type still matches. 2010-08-17 13:50:45 -07:00
ir_basic_block.cpp glsl2: Don't dead-code eliminate a call where the return value is unused. 2010-08-05 12:56:03 -07:00
ir_basic_block.h glsl2: Move the compiler to the subdirectory it will live in in Mesa. 2010-06-24 15:36:00 -07:00
ir_clone.cpp glsl: Silence gcc warning "control reaches end of non-void function". 2010-08-14 14:40:39 +01:00
ir_constant_expression.cpp ir_constant_expression: Implement equal/notEqual for booleans. 2010-08-18 12:08:19 -07:00
ir_constant_folding.cpp glsl2: Constant fold the children of many more ir_instruction types. 2010-08-04 16:25:56 -07:00
ir_constant_propagation.cpp glsl2: Convert ir_constant_propagation to ir_rvalue_visitor. 2010-08-13 17:54:47 -07:00
ir_constant_variable.cpp glsl2: Add ir_assignment::write_mask and associated methods 2010-08-04 16:47:27 -07:00
ir_copy_propagation.cpp glsl2: Teach copy propagation about "if" and "loop" instructions. 2010-08-09 17:14:31 -07:00
ir_dead_code.cpp glsl2: Fix copy'n'paste hilarity leading to leaking in the refcount visitor. 2010-08-18 14:16:07 -07:00
ir_dead_code_local.cpp glsl2: Don't dead-code eliminate a call where the return value is unused. 2010-08-05 12:56:03 -07:00
ir_dead_functions.cpp glsl2: Add a pass for removing unused functions. 2010-08-05 10:18:31 -07:00
ir_div_to_mul_rcp.cpp glsl2: Fix ir_div_to_mul_rcp for integer division. 2010-07-07 14:06:26 -07:00
ir_explog_to_explog2.cpp glsl2: Add a pass to convert exp and log to exp2 and log2. 2010-08-05 15:34:00 -07:00
ir_expression_flattening.cpp glsl2: Replace insert_before/remove pairs with exec_node::replace_with. 2010-07-21 16:37:57 -07:00
ir_expression_flattening.h glsl2: Move the compiler to the subdirectory it will live in in Mesa. 2010-06-24 15:36:00 -07:00
ir_function.cpp glsl: Silence uninitialized variable warning. 2010-08-21 20:42:47 -07:00
ir_function_can_inline.cpp glsl2: Move the compiler to the subdirectory it will live in in Mesa. 2010-06-24 15:36:00 -07:00
ir_function_inlining.cpp Revert "glsl2: Use stdint.h instead of inttypes.h" 2010-08-14 16:01:24 +01:00
ir_function_inlining.h glsl2: Move the compiler to the subdirectory it will live in in Mesa. 2010-06-24 15:36:00 -07:00
ir_hierarchical_visitor.cpp glsl: Fix uninitialized member in ir_hierarchical_vistor constructor. 2010-08-18 16:10:15 -07:00
ir_hierarchical_visitor.h glsl2: Add a pass to convert mod(a, b) to b * fract(a/b). 2010-07-01 11:07:23 -07:00
ir_hv_accept.cpp glsl2: Make the HV actually call ir_texture's visit_leave. 2010-08-05 13:34:02 -07:00
ir_if_return.cpp glsl2: Do ir_if_return on the way out, not the way in. 2010-07-29 15:56:17 -07:00
ir_if_simplification.cpp glsl2: Move the compiler to the subdirectory it will live in in Mesa. 2010-06-24 15:36:00 -07:00
ir_if_to_cond_assign.cpp glsl2: Add and use new variable mode ir_var_temporary 2010-07-20 17:48:24 -07:00
ir_import_prototypes.cpp glsl2: Make the clone() method take a talloc context. 2010-08-04 12:42:47 -07:00
ir_mat_op_to_vec.cpp glsl2: Add support for ir_unop_neg to ir_mat_op_to_vec 2010-08-13 17:47:00 -07:00
ir_mod_to_fract.cpp glsl2: Add and use new variable mode ir_var_temporary 2010-07-20 17:48:24 -07:00
ir_noop_swizzle.cpp glsl2: Add a pass to strip out noop swizzles. 2010-08-13 17:54:47 -07:00
ir_optimization.h glsl2: Add a pass to strip out noop swizzles. 2010-08-13 17:54:47 -07:00
ir_print_visitor.cpp glsl2: added casts to silence warnings 2010-08-11 15:06:12 -06:00
ir_print_visitor.h glsl2: When dumping IR for debug, indent nested blocks. 2010-07-29 14:38:04 -07:00
ir_reader.cpp glsl2: Rework builtin function generation. 2010-08-13 19:09:36 -07:00
ir_reader.h glsl2: Rework builtin function generation. 2010-08-13 19:09:36 -07:00
ir_rvalue_visitor.cpp glsl2: Add a generic visitor class to call back with pointers to each rvalue. 2010-08-13 17:54:46 -07:00
ir_rvalue_visitor.h glsl2: Add a generic visitor class to call back with pointers to each rvalue. 2010-08-13 17:54:46 -07:00
ir_set_program_inouts.cpp glsl: Fix leak-causing typo in destructor that made it another constructor. 2010-08-18 16:18:38 -07:00
ir_structure_splitting.cpp glsl2: Silence unused variable warning 2010-08-16 13:59:01 -07:00
ir_sub_to_add_neg.cpp glsl2: Add a pass to transform ir_binop_sub to add(op0, neg(op1)) 2010-08-09 21:41:14 -07:00
ir_swizzle_swizzle.cpp glsl2: Move the compiler to the subdirectory it will live in in Mesa. 2010-06-24 15:36:00 -07:00
ir_tree_grafting.cpp glsl: Standardize a few more uses of struct vs class keyword. 2010-08-14 15:35:57 +01:00
ir_validate.cpp Revert "glsl2: Use stdint.h instead of inttypes.h" 2010-08-14 16:01:24 +01:00
ir_variable.cpp glsl2: Use Elements from main/compiler.h instead of open-coding 2010-08-12 15:05:39 -07:00
ir_variable_refcount.cpp glsl2: Factor out the variable refcounting part of ir_dead_code.cpp. 2010-07-31 15:52:21 -07:00
ir_variable_refcount.h glsl2: Fix copy'n'paste hilarity leading to leaking in the refcount visitor. 2010-08-18 14:16:07 -07:00
ir_vec_index_to_cond_assign.cpp glsl2: Make the clone() method take a talloc context. 2010-08-04 12:42:47 -07:00
ir_vec_index_to_swizzle.cpp glsl2: Return progress from ir_vec_index_to_swizzle. 2010-08-04 20:52:33 -07:00
ir_visitor.h glsl2: Define new ir_discard instruction. 2010-06-30 14:54:58 -07:00
link_functions.cpp glsl2: Make the clone() method take a talloc context. 2010-08-04 12:42:47 -07:00
linker.cpp glsl: Also strdup the names of uniform list entries for >vec4 types. 2010-08-18 17:45:08 -07:00
linker.h linker: First bits of intrastage, intershader function linking 2010-07-19 14:50:43 -07:00
list.h glsl2: Fix spelling of "sentinel." 2010-07-29 14:02:19 -07:00
main.cpp mesa: Free old linked shaders when relinking new shaders. 2010-08-18 14:16:07 -07:00
Makefile glsl2/Makefile: Add a 'make builtins' target. 2010-08-13 19:09:36 -07:00
Makefile.am glsl2: Replace the GLSL compiler with the glsl2 project. 2010-06-24 17:23:21 -07:00
Makefile.template Build mesa glsl with make. 2009-12-10 01:03:15 +01:00
program.h mesa: Free old linked shaders when relinking new shaders. 2010-08-18 14:16:07 -07:00
README glsl2: Update README for what I've been thinking about with expr types work. 2010-07-01 11:07:58 -07:00
s_expression.cpp glsl: Replace sscanf in s_expression reader with strspn and strcspn. 2010-08-18 21:33:18 -07:00
s_expression.h glsl: Replace sscanf in s_expression reader with strspn and strcspn. 2010-08-18 21:33:18 -07:00
SConscript scons: Add new source files. 2010-08-14 12:45:14 +01:00
TODO glsl2: Update TODO. 2010-07-21 16:38:33 -07:00

Welcome to Mesa's GLSL compiler.  A brief overview of how things flow:

1) lex and yacc-based preprocessor takes the incoming shader string
and produces a new string containing the preprocessed shader.  This
takes care of things like #if, #ifdef, #define, and preprocessor macro
invocations.  Note that #version, #extension, and some others are
passed straight through.  See glcpp/*

2) lex and yacc-based parser takes the preprocessed string and
generates the AST (abstract syntax tree).  Almost no checking is
performed in this stage.  See glsl_lexer.lpp and glsl_parser.ypp.

3) The AST is converted to "HIR".  This is the intermediate
representation of the compiler.  Constructors are generated, function
calls are resolved to particular function signatures, and all the
semantic checking is performed.  See ast_*.cpp for the conversion, and
ir.h for the IR structures.

4) The driver (Mesa, or main.cpp for the standalone binary) performs
optimizations.  These include copy propagation, dead code elimination,
constant folding, and others.  Generally the driver will call
optimizations in a loop, as each may open up opportunities for other
optimizations to do additional work.  See most files called ir_*.cpp

5) linking is performed.  This does checking to ensure that the
outputs of the vertex shader match the inputs of the fragment shader,
and assigns locations to uniforms, attributes, and varyings.  See
linker.cpp.

6) The driver may perform additional optimization at this point, as
for example dead code elimination previously couldn't remove functions
or global variable usage when we didn't know what other code would be
linked in.

7) The driver performs code generation out of the IR, taking a linked
shader program and producing a compiled program for each stage.  See
ir_to_mesa.cpp for Mesa IR code generation.

FAQ:

Q: What is HIR versus IR versus LIR?

A: The idea behind the naming was that ast_to_hir would produce a
high-level IR ("HIR"), with things like matrix operations, structure
assignments, etc., present.  A series of lowering passes would occur
that do things like break matrix multiplication into a series of dot
products/MADs, make structure assignment be a series of assignment of
components, flatten if statements into conditional moves, and such,
producing a low level IR ("LIR").

However, it now appears that each driver will have different
requirements from a LIR.  A 915-generation chipset wants all functions
inlined, all loops unrolled, all ifs flattened, no variable array
accesses, and matrix multiplication broken down.  The Mesa IR backend
for swrast would like matrices and structure assignment broken down,
but it can support function calls and dynamic branching.  A 965 vertex
shader IR backend could potentially even handle some matrix operations
without breaking them down, but the 965 fragment shader IR backend
would want to break to have (almost) all operations down channel-wise
and perform optimization on that.  As a result, there's no single
low-level IR that will make everyone happy.  So that usage has fallen
out of favor, and each driver will perform a series of lowering passes
to take the HIR down to whatever restrictions it wants to impose
before doing codegen.

Q: How is the IR structured?

A: The best way to get started seeing it would be to run the
standalone compiler against a shader:

./glsl_compiler --dump-lir \
	~/src/piglit/tests/shaders/glsl-orangebook-ch06-bump.frag

So for example one of the ir_instructions in main() contains:

(assign (constant bool (1)) (var_ref litColor)  (expression vec3 * (var_ref Surf
aceColor) (var_ref __retval) ) )

Or more visually:
                     (assign)
                 /       |        \
        (var_ref)  (expression *)  (constant bool 1)
         /          /           \
(litColor)      (var_ref)    (var_ref)
                  /                  \
           (SurfaceColor)          (__retval)

which came from:

litColor = SurfaceColor * max(dot(normDelta, LightDir), 0.0);

(the max call is not represented in this expression tree, as it was a
function call that got inlined but not brought into this expression
tree)

Each of those nodes is a subclass of ir_instruction.  A particular
ir_instruction instance may only appear once in the whole IR tree with
the exception of ir_variables, which appear once as variable
declarations:

(declare () vec3 normDelta)

and multiple times as the targets of variable dereferences:
...
(assign (constant bool (1)) (var_ref __retval) (expression float dot
 (var_ref normDelta) (var_ref LightDir) ) )
...
(assign (constant bool (1)) (var_ref __retval) (expression vec3 -
 (var_ref LightDir) (expression vec3 * (constant float (2.000000))
 (expression vec3 * (expression float dot (var_ref normDelta) (var_ref
 LightDir) ) (var_ref normDelta) ) ) ) )
...

Each node has a type.  Expressions may involve several different types:
(declare (uniform ) mat4 gl_ModelViewMatrix)
((assign (constant bool (1)) (var_ref constructor_tmp) (expression
 vec4 * (var_ref gl_ModelViewMatrix) (var_ref gl_Vertex) ) )

An expression tree can be arbitrarily deep, and the compiler tries to
keep them structured like that so that things like algebraic
optimizations ((color * 1.0 == color) and ((mat1 * mat2) * vec == mat1
* (mat2 * vec))) or recognizing operation patterns for code generation
(vec1 * vec2 + vec3 == mad(vec1, vec2, vec3)) are easier.  This comes
at the expense of additional trickery in implementing some
optimizations like CSE where one must navigate an expression tree.

Q: Why no SSA representation?

A: Converting an IR tree to SSA form makes dead code elmimination,
common subexpression elimination, and many other optimizations much
easier.  However, in our primarily vector-based language, there's some
major questions as to how it would work.  Do we do SSA on the scalar
or vector level?  If we do it at the vector level, we're going to end
up with many different versions of the variable when encountering code
like:

(assign (constant bool (1)) (swiz x (var_ref __retval) ) (var_ref a) ) 
(assign (constant bool (1)) (swiz y (var_ref __retval) ) (var_ref b) ) 
(assign (constant bool (1)) (swiz z (var_ref __retval) ) (var_ref c) ) 

If every masked update of a component relies on the previous value of
the variable, then we're probably going to be quite limited in our
dead code elimination wins, and recognizing common expressions may
just not happen.  On the other hand, if we operate channel-wise, then
we'll be prone to optimizing the operation on one of the channels at
the expense of making its instruction flow different from the other
channels, and a vector-based GPU would end up with worse code than if
we didn't optimize operations on that channel!

Once again, it appears that our optimization requirements are driven
significantly by the target architecture.  For now, targeting the Mesa
IR backend, SSA does not appear to be that important to producing
excellent code, but we do expect to do some SSA-based optimizations
for the 965 fragment shader backend when that is developed.

Q: How should I expand instructions that take multiple backend instructions?

Sometimes you'll have to do the expansion in your code generation --
see, for example, ir_to_mesa.cpp's handling of ir_binop_mul for
matrices.  However, in many cases you'll want to do a pass over the IR
to convert non-native instructions to a series of native instructions.
For example, for the Mesa backend we have ir_div_to_mul_rcp.cpp because
Mesa IR (and many hardware backends) only have a reciprocal
instruction, not a divide.  Implementing non-native instructions this
way gives the chance for constant folding to occur, so (a / 2.0)
becomes (a * 0.5) after codegen instead of (a * (1.0 / 2.0))

Q: How shoud I handle my special hardware instructions with respect to IR?

Our current theory is that if multiple targets have an instruction for
some operation, then we should probably be able to represent that in
the IR.  Generally this is in the form of an ir_{bin,un}op expression
type.  For example, we initially implemented fract() using (a -
floor(a)), but both 945 and 965 have instructions to give that result,
and it would also simplify the implementation of mod(), so
ir_unop_fract was added.  The following areas need updating to add a
new expression type:

ir.h (new enum)
ir.cpp:get_num_operands() (used for ir_reader)
ir.cpp:operator_strs (used for ir_reader)
ir_constant_expression.cpp (you probably want to be able to constant fold)

You may also need to update the backends if they will see the new expr type:

../mesa/shaders/ir_to_mesa.cpp

You can then use the new expression from builtins (if all backends
would rather see it), or scan the IR and convert to use your new
expression type (see ir_mod_to_fract, for example).