opengl-esscenekitmetalglsles

Passing values between SCNShadable entry points


In an OpenGL program you typical declare something like this in a vertex shader

varying bool aBooleanVariable;

and then read the value in the fragment shader. How do you do this within the framework of an SCNShadable entry point? For example from SCNShaderModifierEntryPointGeometry to SCNShaderModifierEntryPointFragment.

Receiving the argument seems to be defined by using the pragma arguments, I provide my test SCNShaderModifierEntryPointFragment to illustrate.

#pragma arguments
bool clipFragment

#pragma body

if (clipFragment) {
    discard_fragment();
}

However, the arguments pragma doesn't work for outputting the value in the SCNShaderModifierEntryPointGeometry entry point.

I found an article that suggests that it can be done with GLSL syntax, but I was trying to find the Metal way and I wasn't even able to reproduce the result.


Solution

  • Forced to use GLSL

    Turns out you can get it to work if you switch the SCNView Rendering API to OpenGL ES. Then it will accept GLSL code including the varying qualifier.

    Geometry entry point

    varying float clipFragment;
    
    #pragma body
    clipFragment = 1.0;
    

    Fragment entry point

    varying float clipFragment;
    
    #pragma body
    if (clipFragment > 0.0) {
        discard;
    }
    

    Update from Apple

    I opened a ticket with Apple and got the following response -

    That’s correct. The SceneKit team has opened up a formal enhancement request (bug #28206462) to investigate adding this capability to Metal. Until then, you’ll have to opt into using the OpenGL renderer.