ios - 在 Metal Shader Vertex 函数中出错

标签 ios metal vertex-shader

代码库有点旧,正在努力支持最新的 iOS 版本。它过去工作正常,但现在出现以下错误。

vertex function has invalid return type 'ColorInOut'

下面是 Metal 着色器代码的一部分。您可以获得完整代码 here .

typedef struct
{
    packed_float3 position;
    packed_float3 normal;
    packed_float2 texCoord1;
    packed_float3 tangent;
    packed_float3 bitagent;
    packed_float4 vertColor;
} vertex_t;

typedef struct
{
    float4 position [[position]] , vertexPosition_ws;
    float2 uv,texture2UV;
    float hasLighting;
    float pointSize [[point_size]];
    float vertexDepth, reflection;
    float hasMeshColor;
    float shadowDarkness, transparency;
    float2 pointCoord [[ point_coord ]];
    float4 eyeVec, lightDir, lightColor;
    float3 meshColor;
    float3 T;
    float3 B;
    float3 N;
} ColorInOut;

vertex ColorInOut Mesh_Vertex(device vertex_t* vertex_array [[ buffer(0) ]],
                                constant float4x4& vp [[ buffer(SHADER_MESH_mvp) ]],
                                constant MatArray* model [[ buffer(SHADER_MESH_model) ]],
                                constant float4x4& lvp [[ buffer(SHADER_MESH_lightViewProjMatrix) ]],
                                constant packed_float3& eyePos [[ buffer(SHADER_MESH_eyePos) ]],
                                constant float& shadowDarkness [[ buffer(SHADER_MESH_shadowDarkness) ]],
                                constant float *hasMeshColor [[ buffer(SHADER_MESH_hasMeshColor) ]],
                                constant float *reflectionValue [[ buffer(SHADER_MESH_reflectionValue) ]],
                                constant float *transparency [[ buffer(SHADER_MESH_transparency) ]],
                                constant float *hasLighting [[ buffer(SHADER_MESH_hasLighting) ]],
                                constant float *uvScaleValue [[ buffer(SHADER_MESH_uvScale) ]],
                                constant float3Struct *meshColor [[buffer(SHADER_MESH_meshColor)]],
                                unsigned int vid [[ vertex_id ]],
                                unsigned int iId [[ instance_id ]]
                                )
{

    float4 vertexPosition_ws = model[iId].data * float4(float3(vertex_array[vid].position), 1.0);
    float4 vertColor = float4(vertex_array[vid].vertColor);
    float3 tangent = float3(vertex_array[vid].tangent);
    float3 bitangent = float3(vertex_array[vid].bitagent);
    float3 normal = float3(vertex_array[vid].normal);

    ColorInOut out;
    out.hasMeshColor = hasMeshColor[iId];
    out.meshColor = (meshColor[iId].data[0] == -1.0) ? vertColor.xyz : float3(meshColor[iId].data);
    out.reflection = reflectionValue[iId];

    out.T = normalize(float3(model[iId].data * float4(tangent, 0.0)));
    out.B = normalize(float3(model[iId].data * float4(bitangent, 0.0)));
    out.N = normalize(float3(model[iId].data * float4(normal,  0.0)));

    out.vertexPosition_ws = vertexPosition_ws;
    out.transparency = transparency[iId];
    out.hasLighting = hasLighting[iId];
    out.position = vp * vertexPosition_ws;
    out.uv = vertex_array[vid].texCoord1 * uvScaleValue[iId];
    out.shadowDarkness = 0.0;

    if(bool(hasLighting[iId])) {
        float4 vertexLightCoord = lvp * vertexPosition_ws;
        float4 texCoords = vertexLightCoord/vertexLightCoord.w;
        out.texture2UV = float4((texCoords / 2.0) + 0.5).xy;
        out.texture2UV.y = (1.0 - out.texture2UV.y); // need to flip metal texture vertically
        out.vertexDepth = texCoords.z;
        out.shadowDarkness = shadowDarkness;
    }

    float4 eye_position_ws = vertexPosition_ws - float4(float3(eyePos), 1.0);
    out.eyeVec = normalize(eye_position_ws);

    return out;
}

最佳答案

我怀疑你引用的是整个错误信息。在询问错误消息时,请始终包括完整的逐字消息。

也就是说,我最好的猜测是 [[ point_coord ]] 不是顶点函数输出的有效属性。

另一件事是你应该将输入参数 vertex_array 声明为 const 以便 Metal 知道你不会写入它:

device const vertex_t* vertex_array [[ buffer(0) ]]

写入缓冲区或纹理的顶点着色器只能返回 void

关于ios - 在 Metal Shader Vertex 函数中出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56830134/

相关文章:

ios - 在创建时停止 UIViewController 的自动旋转

swift - 这个 MVP Metal main.swift 显示为空白

macos - 如何检测 macOS 上的外部 GPU (eGPU) 连接和断开连接?

c++ - OpenGL - 从顶点着色器访问缓冲区中的下 3 个顶点

opengl - 为什么 glGetUniformBlockIndex() 返回 INVALID_INDEX?

ios - iOS 的 Branch.io 链接在安装后不传递数据,但适用于冷启动

ios - 用于用户注册过程的 Segue 类型 - iOS 5

ios - 执行同步操作

scenekit - 为什么将 Metal 着色器渐变作为SCNProgram应用于SceneKit节点要比作为MTKView轻一些?

c - 在 VertexShader 中使用 mat4