C# SharpDX像素着色器对于不同的SpriteBatch和DrawQuad需要不同的参数。可修复的?

C# SharpDX像素着色器对于不同的SpriteBatch和DrawQuad需要不同的参数。可修复的?,c#,directx,hlsl,sharpdx,pixel-shader,C#,Directx,Hlsl,Sharpdx,Pixel Shader,我对构建图形引擎还不熟悉,在一个特定的问题上有些困惑 我有一个问题,让像素着色器使用相同的参数集工作。使我不得不为每种方法创建两种不同的技术。如果我尝试对这两个参数使用相同的参数,其中一个参数将不返回任何内容 我的目标是只为着色器使用一组参数,消除我使用的每种技术的特定于精灵的版本 以下是我目前必须做的事情: HLSL像素着色器 Texture2D Texture : register(t0); SamplerState TextureSampler : register(s0); Textur

我对构建图形引擎还不熟悉,在一个特定的问题上有些困惑

我有一个问题,让像素着色器使用相同的参数集工作。使我不得不为每种方法创建两种不同的技术。如果我尝试对这两个参数使用相同的参数,其中一个参数将不返回任何内容

我的目标是只为着色器使用一组参数,消除我使用的每种技术的特定于精灵的版本

以下是我目前必须做的事情: HLSL像素着色器

Texture2D Texture : register(t0);
SamplerState TextureSampler : register(s0);
Texture2D TextureAlpha;

cbuffer ProjectionMatrix : register(b0)
{
    row_major float4x4 MatrixTransform : packoffset(c0);
};

void VS(
    inout float4 color    : COLOR0,
    inout float2 texCoord : TEXCOORD0,
    inout float4 position : SV_POSITION)
{
    position = mul(position, MatrixTransform);
}

//Method required for using DrawQuad
float4 PS(
    float2 tex : TEXCOORD0
    ) : SV_TARGET
{
    float3 tcolor = Texture.Sample(TextureSampler, tex).rgb * TextureAlpha.Sample(TextureSampler, tex).a;
    float alpha = TextureAlpha.Sample(TextureSampler, tex).a * Texture.Sample(TextureSampler, tex);

    return float4(tcolor, alpha);
}

//Method required for using in SpriteBatches
float4 PSSprite(
    float4 color    : COLOR0,
    float2 tex : TEXCOORD0
    ) : SV_TARGET
{
    return PS(tex);
}

technique MaskSprite
{
    pass
    {
        Profile = 9.3;
        //Sprite's also require the VS to be set as well.
        VertexShader = compile vs_4_0_level_9_1 VS();
        PixelShader = compile ps_4_0_level_9_1 PSSprite();
    }
}

technique Mask
{
    pass
    {
        Profile = 9.3;
        PixelShader = compile ps_4_0_level_9_1 PS();
    }
}
LoadContent()代码

protected override void LoadContent()
{
    //...  
      
    //render target to mask
    var backDesc = GraphicsDevice.BackBuffer.Description;
    renderTargetMask = ToDisposeContent(RenderTarget2D.New(GraphicsDevice, backDesc.Width, backDesc.Height, 1, backDesc.Format));

    //sprite batch initialization
    spriteBatch = ToDisposeContent(new SpriteBatch(GraphicsDevice));

    //load in fx and textures 
    ballsTexture = Content.Load<Texture2D>("Balls");
    alphaMapTexture= Content.Load<Texture2D>("AlphaMap");
    maskEffect = Content.Load<Effect>("Mask");

    //...
}
protected override void Draw(GameTime gameTime)
{
    //...
    maskEffect.CurrentTechnique = bloomEffect.Techniques["MaskSprite"];
    maskEffect.Parameters["Destination"].SetResource(alphaMapTexture); //Texture2D

    spriteBatch.Begin(
                    SpriteSortMode.Deferred, 
                    GraphicsDevice.BlendStates.NonPremultiplied, 
                    null, null, null, maskEffect);

    spriteBatch.Draw(
                    ballsTexture, //Texture2D
                    new Vector2(10, 10),
                    new Rectangle(0, 0, ballsDestTexture.Width, ballsDestTexture.Height),
                    Color.White,
                    0.0f,
                    new Vector2(0, 0),
                    Vector2.One,
                    SpriteEffects.None,
                    0f);
    
    spriteBatch.End();
    //...
}
protected override void Draw(GameTime gameTime)
{
    //Draw stuff to renderTargetMask

    //...
    maskEffect.CurrentTechnique = maskEffect.Techniques["Mask"];
    maskEffect.Parameters["Texture"].SetResource(renderTargetMask);
    maskEffect.Parameters["TextureAlpha"].SetResource(ballsDestTexture);
    maskEffect.Parameters["TextureSampler"]
            .SetResource(GraphicsDevice.SamplerStates.PointClamp);
    var pQuad = new PrimitiveQuad(GraphicsDevice);
    pQuad.Draw(maskEffect.CurrentTechnique.Passes[0]);
    //...
}
绘制四元代码

