Skip to main content
GameDev.net gamedev.net
🔒 Locked

DirectX11 - CopyResource (Basic)

Started by Magmatwister Jun 29, 2011 at 10:07 PM 1 replies 8.8k views
Original Post
Magmatwister
Magmatwister
This is about as basic a code as it gets. Can anyone show me what I'm doing wrong? I used PIX to acquire the textures that I set to the pipeline and I have linked them below. It should be obvious which part of the picture is the resultant texture ;( Will give rep for anyone who helps me



HRESULT DirectXHandler11::CopyResourceTest()
{
ID3D11ShaderResourceView* pResultantTextureSRV = NULL;
ID3D11Texture2D* pResultantTexture = NULL;
ID3D11Texture2D* pStagingTexture = NULL;
ID3D11Texture2D* pOriginalTexture = NULL;
ID3D11ShaderResourceView* pOriginalTextureSRV = NULL;
D3D11_MAPPED_SUBRESOURCE Subresource;

D3D11_TEXTURE2D_DESC RTDESC;
ZeroMemory(&RTDESC, sizeof(D3D11_TEXTURE2D_DESC));
RTDESC.Height = 33;
RTDESC.Width = 33;
RTDESC.MipLevels = 1;
RTDESC.ArraySize = 1;
RTDESC.SampleDesc.Count = 1;
RTDESC.SampleDesc.Quality = 0;
RTDESC.Format = DXGI_FORMAT_R32_FLOAT;
RTDESC.Usage = D3D11_USAGE_DEFAULT;
RTDESC.BindFlags = D3D11_BIND_SHADER_RESOURCE;
RTDESC.CPUAccessFlags = 0;

float* pTexels = new float[33*33];

for(int i = 0; i < 33; i++)
{
for(int j = 0; j < 33; j++)
{
pTexels[(j*33)+i] = (((float)i/32.0f) + ((float)j/32.0f)) / 2;
}
}


D3D11_SUBRESOURCE_DATA InitialData;
ZeroMemory(&InitialData, sizeof(D3D11_SUBRESOURCE_DATA));
InitialData.pSysMem = pTexels;
InitialData.SysMemPitch = sizeof(float)*33;

mHR = m_pDevice->CreateTexture2D(&RTDESC, &InitialData, &pOriginalTexture);

if(SUCCEEDED(mHR))
{
mHR = m_pDevice->CreateShaderResourceView(pOriginalTexture, NULL, &pOriginalTextureSRV);
m_pDeviceContext->PSSetShaderResources(0, 1, &pOriginalTextureSRV);
}





//D3D11_TEXTURE2D_DESC RTDESC;
ZeroMemory(&RTDESC, sizeof(D3D11_TEXTURE2D_DESC));
RTDESC.Height = 33;
RTDESC.Width = 33;
RTDESC.MipLevels = 1;
RTDESC.ArraySize = 1;
RTDESC.SampleDesc.Count = 1;
RTDESC.SampleDesc.Quality = 0;
RTDESC.Format = DXGI_FORMAT_R32_FLOAT;
RTDESC.Usage = D3D11_USAGE_STAGING;
RTDESC.BindFlags = 0;
RTDESC.CPUAccessFlags = D3D11_CPU_ACCESS_READ;

if(SUCCEEDED(mHR))
{
mHR = m_pDevice->CreateTexture2D(&RTDESC, NULL, &pStagingTexture);

//if(SUCCEEDED(mHR))
//{
// mHR = m_pDevice->CreateShaderResourceView(pStagingTexture, NULL, &pStagingTextureSRV);
//}
}

m_pDeviceContext->CopyResource(pStagingTexture, pOriginalTexture);


if(SUCCEEDED(mHR))
{
mHR = m_pDeviceContext->Map(pStagingTexture, 0, D3D11_MAP_READ, 0, &Subresource);

float* pData = (float*)Subresource.pData;


//++++++++++++++++++++++++++++++++++++++
//RECONSTRUCT THE RESULTANT TEXTURE...
//++++++++++++++++++++++++++++++++++++++
//D3D11_TEXTURE2D_DESC RTDESC;
ZeroMemory(&RTDESC, sizeof(D3D11_TEXTURE2D_DESC));
RTDESC.Height = 33;
RTDESC.Width = 33;
RTDESC.MipLevels = 1;
RTDESC.ArraySize = 1;
RTDESC.SampleDesc.Count = 1;
RTDESC.SampleDesc.Quality = 0;
RTDESC.Format = DXGI_FORMAT_R32_FLOAT;
RTDESC.Usage = D3D11_USAGE_DEFAULT;
RTDESC.BindFlags = D3D11_BIND_SHADER_RESOURCE;
RTDESC.CPUAccessFlags = 0;

//D3D11_SUBRESOURCE_DATA InitialData;
ZeroMemory(&InitialData, sizeof(D3D11_SUBRESOURCE_DATA));
InitialData.pSysMem = pData;
InitialData.SysMemPitch = sizeof(float)*33;

mHR = m_pDevice->CreateTexture2D(&RTDESC, &InitialData, &pResultantTexture);

if(SUCCEEDED(mHR))
{
mHR = m_pDevice->CreateShaderResourceView(pResultantTexture, NULL, &pResultantTextureSRV);
m_pDeviceContext->PSSetShaderResources(1, 1, &pResultantTextureSRV);
}

m_pDeviceContext->Unmap(pStagingTexture, 0);
}



//+++++++++++++++
//FIND ANY ERRORS
//+++++++++++++++
if(FAILED(mHR))
{
GeneralErrorHandler::OutputDxError(mHR, "CopyResourceTest", NULL);
GeneralErrorHandler::LogErrorToFile("CopyResourceTest", "[FAILED]", "", "ErrorLog.txt");
}

SafeDelete(pTexels);
SafeRelease(pResultantTextureSRV);
SafeRelease(pResultantTexture);
SafeRelease(pStagingTexture);
SafeRelease(pOriginalTexture);
SafeRelease(pOriginalTextureSRV);

return mHR;
}





