ios – Metallic is premultiplying texture alpha between fragment attachment & fragment shader arg— why?

Spread the love


I am satisfied that Metallic is alpha-premultiplying my texture in some unspecified time in the future between the attachment commandEncoder.setFragmentTexture(…, index: 0) and the fragment shader’s arguments (texture2d<float> texture [[texture(0)]],). The Xcode Metallic Body Seize appears to substantiate this:

Inspecting the draw… command’s fragment texture binding:

Fragment binding in Xcode's frame debugger

exhibits that is clearly a non-alpha-premultiplied picture (which I do know to be true of the CGImage it comes from— it is pulled from the community and is loaded manually with texture.substitute(…, withBytes: …), not MTKTextureLoader):

Inspecting with Pre-Multiplied Alpha enabled; too mild on the semi-transparent edges to be appropriate:
Inspecting with Pre-Multiplied Alpha enabled; too light on the semi-transparent edges to be correct
Inspecting with Pre-Multiplied Alpha enabled; appropriate imagery:
Inspecting with Pre-Multiplied Alpha enabled; correct imagery

Nonetheless, when debugging the identical texel as returned by the dataTexture.pattern(dataTextureSampler, in.dataTextureCoordinate) name within the fragment shader (utilizing the 0 texture fragment binding), the pixels at the moment are clearly alpha-premultiplied:

Inspecting with Pre-Multiplied Alpha enabled; appropriate imagery:
Inspecting with Pre-Multiplied Alpha enabled; correct imagery
Inspecting with Pre-Multiplied Alpha disabled; too darkish on the semi-transparent edges to be appropriate:
Inspecting with Pre-Multiplied Alpha disabled; too dark on the semi-transparent edges to be correct


So why is Metallic doing this? Is that this documented anyplace? (I’ve scoured the Metallic Shading Language Specification Model 3.1 to no avail.) Is that this configurable in some way? (There is a trace to the aim of this by the argument title of dataTexture— I actually want to preserve the literal values within the texture except I say in any other case.)

Mixing is being finished with RGB: add, one, oneMinusSourceAlpha; A: add, one, oneMinusSourceAlpha; as is customary for pre-multiplied alpha mixing (although I am fairly positive that is not related right here since we’re fragment inputs, not blended outputs).

Utilizing Xcode 15.0.1 with an iPad Professional 11″ 2021 iPad13,6 operating iPadOS 17.1; and likewise Xcode 15.1 beta 2 with an iPhone 14 Professional iPhone15,2 operating iOS 17.2 beta 2.

Leave a Reply

Your email address will not be published. Required fields are marked *