简体   繁体   中英

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;
};

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM