1
0
mirror of https://github.com/borgesdan/xn65 synced 2024-12-29 21:54:47 +01:00
xn65/framework/platform/swapchain-dx.cpp

95 lines
3.5 KiB
C++
Raw Normal View History

2024-03-18 15:41:46 -03:00
#include "swapchain-dx.hpp"
#include "adapter-dx.hpp"
#include "device-dx.hpp"
namespace xna {
static bool internalInit(GraphicsDevice& device, HWND windowHandle, IDXGISwapChain1*& swapChain, DXGI_SWAP_CHAIN_DESC1 const& desc, DXGI_SWAP_CHAIN_FULLSCREEN_DESC const& fdesc) {
if (!device._device || !windowHandle)
2024-04-22 11:22:18 -03:00
return false;
if (swapChain) {
swapChain->Release();
swapChain = nullptr;
}
2024-03-18 15:41:46 -03:00
2024-04-22 11:22:18 -03:00
auto adapter = device.Adapter();
2024-04-23 16:11:17 -03:00
auto dxAdapter = adapter->dxadapter;
2024-03-18 15:41:46 -03:00
2024-04-22 11:22:18 -03:00
IDXGIFactory1* dxFactory1 = nullptr;
auto hr = dxAdapter->GetParent(IID_IDXGIFactory1, (void**)&dxFactory1);
2024-03-18 15:41:46 -03:00
2024-04-22 11:22:18 -03:00
if (FAILED(hr)) return false;
2024-03-18 15:41:46 -03:00
2024-04-22 11:22:18 -03:00
IDXGIFactory2* dxFactory2 = nullptr;
hr = dxFactory1->QueryInterface(IID_IDXGIFactory2, (void**)&dxFactory2);
2024-03-18 15:41:46 -03:00
2024-04-22 11:22:18 -03:00
if (FAILED(hr)) return false;
2024-03-18 15:41:46 -03:00
2024-04-22 11:22:18 -03:00
dxFactory2->CreateSwapChainForHwnd(
device._device,
windowHandle,
2024-04-22 11:22:18 -03:00
&desc,
&fdesc,
nullptr,
&swapChain);
2024-03-18 15:41:46 -03:00
return true;
2024-04-22 11:22:18 -03:00
}
bool SwapChain::Initialize(xna_error_ptr_arg) {
2024-04-25 14:51:33 -03:00
if (!m_device || !m_device->_device) {
xna_error_apply(err, XnaErrorCode::INVALID_OPERATION);
return false;
}
const auto parameters = m_device->_presentationParameters;
2024-04-25 14:51:33 -03:00
dxDescription.Width = static_cast<UINT>(parameters.backBufferWidth);
dxDescription.Height = static_cast<UINT>(parameters.backBufferHeight);
dxDescription.Format = GraphicsAdapter::ConvertSurfaceToDXGIFORMAT(parameters.backBufferFormat);
2024-04-23 16:11:17 -03:00
dxDescription.SampleDesc.Count = 1;
dxDescription.SampleDesc.Quality = 0;
dxDescription.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
dxDescription.BufferCount = 2;
dxDescription.SwapEffect = static_cast<DXGI_SWAP_EFFECT>(parameters.swapEffect);
2024-04-23 16:11:17 -03:00
dxDescription.Flags = DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH;
dxDescription.AlphaMode = DXGI_ALPHA_MODE::DXGI_ALPHA_MODE_UNSPECIFIED;
dxFullScreenDescription.RefreshRate.Numerator = 60;
dxFullScreenDescription.RefreshRate.Denominator = 1;
dxFullScreenDescription.Scaling = DXGI_MODE_SCALING_UNSPECIFIED;
dxFullScreenDescription.ScanlineOrdering = DXGI_MODE_SCANLINE_ORDER_UNSPECIFIED;
dxFullScreenDescription.Windowed = !parameters.fullscreen;
2024-04-23 16:11:17 -03:00
return internalInit(*m_device, parameters.windowHandle, dxSwapChain, dxDescription, dxFullScreenDescription);
2024-04-22 11:22:18 -03:00
}
2024-04-25 14:51:33 -03:00
bool SwapChain::Initialize(GameWindow const& gameWindow, DXGI_SWAP_CHAIN_DESC1 const& desc, DXGI_SWAP_CHAIN_FULLSCREEN_DESC const& fullScreenDesc, xna_error_ptr_arg)
2024-04-22 11:22:18 -03:00
{
2024-04-25 14:51:33 -03:00
if (!m_device || !m_device->_device) {
xna_error_apply(err, XnaErrorCode::INVALID_OPERATION);
return false;
}
2024-04-23 16:11:17 -03:00
dxDescription = desc;
dxFullScreenDescription = fullScreenDesc;
return internalInit(*m_device, gameWindow.WindowHandle(), dxSwapChain, dxDescription, dxFullScreenDescription);
2024-04-23 16:11:17 -03:00
}
bool SwapChain::GetBackBuffer(ID3D11Texture2D*& texture2D) {
if (!dxSwapChain)
return false;
const auto hr = dxSwapChain->GetBuffer(0, __uuidof(texture2D), (void**)(&texture2D));
return !FAILED(hr);
}
bool SwapChain::Present(bool vsync) {
if (!dxSwapChain)
return false;
const auto hr = dxSwapChain->Present(vsync, NULL);
return !FAILED(hr);
2024-04-22 11:22:18 -03:00
}
2024-03-18 15:41:46 -03:00
}