Ok iv managed to get my texture perform blur and several other effects.
To make things progress i just performed the blur or other effects on my background texture which seemed to work perfectly and as expected as described in my effects.
Texture2D ShaderTexture : register(t0);
SamplerState Sampler : register (s0);
Texture2D EffectShaderTexture : register(t1);
SamplerState EffectSampler
{
Filter = MIN_MAG_MIP_LINEAR;
AddressU = Wrap;
AddressV = Wrap;
};
cbuffer PerObject : register(b0)
{
float4x4 ViewProjection;
float4x4 World;
};
// ------------------------------------------------------
// A shader that accepts Position and Color
// ------------------------------------------------------
struct ColorVS_IN
{
float4 Position : SV_Position;
float4 ColorRGBA : COLOR;
};
struct ColorPS_IN
{
float4 Position : SV_Position;
float4 ColorRGBA : COLOR;
};
ColorPS_IN ColorVS(ColorVS_IN input)
{
ColorPS_IN output = (ColorPS_IN)0;
float4 transformedPosition = mul(input.Position, World);
transformedPosition = mul(transformedPosition, ViewProjection);
output.Position = transformedPosition;
output.ColorRGBA = input.ColorRGBA;
return output;
}
float4 ColorPS(ColorPS_IN input) : SV_Target
{
return input.ColorRGBA;
}
// ------------------------------------------------------
// A shader that accepts Position and Texture
// ------------------------------------------------------
struct VertexShaderInput
{
float4 Position : SV_Position;
float2 TextureUV : TEXCOORD0;
};
struct VertexShaderOutput
{
float4 Position : SV_Position;
float2 TextureUV : TEXCOORD0;
};
VertexShaderOutput VSMain(VertexShaderInput input)
{
VertexShaderOutput output = (VertexShaderOutput)0;
float4 transformedPosition = mul(input.Position, World);
transformedPosition = mul(transformedPosition, ViewProjection);
output.Position = transformedPosition;
output.TextureUV = input.TextureUV;
return output;
}
float4 PSMain(VertexShaderOutput input) : SV_Target
{
return ShaderTexture.Sample(Sampler, input.TextureUV);
}
// ------------------------------------------------------
// A shader that accepts Position and Texture (shader effects)
// ------------------------------------------------------
// original blur effect
float4 PSMainBlurEffect(VertexShaderOutput input) : SV_Target
{
float fBlurAmont = 0.25;
float Center = 0.5;
float4 rgbaValue = 0;
input.TextureUV -= Center;
for (int i = 0; i < 15; i++)
{
float scale = 1.0 + fBlurAmont * (i / 14.0);
rgbaValue += EffectShaderTexture.Sample(EffectSampler, input.TextureUV * scale + Center);
}
rgbaValue /= 15;
return rgbaValue;
}
// Gray scale effect
float4 PSMainGrayScaleEffect(VertexShaderOutput input) : SV_Target
{
float4 rgbaValue = EffectShaderTexture.Sample(EffectSampler, input.TextureUV);
rgbaValue = dot(rgbaValue, float3(0.3, 0.59, 0.11));
return rgbaValue;
}
// Negative effect
float4 PSMainNegativeEffect(VertexShaderOutput input) : SV_Target
{
float4 rgbaValue = EffectShaderTexture.Sample(EffectSampler, input.TextureUV);
rgbaValue = 1 - rgbaValue;
return rgbaValue;
}
// Pixelated effect
float4 PSMainPixelatedEffect(VertexShaderOutput input) : SV_Target
{
float2 brickCounts = { 50, 50 }; //
float2 brickSize = 1.0 / brickCounts;
// Offset every other row of bricks
float2 offsetuv = input.TextureUV;
bool oddRow = floor(input.TextureUV.y / brickSize.y) % 2.0 >= 1.0;
if (oddRow)
{
input.TextureUV.x += brickSize.x / 2.0;
}
float2 brickNum = floor(input.TextureUV / brickSize);
float2 centerOfBrick = brickNum * brickSize + brickSize / 2;
float4 color = EffectShaderTexture.Sample(EffectSampler, centerOfBrick);
return color;
}
// Bloom effect
float4 AdjustSaturation(float4 color, float saturation)
{
float grey = dot(color, float3(0.3, 0.59, 0.11));
// formula for lerp below taken from msdn
//return lerp(grey, color, saturation);
// x + s(y-x) <=== (x,y,s)
return (grey + (saturation * (color - grey)));
}
float4 PSMainBloomEffect(VertexShaderOutput input) : SV_Target
{
float BloomIntensity = 9; // 0 to 10
float BaseIntensity = 1; // 0 to 10
float BloomSaturation = 7; // 0 to 10
float BaseSaturation = 1; // 0 to 10
float BloomThreshold = 0.25f;
float4 base = EffectShaderTexture.Sample(EffectSampler, input.TextureUV);
float4 bloom = saturate((base - BloomThreshold) / (1 - BloomThreshold));
// Adjust color saturation and intensity.
bloom = AdjustSaturation(bloom, BloomSaturation) * BloomIntensity;
base = AdjustSaturation(base, BaseSaturation) * BaseIntensity;
// Darken down the base image in areas where there is a lot of bloom,
// to prevent things looking excessively burned-out.
base *= (1 - saturate(bloom));
// Combine the two images.
return base + bloom;
}
// NightVision effect
float4 PSMainNightVisionEffect(VertexShaderOutput input) : SV_Target
{
float elapsedTime = 1;
float luminanceThreshold = 0.1;
float colorAmplification = 4;
float2 uv;
uv.x = 0.4*sin(elapsedTime*50.0);
uv.y = 0.4*cos(elapsedTime*50.0);
float3 n = EffectShaderTexture.Sample(EffectSampler, (input.TextureUV*3.5) + uv).rgb;
float3 c = EffectShaderTexture.Sample(EffectSampler, input.TextureUV + n.xy*0.005).rgb;
float lum = dot(float3(0.30, 0.59, 0.11), c);
if (lum < luminanceThreshold)
c *= colorAmplification;
float3 visionColor = float3(0.1, 0.95, 0.2);
return float4((c + (n*0.2)) * visionColor ,1);
}
// Pinch effect
float4 PSMainPinchEffect(VertexShaderOutput input) : SV_Target
{
float CenterX = 0.5;
float CenterY = 0.5;
float Radius = 0.75;
float Amount = 5;
float2 center = { CenterX, CenterY };
float2 displace = center - input.TextureUV;
float range = saturate(1 - (length(displace) / (abs(-sin(Radius * 8) * Radius) + 0.00000001F)));
return EffectShaderTexture.Sample(EffectSampler, input.TextureUV + displace * range * Amount);
}
// Swirl effect
float4 PSMainSwirlEffect(VertexShaderOutput input) : SV_Target
{
float2 center = 0.5;
float spiralStrength = 10;
float2 angleFrequency = { 1, 1 };
float2 dir = input.TextureUV - center;
float l = length(dir);
float angle = atan2(dir.y, dir.x);
float newAng = angle + spiralStrength * l;
float xAmt = cos(angleFrequency.x * newAng) * l;
float yAmt = sin(angleFrequency.y * newAng) * l;
float2 newCoord = center + float2(xAmt, yAmt);
return EffectShaderTexture.Sample(EffectSampler, newCoord);
}
However the problem i now face is when trying to perform the effects within a sub region using a quad.
It seems my effect's texture is displayed to the entire target display area(displayed below) that is covered by the background texture, even though my sub-region quads have their location + size set to the correct parameters.
[attachment=32596:pixelated_quad.PNG]
Can't seems to figure it out why its behavior is different to when drawing a quad that just fills with a color (shown in original post) ?
Any suggestion ?