-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstance_buffer_linux.cpp
More file actions
211 lines (192 loc) · 6.1 KB
/
Copy pathinstance_buffer_linux.cpp
File metadata and controls
211 lines (192 loc) · 6.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#include "detect_os.hpp"
#ifdef PAZ_LINUX
#include "PAZ_Graphics"
#include "util_linux.hpp"
#include "internal_data.hpp"
#include "common.hpp"
#include "gl_core_4_1.h"
#include <numeric>
paz::InstanceBuffer::Data::~Data()
{
for(auto& n : _ids)
{
glDeleteBuffers(1, &n);
}
glDeleteVertexArrays(1, &_id);
}
paz::InstanceBuffer::Data::Data()
{
glGenVertexArrays(1, &_id);
}
paz::InstanceBuffer::InstanceBuffer()
{
initialize();
_data = std::make_shared<Data>();
}
paz::InstanceBuffer::InstanceBuffer(std::size_t size) : InstanceBuffer()
{
_data->_numInstances = size;
}
void paz::InstanceBuffer::Data::checkSize(int dim, std::size_t size)
{
const std::size_t m = size/dim;
if(!_numInstances)
{
_numInstances = m;
}
else if(m != _numInstances)
{
throw std::runtime_error("Number of instances for each attribute must m"
"atch.");
}
}
void paz::InstanceBuffer::Data::addAttribute(int dim, DataType type)
{
const std::size_t i = _ids.size();
_dims.push_back(dim);
glBindVertexArray(_id);
_ids.emplace_back();
glGenBuffers(1, &_ids.back());
glEnableVertexAttribArray(i);
glBindBuffer(GL_ARRAY_BUFFER, _ids.back());
if(type == DataType::Float)
{
glVertexAttribPointer(i, dim, GL_FLOAT, GL_FALSE, 0, nullptr);
}
else
{
glVertexAttribIPointer(i, dim, gl_type(type), 0, nullptr);
}
switch(dim)
{
case 1:
switch(type)
{
case DataType::SInt: _types.push_back(GL_INT); break;
case DataType::UInt: _types.push_back(GL_UNSIGNED_INT); break;
case DataType::Float: _types.push_back(GL_FLOAT); break;
default: throw std::logic_error("Invalid data type.");
}
break;
case 2:
switch(type)
{
case DataType::SInt: _types.push_back(GL_INT_VEC2); break;
case DataType::UInt: _types.push_back(GL_UNSIGNED_INT_VEC2);
break;
case DataType::Float: _types.push_back(GL_FLOAT_VEC2); break;
default: throw std::logic_error("Invalid data type.");
}
break;
case 4:
switch(type)
{
case DataType::SInt: _types.push_back(GL_INT_VEC4); break;
case DataType::UInt: _types.push_back(GL_UNSIGNED_INT_VEC4);
break;
case DataType::Float: _types.push_back(GL_FLOAT_VEC4); break;
default: throw std::logic_error("Invalid data type.");
}
break;
default: throw std::logic_error("Instance attribute dimensions must be "
"1, 2, or 4.");
}
}
void paz::InstanceBuffer::addAttribute(int dim, DataType type)
{
if(!_data->_numInstances)
{
throw std::runtime_error("Instance buffer size has not been set.");
}
_data->addAttribute(dim, type);
std::size_t s = dim*_data->_numInstances;
switch(type)
{
case DataType::SInt: s *= sizeof(int); break;
case DataType::UInt: s *= sizeof(unsigned int); break;
case DataType::Float: s *= sizeof(float); break;
default: throw std::logic_error("Invalid data type.");
}
glBufferData(GL_ARRAY_BUFFER, s, nullptr, GL_DYNAMIC_DRAW);
}
void paz::InstanceBuffer::addAttribute(int dim, const GLfloat* data, std::size_t
size)
{
_data->checkSize(dim, size);
_data->addAttribute(dim, DataType::Float);
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat)*size, data, GL_STATIC_DRAW);
}
void paz::InstanceBuffer::addAttribute(int dim, const GLuint* data, std::size_t
size)
{
_data->checkSize(dim, size);
_data->addAttribute(dim, DataType::UInt);
glBufferData(GL_ARRAY_BUFFER, sizeof(GLuint)*size, data, GL_STATIC_DRAW);
}
void paz::InstanceBuffer::addAttribute(int dim, const GLint* data, std::size_t
size)
{
_data->checkSize(dim, size);
_data->addAttribute(dim, DataType::SInt);
glBufferData(GL_ARRAY_BUFFER, sizeof(GLint)*size, data, GL_STATIC_DRAW);
}
void paz::InstanceBuffer::subAttribute(std::size_t idx, const GLfloat* data,
std::size_t size)
{
if(idx >= _data->_ids.size())
{
throw std::logic_error("Attribute index " + std::to_string(idx) +
" is out of range.");
}
if(_data->_types[idx] != GL_FLOAT && _data->_types[idx] != GL_FLOAT_VEC2 &&
_data->_types[idx] != GL_FLOAT_VEC4)
{
throw std::logic_error("Attribute type does not match.");
}
_data->checkSize(_data->_dims[idx], size);
glBindBuffer(GL_ARRAY_BUFFER, _data->_ids[idx]);
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(GLfloat)*size, data);
}
void paz::InstanceBuffer::subAttribute(std::size_t idx, const GLuint* data,
std::size_t size)
{
if(idx >= _data->_ids.size())
{
throw std::logic_error("Attribute index " + std::to_string(idx) +
" is out of range.");
}
if(_data->_types[idx] != GL_UNSIGNED_INT && _data->_types[idx] !=
GL_UNSIGNED_INT_VEC2 && _data->_types[idx] != GL_UNSIGNED_INT_VEC4)
{
throw std::logic_error("Attribute type does not match.");
}
_data->checkSize(_data->_dims[idx], size);
glBindBuffer(GL_ARRAY_BUFFER, _data->_ids[idx]);
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(GLuint)*size, data);
}
void paz::InstanceBuffer::subAttribute(std::size_t idx, const GLint* data, std::
size_t size)
{
if(idx >= _data->_ids.size())
{
throw std::logic_error("Attribute index " + std::to_string(idx) +
" is out of range.");
}
if(_data->_types[idx] != GL_INT && _data->_types[idx] != GL_INT_VEC2 &&
_data->_types[idx] != GL_INT_VEC4)
{
throw std::logic_error("Attribute type does not match.");
}
_data->checkSize(_data->_dims[idx], size);
glBindBuffer(GL_ARRAY_BUFFER, _data->_ids[idx]);
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(GLint)*size, data);
}
bool paz::InstanceBuffer::empty() const
{
return !_data || !_data->_numInstances;
}
std::size_t paz::InstanceBuffer::size() const
{
return _data ? _data->_numInstances : 0;
}
#endif