r8Unorm 纹理的正确混合模式

Correct blending mode for r8Unorm texture

目标纹理和笔刷纹理是 r8Unorm。在这种情况下,正确的混合模式是什么?

画笔纹理是径向渐变,看起来像这样:

在第一次绘制调用时它看起来是正确的,但在下一次绘制调用时它变成了没有任何渐变的全红色圆圈。

我当前错误的混合设置如下所示:

private func createBrushDescriptor(library: MTLLibrary) -> MTLRenderPipelineDescriptor {
    let vertexDescriptor = MTLVertexDescriptor()
    vertexDescriptor.attributes[0].format = .float2
    vertexDescriptor.attributes[0].offset = 0
    vertexDescriptor.attributes[0].bufferIndex = 0

    vertexDescriptor.layouts[0].stride = 2 * MemoryLayout<simd_float1>.size
    vertexDescriptor.layouts[0].stepFunction = .perVertex
    vertexDescriptor.layouts[0].stepRate = 1
    
    let descriptor = MTLRenderPipelineDescriptor()
    descriptor.vertexFunction = library.makeFunction(name: "brush_vertex_function")
    descriptor.vertexDescriptor = vertexDescriptor
    descriptor.fragmentFunction = library.makeFunction(name: "brush_fragment_function")
    descriptor.colorAttachments[0].pixelFormat = .r8Unorm
    
    descriptor.colorAttachments[0].isBlendingEnabled = true
    descriptor.colorAttachments[0].rgbBlendOperation = .add
    descriptor.colorAttachments[0].sourceRGBBlendFactor = .one
    descriptor.colorAttachments[0].destinationRGBBlendFactor = .one
    
    descriptor.depthAttachmentPixelFormat = .invalid
    
    return descriptor
}

我使用 MTLPrimitiveType.point 原始画笔。

我用这个配置解决了我的问题:

descriptor.colorAttachments[0].isBlendingEnabled = true
descriptor.colorAttachments[0].rgbBlendOperation = .max
descriptor.colorAttachments[0].sourceRGBBlendFactor = .one
descriptor.colorAttachments[0].destinationRGBBlendFactor = .one