Appendix

The HLSL appendix lists the reserved keywords, preprocessor directives, and the grammar for the language.

Keywords

Keywords are predefined reserved identifiers that have special meanings. They cannot be used as identifiers in your program.

asm1 asm_fragment bool column_major compile
compile_fragment const discard decl1 do
double else extern false float
for half if in inline
inout int matrix out pass1
pixelfragment return register row_major sampler
sampler1D sampler2D sampler3D samplerCUBE sampler_state
shared stateblock stateblock_state static string
struct technique1 texture texture1D texture2D
texture3D textureCUBE true typedef uniform
vector vertexfragment void volatile while
  1. These keywords are not case sensitive.

Reserved Words

The following keywords are unused, but are reserved.

auto break case catch
char class const_cast continue
default delete dynamic_cast enum
explicit friend goto long
mutable namespace new operator
private protected public reinterpret_cast
short signed sizeof static_cast
switch template this throw
try typename union unsigned
using virtual

Preprocessor Operator

The following preprocessor operator is recognized:

defined

Refer to the preprocessor operator reference in the C++ Preprocessor Operator documentation for a full description.

Preprocessor Directives

The preprocessor recognizes the following directives. These are all implemented to be compatible with the Microsoft Visual C++ preprocessor, except as noted below. Please refer to the preprocessor reference in the C++ Language Reference documentation for a full description of these directives.

#define #elif #else #endif
#error #if #ifdef #ifndef
#include #line #pragma #undef

The #include directive is valid only when compiling from a file or when the ID3DXInclude handler is used, and supports both of the following syntax alternatives:

When the include handler is not used, the local path is assumed.

Pragmas

Three pragma operators are supported:

pack_matrix warning def

The pack_matrix pragma specifies how matrices are packed by the compiler:

#pragma pack_matrix (row_major)
#pragma pack_matrix (column_major)

The #pragma warning directive allows modification of the behavior of compiler warning messages. The warning pragma supports the following specifiers:

For example:

#pragma warning( disable : 4507 34 )  // Disable warning messages
                                      //  4507 and 34.
#pragma warning( once : 4385 )        // Issue warning 4385
                                      //  only once.
#pragma warning( error : 164 )        // Report warning 164
                                      //  as an error.

The def pragma conveys literal hints to the compiler. For example, the following pragma indicates to the compiler that c10 contains literal values (1, 2, 3, 4) when compiling to the vs_2_0 profile. As it's an optimization hint, using it or not is up to the compiler.

#pragma def(vs_2_0, c10, 1, 2, 3, 4)

The following are the shader profiles for the def pragma:

vs vs_1_1 vs_2_0 vs_2_a
ps ps_1_1 ps_1_2 ps_1_3 ps_1_4 ps_2_0 ps_2_a

