Skip to content

Commit

Permalink
D3D11: Support window resize
Browse files Browse the repository at this point in the history
  • Loading branch information
hrydgard committed Feb 14, 2017
1 parent a9e267a commit 77d0877
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 64 deletions.
19 changes: 2 additions & 17 deletions Windows/GPU/D3D11Context.cpp
Expand Up @@ -84,33 +84,18 @@ bool D3D11Context::Init(HINSTANCE hInst, HWND wnd, std::string *error_message) {
#endif

draw_ = Draw::T3DCreateD3D11Context(device_, context_, hWnd_);
draw_->HandleEvent(Draw::Event::GOT_BACKBUFFER);
return true;
}

void D3D11Context::Resize() {
draw_->HandleEvent(Draw::Event::LOST_BACKBUFFER);
draw_->HandleEvent(Draw::Event::RESIZED);
// This should only be called from the emu thread.
/*
int xres, yres;
GetRes(hWnd, xres, yres);
bool w_changed = pp.BackBufferWidth != xres;
bool h_changed = pp.BackBufferHeight != yres;
if (device_ && (w_changed || h_changed)) {
pp.BackBufferWidth = xres;
pp.BackBufferHeight = yres;
HRESULT hr = device_->Reset(&pp);
if (FAILED(hr)) {
ERROR_LOG_REPORT(G3D, "Unable to reset D3D device");
PanicAlert("Unable to reset D3D11 device");
}
}
*/
draw_->HandleEvent(Draw::Event::GOT_BACKBUFFER);
}

void D3D11Context::Shutdown() {
draw_->HandleEvent(Draw::Event::LOST_BACKBUFFER);
delete draw_;
draw_ = nullptr;
context_->ClearState();
Expand Down
120 changes: 73 additions & 47 deletions ext/native/thin3d/thin3d_d3d11.cpp
Expand Up @@ -109,13 +109,7 @@ class D3D11DrawContext : public DrawContext {
}
}

void HandleEvent(Event ev) override {
switch (ev) {
case Event::PRESENT_REQUESTED:
swapChain_->Present(0, 0);
break;
}
}
void HandleEvent(Event ev) override;

private:
void ApplyCurrentState();
Expand Down Expand Up @@ -213,52 +207,84 @@ D3D11DrawContext::D3D11DrawContext(ID3D11Device *device, ID3D11DeviceContext *co
dxgiFactory->MakeWindowAssociation(hWnd_, DXGI_MWA_NO_ALT_ENTER);
dxgiFactory->Release();

if (FAILED(hr))
return;

// Create a render target view
ID3D11Texture2D* pBackBuffer = nullptr;
hr = swapChain_->GetBuffer(0, __uuidof(ID3D11Texture2D), reinterpret_cast<void**>(&pBackBuffer));
if (FAILED(hr))
return;

hr = device_->CreateRenderTargetView(pBackBuffer, nullptr, &bbRenderTargetView_);
pBackBuffer->Release();
if (FAILED(hr))
return;
CreatePresets();
}

// Create depth stencil texture
D3D11_TEXTURE2D_DESC descDepth{};
descDepth.Width = width;
descDepth.Height = height;
descDepth.MipLevels = 1;
descDepth.ArraySize = 1;
descDepth.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
descDepth.SampleDesc.Count = 1;
descDepth.SampleDesc.Quality = 0;
descDepth.Usage = D3D11_USAGE_DEFAULT;
descDepth.BindFlags = D3D11_BIND_DEPTH_STENCIL;
descDepth.CPUAccessFlags = 0;
descDepth.MiscFlags = 0;
hr = device_->CreateTexture2D(&descDepth, nullptr, &bbDepthStencilTex_);

// Create the depth stencil view
D3D11_DEPTH_STENCIL_VIEW_DESC descDSV{};
descDSV.Format = descDepth.Format;
descDSV.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2D;
descDSV.Texture2D.MipSlice = 0;
hr = device_->CreateDepthStencilView(bbDepthStencilTex_, &descDSV, &bbDepthStencilView_);
D3D11DrawContext::~D3D11DrawContext() {

context_->OMSetRenderTargets(1, &bbRenderTargetView_, bbDepthStencilView_);
}

curRenderTargetView_ = bbRenderTargetView_;
curDepthStencilView_ = bbDepthStencilView_;
void D3D11DrawContext::HandleEvent(Event ev) {
switch (ev) {
case Event::PRESENT_REQUESTED:
swapChain_->Present(0, 0);
break;
case Event::RESIZED: {
int width;
int height;
GetRes(hWnd_, width, height);
swapChain_->ResizeBuffers(0, width, height, DXGI_FORMAT_UNKNOWN, 0);
break;
}
case Event::LOST_BACKBUFFER: {
if (curRenderTargetView_ == bbRenderTargetView_ || curDepthStencilView_ == bbDepthStencilView_) {
ID3D11RenderTargetView *view = nullptr;
context_->OMSetRenderTargets(1, &view, nullptr);
curRenderTargetView_ = nullptr;
curDepthStencilView_ = nullptr;
}
bbRenderTargetView_->Release();
bbRenderTargetView_ = nullptr;
bbDepthStencilView_->Release();
bbDepthStencilView_ = nullptr;
bbDepthStencilTex_->Release();
bbDepthStencilTex_ = nullptr;
break;
}
case Event::GOT_BACKBUFFER: {
int width;
int height;
GetRes(hWnd_, width, height);
// Create a render target view
ID3D11Texture2D* pBackBuffer = nullptr;
HRESULT hr = swapChain_->GetBuffer(0, __uuidof(ID3D11Texture2D), reinterpret_cast<void**>(&pBackBuffer));
if (FAILED(hr))
return;

hr = device_->CreateRenderTargetView(pBackBuffer, nullptr, &bbRenderTargetView_);
pBackBuffer->Release();
if (FAILED(hr))
return;

// Create depth stencil texture
D3D11_TEXTURE2D_DESC descDepth{};
descDepth.Width = width;
descDepth.Height = height;
descDepth.MipLevels = 1;
descDepth.ArraySize = 1;
descDepth.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
descDepth.SampleDesc.Count = 1;
descDepth.SampleDesc.Quality = 0;
descDepth.Usage = D3D11_USAGE_DEFAULT;
descDepth.BindFlags = D3D11_BIND_DEPTH_STENCIL;
descDepth.CPUAccessFlags = 0;
descDepth.MiscFlags = 0;
hr = device_->CreateTexture2D(&descDepth, nullptr, &bbDepthStencilTex_);

CreatePresets();
}
// Create the depth stencil view
D3D11_DEPTH_STENCIL_VIEW_DESC descDSV{};
descDSV.Format = descDepth.Format;
descDSV.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2D;
descDSV.Texture2D.MipSlice = 0;
hr = device_->CreateDepthStencilView(bbDepthStencilTex_, &descDSV, &bbDepthStencilView_);

D3D11DrawContext::~D3D11DrawContext() {
context_->OMSetRenderTargets(1, &bbRenderTargetView_, bbDepthStencilView_);

curRenderTargetView_ = bbRenderTargetView_;
curDepthStencilView_ = bbDepthStencilView_;
break;
}
}
}

void D3D11DrawContext::SetViewports(int count, Viewport *viewports) {
Expand Down

0 comments on commit 77d0877

Please sign in to comment.