【发布时间】:2020-02-12 09:39:06
【问题描述】:
我正在使用 Metal 开发绘图引擎。我正在从以前的版本重新开始,所以从头开始
我收到错误 由于执行期间出现错误,命令缓冲区的执行被中止。导致 GPU 挂起错误(IOAF 代码 3)
经过一些调试,我把责任归咎于我的 drawPrimitives 例程,我发现这个案例很有趣
我会有各种各样的画笔,它们都可以使用特定的顶点信息
所以我说,为什么不呢?让所有画笔响应协议
顶点的协议是这样的:
protocol MetalVertice {}
这个特定画笔使用的顶点信息将是:
struct PointVertex:MetalVertice{
var pointId:UInt32
let relativePosition:UInt32
}
可以通过提供先前创建的顶点或调用函数来创建这些顶点来调用画笔。不管怎样,真正的绘图发生在顶点函数上
var vertices:[PointVertex] = [PointVertex].init(repeating: PointVertex(pointId: 0,
relativePosition: 0),
count: totalVertices)
for (verticeIdx, pointIndex) in pointsIndices.enumerated(){
vertices[verticeIdx].pointId = UInt32(pointIndex)
}
for vertice in vertices{
print("size: \(MemoryLayout.size(ofValue: vertice))")
}
self.renderVertices(vertices: vertices,
forStroke: stroke,
inDrawing: drawing,
commandEncoder: commandEncoder)
return vertices
}
func renderVertices(vertices: [MetalVertice], forStroke stroke: LFStroke, inDrawing drawing:LFDrawing, commandEncoder: MTLRenderCommandEncoder) {
if vertices.count > 1{
print("vertices a escribir: \(vertices.count)")
print("stride: \(MemoryLayout<PointVertex>.stride)")
print("size of array \(MemoryLayout.size(ofValue: vertices))")
for vertice in vertices{
print("ispointvertex: \(vertice is PointVertex)")
print("size: \(MemoryLayout.size(ofValue: vertice))")
}
}
let vertexBuffer = LFDrawing.device.makeBuffer(bytes: vertices,
length: MemoryLayout<PointVertex>.stride * vertices.count,
options: [])
这是问题所在,调用此特定代码会在控制台中产生以下结果:
size: 8
size: 8
vertices a escribir: 2
stride: 8
size of array 8
ispointvertex: true
size: 40
ispointvertex: true
size: 40
在前面的函数中,顶点的大小是8字节,但是由于某种原因,当它们进入下一个函数时变成了40字节,所以缓冲区构造错误
如果我将函数签名更改为:
func renderVertices(vertices: [PointVertex], forStroke stroke: LFStroke, inDrawing drawing:LFDrawing, commandEncoder: MTLRenderCommandEncoder) {
顶点被正确报告为 8 字节长,并且绘制例程按预期工作
我缺少什么吗?如果 MetalVertice 协议引入了一些噪音?
【问题讨论】: