feat: 添加 Renderer 和 DeviceResources

This commit is contained in:
刘旭 2023-06-06 19:28:20 +08:00
commit a68083c68f
12 changed files with 141 additions and 13 deletions

View file

@ -316,7 +316,7 @@ bool ScalingService::_StartScale(HWND hWnd, const Profile& profile) {
options.IsSimulateExclusiveFullscreen(settings.IsSimulateExclusiveFullscreen());
_isAutoScaling = profile.isAutoScale;
_ScalingRuntime->Start(hWnd, options);
_ScalingRuntime->Start(hWnd, std::move(options));
return true;
}

View file

@ -0,0 +1,32 @@
#include "pch.h"
#include "DeviceResources.h"
#include "ScalingOptions.h"
namespace Magpie::Core {
bool DeviceResources::Initialize(HWND /*hwndScaling*/, const ScalingOptions& /*options*/) noexcept {
return true;
}
bool DeviceResources::IsDebugLayersAvailable() noexcept {
#ifdef _DEBUG
static bool result = SUCCEEDED(D3D11CreateDevice(
nullptr,
D3D_DRIVER_TYPE_NULL, // There is no need to create a real hardware device.
nullptr,
D3D11_CREATE_DEVICE_DEBUG, // Check for the SDK layers.
nullptr, // Any feature level will do.
0,
D3D11_SDK_VERSION,
nullptr, // No need to keep the D3D device reference.
nullptr, // No need to know the feature level.
nullptr // No need to keep the D3D device context reference.
));
return result;
#else
// Relaese 配置不使用调试层
return false;
#endif
}
}

View file

@ -0,0 +1,18 @@
#pragma once
namespace Magpie::Core {
struct ScalingOptions;
class DeviceResources {
public:
DeviceResources() noexcept = default;
DeviceResources(const DeviceResources&) = delete;
DeviceResources(DeviceResources&&) noexcept = default;
bool Initialize(HWND hwndScaling, const ScalingOptions& options) noexcept;
static bool IsDebugLayersAvailable() noexcept;
};
}

View file

@ -80,6 +80,7 @@
<ItemGroup>
<ClInclude Include="DDS.h" />
<ClInclude Include="DDSLoderHelpers.h" />
<ClInclude Include="DeviceResources.h" />
<ClInclude Include="DirectXHelper.h" />
<ClInclude Include="EffectCacheManager.h" />
<ClInclude Include="EffectCompiler.h" />
@ -90,6 +91,7 @@
<ClInclude Include="ImGuiHelper.h" />
<ClInclude Include="include\Magpie.Core.h" />
<ClInclude Include="LoggerHelper.h" />
<ClInclude Include="Renderer.h" />
<ClInclude Include="ScalingOptions.h" />
<ClInclude Include="ScalingRuntime.h" />
<ClInclude Include="pch.h" />
@ -100,6 +102,7 @@
<ClInclude Include="YasHelper.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="DeviceResources.cpp" />
<ClCompile Include="DirectXHelper.cpp" />
<ClCompile Include="DllMain.cpp" />
<ClCompile Include="EffectCacheManager.cpp" />
@ -110,6 +113,7 @@
<ClCompile Include="pch.cpp">
<PrecompiledHeader>Create</PrecompiledHeader>
</ClCompile>
<ClCompile Include="Renderer.cpp" />
<ClCompile Include="ScalingRuntime.cpp" />
<ClCompile Include="ScalingWindow.cpp" />
<ClCompile Include="TextureLoader.cpp" />

View file

@ -66,6 +66,8 @@
</ClInclude>
<ClInclude Include="WindowBase.h" />
<ClInclude Include="ScalingWindow.h" />
<ClInclude Include="Renderer.h" />
<ClInclude Include="DeviceResources.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="ScalingRuntime.cpp" />
@ -92,6 +94,8 @@
<Filter>Overlay</Filter>
</ClCompile>
<ClCompile Include="ScalingWindow.cpp" />
<ClCompile Include="Renderer.cpp" />
<ClCompile Include="DeviceResources.cpp" />
</ItemGroup>
<ItemGroup>
<ResourceCompile Include="Magpie.Core.rc">

View file

@ -0,0 +1,24 @@
#include "pch.h"
#include "Renderer.h"
#include "DeviceResources.h"
#include "ScalingOptions.h"
namespace Magpie::Core {
Renderer::Renderer() noexcept {}
Renderer::~Renderer() noexcept {}
bool Renderer::Initialize(HWND hwndScaling, const ScalingOptions& options) noexcept {
_deviceResources = std::make_unique<DeviceResources>();
if (!_deviceResources->Initialize(hwndScaling, options)) {
return false;
}
return true;
}
void Renderer::Render() noexcept {
}
}

View file

@ -0,0 +1,24 @@
#pragma once
namespace Magpie::Core {
struct ScalingOptions;
class DeviceResources;
class Renderer {
public:
Renderer() noexcept;
~Renderer() noexcept;
Renderer(const Renderer&) = delete;
Renderer(Renderer&&) noexcept = default;
bool Initialize(HWND hwndScaling, const ScalingOptions& options) noexcept;
void Render() noexcept;
private:
std::unique_ptr<DeviceResources> _deviceResources;
};
}

View file

@ -23,7 +23,7 @@ ScalingRuntime::~ScalingRuntime() {
}
}
void ScalingRuntime::Start(HWND hwndSrc, const ScalingOptions& options) {
void ScalingRuntime::Start(HWND hwndSrc, ScalingOptions&& options) {
if (_isRunning) {
return;
}
@ -31,9 +31,9 @@ void ScalingRuntime::Start(HWND hwndSrc, const ScalingOptions& options) {
_IsRunning(true);
_EnsureDispatcherQueue();
_dqc.DispatcherQueue().TryEnqueue([this, hwndSrc, options(options)]() mutable {
_dqc.DispatcherQueue().TryEnqueue([this, hwndSrc, options(std::move(options))]() mutable {
_scalingWindow = std::make_unique<ScalingWindow>();
_scalingWindow->Create(GetModuleHandle(nullptr));
_scalingWindow->Create(GetModuleHandle(nullptr), std::move(options));
});
}

View file

@ -19,7 +19,7 @@ public:
return _isRunning ? _hwndSrc : 0;
}
void Start(HWND hwndSrc, const ScalingOptions& options);
void Start(HWND hwndSrc, ScalingOptions&& options);
void ToggleOverlay();

View file

@ -2,10 +2,15 @@
#include "ScalingWindow.h"
#include "CommonSharedConstants.h"
#include "Logger.h"
#include "Renderer.h"
namespace Magpie::Core {
bool ScalingWindow::Create(HINSTANCE hInstance) noexcept {
ScalingWindow::ScalingWindow() noexcept {}
ScalingWindow::~ScalingWindow() noexcept {}
bool ScalingWindow::Create(HINSTANCE hInstance, ScalingOptions&& options) noexcept {
static const int _ = [](HINSTANCE hInstance) {
WNDCLASSEXW wcex{};
wcex.cbSize = sizeof(wcex);
@ -30,23 +35,28 @@ bool ScalingWindow::Create(HINSTANCE hInstance) noexcept {
this
);
if (!Handle()) {
if (!_hWnd) {
return false;
}
// 设置窗口不透明
// 不完全透明时可关闭 DirectFlip
if (!SetLayeredWindowAttributes(Handle(), 0, 255, LWA_ALPHA)) {
if (!SetLayeredWindowAttributes(_hWnd, 0, 255, LWA_ALPHA)) {
Logger::Get().Win32Error("SetLayeredWindowAttributes 失败");
}
ShowWindow(Handle(), SW_SHOWMAXIMIZED);
_renderer = std::make_unique<Renderer>();
if (!_renderer->Initialize(_hWnd, options)) {
return false;
}
ShowWindow(_hWnd, SW_SHOWMAXIMIZED);
return true;
}
void ScalingWindow::Render() noexcept {
_renderer->Render();
}
}

View file

@ -3,13 +3,22 @@
namespace Magpie::Core {
struct ScalingOptions;
class Renderer;
class ScalingWindow : public WindowBase<ScalingWindow> {
friend class base_type;
public:
bool Create(HINSTANCE hInstance) noexcept;
ScalingWindow() noexcept;
~ScalingWindow() noexcept;
bool Create(HINSTANCE hInstance, ScalingOptions&& options) noexcept;
void Render() noexcept;
private:
std::unique_ptr<Renderer> _renderer;
};
}

View file

@ -5,7 +5,11 @@ namespace Magpie::Core {
template <typename T>
class WindowBase {
public:
virtual ~WindowBase() {
WindowBase() noexcept = default;
WindowBase(const WindowBase&) = delete;
WindowBase(WindowBase&&) noexcept = default;
virtual ~WindowBase() noexcept {
Destroy();
}
@ -51,7 +55,6 @@ protected:
return DefWindowProc(_hWnd, msg, wParam, lParam);
}
private:
HWND _hWnd = NULL;
};