SharpDx SpriteBatch中的自定义效果

我试图将一个XNA项目的模糊效果移交给SharpDx,我得到一个“空引用exception”,我无法find原因。

private void blurShadow(RenderTarget2D to, RenderTarget2D from, int dir) { _graphicsDevice.SetRenderTargets(to); _shadowBlurEffect.CurrentTechnique.Passes[dir].Apply(); _spriteBatch.Begin(SpriteSortMode.Deferred, _shadowBlurEffect); _spriteBatch.Draw(from, Vector2.Zero, Color.White); _spriteBatch.End(); // <-- Null Reference Exception here } public void Draw() { _graphicsDevice.SetRenderTargets(_graphicsDevice.DepthStencilBuffer, ShadowDepthTarget); _graphicsDevice.Clear(Color.White); // Clear the render target to 1 (infinite depth) foreach (var obj in ShadowCastingObjects) obj.Draw(Camera, DrawingReason.ShadowDepthMap, this); blurShadow(_shadowBlurTarg, ShadowDepthTarget, 0); blurShadow(ShadowDepthTarget, _shadowBlurTarg, 1); _graphicsDevice.SetRenderTargets(_graphicsDevice.DepthStencilBuffer, _graphicsDevice.BackBuffer); } 

渲染目标是这样创建的:

  ShadowDepthTarget = RenderTarget2D.New(_graphicsDevice, width, height, PixelFormat.R16G16.Float); _shadowBlurTarg = RenderTarget2D.New(_graphicsDevice, width, height, PixelFormat.R16G16.Float); 

这是着色器代码,我已经尝试了variuos版本的方法签名 – 徒劳无功:

 Texture2D Texture; SamplerState TextureSampler; // Precalculated weights and offsets float weights[15] = { 0.1061154, 0.1028506, 0.1028506, 0.09364651, 0.09364651, 0.0801001, 0.0801001, 0.06436224, 0.06436224, 0.04858317, 0.04858317, 0.03445063, 0.03445063, 0.02294906, 0.02294906 }; float offsets[15] = { 0, 0.00125, -0.00125, 0.002916667, -0.002916667, 0.004583334, -0.004583334, 0.00625, -0.00625, 0.007916667, -0.007916667, 0.009583334, -0.009583334, 0.01125, -0.01125 }; float4 BlurHorizontal( float2 UV : TEXCOORD0) : SV_Target { float4 output = float4(0, 0, 0, 1); for (int i = 0; i < 15; i++) output += Texture.Sample(TextureSampler, UV + float2(offsets[i], 0)) * weights[i]; return output; } float4 BlurVertical( float2 UV : TEXCOORD0) : SV_Target { float4 output = float4(0, 0, 0, 1); for (int i = 0; i < 15; i++) output += Texture.Sample(TextureSampler, UV + float2(0, offsets[i])) * weights[i]; return output; } technique Tech { pass Horizontal { SetGeometryShader(0); SetVertexShader(0); SetPixelShader(CompileShader(ps_4_0, BlurHorizontal())); } pass Verical { SetGeometryShader(0); SetVertexShader(0); SetPixelShader(CompileShader(ps_4_0, BlurVertical())); } } 

事实certificate,传递给SpriteBatch.Begin的效果必须遵循特定的约定:

 Texture2D<float4> Texture : register(t0); // SpriteBatch expects that default texture sampler parameter will have name 'TextureSampler' sampler TextureSampler : register(s0); // SpriteBatch expects that default vertex transform parameter will have name 'MatrixTransform' row_major float4x4 MatrixTransform; void VSMain( inout float4 color : COLOR0, inout float2 texCoord : TEXCOORD0, inout float4 position : SV_Position) { position = mul(position, MatrixTransform); } // Precalculated weights and offsets float weights[15] = { 0.1061154, 0.1028506, 0.1028506, 0.09364651, 0.09364651, 0.0801001, 0.0801001, 0.06436224, 0.06436224, 0.04858317, 0.04858317, 0.03445063, 0.03445063, 0.02294906, 0.02294906 }; float offsets[15] = { 0, 0.00125, -0.00125, 0.002916667, -0.002916667, 0.004583334, -0.004583334, 0.00625, -0.00625, 0.007916667, -0.007916667, 0.009583334, -0.009583334, 0.01125, -0.01125 }; // Blurs the input image horizontally float4 PSBlurHorizontal( float4 color: COLOR, float2 UV : TEXCOORD0) : SV_Target { float4 output = float4(0, 0, 0, 1); for (int i = 0; i < 15; i++) output += Texture.Sample(TextureSampler, UV + float2(offsets[i], 0)) * weights[i]; return output * color; } // Blurs the input image vertically float4 PSBlurVertical( float4 color: COLOR, float2 UV : TEXCOORD0) : SV_Target { float4 output = float4(0, 0, 0, 1); for (int i = 0; i < 15; i++) output += Texture.Sample(TextureSampler, UV + float2(0, offsets[i])) * weights[i]; return output * color; } technique Tech { pass Horizontal { SetGeometryShader(0); SetVertexShader(CompileShader(vs_4_0, VSMain())); SetPixelShader(CompileShader(ps_4_0, PSBlurHorizontal())); } pass Verical { SetGeometryShader(0); SetVertexShader(CompileShader(vs_4_0, VSMain())); SetPixelShader(CompileShader(ps_4_0, PSBlurVertical())); } }