我一直在学习Metal for iOS/OSX,我开始遵循Ray Wenderlich教程(https://www.raywenderlich.com/146414/metal-tutorial-swift-3-part-1-getting-started).本教程工作正常,但没有提及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
}
Run Code Online (Sandbox Code Playgroud)
但即使没有MTLVertexDescriptor设置,着色器也可以访问顶点缓冲区和数组中顶点的位置/ texCoord组件.只需设置顶点缓冲区,着色器就可以访问所有组件.这个描述符有什么好处呢?
我想在Metal应用程序中实现A-Buffer算法,以实现顺序无关的透明度。该技术的描述提到了使用原子计数器。我从来没有使用过这些,甚至从未听说过。我只是在《金属着色语言规范》中读到了原子变量,但无法弄清楚如何实际实现或使用一个。
有人在金属领域有经验吗?您能指出一个如何设置和使用简单整数计数器的示例吗?基本上,每个渲染过程我都需要能够从片段着色器中增加一个从零开始的整数。这用于索引A缓冲区。
谢谢!