KWin
Loading...
Searching...
No Matches
glvertexbuffer.h
Go to the documentation of this file.
1/*
2 KWin - the KDE window manager
3 This file is part of the KDE project.
4
5 SPDX-FileCopyrightText: 2006-2007 Rivo Laks <rivolaks@hot.ee>
6 SPDX-FileCopyrightText: 2010, 2011 Martin Gräßlin <mgraesslin@kde.org>
7 SPDX-FileCopyrightText: 2023 Xaver Hugl <xaver.hugl@kde.org>
8
9 SPDX-License-Identifier: GPL-2.0-or-later
10*/
11#pragma once
12#include "kwin_export.h"
13
14#include <QColor>
15#include <QRegion>
16#include <QVector2D>
17
18#include <epoxy/gl.h>
19#include <optional>
20#include <ranges>
21#include <span>
22
23namespace KWin
24{
25
31
33{
34 QVector2D position;
35 QVector2D texcoord;
36};
37
39{
40 QVector3D position;
41 QVector2D texcoord;
42};
43
52{
55 GLenum type;
57};
58
60
70class KWIN_EXPORT GLVertexBuffer
71{
72public:
76 enum UsageHint {
79 Stream
80 };
81
82 explicit GLVertexBuffer(UsageHint hint);
84
110 void setAttribLayout(std::span<const GLVertexAttrib> attribs, size_t stride);
111
115 void setData(const void *data, size_t sizeInBytes);
116
120 void setVertexCount(int count);
121
122 // clang-format off
123 template<std::ranges::contiguous_range T>
124 requires std::is_same<std::ranges::range_value_t<T>, GLVertex2D>::value
125 void setVertices(const T &range)
126 {
127 setData(range.data(), range.size() * sizeof(GLVertex2D));
128 setVertexCount(range.size());
129 setAttribLayout(std::span(GLVertex2DLayout), sizeof(GLVertex2D));
130 }
131
132 template<std::ranges::contiguous_range T>
133 requires std::is_same<std::ranges::range_value_t<T>, GLVertex3D>::value
134 void setVertices(const T &range)
135 {
136 setData(range.data(), range.size() * sizeof(GLVertex3D));
137 setVertexCount(range.size());
138 setAttribLayout(std::span(GLVertex3DLayout), sizeof(GLVertex3D));
139 }
140
141 template<std::ranges::contiguous_range T>
142 requires std::is_same<std::ranges::range_value_t<T>, QVector2D>::value
143 void setVertices(const T &range)
144 {
145 setData(range.data(), range.size() * sizeof(QVector2D));
146 setVertexCount(range.size());
147 static constexpr GLVertexAttrib layout{
149 .componentCount = 2,
150 .type = GL_FLOAT,
151 .relativeOffset = 0,
152 };
153 setAttribLayout(std::span(&layout, 1), sizeof(QVector2D));
154 }
155 // clang-format on
156
175 template<typename T>
176 std::optional<std::span<T>> map(size_t count)
177 {
178 if (const auto m = map(sizeof(T) * count)) {
179 return std::span(reinterpret_cast<T *>(m), count);
180 } else {
181 return std::nullopt;
182 }
183 }
184
188 void unmap();
189
193 void bindArrays();
194
198 void unbindArrays();
199
203 void draw(GLenum primitiveMode, int first, int count);
204
208 void draw(const QRegion &region, GLenum primitiveMode, int first, int count, bool hardwareClipping = false);
209
216 void render(GLenum primitiveMode);
221 void render(const QRegion &region, GLenum primitiveMode, bool hardwareClipping = false);
222
228 void reset();
229
235 void endOfFrame();
236
242 void beginFrame();
243
247 static void initStatic();
248
252 static void cleanup();
253
257 static bool supportsIndexedQuads();
258
263 static GLVertexBuffer *streamingBuffer();
264
265 static constexpr std::array GLVertex2DLayout{
268 .componentCount = 2,
269 .type = GL_FLOAT,
270 .relativeOffset = offsetof(GLVertex2D, position),
271 },
274 .componentCount = 2,
275 .type = GL_FLOAT,
276 .relativeOffset = offsetof(GLVertex2D, texcoord),
277 },
278 };
279 static constexpr std::array GLVertex3DLayout{
282 .componentCount = 3,
283 .type = GL_FLOAT,
284 .relativeOffset = offsetof(GLVertex3D, position),
285 },
288 .componentCount = 2,
289 .type = GL_FLOAT,
290 .relativeOffset = offsetof(GLVertex3D, texcoord),
291 },
292 };
293
294private:
295 GLvoid *map(size_t size);
296
297 const std::unique_ptr<GLVertexBufferPrivate> d;
298};
299
300}
Vertex Buffer Object.
void setVertices(const T &range)
std::optional< std::span< T > > map(size_t count)
@ Static
No changes to data.
@ Dynamic
frequent changes, but used several times for rendering
void setVertices(const T &range)
void setVertices(const T &range)
VertexAttributeType
@ VA_Position
@ VertexAttributeCount
@ VA_TexCoord