Check if callable program is bound?

I’m adding a shader graph system to my renderer. I would like parameters on shading nodes to either have a constant value, or a connection to another node to evaluate to get the value. For instance:

rtDeclareVariable(float3, color_value, , );
rtDeclareVariable(rtCallableProgramId<float3()>, eval_color, , );
rtDeclareVariable(bool, color_is_bound, , );
RT_CALLABLE_PROGRAM float3 shader_node() {
    float3 result = color_value;
    if (color_is_bound) result = eval_color();
    return result;
}

Is there a way of checking if the program is bound without having a manually set flag in there as well? Another alternative would be to bind a different program instead that just supplies the constant value, but this seems like it might be less efficient (or is it)?

What’s the recommended way of doing this?

You can set “eval_color” to RT_PROGRAM_ID_NULL on the host side as a sentinel:

context[ "eval_color" ]->setInt( RT_PROGRAM_ID_NULL );

This is guaranteed to never be a valid program id and is described briefly in optix_host.h.

Thanks! and on the device side would I just:

if (eval_color == RT_PROGRAM_ID_NULL) {
    result = color_value;
} else {
    result = eval_color();
}

?

And as a follow-up question, would the branch here be more or less efficient than just switching out the program bound to eval_color for another one that returns a default value?

Cheers,

Anders

Yes, that should work. Let me know if it doesn’t.

And the branch is probably more efficient than the function call, but it would need to be measured if you’re really doing a lot of this.

Thanks!