Shader: Difference between revisions
m (AAM represent!) |
(added detail on shader models and example shader code) |
||
Line 5: | Line 5: | ||
There are currently three main shader languages, [[HLSL]], [[CG]] and [http://en.wikipedia.org/wiki/GLSL GLSL]. The [[Source Engine]] uses [[HLSL]] based shaders, however [[CG]] is so similar that most [[CG]] shaders can be quickly and easily ported to [[HLSL]]. | There are currently three main shader languages, [[HLSL]], [[CG]] and [http://en.wikipedia.org/wiki/GLSL GLSL]. The [[Source Engine]] uses [[HLSL]] based shaders, however [[CG]] is so similar that most [[CG]] shaders can be quickly and easily ported to [[HLSL]]. | ||
For information on authoring shaders for use in the [[Source Engine]], please see [[ | There are a variety of different models of the shader system; 1.1, 1.4, 2.0 and 3.0. Shader Model (SM) 3.0 is only available on high end graphics cards such as the Nvidia 6600, 6800 and 7800 or ATI X1600, X1800 and X1900. The most common Shader Model version supported is 2.0. It is important to pay attention to the shader model being compiled with when creating shaders as older graphics cards that only support SM 1.4 will not run shaders created using SM 2.0. In this case, shader fallbacks must be specified to provide some form of support for users with slightly outdated hardware. | ||
For information on authoring shaders for use in the [[Source Engine]], please see [[Shader_Authoring]]. | |||
===Vertex Shaders=== | ===Vertex Shaders=== | ||
Vertex shaders are applied for each vertex run on a programmable pipeline. Its most basic goal is to transform geometry into screenspace coordinates so that the Pixel shader can rasterize an image. On a more complex scale, Vertex shaders are responsible for mesh deformation, lighting, shadowing and general vertex displacement. Vertex shaders cannot create vertices. | Vertex shaders are applied for each vertex run on a programmable pipeline. Its most basic goal is to transform geometry into screenspace coordinates so that the Pixel shader can rasterize an image. On a more complex scale, Vertex shaders are responsible for mesh deformation, lighting, shadowing and general vertex displacement. Vertex shaders cannot create vertices. | ||
A heavily commented example vertex shader, ready for use in Source is provided below. | |||
====Example Vertex Shader==== | |||
This is a pass through shader - in so far as it makes no major modification to the vertex data, instead just passing the data through to the pixel shader stage. | |||
// common vertex shader defines provided with this header | |||
#include "common_vs_fxc.h"<br> | |||
// define an output structure | |||
struct VS_OUTPUT | |||
{ | |||
// position vector (float4) | |||
float4 pos : POSITION0; | |||
// texture coordinates (uv - float2) | |||
float2 texCoord : TEXCOORD0; | |||
};<br> | |||
// main function - note C style definition | |||
// takes a position vector (float4) | |||
// returns a VS_OUTPUT struct | |||
VS_OUTPUT main( float4 inPos: POSITION ) | |||
{ | |||
// declare an empty VS_OUTPUT to fill | |||
VS_OUTPUT o = (VS_OUTPUT) 0;<br> | |||
// compute the sign of the input position | |||
inPos.xy = sign( inPos.xy); | |||
// set the output position using the xy of the input | |||
o.pos = float4( inPos.xy, 0.0f, 1.0f);<br> | |||
// get into range [0,1] | |||
o.texCoord = (float2(o.pos.x, -o.pos.y) + 1.0f)/2.0f; | |||
return o; | |||
} | |||
===Pixel Shaders=== | ===Pixel Shaders=== | ||
Pixel shaders are applied for each pixel rendered to the screen. A pixel shader expects input from interpolated vertex values, which it then uses to rasterize the image. | Pixel shaders are applied for each pixel rendered to the screen. A pixel shader expects input from interpolated vertex values, which it then uses to rasterize the image. Pixel shaders can produce a huge range of effects involving the colour of individual pixels such as refraction, per-pixel lighting or reflection. | ||
A heavily commented example pixel shader, ready for use in Source is provided below. | |||
====Example Pixel Shader==== | |||
The pixel shader below is intended for use as a post-process shader and creates a grayscale effect. | |||
// specify a texture sampler, the actual source of this is specified in a vmt | |||
sampler2D Texture0 : register( s0 );<br> | |||
// same function declaration style as vertex shaders | |||
// pixel shaders return the colour value of the pixel (hence the float4) | |||
float4 main( float2 texCoord : TEXCOORD0 ) : COLOR | |||
{ | |||
// sample the texture at the specified texture coordinates | |||
float4 tex = tex2D( Texture0, texCoord );<br> | |||
// greyscale the pixel colour values | |||
// - perform a dot product between the pixel colour and the specified vector | |||
// - 0.222, 0.707, 0.071 - is a vector that is commonly used in gray scale operations | |||
float4 grey = dot(float3(0.222, 0.707, 0.071), tex);<br> | |||
// return the pixel colour in the form of a float4. | |||
return grey; | |||
} | |||
==Applications of Shaders in Source== | ==Applications of Shaders in Source== |
Revision as of 20:34, 27 July 2006
Introduction
A shader is a program that runs on the graphics hardware (GPU) rather than the CPU. There are two variations of shaders, Pixel shaders and Vertex shaders, each of which performs a different task in the rendering pipeline. Shaders form a replacement for the fixed function pipeline and allow developers greater control over rendering output by providing the ability to modify pixels and vertices dynamically.
There are currently three main shader languages, HLSL, CG and GLSL. The Source Engine uses HLSL based shaders, however CG is so similar that most CG shaders can be quickly and easily ported to HLSL.
There are a variety of different models of the shader system; 1.1, 1.4, 2.0 and 3.0. Shader Model (SM) 3.0 is only available on high end graphics cards such as the Nvidia 6600, 6800 and 7800 or ATI X1600, X1800 and X1900. The most common Shader Model version supported is 2.0. It is important to pay attention to the shader model being compiled with when creating shaders as older graphics cards that only support SM 1.4 will not run shaders created using SM 2.0. In this case, shader fallbacks must be specified to provide some form of support for users with slightly outdated hardware.
For information on authoring shaders for use in the Source Engine, please see Shader_Authoring.
Vertex Shaders
Vertex shaders are applied for each vertex run on a programmable pipeline. Its most basic goal is to transform geometry into screenspace coordinates so that the Pixel shader can rasterize an image. On a more complex scale, Vertex shaders are responsible for mesh deformation, lighting, shadowing and general vertex displacement. Vertex shaders cannot create vertices.
A heavily commented example vertex shader, ready for use in Source is provided below.
Example Vertex Shader
This is a pass through shader - in so far as it makes no major modification to the vertex data, instead just passing the data through to the pixel shader stage.
// common vertex shader defines provided with this header #include "common_vs_fxc.h"
// define an output structure struct VS_OUTPUT { // position vector (float4) float4 pos : POSITION0; // texture coordinates (uv - float2) float2 texCoord : TEXCOORD0; };
// main function - note C style definition // takes a position vector (float4) // returns a VS_OUTPUT struct VS_OUTPUT main( float4 inPos: POSITION ) { // declare an empty VS_OUTPUT to fill VS_OUTPUT o = (VS_OUTPUT) 0;
// compute the sign of the input position inPos.xy = sign( inPos.xy); // set the output position using the xy of the input o.pos = float4( inPos.xy, 0.0f, 1.0f);
// get into range [0,1] o.texCoord = (float2(o.pos.x, -o.pos.y) + 1.0f)/2.0f; return o; }
Pixel Shaders
Pixel shaders are applied for each pixel rendered to the screen. A pixel shader expects input from interpolated vertex values, which it then uses to rasterize the image. Pixel shaders can produce a huge range of effects involving the colour of individual pixels such as refraction, per-pixel lighting or reflection.
A heavily commented example pixel shader, ready for use in Source is provided below.
Example Pixel Shader
The pixel shader below is intended for use as a post-process shader and creates a grayscale effect.
// specify a texture sampler, the actual source of this is specified in a vmt sampler2D Texture0 : register( s0 );
// same function declaration style as vertex shaders // pixel shaders return the colour value of the pixel (hence the float4) float4 main( float2 texCoord : TEXCOORD0 ) : COLOR { // sample the texture at the specified texture coordinates float4 tex = tex2D( Texture0, texCoord );
// greyscale the pixel colour values // - perform a dot product between the pixel colour and the specified vector // - 0.222, 0.707, 0.071 - is a vector that is commonly used in gray scale operations float4 grey = dot(float3(0.222, 0.707, 0.071), tex);
// return the pixel colour in the form of a float4. return grey; }
Applications of Shaders in Source
The Source Engine provides for two seperate forms of shaders, Postprocess and Per-Object, the majority of the effects and materials used within the Source Engine rely heavily on their Pixel shader components.
Postprocess
A Postprocess shader is typically a Pixel shader that works on a quad rendered across the entire screen. The quad is textured with a copy of the frame buffer, the Pixel shader can than alter and modify the rendered output to create a variety of effects, such as basic colour modification to more advanced processes such as motion blur and bloom.
The Source SDK provides an example of this form of shader in the sdk_postprocess files (sdk_postprocess.cpp
, sdk_postprocess_vs20.fxc
, and sdk_postprocess_ps20.fxc
)
Per-Object
A Per-Object shader in the Source Engine is used on any object with the shader referenced in the relevant VMT file, such as a model or piece of brushwork. A Per-Object shader could be used to create a refractive material, modify a models vertices dynamically or other advanced rendering effects.
The Source SDK provides an example of a Per-Object shader in the sdk_lightmap files ( sdk_lightmap.cpp
, sdk_lightmap_vs20.fxc
, and sdk_lightmap_ps20.fxc
)