KWin
Loading...
Searching...
No Matches
Public Types | Public Member Functions | Static Public Member Functions | Static Public Attributes | List of all members
KWin::GLVertexBuffer Class Reference

Vertex Buffer Object. More...

#include <glvertexbuffer.h>

Public Types

enum  UsageHint { Dynamic , Static , Stream }
 

Public Member Functions

 GLVertexBuffer (UsageHint hint)
 
 ~GLVertexBuffer ()
 
void setAttribLayout (std::span< const GLVertexAttrib > attribs, size_t stride)
 
void setData (const void *data, size_t sizeInBytes)
 
void setVertexCount (int count)
 
template<std::ranges::contiguous_range T>
requires std::is_same<std::ranges::range_value_t<T>, GLVertex2D>::value
void setVertices (const T &range)
 
template<std::ranges::contiguous_range T>
requires std::is_same<std::ranges::range_value_t<T>, GLVertex3D>::value
void setVertices (const T &range)
 
template<std::ranges::contiguous_range T>
requires std::is_same<std::ranges::range_value_t<T>, QVector2D>::value
void setVertices (const T &range)
 
template<typename T >
std::optional< std::span< T > > map (size_t count)
 
void unmap ()
 
void bindArrays ()
 
void unbindArrays ()
 
void draw (GLenum primitiveMode, int first, int count)
 
void draw (const QRegion &region, GLenum primitiveMode, int first, int count, bool hardwareClipping=false)
 
void render (GLenum primitiveMode)
 
void render (const QRegion &region, GLenum primitiveMode, bool hardwareClipping=false)
 
void reset ()
 
void endOfFrame ()
 
void beginFrame ()
 

Static Public Member Functions

static void initStatic ()
 
static void cleanup ()
 
static bool supportsIndexedQuads ()
 
static GLVertexBufferstreamingBuffer ()
 

Static Public Attributes

static constexpr std::array GLVertex2DLayout
 
static constexpr std::array GLVertex3DLayout
 

Detailed Description

Vertex Buffer Object.

This is a short helper class to use vertex buffer objects (VBO). A VBO can be used to buffer vertex data and to store them on graphics memory.

Author
Martin Gräßlin mgrae.nosp@m.ssli.nosp@m.n@kde.nosp@m..org
Since
4.6

Definition at line 70 of file glvertexbuffer.h.

Member Enumeration Documentation

◆ UsageHint

Enum to define how often the vertex data in the buffer object changes.

Enumerator
Dynamic 

frequent changes, but used several times for rendering

Static 

No changes to data.

Stream 

Data only used once for rendering, updated very frequently.

Definition at line 76 of file glvertexbuffer.h.

Constructor & Destructor Documentation

◆ GLVertexBuffer()

KWin::GLVertexBuffer::GLVertexBuffer ( UsageHint hint)
explicit

Definition at line 383 of file glvertexbuffer.cpp.

◆ ~GLVertexBuffer()

KWin::GLVertexBuffer::~GLVertexBuffer ( )
default

Member Function Documentation

◆ beginFrame()

void KWin::GLVertexBuffer::beginFrame ( )

Notifies the vertex buffer that we are about to paint a frame.

Definition at line 592 of file glvertexbuffer.cpp.

◆ bindArrays()

void KWin::GLVertexBuffer::bindArrays ( )

Binds the vertex arrays to the context.

Definition at line 499 of file glvertexbuffer.cpp.

◆ cleanup()

void KWin::GLVertexBuffer::cleanup ( )
static

Definition at line 639 of file glvertexbuffer.cpp.

◆ draw() [1/2]

void KWin::GLVertexBuffer::draw ( const QRegion & region,
GLenum primitiveMode,
int first,
int count,
bool hardwareClipping = false )

Draws count vertices beginning with first.

Definition at line 514 of file glvertexbuffer.cpp.

◆ draw() [2/2]

void KWin::GLVertexBuffer::draw ( GLenum primitiveMode,
int first,
int count )

Draws count vertices beginning with first.

Definition at line 509 of file glvertexbuffer.cpp.

◆ endOfFrame()

void KWin::GLVertexBuffer::endOfFrame ( )

Notifies the vertex buffer that we are done painting the frame.

Definition at line 561 of file glvertexbuffer.cpp.

◆ initStatic()

void KWin::GLVertexBuffer::initStatic ( )
static

Definition at line 608 of file glvertexbuffer.cpp.

◆ map()

template<typename T >
std::optional< std::span< T > > KWin::GLVertexBuffer::map ( size_t count)
inline

Maps an unused range of the data store into the client's address space.

The data store will be reallocated if it is smaller than the given size.

The buffer object is mapped for writing, not reading. Attempts to read from the mapped buffer range may result in system errors, including program termination. The data in the mapped region is undefined until it has been written to. If subsequent GL calls access unwritten memory, the results are undefined and system errors, including program termination, may occur.

No GL calls that access the buffer object must be made while the buffer object is mapped. The returned pointer must not be passed as a parameter value to any GL function.

It is assumed that the GL_ARRAY_BUFFER_BINDING will not be changed while the buffer object is mapped.

Definition at line 176 of file glvertexbuffer.h.

◆ render() [1/2]

