2011-01-23 83 views
1

我正在使用託管Direct X 2.0與C#,我試圖應用片段着色器通過使用RenderToSurface助手類將屏幕渲染到紋理構建的紋理。託管DirectX後處理碎片着色器呈現問題

我使用的是做到這一點的代碼是:

RtsHelper.BeginScene(RenderSurface); 
device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.White, 1.0f, 0); 
//pre-render shader setup 
preProc.Begin(FX.None); 
    preProc.BeginPass(0); 
     //mesh drawing 
     mesh.DrawSubset(j); 
     preProc.CommitChanges(); 
    preProc.EndPass(); 
preProc.End(); 
RtsHelper.EndScene(Filter.None); 

這使得我的面,RenderSurface,其連接到名爲渲染紋理

然後調用下面的代碼到紋理對象將表面渲染到屏幕,將第二個着色器「PostProc」應用於渲染的紋理。該着色器以每個像素爲基礎組合顏色值,並將場景轉換爲灰度。我在這裏以下教程:http://rbwhitaker.wikidot.com/post-processing-effects

device.BeginScene(); 
{ 
    using (Sprite sprite = new Sprite(device)) 
    { 
     sprite.Begin(SpriteFlags.DoNotSaveState); 
      postProc.Begin(FX.None); 
       postProc.BeginPass(0); 
        sprite.Draw(RenderTexture, new Rectangle(0, 0, WINDOWWIDTH, WINDOWHEIGHT), new Vector3(0, 0, 0), new Vector3(0, 0, 0), Color.White); 
        postProc.CommitChanges(); 
       postProc.EndPass(); 
      postProc.End(); 
     sprite.End(); 
    } 

} 
device.EndScene(); 
device.Present(); 
this.Invalidate(); 

但是我看到的是原始渲染的場景,爲渲染到紋理,但未經修改的第二着色器。

FX文件低於以防萬一它很重要。

//------------------------------ TEXTURE PROPERTIES ---------------------------- 
// This is the texture that Sprite will try to set before drawing 
texture ScreenTexture; 

// Our sampler for the texture, which is just going to be pretty simple 
sampler TextureSampler = sampler_state 
    { 
     Texture = <ScreenTexture>; 
    }; 

//------------------------ PIXEL SHADER ---------------------------------------- 
// This pixel shader will simply look up the color of the texture at the 
// requested point, and turns it into a shade of gray 
float4 PixelShaderFunction(float2 TextureCoordinate : TEXCOORD0) : COLOR0 
{ 
    float4 color = tex2D(TextureSampler, TextureCoordinate); 

    float value = (color.r + color.g + color.b)/3; 
    color.r = value; 
    color.g = value; 
    color.b = value; 

    return color; 
} 

//-------------------------- TECHNIQUES ---------------------------------------- 
// This technique is pretty simple - only one pass, and only a pixel shader 
technique BlackAndWhite 
{ 
    pass Pass1 
    { 
     PixelShader = compile ps_1_1 PixelShaderFunction(); 
    } 
} 

回答

0

修正了它。使用了錯誤的標誌的後處理器着色器初始化

是是:

sprite.Begin(SpriteFlags.DoNotSaveState); 
    postProc.Begin(FX.None); 

應該是:

sprite.Begin(SpriteFlags.DoNotSaveState); 
    postProc.Begin(FX.DoNotSaveState);