当前位置: 首页 > 工具软件 > D3View > 使用案例 >

读取ID3D11ShaderResourceView、ID3D11Texture2D

濮阳烨然
2023-12-01
void SaveD3D11ResourceView(ID3D11Device *device, ID3D11DeviceContext *deviceContext, ID3D11ShaderResourceView *srv)
{
	ID3D11Resource *res;
	ID3D11Texture2D *tex;
	srv->GetResource(&res);
	res->QueryInterface(&tex);
	SaveD3D11Texture(device, deviceContext, tex);
	res->Release();
	res = 0;
	tex->Release();
	tex = 0;
	deviceContext->Flush();
}
void SaveD3D11Texture(ID3D11Device *device, ID3D11DeviceContext *deviceContext, ID3D11Texture2D *src)
{
	D3D11_TEXTURE2D_DESC texDesc;
	src->GetDesc(&texDesc);
	texDesc.Usage = D3D11_USAGE_STAGING;
	texDesc.BindFlags = 0;
	texDesc.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
	texDesc.MiscFlags = 0;

	ID3D11Texture2D *des = 0;
	HR_NO_RETURN(device->CreateTexture2D(&texDesc, NULL, &des));

	deviceContext->CopyResource(des, src);

	D3D11_MAPPED_SUBRESOURCE mapped;
	HR_NO_RETURN(deviceContext->Map(des, 0, D3D11_MAP_READ, 0, &mapped));

	int c = 0;
	if (texDesc.Format == DXGI_FORMAT_R8_UNORM) 
		c = 1;
	else
		c = 4;
	int w = mapped.RowPitch / c;
	int h = mapped.DepthPitch / mapped.RowPitch;

#if 1
    QImage pImage(w, h, QImage::Format_RGBA8888);
    memcpy(pImage.bits(), mapped.pData, w * h * c);
    pImage.save("d:/aaa.png");
#else
	uint8_t *buf = (uint8_t*)mlt_pool_alloc(w * h * c);

	memcpy(buf, mapped.pData, w * h * c);
	QImage img = QImage(buf, w, h, QImage::Format_RGBA8888);
	img.save("e:/dump.png", "PNG");
	//FILE *fp = fopen("d:/aaa.rgba", "wb");
	//fwrite(buf, 1, w * h * c, fp);
	//fclose(fp);
	mlt_pool_release(buf);
#endif

	deviceContext->Unmap(des, 0);

	des->Release();
	des = 0;
}

 

 类似资料: