Jump to content
  • Advertisement
haiiry

DX11 CopyResource crash (raw data access)

Recommended Posts

I'm trying to get, basically, screenshot (each 1 second, without saving) of Direct3D11 application. Code works fine on my PC(Intel CPU, Radeon GPU) but crashes after few iterations on 2 others (Intel CPU + Intel integrated GPU, Intel CPU + Nvidia GPU).

void extractBitmap(void* texture) {

    if (texture) {
        ID3D11Texture2D* d3dtex = (ID3D11Texture2D*)texture;
        ID3D11Texture2D* pNewTexture = NULL;

        D3D11_TEXTURE2D_DESC desc;
        d3dtex->GetDesc(&desc);

        desc.BindFlags = 0;
        desc.CPUAccessFlags = D3D11_CPU_ACCESS_READ | D3D11_CPU_ACCESS_WRITE;
        desc.Usage = D3D11_USAGE_STAGING;
        desc.Format = DXGI_FORMAT_R8G8B8A8_UNORM_SRGB;

        HRESULT hRes = D3D11Device->CreateTexture2D(&desc, NULL, &pNewTexture);

        if (FAILED(hRes)) {
            printCon(std::string("CreateTexture2D FAILED:" + format_error(hRes)).c_str());
            if (hRes == DXGI_ERROR_DEVICE_REMOVED)
                printCon(std::string("DXGI_ERROR_DEVICE_REMOVED -- " + format_error(D3D11Device->GetDeviceRemovedReason())).c_str());
        }
        else {
            if (pNewTexture) {
                D3D11DeviceContext->CopyResource(pNewTexture, d3dtex);

                // Wokring with texture

                pNewTexture->Release();
            }
        }
    }
    return;
}


D3D11SwapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), reinterpret_cast< void** >(&pBackBuffer));
extractBitmap(pBackBuffer);
pBackBuffer->Release();

Crash log:

CreateTexture2D FAILED:887a0005
DXGI_ERROR_DEVICE_REMOVED -- 887a0020

Once I comment out 

D3D11DeviceContext->CopyResource(pNewTexture, d3dtex); 

code works fine on all 3 PC's.

Share this post


Link to post
Share on other sites
Advertisement
2 hours ago, ajmiles said:

Have you run with the Debug Layer turned on on any machines?

As I'm injecting into another application and D3D device is already created, I did not.

Share this post


Link to post
Share on other sites

what exactly are you passing in for "texture"? its just a void pointer, your not passing in the texture bytes are you? 

They source and destination of CopyResource must be the same type and description. i'm assuming this is your problem, if "texture" is already an ID3D11Texture2D. if "texture" is not an actual resource, and instead a byte array of the texture data, you will need to create a new ID3D11Texture2D with that data (not just cast it to a ID3D11Texture2D)

Share this post


Link to post
Share on other sites
4 minutes ago, iedoc said:

what exactly are you passing in for "texture"? its just a void pointer, your not passing in the texture bytes are you? 

They source and destination of CopyResource must be the same type and description. i'm assuming this is your problem, if "texture" is already an ID3D11Texture2D. if "texture" is not an actual resource, and instead a byte array of the texture data, you will need to create a new ID3D11Texture2D with that data (not just cast it to a ID3D11Texture2D)

 

4 hours ago, haiiry said:

D3D11SwapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), reinterpret_cast< void** >(&pBackBuffer));

extractBitmap(pBackBuffer);

pBackBuffer->Release();

 

Share this post


Link to post
Share on other sites

ok, so in that case ,what did you set your render target description as? it should match your description of the texture2d you create in that function exactly

as a side note, i would make that function take in a ID3D11Texture2D* rather than a void*

Share this post


Link to post
Share on other sites

 

15 minutes ago, iedoc said:

ok, so in that case ,what did you set your render target description as? it should match your description of the texture2d you create in that function exactly

To clarify, desc.SampleDesc.Count is 1 always. Original texture is DXGI_FORMAT_R8G8B8A8_UNORM_SRGB aswell. I use format and dimensions from original texture (GetDesc and then applied it to create my texture). I've tried to check if texture is already _STAGING when I pass it to my function, but it never was.

15 minutes ago, iedoc said:

as a side note, i would make that function take in a ID3D11Texture2D* rather than a void*, and when you get the buffer from the swapchain, cast it to a ID3D11Texture2D* instead of a void**

Same as https://msdn.microsoft.com/en-us/library/windows/desktop/bb174570(v=vs.85).aspx

Just tested on 2 another systems with Intel CPU + AMD GPU and AMD CPU + AMD GPU and it didn't crash. Seems like something with intel and nvidia drivers.

Edited by haiiry

Share this post


Link to post
Share on other sites

Got it wokring. I was running my code in separate thread. After I moved it to hooked Present(), application did not crash.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!