1

所以我的问题是我有一个叫做“lightbuffer”的缓冲区,里面有一堆东西,我希望能够修改它的一个或多个元素,而不必再次重写整个东西。具体来说,如果我这样做:

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”,所以光缓冲区的其余部分不会搞砸,但 FAILED(result) 返回为真,所以映射不起作用。我在 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; } 

那么我需要做什么才能修改缓冲区的元素而无需再次重写整个内容?

4

1 回答 1

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 的struct 需要匹配16bytes,这会造成很多麻烦。因此是填充属性。

于 2013-11-22T10:08:16.193 回答