From e7fd80dc300e933bbc9eb9489da65fd1cb3f2d09 Mon Sep 17 00:00:00 2001 From: Chase Payne Date: Sat, 10 Aug 2024 14:00:47 -0500 Subject: [PATCH 01/10] Fix frame capture and output duplication for dual GPU setups and virtual displays - Added `test_frame_capture` function to verify if frames are successfully captured and not empty. - Fixes issues with virtual displays such as IDDSampleDriver when using more than one GPU. --- tools/ddprobe.cpp | 152 ++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 146 insertions(+), 6 deletions(-) diff --git a/tools/ddprobe.cpp b/tools/ddprobe.cpp index a30b2f41..e7040c5f 100644 --- a/tools/ddprobe.cpp +++ b/tools/ddprobe.cpp @@ -69,9 +69,132 @@ syncThreadDesktop() { CloseDesktop(hDesk); } + +/** + * @brief Determines whether a given frame is entirely black by sampling pixels. + * + * This function checks if the provided frame is predominantly black by sampling every 10th pixel in both the x and y dimensions. It inspects the RGB channels of each sampled pixel and compares them against a specified black threshold. If any sampled pixel's RGB values exceed this threshold, the frame is considered not black, and the function returns `false`. Otherwise, if all sampled pixels are below the threshold, the function returns `true`. + * + * @param mappedResource A reference to a `D3D11_MAPPED_SUBRESOURCE` structure that contains the mapped subresource data of the frame to be analyzed. + * @param frameDesc A reference to a `D3D11_TEXTURE2D_DESC` structure that describes the texture properties, including width and height. + * @param blackThreshold A floating-point value representing the threshold above which a pixel's RGB channels are considered non-black. The value ranges from 0.0f to 1.0f, with a default value of 0.1f. + * @return bool Returns `true` if the frame is determined to be black, otherwise returns `false`. + */ +bool +isFrameBlack(const D3D11_MAPPED_SUBRESOURCE &mappedResource, const D3D11_TEXTURE2D_DESC &frameDesc, float blackThreshold = 0.1f) { + const uint8_t *pixels = static_cast(mappedResource.pData); + const int bytesPerPixel = 4; // Assuming RGBA format + const int stride = mappedResource.RowPitch; + const int width = frameDesc.Width; + const int height = frameDesc.Height; + + // Sample every 10th pixel in both dimensions + const int sampleStep = 10; + const int threshold = static_cast(blackThreshold * 255); + + for (int y = 0; y < height; y += sampleStep) { + for (int x = 0; x < width; x += sampleStep) { + const uint8_t *pixel = pixels + y * stride + x * bytesPerPixel; + // Check if any channel (R, G, B) is significantly above black + if (pixel[0] > threshold || pixel[1] > threshold || pixel[2] > threshold) { + return false; + } + } + } + return true; +} + +/** + * @brief Attempts to capture and verify the contents of up to 10 consecutive frames from a DXGI output duplication. + * + * This function tries to acquire the next frame from a provided DXGI output duplication object (`dup`) and inspects its content to determine if the frame is not completely black. If a non-black frame is found within the 10 attempts, the function returns `S_OK`. If all 10 frames are black, it returns `S_FALSE`, indicating that the capture might not be functioning properly. In case of any failure during the process, the appropriate `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 pointer to the ID3D11Device interface that represents the device associated with the Direct3D context. + * @return HRESULT Returns `S_OK` if a non-black frame is captured successfully, `S_FALSE` if all frames are black, or an error code if a failure occurs during the process. + * + * Possible return values: + * - `S_OK`: A non-black frame was captured, indicating capture was successful. + * - `S_FALSE`: All 10 frames were black, indicating potential capture issues. + * - `E_FAIL`, `DXGI_ERROR_*`, or other DirectX HRESULT error codes in case of specific failures during frame capture, texture creation, or resource mapping. + */ +HRESULT +test_frame_capture(dxgi::dup_t &dup, ID3D11Device *device) { + for (int i = 0; i < 10; ++i) { + std::cout << "Attempting to acquire the next frame (" << i + 1 << "/10)..." << std::endl; + IDXGIResource *frameResource = nullptr; + DXGI_OUTDUPL_FRAME_INFO frameInfo; + HRESULT status = dup->AcquireNextFrame(500, &frameInfo, &frameResource); + if (FAILED(status)) { + std::cout << "Failed to acquire next frame [0x" << std::hex << status << "]" << std::endl; + return status; + } + + std::cout << "Frame acquired successfully." << std::endl; + + ID3D11Texture2D *frameTexture = nullptr; + status = frameResource->QueryInterface(__uuidof(ID3D11Texture2D), (void **) &frameTexture); + frameResource->Release(); + if (FAILED(status)) { + std::cout << "Failed to query texture interface from frame resource [0x" << std::hex << status << "]" << 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; + + ID3D11Texture2D *stagingTexture = nullptr; + status = device->CreateTexture2D(&frameDesc, nullptr, &stagingTexture); + if (FAILED(status)) { + std::cout << "Failed to create staging texture [0x" << std::hex << status << "]" << std::endl; + frameTexture->Release(); + return status; + } + + ID3D11DeviceContext *context = nullptr; + device->GetImmediateContext(&context); + context->CopyResource(stagingTexture, frameTexture); + frameTexture->Release(); + + D3D11_MAPPED_SUBRESOURCE mappedResource; + status = context->Map(stagingTexture, 0, D3D11_MAP_READ, 0, &mappedResource); + if (SUCCEEDED(status)) { + std::cout << "Verifying if frame is not empty" << std::endl; + + if (!isFrameBlack(mappedResource, frameDesc)) { + std::cout << "Frame " << i + 1 << " is not empty!" << std::endl; + context->Unmap(stagingTexture, 0); + stagingTexture->Release(); + context->Release(); + dup->ReleaseFrame(); + return S_OK; + } + context->Unmap(stagingTexture, 0); + } + else { + std::cout << "Failed to map the staging texture for inspection [0x" << std::hex << status << "]" << std::endl; + stagingTexture->Release(); + context->Release(); + return status; + } + + stagingTexture->Release(); + context->Release(); + std::cout << "Releasing the frame..." << std::endl; + dup->ReleaseFrame(); + } + + // If all frames are black, then we can assume the capture isn't working properly. + return S_FALSE; +} + HRESULT test_dxgi_duplication(dxgi::adapter_t &adapter, dxgi::output_t &output) { - D3D_FEATURE_LEVEL featureLevels[] { + D3D_FEATURE_LEVEL featureLevels[] = { D3D_FEATURE_LEVEL_11_1, D3D_FEATURE_LEVEL_11_0, D3D_FEATURE_LEVEL_10_1, @@ -82,7 +205,7 @@ test_dxgi_duplication(dxgi::adapter_t &adapter, dxgi::output_t &output) { }; dxgi::device_t device; - auto status = D3D11CreateDevice( + HRESULT status = D3D11CreateDevice( adapter.get(), D3D_DRIVER_TYPE_UNKNOWN, nullptr, @@ -92,24 +215,41 @@ test_dxgi_duplication(dxgi::adapter_t &adapter, dxgi::output_t &output) { &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; + std::cout << "Failed to create D3D11 device for DD test [0x" << std::hex << status << "]" << 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; + std::cout << "Failed to query IDXGIOutput1 from the output [0x" << std::hex << status << "]" << std::endl; return status; } // Ensure we can duplicate the current display syncThreadDesktop(); - // Return the result of DuplicateOutput() to Sunshine + // Attempt to duplicate the output dxgi::dup_t dup; - return output1->DuplicateOutput((IUnknown *) device.get(), &dup); + HRESULT result = output1->DuplicateOutput(static_cast(device.get()), &dup); + + if (SUCCEEDED(result)) { + // If duplication is successful, test frame capture + HRESULT captureResult = test_frame_capture(dup, device.get()); + if (SUCCEEDED(captureResult)) { + return S_OK; + } + else { + std::cout << "Frame capture test failed [0x" << std::hex << captureResult << "]" << std::endl; + return captureResult; + } + } + else { + std::cout << "Failed to duplicate output [0x" << std::hex << result << "]" << std::endl; + return result; + } } int From b1ec2dffe5e82d5fd5ffef4e69974b42afe1379a Mon Sep 17 00:00:00 2001 From: Chase Payne Date: Sat, 10 Aug 2024 14:08:59 -0500 Subject: [PATCH 02/10] Check all pixels instead It occoured to me there might be some desktop images that are predominately black, which could cause a false positive. --- tools/ddprobe.cpp | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/tools/ddprobe.cpp b/tools/ddprobe.cpp index e7040c5f..f49be924 100644 --- a/tools/ddprobe.cpp +++ b/tools/ddprobe.cpp @@ -71,9 +71,9 @@ syncThreadDesktop() { /** - * @brief Determines whether a given frame is entirely black by sampling pixels. + * @brief Determines whether a given frame is entirely black by checking every pixel. * - * This function checks if the provided frame is predominantly black by sampling every 10th pixel in both the x and y dimensions. It inspects the RGB channels of each sampled pixel and compares them against a specified black threshold. If any sampled pixel's RGB values exceed this threshold, the frame is considered not black, and the function returns `false`. Otherwise, if all sampled pixels are below the threshold, the function returns `true`. + * This function checks if the provided frame is entirely black by inspecting each pixel in both the x and y dimensions. It inspects the RGB channels of each pixel and compares them against a specified black threshold. If any pixel's RGB values exceed this threshold, the frame is considered not black, and the function returns `false`. Otherwise, if all pixels are below the threshold, the function returns `true`. * * @param mappedResource A reference to a `D3D11_MAPPED_SUBRESOURCE` structure that contains the mapped subresource data of the frame to be analyzed. * @param frameDesc A reference to a `D3D11_TEXTURE2D_DESC` structure that describes the texture properties, including width and height. @@ -88,12 +88,12 @@ isFrameBlack(const D3D11_MAPPED_SUBRESOURCE &mappedResource, const D3D11_TEXTURE const int width = frameDesc.Width; const int height = frameDesc.Height; - // Sample every 10th pixel in both dimensions - const int sampleStep = 10; + // Convert the threshold to an integer value for comparison const int threshold = static_cast(blackThreshold * 255); - for (int y = 0; y < height; y += sampleStep) { - for (int x = 0; x < width; x += sampleStep) { + // Loop through every pixel + 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 channel (R, G, B) is significantly above black if (pixel[0] > threshold || pixel[1] > threshold || pixel[2] > threshold) { @@ -104,6 +104,7 @@ isFrameBlack(const D3D11_MAPPED_SUBRESOURCE &mappedResource, const D3D11_TEXTURE return true; } + /** * @brief Attempts to capture and verify the contents of up to 10 consecutive frames from a DXGI output duplication. * From f2311bb74c01e58c6f180f1273454be2bef6906c Mon Sep 17 00:00:00 2001 From: Chase Payne Date: Sat, 10 Aug 2024 14:27:54 -0500 Subject: [PATCH 03/10] clang fixes --- tools/ddprobe.cpp | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/tools/ddprobe.cpp b/tools/ddprobe.cpp index f49be924..bfabb503 100644 --- a/tools/ddprobe.cpp +++ b/tools/ddprobe.cpp @@ -69,12 +69,11 @@ syncThreadDesktop() { CloseDesktop(hDesk); } - /** * @brief Determines whether a given frame is entirely black by checking every pixel. - * + * * This function checks if the provided frame is entirely black by inspecting each pixel in both the x and y dimensions. It inspects the RGB channels of each pixel and compares them against a specified black threshold. If any pixel's RGB values exceed this threshold, the frame is considered not black, and the function returns `false`. Otherwise, if all pixels are below the threshold, the function returns `true`. - * + * * @param mappedResource A reference to a `D3D11_MAPPED_SUBRESOURCE` structure that contains the mapped subresource data of the frame to be analyzed. * @param frameDesc A reference to a `D3D11_TEXTURE2D_DESC` structure that describes the texture properties, including width and height. * @param blackThreshold A floating-point value representing the threshold above which a pixel's RGB channels are considered non-black. The value ranges from 0.0f to 1.0f, with a default value of 0.1f. @@ -104,7 +103,6 @@ isFrameBlack(const D3D11_MAPPED_SUBRESOURCE &mappedResource, const D3D11_TEXTURE return true; } - /** * @brief Attempts to capture and verify the contents of up to 10 consecutive frames from a DXGI output duplication. * From eac116658ca0b42490f2eb7a08679fe100f8f278 Mon Sep 17 00:00:00 2001 From: Chase Payne Date: Sun, 11 Aug 2024 23:06:11 -0500 Subject: [PATCH 04/10] code review suggestions --- tools/ddprobe.cpp | 124 +++++++++++++++++++++++----------------------- 1 file changed, 61 insertions(+), 63 deletions(-) diff --git a/tools/ddprobe.cpp b/tools/ddprobe.cpp index bfabb503..f89c085a 100644 --- a/tools/ddprobe.cpp +++ b/tools/ddprobe.cpp @@ -9,10 +9,13 @@ #include #include #include +#include #include "src/utility.h" using namespace std::literals; +using Microsoft::WRL::ComPtr; + namespace dxgi { template void @@ -70,72 +73,80 @@ syncThreadDesktop() { } /** - * @brief Determines whether a given frame is entirely black by checking every pixel. + * @brief Checks whether a given frame is entirely dark by evaluating the RGB values of each pixel. * - * This function checks if the provided frame is entirely black by inspecting each pixel in both the x and y dimensions. It inspects the RGB channels of each pixel and compares them against a specified black threshold. If any pixel's RGB values exceed this threshold, the frame is considered not black, and the function returns `false`. Otherwise, if all pixels are below the threshold, the function returns `true`. + * This function determines if the provided frame is completely dark by analyzing the RGB values of every pixel. + * It iterates over all pixels in the frame and compares each pixel's RGB channels to a defined darkness threshold. + * If any pixel's RGB values exceed this threshold, the function concludes that the frame is not entirely dark and returns `false`. + * If all pixels are below the threshold, indicating a completely dark frame, the function returns `true`. * - * @param mappedResource A reference to a `D3D11_MAPPED_SUBRESOURCE` structure that contains the mapped subresource data of the frame to be analyzed. - * @param frameDesc A reference to a `D3D11_TEXTURE2D_DESC` structure that describes the texture properties, including width and height. - * @param blackThreshold A floating-point value representing the threshold above which a pixel's RGB channels are considered non-black. The value ranges from 0.0f to 1.0f, with a default value of 0.1f. - * @return bool Returns `true` if the frame is determined to be black, otherwise 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 non-dark. The value ranges from 0.0f to 1.0f, with a default value of 0.1f. + * @return bool Returns `true` if the frame is determined to be entirely dark, otherwise returns `false`. */ bool -isFrameBlack(const D3D11_MAPPED_SUBRESOURCE &mappedResource, const D3D11_TEXTURE2D_DESC &frameDesc, float blackThreshold = 0.1f) { +is_valid_frame(const D3D11_MAPPED_SUBRESOURCE &mappedResource, const D3D11_TEXTURE2D_DESC &frameDesc, float darknessThreshold = 0.1f) { const uint8_t *pixels = static_cast(mappedResource.pData); - const int bytesPerPixel = 4; // Assuming RGBA format + 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 threshold to an integer value for comparison - const int threshold = static_cast(blackThreshold * 255); + // Convert the darkness threshold to an integer value for comparison + const int threshold = static_cast(darknessThreshold * 255); - // Loop through every pixel + // 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 channel (R, G, B) is significantly above black + // Check if any RGB channel exceeds the darkness threshold if (pixel[0] > threshold || pixel[1] > threshold || pixel[2] > threshold) { - return false; + // Frame is not dark + return true; } } } - return true; + // Frame is entirely dark + return false; } /** - * @brief Attempts to capture and verify the contents of up to 10 consecutive frames from a DXGI output duplication. + * @brief Captures and verifies the contents of up to 10 consecutive frames from a DXGI output duplication. * - * This function tries to acquire the next frame from a provided DXGI output duplication object (`dup`) and inspects its content to determine if the frame is not completely black. If a non-black frame is found within the 10 attempts, the function returns `S_OK`. If all 10 frames are black, it returns `S_FALSE`, indicating that the capture might not be functioning properly. In case of any failure during the process, the appropriate `HRESULT` error code is returned. + * 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 `S_FALSE`, 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 pointer to the ID3D11Device interface that represents the device associated with the Direct3D context. - * @return HRESULT Returns `S_OK` if a non-black frame is captured successfully, `S_FALSE` if all frames are black, or an error code if a failure occurs during the process. - * - * Possible return values: - * - `S_OK`: A non-black frame was captured, indicating capture was successful. - * - `S_FALSE`: All 10 frames were black, indicating potential capture issues. - * - `E_FAIL`, `DXGI_ERROR_*`, or other DirectX HRESULT error codes in case of specific failures during frame capture, texture creation, or resource mapping. + * @param device A ComPtr to the ID3D11Device interface representing the device associated with the Direct3D context. + * @return HRESULT Returns `S_OK` if a non-empty frame is captured successfully, `S_FALSE` if all frames are empty, or an error code if any failure occurs during the process. */ HRESULT -test_frame_capture(dxgi::dup_t &dup, ID3D11Device *device) { +test_frame_capture(dxgi::dup_t &dup, ComPtr device) { for (int i = 0; i < 10; ++i) { - std::cout << "Attempting to acquire the next frame (" << i + 1 << "/10)..." << std::endl; - IDXGIResource *frameResource = nullptr; + std::cout << "Attempting to acquire frame " << (i + 1) << " of 10..." << std::endl; + + ComPtr frameResource; DXGI_OUTDUPL_FRAME_INFO frameInfo; HRESULT status = dup->AcquireNextFrame(500, &frameInfo, &frameResource); + auto release_frame = util::fail_guard([&dup]() { + dup->ReleaseFrame(); + }); + if (FAILED(status)) { - std::cout << "Failed to acquire next frame [0x" << std::hex << status << "]" << std::endl; + std::cout << "Error: Failed to acquire next frame [0x"sv << util::hex(status).to_string_view() << ']' << std::endl; return status; } std::cout << "Frame acquired successfully." << std::endl; - ID3D11Texture2D *frameTexture = nullptr; - status = frameResource->QueryInterface(__uuidof(ID3D11Texture2D), (void **) &frameTexture); - frameResource->Release(); + ComPtr frameTexture; + status = frameResource->QueryInterface(__uuidof(ID3D11Texture2D), reinterpret_cast(frameTexture.GetAddressOf())); if (FAILED(status)) { - std::cout << "Failed to query texture interface from frame resource [0x" << std::hex << status << "]" << std::endl; + std::cout << "Error: Failed to query texture interface from frame resource [0x"sv << util::hex(status).to_string_view() << ']' << std::endl; return status; } @@ -146,51 +157,37 @@ test_frame_capture(dxgi::dup_t &dup, ID3D11Device *device) { frameDesc.BindFlags = 0; frameDesc.MiscFlags = 0; - ID3D11Texture2D *stagingTexture = nullptr; + ComPtr stagingTexture; status = device->CreateTexture2D(&frameDesc, nullptr, &stagingTexture); if (FAILED(status)) { - std::cout << "Failed to create staging texture [0x" << std::hex << status << "]" << std::endl; - frameTexture->Release(); + std::cout << "Error: Failed to create staging texture [0x"sv << util::hex(status).to_string_view() << ']' << std::endl; return status; } - ID3D11DeviceContext *context = nullptr; + ComPtr context; device->GetImmediateContext(&context); - context->CopyResource(stagingTexture, frameTexture); - frameTexture->Release(); + context->CopyResource(stagingTexture.Get(), frameTexture.Get()); D3D11_MAPPED_SUBRESOURCE mappedResource; - status = context->Map(stagingTexture, 0, D3D11_MAP_READ, 0, &mappedResource); - if (SUCCEEDED(status)) { - std::cout << "Verifying if frame is not empty" << std::endl; - - if (!isFrameBlack(mappedResource, frameDesc)) { - std::cout << "Frame " << i + 1 << " is not empty!" << std::endl; - context->Unmap(stagingTexture, 0); - stagingTexture->Release(); - context->Release(); - dup->ReleaseFrame(); - return S_OK; - } - context->Unmap(stagingTexture, 0); - } - else { - std::cout << "Failed to map the staging texture for inspection [0x" << std::hex << status << "]" << std::endl; - stagingTexture->Release(); - context->Release(); + 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; } - stagingTexture->Release(); - context->Release(); - std::cout << "Releasing the frame..." << std::endl; - dup->ReleaseFrame(); + if (is_valid_frame(mappedResource, frameDesc)) { + std::cout << "Frame " << (i + 1) << " is non-empty (contains visible content)." << std::endl; + context->Unmap(stagingTexture.Get(), 0); + return S_OK; + } + + std::cout << "Frame " << (i + 1) << " is empty (no visible content)." << std::endl; + context->Unmap(stagingTexture.Get(), 0); } - // If all frames are black, then we can assume the capture isn't working properly. + // All frames were empty, indicating potential capture issues. return S_FALSE; } - HRESULT test_dxgi_duplication(dxgi::adapter_t &adapter, dxgi::output_t &output) { D3D_FEATURE_LEVEL featureLevels[] = { @@ -232,11 +229,12 @@ test_dxgi_duplication(dxgi::adapter_t &adapter, dxgi::output_t &output) { // Attempt to duplicate the output dxgi::dup_t dup; - HRESULT result = output1->DuplicateOutput(static_cast(device.get()), &dup); + ComPtr device_ptr(device.get()); + HRESULT result = output1->DuplicateOutput(device_ptr.Get(), &dup); if (SUCCEEDED(result)) { // If duplication is successful, test frame capture - HRESULT captureResult = test_frame_capture(dup, device.get()); + HRESULT captureResult = test_frame_capture(dup, device_ptr.Get()); if (SUCCEEDED(captureResult)) { return S_OK; } From 6d5091bd2954871b95456e07e900722966cf5fbb Mon Sep 17 00:00:00 2001 From: Chase Payne Date: Sun, 11 Aug 2024 23:09:54 -0500 Subject: [PATCH 05/10] attempt to match previous coding styles --- tools/ddprobe.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tools/ddprobe.cpp b/tools/ddprobe.cpp index f89c085a..ad8c5e78 100644 --- a/tools/ddprobe.cpp +++ b/tools/ddprobe.cpp @@ -213,14 +213,14 @@ test_dxgi_duplication(dxgi::adapter_t &adapter, dxgi::output_t &output) { nullptr); if (FAILED(status)) { - std::cout << "Failed to create D3D11 device for DD test [0x" << std::hex << status << "]" << std::endl; + 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 [0x" << std::hex << status << "]" << std::endl; + std::cout << "Failed to query IDXGIOutput1 from the output [0x"sv << util::hex(status).to_string_view() << "]" << std::endl; return status; } @@ -239,12 +239,12 @@ test_dxgi_duplication(dxgi::adapter_t &adapter, dxgi::output_t &output) { return S_OK; } else { - std::cout << "Frame capture test failed [0x" << std::hex << captureResult << "]" << std::endl; + std::cout << "Frame capture test failed [0x"sv << util::hex(captureResult).to_string_view() << "]" << std::endl; return captureResult; } } else { - std::cout << "Failed to duplicate output [0x" << std::hex << result << "]" << std::endl; + std::cout << "Failed to duplicate output [0x"sv << util::hex(result).to_string_view() << "]" << std::endl; return result; } } From 14de699b8d8ce83bcd37acfbe81bca0414f6ecca Mon Sep 17 00:00:00 2001 From: Chase Payne Date: Sun, 11 Aug 2024 23:12:16 -0500 Subject: [PATCH 06/10] remove else statements --- tools/ddprobe.cpp | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/tools/ddprobe.cpp b/tools/ddprobe.cpp index ad8c5e78..62c6cc7f 100644 --- a/tools/ddprobe.cpp +++ b/tools/ddprobe.cpp @@ -188,6 +188,7 @@ test_frame_capture(dxgi::dup_t &dup, ComPtr device) { // All frames were empty, indicating potential capture issues. return S_FALSE; } + HRESULT test_dxgi_duplication(dxgi::adapter_t &adapter, dxgi::output_t &output) { D3D_FEATURE_LEVEL featureLevels[] = { @@ -232,21 +233,19 @@ test_dxgi_duplication(dxgi::adapter_t &adapter, dxgi::output_t &output) { ComPtr device_ptr(device.get()); HRESULT result = output1->DuplicateOutput(device_ptr.Get(), &dup); - if (SUCCEEDED(result)) { - // If duplication is successful, test frame capture - HRESULT captureResult = test_frame_capture(dup, device_ptr.Get()); - if (SUCCEEDED(captureResult)) { - return S_OK; - } - else { - std::cout << "Frame capture test failed [0x"sv << util::hex(captureResult).to_string_view() << "]" << std::endl; - return captureResult; - } - } - else { + if (FAILED(result)) { std::cout << "Failed to duplicate output [0x"sv << util::hex(result).to_string_view() << "]" << std::endl; return result; } + + // If duplication is successful, test 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 From f5fa99cdb3366442760df99e95fd28bd07cc0438 Mon Sep 17 00:00:00 2001 From: Chase Payne Date: Sun, 11 Aug 2024 23:15:28 -0500 Subject: [PATCH 07/10] remove code changes that were not needed --- tools/ddprobe.cpp | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/tools/ddprobe.cpp b/tools/ddprobe.cpp index 62c6cc7f..8aa75b51 100644 --- a/tools/ddprobe.cpp +++ b/tools/ddprobe.cpp @@ -13,9 +13,8 @@ #include "src/utility.h" -using namespace std::literals; using Microsoft::WRL::ComPtr; - +using namespace std::literals; namespace dxgi { template void @@ -191,7 +190,7 @@ test_frame_capture(dxgi::dup_t &dup, ComPtr device) { HRESULT test_dxgi_duplication(dxgi::adapter_t &adapter, dxgi::output_t &output) { - D3D_FEATURE_LEVEL featureLevels[] = { + D3D_FEATURE_LEVEL featureLevels[] { D3D_FEATURE_LEVEL_11_1, D3D_FEATURE_LEVEL_11_0, D3D_FEATURE_LEVEL_10_1, @@ -202,7 +201,7 @@ test_dxgi_duplication(dxgi::adapter_t &adapter, dxgi::output_t &output) { }; dxgi::device_t device; - HRESULT status = D3D11CreateDevice( + auto status = D3D11CreateDevice( adapter.get(), D3D_DRIVER_TYPE_UNKNOWN, nullptr, @@ -212,7 +211,6 @@ test_dxgi_duplication(dxgi::adapter_t &adapter, dxgi::output_t &output) { &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; @@ -221,7 +219,7 @@ test_dxgi_duplication(dxgi::adapter_t &adapter, dxgi::output_t &output) { dxgi::output1_t output1; status = output->QueryInterface(IID_IDXGIOutput1, (void **) &output1); if (FAILED(status)) { - std::cout << "Failed to query IDXGIOutput1 from the output [0x"sv << util::hex(status).to_string_view() << "]" << std::endl; + std::cout << "Failed to query IDXGIOutput1 from the output"sv << std::endl; return status; } From 4fd9bf7873fba86ebe5ad019cf3deb246184f282 Mon Sep 17 00:00:00 2001 From: Chase Payne Date: Mon, 12 Aug 2024 03:03:12 -0500 Subject: [PATCH 08/10] move unmap to fail guard --- tools/ddprobe.cpp | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/tools/ddprobe.cpp b/tools/ddprobe.cpp index 8aa75b51..24ffb3d1 100644 --- a/tools/ddprobe.cpp +++ b/tools/ddprobe.cpp @@ -130,8 +130,16 @@ test_frame_capture(dxgi::dup_t &dup, ComPtr device) { ComPtr frameResource; DXGI_OUTDUPL_FRAME_INFO frameInfo; + ComPtr context; + ComPtr stagingTexture; + HRESULT status = dup->AcquireNextFrame(500, &frameInfo, &frameResource); - auto release_frame = util::fail_guard([&dup]() { + device->GetImmediateContext(&context); + + auto cleanup = util::fail_guard([&dup, &context, &stagingTexture]() { + if (stagingTexture) { + context->Unmap(stagingTexture.Get(), 0); + } dup->ReleaseFrame(); }); @@ -156,15 +164,12 @@ test_frame_capture(dxgi::dup_t &dup, ComPtr device) { frameDesc.BindFlags = 0; frameDesc.MiscFlags = 0; - ComPtr stagingTexture; 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; } - ComPtr context; - device->GetImmediateContext(&context); context->CopyResource(stagingTexture.Get(), frameTexture.Get()); D3D11_MAPPED_SUBRESOURCE mappedResource; @@ -176,12 +181,10 @@ test_frame_capture(dxgi::dup_t &dup, ComPtr device) { if (is_valid_frame(mappedResource, frameDesc)) { std::cout << "Frame " << (i + 1) << " is non-empty (contains visible content)." << std::endl; - context->Unmap(stagingTexture.Get(), 0); return S_OK; } std::cout << "Frame " << (i + 1) << " is empty (no visible content)." << std::endl; - context->Unmap(stagingTexture.Get(), 0); } // All frames were empty, indicating potential capture issues. From 24a4a89d2b88feebf6533938b097201cbe63a0c8 Mon Sep 17 00:00:00 2001 From: Chase Payne Date: Sat, 17 Aug 2024 01:36:44 -0500 Subject: [PATCH 09/10] implement cgutmans ideas --- src/platform/windows/display_base.cpp | 18 +++++++--- tools/ddprobe.cpp | 49 ++++++++++++++++++--------- 2 files changed, 47 insertions(+), 20 deletions(-) diff --git a/src/platform/windows/display_base.cpp b/src/platform/windows/display_base.cpp index 3d178daf..18f5cb8e 100644 --- a/src/platform/windows/display_base.cpp +++ b/src/platform/windows/display_base.cpp @@ -6,6 +6,7 @@ #include #include +#include #include // We have to include boost/process.hpp before display.h due to WinSock.h, @@ -363,8 +364,10 @@ namespace platf::dxgi { // 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) { - // If we've already been through here, there's nothing to do this time. static bool set_gpu_preference = false; + static bool verify_frame_capture = true; + + // If we've already been through here, there's nothing to do this time. if (set_gpu_preference) { return true; } @@ -378,17 +381,22 @@ namespace platf::dxgi { for (int i = 1; i < 5; i++) { // Run the probe tool. It returns the status of DuplicateOutput(). // - // Arg format: [GPU preference] [Display name] + // Arg format: [GPU preference] [Display name] [--verify--frame-capture] HRESULT result; + std::vector args = { std::to_string(i), display_name }; try { - result = bp::system(cmd, std::to_string(i), display_name, bp::std_out > bp::null, bp::std_err > bp::null); + if (verify_frame_capture) { + args.push_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 ["sv << i << "] ["sv << display_name << "] returned: 0x"sv << util::hex(result).to_string_view(); + 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 @@ -410,6 +418,8 @@ namespace platf::dxgi { } // If none of the manual options worked, leave the GPU preference alone + // And set the verify frame capture option to false, just in case there is a chance for a false negative. + verify_frame_capture = false; return false; } diff --git a/tools/ddprobe.cpp b/tools/ddprobe.cpp index 24ffb3d1..79c1c994 100644 --- a/tools/ddprobe.cpp +++ b/tools/ddprobe.cpp @@ -121,7 +121,7 @@ is_valid_frame(const D3D11_MAPPED_SUBRESOURCE &mappedResource, const D3D11_TEXTU * * @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 HRESULT Returns `S_OK` if a non-empty frame is captured successfully, `S_FALSE` if all frames are empty, or an error code if any failure occurs during the process. + * @return HRESULT 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 device) { @@ -188,11 +188,11 @@ test_frame_capture(dxgi::dup_t &dup, ComPtr device) { } // All frames were empty, indicating potential capture issues. - return S_FALSE; + return E_FAIL; } HRESULT -test_dxgi_duplication(dxgi::adapter_t &adapter, dxgi::output_t &output) { +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, @@ -239,13 +239,16 @@ test_dxgi_duplication(dxgi::adapter_t &adapter, dxgi::output_t &output) { return result; } - // If duplication is successful, test 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; + // 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; } @@ -253,20 +256,34 @@ int main(int argc, char *argv[]) { HRESULT status; - // Display name may be omitted - if (argc != 2 && argc != 3) { - std::cout << "ddprobe.exe [GPU preference value] [display name]"sv << std::endl; + // 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; - if (argc == 3) { - std::wstring_convert, wchar_t> converter; - display_name = converter.from_bytes(argv[2]); + 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, 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(atoi(argv[1])); + status = set_gpu_preference(gpu_preference); if (status != ERROR_SUCCESS) { return status; } @@ -310,7 +327,7 @@ main(int argc, char *argv[]) { } // We found the matching output. Test it and return the result. - return test_dxgi_duplication(adapter, output); + return test_dxgi_duplication(adapter, output, verify_frame_capture); } } From 5d88d4a01675f82f37edadda61437cb52fa59615 Mon Sep 17 00:00:00 2001 From: Chase Payne Date: Sat, 17 Aug 2024 01:40:57 -0500 Subject: [PATCH 10/10] clang fixes --- tools/ddprobe.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/tools/ddprobe.cpp b/tools/ddprobe.cpp index 79c1c994..49c2caa0 100644 --- a/tools/ddprobe.cpp +++ b/tools/ddprobe.cpp @@ -248,7 +248,6 @@ test_dxgi_duplication(dxgi::adapter_t &adapter, dxgi::output_t &output, bool ver } } - return S_OK; }