nux-1.14.0
IOpenGLQuery.cpp
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   t_u32 IOpenGLQuery::_CurrentlyActiveQuery = 0;
00035 
00036   IOpenGLQuery::IOpenGLQuery (QUERY_TYPE Type)
00037     : IOpenGLResource (RTQUERY)
00038     , _Type (Type)
00039     , _QueryStarted (false)
00040   {
00041     CHECKGL ( glGenQueriesARB (1, &_OpenGLID) ) ;
00042   }
00043 
00044 // The return type identifies the query state (see Queries).
00045 // The method returns 1 (S_OK) if the query data is available and 0 (S_FALSE) if it is not.
00046 // These are considered successful return values.
00047   int IOpenGLQuery::GetData (
00048     int *pData,
00049     t_u32 Size,
00050     t_u32 GetDataFlags
00051   )
00052   {
00053     unsigned int ResultReady = 0;
00054     glGetQueryObjectuivARB (_OpenGLID, GL_QUERY_RESULT_AVAILABLE_ARB, &ResultReady);
00055     CHECKGL_MSG ( glGetQueryObjectuivARB );
00056 
00057     if (ResultReady)
00058     {
00059       glGetQueryObjectuivARB (_OpenGLID, GL_QUERY_RESULT_ARB, (GLuint *) pData);
00060       CHECKGL_MSG ( glGetQueryObjectuivARB );
00061       return 1;
00062     }
00063     else
00064     {
00065       return 0;
00066     }
00067   }
00068 
00069   t_u32 IOpenGLQuery::GetDataSize()
00070   {
00071     return 0;
00072   }
00073 
00074   void IOpenGLQuery::GetDevice (GpuDevice **ppDevice)
00075   {
00076     // Do not call this function.
00077     *ppDevice = NULL;
00078   }
00079 
00080   QUERY_TYPE IOpenGLQuery::GetType()
00081   {
00082     return _Type;
00083   }
00084 
00085   void IOpenGLQuery::Issue (
00086     t_u32 IssueFlags
00087   )
00088   {
00089     if (IssueFlags == (t_u32) ISSUE_BEGIN)
00090     {
00091       nuxAssert (_CurrentlyActiveQuery == 0);
00092 
00093       if (_QueryStarted == true)
00094       {
00095         nuxError (TEXT ("The Query as already been activated") );
00096       }
00097       else
00098       {
00099         _QueryStarted = true;
00100         CHECKGL ( glBeginQueryARB (GL_SAMPLES_PASSED_ARB, _OpenGLID) );
00101         _CurrentlyActiveQuery = _OpenGLID;
00102       }
00103     }
00104     else if (IssueFlags == (t_u32) ISSUE_END)
00105     {
00106       nuxAssert (_CurrentlyActiveQuery == _OpenGLID);
00107 
00108       if (_QueryStarted == false)
00109       {
00110         nuxError (TEXT ("The Query as already been stoped") );
00111       }
00112       else
00113       {
00114         _QueryStarted = false;
00115         CHECKGL ( glEndQueryARB (GL_SAMPLES_PASSED_ARB) );
00116         _CurrentlyActiveQuery = 0;
00117       }
00118     }
00119   }
00120 
00121 // Return True is the result is available. That is glGetQueryObjectuivARB won't block
00122 // if called with GL_QUERY_RESULT_ARB.
00123   bool IOpenGLQuery::IsResultAvailable()
00124   {
00125     unsigned int ResultReady = 0;
00126     glGetQueryObjectuivARB (_OpenGLID, GL_QUERY_RESULT_AVAILABLE_ARB, &ResultReady);
00127     CHECKGL_MSG ( glGetQueryObjectuivARB );
00128 
00129     return ResultReady != 0;
00130   }
00131 // Return the result of the query. Make sure IsResultAvailable returned TRUE before calling this function.
00132 // If you fail to do that, GetResult will block before returning.
00133   unsigned int IOpenGLQuery::GetResult()
00134   {
00135     unsigned int SamplesPassed = 0;
00136     glGetQueryObjectuivARB (_OpenGLID, GL_QUERY_RESULT_ARB, &SamplesPassed);
00137     CHECKGL_MSG ( glGetQueryObjectuivARB );
00138 
00139     return SamplesPassed;
00140   }
00141 
00142 }
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends