如何将非纹理数据传递给 SCNTechnique 金属着色器
How to pass non-texture data to SCNTechnique Metal shaders
我可以将 sampler2D
类型的自定义参数传递给 SCNTechnique
的 Metal 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)]]
...传递的值永远不会到达着色器函数。
我试过了
- 使用最多 6 个不同的缓冲区 (N) 值
- 在顶点函数中有自定义参数
- 键入 vec3 和 float3 而不是键入 float
- 将我的浮点数编码为 NSData 的不同方法
将我的浮动包装在结构中
[technique setValue:[NSValue valueWithSCNVector3: SCNVector3Make(0.5, 0.5, 0.5)] forKey:@"blob_pos_"];
SCNVector3 xx = SCNVector3Make(0.5, 0.5, 0.5);
[technique setValue:[NSData dataWithBytes:&xx length:sizeof(xx)] forKey:@"blob_pos_"];
[technique setValue:[NSData dataWithBytesNoCopy:&xx length:sizeof(xx)] forKey:@"blob_pos_"];
simd_float3 x = simd_make_float3(0.5, 0.5, 0.5);
[technique setValue:[NSData dataWithBytes:&x length:sizeof(x)] forKey:@"blob_pos_"];
float y = 0.5;
[technique setValue:[NSData dataWithBytes:&y length:sizeof(y)] forKey:@"blob_pos_"];
struct MyStruct {
float x;
};
struct MyStruct myStruct = {
0.5
};
[technique setValue:[NSValue valueWithBytes:&myStruct objCType:@encode(struct MyStruct)] forKey:@"blob_pos_"];
[technique setObject:[NSValue valueWithBytes:&myStruct objCType:@encode(struct MyStruct)] forKeyedSubscript:@"blob_pos_"];
...一切都失败了。
然后我看了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;
...
我可以将 sampler2D
类型的自定义参数传递给 SCNTechnique
的 Metal 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)]]
...传递的值永远不会到达着色器函数。
我试过了
- 使用最多 6 个不同的缓冲区 (N) 值
- 在顶点函数中有自定义参数
- 键入 vec3 和 float3 而不是键入 float
- 将我的浮点数编码为 NSData 的不同方法
将我的浮动包装在结构中
[technique setValue:[NSValue valueWithSCNVector3: SCNVector3Make(0.5, 0.5, 0.5)] forKey:@"blob_pos_"]; SCNVector3 xx = SCNVector3Make(0.5, 0.5, 0.5); [technique setValue:[NSData dataWithBytes:&xx length:sizeof(xx)] forKey:@"blob_pos_"]; [technique setValue:[NSData dataWithBytesNoCopy:&xx length:sizeof(xx)] forKey:@"blob_pos_"]; simd_float3 x = simd_make_float3(0.5, 0.5, 0.5); [technique setValue:[NSData dataWithBytes:&x length:sizeof(x)] forKey:@"blob_pos_"]; float y = 0.5; [technique setValue:[NSData dataWithBytes:&y length:sizeof(y)] forKey:@"blob_pos_"]; struct MyStruct { float x; }; struct MyStruct myStruct = { 0.5 }; [technique setValue:[NSValue valueWithBytes:&myStruct objCType:@encode(struct MyStruct)] forKey:@"blob_pos_"]; [technique setObject:[NSValue valueWithBytes:&myStruct objCType:@encode(struct MyStruct)] forKeyedSubscript:@"blob_pos_"];
...一切都失败了。
然后我看了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;
...