3
3
#include < DirectXMath.h>
4
4
#include < d3dcompiler.h>
5
5
6
+ #define HR_CHECK (exp ) \
7
+ if (!(exp)) { \
8
+ return false ; \
9
+ }
10
+
6
11
using namespace DirectX ;
7
12
using namespace Microsoft ::WRL;
8
13
@@ -56,36 +61,83 @@ float4 main(VS_OUTPUT input) : SV_Target
56
61
bool
57
62
DX11RenderBackend::CreateDeviceAndSwapchain ()
58
63
{
59
- // create device and swapchain
60
- DXGI_SWAP_CHAIN_DESC sd = {};
61
- sd.BufferCount = 1 ;
62
- sd.BufferDesc .Width = m_width;
63
- sd.BufferDesc .Height = m_height;
64
- sd.BufferDesc .Format = DXGI_FORMAT_B8G8R8A8_UNORM;
65
- sd.BufferDesc .RefreshRate .Numerator = 60 ;
66
- sd.BufferDesc .RefreshRate .Denominator = 1 ;
64
+ // we don't render directly to the window, but we
65
+ // use the DirectComposition to render the CefView
66
+
67
+ // Create D3D11 device and context
68
+ ComPtr<ID3D11Device> pD3dDevice;
69
+ ComPtr<ID3D11DeviceContext> pD3dContext;
70
+ HR_CHECK (S_OK == ::D3D11CreateDevice (nullptr ,
71
+ D3D_DRIVER_TYPE_HARDWARE,
72
+ nullptr ,
73
+ 0 ,
74
+ nullptr ,
75
+ 0 ,
76
+ D3D11_SDK_VERSION,
77
+ pD3dDevice.ReleaseAndGetAddressOf (),
78
+ nullptr ,
79
+ pD3dContext.ReleaseAndGetAddressOf ()));
80
+
81
+ // Get DXGI device
82
+ ComPtr<IDXGIDevice1> pDxgiDevice;
83
+ HR_CHECK (S_OK == pD3dDevice.As (&pDxgiDevice));
84
+
85
+ // Get DXGI adapter
86
+ ComPtr<IDXGIAdapter> pDxgiAdapter;
87
+ HR_CHECK (S_OK == pDxgiDevice->GetAdapter (pDxgiAdapter.GetAddressOf ()));
88
+
89
+ // Get DXGI factory
90
+ ComPtr<IDXGIFactory2> pDxgiFactory2;
91
+ HR_CHECK (S_OK == pDxgiAdapter->GetParent (IID_PPV_ARGS (pDxgiFactory2.ReleaseAndGetAddressOf ())));
92
+
93
+ // Create swapchain description
94
+ DXGI_SWAP_CHAIN_DESC1 sd;
95
+ ZeroMemory (&sd, sizeof (sd));
96
+ sd.BufferCount = 2 ;
97
+ sd.Width = (UINT)m_width;
98
+ sd.Height = (UINT)m_height;
99
+ sd.Format = DXGI_FORMAT_B8G8R8A8_UNORM;
67
100
sd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
68
- sd.OutputWindow = m_hWnd;
69
101
sd.SampleDesc .Count = 1 ;
70
102
sd.SampleDesc .Quality = 0 ;
71
- sd.Windowed = TRUE ;
72
-
73
- HRESULT hr = ::D3D11CreateDeviceAndSwapChain (nullptr ,
74
- D3D_DRIVER_TYPE_HARDWARE,
75
- nullptr ,
76
- 0 ,
77
- nullptr ,
78
- 0 ,
79
- D3D11_SDK_VERSION,
80
- &sd,
81
- m_swapChain.ReleaseAndGetAddressOf (),
82
- m_d3dDevice.ReleaseAndGetAddressOf (),
83
- nullptr ,
84
- m_d3dContext.ReleaseAndGetAddressOf ());
85
- if (FAILED (hr)) {
86
- return false ;
87
- }
88
-
103
+ sd.SwapEffect = DXGI_SWAP_EFFECT_FLIP_DISCARD;
104
+ sd.AlphaMode = DXGI_ALPHA_MODE_PREMULTIPLIED;
105
+ sd.Flags = DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH;
106
+
107
+ // Create swapchain
108
+ ComPtr<IDXGISwapChain1> pSwapChain;
109
+ HR_CHECK (S_OK == pDxgiFactory2->CreateSwapChainForComposition (
110
+ pD3dDevice.Get (), &sd, nullptr , pSwapChain.ReleaseAndGetAddressOf ()));
111
+
112
+ // Create DComposition device
113
+ ComPtr<IDCompositionDevice> pDecompositionDevice;
114
+ HR_CHECK (S_OK ==
115
+ ::DCompositionCreateDevice (pDxgiDevice.Get(), IID_PPV_ARGS(pDecompositionDevice.ReleaseAndGetAddressOf())));
116
+
117
+ // Create DComposition visual
118
+ ComPtr<IDCompositionVisual> pDCompositionVisual;
119
+ HR_CHECK (S_OK == pDecompositionDevice->CreateVisual (pDCompositionVisual.ReleaseAndGetAddressOf ()));
120
+
121
+ // Set swapchain to visual
122
+ HR_CHECK (S_OK == pDCompositionVisual->SetContent (pSwapChain.Get ()));
123
+
124
+ // Create DComposition target
125
+ ComPtr<IDCompositionTarget> pDCompositionTarget;
126
+ HR_CHECK (S_OK ==
127
+ pDecompositionDevice->CreateTargetForHwnd (m_hWnd, FALSE , pDCompositionTarget.ReleaseAndGetAddressOf ()));
128
+
129
+ // Set root to target
130
+ HR_CHECK (S_OK == pDCompositionTarget->SetRoot (pDCompositionVisual.Get ()));
131
+
132
+ // Commit
133
+ HR_CHECK (S_OK == pDecompositionDevice->Commit ());
134
+
135
+ // Save
136
+ m_dcompositionTarget = pDCompositionTarget;
137
+ m_dcompositionDevice = pDecompositionDevice;
138
+ m_swapChain = pSwapChain;
139
+ m_d3dContext = pD3dContext;
140
+ m_d3dDevice = pD3dDevice;
89
141
return true ;
90
142
}
91
143
@@ -123,8 +175,9 @@ DX11RenderBackend::CreateShaderResource()
123
175
{ " POSITION" , 0 , DXGI_FORMAT_R32G32B32_FLOAT, 0 , 0 , D3D11_INPUT_PER_VERTEX_DATA, 0 },
124
176
{ " TEXCOORD" , 0 , DXGI_FORMAT_R32G32_FLOAT, 0 , 12 , D3D11_INPUT_PER_VERTEX_DATA, 0 },
125
177
};
178
+ ComPtr<ID3D11InputLayout> pInputLayout;
126
179
hr = m_d3dDevice->CreateInputLayout (
127
- id, ARRAYSIZE (id), vsBlob->GetBufferPointer (), vsBlob->GetBufferSize (), m_inputLayout .ReleaseAndGetAddressOf ());
180
+ id, ARRAYSIZE (id), vsBlob->GetBufferPointer (), vsBlob->GetBufferSize (), pInputLayout .ReleaseAndGetAddressOf ());
128
181
if (FAILED (hr)) {
129
182
return false ;
130
183
}
@@ -146,12 +199,15 @@ DX11RenderBackend::CreateShaderResource()
146
199
return false ;
147
200
}
148
201
202
+ ComPtr<ID3D11PixelShader> pPixelShader;
149
203
hr = m_d3dDevice->CreatePixelShader (
150
- psBlob->GetBufferPointer (), psBlob->GetBufferSize (), nullptr , m_pixelShader .ReleaseAndGetAddressOf ());
204
+ psBlob->GetBufferPointer (), psBlob->GetBufferSize (), nullptr , pPixelShader .ReleaseAndGetAddressOf ());
151
205
if (FAILED (hr)) {
152
206
return false ;
153
207
}
154
208
209
+ m_inputLayout = pInputLayout;
210
+ m_pixelShader = pPixelShader;
155
211
return true ;
156
212
}
157
213
@@ -225,18 +281,26 @@ DX11RenderBackend::CreateRenderTarget()
225
281
vdesc.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2D;
226
282
vdesc.Texture2D .MipSlice = 0 ;
227
283
vdesc.Format = desc.BufferDesc .Format ;
228
- hr = m_d3dDevice->CreateRenderTargetView (pBackBuffer.Get (), &vdesc, m_renderTargetView.ReleaseAndGetAddressOf ());
284
+ ComPtr<ID3D11RenderTargetView> pRenderTargetView;
285
+ hr = m_d3dDevice->CreateRenderTargetView (pBackBuffer.Get (), nullptr , pRenderTargetView.ReleaseAndGetAddressOf ());
229
286
if (FAILED (hr)) {
230
287
return false ;
231
288
}
232
289
233
290
// create vertex buffer
234
- return CreateQuadVertexBuffer (0 .0f , //
235
- 0 .0f , //
236
- m_width, //
237
- m_height, //
238
- m_viewVertexBuffer.ReleaseAndGetAddressOf () //
239
- );
291
+ ComPtr<ID3D11Buffer> pViewVertexBuffer;
292
+ if (!CreateQuadVertexBuffer (0 .0f , //
293
+ 0 .0f , //
294
+ static_cast <float >(m_width), //
295
+ static_cast <float >(m_height), //
296
+ pViewVertexBuffer.ReleaseAndGetAddressOf () //
297
+ )) {
298
+ return false ;
299
+ }
300
+
301
+ m_renderTargetView = pRenderTargetView;
302
+ m_viewVertexBuffer = pViewVertexBuffer;
303
+ return true ;
240
304
}
241
305
242
306
void
@@ -393,6 +457,13 @@ DX11RenderBackend::UpdateTextureResource(Microsoft::WRL::ComPtr<ID3D11Texture2D>
393
457
targetTextureDesc = sharedTextureDesc;
394
458
}
395
459
460
+ void
461
+ DX11RenderBackend::SetTargetView ()
462
+ {
463
+ ID3D11RenderTargetView* rtvList[] = { m_renderTargetView.Get () };
464
+ m_d3dContext->OMSetRenderTargets (ARRAYSIZE (rtvList), rtvList, nullptr );
465
+ }
466
+
396
467
void
397
468
DX11RenderBackend::ClearTargetView ()
398
469
{
@@ -545,8 +616,8 @@ DX11RenderBackend::resize(int width, int height, float scale)
545
616
546
617
// update size
547
618
m_scale = scale;
548
- m_width = width * scale;
549
- m_height = height * scale;
619
+ m_width = static_cast < int >( width * scale) ;
620
+ m_height = static_cast < int >( height * scale) ;
550
621
551
622
// remove current render target
552
623
m_d3dContext->OMSetRenderTargets (0 , nullptr , nullptr );
@@ -589,10 +660,11 @@ void
589
660
DX11RenderBackend::updatePopupRect (const CefRect& rect)
590
661
{
591
662
CefRect newRect = rect;
592
- newRect.x *= m_scale;
593
- newRect.y *= m_scale;
594
- newRect.width *= m_scale;
595
- newRect.height *= m_scale;
663
+ newRect.x = static_cast <int >(newRect.x * m_scale);
664
+ newRect.y = static_cast <int >(newRect.y * m_scale);
665
+ newRect.width = static_cast <int >(newRect.width * m_scale);
666
+ newRect.height = static_cast <int >(newRect.height * m_scale);
667
+
596
668
if (newRect == m_popupRect) {
597
669
return ;
598
670
}
@@ -601,10 +673,10 @@ DX11RenderBackend::updatePopupRect(const CefRect& rect)
601
673
m_popupRect = newRect;
602
674
603
675
// create vertex buffer
604
- CreateQuadVertexBuffer (m_popupRect.x , //
605
- m_popupRect.y , //
606
- m_popupRect.width , //
607
- m_popupRect.height , //
676
+ CreateQuadVertexBuffer (static_cast < float >( m_popupRect.x ), //
677
+ static_cast < float >( m_popupRect.y ), //
678
+ static_cast < float >( m_popupRect.width ), //
679
+ static_cast < float >( m_popupRect.height ), //
608
680
m_popupVertexBuffer.ReleaseAndGetAddressOf () //
609
681
);
610
682
}
@@ -650,6 +722,8 @@ DX11RenderBackend::render(void* painter)
650
722
{
651
723
std::lock_guard<std::mutex> l (m_d3dContextLock);
652
724
725
+ SetTargetView ();
726
+
653
727
ClearTargetView ();
654
728
655
729
DrawCefView ();
0 commit comments