Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How Does Vertex Buffer Description Read Input Data in DirectX 11?

I created a Math Struct that holds positions for vertex coordinates and am wondering how DirectX is able to read the members of the struct without knowing the names of the member values or being able to use them for input despite them being private.

Example:

//The values can be used for input despite being private
class Math3
{
public:
    Math3() {}
    Math3(float _x, float _y, float _z) : x(_x), y(_y), z(_z) {}
private:
    float x;
    float y;
    float z;
};

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

The private vs public only applies to C++ code and is enforced by the compiler. Instances of Math3 are just a block of 12 bytes in memory with no special hardware protections.

In other words, from an ‘in-memory’ perspective the Math3 is exactly the same if it’s:

class Math3
{
public:
    float x;
    float y;
    float z;
};

// this is the same as
struct Math3
{
    float x;
    float y;
    float z;
}

If you do a sizeof on the class, it’s the same.

A vertex buffer is just a block of memory. The GPU knows the data type, size, padding, etc. from the Input Layout description when you create the input layout.

const D3D11_INPUT_ELEMENT_DESC InputElements[] =
{
    { "SV_Position", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0 },
};

If you added a virtual method to your Math3 class, then the layout of the memory would change and it would no longer ‘just work’.

The Input Assembler takes the active Input Layout and uses it to parse the vertex information out of one or more vertex buffers. Therefore, it’s able to understand a variety of complex layouts and even merge from multiple buffers at runtime.

In the end, all that matters is that your C++ code uses the same ‘in-memory’ organization for vertex data as described by the input layout and that your Vertex Buffer itself respects that organization.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading