Skip to content

Instantly share code, notes, and snippets.

@marukun318
Last active February 12, 2018 14:16
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marukun318/f204846fb1a34ee47877bce0c460e77e to your computer and use it in GitHub Desktop.
Save marukun318/f204846fb1a34ee47877bce0c460e77e to your computer and use it in GitHub Desktop.
DirectX11 Bad Knouhow: VertexBuffer / IndexBuffer Write
ID3D11Buffer* m_pVertexBuffer;
ID3D11Buffer* m_pIndexBuffer;
//
D3D11_MAPPED_SUBRESOURCE msr = {};
HRESULT hr;
// vertices upload
hr = m_context->Map(m_pVertexBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &msr);
if (FAILED(hr))
return;
memcpy_s(msr.pData, sizeof(vtx_buf), vtx_buf, msr.RowPitch); // 頂点コピー msr.RowPitchで全体を書き換えないとまれに落ちる
m_context->Unmap(m_pVertexBuffer, 0);
// indices upload
D3D11_MAPPED_SUBRESOURCE ires = {};
hr = m_context->Map(m_pIndexBuffer 0, D3D11_MAP_WRITE_DISCARD, 0, &ires);
if (FAILED(hr))
return;
void* idxbuf = ires.pData;
if (idxbuf) {
// memcpy(idxbuf, idc_buf, sizeof(uint32_t) * m_idc_cnt); // 書き換えたインデックス数のコピーだとまれに落ちる
memcpy_s(idxbuf, sizeof(idc_buf), idc_buf, ires.RowPitch); // msr.RowPitchで全体を書き換えないと落ちる
m_context->Unmap(m_pIndexBuffer, 0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment