夏普DXDX11阿尔法混合

问题描述 投票:1回答:1

我正在尝试使用alpha混合与SharpDX。如果我在我的输出合并上设置了混合状态,什么都没有渲染,我完全不知道为什么。当从不设置混合状态时,一切都能正常工作。即使我用默认的混合描述设置混合状态,也没有任何渲染。我想可能是我遗漏了某些步骤,或者是我做的事情顺序不对,所以我就把我的东西贴上去,希望有人能指出来......。

我用下面的代码设置了一个BlendState:

bs = new BlendState(Devices.Device11, new BlendStateDescription());

var blendDesc = new RenderTargetBlendDescription(
    true,
    BlendOption.SourceAlpha,
    BlendOption.InverseSourceAlpha,
    BlendOperation.Add,
    BlendOption.One,
    BlendOption.Zero,
    BlendOperation.Add,
    ColorWriteMaskFlags.All);

bs.Description.RenderTarget[0] = blendDesc;

...这里是我的渲染循环的内容。如果我只是注释掉context.OutputMerger.SetBlendState(bs),我的meshes渲染得很好(没有任何混合)。

var context = Devices.Device11.ImmediateContext;

context.ClearDepthStencilView(DepthStencilView, DepthStencilClearFlags.Depth, 1.0f, 0);
context.ClearRenderTargetView(RenderTargetView, new Color4());

context.OutputMerger.SetTargets(DepthStencilView, RenderTargetView);
context.OutputMerger.SetBlendState(bs);
context.Rasterizer.State = rs;
context.Rasterizer.SetViewport(Viewport);

context.VertexShader.SetConstantBuffer(0, viewProjBuffer);
context.UpdateSubresource(Camera.ViewProjection.ToFloatArray(), viewProjBuffer);

Dictionary<Mesh, Buffer> vBuffers = VertexBuffers.ToDictionary(k => k.Key, v => v.Value);
Dictionary<Mesh, Buffer> iBuffers = IndexBuffers.ToDictionary(k => k.Key, v => v.Value);

foreach (var mesh in vBuffers.Keys)
{
    if (mesh.MeshType == MeshType.LineStrip)
    {
        context.InputAssembler.PrimitiveTopology = PrimitiveTopology.LineStrip;
        context.InputAssembler.InputLayout = Effects.LineEffect.InputLayout;
        context.VertexShader.Set(Effects.LineEffect.VertexShader);
        context.PixelShader.Set(Effects.LineEffect.PixelShader);
    }
    else
    {
        context.InputAssembler.PrimitiveTopology = PrimitiveTopology.TriangleList;
        context.InputAssembler.InputLayout = Effects.FaceEffect.InputLayout;
        context.VertexShader.Set(Effects.FaceEffect.VertexShader);
        context.PixelShader.Set(Effects.FaceEffect.PixelShader);
    }

    context.InputAssembler.SetVertexBuffers(0, new VertexBufferBinding(vBuffers[mesh], GetMeshStride(mesh) * 4, 0));
    context.InputAssembler.SetIndexBuffer(iBuffers[mesh], Format.R32_UInt, 0);

    context.DrawIndexed(mesh.IndexUsage, 0, 0);
}

context.ResolveSubresource(RenderTarget, 0, SharedTexture, 0, Format.B8G8R8A8_UNorm);
context.Flush();

我正在渲染一个纹理,这个纹理是用下面的纹理描述初始化的。

Texture2DDescription colorDesc = new Texture2DDescription
{
    BindFlags = BindFlags.RenderTarget | BindFlags.ShaderResource,
    Format = Format.B8G8R8A8_UNorm,
    Width = width,
    Height = height,
    MipLevels = 1,
    SampleDescription = new SampleDescription(8, 32),
    Usage = ResourceUsage.Default,
    OptionFlags = ResourceOptionFlags.Shared,
    CpuAccessFlags = CpuAccessFlags.None,
    ArraySize = 1
};

对我来说,渲染到一个纹理并使用该格式是很重要的。我想,也许混合可以用特定的格式,但我找不到任何信息来说明这类问题。

我也在进行多采样,这就是为什么我在渲染方法的最后调用ResolveSubresource(...)。我复制的纹理与RenderTarget的描述完全相同,只是SampleDescription不同。

说到多采样,这里是我使用的RasterizerState。

rs = new RasterizerState(Devices.Device11, new RasterizerStateDescription()
{
    FillMode = FillMode.Solid,
    CullMode = CullMode.Back,
    IsFrontCounterClockwise = true,
    DepthBias = 0,
    DepthBiasClamp = 0,
    SlopeScaledDepthBias = 0,
    IsDepthClipEnabled = true,
    IsScissorEnabled = false,
    IsMultisampleEnabled = true,
    IsAntialiasedLineEnabled = true
});

我正在使用一个着色器进行渲染 基本上是一个 "Hello World "着色器 加上一个摄像机矩阵和基本的正常方向照明。我已经验证了我的顶点alpha值是它们应该有的,就像它们被填充到顶点缓冲区一样......即使我在像素着色器的最后硬编码它们的alpha值为1,只要我使用BlendState,我仍然什么都没有得到。在不使用BlendState的情况下,无论alpha值如何,所有的东西都会按照预期渲染不透明。我甚至尝试在着色器中实现混合状态,但似乎没有任何效果。所有的东西看起来都像没有定义混合状态一样渲染。

如果有关系的话,我的设备使用的是FeatureLevel.Level.Level_11_0。

不幸的是,这是我必须要去做的最多的事情。就像我说的,这个问题对我来说完全是个谜。

c# directx directx-11 sharpdx
1个回答
0
投票

只是想为将来来这里的人更新一下。对我有用的东西很简单。

            BlendStateDescription blendStateDescription = new BlendStateDescription
            {
                AlphaToCoverageEnable = false,
            };

            blendStateDescription.RenderTarget[0].IsBlendEnabled = true;
            blendStateDescription.RenderTarget[0].SourceBlend = BlendOption.SourceAlpha;
            blendStateDescription.RenderTarget[0].DestinationBlend = BlendOption.InverseSourceAlpha;
            blendStateDescription.RenderTarget[0].BlendOperation = BlendOperation.Add;
            blendStateDescription.RenderTarget[0].SourceAlphaBlend = BlendOption.Zero;
            blendStateDescription.RenderTarget[0].DestinationAlphaBlend = BlendOption.Zero;
            blendStateDescription.RenderTarget[0].AlphaBlendOperation = BlendOperation.Add;
            blendStateDescription.RenderTarget[0].RenderTargetWriteMask = ColorWriteMaskFlags.All;

            this._context.OutputMerger.BlendState = new BlendState(_device,blendStateDescription);

我还在我的着色器中处理了alpha组件 以防你想手动添加透明度到模型中,伪代码。

float4 PixelShaderMain( PixelShaderArgs pixelShaderArgs ) 
    : SV_Target
{
    float u = pixelShaderArgs.col.x;
    float v = pixelShaderArgs.col.y;
    float4 color = ShaderTexture.Load(int3(convertUVToPixel(u,v),0));
    return float4(color.r,color.g,color.b,0.5); // 50% transparency
}

希望这能帮助到别人,而不是得到一个基本没有任何指向的死页。祝大家编码愉快!

© www.soinside.com 2019 - 2024. All rights reserved.