Brenta Engine 1.2
Loading...
Searching...
No Matches
buffer.hpp
1// SPDX-License-Identifier: MIT
2// Author: Giovanni Santini
3// Mail: giovanni.santini@proton.me
4// Github: @San7o
5
6#pragma once
7
8#include <glad/glad.h> // OpenGL driver
9
10namespace brenta
11{
12
13class Buffer
14{
15public:
16
17 using Id = unsigned int;
18
19 enum Target {
20 // vertex attributes (positions, normals...)
21 Array = GL_ARRAY_BUFFER,
22 // indices for glDrawElements
23 ElementArray = GL_ELEMENT_ARRAY_BUFFER,
24 // uniform blocks for shared shader data
25 Uniform = GL_UNIFORM_BUFFER,
26 // buffer textures for large data sets
27 Texture = GL_TEXTURE_BUFFER,
28 // capturing output from shaders
29 TransformFeedback = GL_TRANSFORM_FEEDBACK_BUFFER,
30 // used as source/destinaiton for copying between buffers
31 CopyRead = GL_COPY_READ_BUFFER,
32 // source for texture uploads (glTexImag2D)
33 PixelUnpack = GL_PIXEL_UNPACK_BUFFER,
34 // Large, writable data structures
35 ShaderStorage = GL_SHADER_STORAGE_BUFFER,
36 };
37
38 enum DataUsage {
39 // The data is accessed only once and used by the GPU at most a
40 // few times
41 StreamDraw = GL_STREAM_DRAW,
42 StreamRead = GL_STREAM_READ,
43 StreamCopy = GL_STREAM_COPY,
44 // The data is accessed only once and used many times
45 StaticDraw = GL_STATIC_DRAW,
46 StaticRead = GL_STATIC_READ,
47 StaticCopy = GL_STATIC_COPY,
48 // The data is accessed a lot and used many times
49 DynamicDraw = GL_DYNAMIC_DRAW,
50 DynamicRead = GL_DYNAMIC_READ,
51 DynamicCopy = GL_DYNAMIC_COPY,
52 };
53
54 // Used for profiling
55 int memory = 0;
56 static int tot_memory;
57
58 Buffer() = default;
59 Buffer(Target target);
60
61 constexpr Buffer(const Buffer&) = delete;
62 constexpr Buffer& operator=(const Buffer&) = delete;
63
64 constexpr Buffer(Buffer&& other) noexcept
65 {
66 this->id = other.id;
67 this->target = other.target;
68 this->memory = other.memory;
69
70 other.id = 0;
71 other.memory = 0;
72 }
73 constexpr Buffer& operator=(Buffer&& other) noexcept
74 {
75 this->id = other.id;
76 this->target = other.target;
77 this->memory = other.memory;
78
79 other.id = 0;
80 other.memory = 0;
81 return *this;
82 }
83
84 virtual ~Buffer();
85
86 void init(Target target);
87 void destroy();
88
89 virtual void bind() const;
90 virtual void unbind() const;
91
92 // Getters
93
94 Buffer::Id get_id() const;
95 Target &get_target();
96
97 // Setters
98
99 void set_id(Buffer::Id id);
100 void set_target(Target target);
101
102 // Utilities
103
104 void copy_data(const void *data, GLsizeiptr size, DataUsage usage);
105
106protected:
107
108 Buffer::Id id = 0;
109 Target target;
110
111};
112
113//
114// Vertex buffer obejcts
115// ---------------------
116//
117// VBOs store vertex data in the GPU as raw bytes: it
118// does not know the structure of the data. For this reason, we use
119// a vao.
120typedef Buffer Vbo;
121
122//
123// Element buffer objects
124// ----------------------
125//
126// Used to aboid duplicating vertices by reusing them with indices
127typedef Buffer Ebo;
128
129//
130// Feedback buffer objects
131// ------------------------
132//
133// These are used to save the state of some uniforms after draw call
134typedef Buffer Fbo;
135
136} // namespace brenta