CA Correction shader - AVS | Home Theater Discussions And Reviews
Forum Jump: 
Thread Tools
post #1 of 7 Old 11-30-2014, 09:52 AM - Thread Starter
AVS Special Member
 
RLBURNSIDE's Avatar
 
Join Date: Jan 2007
Posts: 1,183
Mentioned: 2 Post(s)
Tagged: 0 Thread(s)
Quoted: 269 Post(s)
Liked: 230
CA Correction shader

Hi Forum, here's the CA correction shader I wrote a while back, which I've decided to share:

https://www.shadertoy.com/view/4s2XDd

Yes, it's linear and quite naive in its implementation, but it should show the gist of it.

The defaults are 10 pixels of skew for two of the colour channels (with green unaffected), which is probably way more than you'll really see in practice on an anamorphic lens. But after about 3 pixels of colour channel offset at the extreme left and right edges of your screen, it becomes noticeable and distracting.

Next step is someone figuring out how to copy-paste that shader code into MPC-HC and testing it out on their lens (minus the time-varying stuff which is just there for show, to show that the skewing effect is proportional to the distance from the center line).

It would be easy to modify this for other lens shapes, multi-step ranges, vertical compression lenses instead of expansion lenses (as it is currently), etc. Then you can talk about correcting geometric distortions at the subpixel level which shaders are great at doing due to the filtering of the texture samples. The higher res your source image, like 1920 horizontal, the less the loss of 1:1 pixel in your source material would be, compared to the benefits in the final image after being skewed by the optical properties of whatever lens you have.

I believe it's possible to correct defocusing issues that vary with position in the image in the same way. Astronomers use such techniques all the time to sharpen and enhance the resolution of things at cosmic scales, such as via gravitational lensing and so on. I'm sure if we put our minds together, we can rig something decent for those of us who can't afford the fancy lenses out there that have all these correcting elements built in.

AVS forum member "dreamer" convinced me to join a prism lens group buy for our W1070 projectors to get CIH on the cheap, so I thank him for nudging me to share this. It's a humble little effort that may or may not help others. Of course it's for HTPC users only for now, but that doesn't mean we can't hack some cheap Bluray player's firmware or even an Oppo's HDMI through to provide this support to anyone who wants it. Or heck, even convince Oppo to officially add this to their next BDP firmwares.
RLBURNSIDE is online now  
Sponsored Links
Advertisement
 
post #2 of 7 Old 11-30-2014, 10:10 AM - Thread Starter
AVS Special Member
 
RLBURNSIDE's Avatar
 
Join Date: Jan 2007
Posts: 1,183
Mentioned: 2 Post(s)
Tagged: 0 Thread(s)
Quoted: 269 Post(s)
Liked: 230
Found the folder where the shaders for MPC-HC live:

C:\Program Files (x86)\SVP\MPC-HC\Shaders

They are all in HLSL, but it's super easy to convert from GLSL to HLSL. I should have something cooked up to share soon.
RLBURNSIDE is online now  
post #3 of 7 Old 11-30-2014, 11:00 AM - Thread Starter
AVS Special Member
 
RLBURNSIDE's Avatar
 
Join Date: Jan 2007
Posts: 1,183
Mentioned: 2 Post(s)
Tagged: 0 Thread(s)
Quoted: 269 Post(s)
Liked: 230
I just modified it to work in MPC-HC, it works!

/*
* (C) 2014 RLBURNSIDE - CA Correction Shader v 1.0 - for use in MPC-HC. Add in post-resize shaders
*/

sampler s0 : register(s0);
float4 p0 : register(c0);

#define width (p0[0])
#define height (p0[1])

float4 main(float2 tex : TEXCOORD0) : COLOR
{
// maximum aberration in number of pixels at tex.x == 0 or 1 (left or right edge)
const float redAberration = 10.0;
const float greenAberration = 0.0;
const float blueAberration = -10.0;

float pctEffect = (tex.x - 0.5) * 2.0;
float3 aberration = float3(redAberration / width, greenAberration / width, blueAberration / width);
aberration *= pctEffect;

float2 uvRedCorrected = float2(tex.x+aberration.x,tex.y);
float2 uvGreenCorrected = float2(tex.x+aberration.y,tex.y);
float2 uvBlueCorrected = float2(tex.x+aberration.z,tex.y);

float3 col;

col.r = tex2D(s0, uvRedCorrected).r;
col.g = tex2D(s0, uvGreenCorrected).g;
col.b = tex2D(s0, uvBlueCorrected).b;

float4 finalColour = float4(col,1.0);
return finalColour;
}


-------
copy paste the above into CACorrection.hlsl and save that file in the shaders dir of MPC-HC, then go to Play->Shaders->Select Shaders and add the .hlsl file to the Shaders list (Add shader file), then select it and click Add To Post resize

