Difference between revisions of "Canonical Stereo Code"

From Bo3b's School for Shaderhackers
Jump to: navigation, search
Line 43: Line 43:
 
...
 
...
 
// Stereo correction constants
 
// Stereo correction constants
dcl_2d s0
 
 
def c200, 1.0, 600, 0.0625, 0
 
def c200, 1.0, 600, 0.0625, 0
 +
dcl_2d s0
 
...
 
...
  
Line 64: Line 64:
 
...
 
...
 
// Stereo correction constants
 
// Stereo correction constants
dcl_2d s0
 
 
def c200, 1.0, 600, 0.0625, 0
 
def c200, 1.0, 600, 0.0625, 0
 +
dcl_2d s0
 
...
 
...
  

Revision as of 00:53, 22 September 2014

This is the prime directive code in ASM for DX9 and HelixMod. And the HLSL version for 3Dmigoto. In all cases, we are implementing the NVidia specified formula of:

clipPos.x += EyeSign * Separation * ( clipPos.w – Convergence )


This is the wordy version with extra comments to make it more clear what is happening.

...
// The required constant for txldl in c200.z
def c200, 1.0, 600, 0.0625, 0
// Sampler used to fetch stereo params, 
// s0 sampler is default for VS
dcl_2d s0
...
 
// 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
def c200, 1.0, 600, 0.0625, 0
dcl_2d s0
...
 
// 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
def c200, 1.0, 600, 0.0625, 0
dcl_2d s0
...
 
// 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);