HLSL Introduction
High Level Shading Language (HLSL) is Microsoft's proprietary shading language for the Direct3D API. It allows developers to write programmable shaders that run on the GPU, enabling complex visual effects, realistic lighting, and custom rendering pipelines.
Getting Started
To use HLSL you need:
- Windows 7 or later
- Direct3D 11 or newer
- A supported graphics driver
Typical workflow:
- Write shader code (*.hlsl)
- Compile with
fxcor at runtime withD3DCompile - Create shader objects (vertex, pixel, geometry, etc.)
- Set shaders on the device context
- Render geometry
Basic Data Types
// Scalar types
float // 32‑bit floating point
int // 32‑bit signed integer
uint // 32‑bit unsigned integer
bool // Boolean
// Vector types
float2, float3, float4
int2, int3, int4
uint2, uint3, uint4
bool2, bool3, bool4
// Matrix types
float2x2, float3x3, float4x4
// Sampler
SamplerState sampler0;
Texture2D tex0;
Simple Vertex Shader Example
// SimplePassThroughVS.hlsl
cbuffer Transform : register(b0)
{
float4x4 worldViewProj;
};
struct VS_IN
{
float3 pos : POSITION;
float3 norm : NORMAL;
};
struct VS_OUT
{
float4 pos : SV_POSITION;
float3 norm : NORMAL;
};
VS_OUT main(VS_IN input)
{
VS_OUT output;
output.pos = mul(worldViewProj, float4(input.pos, 1.0));
output.norm = normalize(input.norm);
return output;
}
Discussion