金属着色器 stage_in
Metal shader stage_in
我从这里下载了一个简单的金属着色器:https://medium.com/@MalikAlayli/metal-with-scenekit-create-your-first-shader-2c4e4e983300。我通过将 clip_distance
添加到 VertexOut
对其进行了修改。如果 clip_distance
是浮点数,一切正常。由于我需要 6 个裁剪平面,因此我尝试将其定义为向量 [[clip_distance]][6]
。然而,Xcode 抱怨 textureSamplerFragment
例程,说“类型 'VertexOut' 对属性 'stage_in' 无效”。我是 Metal 着色器的新手,不知道如何修复错误。
谁能给我指路?提前谢谢你。
#include <metal_stdlib>
using namespace metal;
#include <SceneKit/scn_metal>
struct NodeBuffer {
float4x4 modelTransform;
float4x4 modelViewProjectionTransform;
float4x4 modelViewTransform;
float4x4 normalTransform;
float2x3 boundingBox;
};
struct VertexInput {
float3 position [[attribute(SCNVertexSemanticPosition)]];
float2 uv [[attribute(SCNVertexSemanticTexcoord0)]];
};
struct VertexOut {
float4 position [[position]];
float clip_distance [[clip_distance]][6];
float2 uv;
};
vertex VertexOut textureSamplerVertex(VertexInput in [[ stage_in ]], constant NodeBuffer& scn_node [[buffer(1)]]) {
VertexOut out;
out.position = scn_node.modelViewProjectionTransform * float4(in.position, 1.0);
out.clip_distance[0] = 1.5 - in.position.x;
out.uv = in.uv;
return out;
}
fragment float4 textureSamplerFragment(VertexOut out [[ stage_in ]], texture2d<float, access::sample> customTexture [[texture(0)]]) {
constexpr sampler textureSampler(coord::normalized, filter::linear, address::repeat);
return customTexture.sample(textureSampler, out.uv );
}
基于此Metal Shading Language Specification:
clip_distance 属性数据类型应为 float 或 float[n], n - 必须在编译时知道。您应该将着色器逻辑更改为相应的要求。
我从这里下载了一个简单的金属着色器:https://medium.com/@MalikAlayli/metal-with-scenekit-create-your-first-shader-2c4e4e983300。我通过将 clip_distance
添加到 VertexOut
对其进行了修改。如果 clip_distance
是浮点数,一切正常。由于我需要 6 个裁剪平面,因此我尝试将其定义为向量 [[clip_distance]][6]
。然而,Xcode 抱怨 textureSamplerFragment
例程,说“类型 'VertexOut' 对属性 'stage_in' 无效”。我是 Metal 着色器的新手,不知道如何修复错误。
谁能给我指路?提前谢谢你。
#include <metal_stdlib>
using namespace metal;
#include <SceneKit/scn_metal>
struct NodeBuffer {
float4x4 modelTransform;
float4x4 modelViewProjectionTransform;
float4x4 modelViewTransform;
float4x4 normalTransform;
float2x3 boundingBox;
};
struct VertexInput {
float3 position [[attribute(SCNVertexSemanticPosition)]];
float2 uv [[attribute(SCNVertexSemanticTexcoord0)]];
};
struct VertexOut {
float4 position [[position]];
float clip_distance [[clip_distance]][6];
float2 uv;
};
vertex VertexOut textureSamplerVertex(VertexInput in [[ stage_in ]], constant NodeBuffer& scn_node [[buffer(1)]]) {
VertexOut out;
out.position = scn_node.modelViewProjectionTransform * float4(in.position, 1.0);
out.clip_distance[0] = 1.5 - in.position.x;
out.uv = in.uv;
return out;
}
fragment float4 textureSamplerFragment(VertexOut out [[ stage_in ]], texture2d<float, access::sample> customTexture [[texture(0)]]) {
constexpr sampler textureSampler(coord::normalized, filter::linear, address::repeat);
return customTexture.sample(textureSampler, out.uv );
}
基于此Metal Shading Language Specification:
clip_distance 属性数据类型应为 float 或 float[n], n - 必须在编译时知道。您应该将着色器逻辑更改为相应的要求。