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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T03:48:33+00:00 2026-06-18T03:48:33+00:00

I am passing in a constant buffer with the following layout: struct { float

  • 0

I am passing in a constant buffer with the following layout:

struct 
{
    float spread;
    D2D1_POINT_2F dimension;
    D2D1_POINT_2F dimension2;
} m_constants;

for debugging sake, dimension and dimension2 have the same values.

In the shader i have:

cbuffer constants
{
    float spread;
    float2 dimension;
    float2 dimension2;
};

float4 main(
    float4 pos      : SV_POSITION,
    float4 posScene : SCENE_POSITION,
    float4 uv0      : TEXCOORD0
    ) : SV_Target
{
    float width = dimension.x;
    float height = dimension.y;
    float2 uv2 = float2(posScene.x / width, posScene.y / height);
    color.rgb = float3(uv2.xy,  0);
    return color;
}

this, in theory, should output a gradient with green on the bottom left and red at the top right. And it does.
But if, in the shader i have the width and height to use dimension2 instead. i get a horizontal gradient from green on the left to yellow on the right.

Why is that? both dimensions have the same value when i passed the m_constants to the shader

  • 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-06-18T03:48:37+00:00Added an answer on June 18, 2026 at 3:48 am

    Constant buffers data is aligned by 16 bytes by default, so this means:

    cbuffer constants
    {
        float spread;
        float2 dimension;
        float2 dimension2;
    };
    

    will be

    cbuffer constants
    {
        float spread; // 4 bytes
        float2 dimension; // 4 + 8 = 12 bytes
        float dummy; //12+8 = 20, which means we cross 16 for dimension 2, hence a dummy 4 bytes element is added
        float2 dimension2;
    };
    

    here is a link that describes this.

    So a better way to arrange your structure would be:

    struct 
    {
        D2D1_POINT_2F dimension;
        D2D1_POINT_2F dimension2;
        float spread;
    } m_constants;
    

    and modify the hlsl counterpart accordingly:

    cbuffer constants
    {
        float2 dimension;
        float2 dimension2;
        float spread; // No more 16 bytes crossing problem
    };
    

    Another way, without modifying initial layout, in c++ side, either declare your structure like:

    #pragma pack(push)
    #pragma pack(16) 
    struct 
    {
        float spread;
        D2D1_POINT_2F dimension;
        D2D1_POINT_2F dimension2;     
    } m_constants;
    #pragma pack(pop)
    

    That will force structure to be 16 bytes aligned.

    You can also use /Zp16 compiler flag , but that will then apply to every structure in your program (which is not always desirable). In visual studio go to project properties -> c/c++ -> Code Generation, then you have option “Struct Member Alignment”, where you can set it from.

    You can also use packoffset on hlsl side, but then it means that the c++ layout needs to match the packed hlsl one (which means you keep same order in hlsl constant buffer, but still have to modify the c++ version).

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

Sidebar

Related Questions

people. I have a problem passing a float array to vertex shader (HLSL) through
I am having a hard time passing a string constant into an onchange event.
What's the best/most cannonical way of passing in a constant integer value to a
Here's a tip that makes some confusions for me:to have the best passing method
I have the following pattern. Or better the IDEA! There are unit classes inherited
Passing User defined argument to RPM is possible while installing?. for example: ~>rpm -i
passing post data using cURL requires that the name of the input. However, I
Passing in an Expression to a Linq query behaves differently depending on syntax used,
When passing a date from datepicker to sort an excel table, somewhere the format
when passing parameters after index.php its showing 404 error .. its apache error not

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.