GLSL and HLSL Example

The code below defines a shader consisting of vShader.{glsl/hlsl} vertex shader and fShader.{glsl/hlsl} fragment shader. No special flag is given.

Vertex Shader (GLSL version, vShader.glsl)

attribute highp vec3 vVertex;
attribute mediump vec2 vTexCoord;
uniform highp mat4 vMatrix;
varying mediump vec2 fTexCoord; 
 
void main() {
  vec4 vertex = vec4(vVertex,1.0);
  gl_Position = vMatrix*vertex;
  fTexCoord=vTexCoord;
}

Vertex Shader (HLSL version, vShader.hlsl)

struct VOut
{
  float4 position : SV_POSITION;
  float2 texcoord : TEXCOORD;
}; 
 
cbuffer cbv : register(b0)
{
  float4x4 vMatrix;
}; 
 
VOut VShader(float4 position : vVertex, float2 texcoord : vTexCoord)
{
  VOut output; 

  position.w = 1.0f; 

  output.position = mul(vMatrix, position);
  output.texcoord = texcoord; 
 
  return output;
}

Fragment Shader (GLSL version, fShader.glsl)

uniform lowp vec4 fColor;
uniform lowp sampler2D fTexture;
uniform int fRad;
uniform mediump vec4 fTexelSize;
varying mediump vec2 fTexCoord; 
 
void main() {
  lowp vec4 frag=vec4(0,0,0,0);
  int ext=2*fRad+1;
  mediump vec2 tc=fTexCoord-fTexelSize.xy*fRad;
  for (int v=0;v<ext;v++)
  {
    frag=frag+texture2D(fTexture, tc);
    tc+=fTexelSize.xy;
  }
  frag=frag/ext;
  if (frag.a==0.0) discard;
  gl_FragColor = frag;
}

Fragment Shader (HLSL version, fShader.hlsl)

Texture2D myTexture : register(t0);
SamplerState samLinear : register(s0); 
 
cbuffer cbp : register(b1)
{
  float4 fColor;
  float4 fTexelSize;
  int fRad;
}; 
 
float4 PShader(float4 position : SV_POSITION, float2 texcoord : TEXCOORD) : SV_TARGET
{
  float4 frag=float4(0,0,0,0);
  int ext=(2*fRad+1);
  float2 tc=texcoord-fTexelSize.xy*fRad;
  [unroll(21)]
  for (int v=0;v<ext;v++)
  {
    frag=frag+myTexture.Sample(samLinear, tc)/ext;
    tc+=fTexelSize.xy;
  }
  if (frag.a == 0.0) discard;
  return frag;
}

Lua Code

local shader=Shader.new("vShader","fShader",0,
{
  {name="vMatrix",type=Shader.CMATRIX,sys=Shader.SYS_WVP,vertex=true},
  {name="fColor",type=Shader.CFLOAT4,sys=Shader.SYS_COLOR,vertex=false },
  {name="fTexture",type=Shader.CTEXTURE,vertex=false},
  {name="fTexelSize",type=Shader.CFLOAT4,vertex=false},
  {name="fRad",type=Shader.CINT,vertex=false},
},
{
  {name="vVertex",type=Shader.DFLOAT,mult=3,slot=0,offset=0},
  {name="vColor",type=Shader.DUBYTE,mult=4,slot=1,offset=0},
  {name="vTexCoord",type=Shader.DFLOAT,mult=2,slot=2,offset=0},
});

Five uniforms are also defined, named vMatrix,fColor,fTexture,fTexelSize and fRad. Only the first one is associated to the vertex program (you can check that in the respective programs seen earlier).

Furthermore, we ask Gideros to take responsibility of setting vMatrix to the Matrix/View/Projection matrix (SYS_WVP), and fColor to the fixed color of the shape being rendered (SYS_COLOR).

fTexture is actually a texture (Shader.CTEXTURE) and declared appropriately in GLSL or HLSL.

fTexelSize and fRad have not specific meaning and will be set by the Lua code.

The three standard attributes are also defined.

Changing Uniforms

shader:setConstant("fRad",Shader.CINT,1,0) --Initial blur level
shader:setConstant("fTexelSize",Shader.CFLOAT4,1,{1/texw,1/texh,0,0} ) --Initial texel size

Apply to a Sprite

spr:setShader(shader)