Jump to content

  • Log In with Google      Sign In   
  • Create Account

[DX11] Constant buffer only updates a few variables?


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
4 replies to this topic

#1 a2ps   Members   -  Reputation: 139

Like
0Likes
Like

Posted 21 December 2011 - 12:41 PM

hi,

so im currently on tutorial 6 of the dx sdk (lightning), and im trying to adapt it to my little framework. problem is it seems that the constant buffer only updates a few variables (at least the world matrix) but doesnt update others. the problem is such that i've removed any lightning and i only want to output a solid color, and it still doesnt work. when i hard-code the color on the pixel shader it works, when i try to use a color from the constant buffer it doesnt work..

its even more weird because all my attempts at the previous tutorials worked just fine, even passing a color through the constant buffer worked fine.. but im tired of going through all my code and i cant seem to find anything wrong but it still outputs black (like the constant buffer color is == 0.0f).

ill try to post every relevant code

relevant structs:
// the normal vertex im using
struct NormalVertex
{
		XMFLOAT3 Pos;
		XMFLOAT3 Normal;
};

//my constant buffer:
struct ConstantBuffer
 {
		XMMATRIX World;
		XMMATRIX View;
		XMMATRIX Projection;

		XMFLOAT4 lightDir[2];
		XMFLOAT4 lightCol[2];

		XMFLOAT4 outputColor;
};

at the setup of my cube i create the constant buffer with an empty ConstantBuffer struct:
//create constant buffer:
array <ConstantBuffer, 1> tmpCB = { ConstantBuffer() };
inst.CreateBuffer(D3DEngine::Constant, tmpCB, &constantBuffer);

//the CreateBuffer method:
template <typename T, size_t Size>
void CreateBuffer(BufferType bufferType, const std::array <T, Size>& source, ID3D11Buffer** bufferOut)
{
		HRESULT hr = S_OK;

		D3D11_BUFFER_DESC bd;
		ZeroMemory(&bd, sizeof(bd));
		bd.Usage = D3D11_USAGE_DEFAULT;
		bd.ByteWidth = sizeof(T) * source.size();
		bd.BindFlags = bufferType;
		bd.CPUAccessFlags = 0;

 		D3D11_SUBRESOURCE_DATA initData;
 		ZeroMemory(&initData, sizeof(initData));
 		initData.pSysMem = source.data();

 		hr = device->CreateBuffer(&bd, &initData, bufferOut);
 		if(FAILED(hr))
 			throw std::exception("ID3D11Device::CreateBuffer() -> Failed.");
}

and this worked just fine when i tried a simple 2d triangle

and my render function for the cube:
ConstantBuffer cb;
cb.world = world;
cb.view = view;
cb.projection = projection;
cb.outputColor = XMFLOAT4(1.0f, 0.0f, 0.0f, 1.0f);

inst.UpdateBuffer(constantBuffer, cb);
inst.DrawRenderData(36); //36 vertices

//the update buffer method:
template <typename T>
void UpdateBuffer(ID3D11Buffer* buffer, const T& source)
{
		deviceContext->UpdateSubresource(buffer, 0, nullptr, &source, 0, 0);
}

i set all the buffers and shaders and etc to the deviceContext at the cube setup.

now my shader:
cbuffer ConstantBuffer : register(b0)
{
	matrix World;
	matrix View;
	matrix Projection;
	float4 lightDir[2];
	float4 lightCol[2];
	float4 outputColor;
};

struct VS_INPUT
{
	float4 Pos : POSITION;
	float3 Normal : NORMAL;
};

struct PS_INPUT
{
	float4 Pos : SV_POSITION;
	float3 Normal : NORMAL;
};

PS_INPUT VS(VS_INPUT input)
{
	PS_INPUT output = (PS_INPUT)0;
	output.Pos = mul(input.Pos, World);
	output.Pos = mul(output.Pos, View);
	output.Pos = mul(output.Pos, Projection);
	output.Normal = mul(input.Normal, World);

	return output;
}

float4 PS(PS_INPUT input) : SV_TARGET
{
	return outputColor;
}

i KNOW it updates a few constantbuffer variables because i can rotate the cube, so at least the world matrix is being updated but it draws the cube black..
if on the pixel shader i do: return float4(1.0f, 0.0f, 0.0f, 1.0f); it works!

this is driving me mad, if anyone can throw a tip ill really appreciate it.
thanks.
yet, another stupid signature..

Sponsor:

#2 BornToCode   Members   -  Reputation: 930

Like
1Likes
Like

Posted 21 December 2011 - 01:55 PM

Did you try to debug it with Pix and see what is store in your color.

Edited by phantom, 22 December 2011 - 11:45 AM.
Removing massive unneeded quote of first post


#3 a2ps   Members   -  Reputation: 139

Like
0Likes
Like

Posted 21 December 2011 - 03:00 PM

Did you try to debug it with Pix and see what is store in your color.

thanks for the answer.

yes i just debbuged it using PIX and it says that outputColor = float4(0.0f, 0.0f, 0.0f, 0.0f);
thats so weird because i dont even initialize it with that value let alone update the constant buffer with that value. i set it as (1.0f, 0.0f, 0.0f, 1.0f) but for some reason on the pixel shader its equal to 0 :\


EDIT: when i check the buffer on PIX it has the correct value! i've even recompiled with a different value and it shown on the buffer as correct.. but on the pixel shader it says the value is 0.

since the dx tutorial runs fine on my computer i dont think its a driver issue or something.. this is really frustrating.
yet, another stupid signature..

#4 kauna   Crossbones+   -  Reputation: 2571

Like
1Likes
Like

Posted 21 December 2011 - 03:27 PM

Let me take a guess : you haven't bound your constant buffer to pixel shader resources, only to the vertex shader resources.

You'll need to call PSSetConstantBuffers too on the same constant buffer.

Best regards!

[Edit] You should enable Direct3D debug output which would help you track these kind of issues

#5 a2ps   Members   -  Reputation: 139

Like
0Likes
Like

Posted 22 December 2011 - 09:23 AM

Let me take a guess : you haven't bound your constant buffer to pixel shader resources, only to the vertex shader resources.

You'll need to call PSSetConstantBuffers too on the same constant buffer.

Best regards!

[Edit] You should enable Direct3D debug output which would help you track these kind of issues

that was it, that solved it!

i had debug on on the dx control panel but the "filter info" checkbox was checked so it didint output any info (corrected now).
thank you so much for your help!
yet, another stupid signature..




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