Shaders - hpgDesigns/hpgdesigns-dev.io GitHub Wiki
This article is an overview of using shaders in ENIGMA. All of the shader related constants can be found on the page Shader constants, and the functions are on the page Shader Functions.
CG Shading Language
CG can be used to write shader code once and compile optimized code for your shader to both GLSL and HLSL eliminating the need for you to write your shader code twice. We plan on supporting these in the future.
OpenGL Shading Language
GLSL would be used in our OpenGL graphics port.
Types
There are various types of shaders that exist GLSL.
Vertex
The Vertex Shader is the programmable Shader stage in the rendering pipeline that handles the processing of individual vertices. A vertex shader receives a single vertex composed of a series of Vertex Attributes. This input vertex is processed arbitrarily to produce an output vertex. There must be a 1:1 mapping from input vertices to output vertices.
Tessellation Evaluation Shader
The Tessellation Evaluation Shader (TES) is a Shader program written in GLSL that takes the results of a Tessellation operation and computes the interpolated positions and other attributes. These values are passed on to the next stage in the pipeline.
Geometry
A Geometry Shader (GS) is a Shader program written in GLSL that governs the processing of primitives. It happens after primitive assembly, as an additional optional step in that part of the pipeline. A GS can create new primitives, unlike vertex shaders, which are limited to a 1:1 input to output ratio. A GS can also do layered rendering; this means that the GS can specifically say that a primitive is to be rendered to a particular layer of the framebuffer.
Fragment
A Fragment Shader is a user-supplied program that, when executed, will process a Fragment from the rasterization process into a set of colors and a single depth value.
Compute
A Compute Shader is a Shader Stage that is used entirely for computing arbitrary information. While it can do rendering, it is generally used for tasks not directly related to drawing triangles and pixels. These are the only GLSL shaders we currently do not support.
Example GLSL Program
This is an example GLSL per-pixel cartoon shader program.
The fragment shader source...
#version 330
varying vec3 normal;
void main()
{
float intensity;
vec4 color;
vec3 n = normalize(normal);
intensity = dot(vec3(gl_LightSource[0].position),n);
if (intensity > 0.95)
color = vec4(1.0,0.5,0.5,1.0);
else if (intensity > 0.5)
color = vec4(0.6,0.3,0.3,1.0);
else if (intensity > 0.25)
color = vec4(0.4,0.2,0.2,1.0);
else
color = vec4(0.2,0.1,0.1,1.0);
gl_FragColor = color;
}
The vertex shader source...
#version 330
varying vec3 normal;
void main()
{
normal = gl_NormalMatrix * gl_Normal;
gl_Position = ftransform();
}
High Level Shader Language
HLSL would be used in our DirectX graphics port, we currently do not support shaders written in HLSL.
Types
There are various types of shaders that exist HLSL.
Vertex
When in operation, a programmable vertex shader replaces the vertex processing done by the Microsoft Direct3D graphics pipeline. While using a vertex shader, state information regarding transformation and lighting operations is ignored by the fixed function pipeline. When the vertex shader is disabled and fixed function processing is returned, all current state settings apply.
Pixel
Pixel processing is performed by pixel shaders on individual pixels. Pixel shaders work in concert with vertex shaders; the output of a vertex shader provides the inputs for a pixel shader. Other pixel operations (fog blending, stencil operations, and render-target blending) occur after execution of the shader.