DX11 Oculus Rift Shader

Hello, I’ve got this up and running but I think it could still do with some optimization.

Any takers (testers)?


//@author: vux
//@help: template for texture fx
//@tags: texture

Texture2D texture2d : PREVIOUS;

SamplerState linearSampler : IMMUTABLE
    Filter = MIN_MAG_MIP_LINEAR;
    AddressU = Wrap;
    AddressV = Wrap;
cbuffer cbPerObj : register( b1 )

float2 _Center 					= float2(0,0);
float2 _ScaleIn 				= float2(0,0);
float2 _Scale  					= float2(0,0);
float4 _HmdWarpParam 			= float4(0,0,0,0);
float4 _ChromaticAberration 	= float4(0,0,0,0);

float4 PDistort(float4 PosWVP:SV_POSITION,float2 uv:TEXCOORD0) : SV_Target
	/*float4 c = texture2d.Sample(linearSampler,input.uv);
	return c;
	    // calculate normal distortion values
	float4 i = texture2d.Sample(linearSampler,uv);
	float2 theta  = (uv - _Center) * _ScaleIn; 
	float  rSq    = theta.x * theta.x + theta.y * theta.y;
	float2 theta1 = theta * (_HmdWarpParam.x + 
							 _HmdWarpParam.y * rSq + 
    	                     _HmdWarpParam.z * rSq * rSq + 
       	                 	 _HmdWarpParam.w * rSq * rSq * rSq);
    // calculate the texture co-ordinates for each color channel  	 
    float2 thetaRed  = (theta1 * _ChromaticAberration.x) + 
		              (theta1 * rSq * _ChromaticAberration.y);
	float2 tcRed     = _Center + _Scale * thetaRed;

    float2 tcGreen   = _Center + _Scale * theta1;

	float2 thetaBlue = (theta1 * _ChromaticAberration.z) + 
	                   (theta1 * rSq * _ChromaticAberration.w);
	float2 tcBlue    = _Center + _Scale * thetaBlue;

	// Check to see if we are out of range on the texture co-ordinates
	// (green channel is the same as normal distortion)

	// Using 3 different texture co-ordinates, sample each channel
	// to correct for color aberration
	float red   = texture2d.Sample(linearSampler, tcRed).x;
	float green = texture2d.Sample(linearSampler, tcGreen).y;    
	float blue  = texture2d.Sample(linearSampler, tcBlue).z;
	float alpha = 1;
	/*float red = tex2D (_MainTex, i.uv + float2(0.1, 0.0)).x;;
	float green = tex2D (_MainTex, i.uv).y;
	float blue = tex2D (_MainTex, i.uv + float2(0.0, 0.1)).z;;
	float alpha = 1;*/
	if (any(clamp(tcGreen, float2(0, 0), float2(1, 1)) - tcGreen))
    	red   = 0;
		green = 0;    
		blue  = 0;
		alpha = 0;
	return float4(red, green, blue, alpha);  	  		


technique10 Distort
	pass P0

I’m using pretty much exactly the same. Was meaning to try some other sampler modes too- won’t make it faster though. I remember reading about someone using a lookup texture for the warp, that might be something to also look at. I’ve not really found a good way to use post fx with it, since they should really go before this pass. I mean it works, but using tfx on both of the pre warped scaled up textures is expensive.

I hear you. I tried a MRT renderer but it didn’t work out at the time. Doing that and then sending both textures through the node at a time: no luck. Still, I’ll post up some findings soon.

Cheers mate.

I ended up using two temp target renderers for some reason (can’t remember why MRT didn’t work in this case- maybe the transforms being different?) but both textures worked fine on the tfx after a cons.

I’d like to update the OR contribution with some modules to separate the plugin and dx9/11 shaders.

Are you on skype at all Hadasi? My username is the same.