protected override void LoadContent()
{
    //...  
      
    //render target to mask
    var backDesc = GraphicsDevice.BackBuffer.Description;
    renderTargetMask = ToDisposeContent(RenderTarget2D.New(GraphicsDevice, backDesc.Width, backDesc.Height, 1, backDesc.Format));

    //sprite batch initialization
    spriteBatch = ToDisposeContent(new SpriteBatch(GraphicsDevice));

    //load in fx and textures 
    ballsTexture = Content.Load<Texture2D>("Balls");
    alphaMapTexture= Content.Load<Texture2D>("AlphaMap");
    maskEffect = Content.Load<Effect>("Mask");

    //...
}
protected override void Draw(GameTime gameTime)
{
    //...
    maskEffect.CurrentTechnique = bloomEffect.Techniques["MaskSprite"];
    maskEffect.Parameters["Destination"].SetResource(alphaMapTexture); //Texture2D

    spriteBatch.Begin(
                    SpriteSortMode.Deferred, 
                    GraphicsDevice.BlendStates.NonPremultiplied, 
                    null, null, null, maskEffect);

    spriteBatch.Draw(
                    ballsTexture, //Texture2D
                    new Vector2(10, 10),
                    new Rectangle(0, 0, ballsDestTexture.Width, ballsDestTexture.Height),
                    Color.White,
                    0.0f,
                    new Vector2(0, 0),
                    Vector2.One,
                    SpriteEffects.None,
                    0f);
    
    spriteBatch.End();
    //...
}
protected override void Draw(GameTime gameTime)
{
    //Draw stuff to renderTargetMask

    //...
    maskEffect.CurrentTechnique = maskEffect.Techniques["Mask"];
    maskEffect.Parameters["Texture"].SetResource(renderTargetMask);
    maskEffect.Parameters["TextureAlpha"].SetResource(ballsDestTexture);
    maskEffect.Parameters["TextureSampler"]
            .SetResource(GraphicsDevice.SamplerStates.PointClamp);
    var pQuad = new PrimitiveQuad(GraphicsDevice);
    pQuad.Draw(maskEffect.CurrentTechnique.Passes[0]);
    //...
}

你为什么不对两者使用相同的技术呢<代码>掩码和
PS
就足够了。顶点声明可以有比着色器实际使用的属性更多的属性。哦,我想我没有提到为什么我必须这样做。如果在SpriteBatch中使用
Mask
,或在DrawQuad中使用
MaskSprite
,则不会显示任何内容。只是什么也没回来,奇怪。是否有任何错误消息?你使用哪个版本的DX?哦,我刚刚意识到Sprite批处理需要VS和PS。(我编辑代码以反映这一点)它使用DX11使用SharpDX工具套件在DX11中,你必须为你使用的每个顶点类型和效果过程创建一个输入布局。所以这里需要两个输入布局。一个用于法线顶点(仅包含tex坐标),另一个用于精灵批次顶点(包含附加颜色)。两种输入布局都使用
掩码
技术。这是您已经做过的还是由SharpDX内部处理的(SharpDX还没有做多少)?如果是内部处理的,这可能是没有考虑到的。