Get VBO data back from the GPU.
Some checks failed
Run ReCI Build Test / Explore-Gitea-Actions (push) Failing after 7m17s

This commit is contained in:
2024-09-19 10:55:13 -04:00
parent 5fc4914180
commit b86377a092
2 changed files with 76 additions and 4 deletions

View File

@@ -3,7 +3,10 @@
#include <J3ML/LinearAlgebra/Vector2.hpp>
#include <J3ML/LinearAlgebra/Vector3.hpp>
#include <J3ML/LinearAlgebra/Vector4.hpp>
#include <JGL/logger/logger.h>
#include <type_traits>
#include <vector>
#include <cstring>
#include <string>
namespace JGL {
@@ -11,10 +14,12 @@ namespace JGL {
}
/// A wrapper for VBO, Storing texture coordinates or vertices or indices in vram.
/// For J2D, We can't really store the vertices into vram. But we can store the texture coordinates.
class JGL::VRamList {
private:
GLuint list_handle = 0;
long size = 0;
bool element_array_buffer = false;
void load(const GLfloat* data, const long& size);
void load(const GLuint* data, const long& size);
public:
@@ -26,5 +31,38 @@ public:
VRamList(const GLuint* data, const long& size);
public:
[[nodiscard]] GLuint GetHandle() const;
/// Returns the number of GLfloat or GLuint in the list.
[[nodiscard]] long GetSize() const;
/// Returns the size of the data in bytes.
[[nodiscard]] long GetDataSize() const;
[[nodiscard]] bool IsIntegerArray() const;
[[nodiscard]] bool IsFloatArray() const;
void Erase();
/// Get list data back from the GPU. This is *very* slow.
/// It's not recommended you use this in your normal rendering routines.
template <typename T>
[[nodiscard]] std::vector<T> GetListData() const {
GLenum buffer_type;
GLint current_buffer = 0;
if constexpr (std::is_same<T, GLfloat>::value)
buffer_type = GL_ARRAY_BUFFER,
glGetIntegerv(GL_ARRAY_BUFFER_BINDING, &current_buffer);
else if constexpr (std::is_same<T, GLuint>::value)
buffer_type = GL_ELEMENT_ARRAY_BUFFER,
glGetIntegerv(GL_ELEMENT_ARRAY_BUFFER_BINDING, &current_buffer);
else {
Logger::Fatal("Typename T must be either GLfloat or GLuint.");
exit(-1);
}
glBindBuffer(buffer_type, list_handle);
std::vector<T> data(size);
memcpy(data.data(), (T*) glMapBuffer(buffer_type, GL_READ_ONLY), size * sizeof(T));
glUnmapBuffer(buffer_type);
glBindBuffer(buffer_type, current_buffer);
return data;
}
};

View File

@@ -1,26 +1,31 @@
#include <JGL/types/VRamList.h>
#include <jlog/Logger.hpp>
#include <cstring>
void JGL::VRamList::load(const GLfloat* data, const long& size) {
void JGL::VRamList::load(const GLfloat* data, const long& s) {
GLint current_array_buffer = 0;
glGetIntegerv(GL_ARRAY_BUFFER_BINDING, &current_array_buffer);
glGenBuffers(1, &list_handle);
glBindBuffer(GL_ARRAY_BUFFER, list_handle);
glBufferData(GL_ARRAY_BUFFER, size, data, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, current_array_buffer);
size = s;
}
void JGL::VRamList::load(const GLuint* data, const long& size) {
void JGL::VRamList::load(const GLuint* data, const long& s) {
GLint current_element_array_buffer = 0;
glGetIntegerv(GL_ELEMENT_ARRAY_BUFFER_BINDING, &current_element_array_buffer);
glGenBuffers(1, &list_handle);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, list_handle);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, size, data, GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, current_element_array_buffer);
element_array_buffer = true;
size = s;
}
JGL::VRamList::VRamList(const GLfloat* data, const long& size) {
load(data, (long) sizeof(GLfloat) * size);
long data_size = (long) sizeof(GLfloat) * size;
load(data, data_size);
}
JGL::VRamList::VRamList(const GLuint* data, const long& size) {
@@ -43,9 +48,38 @@ void JGL::VRamList::Erase() {
if (list_handle == 0)
jlog::Warning("Erasing an uninitialized array buffer?");
GLint current_element_array_buffer = 0;
glGetIntegerv(GL_ELEMENT_ARRAY_BUFFER_BINDING, &current_element_array_buffer);
GLint current_array_buffer = 0;
glGetIntegerv(GL_ARRAY_BUFFER_BINDING, &current_array_buffer);
if (element_array_buffer && current_element_array_buffer == list_handle)
jlog::Warning("Erasing an element array buffer while it's in use?");
else if (!element_array_buffer && current_array_buffer == list_handle)
jlog::Warning("Erasing an array buffer while it's in use?");
glDeleteBuffers(1, &list_handle);
}
GLuint JGL::VRamList::GetHandle() const {
return list_handle;
}
bool JGL::VRamList::IsIntegerArray() const {
return element_array_buffer;
}
bool JGL::VRamList::IsFloatArray() const {
return !element_array_buffer;
}
long JGL::VRamList::GetSize() const {
return size;
}
long JGL::VRamList::GetDataSize() const {
if (element_array_buffer)
return (long) sizeof(GLuint) * size;
return (long) sizeof(GLfloat) * size;
}