gKit2 light
Loading...
Searching...
No Matches
pipeline_statistics.cpp
1
2#include "app_time.h"
3#include "app_camera.h"
4
5#include "mesh.h"
6#include "wavefront_fast.h"
7#include "draw.h"
8
9#include "program.h"
10#include "uniforms.h"
11
12
13struct TP : public AppTime
14{
15 TP( const char *filename ) : AppTime(1024, 640)
16 {
17 m_mesh= read_indexed_mesh_fast( filename );
18 }
19
20 int init( )
21 {
22 if(m_mesh.vertex_count() == 0)
23 return -1;
24
25 m_program= read_program("gkit2_tutos/M2/statistics.glsl");
26 program_print_errors(m_program);
27
28 // camera
29 m_camera.projection(window_width(), window_height(), 45);
30 Point pmin, pmax;
31 m_mesh.bounds(pmin, pmax);
32 m_camera.lookat(pmin, pmax);
33
34 // requetes
35 glGenQueries(1, &m_sample_query);
36 glGenQueries(1, &m_vertex_query);
37 glGenQueries(1, &m_primitive_query);
38 glGenQueries(1, &m_vertex_shader_query);
39 glGenQueries(1, &m_culling_query);
40 glGenQueries(1, &m_clipping_query);
41 glGenQueries(1, &m_sample_query);
42 glGenQueries(1, &m_fragment_shader_query);
43
44 // etat openGL par defaut
45 glClearColor(0.2f, 0.2f, 0.2f, 1.f); // couleur par defaut de la fenetre
46
47 glClearDepth(1.f); // profondeur par defaut
48 glDepthFunc(GL_LEQUAL); // ztest, conserver l'intersection la plus proche de la camera
49 glEnable(GL_DEPTH_TEST); // activer le ztest
50 glEnable(GL_CULL_FACE);
51
52 return 0;
53 }
54
55 int quit( )
56 {
57 return 0;
58 }
59
60 int render( )
61 {
62 update_camera( m_camera );
63
64 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
65
66 static bool capture= false;
67 static bool rasterizer_discard= false;
68 if(key_state('d'))
69 {
70 clear_key_state('d');
71 rasterizer_discard= !rasterizer_discard;
72 capture= true; // force une capture
73
74 if(!rasterizer_discard)
75 printf("rasterizer ON\n");
76 else
77 printf("rasterizer off\n");
78 }
79 if(rasterizer_discard)
80 glEnable(GL_RASTERIZER_DISCARD);
81 else
82 glDisable(GL_RASTERIZER_DISCARD);
83
84 static bool zinit= false;
85 if(key_state('z'))
86 {
87 clear_key_state('z');
88 zinit= !zinit;
89 capture= true; // force une capture
90
91 if(zinit)
92 printf("Z prepass ON\n");
93 else
94 printf("Z prepass off\n");
95 }
96
97 //
98 static bool cull= true;
99 if(key_state('f'))
100 {
101 clear_key_state('f');
102 cull= !cull;
103 capture= true; // force une capture
104
105 if(cull)
106 glEnable(GL_CULL_FACE);
107 else
108 glDisable(GL_CULL_FACE);
109
110 if(cull)
111 printf("face culling ON\n");
112 else
113 printf("face culling off\n");
114 }
115
116 static bool early_tests= false;
117 static const char *early_tests_definition= "";
118 if(key_state('e'))
119 {
120 clear_key_state('e');
121 early_tests= !early_tests;
122 capture= true; // force une capture
123
124 if(early_tests)
125 early_tests_definition= "#define EARLY_TESTS";
126 else
127 early_tests_definition= "#undef EARLY_TESTS";
128
129 if(early_tests)
130 printf("early fragment tests ON\n");
131 else
132 printf("early fragment test off\n");
133
134 reload_program(m_program, early_tests_definition);
135 program_print_errors(m_program);
136 }
137
138 //
139 Transform model;
140 Transform view= m_camera.view();
141 Transform projection= m_camera.projection(window_width(), window_height(), 45);
142 Transform mv= view * model;
143 Transform mvp= projection * mv;
144
145 //
146 if(zinit)
147 draw( m_mesh, Identity(), m_camera );
148
149 //
150 glBeginQuery(GL_VERTICES_SUBMITTED, m_vertex_query);
151 glBeginQuery(GL_PRIMITIVES_SUBMITTED, m_primitive_query);
152 glBeginQuery(GL_VERTEX_SHADER_INVOCATIONS, m_vertex_shader_query);
153 glBeginQuery(GL_CLIPPING_INPUT_PRIMITIVES, m_culling_query); // reussi le test de front/back face culling
154 glBeginQuery(GL_CLIPPING_OUTPUT_PRIMITIVES, m_clipping_query);
155 glBeginQuery(GL_SAMPLES_PASSED, m_sample_query);
156 glBeginQuery(GL_FRAGMENT_SHADER_INVOCATIONS, m_fragment_shader_query);
157
158 glUseProgram(m_program);
159 program_uniform(m_program, "mvMatrix", mv);
160 program_uniform(m_program, "mvpMatrix", mvp);
161
162 m_mesh.draw(m_program);
163 // draw( m_mesh, model, m_camera );
164
165 glEndQuery(GL_VERTICES_SUBMITTED);
166 glEndQuery(GL_PRIMITIVES_SUBMITTED);
167 glEndQuery(GL_VERTEX_SHADER_INVOCATIONS);
168 glEndQuery(GL_CLIPPING_INPUT_PRIMITIVES);
169 glEndQuery(GL_CLIPPING_OUTPUT_PRIMITIVES);
170 glEndQuery(GL_SAMPLES_PASSED);
171 glEndQuery(GL_FRAGMENT_SHADER_INVOCATIONS);
172
173 // recuperer les resultats.
174 GLint64 gpu_vertex= 0;
175 glGetQueryObjecti64v(m_vertex_query, GL_QUERY_RESULT, &gpu_vertex);
176 GLint64 gpu_primitive= 0;
177 glGetQueryObjecti64v(m_primitive_query, GL_QUERY_RESULT, &gpu_primitive);
178 GLint64 gpu_vertex_shader= 0;
179 glGetQueryObjecti64v(m_vertex_shader_query, GL_QUERY_RESULT, &gpu_vertex_shader);
180 GLint64 gpu_culling= 0;
181 glGetQueryObjecti64v(m_culling_query, GL_QUERY_RESULT, &gpu_culling);
182 GLint64 gpu_clipping= 0;
183 glGetQueryObjecti64v(m_clipping_query, GL_QUERY_RESULT, &gpu_clipping);
184 GLint64 gpu_samples= 0;
185 glGetQueryObjecti64v(m_sample_query, GL_QUERY_RESULT, &gpu_samples);
186 GLint64 gpu_fragment_shader= 0;
187 glGetQueryObjecti64v(m_fragment_shader_query, GL_QUERY_RESULT, &gpu_fragment_shader);
188
189 if(capture || key_state(' '))
190 {
191 capture= false;
192 clear_key_state(' ');
193
194 if(zinit) printf("Z prepass ON, ");
195 else printf("Z prepass off, ");
196 if(cull) printf("face culling ON, ");
197 else printf("face culling off, ");
198 if(early_tests) printf("early fragment tests ON");
199 else printf("early fragment test off");
200 printf("\n");
201
202 printf("primitives %d\n", int(gpu_primitive));
203 printf("positions %d, indices %d, vertex shader %d\n", m_mesh.vertex_count(), int(gpu_vertex), int(gpu_vertex_shader));
204 printf("culling primitives %d, clipping primitives %d\n", int(gpu_culling), int(gpu_clipping));
205 printf("samples %d, fragment shaders %d\n", int(gpu_samples), int(gpu_fragment_shader));
206 printf("\n");
207 }
208
209 glDisable(GL_RASTERIZER_DISCARD); // le rasterizer doit etre actif, sinon pas de clear, et de pas de texte
210 return 1;
211 }
212
213protected:
214 Mesh m_mesh;
215 Orbiter m_camera;
216 GLuint m_program;
217
218 GLuint m_vertex_query;
219 GLuint m_primitive_query;
220 GLuint m_vertex_shader_query;
221 GLuint m_culling_query;
222 GLuint m_clipping_query;
223 GLuint m_sample_query;
224 GLuint m_fragment_shader_query;
225};
226
227int main( int argc, char **argv )
228{
229 const char *filename= "data/bigguy.obj";
230 if(argc > 1)
231 filename= argv[1];
232
233 TP app( filename );
234 app.run();
235
236 return 0;
237}
238
239
AppTime(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_time.cpp:8
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
Definition alpha.cpp:58
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 init()
a deriver pour creer 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 clear_key_state(const SDL_Keycode key)
desactive une touche du clavier.
Definition window.cpp:69
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 key_state(const SDL_Keycode key)
renvoie l'etat d'une touche du clavier. cf la doc SDL2 pour les codes.
Definition window.cpp:63
int window_width()
renvoie la largeur de la fenetre de l'application.
Definition window.cpp:23
Transform Identity()
construit la transformation identite.
Definition mat.cpp:187
Mesh read_indexed_mesh_fast(const char *filename)
charge un fichier wavefront .obj et renvoie un mesh compose de triangles indexes. utiliser glDrawElem...
int capture(const char *prefix)
Definition texture.cpp:205
GLuint read_program(const char *filename, const char *definitions)
Definition program.cpp:214
int program_print_errors(const GLuint program, const char *filename)
affiche les erreurs de compilation.
Definition program.cpp:459
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