开发者

DirectX 9 HLSL Texture Sampling Issues

I'm making an application using managed directX 2.0 (which I believe is the same as DirectX9) and I'm new to HLSL, so I'm sorry if what I'm doing is idiotic. I'm writing a simple pixel shader that simply outputs a stored texture (assigned as a global variab开发者_运维知识库le) to screen, but I'm finding that it is instead rendering the texture passed to it (I am also passing a different rendered texture in, this shader is a test shader).

My HLSL code is as follows:

texture inputTex;
sampler2D InputSampler = sampler_state
{
    Texture = <inputTex>;
};
texture DepthTexture;
sampler2D DepthSampler = sampler_state
{
    Texture = (DepthTexture);
    MinFilter = Linear;
    MagFilter = Linear;
    MipFilter = Linear;   
    AddressU  = Clamp;
    AddressV  = Clamp;
};
float4 testPass(float2 TextureCoordinate : TEXCOORD0) : COLOR0
{
    float4 new_color = saturate(tex2D(DepthSampler,TextureCoordinate));
    new_color.a=1;
    return new_color;
}
technique DoF
{
    pass Pass0
    {
        PixelShader = compile ps_2_0 testPass();
    }
}

and my C# is:

postProc.SetValue("DepthTexture", DepthTexture);
postProc.Begin(FX.DoNotSaveState);
device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.White, 1.0f, 0);
device.BeginScene();
{
    postProc.SetValue("inputTex", RenderTexture);
    postProc.CommitChanges();
    postProc.BeginPass(0);
    sprite.Draw(RenderTexture, new Rectangle(0, 0, WINDOWWIDTH, WINDOWHEIGHT), new Vector3(0, 0, 0), new Vector3(0, 0, 0), Color.Black);
    postProc.EndPass();
    postProc.End();
    sprite.End();
}
device.EndScene();
device.Present();

The output is whatever is stored in RenderTexture, as opposed to what is stored in DepthTexture, which is what it is supposed to do. I have tried swapping the RenderTexture with DepthTexture in this line:

sprite.Draw(RenderTexture, new Rectangle(0, 0, WINDOWWIDTH, WINDOWHEIGHT), new Vector3(0, 0, 0), new Vector3(0, 0, 0), Color.Black);

and the output is DepthTexture, so it's clearly taking the data from there and the texture is formatted properly.

Does anybody know of any good tutorials of how to tell HLSL when to sample from a texture passed in and when to sample from the drawn data?


I think Sprite ignores effect state -- it just copies the portion of the texture you specify to the given location. It doesn't run the current shader. It looks like you're just trying to draw a window-size quad with the current effect; instead of using Sprite, you probably just want to use Device.DrawPrimitives directly.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