0

我正在尝试使用 SharpDX (DX11) 在屏幕上绘制一个三角形。无论出于何种原因,三角形似乎仅每隔一帧绘制一次。我的设备初始化代码如下所示:

public void Init()
    {
        renderForm = new RenderForm(Engine.GameTitle);
        renderForm.ClientSize = new Size(Engine.Settings.Screen.Width, Engine.Settings.Screen.Height);
        renderForm.MaximizeBox = false;

        var desc = new SwapChainDescription()
        {
          BufferCount = 2,
          ModeDescription = new ModeDescription(renderForm.ClientSize.Width, renderForm.ClientSize.Height, new Rational(60, 1), Format.R8G8B8A8_UNorm),
          IsWindowed = true,
          OutputHandle = renderForm.Handle,
          SampleDescription = new SampleDescription(1, 0),
          SwapEffect = SwapEffect.Sequential,
          Usage = Usage.RenderTargetOutput
        };  

        Device.CreateWithSwapChain(DriverType.Hardware, DeviceCreationFlags.Debug, desc, out device, out swapChain);
        deviceContext = device.ImmediateContext;  

        var factory = swapChain.GetParent<Factory>();
        factory.MakeWindowAssociation(renderForm.Handle, WindowAssociationFlags.IgnoreAll); 

        backBuffer = Texture2D.FromSwapChain<Texture2D>(swapChain, 0);
        renderView = new RenderTargetView(device, backBuffer);

        backBuffer.Dispose();

        deviceContext.OutputMerger.SetTargets(renderView);

        deviceContext.Rasterizer.SetViewports(new Viewport(0, 0, renderForm.ClientSize.Width, renderForm.ClientSize.Height, 0.0f, 1.0f));

        ProjectionMatrix = Matrix.PerspectiveFovLH(
            (float)(Math.PI / 4),
            (float)(renderForm.ClientSize.Width / renderForm.ClientSize.Height),
            nearPlane,
            farPlane);

        WorldMatrix = Matrix.Identity;

        renderForm.Location = new Point(Screen.PrimaryScreen.Bounds.Width / 2 - Engine.Settings.Screen.Width / 2, Screen.PrimaryScreen.Bounds.Height / 2 - Engine.Settings.Screen.Height / 2);
    }

渲染三角形的代码如下所示:

public void Render()
{
    DeviceContext context = D3DRenderer.Instance.GetDevice().ImmediateContext;

    context.InputAssembler.SetVertexBuffers(0, new VertexBufferBinding(VertexBuffer, Utilities.SizeOf<Vertex>(), 0));
    context.InputAssembler.SetIndexBuffer(IndexBuffer, Format.R32_UInt, 0);
    context.InputAssembler.PrimitiveTopology = PrimitiveTopology.TriangleList;
}

public void RenderShader(int indexCount)
{
    device.ImmediateContext.DrawIndexed(indexCount, 0, 0);
}

而 Render() 在 RenderShader() 之前被调用。除 Direct3D 警告外,任何函数均未返回错误消息:

D3D11:警告:ID3D11DeviceContext::DrawIndexed:顶点着色器单元插槽 0 处的常量缓冲区的大小太小(提供 64 字节,预计至少 192 字节)。

我的 MatrixBuffer 结构如下所示:

[StructLayout(LayoutKind.Sequential)]
internal struct MatrixBuffer
{
    public Matrix world;
    public Matrix view;
    public Matrix projection;
}

我一直在用不同的颜色每隔一帧清除一次后台缓冲区,以确保不正确交换后台缓冲区不是问题。这工作正常。

我很困惑为什么这现在不起作用。我希望有人知道这个问题的答案。

4

1 回答 1

1

女士们先生们,今天吸取教训..永远不要复制和粘贴教程代码。曾经。原来问题出在着色器渲染代码中。我复制声明的教程(并没有在这里发布,否则对你们来说可能已经很明显了)的世界/视图/投影矩阵声明如下:

public Matrix WorldMatrix { get; private set; }

然后我尝试这样做:

D3DRenderer.Instance.WorldMatrix.Transpose();

目前明显的原因是行不通的。有趣的是,它似乎每隔一帧都有效。为什么会这样我不知道。但是在将矩阵定义从私有集更改为设置后,现在一切正常。

于 2013-03-09T21:42:52.653 回答