[DX11] Constant buffer only updates a few variables?

Started by
3 comments, last by a2ps 12 years, 3 months ago
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..
Advertisement
Did you try to debug it with Pix and see what is store in your color.

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..
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

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..

This topic is closed to new replies.

Advertisement