Simple HLSL question

Started by
2 comments, last by jollyjeffers 17 years, 6 months ago
Hi, I've got a pixel shader that does Gaussian blur. Now it looks like this:

float4 gaussOffsets[5]; // passed to the shader

float4 PS_5x5GaussianHorizontalPass( VSOutput input ) : COLOR
{    
  float gauss = 0.0f;  
  for( int i = 0; i < 5; i++ )
    gauss += tex2D( gaussTextureSampler, input.TexCoords + float2( gaussOffsets.r, 0.0f )).r * gaussOffsets.g;
  return float4(gauss, 0.0f, 0.0f, 0.0f);
}

technique Gaussian5x5Horizontal
{
  pass P0
  {
    VertexShader = null;
    PixelShader  = compile ps_2_0 PS_5x5GaussianHorizontalPass();
  }
}

Now I need a separate pixel shader and separate technique for every kernel size (there will be 7 different sizes). How to do it, so I could have just one pixel shader, many techniques and just pass necessary parameters (like kernel size, offsets and weights) to the pixel shader? If anyone could post a code for a pixel shader and single technique with parameters passed, I'd be very grateful.
Advertisement
In some of the SDK examples they use multiple techniques, one of which you select when making use of the shader. Each technique could use the same vertex shader but have a different function in the file for the pixel shader.

I suggest you open up the projects in the SDK and take a peek. I think the HLSL Workshop example has what you require.

Dave
Thanks.
I get the idea of multiple techniques, but I've got some problems with implementing what I need.

Here's what I got now:
float4 offsets[27]; // 27 is the maximum kernel sizefloat4 PS_GaussianHorizontal( VSOutput input, int samples, float4 offsets[27] ){  float gauss= 0.0f;  for( int i = 0; i < samples; i++ )    gauss += tex2D( gaussTextureSampler, input.TexCoords + float2(offsets.r, 0.0f)).r * offsets.b;  return float4(gauss , 0.0f, 0.0f, 0.0f);}technique Gaussian5x5HorizontalPass{  Pass P0  {    VertexShader = null;    PixelShader  = compile ps_3_0 PS_GaussianHorizontal(5, offsets);  }}


And I get the following error:

error X3013: 'PS_GaussianHorizontal': function does not take 2 parameters

I don't need to pass output of the vertex shader, so the pixel shader does take 2 parameters. Probably I'm incorrectly passing offsets array, but I don't know how it should look like.
Try making use of the uniform keyword, I don't think the compilation stage will take dynamic/variable parameters (although regular HLSL "functions" will). The uniform keyword is analagous to a constant parameter (like you get in other languages) and thus the compile statement can hardcode the combination.

I make extensive use of uniform parameters in order to generate multiple shaders from the same/common HLSL code. You'll find lots of effects in the SDK and other samples do the same [smile]

hth
Jack

<hr align="left" width="25%" />
Jack Hoxley <small>[</small><small> Forum FAQ | Revised FAQ | MVP Profile | Developer Journal ]</small>

This topic is closed to new replies.

Advertisement