我一直在学习适用于 iOS / OSX 的 Metal,我从遵循Ray Wenderlich 教程开始。本教程工作正常,但没有提及MTLVertexAttributeDescriptors
.
现在我正在开发自己的应用程序,我遇到了奇怪的故障,我想知道我不使用的事实是否MTLVertexAttributeDescriptors
与问题有关。
它们有什么区别?我已经能够制作各种具有不同顶点结构的着色器,而我什至不知道这些东西。
我知道您使用它们来描述在着色器中使用的顶点组件的布局。例如,着色器可能会将此结构用于顶点,并且将在下面函数的顶点描述符中设置它。
typedef struct
{
float3 position [[attribute(T_VertexAttributePosition)]];
float2 texCoord [[attribute(T_VertexAttributeTexcoord)]];
} Vertex;
class func buildMetalVertexDescriptor() -> MTLVertexDescriptor {
let mtlVertexDescriptor = MTLVertexDescriptor()
mtlVertexDescriptor.attributes[T_VertexAttribute.position.rawValue].format = MTLVertexFormat.float3
mtlVertexDescriptor.attributes[T_VertexAttribute.position.rawValue].offset = 0
mtlVertexDescriptor.attributes[T_VertexAttribute.position.rawValue].bufferIndex = T_BufferIndex.meshPositions.rawValue
mtlVertexDescriptor.attributes[T_VertexAttribute.texcoord.rawValue].format = MTLVertexFormat.float2
mtlVertexDescriptor.attributes[T_VertexAttribute.texcoord.rawValue].offset = 0
mtlVertexDescriptor.attributes[T_VertexAttribute.texcoord.rawValue].bufferIndex = T_BufferIndex.meshGenerics.rawValue
mtlVertexDescriptor.layouts[T_BufferIndex.meshPositions.rawValue].stride = 12
mtlVertexDescriptor.layouts[T_BufferIndex.meshPositions.rawValue].stepRate = 1
mtlVertexDescriptor.layouts[T_BufferIndex.meshPositions.rawValue].stepFunction = MTLVertexStepFunction.perVertex
mtlVertexDescriptor.layouts[T_BufferIndex.meshGenerics.rawValue].stride = 8
mtlVertexDescriptor.layouts[T_BufferIndex.meshGenerics.rawValue].stepRate = 1
mtlVertexDescriptor.layouts[T_BufferIndex.meshGenerics.rawValue].stepFunction = MTLVertexStepFunction.perVertex
return mtlVertexDescriptor
}
但即使没有MTLVertexDescriptor
设置,着色器也可以访问顶点缓冲区和position / texCoord
数组中顶点的组件。只需设置顶点缓冲区,着色器就可以访问所有组件。那么描述符有什么用呢?