Sign in to follow this  
stoneMcClane

HLSL Line geometry shader

Recommended Posts

[font=arial, verdana, tahoma, sans-serif][size=2]


Hi,[/size][/font]

I need to render lines with adjustable width in DX10 so I went ahead and thought this would be a good time to try out geometry shaders (for the first time), and I came up with the following shader which does essentially this:

Application:
* draws lines with LineList or LineStrip topology and my shader enabled

VertexShader:
* just transforms vertices to non-homogenous screen-space

GeometryShader:
* constructs a 4-vertex quad per line primitive
* vertex positions are calculated based on the line direction / normal and the given line width (in screen-space)
* extends the vertex positions in the line direction so the anchor points between two connected lines overlap

PixelShader:
* just outputs a simple color for now

That's the shader code...

[code]

//#include "Include/ShaderStates.fx"

Texture2D LineTexture;

//-----------------------------------------------------------------------------
// Constant Buffers
//-----------------------------------------------------------------------------
cbuffer cbChangeRare
{
float2 RenderTargetSize;
}

cbuffer cbChangePerFrame
{
}

cbuffer cbChangePerObject
{
matrix WorldViewProjection;
}

//-----------------------------------------------------------------------------
// Shader Input / Output Structures
//-----------------------------------------------------------------------------
struct VS_INPUT
{
float4 Position : POSITION0;
};

struct GEO_IN
{
float4 Position : POSITION0;
};

struct GEO_OUT
{
float4 Position : SV_POSITION;
float2 TexCoord : TEXCOORD;
};

struct PS_OUTPUT
{
float4 Color : SV_Target0;
};

GEO_IN VS_LineV2(in VS_INPUT input)
{
GEO_IN output;
output.Position = mul(input.Position, WorldViewProjection);
//output.Position = input.Position;
return output;
}

[maxvertexcount(6)]
void GS(line GEO_IN points[2], inout TriangleStream<GEO_OUT> output)
{
float4 p0 = points[0].Position;
float4 p1 = points[1].Position;

float w0 = p0.w;
float w1 = p1.w;

p0.xyz /= p0.w;
p1.xyz /= p1.w;

float3 line01 = p1 - p0;
float3 dir = normalize(line01);

// scale to correct window aspect ratio
float3 ratio = float3(RenderTargetSize.y, RenderTargetSize.x, 0);
ratio = normalize(ratio);

float3 unit_z = normalize(float3(0, 0, -1));

float3 normal = normalize(cross(unit_z, dir) * ratio);

float width = 0.01;

GEO_OUT v[4];

float3 dir_offset = dir * ratio * width;
float3 normal_scaled = normal * ratio * width;

float3 p0_ex = p0 - dir_offset;
float3 p1_ex = p1 + dir_offset;

v[0].Position = float4(p0_ex - normal_scaled, 1) * w0;
v[0].TexCoord = float2(0,0);

v[1].Position = float4(p0_ex + normal_scaled, 1) * w0;
v[1].TexCoord = float2(0,0);

v[2].Position = float4(p1_ex + normal_scaled, 1) * w1;
v[2].TexCoord = float2(0,0);

v[3].Position = float4(p1_ex - normal_scaled, 1) * w1;
v[3].TexCoord = float2(0,0);

output.Append(v[2]);
output.Append(v[1]);
output.Append(v[0]);

output.RestartStrip();

output.Append(v[3]);
output.Append(v[2]);
output.Append(v[0]);

output.RestartStrip();
}

PS_OUTPUT PS_LineV2(GEO_OUT input)
{
PS_OUTPUT output;

//output.Color = LineTexture.Sample(LinearSampler, input.TexCoord);
//output.Color = float4(input.TexCoord.xy, 0, 1);
output.Color = float4(1, 0.5, 0, 0.5);

return output;
}

technique10 LineV2Technique
{
pass P0
{
SetVertexShader(CompileShader(vs_4_0, VS_LineV2()));
SetGeometryShader(CompileShader(gs_4_0, GS()));
SetPixelShader(CompileShader(ps_4_0, PS_LineV2()));
}
}[/code]

Now the problem with this shader is the following. If I enable alpha-blending I get a strange behaviour which seems to be related to the projection / camera view...

This is what a test using the shader looks like with alpha-blending enabled and a look direction approximately along the Z-axis (X-Axis -> red, Y-Axis -> green, Z-Axis -> blue)
[URL=http://imgur.com/B37K9][IMG]http://i.imgur.com/B37K9.png[/IMG][/URL]

If I rotate the camera to the right a little ...
[URL=http://imgur.com/vxmrG][IMG]http://i.imgur.com/vxmrG.png[/IMG][/URL]

... or to the left ...
[URL=http://imgur.com/I4Bii][IMG]http://i.imgur.com/I4Bii.png[/IMG][/URL]

Here I increased the line width and reduced the number of line elements so it's easier to see the artifacts that produce the issue...
(I also changed the pixel-shader to print the texture coordinates ... output.Color = float4(input.TexCoord.xy, 0, 0.5); ... which are in the range 0,0 to 1,0 for each line segment)
[URL=http://imgur.com/51t5F][IMG]http://i.imgur.com/51t5F.png[/IMG][/URL]

The strange thing is that the error only occurs when alpha blending is enabled, with default blending the line looks fine.

My guess is that I screw something up in the geometry shader about converting from non-homogeneous to homogeneous coordinate space or vice-versa, is the math in the geometry shader about right or total BS ? xD
I'd be very thankful for any hints from someone who has more experience with writing (geometry) shaders than me.

Thanks

Share this post


Link to post
Share on other sites
Turns out this was caused by the [url="http://msdn.microsoft.com/en-us/library/bb172408(v=vs.85).aspx"]D3D10_RASTERIZER_DESC's AntialiasedLineEnable[/url] property (which I had set to "true") ... now I have it set to "false" and it works now regardless of the blend mode I'm using (is this a driver issue, since I guess this isn't supposed to happen?!).
Anyway, I'd still love to hear suggestions if I could improve the math in my geometry shader in some way.

Cheers

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this