• Advertisement
  • Popular Tags

  • Popular Now

  • Advertisement
  • Similar Content

    • By Jiraya
      For a 2D game, does using a float2 for position increases performance in any way?
      I know that in the end the vertex shader will have to return a float4 anyway, but does using a float2 decreases the amount of data that will have to be sent from the CPU to the GPU?
    • By ucfchuck
      I am feeding in 16 bit unsigned integer data to process in a compute shader and i need to get a standard deviation.
      So I read in a series of samples and push them into float arrays
      float vals1[9], vals2[9], vals3[9], vals4[9]; int x = 0,y=0; for ( x = 0; x < 3; x++) { for (y = 0; y < 3; y++) { vals1[3 * x + y] = (float) (asuint(Input1[threadID.xy + int2(x - 1, y - 1)].x)); vals2[3 * x + y] = (float) (asuint(Input2[threadID.xy + int2(x - 1, y - 1)].x)); vals3[3 * x + y] = (float) (asuint(Input3[threadID.xy + int2(x - 1, y - 1)].x)); vals4[3 * x + y] = (float) (asuint(Input4[threadID.xy + int2(x - 1, y - 1)].x)); } } I can send these values out directly and the data is as expected

      Output1[threadID.xy] = (uint) (vals1[4] ); Output2[threadID.xy] = (uint) (vals2[4] ); Output3[threadID.xy] = (uint) (vals3[4] ); Output4[threadID.xy] = (uint) (vals4[4] ); however if i do anything to that data it is destroyed.
      If i add a
      vals1[4] = vals1[4]/2; 
      or a
      vals1[4] = vals[1]-vals[4];
      the data is gone and everything comes back 0.
      How does one go about converting a uint to a float and performing operations on it and then converting back to a rounded uint?
    • By fs1
      I have been trying to see how the ID3DInclude, and how its methods Open and Close work.
      I would like to add a custom path for the D3DCompile function to search for some of my includes.
      I have not found any working example. Could someone point me on how to implement these functions? I would like D3DCompile to look at a custom C:\Folder path for some of the include files.
    • By stale
      I'm continuing to learn more about terrain rendering, and so far I've managed to load in a heightmap and render it as a tessellated wireframe (following Frank Luna's DX11 book). However, I'm getting some really weird behavior where a large section of the wireframe is being rendered with a yellow color, even though my pixel shader is hard coded to output white. 

      The parts of the mesh that are discolored changes as well, as pictured below (mesh is being clipped by far plane).

      Here is my pixel shader. As mentioned, I simply hard code it to output white:
      float PS(DOUT pin) : SV_Target { return float4(1.0f, 1.0f, 1.0f, 1.0f); } I'm completely lost on what could be causing this, so any help in the right direction would be greatly appreciated. If I can help by providing more information please let me know.
    • By evelyn4you
      i try to implement voxel cone tracing in my game engine.
      I have read many publications about this, but some crucial portions are still not clear to me.
      At first step i try to emplement the easiest "poor mans" method
      a.  my test scene "Sponza Atrium" is voxelized completetly in a static voxel grid 128^3 ( structured buffer contains albedo)
      b. i dont care about "conservative rasterization" and dont use any sparse voxel access structure
      c. every voxel does have the same color for every side ( top, bottom, front .. )
      d.  one directional light injects light to the voxels ( another stuctured buffer )
      I will try to say what i think is correct ( please correct me )
      GI lighting a given vertecie  in a ideal method
      A.  we would shoot many ( e.g. 1000 ) rays in the half hemisphere which is oriented according to the normal of that vertecie
      B.  we would take into account every occluder ( which is very much work load) and sample the color from the hit point.
      C. according to the angle between ray and the vertecie normal we would weigth ( cosin ) the color and sum up all samples and devide by the count of rays
      Voxel GI lighting
      In priciple we want to do the same thing with our voxel structure.
      Even if we would know where the correct hit points of the vertecie are we would have the task to calculate the weighted sum of many voxels.
      Saving time for weighted summing up of colors of each voxel
      To save the time for weighted summing up of colors of each voxel we build bricks or clusters.
      Every 8 neigbour voxels make a "cluster voxel" of level 1, ( this is done recursively for many levels ).
      The color of a side of a "cluster voxel" is the average of the colors of the four containing voxels sides with the same orientation.

      After having done this we can sample the far away parts just by sampling the coresponding "cluster voxel with the coresponding level" and get the summed up color.
      Actually this process is done be mip mapping a texture that contains the colors of the voxels which places the color of the neighbouring voxels also near by in the texture.
      Cone tracing, howto ??
      Here my understanding is confus ?? How is the voxel structure efficiently traced.
      I simply cannot understand how the occlusion problem is fastly solved so that we know which single voxel or "cluster voxel" of which level we have to sample.
      Supposed,  i am in a dark room that is filled with many boxes of different kind of sizes an i have a pocket lamp e.g. with a pyramid formed light cone
      - i would see some single voxels near or far
      - i would also see many different kind of boxes "clustered voxels" of different sizes which are partly occluded
      How do i make a weighted sum of this ligting area ??
      e.g. if i want to sample a "clustered voxel level 4" i have to take into account how much per cent of the area of this "clustered voxel" is occluded.
      Please be patient with me, i really try to understand but maybe i need some more explanation than others
      best regards evelyn
  • Advertisement
  • Advertisement
Sign in to follow this  

DX11 D3D11 Constant Buffer not being sent correctly!

This topic is 1886 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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(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!

Share this post

Link to post
Share on other sites

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

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

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement