Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error when resizing window #31

Closed
KangUnia opened this issue Jan 27, 2023 · 2 comments
Closed

Error when resizing window #31

KangUnia opened this issue Jan 27, 2023 · 2 comments

Comments

@KangUnia
Copy link

KangUnia commented Jan 27, 2023

First of all I am using this library very gratefully.
I am testing directx using winui3 swapchain.

However, when resizing the window, Device.CreateRenderTargetView throws an error. [COM object that has been separated from its underlying RCW cannot be used.
]

I don't understand this error, so I can't fix it.

please help ........

public void Resize(uint width, uint height)
{
mDeviceContext.Object.OMSetRenderTargets(0, null, null);
mDeviceContext.Object.Flush();
mRenderTargetView.Dispose();
mRenderTargetView = null;

var hResult = mSwapChain.Object.ResizeBuffers(0, width, height, DXGI_FORMAT.DXGI_FORMAT_UNKNOWN, 0);

var frameBuffer = mSwapChain.GetBuffer<ID3D11Texture2D>(0);
mRenderTargetView = mDevice.CreateRenderTargetView(frameBuffer);

...
}

@KangUnia
Copy link
Author

use ComObject new device

@stephensheehy
Copy link

stephensheehy commented Sep 18, 2024

For reference by those who come after us:

I had the same issue but for me creating a new device was not an option*, nor was it necessary... In my case the error was allowing the IDXGIDevice1 I used to create my swap chain to get cleaned by the garbage collector so all I had to do was hold a reference to it.

Something like the following code works just fine:

public class WindowsCanvas : SwapChainPanel
{
    // some of these are members because they are reused and passed around
    // some (dxgiDevice) just need to persist in memory to sustain valid device state
    private IComObject<ID3D11Device>            dxDevice;
    private IComObject<IDXGIDevice1>            dxgiDevice;
    private IComObject<ID3D11DeviceContext>     dxDeviceContext;
    private IComObject<IDXGISwapChain1>         dxSwapChain;
    private IComObject<ID3D11RenderTargetView>  dxRenderTargetView;
    private IComObject<ID3D11DepthStencilView>  dxDepthStencilView;
    
    void ResizeCanvas()
    {
        dxDeviceContext.Object.OMSetRenderTargets(0, null, null);
        dxRenderTargetView?.Dispose();
        dxRenderTargetView = null;
        dxDepthStencilView?.Dispose();
        dxDepthStencilView = null;

        var width = (int)Math.Ceiling(this.ActualWidth);
        var height = (int)Math.Ceiling(this.ActualHeight);
        width = width <= 0 ? 0 : width;
        height = height <= 0 ? 0 : height;

        dxSwapChain.ResizeBuffers(2, (uint)width, (uint)height, DXGI_FORMAT.DXGI_FORMAT_B8G8R8A8_UNORM, 0);

        var framebufferTexture = dxSwapChain.GetBuffer<ID3D11Texture2D>(0);
        var framebufferDesc = new D3D11_RENDER_TARGET_VIEW_DESC
        {
            Format = DXGI_FORMAT.DXGI_FORMAT_B8G8R8A8_UNORM_SRGB,
            ViewDimension = D3D11_RTV_DIMENSION.D3D11_RTV_DIMENSION_TEXTURE2D
        };

        dxRenderTargetView = dxDevice.CreateRenderTargetView(framebufferTexture, framebufferDesc);

        var framebufferDepthDesc = framebufferTexture.GetDesc();
        framebufferTexture?.Dispose();

        framebufferDepthDesc.Format = DXGI_FORMAT.DXGI_FORMAT_D24_UNORM_S8_UINT;
        framebufferDepthDesc.BindFlags = (uint)D3D11_BIND_FLAG.D3D11_BIND_DEPTH_STENCIL;
        var framebufferDepthTexture = dxDevice.CreateTexture2D<ID3D11Texture2D>(framebufferDepthDesc);
        
        dxDepthStencilView = dxDevice.CreateDepthStencilView(framebufferDepthTexture);
        
        framebufferDepthTexture?.Dispose();
    }
}
  • Creating a new device is in itself inefficient but in addition I have already passed the device to a native c++ library and swapping it out will cause a number of other issues.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants