23 unsigned int vertex_count;
24 unsigned int instance_count;
25 unsigned int first_vertex;
26 unsigned int first_instance;
33 unsigned int vertex_count;
35 unsigned int vertex_base;
42 TP( ) :
App(1024, 640, 4,3) {}
47 if(!GLEW_ARB_indirect_parameters)
49 printf(
"GL_ARB_indirect_parameters ON\n");
51 if(!GLEW_ARB_shader_draw_parameters)
53 printf(
"GL_ARB_shader_draw_parameters ON\n");
57 m_object.
bounds(pmin, pmax);
61 for(
int y= -15; y <= 15; y++)
62 for(
int x= -15; x <= 15; x++)
64 m_multi_model.push_back(
Translation(x *20, y *20, 0) );
67 m_objects.push_back( {pmin +
Vector(x *20, y *20, 0), unsigned(m_object.
vertex_count()), pmax +
Vector(x *20, y *20, 0), 0} );
72 glGenBuffers(1, &m_model_buffer);
73 glBindBuffer(GL_SHADER_STORAGE_BUFFER, m_model_buffer);
74 glBufferData(GL_SHADER_STORAGE_BUFFER,
sizeof(
Transform) * m_multi_model.size(), m_multi_model.data(), GL_DYNAMIC_DRAW);
77 glGenBuffers(1, &m_object_buffer);
78 glBindBuffer(GL_SHADER_STORAGE_BUFFER, m_object_buffer);
79 glBufferData(GL_SHADER_STORAGE_BUFFER,
sizeof(
Object) * m_objects.size(), m_objects.data(), GL_DYNAMIC_DRAW);
82 glGenBuffers(1, &m_remap_buffer);
83 glBindBuffer(GL_SHADER_STORAGE_BUFFER, m_remap_buffer);
84 glBufferData(GL_SHADER_STORAGE_BUFFER,
sizeof(
int) * m_objects.size(),
nullptr, GL_DYNAMIC_DRAW);
87 glGenBuffers(1, &m_indirect_buffer);
88 glBindBuffer(GL_DRAW_INDIRECT_BUFFER, m_indirect_buffer);
89 glBufferData(GL_DRAW_INDIRECT_BUFFER,
sizeof(
IndirectParam) * m_objects.size(),
nullptr, GL_DYNAMIC_DRAW);
92 glGenBuffers(1, &m_parameter_buffer);
93 glBindBuffer(GL_PARAMETER_BUFFER_ARB, m_parameter_buffer);
94 glBufferData(GL_PARAMETER_BUFFER_ARB,
sizeof(
int),
nullptr, GL_DYNAMIC_DRAW);
100 m_program_cull=
read_program(
"tutos/M2/indirect_cull.glsl");
103 m_program=
read_program(
"tutos/M2/indirect_remap.glsl");
110 glGenQueries(1, &m_time_query);
113 glClearColor(0.2f, 0.2f, 0.2f, 1.f);
116 glDepthFunc(GL_LESS);
117 glEnable(GL_DEPTH_TEST);
124 glDeleteQueries(1, &m_time_query);
132 glDeleteBuffers(1, &m_indirect_buffer);
133 glDeleteBuffers(1, &m_parameter_buffer);
134 glDeleteBuffers(1, &m_remap_buffer);
135 glDeleteBuffers(1, &m_object_buffer);
140 int update(
const float time,
const float delta )
148 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
152 unsigned int mb= SDL_GetRelativeMouseState(&mx, &my);
153 if(mb & SDL_BUTTON(1))
155 else if(mb & SDL_BUTTON(3))
157 else if(mb & SDL_BUTTON(2))
161 glBeginQuery(GL_TIME_ELAPSED, m_time_query);
162 std::chrono::high_resolution_clock::time_point cpu_start= std::chrono::high_resolution_clock::now();
165 glUseProgram(m_program_cull);
174 glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, m_object_buffer);
175 glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, m_remap_buffer);
176 glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 2, m_indirect_buffer);
179 glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 3, m_parameter_buffer);
181 unsigned int zero= 0;
182 glClearBufferData(GL_SHADER_STORAGE_BUFFER, GL_R32UI, GL_RED_INTEGER, GL_UNSIGNED_INT, &zero);
187 int n= m_objects.size() / 256;
188 if(m_objects.size() % 256)
191 glDispatchCompute(n, 1, 1);
194 glMemoryBarrier(GL_COMMAND_BARRIER_BIT | GL_SHADER_STORAGE_BARRIER_BIT);
197 glBindVertexArray(m_vao);
198 glUseProgram(m_program);
206 glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, m_model_buffer);
207 glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, m_remap_buffer);
210 glBindBuffer(GL_PARAMETER_BUFFER_ARB, m_parameter_buffer);
211 glBindBuffer(GL_DRAW_INDIRECT_BUFFER, m_indirect_buffer);
213 glMultiDrawArraysIndirectCountARB(m_object.
primitives(), 0, 0, m_objects.size(), 0);
216 glEndQuery(GL_TIME_ELAPSED);
217 std::chrono::high_resolution_clock::time_point cpu_stop= std::chrono::high_resolution_clock::now();
218 long long int cpu_time= std::chrono::duration_cast<std::chrono::nanoseconds>(cpu_stop - cpu_start).count();
222 glGetQueryObjecti64v(m_time_query, GL_QUERY_RESULT, &gpu_time);
225 printf(m_console, 0, 0,
"cpu %02dms %03dus", (
int) (cpu_time / 1000000), (
int) ((cpu_time / 1000) % 1000));
226 printf(m_console, 0, 1,
"gpu %02dms %03dus", (
int) (gpu_time / 1000000), (
int) ((gpu_time / 1000) % 1000));
230 printf(
"cpu %02dms %03dus ", (
int) (cpu_time / 1000000), (
int) ((cpu_time / 1000) % 1000));
231 printf(
"gpu %02dms %03dus\n", (
int) (gpu_time / 1000000), (
int) ((gpu_time / 1000) % 1000));
237 GLuint m_parameter_buffer;
238 GLuint m_indirect_buffer;
239 GLuint m_model_buffer;
240 GLuint m_object_buffer;
241 GLuint m_remap_buffer;
245 GLuint m_program_cull;
254 std::vector<Transform> m_multi_model;
255 std::vector<Object> m_objects;
260 int main(
int argc,
char **argv )
int run()
execution de l'application.
representation d'un objet / maillage.
GLuint create_buffers(const bool use_texcoord, const bool use_normal, const bool use_color, const bool use_material_index)
construit les buffers et le vertex array object necessaires pour dessiner l'objet avec openGL....
void bounds(Point &pmin, Point &pmax) const
renvoie min et max les coordonnees des extremites des positions des sommets de l'objet (boite engloba...
int vertex_count() const
renvoie le nombre de sommets.
GLenum primitives() const
renvoie le type de primitives.
void release()
detruit les objets openGL.
representation de la camera, type orbiter, placee sur une sphere autour du centre de l'objet.
void lookat(const Point ¢er, const float size)
observe le point center a une distance size.
void move(const float z)
rapproche / eloigne la camera du centre.
Transform projection(const int width, const int height, const float fov)
fixe la projection reglee pour une image d'aspect width / height, et une demi ouverture de fov degres...
void translation(const float x, const float y)
deplace le centre / le point observe.
void rotation(const float x, const float y)
change le point de vue / la direction d'observation.
Transform view() const
renvoie la transformation vue.
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 update(const float time, const float delta)
a deriver et redefinir pour animer les objets en fonction du temps.
int init()
a deriver pour creer les objets openGL. renvoie -1 pour indiquer une erreur, 0 sinon.
void clear(Text &text)
efface le contenu de la console.
int window_height()
renvoie la hauteur de la fenetre de l'application.
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().
Text create_text()
cree une console. a detruire avec release_text( ).
void release_text(Text &text)
detruit une console.
int window_width()
renvoie la largeur de la fenetre de l'application.
Transform RotationY(const float a)
renvoie la matrice representation une rotation de a degree autour de l'axe Y.
Transform Translation(const Vector &v)
renvoie la matrice representant une translation par un vecteur.
Mesh read_mesh(const char *filename)
charge un fichier wavefront .obj et renvoie un mesh compose de triangles non indexes....
GLuint read_program(const char *filename, const char *definitions)
void program_uniform(const GLuint program, const char *uniform, const std::vector< unsigned > &v)
affecte un tableau de valeurs a un uniform du shader program.
int program_print_errors(const GLuint program)
affiche les erreurs de compilation.
int release_program(const GLuint program)
detruit les shaders et le program.
tuto_mdi_elements.cpp exemple d'utilisation de multidrawindirect pour des triangles indexes.
representation d'un point 3d.
representation d'un vecteur 3d.