iOS Metal shader中的if条件该怎么写?

各位大神你们好,我想问问在OpenGLES或者Metal中,是不是if语句中的判断条件不能写得太长太复杂?
就比如说iOS平台中的Metal框架吧,我在它的shader中写了一个if语句,看上去有点长,不过在CPU端处理的时候根本不叫事。
语句是下面这样的

if ((vector0.x * vector1.y == vector0.y * vector1.x) && (((point.x <= segment.x && point.x >= segment.z) || (point.x <= segment.z && point.x >= segment.x)) && ((point.y <= segment.y && point.y >= segment.w) || (point.y <= segment.w && point.y >= segment.y))))

我发现程序停留在

[MetalGPU().gpuDevice newComputePipelineStateWithFunction:kernelFunction error:&error];

也不报错,也不崩溃,也没有error,就是没有响应了。
当时我的想法是可能是这个条件判断写得太长太复杂了,于是我想分布计算bool值于是写成了下面这个样子。

bool isPointOnSegment(float2 point,float4 segment) {
    float2 vector0 = segment.xy - point;
    float2 vector1 = segment.zw - segment.xy;
    bool condition = vector0.x * vector1.y == vector0.y * vector1.x;
    condition = condition && ((point.x <= segment.x && point.x >= segment.z) || (point.x <= segment.z && point.x >= segment.x));
    condition = condition && ((point.y <= segment.y && point.y >= segment.w) || (point.y <= segment.w && point.y >= segment.y));
    if (condition) {
        return true;
    }
    return false;
}

我发现依旧是不响应。于是我在想是不是condition里面不是bool值呢?
于是我直接塞了个bool值到里面,就变成了下面这个样子。

bool isPointOnSegment(float2 point,float4 segment) {
    float2 vector0 = segment.xy - point;
    float2 vector1 = segment.zw - segment.xy;
    bool condition = vector0.x * vector1.y == vector0.y * vector1.x;
    condition = true;
    condition = condition && ((point.x <= segment.x && point.x >= segment.z) || (point.x <= segment.z && point.x >= segment.x));
    condition = false;
    condition = condition && ((point.y <= segment.y && point.y >= segment.w) || (point.y <= segment.w && point.y >= segment.y));
    if (condition) {
        return true;
    }
    return false;
}

然后就好使了,我就不明白了这前后有啥区别。
那么我的问题就是
Metal的if条件在写法上有啥讲究?
到底是什么原因导致上述现象?

阅读 1.4k
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题