Lens distortion shader

Advanced OpenGL source port fork from ZDoom, picking up where ZDoomGL left off.
[Home] [Download] [Git builds (Win)] [Git builds (Mac)] [Wiki] [Repo] [Bugs&Suggestions]

Moderator: Graf Zahl

dpJudas
Developer
Developer
Posts: 798
Joined: Sat Jul 23, 2016 7:53

Lens distortion shader

Post by dpJudas »

I've created a lens distortion shader effect. Pull request: https://github.com/coelckers/gzdoom/pull/74

Not so much to say about it except that I added it to the menus (off by default) along with a few cvars to configure it.

The PR also simplifies/standardizes how post processing shaders interact with the render buffers. Any number of additional shaders can now be added by using BindCurrentTexture, BindCurrentFB, BindNextFB and NextTexture.
User avatar
Graf Zahl
GZDoom Developer
GZDoom Developer
Posts: 7148
Joined: Wed Jul 20, 2005 9:48
Location: Germany

Re: Lens distortion shader

Post by Graf Zahl »

This barely makes a difference to me. What's the point of this particular effect?
dpJudas
Developer
Developer
Posts: 798
Joined: Sat Jul 23, 2016 7:53

Re: Lens distortion shader

Post by dpJudas »

Honestly? I barely notice any change myself. The walls bend a little bit and the colors get distorted in the edge of the screen. :)

The effect is supposed to simulate the distortion effects caused by a real physical camera lens. I was reading this article about how the pipeline of GTA V looks like (http://www.adriancourreges.com/blog/201 ... ics-study/ - scroll down to "Anti-Aliasing and Lens Distortion") and one of the things they bothered throwing GPU resources after was this lens effect. So I wanted to see exactly what difference it makes to a scene.

Anyhow, the part of the PR that adjusts FGLRenderBuffers should be good to have for the future. Even if the lens shader itself is removed again (all it takes is to delete the shader files and the LensDistortScene function). On the other hand we can brag we are just as advanced as Rockstar by keeping it. ;)
_mental_
Developer
Developer
Posts: 259
Joined: Sun Aug 07, 2011 13:36

Re: Lens distortion shader

Post by _mental_ »

IMHO it's worth merging, for the buffers part at least.
Little bit unrelated but still: prologue/epilogue of post-processing steps (saving/restoring GL states) are mostly the same and they can be moved to separate functions.
dpJudas
Developer
Developer
Posts: 798
Joined: Sat Jul 23, 2016 7:53

Re: Lens distortion shader

Post by dpJudas »

Hmm yes, they probably should be merged now. Was waiting to see what pattern that was in what needed to be saved. I can do a commit to this PR or create a new one (whichever Graf prefers) that cleans this up.

I think the best way to deal with it probably to create some FGLPostProcessState class that saves things in the constructor and restores it in the destructor. Then it can be added as the first line in each post process function. And if some post process in the future needs additional states saved they can be done as functions on this class.
User avatar
Graf Zahl
GZDoom Developer
GZDoom Developer
Posts: 7148
Joined: Wed Jul 20, 2005 9:48
Location: Germany

Re: Lens distortion shader

Post by Graf Zahl »

This one's already pushed, so a new one, based on the current master, would be preferable.
dpJudas
Developer
Developer
Posts: 798
Joined: Sat Jul 23, 2016 7:53

Re: Lens distortion shader

Post by dpJudas »

Pull request for the FGLPostProcessState class: https://github.com/coelckers/gzdoom/pull/75

I merged the vertex shaders into a single screenquad.vp as well.
User avatar
Graf Zahl
GZDoom Developer
GZDoom Developer
Posts: 7148
Joined: Wed Jul 20, 2005 9:48
Location: Germany

Re: Lens distortion shader

Post by Graf Zahl »

While testing some other stuff I noticed that the automap is missing some parts if the game is not running fullscreen. Are you doing some scaling here? It looks like the internal framebuffer you render to is larger than the actual client area of the window.
dpJudas
Developer
Developer
Posts: 798
Joined: Sat Jul 23, 2016 7:53

Re: Lens distortion shader

Post by dpJudas »

You mean the monsters/secrets thing? Zdoom seems to be doing that as well.

The framebuffers always has the size of the of the entire canvas screen (screen->GetWidth/GetHeight). The SetOutputViewport function calculates three viewports within the buffers: mScreenViewport, mOutputViewportLB and mOutputViewport. The screen viewport covers the entire canvas and is used for general 2D drawing. The two other viewports are used to render the scene - maybe they should be renamed to mSceneViewportLB and mSceneViewport.

The only scaling going on is in CopyToBackbuffer and is always happening regardless of what the game is doing. It letterboxes and scales the final copy to the backbuffer for windowed mode. For fullscreen the calculations should amount to no scaling as the window client area should be a 1:1 to screen->GetWidth/GetHeight.
User avatar
Graf Zahl
GZDoom Developer
GZDoom Developer
Posts: 7148
Joined: Wed Jul 20, 2005 9:48
Location: Germany

Re: Lens distortion shader

Post by Graf Zahl »

No. What I mean is that some line segments are missing, as if specific x and y positions were just discarded by some slight downscaling.
dpJudas
Developer
Developer
Posts: 798
Joined: Sat Jul 23, 2016 7:53

Re: Lens distortion shader

Post by dpJudas »

Hmm yes, framebuffer->GetClientWidth() returns 3828 here while it should have returned 3840 (for 3840x2160). That would give the effect you are describing. Investigating..
dpJudas
Developer
Developer
Posts: 798
Joined: Sat Jul 23, 2016 7:53

Re: Lens distortion shader

Post by dpJudas »

It seems to only happen on my computer if I run a "full screen" resolution in windowed mode. I think it is because Windows prevents the window (with its frame) to get larger than the screen and that leaves the client area slightly too small. You can see the same window size issue in the latest gzdoom release where the margin of the HUD is being cut off slightly.

I'm not sure what is the best way to deal with it. Maybe snap the scale to 1.0 if its within some threshold. It won't fix the wrong client size, but it will at least keep the scale correct and just cut off a few pixels at the edge.
User avatar
Graf Zahl
GZDoom Developer
GZDoom Developer
Posts: 7148
Joined: Wed Jul 20, 2005 9:48
Location: Germany

Re: Lens distortion shader

Post by Graf Zahl »

I think the cause of this problem is somewhere in ZDoom's messy video code. Yeah, great fun with that stuff...
Normally it should use GetClientRect to determine the proper canvas size.
dpJudas
Developer
Developer
Posts: 798
Joined: Sat Jul 23, 2016 7:53

Re: Lens distortion shader

Post by dpJudas »

Yes, that would be the ideal solution - but I don't think I can get it to use GetClientRect without refactoring the whole thing. SCREENWIDTH and friends are so deeply entrenched in zdoom that it would require tons of testing even if it was attempted. :/
User avatar
Graf Zahl
GZDoom Developer
GZDoom Developer
Posts: 7148
Joined: Wed Jul 20, 2005 9:48
Location: Germany

Re: Lens distortion shader

Post by Graf Zahl »

All it needs to do is setting the current framebuffer size to the client area. Currently it looks like it sets it to the window's size including the frame.

Return to “GZDoom”