From 080cd5523b04501887b74152f568eaeb12ab245e Mon Sep 17 00:00:00 2001 From: Nomango Date: Mon, 29 Jul 2019 09:40:39 +0800 Subject: [PATCH] update D3DDeviceResources API minor --- Kiwano/platform/Application.cpp | 4 +-- Kiwano/renderer/D3D10DeviceResources.cpp | 27 ++++++++++++++-- Kiwano/renderer/D3D10DeviceResources.h | 8 +++++ Kiwano/renderer/D3D11DeviceResources.cpp | 22 ++++++++++++- Kiwano/renderer/D3D11DeviceResources.h | 8 +++++ Kiwano/renderer/render.cpp | 41 ++++++++---------------- Kiwano/renderer/render.h | 6 ++-- 7 files changed, 80 insertions(+), 36 deletions(-) diff --git a/Kiwano/platform/Application.cpp b/Kiwano/platform/Application.cpp index 28f08590..a6da25bd 100644 --- a/Kiwano/platform/Application.cpp +++ b/Kiwano/platform/Application.cpp @@ -227,12 +227,12 @@ namespace kiwano if (show) { debug_node_ = new DebugNode; - Renderer::Instance().StartCollectData(); + Renderer::Instance().SetCollectingStatus(true); } else { debug_node_.Reset(); - Renderer::Instance().StopCollectData(); + Renderer::Instance().SetCollectingStatus(false); } } diff --git a/Kiwano/renderer/D3D10DeviceResources.cpp b/Kiwano/renderer/D3D10DeviceResources.cpp index 11c967c4..8b26085d 100644 --- a/Kiwano/renderer/D3D10DeviceResources.cpp +++ b/Kiwano/renderer/D3D10DeviceResources.cpp @@ -29,7 +29,7 @@ namespace kiwano { -#if defined(_DEBUG) + namespace DX { HRESULT CreateD3DDevice(IDXGIAdapter *adapter, D3D10_DRIVER_TYPE driver_type, UINT flags, ID3D10Device1 **device) @@ -63,6 +63,7 @@ namespace kiwano return hr; } +#if defined(KGE_DEBUG) inline bool SdkLayersAvailable() { HRESULT hr = CreateD3DDevice( @@ -74,9 +75,11 @@ namespace kiwano return SUCCEEDED(hr); } - } #endif + } // namespace DX + + D3D10DeviceResources::D3D10DeviceResources() : hwnd_(nullptr) { @@ -133,6 +136,26 @@ namespace kiwano return hr; } + HRESULT D3D10DeviceResources::Present(bool vsync) + { + // The first argument instructs DXGI to block until VSync. + return dxgi_swap_chain_->Present(vsync ? 1 : 0, 0); + } + + HRESULT D3D10DeviceResources::ClearRenderTarget(Color& clear_color) + { + d3d_device_->OMSetRenderTargets( + 1, + &d3d_rt_view_, + d3d_ds_view_.Get() + ); + d3d_device_->ClearRenderTargetView( + d3d_rt_view_.Get(), + reinterpret_cast(&clear_color) + ); + return S_OK; + } + void D3D10DeviceResources::DiscardResources() { d3d_device_.Reset(); diff --git a/Kiwano/renderer/D3D10DeviceResources.h b/Kiwano/renderer/D3D10DeviceResources.h index 35a5176d..feb9f228 100644 --- a/Kiwano/renderer/D3D10DeviceResources.h +++ b/Kiwano/renderer/D3D10DeviceResources.h @@ -38,6 +38,14 @@ namespace kiwano HWND hwnd ); + HRESULT Present( + bool vsync + ); + + HRESULT ClearRenderTarget( + Color& clear_color + ); + HRESULT HandleDeviceLost(); HRESULT SetLogicalSize( diff --git a/Kiwano/renderer/D3D11DeviceResources.cpp b/Kiwano/renderer/D3D11DeviceResources.cpp index 9b3c166b..555d6d16 100644 --- a/Kiwano/renderer/D3D11DeviceResources.cpp +++ b/Kiwano/renderer/D3D11DeviceResources.cpp @@ -29,7 +29,7 @@ namespace kiwano { -#if defined(_DEBUG) +#if defined(KGE_DEBUG) namespace DX { inline bool SdkLayersAvailable() @@ -109,6 +109,26 @@ namespace kiwano return hr; } + HRESULT D3D11DeviceResources::Present(bool vsync) + { + // The first argument instructs DXGI to block until VSync. + return dxgi_swap_chain_->Present(vsync ? 1 : 0, 0); + } + + HRESULT D3D11DeviceResources::ClearRenderTarget(Color& clear_color) + { + d3d_device_context_->OMSetRenderTargets( + 1, + &d3d_rt_view_, + d3d_ds_view_.Get() + ); + d3d_device_context_->ClearRenderTargetView( + d3d_rt_view_.Get(), + reinterpret_cast(&clear_color) + ); + return S_OK; + } + void D3D11DeviceResources::DiscardResources() { d3d_device_.Reset(); diff --git a/Kiwano/renderer/D3D11DeviceResources.h b/Kiwano/renderer/D3D11DeviceResources.h index 8d62f24d..3c9cba73 100644 --- a/Kiwano/renderer/D3D11DeviceResources.h +++ b/Kiwano/renderer/D3D11DeviceResources.h @@ -38,6 +38,14 @@ namespace kiwano HWND hwnd ); + HRESULT Present( + bool vsync + ); + + HRESULT ClearRenderTarget( + Color& clear_color + ); + HRESULT HandleDeviceLost(); HRESULT SetLogicalSize( diff --git a/Kiwano/renderer/render.cpp b/Kiwano/renderer/render.cpp index 56b7485c..abd7501e 100644 --- a/Kiwano/renderer/render.cpp +++ b/Kiwano/renderer/render.cpp @@ -32,7 +32,7 @@ namespace kiwano , text_antialias_(TextAntialias::ClearType) , clear_color_(Color::Black) , opacity_(1.f) - , collecting_data_(false) + , collecting_status_(false) { status_.primitives = 0; } @@ -128,7 +128,7 @@ namespace kiwano if (!device_context_) return E_UNEXPECTED; - if (collecting_data_) + if (collecting_status_) { status_.start = Time::Now(); status_.primitives = 0; @@ -151,20 +151,12 @@ namespace kiwano if (SUCCEEDED(hr)) { - // The first argument instructs DXGI to block until VSync. - hr = device_resources_->GetDXGISwapChain()->Present(vsync_ ? 1 : 0, 0); + hr = device_resources_->Present(vsync_); + } - auto main_rt_view = device_resources_->GetD3DRenderTargetView(); - device_resources_->GetD3DDeviceContext()->OMSetRenderTargets( - 1, - &main_rt_view, - device_resources_->GetD3DDepthStencilView() - ); - - device_resources_->GetD3DDeviceContext()->ClearRenderTargetView( - main_rt_view, - reinterpret_cast(&clear_color_) - ); + if (SUCCEEDED(hr)) + { + hr = device_resources_->ClearRenderTarget(clear_color_); } if (hr == DXGI_ERROR_DEVICE_REMOVED || hr == DXGI_ERROR_DEVICE_RESET) @@ -173,7 +165,7 @@ namespace kiwano hr = HandleDeviceLost(); } - if (collecting_data_) + if (collecting_status_) { status_.duration = Time::Now() - status_.start; } @@ -208,7 +200,7 @@ namespace kiwano device_resources_->GetStrokeStyle(stroke) ); - if (collecting_data_) + if (collecting_status_) ++status_.primitives; return S_OK; } @@ -243,7 +235,7 @@ namespace kiwano DX::ConvertToRectF(image->GetCropRect()) ); - if (collecting_data_) + if (collecting_status_) ++status_.primitives; return S_OK; } @@ -265,7 +257,7 @@ namespace kiwano DX::ConvertToRectF(src_rect) ); - if (collecting_data_) + if (collecting_status_) ++status_.primitives; return S_OK; } @@ -275,7 +267,7 @@ namespace kiwano if (!text_renderer_) return E_UNEXPECTED; - if (collecting_data_) + if (collecting_status_) ++status_.primitives; return text_layout->Draw(nullptr, text_renderer_.Get(), 0, 0); } @@ -347,14 +339,9 @@ namespace kiwano return S_OK; } - void Renderer::StartCollectData() + void Renderer::SetCollectingStatus(bool collecting) { - collecting_data_ = true; - } - - void Renderer::StopCollectData() - { - collecting_data_ = false; + collecting_status_ = collecting; } void Renderer::SetClearColor(const Color & color) diff --git a/Kiwano/renderer/render.h b/Kiwano/renderer/render.h index 47bb45d6..0ae2e5f2 100644 --- a/Kiwano/renderer/render.h +++ b/Kiwano/renderer/render.h @@ -141,9 +141,7 @@ namespace kiwano void DestroyComponent() override; - void StartCollectData(); - - void StopCollectData(); + void SetCollectingStatus(bool collecting); inline HWND GetTargetWindow() const { return hwnd_; } @@ -173,7 +171,7 @@ namespace kiwano float opacity_; bool antialias_; bool vsync_; - bool collecting_data_; + bool collecting_status_; Size output_size_; Color clear_color_;