1

So my problem is that I have a buffer called the "lightbuffer", which has a bunch of stuff in it, and I want to be able to modify one or more elements of it, without having to rewrite the whole thing again. Specifically if I do:

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);

This code pretty much deletes everything except "diffuse" because I used "D3D11_MAP_WRITE_DISCARD". I tried using "D3D11_MAP_WRITE", so the rest of the lightbuffer wouldn't get screwed up, but FAILED(result) returned as true, so the mapping didn't work. I read on MSDN that I need to use "D3D11_CPU_ACCESS_WRITE" in the buffer description if I want to do this, but I do, my buffer description is as follows:

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; } 

So what do I need to do to be able to modify elements of the buffer without rewriting the whole thing again?

CivDav
  • 102
  • 2
  • 11

1 Answers1

0

I'm not quite sure, if I understood correctly. do you want to use one buffer description and create some buffers out of that? Like that:

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

or do you want to bind some other data to your dataPtr2? like:

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);

the struct of dataPtr2 need to match 16bytes, that can cause a lot of troubles. therefore is the padding attribut.

Jinxi
  • 303
  • 1
  • 13
  • 1
    actually, I just want to be able to use D3D11_MAP_WRITE, instead of D3D11_MAP_WRITE_DISCARD, but for some reason it doesn't work – CivDav Nov 22 '13 at 10:23
  • ja if I trie MAP_WRITE it doesn;t work too... and I also put the cpuAccess to D3D11_CPU_ACCESS_WRITE... does it work with D3D11_MAP_WRITE_DISCARD? is there any reason to change from WRITE_DISCARD to WRITE? – Jinxi Nov 24 '13 at 11:51