D3D11 Constant Buffer not being sent correctly!

Started by
1 comment, last by Tasche 11 years, 1 month ago

Hi guys,

this error has currently been a pain in the ass for me, but I haven't given up yet! So basically I have a constant buffer called UberData which contains loads of floats (code below). In my shader I have around 5 buffers and all have the correct size. So there is a float called _texture, when i debug the UberBuffer, _texture is ALWAYS 1, but in the shader it is 821371982 (basically unset). This float is used as a bool for now. So i tried to change the if statement (code in hlsl) from if (_texture == 1) to if (1) , so it will execute it, and the code inside the statement works perfectly, but the buffer somehow isn't being set correctly!

One thing that is irritating me is this message:


D3D11: WARNING: ID3D11DeviceContext::DrawIndexed: The size of the Constant Buffer at slot 2 of the Pixel Shader unit is too small (32 bytes provided, 208 bytes, at least, expected). This is OK, as out-of-bounds reads are defined to return 0. It is also possible the developer knows the missing data will not be used anyway. This is only a problem if the developer actually intended to bind a sufficiently large Constant Buffer for what the shader expects.  [ EXECUTION WARNING #351: DEVICE_DRAW_CONSTANT_BUFFER_TOO_SMALL ]

I perfectly understand this, but I don't see where (in my code) I'm doing it wrong, though it will probably be a simple mistake biggrin.png !

Code:

Creation of Uber Shader


BOOL UberShaderObject::Create( ID3D11Device *&dev )
{
	string finals = string(
		"C:\\Users\\Utilizador\\Documents\\Visual Studio 2012\\Projects\\Cube3D\\Product\\Bin32\\") + string("UberShader.hlsl").c_str();

	ID3D10Blob *vserrors;
	ID3D10Blob *pserrors;
	HRESULT HR;

	D3DX11CompileFromFile(finals.c_str(), 0, 0, "VShader", "vs_5_0", D3DCOMPILE_DEBUG, 0, 0, &VS, &vserrors, &HR);
	D3DX11CompileFromFile(finals.c_str(), 0, 0, "PShader", "ps_5_0", D3DCOMPILE_DEBUG, 0, 0, &PS, &pserrors, &HR);
	
	// create the shader objects
	if (!VS)
	{
		DXTRACE_ERR(TEXT(DXGetErrorDescription(HR)),HR);

		MessageBoxA(NULL, "The uber vertex shader creation has failed, program will now exit!", "ERROR", 0);
		exit(0);
	}
	if (!PS)
	{
		DXTRACE_ERR(TEXT(DXGetErrorDescription(HR)),HR);

		MessageBoxA(NULL, "The uber pixel shader creation has failed, program will now exit!", "ERROR", 0);
		exit(0);
	}
	
	dev->CreateVertexShader(VS->GetBufferPointer(), VS->GetBufferSize(), NULL, &pVS);
	dev->CreatePixelShader(PS->GetBufferPointer(), PS->GetBufferSize(), NULL, &pPS);

	D3D11_INPUT_ELEMENT_DESC ied[] =
	{
		{"POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0},
		{"NORMAL", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0},
		{"TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0},
		{"TANGENT", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0},
	};
	
	if (dev->CreateInputLayout(ied, 4, VS->GetBufferPointer(), VS->GetBufferSize(), &pLayout) != S_OK)
		CE_WARNING("Input Layout Creation", "Input Layout creation in Uber Shader has failed!");

	pCBuffer = new ID3D11Buffer*[4]; // Object || Frame || Uber || Matrix || Fog

	// create the constant buffer
	D3D11_BUFFER_DESC bd;
	ZeroMemory(&bd, sizeof(bd));
	bd.Usage = D3D11_USAGE_DEFAULT;

	//---------------------------------------//
	
	bd.BindFlags = D3D11_BIND_CONSTANT_BUFFER;

	bd.ByteWidth = 144; // Object
	if(dev->CreateBuffer(&bd, NULL, &pCBuffer[0]) != S_OK)
		CE_WARNING("Buffer Creation", "Object Buffer creation in Uber Shader has failed!");

    bd.ByteWidth = 32; // Frame
	if(dev->CreateBuffer(&bd, NULL, &pCBuffer[1]) != S_OK)
		CE_WARNING("Buffer Creation", "Frame Buffer creation in Uber Shader has failed!");

	bd.ByteWidth = 32; // Uber
	if(dev->CreateBuffer(&bd, NULL, &pCBuffer[2]) != S_OK)
		CE_WARNING("Buffer Creation", "Uber Buffer creation in Uber Shader has failed!");
	
	bd.ByteWidth = 224; // Matrix
	if(dev->CreateBuffer(&bd, NULL, &pCBuffer[3]) != S_OK)
		CE_WARNING("Buffer Creation", "Matrix Buffer creation in Uber Shader has failed!");
	
	bd.ByteWidth = 32; // Fog
	if(dev->CreateBuffer(&bd, NULL, &pCBuffer[4]) != S_OK)
		CE_WARNING("Buffer Creation", "Fog Buffer creation in Uber Shader has failed!");
}

Sending Buffers:


void UberShaderObject::SendObjectBuffer(CBUFFER_PEROBJECT &bff, ID3D11DeviceContext *&devcon)
{
	devcon->UpdateSubresource(pCBuffer[0], 0, 0, &bff, NULL, NULL);
}

void UberShaderObject::SendFrameBuffer(CBUFFER_PERFRAME &bff, ID3D11DeviceContext *&devcon)
{
	devcon->UpdateSubresource(pCBuffer[1], 0, 0, &bff, NULL, NULL);
}

void UberShaderObject::SendUberBuffer(CBUFFER_UBERDATA &bff, ID3D11DeviceContext *&devcon)
{
	devcon->UpdateSubresource(pCBuffer[2], 0, 0, &bff, NULL, NULL);
}

void UberShaderObject::SendMatrixBuffer(CBUFFER_MATRIX &bff, ID3D11DeviceContext *&devcon)
{
	devcon->UpdateSubresource(pCBuffer[3], 0, 0, &bff, NULL, NULL);
}

void UberShaderObject::SendFogBuffer(CBUFFER_FOG &bff, ID3D11DeviceContext *&devcon)
{
	devcon->UpdateSubresource(pCBuffer[4], 0, 0, &bff, NULL, NULL);
}

Buffers in C++:


struct CBUFFER_PERFRAME
{
	D3DXVECTOR4 LightVector;
	D3DXCOLOR LightColor;
};

struct CBUFFER_PEROBJECT
{
	D3DXMATRIX Final;
	D3DXMATRIX Rotation;
	D3DXCOLOR AmbientColor;
};

struct CBUFFER_UBERDATA
{
	float _diffuse;
	float _texture;
	float _directionall;
	float _bumpmap;

	float _fog;
	D3DXVECTOR3 space5;
};

struct CBUFFER_MATRIX
{
	D3DXMATRIX world;
	D3DXMATRIX view;
	D3DXMATRIX projection;
	D3DXVECTOR4 EyePosition;
	D3DXVECTOR3 Forward;
	float space;
};

struct CBUFFER_FOG
{
	float FogStart;
	float FogEnd;
	D3DXVECTOR2 __space;
	D3DXVECTOR3 FogColor;
	float __space2;
};

Shader (Some code like bump mapping probably isn't working yet biggrin.png , the important part is the buffers!)


cbuffer ConstantObjectBuffer
{
	float4x4 final;
	float4x4 rotation;
	float4 ambientcol;
};

cbuffer ConstantFrameBuffer
{
	float4 lightvec;
	float4 lightcol;
};

cbuffer UberData
{
	float _diffuse;
	float _texture;
	float _directionall;
	float _bumpmap;

	float _fog;
	float3 space5;
};

cbuffer ConstantMatrixBuffer
{
	float4x4 world;
	float4x4 view;
	float4x4 projection;
	float4 eyepos;
	float3 cforward;
	float _space;
};

cbuffer ConstantFogBuffer
{
	float FogStart;
	float FogEnd;
	float2 __space;
	float3 FogColor;
	float __space2;
};

Texture2D txt;
Texture2D txt2;

SamplerState ss;

struct VOut
{
    float4 position : SV_POSITION;
    float4 worldPos : POSITION;
    float2 texcoord : TEXCOORD;
	float4 normal : NORMAL;
	float4 tangent : TANGENT;
	float Depth : FOG;
    float4 color : COLOR;
};

VOut VShader(float4 position : POSITION, float4 normal : NORMAL, float2 texcoord : TEXCOORD, float4 tangent : TANGENT)
{
    VOut output;
    output.position = mul(final, position);
    output.worldPos = mul(position, final);

	output.color = float4(0,0,0,1);

    // set the ambient light
	if (_diffuse == 1)
	{
		output.color += ambientcol;
	}

	if (_texture == 1)
	{
		output.texcoord = texcoord;
	}

	// Stuff for dir and bump
	if (_directionall == 1)
	{
		float4 norm = normalize(mul(rotation, normal));
		float diffusebrightness = saturate(dot(norm, lightvec));
		output.color += lightcol * diffusebrightness;
	}

	if (_bumpmap)
	{
		output.normal = normal;
		output.tangent = tangent;
	}

	// Final stuff
	output.Depth = output.position.z;
	output.color.a = 1;

    return output;
}

float4 PShader(VOut input) : SV_TARGET
{
	float4 color = input.color;

	float4 spec = float4(0,0,0, 1);
	float4 tex = float4(0,0,0, 1);
	float4 bump = float4(0,0,0, 1);

	if (_texture == 1)
	{
		color *= txt.Sample(ss, input.texcoord);
	}

	if (_directionall == 1)
	{
		float3 h = normalize(normalize(eyepos - input.position) - lightvec);
		float specLighting = pow(saturate(dot(h, input.normal)), 200);

		color += saturate(float4(1, 1, 1, 1) * float4(1, 1, 1, 1) * specLighting * 0.5);
	}

	if (_bumpmap== 1)
	{
		float4 light = normalize(input.position-lightvec);
		float4 eye = normalize(eyepos-light);
		float4 vhalf = normalize(eye-lightvec);

		float4 Normal = txt2.Sample(ss, input.texcoord);
		color *= dot(2.0*(Normal-0.5), 2.0*(color-0.5));

		input.tangent = mul(float4(input.tangent.xyz,0.0),final);
		input.normal = mul(float4(input.normal.xyz,0.0),final);
		float3 binormal = cross(input.normal.xyz,input.tangent.xyz);

		float3 L = float3(dot(input.tangent, light), dot(binormal, light.xyz), dot(input.normal, light));
		float3 H = float3(dot(input.tangent, vhalf), dot(binormal, vhalf.xyz), dot(input.normal, vhalf));

		float diffuse = dot(input.normal, L);
		float specular = dot(input.normal, H);
		specular = pow(specular, 2);

		bump = 2.0*(diffuse + specular);
	}

	color = saturate(color + spec);;
	
	if (_fog == 1)
	{
		float l = saturate((input.Depth - FogStart) / (FogEnd - FogStart));
		color = float4(lerp(color,FogColor, l), 1);
	}

	color.a = 1;

    return color;
}

Sorry for such a long post, but i kinda see you people as a last hope for this wink.png !

Thank You!

FastCall22: "I want to make the distinction that my laptop is a whore-box that connects to different network"

Blog about... stuff (GDNet, WordPress): www.gamedev.net/blog/1882-the-cuboid-zone/, cuboidzone.wordpress.com/

Advertisement

You don't specify registers for your buffers so they might be different order than you expect.

Error message says it expects 208 bytes, which is 64 + 64 + 16 = float4x4 + float4x4 + float4 = ConstantObjectBuffer. This buffer has index 2 for some reason, you probably thought it's going to be 0.

didn't go through all the code... so this may not be applicable

  • this is HLSL code as far as i can tell... use fxc.exe to test your shader code explicitly without having to feed it any info.at the top of the output it gives you info of what sizes the shader expects for all your constant buffers. fxc.exe is part of the directx sdk for windows 7 and below, and (afaik) part of the windows sdk as of windows 8.
  • make sure the sizes of your constant buffers in C++ match the sizes of the shaders'. easily done by making your struct mirror the cbuffer, and using sizeof() when mapping the memory.
  • make sure all your buffers are multiples of 16 (in bytes that is). i always found this restriction to be stupid, but probably has some performance reason.
  • make sure all buffers are actually created and mapped, send in dummy values if you have to.
  • make sure slot numbers are passed in correctly when calling the XXSetConstantBuffer() method, where XX is the appropriate shader stage.
  • make sure you set the buffers for the correct shader stage. (see your fxc output to find out what is expected where)

well this is a very general troubleshooting list, but running through these i usually solve all my constant buffer issues.

good luck,

Tasche =)

This topic is closed to new replies.

Advertisement