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

91 lines
3.5 KiB
C++
Raw Permalink Normal View History

2024-06-03 21:55:09 -03:00
#include "xna/graphics/adapter.hpp"
#include "xna/graphics/swapchain.hpp"
2024-09-06 22:23:32 -03:00
#include "xna-dx/framework.hpp"
2024-06-03 21:55:09 -03:00
#include "xna/graphics/device.hpp"
2024-03-18 15:41:46 -03:00
namespace xna {
2024-05-22 20:05:52 -03:00
SwapChain::SwapChain() : GraphicsResource(nullptr) {
impl = unew<PlatformImplementation>();
}
SwapChain::SwapChain(sptr<GraphicsDevice> const& device) : GraphicsResource(device) {
impl = unew<PlatformImplementation>();
}
SwapChain::~SwapChain() {
impl = nullptr;
}
2024-06-25 17:06:37 -03:00
static bool internalInit(GraphicsDevice& device, HWND windowHandle, comptr<IDXGISwapChain1>& swapChain, DXGI_SWAP_CHAIN_DESC1 const& desc, DXGI_SWAP_CHAIN_FULLSCREEN_DESC const& fdesc) {
if (!device.Implementation->Device || !windowHandle)
2024-04-22 11:22:18 -03:00
return false;
if (swapChain) {
2024-06-25 17:06:37 -03:00
swapChain.ReleaseAndGetAddressOf();
2024-04-22 11:22:18 -03:00
}
2024-03-18 15:41:46 -03:00
2024-07-16 12:28:03 -03:00
auto adapter = device.Adapter();
2024-03-18 15:41:46 -03:00
2024-07-16 12:28:03 -03:00
comptr<IDXGIFactory2> dxFactory2 = nullptr;
const auto hr = adapter->Implementation->Factory->QueryInterface(IID_IDXGIFactory2, (void**)&dxFactory2);
2024-03-18 15:41:46 -03:00
2024-07-16 12:28:03 -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.Implementation->Device.Get(),
windowHandle,
2024-04-22 11:22:18 -03:00
&desc,
&fdesc,
nullptr,
2024-06-25 17:06:37 -03:00
swapChain.GetAddressOf());
2024-03-18 15:41:46 -03:00
return true;
2024-04-22 11:22:18 -03:00
}
bool SwapChain::Initialize() {
if (!impl || !BaseGraphicsDevice || !BaseGraphicsDevice->Implementation->Device) {
throw csharp::InvalidOperationException();
2024-04-25 14:51:33 -03:00
}
const auto parameters = BaseGraphicsDevice->PresentParameters();
2024-04-25 14:51:33 -03:00
2024-05-24 12:31:24 -03:00
impl->dxDescription.Width = static_cast<UINT>(parameters->BackBufferWidth);
impl->dxDescription.Height = static_cast<UINT>(parameters->BackBufferHeight);
2024-06-22 22:05:35 -03:00
impl->dxDescription.Format = DxHelpers::SurfaceFormatToDx(parameters->BackBufferFormat);
2024-05-22 20:05:52 -03:00
impl->dxDescription.SampleDesc.Count = 1;
impl->dxDescription.SampleDesc.Quality = 0;
impl->dxDescription.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
impl->dxDescription.BufferCount = 2;
2024-05-24 12:31:24 -03:00
impl->dxDescription.SwapEffect = static_cast<DXGI_SWAP_EFFECT>(parameters->PresentationSwapEffect);
2024-05-22 20:05:52 -03:00
impl->dxDescription.Flags = DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH;
impl->dxDescription.AlphaMode = DXGI_ALPHA_MODE::DXGI_ALPHA_MODE_UNSPECIFIED;
impl->dxFullScreenDescription.RefreshRate.Numerator = 60;
impl->dxFullScreenDescription.RefreshRate.Denominator = 1;
impl->dxFullScreenDescription.Scaling = DXGI_MODE_SCALING_UNSPECIFIED;
impl->dxFullScreenDescription.ScanlineOrdering = DXGI_MODE_SCANLINE_ORDER_UNSPECIFIED;
impl->dxFullScreenDescription.Windowed = !parameters->IsFullscreen;
2024-04-23 16:11:17 -03:00
2024-05-24 12:31:24 -03:00
HWND hwnd = reinterpret_cast<HWND>(parameters->DeviceWindowHandle);
return internalInit(*BaseGraphicsDevice, hwnd, impl->dxSwapChain, impl->dxDescription, impl->dxFullScreenDescription);
2024-05-22 20:05:52 -03:00
}
2024-04-23 16:11:17 -03:00
2024-05-22 20:05:52 -03:00
bool SwapChain::GetBackBuffer(Texture2D& texture2D) {
if (!impl || !impl->dxSwapChain)
2024-04-23 16:11:17 -03:00
return false;
const auto hr = impl->dxSwapChain->GetBuffer(0, IID_ID3D11Texture2D, (void**)(&texture2D.Implementation->Texture2D));
2024-04-23 16:11:17 -03:00
return !FAILED(hr);
}
bool SwapChain::Present(bool vsync) const {
2024-05-22 20:05:52 -03:00
if (!impl || !impl->dxSwapChain)
2024-04-23 16:11:17 -03:00
return false;
const auto presentValue = static_cast<UINT>(vsync);
const auto hr = impl->dxSwapChain->Present(presentValue, NULL);
2024-04-23 16:11:17 -03:00
return !FAILED(hr);
2024-04-22 11:22:18 -03:00
}
2024-03-18 15:41:46 -03:00
}