gKit2 light
Loading...
Searching...
No Matches
tuto_vertex_compute.cpp
Go to the documentation of this file.
1
3
4#include "app.h"
5
6#include "vec.h"
7#include "mat.h"
8
9#include "program.h"
10#include "uniforms.h"
11#include "texture.h"
12
13#include "mesh.h"
14#include "wavefront.h"
15
16#include "orbiter.h"
17
18
19// cf tuto_storage
20namespace glsl
21{
22 template < typename T >
23 struct alignas(8) gvec2
24 {
25 alignas(4) T x, y;
26
27 gvec2( ) {}
28 gvec2( const ::vec2& v ) : x(v.x), y(v.y) {}
29 };
30
31 typedef gvec2<float> vec2;
32 typedef gvec2<int> ivec2;
33 typedef gvec2<unsigned int> uvec2;
34 typedef gvec2<int> bvec2;
35
36 template < typename T >
37 struct alignas(16) gvec3
38 {
39 alignas(4) T x, y, z;
40
41 gvec3( ) {}
42 gvec3( const ::vec3& v ) : x(v.x), y(v.y), z(v.z) {}
43 gvec3( const Point& v ) : x(v.x), y(v.y), z(v.z) {}
44 gvec3( const Vector& v ) : x(v.x), y(v.y), z(v.z) {}
45 };
46
47 typedef gvec3<float> vec3;
48 typedef gvec3<int> ivec3;
49 typedef gvec3<unsigned int> uvec3;
50 typedef gvec3<int> bvec3;
51
52 template < typename T >
53 struct alignas(16) gvec4
54 {
55 alignas(4) T x, y, z, w;
56
57 gvec4( ) {}
58 gvec4( const ::vec4& v ) : x(v.x), y(v.y), z(v.z), w(v.w) {}
59 };
60
61 typedef gvec4<float> vec4;
62 typedef gvec4<int> ivec4;
63 typedef gvec4<unsigned int> uvec4;
64 typedef gvec4<int> bvec4;
65}
66
67
68class VertexCompute : public App
69{
70public:
71 // application openGL 4.3
72 VertexCompute( ) : App(1024, 640, 4,3) {}
73
74 int init( )
75 {
76 m_mesh= read_mesh("data/bigguy.obj");
77 printf(" positions %d\n", m_mesh.vertex_count());
78 printf(" triangles %d\n", m_mesh.triangle_count());
79
80 // construit le storage buffer contenant les positions, les normales et les texcoords, en utilisant les types alignes
81 struct vertex
82 {
83 glsl::vec3 position;
84 };
85
86 // recupere les attributs du mesh
87 std::vector<vertex> data(m_mesh.vertex_count());
88 for(int i= 0; i < m_mesh.vertex_count(); i++)
89 data[i].position= m_mesh.positions().at(i);
90
91 // vao par defaut, pas d'attribut. les positions des sommets sont dans le storage buffer...
92 glGenVertexArrays(1, &m_vao);
93 glBindVertexArray(m_vao);
94
95 // storage buffers
96 glGenBuffers(1, &m_buffer);
97 glBindBuffer(GL_SHADER_STORAGE_BUFFER, m_buffer);
98 glBufferData(GL_SHADER_STORAGE_BUFFER, sizeof(vertex) * data.size(), data.data(), GL_STREAM_READ);
99
100 glGenBuffers(1, &m_transformed_buffer);
101 glBindBuffer(GL_SHADER_STORAGE_BUFFER, m_transformed_buffer);
102 glBufferData(GL_SHADER_STORAGE_BUFFER, sizeof(vec4) * data.size(), std::vector<vec4>(data.size(), vec4()).data(), GL_STREAM_COPY);
103
104 //
105 m_program= read_program("gkit2_tutos/pipeline_compute.glsl");
106 program_print_errors(m_program);
107
108 // compute shader
109 m_compute_program= read_program("gkit2_tutos/vertex_compute.glsl");
110 program_print_errors(m_compute_program);
111
112 // recupere le nombre de threads de chaque groupe du compute shader
113 GLint threads[3]= { };
114 glGetProgramiv(m_compute_program, GL_COMPUTE_WORK_GROUP_SIZE, threads);
115 printf("threads / group x %d, y %d, z %d\n", threads[0], threads[1], threads[2]);
116 m_compute_threads= threads[0];
117
118 // nombre de groupes de threads a executer pour transformer les sommets du mesh
119 m_compute_groups= m_mesh.vertex_count() / m_compute_threads;
120 if(m_mesh.vertex_count() % m_compute_threads)
121 m_compute_groups= m_compute_groups +1;
122
123 printf("groups %d= %d threads\n", m_compute_groups, m_compute_groups*m_compute_threads);
124
125 //
126 Point pmin, pmax;
127 m_mesh.bounds(pmin, pmax);
128 m_camera.lookat(pmin, pmax);
129
130 // etat openGL par defaut
131 glClearColor(0.2f, 0.2f, 0.2f, 1.f); // couleur par defaut de la fenetre
132
133 glClearDepth(1.f); // profondeur par defaut
134 glDepthFunc(GL_LESS); // ztest, conserver l'intersection la plus proche de la camera
135 glEnable(GL_DEPTH_TEST); // activer le ztest
136
137 glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); // ne dessine que les aretes des triangles
138 glLineWidth(2);
139
140 return 0; // ras, pas d'erreur
141 }
142
143 // destruction des objets de l'application
144 int quit( )
145 {
146 m_mesh.release();
147 release_program(m_program);
148 release_program(m_compute_program);
149 glDeleteBuffers(1, &m_buffer);
150 glDeleteBuffers(1, &m_transformed_buffer);
151 glDeleteVertexArrays(1, &m_vao);
152 return 0;
153 }
154
155 // dessiner une nouvelle image
156 int render( )
157 {
158 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
159
160 // deplace la camera
161 int mx, my;
162 unsigned int mb= SDL_GetRelativeMouseState(&mx, &my);
163 if(mb & SDL_BUTTON(1)) // le bouton gauche est enfonce
164 m_camera.rotation(mx, my);
165 else if(mb & SDL_BUTTON(3)) // le bouton droit est enfonce
166 m_camera.move(mx);
167 else if(mb & SDL_BUTTON(2)) // le bouton du milieu est enfonce
168 m_camera.translation((float) mx / (float) window_width(), (float) my / (float) window_height());
169
170 // selectionne les buffers
171 glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, m_buffer);
172 glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, m_transformed_buffer);
173
174 // etape 1 : utilise le compute shader pour transformer les positions des sommets du mesh.
175 glUseProgram(m_compute_program);
176
177 // uniforms du compute shader
178 Transform model= RotationY(global_time() / 60);
179 Transform view= m_camera.view();
180 Transform projection= m_camera.projection(window_width(), window_height(), 45);
181 Transform mvp= projection * view * model;
182
183 program_uniform(m_compute_program, "mvpMatrix", mvp);
184
185 // go !!
186 glDispatchCompute(m_compute_groups, 1, 1);
187
188 // etape 2 : synchronisation, attendre les resultats du compute shader
189 glMemoryBarrier(GL_SHADER_STORAGE_BARRIER_BIT);
190
191 // etape 3 : utilise les sommets transformes (par le compute shader) pour afficher le mesh.
192 glBindVertexArray(m_vao);
193 glUseProgram(m_program);
194 glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, m_transformed_buffer);
195
196 glDrawArrays(GL_TRIANGLES, 0, m_mesh.vertex_count());
197
198 glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, 0);
199 glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, 0);
200
201 return 1;
202 }
203
204protected:
205 Mesh m_mesh;
206 Orbiter m_camera;
207
208 GLuint m_vao;
209 GLuint m_buffer;
210 GLuint m_transformed_buffer;
211 GLuint m_program;
212 GLuint m_compute_program;
213 int m_compute_threads;
214 int m_compute_groups;
215};
216
217
218int main( int argc, char **argv )
219{
220 VertexCompute app;
221 app.run();
222
223 return 0;
224}
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
representation d'un objet / maillage.
Definition mesh.h:121
representation de la camera, type orbiter, placee sur une sphere autour du centre de l'objet.
Definition orbiter.h:17
int init()
a deriver pour creer 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 quit()
a deriver pour detruire les objets openGL. renvoie -1 pour indiquer une erreur, 0 sinon.
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
float global_time()
renvoie le temps ecoule depuis le lancement de l'application, en millisecondes.
Definition window.cpp:139
Transform RotationY(const float a)
renvoie la matrice representation une rotation de a degree autour de l'axe Y.
Definition mat.cpp:242
Mesh read_mesh(const char *filename)
charge un fichier wavefront .obj et renvoie un mesh compose de triangles non indexes....
Definition wavefront.cpp:14
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
representation d'un point 3d.
Definition vec.h:21
representation d'une transformation, une matrice 4x4, organisee par ligne / row major.
Definition mat.h:21
vecteur generique 4d, ou 3d homogene, utilitaire.
Definition vec.h:192
representation de l'indexation complete d'un sommet