Metal: Blending issues when rendering to a custom render texture

Goal: Render solid color quads with 0.5 alpha to my own transparent render target texture, then map the texture to a quad and render it to the screen. The texture should blend correctly (normal blend mode) with whatever is on the screen.

Problem: When rendering the resulting texture, the quads are rendered with half the alpha and never exceeding 0.5 alpha even when multiple quads are rendered on top of each other (within the custom render texture). My fragment shaders output straight alpha colors but it seems that the colors written to the render target texture are not straight alpha colors. When I sample the texture later, the colors are not what I would expect them to be. Ideally the texture would contain exactly the colors I output in my fragment shader, but that’s not the case for some reason.

Details: For simplicity, I’m rendering only a red color with 0.5 alpha. In the texture debugging view the pixel output shows a different color though. When I draw multiple quads on top of each other, I’d expect the color to eventually sample to fully opaque red (1, 0, 0, 1), but it never goes above 0.5 alpha as you can see in the screenshot (even though the color is clearly fully opaque red for the pixel [753, 498].

Here’s my texture, render pass and pipeline for rending the quads into the texture:

let textureDescriptor = MTLTextureDescriptor()
textureDescriptor.pixelFormat = pixelFormat
textureDescriptor.width = width
textureDescriptor.height = height
textureDescriptor.usage = [.renderTarget, .shaderRead]
textureDescriptor.storageMode = .shared
textureDescriptor.sampleCount = 1
textureDescriptor.textureType = .type2D

guard let texture = device.makeTexture(descriptor: textureDescriptor) else {
    throw MetalError.imageTexture
}

let renderPassDescriptor = MTLRenderPassDescriptor()
renderPassDescriptor.colorAttachments[0].loadAction = .clear
renderPassDescriptor.colorAttachments[0].clearColor = UIColor.white.withAlphaComponent(0).asMetalClearColor
renderPassDescriptor.colorAttachments[0].texture = texture
renderPassDescriptor.colorAttachments[0].storeAction = .store

...

let descriptor = MTLRenderPipelineDescriptor()
descriptor.vertexFunction = library.makeFunction(name: "vertexSolidShader")
descriptor.fragmentFunction = library.makeFunction(name: "fragmentColoredShader")
descriptor.colorAttachments[0].pixelFormat = pixelFormat
descriptor.colorAttachments[0].isBlendingEnabled = true
descriptor.colorAttachments[0].rgbBlendOperation = .add
descriptor.colorAttachments[0].alphaBlendOperation = .add
descriptor.colorAttachments[0].sourceRGBBlendFactor = .sourceAlpha
descriptor.colorAttachments[0].sourceAlphaBlendFactor = .sourceAlpha
descriptor.colorAttachments[0].destinationRGBBlendFactor = .oneMinusSourceAlpha
descriptor.colorAttachments[0].destinationAlphaBlendFactor = .oneMinusSourceAlpha
descriptor.sampleCount = 1
descriptor.rasterSampleCount = 1
descriptor.vertexBuffers[.vertexInputVerticesIndex].mutability = .immutable
descriptor.vertexBuffers[.vertexInputViewportSizeIndex].mutability = .immutable
descriptor.vertexBuffers[.vertexInputWorldUniformsIndex].mutability = .immutable
descriptor.vertexBuffers[.vertexInputRenderableUniformsIndex].mutability = .immutable

I’ve confirmed the fragmentColoredShader is returning the colors as expected (1, 0, 0, 0.5).

For testing purposes, I’m first rendering a large red quad with 0.5 alpha directly to the screen (MTKView’s render target) using the pipeline above and then on top of that I render a quad that has the custom texture mapped to it. Even the blending of the large solid quad and the textured quad is a bit weird (as if the alpha was not additive).

The render pipeline for rendering the textured quad is the same as above, except for the vertex/fragment functions, omitting vertex shader for clarity, nothing unusual there:

fragment half4 fragmentShader(MetalTexturedVertexOutput in [[stage_in]],
                              texture2d<half> colorTexture [[ texture(MetalTextureInputIndex) ]])
{
    constexpr sampler textureSampler (mag_filter::linear,
                                      min_filter::linear,
                                      address::repeat);
    const half4 colorSample = colorTexture.sample(textureSampler, in.textureCoordinate);
    return colorSample;
}

Notes:

I tried to play around with the sourceRGB and sourceAlpha blending factors – setting sourceAlpha to .one allows the quads within the texture go over the 0.5 alpha threshold, but each individual quad is still rendered with 0.25 alpha only. Blending with the large red quad still doesn’t work.

When I stop using my own texture for rendering the quads and render them to the MTKView’s texture right away, it all works as expected. Here’s the screenshot of what the actual result should be:

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật