Skip to content

Commit 4bfd962

Browse files
committed
fix: 多个错误修复
1 parent fb4c3b1 commit 4bfd962

19 files changed

+205
-110
lines changed

src/Magpie.Core/CursorDrawer2.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#include "pch.h"
2+
#include "CursorDrawer2.h"
3+
4+
namespace Magpie {
5+
6+
bool CursorDrawer2::Initialize(GraphicsContext& graphicsContext) noexcept {
7+
_graphicsContext = &graphicsContext;
8+
9+
return true;
10+
}
11+
12+
}

src/Magpie.Core/CursorDrawer2.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#pragma once
2+
3+
namespace Magpie {
4+
5+
class GraphicsContext;
6+
7+
class CursorDrawer2 {
8+
public:
9+
CursorDrawer2() noexcept = default;
10+
CursorDrawer2(const CursorDrawer2&) = delete;
11+
CursorDrawer2(CursorDrawer2&&) = delete;
12+
13+
bool Initialize(GraphicsContext& graphicsContext) noexcept;
14+
15+
private:
16+
GraphicsContext* _graphicsContext = nullptr;
17+
};
18+
19+
}

src/Magpie.Core/FrameProducer.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ void FrameProducer::OnResizedAsync(
7878
Size& outputSize,
7979
SimpleTask<HRESULT>& task
8080
) noexcept {
81-
_dispatcher.TryEnqueue([&] {
81+
_dispatcher.TryEnqueue([&, rendererSize] {
8282
HRESULT hr = S_OK;
8383
auto se = wil::scope_exit([&] {
8484
// 同步 outputSize
@@ -211,18 +211,18 @@ void FrameProducer::_ProducerThreadProc(
211211
RECT srcRect,
212212
Size rendererSize,
213213
Size& outputSize,
214-
SimpleTask<bool>& task
214+
SimpleTask<bool>& initializeTask
215215
) noexcept {
216216
#ifdef _DEBUG
217217
SetThreadDescription(GetCurrentThread(), L"Magpie-缩放生产者线程");
218218
#endif
219219

220220
if (_Initialize(colorInfo, hMonSrc, srcRect, rendererSize, outputSize)) {
221221
// 同步 outputSize
222-
task.SetResult(true, std::memory_order_release);
222+
initializeTask.SetResult(true, std::memory_order_release);
223223
} else {
224224
Logger::Get().Error("_Initialize 失败");
225-
task.SetResult(false);
225+
initializeTask.SetResult(false);
226226
return;
227227
}
228228

src/Magpie.Core/FrameProducer.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ class FrameProducer {
5151
RECT srcRect,
5252
Size rendererSize,
5353
Size& outputSize,
54-
SimpleTask<bool>& task
54+
SimpleTask<bool>& initializeTask
5555
) noexcept;
5656

5757
bool _Initialize(

src/Magpie.Core/GraphicsCaptureFrameSource.cpp

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -673,27 +673,6 @@ HRESULT GraphicsCaptureFrameSource::OnCursorVisibilityChanged(bool isVisible, bo
673673
return S_OK;
674674
}
675675

676-
static bool IsDebugLayersAvailable() noexcept {
677-
#ifdef _DEBUG
678-
static bool result = SUCCEEDED(D3D11CreateDevice(
679-
nullptr,
680-
D3D_DRIVER_TYPE_NULL, // There is no need to create a real hardware device.
681-
nullptr,
682-
D3D11_CREATE_DEVICE_DEBUG, // Check for the SDK layers.
683-
nullptr, // Any feature level will do.
684-
0,
685-
D3D11_SDK_VERSION,
686-
nullptr, // No need to keep the D3D device reference.
687-
nullptr, // No need to know the feature level.
688-
nullptr // No need to keep the D3D device context reference.
689-
));
690-
return result;
691-
#else
692-
// Relaese 配置不使用调试层
693-
return false;
694-
#endif
695-
}
696-
697676
bool GraphicsCaptureFrameSource::_CreateCaptureDevice(HMONITOR hMonSrc) noexcept {
698677
// 查找源窗口所在屏幕连接的适配器
699678
winrt::com_ptr<IDXGIAdapter1> srcMonAdapter =
@@ -729,9 +708,9 @@ bool GraphicsCaptureFrameSource::_CreateCaptureDevice(HMONITOR hMonSrc) noexcept
729708
const UINT nFeatureLevels = ARRAYSIZE(featureLevels);
730709

731710
UINT createDeviceFlags = D3D11_CREATE_DEVICE_BGRA_SUPPORT;
732-
if (IsDebugLayersAvailable()) {
733-
createDeviceFlags |= D3D11_CREATE_DEVICE_DEBUG;
734-
}
711+
#ifdef _DEBUG
712+
createDeviceFlags |= D3D11_CREATE_DEVICE_DEBUG;
713+
#endif
735714

736715
winrt::com_ptr<ID3D11Device> d3dDevice;
737716
winrt::com_ptr<ID3D11DeviceContext> d3dDC;
@@ -780,7 +759,16 @@ bool GraphicsCaptureFrameSource::_CreateCaptureDevice(HMONITOR hMonSrc) noexcept
780759
return false;
781760
}
782761

783-
winrt::com_ptr<IDXGIDevice> dxgiDevice = _d3d11Device.try_as<IDXGIDevice>();
762+
#ifdef _DEBUG
763+
// 调试层汇报错误或警告时中断
764+
if (winrt::com_ptr<ID3D11InfoQueue> infoQueue = d3dDevice.try_as<ID3D11InfoQueue>()) {
765+
infoQueue->SetBreakOnSeverity(D3D11_MESSAGE_SEVERITY_CORRUPTION, TRUE);
766+
infoQueue->SetBreakOnSeverity(D3D11_MESSAGE_SEVERITY_ERROR, TRUE);
767+
infoQueue->SetBreakOnSeverity(D3D11_MESSAGE_SEVERITY_WARNING, TRUE);
768+
}
769+
#endif
770+
771+
winrt::com_ptr<IDXGIDevice> dxgiDevice = d3dDevice.try_as<IDXGIDevice>();
784772
if (!dxgiDevice) {
785773
Logger::Get().Error("获取 IDXGIDevice 失败");
786774
return false;
@@ -807,6 +795,15 @@ bool GraphicsCaptureFrameSource::_CreateBridgeDeviceResources(IDXGIAdapter1* dxg
807795

808796
Logger::Get().Info("已创建 D3D12 设备");
809797

798+
#ifdef _DEBUG
799+
// 调试层汇报错误或警告时中断
800+
if (winrt::com_ptr<ID3D12InfoQueue> infoQueue = _bridgeDevice.try_as<ID3D12InfoQueue>()) {
801+
infoQueue->SetBreakOnSeverity(D3D12_MESSAGE_SEVERITY_CORRUPTION, TRUE);
802+
infoQueue->SetBreakOnSeverity(D3D12_MESSAGE_SEVERITY_ERROR, TRUE);
803+
infoQueue->SetBreakOnSeverity(D3D12_MESSAGE_SEVERITY_WARNING, TRUE);
804+
}
805+
#endif
806+
810807
// 不应使用集成显卡捕获,集成显卡没有高速的专用显存,捕获延迟很高
811808
{
812809
D3D12_FEATURE_DATA_ARCHITECTURE1 value{};

src/Magpie.Core/GraphicsContext.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,15 @@ bool GraphicsContext::Initialize(
2525
return false;
2626
}
2727

28+
#ifdef _DEBUG
29+
// 调试层汇报错误或警告时中断
30+
if (winrt::com_ptr<ID3D12InfoQueue> infoQueue = _device.try_as<ID3D12InfoQueue>()) {
31+
infoQueue->SetBreakOnSeverity(D3D12_MESSAGE_SEVERITY_CORRUPTION, TRUE);
32+
infoQueue->SetBreakOnSeverity(D3D12_MESSAGE_SEVERITY_ERROR, TRUE);
33+
infoQueue->SetBreakOnSeverity(D3D12_MESSAGE_SEVERITY_WARNING, TRUE);
34+
}
35+
#endif
36+
2837
// 检查根签名版本
2938
{
3039
D3D12_FEATURE_DATA_ROOT_SIGNATURE featureData = { .HighestVersion = D3D_ROOT_SIGNATURE_VERSION_1_1 };

src/Magpie.Core/Magpie.Core.vcxproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
<ClInclude Include="BackendDescriptorStore.h" />
5151
<ClInclude Include="CatmullRomEffectDrawer.h" />
5252
<ClInclude Include="CompSwapchainPresenter.h" />
53+
<ClInclude Include="CursorDrawer2.h" />
5354
<ClInclude Include="CursorManager.h" />
5455
<ClInclude Include="CursorDrawer.h" />
5556
<ClInclude Include="DDS.h" />
@@ -104,6 +105,7 @@
104105
<ClCompile Include="BackendDescriptorStore.cpp" />
105106
<ClCompile Include="CatmullRomEffectDrawer.cpp" />
106107
<ClCompile Include="CompSwapchainPresenter.cpp" />
108+
<ClCompile Include="CursorDrawer2.cpp" />
107109
<ClCompile Include="CursorManager.cpp" />
108110
<ClCompile Include="CursorDrawer.cpp" />
109111
<ClCompile Include="DDSHelper.cpp" />

src/Magpie.Core/Magpie.Core.vcxproj.filters

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,9 @@
168168
<ClInclude Include="EffectsDrawer.h">
169169
<Filter>Render</Filter>
170170
</ClInclude>
171+
<ClInclude Include="CursorDrawer2.h">
172+
<Filter>Render</Filter>
173+
</ClInclude>
171174
</ItemGroup>
172175
<ItemGroup>
173176
<ClCompile Include="ScalingRuntime.cpp" />
@@ -286,6 +289,9 @@
286289
<ClCompile Include="EffectsDrawer.cpp">
287290
<Filter>Render</Filter>
288291
</ClCompile>
292+
<ClCompile Include="CursorDrawer2.cpp">
293+
<Filter>Render</Filter>
294+
</ClCompile>
289295
</ItemGroup>
290296
<ItemGroup>
291297
<FxCompile Include="shaders\SimpleVS.hlsl">

src/Magpie.Core/Renderer.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
#endif
2525
#include <dispatcherqueue.h>
2626
#include <d3dkmthk.h>
27-
#include "Renderer2.h"
2827

2928
namespace Magpie {
3029

src/Magpie.Core/Renderer2.cpp

Lines changed: 77 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,11 @@ ScalingError Renderer2::Initialize(
9696
_frameProducer.InitializeAsync(
9797
_graphicsContext, _colorInfo, hMonitor, srcRect, size, outputSize, task);
9898

99+
if (!_cursorDrawer.Initialize(_graphicsContext)) {
100+
Logger::Get().Error("CursorDrawer2::Initialize 失败");
101+
return ScalingError::ScalingFailedGeneral;
102+
}
103+
99104
_presenter = std::make_unique<SwapChainPresenter>();
100105
if (!_presenter->Initialize(_graphicsContext, hwndAttach, size, _colorInfo)) {
101106
Logger::Get().Error("SwapChainPresenter::Initialize 失败");
@@ -172,6 +177,11 @@ void Renderer2::OnResized(Size size) noexcept {
172177
return;
173178
}
174179

180+
// 确保消费者不再使用环形缓冲区
181+
if (!_CheckResult(_graphicsContext.WaitForGpu(), "GraphicsContext::WaitForGpu 失败")) {
182+
return;
183+
}
184+
175185
Size outputSize;
176186
SimpleTask<HRESULT> task;
177187
_frameProducer.OnResizedAsync(size, outputSize, task);
@@ -186,8 +196,6 @@ void Renderer2::OnResized(Size size) noexcept {
186196
}
187197

188198
_UpdateOutputRect(outputSize);
189-
190-
_CheckResult(_RenderImpl(true), "_RenderImpl 失败");
191199
}
192200

193201
void Renderer2::OnMsgDisplayChanged() noexcept {
@@ -339,22 +347,29 @@ HRESULT Renderer2::_UpdateColorSpace() noexcept {
339347
return S_OK;
340348
}
341349

350+
// 确保消费者不再使用环形缓冲区
351+
HRESULT hr = _graphicsContext.WaitForGpu();
352+
if (FAILED(hr)) {
353+
Logger::Get().ComError("GraphicsContext::WaitForGpu 失败", hr);
354+
return hr;
355+
}
356+
342357
SimpleTask<HRESULT> task;
343358
_frameProducer.OnColorInfoChangedAsync(_colorInfo, task);
344359

345-
HRESULT hr = _presenter->OnColorInfoChanged(_colorInfo);
360+
hr = _presenter->OnColorInfoChanged(_colorInfo);
346361
if (FAILED(hr)) {
347362
Logger::Get().ComError("SwapChainPresenter::OnColorInfoChanged 失败", hr);
348363
return hr;
349364
}
350365

351366
hr = task.GetResult();
352-
if (!_CheckResult(hr, "FrameProducer::OnColorInfoChangedAsync 失败")) {
367+
if (FAILED(hr)) {
368+
Logger::Get().ComError("FrameProducer::OnColorInfoChangedAsync 失败", hr);
353369
return hr;
354370
}
355371

356-
_CheckResult(_RenderImpl(true), "_RenderImpl 失败");
357-
372+
ScalingWindow::Get().Render();
358373
return S_OK;
359374
}
360375

@@ -383,41 +398,77 @@ HRESULT Renderer2::_RenderImpl(bool waitForGpu) noexcept {
383398

384399
ID3D12GraphicsCommandList* commandList = _graphicsContext.GetCommandList();
385400

386-
if (const Size size = _presenter->GetSize(); _outputRect == RECT{ 0,0,(LONG)size.width,(LONG)size.height }) {
401+
{
402+
CD3DX12_RESOURCE_BARRIER barrier = CD3DX12_RESOURCE_BARRIER::Transition(
403+
frameTex, D3D12_RESOURCE_STATE_PRESENT, D3D12_RESOURCE_STATE_COPY_DEST, 0);
404+
commandList->ResourceBarrier(1, &barrier);
405+
}
406+
407+
if (Size size = _presenter->GetSize(); _outputRect == Rect{ 0,0,size.width,size.height }) {
408+
commandList->CopyResource(frameTex, curBuffer);
409+
387410
{
388411
CD3DX12_RESOURCE_BARRIER barrier = CD3DX12_RESOURCE_BARRIER::Transition(
389-
frameTex, D3D12_RESOURCE_STATE_PRESENT, D3D12_RESOURCE_STATE_COPY_DEST, 0);
412+
frameTex, D3D12_RESOURCE_STATE_COPY_DEST, D3D12_RESOURCE_STATE_PRESENT, 0);
390413
commandList->ResourceBarrier(1, &barrier);
391414
}
392-
393-
commandList->CopyResource(frameTex, curBuffer);
394415
} else {
416+
CD3DX12_TEXTURE_COPY_LOCATION src(curBuffer, 0);
417+
CD3DX12_TEXTURE_COPY_LOCATION dest(frameTex, 0);
418+
commandList->CopyTextureRegion(&dest, _outputRect.left, _outputRect.top, 0, &src, nullptr);
419+
395420
{
396421
D3D12_RESOURCE_BARRIER barrier = CD3DX12_RESOURCE_BARRIER::Transition(
397-
frameTex, D3D12_RESOURCE_STATE_PRESENT, D3D12_RESOURCE_STATE_RENDER_TARGET, 0);
422+
frameTex, D3D12_RESOURCE_STATE_COPY_DEST, D3D12_RESOURCE_STATE_RENDER_TARGET, 0);
398423
commandList->ResourceBarrier(1, &barrier);
399424
}
400425

401426
// 存在黑边时应填充背景。使用交换链呈现时需要这个操作,因为我们指定了
402427
// DXGI_SWAP_EFFECT_FLIP_DISCARD,同时也是为了和 RTSS 兼容。
403-
static constexpr FLOAT BLACK[4] = { 0.0f,0.0f,0.0f,1.0f };
404-
commandList->ClearRenderTargetView(rtvHandle, BLACK, 0, nullptr);
428+
{
429+
SmallVector<D3D12_RECT, 4> rects;
430+
if (_outputRect.left > 0) {
431+
rects.push_back({
432+
0,
433+
0,
434+
(LONG)_outputRect.left,
435+
(LONG)size.height
436+
});
437+
}
438+
if (_outputRect.top > 0) {
439+
rects.push_back({
440+
(LONG)_outputRect.left,
441+
0,
442+
(LONG)_outputRect.right,
443+
(LONG)_outputRect.top
444+
});
445+
}
446+
if (_outputRect.right < size.width) {
447+
rects.push_back({
448+
(LONG)_outputRect.right,
449+
0,
450+
(LONG)size.width,
451+
(LONG)size.height
452+
});
453+
}
454+
if (_outputRect.bottom < size.height) {
455+
rects.push_back({
456+
(LONG)_outputRect.left,
457+
(LONG)_outputRect.bottom,
458+
(LONG)_outputRect.right,
459+
(LONG)size.height
460+
});
461+
}
462+
463+
static constexpr FLOAT BLACK[4] = { 0.0f,0.0f,0.0f,1.0f };
464+
commandList->ClearRenderTargetView(rtvHandle, BLACK, (UINT)rects.size(), rects.data());
465+
}
405466

406467
{
407468
CD3DX12_RESOURCE_BARRIER barrier = CD3DX12_RESOURCE_BARRIER::Transition(
408-
frameTex, D3D12_RESOURCE_STATE_RENDER_TARGET, D3D12_RESOURCE_STATE_COPY_DEST, 0);
469+
frameTex, D3D12_RESOURCE_STATE_RENDER_TARGET, D3D12_RESOURCE_STATE_PRESENT, 0);
409470
commandList->ResourceBarrier(1, &barrier);
410471
}
411-
412-
CD3DX12_TEXTURE_COPY_LOCATION src(curBuffer, 0);
413-
CD3DX12_TEXTURE_COPY_LOCATION dest(frameTex, 0);
414-
commandList->CopyTextureRegion(&dest, _outputRect.left, _outputRect.top, 0, &src, nullptr);
415-
}
416-
417-
{
418-
CD3DX12_RESOURCE_BARRIER barrier = CD3DX12_RESOURCE_BARRIER::Transition(
419-
frameTex, D3D12_RESOURCE_STATE_COPY_DEST, D3D12_RESOURCE_STATE_PRESENT, 0);
420-
commandList->ResourceBarrier(1, &barrier);
421472
}
422473

423474
hr = commandList->Close();
@@ -480,8 +531,8 @@ void Renderer2::_UpdateOutputRect(Size outputSize) noexcept {
480531
_outputRect.bottom = rendererSize.height;
481532
}
482533

483-
assert(_outputRect.left + (LONG)outputSize.width == _outputRect.right);
484-
assert(_outputRect.top + (LONG)outputSize.height == _outputRect.bottom);
534+
assert(_outputRect.left + outputSize.width == _outputRect.right);
535+
assert(_outputRect.top + outputSize.height == _outputRect.bottom);
485536
}
486537

487538
bool Renderer2::_CheckResult(bool success, std::string_view errorMsg) noexcept {

0 commit comments

Comments
 (0)