4
votes

Despite the simplicity of the problem I am facing, I cannot find an answer ANYWHERE on the internet. I have a source texture that I get from the RenderTarget I am using, which I then pass to my effect. My effect has a vertex shader and a pixel shader, but the pixel shader is the important one. I want the vertex shader to do absolutely nothing to the input vertices. The output should then get passed to my pixel shader, which will perform image processing (like blurring, etc.) on the image. The result of this will be drawn to the screen.

So basically, I just want a vertex shader that takes a texture in and passes it out completely unaltered. No 3D geometry here to speak of.

Here is the code I wrote to do this:


float4 VSBasic(float4 vin : POSITION) : POSITION
{
    return vin;
}

And here is my pixel shader, which should turn all pixels red:


float4 PS_GaussianBlur(float2 texCoord : TEXCOORD) : COLOR0
{
    float4 color = float4(0.0f, 0.0f, 0.0f, 1.0f);

    //int kernelLength = min(31, kernelRadius * 2 + 1);
    //for (int i = 0; i &lt kernelLength; i++)
        //color += tex2D(colorMap, texCoord + offsets[i]) * kernel[i];

    color = float4(1.0f, 0.0f, 0.0f, 1.0f);
    return color;
}

Note that the blurring part is commented out for testing.

So what am I doing wrong??!

P.S. The only reason I even use a vertex shader is because XNA will complain that I am using conflicting pixel/vertex shader versions unless I specify in the effect that I should compile PS_3_0 and VS_3_0 (because I need PS_3_0, I have this working in PS_2_0 without a vertex shader). So if there is a different way to specify the vertex shader version without actually implementing a vertex shader let me know.

EDIT: To clarify, my solution works great in PS_2_0, but PS_2_0 doesn't have enough constant registers in my opinion. What I want to do is extend my shader into PS_3_0 so that I can work with a lot more registers. However, when I do this, XNA complains that PS_3_0 isn't backwards compatible with lower vertex shaders, so I had to make my own vertex shader. However, I don't want this vertex shader to DO anything, since all the work is done in my pixel shader. I hope this clears things up.

1
Can you please clarify the question? Does current solution works and why it doesn't suits you?Petr Abdulin
Are you sure this is C#? The code does not appear to be C#.devuxer
It's HLSL. I removed the C# tag. XNA is C#, but the question isn't really about C# at all.Andrew Russell
@AndrewRussell, thank you. I was perplexed by all the colons :)devuxer

1 Answers

2
votes

I can't tell what you're doing wrong, because you haven't said how you're rendering. I'm going to assume you're using SpriteBatch and this is a coordinate system problem.

You can find the shader that SpriteBatch uses internally in the XNA Stock Effects download.

You'll note that it has a projection transform. You input coordinates to SpriteBatch in client space. But rendering actually takes place in raster space (a projection matrix transforms vertices into this space).

Client space goes from (0,0) in the top-left corner to (width,height) in the bottom-right corner of the viewport.

Projection space goes from (-1,-1) in the bottom-left corner, to (1,1) in the top right corner of the viewport.

Usually, for a full-screen effect, the easiest method is to simply render to a fixed quad that covers raster space (ie: don't use SpriteBatch, use a vertex buffer instead). You don't need any projection matrix, and your vertex shader can be empty.

This blog post might be useful to read if you do want to use SpriteBatch. Note that the code given for the vertex shader is actually wrong - it doesn't include the projection transform.

Also, I suspect it will be a problem that you're not passing TEXCOORD0 (and maybe COLOR0) though your shader.