Skip navigation

HLSL starter shader

A little background. I find the shader that gets filled in automatically when you .Add new Effect.fx file in XNA 3.0 a little annoying.

For starters, its too long and has too many silly comments that mean nothing to someone who doesn’t know what they’re doing, and are redundant and useless to someone who knows what they’re doing. So the first thing I do is strip them out.

Next, I don’t like/approve of the use of STRUCTS in a shader. Its annoying and bulks up the code. Observe what I mean below. Instead of structs and return values, I’m attaching keyword out to the output parameters. This works perfectly well and doesn’t have any problems or difference from using structs – only it appears cleaner – to me.

float4x4 World;
float4x4 View;
float4x4 Projection;



void VertexShaderFunction(

  float4 Position : POSITION0,
  float4 Color : COLOR0,

  // Outputs (feed directly into the PixelShaderFunction)
  out float4 oPosition : POSITION0,
  out float4 oColor : COLOR0
)
{
  // See http://http.developer.nvidia.com/CgTutorial/cg_tutorial_chapter04.html
  
  // Take vertex to world space
  float4 worldPosition = mul( Position, World );
  
  // Take vertex to eye-space
  float4 viewPosition  = mul( worldPosition, View );
  
  // Take vertex to clip-space
  oPosition            = mul( viewPosition, Projection );

  // Simply pass through the color.  If we were using lighting,
  // the color would be affected by the light.
  oColor = Color ;
}



void PixelShaderFunction(
  
  float4 Color : COLOR0,
 
  out float4 oColor : COLOR0
  
)
{
  // just pass through the color as calculated
  // by the vertex shader
  oColor = Color ;
}



technique Technique1
{
  pass Pass1
  {
    VertexShader = compile vs_3_0 VertexShaderFunction();
    PixelShader = compile ps_3_0 PixelShaderFunction();
  }
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: