Skip to content

Commit 9a037b5

Browse files
committedMar 27, 2014
experimented with sorting by texture/program
1 parent 2d5b448 commit 9a037b5

File tree

1 file changed

+46
-2
lines changed

1 file changed

+46
-2
lines changed
 

‎gfx.cpp

+46-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#include <cassert>
22

3+
#include <algorithm>
34
#include <list>
5+
#include <vector>
46

57
#include "file.hpp"
68
#include "gfx.hpp"
@@ -363,22 +365,32 @@ struct render_command_t
363365
float uniform3f0[3];
364366
};
365367

368+
int cnt_render_commands = 0;
369+
int cnt_use_program = 0;
370+
int cnt_bind_tex0 = 0;
371+
int cnt_bind_tex1 = 0;
372+
int cnt_glbegin = 0;
373+
int cnt_glend = 0;
374+
366375
int bound_program = 0;
367376
int bound_tex0 = 0;
368377
int bound_tex1 = 0;
369378
bool did_begin = false;
370379

371380
static void render_command(render_command_t *cmd)
372381
{
382+
cnt_render_commands += 1;
373383
if (cmd->program != bound_program)
374384
{
375385
if (did_begin)
376386
{
377387
glEnd();
378388
did_begin = false;
389+
cnt_glend += 1;
379390
}
380391
glUseProgram(cmd->program);
381392
bound_program = cmd->program;
393+
cnt_use_program += 1;
382394
}
383395

384396
if (cmd->program != 0)
@@ -387,6 +399,7 @@ static void render_command(render_command_t *cmd)
387399
{
388400
glEnd();
389401
did_begin = false;
402+
cnt_glend += 1;
390403
}
391404
if (cmd->uniform1i0_loc != -1)
392405
{
@@ -407,21 +420,25 @@ static void render_command(render_command_t *cmd)
407420
{
408421
glEnd();
409422
did_begin = false;
423+
cnt_glend += 1;
410424
}
411425
glBindTexture(GL_TEXTURE_2D, cmd->tex0);
412426
bound_tex0 = cmd->tex0;
427+
cnt_bind_tex0 += 1;
413428
}
414429
if (cmd->tex1 != bound_tex1)
415430
{
416431
if (did_begin)
417432
{
418433
glEnd();
419434
did_begin = false;
435+
cnt_glend += 1;
420436
}
421437
glActiveTexture(GL_TEXTURE1);
422438
glBindTexture(GL_TEXTURE_2D, cmd->tex1);
423439
glActiveTexture(GL_TEXTURE0);
424440
bound_tex1 = cmd->tex1;
441+
cnt_bind_tex1 += 1;
425442
}
426443

427444
//check_gl_error(__LINE__);
@@ -430,6 +447,7 @@ static void render_command(render_command_t *cmd)
430447
{
431448
glBegin(GL_QUADS);
432449
did_begin = true;
450+
cnt_glbegin += 1;
433451
}
434452
for (int i = 0; i < 4; i++)
435453
{
@@ -438,10 +456,34 @@ static void render_command(render_command_t *cmd)
438456
}
439457
}
440458

441-
std::list<render_command_t> cmds;
459+
std::vector<render_command_t> cmds;
460+
461+
bool compare_program(const render_command_t &a, const render_command_t &b)
462+
{
463+
return a.program < b.program;
464+
}
465+
466+
bool compare_tex0(const render_command_t &a, const render_command_t &b)
467+
{
468+
return a.tex0 < b.tex0;
469+
}
470+
471+
/*bool myfunction (int i,int j) { return (i<j); }
472+
473+
struct myclass {
474+
bool operator() (int i,int j) { return (i<j);}
475+
} myobject;
476+
*/
477+
478+
struct myclass {
479+
bool operator() (const render_command_t &a, const render_command_t &b) { return a.tex0 < b.tex1; }
480+
} mysorter;
442481

443482
void gfx_flush()
444483
{
484+
std::sort(cmds.begin(), cmds.end(), compare_tex0);
485+
//std::sort(cmds.begin(), cmds.end(), compare_program);
486+
445487
// prepare a good state
446488
{
447489
glMatrixMode(GL_PROJECTION);
@@ -458,8 +500,9 @@ void gfx_flush()
458500
}
459501
//check_gl_error(__LINE__);
460502

503+
461504
// execute all render commands
462-
for (std::list<render_command_t>::iterator it = cmds.begin(); it != cmds.end(); ++it)
505+
for (std::vector<render_command_t>::iterator it = cmds.begin(); it != cmds.end(); ++it)
463506
{
464507
render_command_t *cmd = &(*it);
465508
render_command(cmd);
@@ -472,6 +515,7 @@ void gfx_flush()
472515
{
473516
glEnd();
474517
did_begin = false;
518+
cnt_glend += 1;
475519
}
476520

477521
// clean up state

0 commit comments

Comments
 (0)
Please sign in to comment.