gKit2 light
Public Member Functions | Public Attributes | List of all members
ReadBuffer Struct Reference
+ Inheritance diagram for ReadBuffer:

Public Member Functions

int init ()
 a deriver pour creer les objets openGL. renvoie -1 pour indiquer une erreur, 0 sinon. More...
 
int quit ()
 a deriver pour detruire les objets openGL. renvoie -1 pour indiquer une erreur, 0 sinon. More...
 
int render ()
 a deriver pour afficher les objets. renvoie 1 pour continuer, 0 pour fermer l'application. More...
 
int init ()
 a deriver pour creer les objets openGL. renvoie -1 pour indiquer une erreur, 0 sinon. More...
 
int quit ()
 a deriver pour detruire les objets openGL. renvoie -1 pour indiquer une erreur, 0 sinon. More...
 
int render ()
 a deriver pour afficher les objets. renvoie 1 pour continuer, 0 pour fermer l'application. More...
 
- Public Member Functions inherited from App
 App (const int width, const int height, const int major=3, const int minor=3, const int samples=0)
 constructeur, dimensions de la fenetre et version d'openGL. More...
 
virtual int update (const float time, const float delta)
 a deriver et redefinir pour animer les objets en fonction du temps. More...
 
int run ()
 execution de l'application. More...
 

Public Attributes

GLuint m_gpu_buffer1
 
GLuint m_gpu_buffer2
 
GLuint m_gpu_count
 
GLuint m_read_buffer
 
GLuint m_program
 

Additional Inherited Members

- Protected Member Functions inherited from App
virtual int prerender ()
 
virtual int postrender ()
 
void vsync_off ()
 
- Protected Attributes inherited from App
Window m_window
 
Context m_context
 
bool sync
 

Detailed Description

Definition at line 25 of file tuto_count_buffer.cpp.

Member Function Documentation

◆ init() [1/2]

int ReadBuffer::init ( )
inlinevirtual

a deriver pour creer les objets openGL. renvoie -1 pour indiquer une erreur, 0 sinon.

Implements App.

Definition at line 29 of file tuto_count_buffer.cpp.

30  {
31  m_program= read_program("tutos/M2/count_buffer.glsl");
32  program_print_errors(m_program);
33 
34  std::vector<int> data(1024);
35  for(unsigned i= 0; i < data.size(); i++)
36  data[i]= i % 16;
37 
38 
39  glGenBuffers(1, &m_gpu_buffer1);
40  glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, m_gpu_buffer1);
41  #ifdef USE_BUFFER_STORAGE
42  glBufferStorage(GL_SHADER_STORAGE_BUFFER, sizeof(int) * data.size(), data.data(), 0);
43  #else
44  glBufferData(GL_SHADER_STORAGE_BUFFER, sizeof(int) * data.size(), data.data(), GL_STATIC_COPY);
45  #endif
46 
47  glGenBuffers(1, &m_gpu_buffer2);
48  glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, m_gpu_buffer2);
49  #ifdef USE_BUFFER_STORAGE
50  glBufferStorage(GL_SHADER_STORAGE_BUFFER, sizeof(int) * data.size(), nullptr, 0);
51  #else
52  glBufferData(GL_SHADER_STORAGE_BUFFER, sizeof(int) * data.size(), nullptr, GL_STATIC_COPY);
53  #endif
54 
55  glGenBuffers(1, &m_gpu_count);
56  glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 2, m_gpu_count);
57  #ifdef USE_BUFFER_STORAGE
58  glBufferStorage(GL_SHADER_STORAGE_BUFFER, sizeof(int), nullptr, 0);
59  #else
60  glBufferData(GL_SHADER_STORAGE_BUFFER, sizeof(int), nullptr, GL_STATIC_COPY);
61  #endif
62 
63  glGenBuffers(1, &m_read_buffer);
64  glBindBuffer(GL_COPY_READ_BUFFER, m_read_buffer);
65  #ifdef USE_BUFFER_STORAGE
66  #ifdef USE_MAP
67  glBufferStorage(GL_COPY_READ_BUFFER, sizeof(int) * data.size(), nullptr, GL_CLIENT_STORAGE_BIT | GL_MAP_READ_BIT);
68  #else
69  glBufferStorage(GL_COPY_READ_BUFFER, sizeof(int) * data.size(), nullptr, GL_CLIENT_STORAGE_BIT);
70  #endif
71  #else
72  glBufferData(GL_COPY_READ_BUFFER, sizeof(int) * data.size(), nullptr, GL_DYNAMIC_READ);
73  #endif
74 
75  #ifdef USE_BUFFER_STORAGE
76  printf("!! use buffer storage\n");
77  #else
78  printf("!! use buffer data\n");
79  #endif
80 
81  return 0;
82  }
void printf(Text &text, const int px, const int py, const char *format,...)
affiche un texte a la position x, y. meme utilisation que printf().
Definition: text.cpp:140
GLuint read_program(const char *filename, const char *definitions)
Definition: program.cpp:204
int program_print_errors(const GLuint program)
affiche les erreurs de compilation.
Definition: program.cpp:432

