# SharpDX Constant buffer not passing values

I'm trying to pass values to a Pixel Shader. I've added the following to the HLSL files.

cbuffer QuadShaderValues : register(b0) { float3 PixelDropThresholds; float4 OverlayColor; uint PixelDropEnabled; }

[StructLayout(LayoutKind.Sequential, Pack = 1)]
{
public SharpDX.Vector3 PixelDropThresholds;
public SharpDX.Color4 OverlayColor;
public uint PixelDropEnabled;
}


I've added the following code to the Initialization of the device :

m_ShaderValueBuffer =
SharpDX.Direct3D11.ResourceUsage.Default,
SharpDX.Direct3D11.BindFlags.ConstantBuffer,
SharpDX.Direct3D11.CpuAccessFlags.None,
SharpDX.Direct3D11.ResourceOptionFlags.None, 0));



I've added the following code to the Render Method :

deviceContext.UpdateSubresource(ref ShaderValues, m_ShaderValueBuffer);


The values do not seem to be making it into the shader.

A couple of things, I think your pack for example needs to be 16. I usually cheat and shove a dummy float in after the vector3. I would also try to pack on borders of 16 bytes. Your current pack is to 1 byte alignment, possibly try 4 or 16.

[StructLayout(LayoutKind.Sequential)]
public struct PerFrameBuffer
{
public Matrix wvpMatrix; // 16 floats
public Matrix worldMatrix; // 16 floats
public Matrix shadowMatrix; // 16 floats
public Matrix decalMatrix; // 16 floats
public Vector3 cameraPosition; // 3 floats
};


I use the static buffer version of the call, don't think it makes a difference.

    D3D11.Buffer.Create(GlobalStatics.Device,
D3D11.BindFlags.ConstantBuffer,
ref Framebuffer,
Constants.c_PerFrameBufferSize,
D3D11.ResourceUsage.Default,
D3D11.CpuAccessFlags.None,
D3D11.ResourceOptionFlags.None,
0);


My only other questions would be if you are using the same context? That is, the context you are sending your render commands to is the same one you have bound your buffer to. Will update this post if anything else comes to mind, but buffer alignment might be your first issue.

• his vector 4 is crashing into his vector 3 its should be rearranged so the vector4 is first then the vector3 and then the uint that way you know there will be no tricky alignment problems going on or put the uint between the vector3 and vector4. Maybe add that to your answer too? – Justin William Stanley Bryant Apr 15 '17 at 1:49
• Yep, I was having a senior moment there. – user96556 Oct 24 '17 at 17:47

Hey thank you guys for your input. I had figured out how to get around my issue. I actually have added more values. Here is what I came up with. I actually switch to all float4.

/// <summary>
/// </summary>
[StructLayout(LayoutKind.Sequential, Pack = 1)]
{
/// <summary>
/// The overlay color (A,R,G,B)
/// </summary>
public SharpDX.Vector4 OverlayColor;
/// <summary>
/// The pixel drop thresholds(R,G,B, Enabled)
/// </summary>
public SharpDX.Vector4 PixelDropThresholds;
/// <summary>
/// The channel settings(Mixed)
/// </summary>
public SharpDX.Vector4 ChannelSettings;
/// <summary>
/// The renderer settings (Mixed)
/// </summary>
public SharpDX.Vector4 RendererSettings;
}


If you want to pass values to a pixel shader, make sure to activate your buffer for the pixel shader.

direct3Dcontext.VertexShader.SetConstantBuffer(0, m_ShaderValueBuffer);

direct3Dcontext.VertexShader.SetConstantBuffer(0, m_ShaderValueBuffer);