Hi all,
I've just implemented a screenshot function for my engine, using dx11.
This all works fine, also when the rendertarget has MSAA applied, then I use resolvesubresource into an additional rendertarget.
Basically, without AA:
- copy primary RT to RT with usage STAGING and CPU access (staging texture)
- map the RT, copy the pixel data and unmap
And with AA:
- copy primary RT with AA to temporary 1 without AA (usage default, no CPU access) (resolve texture)
- copy temporary RT without AA to new RT with usage STAGING and CPU access (staging texture)
- map the RT, copy the pixel data and unmap
So it all works fine, I applied a branch for MSAA enabled/ disabled.
My question; according to the MSDN documentation, "Resolvesubresource", should only work when 'source' has >1 samples and 'dest' has 1 sample (in the desc). But somehow the process with the resolve texture, including using 'resolvesubresource', also works when I don't have MSAA enabled. So both source + dest has 1 sample.
I would expect the D3D debugger/ logging to give an error or at least a warning.
Below the code in which this applies (don't mind it not being optimized/ cleaned up yet, just for illustration)
std::vector<char> DX11RenderTarget::GetRGBByteArray() const
{
CComPtr<ID3D11Device> myDev;
CComPtr<ID3D11DeviceContext> myDevContext;
mBuffer->GetDevice(&myDev);
myDev->GetImmediateContext(&myDevContext);
// resolve texture
D3D11_TEXTURE2D_DESC tempRTDesc;
tempRTDesc.Width = mWidth;
tempRTDesc.Height = mHeight;
tempRTDesc.MipLevels = 1;
tempRTDesc.ArraySize = 1;
tempRTDesc.Format = mDXGIFormat;
tempRTDesc.BindFlags = 0;
tempRTDesc.MiscFlags = 0;
tempRTDesc.SampleDesc.Count = 1;
tempRTDesc.SampleDesc.Quality = 0;
tempRTDesc.Usage = D3D11_USAGE_DEFAULT;
tempRTDesc.CPUAccessFlags = 0;
CComPtr<ID3D11Texture2D> tempTex;
CComPtr<ID3D11Texture2D> anotherTempTex;
myDev->CreateTexture2D(&tempRTDesc, 0, &tempTex); // add check if(FAILED), logging
myDevContext->ResolveSubresource(tempTex, 0, mBuffer, 0, GetDXGIFormat(mBufferFormat)); // format = DXGI_FORMAT_R8G8B8A8_UNORM_SRGB ; not flexible now
// staging texture
tempRTDesc.Usage = D3D11_USAGE_STAGING;
tempRTDesc.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
myDev->CreateTexture2D(&tempRTDesc, 0, &anotherTempTex); // add check if(FAILED), logging
myDevContext->CopyResource(anotherTempTex, tempTex);
// map, copy pixels and unmap
D3D11_MAPPED_SUBRESOURCE loadPixels;
myDevContext->Map(anotherTempTex, 0, D3D11_MAP_READ, 0, &loadPixels);
std::vector<char> image(mWidth*mHeight*3);
char *texPtr = (char*)loadPixels.pData;
int currPos = 0;
for(size_t row=0;row<mHeight;++row)
{
texPtr = (char*)loadPixels.pData + loadPixels.RowPitch * row;
for(size_t j=0;j<mWidth*4;j+=4) // RGBA, skip Alpha
{
image[currPos] = texPtr[j];
image[currPos+1] = texPtr[j+1];
image[currPos+2] = texPtr[j+2];
currPos += 3;
}
}
myDevContext->Unmap(anotherTempTex, 0);
return image;
}
↧