• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Antigroup

Updating constant buffers corrupting some of the data

5 posts in this topic

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?

0

Share this post


Link to post
Share on other sites

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.

2

Share this post


Link to post
Share on other sites

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?

0

Share this post


Link to post
Share on other sites
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
0

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites

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

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0