Geogram Version 1.10.2
A programming library of geometric algorithms
Loading...
Searching...
No Matches
GLUP_buffer_object.h
1/*
2 * Copyright (c) 2000-2022 Inria
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * * Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * * Neither the name of the ALICE Project-Team nor the names of its
14 * contributors may be used to endorse or promote products derived from this
15 * software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 * POSSIBILITY OF SUCH DAMAGE.
28 *
29 * Contact: Bruno Levy
30 *
31 * https://www.inria.fr/fr/bruno-levy
32 *
33 * Inria,
34 * Domaine de Voluceau,
35 * 78150 Le Chesnay - Rocquencourt
36 * FRANCE
37 *
38 */
39
40#ifndef GLUP_BUFFER_OBJECT_H
41#define GLUP_BUFFER_OBJECT_H
42
48#include <vector>
49#include <typeinfo>
50
51#define GEO_TBO_SUPPORTED
52#define GEO_SSBO_SUPPORTED
53
54// Note: for molecules with 2.5M atoms, it is FASTER
55// using standard texture than TBOs (with GEO_TBO_SUPPORTED *deactivated*
56// It seems that with TBOs it swaps between main memory and GPU
57// ... but for smaller molecules, TBOs seems worth it.
58// It seems to be the same thing with SSBOs (!)
59// Probably caused by the way I'm sending data to the buffer, to be revised...
60
61// 1HTQ (900K atoms)
62// textures: 15 fps
63// TBOS: 25.5 fps
64// SSBOS: 25.5 fps
65
66#ifdef GEO_OS_EMSCRIPTEN
67 #ifdef GEO_TBO_SUPPORTED
68 #undef GEO_TBO_SUPPORTED
69 #endif
70 #ifdef GEO_SSBO_SUPPORTED
71 #undef GEO_SSBO_SUPPORTED
72 #endif
73#endif
74
75#ifdef GEO_OS_APPLE
76 #ifdef GEO_SSBO_SUPPORTED
77 #undef GEO_SSBO_SUPPORTED
78 #endif
79#endif
80
81#ifdef GEO_TBO_SUPPORTED
82#define GEO_TBO_FUNC
83#else
84#define GEO_TBO_FUNC [[noreturn]]
85#endif
86
87#ifdef GEO_SSBO_SUPPORTED
88#define GEO_SSBO_FUNC
89#else
90#define GEO_SSBO_FUNC [[noreturn]]
91#endif
92
93namespace GLUP {
94 using namespace GEO;
95
103 public:
104 enum Type {UNINITIALIZED, TEXTURE_2D, TBO, SSBO};
105
111 }
112
117 reset();
118 }
119
124 Type type() const {
125 return type_;
126 }
127
134 bool initialized() const {
135 return type_ != UNINITIALIZED;
136 }
137
144
150 static void initialize_class();
151
156 static Type default_type();
157
163 void reset();
164
174 template <class T> void create_or_update(
175 index_t nb, const T* data, GLuint unit=0
176 ) {
177 if(unit != 0) {
178 active_unit(unit);
179 }
180 Type type = default_type();
181 switch(type) {
182 case UNINITIALIZED:
184 break;
185 case TEXTURE_2D:
186 create_or_update_TEXTURE_2D(nb, data);
187 break;
188 case TBO:
189 create_or_update_TBO(nb,data);
190 break;
191 case SSBO:
192 create_or_update_SSBO(nb,data);
193 break;
194 }
195 if(unit != 0) {
196 active_unit(0);
197 }
198 }
199
208 template <class T> void create_or_update(
209 const std::vector<T>& v, index_t unit=0
210 ) {
211 create_or_update(index_t(v.size()), v.data(), unit);
212 }
213
222 template <class T> void create_or_update(
223 const GEO::vector<T>& v, index_t unit=0
224 ) {
225 create_or_update(v.size(), v.data(), unit);
226 }
227
233 static void active_unit(GLuint unit);
234
241 void bind(GLuint unit=0);
242
252 GLuint program, const char* name, GLuint unit
253 );
254
255 protected:
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);
259
260 void reset_TEXTURE_2D();
261 GEO_TBO_FUNC void reset_TBO();
262 GEO_SSBO_FUNC void reset_SSBO();
263
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);
267
268 template <class T> void create_or_update_TEXTURE_2D(
269 index_t nb, const T* data
270 ) {
271 geo_debug_assert(type_ == TEXTURE_2D || type_ == UNINITIALIZED);
272 type_ = TEXTURE_2D;
273 if(texture_ == 0) {
274 glGenTextures(1, &texture_);
275 }
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);
280 GLsizei height = 1;
281 if(nb > 1024) {
282 width = GLsizei(::sqrt(nb));
283 height = GLsizei(nb) / width;
284 while(width*height < GLsizei(nb)) {
285 ++height;
286 }
287 }
288 glPixelStorei(GL_PACK_ALIGNMENT, 1);
289 if(width_ != width || height_ != height) {
290 glTexImage2D(
291 GL_TEXTURE_2D,
292 0, // level
293 GLint(internal_format(T())),
294 width, height,
295 0, // border
296 format(T()), type(T()),
297 nullptr
298 );
299 width_ = width;
300 height_ = height;
301 }
302 if(GLsizei(nb) == width*height) {
303 glTexSubImage2D(
304 GL_TEXTURE_2D,
305 0, // level
306 0, 0, // xoffset, yoffset
307 width, height,
308 format(T()), type(T()),
309 data
310 );
311 } else {
312 // If texture's last row is slightly larger than input
313 // data, avoid reading past the end of data by transfering
314 // the (partial) last row:
315 glTexSubImage2D(
316 GL_TEXTURE_2D,
317 0, // level
318 0, 0, // xoffset, yoffset
319 width, height-1, // width, height
320 format(T()), type(T()),
321 data
322 );
323 glTexSubImage2D(
324 GL_TEXTURE_2D,
325 0, // level
326 0, height-1, // xoffset, yoffset
327 (GLsizei(nb) % width), 1, // width, height
328 format(T()), type(T()),
329 data + (height-1)*width
330 );
331 }
332 }
333
334 template <class T> GEO_TBO_FUNC void create_or_update_TBO(
335 index_t nb, const T* data
336 ){
337#ifdef GEO_TBO_SUPPORTED
338 geo_debug_assert(type_ == TBO || type_ == UNINITIALIZED);
339 type_ = TBO;
340 geo_assert(GLint(nb) < max_TBO_size_);
342 BO_, GL_ARRAY_BUFFER,
343 nb * sizeof(T), data,
344 true
345 );
346 if(texture_ == 0) {
347 glGenTextures(1, &texture_);
348 glBindTexture(GL_TEXTURE_BUFFER, texture_);
349 glTexBuffer(GL_TEXTURE_BUFFER, internal_format(T()), BO_);
350 }
351#else
352 geo_argused(nb);
353 geo_argused(data);
355#endif
356 }
357
358 template <class T> GEO_SSBO_FUNC void create_or_update_SSBO(
359 index_t nb, const T* data
360 ) {
361#ifdef GEO_SSBO_SUPPORTED
362 geo_debug_assert(type_ == TBO || type_ == UNINITIALIZED);
363 type_ = SSBO;
364 // TODO: check SSBO size
366 BO_, GL_SHADER_STORAGE_BUFFER,
367 nb * sizeof(T), data,
368 true
369 );
370#else
371 geo_argused(nb);
372 geo_argused(data);
374#endif
375 }
376
377 protected:
378 // Just to map geogram types to OpenGL storage
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; }
388
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; }
398
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; }
408
409
410 private:
411 Type type_ = UNINITIALIZED;
412 GLuint texture_ = 0;
413 GLuint BO_ = 0;
414 GLsizei width_ = 0;
415 GLsizei height_ = 0;
416
417 static bool class_initialized_;
418 static bool GLSL_sources_registered_;
419 static GLint max_TBO_size_; // in texels.
420 static GLint max_texture_size_; // in texels.
421 static Type default_type_;
422 static GLuint active_unit_;
423 };
424}
425
426#endif
GLUP: GL Useful Primitives.
#define geo_assert_not_reached
Sets a non reachable point in the program.
Definition assert.h:183
#define geo_assert(x)
Verifies that a condition is met.
Definition assert.h:155
#define geo_debug_assert(x)
Verifies that a condition is met.
Definition assert.h:202
Vector with aligned memory allocation.
Definition memory.h:703
T * data()
Gets a pointer to the array of elements.
Definition memory.h:849
index_t size() const
Gets the number of elements.
Definition memory.h:749
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.
Definition defs.h:55
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.
Definition argused.h:60
geo_index_t index_t
The type for storing and manipulating indices.
Definition numeric.h:341
std::string format(const char *format,...)
Creates a string from a format string and additional arguments. Works like sprintf().