Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resize Texture2D DirectX 11 c++ #93

Closed
AhmedX6 opened this issue May 30, 2017 · 8 comments
Closed

Resize Texture2D DirectX 11 c++ #93

AhmedX6 opened this issue May 30, 2017 · 8 comments
Labels

Comments

@AhmedX6
Copy link

AhmedX6 commented May 30, 2017

Hello,

Is it possible to resize a texture2D obtained from AcquireNextFrame function ?
I would like to decrease the resolution of the image using DirectX11.

Thanks

@walbourn
Copy link
Member

The easiest way to do the resize is to just create a new texture that is the size you want with the option to bind as both a shader resource view and as a render target. Then render to that new texture a full-screen quad (using SpriteBatch) of the original image. This leverages the GPU to do the resizing.

Alternatively, if you need a CPU solution the DirectXTex library supports a number of resizing options.

@AhmedX6
Copy link
Author

AhmedX6 commented May 31, 2017

Hello Walbourn,

Thank you for your help.
Can you please show me some code ? I need the pixel buffer resized after applying SpriteBatch resizing.
Here is my code :
if (gRealTexture == 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 = gDevice->CreateTexture2D(&description, NULL, &gRealTexture); if (FAILED(hr)) { if (gRealTexture) { gRealTexture->Release(); gRealTexture = nullptr; } return NULL; } }
this is the texture I have from AcquireNextFrame. I can map it and get pixel buffer data. (1920x1080x4)
Now before get the pixel buffer, I would like to resize the realTexture.
Here is the texture I created (1280x720)
if (gScaledTexture == nullptr) { D3D11_TEXTURE2D_DESC description; description.BindFlags = D3D11_BIND_RENDER_TARGET | D3D11_BIND_SHADER_RESOURCE; description.CPUAccessFlags = 0; description.Usage = D3D11_USAGE_DEFAULT; description.MiscFlags = 0; description.Width = 1280; description.Height = 720; hr = gDevice->CreateTexture2D(&description, NULL, &gScaledTexture); if (FAILED(hr)) { if (gRealTexture) { gRealTexture->Release(); gRealTexture = nullptr; } return NULL; } }
how to use SpriteBatch to copy the RealTexture into the ScaledTexture ?
Thank you I'm totally stuck...

@walbourn
Copy link
Member

SpriteBatch can only render to textures that are something other than USAGE_STAGING. If you need to read back the texture from the GPU to the CPU, then you'd use CopyResource to get it from a USAGE_DEFAULT to a USAGE_STAGING texture. Note that CopyResource will not do resizing, so you need to make sure you've already done the resize on the GPU before copying it.

@AhmedX6
Copy link
Author

AhmedX6 commented May 31, 2017

The texture with USAGE_STAGING is the texture I get from AcquireNextFrame. I can copy resource on a texture with USAGE_DEFAULT with the same resolution. But how to reduce resolution after that ?
Can you please show me a sample code with SpriteBatch and D3D11Texture2D ?
Thank you very much for your help.

@walbourn
Copy link
Member

I don't really have the time on-hand to code up a full solution, but if you already have everything in CPU memory (which is what's happening with USAGE_STAGING) rather than on the GPU, then you should probably just look at using DirectXTex to do the resize on the CPU. The SpriteBatch render-to-texture solution only makes sense if your original is on the GPU and you want to create a new smaller texture on the GPU.

@AhmedX6
Copy link
Author

AhmedX6 commented May 31, 2017

Oh okk so I should check with DirectXTex.
Please can you tell me which function in particular in DirectXTex should I use for my case ?
Thanks

@walbourn
Copy link
Member

I don't understand your usage case in detail here, but you can find docs on the DirectXTex Resize function here.

@kmaki565
Copy link

kmaki565 commented May 4, 2021

For those who have the same question...
I followed the first advice of @walbourn and created a simple app to do this: https://github.com/kmaki565/ResizeGfx
Also integrated it to a screen recorder solution: sskodje/ScreenRecorderLib#136

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants