What is the correct approach to creating UberShader using function_constants in Metal?

I just found out about function_constants in the WWDC 2016 What's New in Metal video and he mentioned UberShaders quite a few times. I want to create a uber shader fragment that can be used for different types of passes, such as simplePassThrough, defferred, etc. Below I want to use it.

constant int passType [[function_constant(0)]];
constant bool simplePassThrough = (passType == 0);
constant bool forwardShading = (passType == 1);
constant bool deferredShading = (passType == 2);

fragment FragmentOutStruct UberFragmentShader()
{
FragmentOutputStruct frgOut;
if (simplePassThrough) {
    // Update frgOut
} else if (forwardShading) {
    // Update frgOut
} else if (deferredShading) {
    // Update frgOut
}
return frgOut;
}

Is this the right approach? Will my last compiled MTLFunction see too many branches if I use this approach?

+4
source share
2 answers

. , , , (, if(false) { ... }).

+6

, . ( @warrenm. ...)

, , , Apple WWDC16, : "" function-constant value, , ( ) IR- , .

int, , 2 32 shader - , (0, 1, 2 -else, if frgOut).

"" , , , , / . , 1, , :

fragment FragmentOutStruct UberFragmentShader() {
    FragmentOutputStruct frgOut;
    // Update frgOut per `if (forwardShading)` chunk of original shader source
    return frgOut;
}

, .

+6

Source: https://habr.com/ru/post/1657657/


All Articles