# Colons, semi colons…

### HLSL coding style

If you know your way around any mature language, you’re half way there to understanding how to read HLSL (sorry python users...).

Instead of a long, tedious article, here is a summary of the most important points. if you're not satisfied, here is a link to MS official page:

### Variables

Declaring, initializing and using variables is very much the same as anywhere:

```float fVar;			//declare a float called fVar
float fVar = 3.1f;		// initialize with value of 3.1
int iVar[3];			// declare array of 3 integers
int iVar[3] = {1,2,3};		// declare int array and initialize```

### Matrices and vectors

Since matrices are used very often in shaders, HLSL (as do GLSL and CG) have a “shortcut” to creating them:

important note : both vectors and matrices are limited to 4 members ( or 4 on 4 in matrix case)

```Vector a;                        			// a vector…
float4 color;                    			// a vector called color with 4 float members
float4 lightDirection = {0,0,1};			// a vector with initialization
float4 diffuseMaterial = float4(0.5, 0.5, 1.0, 1.0); 	// vector initialized using constructor
float4x4 mTot;                   			// declaration of a 4 on 4 matrix
// matrices don’t have to be symmetrical```

### Structs

If you know structs in any other language, you know them in HLSL.

```// a struct with two members

struct VS_OUTPUT
{
float4 pos;
float4 diff;
};
//another note: the semi-colons at the end of the definition are MANDATORY (if you forgot)```

### Functions

Same as everywhere:

```//A function called VS_func, returns a VS_OUTPUT (struct)
// receives an object of VS_INPUT (another struct)
// and a vector called direction

VS_OUTPUT VS_func (const VS_INPUT input, float3 direction)
{
VS_OUTPUT output;
return output;
}

// a different type of function declaration, which we will discuss later
Float4 VS_func (const VS_INPUT input) : POSITION
{
return input.position;
}```

### Flow control

HLSL has the same flow control syntax as C;

### Techniques

If you already know what techniques and passes are in ogre scripts, this will not be foreign.

If you don’t, jump.

```technique Technique1
{
pass Pass1
{
// [program type] = compile [profile] [function-name];
}
}```

### SOME-THINGS-WITH-CAPSLOCK-THAT-COMES-AFTER-COLONS

…Or in professional terms: semantics

such as:

`float4	pos	:	POSITION;`

Semantics are a somewhat unique feature of shading languages. They describe the purpose/role of the parameter/vector/matrix. They are the first comprehension barrier of understanding HLSL code.

Remember the remark: "some things are done behind the curtain"? Semantics are the key. You define the role; the GPU does the rest.

129 online users