DXGI fails to scan 60 fps pixels

本小妞迷上赌 提交于 2020-12-15 05:26:34

问题


#include "D3D9.h"
#include <Wincodec.h>
#include <chrono>

#include <shellapi.h>
#include <d3d11.h>
#include <dxgi1_2.h>
#include <Atlbase.h>
#include <comdef.h>

#include <windows.h>
#include <shlobj.h>
#include <shellapi.h>
#include <dxgi1_2.h>
#include <d3d11.h>
#include <memory>
#include <algorithm>
#include <string>
#include <iostream>

#pragma comment(lib, "D3D11.lib")
#pragma comment(lib, "D3d9.lib")
#pragma comment(lib, "dxgi.lib")

#pragma comment(lib, "gdi32.lib")
//using namespace System;
#define EXIT(hr) { if (FAILED(hr)) \
            {std::cout<<"ERROR"<<std::endl; return -1; } }

HBITMAP GetPix(ID3D11Texture2D* d3dtex, ID3D11Device* pDevice, DXGI_OUTDUPL_DESC OutputDuplDesc)
{
HRESULT hr;

HBITMAP hBitmapTexture = NULL;
HGDIOBJ hBitmap;

D3D11_TEXTURE2D_DESC desc;
ID3D11Texture2D* pNewTexture = NULL;

D3D11_TEXTURE2D_DESC description;

d3dtex->GetDesc(&desc);
d3dtex->GetDesc(&description);

description.BindFlags = 0;
description.CPUAccessFlags = D3D11_CPU_ACCESS_READ | D3D11_CPU_ACCESS_WRITE;
description.Usage = D3D11_USAGE_STAGING;
description.MiscFlags = 0;

if (FAILED(pDevice->CreateTexture2D(&description, NULL, &pNewTexture)))
{
    return NULL;
}

ID3D11DeviceContext* ctx = NULL;
pDevice->GetImmediateContext(&ctx);

ctx->CopyResource(pNewTexture, d3dtex);

D3D11_MAPPED_SUBRESOURCE resource;
UINT subresource = D3D11CalcSubresource(0, 0, 0);
ctx->Map(pNewTexture, subresource, D3D11_MAP_READ_WRITE, 0, &resource);

// Copy from texture to bitmap buffer.
std::unique_ptr<BYTE> pBuf(new BYTE[resource.RowPitch * desc.Height]);
UINT lBmpRowPitch = OutputDuplDesc.ModeDesc.Width * 4;
BYTE* sptr = reinterpret_cast<BYTE*>(resource.pData);
std::cout << (int)sptr[2000] << " " << (int)sptr[2001] <<" " << (int)sptr[2002] << std::endl;
BYTE* dptr = pBuf.get() + resource.RowPitch * desc.Height - lBmpRowPitch;
UINT lRowPitch = std::min<UINT>(lBmpRowPitch, resource.RowPitch);
///std::cout << dptr << std::endl;



for (size_t h = 0; h < OutputDuplDesc.ModeDesc.Height; ++h)
{
    memcpy_s(dptr, lBmpRowPitch, sptr, lRowPitch);
    sptr += resource.RowPitch;
    dptr -= lBmpRowPitch;

}


//std::cout << resource.pData << std::endl;
ctx->Unmap(pNewTexture, subresource);
long g_captureSize = lRowPitch * desc.Height;
UCHAR* g_iMageBuffer = nullptr;
g_iMageBuffer = new UCHAR[g_captureSize];
g_iMageBuffer = (UCHAR*)malloc(g_captureSize);

//Copying to UCHAR buffer 
//memcpy(g_iMageBuffer, pBuf, g_captureSize);
//std::cout << sptr << std::endl;

}
int main()
{
HRESULT hr;
D3D_FEATURE_LEVEL featureLevels[] =
{
    D3D_FEATURE_LEVEL_11_0,
    D3D_FEATURE_LEVEL_10_1,
    D3D_FEATURE_LEVEL_10_0,
    D3D_FEATURE_LEVEL_9_1
};

D3D_FEATURE_LEVEL d3dFeatLvl;
ID3D11Device* pDevice = nullptr;
ID3D11DeviceContext* pImmediateContext = nullptr;

hr = D3D11CreateDevice(NULL, D3D_DRIVER_TYPE_HARDWARE,
    NULL, D3D11_CREATE_DEVICE_DEBUG, featureLevels,
    ARRAYSIZE(featureLevels),
    D3D11_SDK_VERSION,
    &pDevice,
    &d3dFeatLvl,
    &pImmediateContext);
EXIT(hr);

IDXGIDevice* DxgiDevice = nullptr;
hr = pDevice->QueryInterface(__uuidof(IDXGIDevice), reinterpret_cast<void**>(&DxgiDevice));


IDXGIAdapter* DxgiAdapter = nullptr;
hr = DxgiDevice->GetParent(__uuidof(IDXGIAdapter), reinterpret_cast<void**>(&DxgiAdapter));
EXIT(hr);
DxgiDevice->Release();
DxgiDevice = nullptr;

IDXGIOutput* DxgiOutput = nullptr;
hr = DxgiAdapter->EnumOutputs(0, &DxgiOutput);
EXIT(hr);
DxgiAdapter->Release();
DxgiAdapter = nullptr;

DXGI_OUTPUT_DESC OutputDesc;
DxgiOutput->GetDesc(&OutputDesc);


IDXGIOutput1* DxgiOutput1 = nullptr;
hr = DxgiOutput->QueryInterface(__uuidof(IDXGIOutput1), reinterpret_cast<void**>(&DxgiOutput1));
EXIT(hr);

DxgiOutput->Release();
DxgiOutput = nullptr;


IDXGIOutputDuplication* DeskDupl = nullptr;
hr = DxgiOutput1->DuplicateOutput(pDevice, &DeskDupl);
EXIT(hr);
DxgiOutput1->Release();
DxgiOutput1 = nullptr;

DXGI_OUTDUPL_DESC OutputDuplDesc;
DeskDupl->GetDesc(&OutputDuplDesc);

ID3D11Texture2D* AcquiredDesktopImage = nullptr;

IDXGIResource* DesktopResource = nullptr;
DXGI_OUTDUPL_FRAME_INFO FrameInfo;
for (; ; )
{
    hr = DeskDupl->AcquireNextFrame(20, &FrameInfo, &DesktopResource);
    if (hr != 0) {
        std::cout << "Error";
        continue;
    }
    if (FrameInfo.LastPresentTime.QuadPart == 0)
    {
        hr = DesktopResource->QueryInterface(__uuidof(ID3D11Texture2D), reinterpret_cast<void**>(&AcquiredDesktopImage));
        DesktopResource->Release();
        hr = DeskDupl->ReleaseFrame();
        EXIT(hr);
        continue;
    }
    //hr = DesktopResource->QueryInterface(__uuidof(ID3D11Texture2D), reinterpret_cast<void**>(&AcquiredDesktopImage));
    GetPix(AcquiredDesktopImage, pDevice, OutputDuplDesc);
    break;
}


// <<--- END ADDED

EXIT(hr);
DesktopResource->Release();
DesktopResource = nullptr;
//GetPix(AcquiredDesktopImage, pDevice);
//GETRGB(AcquiredDesktopImage, pDevice);
return 0;
}

Good afternoon. This program outputs pixel 1 pixel. I try to make the program print it every 1/60 of a second, but my loop doesn't work, but only works once at the beginning, and then just displays an error. help me please

while (1) {
    for (; ; )
    {
        hr = DeskDupl->AcquireNextFrame(20, &FrameInfo, &DesktopResource);
        if (hr != 0) {
            std::cout << "Error";
            continue;
        }
        if (FrameInfo.LastPresentTime.QuadPart == 0)
        {
            hr = DesktopResource->QueryInterface(__uuidof(ID3D11Texture2D), reinterpret_cast<void**>(&AcquiredDesktopImage));
            DesktopResource->Release();
            hr = DeskDupl->ReleaseFrame();
            EXIT(hr);
            continue;
        }
        //hr = DesktopResource->QueryInterface(__uuidof(ID3D11Texture2D), reinterpret_cast<void**>(&AcquiredDesktopImage));
        GetPix(AcquiredDesktopImage, pDevice, OutputDuplDesc);
        break;
    }
}

Perhaps there is a problem with the override? I even loaded int main into this loop, but it still fires only once at the beginning.

来源:https://stackoverflow.com/questions/65065765/dxgi-fails-to-scan-60-fps-pixels

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!