voila. tweak the values of red, green, blue aberration until you dial in the setting. Drag and drop a white grid of 1920x1080 size into MPC-HC and display it full screen, then in a text editor edit the values of the aberrations until the colors converge. Might want to do it pairwise, i.e. use a yellow grid to dial in the red towards the green (leave green untouched). Then use a cyan grid to dial in the blue towards the green.

Can someone make us a 1920x1080 grid .png file of yellow and cyan? And maybe a white one to validate the results of the CA correction?
RLBURNSIDE is online now  
post #4 of 7 Old 12-02-2014, 09:26 AM
AVS Special Member
 
greg1292's Avatar
 
Join Date: Oct 2004
Location: INDIANA
Posts: 1,155
Mentioned: 0 Post(s)
Tagged: 0 Thread(s)
Quoted: 38 Post(s)
Liked: 22
Great work glad to see it being addresses on the software side of things I will
follow with great interest as people using less expensive A-lens will benefit.
How would someone adjust this for HE lens?
greg1292 is online now  
post #5 of 7 Old 12-02-2014, 02:37 PM - Thread Starter
AVS Special Member
 
RLBURNSIDE's Avatar
 
Join Date: Jan 2007
Posts: 1,183
Mentioned: 2 Post(s)
Tagged: 0 Thread(s)
Quoted: 269 Post(s)
Liked: 230
The code there is already for HE, but it's a linear function meant just for prisms or flat lenses, not gradual ones with a curve and therefore non-linear CA gradient, in fact I was thinking I might have to modify it for the VC lens I just bought from you. We'll see what the optics are like when I get it

In my day job making videogames, I'm trying various methods to do this correction during the rendering phase instead of as a postprocessing step, so that you can select (on PC or on next gen consoles), in the menu, whether you want 16:9, letterbox scope, or anamorphic scope, and if other of the scope formats, if you want geometry/perspective correction and how much, and if you want CA correction on top of that. Both of those things can be done during rendering prior to the final image being rendered, so that you can end up with a pristine, sharp image. I believe correcting for not having a curved screen could be simply a matter of modifying the viewport matrix slightly and that's it, you've got straight lines again.

The shader toy here I will be testing and iterating on continuously until I'm satisfied I've done everything I can to maximize the final image I see on my wall. Then maybe get a few different .hlsl files for different lens shapes and configs (e.g. whether you have an HE/VC lens, and whether you have a curved screen or not. Those two issues are independent of each other some people might require only one or the other of the fixes, or both).

As a first step, I'm going to output the textures on the shader toy as a yellow or cyan grid files, so you can run that shader full screen, and add sliders to adjust the CA correction per channel. That way you can see on your projector what settings work, in your browser (full screen mode).

I suspect the MPC-HC hlsl code also has a way to determine what is your current window's top left position relative to the screen, so that the CA correction could still work even if you're watching the image fullscreen. Then if you display a shader that simply renders a fully transparent background, you could run the MPC-HC as a sort of overlay on your windows desktop to apply the CA / geometry / whatever correction to everything on your desktop.

I could also then send an email to my contacts over at AMD to add this type of effect into the overlay controls of a future Catalyst driver, so that it could be "always on" no matter how you use it : windows / linux / mac / games / media players / desktop, etc. Then get NVidia on board too.

Last edited by RLBURNSIDE; 12-02-2014 at 02:40 PM.
RLBURNSIDE is online now  
post #6 of 7 Old 02-17-2015, 09:28 AM
Member
 
latexii's Avatar
 
Join Date: Feb 2007
Posts: 33
Mentioned: 0 Post(s)
Tagged: 0 Thread(s)
Quoted: 1 Post(s)
Liked: 10
Whats going on with this one on these dayz ?
latexii is offline  
post #7 of 7 Unread Today, 08:08 PM - Thread Starter
AVS Special Member
 
RLBURNSIDE's Avatar
 
Join Date: Jan 2007
Posts: 1,183
Mentioned: 2 Post(s)
Tagged: 0 Thread(s)
Quoted: 269 Post(s)
Liked: 230
Haven't touched it in a while, but my projector was in the shop so I didn't have a use for it. Now that I got it back (a w1070), I'm building a hushbox out of an old HTPC case so I can mount my VC lens on it, and will probably be updating the shader (if I see any substantial CA) to accomodate the specifics of this lens.

However, from what I saw before, it looks like the formula for a VC lens is quite different, for starters the CA is minimal at the top (not the center) and grows towards the bottom. That's different than HE lens types but should be similarly easy to tweak the shader to correct it.

Feel free to adapt the shader yourself to your own lens to get the desired correction formula and coefficients, I'm certainly in no position to do that myself to adapt it to a wide range of lenses that I don't own.
RLBURNSIDE is online now  
Sponsored Links
Advertisement
 
Reply 2.35:1 Constant Image Height Chat

Thread Tools
Show Printable Version Show Printable Version
Email this Page Email this Page


Forum Jump: 

Posting Rules  
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off