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

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...
 
- 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

std::vector< int > m_data
 
GLuint m_gpu_buffer1
 
GLuint m_gpu_buffer2
 
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 9 of file tuto_compute_buffer.cpp.

Member Function Documentation

◆ init()

int ComputeBuffer::init ( )
inlinevirtual

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

Implements App.

Definition at line 13 of file tuto_compute_buffer.cpp.

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  }
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()

int ComputeBuffer::quit ( )
inlinevirtual

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

Implements App.

Definition at line 38 of file tuto_compute_buffer.cpp.

39  {
40  release_program(m_program);
41  glDeleteBuffers(1, &m_gpu_buffer1);
42  glDeleteBuffers(1, &m_gpu_buffer2);
43 
44  return 0;
45  }
int release_program(const GLuint program)
detruit les shaders et le program.
Definition: program.cpp:211

◆ render()

int ComputeBuffer::render ( )
inlinevirtual

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

Implements App.

Definition at line 47 of file tuto_compute_buffer.cpp.

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  }
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

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