2013-03-17 91 views
3

是否可以將由C++ AMP生成的紋理用作屏幕緩衝區?在Direct2D中使用C++ AMP

我想用我的C++ AMP代碼(已完成)生成圖像,並使用此圖像填充Windows 8 metro應用程序的整個屏幕。圖像每秒更新60次。

我完全不會說流利的Direct3D。我使用Direct2d模板應用程序作爲起點。

首先,我試圖直接在C++ AMP代碼中從交換鏈處理緩衝區,但是任何嘗試寫入該紋理都會導致錯誤。

在GPU上使用AMP處理數據,然後將其移至CPU內存以創建可在D2D API中使用的位圖,但效率不高。

有人可以共享一段代碼,它允許我直接使用C++ AMP操作交換鏈緩衝區紋理(沒有數據離開GPU),或者至少使用另一個不離開GPU的紋理填充數據?

回答

2

您可以在AMP Texture <>和ID3D11Texture2D緩衝區之間進行互操作。完整的代碼和interop的其他例子可以在Chapter 11 samples here中找到。

// Get a D3D texture resource from an AMP texture. 

texture<int, 2> text(100, 100); 
CComPtr<ID3D11Texture2D> texture; 
IUnknown* unkRes = get_texture(text); 
hr = unkRes->QueryInterface(__uuidof(ID3D11Texture2D), 
    reinterpret_cast<LPVOID*>(&texture)); 
assert(SUCCEEDED(hr)); 

// Create a texture from a D3D texture resource 

const int height = 100; 
const int width = 100; 

D3D11_TEXTURE2D_DESC desc; 
ZeroMemory(&desc, sizeof(desc)); 
desc.Height = height; 
desc.Width = width; 
desc.MipLevels = 1; 
desc.ArraySize = 1; 
desc.Format = DXGI_FORMAT_R8G8B8A8_UINT; 
desc.SampleDesc.Count = 1; 
desc.SampleDesc.Quality = 0; 
desc.Usage = D3D11_USAGE_DEFAULT; 
desc.BindFlags = D3D11_BIND_UNORDERED_ACCESS | D3D11_BIND_SHADER_RESOURCE; 
desc.CPUAccessFlags = 0; 
desc.MiscFlags = 0; 

CComPtr<ID3D11Texture2D> dxTexture = nullptr; 
hr = device->CreateTexture2D(&desc, nullptr, &dxTexture); 
assert(SUCCEEDED(hr)); 

texture<uint4, 2> ampTexture = make_texture<uint4, 2>(dxView, dxTexture);