Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7014011
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T22:27:54+00:00 2026-05-27T22:27:54+00:00

I’m playing around with SlimDX (with DX10/11) a bit and I got some nice

  • 0

I’m playing around with SlimDX (with DX10/11) a bit and I got some nice results so far, but I’m stuck on passing an array of Vector4s to the shaders.

If I try the following struct:

[StructLayout(LayoutKind.Sequential)]
        public struct PerFrameBuffer
        {
            public Vector4 Position;
            public Matrix World;
            public Matrix View;
            public Matrix Projection;

            [MarshalAs( UnmanagedType.ByValArray, SizeConst=2)]
            public Vector4[] Lights;
        }

and try to store it in the following cbuffer:

cbuffer cPerFrame : register( b0 )
{

    float4 xPosition;
    matrix xWorld;
    matrix xView;
    matrix xProjection;

    float4 pointLights[2];
}

I don’t get any errors, but the values in the matrices are broken.

However when I declare my struct in c# as follows:

[StructLayout(LayoutKind.Sequential)]
        public struct PerFrameBuffer
        {
            public Vector4 Position;
            public Matrix World;
            public Matrix View;
            public Matrix Projection;
            public Vector4 Light1;
            public Vector4 Light2;
        }

Then everything works fine as expected.

I don’t really want to make 64 fields in the struct and fill them up like that (I could with reflection but still). How can I use an array to pass the Vector4’s to my constantbuffer?

If by any means this is a dumb idea and should be done in another way, please let me know. It’s my first time programming directx and shader related stuff.

To be complete:

Declaration of the buffer in C#

 cPerFrameBuffer = new SlimDX.Direct3D11.Buffer(device, new BufferDescription
            {
                Usage = ResourceUsage.Default,
                SizeInBytes = Marshal.SizeOf(typeof(PerFrameBuffer)),
                BindFlags = BindFlags.ConstantBuffer
            });
            context.VertexShader.SetConstantBuffer(cPerFrameBuffer, 0);

How I’m updating the buffer:

var cb = new AbstractDXManager.PerFrameBuffer()
   {
       Position = new Vector4(camera.Position, 1.0f),
       Projection = Matrix.Transpose(camera.ProjectionMatrix),
       View = Matrix.Transpose(camera.ViewMatrix),
       World = Matrix.Transpose(Matrix.Identity),

   };
   cb.Lights = new Vector4[2];
   cb.Lights[0] = cb.Position;
   cb.Lights[1] = new Vector4(20, 20, 20, 1);
   var context = device.ImmediateContext;

   using (DataStream data = new DataStream(Marshal.SizeOf(typeof(PerFrameBuffer)), true, true))
   {
       data.Write(cb);
       data.Position = 0;
       context.UpdateSubresource(new DataBox(0, 0, data), cPerFrameBuffer, 0);
   }

Thanks in advance

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T22:27:55+00:00Added an answer on May 27, 2026 at 10:27 pm

    I had the same problem. I changed buffer writing. Now it works

    This is my struct

    [StructLayout(LayoutKind.Sequential,Pack=1)]
    public struct WorldConstants
    {
        public Matrix View;
        public Matrix Projection;
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)]
        public Color4 [] DiffuseColor;
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)]
        public Color4[] AmbientColor;
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)]
        public Vector4[] LightDirection;
        public Vector4 ViewPosition;
    }
    

    I used StructToPtr to get the alligned data.

            using (DataStream stream = new DataStream(streamSize, true, true))
            {
                int rawsize = Marshal.SizeOf(typeof(ElementT));
                IntPtr buffer = Marshal.AllocHGlobal(rawsize);
                foreach (var element in elements)
                {
                    Marshal.StructureToPtr(element, buffer, false);
                    stream.WriteRange(buffer,rawsize);
                }
                Marshal.FreeHGlobal(buffer);
    

    Unfortunately this causes an additional copy of the data. In my case I didn’t notice any performance problems.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
This could be a duplicate question, but I have no idea what search terms
I have an array which has BIG numbers and small numbers in it. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.