ResizeBuffers时DirectX无效调用

时间:2012-10-16 08:49:11

标签: c# directx slimdx

我正在编写一个非常简单的建模软件,而不是其他任何东西。 大约3周前,我对SwapChain.ResizeBuffers()函数没有任何实际问题。

我更换了PC,切换到Visual Studio Express 2012(从Pro 9开始),并使用相应的SlimDX.dll将我的解决方案切换到x64。

它仍然正常运行,但是当我调整托管视口的窗口大小时,它会得到:DXGI_ERROR_INVALID_CALL(-2005270527)。

快速搜索谷歌告诉我,战地3也可能与某些特定的驱动程序有这个问题。有可能吗?

我已经阅读了有关该功能的所有内容,并且无论如何我无法找到现在正在弄乱的变化。希望有人能看出我做错了什么。

    // Form we are attached to.
    private Dockable dock;

    // Rendering stuff.
    private Device device;
    private Viewport viewport;
    private SwapChain swapChain;
    private RenderTargetView renderView;
    private DepthStencilView depthView;

    public Renderer(Dockable form)
    {
        if (form == null)
            return;

        dock = form;

        CreateSwapchain();
        Resize();
    }

    private void CreateSwapchain()
    {
        // Swap Chain & Device
        SwapChainDescription description = new SwapChainDescription()
        {
            BufferCount = 1,
            Usage = Usage.RenderTargetOutput,
            OutputHandle = dock.Handle,
            IsWindowed = true,
            ModeDescription = new ModeDescription(dock.ClientSize.Width, dock.ClientSize.Height, new Rational(60, 1), Format.R8G8B8A8_UNorm),
            SampleDescription = new SampleDescription(1, 0),
            Flags = SwapChainFlags.AllowModeSwitch,
            SwapEffect = SwapEffect.Discard
        };

        Device.CreateWithSwapChain(DriverType.Hardware, DeviceCreationFlags.Debug, description, out device, out swapChain);
    }

    private void CreateRenderView()
    {
        // Dispose before resizing.
        if (renderView != null)
            renderView.Dispose();

        if (depthView != null)
            depthView.Dispose();

        swapChain.ResizeBuffers(0, 0, 0, Format.Unknown, 0); // ERROR : DXGI_ERROR_INVALID_CALL when resizing the window, but not when creating it.
        renderView = new RenderTargetView(device, Resource.FromSwapChain<Texture2D>(swapChain, 0));

        Texture2DDescription depthBufferDesc = new Texture2DDescription()
        {
            ArraySize = 1,
            BindFlags = BindFlags.DepthStencil,
            CpuAccessFlags = CpuAccessFlags.None,
            Format = Format.D16_UNorm,
            Height = dock.ClientSize.Height,
            Width = dock.ClientSize.Width,
            MipLevels = 1,
            OptionFlags = ResourceOptionFlags.None,
            SampleDescription = new SampleDescription(1, 0),
            Usage = ResourceUsage.Default
        };

        depthView = new DepthStencilView(device, new Texture2D(device, depthBufferDesc));
    }

    public void Resize()
    {
        CreateRenderView();

        viewport = new Viewport(0.0f, 0.0f, dock.ClientSize.Width, dock.ClientSize.Height);
        device.ImmediateContext.Rasterizer.SetViewports(viewport);
        device.ImmediateContext.OutputMerger.SetTargets(depthView, renderView);
    }

1 个答案:

答案 0 :(得分:3)

您需要先释放与交换链相关的所有资源,然后再调整它。

这包括渲染视图(您可以这样做),但在创建渲染目标视图时在资源上执行addref。

Resource.FromSwapChain<Texture2D>(swapChain, 0)

为纹理添加ref计数器。由于您没有缓存它,因此无法释放它。

所以你需要这样做:

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

然后才调用resize:

if (resource != null) { resource.Dispose(); }

刚刚在我的引擎上进行了测试并且它可以正常工作(你也是正确的0,它也适用)。