Back to index

nux  3.0.0
IOpenGLQuery.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright 2010 Inalogic® Inc.
00003  *
00004  * This program is free software: you can redistribute it and/or modify it
00005  * under the terms of the GNU Lesser General Public License, as
00006  * published by the  Free Software Foundation; either version 2.1 or 3.0
00007  * of the License.
00008  *
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranties of
00011  * MERCHANTABILITY, SATISFACTORY QUALITY or FITNESS FOR A PARTICULAR
00012  * PURPOSE.  See the applicable version of the GNU Lesser General Public
00013  * License for more details.
00014  *
00015  * You should have received a copy of both the GNU Lesser General Public
00016  * License along with this program. If not, see <http://www.gnu.org/licenses/>
00017  *
00018  * Authored by: Jay Taoko <jaytaoko@inalogic.com>
00019  *
00020  */
00021 
00022 
00023 #include "GLResource.h"
00024 #include "GpuDevice.h"
00025 
00026 #include "GLDeviceObjects.h"
00027 #include "IOpenGLQuery.h"
00028 
00029 namespace nux
00030 {
00031 
00032   NUX_IMPLEMENT_OBJECT_TYPE(IOpenGLQuery);
00033 
00034   unsigned int IOpenGLQuery::_CurrentlyActiveQuery = 0;
00035 
00036   IOpenGLQuery::IOpenGLQuery(QUERY_TYPE Type)
00037     : IOpenGLResource(RTQUERY)
00038     , _Type(Type)
00039     , _QueryStarted(false)
00040   {
00041 #ifndef NUX_OPENGLES_20
00042     CHECKGL(glGenQueriesARB(1, &_OpenGLID)) ;
00043 #endif
00044   }
00045 
00046 // The return type identifies the query state(see Queries).
00047 // The method returns 1(S_OK) if the query data is available and 0(S_FALSE) if it is not.
00048 // These are considered successful return values.
00049   int IOpenGLQuery::GetData(
00050     int *pData,
00051     unsigned int Size,
00052     unsigned int GetDataFlags
00053   )
00054   {
00055 #ifndef NUX_OPENGLES_20
00056     unsigned int ResultReady = 0;
00057     glGetQueryObjectuivARB(_OpenGLID, GL_QUERY_RESULT_AVAILABLE_ARB, &ResultReady);
00058     CHECKGL_MSG( glGetQueryObjectuivARB );
00059 
00060     if (ResultReady)
00061     {
00062       glGetQueryObjectuivARB(_OpenGLID, GL_QUERY_RESULT_ARB, (GLuint *) pData);
00063       CHECKGL_MSG( glGetQueryObjectuivARB );
00064       return 1;
00065     }
00066 #endif
00067 
00068     return 0;
00069   }
00070 
00071   unsigned int IOpenGLQuery::GetDataSize()
00072   {
00073     return 0;
00074   }
00075 
00076   void IOpenGLQuery::GetDevice(GpuDevice **ppDevice)
00077   {
00078     // Do not call this function.
00079     *ppDevice = NULL;
00080   }
00081 
00082   QUERY_TYPE IOpenGLQuery::GetType()
00083   {
00084     return _Type;
00085   }
00086 
00087   void IOpenGLQuery::Issue(
00088     unsigned int IssueFlags
00089   )
00090   {
00091 #ifndef NUX_OPENGLES_20
00092     if (IssueFlags == (unsigned int) ISSUE_BEGIN)
00093     {
00094       nuxAssert(_CurrentlyActiveQuery == 0);
00095 
00096       if (_QueryStarted == true)
00097       {
00098         nuxError("The Query as already been activated");
00099       }
00100       else
00101       {
00102         _QueryStarted = true;
00103         CHECKGL(glBeginQueryARB(GL_SAMPLES_PASSED_ARB, _OpenGLID));
00104         _CurrentlyActiveQuery = _OpenGLID;
00105       }
00106     }
00107     else if (IssueFlags == (unsigned int) ISSUE_END)
00108     {
00109       nuxAssert(_CurrentlyActiveQuery == _OpenGLID);
00110 
00111       if (_QueryStarted == false)
00112       {
00113         nuxError("The Query as already been stoped");
00114       }
00115       else
00116       {
00117         _QueryStarted = false;
00118         CHECKGL(glEndQueryARB(GL_SAMPLES_PASSED_ARB));
00119         _CurrentlyActiveQuery = 0;
00120       }
00121     }
00122 #endif
00123   }
00124 
00125 // Return True is the result is available. That is glGetQueryObjectuivARB won't block
00126 // if called with GL_QUERY_RESULT_ARB.
00127   bool IOpenGLQuery::IsResultAvailable()
00128   {
00129     unsigned int ResultReady = 0;
00130 #ifndef NUX_OPENGLES_20
00131     glGetQueryObjectuivARB(_OpenGLID, GL_QUERY_RESULT_AVAILABLE_ARB, &ResultReady);
00132     CHECKGL_MSG( glGetQueryObjectuivARB );
00133 #endif
00134 
00135     return ResultReady != 0;
00136   }
00137 // Return the result of the query. Make sure IsResultAvailable returned TRUE before calling this function.
00138 // If you fail to do that, GetResult will block before returning.
00139   unsigned int IOpenGLQuery::GetResult()
00140   {
00141     unsigned int SamplesPassed = 0;
00142 #ifndef NUX_OPENGLES_20
00143     glGetQueryObjectuivARB(_OpenGLID, GL_QUERY_RESULT_ARB, &SamplesPassed);
00144     CHECKGL_MSG( glGetQueryObjectuivARB );
00145 #endif
00146 
00147     return SamplesPassed;
00148   }
00149 
00150 }
00151