Hi,
Im a Vvvv-Newbie and started to program a little shader. Seems easier than I thought. But its just a 2d-Shader.
I wrote a Erode-Dilate-Shader for cleaning my Livefeed of noise at lowlevel lighting. It works well for the Dilate and the Erode-mode. But now I tried to combine both - one after the other - to get a Open-mode (like the Intel Processing Library).
-But that doesnt work, because I dont know how to save the results of the first pass (Dilate code) and grab it with the later Erode code.
Thanks Frank
Here is the code - a modified kernel example:
{CODE(ln=>1)}
// this is an effect template. use it to start writing your own effects.
// -------------------------------------------------------------------------
// PARAMETERS:
// -------------------------------------------------------------------------
//transforms
float4x4 tW: WORLD; //the models world matrix
float4x4 tV: VIEW; //view matrix as set via Renderer (DX9)
float4x4 tP: PROJECTION; //projection matrix as set via Renderer (DX9)
float4x4 tWVP: WORLDVIEWPROJECTION;
//texture
texture Tex <string uiname=“Texture”;>;
sampler Samp = sampler_state //sampler for doing the texture-lookup
{
Texture = (Tex); //apply a texture to the sampler
MipFilter = LINEAR; //set the sampler states LINEAR
MinFilter = LINEAR;
MagFilter = LINEAR;
};
//texture transformation marked with semantic TEXTUREMATRIX to achieve symmetric transformations
float4x4 tTex: TEXTUREMATRIX <string uiname=“Texture Transform”;>;
//pin for pixeldiameter
int TexWidth <string uiname=“Texture Width”;> = 512;
int TexHeight <string uiname=“Texture Height”;> = 512;
//the data structure: “vertexshader to pixelshader”
//used as output data with the VS function
//and as input data with the PS function
struct vs2ps
{
float4 Pos : POSITION;
float4 TexCd : TEXCOORD0;
float2 Pixelsize : TEXCOORD1;
};
// -------------------------------------------------------------------------
// VERTEXSHADERS
// -------------------------------------------------------------------------
vs2ps VS(float4 Pos : POSITION, float4 TexCd : TEXCOORD0) {
//inititalize all fields of output struct with 0
vs2ps Out = (vs2ps)0;
//transform position
Out.Pos = mul(Pos, tWVP);
//transform texturecoordinates
Out.TexCd = mul(TexCd, tTex);
//you can calculated things here aswell
//we calculated here the "pixelsize" = 1/texture size
Out.Pixelsize = 1 /float (TexWidth);
return Out;
}
// -------------------------------------------------------------------------
// PIXELSHADERS:
// -------------------------------------------------------------------------
// The surrounding pixels
float2 neighbours8 = {
-1, -1,
0, -1,
1, -1,
-1, 0,
1, 0,
-1, 1,
0, 1,
1, 1,
};
//Dilate - Outputs the minimum of the center and neighbours
float4 PS_erode(vs2ps In): COLOR {
float4 minSamp = tex2D(Samp, In.TexCd);
for (int i = 0; i < 8; i++) {
float4 tmpSample = tex2D(Samp, In.TexCd + In.Pixelsize * neighboursi);
minSamp = min(minSamp, tmpSample);
}
return minSamp;
}
//Dilate - Outputs the minimum of the center and neighbours
float4 PS_dilate(vs2ps In): COLOR {
float4 maxSamp = tex2D(Samp, In.TexCd);
for (int i = 0; i < 8; i++) {
float4 tmpSample = tex2D(Samp, In.TexCd + In.Pixelsize * neighboursi);
maxSamp = max(maxSamp, tmpSample);
}
return maxSamp;
}
//Open - First Dilate then Erode
float4 PS_open(vs2ps In): COLOR {
float4 minSamp = tex2D(Samp, In.TexCd);
for (int i = 0; i < 8; i++) {
float4 tmpSample = tex2D(Samp, In.TexCd + In.Pixelsize * neighboursi);
minSamp = min(minSamp, tmpSample);
}
float4 maxSamp = tex2D(Samp, In.TexCd);
for (int i = 0; i < 8; i++) {
float4 tmpSample = tex2D(Samp, In.TexCd + In.Pixelsize * neighboursi);
maxSamp = max(maxSamp, tmpSample);
}
return maxSamp;
}
// bypass
float4 PS0(vs2ps In): COLOR {
float4 col = tex2D(Samp, float2 (In.TexCd.x, In.TexCd.y));
return col;
}
// -------------------------------------------------------------------------
// TECHNIQUES:
// -------------------------------------------------------------------------
technique Erode
{
pass P0
{
VertexShader = compile vs_1_1 VS();
PixelShader = compile ps_2_0 PS_erode();
}
}
technique Dilate
{
pass P0
{
VertexShader = compile vs_1_1 VS();
PixelShader = compile ps_2_0 PS_dilate();
}
}
technique Open
{
pass P0
{
VertexShader = compile vs_1_1 VS();
PixelShader = compile ps_2_0 PS_open();
}
}
technique Bypass
{
pass P0
{
VertexShader = compile vs_1_1 VS();
PixelShader = compile ps_2_0 PS0();
}
}
^