【问题标题】:SCNTechnique pipeline crashing almost immediately when I use DRAW_SCENE当我使用 DRAW_SCENE 时,SCNTechnique 管道几乎立即崩溃
【发布时间】:2019-12-16 04:02:49
【问题描述】:

我是 Metal 新手(以及一般的着色器)。我一直在尝试使用MSL specSCNTechnique docs 创建一个ARSCNView 应用程序来应用着色器。

我想要一个着色器管道,它可以让我仅更改相机馈送视图 (sceneView.scene.background),但允许所有 SCNNode 与它们重叠的未更改相机馈送混合。这是我的计划:

我的计划

1) 第一次将我的第一个风格化着色器应用到场景(它只是调整颜色/亮度,我已经用DRAW_QUAD 对其进行了测试,它工作正常)到DRAW_SCENE 并使用excludeCategoryMask 排除所有节点。

着色器应该包含某种统一的数据结构,其中包含我场景中的所有其他节点(当前为空,因为节点尚未渲染)并使用它们的屏幕空间坐标来切割颜色中适当大小/定位的形状过滤节点重叠的位置。

2) 第二遍使用标准/默认 SCNKit 着色器使用 DRAW_NODE 渲染我的其余节点(球体等)。

问题

我不知道这是否是一种有效的方法,因为我的应用程序几乎立即与com.apple.scenekit.scnview-renderer (15): EXC_BAD_ACCESS (code=1, address=0xeb52be860) 一起崩溃。当我从DRAW_QUAD 切换到DRAW_SCENE 时,着色器崩溃。我想我缺少深度缓冲区或其他东西。

#include <metal_stdlib>
using namespace metal;
#include <SceneKit/scn_metal>

struct VertexInput_0 {
    float4 position [[ attribute(SCNVertexSemanticPosition) ]];
    float2 texcoord [[ attribute(SCNVertexSemanticTexcoord0) ]];
};

struct VertexOutput_0 {
    float4 position [[position]];
    float2 texcoord;
};

// metalVertexShader
vertex VertexOutput_0 scene_filter_vertex_0(VertexInput_0 in [[stage_in]])
{
    VertexOutput_0 out;
    out.position = in.position;
    out.texcoord = float2((in.position.x + 1.0) * 0.5 , (in.position.y + 1.0) * -0.5);
    return out;
}

// metalFragmentShader
fragment half4 scene_filter_fragment_0(VertexOutput_0 vert [[stage_in]],
                                     texture2d<half, access::sample> scene [[texture(0)]])
{
    constexpr sampler samp = sampler(coord::normalized, address::repeat, filter::nearest);
    half4 color = scene.sample(samp, vert.texcoord);

    constexpr half3 weights = half3(2, 0.7152, 0.0722);
    color.rgb = half3(dot(color.rgb, weights));


    return color;
}

// Doesn't reach here, crashes before second pass below

struct VertexInput_1 {
    float4 position [[ attribute(SCNVertexSemanticPosition) ]];
    float2 texcoord [[ attribute(SCNVertexSemanticTexcoord0) ]];
};

struct VertexOutput_1 {
    float4 position [[position]];
    float2 texcoord;
};

// metalVertexShader
vertex VertexOutput_1 scene_filter_vertex_1(VertexInput_1 in [[stage_in]])
{
    VertexOutput_1 out;
    out.position = in.position;
    out.texcoord = float2((in.position.x + 1.0) * 0.5 , (in.position.y + 1.0) * -0.5);
    return out;
}

// metalFragmentShader
fragment half4 scene_filter_fragment_1(VertexOutput_1 vert [[stage_in]],
                                     texture2d<half, access::sample> scene [[texture(0)]])
{
    constexpr sampler samp = sampler(coord::normalized, address::repeat, filter::nearest);
    half4 color = scene.sample(samp, vert.texcoord);

    return color;
}

shaders.metal

<plist version="1.0">
<dict>
    <key>sequence</key>
    <array>
        <string>filter_0</string>
        <string>filter_1</string>
    </array>
    <key>passes</key>
    <dict>
        <key>filter_1</key>
        <dict>
            <key>outputs</key>
            <dict>
                <key>color</key>
                <string>COLOR</string>
            </dict>
            <key>inputs</key>
            <dict>
                <key>scene</key>
                <string>COLOR</string>
            </dict>
            <key>draw</key>
            <string>DRAW_QUAD</string>
            <key>metalVertexShader</key>
            <string>scene_filter_vertex_2</string>
            <key>metalFragmentShader</key>
            <string>scene_filter_fragment_2</string>
        </dict>
        <key>filter_0</key>
        <dict>
            <key>metalFragmentShader</key>
            <string>scene_filter_fragment_0</string>
            <key>metalVertexShader</key>
            <string>scene_filter_vertex_0</string>
            <key>excludeCategoryMask</key>
            <integer>2</integer>
            <key>outputs</key>
            <dict>
                <key>color</key>
                <string>COLOR</string>
            </dict>
            <key>inputs</key>
            <dict>
                <key>scene</key>
                <string>COLOR</string>
            </dict>
            <key>draw</key>
            <string>DRAW_SCENE</string>
        </dict>
    </dict>
</dict>
</plist>

为什么会崩溃?我在某处缺少参数吗?

【问题讨论】:

  • 你让它工作了吗?我正在尝试类似的东西

标签: ios shader scenekit arkit metal


【解决方案1】:

可能是scene_filter_vertex_2scene_filter_fragment_2需要重命名scene_filter_vertex_1scene_filter_fragment_1

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多