We are testing our HLSL parser with the following shaders:
Basic Shader/*
* Basic Fragment Shader.
* by Vitor Pamplona
*/
// Main function of the fragment shader.
float4 pixelShader(float4 color : COLOR0) : COLOR {
// Creating Green Color
float4 teste = float4(0,1,0,0);
return teste; //returns always black
}
Attributes and global vars /*
* Testing variable from outside and .xy atributes. From: http://www.facewound.com/tutorials/shader1/
* by Vitor Pamplona
*/
sampler2D g_samSrcColor;
float timeraslow=1.0;
float4 MyShader( float2 Tex : TEXCOORD0 ) : COLOR0
{
float4 Color;
Color.a = 1.0f;
Color.rgb = 0.5f;
Tex.y = Tex.y + (sin(timeraslow)*0.01);
Tex.x = Tex.x + (cos(timeraslow)*0.01);
Color = tex2D( g_samSrcColor, Tex.xy);
Color -= tex2D( g_samSrcColor, Tex.xy-0.001)*2.0f;
Color += tex2D( g_samSrcColor, Tex.xy+0.001)*2.0f;
Color.rgb = (Color.r+Color.g+Color.b)/3.0f;
return Color;
}
Functions and operators/*
* Multi-function and operators Shader Test.
* by Vitor Pamplona
*/
float otherFunction(float4 teste, float4 outroTeste) {
return dot(teste, outroTeste);
}
// Main function of the fragment shader.
float4 pixelShader(float4 color : COLOR0) : COLOR {
// Creating Green Color
float4 teste = float4(0+0.5,1,40*0.5,0);
teste++;
// Other function
otherFunction(teste, teste);
return teste; //returns always black
}
Struct test/*
* Testing Structs, in_out variables and matrixes. From: http://www.neatware.com/lbstudio/web/hlsl.html
* by Vitor Pamplona
*/
struct a2v {
float4 Position : POSITION;
};
struct v2p {
float4 Position : POSITION;
};
void main(in a2v IN, out v2p OUT, uniform float4x4 ModelViewMatrix)
{
OUT.Position = mul(IN.Position, ModelViewMatrix);
}
Flow control /*
* Flow control shader Test.
* by Vitor Pamplona
*/
// Main function of the fragment shader.
float4 pixelShader(float4 color : COLOR0) : COLOR {
int i = 0;
int type = 0;
// Creating Green Color
float4 teste = float4(0+0.5,1,40*0.5,0);
// Branch: Evaluate only one side of the if
// statement depending on the given condition.
branch if (teste.x+4 > 0) {
teste++;
} else {
// Unroll(x): Unroll the loop until it stops executing. Can optionally
// specify the maximum number of times the loop is to execute
unroll(10) for (int i=0; i+4<10; i+=4) {
teste--;
// Stop executing the current loop (do, for, while),
// update the loop conditions, and begin executing from the top of the loop.
continue;
}
// Flatten: Evaluate both sides of the if statement
// and choose between the two resulting values.
flatten if (teste.x > 0) {
// Loop: Generate code that uses flow control to execute each iteration of the loop.
loop for (;;) {
teste--;
stop; // Stop execution at the current statement and return the output.
}
teste--;
} else {
do {
break; // Exit the surrounding loop (do, for, while).
} while (teste.x+4 > 0);
discard; // Discarte: Do not output the result of the current pixel.
// flatten : Compile the statement as a series of if statements,
// each with the flatten attribute.
// branch : Compile the statement as a series of if statements
// each with the branch attribute.
// forcecase : Force a switch statement in the hardware.
// call : The bodies of the individual cases in the switch will be moved into
// hardware subroutines and the switch will be a series of subroutine calls.
forcecase switch( teste.y ) {
case 0: k++; return teste;
case 1: return teste;
case 2: t++; return teste;
default: {
teste++;
teste++;
teste++;
}
}
}
}
return teste; //returns always black
}
Technique Test /*
* Bump Mapping Tutorial : http://dotnet.org.za/pieterg/archive/2005/07/29/40407.aspx
* by Vitor Pamplona
*/
float4x4 ModelViewProj : WORLDVIEWPROJ; //our world view projection matrix
float4x4 ModelViewIT : WORLDVIEWIT; //our inverse transpose matrix
float4x4 ModelWorld : WORLD; //our world matrix
float4 lightPos; //our light position in object space
texture texture0; //our texture
texture texture1; //our normal map
float value = 1.5f;
sampler2D texSampler0 : TEXUNIT0 = sampler_state
{
Texture = (texture0);
MIPFILTER = LINEAR;
MAGFILTER = LINEAR;
MINFILTER = LINEAR;
};
sampler2D texSampler1 : TEXUNIT1 = sampler_state
{
Texture = (texture1);
MIPFILTER = LINEAR;
MAGFILTER = LINEAR;
MINFILTER = LINEAR;
};
//application to vertex structure
struct a2v
{
float4 position : POSITION0;
float3 normal : NORMAL;
float2 tex0 : TEXCOORD0;
float3 tangent : TANGENT;
float3 binormal : BINORMAL;
};
//vertex to pixel shader structure
struct v2p
{
float4 position : POSITION0;
float2 tex0 : TEXCOORD0;
float2 tex1 : TEXCOORD1;
float3 lightVec : TEXCOORD2;
float att : TEXCOORD3;
};
//pixel shader to screen
struct p2f
{
float4 color : COLOR0;
};
//VERTEX SHADER
void vs( in a2v IN, out v2p OUT )
{
//getting to position to object space
OUT.position = mul(IN.position, ModelViewProj);
//getting the position of the vertex in the world
float4 posWorld = mul(IN.position, ModelWorld);
//getting vertex -> light vector
float3 light = normalize(lightPos - posWorld);
//calculating the binormal and setting the Tangent Binormal and Normal matrix
float3x3 TBNMatrix = float3x3(IN.tangent, IN.binormal , IN.normal);
//setting the lightVector
OUT.lightVec = mul(TBNMatrix, light);
//calculate the attenuation
OUT.att = 1/( 1 + ( 0.005 * distance(lightPos.xyz, posWorld) ) );
OUT.tex0 = IN.tex0;
OUT.tex1 = IN.tex0;
}
//PIXEL SHADER
void ps( in v2p IN, out p2f OUT )
{
//calculate the color and the normal
float4 color = tex2D(texSampler0, IN.tex0);
/*this is how you uncompress a normal map*/
float3 normal = 2.0f * tex2D(texSampler1, IN.tex1).rgb - 1.0f;
//normalize the light
float3 light = normalize(IN.lightVec);
//set the output color
float diffuse = saturate(dot(normal, light));
//multiply the attenuation with the color
OUT.color = IN.att * color * diffuse;
}
technique test
{
pass p0
{
vertexshader = compile vs_1_1 vs();
pixelshader = compile ps_2_0 ps();
}
}