0

I would like to resize a D3D11Texture2D to make it smaller. For example I have a texture in 1920x1080 and I would like to scale it 1280x720 for example. Just so you know I'm not drawing at all, I just want to get the byte buffer scaled. 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;
        }
    } //I want to copy the mRealTexture on the mScaledTexture, map the scaledTexture and get the buffer.

Thanks for help

X6Entrepreneur
  • 971
  • 2
  • 10
  • 30
  • https://stackoverflow.com/a/24070486/8111865 might help you - it's not working code, but a general overview of how to resize a resource using Direct2D and DXGI. – ozeanix Jun 12 '17 at 23:39
  • Too bad there is no sample code ... I think a lot of people need to do this... It is too vague for me to understand the steps to be taken. (In code I mean) – X6Entrepreneur Jun 13 '17 at 08:13
  • I know it sounds trite and I don't mean it that way, but try writing realistic looking pseudocode for the overview and use the MSDN docs for each interface to slowly turn it into a real implementation. Work with small parts at first and watch your HRESULTs. I've found working with DirectX and similarly structured things like DirectShow and Media Foundation can be very frustrating, but it just takes an investment of time and effort to really learn it. Once you do, solutions to problems like this will come very naturally as you have a wealth of knowledge of what DirectX can do, and how to do it. – ozeanix Jun 13 '17 at 09:32
  • So just to clear your problem - do you _HAVE TO_ make it smaller? Because you could sample the same texture a bit differently in order to get the results without scaling it. – Bartosz Boczula Jun 21 '17 at 11:37

1 Answers1

0

Having thought about this and that you are left with only a couple of options.

1 - You create a viewport etc and render to your target size with a full screen quad. Which I get the feeling you don't want to do.

2 - You roll your own scaling which isn't too bad and scale texture as you copy the data from one buffer to another.

Option 2 isn't too bad, the roughest scaling would be reading a points based on the scaling ratio, but a more accurate version would be to average a number of samples based a weighted grid (the weights need to be recalculated for each pixel you visit on your target.)

ErnieDingo
  • 444
  • 5
  • 11