Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Client-side MetalFX upscaling for image quality enhancement. #97

Open
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

xuhao1
Copy link

@xuhao1 xuhao1 commented Mar 23, 2024

This enables MetalFX for ALVR-visionos.
With a toggle to control if use MetalFX and upscaling factor selection, and also debug option to save raw image and upscaled image.
To use MetalFX, we need to convert YUV420 to RGB first.

UI

IMG_0243

IMG_0242

Comparsions:
Top: raw
Bottom: Upscaled

截屏2024-03-23 18 04 18 截屏2024-03-23 18 05 23

Known issue:
The R and B channel of saved image is opposite, but it's ok for debugging seeing these images.
Honestly, due to the bandwidth limitation of apple, metalfx does not change a lot currently.

Copy link
Collaborator

@shinyquagsire23 shinyquagsire23 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall it looks great, my main concern is with the color accuracy on the YUV conversion, since I spent a lot of time getting that right for the existing pipeline. Also want to make sure it works correctly with HDR/BT.2020 inputs.

}


func convertRGBA16FloatToRGBA8Unorm(device: MTLDevice, commandQueue: MTLCommandQueue, sourceTexture: MTLTexture) -> MTLTexture? {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason we can't keep the textures in RGB16? The drawable framebuffer is RGBA16Float so ideally we'd like to keep it so that HDR still works

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually wait, is this only for the PNG? If that's the case, then it's fine.


// Simple YUV to RGB conversion
float3 rgb;
rgb.r = y + 1.402 * uv.y;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should use the same YUV conversion matrix as the YUV fragment shaders, otherwise it will have different colors between HDR (BT.2020) and SDR (BT.709)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May or may not want to also do NonlinearToLinearRGB and EncodingNonlinearToLinearRGB here too? Not sure if the MetalFX upscaler cares about the images being a bit over-bright.


float3 rgb_uncorrect = in_tex_rgb.sample(colorSampler, sampleCoord).rgb;

const float DIV12 = 1. / 12.92;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs rebasing to use NonlinearToLinearRGB and EncodingNonlinearToLinearRGB now

desc.outputHeight = Int(Float(inputHeight)*scaling)
desc.colorTextureFormat = pixelFormat
desc.outputTextureFormat = pixelFormat
desc.colorProcessingMode = .perceptual
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the YUV conversion shader is changed to use NonlinearToLinearRGB and EncodingNonlinearToLinearRGB, change this to .hdr or .linear

@xuhao1
Copy link
Author

xuhao1 commented Mar 27, 2024

Overall it looks great, my main concern is with the color accuracy on the YUV conversion, since I spent a lot of time getting that right for the existing pipeline. Also want to make sure it works correctly with HDR/BT.2020 inputs.

Basically these code is generated by ChatGPT, I’ll carefully check them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants