Houdini 学习文档 -VEX- Shader Calls

Shader Calls

Beginning with Houdini 12.5, VEX shader functions can call other shader functions. This technique makes it possible to optimize VEX compiler and optimizer performance for large shaders, since code that is invoked multiple times within a shader or in other shaders can be built once and used many times without additional runtime overhead.

从Houdini 12.5开始,VEX着色器函数可以调用其他着色器函数。这种技术使优化大型着色器的VEX编译器和优化器性能成为可能,因为在着色器或其他着色器中多次调用的代码可以构建一次并多次使用,而不需要额外的运行时开销。

The import keyword

The import keyword introduces another shader function by name into the current shader. The imported shader must be accessible in the houdini path for compilation to succeed - if it isn’t found, shader compilation will fail. So when constructing shaders that call other shaders, you’ll need to build the shaders in dependency order - called shaders, then their callers. Circular calls are possible but you’ll need to add the import keyword to the callee after building the first caller.

mport关键字按名称将另一个着色器函数引入到当前着色器中。导入的着色器必须在houdini路径中可访问,以便编译成功——如果没有找到,着色器编译将失败。因此,当构建调用其他着色器的着色器时,您需要按照依赖关系顺序构建着色器——称为着色器,然后是它们的调用者。循环调用是可能的,但是您需要在构建第一个调用程序之后将import关键字添加到被调用程序中。

For example, importing the plastic shader:

例如,导入 plastic shader:

import plastic;

Shaders can call themselves recursively - in this case, the import keyword isn’t required.

着色器可以递归地调用它们自己——在本例中,import关键字不是必需的。

Invoking a shader

Shaders are invoked by name and are passed keyword arguments - string/value pairs that identify the arguments to be passed or received from the invoked shader. It’s possible to only bind some parameters, in which case the called shader will use it’s default values for the parameters that are not bound. Additionally, only a subset of the exports from the called shader need to be bound. In this case, the VEX optimizer will strip out any dead code that computes the exports that are not required, leading to improved performance.

着色器按名称调用,并传递关键字参数—字符串/值对,它们标识要从调用的着色器传递或接收的参数。可以只绑定一些参数,在这种情况下,被调用的着色器将使用未绑定参数的默认值。此外,只需要绑定来自被调用着色器的导出的子集。在这种情况下,VEX优化器将删除计算不需要的导出的任何死代码,从而提高性能。

For example, this code invokes the plastic shader asking for the Cf export and providing the diff input:

例如,这段代码调用plastic shader ,请求Cf导出并提供diff输入:

import plastic;

surface caller(vector diff = {1,0.5,0})
{
    plastic("diff", diff, "Cf", Cf);
}

vcc will check all variadic arguments passed to a called shader to ensure that they correspond with an argument or export that exists in the called shader’s parameter list - if the type or access mode doesn’t match, an error will be reported.

vcc将检查传递给被调用着色器的所有可变参数,以确保它们与存在于被调用着色器的参数列表中的参数或导出相对应——如果类型或访问模式不匹配,将报告错误。

Context of the called shader 

Shaders can currently only call shaders that have a matching context type. For contexts with global variables, any global variables that are not provided explicitly to the shader as keyword arguments are copied unchanged from the calling shader to the called shader. For contexts that carry additional opaque state information (such as the surface context, which maintains state about the hit surface), this information is also maintained in the called shader so that calling methods like getraylevel() will produce the same result in the caller and callee.

着色器目前只能调用具有匹配上下文类型的着色器。对于具有全局变量的上下文,没有作为关键字参数显式提供给着色器的任何全局变量都将不加更改地从被调用着色器复制到调用着色器。对于带有附加不透明状态信息的上下文(例如 surface context,它维护关于hit surface的状态),也在被调用的着色器中维护该信息,以便像getraylevel()这样的调用方法将在调用者和被调用者中产生相同的结果。

Examples

The called shader:

cvex callee(export int mval = 0;
        int rval = 0;
        export int wval = 0;
        float castval = 0)
{
    mval *= 2;
    wval = rval;
}

The calling shader:

import callee;

cvex caller()
{
    int mval = 1;
    int rval = 2;
    int wval = 1;
    callee("mval", mval, "rval", rval, "wval", wval, "castval",
            1);
    printf("%d %d %d\n", mval, rval, wval);
}

A recursive shader:

cvex fib(int i = 0; export int rval = 0)
{
    if (i >= 2)
    {
        int v1, v2;
        fib("i", i-1, "rval", v1);
        fib("i", i-2, "rval", v2);
        rval = v1 + v2;
    }
    else
        rval = i;
    printf("%d: %d\n", i, rval);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值