Canonical Stereo Code

From Bo3b's School for Shaderhackers
Revision as of 23:31, 20 September 2014 by Bo3b admin (Talk | contribs)

Jump to: navigation, search
...
// Sampler used to fetch stereo params, 
// and the required constant for txldl in c200.z
dcl_2d s0
def c200, 1.0, 600, 0.0625, 0
...
 
// At this point r0 is the output position, correctly
// placed, but without stereo.
 
// To create stereo effects, we need to calculate:
//  Xnew = Xold + Separation * (W - Convergence)
 
// Fetch the Separation (r30.x) and Convergence (r30.y)  
// using the Helix NVapi trick
texldl r30, c200.z, s0
 
// (W - Convergence)
add r30.w, r0.w, -r30.y
 
// multiply that times Separation for:
//   Separation * (W - Convergence)
mul r30.z, r30.x, r30.w
 
// Add that to Xold for the complete:
//  Xold + Separation * (W - Convergence)
add r0.x, r0.x, r30.z


Another variant that is more concise, but less clear. As you get more accustomed to seeing this sequence of code, or are sharing with an expert crowd, it's less necessary to fully document this part, as it is always the same sequence and easy to recognize because of the texldl of 0.0625.

...
// Stereo correction constants
dcl_2d s0
def c200, 1.0, 600, 0.0625, 0
...
 
// At this point r0 is the output position, correctly
// placed, but without stereo.
 
// To create stereo effects, we need to calculate:
//  Xnew = Xold + Separation * (W - Convergence)
 
texldl r30, c200.z, s0
add r30.w, r0.w, -r30.y
mad r0.x, r30.x, r30.w, r0.x


Another very common variant you'll see in HelixMod fixes is the four line version, with no comments. This is less optimal than the two above, but is worth seeing to be able to recognize it.

...
// Stereo correction constants
dcl_2d s0
def c200, 1.0, 600, 0.0625, 0
...
 
// At this point r0 is the output position, correctly
// placed, but without stereo.
texldl r30, c200.z, s0
add r30.w, r0.w, -r30.y
mul r30.z, r30.x, r30.w
add r0.x, r0.x, r30.z


The HLSL version is very similar, but since it's a compile language there is no need to be terse when writing the code. We can make it "self-documenting" by using good variable names.

...
// .Load should only be done once, but can be done anywhere in the code.
 
float4 stereo = StereoParams.Load(0);
float separation = stereo.x; float convergence = stereo.y;
 
// At this point, "location" is the output position, but missing stereo.
 
location.x += separation * (location.w - convergence);