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

[Solved] DX11 Updating texture data

Started by Sturmh Jul 12, 2011 at 3:46 AM 20 replies 45.9k views
Original Post
Sturmh
Sturmh
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)
RDragon1
RDragon1
[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]
Sturmh
Sturmh
so just use memcpy? or is there a directX function to do this smoothly.
"There is no secret ingredient.." - Po (Kung Fu Panda)
RDragon1
RDragon1
memcpy will work just fine
Sturmh
Sturmh
Thanks for the help. I think I got it all figured out.
"There is no secret ingredient.." - Po (Kung Fu Panda)
Sturmh
Sturmh
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)
MJP
MJP
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;
}
Sturmh
Sturmh

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)
Demirug
Demirug
[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]

Sturmh
Sturmh
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)
Sturmh
Sturmh
Everything is working and I am updating the texture several times a second. After about 30 seconds or so I get a E_OUTOFMEMORY error from:

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

All I am doing in my program is updating the texture, then drawing the texture on a quad.

Could it be how I am updating my constant buffer and resource view for the shader that is causing this problem? I dont feel like that would be it assuming I was doing something incorrectly.
[font="CourierNew, monospace"] [/font]
[font="CourierNew, monospace"] [/font]
"There is no secret ingredient.." - Po (Kung Fu Panda)
Magmatwister
Magmatwister

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;
}



MJP, I've seen you post this at least twice now besides for me, I'm thinking that maybe msdn should post the information about this under the map method in remarks?
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

Everything is working and I am updating the texture several times a second. After about 30 seconds or so I get a E_OUTOFMEMORY error from:

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

All I am doing in my program is updating the texture, then drawing the texture on a quad.

Could it be how I am updating my constant buffer and resource view for the shader that is causing this problem? I dont feel like that would be it assuming I was doing something incorrectly.
[font="CourierNew, monospace"] [/font]
[font="CourierNew, monospace"] [/font]


I've never heard of an out of memory error happening from mapping a resource. You're not creating any resources during the frame, are you? Also, did you remember to call Unmap after copying in the data?
MJP
MJP

[quote name='MJP' timestamp='1310492347' post='4834406']
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;
}



MJP, I've seen you post this at least twice now besides for me, I'm thinking that maybe msdn should post the information about this under the map method in remarks?
[/quote]

Yeah, that would be nice. I can also add it under the "community content" section at the bottom of the page, which might be helpful for some people.
Sturmh
Sturmh

[quote name='Sturmh' timestamp='1310580540' post='4834932']
Everything is working and I am updating the texture several times a second. After about 30 seconds or so I get a E_OUTOFMEMORY error from:

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

All I am doing in my program is updating the texture, then drawing the texture on a quad.

Could it be how I am updating my constant buffer and resource view for the shader that is causing this problem? I dont feel like that would be it assuming I was doing something incorrectly.
[font="CourierNew, monospace"] [/font]
[font="CourierNew, monospace"] [/font]


I've never heard of an out of memory error happening from mapping a resource. You're not creating any resources during the frame, are you? Also, did you remember to call Unmap after copying in the data?
[/quote]

No I am not.
Each frame i call two functions, update() and render().
update -> update a buffer, check if it changed from last frame, and if it did, update the texture with:


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

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


Additionally, every time i call map here, the D3D debug output says "Error:......Resource is already mapped" or something of the sort. I know for a fact that the above map call is the cause of the "error"

I ran it once with the texture size at about 300 x 150, as opposed to about 800 x 400, and I never encountered the E_OUTOFMEMORY problem,
"There is no secret ingredient.." - Po (Kung Fu Panda)
MJP
MJP

Additionally, every time i call map here, the D3D debug output says "Error:......Resource is already mapped" or something of the sort.


Well that definitely sounds like your problem. The driver is probably continuing to allocate hidden textures behind the scenes (since that's what they do to avoid stalling when updating dynamic resources), and not freeing them because you're not unmapping them.
Sturmh
Sturmh

[quote name='Sturmh' timestamp='1310669951' post='4835383']
Additionally, every time i call map here, the D3D debug output says "Error:......Resource is already mapped" or something of the sort.


Well that definitely sounds like your problem. The driver is probably continuing to allocate hidden textures behind the scenes (since that's what they do to avoid stalling when updating dynamic resources), and not freeing them because you're not unmapping them.
[/quote]

Sorry but I am still a little confused. How do I free these hidden textures haha. I'm not really sure where I should be looking to try and fix this. Could I be using incorrect parameters for the Map and Unmap calls. Also, could it be my code to render, even though I have pinpointed the "Resource already mapped" error. I'm just stumped haha.
"There is no secret ingredient.." - Po (Kung Fu Panda)
Sturmh
Sturmh
To expand on what I said previously, could I be using the Subresource parameter of the map call incorrectly? I dont really understand what that corresponds to. I dont know if this is correct but I am using the same subresource parameter when mapping the buffer to the texture as when I map my constant buffer for the vertex shader. This is the only other map call in the program. I dont know if posting this code will be any help, but it wont hurt:




//Lock constant buffer to write to it
HR(deviceContext->Map(m_matrixBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResources));

//Get point to data in constant buffer
matrixBuffer = (MatrixBuffer*)mappedResources.pData;

matrixBuffer->world = world;
matrixBuffer->view = view;
matrixBuffer->proj = proj;

//Unlock constant buffer after done writing to it
deviceContext->Unmap(m_matrixBuffer, 0);

//Set contant buffer in the vertex shader with new values
deviceContext->VSSetConstantBuffers(0, 1, &m_matrixBuffer);

//Set shader texture resource in the pixel shader
deviceContext->PSSetShaderResources(0, 1, &m_textureRV);



Edit:
Hmm, I just noticed when I comment out the map call to update the texture I still get:
D3D11: ERROR: ID3D11DeviceContext::Map: This resource is already mapped! [ RESOURCE_MANIPULATION ERROR #2097213: RESOURCE_MAP_ALREADYMAPPED ]

This also happens when I comment out the map call for updating the constant buffer and running the map call updating the texture;

I would seem all my attempts to use the map function are not working out to well. Some additional understanding of how to properly use of the map function, if I am indeed using it incorrectly, would be greatly appreciated.
"There is no secret ingredient.." - Po (Kung Fu Panda)
Sturmh
Sturmh
I changed up the program to see if some other part of my code was indirectly causing the mapping issue. Now, all I am doing is initializing D3D then calling this

//Fill out texture desc
D3D11_TEXTURE2D_DESC td;
td.ArraySize = 1;
td.BindFlags = D3D11_BIND_SHADER_RESOURCE;
td.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
td.Format = DXGI_FORMAT_B8G8R8A8_UNORM;
td.Height = 500;
td.MipLevels = 1;
td.MiscFlags = 0;
td.SampleDesc.Count = 1;
td.SampleDesc.Quality = 0;
td.Usage = D3D11_USAGE_DYNAMIC;
td.Width = 500;

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

D3D11_MAPPED_SUBRESOURCE mappedResource;
HR(dc->Map(m_texture, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource));

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

dc->Unmap(m_texture, 0);

And I am getting the "already mapped error." Now I am really confused. How can it be already mapped if this the first and only map call I am making. To quote msdn for initializing a dynamic texture


Dynamic Usage
To fill a dynamic texture (one created with D3D11_USAGE_DYNAMIC):

  1. Get a pointer to the texture memory by passing in D3D11_MAP_WRITE_DISCARD when calling ID3D11DeviceContext::Map.
  2. Write data to the memory.
  3. Call ID3D11DeviceContext::Unmap when you are finished writing data

[/Quote]

Thats exactly what you guys are telling me and what I am doing haha.
Could it be possible that I am initializing my devices incorrectly?

Edit:
Tried it on a different computer and also got out of memory error, so pretty obvious now that im doing something wrong haha.
"There is no secret ingredient.." - Po (Kung Fu Panda)
Sturmh
Sturmh
Well I figured out what the problem was. It was my HR macro that I was using around the map call.




#define HR(x) \
{ \
HRESULT hr = (x); \
if(FAILED(x)) \
{ \
DXTrace(__FILE__, (DWORD)__LINE__, hr, L#x, true); \
} \
}


No Idea why this was causing the issue with map, but everything seems to work once i removed it.
"There is no secret ingredient.." - Po (Kung Fu Panda)

Topic Locked

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

Sign in to reply to this topic.