Jump to content

  • Log In with Google      Sign In   
  • Create Account


Updating constant buffers corrupting some of the data


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
5 replies to this topic

#1 Antigroup   Members   -  Reputation: 104

Like
0Likes
Like

Posted 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
{
private:
	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;
public:
	LitEffect(void);
	LitEffect(ID3D11Device * device);
	~LitEffect(void);
	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)
{
	if(perObjectChanged)
	{
		D3D11_MAPPED_SUBRESOURCE sr = {0};
		
		//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;
	}

	if(perFrameChanged)
	{
		D3D11_MAPPED_SUBRESOURCE sr = {0};
		
		//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->IASetInputLayout(inputLayout);

	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?



Sponsor:

#2 Zaoshi Kaba   Crossbones+   -  Reputation: 3608

Like
2Likes
Like

Posted 12 May 2013 - 02:22 PM

In vertex shader you set Constant Buffers 0 and 1, while in PS it's 2 and 3, are you sure it shouldn't be 0 and 1? I think VS and PS use separate registers for these buffers, so it won't overwrite.



#3 Antigroup   Members   -  Reputation: 104

Like
0Likes
Like

Posted 12 May 2013 - 02:57 PM

I thought they should be able to use the same slots, but something in another post I read while trying to figure this out seemed to imply that you should use different slots, and when I used the same slots for both, I got a warning about the constant buffer being smaller than expected for the pixel shader.

 

I just tried using slots 0 and 1 on both again, and for some reason the pixel shader has the buffers in the opposite slots from the way the vertex shader has them, but it works not using this code.

 

context->VSSetConstantBuffers(1, 1, &perFrameConstantBuffer);
context->VSSetConstantBuffers(0, 1, &perObjectConstantBuffer);
context->VSSetShader(vertexShader, NULL, 0);

context->PSSetConstantBuffers(1, 1, &perObjectConstantBuffer);
context->PSSetConstantBuffers(0, 1, &perFrameConstantBuffer);
context->PSSetShader(pixelShader, NULL, 0);

 

 

I'm not sure how the slots for constant buffers are chosen. I thought they would be in the order declared in the shader files. Maybe I'll see how I can use reflection to figure that out rather than trial and error.

 

While I'm asking about constant buffers, when using xnamath matrices (with XMFLOAT4X4 for members), is there a reason for it to be necessary to transpose them before sending them to the constant buffer? I didn't see this mentioned anywhere, but a while back I found that not transposing them lead to very wrong results visually, but everything works fine when you transpose them. Does anybody know why this would be, or should I just keep doing it?



#4 NightCreature83   Crossbones+   -  Reputation: 2670

Like
0Likes
Like

Posted 12 May 2013 - 03:19 PM

Pixel shader and vertex shader constant buffers are seperate, I use this to set mine. You need to also make sure that in your shaders you are using cb0 and not set them on the vertex shader object by accident as that will give an error.
void Effect::setWVPContent( WVPBufferContent& wvpContent )
{
DeviceManager::getInstance().getDeviceContext()->UpdateSubresource( m_constantBuffers[0], 0, 0, (void*)&wvpContent, 0, 0);
}

void Effect::setMaterialContent( MaterialContent& materialContent )
{
DeviceManager::getInstance().getDeviceContext()->UpdateSubresource( m_constantBuffers[1], 0, 0, (void*)&materialContent, 0, 0);
}

//In the shader
cbuffer WVPConstants : register(cb0)
{
matrix World;
matrix View;
matrix Projection;
};

cbuffer MaterialConstants : register(cb0)
{
float4 ambient;
float4 diffuse;
float4 specular;
float4 emissive;
float shininess;
};
The error is actually in your code after seeing your last message have a good look at the Pixel shader setup as you are setting your buffers in the oposite order from the Vertex Shader. If that is how you have setup your shaders and that is how they are bound to the registers this is fine otherwise flip them.

While I'm asking about constant buffers, when using xnamath matrices (with XMFLOAT4X4 for members), is there a reason for it to be necessary to transpose them before sending them to the constant buffer? I didn't see this mentioned anywhere, but a while back I found that not transposing them lead to very wrong results visually, but everything works fine when you transpose them. Does anybody know why this would be, or should I just keep doing it?

You either tell the DX11 runtime that your matrices are row order or you transpose them on the CPU so that the shader interprets them correctly. The flag you are interested in is D3DCOMPILE_PACK_MATRIX_ROW_MAJOR or D3DCOMPILE_PACK_MATRIX_COLUMN_MAJOR and should be passed to the D3DX11CompileFromFile function call.

Edited by NightCreature83, 12 May 2013 - 03:24 PM.

Worked on titles: CMR:DiRT2, DiRT 3, DiRT: Showdown, GRID 2, Mad Max

#5 Antigroup   Members   -  Reputation: 104

Like
0Likes
Like

Posted 12 May 2013 - 06:56 PM

Thanks. I'm not using D3DX, but I found the option for fxc to use different matrix packing.

 

I'm not sure how the order of the constant buffers is decided, but I got it to work as I'd expect it to using this code:

 

context->VSSetConstantBuffers(0, 1, &perFrameConstantBuffer);
context->VSSetConstantBuffers(1, 1, &perObjectConstantBuffer);
context->VSSetShader(vertexShader, NULL, 0);
	
context->PSSetConstantBuffers(0, 1, &perFrameConstantBuffer);
context->PSSetConstantBuffers(1, 1, &perObjectConstantBuffer);
context->PSSetShader(pixelShader, NULL, 0);

In the shader files, the buffers are the exact same:

cbuffer PerFrameConstantBuffer : register(b0)
{
	float4 lightColor;
	float4 lightAmbientColor;
	float4 lightDirection;
	float4 cameraPosition;
};

cbuffer PerObjectConstantBuffer : register(b1)
{
	float4x4 world;
	float4x4 wvp;
	float4 materialDiffuse;
	float4 materialSpecular;
	float4 materialAmbient;
};


 

This works, I'm just not sure why, but I guess I'll figure it out eventually.

 

Thanks for the help.



#6 NightCreature83   Crossbones+   -  Reputation: 2670

Like
0Likes
Like

Posted 13 May 2013 - 01:44 AM

context->VSSetConstantBuffers(1, 1, &perFrameConstantBuffer);
context->VSSetConstantBuffers(0, 1, &perObjectConstantBuffer);
context->VSSetShader(vertexShader, NULL, 0);

context->PSSetConstantBuffers(1, 1, &perObjectConstantBuffer);
context->PSSetConstantBuffers(0, 1, &perFrameConstantBuffer);
context->PSSetShader(pixelShader, NULL, 0);

 

 

context->VSSetConstantBuffers(0, 1, &perFrameConstantBuffer);
context->VSSetConstantBuffers(1, 1, &perObjectConstantBuffer);
context->VSSetShader(vertexShader, NULL, 0);
	
context->PSSetConstantBuffers(0, 1, &perFrameConstantBuffer);
context->PSSetConstantBuffers(1, 1, &perObjectConstantBuffer);
context->PSSetShader(pixelShader, NULL, 0);

This works because you are now setting the perFrameCB and the perObjectCB on the same registers in the pixel and vertex shader. Have a look at the code above and you will see what I mean, this is something stupid but without help you wouldn't have found this as you have been staring at this for a while. We all have these moments as programmers and if you'd had someone else there coding with you as soon as you asked for help you would have spotted this, as you would have had to explain the code to someone else. To do this on your own put a figurine or something in front of you and explain the situation to them it helps :)


Worked on titles: CMR:DiRT2, DiRT 3, DiRT: Showdown, GRID 2, Mad Max




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS