如何将非纹理数据传递给 SCNTechnique 金属着色器

How to pass non-texture data to SCNTechnique Metal shaders

我可以将 sampler2D 类型的自定义参数传递给 SCNTechniqueMetal fragment function,并且我有一个有效的第二遍:

P列表:

<key>inputs</key>
<dict>
    <key>imageFromPass1</key>
    <string>COLOR</string>
    <key>myCustomImage</key>
    <string>myCustomImage_sym</string>
</dict>

...

<key>symbols</key>
<dict>
    <key>myCustomImage_sym</key>
    <dict>
        <key>type</key>
        <string>sampler2D</string>
    </dict>
</dict>

相关的 Obj-C 代码:

[technique setValue: UIImagePNGRepresentation(myCustomTexture) forKey:@"myCustomImage_sym"];

金属功能参数:

fragment half4 myFS(out_vertex_t vert [[stage_in]],
    texture2d<float, access::sample> imageFromPass1 [[texture(0)]],
    texture2d<float, access::sample> myCustomImage [[texture(1)]],
    constant SCNSceneBuffer& scn_frame [[buffer(0)]]) { ...

我在着色器函数中访问和使用所有这些输入。有效!

到目前为止一切顺利!

但是,当我添加另一个 float 类型的自定义参数时 ...

<key>blob_pos</key>
<string>blob_pos_sym</string>

...

<key>blob_pos_sym</key>
<dict>
    <key>type</key>
    <string>float</string>
</dict>

[_sceneView.technique setValue:[NSNumber numberWithFloat:0.5f] forKey:@"blob_pos_sym"];

 constant float& blob_pos [[buffer(2)]]

...传递的值永远不会到达着色器函数。

我试过了

...一切都失败了。

然后我看了handleBindingOfSymbol:usingBlock: ...但它只是GLSL。

我发现它是金属对应物,handleBindingOfBufferNamed:frequency:usingBlock: ... SCNTechnique 中没有。

我用 Google 搜索 SCNTechnique Metal ... 并发现 所有 项目仅使用 sampler2D 参数。

最后我了解到这不是新的而是 bugs developers for years

在我开始将这个浮点数编码到纹理中之前,让我知道缺失的位以使其按预期方式工作。

您必须使用结构来包装您的输入符号,并确保使用 [SCNTechnique setObject:forKeyedSubscript:] 将您的符号值传递给您的技术。 setObject:forKeyedSubscript: 的文档提到了 Metal,但是它没有解释如何在 Metal 函数中接收值,这很不幸。

使用你的例子:

技术定义:

"inputs": [
    "imageFromPass1": "COLOR",
    "myCustomImage": "myCustomImage_sym",
    "blob_pos": "blob_pos_sym",
],
...

"symbols": [
    "myCustomImage_sym": ["type": "sampler2D"],
    "blob_pos_sym": ["type": "float"]
 ]

Obj-C:

[_sceneView.technique setObject:[NSNumber numberWithFloat:0.5f] forKeyedSubscript:@"blob_pos_sym"];

金属:

typedef struct {
    float blob_pos; // Must be spelled exactly the same as in inputs dictionary.
} Inputs;

fragment half4 myFS(out_vertex_t vert [[stage_in]],
    texture2d<float, access::sample> imageFromPass1 [[texture(0)]],
    texture2d<float, access::sample> myCustomImage [[texture(1)]],
    constant Inputs& myInputs [[buffer(0)]]) { 

    float blob_pos = myInputs.blob_pos;

    ...