How to make portable & agnostic compiler glBufferData?

This question asks if it can rely on the compiler to not be confused with the order and indentation of values struct.

In accordance with the answer to this question

OpenGL very clearly defines what an interface byte layout is std140.

C ++ 11 defines the concept of "standard layout types . "

The only thing C ++ says about standard layout types with respect to layout is that empty base classes are ignored (as long as it remains a standard layout) and that the first NSDM will be at the very beginning of the class. That is, there will never be a gasket on the front panel.

Another thing is that the standard says that NSDMs of the same access class will be distributed in order, and later ones with greater offsets than the previous ones.

But as for the C ++ standard. [class.mem] / 13 states that implementations may add additions between members for various reasons.

This possible, but not always existing addition can really mess things up, and the worst part is the compiler.


To avoid mistakes and nightmares, is not it better to use a compromise-agnostic approach?

For instance:

class BufferData
{
private:
    GLfloat data[12];

public:
    GLfloat* getCameraPosition()
    {
        return (GLfloat*) &data[0];
    }
    GLfloat* getLightPosition()
    {
        return (GLfloat*) &data[4];
    }
    GLfloat* getLightDiffuse()
    {
        return (GLfloat*) &data[8];
    }
    GLfloat* getData()
    {
        return data;
    }
};

Unlike the naive:

struct BufferData
{
    GLfloat camera_position[4];
    GLfloat light_position[4];
    GLfloat light_diffuse[4];
};

Or is the naive approach good enough?

(Assume that the / struct class has more than just that and can change)

+4
source share
1 answer

" "? . . :

GLfloat data[12];

GLfloat. , ++ . OpenGL .

OpenGL : IEEE-754 BINARY32.

, ++ , float . , ++ , - . float - IEEE-754, .

, OpenGL GLfloat 32 , float IEEE-754. , ... 32- .

9- . 18- . ++. , 32 .

32- ( 16- 8-) OpenGL. - -. ++ .

, OpenGL glVertexAttribPointer. , void*, .

++ , . ++ , , , , ( , ptr- > int- > ptr , ).

OpenGL . ( , ), OpenGL, , undefined.

OpenGL GLint 32- . ++ , - .

OpenGL .

OpenGL flat out . 9- . , . , , .

OpenGL (Vulkan , ), , . , , , , , , ?

; .

+1

Source: https://habr.com/ru/post/1648201/


All Articles