Sign in to follow this  
Sturmh

DX11 [Solved] DX11 Updating texture data

Recommended Posts

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:
[Code]
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));
[/Code]

which I then bind to a ShaderResourceView and set as a shader resource
[Code]
HR(device->CreateShaderResourceView(m_texture, 0, &m_textureRV));
.
.
deviceContext->PSSetShaderResources(0, 1, &m_textureRV);
[/Code]

I render this once and it works just as expected.
Then I try to update the texture..
[Code]
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);
[/Code]

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?

Share this post


Link to post
Share on other sites
[font="Arial"][size="2"]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][color="#666600"].[/color][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.[/color][/color][/size][/font]

Share this post


Link to post
Share on other sites
So I am trying to update my buffer and after copying my new texture data in, the texture is rendered with a shearing effect.

[Code]

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);
[/Code]

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.

Share this post


Link to post
Share on other sites
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:
[code]
BYTE* mappedData = reinterpret_cast<BYTE*>(mappedResource.pData);
for(UINT i = 0; i < height; ++i)
{
memcpy(mappedData, buffer, rowspan);
mappedData += mappedResource.RowPitch;
buffer += rowspan;
}
[/code]

Share this post


Link to post
Share on other sites
[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:
[code]
BYTE* mappedData = reinterpret_cast<BYTE*>(mappedResource.pData);
for(UINT i = 0; i < height; ++i)
{
memcpy(mappedData, buffer, rowspan);
mappedData += mappedResource.RowPitch;
buffer += rowspan;
}
[/code]
[/quote]

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.

Share this post


Link to post
Share on other sites
[size="3"][font="Calibri"][size="3"][font="Calibri"][/font][/size]

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][/size]

Share this post


Link to post
Share on other sites
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:

[size="2"][Code]deviceContext->Map(m_texture, 0, D3D11_MAP_WRITE_DISCARD], 0, &mappedResource)[/Code][/size]

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]

Share this post


Link to post
Share on other sites
[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:
[code]
BYTE* mappedData = reinterpret_cast<BYTE*>(mappedResource.pData);
for(UINT i = 0; i < height; ++i)
{
memcpy(mappedData, buffer, rowspan);
mappedData += mappedResource.RowPitch;
buffer += rowspan;
}
[/code]
[/quote]

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?

Share this post


Link to post
Share on other sites
[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:

[size="2"][Code]deviceContext->Map(m_texture, 0, D3D11_MAP_WRITE_DISCARD], 0, &mappedResource)[/Code][/size]

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]
[/quote]

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?

Share this post


Link to post
Share on other sites
[quote name='SnailCoder' timestamp='1310655297' post='4835277']
[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:
[code]
BYTE* mappedData = reinterpret_cast<BYTE*>(mappedResource.pData);
for(UINT i = 0; i < height; ++i)
{
memcpy(mappedData, buffer, rowspan);
mappedData += mappedResource.RowPitch;
buffer += rowspan;
}
[/code]
[/quote]

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.

Share this post


Link to post
Share on other sites
[quote name='MJP' timestamp='1310668864' post='4835371']
[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:

[size="2"][Code]deviceContext->Map(m_texture, 0, D3D11_MAP_WRITE_DISCARD], 0, &mappedResource)[/Code][/size]

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]
[/quote]

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:

[Code]
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);
[/Code]

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,

Share this post


Link to post
Share on other sites
[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.
[/quote]

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.

Share this post


Link to post
Share on other sites
Hidden
[quote name='MJP' timestamp='1310677463' post='4835435']
[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.
[/quote]

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]

I'm sorry but I am still confused haha. Like I said before every frame, I am only updating the texture and rendering it.
[Code]

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

BYTE* mappedData = reinterpret_cast<BYTE*>(mappedResource.pData);
unsigned char* keep = m_buffer.buffer;
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;
}
m_buffer.buffer = keep;
deviceContext->Unmap(m_texture, 0);
[/Code]
allocating resources and furthermore how do I figure out how to free it. Is it the second parameter of Unmap? Either way I'm stumped haha.

Share this post


