Molecular Vis
Improving perception of molecular visualization
SSBO.h
1 #pragma once
2 #include <GL/glew.h>
3 #include <vector>
4 
11 template<class T>
12 class SSBO
13 {
14 private:
15  GLuint handle;
16  unsigned int size;
17  unsigned int target;
18  bool isInit = false;
19 public:
24  SSBO();
30  SSBO(unsigned int size);
31  ~SSBO();
32 
36  unsigned int getCapacity();
37 
42  void uploadData(const std::vector<T>& data);
43 
47  void bindToTarget(unsigned int target);
48 
52  void release();
53 };
54 
55 
56 /********************************************
57 * IMPLEMENTATION
58 *********************************************/
59 
60 template<class T>
62  :size(0), target(-1)
63 {}
64 
65 template<class T>
66 SSBO<T>::SSBO(unsigned int size)
67  :size(size), target(-1), isInit(true)
68 {
69  glGenBuffers(1, &handle);
70  glBindBuffer(GL_SHADER_STORAGE_BUFFER, handle);
71  std::cout << handle << '\n';
72  glBufferData(GL_SHADER_STORAGE_BUFFER, size*sizeof(T), NULL, GL_DYNAMIC_DRAW);
73  glBindBuffer(GL_SHADER_STORAGE_BUFFER, 0);
74 }
75 
76 template<class T>
78 {
79  if (isInit)
80  {
81  std::cout << "Deleted SSBO with handle: " << handle << '\n';
82  glDeleteBuffers(1, &handle);
83  }
84 }
85 template<class T>
86 unsigned int SSBO<T>::getCapacity()
87 {
88  return size;
89 }
90 
91 template<class T>
92 void SSBO<T>::uploadData(const std::vector<T>& data)
93 {
94  glBindBuffer(GL_SHADER_STORAGE_BUFFER, handle);
95  glBufferSubData(GL_SHADER_STORAGE_BUFFER, 0, data.size() * sizeof(T), data.data());
96  glBindBuffer(GL_SHADER_STORAGE_BUFFER, 0);
97 }
98 
99 template<class T>
100 void SSBO<T>::bindToTarget(unsigned int target)
101 {
102  if (this->target != target)
103  {
104  glBindBufferBase(GL_SHADER_STORAGE_BUFFER, target, handle);
105  this->target = target;
106  }
107 }
108 
109 template<class T>
111 {
112  glDeleteBuffers(1, &handle);
113 }
Wrapper for SSBO buffer (shader storage buffer object) T is the type of the data (struct and has to s...
Definition: SSBO.h:12
void uploadData(const std::vector< T > &data)
Definition: SSBO.h:92
void bindToTarget(unsigned int target)
Definition: SSBO.h:100
SSBO()
Definition: SSBO.h:61
unsigned int getCapacity()
Definition: SSBO.h:86
void release()
Definition: SSBO.h:110