#include "NVAPIGrab.h"
NVAPIGrab::NVAPIGrab()
{
m_pD3DEx = NULL;
m_pD3D9Device = NULL;
m_pD3D9Surf = NULL;
m_bNvFBCLibLoaded = false;
m_pNvFBCDX9 = NULL;
m_pNVFBCLib = NULL;
m_hGrabShare = NULL;
m_pGrabResult = NULL;
m_width = GetSystemMetrics(SM_CXSCREEN);
m_height = GetSystemMetrics(SM_CYSCREEN);
cont = 0;
}
NVAPIGrab::~NVAPIGrab()
{
ReleaseGrab();
}
BOOL NVAPIGrab::InitGrab(HANDLE & sharehand)
{
DWORD maxDisplayWidth = -1, maxDisplayHeight = -1;
NVFBC_TODX9VID_OUT_BUF NvFBC_OutBuf;
//加载NvFBCLibary
m_pNVFBCLib = new NvFBCLibrary();
if (!m_pNVFBCLib->load())
{
fprintf(stderr, "Unable to load the NvFBC library.\n");
return FALSE;
}
m_bNvFBCLibLoaded = true;
if (!InitD3D(0))
{
fprintf(stderr, "Unable to create a D3D9Ex Device.\n");
ReleaseGrab();
return FALSE;
}
if (!InitD3DSurfaces())
{
fprintf(stderr, "Unable to create a D3D9Ex Device.\n");
ReleaseGrab();
return FALSE;
}
m_pNvFBCDX9 = (NvFBCToDx9Vid *)m_pNVFBCLib->create(NVFBC_TO_DX9_VID, &maxDisplayWidth, &maxDisplayHeight, 0, (void *)m_pD3D9Device);
if (!m_pNvFBCDX9)
{
fprintf(stderr, "Failed to create an instance of NvFBCToDx9Vid.\n");
ReleaseGrab();
return FALSE;
}
NvFBC_OutBuf.pPrimary = m_pD3D9Surf;
NVFBC_TODX9VID_SETUP_PARAMS DX9SetupParams = { 0 };
DX9SetupParams.dwVersion = NVFBC_TODX9VID_SETUP_PARAMS_V2_VER;
DX9SetupParams.bWithHWCursor = 0;
DX9SetupParams.bStereoGrab = 0;
DX9SetupParams.bDiffMap = 0;
DX9SetupParams.ppBuffer = &NvFBC_OutBuf;
DX9SetupParams.eMode = NVFBC_TODX9VID_ARGB;
DX9SetupParams.dwNumBuffers = 1;
//设置抓屏信息
if (NVFBC_SUCCESS != m_pNvFBCDX9->NvFBCToDx9VidSetUp(&DX9SetupParams))
{
fprintf(stderr, "Failed when calling NvFBCDX9->NvFBCToDX9VidSetup()\n");
ReleaseGrab();
return FALSE;
}
//创建一个新的Texture用于D3D9传递数据给D3D11
D3DSURFACE_DESC _desc;
m_pD3D9Surf->GetDesc(&_desc);
HRESULT hr = m_pD3D9Device->CreateTexture(m_width, m_height, 1, D3DUSAGE_RENDERTARGET, _desc.Format, D3DPOOL_DEFAULT, &m_pGrabResult, &m_hGrabShare);
if (hr != D3D_OK)
return FALSE;
sharehand = m_hGrabShare;
return TRUE;
}
BOOL NVAPIGrab::GrabOneFarme()
{
NVFBC_TODX9VID_GRAB_FRAME_PARAMS fbcDX9GrabParams = { 0 };
NvFBCFrameGrabInfo frameGrabInfo = { 0 };
NVFBCRESULT fbcRes = NVFBC_SUCCESS;
fbcDX9GrabParams.dwVersion = NVFBC_TODX9VID_GRAB_FRAME_PARAMS_VER;
fbcDX9GrabParams.dwFlags = NVFBC_TODX9VID_NOWAIT;
fbcDX9GrabParams.eGMode = NVFBC_TODX9VID_SOURCEMODE_SCALE;
fbcDX9GrabParams.dwTargetWidth = m_width;
fbcDX9GrabParams.dwTargetHeight = m_height;
fbcDX9GrabParams.pNvFBCFrameGrabInfo = &frameGrabInfo;
fbcRes = m_pNvFBCDX9->NvFBCToDx9VidGrabFrame(&fbcDX9GrabParams);
if (fbcRes == NVFBC_SUCCESS)
{
//抓屏成功
//将OffscreenSurface的数据给m_pGrabResult
IDirect3DSurface9* pGrabSurface = NULL;
m_pGrabResult->GetSurfaceLevel(0, &pGrabSurface);
m_pD3D9Device->StretchRect(m_pD3D9Surf, NULL, pGrabSurface, NULL, D3DTEXF_NONE);
CString strIniFileB;
strIniFileB.Format(_T(".\\%d.bmp"), cont);
D3DXSaveTextureToFile(strIniFileB, D3DXIFF_BMP, m_pGrabResult, NULL);
cont++;
m_pD3D9Device->Present(0, 0, 0, 0);//提交一次避免数据没有刷新
return TRUE;
}
else
{
return FALSE;
}
return TRUE;
}
void NVAPIGrab::ReleaseGrab()
{
if (m_pNvFBCDX9)
{
m_pNvFBCDX9->NvFBCToDx9VidRelease();
m_pNvFBCDX9 = NULL;
}
if (m_pD3D9Surf)
{
m_pD3D9Surf->Release();
m_pD3D9Surf = NULL;
}
if (m_pD3D9Device)
{
m_pD3D9Device->Release();
m_pD3D9Device = NULL;
}
if (m_pD3DEx)
{
m_pD3DEx->Release();
m_pD3DEx = NULL;
}
if (m_bNvFBCLibLoaded)
{
m_pNVFBCLib->close();
}
if (m_pNVFBCLib)
{
delete m_pNVFBCLib;
m_pNVFBCLib = NULL;
}
if (m_pGrabResult != NULL)
{
m_pGrabResult->Release();
m_pGrabResult = NULL;
}
}
//初始化D3d设备 deviceID为显示器ID
BOOL NVAPIGrab::InitD3D(unsigned int deviceID)
{
HRESULT hr = S_OK;
D3DPRESENT_PARAMETERS d3dpp;
D3DADAPTER_IDENTIFIER9 adapterId;
unsigned int iAdapter = NULL;
//判断能否抓对应显示器的数据
Direct3DCreate9Ex(D3D_SDK_VERSION, &m_pD3DEx);
if (deviceID >= m_pD3DEx->GetAdapterCount())
{
printf("Error: (deviceID=%d) is not a valid GPU device. Headless video devices will not be detected. <<\n\n", deviceID);
return FALSE;
}
hr = m_pD3DEx->GetAdapterIdentifier(deviceID, 0, &adapterId);
if (hr != S_OK)
{
printf("Error: (deviceID=%d) is not a valid GPU device. <<\n\n", deviceID);
return FALSE;
}
ZeroMemory(&d3dpp, sizeof(d3dpp));
d3dpp.Windowed = true;
d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8;
d3dpp.BackBufferWidth = m_width;
d3dpp.BackBufferHeight = m_height;
d3dpp.BackBufferCount = 1;
d3dpp.SwapEffect = D3DSWAPEFFECT_COPY;
d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
d3dpp.Flags = D3DPRESENTFLAG_VIDEO;
DWORD dwBehaviorFlags = D3DCREATE_FPU_PRESERVE | D3DCREATE_MULTITHREADED | D3DCREATE_HARDWARE_VERTEXPROCESSING;
hr = m_pD3DEx->CreateDeviceEx(deviceID, D3DDEVTYPE_HAL, NULL, dwBehaviorFlags, &d3dpp, NULL, &m_pD3D9Device);
if (FAILED(hr))
{
OutputDebugString(L"CreateDevice Filed!");
return FALSE;
}
return TRUE;
}
//创建抓屏表面
BOOL NVAPIGrab::InitD3DSurfaces()
{
HRESULT hr = E_FAIL;
if (m_pD3D9Device)
{
hr = m_pD3D9Device->CreateOffscreenPlainSurface(m_width, m_height, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &m_pD3D9Surf, NULL);
if (FAILED(hr))
{
fprintf(stderr, "Failed to create D3D9 surfaces for output. Error 0x%08x\n", hr);
return FALSE;
}
}
return TRUE;
}
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
1、用NVAPI 进行D3D9抓屏,抓屏后创建HANDLE进行数据交换 2、D3D11利用D3D9传入的HANDLE使用OpenSharedResource来打开句柄实现D3D9到D3D11的数据传递 需要的库 1、NVAPI 安装完成后需要运行NVIDIA Capture SDK\bin目录下的NvFBCEnable.exe使能NVFBC抓屏 2、DXSDK_Jun10 坑(虽然已解决但是有没有大神来解释一下原理): D3D9直接传递OffcreenSurface的HANDLE,D3D11在进行某些操作时会导致设备停止工作。解决办法:从新创建了Texture来曲线救国。 抓屏成功后D3D9Device要调用Present(0, 0, 0, 0)来进行数据提交。否则第一帧D3D11拿到的数据为空。
资源推荐
资源详情
资源评论
收起资源包目录
NvapiGrabVideoToD3D11.rar (9个子文件)
NvapiGrabVideoToD3D11
NvapiGrabVideoToD3D11.sln 1KB
NvapiGrabVideoToD3D11
D3D11.cpp 2KB
Main.cpp 543B
NvapiGrabVideoToD3D11.vcxproj.filters 1KB
MyD3D11.h 468B
NvapiGrabVideoToD3D11.vcxproj.user 165B
NvapiGrabVideoToD3D11.vcxproj 6KB
NVAPIGrab.cpp 6KB
NVAPIGrab.h 783B
共 9 条
- 1
资源评论
- 老断2019-12-18编译不通过NVFBC_TODX9VID_SETUP_PARAMS_V2_VER这个变量哪里定义的?枫林古月2020-01-02这个变量在NVIDIA Capture SDK的NvFBCToDx9vid.h里面 这是Nvidia的库,可以直接去NVIDIA下
枫林古月
- 粉丝: 23
- 资源: 8
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功