Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Code Snippets / [DBP] Fisheye Shader Demonstration

Author
Message
Dr Tank
14
Years of Service
User Offline
Joined: 1st Apr 2009
Location: Southampton, UK
Posted: 20th May 2010 05:02


This is a demonstration of fisheye cameras using a dynamic cube mapping shader. Shader has many techniques for different fisheye variations: Stereographic, Area Preserving, Linear Angle, Cylindrical. Most have an exact and faster approximate version using series expansions instead of slow functions (sin, cos, sqrt) in pixel shader. Standard flat camera is also implemented for comparison.

Attached is project folder with .dba and .fx files in a .rar. Download is 7.19kB. I append the code for reference purposes. I have tried to clearly lay out and comment the code, but if you have questions, please ask.

.DBA code:


.FX (FX/final.fx) code:

Attachments

Login to view attachments
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 20th May 2010 12:55
Very nice demo.

Two comments:

1. I had to change rgba to rgbaAM (or anything really) as rgba is one of IanM's Matrix1 functions (which I have) which probably does the same thing.

2. I didn't understand your comment "don't think can send 2vectors to shader," in the dba file. You can only pass vector4's to shaders using DBPro but you can declare them as float2's in the shader, the extra components get ignored. If you want to play safe by making the types match then declare them as float4's in the shader as well but use swizzling to pick out the components you want, e.g.



You could also pack two float2's into a single float4 and use swizzling to pick out the ones you want.

You can swizzle constants as well. For example if you want the vector {1.0, 1.0, 1.0, 1.0} you can write it as 1.0.xxxx in calculations, i.e. 4 copies of the first (and only) component of the constant 1.0.

PS2 and above support arbitrary swizzling, earlier PS1 versions have irritating limitations hidden away in the small print. But you're using SM2 so you're OK.

Hope this helps.
Dr Tank
14
Years of Service
User Offline
Joined: 1st Apr 2009
Location: Southampton, UK
Posted: 21st May 2010 17:30 Edited at: 21st May 2010 17:39
Thanks dude. I pondered doing the my4vector.xy thing but in the end went with easier solution. Perhaps i'll implement it when I update. Difference is that currently for 4:3, the sides of the object seen by cam 0 are off screen. But I figure the shader skips over drawing these parts anyway so with luck speed is unaffected.

I did wonder what might happen if a 4vector was sent to a 2 vector. Might it write to the pointer or something and overwrite other data? But as you say there's no need to do that.

I'm basically using shader model 2 for everything now. I get the impression that most users' computers support it. Is this a valid assumption? I find swizzling, casting etc kind of confusing. If you check out the pixel shaders here maybe you can see some speed improvements. I'm doing things like:



when i would rather do something like

but i couldn't figure out how to get that kind of thing working. Perhaps they result in the same compiled code. I've really got to check out FXComposer!

Played a little with this since. By sync masking out the back facing camera when it's not required, FPS gains about 10%. Possible improvements to this include squashing the cube map co-ordinates and camera frustrums along the z-axis to achieve more uniform texture scaling across the screen, and switching to a single extra camera with variable FOV when zoomed in.
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 21st May 2010 23:34
Quote: "when i would rather do something like"




Quote: "but i couldn't figure out how to get that kind of thing working."


I do that a lot. Can you give an example where that snippet doesn't work for you?

Quote: "Perhaps they result in the same compiled code."


Quite possible. I've often tried fiddling with things like that to save an instruction or two and failed. The DX9 fx compiler has a built-in optimizer which is hard to beat.

Quote: "I've really got to check out FXComposer!"


Or Dark Shader - both will give you the compiled asm listing.

Quote: "I'm basically using shader model 2 for everything now. I get the impression that most users' computers support it. Is this a valid assumption?"


Yes. I use SM3 (or PS2a) when I run into instruction count limits, or just PS3 if I want texture reads in the vertex shader. Both are supported by DBPro. I haven't tried SM4 yet because it's not supported by DX9/DBPro.
Dr Tank
14
Years of Service
User Offline
Joined: 1st Apr 2009
Location: Southampton, UK
Posted: 22nd May 2010 13:56
Quote: "I do that a lot. Can you give an example where that snippet doesn't work for you?"

It appears I may have been talking out of my bottom. What i said didn't work did work.

I'm will give shader model 3 a bash, because i want to use texture lookup in the vertex shader to scrolling, level of detail terrain. Any idea how commonly suppoerted it is?
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 23rd May 2010 01:17
Quote: "What i said didn't work did work."




Quote: "Any idea how commonly suppoerted it is?"


It's quite widely supported on games machines with a reasonable spec - but I'd guess it is less common on machines you might find in a standard commercial, industrial or education environment. SM2 is more standard there.
Dr Tank
14
Years of Service
User Offline
Joined: 1st Apr 2009
Location: Southampton, UK
Posted: 15th Jul 2010 16:39 Edited at: 15th Jul 2010 16:44
Sorry for tardy reply. I guess I'll stick with SM2 for a bit.

Here's another demo. This only has the sterographic camera (angle, circle preserving), but has a clear zoom, using a single extra camera instead of the cube map for small FOV.

I attach the rar compressed project folder. Exe is included because future versions of DBP may complile the code differently. Download is 880 kB.

I also append the code for quick reference. Note that from the second shader, only one technique (Spherical) and pixel shader (PShader) is used by the program.

Here is the .dba:


shader "fx/singlecam16by9zoom.fx":


shader "fx/final2.fx":

Attachments

Login to view attachments

Login to post a reply

Server time is: 2024-03-28 09:10:37
Your offset time is: 2024-03-28 09:10:37