void KWin::GLVertexBuffer::render ( const QRegion & region,
GLenum primitiveMode,
bool hardwareClipping = false )

Same as above restricting painting to region if hardwareClipping is true. It's within the caller's responsibility to enable GL_SCISSOR_TEST.

Definition at line 492 of file glvertexbuffer.cpp.

◆ render() [2/2]

void KWin::GLVertexBuffer::render ( GLenum primitiveMode)

Renders the vertex data in given primitiveMode. Please refer to OpenGL documentation of glDrawArrays or glDrawElements for allowed values for primitiveMode. Best is to use GL_TRIANGLES or similar to be future compatible.

Definition at line 487 of file glvertexbuffer.cpp.

◆ reset()

void KWin::GLVertexBuffer::reset ( )

Resets the instance to default values. Useful for shared buffers.

Since
4.7

Definition at line 556 of file glvertexbuffer.cpp.

◆ setAttribLayout()

void KWin::GLVertexBuffer::setAttribLayout ( std::span< const GLVertexAttrib > attribs,
size_t stride )

Specifies how interleaved vertex attributes are laid out in the buffer object.

Note that the attributes and the stride should be 32 bit aligned or a performance penalty may be incurred.

For some hardware the optimal stride is a multiple of 32 bytes.

Example:

struct Vertex {
    QVector3D position;
    QVector2D texcoord;
};

const std::array attribs = {
    GLVertexAttrib{ VA_Position, 3, GL_FLOAT, offsetof(Vertex, position) },
    GLVertexAttrib{ VA_TexCoord, 2, GL_FLOAT, offsetof(Vertex, texcoord) },
};

Vertex vertices[6];
vbo->setAttribLayout(std::span(attribs), sizeof(Vertex));
vbo->setData(vertices, sizeof(vertices));

Definition at line 474 of file glvertexbuffer.cpp.

◆ setData()

void KWin::GLVertexBuffer::setData ( const void * data,
size_t sizeInBytes )

Uploads data into the buffer object's data store.

Definition at line 390 of file glvertexbuffer.cpp.

◆ setVertexCount()

void KWin::GLVertexBuffer::setVertexCount ( int count)

Sets the number of vertices that will be drawn by the render() method.

Definition at line 469 of file glvertexbuffer.cpp.

◆ setVertices() [1/3]

template<std::ranges::contiguous_range T>
requires std::is_same<std::ranges::range_value_t<T>, GLVertex2D>::value
void KWin::GLVertexBuffer::setVertices ( const T & range)
inline

Definition at line 125 of file glvertexbuffer.h.

◆ setVertices() [2/3]

template<std::ranges::contiguous_range T>
requires std::is_same<std::ranges::range_value_t<T>, GLVertex3D>::value
void KWin::GLVertexBuffer::setVertices ( const T & range)
inline

Definition at line 134 of file glvertexbuffer.h.

◆ setVertices() [3/3]

template<std::ranges::contiguous_range T>
requires std::is_same<std::ranges::range_value_t<T>, QVector2D>::value
void KWin::GLVertexBuffer::setVertices ( const T & range)
inline

Definition at line 143 of file glvertexbuffer.h.

◆ streamingBuffer()

GLVertexBuffer * KWin::GLVertexBuffer::streamingBuffer ( )
static
Returns
A shared VBO for streaming data
Since
4.7

Definition at line 647 of file glvertexbuffer.cpp.

◆ supportsIndexedQuads()

bool KWin::GLVertexBuffer::supportsIndexedQuads ( )
static

Returns true if indexed quad mode is supported, and false otherwise.

Definition at line 551 of file glvertexbuffer.cpp.

◆ unbindArrays()

void KWin::GLVertexBuffer::unbindArrays ( )

Disables the vertex arrays.

Definition at line 504 of file glvertexbuffer.cpp.

◆ unmap()

void KWin::GLVertexBuffer::unmap ( )

Flushes the mapped buffer range and unmaps the buffer.

Definition at line 427 of file glvertexbuffer.cpp.

Member Data Documentation

◆ GLVertex2DLayout

constexpr std::array KWin::GLVertexBuffer::GLVertex2DLayout
staticconstexpr
Initial value:
{
GLVertexAttrib{
.attributeIndex = VA_Position,
.componentCount = 2,
.type = GL_FLOAT,
.relativeOffset = offsetof(GLVertex2D, position),
},
.componentCount = 2,
.type = GL_FLOAT,
.relativeOffset = offsetof(GLVertex2D, texcoord),
},
}
@ VA_Position
@ VA_TexCoord

Definition at line 265 of file glvertexbuffer.h.

◆ GLVertex3DLayout

constexpr std::array KWin::GLVertexBuffer::GLVertex3DLayout
staticconstexpr
Initial value:
{
GLVertexAttrib{
.attributeIndex = VA_Position,
.componentCount = 3,
.type = GL_FLOAT,
.relativeOffset = offsetof(GLVertex3D, position),
},
.componentCount = 2,
.type = GL_FLOAT,
.relativeOffset = offsetof(GLVertex3D, texcoord),
},
}

Definition at line 279 of file glvertexbuffer.h.


The documentation for this class was generated from the following files: