sharpdx / SharpDX

SharpDX GitHub Repository
http://sharpdx.org
MIT License
1.69k stars 638 forks source link

Resize D3D11Texture2D from AcquireNextFrame #920

Closed AhmedX6 closed 7 years ago

AhmedX6 commented 7 years ago

Hello, I'm posting there an issue I'm facing from a long time.. There is no documentation. Hope you will help me heartily as I am stuck for a long time now. I would like to scale a D3D11Texture2D to make it smaller. I'm using AcquireNextFrame to get an output of my desktop but it's on FullHD resolution. I just want to scale the texture because I need then to send the buffer over network. I'm on MFC Application and C++. Here is my code :

if (mRealTexture == nullptr) {
            D3D11_TEXTURE2D_DESC description;
            texture2D->GetDesc(&description);
            description.BindFlags = 0;
            description.CPUAccessFlags = D3D11_CPU_ACCESS_READ | D3D11_CPU_ACCESS_WRITE;
            description.Usage = D3D11_USAGE_STAGING;
            description.MiscFlags = 0;
            hr = mDevice->CreateTexture2D(&description, NULL, &mRealTexture);
            if (FAILED(hr)) {
                if (mRealTexture) {
                    mRealTexture->Release();
                    mRealTexture = nullptr;
                }
                return NULL;
            }
        }
        mImmediateContext->CopyResource(mRealTexture, texture2D);

        if (mScaledTexture == nullptr) {
            D3D11_TEXTURE2D_DESC description;
            texture2D->GetDesc(&description);
            description.CPUAccessFlags = D3D11_CPU_ACCESS_READ | D3D11_CPU_ACCESS_WRITE;
            description.BindFlags = D3D11_BIND_RENDER_TARGET | D3D11_BIND_SHADER_RESOURCE;
            description.Width = 1440;
            description.Height = 585;
            description.MipLevels = 4;
            description.ArraySize = 1;
            description.SampleDesc.Count = 1;
            description.SampleDesc.Quality = 0;
            description.Usage = D3D11_USAGE_DEFAULT;
            hr = mDevice->CreateTexture2D(&description, NULL, &mScaledTexture);
            if (FAILED(hr)) {
                if (mScaledTexture) {
                    mScaledTexture->Release();
                    mScaledTexture = nullptr;
                }
                return NULL;
            }
        } //How to copy the mRealTexture to the mScaledTexture and get the new buffer ?

        D3D11_MAPPED_SUBRESOURCE mapped;
        hr = mImmediateContext->Map(mRealTexture, 0, D3D11_MAP_READ_WRITE, 0, &mapped);
        if (FAILED(hr)) {
            mRealTexture->Release();
            mRealTexture = NULL;
            return NULL;
        }
        unsigned char *source = static_cast<unsigned char *>(mapped.pData);

Thank you very much for your help.

xoofx commented 7 years ago

This is not a good place to ask question about the Direct3D API. You should try either on https://www.gamedev.net/forum/10-directx-and-xna/ or https://gamedev.stackexchange.com/
But bear in mind that if you are trying to use such a low level API, you will need to learn a bit more the Direct3D API and/or how to perform image manipulation in memory and/or use an existing library to do this, otherwise it is going to be difficult to get any help on a forum.