40#ifndef GLUP_BUFFER_OBJECT_H
41#define GLUP_BUFFER_OBJECT_H
51#define GEO_TBO_SUPPORTED
52#define GEO_SSBO_SUPPORTED
66#ifdef GEO_OS_EMSCRIPTEN
67 #ifdef GEO_TBO_SUPPORTED
68 #undef GEO_TBO_SUPPORTED
70 #ifdef GEO_SSBO_SUPPORTED
71 #undef GEO_SSBO_SUPPORTED
76 #ifdef GEO_SSBO_SUPPORTED
77 #undef GEO_SSBO_SUPPORTED
81#ifdef GEO_TBO_SUPPORTED
84#define GEO_TBO_FUNC [[noreturn]]
87#ifdef GEO_SSBO_SUPPORTED
90#define GEO_SSBO_FUNC [[noreturn]]
104 enum Type {UNINITIALIZED, TEXTURE_2D, TBO, SSBO};
135 return type_ != UNINITIALIZED;
175 index_t nb,
const T* data, GLuint unit=0
186 create_or_update_TEXTURE_2D(nb, data);
189 create_or_update_TBO(nb,data);
192 create_or_update_SSBO(nb,data);
209 const std::vector<T>& v,
index_t unit=0
252 GLuint program,
const char* name, GLuint unit
256 static void active_unit_TEXTURE_2D(GLuint unit);
257 GEO_TBO_FUNC
static void active_unit_TBO(GLuint unit);
258 GEO_SSBO_FUNC
static void active_unit_SSBO(GLuint unit);
260 void reset_TEXTURE_2D();
261 GEO_TBO_FUNC
void reset_TBO();
262 GEO_SSBO_FUNC
void reset_SSBO();
264 void bind_TEXTURE_2D(GLuint unit);
265 GEO_TBO_FUNC
void bind_TBO(GLuint unit);
266 GEO_SSBO_FUNC
void bind_SSBO(GLuint unit);
268 template <
class T>
void create_or_update_TEXTURE_2D(
274 glGenTextures(1, &texture_);
276 glBindTexture(GL_TEXTURE_2D, texture_);
277 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
278 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
279 GLsizei width = GLsizei(nb);
282 width = GLsizei(::sqrt(nb));
283 height = GLsizei(nb) / width;
284 while(width*height < GLsizei(nb)) {
288 glPixelStorei(GL_PACK_ALIGNMENT, 1);
289 if(width_ != width || height_ != height) {
293 GLint(internal_format(T())),
302 if(GLsizei(nb) == width*height) {
327 (GLsizei(nb) % width), 1,
329 data + (height-1)*width
334 template <
class T> GEO_TBO_FUNC
void create_or_update_TBO(
335 index_t nb,
const T* data
337#ifdef GEO_TBO_SUPPORTED
342 BO_, GL_ARRAY_BUFFER,
343 nb *
sizeof(T), data,
347 glGenTextures(1, &texture_);
348 glBindTexture(GL_TEXTURE_BUFFER, texture_);
349 glTexBuffer(GL_TEXTURE_BUFFER, internal_format(T()), BO_);
358 template <
class T> GEO_SSBO_FUNC
void create_or_update_SSBO(
359 index_t nb,
const T* data
361#ifdef GEO_SSBO_SUPPORTED
366 BO_, GL_SHADER_STORAGE_BUFFER,
367 nb *
sizeof(T), data,
379 static GLuint internal_format(uint32_t) {
return GL_R32UI; }
380 static GLuint internal_format(int32_t) {
return GL_R32I; }
381 static GLuint internal_format(vec2i) {
return GL_RG32I; }
382 static GLuint internal_format(vec3i) {
return GL_RGB32I; }
383 static GLuint internal_format(vec4i) {
return GL_RGBA32I; }
384 static GLuint internal_format(
float) {
return GL_R32F; }
385 static GLuint internal_format(vec2f) {
return GL_RG32F; }
386 static GLuint internal_format(vec3f) {
return GL_RGB32F; }
387 static GLuint internal_format(vec4f) {
return GL_RGBA32F; }
389 static GLuint
format(uint32_t) {
return GL_RED_INTEGER; }
390 static GLuint
format(int32_t) {
return GL_RED_INTEGER; }
391 static GLuint
format(vec2i) {
return GL_RG_INTEGER; }
392 static GLuint
format(vec3i) {
return GL_RGB_INTEGER; }
393 static GLuint
format(vec4i) {
return GL_RGBA_INTEGER; }
394 static GLuint
format(
float) {
return GL_RED; }
395 static GLuint
format(vec2f) {
return GL_RG; }
396 static GLuint
format(vec3f) {
return GL_RGB; }
397 static GLuint
format(vec4f) {
return GL_RGBA; }
399 static GLuint type(uint32_t) {
return GL_UNSIGNED_INT; }
400 static GLuint type(int32_t) {
return GL_INT; }
401 static GLuint type(vec2i) {
return GL_INT; }
402 static GLuint type(vec3i) {
return GL_INT; }
403 static GLuint type(vec4i) {
return GL_INT; }
404 static GLuint type(
float) {
return GL_FLOAT; }
405 static GLuint type(vec2f) {
return GL_FLOAT; }
406 static GLuint type(vec3f) {
return GL_FLOAT; }
407 static GLuint type(vec4f) {
return GL_FLOAT; }
411 Type type_ = UNINITIALIZED;
417 static bool class_initialized_;
418 static bool GLSL_sources_registered_;
419 static GLint max_TBO_size_;
420 static GLint max_texture_size_;
421 static Type default_type_;
422 static GLuint active_unit_;
GLUP: GL Useful Primitives.
#define geo_assert_not_reached
Sets a non reachable point in the program.
#define geo_assert(x)
Verifies that a condition is met.
#define geo_debug_assert(x)
Verifies that a condition is met.
Vector with aligned memory allocation.
T * data()
Gets a pointer to the array of elements.
index_t size() const
Gets the number of elements.
void create_or_update(const std::vector< T > &v, index_t unit=0)
Creates an uninitialized BufferObject or updates an initialized one with new data.
static void initialize_class()
Initializes static class variables.
bool initialized() const
Tests whether a BufferObject is initialized.
static void active_unit(GLuint unit)
Active_Units a unit.
static Type default_type()
Gets the default type for BufferObjects.
void create_or_update(const GEO::vector< T > &v, index_t unit=0)
Creates an uninitialized BufferObject or updates an initialized one with new data.
static void register_GLSL_sources()
Registers GLSL sources for using portable wrappers around buffers. \TODOC.
BufferObject()
BufferObject constructor.
void create_or_update(index_t nb, const T *data, GLuint unit=0)
Creates an uninitialized BufferObject or updates an initialized one with new data.
void bind(GLuint unit=0)
Binds this buffer to a unit.
~BufferObject()
BufferObject destructor.
static void set_program_buffer_unit_by_name(GLuint program, const char *name, GLuint unit)
Sets a program uniform buffer unit.
void reset()
Resets a BufferObject.
Type type() const
Gets the type of a BufferObject.
Geometric functions in 2d and 3d.
#define GEOGRAM_GFX_API
Linkage declaration for geogram symbols.
Common include file, providing basic definitions. Should be included before anything else by all head...
Generic logging mechanism.
Types and functions for memory manipulation.
void GEOGRAM_GFX_API update_or_check_buffer_object(GLuint &buffer_id, GLenum target, size_t new_size, const void *data, bool update)
Updates the content of an OpenGL buffer object, and resizes it if need be, or tests whether it has th...
void geo_argused(const T &)
Suppresses compiler warnings about unused parameters.
geo_index_t index_t
The type for storing and manipulating indices.
std::string format(const char *format,...)
Creates a string from a format string and additional arguments. Works like sprintf().