Search code examples
c++shaderdirectx-11

DirectX 11 How process an image from sharedHandle on the GPU


I have a texture that exists on the GPU, I have a sharedHandle to that texture and want to do some image processing on that picture. I want to get the texture cordinates of the pixels of a certen color.

For example I want all pixels of the color r135, g80, b175 with a shade variation of 40 (so a green color of 70 and 90 is acceptable, but not something below 40 or above 120 and so on)

The problem is that I want to do this on the GPU via a shader, but I dont know how to.

I have a working version that works by coping the texture to ram and then reading from base adress of that. The problem is that it is too slow. Just coping the texture from the gpu to ram takes over 20ms. I want this to be max 5ms total, and that is why I want to just send the cordinates of the pixels insted of the entire picture to ram.

this is the code I used to copy the texture into ram:

gs_texture_2d *_tex = (gs_texture_2d *)updateTex;

updateTex->device->device.Get()->CreateTexture2D(&(desc), NULL,
                                 &currTexture);

updateTex->device->context.Get()->CopyResource(currTexture,
                                   _tex->texture);

UINT subresource = D3D11CalcSubresource(0, 0, 0);
D3D11_MAPPED_SUBRESOURCE *pRes = new D3D11_MAPPED_SUBRESOURCE;

updateTex->device->context->Map(currTexture, subresource,
                        D3D11_MAP_READ_WRITE, 0, pRes);

void *baseadress = pRes->pData;

How do you write a shader to read the pixels given tex_handle/sharedHandle and then sending that data to the cpu?

Im kinda new to both c++ and directX so any help would be very appreciated!


Solution

  • Solved this by first calling OpenSharedResource, CreateShaderResourceView, CopyResource, Map and then using the D3D11_MAPPED_SUBRESOURCE.pData as a starting pointer.