Function Declaration Syntax

HLSL functions are declared with the following syntax.

[StorageClass] [clipplanes()] [precise] Return_Value Name ( [ArgumentList] ) [: Semantic] {   [StatementBlock] };

 

Parameters

StorageClass

Modifier that redefines a function declaration. inline is currently the only modifier value. The modifier value must be inline because it is also the default value. Therefore, a function is inline regardless of whether you specify inline, and all functions in HLSL are inline. An inline function generates a copy of the function body (when compiling) for each function call. This is done to decrease the overhead of calling the function.

Clipplanes

Optional list of clip planes, which is up to 6 user-specified clip planes. This is an alternate mechanism for SV_ClipDistance that works on feature level 9_x and higher.

Name

An ASCII string that uniquely identifies the name of the shader function.

ArgumentList

Optional argument list, which is a comma-separated list of arguments passed into a function.

Semantic

Optional string that identifies the intended usage of the return data (see Semantics (DirectX HLSL)).

StatementBlock

Optional statements that make up the body of the function. A function defined without a body is called a function prototype; the body of a prototype function must be defined elsewhere before the function can be called.

Return Value

The return type can be any one of these HLSL types.

Remarks

The syntax on this page describes almost every type of HLSL function, this includes vertex shaders, pixel shaders, and helper functions. While a geometry shader is also implemented with a function, its syntax is a little more complicated, so there is a separate page that defines a geometry shader function declaration (see Geometry-Shader Object (DirectX HLSL)).

A function can be overloaded as long as it is given a unique combination of parameter types and/or parameter order. HLSL also implements a number of built in, or intrinsic functions.

You can specify user-specific clip planes with the clipplanes attribute. Windows applies these clip planes to all of the primitives drawn. The clipplanes attribute works like SV_ClipDistance but works on all hardware feature level 9_x and higher. For more info, see User clip planes on feature level 9 hardware.

Examples

This example is from BasicHLSL10.fx from the BasicHLSL10 Sample.

struct VS_OUTPUT
{
    float4 Position   : SV_POSITION; 
    float4 Diffuse    : COLOR0;
    float2 TextureUV  : TEXCOORD0;
};

VS_OUTPUT RenderSceneVS( float4 vPos : POSITION,
                         float3 vNormal : NORMAL,
                         float2 vTexCoord0 : TEXCOORD,
                         uniform int nNumLights,
                         uniform bool bTexture,
                         uniform bool bAnimate )
{
    VS_OUTPUT Output;
    ...
    return Output;    
}

This example from AdvancedParticles.fx from the AdvancedParticles Sample, illustrates using a semantic for the return type.

//
// PS for particles
//
float4 PSPointSprite(PSSceneIn input) : SV_Target
{   
    return g_txDiffuse.Sample( g_samLinear, input.tex ) * input.color;
}

Functions (DirectX HLSL)