[Mesa-dev] [PATCH 0/6] glsl: Add support for varying structs.

Paul Berry stereotype441 at gmail.com
Mon Jan 21 14:16:20 PST 2013

This patch series adds support for varying structs, which are a
required part of GLSL ES 3.00 and GLSL 1.50.

I can see two principal ways to implement this feature: a "flattening"
approach, and a "packing" approach.  In the "flattening" approach, the
linker replaces each varying struct with a set of varyings, one for
each field in the struct, and adjusts the shader code to refer to the
new set of varyings (this is what a programmer would likely try to do
when manually porting a shader that uses structs to a version of GLSL
that didn't support them).  In the "packing" approach, we keep the
varying structs as single variables, and lay out their contents
contiguously in GPU memory (in much the same way that a CPU compiler
would handle an ordinary C struct).

In principle, the "flattening" approach carries some potential
performance benefits, since it frees the linker to assign each struct
element a location that is properly aligned to its type.  However, it
is much more difficult to implement, since it requires a lowering pass
to traverse the entire shader and adjust all references to the varying
structs, including function in/out parameters and whole structure
assignment.  To make matters worse, there would be subtle interactions
between the linker and the lowering pass, since error checking would
have to be performed based on the un-lowered shaders, but the location
assignment would have to be applied to the lowered varyings.

Because of these difficulties, I've used the "packing" approach in
this initial implementation--this allowed me to take advantage of the
existing lower_packed_varyings() function to pack and unpack varying
structs, so the code delta is fairly small.  We can always switch to
the "flattening" approach if, in the future, we discover some programs
whose performance would be dramatically improved by it.

Patch 1/6 eliminates the ambiguity in the ir_variable_mode enum
between "in/out" variables that are inputs/outputs of functions and
inputs/outputs of shaders.  This makes possible patch 2/6, which
modifies opt_structure_splitting to leave varying structs alone (we
can't have it splitting varying structs, otherwise the linker won't be
able to find them).  Patches 3/6 and 4/6 update the linker and
lower_packed_varyings, respectively, to handle varying structs, and
patch 5/6 disables transform feedback for varying structs (we haven't
yet heard from Khronos how this is supposed to work).  Finally, patch
6/6 enables the feature when the GLSL version is 1.50 or 3.00 ES.

[PATCH 1/6] glsl: Eliminate ambiguity between function ins/outs and shader ins/outs
[PATCH 2/6] glsl: Disable structure splitting for shader ins/outs.
[PATCH 3/6] glsl: Generalize compute_packing_order for varying structs.
[PATCH 4/6] glsl: Update lower_packed_varyings to handle varying structs.
[PATCH 5/6] glsl: Disable transform feedback of varying structs.
[PATCH 6/6] glsl: Allow varying structs in GLSL ES 3.00 and GLSL 1.50.

More information about the mesa-dev mailing list