1. 程式人生 > >Direct3D11 儲存Texture2D到本地檔案

Direct3D11 儲存Texture2D到本地檔案

	#include <D3Dx11tex.h>
	#include <d3d11_1.h>
	#pragma comment(lib, "D3DX11.lib")
	#pragma comment(lib, "d3d11.lib")
	#pragma comment(lib, "dxgi.lib")
	ID3D11Texture2D *    SaveTexture2D;
 	ID3D11Resource  *    SaveTextureResource;
	D3D11_TEXTURE2D_DESC saveTextureDesc;
	ZeroMemory(&saveTextureDesc, sizeof(saveTextureDesc));

	saveTextureDesc.Width = PixWidth;
	saveTextureDesc.Height = PixHight;
	saveTextureDesc.MipLevels = 1;
	saveTextureDesc.ArraySize = 1;
	saveTextureDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
	saveTextureDesc.SampleDesc.Count = 1;
	saveTextureDesc.Usage = D3D11_USAGE_DEFAULT;
	saveTextureDesc.BindFlags = D3D11_BIND_UNORDERED_ACCESS;
	saveTextureDesc.CPUAccessFlags = 0;
	saveTextureDesc.MiscFlags = D3D11_RESOURCE_MISC_SHARED;

	if (FAILED(D3D11_Device->CreateTexture2D(&saveTextureDesc, NULL, &SaveTexture2D)))	return false;
	
	hr = SaveTexture2D->QueryInterface(__uuidof(ID3D11Resource), (void**)&SaveTextureResource);

	if (FAILED(hr))
	{
		return false;
	}
	D3D11_Context->CopySubresourceRegion(SaveTexture2D, 0, 0, 0, 0, SourceTexture2D, 0, nullptr);
	HRESULT hr = D3DX11SaveTextureToFile(D3D11_Context, SaveTextureResource, D3DX11_IFF_JPG, "E:\\Test0.jpg");