diff --git a/framework/platform/blendstate-dx.cpp b/framework/platform/blendstate-dx.cpp index 625c3e7..3a4fad6 100644 --- a/framework/platform/blendstate-dx.cpp +++ b/framework/platform/blendstate-dx.cpp @@ -1,6 +1,5 @@ #include "graphics/blendstate.hpp" #include "graphics/gresource.hpp" -#include "platform-dx/device-dx.hpp" #include "platform-dx/dxheaders.hpp" #include "platform-dx/dxhelpers.hpp" #include "graphics/blendstate.hpp" @@ -21,7 +20,7 @@ namespace xna { bool BlendState::Initialize(xna_error_ptr_arg) { - if (!m_device || !m_device->_device) { + if (!m_device || !m_device->impl->_device) { xna_error_apply(err, XnaErrorCode::INVALID_OPERATION); return false; } @@ -31,7 +30,7 @@ namespace xna { impl->dxBlendState = nullptr; } - const auto hr = m_device->_device->CreateBlendState( + const auto hr = m_device->impl->_device->CreateBlendState( &impl->dxDescription, &impl->dxBlendState); @@ -44,7 +43,7 @@ namespace xna { } bool BlendState::Apply(xna_error_ptr_arg) { - if (!m_device || !m_device->_context) { + if (!m_device || !m_device->impl->_context) { xna_error_apply(err, XnaErrorCode::INVALID_OPERATION); return false; } @@ -54,7 +53,7 @@ namespace xna { return false; } - m_device->_context->OMSetBlendState( + m_device->impl->_context->OMSetBlendState( impl->dxBlendState, impl->blendFactor, impl->sampleMask); diff --git a/framework/platform/buffer.cpp b/framework/platform/buffer.cpp index 4c663e9..05e199c 100644 --- a/framework/platform/buffer.cpp +++ b/framework/platform/buffer.cpp @@ -1,7 +1,6 @@ #include "graphics/buffer.hpp" #include "common/numerics.hpp" #include "platform-dx/dxheaders.hpp" -#include "platform-dx/device-dx.hpp" #include "platform-dx/implementations.hpp" namespace xna { @@ -19,7 +18,7 @@ namespace xna { bool ConstantBuffer::Initialize(xna_error_ptr_arg) { - if (!m_device || !m_device->_device) { + if (!m_device || !m_device->impl->_device) { xna_error_apply(err, XnaErrorCode::INVALID_OPERATION); return false; } @@ -29,7 +28,7 @@ namespace xna { impl->_buffer = nullptr; } - const auto hr = m_device->_device->CreateBuffer( + const auto hr = m_device->impl->_device->CreateBuffer( &impl->_description, &impl->_subResource, &impl->_buffer); @@ -55,7 +54,7 @@ namespace xna { } bool DataBuffer::Initialize(xna_error_ptr_arg) { - if (!m_device || !m_device->_device) { + if (!m_device || !m_device->impl->_device) { xna_error_apply(err, XnaErrorCode::INVALID_OPERATION); return false; } @@ -81,12 +80,12 @@ namespace xna { } bool IndexBuffer::Apply(xna_error_ptr_arg) { - if (!m_device || !m_device->_context || !impl || !impl->dxBuffer) { + if (!m_device || !m_device->impl->_context || !impl || !impl->dxBuffer) { xna_error_apply(err, XnaErrorCode::INVALID_OPERATION); return false; } - m_device->_context->IASetIndexBuffer(impl->dxBuffer, DXGI_FORMAT_R16_UINT, 0); + m_device->impl->_context->IASetIndexBuffer(impl->dxBuffer, DXGI_FORMAT_R16_UINT, 0); return true; } @@ -104,7 +103,7 @@ namespace xna { } bool VertexBuffer::Apply(xna_error_ptr_arg) { - if (!impl || !m_device || !m_device->_context) { + if (!impl || !m_device || !m_device->impl->_context) { xna_error_apply(err, XnaErrorCode::INVALID_OPERATION); return false; } @@ -116,7 +115,7 @@ namespace xna { UINT stride = impl->size; UINT offset = 0; - m_device->_context->IASetVertexBuffers(0, 1, + m_device->impl->_context->IASetVertexBuffers(0, 1, &impl->dxBuffer, &stride, &offset); return true; @@ -135,7 +134,7 @@ namespace xna { } bool VertexInputLayout::Initialize(DataBuffer& blob, xna_error_ptr_arg) { - if (!impl || !m_device || !m_device->_device || !blob.impl->_blob) { + if (!impl || !m_device || !m_device->impl->_device || !blob.impl->_blob) { xna_error_apply(err, XnaErrorCode::INVALID_OPERATION); return false; } @@ -145,7 +144,7 @@ namespace xna { impl->_inputLayout = nullptr; } - const auto hr = m_device->_device->CreateInputLayout( + const auto hr = m_device->impl->_device->CreateInputLayout( impl->_description.data(), static_cast(impl->_description.size()), blob.impl->_blob->GetBufferPointer(), diff --git a/framework/platform/depthstencilstate-dx.cpp b/framework/platform/depthstencilstate-dx.cpp index 8b32bf7..2819f8b 100644 --- a/framework/platform/depthstencilstate-dx.cpp +++ b/framework/platform/depthstencilstate-dx.cpp @@ -1,4 +1,3 @@ -#include "platform-dx/device-dx.hpp" #include "graphics/depthstencilstate.hpp" #include "platform-dx/dxheaders.hpp" #include "platform-dx/implementations.hpp" @@ -43,7 +42,7 @@ namespace xna { bool DepthStencilState::Initialize(xna_error_ptr_arg) { - if (!m_device || !m_device->_device) { + if (!m_device || !m_device->impl->_device) { xna_error_apply(err, XnaErrorCode::INVALID_OPERATION); return false; } @@ -53,7 +52,7 @@ namespace xna { impl->dxDepthStencil = nullptr; } - const auto hr = m_device->_device->CreateDepthStencilState( + const auto hr = m_device->impl->_device->CreateDepthStencilState( &impl->dxDescription, &impl->dxDepthStencil); @@ -67,7 +66,7 @@ namespace xna { bool DepthStencilState::Apply(xna_error_ptr_arg) { - if (!m_device || !m_device->_context) { + if (!m_device || !m_device->impl->_context) { xna_error_apply(err, XnaErrorCode::INVALID_OPERATION); return false; } @@ -77,7 +76,7 @@ namespace xna { return false; } - m_device->_context->OMSetDepthStencilState(impl->dxDepthStencil, 0); + m_device->impl->_context->OMSetDepthStencilState(impl->dxDepthStencil, 0); return true; } diff --git a/framework/platform/device-dx.cpp b/framework/platform/device-dx.cpp index 861b2d8..cf5bfa9 100644 --- a/framework/platform/device-dx.cpp +++ b/framework/platform/device-dx.cpp @@ -1,157 +1,205 @@ -#include "common/color.hpp" -#include "graphics/adapter.hpp" -#include "graphics/blendstate.hpp" -#include "graphics/rendertarget.hpp" -#include "platform-dx/device-dx.hpp" #include "platform-dx/implementations.hpp" -#include "game/gdeviceinfo.hpp" #include "game/gdevicemanager.hpp" namespace xna { + void reset(GraphicsDevice::PlatformImplementation& impl) + { + if (impl._device) { + impl._device->Release(); + impl._device = nullptr; + } + + if (impl._context) { + impl._context->Release(); + impl._context = nullptr; + } + + if (impl._factory) { + impl._factory->Release(); + impl._factory = nullptr; + } + + impl._blendState = nullptr; + impl._swapChain = nullptr; + impl._renderTarget2D = nullptr; + } + + bool createDevice(GraphicsDevice::PlatformImplementation& impl) { + auto createDeviceFlags = 0; +#if _DEBUG + createDeviceFlags = D3D11_CREATE_DEVICE_FLAG::D3D11_CREATE_DEVICE_DEBUG; +#endif + + + auto hr = D3D11CreateDevice( + impl._adapter->impl->dxadapter, + D3D_DRIVER_TYPE_UNKNOWN, + NULL, + createDeviceFlags, + NULL, + 0, + D3D11_SDK_VERSION, + &impl._device, + &impl._featureLevel, + &impl._context); + + if (FAILED(hr)) { + OutputDebugString("---> Usando Adaptador WARP: não há suporte ao D3D11\n"); + + hr = D3D11CreateDevice( + NULL, + D3D_DRIVER_TYPE_WARP, + NULL, + createDeviceFlags, + NULL, + 0, + D3D11_SDK_VERSION, + &impl._device, + &impl._featureLevel, + &impl._context); + } + + return SUCCEEDED(hr); + } + GraphicsDevice::GraphicsDevice() { - _adapter = GraphicsAdapter::DefaultAdapter(); - _adapter->CurrentDisplayMode(SurfaceFormat::Color, GraphicsDeviceManager::DefaultBackBufferWidth, GraphicsDeviceManager::DefaultBackBufferHeight); + impl = unew(); + impl->_adapter = GraphicsAdapter::DefaultAdapter(); + impl->_adapter->CurrentDisplayMode( + SurfaceFormat::Color, + GraphicsDeviceManager::DefaultBackBufferWidth, + GraphicsDeviceManager::DefaultBackBufferHeight); } GraphicsDevice::GraphicsDevice(GraphicsDeviceInformation const& info) { - _adapter = info.Adapter; - _presentationParameters = info.Parameters; - _adapter->CurrentDisplayMode( - _presentationParameters->BackBufferFormat, - _presentationParameters->BackBufferWidth, - _presentationParameters->BackBufferHeight); + impl = unew(); + + impl->_adapter = info.Adapter; + impl->_presentationParameters = info.Parameters; + impl->_adapter->CurrentDisplayMode( + impl->_presentationParameters->BackBufferFormat, + impl->_presentationParameters->BackBufferWidth, + impl->_presentationParameters->BackBufferHeight); + } + + GraphicsDevice::~GraphicsDevice() { + impl = nullptr; } bool GraphicsDevice::Initialize(GameWindow& gameWindow) { - reset(); + if (!impl) + impl = uptr(); + + reset(*impl); auto _this = shared_from_this(); - if (!createDevice()) return false; + if (!createDevice(*impl)) + return false; - auto hr = CreateDXGIFactory1(__uuidof(IDXGIFactory1), (void**)&_factory); - if (FAILED(hr)) return false; + auto hr = CreateDXGIFactory1(__uuidof(IDXGIFactory1), (void**)&impl->_factory); + if (FAILED(hr)) + return false; const auto bounds = gameWindow.ClientBounds(); - _viewport = xna::Viewport(0.0F, 0.0F, + impl->_viewport = xna::Viewport(0.0F, 0.0F, static_cast(bounds.Width), static_cast(bounds.Height), 0.0F, 1.F); COLORREF color = gameWindow.impl->Color(); - _backgroundColor[0] = GetRValue(color) / 255.0f; - _backgroundColor[1] = GetGValue(color) / 255.0f; - _backgroundColor[2] = GetBValue(color) / 255.0f; - _backgroundColor[3] = 1.0f; + impl->_backgroundColor[0] = GetRValue(color) / 255.0f; + impl->_backgroundColor[1] = GetGValue(color) / 255.0f; + impl->_backgroundColor[2] = GetBValue(color) / 255.0f; + impl->_backgroundColor[3] = 1.0f; - _swapChain = New(_this); - _swapChain->Initialize(); + impl->_swapChain = New(_this); + impl->_swapChain->Initialize(); - hr = _factory->MakeWindowAssociation(gameWindow.impl->WindowHandle(), DXGI_MWA_NO_ALT_ENTER); + hr = impl->_factory->MakeWindowAssociation(gameWindow.impl->WindowHandle(), DXGI_MWA_NO_ALT_ENTER); if (FAILED(hr)) return false; - _renderTarget2D = New(_this); - if (!_renderTarget2D->Initialize()) + impl->_renderTarget2D = New(_this); + if (!impl->_renderTarget2D->Initialize()) return false; - _renderTarget2D->Apply(); + impl->_renderTarget2D->Apply(); D3D11_VIEWPORT view{}; - view.TopLeftX = _viewport.X; - view.TopLeftY = _viewport.Y; - view.Width = _viewport.Width; - view.Height = _viewport.Height; - view.MinDepth = _viewport.MinDetph; - view.MaxDepth = _viewport.MaxDepth; + view.TopLeftX = impl->_viewport.X; + view.TopLeftY = impl->_viewport.Y; + view.Width = impl->_viewport.Width; + view.Height = impl->_viewport.Height; + view.MinDepth = impl->_viewport.MinDetph; + view.MaxDepth = impl->_viewport.MaxDepth; - _context->RSSetViewports(1, &view); + impl->_context->RSSetViewports(1, &view); - _blendState = BlendState::NonPremultiplied(); - _blendState->Bind(_this); - _blendState->Apply(); + impl->_blendState = BlendState::NonPremultiplied(); + impl->_blendState->Bind(_this); + impl->_blendState->Apply(); return true; } bool GraphicsDevice::Present() { - const auto result = _swapChain->Present(_usevsync); - _context->OMSetRenderTargets(1, &_renderTarget2D->render_impl->_renderTargetView, nullptr); + if (!impl) return false; + + const auto result = impl->_swapChain->Present(impl->_usevsync); + impl->_context->OMSetRenderTargets(1, &impl->_renderTarget2D->render_impl->_renderTargetView, nullptr); return result; - } - - bool GraphicsDevice::createDevice() { -#if _DEBUG - _createDeviceFlags |= D3D11_CREATE_DEVICE_FLAG::D3D11_CREATE_DEVICE_DEBUG; -#endif - - if FAILED( - D3D11CreateDevice( - _adapter->impl->dxadapter, - D3D_DRIVER_TYPE_UNKNOWN, - NULL, - _createDeviceFlags, - NULL, - 0, - D3D11_SDK_VERSION, - &_device, - &_featureLevel, - &_context)) { - - if FAILED(D3D11CreateDevice( - NULL, - D3D_DRIVER_TYPE_WARP, - NULL, - _createDeviceFlags, - NULL, - 0, - D3D11_SDK_VERSION, - &_device, - &_featureLevel, - &_context)) - return false; - - OutputDebugString("---> Usando Adaptador WARP: não há suporte ao D3D11\n"); - } - - return true; - } - - void GraphicsDevice::reset() - { - if (_device) { - _device->Release(); - _device = nullptr; - } - - if (_context) { - _context->Release(); - _context = nullptr; - } - - if (_factory) { - _factory->Release(); - _factory = nullptr; - } - - _blendState = nullptr; - _swapChain = nullptr; - _renderTarget2D = nullptr; - } + } void GraphicsDevice::Clear() { - _context->ClearRenderTargetView(_renderTarget2D->render_impl->_renderTargetView, _backgroundColor); + if (!impl) return; + + impl->_context->ClearRenderTargetView(impl->_renderTarget2D->render_impl->_renderTargetView, impl->_backgroundColor); } void GraphicsDevice::Clear(Color const& color) { + if (!impl) return; + const auto v4 = color.ToVector4(); - _backgroundColor[0] = v4.X; - _backgroundColor[1] = v4.Y; - _backgroundColor[2] = v4.Z; - _backgroundColor[3] = v4.W; + impl->_backgroundColor[0] = v4.X; + impl->_backgroundColor[1] = v4.Y; + impl->_backgroundColor[2] = v4.Z; + impl->_backgroundColor[3] = v4.W; - _context->ClearRenderTargetView(_renderTarget2D->render_impl->_renderTargetView, _backgroundColor); + impl->_context->ClearRenderTargetView( + impl->_renderTarget2D->render_impl->_renderTargetView, + impl->_backgroundColor); } + + sptr GraphicsDevice::Adapter() const { + if (!impl) return nullptr; + + return impl->_adapter; + } + + void GraphicsDevice::Adapter(sptr const& adapter) { + if (!impl) return; + + impl->_adapter = adapter; + } + + xna::Viewport GraphicsDevice::Viewport() const { + if (!impl) return {}; + + return impl->_viewport; + } + + void GraphicsDevice::Viewport(xna::Viewport const& viewport) { + if (!impl) return; + + impl->_viewport = viewport; + } + + void GraphicsDevice::UseVSync(bool use) { + if (!impl) return; + + impl->_usevsync = use; + } } \ No newline at end of file diff --git a/framework/platform/gdevicemanager-dx.cpp b/framework/platform/gdevicemanager-dx.cpp index 365313d..2fe6b0f 100644 --- a/framework/platform/gdevicemanager-dx.cpp +++ b/framework/platform/gdevicemanager-dx.cpp @@ -1,7 +1,6 @@ #include "game/gdevicemanager.hpp" #include "graphics/presentparams.hpp" #include "graphics/swapchain.hpp" -#include "platform-dx/device-dx.hpp" #include "platform-dx/game-dx.hpp" #include "platform-dx/implementations.hpp" @@ -34,10 +33,10 @@ namespace xna { } bool GraphicsDeviceManager::ToggleFullScreen() { - if (!_game || !_game->graphicsDevice || !_game->graphicsDevice->_swapChain) + if (!_game || !_game->graphicsDevice || !_game->graphicsDevice->impl->_swapChain) return false; - auto& swap = _game->graphicsDevice->_swapChain; + auto& swap = _game->graphicsDevice->impl->_swapChain; BOOL state = false; auto hr = swap->impl->dxSwapChain->GetFullscreenState(&state, nullptr); diff --git a/framework/platform/rasterizerstate-dx.cpp b/framework/platform/rasterizerstate-dx.cpp index 43ef107..70e1ebc 100644 --- a/framework/platform/rasterizerstate-dx.cpp +++ b/framework/platform/rasterizerstate-dx.cpp @@ -1,6 +1,5 @@ #include "graphics/rasterizerstate.hpp" #include "platform-dx/implementations.hpp" -#include "platform-dx/device-dx.hpp" namespace xna { @@ -18,7 +17,7 @@ namespace xna { bool RasterizerState::Initialize(xna_error_ptr_arg) { - if (!impl || !m_device || !m_device->_device) { + if (!impl || !m_device || !m_device->impl->_device) { xna_error_apply(err, XnaErrorCode::INVALID_OPERATION); return false; } @@ -28,7 +27,7 @@ namespace xna { impl->dxRasterizerState = nullptr; } - const auto hr = m_device->_device->CreateRasterizerState( + const auto hr = m_device->impl->_device->CreateRasterizerState( &impl->dxDescription, &impl->dxRasterizerState); @@ -42,7 +41,7 @@ namespace xna { bool RasterizerState::Apply(xna_error_ptr_arg) { - if (!impl || !m_device || !m_device->_context) { + if (!impl || !m_device || !m_device->impl->_context) { xna_error_apply(err, XnaErrorCode::INVALID_OPERATION); return false; } @@ -52,7 +51,7 @@ namespace xna { return false; } - m_device->_context->RSSetState(impl->dxRasterizerState); + m_device->impl->_context->RSSetState(impl->dxRasterizerState); return true; } diff --git a/framework/platform/rendertarget-dx.cpp b/framework/platform/rendertarget-dx.cpp index 16ebe1f..5363663 100644 --- a/framework/platform/rendertarget-dx.cpp +++ b/framework/platform/rendertarget-dx.cpp @@ -1,7 +1,4 @@ -#include "graphics/device.hpp" -#include "platform-dx/device-dx.hpp" #include "platform-dx/implementations.hpp" -#include "graphics/rendertarget.hpp" namespace xna { RenderTarget2D::RenderTarget2D() : Texture2D() { @@ -17,7 +14,7 @@ namespace xna { } bool RenderTarget2D::Initialize(xna_error_ptr_arg) { - if (!impl || !m_device || !m_device->_device) { + if (!impl || !m_device || !m_device->impl->_device) { xna_error_apply(err, XnaErrorCode::INVALID_OPERATION); return false; } @@ -27,10 +24,10 @@ namespace xna { impl->dxTexture2D = nullptr; } - if (!m_device->_swapChain->impl->GetBackBuffer(impl->dxTexture2D)) + if (!m_device->impl->_swapChain->impl->GetBackBuffer(impl->dxTexture2D)) return false; - auto& dxdevice = m_device->_device; + auto& dxdevice = m_device->impl->_device; const auto hr = dxdevice->CreateRenderTargetView(impl->dxTexture2D, NULL, &render_impl->_renderTargetView); @@ -43,12 +40,12 @@ namespace xna { } bool RenderTarget2D::Apply(xna_error_ptr_arg) { - if (!m_device || !m_device->_context) { + if (!m_device || !m_device->impl->_context) { xna_error_apply(err, XnaErrorCode::INVALID_OPERATION); return false; } - auto& context = m_device->_context; + auto& context = m_device->impl->_context; context->OMSetRenderTargets(1, &render_impl->_renderTargetView, nullptr); return true; } diff --git a/framework/platform/samplerstate-dx.cpp b/framework/platform/samplerstate-dx.cpp index 8562704..f1ead60 100644 --- a/framework/platform/samplerstate-dx.cpp +++ b/framework/platform/samplerstate-dx.cpp @@ -1,5 +1,4 @@ #include "graphics/samplerstate.hpp" -#include "platform-dx/device-dx.hpp" #include "graphics/samplerstate.hpp" #include "platform-dx/implementations.hpp" #include "platform-dx/dxhelpers.hpp" @@ -19,7 +18,7 @@ namespace xna { bool SamplerState::Initialize(xna_error_ptr_arg) { - if (!impl || !m_device || !m_device->_device) { + if (!impl || !m_device || !m_device->impl->_device) { xna_error_apply(err, XnaErrorCode::INVALID_OPERATION); return false; } @@ -29,7 +28,7 @@ namespace xna { impl->_samplerState = nullptr; } - const auto hr = m_device->_device->CreateSamplerState( + const auto hr = m_device->impl->_device->CreateSamplerState( &impl->_description, &impl->_samplerState); @@ -43,7 +42,7 @@ namespace xna { bool SamplerState::Apply(xna_error_ptr_arg) { - if (!impl || !m_device || !m_device->_context) { + if (!impl || !m_device || !m_device->impl->_context) { xna_error_apply(err, XnaErrorCode::INVALID_OPERATION); return false; } @@ -53,7 +52,7 @@ namespace xna { return false; } - m_device->_context->PSSetSamplers(0, 1, &impl->_samplerState); + m_device->impl->_context->PSSetSamplers(0, 1, &impl->_samplerState); return true; } diff --git a/framework/platform/shader-dx.cpp b/framework/platform/shader-dx.cpp index 49bf967..70f8111 100644 --- a/framework/platform/shader-dx.cpp +++ b/framework/platform/shader-dx.cpp @@ -1,4 +1,3 @@ -#include "platform-dx/device-dx.hpp" #include "graphics/buffer.hpp" #include "platform-dx/implementations.hpp" #include "graphics/shader.hpp" @@ -64,7 +63,7 @@ namespace xna { bool VertexShader::Initialize(DataBuffer& buffer, xna_error_ptr_arg) { - if (!impl || !m_device || !m_device->_device || !buffer.impl->_blob) { + if (!impl || !m_device || !m_device->impl->_device || !buffer.impl->_blob) { xna_error_apply(err, XnaErrorCode::INVALID_OPERATION); return false; } @@ -74,7 +73,7 @@ namespace xna { impl->_vertexShader = nullptr; } - const auto hr = m_device->_device->CreateVertexShader( + const auto hr = m_device->impl->_device->CreateVertexShader( buffer.impl->_blob->GetBufferPointer(), buffer.impl->_blob->GetBufferSize(), NULL, @@ -90,7 +89,7 @@ namespace xna { bool PixelShader::Initialize(DataBuffer& buffer, xna_error_ptr_arg) { - if (!impl || !m_device || !m_device->_device || !buffer.impl->_blob) { + if (!impl || !m_device || !m_device->impl->_device || !buffer.impl->_blob) { xna_error_apply(err, XnaErrorCode::INVALID_OPERATION); return false; } @@ -100,7 +99,7 @@ namespace xna { impl->_pixelShader = nullptr; } - const auto hr = m_device->_device->CreatePixelShader( + const auto hr = m_device->impl->_device->CreatePixelShader( buffer.impl->_blob->GetBufferPointer(), buffer.impl->_blob->GetBufferSize(), NULL, diff --git a/framework/platform/sprite-dx.cpp b/framework/platform/sprite-dx.cpp index c3ea32c..62314ad 100644 --- a/framework/platform/sprite-dx.cpp +++ b/framework/platform/sprite-dx.cpp @@ -1,4 +1,3 @@ -#include "platform-dx/device-dx.hpp" #include "graphics/rasterizerstate.hpp" #include "graphics/samplerstate.hpp" #include "common/color.hpp" @@ -24,7 +23,7 @@ namespace xna { { const auto wString = XnaHToWString(fontFileName); implementation = uNew(); - implementation->_dxSpriteFont = New(device._device, wString.c_str()); + implementation->_dxSpriteFont = New(device.impl->_device, wString.c_str()); } SpriteFont::~SpriteFont() {} @@ -47,11 +46,11 @@ namespace xna { } SpriteBatch::SpriteBatch(GraphicsDevice& device) { - if (!device._context) + if (!device.impl->_context) return; implementation = uNew(); - implementation->_dxspriteBatch = New(device._context); + implementation->_dxspriteBatch = New(device.impl->_context); Viewport(device.Viewport()); } diff --git a/framework/platform/swapchain-dx.cpp b/framework/platform/swapchain-dx.cpp index 8b2202e..180f973 100644 --- a/framework/platform/swapchain-dx.cpp +++ b/framework/platform/swapchain-dx.cpp @@ -1,8 +1,8 @@ -#include "platform-dx/device-dx.hpp" #include "platform-dx/dxhelpers.hpp" #include "graphics/adapter.hpp" #include "graphics/swapchain.hpp" #include "platform-dx/implementations.hpp" +#include "graphics/device.hpp" namespace xna { SwapChain::SwapChain() : GraphicsResource(nullptr) { @@ -18,7 +18,7 @@ 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) + if (!device.impl->_device || !windowHandle) return false; if (swapChain) { @@ -40,7 +40,7 @@ namespace xna { if (FAILED(hr)) return false; dxFactory2->CreateSwapChainForHwnd( - device._device, + device.impl->_device, windowHandle, &desc, &fdesc, @@ -52,12 +52,12 @@ namespace xna { } bool SwapChain::Initialize(xna_error_ptr_arg) { - if (!impl || !m_device || !m_device->_device) { + if (!impl || !m_device || !m_device->impl->_device) { xna_error_apply(err, XnaErrorCode::INVALID_OPERATION); return false; } - const auto parameters = m_device->_presentationParameters; + const auto parameters = m_device->impl->_presentationParameters; impl->dxDescription.Width = static_cast(parameters->BackBufferWidth); impl->dxDescription.Height = static_cast(parameters->BackBufferHeight); diff --git a/framework/platform/texture-dx.cpp b/framework/platform/texture-dx.cpp index 3ea8104..5dc491c 100644 --- a/framework/platform/texture-dx.cpp +++ b/framework/platform/texture-dx.cpp @@ -1,5 +1,3 @@ -#include "platform-dx/device-dx.hpp" -#include "graphics/adapter.hpp" #include "platform-dx/implementations.hpp" #include "platform-dx/dxhelpers.hpp" @@ -16,8 +14,8 @@ namespace xna { auto wstr = XnaHToWString(fileName); HRESULT result = DirectX::CreateWICTextureFromFile( - device._device, - device._context, + device.impl->_device, + device.impl->_context, wstr.c_str(), &resource, &texture2d->impl->dxShaderResource, @@ -65,12 +63,12 @@ namespace xna { return false; } - if (!m_device || !m_device->_device) { + if (!m_device || !m_device->impl->_device) { xna_error_apply(err, XnaErrorCode::INVALID_OPERATION); return false; } - auto hr = m_device->_device->CreateTexture2D(&impl->dxDescription, nullptr, &impl->dxTexture2D); + auto hr = m_device->impl->_device->CreateTexture2D(&impl->dxDescription, nullptr, &impl->dxTexture2D); if (FAILED(hr)) { xna_error_apply(err, XnaErrorCode::FAILED_OPERATION); @@ -85,7 +83,7 @@ namespace xna { return false; } - hr = m_device->_device->CreateShaderResourceView(resource, &impl->dxShaderDescription, &impl->dxShaderResource); + hr = m_device->impl->_device->CreateShaderResourceView(resource, &impl->dxShaderDescription, &impl->dxShaderResource); if (resource) { resource->Release(); @@ -144,7 +142,7 @@ namespace xna { HRESULT internalSetData(Texture2D::PlatformImplementation& impl, GraphicsDevice& device, UINT const* data, xna_error_ptr_arg) { if (!impl.dxTexture2D) { - auto hr = device._device->CreateTexture2D(&impl.dxDescription, nullptr, &impl.dxTexture2D); + auto hr = device.impl->_device->CreateTexture2D(&impl.dxDescription, nullptr, &impl.dxTexture2D); if (FAILED(hr)) { xna_error_apply(err, XnaErrorCode::FAILED_OPERATION); @@ -161,7 +159,7 @@ namespace xna { } constexpr int R8G8B8A8U_BYTE_SIZE = 4; - device._context->UpdateSubresource(resource, 0, nullptr, data, impl.dxDescription.Width * R8G8B8A8U_BYTE_SIZE, 0); + device.impl->_context->UpdateSubresource(resource, 0, nullptr, data, impl.dxDescription.Width * R8G8B8A8U_BYTE_SIZE, 0); if (impl.dxShaderResource) { impl.dxShaderResource->Release(); @@ -169,7 +167,7 @@ namespace xna { } impl.dxShaderDescription.Texture2D.MipLevels = impl.dxDescription.MipLevels; - hr = device._device->CreateShaderResourceView(resource, &impl.dxShaderDescription, &impl.dxShaderResource); + hr = device.impl->_device->CreateShaderResourceView(resource, &impl.dxShaderDescription, &impl.dxShaderResource); if (resource) { resource->Release(); @@ -188,7 +186,7 @@ namespace xna { void Texture2D::SetData(std::vector const& data, size_t startIndex, size_t elementCount, xna_error_ptr_arg) { - if (!impl || !m_device || !m_device->_device || !m_device->_context) { + if (!impl || !m_device || !m_device->impl->_device || !m_device->impl->_context) { xna_error_apply(err, XnaErrorCode::INVALID_OPERATION); return; } @@ -198,7 +196,7 @@ namespace xna { void Texture2D::SetData(std::vector const& data, size_t startIndex, size_t elementCount, xna_error_ptr_arg) { - if (!m_device || !m_device->_device || !m_device->_context) { + if (!m_device || !m_device->impl->_device || !m_device->impl->_context) { xna_error_apply(err, XnaErrorCode::INVALID_OPERATION); return; } @@ -220,7 +218,7 @@ namespace xna { void Texture2D::SetData(Int level, Rectangle* rect, std::vector const& data, size_t startIndex, size_t elementCount, xna_error_ptr_arg) { - if (!m_device || !m_device->_device || !m_device->_context) { + if (!m_device || !m_device->impl->_device || !m_device->impl->_context) { xna_error_apply(err, XnaErrorCode::INVALID_OPERATION); return; } @@ -238,7 +236,7 @@ namespace xna { } if (!impl->dxTexture2D) { - auto hr = m_device->_device->CreateTexture2D(&impl->dxDescription, nullptr, &impl->dxTexture2D); + auto hr = m_device->impl->_device->CreateTexture2D(&impl->dxDescription, nullptr, &impl->dxTexture2D); if (FAILED(hr)) { xna_error_apply(err, XnaErrorCode::FAILED_OPERATION); @@ -266,7 +264,7 @@ namespace xna { } constexpr int R8G8B8A8U_BYTE_SIZE = 4; - m_device->_context->UpdateSubresource(resource, 0, rect ? &box : nullptr, finalData.data(), impl->dxDescription.Width * R8G8B8A8U_BYTE_SIZE, 0); + m_device->impl->_context->UpdateSubresource(resource, 0, rect ? &box : nullptr, finalData.data(), impl->dxDescription.Width * R8G8B8A8U_BYTE_SIZE, 0); if (impl->dxShaderResource) { impl->dxShaderResource->Release(); @@ -274,7 +272,7 @@ namespace xna { } impl->dxShaderDescription.Texture2D.MipLevels = impl->dxDescription.MipLevels; - hr = m_device->_device->CreateShaderResourceView(resource, &impl->dxShaderDescription, &impl->dxShaderResource); + hr = m_device->impl->_device->CreateShaderResourceView(resource, &impl->dxShaderDescription, &impl->dxShaderResource); if (resource) { resource->Release(); @@ -291,7 +289,7 @@ namespace xna { void Texture2D::SetData(std::vector const& data, size_t startIndex, size_t elementCount, xna_error_ptr_arg) { - if (!m_device || !m_device->_device || !m_device->_context) { + if (!m_device || !m_device->impl->_device || !m_device->impl->_context) { xna_error_apply(err, XnaErrorCode::INVALID_OPERATION); return; } @@ -314,8 +312,8 @@ namespace xna { ID3D11Resource* resource = nullptr; auto hr = DirectX::CreateWICTextureFromMemory( - device._device, - device._context, + device.impl->_device, + device.impl->_context, data.data(), data.size(), &resource, diff --git a/inc/graphics/device.hpp b/inc/graphics/device.hpp index 8b4750b..8642bc8 100644 --- a/inc/graphics/device.hpp +++ b/inc/graphics/device.hpp @@ -1,29 +1,27 @@ #ifndef XNA_GRAPHICS_DEVICE_HPP #define XNA_GRAPHICS_DEVICE_HPP -#include "../enums.hpp" -#include "../forward.hpp" -#include "../game/window.hpp" -#include "../types.hpp" -#include "adapter.hpp" -#include "rendertarget.hpp" -#include "swapchain.hpp" -#include "viewport.hpp" -#include "blendstate.hpp" +#include "../default.hpp" namespace xna { - class IGraphicsDevice { + class GraphicsDevice : public std::enable_shared_from_this { public: - virtual ~IGraphicsDevice() {} - virtual void Clear() = 0; - virtual void Clear(Color const& color) = 0; - virtual bool Initialize(GameWindow& gameWindow) = 0; - virtual bool Present() = 0; - virtual sptr Adapter() const = 0; - virtual void Adapter(sptr const& adapter) = 0; - virtual xna::Viewport Viewport() const = 0; - virtual void Viewport(xna::Viewport const& viewport) = 0; - virtual void UseVSync(bool use) = 0; + GraphicsDevice(); + GraphicsDevice(GraphicsDeviceInformation const& info); + ~GraphicsDevice(); + void Clear(); + void Clear(Color const& color); + bool Initialize(GameWindow& gameWindow); + bool Present(); + sptr Adapter() const; + void Adapter(sptr const& adapter); + xna::Viewport Viewport() const; + void Viewport(xna::Viewport const& viewport); + void UseVSync(bool use); + + public: + struct PlatformImplementation; + uptr impl = nullptr; }; } diff --git a/inc/graphics/sprite.hpp b/inc/graphics/sprite.hpp index 0de4ec6..96061e2 100644 --- a/inc/graphics/sprite.hpp +++ b/inc/graphics/sprite.hpp @@ -2,7 +2,7 @@ #define XNA_GRAPHICS_SPRITE_HPP #include "../default.hpp" -#include "../common/numerics.hpp" +#include "common/numerics.hpp" #include "common/color.hpp" namespace xna { diff --git a/inc/platform-dx/device-dx.hpp b/inc/platform-dx/device-dx.hpp deleted file mode 100644 index 2d9b679..0000000 --- a/inc/platform-dx/device-dx.hpp +++ /dev/null @@ -1,91 +0,0 @@ -#ifndef XNA_PLATFORM_DEVICE_DX_HPP -#define XNA_PLATFORM_DEVICE_DX_HPP - -#include "../common/color.hpp" -#include "../graphics/device.hpp" -#include "../graphics/presentparams.hpp" -#include "../graphics/viewport.hpp" -#include "dxheaders.hpp" -#include "game/gdeviceinfo.hpp" - -namespace xna { - class GraphicsDevice : public IGraphicsDevice, public std::enable_shared_from_this { - public: - GraphicsDevice(); - GraphicsDevice(GraphicsDeviceInformation const& info); - - virtual ~GraphicsDevice() override { - if (_device) { - _device->Release(); - _device = nullptr; - } - - if (_context) { - _context->Release(); - _context = nullptr; - } - - if (_factory) { - _factory->Release(); - _factory = nullptr; - } - } - - virtual void Clear() override; - virtual void Clear(Color const& color) override; - virtual bool Initialize(GameWindow& gameWindow) override; - virtual bool Present() override; - - virtual sptr Adapter() const override { - return _adapter; - } - - virtual void Adapter(sptr const& adapter) override { - _adapter = adapter; - } - - virtual constexpr xna::Viewport Viewport() const override { - return _viewport; - } - - virtual constexpr void Viewport(xna::Viewport const& viewport) override { - _viewport = viewport; - } - - virtual void UseVSync(bool use) override { - _usevsync = use; - } - - constexpr void SetCreateFlags(D3D11_CREATE_DEVICE_FLAG flags) { - _createDeviceFlags |= flags; - } - - constexpr void ClearCreateFlags() { - _createDeviceFlags = 0; - } - - bool GetSwapChainBackBuffer(ID3D11Texture2D*& texture2D); - - public: - ID3D11Device* _device{ nullptr }; - ID3D11DeviceContext* _context{ nullptr }; - IDXGIFactory1* _factory = nullptr; - sptr _swapChain{ nullptr }; - sptr _adapter{ nullptr }; - sptr _renderTarget2D{ nullptr }; - sptr _blendState{ nullptr }; - xna::Viewport _viewport{}; - sptr _presentationParameters; - - private: - unsigned int _createDeviceFlags{ 0 }; - D3D_FEATURE_LEVEL _featureLevel{ D3D_FEATURE_LEVEL::D3D_FEATURE_LEVEL_11_0 }; - float _backgroundColor[4] = { 0, 0, 0, 0 }; - bool _usevsync{ true }; - - bool createDevice(); - void reset(); - }; -} - -#endif \ No newline at end of file diff --git a/inc/platform-dx/implementations.hpp b/inc/platform-dx/implementations.hpp index e88b2af..a0545fc 100644 --- a/inc/platform-dx/implementations.hpp +++ b/inc/platform-dx/implementations.hpp @@ -2,7 +2,7 @@ #define XNA_PLATFORM_DX_IMPLEMENTATIONS_HPP #include "dxheaders.hpp" -#include "platform-dx/device-dx.hpp" +#include "graphics/device.hpp" #include "graphics/adapter.hpp" #include "graphics/blendstate.hpp" #include "graphics/buffer.hpp" @@ -21,6 +21,8 @@ #include "graphics/rendertarget.hpp" #include "game/window.hpp" #include "audio/audioengine.hpp" +#include "graphics/viewport.hpp" +#include "common/color.hpp" namespace xna { struct SpriteFont::PlatformImplementation { @@ -174,24 +176,7 @@ namespace xna { } ID3D11Buffer* dxBuffer = nullptr; - }; - - template - inline bool IndexBuffer::Initialize(std::vector const& data, xna_error_ptr_arg) { - if (!impl || !m_device || !m_device->_device || data.empty()) { - xna_error_apply(err, XnaErrorCode::INVALID_OPERATION); - return false; - } - - const auto hr = DirectX::CreateStaticBuffer(m_device->_device, data.data(), data.size(), sizeof(T), D3D11_BIND_INDEX_BUFFER, &impl->dxBuffer); - - if (FAILED(hr)) { - xna_error_apply(err, XnaErrorCode::FAILED_OPERATION); - return false; - } - - return true; - } + }; struct Keyboard::PlatformImplementation { inline static uptr _dxKeyboard = nullptr; @@ -314,26 +299,7 @@ namespace xna { ID3D11Buffer* dxBuffer = nullptr; UINT size{ 0 }; - }; - - template - inline bool VertexBuffer::Initialize(std::vector const& data, xna_error_ptr_arg) { - if (!impl || !m_device || !m_device->_device || data.empty()) { - xna_error_apply(err, XnaErrorCode::INVALID_OPERATION); - return false; - } - - const auto hr = DirectX::CreateStaticBuffer(m_device->_device, data.data(), data.size(), sizeof(T), D3D11_BIND_VERTEX_BUFFER, &impl->dxBuffer); - - if (FAILED(hr)) { - xna_error_apply(err, XnaErrorCode::FAILED_OPERATION); - return false; - } - - impl->size = sizeof(T); - - return true; - } + }; struct VertexInputLayout::PlatformImplementation { ~PlatformImplementation() { @@ -476,6 +442,60 @@ namespace xna { uptr _dxAudioEngine = nullptr; }; + + struct GraphicsDevice::PlatformImplementation { + ID3D11Device* _device{ nullptr }; + ID3D11DeviceContext* _context{ nullptr }; + IDXGIFactory1* _factory = nullptr; + sptr _swapChain{ nullptr }; + sptr _adapter{ nullptr }; + sptr _renderTarget2D{ nullptr }; + sptr _blendState{ nullptr }; + xna::Viewport _viewport{}; + sptr _presentationParameters; + D3D_FEATURE_LEVEL _featureLevel{ D3D_FEATURE_LEVEL::D3D_FEATURE_LEVEL_11_0 }; + + private: + friend class GraphicsDevice; + float _backgroundColor[4] = { 0, 0, 0, 0 }; + bool _usevsync{ true }; + }; + + template + inline bool IndexBuffer::Initialize(std::vector const& data, xna_error_ptr_arg) { + if (!impl || !m_device || !m_device->impl->_device || data.empty()) { + xna_error_apply(err, XnaErrorCode::INVALID_OPERATION); + return false; + } + + const auto hr = DirectX::CreateStaticBuffer(m_device->impl->_device, data.data(), data.size(), sizeof(T), D3D11_BIND_INDEX_BUFFER, &impl->dxBuffer); + + if (FAILED(hr)) { + xna_error_apply(err, XnaErrorCode::FAILED_OPERATION); + return false; + } + + return true; + } + + template + inline bool VertexBuffer::Initialize(std::vector const& data, xna_error_ptr_arg) { + if (!impl || !m_device || !m_device->impl->_device || data.empty()) { + xna_error_apply(err, XnaErrorCode::INVALID_OPERATION); + return false; + } + + const auto hr = DirectX::CreateStaticBuffer(m_device->impl->_device, data.data(), data.size(), sizeof(T), D3D11_BIND_VERTEX_BUFFER, &impl->dxBuffer); + + if (FAILED(hr)) { + xna_error_apply(err, XnaErrorCode::FAILED_OPERATION); + return false; + } + + impl->size = sizeof(T); + + return true; + } } #endif \ No newline at end of file diff --git a/inc/platform-dx/xna-dx.hpp b/inc/platform-dx/xna-dx.hpp index ed3bd63..8b893e9 100644 --- a/inc/platform-dx/xna-dx.hpp +++ b/inc/platform-dx/xna-dx.hpp @@ -1,5 +1,4 @@ #include "content-readers/texture2Dreader-dx.hpp" -#include "device-dx.hpp" #include "dx/StepTimer.hpp" #include "dxheaders.hpp" #include "game-dx.hpp"