Skip to content

Commit cbb8d8e

Browse files
author
trepan
committed
* Replaced gl.OcclusionQuery() with:
gl.CreateQuery() -> query gl.DeleteQuery(query) gl.RunQuery(query, function, ...) gl.GetQuery(query) -> number - this setup is much faster as it allows you to run a query, and check on the result later (avoiding the immediate glGet that gl.OcclusionQuery() was using) - the ready test needs a litte work git-svn-id: https://spring.clan-sy.com/svn/spring/trunk@5383 37977431-3df6-0310-b722-df95706aa16b
1 parent 8933083 commit cbb8d8e

File tree

2 files changed

+97
-27
lines changed

2 files changed

+97
-27
lines changed

rts/Lua/LuaOpenGL.cpp

Lines changed: 90 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
using std::max;
6363
using std::string;
6464
using std::vector;
65+
using std::set;
6566

6667

6768
static const int MAX_TEXTURE_UNITS = 32;
@@ -78,7 +79,7 @@ extern GLfloat LightAmbientLand[];
7879
void (*LuaOpenGL::resetMatrixFunc)(void) = NULL;
7980

8081
unsigned int LuaOpenGL::resetStateList = 0;
81-
unsigned int LuaOpenGL::occlusionQuery = 0;
82+
set<unsigned int> LuaOpenGL::occlusionQueries;
8283

8384
LuaOpenGL::DrawMode LuaOpenGL::drawMode = LuaOpenGL::DRAW_NONE;
8485
LuaOpenGL::DrawMode LuaOpenGL::prevDrawMode = LuaOpenGL::DRAW_NONE;
@@ -113,18 +114,18 @@ void LuaOpenGL::Init()
113114
if (haveGL20 && !!configHandler.GetInt("LuaShaders", 1)) {
114115
canUseShaders = true;
115116
}
116-
117-
if (haveGL20) {
118-
glGenQueries(1, &occlusionQuery);
119-
}
120117
}
121118

122119

123120
void LuaOpenGL::Free()
124121
{
125122
glDeleteLists(resetStateList, 1);
123+
126124
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+
}
128129
}
129130
}
130131

@@ -277,7 +278,10 @@ bool LuaOpenGL::PushEntries(lua_State* L)
277278
REGISTER_LUA_CFUNC(ReadPixels);
278279

279280
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);
281285
}
282286

283287
REGISTER_LUA_CFUNC(GetGlobalTexNames);
@@ -4489,36 +4493,98 @@ int LuaOpenGL::ReadPixels(lua_State* L)
44894493

44904494
/******************************************************************************/
44914495

4492-
int LuaOpenGL::OcclusionQuery(lua_State* L)
4496+
int LuaOpenGL::CreateQuery(lua_State* L)
44934497
{
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+
}
44954507

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);
44994521
}
4522+
return 0;
4523+
}
45004524

4501-
glBeginQuery(GL_SAMPLES_PASSED, occlusionQuery);
45024525

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
45054544

4545+
running = true;
4546+
glBeginQuery(GL_SAMPLES_PASSED, q);
4547+
const int error = lua_pcall(L, (args - 2), 0, 0);
45064548
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+
}
45074559

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);
45114575
if (ready == GL_TRUE) {
45124576
break;
45134577
}
4578+
if (currQ == 0) {
4579+
glGetQueryiv(GL_SAMPLES_PASSED, GL_CURRENT_QUERY, &currQ);
4580+
if (currQ != q) {
4581+
return 0;
4582+
}
4583+
}
45144584
}
4515-
glGetQueryObjectuiv(occlusionQuery, GL_QUERY_RESULT, &count);
45164585

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);
45224588

45234589
lua_pushnumber(L, count);
45244590

rts/Lua/LuaOpenGL.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
//////////////////////////////////////////////////////////////////////
66

77
#include <string>
8+
#include <set>
89

910

1011
struct lua_State;
@@ -110,7 +111,7 @@ class LuaOpenGL {
110111
static float screenDistance;
111112
static void (*resetMatrixFunc)(void);
112113
static unsigned int resetStateList;
113-
static unsigned int occlusionQuery;
114+
static std::set<unsigned int> occlusionQueries;
114115

115116
private:
116117
static void CheckDrawingEnabled(lua_State* L, const char* caller);
@@ -244,8 +245,11 @@ class LuaOpenGL {
244245

245246
static int ReadPixels(lua_State* L);
246247

247-
static int OcclusionQuery(lua_State* L);
248-
248+
static int CreateQuery(lua_State* L);
249+
static int DeleteQuery(lua_State* L);
250+
static int RunQuery(lua_State* L);
251+
static int GetQuery(lua_State* L);
252+
249253
static int GetGlobalTexNames(lua_State* L);
250254
static int GetGlobalTexCoords(lua_State* L);
251255
static int GetShadowMapParams(lua_State* L);

0 commit comments

Comments
 (0)