Skip to content

Commit cc50ccf

Browse files
committed
Added capacity member to OpenGL::Buffer.
Refactored the Buffer class to use capacity as the total allocation and size as the in-use size in bytes. Renamed Buffer functions and params to match new system. Refactored all client code to use the new API.
1 parent def250d commit cc50ccf

File tree

10 files changed

+276
-172
lines changed

10 files changed

+276
-172
lines changed

source/Component/Mesh.hpp

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ namespace Data
2626
requires Data::is_valid_mesh_vert<VertexType>
2727
Mesh(const std::vector<VertexType>& vertex_data, OpenGL::PrimitiveMode primitive_mode)
2828
: VAO{}
29-
, vert_buffer{{OpenGL::BufferStorageFlag::DynamicStorageBit}}
29+
, vert_buffer{{OpenGL::BufferStorageFlag::DynamicStorageBit}, vertex_data}
3030
, index_buffer{}
3131
, vertex_positions{}// }
3232
, AABB{} // |> TODO: these out of the MeshBuilder directly.
@@ -73,7 +73,6 @@ namespace Data
7373
else
7474
[]<bool flag = false>() { static_assert(flag, "Unsupported Vertex type"); }(); // #CPP23 P2593R0 swap for static_assert(false)
7575

76-
vert_buffer.upload_data(vertex_data);
7776
VAO.attach_buffer(vert_buffer, 0, 0, sizeof(VertexType));
7877

7978
for (const auto& vertex : vertex_data)
@@ -84,8 +83,8 @@ namespace Data
8483
requires Data::is_valid_mesh_vert<VertexType>
8584
Mesh(std::vector<VertexType>&& vertex_data, std::vector<unsigned int> indices, OpenGL::PrimitiveMode primitive_mode)
8685
: VAO{}
87-
, vert_buffer{{OpenGL::BufferStorageFlag::DynamicStorageBit}}
88-
, index_buffer{OpenGL::Buffer({OpenGL::BufferStorageFlag::DynamicStorageBit})}
86+
, vert_buffer{{OpenGL::BufferStorageFlag::DynamicStorageBit}, vertex_data}
87+
, index_buffer{OpenGL::Buffer{{OpenGL::BufferStorageFlag::DynamicStorageBit}, indices}}
8988
, vertex_positions{}// TODO: Feed vertex_positions out of the MeshBuilder directly.
9089
, AABB{} // TODO: Feed AABB out of the MeshBuilder directly.
9190
{
@@ -126,10 +125,7 @@ namespace Data
126125
else
127126
[]<bool flag = false>() { static_assert(flag, "Unsupported Vertex type"); }(); // #CPP23 P2593R0 swap for static_assert(false)
128127

129-
vert_buffer.upload_data(vertex_data);
130128
VAO.attach_buffer(vert_buffer, 0, 0, sizeof(VertexType));
131-
132-
index_buffer->upload_data(indices);
133129
VAO.attach_element_buffer(index_buffer.value(), (GLsizei)indices.size());
134130

135131
for (const auto& vertex : vertex_data)

source/OpenGL/GLState.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,12 @@ namespace OpenGL
142142

143143
glDeleteBuffers(1, &p_buffer);
144144
}
145+
GLHandle State::create_buffer()
146+
{
147+
GLHandle buffer;
148+
glCreateBuffers(1, &buffer);
149+
return buffer;
150+
}
145151

