简体   繁体   English

在金属着色器代码中,如何定义函数的输入/输出参数变量?

[英]Inside a metal shader code, how to define an in/out parameter variable of a function?

I have this function:我有这个功能:

void myFunct(const int A, ?out? int B){B = B + A;} 

How to declare B in such a way that the function can update its value and return it to the caller?如何以函数可以更新其值并将其返回给调用者的方式声明 B?

Metal is a C++ based programming language that allows you to pass a pointer to a function. Metal 是一种基于 C++ 的编程语言,它允许您将指针传递给函数。 To do so, simply declare the function parameter as a pointer type.为此,只需将函数参数声明为指针类型。

void myFunct(const int A, device int *B)
{
   *B = *B + A;
} 

device int *C = 0;
myFunct(1, C);

You can also pass variable by reference:您还可以通过引用传递变量:

void myFunct(const int A, device int &B)
{
   B = B + A;
} 

device int *C = 0;
myFunct(1, *C)

or pointer by reference:或通过引用指针:

void myFunct(const int A, device int *&B)
{
   *B = *B + A;
} 

device int *C = 0;
myFunct(1, C);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM