【问题标题】:Render second vertex buffer with Apple's Metal使用 Apple 的 Metal 渲染第二个顶点缓冲区
【发布时间】:2016-05-07 06:12:55
【问题描述】:

我遇到了一个问题,我只想渲染两个三角形(每个三角形都存储在单独的缓冲区中),Metal API 拒绝渲染第二个顶点缓冲区的尝试。我怀疑这是关于对齐的。断言消息是failed assertion `(length - offset)(0) must be >= 32 at buffer binding at index 0 for vertexArray[0]。'这里是代码:

顶点和常量结构体:

struct VertexPositionColor
{
    VertexPositionColor(const simd::float4& pos,
                        const simd::float4& col)
    : position(pos), color(col) {}

    simd::float4 position;
    simd::float4 color;
};
typedef struct
{
    simd::float4x4 model_view_projection;
} constants_t;

这就是我存储和添加新缓冲区的方式(函数被调用两次):

NSMutableArray<id<MTLBuffer>> *_vertexBuffer;
NSMutableArray<id<MTLBuffer>> *_uniformBuffer;
NSMutableArray<id<MTLBuffer>> *_indexBuffer;

- (void)linkGeometry:(metalGeometry*)geometry
{
    [_vertexBuffer addObject:[_device newBufferWithBytes:[geometry vertices]
                                         length:[geometry vertices_length]
                                        options:0]
     ];

    [_uniformBuffer addObject:[_device newBufferWithLength:[geometry uniforms_length]
                                          options:0]
     ];

    RCB::constants_t* guts = (RCB::constants_t*) [[_uniformBuffer lastObject] contents];
    guts->model_view_projection = [geometry uniforms]->model_view_projection;

    [geometry linkTransformation:(RCB::constants_t *)[[_uniformBuffer lastObject] contents]];
}

接下来是断言失败的行(最后一行):

[render setVertexBuffer:_vertexBuffer[0] offset:0 atIndex:0];
[render setVertexBuffer:_uniformBuffer[0] offset:0 atIndex:1];
[render drawPrimitives:MTLPrimitiveTypeTriangle vertexStart:0 vertexCount:3];

[render setVertexBuffer:_vertexBuffer[1] offset:3*sizeof(VertexPositionColor) atIndex:0];
[render setVertexBuffer:_uniformBuffer[1] offset:sizeof(constants_t) atIndex:1];
[render drawPrimitives:MTLPrimitiveTypeTriangle vertexStart:3 vertexCount:3];

所以,我们只是使偏移量等于前一个缓冲区占用的内存大小。请注意,如果我们将最后一行注释掉,第一个三角形将按预期呈现。

谁能理解我错过了什么?我真的很感激。

问候

【问题讨论】:

  • offsets 参数表示到提供的缓冲区中数据开头的偏移量。如果您为每个对象使用单独的缓冲区,偏移量不应该始终为 0 吗?
  • @warrenm 你完全正确,伙计!现在可以正常使用了,谢谢!不知道是什么让我认为它在前一个缓冲区中附加了一个新缓冲区...

标签: rendering memory-alignment gpu metal


【解决方案1】:

offset 参数表示到所提供缓冲区中数据开头的偏移量。如果您为每个对象使用单独的缓冲区,则偏移量应为 0。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-13
    相关资源
    最近更新 更多