Canonical Stereo Code

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

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
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 Helix's 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, it's less useful to fully document 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.

// 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