[media]http://imageshack.us/f/830/copyresourceproblem.png/[/media]
Currently trying to make a planet renderer. After many hours of work, somehow I know It'll never be complete.
Also, If I help you, please give me an ++
MJP
MJP
When you map the staging texture to read back the data, you're not checking the pitch. When you specify the pitch while initializing a texture (the SysMemPitch member of the D3D11_SUBRESOURCE_DATA) , basically you're saying "here is some data, and here is the pitch of that data". It doesn't specify the pitch to be used of the actual texture resource memory, which will instead be based on hardware and driver requirements. So when you map that staging texture, you have to use the pitch specified by the RowPitch member of the D3D11_MAPPED_SUBRESOURCE you get back from ID3D11DeviceContext::Map.
Magmatwister
Magmatwister

When you map the staging texture to read back the data, you're not checking the pitch. When you specify the pitch while initializing a texture (the SysMemPitch member of the D3D11_SUBRESOURCE_DATA) , basically you're saying "here is some data, and here is the pitch of that data". It doesn't specify the pitch to be used of the actual texture resource memory, which will instead be based on hardware and driver requirements. So when you map that staging texture, you have to use the pitch specified by the RowPitch member of the D3D11_MAPPED_SUBRESOURCE you get back from ID3D11DeviceContext::Map.


It's times like this, that I love gamedev. Concise, non-condescending replies that genuinely help people. Thanks a bunch, enjoy your rep and I hope you will stick on gamedev for a long time to come. We need more people like you in this forum. Thanks!

Edit:

float* pHeightmapData = (float*)HeightmapSubresource.pData;
float HeightValue = pHeightmapData[nCurrentVertex]


If I was to access the data like this, how would I incorporate the pitch?

Edit2:
I think I figured it out, for anyone wondering, I just did


UINT uPitch = HeightmapSubresource.RowPitch;
pHeightmapData[((uPitch/sizeof(float))*nHeightY) + nHeightX]
Currently trying to make a planet renderer. After many hours of work, somehow I know It'll never be complete.
Also, If I help you, please give me an ++

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.