2013-11-22 131 views
1

所以我的問題是我有一個名爲「lightbuffer」的緩衝區,它有一堆東西在裏面,我希望能夠修改它的一個或多個元素,重寫整個事情。具體來說,如果我做的:在DirectX,C++中映射緩衝區

result = devicecontext->Map(lightbuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource); 
if(FAILED(result)) { die("map lightbuffer"); } 
dataPtr2 = (dxapp::LightBufferType*)mappedResource.pData; 
dataPtr2->diffuse = diffuse; 
devicecontext->Unmap(lightbuffer, 0); 

此代碼幾乎刪除除了因爲我用「D3D11_MAP_WRITE_DISCARD」「漫」的一切。我嘗試使用「D3D11_MAP_WRITE」,所以其餘的lightbuffer不會搞砸,但FAILED(result)返回爲true,所以映射不起作用。我讀的MSDN,我需要在緩衝器描述使用「D3D11_CPU_ACCESS_WRITE」如果我想這樣做,但我做的,我的緩衝器描述如下:

lightBufferDesc.Usage = D3D11_USAGE_DYNAMIC; 
lightBufferDesc.ByteWidth = sizeof(LightBufferType); 
lightBufferDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER; 
lightBufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE; 
lightBufferDesc.MiscFlags = 0; 
lightBufferDesc.StructureByteStride = 0; 
result = mydevice->CreateBuffer(&lightBufferDesc, NULL, &mylightbuffer); 
if(FAILED(result)) { return false; } 

那麼,我需要做的是能夠修改緩衝區的元素而不用重寫整個事物?

+0

啓用[DirectX調試圖層](http://msdn.microsoft.com/en-us/library/windows/desktop/jj200584%28v=vs.85%29.aspx)並從其輸出中發佈消息。 – Drop

回答

0

我不太確定,如果我理解正確。 你想使用一個緩衝區描述並創建一些緩衝區嗎?像這樣:

bufferDesc.Usage = D3D11_USAGE_DYNAMIC; 
bufferDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER; 
bufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE; 
bufferDesc.MiscFlags = 0; 
bufferDesc.StructureByteStride = 0; 
bufferDesc.ByteWidth = sizeof(matrixBufferType); 

// create matrixBuffer 
result = device->CreateBuffer(&bufferDesc, NULL, &m_matrixBuffer); 
if(FAILED(result)){ return false; } 

// Change the description for the smoothing dynamic constant buffer. 
bufferDesc.ByteWidth = sizeof(smoothingBufferType); 
result = device->CreateBuffer(&bufferDesc, NULL, &m_smoothingBuffer); 
if(FAILED(result)){ return false; } 

// and so on 

或者你想綁定一些其他的數據到你的dataPtr2?像:

result = deviceContext->Map(m_lightBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource); 
if(FAILED(result)){ return false; } 

dataPtr2= (lightBufferType*)mappedResource.pData; 

dataPtr2->ambientColor = ambientColor; 
dataPtr2->diffuseColor = diffuseColor; 
dataPtr2->lightDirection = lightDirection; 
lightDataPtr->padding = 0.0f; 

deviceContext->Unmap(m_lightBuffer, 0); 

dataPtr2的結構需要匹配16字節,這可能會導致很多麻煩。因此是填充屬性。

+1

實際上,我只是希望能夠使用D3D11_MAP_WRITE而不是D3D11_MAP_WRITE_DISCARD,但由於某種原因,它不起作用 – CivDav

+0

ja如果我使用MAP_WRITE它不工作太...而且我也把cpuAccess放到D3D11_CPU_ACCESS_WRITE。 ..它與D3D11_MAP_WRITE_DISCARD一起工作嗎?是否有任何理由從WRITE_DISCARD改爲WRITE? – Jinxi