Link to post
[quote name='MJP' timestamp='1310677463' post='4835435']
[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.
[/quote]

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.

Share this post


Link to post
Share on other sites
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:


[Code]

//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);
[/Code]


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.

Share this post


Link to post
Share on other sites
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
[Code]
//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);
[/Code]
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
[Quote]

[b]Dynamic Usage[/b]
To fill a dynamic texture (one created with D3D11_USAGE_DYNAMIC):

[list=1][*]Get a pointer to the texture memory by passing in D3D11_MAP_WRITE_DISCARD when calling [url="http://msdn.microsoft.com/en-us/library/ff476457%28v=vs.85%29.aspx"][b]ID3D11DeviceContext::Map[/b][/url].[*]Write data to the memory.[*]Call [url="http://msdn.microsoft.com/en-us/library/ff476485%28v=vs.85%29.aspx"][b]ID3D11DeviceContext::Unmap[/b][/url] when you are finished writing data[/list]
[/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.

Share this post


Link to post
Share on other sites
Well I figured out what the problem was. It was my HR macro that I was using around the map call.


[Code]

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

No Idea why this was causing the issue with map, but everything seems to work once i removed it.

Share this post


Link to post
Share on other sites
[quote name='Sturmh' timestamp='1311009253' post='4836886']
Well I figured out what the problem was. It was my HR macro that I was using around the map call.


[Code]

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

No Idea why this was causing the issue with map, but everything seems to work once i removed it.
[/quote]
Because [url="http://www.parashift.com/c++-faq-lite/inline-functions.html#faq-9.5"]macros are evil[/url]. Your macro was calling map() at least twice in that 'function' (i.e. it's not doing what you intuitively think it should be doing).

Cheers
Dark Sylinc

Share this post


Link to post
Share on other sites
Yeah your macro would expand to this if you looked at the preprocessed version:
[code]
HRESULT hr = deviceContext->Map(m_matrixBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResources);
if(FAILED(deviceContext->Map(m_matrixBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResources)))
{
DXTrace(__FILE__, (DWORD)__LINE__, hr, L#x, true);
}
[/code]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this  

  • Forum Statistics

    • Total Topics
      627708
    • Total Posts
      2978730
  • Similar Content

    • By Baemz
      Hello,
      I've been working on some culling-techniques for a project. We've built our own engine so pretty much everything is built from scratch. I've set up a frustum with the following code, assuming that the FOV is 90 degrees.
      float angle = CU::ToRadians(45.f); Plane<float> nearPlane(Vector3<float>(0, 0, aNear), Vector3<float>(0, 0, -1)); Plane<float> farPlane(Vector3<float>(0, 0, aFar), Vector3<float>(0, 0, 1)); Plane<float> right(Vector3<float>(0, 0, 0), Vector3<float>(angle, 0, -angle)); Plane<float> left(Vector3<float>(0, 0, 0), Vector3<float>(-angle, 0, -angle)); Plane<float> up(Vector3<float>(0, 0, 0), Vector3<float>(0, angle, -angle)); Plane<float> down(Vector3<float>(0, 0, 0), Vector3<float>(0, -angle, -angle)); myVolume.AddPlane(nearPlane); myVolume.AddPlane(farPlane); myVolume.AddPlane(right); myVolume.AddPlane(left); myVolume.AddPlane(up); myVolume.AddPlane(down); When checking the intersections I am using a BoundingSphere of my models, which is calculated by taking the average position of all vertices and then choosing the furthest distance to a vertex for radius. The actual intersection test looks like this, where the "myFrustum90" is the actual frustum described above.
      The orientationInverse is the viewMatrix in this case.
      bool CFrustum::Intersects(const SFrustumCollider& aCollider) { CU::Vector4<float> position = CU::Vector4<float>(aCollider.myCenter.x, aCollider.myCenter.y, aCollider.myCenter.z, 1.f) * myOrientationInverse; return myFrustum90.Inside({ position.x, position.y, position.z }, aCollider.myRadius); } The Inside() function looks like this.
      template <typename T> bool PlaneVolume<T>::Inside(Vector3<T> aPosition, T aRadius) const { for (unsigned short i = 0; i < myPlaneList.size(); ++i) { if (myPlaneList[i].ClassifySpherePlane(aPosition, aRadius) > 0) { return false; } } return true; } And this is the ClassifySpherePlane() function. (The plane is defined as a Vector4 called myABCD, where ABC is the normal)
      template <typename T> inline int Plane<T>::ClassifySpherePlane(Vector3<T> aSpherePosition, float aSphereRadius) const { float distance = (aSpherePosition.Dot(myNormal)) - myABCD.w; // completely on the front side if (distance >= aSphereRadius) { return 1; } // completely on the backside (aka "inside") if (distance <= -aSphereRadius) { return -1; } //sphere intersects the plane return 0; }  
      Please bare in mind that this code is not optimized nor well-written by any means. I am just looking to get it working.
      The result of this culling is that the models seem to be culled a bit "too early", so that the culling is visible and the models pops away.
      How do I get the culling to work properly?
      I have tried different techniques but haven't gotten any of them to work.
      If you need more code or explanations feel free to ask for it.

      Thanks.
       
    • By evelyn4you
      hi,
      i have read very much about the binding of a constantbuffer to a shader but something is still unclear to me.
      e.g. when performing :   vertexshader.setConstantbuffer ( buffer,  slot )
       is the buffer bound
      a.  to the VertexShaderStage
      or
      b. to the VertexShader that is currently set as the active VertexShader
      Is it possible to bind a constantBuffer to a VertexShader e.g. VS_A and keep this binding even after the active VertexShader has changed ?
      I mean i want to bind constantbuffer_A  to VS_A, an Constantbuffer_B to VS_B  and  only use updateSubresource without using setConstantBuffer command every time.

      Look at this example:
      SetVertexShader ( VS_A )
      updateSubresource(buffer_A)
      vertexshader.setConstantbuffer ( buffer_A,  slot_A )
      perform drawcall       ( buffer_A is used )

      SetVertexShader ( VS_B )
      updateSubresource(buffer_B)
      vertexshader.setConstantbuffer ( buffer_B,  slot_A )
      perform drawcall   ( buffer_B is used )
      SetVertexShader ( VS_A )
      perform drawcall   (now which buffer is used ??? )
       
      I ask this question because i have made a custom render engine an want to optimize to
      the minimum  updateSubresource, and setConstantbuffer  calls
       
       
       
       
       
    • By noodleBowl
      I got a quick question about buffers when it comes to DirectX 11. If I bind a buffer using a command like:
      IASetVertexBuffers IASetIndexBuffer VSSetConstantBuffers PSSetConstantBuffers  and then later on I update that bound buffer's data using commands like Map/Unmap or any of the other update commands.
      Do I need to rebind the buffer again in order for my update to take effect? If I dont rebind is that really bad as in I get a performance hit? My thought process behind this is that if the buffer is already bound why do I need to rebind it? I'm using that same buffer it is just different data
       
    • By Rockmover
      I am really stuck with something that should be very simple in DirectX 11. 
      1. I can draw lines using a PC (position, colored) vertices and a simple shader just fine.
      2. I can draw 3D triangles using PCN (position, colored, normal) vertices just fine (even transparency and SpecularBlinnPhong shaders).
       
      However, if I'm using my 3D shader, and I want to draw my PC lines in the same scene how can I do that?
       
      If I change my lines to PCN and pass them to the 3D shader with my triangles, then the lighting screws them all up.  I only want the lighting for the 3D triangles, but no SpecularBlinnPhong/Lighting for the lines (just PC). 
      I am sure this is because if I change the lines to PNC there is not really a correct "normal" for the lines.  
      I assume I somehow need to draw the 3D triangles using one shader, and then "switch" to another shader and draw the lines?  But I have no clue how to use two different shaders in the same scene.  And then are the lines just drawn on top of the triangles, or vice versa (maybe draw order dependent)?  
      I must be missing something really basic, so if anyone can just point me in the right direction (or link to an example showing the implementation of multiple shaders) that would be REALLY appreciated.
       
      I'm also more than happy to post my simple test code if that helps as well!
       
      THANKS SO MUCH IN ADVANCE!!!
    • By Reitano
      Hi,
      I am writing a linear allocator of per-frame constants using the DirectX 11.1 API. My plan is to replace the traditional constant allocation strategy, where most of the work is done by the driver behind my back, with a manual one inspired by the DirectX 12 and Vulkan APIs.
      In brief, the allocator maintains a list of 64K pages, each page owns a constant buffer managed as a ring buffer. Each page has a history of the N previous frames. At the beginning of a new frame, the allocator retires the frames that have been processed by the GPU and frees up the corresponding space in each page. I use DirectX 11 queries for detecting when a frame is complete and the ID3D11DeviceContext1::VS/PSSetConstantBuffers1 methods for binding constant buffers with an offset.
      The new allocator appears to be working but I am not 100% confident it is actually correct. In particular:
      1) it relies on queries which I am not too familiar with. Are they 100% reliable ?
      2) it maps/unmaps the constant buffer of each page at the beginning of a new frame and then writes the mapped memory as the frame is built. In pseudo code:
      BeginFrame:
          page.data = device.Map(page.buffer)
          device.Unmap(page.buffer)
      RenderFrame
          Alloc(size, initData)
              ...
              memcpy(page.data + page.start, initData, size)
          Alloc(size, initData)
              ...
              memcpy(page.data + page.start, initData, size)
      (Note: calling Unmap at the end of a frame prevents binding the mapped constant buffers and triggers an error in the debug layer)
      Is this valid ? 
      3) I don't fully understand how many frames I should keep in the history. My intuition says it should be equal to the maximum latency reported by IDXGIDevice1::GetMaximumFrameLatency, which is 3 on my machine. But, this value works fine in an unit test while on a more complex demo I need to manually set it to 5, otherwise the allocator starts overwriting previous frames that have not completed yet. Shouldn't the swap chain Present method block the CPU in this case ?
      4) Should I expect this approach to be more efficient than the one managed by the driver ? I don't have meaningful profile data yet.
      Is anybody familiar with the approach described above and can answer my questions and discuss the pros and cons of this technique based on his experience ? 
      For reference, I've uploaded the (WIP) allocator code at https://paste.ofcode.org/Bq98ujP6zaAuKyjv4X7HSv.  Feel free to adapt it in your engine and please let me know if you spot any mistakes
      Thanks
      Stefano Lanza
       
  • Popular Now