1
\$\begingroup\$

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.

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.

\$\endgroup\$
1
\$\begingroup\$

CopyResource does not support scaling.

Mapping would work, but:

  1. You would need to write your own scaling algorithm
  2. You would need to download the data from GPU to system memory before scaling,
  3. You would need to re-upload the data back to the GPU after scaling.

Two ways spring to mind.

One

Create the texture with D3D11_BIND_RENDER_TARGET, use ID3D11DeviceContext::GenerateMips, then send one of the miplevels; miplevel 0 is the full resolution texture, miplevel 1 is half resolution in each dimension, miplevel 2 is quarter resolution in each dimension, etc.

Two

Draw the texture to a lower-resolution rendertarget, then use that rendertarget instead of the original source texture.

\$\endgroup\$
  • \$\begingroup\$ So is it possible to scale a buffer of pixel (1920x1080) to (1280x720) if it's the only way..? I got the buffer of data with mapping. \$\endgroup\$ – DevAndroid Jun 13 '17 at 11:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.