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 4001704
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T07:56:15+00:00 2026-05-20T07:56:15+00:00

I have the following code to instantiate a constantbuffer and bind it to the

  • 0

I have the following code to instantiate a constantbuffer and bind it to the pixel shader. ConstantBuffers.EveryFrame is a struct in the ConstantBuffers class which holds all of the data I need to update on a per frame basis (currently just a single Color3 object).

everyFrame is a Direct3D11 Buffer object which I use as my constant buffer. Context is my D3DDevice.ImmediateContext

int sizeInBytes;
ConstantBuffers.EveryFrame cb1 = new ConstantBuffers.EveryFrame();
cb1.Color = new Color3(0, 0, 0);
sizeInBytes = Marshal.SizeOf(typeof(ConstantBuffers.EveryFrame));
using (DataStream data = new DataStream(sizeInBytes, true, true))
{
    data.Write(cb1);
    data.Position = 0;
    everyFrame = new D3D.Buffer(device, data, new BufferDescription
    {
        Usage = ResourceUsage.Default,
        SizeInBytes = sizeInBytes,
        BindFlags = BindFlags.ConstantBuffer
    });
    context.PixelShader.SetConstantBuffer(everyFrame, 0);
}

When I run this code I get the following error:

SlimDX.Direct3D11.Direct3D11Exception was unhandled
  Message=E_INVALIDARG: An invalid parameter was passed to the returning function (-2147024809)
  Source=SlimDX
  StackTrace:
   at SlimDX.Result.Throw[T](Object dataKey, Object dataValue)
   at SlimDX.Result.Record[T](Int32 hr, Boolean failed, Object dataKey, Object dataValue)
   at SlimDX.Direct3D11.Buffer.Build(Device device, DataStream data, Int32 sizeInBytes, ResourceUsage usage, BindFlags bindFlags, CpuAccessFlags accessFlags, ResourceOptionFlags optionFlags, Int32 structureByteStride)
   at SlimDX.Direct3D11.Buffer..ctor(Device device, DataStream data, BufferDescription description)
   at VoxelGame.Form1.SetupConstantBuffers() in d:\files\my documents\visual studio 2010\Projects\VoxelGame\VoxelGame\Form1.cs:line 119
   at VoxelGame.Form1..ctor() in d:\files\my documents\visual studio 2010\Projects\VoxelGame\VoxelGame\Form1.cs:line 91
   at VoxelGame.Program.Main() in d:\files\my documents\visual studio 2010\Projects\VoxelGame\VoxelGame\Program.cs:line 21
   at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
   at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
   at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
   at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ThreadHelper.ThreadStart()
  InnerException: 

However when this following code runs (just a few calls before the above code) it works fine. This code doesn’t touch any of the variables the code above uses, and mostly operates in a separate class (the device is passed in through the class’ constructor, constantBuffer is a Direct3D11 Buffer stored in the class’ variables)

ConstantBuffers.EveryMotion cb2 = new ConstantBuffers.EveryMotion();
int sizeInBytes = Marshal.SizeOf(typeof(ConstantBuffers.EveryMotion));
using (DataStream data = new DataStream(sizeInBytes, true, true))
{
    data.Write(cb2);
    data.Position = 0;
    constantBuffer = new D3D.Buffer(device, data, new BufferDescription
    {
        Usage = ResourceUsage.Default,
        SizeInBytes = sizeInBytes,
        BindFlags = BindFlags.ConstantBuffer
    });
    device.ImmediateContext.VertexShader.SetConstantBuffer(constantBuffer, 0);
}

for reference here are the ConstantBuffer structs

[StructLayout(LayoutKind.Sequential)]
public struct EveryMotion
{
    public Matrix WorldViewProjection;
}

[StructLayout(LayoutKind.Sequential)]
public struct EveryFrame
{
    public Color3 Color;
}
  • 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-20T07:56:15+00:00Added an answer on May 20, 2026 at 7:56 am

    Constant buffers need their data aligned properly; the SizeInBytes property needs to be a multiple of 16, which is the case for a Matrix (4*4*4), but not the case for Color3 (4*3).

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

Sidebar

Related Questions

I have following Code Block Which I tried to optimize in the Optimized section
I'm trying to use opengl in C#. I have following code which fails with
I have the following code: $bind = new COM(LDAP://CN=GroupName,OU=Groups,OU=Division,DC=company,DC=local); When I execute it from
I have the following code to instantiate a JTable: the table comes up with
I have the following code in a class that is called from a web
I Have following code: Controller: public ActionResult Step1() { return View(); } [AcceptVerbs(HttpVerbs.Post)] public
I have the following code in a web.config file of the default IIS site.
I have the following code snippet. $items['A'] = Test; $items['B'] = Test; $items['C'] =
I have the following code: SELECT <column>, count(*) FROM <table> GROUP BY <column> HAVING
I have the following code: String inputFile = somefile.txt; FileInputStream in = new FileInputStream(inputFile);

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.