I have a texture that becomes the input to a pixel shader.
Code snippet:
mDevice->CreateShaderResourceView(myTexture, nullptr, byRef(mBckSRView));
...->AsShaderResource()->SetResource(mBckSRView);
However, I want to apply a blur filter to this texture before it is used in the shader. How do I go about this?
How?
Simplest blur, algorithm is as follows: sample multiple texels around your current texel and combine their colors in whatever fashion you like: mean value, linear interpolation, multiply on weights, Gaussian function, combination of all of that etc.
Of course, that algorithm is a brute-force solution. There are huge field for optimizations. But it simple and can be enough for you. If not, just Google up for algorithm you need for language you using and read some papers.
Where?
You can do it in pixel shader.
You can do it on CPU side: just apply effect to raw bitmap data itself (array of pixels) before resource creation (after loading but before
CreateTexture2D
andCreateShaderResourceView
).You can apply effect offline, i.e. before application even starts. Ask your artist to make bunch of textures with different effects applied in Photoshop or GIMP. Or automate it using scripts.
Edit: combining complex multisample effects in shader.
So far so good, but what if you want to combine effects in shader? Well, you just applying them one by one in a serial way in shader code: after first effect you have a pixel's color and you use it as a source for second effect, etc.
But what if you want to apply two different blurs one after another? Blur requires neighbor pixels of the source texture to be sampled, but we have only current pixel. Solution is to render to texture and make another pass (draw call) with rendered texture as input and second blur shader.
Edit2
I'll try to clarify. You have at least t̶w̶o̶ three alternatives. In short:
Do it directly in "another shader", just imagine that instead of sampling, you sampling + applying first effect. Here is example for pixel shader (but it can be any shader stage):
You cannot apply blur as
"second effect"
orMyThirdEffect
, because it requires multiple texels to sample, but you have only one as output of first effect. For that we need to have whole texture being already preprocessed. To achieve that, you will need to render to texture and move second effect to second pass:If you can, use Compute shader and forget all of above =)
Hope it helps!