Repository navigation
Expand file tree
/
Copy pathScreenCapture.cpp
More file actions
356 lines (299 loc) · 12.3 KB
/
Copy pathScreenCapture.cpp
File metadata and controls
356 lines (299 loc) · 12.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
#include "ScreenCapture.h"
#include <d3d11.h>
#include <d3d12.h>
#include <dxgi1_4.h>
#include <d3dx12.h>
#include <d3dcompiler.h>
#include <wrl/client.h>
#include <iostream>
using Microsoft::WRL::ComPtr;
// Global variables for D3D11 and D3D12
enum class GraphicsAPI { None, Direct3D11, Direct3D12 };
GraphicsAPI g_currentAPI = GraphicsAPI::None;
ComPtr<ID3D11Device> g_d3d11Device;
ComPtr<ID3D11DeviceContext> g_d3d11Context;
ComPtr<IDXGISwapChain> g_d3d11SwapChain;
ComPtr<ID3D12Device> g_d3d12Device;
ComPtr<ID3D12CommandQueue> g_d3d12CommandQueue;
ComPtr<IDXGISwapChain3> g_d3d12SwapChain;
// Detect and initialize the appropriate DirectX version
bool InitializeGraphicsAPI() {
// Try initializing Direct3D 12 first
HRESULT hr = D3D12CreateDevice(nullptr, D3D_FEATURE_LEVEL_11_0, IID_PPV_ARGS(&g_d3d12Device));
if (SUCCEEDED(hr)) {
std::cout << "Direct3D 12 initialized successfully." << std::endl;
g_currentAPI = GraphicsAPI::Direct3D12;
return true;
}
// If Direct3D 12 is not available, fallback to Direct3D 11
hr = D3D11CreateDevice(
nullptr,
D3D_DRIVER_TYPE_HARDWARE,
nullptr,
D3D11_CREATE_DEVICE_BGRA_SUPPORT,
nullptr,
0,
D3D11_SDK_VERSION,
&g_d3d11Device,
nullptr,
&g_d3d11Context
);
if (SUCCEEDED(hr)) {
std::cout << "Direct3D 11 initialized successfully." << std::endl;
g_currentAPI = GraphicsAPI::Direct3D11;
return true;
}
std::cerr << "Failed to initialize Direct3D 11 or 12." << std::endl;
g_currentAPI = GraphicsAPI::None;
return false;
}
bool GetScreenDataD3D11(float* buffer, int width, int height) {
if (!buffer || width <= 0 || height <= 0) {
std::cerr << "Invalid buffer or dimensions." << std::endl;
return false;
}
// Step 1: Initialize Direct3D 11
ComPtr<ID3D11Device> device;
ComPtr<ID3D11DeviceContext> context;
ComPtr<IDXGISwapChain> swapChain;
// Step 2: Get the back buffer
ComPtr<ID3D11Texture2D> backBuffer;
HRESULT hr = swapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), reinterpret_cast<void**>(backBuffer.GetAddressOf()));
if (FAILED(hr)) {
std::cerr << "Failed to get swap chain back buffer: " << std::hex << hr << std::endl;
return false;
}
// Step 3: Create a staging texture for CPU read access
D3D11_TEXTURE2D_DESC desc = {};
backBuffer->GetDesc(&desc);
desc.Usage = D3D11_USAGE_STAGING;
desc.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
desc.BindFlags = 0;
ComPtr<ID3D11Texture2D> stagingTexture;
hr = device->CreateTexture2D(&desc, nullptr, stagingTexture.GetAddressOf());
if (FAILED(hr)) {
std::cerr << "Failed to create staging texture: " << std::hex << hr << std::endl;
return false;
}
// Step 4: Copy the back buffer to the staging texture
context->CopyResource(stagingTexture.Get(), backBuffer.Get());
// Step 5: Map the staging texture for CPU read access
D3D11_MAPPED_SUBRESOURCE mappedResource = {};
hr = context->Map(stagingTexture.Get(), 0, D3D11_MAP_READ, 0, &mappedResource);
if (FAILED(hr)) {
std::cerr << "Failed to map staging texture: " << std::hex << hr << std::endl;
return false;
}
// Step 6: Convert pixel data and store it in the buffer
const BYTE* srcData = static_cast<const BYTE*>(mappedResource.pData);
for (int y = 0; y < height; ++y) {
for (int x = 0; x < width; ++x) {
int dstIdx = (y * width + x) * 3; // RGB channels
int srcIdx = y * mappedResource.RowPitch + x * 4; // RGBA format
buffer[dstIdx + 0] = srcData[srcIdx + 2] / 255.0f; // R
buffer[dstIdx + 1] = srcData[srcIdx + 1] / 255.0f; // G
buffer[dstIdx + 2] = srcData[srcIdx + 0] / 255.0f; // B
}
}
// Step 7: Unmap the resource
context->Unmap(stagingTexture.Get(), 0);
std::cout << "Captured screen successfully using Direct3D 11." << std::endl;
return true;
}
// Direct3D 11 implementation
bool InitializeD3D11(ID3D11Device** device, ID3D11DeviceContext** context, IDXGISwapChain** swapChain, int width, int height) {
DXGI_SWAP_CHAIN_DESC swapChainDesc = {};
swapChainDesc.BufferCount = 1;
swapChainDesc.BufferDesc.Width = width;
swapChainDesc.BufferDesc.Height = height;
swapChainDesc.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
swapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
swapChainDesc.OutputWindow = GetDesktopWindow();
swapChainDesc.SampleDesc.Count = 1;
swapChainDesc.Windowed = TRUE;
HRESULT hr = D3D11CreateDeviceAndSwapChain(
nullptr,
D3D_DRIVER_TYPE_HARDWARE,
nullptr,
D3D11_CREATE_DEVICE_BGRA_SUPPORT,
nullptr,
0,
D3D11_SDK_VERSION,
&swapChainDesc,
swapChain,
device,
nullptr,
context
);
if (FAILED(hr)) {
std::cerr << "Failed to initialize Direct3D 11: " << std::hex << hr << std::endl;
return false;
}
return true;
}
bool GetScreenData(float* buffer, int width, int height) {
if (!buffer || width <= 0 || height <= 0) {
std::cerr << "Invalid buffer or dimensions." << std::endl;
return false;
}
// Initialize Direct3D 11
ComPtr<ID3D11Device> device;
ComPtr<ID3D11DeviceContext> context;
ComPtr<IDXGISwapChain> swapChain;
if (!InitializeD3D11(device.GetAddressOf(), context.GetAddressOf(), swapChain.GetAddressOf(), width, height)) {
return false;
}
// Create a staging texture to read screen data
ComPtr<ID3D11Texture2D> backBuffer;
swapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), reinterpret_cast<void**>(backBuffer.GetAddressOf()));
D3D11_TEXTURE2D_DESC desc = {};
backBuffer->GetDesc(&desc);
desc.Usage = D3D11_USAGE_STAGING;
desc.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
desc.BindFlags = 0;
ComPtr<ID3D11Texture2D> stagingTexture;
HRESULT hr = device->CreateTexture2D(&desc, nullptr, stagingTexture.GetAddressOf());
if (FAILED(hr)) {
std::cerr << "Failed to create staging texture: " << std::hex << hr << std::endl;
return false;
}
// Copy the back buffer to the staging texture
context->CopyResource(stagingTexture.Get(), backBuffer.Get());
// Map the staging texture to read the pixel data
D3D11_MAPPED_SUBRESOURCE mappedResource = {};
hr = context->Map(stagingTexture.Get(), 0, D3D11_MAP_READ, 0, &mappedResource);
if (FAILED(hr)) {
std::cerr << "Failed to map staging texture: " << std::hex << hr << std::endl;
return false;
}
// Copy the pixel data to the buffer
const BYTE* srcData = static_cast<const BYTE*>(mappedResource.pData);
for (int y = 0; y < height; ++y) {
for (int x = 0; x < width; ++x) {
int dstIdx = (y * width + x) * 3;
int srcIdx = y * mappedResource.RowPitch + x * 4; // RGBA
buffer[dstIdx + 0] = srcData[srcIdx + 2] / 255.0f; // R
buffer[dstIdx + 1] = srcData[srcIdx + 1] / 255.0f; // G
buffer[dstIdx + 2] = srcData[srcIdx + 0] / 255.0f; // B
}
}
context->Unmap(stagingTexture.Get(), 0);
return true;
}
// Direct3D 12 implementation
bool InitializeD3D12(
ComPtr<ID3D12Device>& device,
ComPtr<ID3D12CommandQueue>& commandQueue,
ComPtr<IDXGISwapChain3>& swapChain,
ComPtr<ID3D12DescriptorHeap>& rtvHeap,
ComPtr<ID3D12Resource>& backBuffer,
int width, int height
) {
// Create a DXGI factory
ComPtr<IDXGIFactory4> factory;
HRESULT hr = CreateDXGIFactory1(IID_PPV_ARGS(&factory));
if (FAILED(hr)) {
std::cerr << "Failed to create DXGI factory: " << std::hex << hr << std::endl;
return false;
}
// Create a Direct3D 12 device
hr = D3D12CreateDevice(nullptr, D3D_FEATURE_LEVEL_11_0, IID_PPV_ARGS(&device));
if (FAILED(hr)) {
std::cerr << "Failed to create D3D12 device: " << std::hex << hr << std::endl;
return false;
}
// Create a command queue
D3D12_COMMAND_QUEUE_DESC commandQueueDesc = {};
commandQueueDesc.Type = D3D12_COMMAND_LIST_TYPE_DIRECT;
hr = device->CreateCommandQueue(&commandQueueDesc, IID_PPV_ARGS(&commandQueue));
if (FAILED(hr)) {
std::cerr << "Failed to create command queue: " << std::hex << hr << std::endl;
return false;
}
// Create a swap chain
DXGI_SWAP_CHAIN_DESC1 swapChainDesc = {};
swapChainDesc.Width = width;
swapChainDesc.Height = height;
swapChainDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
swapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
swapChainDesc.BufferCount = 2;
swapChainDesc.SampleDesc.Count = 1;
swapChainDesc.SwapEffect = DXGI_SWAP_EFFECT_FLIP_DISCARD;
ComPtr<IDXGISwapChain1> tempSwapChain;
hr = factory->CreateSwapChainForHwnd(
commandQueue.Get(), GetDesktopWindow(), &swapChainDesc, nullptr, nullptr, &tempSwapChain
);
if (FAILED(hr)) {
std::cerr << "Failed to create swap chain: " << std::hex << hr << std::endl;
return false;
}
hr = tempSwapChain.As(&swapChain);
if (FAILED(hr)) {
std::cerr << "Failed to cast swap chain: " << std::hex << hr << std::endl;
return false;
}
// Create RTV descriptor heap
D3D12_DESCRIPTOR_HEAP_DESC rtvHeapDesc = {};
rtvHeapDesc.NumDescriptors = 2;
rtvHeapDesc.Type = D3D12_DESCRIPTOR_HEAP_TYPE_RTV;
rtvHeapDesc.Flags = D3D12_DESCRIPTOR_HEAP_FLAG_NONE;
hr = device->CreateDescriptorHeap(&rtvHeapDesc, IID_PPV_ARGS(&rtvHeap));
if (FAILED(hr)) {
std::cerr << "Failed to create RTV descriptor heap: " << std::hex << hr << std::endl;
return false;
}
// Get the back buffer
hr = swapChain->GetBuffer(0, IID_PPV_ARGS(&backBuffer));
if (FAILED(hr)) {
std::cerr << "Failed to get back buffer: " << std::hex << hr << std::endl;
return false;
}
return true;
}
bool GetScreenDataD3D12(float* buffer, int width, int height) {
if (!buffer || width <= 0 || height <= 0) {
std::cerr << "Invalid buffer or dimensions." << std::endl;
return false;
}
// Initialize Direct3D 12
ComPtr<ID3D12Device> device;
ComPtr<ID3D12CommandQueue> commandQueue;
ComPtr<IDXGISwapChain3> swapChain;
ComPtr<ID3D12DescriptorHeap> rtvHeap;
ComPtr<ID3D12Resource> backBuffer;
if (!InitializeD3D12(device, commandQueue, swapChain, rtvHeap, backBuffer, width, height)) {
return false;
}
// Map the back buffer to retrieve pixel data
D3D12_PLACED_SUBRESOURCE_FOOTPRINT footprint = {};
D3D12_RESOURCE_DESC desc = backBuffer->GetDesc();
footprint.Footprint.Width = static_cast<UINT>(desc.Width);
footprint.Footprint.Height = static_cast<UINT>(desc.Height);
footprint.Footprint.Depth = 1;
footprint.Footprint.RowPitch = (desc.Width * 4 + 255) & ~255; // Align to 256 bytes
footprint.Footprint.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
ComPtr<ID3D12Resource> stagingBuffer;
D3D12_HEAP_PROPERTIES heapProps = {};
heapProps.Type = D3D12_HEAP_TYPE_READBACK;
D3D12_RESOURCE_DESC stagingDesc = {};
stagingDesc.Dimension = D3D12_RESOURCE_DIMENSION_BUFFER;
stagingDesc.Width = footprint.Footprint.RowPitch * desc.Height;
stagingDesc.Height = 1;
stagingDesc.DepthOrArraySize = 1;
stagingDesc.MipLevels = 1;
stagingDesc.Format = DXGI_FORMAT_UNKNOWN;
stagingDesc.SampleDesc.Count = 1;
stagingDesc.Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR;
HRESULT hr = device->CreateCommittedResource(
&heapProps, D3D12_HEAP_FLAG_NONE, &stagingDesc,
D3D12_RESOURCE_STATE_COPY_DEST, nullptr, IID_PPV_ARGS(&stagingBuffer)
);
if (FAILED(hr)) {
std::cerr << "Failed to create staging buffer: " << std::hex << hr << std::endl;
return false;
}
// Copy back buffer data to the staging buffer
// (Set up command lists, execute, and read back data)
// Convert pixel data to the buffer (logic similar to D3D11 version)
return true;
}