UIImage created from MTKView results in color/opacity differences
Your CGColorSpace
is .sRGB
but your renderPipelineDescriptor's pixelFormat is .bgra8Unorm
. Try changing that line to:
renderPipelineDescriptor.colorAttachments[0].pixelFormat = MTLPixelFormat.bgra8Unorm_srgb
how to display MTKView with rgba16Float MTLPixelFormat
So, it looks like you are very close to the complete solution. But what you have is not quite correct. Here is a Metal function that will convert from sRGB to a linear value which you can then write in your Metal shader (I still suggest that you write to a sRGB texture but you can also write to a 16 bit texture). Note that sRGB is not a simple 2.2 gamma curve.
// Convert a non-linear log value to a linear value.
// Note that normV must be normalized in the range [0.0 1.0].
static inline
float sRGB_nonLinearNormToLinear(float normV)
{
if (normV <= 0.04045f) {
normV *= (1.0f / 12.92f);
} else {
const float a = 0.055f;
const float gamma = 2.4f;
//const float gamma = 1.0f / (1.0f / 2.4f);
normV = (normV + a) * (1.0f / (1.0f + a));
normV = pow(normV, gamma);
}
return normV;
}
Related Topics
Uitableviewcell Asynchronously Loading Images Issue - Swift
Interactivepopgesturerecognizer Corrupts Navigation Stack on Root View Controller
Swift: How to Expand a Tilde in a Path String
Executefetchrequest Doesn't Return the Nsmanagedobject Subclass
Cut a Circle Out of a Uiview Using Mask
How to Capture Local Variable Inside an Async Closure in Swift
Swift Arc4Random_Uniform(Max) in Linux
Swift - How to Save Audio from Avaudioengine, or from Audioplayernode? If Yes, How
Accessing Multiple Audio Hardware Outputs/Channels Using Avfoundation and Swift
Why Is the ! in Swift Called an 'Implicitly' Rather Than 'Explicitly' Unwrapped Optional
Rxswift: Use Zip with Different Type Observables
How to Automatically Reflect Coredata+Icloud Changes in Swiftui View
Scenekit Object Between Two Points
Understanding @Binding in Swiftui
Making Nsdecimalnumber Codable
Swiftui Navigationview Trying to Pop to Missing Destination (Monoceros)