我正在创建一个使用Metal来渲染所有内容的图形应用程序 . 当我在所有绘制调用的管道统计信息下进行帧调试时,有一个!! Headers 为“预防设备地址模式加载”的优先级警报,详细信息如下:

Indexing using unsigned int for offset prevents addressing calculation in device. To prevent this extra ALU operation use int for offset.

因此,对于我这里涉及到的最简单的绘制调用是正在发生的事情 . 有大量的顶点数据后跟索引缓冲区 . 索引缓冲区在开始时创建并填充,然后从那时开始不变 . 顶点数据不断变化 .

我有以下类型:

struct Vertex {
    float3 data;
};
typedef int32_t indexType;

然后是下面的绘制调用

[encoder drawIndexedPrimitives:MTLPrimitiveTypeTriangle indexCount:/*int here*/ indexType:MTLIndexTypeUInt32 indexBuffer:indexBuffer indexBufferOffset:0];

这转到了以下顶点函数

vertex VertexOutTC vertex_fun(constant Vertex * vertexBuffer [[ buffer(0) ]],
                                indexType vid [[ vertex_id ]],
                                 constant matrix_float3x3* matrix [[buffer(1)]]) {

    const float2 coords[] = {float2(-1, -1), float2(-1, 1), float2(1, -1), float2(1, 1)};
    CircleVertex vert = vertexBuffer[vid];
    VertexOutTC out;
    out.position = float4((*matrix * float3(vert.data.x, vert.data.y, 1.0)).xy, ((float)((int)vid/4))/10000.0, 1.0);
    out.color = HSVtoRGB(vert.data.z, 1.0, 1.0);
    out.tc = coords[vid % 4];
    return out;
}

我很困惑我在这里做错了什么 . 该错误似乎表明我不应该使用无符号类型的偏移量,我猜测是索引缓冲区 .

事情是最终的索引缓冲区只有 MTLIndexTypeUInt32MTLIndexTypeUInt16 都是无符号的 . 此外,如果我尝试使用原始 int 作为着色器不会编译的类型 . 这里发生了什么?