diff --git a/framework/forward.hpp b/framework/forward.hpp index 56276f0..e811416 100644 --- a/framework/forward.hpp +++ b/framework/forward.hpp @@ -100,8 +100,10 @@ namespace xna { using PDisplayMode = std::shared_ptr; class DisplayModeCollection; using PDisplayModeCollection = std::shared_ptr; + using UDisplayModeCollection = std::unique_ptr; class GraphicsAdapter; using PGraphicsAdapter = std::shared_ptr; + using UGraphicsAdapter = std::unique_ptr; class GraphicsDevice; using PGraphicsDevice = std::shared_ptr; class GraphicsDeviceInformation; diff --git a/framework/graphics/adapter.hpp b/framework/graphics/adapter.hpp index 996502c..14aac19 100644 --- a/framework/graphics/adapter.hpp +++ b/framework/graphics/adapter.hpp @@ -19,7 +19,11 @@ namespace xna { virtual Uint Revision() const = 0; virtual Uint SubSystemId() const = 0; virtual Uint VendorId() const = 0; - virtual PDisplayModeCollection SupportedDisplayModes() const = 0; + virtual UDisplayModeCollection SupportedDisplayModes() const = 0; + + static UGraphicsAdapter DefaultAdapter(); + static void GetAllAdapters(std::vector& adapters); + static void GetAllAdapters(std::vector& adapters); }; } diff --git a/framework/platform/adapter-dx.cpp b/framework/platform/adapter-dx.cpp index b3a970c..8e015d9 100644 --- a/framework/platform/adapter-dx.cpp +++ b/framework/platform/adapter-dx.cpp @@ -3,17 +3,76 @@ namespace xna { - PGraphicsAdapter GraphicsAdapter::DefaultAdapter() { - if (_adaptersList.empty()) + UGraphicsAdapter IGraphicsAdapter::DefaultAdapter() { + IDXGIFactory1* pFactory = nullptr; + + if FAILED(CreateDXGIFactory1(__uuidof(IDXGIFactory1), (void**)&pFactory)) return nullptr; - if (_defaultAdapterIndex >= _adaptersList.size()) - return nullptr; + IDXGIAdapter1* pAdapter = nullptr; + + if (pFactory->EnumAdapters1(0, &pAdapter) != DXGI_ERROR_NOT_FOUND) { + auto adp = uNew(); - return _adaptersList[_defaultAdapterIndex]; + adp->_index = 0; + adp->_adapter = pAdapter; + + return std::move(adp); + } + + pFactory->Release(); + pFactory = nullptr; + + return nullptr; + } + + void IGraphicsAdapter::GetAllAdapters(std::vector& adapters){ + IDXGIFactory1* pFactory = nullptr; + + if FAILED(CreateDXGIFactory1(__uuidof(IDXGIFactory1), (void**)&pFactory)) + return; + + IDXGIAdapter1* pAdapter = nullptr; + UINT count = 0; + + for (; pFactory->EnumAdapters1(count, &pAdapter) != DXGI_ERROR_NOT_FOUND; ++count) { + auto adp = New(); + + adp->_index = count; + adp->_adapter = pAdapter; + + adapters.push_back(adp); + } + + pFactory->Release(); + pFactory = nullptr; + } + + void IGraphicsAdapter::GetAllAdapters(std::vector& adapters) { + IDXGIFactory1* pFactory = nullptr; + + if FAILED(CreateDXGIFactory1(__uuidof(IDXGIFactory1), (void**)&pFactory)) + return; + + IDXGIAdapter1* pAdapter = nullptr; + UINT count = 0; + + for (; pFactory->EnumAdapters1(count, &pAdapter) != DXGI_ERROR_NOT_FOUND; ++count) { + auto adp = uNew(); + + adp->_index = count; + adp->_adapter = pAdapter; + + adapters.push_back(std::move(adp)); + } + + pFactory->Release(); + pFactory = nullptr; } String GraphicsAdapter::Description() const { + if (!_adapter) return String(); + DXGI_ADAPTER_DESC1 desc; _adapter->GetDesc1(&desc); String description = XnaHToString(desc.Description); @@ -21,6 +80,8 @@ namespace xna { } Uint GraphicsAdapter::DeviceId() const { + if (!_adapter) return 0; + DXGI_ADAPTER_DESC1 desc; _adapter->GetDesc1(&desc); @@ -28,6 +89,8 @@ namespace xna { } String GraphicsAdapter::DeviceName() const { + if (!_adapter) return String(); + IDXGIOutput* pOutput = nullptr; DXGI_OUTPUT_DESC outputDesc; @@ -45,6 +108,8 @@ namespace xna { } intptr_t GraphicsAdapter::MonitorHandle() const { + if (!_adapter) return 0; + IDXGIOutput* pOutput = nullptr; DXGI_OUTPUT_DESC outputDesc; @@ -61,6 +126,8 @@ namespace xna { } Uint GraphicsAdapter::Revision() const { + if (!_adapter) return 0; + DXGI_ADAPTER_DESC1 desc; _adapter->GetDesc1(&desc); @@ -68,6 +135,8 @@ namespace xna { } Uint GraphicsAdapter::SubSystemId() const { + if (!_adapter) return 0; + DXGI_ADAPTER_DESC1 desc; _adapter->GetDesc1(&desc); @@ -75,13 +144,17 @@ namespace xna { } Uint GraphicsAdapter::VendorId() const { + if (!_adapter) return 0; + DXGI_ADAPTER_DESC1 desc; _adapter->GetDesc1(&desc); return static_cast(desc.VendorId); } - PDisplayModeCollection GraphicsAdapter::SupportedDisplayModes() const { + UDisplayModeCollection GraphicsAdapter::SupportedDisplayModes() const { + if (!_adapter) return nullptr; + IDXGIOutput* pOutput = nullptr; UINT numModes = 0; UINT totalModes = 0; @@ -90,7 +163,7 @@ namespace xna { if (_adapter->EnumOutputs(0, &pOutput) != DXGI_ERROR_NOT_FOUND) { for (size_t f = 0; f < SURFACE_FORMAT_COUNT; ++f) { const auto currentSurface = static_cast(f); - DXGI_FORMAT format = SurfaceFormatMapper::ParseToDXGI(currentSurface); + DXGI_FORMAT format = GraphicsAdapter::ToDXGI(currentSurface); pOutput->GetDisplayModeList(format, 0, &numModes, nullptr); @@ -107,47 +180,18 @@ namespace xna { pOutput->Release(); - auto collection = New(totalModes); + auto collection = uNew(totalModes); for (size_t i = 0; i < totalModes; ++i) { const auto& modedesc = buffer[i]; - const auto surface = SurfaceFormatMapper::ParseToSurface(modedesc.Format); + const auto surface = GraphicsAdapter::ToSurface(modedesc.Format); collection->_displayModes[i] = DisplayMode(modedesc.Width, modedesc.Height, surface); } - return collection; + return std::move(collection); } - return New(); - } - - std::vector GraphicsAdapter::getAllAdapters() { - IDXGIFactory1* pFactory = nullptr; - - if FAILED(CreateDXGIFactory1(__uuidof(IDXGIFactory1), (void**)&pFactory)) { - return std::vector(); - } - - std::vector adapters(2); - IDXGIAdapter1* pAdapter = nullptr; - UINT count = 0; - - for (; pFactory->EnumAdapters1(count, &pAdapter) != DXGI_ERROR_NOT_FOUND; ++count) { - auto adp = New(); - - adp->_index = count; - adp->_adapter = pAdapter; - - if (adapters.size() == count) - adapters.push_back(adp); - else - adapters[count] = adp; - } - - if (!adapters.empty() && adapters.size() != count) - adapters.resize(count); - - return adapters; - } + return nullptr; + } } \ No newline at end of file diff --git a/framework/platform/adapter-dx.hpp b/framework/platform/adapter-dx.hpp index 4e7be49..0fb21ea 100644 --- a/framework/platform/adapter-dx.hpp +++ b/framework/platform/adapter-dx.hpp @@ -2,12 +2,13 @@ #define XNA_PLATFORM_ADAPTER_DX_HPP #include "../graphics/adapter.hpp" -#include "dxgi.h" -#include "d3d11.h" +#include "dxheaders.hpp" namespace xna { class GraphicsAdapter : public IGraphicsAdapter { public: + friend class IGraphicsAdapter; + GraphicsAdapter() {} virtual ~GraphicsAdapter() override { @@ -24,31 +25,16 @@ namespace xna { virtual Uint Revision() const override; virtual Uint SubSystemId() const override; virtual Uint VendorId() const override; - virtual PDisplayModeCollection SupportedDisplayModes() const override; - virtual constexpr bool IsDefaultAdapter() const { return _index == _defaultAdapterIndex; } - - static PGraphicsAdapter DefaultAdapter(); - - static constexpr void DefaultAdapter(size_t index) { - _defaultAdapterIndex = index; - } - - static constexpr std::vector Adapters() { - return _adaptersList; - } + virtual UDisplayModeCollection SupportedDisplayModes() const override; + virtual constexpr bool IsDefaultAdapter() const { return _index == 0; } public: - IDXGIAdapter1* _adapter{ nullptr }; - + IDXGIAdapter1* _adapter{ nullptr }; private: - Uint _index{ 0 }; - inline static size_t _defaultAdapterIndex = 0; - static std::vector getAllAdapters(); - inline static std::vector _adaptersList = getAllAdapters(); - }; + Uint _index{ 0 }; - struct SurfaceFormatMapper { - static constexpr DXGI_FORMAT ParseToDXGI(SurfaceFormat format) + public: + static constexpr DXGI_FORMAT ToDXGI(SurfaceFormat format) { switch (format) { @@ -97,7 +83,7 @@ namespace xna { } } - static constexpr SurfaceFormat ParseToSurface(DXGI_FORMAT format) { + static constexpr SurfaceFormat ToSurface(DXGI_FORMAT format) { switch (format) { case DXGI_FORMAT_B8G8R8A8_UNORM: @@ -140,7 +126,7 @@ namespace xna { return SurfaceFormat::Color; } } - }; + }; } #endif diff --git a/framework/platform/audioengine-dx.hpp b/framework/platform/audioengine-dx.hpp index 8a3499d..9741de7 100644 --- a/framework/platform/audioengine-dx.hpp +++ b/framework/platform/audioengine-dx.hpp @@ -85,7 +85,7 @@ namespace xna { if (!_dxAudioEngine) return; const auto reverb = static_cast(value); - _dxAudioEngine->SetMasterVolume(reverb); + _dxAudioEngine->SetReverb(reverb); } public: diff --git a/framework/platform/gdevicemanager-dx.cpp b/framework/platform/gdevicemanager-dx.cpp index 83ba5fe..aefab78 100644 --- a/framework/platform/gdevicemanager-dx.cpp +++ b/framework/platform/gdevicemanager-dx.cpp @@ -11,8 +11,9 @@ namespace xna { bool GraphicsDeviceManager::Initialize() { GraphicsDeviceInformation information; - const auto adp = GraphicsAdapter::DefaultAdapter(); - information.Adapter(adp); + auto adp = GraphicsAdapter::DefaultAdapter(); + const PGraphicsAdapter sadp = std::move(adp); + information.Adapter(sadp); information.GraphicsProfile(xna::GraphicsProfile::HiDef); PresentationParameters parameters; diff --git a/framework/types.hpp b/framework/types.hpp index f162b5e..c37d8a0 100644 --- a/framework/types.hpp +++ b/framework/types.hpp @@ -60,8 +60,6 @@ namespace xna { return std::make_unique<_Ty>(std::forward<_Types>(_Args)...); } -#define PLATFORM_DEVELOPMENT - //See ref: https://en.cppreference.com/w/cpp/error/assert #define assertm(exp, msg) assert(((void)msg, exp)) }