Jump to content

  • Log In with Google      Sign In   
  • Create Account


Member Since 12 May 2013
Offline Last Active May 24 2013 12:53 PM

Topics I've Started

Updating constant buffers corrupting some of the data

12 May 2013 - 12:56 PM

I'm learning Direct3D 11 and I'm working on getting lighting to work, but for some reason the geometry I'm drawing is showing up black, and I've traced what seems to be the problem to the code that actually updates the constant buffers.


Here's the relevant code:


class LitEffect: public Effect
	static ID3D11InputLayout *inputLayout;
	static ID3D11VertexShader *vertexShader;
	static ID3D11PixelShader *pixelShader;
	static ID3D11Buffer *perFrameConstantBuffer;
	static ID3D11Buffer *perObjectConstantBuffer;
	struct PerFrameConstants
		XMFLOAT4 lightColor;
		XMFLOAT4 lightAmbientColor;
		XMFLOAT4 lightDirection;
		XMFLOAT4 cameraPosition;
	} perFrameCBuffer;
	bool perFrameChanged;
	struct PerObjectConstants
		XMFLOAT4X4 world;
		XMFLOAT4X4 wvp;
		XMFLOAT4 diffuseColor;
		XMFLOAT4 specularColor;
		XMFLOAT4 ambientColor;
	} perObjectCBuffer;
	bool perObjectChanged;
	LitEffect(ID3D11Device * device);
	void setWVP(XMFLOAT4X4 wvp);
	void setWorld(XMFLOAT4X4 world);
	void setMaterial(XMFLOAT4 diffuse, XMFLOAT4 specular, XMFLOAT4 ambient);
	void setLight(XMFLOAT4 color, XMFLOAT4 ambient, XMFLOAT4 direction);
	void setCameraPosition(XMFLOAT4 position);
	void apply(ID3D11DeviceContext * context);


Effect is just an abstract class that defines the apply(ID3D11DeviceContext*) method.

I'm using two constant buffers based on how often they should be updated.


The problem seems to be happening in the apply() method.

void LitEffect::apply(ID3D11DeviceContext * context)
		//context->Map(perObjectConstantBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &sr);
		////*((PerObjectConstants*)sr.pData) = perObjectCBuffer;
		//memcpy(sr.pData, &perObjectCBuffer, sr.RowPitch);
		//context->Unmap(perObjectConstantBuffer, 0);

		context->UpdateSubresource(perObjectConstantBuffer, 0, NULL, &perObjectCBuffer, sizeof(PerObjectConstants), sizeof(PerObjectConstants));

		perObjectChanged = false;

		//context->Map(perFrameConstantBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &sr);
		////*((PerFrameConstants*)sr.pData) = perFrameCBuffer;
		//memcpy(sr.pData, &perFrameCBuffer, sr.RowPitch);
		//context->Unmap(perFrameConstantBuffer, 0);

		context->UpdateSubresource(perFrameConstantBuffer, 0, NULL, &perFrameCBuffer, sizeof(PerFrameConstants), sizeof(PerFrameConstants));

		perFrameChanged = false;

	ID3D11Buffer *buffers[2];
	buffers[0] = perObjectConstantBuffer;
	buffers[1] = perFrameConstantBuffer;


	context->VSSetConstantBuffers(0, 2, buffers);
	context->VSSetShader(vertexShader, NULL, 0);

	context->PSSetConstantBuffers(2, 2, buffers);
	context->PSSetShader(pixelShader, NULL, 0);


From using the Visual Studio debugging tools, I've noticed that before calling Map() or UpdateSubresource(), the data in the constant buffer structs is as it should be. Afterwards, however, much of the data is changed to random really small floating-point values, but somehow the world-view-projection matrix is fine, and the geometry appears on screen, but black.


I've done plenty of checking on the shaders and they seem to work correctly, but the correct data never makes it to them.


Is there something I'm not understanding about using constant buffers?