Some trouble with shaders and pixel / sub-pixel alignment

The place for all discussion of the Oculus Rift compatible open source 3D drivers.
Post Reply
moddy3d
One Eyed Hopeful
Posts: 2
Joined: Sat May 25, 2013 9:35 am

Some trouble with shaders and pixel / sub-pixel alignment

Post by moddy3d »

Hello all... new to this forum.. but an enthusiast of 3D

I'm trying to write a shader to target Auto-stereoscopic displays.. I've used ARB assembly and GLSL to do it in other applications before but I'm not very familiar with the one used here (HLSL I believe?) I haven't done any DirectX programming before so I'm not familiar with the settings/API control and whatnot.

I've been testing it on Unreal.. and I can't seem to wrap my head about why sometimes it crashes and sometimes it doesn't. Sometimes I just take remove a simple operation like - 1.0 and all the sudden it works again, but when I add it back it crashes. Maybe I'm doing something terribly wrong.

Another thing is, how do I enable per-pixel calculation? Because for auto-stereoscopic displays the final output out of this shader cannot be scaled or anything or else it won't align with the attenuator (lenticular in this case). In Unreal when it goes into full screen the pixels seem scaled. When I go into window mode it looks a bit better but it's still not right as the pixels scale according to the viewport size. So how do I make sure that the pixel that is being processed in the shader is the one that's going to be in the exact same spot on the screen? Is there something similar to gl_FragCoord from GLSL in HLSL?

Code is below .. basically it finds the view this particular fragment's R component should be based on the coordinate of the pixel, then figures out the G and B, then samples. My auto-stereoscopic TV is 8 views, but for test case I just used the right view to fill in for views 1-4 and left view to fill in for views 5-8 which should still work just for testing purposes.

Code: Select all

	float2 fragCoord;

	fragCoord.x = Tex.x * viewWidth;
	fragCoord.y = Tex.y * viewHeight;

	int r_view; 
	int g_view;
	int b_view;
	r_view = int(t_view);
	g_view = r_view + 1;
	b_view = g_view + 1;
	if (r_view > 7)
	{
		r_view = r_view - 8;
	}
	if (g_view > 7)
	{
		g_view = g_view - 8;
	}
	if (b_view > 7)
	{
		b_view = b_view - 8;
	}

	float4 r;
	float4 g;
	float4 b;

	if (r_view < 4)
	{
		r = tex2D(TexMap0, Tex);
	else 
	{
		r = tex2D(TexMap1, Tex);
	}
	if (g_view < 4)
	{
		g = tex2D(TexMap0, Tex);
	else 
	{
		g = tex2D(TexMap1, Tex);
	}
	if (b_view < 4)
	{
		b = tex2D(TexMap0, Tex);
	else 
	{
		b = tex2D(TexMap1, Tex);
	}

	float4 tColor = float4(r[0], g[1], b[2], r[3]);

	return tColor;
If anybody has some insight into this, that'd be great. Thanks for your time!
Last edited by moddy3d on Sat Aug 03, 2013 4:24 am, edited 1 time in total.
User avatar
cybereality
3D Angel Eyes (Moderator)
Posts: 11407
Joined: Sat Apr 12, 2008 8:18 pm

Re: Some trouble with shaders and pixel / sub-pixel alignmen

Post by cybereality »

The resolution used by the pixel shader will be whatever resolution you have set in the game. You should always be using the native resolution of the panel to get the best possible quality. The shader will also use this same setting. Most likely the in-game resolution has defaulted to something other than the Windows resolution.

Honestly not sure what is wrong with the shader. Maybe you are using too many operations. You could try simplifying the code somewhat or using a newer shader model that allows more instructions (SM 3.0 should be enough).
moddy3d
One Eyed Hopeful
Posts: 2
Joined: Sat May 25, 2013 9:35 am

Re: Some trouble with shaders and pixel / sub-pixel alignmen

Post by moddy3d »

Alright, thanks for the heads up on the resolution issue.

I tried using Shader Model 3.0 and after some tweaking of the code it's running, but it's still not looking right. May have to look deeper into what's going on with the big picture.

Do you have a good learning resources to get familiar with DirectX programming and this field of DirectX Hooking? I think this project is awesome and would like to contribute it when I start to understand what's going on.

Thanks again!
Post Reply

Return to “Development / General Discussion”