OpenShaderDesigner 0.0.1
Loading...
Searching...
No Matches
BufferObject.h
1// =====================================================================================================================
2// Copyright 2024 Medusa Slockbower
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14// =====================================================================================================================
15
16#ifndef BUFFEROBJECT_H
17#define BUFFEROBJECT_H
18
19#ifndef NULL
20#define NULL 0
21#endif
22
23#include <algorithm>
24
25#include "Enum.h"
26#include "Type.h"
27
28namespace GLW
29{
30// Definition ==========================================================================================================
31
32 template<BufferType T, BufferUsage U, BufferStorage S = GPU>
34 {
35 public:
36 static constexpr BufferType Type = T;
37 static constexpr BufferUsage Usage = U;
38 static constexpr BufferStorage Storage = S;
39
45 BufferObject(SizeT size, void* data = nullptr);
46
51
55 BufferObject(const BufferObject& other);
56
61
65 [[nodiscard]] operator bool() const { return Handle != 0; }
66
70 BufferObject& operator=(const BufferObject& other);
71
75 BufferObject& operator=(BufferObject&& other) noexcept;
76
77 [[nodiscard]] SizeT Size() const { return Size; }
78 void Resize(SizeT size);
79
80 private:
81 HandleT Handle;
82 SizeT Size;
83 void* Mapping;
84 };
85
86// Constructors ========================================================================================================
87
88 template <BufferType T, BufferUsage U, BufferStorage S>
90 : Handle(NULL)
91 , Size(size)
92 , Mapping(nullptr)
93 {
94 glGenBuffers(1, &Handle);
95
96 if(!*this) return;
97
98 glBindBuffer(Type, Handle);
99 glBufferStorage(Type, Size, data, Usage);
100 glBindBuffer(Type, 0);
101 }
102
103 template <BufferType T, BufferUsage U, BufferStorage S>
105 : Handle(other.Handle)
106 , Size(other.Size)
107 , Mapping(other.Size)
108 {
109 other.Handle = NULL;
110 other.Size = 0;
111 other.Mapping = nullptr;
112 }
113
114 template <BufferType T, BufferUsage U, BufferStorage S>
116 : BufferObject(other.Size)
117 {
118 if(Handle == NULL) return;
119 if(other.Handle == NULL) return;
120
121 glCopyNamedBufferSubData(other.Handle, Handle, 0, 0, Size);
122 }
123
124 template <BufferType T, BufferUsage U, BufferStorage S>
126 {
127 glDeleteBuffers(1, &Handle);
128 }
129
130// Assignment Operators ================================================================================================
131
132 template <BufferType T, BufferUsage U, BufferStorage S>
134 {
135 BufferObject temp(other);
136 return (*this = std::move(temp)); // NOLINT(*-unconventional-assign-operator)
137 }
138
139 template <BufferType T, BufferUsage U, BufferStorage S>
141 {
142 glDeleteBuffers(1, &Handle);
143
144 Handle = other.Handle;
145 Size = other.Size;
146 Mapping = other.Mapping;
147
148 other.Handle = NULL;
149 other.Size = 0;
150 other.Mapping = nullptr;
151
152 return *this;
153 }
154
155 template <BufferType T, BufferUsage U, BufferStorage S>
156 void BufferObject<T, U, S>::Resize(SizeT size)
157 {
158 BufferObject temp(size);
159 glCopyNamedBufferSubData(Handle, temp.Handle, 0, 0, Size);
160 *this = std::move(temp);
161 }
162}
163
164#endif //BUFFEROBJECT_H
Definition BufferObject.h:34
BufferObject & operator=(const BufferObject &other)
Copy Assignment.
Definition BufferObject.h:133
BufferObject(SizeT size, void *data=nullptr)
BufferObject constructor.
Definition BufferObject.h:89
~BufferObject()
Destructor.
Definition BufferObject.h:125