[Mesa-dev] ARB_shader_subroutine notes

Ian Romanick idr at freedesktop.org
Tue Feb 10 16:00:32 PST 2015


I just wanted to get some ideas for implementing ARB_shader_subroutine
out of my brain and into the public... just in case anyone wants to work
on it.  It's not a newbie project by any stretch of the imagination.

My basic idea for a quick-and-dirty implementation had 5 steps.  All of
this happens after linking.

1. Make duplicate copies of subroutine functions that have multiple
subroutine types.  A function like

    subroutine(a, b, c) void foo(void) { ... }

would become

    subroutine(a) void foo$a(void) { ... }

    subroutine(b) void foo$b(void) { ... }

    subroutine(c) void foo$c(void) { ... }

Since recursion is not allowed in GLSL, this step may not actually be
necessary (see step #2 below), but I'm having a little trouble
convincing myself.  The reason doing this split should be apparent in
the next step...

2. Convert the parameters to each instance of a particular subroutine
into global variables.  A set of functions like

    subroutine(a) void foo(int x) { ... }

    subroutine(a) void bar(int y) { ... }

would become

    int x$a;

    subroutine(a) void foo(void) { ... }

    subroutine(a) void bar(void) { ... }

In this set the function bodies and all call sites would be updated to
use the new globals.  Remember that there is no recursion allowed in
GLSL, at all, so this "just works."

3. Assign each subroutine function an integer index.

4. Each subroutine uniform is an unsigned integer.

5. Replace each subroutine call site with the "obvious" switch statement
/ if-ladder, and let function inlining do the rest of the work.

A significant improvement would be to add some sort of switch-statement
IR that could generate a jump table for uniform flow control.  We can
cross that bridge as soon as we see an application that actually uses
subroutines.

There are a couple other trick bits that need strong testing, at the
very least:

1. Subroutine functions can also be called directly (i.e., just like
other functions).

2. "Subroutine variables may be declared as explicitly-sized arrays,
which can be indexed only with dynamically uniform expressions."

3. "Unlike other uniform variables, subroutine uniform variables are
scoped to the shader execution stage the variable is declared in."
There are new commands for setting subroutine uniforms, and these
explicitly operate per-stage.


More information about the mesa-dev mailing list