gKit2 light
tuto_compute_buffer.cpp
Go to the documentation of this file.
1 
3 
4 #include <vector>
5 
6 #include "app.h"
7 #include "program.h"
8 
9 struct ComputeBuffer : public App
10 {
11  ComputeBuffer( ) : App(1280, 768, 4,3) {}
12 
13  int init( )
14  {
15  m_program= read_program("tutos/M2/compute_buffer.glsl");
16  program_print_errors(m_program);
17 
18  // initialise un tableau de valeurs
19  m_data= std::vector<int>(1024);
20 
21  // cree les buffers pour les parametres du shader
22  glGenBuffers(1, &m_gpu_buffer1);
23  glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, m_gpu_buffer1);
24  glBufferData(GL_SHADER_STORAGE_BUFFER, sizeof(int) * m_data.size(), m_data.data(), GL_STATIC_COPY);
25  // ou :
26  // glBufferStorage(GL_SHADER_STORAGE_BUFFER, sizeof(int) * data.size(), data.data(), 0);
27 
28  // buffer resultat, meme taille, mais pas de donnees...
29  glGenBuffers(1, &m_gpu_buffer2);
30  glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, m_gpu_buffer2);
31  glBufferData(GL_SHADER_STORAGE_BUFFER, sizeof(int) * m_data.size(), nullptr, GL_STATIC_COPY);
32  // ou :
33  // glBufferStorage(GL_SHADER_STORAGE_BUFFER, sizeof(int) * data.size(), nullptr, 0);
34 
35  return 0;
36  }
37 
38  int quit( )
39  {
40  release_program(m_program);
41  glDeleteBuffers(1, &m_gpu_buffer1);
42  glDeleteBuffers(1, &m_gpu_buffer2);
43 
44  return 0;
45  }
46 
47  int render( )
48  {
49  glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, m_gpu_buffer1);
50  glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, m_gpu_buffer2);
51 
52  // execute les shaders
53  glUseProgram(m_program);
54 
55  // recupere le nombre de threads declare par le shader
56  int threads[3]= {};
57  glGetProgramiv(m_program, GL_COMPUTE_WORK_GROUP_SIZE, threads);
58 
59  int n= m_data.size() / threads[0];
60  // nombre de groupes de threads, arrondi...
61  if(m_data.size() % threads[0])
62  n++;
63  // oui on peut calculer ca de maniere plus directe...
64  // ou utiliser directement 256, comme dans le shader...
65 
66  // go !
67  glDispatchCompute(n, 1, 1);
68 
69  // attendre que les resultats soient disponibles
70  glMemoryBarrier(GL_BUFFER_UPDATE_BARRIER_BIT);
71 
72  // relire le resultat
73  std::vector<int> tmp(1024);
74  glBindBuffer(GL_SHADER_STORAGE_BUFFER, m_gpu_buffer2);
75  glGetBufferSubData(GL_SHADER_STORAGE_BUFFER, 0, sizeof(int) * tmp.size(), tmp.data());
76 
77  for(unsigned i= 0; i < tmp.size(); i++)
78  printf("%d ", tmp[i]);
79  printf("\n");
80 
81  return 0;
82  }
83 
84  std::vector<int> m_data;
85  GLuint m_gpu_buffer1;
86  GLuint m_gpu_buffer2;
87  GLuint m_read_buffer;
88  GLuint m_program;
89 };
90 
91 int main( )
92 {
93  ComputeBuffer app;
94  app.run();
95 
96  return 0;
97 }
classe application.
Definition: app.h:20
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.
Definition: app.cpp:11
int run()
execution de l'application.
Definition: app.cpp:36
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
int release_program(const GLuint program)
detruit les shaders et le program.
Definition: program.cpp:211
int quit()
a deriver pour detruire les objets openGL. renvoie -1 pour indiquer une erreur, 0 sinon.
int render()
a deriver pour afficher les objets. renvoie 1 pour continuer, 0 pour fermer l'application.
int init()
a deriver pour creer les objets openGL. renvoie -1 pour indiquer une erreur, 0 sinon.