47 if(!GLAD_GL_ARB_indirect_parameters)
49 printf(
"GL_ARB_indirect_parameters ON\n");
51 if(!GLAD_GL_ARB_shader_draw_parameters)
53 printf(
"GL_ARB_shader_draw_parameters ON\n");
57 m_object.bounds(pmin, pmax);
58 m_camera.lookat(pmin -
Vector(200, 200, 0), pmax +
Vector(200, 200, 0));
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);
97 m_vao= m_object.create_buffers(
false,
false,
false,
false);
100 m_program_cull=
read_program(
"gkit2_tutos/M2/indirect_cull.glsl");
103 m_program=
read_program(
"gkit2_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);
148 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
152 unsigned int mb= SDL_GetRelativeMouseState(&mx, &my);
153 if(mb & SDL_BUTTON(1))
154 m_camera.rotation(mx, my);
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);
168 program_uniform(m_program_cull,
"bmin",
Point(-60, -60, -10));
169 program_uniform(m_program_cull,
"bmax",
Point(60, 60, 10));
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);
201 program_uniform(m_program,
"modelMatrix", m_model);
203 program_uniform(m_program,
"viewMatrix", m_camera.view());
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));
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.
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().