Quantcast
Channel: Question and Answer » hlsl
Viewing all articles
Browse latest Browse all 69

marshaling c# struct with array to const buffer

$
0
0

I am trying to use a const buffer to pass a structure with an array of values into the pixel shader. However, all my color[] array values are coming in as zeros.
I have the struct defined as:

[StructLayout(LayoutKind.Explicit, Size = 1056)]
public struct MyColorMap
{
    public MyColorMap(float min, float max, Color4 nanColor)
    {
        Min = min;
        Max = max;
        NanColor = nanColor;
        ColoMap = new Color4[64];
    }

    [FieldOffset(0), MarshalAs(UnmanagedType.ByValArray, SizeConst = 64)]
    public Color4[] ColoMap;

    [FieldOffset(1024)]
    public Color4 NanColor;

    [FieldOffset(1040)]
    public float Min;

    [FieldOffset(1044)]
    public float Max;

    public static int Size
    {
        get { return Marshal.SizeOf(typeof(MyColorMap)); }
    }

}

The cbuffer in the HLSL matches as so:

cbuffer colormap :register(b1)
{
    float4 cmap[64];
    float4 nanColor;
    float cmapMin;  
    float cmapMax;      
};

In my C# code i initialize a structure and write some colors into the MyColorMap.Color map array. I pass this through by creating a Buffer and using Updatesubresource, but when I use any of the color map array values inside the pixel shader, they are all zeros, and I get a black screen.

I am getting some parts of the buffer through — the “nanColor” value is coming through fine, as are the min and max floats.

Any advice on how to pass an array of colors through to the pixel shader? (Or, hints what I am doing wrong).

TIA.


Viewing all articles
Browse latest Browse all 69

Trending Articles