◆ quit() [1/2]

int ReadBuffer::quit ( )
inlinevirtual

a deriver pour detruire les objets openGL. renvoie -1 pour indiquer une erreur, 0 sinon.

Implements App.

Definition at line 84 of file tuto_count_buffer.cpp.

85  {
86  release_program(m_program);
87  glDeleteBuffers(1, &m_gpu_buffer1);
88  glDeleteBuffers(1, &m_gpu_buffer2);
89  glDeleteBuffers(1, &m_read_buffer);
90 
91  return 0;
92  }
int release_program(const GLuint program)
detruit les shaders et le program.
Definition: program.cpp:211

◆ render() [1/2]

int ReadBuffer::render ( )
inlinevirtual

a deriver pour afficher les objets. renvoie 1 pour continuer, 0 pour fermer l'application.

Implements App.

Definition at line 94 of file tuto_count_buffer.cpp.

95  {
96  glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, m_gpu_buffer1);
97  glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, m_gpu_buffer2);
98 
99  glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 2, m_gpu_count);
100  // remet le compteur a zero
101  int zero= 0;
102  glClearBufferData(GL_SHADER_STORAGE_BUFFER, GL_R32UI, GL_RED_INTEGER, GL_UNSIGNED_INT, &zero);
103 
104  glUseProgram(m_program);
105  glDispatchCompute(4, 1, 1);
106 
107  glMemoryBarrier(GL_BUFFER_UPDATE_BARRIER_BIT);
108 
109  glBindBuffer(GL_COPY_READ_BUFFER, m_read_buffer);
110  glBindBuffer(GL_SHADER_STORAGE_BUFFER, m_gpu_buffer2);
111  glCopyBufferSubData(GL_SHADER_STORAGE_BUFFER, GL_COPY_READ_BUFFER, 0, 0, sizeof(int)*1024);
112 
113  #ifdef USE_MAP
114  printf("!! use map\n");
115 
116  // TODO: recupere n, le nombre de valeurs...
117 
118  //~ int *tmp= (int *) glMapBuffer(GL_COPY_READ_BUFFER, GL_READ_ONLY);
119  //~ {
120  //~ for(unsigned i= 0; i < 1024; i++)
121  //~ printf("%d ", tmp[i]);
122  //~ printf("\n");
123  //~ }
124  //~ glUnmapBuffer(GL_COPY_READ_BUFFER);
125 
126  #else
127  printf("!! use get buffer\n");
128 
129  // recupere le nombre de valeurs stockees dans le buffer resultat
130  int n= 0;
131  glBindBuffer(GL_SHADER_STORAGE_BUFFER, m_gpu_count);
132  glGetBufferSubData(GL_SHADER_STORAGE_BUFFER, 0, sizeof(int), &n);
133 
134  printf("%d values\n", n);
135 
136  // recupere les valeurs
137  std::vector<int> tmp(n);
138  glGetBufferSubData(GL_COPY_READ_BUFFER, 0, sizeof(int) * tmp.size(), tmp.data());
139 
140  for(unsigned i= 0; i < tmp.size(); i++)
141  printf("%d ", tmp[i]);
142  printf("\n");
143  #endif
144 
145  return 0;
146  }

◆ init() [2/2]

int ReadBuffer::init ( )
inlinevirtual

a deriver pour creer les objets openGL. renvoie -1 pour indiquer une erreur, 0 sinon.

Implements App.

Definition at line 32 of file tuto_read_buffer.cpp.