Note that the def pragma only supports float constant register (c#).

The following tokens are automatically defined:

#define D3DX
#define D3DX_VERSION 0x0901
#define DIRECT3D
#define DIRECT3D_VERSION 0x0900
#define __FILE__
#define __LINE__

For more information, see C++ Language - pragma.

Lexical Conventions

Lexical conventions for the language are as follows.

Whitespace

The following characters are recognized as white space.

SPACE
TAB
EOL
C style comments (/* */)
C++ style comments (//)
Assembly style comments in asm blocks (;)

Floating point numbers

Floating point numbers are represented in HLSL as follows:

Integer numbers

Integer numbers are represented in HLSL as follows:

Characters

Characters are represented in HLSL as follows:

'c' (character)
'\a' '\b' '\f' '\b' '\r' '\t' '\v' (escapes)
'\###' (octal escape, each # is an octal digit)
'\x#' (hex escape, # is hex number, any number of digits)
'\c' (c is other character, including backslash and quotation marks)

Escapes are not supported in preprocessor expressions.

Strings

Strings are represented in HLSL as follows:

"s" (s is any string with escapes).

Identifiers

Identifiers are represented in HLSL as follows:

	[A-Za-z_][A-Za-z0-9_]*

Operators

##, #@, ++, --, &, &, &, ||, ==, ::, <<, <<=, >>, >>=, ..., 
<=, >=, !=, *=, /=, +=, -=, %=, &=, |=, ^=, ->

Also any other single character that did not match another rule.

Language Syntax

The following is a listing of the language constructs, from which the HLSL statements are constructed.

Program :
Program : Decls

Decls : Decl
Decls : Decls Decl
Decl : ';'
Decl : TypeDecl
Decl : VariableDecl
Decl : VarStructDecl
Decl : FunctionDecl
Decl : TechniqueDecl

Usages : Usage
Usages : Usages Usage
Usage : T_KW_STATIC
Usage : T_KW_UNIFORM
Usage : T_KW_EXTERN
Usage : T_KW_VOLATILE
Usage : T_KW_INLINE
Usage : T_KW_SHARED
Usage : ConstUsage
Usage : Target

ConstUsages : ConstUsage
ConstUsages : ConstUsages ConstUsage
ConstUsage : T_KW_CONST
ConstUsage : T_KW_ROW_MAJOR
ConstUsage : T_KW_COLUMN_MAJOR

UsageType : Type
UsageType : Usages Type

UsageStructDecl : StructDecl
UsageStructDecl : Usages StructDecl

TypeDecl : T_KW_TYPEDEF ConstType TypeDefs ';'
TypeDecl : T_KW_TYPEDEF ConstStructDecl TypeDefs ';'
TypeDecl : StructDecl ';'
TypeDefs : VariableDim
TypeDefs : VariableDim ',' TypeDefs

Type : BaseType
Type : Struct
Type : TypeId
Type : T_KW_STRUCT TypeId

ConstType : Type
ConstType : ConstUsages Type
ConstTypeDim : ConstType
ConstTypeDim : ConstTypeDim '[' ConstantExpr ']'

BaseType : T_KW_VOID
BaseType : ScalarType
BaseType : VectorType
BaseType : MatrixType
BaseType : ObjectType

ScalarType : T_KW_BOOL
ScalarType : T_KW_INT
ScalarType : T_KW_HALF
ScalarType : T_KW_FLOAT
ScalarType : T_KW_DOUBLE

VectorType : T_KW_VECTOR
VectorType : T_KW_VECTOR '<' ScalarType ',' AddExpr '>'

MatrixType : T_KW_MATRIX
MatrixType : T_KW_MATRIX '<' ScalarType ',' ConstantExpr ',' AddExpr '>'

ObjectType : T_KW_STRING
ObjectType : T_KW_TEXTURE
ObjectType : T_KW_TEXTURE1D
ObjectType : T_KW_TEXTURE2D
ObjectType : T_KW_TEXTURE3D
ObjectType : T_KW_TEXTURECUBE
ObjectType : T_KW_SAMPLER
ObjectType : T_KW_SAMPLER1D
ObjectType : T_KW_SAMPLER2D
ObjectType : T_KW_SAMPLER3D
ObjectType : T_KW_SAMPLERCUBE
ObjectType : T_KW_PIXELSHADER
ObjectType : T_KW_VERTEXSHADER
ObjectType : T_KW_PIXELFRAGMENT
ObjectType : T_KW_VERTEXFRAGMENT
ObjectType : T_KW_STATEBLOCK

Struct : T_KW_STRUCT StructBegin StructEnd
Struct : T_KW_STRUCT StructBegin StructDecls StructEnd
StructDecl : T_KW_STRUCT Id StructBegin StructEnd
StructDecl : T_KW_STRUCT Id StructBegin StructDecls StructEnd

ConstStructDecl : StructDecl
ConstStructDecl : ConstUsages StructDecl

StructBegin : '{'
StructDecls : VariableDecl
StructDecls : VariableDecl StructDecls
StructEnd : '}'

Semantic : ':' Id
Semantic : ':' T_KW_REGISTER '(' Register ')'
Semantics : Semantic
Semantics : Semantics Semantic
SemanticsOpt :
SemanticsOpt : Semantics

Register : Id
Register : Target ',' Id

Annotation : AnnotationBegin AnnotationEnd
Annotation : AnnotationBegin AnnotationDecls AnnotationEnd
AnnotationOpt :
AnnotationOpt : Annotation
AnnotationBegin : '<'
AnnotationDecls : VariableDecl
AnnotationDecls : VariableDecl AnnotationDecls
AnnotationEnd : '>'

Initializer : '=' AssignmentExpr
Initializer : '=' '{' InitExprs '}'
Initializer : '=' '{' InitExprs ',' '}'
InitializerOpt :
InitializerOpt : Initializer

VariableDecl : UsageType Variables ';'
VarStructDecl : UsageStructDecl Variables ';'
Variables : Variable
Variables : Variables ',' Variable
Variable : VariableDim SemanticsOpt AnnotationOpt InitializerOpt
VariableDim : Id
VariableDim : VariableDim '[' ']'
VariableDim : VariableDim '[' ConstantExpr ']'

FunctionDecl : FunctionDef ';'
FunctionDecl : FunctionDef AnnotationOpt StmtBlock
FunctionDef : UsageType Id ParamList SemanticsOpt

ParamList : ParamListBegin ParamListEnd
ParamList : ParamListBegin T_KW_VOID ParamListEnd
ParamList : ParamListBegin ParameterDecls ParamListEnd
ParamListBegin : '('
ParamListEnd : ')'

ParameterDecls : ParameterDecl
ParameterDecls : ParameterDecls ',' ParameterDecl
ParameterDecl : ParamUsageType Variable

ParamUsageType : Type
ParamUsageType : ParamUsages Type

ParamUsages : ParamUsage
ParamUsages : ParamUsages ParamUsage
ParamUsage : T_KW_IN
ParamUsage : T_KW_OUT
ParamUsage : T_KW_INOUT
ParamUsage : T_KW_UNIFORM
ParamUsage : ConstUsage
TechniqueDecl : T_KW_TECHNIQUE IdOpt AnnotationOpt TechniqueBody
TechniqueBody : TechniqueBegin TechniqueEnd
TechniqueBody : TechniqueBegin PassDecls TechniqueEnd
TechniqueBegin : '{'
TechniqueEnd : '}'

PassDecls : PassDecl
PassDecls : PassDecls PassDecl
PassDecl : T_KW_PASS IdOpt AnnotationOpt StateBlock

StateBlock : StateBlockBegin StateBlockEnd
StateBlock : StateBlockBegin States StateBlockEnd
StateBlockBegin : '{'
StateBlockEnd : '}'

States : State
States : States State
State : Id StateIndex StateExprBegin StateExpr StateExprEnd

StateIndex :
StateIndex : '[' Uint ']'
StateExprBegin : '='
StateExprEnd : ';'

StmtBlock : StmtBlockBegin StmtBlockEnd
StmtBlock : StmtBlockBegin Stmts StmtBlockEnd
StmtBlockBegin : '{'
StmtBlockEnd : '}'
Stmts : Stmt
Stmts : Stmts Stmt
SimpleStmt : ';'
SimpleStmt : Expr ';'
SimpleStmt : T_KW_RETURN ';'
SimpleStmt : T_KW_RETURN Expr ';'
SimpleStmt : T_KW_DO Stmt T_KW_WHILE '(' Expr ')' ';'
SimpleStmt : StmtBlock
SimpleStmt : T_KW_DISCARD ';'
SimpleStmt : TypeDecl
SimpleStmt : VariableDecl
SimpleStmt : VarStructDecl

NonIfStmt : SimpleStmt
NonIfStmt : T_KW_WHILE '(' Expr ')' NonIfStmt
NonIfStmt : For '(' ForInit ForCond ForStep ')' NonIfStmt

Stmt : SimpleStmt
Stmt : T_KW_WHILE '(' Expr ')' Stmt
Stmt : For '(' ForInit ForCond ForStep ')' Stmt
Stmt : T_KW_IF '(' Expr ')' Stmt
Stmt : T_KW_IF '(' Expr ')' NonIfStmt T_KW_ELSE Stmt

For : T_KW_FOR
ForInit : ';'
ForInit : Expr ';'
ForInit : VariableDecl
ForCond : ';'
ForCond : Expr ';'
ForStep :
ForStep : Expr

DwordExpr : Dword
DwordExpr : Dword '|' DwordExpr

StateExpr : DwordExpr
StateExpr : ComplexExpr
StateExpr : '{' InitExprs '}'
StateExpr : '{' InitExprs ',' '}'
StateExpr : '<' RelationalExpr '>'

SimpleExpr : T_KW_TRUE
SimpleExpr : T_KW_FALSE
SimpleExpr : Uint
SimpleExpr : Float
SimpleExpr : Strings
SimpleExpr : NonTypeId

ComplexExpr : '(' Expr ')'
ComplexExpr : TypeId '(' ArgumentsOpt ')'
ComplexExpr : BaseType '(' ArgumentsOpt ')'
ComplexExpr : NonTypeId '(' ArgumentsOpt ')'
ComplexExpr : ObjectExpr

ObjectExpr : AsmDecl
ObjectExpr : AsmDecl Asm
ObjectExpr : Asm
ObjectExpr : AsmFragment
ObjectExpr : T_KW_COMPILE Target NonTypeId '(' ArgumentsOpt ')'
ObjectExpr : T_KW_SAMPLER_STATE StateBlock
ObjectExpr : T_KW_COMPILE_FRAGMENT Target NonTypeId '(' ArgumentsOpt ')'
ObjectExpr : T_KW_STATEBLOCK_STATE StateBlock

PrimaryExpr : SimpleExpr
PrimaryExpr : ComplexExpr
PostfixExpr : PrimaryExpr
PostfixExpr : PostfixExpr '[' Expr ']'
PostfixExpr : PostfixExpr '.' Id
PostfixExpr : PostfixExpr T_OP_INC
PostfixExpr : PostfixExpr T_OP_DEC

UnaryExpr : PostfixExpr
UnaryExpr : T_OP_INC UnaryExpr
UnaryExpr : T_OP_DEC UnaryExpr
UnaryExpr : '!' CastExpr
UnaryExpr : '-' CastExpr
UnaryExpr : '+' CastExpr

CastExpr : UnaryExpr
CastExpr : '(' ConstTypeDim ')' CastExpr

MulExpr : CastExpr
MulExpr : MulExpr '*' CastExpr
MulExpr : MulExpr '/' CastExpr
MulExpr : MulExpr '%' CastExpr

AddExpr : MulExpr
AddExpr : AddExpr '+' MulExpr
AddExpr : AddExpr '-' MulExpr

RelationalExpr : AddExpr
RelationalExpr : RelationalExpr '<' AddExpr
RelationalExpr : RelationalExpr '>' AddExpr
RelationalExpr : RelationalExpr T_OP_LE AddExpr
RelationalExpr : RelationalExpr T_OP_GE AddExpr

EqualityExpr : RelationalExpr
EqualityExpr : EqualityExpr T_OP_EQ RelationalExpr
EqualityExpr : EqualityExpr T_OP_NE RelationalExpr

AndExpr : EqualityExpr
AndExpr : AndExpr T_OP_AND EqualityExpr
OrExpr : AndExpr
OrExpr : OrExpr T_OP_OR AndExpr
ConditionalExpr : OrExpr
ConditionalExpr : OrExpr '?' AssignmentExpr ':' ConditionalExpr

AssignmentExpr : ConditionalExpr
AssignmentExpr : CastExpr '=' AssignmentExpr
AssignmentExpr : CastExpr T_OP_ME AssignmentExpr
AssignmentExpr : CastExpr T_OP_DE AssignmentExpr
AssignmentExpr : CastExpr T_OP_RE AssignmentExpr
AssignmentExpr : CastExpr T_OP_AE AssignmentExpr
AssignmentExpr : CastExpr T_OP_SE AssignmentExpr

Arguments : AssignmentExpr
Arguments : Arguments ',' AssignmentExpr
ArgumentsOpt :
ArgumentsOpt : Arguments

InitExpr : AssignmentExpr
InitExpr : '{' InitExprs '}'
InitExpr : '{' InitExprs ',' '}'
InitExprs : InitExpr
InitExprs : InitExprs ',' InitExpr

ConstantExpr : AssignmentExpr
Expr : AssignmentExpr
Expr : Expr ',' AssignmentExpr

Dword : Uint
Dword : '-' Uint
Dword : Float
Dword : '-' Float
Dword : DwordId
Dword : Uint DwordId
DwordId : Id
DwordId : T_KW_TRUE
DwordId : T_KW_FALSE
DwordId : T_KW_TEXTURE

Id : TypeId
Id : NonTypeId
IdOpt :
IdOpt : Id

Target : NonTypeId

Uint : T_UINT
Uint : T_INT32
Uint : T_UINT32

Float : T_FLOAT
Float : T_FLOAT16
Float : T_FLOAT32
Float : T_FLOAT64

Strings : String
Strings : Strings String
String : T_STRING

TypeId : T_TYPE_ID
NonTypeId : T_NON_TYPE_ID

AsmDecl : T_KW_DECL '{'
Asm : T_KW_ASM '{'
AsmFragment : T_KW_ASM_FRAGMENT '{'