使用DirectX 11在渲染目标上绘制/删除几何形状

时间:2015-11-11 16:38:24

标签: directx directx-11 direct3d11

我正在尝试在渲染目标上绘制/删除几何形状,以显示图像。

在每个帧渲染期间,通过纹理映射更新资源,将新图像渲染到渲染目标,该映射完全按预期工作。

现在,我正在尝试在渲染目标的顶部绘制一个填充纯色的新几何形状,这只在每10帧渲染过程中完成。

但是目前我还是应该如何解决这个问题。

我在带有C#的Windows 7 PC上使用DirectX 11(对于DirectX,使用slimDx或sharpDx)。

任何建议都会很棒。

感谢。

代码: 在rendring循环中,我添加了下面的代码来绘制叠加层,在我的情况下是三角形。

       var device = this.Device;
        var context = device.ImmediateContext;
        var effectsFileResource = Properties.Resources.ShapeEffect;
        ShaderBytecode shaderByteCode = ShaderBytecode.Compile(effectsFileResource, "fx_5_0", ShaderFlags.EnableStrictness | ShaderFlags.Debug, EffectFlags.None);

        var effect = new Effect(device, shaderByteCode);

        // create triangle vertex data, making sure to rewind the stream afterward
        var verticesTriangle = new DataStream(VertexPositionColor.SizeInBytes * 3, true, true);
        verticesTriangle.Write(new VertexPositionColor(new Vector3(0.0f, 0.5f, 0.5f), new Color4(1.0f, 0.0f, 0.0f, 1.0f)));
        verticesTriangle.Write(new VertexPositionColor(new Vector3(0.5f, -0.5f, 0.5f), new Color4(0.0f, 1.0f, 0.0f, 1.0f)));
        verticesTriangle.Write(new VertexPositionColor(new Vector3(-0.5f, -0.5f, 0.5f), new Color4(0.0f, 0.0f, 1.0f, 1.0f)));

        verticesTriangle.Position = 0;

        // create the triangle vertex layout and buffer
        var layoutColor = new InputLayout(device, effect.GetTechniqueByName("Color").GetPassByIndex(0).Description.Signature, VertexPositionColor.inputElements);
        var vertexBufferColor = new SharpDX.Direct3D11.Buffer(device, verticesTriangle, (int)verticesTriangle.Length, ResourceUsage.Default, BindFlags.VertexBuffer, CpuAccessFlags.None, ResourceOptionFlags.None, 0);
        verticesTriangle.Close();

        var srv = new ShaderResourceView(device, this.RenderTarget);
        effect.GetVariableByName("g_Overlay").AsShaderResource().SetResource(srv);

        // Think of the shared textureD3D10 as an overlay.
        // The overlay needs to show the 2d content but let the underlying triangle (or whatever)
        // show thru, which is accomplished by blending.
        var bsd = new BlendStateDescription();
        bsd.RenderTarget[0].IsBlendEnabled = true;
        bsd.RenderTarget[0].SourceBlend = BlendOption.SourceColor;
        bsd.RenderTarget[0].DestinationBlend = BlendOption.BlendFactor;
        bsd.RenderTarget[0].BlendOperation = BlendOperation.Add;
        bsd.RenderTarget[0].SourceAlphaBlend = BlendOption.One;
        bsd.RenderTarget[0].DestinationAlphaBlend = BlendOption.Zero;
        bsd.RenderTarget[0].AlphaBlendOperation = BlendOperation.Add;
        bsd.RenderTarget[0].RenderTargetWriteMask = ColorWriteMaskFlags.All;

        var blendStateTransparent = new BlendState(device, bsd);

        context.InputAssembler.InputLayout = layoutColor;
        context.InputAssembler.PrimitiveTopology = PrimitiveTopology.TriangleStrip;
        context.InputAssembler.SetVertexBuffers(0, new VertexBufferBinding(vertexBufferColor, VertexPositionColor.SizeInBytes, 0));
        context.OutputMerger.BlendState = blendStateTransparent;

        var currentTechnique = effect.GetTechniqueByName("Color");
        for (var pass = 0; pass < currentTechnique.Description.PassCount; ++pass)
        {
            using (var effectPass = currentTechnique.GetPassByIndex(pass))
            {
                System.Diagnostics.Debug.Assert(effectPass.IsValid, "Invalid EffectPass");
                effectPass.Apply(context);
            }
            context.Draw(3, 0);
        };

        srv.Dispose();

以下是效果的着色器文件:

Texture2D g_Overlay;

SamplerState g_samLinear
{
    Filter = MIN_MAG_MIP_LINEAR;
    AddressU = CLAMP;
    AddressV = CLAMP;
 };

 // ------------------------------------------------------
 // A shader that accepts Position and Color
 // ------------------------------------------------------

struct ColorVS_IN
{
    float4 pos : POSITION;
    float4 col : COLOR;
 };

struct ColorPS_IN
{
    float4 pos : SV_POSITION;
    float4 col : COLOR;
};

ColorPS_IN ColorVS(ColorVS_IN input)
{
    ColorPS_IN output = (ColorPS_IN)0;
    output.pos = input.pos;
    output.col = input.col;
    return output;
  }

 float4 ColorPS(ColorPS_IN input) : SV_Target
 {
    return input.col;
 }

 // ------------------------------------------------------
 // A shader that accepts Position and Texture
 // Used as an overlay
 // ------------------------------------------------------

 struct OverlayVS_IN
 {
     float4 pos : POSITION;
     float2 tex : TEXCOORD0;
  };

  struct OverlayPS_IN
  {
    float4 pos : SV_POSITION;
    float2 tex : TEXCOORD0;
  };

  OverlayPS_IN OverlayVS(OverlayVS_IN input)
  {
     OverlayPS_IN output = (OverlayPS_IN)0;
     output.pos = input.pos;
     output.tex = input.tex;
     return output;
   }

  float4 OverlayPS(OverlayPS_IN input) : SV_Target
  {
     float4 color = g_Overlay.Sample(g_samLinear, input.tex);
     return color;
  }

 // ------------------------------------------------------
 // Techniques
 // ------------------------------------------------------

 technique11 Color
 {
    pass P0
    {
        SetGeometryShader(0);
        SetVertexShader(CompileShader(vs_4_0, ColorVS()));
        SetPixelShader(CompileShader(ps_4_0, ColorPS()));
     }
  }

  technique11 Overlay
  {
    pass P0
    {
        SetGeometryShader(0);
        SetVertexShader(CompileShader(vs_4_0, OverlayVS()));
        SetPixelShader(CompileShader(ps_4_0, OverlayPS()));
    }
  }

以上代码取自:SharedResources using SharpDX

0 个答案:

没有答案