3
votes

Is it possible to use a texture generated by C++ AMP as a screen buffer?

I would like to generate an image with my C++ AMP code (already done) and use this image to fill the entire screen of Windows 8 metro app. The image is updated 60 times per second.

I'm not at all fluent in Direct3D. I used Direct2d template app as a starting point.

First I tried to manipulate the buffer from swap chain in the C++ AMP code directly, but any attempt to write to that texture caused an error.

Processing data with AMP on GPU, then moving it to CPU memory to create a bitmap that I can use in D2D API seems way inefficient.

Can somebody share a piece of code that would allow me to manipulate swap chain buffer texture with C++ AMP directly (without data leaving the GPU) or at least populate that buffer with data from another texture that doesn't leave the GPU?

1

1 Answers

3
votes

You can interop between an AMP Texture<> and a ID3D11Texture2D buffer. The complete code and other examples of interop can be found in the 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);