无法在纹理上绘制rendertarget(d3d11)

时间:2014-12-29 20:50:23

标签: direct3d sharpdx render-to-texture

我已经编写了一些代码来使用SharpDX将纹理(_textureResource)绘制成一个表单。现在我想调整代码,使其首先渲染到另一个纹理(outputTexture),然后将outputTexture渲染到表单中。当我尝试这样做时,表单显示outputTexture的清晰颜色而不是_textureResource。对我做错了什么的想法?

感谢。

下面的代码显示了renderloop在使用outputTexture和不使用它之间的变化。

RenderLoop.Run(_form, () =>
{
    if (isFormClosed)
    {
        return;
    }

    bool useTexture = true;
    if (useTexture)
    {
        _context.ClearDepthStencilView(_depthStencilView, DepthStencilClearFlags.Depth, 1, 0);
        _context.OutputMerger.SetTargets(_depthStencilView, outputTextureRenderTargetView);
        _context.ClearRenderTargetView(outputTextureRenderTargetView, new Color4(1, 1, 0, 1));
        _context.PixelShader.SetShaderResource(0, _textureResource);
        _context.DrawIndexed(_indexCount, 0, 0);
        _context.Flush();
    }

    _context.ClearDepthStencilView(_depthStencilView, DepthStencilClearFlags.Depth, 1, 0);
    _context.OutputMerger.SetTargets(_depthStencilView, _renderTargetView);
    if (useTexture)
    {
        _context.PixelShader.SetShaderResource(0, outputTextureShaderResourceView);
    }
    else
    {
        _context.PixelShader.SetShaderResource(0, _textureResource);
    }
    _context.DrawIndexed(_indexCount, 0, 0);
    _swapChain.Present(0, PresentFlags.None);
  });

这些可能是初始化代码中最有趣的部分:

 int width = 800;
 int height = 600;
 ...
 var ProjectionMatrix = Matrix.PerspectiveLH(width, height, 10, -10);
 var WorldMatrix = Matrix.Identity;
 Vector3 position = new Vector3(width / 2, height / 2, -10);
 Vector3 lookAt = new Vector3(width / 2, height / 2, -9);
 Vector3 up = new Vector3(0, 1, 0);
 var ViewMatrix = Matrix.LookAtLH(position, lookAt, up);
 ...
var vertices = new[]
{
    // Bottom left.
    new Vertex()
    {
        position = new Vector3(0, 0, 0),
        texture = new Vector2(0, 1)
    },
    // Top left.
    new Vertex()
    {
        position = new Vector3(0, height, 0),
        texture = new Vector2(0, 0)
    },
    // Bottom right.
    new Vertex()
    {
        position = new Vector3(width, 0, 0),
        texture = new Vector2(1, 1)
    },
    // Top right.
    new Vertex()
    {
        position = new Vector3(width, height, 0),
        texture = new Vector2(1, 0)
    }
};

Texture2DDescription outputTextureDescription = new Texture2DDescription()
{
    ArraySize = 1,
    BindFlags = BindFlags.RenderTarget | BindFlags.ShaderResource,
    CpuAccessFlags = CpuAccessFlags.None,
    Format = Format.R32G32B32A32_Float,
    Height = width,
    Width = height,
    MipLevels = 1,
    OptionFlags = ResourceOptionFlags.None,
    SampleDescription = new SampleDescription(1, 0),
    Usage = ResourceUsage.Default
};
Texture2D outputTexture = new Texture2D(_device, outputTextureDescription);

var renderTargetViewDesc = new RenderTargetViewDescription()
{
    Format = outputTextureDescription.Format,
    Dimension = RenderTargetViewDimension.Texture2D
};
renderTargetViewDesc.Texture2D.MipSlice = 0;
var outputTextureRenderTargetView = new RenderTargetView(_device, outputTexture, renderTargetViewDesc);

var shaderResourceViewDesc = new ShaderResourceViewDescription()
{
    Format = outputTextureDescription.Format,
    Dimension = ShaderResourceViewDimension.Texture2D,
};
shaderResourceViewDesc.Texture2D.MipLevels = 1;
shaderResourceViewDesc.Texture2D.MostDetailedMip = 0;
// Create the render target view.
var outputTextureShaderResourceView = new ShaderResourceView(_device, outputTexture, shaderResourceViewDesc);

1 个答案:

答案 0 :(得分:1)

启用directx的调试输出向我显示模板视图和渲染目标分辨率不匹配。 问题中发布的代码不包含错误。