2017-11-16 9 views
1

ここのコードはGPUで実行され、Windowsの画面をキャプチャすると、ID3D11Texture2Dリソースになります。 g_iMageBuffer私はCPUのメモリにして​​バッファから​​バッファにGPU resourceを取ってそのUCHARID3D11DeviceContext::Mapを使用します。GPUメモリ(ID3D11Texture2Dリソース)にCPUメモリ(UCHARバッファ)を取り込む方法

リバースエンジニアリングをやりたいので、g_iMageBufferバッファ(CPUメモリ)をID3D11Texture2D(GPUメモリ)に入れたいと思います。誰かが私にこのリバースエンジニアリングを行う方法を手伝ってください。私はグラフィカルな部分が初めてです。

//Variable Declaration 
IDXGIOutputDuplication* IDeskDupl; 
IDXGIResource*   lDesktopResource = nullptr; 
DXGI_OUTDUPL_FRAME_INFO IFrameInfo; 
ID3D11Texture2D*  IAcquiredDesktopImage; 
ID3D11Texture2D*  lDestImage; 
ID3D11DeviceContext* lImmediateContext; 
UCHAR*     g_iMageBuffer=nullptr; 

//Screen capture start here 
hr = lDeskDupl->AcquireNextFrame(20, &lFrameInfo, &lDesktopResource); 

// >QueryInterface for ID3D11Texture2D 
hr = lDesktopResource->QueryInterface(IID_PPV_ARGS(&lAcquiredDesktopImage)); 
lDesktopResource.Release(); 

// Copy image into GDI drawing texture 
lImmediateContext->CopyResource(lDestImage,lAcquiredDesktopImage); 
lAcquiredDesktopImage.Release(); 
lDeskDupl->ReleaseFrame(); 

// Copy GPU Resource to CPU 
D3D11_TEXTURE2D_DESC desc; 
lDestImage->GetDesc(&desc); 
D3D11_MAPPED_SUBRESOURCE resource; 
UINT subresource = D3D11CalcSubresource(0, 0, 0); 
lImmediateContext->Map(lDestImage, subresource, D3D11_MAP_READ_WRITE, 0, &resource); 

std::unique_ptr<BYTE> pBuf(new BYTE[resource.RowPitch*desc.Height]); 
UINT lBmpRowPitch = lOutputDuplDesc.ModeDesc.Width * 4; 
BYTE* sptr = reinterpret_cast<BYTE*>(resource.pData); 
BYTE* dptr = pBuf.get() + resource.RowPitch*desc.Height - lBmpRowPitch; 
UINT lRowPitch = std::min<UINT>(lBmpRowPitch, resource.RowPitch); 

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

lImmediateContext->Unmap(lDestImage, subresource); 
long g_captureSize=lRowPitch*desc.Height; 
g_iMageBuffer= new UCHAR[g_captureSize]; 
g_iMageBuffer = (UCHAR*)malloc(g_captureSize); 

//Copying to UCHAR buffer 
memcpy(g_iMageBuffer,pBuf,g_captureSize); 
+0

最後に '' malloc''と '' memcpy''より良い解決法は、あなたが得ることができる '' std :: unique_ptr <> ''に割り当てられたバッファを単に "移動"することです'' release''と呼んでください。もちろん、これは空き時間の代わりに削除を使ってクリーンアップすることを前提としています。または、単に '' std :: unique_ptr <> ''を使うだけです。 '' free''を使わなければならない場合は、最初に '' std :: unique_ptr <> ''を初期化する '' malloc''を使います(そして、 '' free''の代わりに ' 'delete'')。また、' 'new''に続いて' 'malloc''がメモリをリークしていることにも注意してください。 –

+0

@ChuckWalbourn ya私はそれを得た...ありがとう。 – Krish

答えて

0

リバースエンジニアリングは必要ありません。あなたが言うことは、「テクスチャの読み込み」と呼ばれます。

How to: Initialize a Texture Programmatically
How to: Initialize a Texture From a File

あなたはDirectXのプログラミングに新しいように見えるとして、DirectX Tool Kit for DX11 tutorialsて作業を検討してください。特に、ComPtrThrowIfFailedのセクションを必ず読んでください。

関連する問題