146152
void State::bind_texture_unit(GLuint p_texture_unit, GLHandle p_texture)
147153
{

source/OpenGL/GLState.hpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ using GLintptr = std::ptrdiff_t;
3131
/// STRONGLY TYPED ENUM WRAPPERS
3232
namespace OpenGL
3333
{
34-
constexpr inline static bool LogGLTypeEvents = false;
34+
constexpr inline static bool LogGLTypeEvents = false;
35+
constexpr inline static bool LogGLBufferEvents = false;
3536

3637
enum class BufferType : uint8_t
3738
{
@@ -407,6 +408,7 @@ namespace OpenGL
407408
void bind_shader_storage_buffer(GLuint p_index, GLHandle p_buffer, GLintptr p_offset, GLsizeiptr p_size);
408409
void bind_uniform_buffer(GLuint p_index, GLHandle p_buffer, GLintptr p_offset, GLsizeiptr p_size);
409410
void delete_buffer(GLHandle p_buffer);
411+
GLHandle create_buffer();
410412

411413
void bind_texture_unit(GLuint p_texture_unit, GLHandle p_texture);
412414
void delete_texture(GLHandle p_texture);
@@ -662,7 +664,7 @@ namespace OpenGL
662664
//@param p_size Specifies the size in basic machine units of the range of the data store to fill.
663665
//@param p_format Specifies the format of the data in memory addressed by data.
664666
//@param p_type Specifies the type of the data in memory addressed by data.
665-
//@param data Specifies the address of a memory location storing the data to be replicated into the buffer's data store.
667+
//@param data Specifies the address of a memory location storing the data to be replicated into the buffer's data store. If data is nullptr, then the subrange of the buffer's data store is filled with zeros.
666668
void clear_named_buffer_sub_data(GLHandle p_buffer, GLenum p_internal_format, GLintptr p_offset, GLsizeiptr p_size, GLenum p_format, GLenum p_type, const void *data);
667669

668670
// Creates and initializes a buffer object's immutable data store

source/OpenGL/OpenGLRenderer.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ namespace OpenGL
3434
, m_screen_framebuffer{m_window.size()}
3535
, m_asset_manager{p_asset_manager}
3636
, m_scene_system{p_scene_system}
37-
, m_view_properties_buffer{{OpenGL::BufferStorageFlag::DynamicStorageBit}}
37+
, m_view_properties_buffer{{OpenGL::BufferStorageFlag::DynamicStorageBit}, sizeof(Component::ViewInformation)}
3838
, m_uniform_colour_shader{"uniformColour"}
3939
, m_colour_shader{"colour"}
4040
, m_texture_shader{"texture1"}
@@ -71,15 +71,14 @@ namespace OpenGL
7171
, "ViewProperties.view_position block variable mismatch. Has the shader or the Component::ViewInformation struct changed?");
7272
}
7373
#endif
74-
m_view_properties_buffer.resize(sizeof(Component::ViewInformation));
75-
m_view_properties_buffer.buffer_sub_data(0, m_scene_system.get_current_scene_view_info());
74+
m_view_properties_buffer.set_data(m_scene_system.get_current_scene_view_info(), 0);
7675

7776
LOG("[OPENGL] Constructed new OpenGLRenderer instance");
7877
}
7978

8079
void OpenGLRenderer::start_frame()
8180
{
82-
m_view_properties_buffer.buffer_sub_data(0, m_scene_system.get_current_scene_view_info());
81+
m_view_properties_buffer.set_data(m_scene_system.get_current_scene_view_info(), 0);
8382

8483
m_shadow_mapper.shadow_pass(m_scene_system.get_current_scene());
8584

source/OpenGL/ParticleRenderer.cpp

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -121,15 +121,11 @@ namespace OpenGL
121121
if (p_emitter.particle_buf.size() < new_size)
122122
{
123123
const GLsizeiptr new_size_pwr_2 = Utility::next_power_of_2(new_size);
124-
LOG("Resizing particle buffer from {}B to {}B", p_emitter.particle_buf.size(), new_size_pwr_2)
125-
126-
auto new_particle_buff = OpenGL::Buffer({OpenGL::BufferStorageFlag::DynamicStorageBit});
127-
new_particle_buff.resize(new_size_pwr_2);
128-
new_particle_buff.copy_sub_data(p_emitter.particle_buf, 0, 0, p_emitter.particle_buf.size());
129-
p_emitter.particle_buf = std::move(new_particle_buff);
124+
p_emitter.particle_buf.reserve(new_size_pwr_2);
125+
LOG("Resizing particle buffer from {}B to {}B", p_emitter.particle_buf.size(), new_size_pwr_2);
130126
}
131127

132-
p_emitter.particle_buf.buffer_sub_data(particle_stride * p_emitter.alive_count, new_particles);
128+
p_emitter.particle_buf.set_data(new_particles, particle_stride * p_emitter.alive_count);
133129
p_emitter.alive_count += new_particle_count;
134130
}
135131

source/OpenGL/PhongRenderer.cpp

