Surface Shaders with DX11 / OpenGL Core Tessellation
Vertex and fragment shader examples
Writing vertex and fragment shaders
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.
If you want to write shaders that interact with lighting, take a look at Surface Shaders documentation. For some examples, take a look at Vertex and Fragment Shader Examples. The rest of this page assumes shaders do not interact with Unity lights (for example special effects, post-processed effects etc.)
Shader programs are written in HLSL language, by embedding “snippets” in the shader text, somewhere inside the Pass command. They usually look like this:
Pass {
// ... the usual pass state setup ...
CGPROGRAM
// compilation directives for this snippet, e.g.:
#pragma vertex vert
#pragma fragment frag
// the Cg/HLSL code itself
ENDCG
// ... the rest of pass setup ...
}
HLSL snippets
HLSL program snippets are written between CGPROGRAM and ENDCG keywords, or alternatively between HLSLPROGRAM and ENDHLSL. The latter form does not automatically include HLSLSupport and UnityShaderVariables built-in header files.
At the start of the snippet compilation directives can be given as #pragma statements. Directives indicating which shader functions to compile:
-
#pragma vertex name - compile function name as the vertex shader.
-
#pragma fragment name - compile function name as the fragment shader.
-
#pragma geometry name - compile function name as DX10 geometry shader. Having this option automatically turns on #pragma target 4.0, described below.
-
#pragma hull name - compile function name as DX11 hull shader. Having this option automatically turns on #pragma target 5.0, described below.
-
#pragma domain name - compile function name as DX11 domain shader. Having this option automatically turns on #pragma target 5.0, described below.
Other compilation directives:
-
#pragma target name - which shader target to compile to. See Shader Compilation Targets page for details.
-
#pragma only_renderers space separated names - compile shader only for given renderers. By default shaders are compiled for all renderers. See Renderers below for details.
-
#pragma exclude_renderers space separated names - do not compile shader for given renderers. By default shaders are compiled for all renderers. See Renderers below for details.
-
#pragma multi_compile … - for working with multiple shader variants.
-
#pragma enable_d3d11_debug_symbols - generate debug information for shaders compiled for DirectX 11, this will allow you to debug shaders via Visual Studio 2012 (or higher) Graphics debugger.
-
#pragma hardware_tier_variants renderer name - generate multiple shader hardware variants of each compiled shader, for each hardware tier that could run the selected renderer. See Renderers below for details.
Each snippet must contain at least a vertex program and a fragment program. Thus #pragma vertex and #pragma fragment directives are required.
Compilation directives that don’t do anything starting with Unity 5.0 and can be safely removed: #pragma glsl
, #pragma glsl_no_auto_normalization
, #pragma profileoption
, #pragma fragmentoption
.
Rendering platforms
Unity supports several rendering APIs (e.g. Direct3D 11 and OpenGL), and by default all shader programs are compiled into all supported renderers. You can indicate which renderers to compile to using #pragma only_renderers or #pragma exclude_renderers directives. This is mostly useful in cases where you are explicitly using some shader language features that you know aren’t possible on some platforms. Supported renderer names are:
-
d3d11 - Direct3D 11/12
-
glcore - OpenGL 3.x/4.x
-
gles - OpenGL ES 2.0
-
gles3 - OpenGL ES 3.x
-
metal - iOS/Mac Metal
-
vulkan - Vulkan
-
d3d11_9x - Direct3D 11 9.x feature level, as commonly used on WSA platforms
-
xboxone - Xbox One
-
ps4 - PlayStation 4
-
psp2 - PlayStation Vita
-
n3ds - Nintendo 3DS
-
wiiu - Nintendo Wii U
For example, this line would only compile shader into D3D11 mode:
#pragma only_renderers d3d11
See Also
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Is something described here not working as you expect it to? It might be a Known Issue. Please check with the Issue Tracker at issuetracker.unity3d.com.
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thanks for helping to make the Unity documentation better!
Surface Shaders with DX11 / OpenGL Core Tessellation
Vertex and fragment shader examples