This project is read-only.

Shaders Concept

Vortex2D uses vertex and pixel shaders for processing drawing output. Shader is software instructions which are executed on GPU side. Vertex shader takes vertex structure as input and produces transformed vertex into 2D device space. Then rasterizer draw primitives pixel by pixel. For each of this step pixel shader program is invoked. It allows to calculate color of result pixel with some function or so.

Vortex2D supports shader model 2.0 written in HLSL which gives enough space to implement most of cool 2D effects.

Vertex Shader

By default it uses simple vertex shader which just transform 2D coordinates from screen to device space (-1..1) with TRANSFORM_POSITION method.

Default vertex shader:

#include <system.vsh>

VS_OUT vs_main(in VS_IN In) {
	VS_OUT Out;
	Out.Pos.xy = __TransformXY(In.Pos.xy);
	Out.Pos.z = In.Pos.z;
	Out.Pos.w = 1.0;
	Out.Color = In.Diffuse;
	Out.Tex0 =  In.Tex0;
	Out.Tex1 =  In.Tex1;
	return Out;

VS_IN, VS_OUT and __TransformXY are declared in <system.vsh>. It is system include which provides in, out structures of vertex data. It is recommended to use it for each custom vertex shader.

Content of <system.vsh>:

struct VS_IN {
	float4 Pos		: POSITION;
	float4 Diffuse	: DIFFUSE;
	float2 Tex0    	: TEXCOORD0;
	float2 Tex1     : TEXCOORD1;

struct VS_OUT {
  float4 Pos : POSITION;
  float4 Color : COLOR0;
  float2 Tex0 : TEXCOORD0;
  float2 Tex1 : TEXCOORD1;


float2 __TransformXY(float2 pos) {
	float3 pos3 = float3(pos.x, pos.y, 1);
	return mul(VIEW_TRANSFORM, pos3).xy;
Anyway you can override everything but don't forget two things:
  1. Vortex2D provides vertex position, diffuse color and pair of texture coordinates for vertex shader
  2. Output vertex position should be in dimensions: X: -1..1, Y: 1..-1. (means 0,0 is screen center)

Pixel Shader

Default pixel shader is designed to work with brushes. It uses per-channel color multiplication between primary texture (specified by sprite), overlay texture and color tint. Code of default shader:

#include <system.psh>

sampler2D SAMPLER0 : register(s0);
sampler2D SAMPLER1 : register(s1);

float4 ps_main(PS_IN In) : COLOR {
	float4 Out = In.Color * tex2D(SAMPLER0, In.Tex0.xy) * tex2D(SAMPLER1, In.Tex1.xy);
	return Out;

Here is the same situation. *PS_IN* structure is declared in <system.psh>.

struct PS_IN {
  float4 Color : COLOR0;
  float2 Tex0 : TEXCOORD0;
  float2 Tex1 : TEXCOORD1;

Some pixel shaders could be based on multiple texture usage, e.g. 3, 4 or even 8. Vortex2D supports up to 8 textures which could be setup with samplers. Read Multitexturing and Samplers to get more information about.

Include Directive

Include directive could be very useful for applications which uses VS & PS heavily. It could help to organize your shader library. You can include another shader files specifying relative path from appdir.

Last edited Dec 20, 2010 at 7:17 PM by AlexKhomich, version 7


No comments yet.