[Solved] DX11 Updating texture data

Started by
20 comments, last by MJP 12 years, 9 months ago
After looking through all the threads I could find relating to this topic and failing, it would seem it is now time to ask for some help.


I have a 32bit BGRA buffer that I am binding to a texture:

D3D11_TEXTURE2D_DESC td;
td.ArraySize = 1;
td.BindFlags = D3D11_BIND_SHADER_RESOURCE;
td.Usage = D3D11_USAGE_DYNAMIC;
td.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
td.Format = DXGI_FORMAT_B8G8R8A8_UNORM;
td.Height = m_renderBuffer.height;
td.Width = m_renderBuffer.width;
td.MipLevels = 1;
td.MiscFlags = 0;
td.SampleDesc.Count = 1;
td.SampleDesc.Quality = 0;

D3D11_SUBRESOURCE_DATA srd;
srd.pSysMem = m_renderBuffer.buffer;
srd.SysMemPitch = m_renderBuffer.rowspan;
srd.SysMemSlicePitch = 0;

HR(device->CreateTexture2D(&td, &srd, &m_texture));


which I then bind to a ShaderResourceView and set as a shader resource

HR(device->CreateShaderResourceView(m_texture, 0, &m_textureRV));
.
.
deviceContext->PSSetShaderResources(0, 1, &m_textureRV);


I render this once and it works just as expected.
Then I try to update the texture..

HR(deviceContext->Map(m_texture, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource)

mappedResource.pData = (void*)m_renderBuffer.buffer;
mappedResource.RowPitch = m_renderBuffer.rowspan;
mappedResource.DepthPitch = 0;

deviceContext->Unmap(m_texture, 0);


Right now Im just updating with the same buffer but that wont be the case in reality. After I try to update the texture, the texture is no longer shows up, and nothing shows up anymore. I am not getting any errors related to this from the Debug Device. I feel like I am doing everything correctly. Creating the texture with D3D11_USAGE_DYNAMIC, and D3D11_CPU_ACCESS_WRITE then using Map to update the texture. Everything I read says this is the proper method. Either I missed something, or I am doing it incorrectly.

Any ideas?
"There is no secret ingredient.." - Po (Kung Fu Panda)
Advertisement
[font="Arial"]You're not using this right. What Map() does is reserve a piece of GPU memory for you to copy your data in, and gives you a pointer that you can use to write into that memory. That's the [color="#1C2837"][color="#000000"]mappedResource[color="#666600"].[color="#000000"]pData pointer. You're not meant to set that pointer to your data, but instead copy your data into the memory it points to.[/font]
so just use memcpy? or is there a directX function to do this smoothly.
"There is no secret ingredient.." - Po (Kung Fu Panda)
memcpy will work just fine
Thanks for the help. I think I got it all figured out.
"There is no secret ingredient.." - Po (Kung Fu Panda)
So I am trying to update my buffer and after copying my new texture data in, the texture is rendered with a shearing effect.



D3D11_MAPPED_SUBRESOURCE mappedResource;
HR(deviceContext->Map(m_texture, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource));
memcpy(mappedResource.pData, buffer, rowspan*height);
deviceContext->Unmap(m_texture, 0);


I am pretty sure the buffer itself is not the issue because so my guess is it has something to do with my use of memcpy but i just do see it.
I feel like I am being a little vague, but cant think of anything else to say haha. I can try to elaborate if need be.
"There is no secret ingredient.." - Po (Kung Fu Panda)
The texture resource will have it's own pitch (the number of bytes in a row), which is probably different than the pitch of your source data. This pitch is given to you as the "RowPitch" member of D3D11_MAPPED_SUBRESOURCE. So typically you do something like this:

BYTE* mappedData = reinterpret_cast<BYTE*>(mappedResource.pData);
for(UINT i = 0; i < height; ++i)
{
memcpy(mappedData, buffer, rowspan);
mappedData += mappedResource.RowPitch;
buffer += rowspan;
}

The texture resource will have it's own pitch (the number of bytes in a row), which is probably different than the pitch of your source data. This pitch is given to you as the "RowPitch" member of D3D11_MAPPED_SUBRESOURCE. So typically you do something like this:

BYTE* mappedData = reinterpret_cast<BYTE*>(mappedResource.pData);
for(UINT i = 0; i < height; ++i)
{
memcpy(mappedData, buffer, rowspan);
mappedData += mappedResource.RowPitch;
buffer += rowspan;
}



Excellent, works perfectly. Except now I am curious why the pitch of the texture is different when I set the pitch in the D3D11_SUBRESOURCE_DATA description.
"There is no secret ingredient.." - Po (Kung Fu Panda)
[font="Calibri"][font="Calibri"][/font]

You don’t set the pitch with D3D11_SUBRESOURCE_DATA. You just tell the runtime what pitch your data uses. The runtime will use this information and the real pitch from your texture to copy the data correctly. As Map gives you a memory pointer were the driver wants the data to be copied it tells you the real pitch.[/font]

Ok I understand whats going on much better now. Thank you all for the help and the insight.
"There is no secret ingredient.." - Po (Kung Fu Panda)

This topic is closed to new replies.

Advertisement