62
62
using std::max;
63
63
using std::string;
64
64
using std::vector;
65
+ using std::set;
65
66
66
67
67
68
static const int MAX_TEXTURE_UNITS = 32 ;
@@ -78,7 +79,7 @@ extern GLfloat LightAmbientLand[];
78
79
void (*LuaOpenGL::resetMatrixFunc)(void ) = NULL ;
79
80
80
81
unsigned int LuaOpenGL::resetStateList = 0 ;
81
- unsigned int LuaOpenGL::occlusionQuery = 0 ;
82
+ set< unsigned int > LuaOpenGL::occlusionQueries ;
82
83
83
84
LuaOpenGL::DrawMode LuaOpenGL::drawMode = LuaOpenGL::DRAW_NONE;
84
85
LuaOpenGL::DrawMode LuaOpenGL::prevDrawMode = LuaOpenGL::DRAW_NONE;
@@ -113,18 +114,18 @@ void LuaOpenGL::Init()
113
114
if (haveGL20 && !!configHandler.GetInt (" LuaShaders" , 1 )) {
114
115
canUseShaders = true ;
115
116
}
116
-
117
- if (haveGL20) {
118
- glGenQueries (1 , &occlusionQuery);
119
- }
120
117
}
121
118
122
119
123
120
void LuaOpenGL::Free ()
124
121
{
125
122
glDeleteLists (resetStateList, 1 );
123
+
126
124
if (haveGL20) {
127
- glDeleteQueries (1 , &occlusionQuery);
125
+ set<unsigned int >::const_iterator it;
126
+ for (it = occlusionQueries.begin (); it != occlusionQueries.end (); ++it) {
127
+ glDeleteQueries (1 , &(*it));
128
+ }
128
129
}
129
130
}
130
131
@@ -277,7 +278,10 @@ bool LuaOpenGL::PushEntries(lua_State* L)
277
278
REGISTER_LUA_CFUNC (ReadPixels);
278
279
279
280
if (haveGL20) {
280
- REGISTER_LUA_CFUNC (OcclusionQuery);
281
+ REGISTER_LUA_CFUNC (CreateQuery);
282
+ REGISTER_LUA_CFUNC (DeleteQuery);
283
+ REGISTER_LUA_CFUNC (RunQuery);
284
+ REGISTER_LUA_CFUNC (GetQuery);
281
285
}
282
286
283
287
REGISTER_LUA_CFUNC (GetGlobalTexNames);
@@ -4489,36 +4493,98 @@ int LuaOpenGL::ReadPixels(lua_State* L)
4489
4493
4490
4494
/* *****************************************************************************/
4491
4495
4492
- int LuaOpenGL::OcclusionQuery (lua_State* L)
4496
+ int LuaOpenGL::CreateQuery (lua_State* L)
4493
4497
{
4494
- CheckDrawingEnabled (L, __FUNCTION__);
4498
+ GLuint q;
4499
+ glGenQueries (1 , &q);
4500
+ if (q == 0 ) {
4501
+ return 0 ;
4502
+ }
4503
+ occlusionQueries.insert (q);
4504
+ lua_pushlightuserdata (L, (void *)q);
4505
+ return 1 ;
4506
+ }
4495
4507
4496
- const int args = lua_gettop (L); // number of arguments
4497
- if (!lua_isfunction (L, 1 )) {
4498
- luaL_error (L, " Incorrect arguments to gl.OcclusionQuery(func, ...)" );
4508
+
4509
+ int LuaOpenGL::DeleteQuery (lua_State* L)
4510
+ {
4511
+ if (lua_isnil (L, 1 )) {
4512
+ return 0 ;
4513
+ }
4514
+ if (!lua_islightuserdata (L, 1 )) {
4515
+ luaL_error (L, " invalid argument" );
4516
+ }
4517
+ GLuint q = (unsigned long int )lua_topointer (L, 1 );
4518
+ if (occlusionQueries.find (q) != occlusionQueries.end ()) {
4519
+ occlusionQueries.erase (q);
4520
+ glDeleteQueries (1 , &q);
4499
4521
}
4522
+ return 0 ;
4523
+ }
4500
4524
4501
- glBeginQuery (GL_SAMPLES_PASSED, occlusionQuery);
4502
4525
4503
- // call the function
4504
- const int error = lua_pcall (L, (args - 1 ), 0 , 0 );
4526
+ int LuaOpenGL::RunQuery (lua_State* L)
4527
+ {
4528
+ static bool running = false ;
4529
+
4530
+ if (running) {
4531
+ luaL_error (L, " not re-entrant" );
4532
+ }
4533
+ if (!lua_islightuserdata (L, 1 )) {
4534
+ luaL_error (L, " expecting a query" );
4535
+ }
4536
+ GLuint q = (unsigned long int )lua_topointer (L, 1 );
4537
+ if (occlusionQueries.find (q) == occlusionQueries.end ()) {
4538
+ return 0 ;
4539
+ }
4540
+ if (!lua_isfunction (L, 2 )) {
4541
+ luaL_error (L, " expecting a function" );
4542
+ }
4543
+ const int args = lua_gettop (L); // number of arguments
4505
4544
4545
+ running = true ;
4546
+ glBeginQuery (GL_SAMPLES_PASSED, q);
4547
+ const int error = lua_pcall (L, (args - 2 ), 0 , 0 );
4506
4548
glEndQuery (GL_SAMPLES_PASSED);
4549
+ running = false ;
4550
+
4551
+ if (error != 0 ) {
4552
+ logOutput.Print (" gl.RunQuery: error(%i) = %s\n " ,
4553
+ error, lua_tostring (L, -1 ));
4554
+ lua_error (L);
4555
+ }
4556
+
4557
+ return 0 ;
4558
+ }
4507
4559
4508
- GLuint ready, count;
4509
- while (true ) {
4510
- glGetQueryObjectuiv (occlusionQuery, GL_QUERY_RESULT_AVAILABLE, &ready);
4560
+
4561
+ int LuaOpenGL::GetQuery (lua_State* L)
4562
+ {
4563
+ if (!lua_islightuserdata (L, 1 )) {
4564
+ luaL_error (L, " invalid argument" );
4565
+ }
4566
+ GLuint q = (unsigned long int )lua_topointer (L, 1 );
4567
+ if (occlusionQueries.find (q) == occlusionQueries.end ()) {
4568
+ return 0 ;
4569
+ }
4570
+
4571
+ GLint currQ = 0 ;
4572
+ while (false ) { // FIXME
4573
+ GLuint ready;
4574
+ glGetQueryObjectuiv (q, GL_QUERY_RESULT_AVAILABLE, &ready);
4511
4575
if (ready == GL_TRUE) {
4512
4576
break ;
4513
4577
}
4578
+ if (currQ == 0 ) {
4579
+ glGetQueryiv (GL_SAMPLES_PASSED, GL_CURRENT_QUERY, &currQ);
4580
+ if (currQ != q) {
4581
+ return 0 ;
4582
+ }
4583
+ }
4514
4584
}
4515
- glGetQueryObjectuiv (occlusionQuery, GL_QUERY_RESULT, &count);
4516
4585
4517
- if (error != 0 ) {
4518
- logOutput.Print (" gl.OcclusionQuery: error(%i) = %s\n " ,
4519
- error, lua_tostring (L, -1 ));
4520
- lua_error (L);
4521
- }
4586
+ GLuint count;
4587
+ glGetQueryObjectuiv (q, GL_QUERY_RESULT, &count);
4522
4588
4523
4589
lua_pushnumber (L, count);
4524
4590
0 commit comments