0
\$\begingroup\$

I have a Direct3D 11 application and recently I've started to implement a new feature on it, the UI(User Interface).

It seems to work well, but I'm having one problem with optimization when it comes to moving stuff on my window, currently I can create a 2D square with textures and a 300x300 resolution.I can drag it around my screen by calling a function that updates the square position on my screen.

bool Model::UpdateModel2D(ID3D11DeviceContext* pDeviceContext, short NewX, short NewY)

{
if ((m_PreviousX == NewX) || (m_PreviousY == NewY)) // detects if the UI position is the same as before
    return true;
D3D11_MAPPED_SUBRESOURCE ms;
ModelData* pNewData;
ZeroMemory(&ms, sizeof(D3D11_MAPPED_SUBRESOURCE));

ModelData pData[6]; //Builds two triangles in order to form a square.

pData[0].VertexData = XMFLOAT3(NewX, NewY, 0.0f);
pData[0].TextureCoords = XMFLOAT2(0.0f, 0.0f);

pData[1].VertexData = XMFLOAT3(NewX + 300.0f, NewY + 300.0f, 0.0f);
pData[1].TextureCoords = XMFLOAT2(1.0f, 1.0f);

pData[2].VertexData = XMFLOAT3(NewX, NewY + 300, 0.0f);
pData[2].TextureCoords = XMFLOAT2(0.0f, 1.0f);

pData[3].VertexData = XMFLOAT3(NewX, NewY, 0.0f);
pData[3].TextureCoords = XMFLOAT2(0.0f, 0.0f);

pData[4].VertexData = XMFLOAT3(NewX + 300.0f, NewY, 0.0f);
pData[4].TextureCoords = XMFLOAT2(1.0f, 0.0f);

pData[5].VertexData = XMFLOAT3(NewX + 300.0f, NewY + 300.0f, 0.0f);
pData[5].TextureCoords = XMFLOAT2(1.0f, 1.0f);

hr = pDeviceContext->Map(m_pVertexBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &ms);
if (FAILED(hr))
    return false;

pNewData = (ModelData*)ms.pData;
memcpy(pNewData, pData, sizeof(ModelData) * m_NumVertices); //updates the position of the 2D square
pDeviceContext->Unmap(m_pVertexBuffer, 0);

return true;
}

To drag this square I have to left click on it and move my mouse, but this is an issue because my application gets too slow, and it looks like the longer I hold it on my mouse, the slower the application gets, until it gets released. If someone knows how to improve updating my buffers, please help me.

\$\endgroup\$
2
  • 3
    \$\begingroup\$ This question is more suited towards the Computer Graphics or Gamedev stackexchanges. Anyway, it may be possible that your app is reading the subresource data while it's being mapped. See here ("Don't read from a subresource mapped for writing") \$\endgroup\$ – Rish May 10 at 13:07
  • \$\begingroup\$ Oh, looks like it was actually an issue. I've changed the part where I'm reading the previous buffer data before updating and now I'm instead copying the new data directly over the old buffer, the performance increased pretty much by doing so. If you want, you can post here your answer and I'll give you the best answer. And thanks for providing these links, I'll use them if I have a related question in the future. \$\endgroup\$ – Rafael Ferreira May 11 at 0:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.