gKit2 light
Loading...
Searching...
No Matches
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
9struct ComputeBuffer : public App
10{
11 ComputeBuffer( ) : App(1280, 768, 4,3) {}
12
13 int init( )
14 {
15 m_program= read_program("gkit2_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
26 // buffer resultat, meme taille, mais pas de donnees...
27 glGenBuffers(1, &m_gpu_buffer2);
28 glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, m_gpu_buffer2);
29 glBufferData(GL_SHADER_STORAGE_BUFFER, sizeof(int) * m_data.size(), nullptr, GL_STATIC_COPY);
30
31 return 0;
32 }
33
34 int quit( )
35 {
36 release_program(m_program);
37 glDeleteBuffers(1, &m_gpu_buffer1);
38 glDeleteBuffers(1, &m_gpu_buffer2);
39
40 return 0;
41 }
42
43 int render( )
44 {
45 glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, m_gpu_buffer1);
46 glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, m_gpu_buffer2);
47
48 // execute les shaders
49 glUseProgram(m_program);
50
51 // recupere le nombre de threads declare par le shader
52 int threads[3]= {};
53 glGetProgramiv(m_program, GL_COMPUTE_WORK_GROUP_SIZE, threads);
54
55 int n= m_data.size() / threads[0];
56 // nombre de groupes de threads, arrondi...
57 if(m_data.size() % threads[0])
58 n++;
59 // oui on peut calculer ca de maniere plus directe...
60 // ou utiliser directement 256, comme dans le shader...
61
62 // go !
63 glDispatchCompute(n, 1, 1);
64
65 // attendre que les resultats soient disponibles
66 glMemoryBarrier(GL_BUFFER_UPDATE_BARRIER_BIT);
67
68 // relire le resultat
69 std::vector<int> tmp(1024);
70 glBindBuffer(GL_SHADER_STORAGE_BUFFER, m_gpu_buffer2);
71 glGetBufferSubData(GL_SHADER_STORAGE_BUFFER, 0, sizeof(int) * tmp.size(), tmp.data());
72
73 for(unsigned i= 0; i < tmp.size(); i++)
74 printf("%d ", tmp[i]);
75 printf("\n");
76
77 return 0;
78 }
79
80 std::vector<int> m_data;
81 GLuint m_gpu_buffer1;
82 GLuint m_gpu_buffer2;
83 GLuint m_read_buffer;
84 GLuint m_program;
85};
86
87int main( )
88{
89 ComputeBuffer app;
90 app.run();
91
92 return 0;
93}
int run()
execution de l'application.
Definition app.cpp:36
App(const int width, const int height, const int major=3, const int minor=3)
constructeur, dimensions de la fenetre et version d'openGL.
Definition app.cpp:11
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:217
int program_print_errors(const GLuint program, const char *filename)
affiche les erreurs de compilation.
Definition program.cpp:461
int release_program(const GLuint program)
detruit les shaders et le program.
Definition program.cpp:240
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.