0
\$\begingroup\$

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; }

I've added the follow structure to the C# code;

[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct QuadShaderValues
{
    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 =
            AddDisposable(new SharpDX.Direct3D11.Buffer(direct3DDevice, SharpDX.Utilities.SizeOf<QuadShaderValues>(),
                SharpDX.Direct3D11.ResourceUsage.Default, 
                SharpDX.Direct3D11.BindFlags.ConstantBuffer,
                SharpDX.Direct3D11.CpuAccessFlags.None,
                SharpDX.Direct3D11.ResourceOptionFlags.None, 0));

        direct3Dcontext.VertexShader.SetConstantBuffer(0, m_ShaderValueBuffer);

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.

\$\endgroup\$
0
\$\begingroup\$

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
    float padding; // 1 float
};

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.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ 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? \$\endgroup\$ – Justin William Stanley Bryant Apr 15 '17 at 1:49
  • \$\begingroup\$ Yep, I was having a senior moment there. \$\endgroup\$ – user96556 Oct 24 '17 at 17:47
0
\$\begingroup\$

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>
/// Shader Values Struct used to pass values to shader
/// </summary>
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct ShaderValues 
{
    /// <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;
}
| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

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

I read

direct3Dcontext.VertexShader.SetConstantBuffer(0, m_ShaderValueBuffer);

.. which should be:

direct3Dcontext.VertexShader.SetConstantBuffer(0, m_ShaderValueBuffer);
direct3Dcontext.PixelShader.SetConstantBuffer(0, m_ShaderValueBuffer);

I ran into the same this afternoon. Putting value for light direction into the buffer, HLSL pixel shader read (0,0,0) instead, no exception occurred. This solved it.

| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.