Sign in to follow this  

DX11 D3D11 Constant Buffer not being sent correctly!

Recommended Posts

Migi0027    4630

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 !




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;

	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)

		MessageBoxA(NULL, "The uber vertex shader creation has failed, program will now exit!", "ERROR", 0);
	if (!PS)

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

	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
	ZeroMemory(&bd, sizeof(bd));
	bd.Usage = D3D11_USAGE_DEFAULT;


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

	D3DXVECTOR4 LightVector;
	D3DXCOLOR LightColor;

	D3DXMATRIX Rotation;
	D3DXCOLOR AmbientColor;

	float _diffuse;
	float _texture;
	float _directionall;
	float _bumpmap;

	float _fog;
	D3DXVECTOR3 space5;

	D3DXMATRIX world;
	D3DXMATRIX projection;
	D3DXVECTOR4 EyePosition;
	D3DXVECTOR3 Forward;
	float space;

	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(,0.0),final);
		input.normal = mul(float4(,0.0),final);
		float3 binormal = cross(,;

		float3 L = float3(dot(input.tangent, light), dot(binormal,, dot(input.normal, light));
		float3 H = float3(dot(input.tangent, vhalf), dot(binormal,, 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!

Share this post

Link to post
Share on other sites
Zaoshi Kaba    8434

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.

Share this post

Link to post
Share on other sites
Tasche    222

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

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  

  • Partner Spotlight

  • Similar Content

    • By RubenRS
      How do i open an image to use it as Texture2D information without D3DX11CreateShaderResourceViewFromFile? And how it works for different formats like (JPG, PNG, BMP, DDS,  etc.)?
      I have an (512 x 512) image with font letters, also i have the position and texcoord of every letter. The main idea is that i want to obtain the image pixel info, use the position and texcoords to create a new texture with one letter and render it. Or am I wrong in something?
    • By thmfrnk
      I found a very interesting blog post here:
      However, I didn't really got how to use his "TestConeVsSphere" test in 3D (last piece of code on his post). I have the frustumCorners of a 2D Tile cell in ViewSpace and my 3D Cone Origin and Direction, so where to place the "testSphere"? I thought about to also move the Cone into viewspace and put the sphere to the Center of the Cell with the radius of half-cellsize, however what about depth? A sphere does not have inf depth?
      I am missing anything? Any Ideas?
      Thx, Thomas
    • By Modymek
      hi all
      I want to enable and disable shader in MPCH Media player Classic
      the MPCH have shader option using HLSL shaders
      I want the shader to read each file extension before it plays the file
      so if the video file name is video.GR.Mp4 it will play it in Grayscale shader 
      if it is not and standard file name Video.Mp4 without GR. unique extension so it plays standard without shader or end the shader
      here is the shader I have for grayscale
      // $MinimumShaderProfile: ps_2_0
      sampler s0 : register(s0);
      float4 main(float2 tex : TEXCOORD0) : COLOR {
          float c0 = dot(tex2D(s0, tex), float4(0.299, 0.587, 0.114, 0));
          return c0;
      I want to add if or block stantement or bloean to detect file name before it call the shader in order to go to the procedure or disable it or goto end direct without it
      any thoughts or help
    • By noodleBowl
      I've gotten to part in my DirectX 11 project where I need to pass the MVP matrices to my vertex shader. And I'm a little lost when it comes to the use of the constant buffer with the vertex shader
      I understand I need to set up the constant buffer just like any other buffer:
      1. Create a buffer description with the D3D11_BIND_CONSTANT_BUFFER flag 2. Map my matrix data into the constant buffer 3. Use VSSetConstantBuffers to actually use the buffer But I get lost at the VertexShader part, how does my vertex shader know to use this constant buffer when we get to the shader side of things
      In the example I'm following I see they have this as their vertex shader, but I don't understand how the shader knows to use the MatrixBuffer cbuffer. They just use the members directly. What if there was multiple cbuffer declarations like the Microsoft documentation says you could have?
      //Inside vertex shader cbuffer MatrixBuffer { matrix worldMatrix; matrix viewMatrix; matrix projectionMatrix; }; struct VertexInputType { float4 position : POSITION; float4 color : COLOR; }; struct PixelInputType { float4 position : SV_POSITION; float4 color : COLOR; }; PixelInputType ColorVertexShader(VertexInputType input) { PixelInputType output; // Change the position vector to be 4 units for proper matrix calculations. input.position.w = 1.0f; // Calculate the position of the vertex against the world, view, and projection matrices. output.position = mul(input.position, worldMatrix); output.position = mul(output.position, viewMatrix); output.position = mul(output.position, projectionMatrix); // Store the input color for the pixel shader to use. output.color = input.color; return output; }  
    • By gomidas
      I am trying to add normal map to my project I have an example of a cube: 
      I have normal in my shader I think. Then I set shader resource view for texture (NOT BUMP)
                  device.ImmediateContext.PixelShader.SetShaderResource(0, textureView);             device.ImmediateContext.Draw(VerticesCount,0); What should I do to set my normal map or how it is done in dx11 generally example c++?
  • Popular Now