33  {
34  m_program= read_program("tutos/M2/read_buffer.glsl");
35  program_print_errors(m_program);
36 
37  std::vector<int> data(1024);
38 
39  glGenBuffers(1, &m_gpu_buffer1);
40  glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, m_gpu_buffer1);
41  #ifdef USE_BUFFER_STORAGE
42  glBufferStorage(GL_SHADER_STORAGE_BUFFER, sizeof(int) * data.size(), data.data(), 0);
43  #else
44  glBufferData(GL_SHADER_STORAGE_BUFFER, sizeof(int) * data.size(), data.data(), GL_STATIC_COPY);
45  #endif
46 
47  glGenBuffers(1, &m_gpu_buffer2);
48  glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, m_gpu_buffer2);
49  #ifdef USE_BUFFER_STORAGE
50  glBufferStorage(GL_SHADER_STORAGE_BUFFER, sizeof(int) * data.size(), nullptr, 0);
51  #else
52  glBufferData(GL_SHADER_STORAGE_BUFFER, sizeof(int) * data.size(), nullptr, GL_STATIC_COPY);
53  #endif
54 
55  glGenBuffers(1, &m_read_buffer);
56  glBindBuffer(GL_COPY_READ_BUFFER, m_read_buffer);
57  #ifdef USE_BUFFER_STORAGE
58  #ifdef USE_MAP
59  glBufferStorage(GL_COPY_READ_BUFFER, sizeof(int) * data.size(), nullptr, GL_CLIENT_STORAGE_BIT | GL_MAP_READ_BIT);
60  #else
61  glBufferStorage(GL_COPY_READ_BUFFER, sizeof(int) * data.size(), nullptr, GL_CLIENT_STORAGE_BIT);
62  #endif
63  #else
64  glBufferData(GL_COPY_READ_BUFFER, sizeof(int) * data.size(), nullptr, GL_DYNAMIC_READ);
65  #endif
66 
67  #ifdef USE_BUFFER_STORAGE
68  printf("!! use buffer storage\n");
69  #else
70  printf("!! use buffer data\n");
71  #endif
72 
73  return 0;
74  }

◆ quit() [2/2]

int ReadBuffer::quit ( )
inlinevirtual

a deriver pour detruire les objets openGL. renvoie -1 pour indiquer une erreur, 0 sinon.

Implements App.

Definition at line 76 of file tuto_read_buffer.cpp.

77  {
78  release_program(m_program);
79  glDeleteBuffers(1, &m_gpu_buffer1);
80  glDeleteBuffers(1, &m_gpu_buffer2);
81  glDeleteBuffers(1, &m_read_buffer);
82 
83  return 0;
84  }

◆ render() [2/2]

int ReadBuffer::render ( )
inlinevirtual

a deriver pour afficher les objets. renvoie 1 pour continuer, 0 pour fermer l'application.

Implements App.

Definition at line 86 of file tuto_read_buffer.cpp.

87  {
88  glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, m_gpu_buffer1);
89  glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, m_gpu_buffer2);
90 
91  // execute le shader
92  glUseProgram(m_program);
93  glDispatchCompute(4, 1, 1);
94 
95  // attends les resultats
96  glMemoryBarrier(GL_BUFFER_UPDATE_BARRIER_BIT);
97 
98  // et maintenant comment on recupere les donnees qui se trouvent dans un buffer prive du gpu ?
99  // etape 1 : on commence par copier les donnees dans un buffer intermediaire qui est accessible a l'application,
100  // ie pas un buffer prive, cf la creation des 2 types de buffers dans init()...
101  glBindBuffer(GL_COPY_READ_BUFFER, m_read_buffer);
102  glCopyBufferSubData(GL_SHADER_STORAGE_BUFFER, GL_COPY_READ_BUFFER, 0, 0, sizeof(int)*1024);
103 
104  // etape 2 : on relit le contenu du buffer qui contient la copie des donnees
105  #ifdef USE_MAP
106  // soit avec map()
107  printf("!! use map\n");
108 
109  int *tmp= (int *) glMapBuffer(GL_COPY_READ_BUFFER, GL_READ_ONLY);
110  {
111  for(unsigned i= 0; i < 1024; i++)
112  printf("%d ", tmp[i]);
113  printf("\n");
114  }
115  glUnmapBuffer(GL_COPY_READ_BUFFER);
116 
117  #else
118  // soit avec getbuffer()
119  printf("!! use get buffer\n");
120 
121  std::vector<int> tmp(1024);
122  glGetBufferSubData(GL_COPY_READ_BUFFER, 0, sizeof(int) * tmp.size(), tmp.data());
123  for(unsigned i= 0; i < tmp.size(); i++)
124  printf("%d ", tmp[i]);
125  printf("\n");
126  #endif
127 
128  return 0;
129  }

The documentation for this struct was generated from the following files: