Writing shader programs
Reference Manual > ShaderLab Reference > Writing shader programs

Writing shader programs

ShaderLab shaders encompass more than just "hardware shaders". They do many things. They describe properties that are displayed in the Material Inspector, contain multiple shader implementations for different graphics hardware, configure fixed function hardware state and so on. The actual programmable shaders - like vertex and fragment programs - are just a part of the whole ShaderLab's "shader" concept. Take a look at shader tutorial for a basic introduction. Here we'll call the low-level hardware shaders shader programs.

Shader programs are written in either Cg or GLSL language, by embedding "snippets" in the shader text, somewhere inside the Pass command. They usually look like this:

  Pass {
      // ... the usual pass state setup ...

      // compilation directives for this snippet, e.g.:
      #pragma vertex vert
      #pragma fragment frag

      // the Cg code itself

      // ... the rest of pass setup ...

Cg snippets

Cg program snippets are written between CGPROGRAM and ENDCG.

At the start of the snippet compilation directives can be given as #pragma statements. Directives recognized by Unity are:

Each snippet must contain a vertex program, a fragment program, or both. Thus a #pragma vertex or #pragma fragment directive is required, or both.

Shader targets

By default, Unity compiles vertex programs into a shader model 1.1 equivalent, and fragment programs into a shader model 2.0 equivalent. Using #pragma target allows shaders to be compiled into other capability levels. Currently these targets are supported:


Unity supports several rendering APIs (e.g. Direct3D 9 and OpenGL), and by default all shader programs are compiled into for supported renderers. You can indicate which renderers to compile to using #pragma only_renderers or #pragma exclude_renderers directives. This is useful if you know you will only target Mac OS X (where there's no Direct3D), or only Windows (where Unity defaults to D3D), or if some particular shader is only possible in one renderer and not others. Currently supported renderer names are:

For example, this line would only compile shader into D3D9 mode:

  #pragma only_renderers d3d9

GLSL snippets

Use of GLSL shader programs is only recommended for testing. GLSL language is specific to OpenGL, whereas using Cg will compile shaders to both Direct3D and OpenGL.

See GLSL Shader Programs page for details.