feat(capture/windows): hook APIs to avoid output reparenting that breaks DDA (#3530)
Some checks failed
CI / GitHub Env Debug (push) Waiting to run
CI / Setup Release (push) Waiting to run
CI / Setup Flatpak Matrix (push) Waiting to run
CI / Linux Flatpak (push) Blocked by required conditions
CI / Linux ${{ matrix.type }} (--appimage-build, 22.04, AppImage) (push) Blocked by required conditions
CI / Homebrew (${{ matrix.os_name }}-${{ matrix.os_version }}${{ matrix.release == true && ' (Release)' || '' }}) (macos, 13) (push) Blocked by required conditions
CI / Homebrew (${{ matrix.os_name }}-${{ matrix.os_version }}${{ matrix.release == true && ' (Release)' || '' }}) (macos, 14) (push) Blocked by required conditions
CI / Homebrew (${{ matrix.os_name }}-${{ matrix.os_version }}${{ matrix.release == true && ' (Release)' || '' }}) (ubuntu, latest) (push) Blocked by required conditions
CI / Homebrew (${{ matrix.os_name }}-${{ matrix.os_version }}${{ matrix.release == true && ' (Release)' || '' }}) (ubuntu, latest, true) (push) Blocked by required conditions
CI / Windows (push) Blocked by required conditions
CI Docker / Check Dockerfiles (push) Waiting to run
CI Docker / Setup Release (push) Blocked by required conditions
CI Docker / Docker${{ matrix.tag }} (push) Blocked by required conditions
CodeQL / Get language matrix (push) Waiting to run
CodeQL / Analyze (${{ matrix.name }}) (push) Blocked by required conditions
Build GH-Pages / prep (push) Waiting to run
Build GH-Pages / call-jekyll-build (push) Blocked by required conditions
localize / Update Localization (push) Has been cancelled

* Revert "feat(ddprobe): allow to manually specify gpu preference (#3521)"

This reverts commit 6a233cbcbfe1475d88bbedd03b848df205f2b268.

* Keep display revert delay input type change from 6a233cbcb

* Remove ddprobe

* feat(capture/windows): hook APIs to avoid output reparenting that breaks DDA
This commit is contained in:
Cameron Gutman 2025-01-12 17:11:03 -06:00 committed by GitHub
parent c369e8e5b6
commit 8392bdc98f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
14 changed files with 51 additions and 519 deletions

View File

@ -11,6 +11,7 @@ dependencies=(
"mingw-w64-ucrt-x86_64-cmake"
"mingw-w64-ucrt-x86_64-cppwinrt"
"mingw-w64-ucrt-x86_64-curl-winssl"
"mingw-w64-ucrt-x86_64-MinHook"
"mingw-w64-ucrt-x86_64-miniupnpc"
"mingw-w64-ucrt-x86_64-nlohmann-json"
"mingw-w64-ucrt-x86_64-nodejs"

View File

@ -885,6 +885,7 @@ jobs:
mingw-w64-ucrt-x86_64-cppwinrt
mingw-w64-ucrt-x86_64-curl-winssl
mingw-w64-ucrt-x86_64-graphviz
mingw-w64-ucrt-x86_64-MinHook
mingw-w64-ucrt-x86_64-miniupnpc
mingw-w64-ucrt-x86_64-nlohmann-json
mingw-w64-ucrt-x86_64-nodejs

View File

@ -28,7 +28,7 @@ include_directories(SYSTEM ${MINIUPNP_INCLUDE_DIRS})
# ffmpeg pre-compiled binaries
if(NOT DEFINED FFMPEG_PREPARED_BINARIES)
if(WIN32)
set(FFMPEG_PLATFORM_LIBRARIES mfplat ole32 strmiids mfuuid vpl)
set(FFMPEG_PLATFORM_LIBRARIES mfplat ole32 strmiids mfuuid vpl MinHook)
elseif(UNIX AND NOT APPLE)
set(FFMPEG_PLATFORM_LIBRARIES numa va va-drm va-x11 X11)
endif()

View File

@ -11,7 +11,6 @@ install(TARGETS dxgi-info RUNTIME DESTINATION "tools" COMPONENT dxgi)
install(TARGETS audio-info RUNTIME DESTINATION "tools" COMPONENT audio)
# Mandatory tools
install(TARGETS ddprobe RUNTIME DESTINATION "tools" COMPONENT application)
install(TARGETS sunshinesvc RUNTIME DESTINATION "tools" COMPONENT application)
# Mandatory scripts

View File

@ -90,6 +90,7 @@ dependencies=(
"mingw-w64-ucrt-x86_64-curl-winssl"
"mingw-w64-ucrt-x86_64-doxygen" # Optional, for docs... better to install official Doxygen
"mingw-w64-ucrt-x86_64-graphviz" # Optional, for docs
"mingw-w64-ucrt-x86_64-MinHook"
"mingw-w64-ucrt-x86_64-miniupnpc"
"mingw-w64-ucrt-x86_64-nlohmann-json"
"mingw-w64-ucrt-x86_64-nodejs"

View File

@ -848,37 +848,6 @@ editing the `conf` file in a text editor. Use the examples as reference.
</tr>
</table>
### gpu_preference
<table>
<tr>
<td>Description</td>
<td colspan="2">
Specify the GPU preference for the Sunshine process.
<br>
<br>
If set to negative number (-1 by default), Sunshine will try to detect the best GPU for the streamed display, but if it fails you will get a black screen.
<br>
Setting it to 0 will allow Windows to try and select the best GPU.
<br>
Setting it to 1 and above will prioritize the GPU that matches this number (the number has to be guessed, but it starts at 1 and increases).
@note{Applies to Windows only.}
</td>
</tr>
<tr>
<td>Default</td>
<td colspan="2">@code{}
-1
@endcode</td>
</tr>
<tr>
<td>Example</td>
<td colspan="2">@code{}
2
@endcode</td>
</tr>
</table>
### output_name
<table>

View File

@ -468,7 +468,6 @@ namespace config {
{}, // capture
{}, // encoder
{}, // adapter_name
-1, // gpu_preference
{}, // output_name
{
@ -1122,7 +1121,6 @@ namespace config {
string_f(vars, "capture", video.capture);
string_f(vars, "encoder", video.encoder);
string_f(vars, "adapter_name", video.adapter_name);
int_f(vars, "gpu_preference", video.gpu_preference);
string_f(vars, "output_name", video.output_name);
generic_f(vars, "dd_configuration_option", video.dd.configuration_option, dd::config_option_from_view);

View File

@ -77,7 +77,6 @@ namespace config {
std::string capture;
std::string encoder;
std::string adapter_name;
int gpu_preference;
std::string output_name;
struct dd_t {

View File

@ -9,10 +9,22 @@
#include <boost/algorithm/string/join.hpp>
#include <boost/process/v1.hpp>
#include <MinHook.h>
// We have to include boost/process/v1.hpp before display.h due to WinSock.h,
// but that prevents the definition of NTSTATUS so we must define it ourself.
typedef long NTSTATUS;
// Definition from the WDK's d3dkmthk.h
typedef enum _D3DKMT_GPU_PREFERENCE_QUERY_STATE: DWORD {
D3DKMT_GPU_PREFERENCE_STATE_UNINITIALIZED, ///< The GPU preference isn't initialized.
D3DKMT_GPU_PREFERENCE_STATE_HIGH_PERFORMANCE, ///< The highest performing GPU is preferred.
D3DKMT_GPU_PREFERENCE_STATE_MINIMUM_POWER, ///< The minimum-powered GPU is preferred.
D3DKMT_GPU_PREFERENCE_STATE_UNSPECIFIED, ///< A GPU preference isn't specified.
D3DKMT_GPU_PREFERENCE_STATE_NOT_FOUND, ///< A GPU preference isn't found.
D3DKMT_GPU_PREFERENCE_STATE_USER_SPECIFIED_GPU ///< A specific GPU is preferred.
} D3DKMT_GPU_PREFERENCE_QUERY_STATE;
#include "display.h"
#include "misc.h"
#include "src/config.h"
@ -329,115 +341,6 @@ namespace platf::dxgi {
return capture_e::ok;
}
bool
set_gpu_preference_on_self(int preference) {
// The GPU preferences key uses app path as the value name.
WCHAR sunshine_path[MAX_PATH];
GetModuleFileNameW(NULL, sunshine_path, ARRAYSIZE(sunshine_path));
WCHAR value_data[128];
swprintf_s(value_data, L"GpuPreference=%d;", preference);
auto status = RegSetKeyValueW(HKEY_CURRENT_USER,
L"Software\\Microsoft\\DirectX\\UserGpuPreferences",
sunshine_path,
REG_SZ,
value_data,
(wcslen(value_data) + 1) * sizeof(WCHAR));
if (status != ERROR_SUCCESS) {
BOOST_LOG(error) << "Failed to set GPU preference: "sv << status;
return false;
}
BOOST_LOG(info) << "Set GPU preference: "sv << preference;
return true;
}
bool
validate_and_test_gpu_preference(const std::string &display_name, bool verify_frame_capture) {
std::string cmd = "tools\\ddprobe.exe";
// We start at 1 because 0 is automatic selection which can be overridden by
// the GPU driver control panel options. Since ddprobe.exe can have different
// GPU driver overrides than Sunshine.exe, we want to avoid a scenario where
// autoselection might work for ddprobe.exe but not for us.
for (int i = 1; i < 5; i++) {
// Run the probe tool. It returns the status of DuplicateOutput().
//
// Arg format: [GPU preference] [Display name] [--verify-frame-capture]
HRESULT result;
std::vector<std::string> args = { std::to_string(i), display_name };
try {
if (verify_frame_capture) {
args.emplace_back("--verify-frame-capture");
}
result = bp::system(cmd, bp::args(args), bp::std_out > bp::null, bp::std_err > bp::null);
}
catch (bp::process_error &e) {
BOOST_LOG(error) << "Failed to start ddprobe.exe: "sv << e.what();
return false;
}
BOOST_LOG(info) << "ddprobe.exe " << boost::algorithm::join(args, " ") << " returned 0x"
<< util::hex(result).to_string_view();
// E_ACCESSDENIED can happen at the login screen. If we get this error,
// we know capture would have been supported, because DXGI_ERROR_UNSUPPORTED
// would have been raised first if it wasn't.
if (result == S_OK || result == E_ACCESSDENIED) {
// We found a working GPU preference, so set ourselves to use that.
return set_gpu_preference_on_self(i);
}
}
// If no valid configuration was found, return false
return false;
}
// On hybrid graphics systems, Windows will change the order of GPUs reported by
// DXGI in accordance with the user's GPU preference. If the selected GPU is a
// render-only device with no displays, DXGI will add virtual outputs to the
// that device to avoid confusing applications. While this works properly for most
// applications, it breaks the Desktop Duplication API because DXGI doesn't proxy
// the virtual DXGIOutput to the real GPU it is attached to. When trying to call
// DuplicateOutput() on one of these virtual outputs, it fails with DXGI_ERROR_UNSUPPORTED
// (even if you try sneaky stuff like passing the ID3D11Device for the iGPU and the
// virtual DXGIOutput from the dGPU). Because the GPU preference is once-per-process,
// we spawn a helper tool to probe for us before we set our own GPU preference.
bool
probe_for_gpu_preference(const std::string &display_name) {
static bool set_gpu_preference = false;
// If we've already been through here, there's nothing to do this time.
if (set_gpu_preference) {
return true;
}
// If the GPU preference was manually specified, we can skip the probe.
if (config::video.gpu_preference >= 0) {
if (set_gpu_preference_on_self(config::video.gpu_preference)) {
set_gpu_preference = true;
return true;
}
}
else {
// Try probing with different GPU preferences and verify_frame_capture flag
if (validate_and_test_gpu_preference(display_name, true)) {
set_gpu_preference = true;
return true;
}
// If no valid configuration was found, try again with verify_frame_capture == false
if (validate_and_test_gpu_preference(display_name, false)) {
set_gpu_preference = true;
return true;
}
}
// If neither worked, return false
return false;
}
/**
* @brief Tests to determine if the Desktop Duplication API can capture the given output.
* @details When testing for enumeration only, we avoid resyncing the thread desktop.
@ -510,6 +413,27 @@ namespace platf::dxgi {
return false;
}
/**
* @brief Hook for NtGdiDdDDIGetCachedHybridQueryValue() from win32u.dll.
* @param gpuPreference A pointer to the location where the preference will be written.
* @return Always STATUS_SUCCESS if valid arguments are provided.
*/
NTSTATUS
__stdcall NtGdiDdDDIGetCachedHybridQueryValueHook(D3DKMT_GPU_PREFERENCE_QUERY_STATE *gpuPreference) {
// By faking a cached GPU preference state of D3DKMT_GPU_PREFERENCE_STATE_UNSPECIFIED, this will
// prevent DXGI from performing the normal GPU preference resolution that looks at the registry,
// power settings, and the hybrid adapter DDI interface to pick a GPU. Instead, we will not be
// bound to any specific GPU. This will prevent DXGI from performing output reparenting (moving
// outputs from their true location to the render GPU), which breaks DDA.
if (gpuPreference) {
*gpuPreference = D3DKMT_GPU_PREFERENCE_STATE_UNSPECIFIED;
return 0; // STATUS_SUCCESS
}
else {
return STATUS_INVALID_PARAMETER;
}
}
int
display_base_t::init(const ::video::config_t &config, const std::string &display_name) {
std::once_flag windows_cpp_once_flag;
@ -519,13 +443,22 @@ namespace platf::dxgi {
typedef BOOL (*User32_SetProcessDpiAwarenessContext)(DPI_AWARENESS_CONTEXT value);
auto user32 = LoadLibraryA("user32.dll");
auto f = (User32_SetProcessDpiAwarenessContext) GetProcAddress(user32, "SetProcessDpiAwarenessContext");
if (f) {
f(DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2);
{
auto user32 = LoadLibraryA("user32.dll");
auto f = (User32_SetProcessDpiAwarenessContext) GetProcAddress(user32, "SetProcessDpiAwarenessContext");
if (f) {
f(DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2);
}
FreeLibrary(user32);
}
FreeLibrary(user32);
{
// We aren't calling MH_Uninitialize(), but that's okay because this hook lasts for the life of the process
MH_Initialize();
MH_CreateHookApi(L"win32u.dll", "NtGdiDdDDIGetCachedHybridQueryValue", (void *) NtGdiDdDDIGetCachedHybridQueryValueHook, nullptr);
MH_EnableHook(MH_ALL_HOOKS);
}
});
// Get rectangle of full desktop for absolute mouse coordinates
@ -534,11 +467,6 @@ namespace platf::dxgi {
HRESULT status;
// We must set the GPU preference before calling any DXGI APIs!
if (!probe_for_gpu_preference(display_name)) {
BOOST_LOG(warning) << "Failed to set GPU preference. Capture may not work!"sv;
}
status = CreateDXGIFactory1(IID_IDXGIFactory1, (void **) &factory);
if (FAILED(status)) {
BOOST_LOG(error) << "Failed to create DXGIFactory1 [0x"sv << util::hex(status).to_string_view() << ']';
@ -1105,12 +1033,6 @@ namespace platf {
BOOST_LOG(debug) << "Detecting monitors..."sv;
// We must set the GPU preference before calling any DXGI APIs!
const auto output_name { display_device::map_output_name(config::video.output_name) };
if (!dxgi::probe_for_gpu_preference(output_name)) {
BOOST_LOG(warning) << "Failed to set GPU preference. Capture may not work!"sv;
}
// We sync the thread desktop once before we start the enumeration process
// to ensure test_dxgi_duplication() returns consistent results for all GPUs
// even if the current desktop changes during our enumeration process.

View File

@ -167,7 +167,6 @@
"virtual_sink": "",
"install_steam_audio_drivers": "enabled",
"adapter_name": "",
"gpu_preference": -1,
"output_name": "",
"dd_configuration_option": "verify_only",
"dd_resolution_option": "auto",

View File

@ -68,18 +68,6 @@ const config = ref(props.config)
:config="config"
/>
<PlatformLayout :platform="platform">
<template #windows>
<!-- GPU Preference -->
<div class="mb-3">
<label for="gpu_preference" class="form-label">{{ $t('config.gpu_preference') }}</label>
<input type="number" class="form-control" id="gpu_preference" placeholder="-1" min="-1"
v-model="config.gpu_preference" />
<div class="form-text">{{ $t('config.gpu_preference_desc') }}</div>
</div>
</template>
</PlatformLayout>
<DisplayOutputSelector
:platform="platform"
:config="config"

View File

@ -199,8 +199,6 @@
"fec_percentage": "FEC Percentage",
"fec_percentage_desc": "Percentage of error correcting packets per data packet in each video frame. Higher values can correct for more network packet loss, but at the cost of increasing bandwidth usage.",
"ffmpeg_auto": "auto -- let ffmpeg decide (default)",
"gpu_preference": "GPU Preference",
"gpu_preference_desc": "Specify the GPU preference for the Sunshine process. If set to negative number (-1 by default), Sunshine will try to detect the best GPU for the streamed display, but if it fails you will get a black screen. Setting it to 0 will allow Windows to try and select the best GPU. Setting it to 1 and above will prioritize the GPU that matches this number (the number has to be guessed, but it starts at 1 and increases).",
"file_apps": "Apps File",
"file_apps_desc": "The file where current apps of Sunshine are stored.",
"file_state": "State File",

View File

@ -27,12 +27,3 @@ target_link_libraries(sunshinesvc
wtsapi32
${PLATFORM_LIBRARIES})
target_compile_options(sunshinesvc PRIVATE ${SUNSHINE_COMPILE_OPTIONS})
add_executable(ddprobe ddprobe.cpp)
set_target_properties(ddprobe PROPERTIES CXX_STANDARD 20)
target_link_libraries(ddprobe
${CMAKE_THREAD_LIBS_INIT}
dxgi
d3d11
${PLATFORM_LIBRARIES})
target_compile_options(ddprobe PRIVATE ${SUNSHINE_COMPILE_OPTIONS})

View File

@ -1,334 +0,0 @@
/**
* @file tools/ddprobe.cpp
* @brief Handles probing for DXGI duplication support.
*/
#include <d3d11.h>
#include <dxgi1_2.h>
#include <codecvt>
#include <iostream>
#include <locale>
#include <string>
#include <wrl.h>
#include "src/utility.h"
using Microsoft::WRL::ComPtr;
using namespace std::literals;
namespace dxgi {
template <class T>
void
Release(T *dxgi) {
dxgi->Release();
}
using factory1_t = util::safe_ptr<IDXGIFactory1, Release<IDXGIFactory1>>;
using adapter_t = util::safe_ptr<IDXGIAdapter1, Release<IDXGIAdapter1>>;
using output_t = util::safe_ptr<IDXGIOutput, Release<IDXGIOutput>>;
using output1_t = util::safe_ptr<IDXGIOutput1, Release<IDXGIOutput1>>;
using device_t = util::safe_ptr<ID3D11Device, Release<ID3D11Device>>;
using dup_t = util::safe_ptr<IDXGIOutputDuplication, Release<IDXGIOutputDuplication>>;
} // namespace dxgi
LSTATUS
set_gpu_preference(int preference) {
// The GPU preferences key uses app path as the value name.
WCHAR executable_path[MAX_PATH];
GetModuleFileNameW(NULL, executable_path, ARRAYSIZE(executable_path));
WCHAR value_data[128];
swprintf_s(value_data, L"GpuPreference=%d;", preference);
auto status = RegSetKeyValueW(HKEY_CURRENT_USER,
L"Software\\Microsoft\\DirectX\\UserGpuPreferences",
executable_path,
REG_SZ,
value_data,
(wcslen(value_data) + 1) * sizeof(WCHAR));
if (status != ERROR_SUCCESS) {
std::cout << "Failed to set GPU preference: "sv << status << std::endl;
return status;
}
return ERROR_SUCCESS;
}
void
syncThreadDesktop() {
auto hDesk = OpenInputDesktop(DF_ALLOWOTHERACCOUNTHOOK, FALSE, GENERIC_ALL);
if (!hDesk) {
auto err = GetLastError();
std::cout << "Failed to Open Input Desktop [0x"sv << util::hex(err).to_string_view() << ']' << std::endl;
return;
}
if (!SetThreadDesktop(hDesk)) {
auto err = GetLastError();
std::cout << "Failed to sync desktop to thread [0x"sv << util::hex(err).to_string_view() << ']' << std::endl;
}
CloseDesktop(hDesk);
}
/**
* @brief Determines if a given frame is valid by checking if it contains any non-dark pixels.
*
* This function analyzes the provided frame to determine if it contains any pixels that exceed a specified darkness threshold.
* It iterates over all pixels in the frame, comparing each pixel's RGB values to the defined darkness threshold.
* If any pixel's RGB values exceed this threshold, the function concludes that the frame is valid (i.e., not entirely dark) and returns `true`.
* If all pixels are below or equal to the threshold, indicating a completely dark frame, the function returns `false`.
* @param mappedResource A reference to a `D3D11_MAPPED_SUBRESOURCE` structure containing the mapped subresource data of the frame to be analyzed.
* @param frameDesc A reference to a `D3D11_TEXTURE2D_DESC` structure describing the texture properties, including width and height.
* @param darknessThreshold A floating-point value representing the threshold above which a pixel's RGB values are considered dark. The value ranges from 0.0f to 1.0f, with a default value of 0.1f.
* @return Returns `true` if the frame contains any non-dark pixels, indicating it is valid; otherwise, returns `false`.
*/
bool
is_valid_frame(const D3D11_MAPPED_SUBRESOURCE &mappedResource, const D3D11_TEXTURE2D_DESC &frameDesc, float darknessThreshold = 0.1f) {
const auto *pixels = static_cast<const uint8_t *>(mappedResource.pData);
const int bytesPerPixel = 4; // (8 bits per channel, excluding alpha). Factoring HDR is not needed because it doesn't cause black levels to raise enough to be a concern.
const int stride = mappedResource.RowPitch;
const int width = frameDesc.Width;
const int height = frameDesc.Height;
// Convert the darkness threshold to an integer value for comparison
const auto threshold = static_cast<int>(darknessThreshold * 255);
// Iterate over each pixel in the frame
for (int y = 0; y < height; ++y) {
for (int x = 0; x < width; ++x) {
const uint8_t *pixel = pixels + y * stride + x * bytesPerPixel;
// Check if any RGB channel exceeds the darkness threshold
if (pixel[0] > threshold || pixel[1] > threshold || pixel[2] > threshold) {
// Frame is not dark
return true;
}
}
}
// Frame is entirely dark
return false;
}
/**
* @brief Captures and verifies the contents of up to 10 consecutive frames from a DXGI output duplication.
*
* This function attempts to acquire and analyze up to 10 frames from a DXGI output duplication object (`dup`).
* It checks if each frame is non-empty (not entirely dark) by using the `is_valid_frame` function.
* If any non-empty frame is found, the function returns `S_OK`.
* If all 10 frames are empty, it returns `E_FAIL`, suggesting potential issues with the capture process.
* If any error occurs during the frame acquisition or analysis process, the corresponding `HRESULT` error code is returned.
*
* @param dup A reference to the DXGI output duplication object (`dxgi::dup_t&`) used to acquire frames.
* @param device A ComPtr to the ID3D11Device interface representing the device associated with the Direct3D context.
* @return Returns `S_OK` if a non-empty frame is captured successfully, `E_FAIL` if all frames are empty, or an error code if any failure occurs during the process.
*/
HRESULT
test_frame_capture(dxgi::dup_t &dup, ComPtr<ID3D11Device> device) {
for (int i = 0; i < 10; ++i) {
std::cout << "Attempting to acquire frame " << (i + 1) << " of 10..." << std::endl;
ComPtr<IDXGIResource> frameResource;
DXGI_OUTDUPL_FRAME_INFO frameInfo;
ComPtr<ID3D11DeviceContext> context;
ComPtr<ID3D11Texture2D> stagingTexture;
HRESULT status = dup->AcquireNextFrame(500, &frameInfo, &frameResource);
device->GetImmediateContext(&context);
if (FAILED(status)) {
std::cout << "Error: Failed to acquire next frame [0x"sv << util::hex(status).to_string_view() << ']' << std::endl;
return status;
}
auto cleanup = util::fail_guard([&dup]() {
dup->ReleaseFrame();
});
std::cout << "Frame acquired successfully." << std::endl;
ComPtr<ID3D11Texture2D> frameTexture;
status = frameResource->QueryInterface(IID_PPV_ARGS(&frameTexture));
if (FAILED(status)) {
std::cout << "Error: Failed to query texture interface from frame resource [0x"sv << util::hex(status).to_string_view() << ']' << std::endl;
return status;
}
D3D11_TEXTURE2D_DESC frameDesc;
frameTexture->GetDesc(&frameDesc);
frameDesc.Usage = D3D11_USAGE_STAGING;
frameDesc.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
frameDesc.BindFlags = 0;
frameDesc.MiscFlags = 0;
status = device->CreateTexture2D(&frameDesc, nullptr, &stagingTexture);
if (FAILED(status)) {
std::cout << "Error: Failed to create staging texture [0x"sv << util::hex(status).to_string_view() << ']' << std::endl;
return status;
}
context->CopyResource(stagingTexture.Get(), frameTexture.Get());
D3D11_MAPPED_SUBRESOURCE mappedResource;
status = context->Map(stagingTexture.Get(), 0, D3D11_MAP_READ, 0, &mappedResource);
if (FAILED(status)) {
std::cout << "Error: Failed to map the staging texture for inspection [0x"sv << util::hex(status).to_string_view() << ']' << std::endl;
return status;
}
auto contextCleanup = util::fail_guard([&context, &stagingTexture]() {
context->Unmap(stagingTexture.Get(), 0);
});
if (is_valid_frame(mappedResource, frameDesc)) {
std::cout << "Frame " << (i + 1) << " is non-empty (contains visible content)." << std::endl;
return S_OK;
}
std::cout << "Frame " << (i + 1) << " is empty (no visible content)." << std::endl;
}
// All frames were empty, indicating potential capture issues.
return E_FAIL;
}
HRESULT
test_dxgi_duplication(dxgi::adapter_t &adapter, dxgi::output_t &output, bool verify_frame_capture) {
D3D_FEATURE_LEVEL featureLevels[] {
D3D_FEATURE_LEVEL_11_1,
D3D_FEATURE_LEVEL_11_0,
D3D_FEATURE_LEVEL_10_1,
D3D_FEATURE_LEVEL_10_0,
D3D_FEATURE_LEVEL_9_3,
D3D_FEATURE_LEVEL_9_2,
D3D_FEATURE_LEVEL_9_1
};
dxgi::device_t device;
auto status = D3D11CreateDevice(
adapter.get(),
D3D_DRIVER_TYPE_UNKNOWN,
nullptr,
D3D11_CREATE_DEVICE_VIDEO_SUPPORT,
featureLevels, sizeof(featureLevels) / sizeof(D3D_FEATURE_LEVEL),
D3D11_SDK_VERSION,
&device,
nullptr,
nullptr);
if (FAILED(status)) {
std::cout << "Failed to create D3D11 device for DD test [0x"sv << util::hex(status).to_string_view() << ']' << std::endl;
return status;
}
dxgi::output1_t output1;
status = output->QueryInterface(IID_IDXGIOutput1, (void **) &output1);
if (FAILED(status)) {
std::cout << "Failed to query IDXGIOutput1 from the output"sv << std::endl;
return status;
}
// Ensure we can duplicate the current display
syncThreadDesktop();
// Attempt to duplicate the output
dxgi::dup_t dup;
ComPtr<ID3D11Device> device_ptr(device.get());
HRESULT result = output1->DuplicateOutput(device_ptr.Get(), &dup);
if (FAILED(result)) {
std::cout << "Failed to duplicate output [0x"sv << util::hex(result).to_string_view() << "]" << std::endl;
return result;
}
// To prevent false negatives, we'll make it optional to test for frame capture.
if (verify_frame_capture) {
HRESULT captureResult = test_frame_capture(dup, device_ptr.Get());
if (FAILED(captureResult)) {
std::cout << "Frame capture test failed [0x"sv << util::hex(captureResult).to_string_view() << "]" << std::endl;
return captureResult;
}
}
return S_OK;
}
int
main(int argc, char *argv[]) {
HRESULT status;
// Usage message
if (argc < 2 || argc > 4) {
std::cout << "Usage: ddprobe.exe [GPU preference value] [display name] [--verify-frame-capture]"sv << std::endl;
return -1;
}
std::wstring display_name;
bool verify_frame_capture = false;
// Parse GPU preference value (required)
int gpu_preference = atoi(argv[1]);
// Parse optional arguments
for (int i = 2; i < argc; ++i) {
std::string arg = argv[i];
if (arg == "--verify-frame-capture") {
verify_frame_capture = true;
}
else {
// Assume any other argument is the display name
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>, wchar_t> converter;
display_name = converter.from_bytes(arg);
}
}
// We must set the GPU preference before making any DXGI/D3D calls
status = set_gpu_preference(gpu_preference);
if (status != ERROR_SUCCESS) {
return status;
}
// Remove the GPU preference when we're done
auto reset_gpu = util::fail_guard([]() {
WCHAR tool_path[MAX_PATH];
GetModuleFileNameW(NULL, tool_path, ARRAYSIZE(tool_path));
RegDeleteKeyValueW(HKEY_CURRENT_USER,
L"Software\\Microsoft\\DirectX\\UserGpuPreferences",
tool_path);
});
dxgi::factory1_t factory;
status = CreateDXGIFactory1(IID_IDXGIFactory1, (void **) &factory);
if (FAILED(status)) {
std::cout << "Failed to create DXGIFactory1 [0x"sv << util::hex(status).to_string_view() << ']' << std::endl;
return status;
}
dxgi::adapter_t::pointer adapter_p {};
for (int x = 0; factory->EnumAdapters1(x, &adapter_p) != DXGI_ERROR_NOT_FOUND; ++x) {
dxgi::adapter_t adapter { adapter_p };
dxgi::output_t::pointer output_p {};
for (int y = 0; adapter->EnumOutputs(y, &output_p) != DXGI_ERROR_NOT_FOUND; ++y) {
dxgi::output_t output { output_p };
DXGI_OUTPUT_DESC desc;
output->GetDesc(&desc);
// If a display name was specified and this one doesn't match, skip it
if (!display_name.empty() && desc.DeviceName != display_name) {
continue;
}
// If this display is not part of the desktop, we definitely can't capture it
if (!desc.AttachedToDesktop) {
continue;
}
// We found the matching output. Test it and return the result.
return test_dxgi_duplication(adapter, output, verify_frame_capture);
}
}
return 0;
}