Lines changed: 47 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -193,51 +193,40 @@ namespace OpenGL
193193

194194
void PhongRenderer::update_light_data(System::Scene& p_scene)
195195
{
196+
// TODO: Seperate the light storage blocks into meta data and data, so we can push the entire light data in one go per instance.
197+
196198
{ // Set DirectonalLight buffer data
197199
GLuint directional_light_count = static_cast<GLuint>(p_scene.m_entities.count_components<Component::DirectionalLight>());
198-
{
199-
const GLsizeiptr required_size = m_directional_light_fixed_size + (m_directional_light_array_stride * directional_light_count);
200-
{ // Resize the buffer to accomodate at least the directional_light_count
201-
if (required_size > m_directional_lights_buffer.size())
202-
{
203-
OpenGL::Buffer new_buffer{{OpenGL::BufferStorageFlag::DynamicStorageBit}};
204-
new_buffer.resize(required_size);
205-
m_directional_lights_buffer = std::move(new_buffer);
206-
LOG("[OPENGL][PHONG] DirectionalLight count changed ({}), resized the directional light buffer to {}B", directional_light_count, required_size);
207-
}
208-
}
209-
m_directional_lights_buffer.buffer_sub_data(m_directional_light_count_offset, directional_light_count);
210200

211-
GLuint i = 0;
212-
p_scene.m_entities.foreach([&](Component::DirectionalLight& p_directional_light)
213-
{
214-
const glm::vec3 diffuse = p_directional_light.m_colour * p_directional_light.m_diffuse_intensity;
215-
const glm::vec3 ambient = p_directional_light.m_colour * p_directional_light.m_ambient_intensity;
216-
const glm::vec3 specular = glm::vec3(p_directional_light.m_specular_intensity);
201+
const GLsizeiptr required_capacity = m_directional_light_fixed_size + (m_directional_light_array_stride * directional_light_count);
202+
if (required_capacity > m_directional_lights_buffer.capacity())
203+
m_directional_lights_buffer.reserve(required_capacity);
217204

218-
m_directional_lights_buffer.buffer_sub_data(m_directional_light_direction_offset + (m_directional_light_array_stride * i), p_directional_light.m_direction);
219-
m_directional_lights_buffer.buffer_sub_data(m_directional_light_ambient_offset + (m_directional_light_array_stride * i), ambient);
220-
m_directional_lights_buffer.buffer_sub_data(m_directional_light_diffuse_offset + (m_directional_light_array_stride * i), diffuse);
221-
m_directional_lights_buffer.buffer_sub_data(m_directional_light_specular_offset + (m_directional_light_array_stride * i), specular);
205+
m_directional_lights_buffer.set_data(directional_light_count, m_directional_light_count_offset);
222206

223-
i++;
224-
});
225-
}
207+
GLuint i = 0;
208+
p_scene.m_entities.foreach([&](Component::DirectionalLight& p_directional_light)
209+
{
210+
const glm::vec3 diffuse = p_directional_light.m_colour * p_directional_light.m_diffuse_intensity;
211+
const glm::vec3 ambient = p_directional_light.m_colour * p_directional_light.m_ambient_intensity;
212+
const glm::vec3 specular = glm::vec3(p_directional_light.m_specular_intensity);
213+
214+
m_directional_lights_buffer.set_data(p_directional_light.m_direction, m_directional_light_direction_offset + (m_directional_light_array_stride * i));
215+
m_directional_lights_buffer.set_data(ambient, m_directional_light_ambient_offset + (m_directional_light_array_stride * i));
216+
m_directional_lights_buffer.set_data(diffuse, m_directional_light_diffuse_offset + (m_directional_light_array_stride * i));
217+
m_directional_lights_buffer.set_data(specular, m_directional_light_specular_offset + (m_directional_light_array_stride * i));
218+
219+
i++;
220+
});
226221
}
227222
{ // Set PointLight buffer data
228223
GLuint point_light_count = static_cast<GLuint>(p_scene.m_entities.count_components<Component::PointLight>());
229224
{
230-
const GLsizeiptr required_size = m_point_light_fixed_size + (m_point_light_array_stride * point_light_count);
231-
{ // Resize the buffer to accomodate at least the point_light_count
232-
if (required_size > m_point_lights_buffer.size())
233-
{
234-
OpenGL::Buffer new_buffer{{OpenGL::BufferStorageFlag::DynamicStorageBit}};
235-
new_buffer.resize(required_size);
236-
m_point_lights_buffer = std::move(new_buffer);
237-
LOG("[OPENGL][PHONG] PointLight count changed ({}), resized the point light buffer to {}B", point_light_count, required_size);
238-
}
239-
}
240-
m_point_lights_buffer.buffer_sub_data(m_point_light_count_offset, point_light_count);
225+
const GLsizeiptr required_capacity = m_point_light_fixed_size + (m_point_light_array_stride * point_light_count);
226+
if (required_capacity > m_point_lights_buffer.capacity())
227+
m_point_lights_buffer.reserve(required_capacity);
228+
229+
m_point_lights_buffer.set_data(point_light_count, m_point_light_count_offset);
241230

242231
GLuint i = 0;
243232
p_scene.m_entities.foreach([&](Component::PointLight& p_point_light)
@@ -246,13 +235,13 @@ namespace OpenGL
246235
const glm::vec3 ambient = p_point_light.m_colour * p_point_light.m_ambient_intensity;
247236
const glm::vec3 specular = glm::vec3(p_point_light.m_specular_intensity);
248237

249-
m_point_lights_buffer.buffer_sub_data(m_point_light_position_offset + (m_point_light_array_stride * i), p_point_light.m_position);
250-
m_point_lights_buffer.buffer_sub_data(m_point_light_constant_offset + (m_point_light_array_stride * i), p_point_light.m_constant);
251-
m_point_lights_buffer.buffer_sub_data(m_point_light_linear_offset + (m_point_light_array_stride * i), p_point_light.m_linear);
252-
m_point_lights_buffer.buffer_sub_data(m_point_light_quadratic_offset + (m_point_light_array_stride * i), p_point_light.m_quadratic);
253-
m_point_lights_buffer.buffer_sub_data(m_point_light_ambient_offset + (m_point_light_array_stride * i), ambient);
254-
m_point_lights_buffer.buffer_sub_data(m_point_light_diffuse_offset + (m_point_light_array_stride * i), diffuse);
255-
m_point_lights_buffer.buffer_sub_data(m_point_light_specular_offset + (m_point_light_array_stride * i), specular);
238+
m_point_lights_buffer.set_data(p_point_light.m_position, m_point_light_position_offset + (m_point_light_array_stride * i));
239+
m_point_lights_buffer.set_data(p_point_light.m_constant, m_point_light_constant_offset + (m_point_light_array_stride * i));
240+
m_point_lights_buffer.set_data(p_point_light.m_linear, m_point_light_linear_offset + (m_point_light_array_stride * i));
241+
m_point_lights_buffer.set_data(p_point_light.m_quadratic, m_point_light_quadratic_offset + (m_point_light_array_stride * i));
242+
m_point_lights_buffer.set_data(ambient, m_point_light_ambient_offset + (m_point_light_array_stride * i));
243+
m_point_lights_buffer.set_data(diffuse, m_point_light_diffuse_offset + (m_point_light_array_stride * i));
244+
m_point_lights_buffer.set_data(specular, m_point_light_specular_offset + (m_point_light_array_stride * i));
256245

257246
i++;
258247
});
@@ -261,18 +250,11 @@ namespace OpenGL
261250
{ // Set Spotlight buffer data
262251
GLuint spot_light_count = static_cast<GLuint>(p_scene.m_entities.count_components<Component::SpotLight>());
263252
{
264-
const GLsizeiptr required_size = m_spot_light_fixed_size + (m_spot_light_array_stride * spot_light_count);
265-
{ // Resize the buffer to accomodate at least the spot_light_count
266-
if (required_size > m_spot_lights_buffer.size())
267-
{
268-
OpenGL::Buffer new_buffer{{OpenGL::BufferStorageFlag::DynamicStorageBit}};
269-
new_buffer.resize(required_size);
270-
m_spot_lights_buffer = std::move(new_buffer);
271-
LOG("[OPENGL][PHONG] SpotLight count changed ({}), resized the spot light buffer to {}B", spot_light_count, required_size);
272-
}
273-
}
274-
275-
m_spot_lights_buffer.buffer_sub_data(m_spot_light_count_offset, spot_light_count);
253+
const GLsizeiptr required_capacity = m_spot_light_fixed_size + (m_spot_light_array_stride * spot_light_count);
254+
if (required_capacity > m_spot_lights_buffer.capacity())
255+
m_spot_lights_buffer.reserve(required_capacity);
256+
257+
m_spot_lights_buffer.set_data(spot_light_count, m_spot_light_count_offset);
276258

277259
GLuint i = 0;
278260
p_scene.m_entities.foreach([&](Component::SpotLight& p_spotlight)
@@ -281,16 +263,16 @@ namespace OpenGL
281263
const glm::vec3 ambient = p_spotlight.m_colour * p_spotlight.m_ambient_intensity;
282264
const glm::vec3 specular = glm::vec3(p_spotlight.m_specular_intensity);
283265

284-
m_spot_lights_buffer.buffer_sub_data(m_spot_light_position_offset + (m_spot_light_array_stride * i), p_spotlight.m_position);
285-
m_spot_lights_buffer.buffer_sub_data(m_spot_light_direction_offset + (m_spot_light_array_stride * i), p_spotlight.m_direction);
286-
m_spot_lights_buffer.buffer_sub_data(m_spot_light_cutoff_offset + (m_spot_light_array_stride * i), p_spotlight.m_cutoff);
287-
m_spot_lights_buffer.buffer_sub_data(m_spot_light_outer_cutoff_offset + (m_spot_light_array_stride * i), p_spotlight.m_outer_cutoff);
288-
m_spot_lights_buffer.buffer_sub_data(m_spot_light_constant_offset + (m_spot_light_array_stride * i), p_spotlight.m_constant);
289-
m_spot_lights_buffer.buffer_sub_data(m_spot_light_linear_offset + (m_spot_light_array_stride * i), p_spotlight.m_linear);
290-
m_spot_lights_buffer.buffer_sub_data(m_spot_light_quadratic_offset + (m_spot_light_array_stride * i), p_spotlight.m_quadratic);
291-
m_spot_lights_buffer.buffer_sub_data(m_spot_light_ambient_offset + (m_spot_light_array_stride * i), ambient);
292-
m_spot_lights_buffer.buffer_sub_data(m_spot_light_diffuse_offset + (m_spot_light_array_stride * i), diffuse);
293-
m_spot_lights_buffer.buffer_sub_data(m_spot_light_specular_offset + (m_spot_light_array_stride * i), specular);
266+
m_spot_lights_buffer.set_data(p_spotlight.m_position, m_spot_light_position_offset + (m_spot_light_array_stride * i));
267+
m_spot_lights_buffer.set_data(p_spotlight.m_direction, m_spot_light_direction_offset + (m_spot_light_array_stride * i));
268+
m_spot_lights_buffer.set_data(p_spotlight.m_cutoff, m_spot_light_cutoff_offset + (m_spot_light_array_stride * i));
269+
m_spot_lights_buffer.set_data(p_spotlight.m_outer_cutoff, m_spot_light_outer_cutoff_offset + (m_spot_light_array_stride * i));
270+
m_spot_lights_buffer.set_data(p_spotlight.m_constant, m_spot_light_constant_offset + (m_spot_light_array_stride * i));
271+
m_spot_lights_buffer.set_data(p_spotlight.m_linear, m_spot_light_linear_offset + (m_spot_light_array_stride * i));
272+
m_spot_lights_buffer.set_data(p_spotlight.m_quadratic, m_spot_light_quadratic_offset + (m_spot_light_array_stride * i));
273+
m_spot_lights_buffer.set_data(ambient, m_spot_light_ambient_offset + (m_spot_light_array_stride * i));
274+
m_spot_lights_buffer.set_data(diffuse, m_spot_light_diffuse_offset + (m_spot_light_array_stride * i));
275+
m_spot_lights_buffer.set_data(specular, m_spot_light_specular_offset + (m_spot_light_array_stride * i));
294276
i++;
295277
});
296278
}

0 commit comments

Comments
 (0)