cleanup & performance optimization.
Some checks failed
Run ReCI Build Test / Explore-Gitea-Actions (push) Has been cancelled
Some checks failed
Run ReCI Build Test / Explore-Gitea-Actions (push) Has been cancelled
This commit is contained in:
@@ -65,6 +65,22 @@ vec4 Default() {
|
||||
return gl_ModelViewProjectionMatrix * gl_Vertex;
|
||||
}
|
||||
|
||||
vec4 DefaultInstanced() {
|
||||
v_color = a_instance_color;
|
||||
vec2 scaled = a_vertex_position * a_instance_size;
|
||||
vec2 world_pos = scaled + a_instance_position;
|
||||
return gl_ModelViewProjectionMatrix * vec4(world_pos, 0.0, 1.0);
|
||||
}
|
||||
|
||||
vec4 FillRectInstanced() {
|
||||
vec2 instance_pos = a_instance_position + vec2(0.0, a_instance_size.y);
|
||||
vec2 scaled = a_vertex_position * a_instance_size;
|
||||
vec2 world_pos = scaled + instance_pos;
|
||||
|
||||
v_color = a_instance_color;
|
||||
return gl_ModelViewProjectionMatrix * vec4(world_pos, 0.0, 1.0);
|
||||
}
|
||||
|
||||
void main() {
|
||||
GL_TEXTURE0_COORD = gl_MultiTexCoord0.xy;
|
||||
GL_TEXTURE1_COORD = gl_MultiTexCoord1.xy;
|
||||
@@ -139,13 +155,8 @@ void main() {
|
||||
else { gl_Position = Default(); }
|
||||
*/
|
||||
|
||||
if (JGL_RENDERING_ROUTINE == J2D_FillRect) {
|
||||
if (!JGL_INSTANCED_RENDERING) { gl_Position = Default(); return; }
|
||||
|
||||
vec2 scaled = a_vertex_position * a_instance_size;
|
||||
vec2 world_pos = scaled + a_instance_position;
|
||||
gl_Position = gl_ModelViewProjectionMatrix * vec4(world_pos, 0.0, 1.0);
|
||||
v_color = a_instance_color;
|
||||
}
|
||||
else { gl_Position = Default(); }
|
||||
if (JGL_RENDERING_ROUTINE == J2D_FillRect && JGL_INSTANCED_RENDERING)
|
||||
gl_Position = FillRectInstanced();
|
||||
else
|
||||
gl_Position = Default();
|
||||
}
|
||||
|
@@ -20,14 +20,14 @@ namespace JGL {
|
||||
};
|
||||
}
|
||||
|
||||
/// A wrapped for "Vertex Buffer Object" In OpenGL, Store things in VRam.
|
||||
/// A wrapped for "Vertex Buffer Object" In OpenGL, Store things in V-ram.
|
||||
class JGL::VRamList {
|
||||
private:
|
||||
VRamUsageHint usage_hint = Fixed;
|
||||
GLuint list_handle = 0;
|
||||
long num_elements = 0;
|
||||
long byte_count = 0;
|
||||
bool element_array_buffer = false;
|
||||
bool spin_lock = false;
|
||||
// TODO mutex lock.
|
||||
void load(const GLfloat* data, const long& size);
|
||||
void load(const GLuint* data, const long& size);
|
||||
void SetData(void* data, const long& count);
|
||||
@@ -40,23 +40,27 @@ public:
|
||||
VRamList(const Vector3* data, const long& count, VRamUsageHint hint = Fixed);
|
||||
VRamList(const Vector4* data, const long& count, VRamUsageHint hint = Fixed);
|
||||
VRamList(const Color4* data, const long& count, VRamUsageHint hint = Fixed);
|
||||
/// Allocate an empty VBO
|
||||
/// @param byte_count the size of the buffer in bytes.
|
||||
/// @param element_array_buffer if applicable, whether the buffer is to be used for GLuint indices.
|
||||
/// @param hint A hint to the graphics driver for what the buffer is to be used for.
|
||||
VRamList(const size_t& byte_count, bool element_array_buffer, VRamUsageHint hint = Fixed);
|
||||
|
||||
~VRamList();
|
||||
/** Copying around the VBO data to a new VBO like this is slow.
|
||||
* Pass to function by const reference or pointer always. */
|
||||
VRamList(const VRamList& rhs);
|
||||
VRamList() : list_handle(0), num_elements(0), element_array_buffer(false), spin_lock(false) {}
|
||||
VRamList() : list_handle(0), byte_count(0), element_array_buffer(false) {}
|
||||
public:
|
||||
[[nodiscard]] GLuint GetHandle() const;
|
||||
[[nodiscard]] GLuint Handle() const;
|
||||
/// Returns the number of elements in the list.
|
||||
[[nodiscard]] long GetLength() const;
|
||||
[[nodiscard]] long Length() const;
|
||||
/// Returns the size of the data in bytes.
|
||||
[[nodiscard]] size_t GetDataSize() const;
|
||||
[[nodiscard]] size_t Size() const;
|
||||
/** Get VBO data back from the GPU. This is *bad* because the CPU is going to wait
|
||||
* for the transfer to finish. Has limited use other than testing. */
|
||||
[[nodiscard]] std::vector<GLfloat> GetDataF() const;
|
||||
[[nodiscard]] std::vector<GLuint> GetDataUI() const;
|
||||
[[nodiscard]] bool IsFloatArray() const;
|
||||
/** Replace the data of an existing VBO in it's entirety. Must be same type. */
|
||||
void SetData(const GLfloat* data, const long& count);
|
||||
void SetData(const Vector2* data, const long& count);
|
||||
@@ -70,6 +74,8 @@ public:
|
||||
/** Update only a portion of the data in a VBO. Must be same type.
|
||||
* "offset" refers the number of Typename T into the buffer the data you want to change is.
|
||||
* For ex, offset 0 and length of 1 overwrites the first value. Offset 1 the second etc */
|
||||
|
||||
// TODO provide a bool to specify whether the current buffer should be orphaned.
|
||||
void UpdateData(const GLfloat* data, const long& offset, const long& count);
|
||||
void UpdateData(const Vector2* data, const long& offset, const long& count);
|
||||
void UpdateData(const Vector3* data, const long& offset, const long& count);
|
||||
@@ -77,4 +83,8 @@ public:
|
||||
void UpdateData(const Color4* data, const long& offset, const long& count);
|
||||
void UpdateData(const GLuint* data, const long& offset, const long& count);
|
||||
void UpdateData(const Vector2i* data, const long& offset, const long& count);
|
||||
|
||||
// Update only a portion of the data in a VBO using bytes.
|
||||
// TODO This version of the function has no protection for out of bounds writes.
|
||||
void UpdateData(const uint8_t* data, const long& offset, const long& count);
|
||||
};
|
2
main.cpp
2
main.cpp
@@ -142,7 +142,7 @@ public:
|
||||
shader = new Shader(std::filesystem::path("assets/shader_programs/test_vertex.glsl"), std::filesystem::path("assets/shader_programs/test_fragment.glsl"),
|
||||
{{"a_vertex_position", 0}, {"a_instance_position", 1}, {"a_instance_size", 2}, {"a_instance_color", 3}});
|
||||
|
||||
for (unsigned int i = 0; i < 100; i++) {
|
||||
for (unsigned int i = 0; i < 1000000; i++) {
|
||||
rect_pos.emplace_back(420, 420);
|
||||
rect_size.emplace_back(20, 20);
|
||||
rect_colors.emplace_back(Colors::Red);
|
||||
|
@@ -275,7 +275,7 @@ void J2D::BatchFillRect(const Color4* colors, const Vector2* positions, const Ve
|
||||
if (current_state.current_shader)
|
||||
current_state.current_shader->SetInt("JGL_RENDERING_ROUTINE", RENDERING_ROUTINE_ID::J2D_FillRect);
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, ShapeCache::square_origin_topleft_vertex_data->GetHandle());
|
||||
glBindBuffer(GL_ARRAY_BUFFER, ShapeCache::square_origin_topleft_vertex_data->Handle());
|
||||
|
||||
if (rect_count == 1 || !supports_instanced || !current_state.current_shader) {
|
||||
glVertexPointer(2, GL_FLOAT, sizeof(Vector2), nullptr);
|
||||
@@ -294,20 +294,20 @@ void J2D::BatchFillRect(const Color4* colors, const Vector2* positions, const Ve
|
||||
else {
|
||||
current_state.current_shader->SetBool("JGL_INSTANCED_RENDERING", true);
|
||||
|
||||
std::vector<Instance> instances;
|
||||
instances.reserve(rect_count);
|
||||
|
||||
std::vector<Instance> instances(rect_count);
|
||||
Instance* ptr = instances.data();
|
||||
// Shader does translation to top left corner in this path - Redacted.
|
||||
for (size_t i = 0; i < rect_count; ++i)
|
||||
instances.emplace_back(Vector2(positions[i].x, positions[i].y + sizes[i].y), sizes[i], colors[i]);
|
||||
ptr[i] = { positions[i], sizes[i], colors[i] };
|
||||
|
||||
glEnableVertexAttribArray(0);
|
||||
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(Vector2), nullptr);
|
||||
glVertexAttribDivisorARB(0, 0);
|
||||
|
||||
// count * 5 because 4x float & 4x uint8_t = sizeof(float).
|
||||
VRamList instance_buffer((GLfloat*) instances.data(), instances.size() * 5, VRamUsageHint::Stream);
|
||||
// * 5 because 4x float & 4x uint8_t = sizeof(float) - Redacted.
|
||||
VRamList instance_buffer((GLfloat*) instances.data(), instances.size() * 5, Stream);
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, instance_buffer.GetHandle());
|
||||
glBindBuffer(GL_ARRAY_BUFFER, instance_buffer.Handle());
|
||||
glEnableVertexAttribArray(1);
|
||||
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(Instance), (GLvoid*) offsetof(Instance, position));
|
||||
glVertexAttribDivisorARB(1, 1);
|
||||
@@ -1255,10 +1255,10 @@ void J2D::DrawPoints(const Color4* colors, const Vector2* points, int point_coun
|
||||
|
||||
glPointSize(radius);
|
||||
glEnableClientState(GL_COLOR_ARRAY);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, ShapeCache::draw_points_colors->GetHandle());
|
||||
glBindBuffer(GL_ARRAY_BUFFER, ShapeCache::draw_points_colors->Handle());
|
||||
glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(Color4), nullptr);
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, ShapeCache::draw_points_positions->GetHandle());
|
||||
glBindBuffer(GL_ARRAY_BUFFER, ShapeCache::draw_points_positions->Handle());
|
||||
glVertexPointer(2, GL_FLOAT, sizeof(Vector2), nullptr);
|
||||
|
||||
if (current_state.current_shader)
|
||||
@@ -1278,7 +1278,7 @@ void J2D::DrawPoints(const Color4& color, const Vector2* points, int point_count
|
||||
ShapeCache::draw_points_positions->SetData(points, point_count);
|
||||
glPointSize(radius);
|
||||
glColor4ubv(color.ptr());
|
||||
glBindBuffer(GL_ARRAY_BUFFER, ShapeCache::draw_points_positions->GetHandle());
|
||||
glBindBuffer(GL_ARRAY_BUFFER, ShapeCache::draw_points_positions->Handle());
|
||||
glVertexPointer(2, GL_FLOAT, sizeof(Vector2), nullptr);
|
||||
|
||||
if (current_state.current_shader)
|
||||
|
@@ -238,7 +238,7 @@ void JGL::J3D::BatchWireframeRevoSphere(const Color4& color, const Sphere* spher
|
||||
|
||||
// TODO allocate once.
|
||||
VRamList vertex_data(vertices.data(), vertices.size());
|
||||
glBindBuffer(GL_ARRAY_BUFFER, vertex_data.GetHandle());
|
||||
glBindBuffer(GL_ARRAY_BUFFER, vertex_data.Handle());
|
||||
glVertexPointer(3, GL_FLOAT, sizeof(Vector3), nullptr);
|
||||
|
||||
// Render each sphere in the batch at their given position and radius.
|
||||
@@ -246,10 +246,10 @@ void JGL::J3D::BatchWireframeRevoSphere(const Color4& color, const Sphere* spher
|
||||
glPushMatrix();
|
||||
glTranslatef(spheres[i].Position.x, spheres[i].Position.y, spheres[i].Position.z);
|
||||
glScalef(spheres[i].Radius, spheres[i].Radius, spheres[i].Radius);
|
||||
glDrawArrays(GL_LINE_LOOP, 0, vertex_data.GetLength());
|
||||
glDrawArrays(GL_LINE_LOOP, 0, vertex_data.Length());
|
||||
if (draw_stacks)
|
||||
glRotatef(90, 0, 1, 0),
|
||||
glDrawArrays(GL_LINE_LOOP, 0, vertex_data.GetLength());
|
||||
glDrawArrays(GL_LINE_LOOP, 0, vertex_data.Length());
|
||||
glPopMatrix();
|
||||
}
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
@@ -372,9 +372,9 @@ void JGL::J3D::BatchWireframeAABB(const Color4& color, const AABB* boxes, const
|
||||
glColor4ubv(color.ptr());
|
||||
glLineWidth(thickness);
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, ShapeCache::cube_vertex_data->GetHandle());
|
||||
glBindBuffer(GL_ARRAY_BUFFER, ShapeCache::cube_vertex_data->Handle());
|
||||
glVertexPointer(3, GL_FLOAT, sizeof(Vector3), nullptr);
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ShapeCache::cube_index_data->GetHandle());
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ShapeCache::cube_index_data->Handle());
|
||||
|
||||
for (size_t i = 0; i < box_count; i++) {
|
||||
Vector3 delta = (boxes[i].maxPoint - boxes[i].minPoint) / 2;
|
||||
@@ -382,7 +382,7 @@ void JGL::J3D::BatchWireframeAABB(const Color4& color, const AABB* boxes, const
|
||||
glPushMatrix();
|
||||
glTranslatef(center.x, center.y, center.z);
|
||||
glScalef(delta.x, delta.y, delta.z);
|
||||
glDrawElements(GL_LINE_LOOP, ShapeCache::cube_index_data->GetLength(), GL_UNSIGNED_INT, nullptr);
|
||||
glDrawElements(GL_LINE_LOOP, ShapeCache::cube_index_data->Length(), GL_UNSIGNED_INT, nullptr);
|
||||
glPopMatrix();
|
||||
}
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
@@ -397,9 +397,9 @@ void JGL::J3D::WireframeAABB(const Color4& color, const Vector3& pos, const Vect
|
||||
void JGL::J3D::BatchFillAABB(const Color4& color, const AABB* boxes, const size_t& box_count) {
|
||||
glColor4ubv(color.ptr());
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, ShapeCache::cube_vertex_data->GetHandle());
|
||||
glBindBuffer(GL_ARRAY_BUFFER, ShapeCache::cube_vertex_data->Handle());
|
||||
glVertexPointer(3, GL_FLOAT, sizeof(Vector3), nullptr);
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ShapeCache::cube_index_data->GetHandle());
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ShapeCache::cube_index_data->Handle());
|
||||
|
||||
glEnable(GL_CULL_FACE);
|
||||
glCullFace(GL_BACK);
|
||||
@@ -408,7 +408,7 @@ void JGL::J3D::BatchFillAABB(const Color4& color, const AABB* boxes, const size_
|
||||
if (UsingLighting()) {
|
||||
using_lights = true;
|
||||
glEnableClientState(GL_NORMAL_ARRAY);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, ShapeCache::cube_normal_data->GetHandle());
|
||||
glBindBuffer(GL_ARRAY_BUFFER, ShapeCache::cube_normal_data->Handle());
|
||||
glNormalPointer(GL_FLOAT, sizeof(float), nullptr);
|
||||
}
|
||||
|
||||
@@ -487,8 +487,8 @@ void JGL::J3D::BatchFillSphere(const Color4& color, const Sphere* spheres, const
|
||||
VRamList index_data(indices.data(), indices.size());
|
||||
|
||||
glColor4ubv(color.ptr());
|
||||
glBindBuffer(GL_ARRAY_BUFFER, vertex_data.GetHandle());
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, index_data.GetHandle());
|
||||
glBindBuffer(GL_ARRAY_BUFFER, vertex_data.Handle());
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, index_data.Handle());
|
||||
glVertexPointer(3, GL_FLOAT, sizeof(Vector3), nullptr);
|
||||
|
||||
for (size_t i = 0; i < sphere_count; i++) {
|
||||
@@ -496,7 +496,7 @@ void JGL::J3D::BatchFillSphere(const Color4& color, const Sphere* spheres, const
|
||||
glPushMatrix();
|
||||
glTranslatef(position.x, position.y, position.z);
|
||||
glScalef(spheres[i].Radius, spheres[i].Radius, spheres[i].Radius);
|
||||
glDrawElements(GL_TRIANGLES, index_data.GetLength(), GL_UNSIGNED_INT, nullptr);
|
||||
glDrawElements(GL_TRIANGLES, index_data.Length(), GL_UNSIGNED_INT, nullptr);
|
||||
glPopMatrix();
|
||||
}
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
@@ -567,13 +567,13 @@ void JGL::J3D::DrawVertexArray(const Color4& color, const VertexArray& vertex_ar
|
||||
glColor4ubv(color.ptr());
|
||||
|
||||
glEnableClientState(GL_VERTEX_ARRAY);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, vertex_array.GetVertices()->GetHandle());
|
||||
glBindBuffer(GL_ARRAY_BUFFER, vertex_array.GetVertices()->Handle());
|
||||
glVertexPointer(3, GL_FLOAT, 0, nullptr);
|
||||
|
||||
glPushMatrix();
|
||||
glTranslatef(position.x, position.y, position.z);
|
||||
//glScalef(1,1,1);
|
||||
glDrawArrays(GL_TRIANGLES, 0, vertex_array.GetVertices()->GetLength());
|
||||
glDrawArrays(GL_TRIANGLES, 0, vertex_array.GetVertices()->Length());
|
||||
glPopMatrix();
|
||||
|
||||
//glDrawElements(GL_LINES, vertex_array.GetIndices()->GetLength(), GL_UNSIGNED_INT, nullptr);
|
||||
|
@@ -105,6 +105,7 @@ namespace JGL {
|
||||
Color4 color;
|
||||
public:
|
||||
Instance(const Vector2& position, const Vector2& size, const Color4& color) : position(position), size(size), color(color) {}
|
||||
Instance() = default;
|
||||
~Instance() = default;
|
||||
};
|
||||
}
|
||||
|
@@ -5,7 +5,6 @@
|
||||
// TODO combine the two load functions.
|
||||
|
||||
void JGL::VRamList::load(const GLfloat* data, const long& size) {
|
||||
spin_lock = true;
|
||||
|
||||
GLint current_array_buffer = 0;
|
||||
glGetIntegerv(GL_ARRAY_BUFFER_BINDING, ¤t_array_buffer);
|
||||
@@ -14,14 +13,12 @@ void JGL::VRamList::load(const GLfloat* data, const long& size) {
|
||||
glBufferData(GL_ARRAY_BUFFER, size, data, usage_hint);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, current_array_buffer);
|
||||
element_array_buffer = false;
|
||||
num_elements = size / sizeof(GLfloat);
|
||||
byte_count = size;
|
||||
|
||||
spin_lock = false;
|
||||
}
|
||||
|
||||
|
||||
void JGL::VRamList::load(const GLuint* data, const long& size) {
|
||||
spin_lock = true;
|
||||
|
||||
GLint current_element_array_buffer = 0;
|
||||
glGetIntegerv(GL_ELEMENT_ARRAY_BUFFER_BINDING, ¤t_element_array_buffer);
|
||||
@@ -30,17 +27,14 @@ void JGL::VRamList::load(const GLuint* data, const long& size) {
|
||||
glBufferData(GL_ELEMENT_ARRAY_BUFFER, size, data, usage_hint);
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, current_element_array_buffer);
|
||||
element_array_buffer = true;
|
||||
num_elements = size / sizeof(GLuint);
|
||||
byte_count = size;
|
||||
|
||||
spin_lock = false;
|
||||
}
|
||||
|
||||
void JGL::VRamList::Erase() {
|
||||
if (list_handle == 0)
|
||||
return;
|
||||
|
||||
while (spin_lock) {}
|
||||
spin_lock = true;
|
||||
|
||||
GLint current_element_array_buffer = 0;
|
||||
glGetIntegerv(GL_ELEMENT_ARRAY_BUFFER_BINDING, ¤t_element_array_buffer);
|
||||
@@ -56,37 +50,28 @@ void JGL::VRamList::Erase() {
|
||||
glDeleteBuffers(1, &list_handle);
|
||||
list_handle = 0;
|
||||
|
||||
spin_lock = false;
|
||||
}
|
||||
|
||||
GLuint JGL::VRamList::GetHandle() const {
|
||||
GLuint JGL::VRamList::Handle() const {
|
||||
return list_handle;
|
||||
}
|
||||
|
||||
bool JGL::VRamList::IsFloatArray() const {
|
||||
return !element_array_buffer;
|
||||
long JGL::VRamList::Length() const {
|
||||
// sizeof(GLfloat) & sizeof(GLuint) are both 4 - Redacted.
|
||||
return byte_count / 4;
|
||||
}
|
||||
|
||||
long JGL::VRamList::GetLength() const {
|
||||
return num_elements;
|
||||
}
|
||||
|
||||
size_t JGL::VRamList::GetDataSize() const {
|
||||
if (element_array_buffer)
|
||||
return sizeof(GLuint) * num_elements;
|
||||
return sizeof(GLfloat) * num_elements;
|
||||
size_t JGL::VRamList::Size() const {
|
||||
return byte_count;
|
||||
}
|
||||
|
||||
void JGL::VRamList::SetData(void* data, const long& length) {
|
||||
|
||||
while (spin_lock) {}
|
||||
spin_lock = true;
|
||||
|
||||
bool should_resize = (this->num_elements != length);
|
||||
bool should_resize = (this->byte_count != length * 4);
|
||||
if (should_resize) {
|
||||
glDeleteBuffers(1, &list_handle);
|
||||
list_handle = 0;
|
||||
spin_lock = false;
|
||||
element_array_buffer ? load((GLuint*) data, sizeof(GLuint) * length) : load((GLfloat*) data, sizeof(GLfloat) * length);
|
||||
return;
|
||||
}
|
||||
@@ -110,21 +95,18 @@ void JGL::VRamList::SetData(void* data, const long& length) {
|
||||
|
||||
glBindBuffer(buffer_type, current_buffer);
|
||||
|
||||
spin_lock = false;
|
||||
}
|
||||
|
||||
void JGL::VRamList::UpdateData(void* data, const long& offset, const long& length) {
|
||||
while (spin_lock) {}
|
||||
spin_lock = true;
|
||||
|
||||
if (offset + length > num_elements) {
|
||||
unsigned long oob_delta = (offset + length) - num_elements;
|
||||
if (offset + length > Length()) {
|
||||
unsigned long oob_delta = (offset + length) - Length();
|
||||
|
||||
if (element_array_buffer) {
|
||||
auto list_data = GetDataUI();
|
||||
list_data.resize(list_data.size() + oob_delta);
|
||||
memcpy(list_data.data() + (offset * sizeof(GLuint)), data, length * sizeof(GLuint));
|
||||
spin_lock = false; // This is going unlock and relock really fast, But this code fixes something considered wrong anyway - Redacted.
|
||||
// This is going unlock and relock really fast, But this code fixes something considered wrong anyway - Redacted.
|
||||
return SetData(list_data.data(), list_data.size());
|
||||
}
|
||||
|
||||
@@ -132,7 +114,6 @@ void JGL::VRamList::UpdateData(void* data, const long& offset, const long& lengt
|
||||
auto list_data = GetDataF();
|
||||
list_data.resize(list_data.size() + oob_delta);
|
||||
memcpy(list_data.data() + (offset * sizeof(GLfloat)), data, length * sizeof(GLfloat));
|
||||
spin_lock = false;
|
||||
return SetData(list_data.data(), list_data.size());
|
||||
}
|
||||
|
||||
@@ -149,17 +130,11 @@ void JGL::VRamList::UpdateData(void* data, const long& offset, const long& lengt
|
||||
|
||||
size_t element_size = element_array_buffer ? sizeof(GLuint) : sizeof(GLfloat);
|
||||
|
||||
if (usage_hint != Fixed)
|
||||
glBufferData(GL_ARRAY_BUFFER, length * element_size, nullptr, usage_hint);
|
||||
glBufferSubData(buffer_type, offset * element_size, length * element_size, data);
|
||||
|
||||
glBindBuffer(buffer_type, current_buffer);
|
||||
|
||||
spin_lock = false;
|
||||
}
|
||||
|
||||
std::vector<GLfloat> JGL::VRamList::GetDataF() const {
|
||||
while (spin_lock) {}
|
||||
|
||||
if (element_array_buffer)
|
||||
JGL::Logger::Warning("Getting data as GLfloat but the buffer data is GLuint?");
|
||||
@@ -172,12 +147,12 @@ std::vector<GLfloat> JGL::VRamList::GetDataF() const {
|
||||
glGetIntegerv(GL_ARRAY_BUFFER_BINDING, ¤t_buffer);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, list_handle);
|
||||
|
||||
std::vector<GLfloat> data(num_elements);
|
||||
std::vector<GLfloat> data(byte_count / 4);
|
||||
void* vram = glMapBuffer(GL_ARRAY_BUFFER, GL_READ_ONLY);
|
||||
if (vram == nullptr)
|
||||
JGL::Logger::Fatal("Mapping in a VBO that doesn't exist?");
|
||||
|
||||
memcpy(data.data(), vram, num_elements * sizeof(GLfloat));
|
||||
memcpy(data.data(), vram, (byte_count / 4) * sizeof(GLfloat));
|
||||
|
||||
glUnmapBuffer(GL_ARRAY_BUFFER);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, current_buffer);
|
||||
@@ -188,7 +163,6 @@ std::vector<GLfloat> JGL::VRamList::GetDataF() const {
|
||||
}
|
||||
|
||||
std::vector<GLuint> JGL::VRamList::GetDataUI() const {
|
||||
while (spin_lock) {}
|
||||
|
||||
if (!element_array_buffer)
|
||||
JGL::Logger::Warning("Getting data as GLuint but the buffer data is GLfloat?");
|
||||
@@ -197,13 +171,13 @@ std::vector<GLuint> JGL::VRamList::GetDataUI() const {
|
||||
glGetIntegerv(GL_ELEMENT_ARRAY_BUFFER_BINDING, ¤t_buffer);
|
||||
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, list_handle);
|
||||
std::vector<GLuint> data(num_elements);
|
||||
std::vector<GLuint> data(byte_count / 4);
|
||||
|
||||
void* vram = glMapBuffer(GL_ELEMENT_ARRAY_BUFFER, GL_READ_ONLY);
|
||||
if (vram == nullptr)
|
||||
JGL::Logger::Fatal("Mapping in a VBO that doesn't exist?");
|
||||
|
||||
memcpy(data.data(), vram, num_elements * sizeof(GLuint));
|
||||
memcpy(data.data(), vram, (byte_count / 4) * sizeof(GLuint));
|
||||
|
||||
glUnmapBuffer(GL_ELEMENT_ARRAY_BUFFER);
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, current_buffer);
|
||||
@@ -301,8 +275,6 @@ JGL::VRamList::~VRamList() {
|
||||
}
|
||||
|
||||
JGL::VRamList::VRamList(const JGL::VRamList& rhs) {
|
||||
while (rhs.spin_lock) {}
|
||||
|
||||
if (rhs.element_array_buffer) {
|
||||
auto data_array = rhs.GetDataUI();
|
||||
this->load(data_array.data(), data_array.size());
|
||||
@@ -311,3 +283,32 @@ JGL::VRamList::VRamList(const JGL::VRamList& rhs) {
|
||||
auto data_array = rhs.GetDataF();
|
||||
this->load(data_array.data(), data_array.size());
|
||||
}
|
||||
|
||||
JGL::VRamList::VRamList(const size_t& byte_count, bool element_array_buffer, VRamUsageHint hint) {
|
||||
this->element_array_buffer = element_array_buffer;
|
||||
this->byte_count = byte_count;
|
||||
|
||||
GLint current_buffer = 0;
|
||||
GLenum buffer = element_array_buffer ? GL_ELEMENT_ARRAY_BUFFER : GL_ARRAY_BUFFER;
|
||||
GLenum buffer_binding = element_array_buffer ? GL_ELEMENT_ARRAY_BUFFER_BINDING : GL_ARRAY_BUFFER_BINDING;
|
||||
|
||||
glGetIntegerv(buffer_binding, ¤t_buffer);
|
||||
glGenBuffers(1, &list_handle);
|
||||
glBindBuffer(buffer, list_handle);
|
||||
glBufferData(buffer, byte_count, nullptr, hint);
|
||||
|
||||
glBindBuffer(buffer, current_buffer);
|
||||
}
|
||||
|
||||
void JGL::VRamList::UpdateData(const uint8_t* data, const long& offset, const long& count) {
|
||||
GLint current_buffer = 0;
|
||||
GLenum buffer_type = element_array_buffer ? GL_ELEMENT_ARRAY_BUFFER : GL_ARRAY_BUFFER;
|
||||
GLenum buffer_binding = element_array_buffer ? GL_ELEMENT_ARRAY_BUFFER_BINDING : GL_ARRAY_BUFFER_BINDING;
|
||||
|
||||
glGetIntegerv(buffer_binding, ¤t_buffer);
|
||||
glBindBuffer(buffer_type, list_handle);
|
||||
|
||||
glBufferSubData(buffer_type, offset, count, data);
|
||||
|
||||
glBindBuffer(buffer_type, current_buffer);
|
||||
}
|
||||
|
Reference in New Issue
Block a user