gKit2 light
Loading...
Searching...
No Matches
tuto_histogram1_compute.cpp
1
2#include <chrono>
3
4#include "app.h"
5
6#include "color.h"
7#include "image.h"
8#include "image_io.h"
9
10#include "program.h"
11#include "uniforms.h"
12
13struct Histogram : public App
14{
15 Histogram( const char *filename ) : App(1024, 640, 4,3)
16 {
17 m_image= read_image_data(filename);
18 }
19
20 int init( )
21 {
22 if(m_image.pixels.size() == 0)
23 // pas d'image
24 return -1;
25
26 // cree la texture pour stocker l'image
27 GLenum data_type= GL_UNSIGNED_BYTE;
28 GLenum data_format= GL_RGBA;
29 if(m_image.channels == 1)
30 data_format= GL_RED;
31 else if(m_image.channels == 2)
32 data_format= GL_RG;
33 else if(m_image.channels == 3)
34 data_format= GL_RGB;
35
36 glGenTextures(1, &m_texture);
37 glBindTexture(GL_TEXTURE_2D, m_texture);
38 glTexImage2D(GL_TEXTURE_2D, 0,
39 GL_RGBA8, m_image.width, m_image.height, 0,
40 data_format, data_type, m_image.data());
41
42 // pas la peine de construire les mipmaps, le shader ne va ecrire que le mipmap 0
43 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 0);
44
45 // cree le buffer pour stocker l'histogramme
46 glGenBuffers(1, &m_histogram);
47 glBindBuffer(GL_SHADER_STORAGE_BUFFER, m_histogram);
48 glBufferData(GL_SHADER_STORAGE_BUFFER, sizeof(int) * 16, nullptr, GL_STATIC_COPY);
49
50 m_program= read_program("gkit2_tutos/M2/histogram1.glsl");
51 program_print_errors(m_program);
52
53 glGetProgramiv(m_program, GL_COMPUTE_WORK_GROUP_SIZE, m_threads);
54
55 // mesure temps gpu
56 glGenQueries(1, &m_time_query);
57
58 // histogramme cpu pour comparer...
59 {
60 auto start= std::chrono::high_resolution_clock::now();
61
62 int histogram[16]= { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
63 for(unsigned y= 0; y < m_image.height; y++)
64 for(unsigned x= 0; x < m_image.width; x++)
65 {
66 unsigned offset= m_image.offset(x, y);
67 int r= m_image.pixels[offset];
68 int g= m_image.pixels[offset+1];
69 int b= m_image.pixels[offset+2];
70 int bin= 15 * (r+g+b) / (255*3);
71 histogram[bin]++;
72 }
73
74 auto stop= std::chrono::high_resolution_clock::now();
75 int cpu_time= std::chrono::duration_cast<std::chrono::microseconds>(stop - start).count();
76
77 for(int i= 0; i < 16; i++)
78 printf("bin %d: %d pixels, %d%%\n", i, histogram[i], 100*histogram[i] / (m_image.width * m_image.height));
79
80 printf("cpu %dus\n", cpu_time);
81 }
82
83 return 1;
84 }
85
86 int quit( )
87 {
88 release_program(m_program);
89 glDeleteBuffers(1, &m_histogram);
90 glDeleteTextures(1, &m_texture);
91 glDeleteQueries(1, &m_time_query);
92 return 0;
93 }
94
95 int render( )
96 {
97 glViewport(0, 0, window_width(), window_height());
98 glClear(GL_COLOR_BUFFER_BIT);
99
100 glUseProgram(m_program);
101
102 // storage buffer 0
103 glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, m_histogram);
104 // remet a zero l'histogramme
105 glClearBufferData(GL_SHADER_STORAGE_BUFFER, GL_R32UI, GL_RED_INTEGER, GL_UNSIGNED_INT, 0);
106
107 // image texture 0, ecriture seule, mipmap 0 + format rgba8 classique
108 glBindImageTexture(0, m_texture, 0, GL_FALSE, 0, GL_READ_ONLY, GL_RGBA8);
109 // configurer le shader
110 program_uniform(m_program, "image", 0);
111
112 int nx= m_image.width / m_threads[0];
113 int ny= m_image.height / m_threads[1];
114 // on suppose que les dimensions de l'image sont multiples de 8...
115 // sinon calculer correctement le nombre de groupes pour x et y.
116
117 glBeginQuery(GL_TIME_ELAPSED, m_time_query);
118 {
119 // go !!
120 //~ for(int i= 0; i < 100; i++) // eventuellement recommencer plein de fois pour stabiliser les frequences du gpu...
121 glDispatchCompute(nx, ny, 1);
122
123 // attendre le resultat
124 glMemoryBarrier(GL_ALL_BARRIER_BITS);
125 }
126 glEndQuery(GL_TIME_ELAPSED);
127
128 // relire le buffer resultat
129 int histogram[16];
130 {
131 // creer un buffer temporaire pour le transfert depuis le buffer resultat
132 GLuint buffer= 0;
133 glGenBuffers(1, &buffer);
134 glBindBuffer(GL_COPY_READ_BUFFER, buffer);
135 glBufferData(GL_COPY_READ_BUFFER, sizeof(int) * 16, nullptr, GL_DYNAMIC_READ);
136
137 // copie les resultats
138 glCopyBufferSubData(GL_SHADER_STORAGE_BUFFER, GL_COPY_READ_BUFFER, 0, 0, sizeof(int) * 16);
139
140 // recupere les resultats depuis le buffer intermediaire
141 glGetBufferSubData(GL_COPY_READ_BUFFER, 0, sizeof(int) * 16, histogram);
142
143 // detruit le buffer intermediaire
144 glDeleteBuffers(1, &buffer);
145 }
146
147 for(int i= 0; i < 16; i++)
148 printf("bin %d: %d pixels, %d%%\n", i, histogram[i], 100*histogram[i] / (m_image.width * m_image.height));
149
150 printf("\n%dx%d groups, %d threads\n", nx, ny, nx*ny*m_threads[0]*m_threads[1]);
151
152 // attendre le resultat de la requete
153 GLint64 gpu_time= 0;
154 glGetQueryObjecti64v(m_time_query, GL_QUERY_RESULT, &gpu_time);
155 //~ gpu_time/= 100;
156 printf("gpu %02dms %03dus\n\n", int(gpu_time / 1000000), int((gpu_time / 1000) % 1000));
157
158 return 0; // une seule fois
159 //~ return 1; // recommencer jusqu'a la fermeture de la fenetre...
160 }
161
162 ImageData m_image;
163 GLuint m_program;
164 GLuint m_time_query;
165 GLuint m_histogram;
166 GLuint m_texture;
167 GLint m_threads[3];
168};
169
170
171
172int main( int argc, char **argv )
173{
174 const char *filename= "data/papillon.png";
175 if(argc > 1)
176 filename= argv[1];
177
178 Histogram app(filename);
179 app.run();
180
181 return 0;
182}
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 window_height()
renvoie la hauteur de la fenetre de l'application.
Definition window.cpp:27
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
int window_width()
renvoie la largeur de la fenetre de l'application.
Definition window.cpp:23
ImageData read_image_data(const void *buffer, const unsigned size, const bool flipY)
charge les donnees d'un fichier png stocke en memoire. renvoie une image initialisee par defaut en ca...
Definition image_io.cpp:315
GLuint read_program(const char *filename, const char *definitions)
Definition program.cpp:218
int program_print_errors(const GLuint program)
affiche les erreurs de compilation.
Definition program.cpp:446
int release_program(const GLuint program)
detruit les shaders et le program.
Definition program.cpp:225
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.
int quit()
a deriver pour detruire les objets openGL. renvoie -1 pour indiquer une erreur, 0 sinon.
stockage temporaire des donnees d'une image.
Definition image_io.h:53