• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
stoneMcClane

HLSL Line geometry shader

2 posts in this topic

[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
0

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
0

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  
Followers 0