~thumper/nux/next-changes

« back to all changes in this revision

Viewing changes to NuxGraphics/IOpenGLQuery.cpp

  • Committer: Neil Jagdish Patel
  • Date: 2010-09-01 21:15:42 UTC
  • Revision ID: neil.patel@canonical.com-20100901211542-cw2ce3ak28unouwb
Add NuxGraphics with licensing

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright 2010 Inalogic Inc.
 
3
 *
 
4
 * This program is free software: you can redistribute it and/or modify it 
 
5
 * under the terms of the GNU Lesser General Public License version 3, as
 
6
 * published by the  Free Software Foundation.
 
7
 *
 
8
 * This program is distributed in the hope that it will be useful, but 
 
9
 * WITHOUT ANY WARRANTY; without even the implied warranties of 
 
10
 * MERCHANTABILITY, SATISFACTORY QUALITY or FITNESS FOR A PARTICULAR 
 
11
 * PURPOSE.  See the applicable version of the GNU Lesser General Public 
 
12
 * License for more details.
 
13
 * 
 
14
 * You should have received a copy of both the GNU Lesser General Public 
 
15
 * License version 3 along with this program.  If not, see 
 
16
 * <http://www.gnu.org/licenses/>
 
17
 *
 
18
 * Authored by: Jay Taoko <jay.taoko_AT_gmail_DOT_com>
 
19
 *
 
20
 */
 
21
 
 
22
 
 
23
#include "GLResource.h"
 
24
#include "GLDeviceFactory.h"
 
25
 
 
26
#include "GLDeviceObjects.h"
 
27
#include "IOpenGLQuery.h"
 
28
 
 
29
NAMESPACE_BEGIN_OGL
 
30
 
 
31
IMPLEMENT_OBJECT_TYPE(IOpenGLQuery);
 
32
 
 
33
int IOpenGLQuery::_CurrentlyActiveQuery = 0;
 
34
IOpenGLQuery::IOpenGLQuery(QUERY_TYPE Type)
 
35
: _Type(Type)
 
36
, _QueryStarted(false)
 
37
, IOpenGLResource(RTQUERY)
 
38
{
 
39
    CHECKGL( glGenQueriesARB(1, &_OpenGLID)) ;
 
40
}
 
41
 
 
42
// The return type identifies the query state (see Queries).
 
43
// The method returns 1 (S_OK) if the query data is available and 0 (S_FALSE) if it is not.
 
44
// These are considered successful return values. 
 
45
int IOpenGLQuery::GetData(
 
46
                          int* pData,
 
47
                          DWORD Size,
 
48
                          DWORD GetDataFlags
 
49
                          )
 
50
{
 
51
    unsigned int ResultReady = 0;
 
52
    glGetQueryObjectuivARB(_OpenGLID, GL_QUERY_RESULT_AVAILABLE_ARB, &ResultReady);
 
53
    CHECKGL_MSG( glGetQueryObjectuivARB );
 
54
 
 
55
    if(ResultReady)
 
56
    {
 
57
        glGetQueryObjectuivARB(_OpenGLID, GL_QUERY_RESULT_ARB, (GLuint*)pData);
 
58
        CHECKGL_MSG( glGetQueryObjectuivARB );
 
59
        return 1;
 
60
    }
 
61
    else
 
62
    {
 
63
        return 0;
 
64
    }
 
65
}
 
66
 
 
67
DWORD IOpenGLQuery::GetDataSize()
 
68
{
 
69
    return 0;
 
70
}
 
71
 
 
72
void IOpenGLQuery::GetDevice(GLDeviceFactory **ppDevice)
 
73
{
 
74
    // Do not call this function.
 
75
    *ppDevice = NULL;
 
76
}
 
77
 
 
78
QUERY_TYPE IOpenGLQuery::GetType()
 
79
{
 
80
    return _Type;
 
81
}
 
82
 
 
83
void IOpenGLQuery::Issue(
 
84
                         DWORD IssueFlags
 
85
                         )
 
86
{
 
87
    if(IssueFlags == ISSUE_BEGIN)
 
88
    {
 
89
        nuxAssert(_CurrentlyActiveQuery == 0);
 
90
        if(_QueryStarted == true)
 
91
        {
 
92
            nuxError(TEXT("The Query as already been activated"));
 
93
        }
 
94
        else
 
95
        {
 
96
            _QueryStarted = true;
 
97
            CHECKGL( glBeginQueryARB(GL_SAMPLES_PASSED_ARB, _OpenGLID) );
 
98
            _CurrentlyActiveQuery = _OpenGLID;
 
99
        }
 
100
    }
 
101
    else if(IssueFlags == ISSUE_END)
 
102
    {
 
103
        nuxAssert(_CurrentlyActiveQuery == _OpenGLID);
 
104
        if(_QueryStarted == false)
 
105
        {
 
106
            nuxError(TEXT("The Query as already been stoped"));
 
107
        }
 
108
        else
 
109
        {
 
110
            _QueryStarted = false;
 
111
            CHECKGL( glEndQueryARB(GL_SAMPLES_PASSED_ARB) );
 
112
            _CurrentlyActiveQuery = 0;
 
113
        }
 
114
    }
 
115
}
 
116
 
 
117
// Return True is the result is available. That is glGetQueryObjectuivARB won't block
 
118
// if called with GL_QUERY_RESULT_ARB.
 
119
bool IOpenGLQuery::IsResultAvailable()
 
120
{
 
121
    unsigned int ResultReady = 0;
 
122
    glGetQueryObjectuivARB(_OpenGLID, GL_QUERY_RESULT_AVAILABLE_ARB, &ResultReady);
 
123
    CHECKGL_MSG( glGetQueryObjectuivARB );
 
124
 
 
125
    return ResultReady != 0;
 
126
}
 
127
// Return the result of the query. Make sure IsResultAvailable returned TRUE before calling this function.
 
128
// If you fail to do that, GetResult will block before returning.
 
129
unsigned int IOpenGLQuery::GetResult()
 
130
{
 
131
    unsigned int SamplesPassed = 0;
 
132
    glGetQueryObjectuivARB(_OpenGLID, GL_QUERY_RESULT_ARB, &SamplesPassed);
 
133
    CHECKGL_MSG( glGetQueryObjectuivARB );
 
134
 
 
135
    return SamplesPassed;
 
136
}
 
137
 
 
138
NAMESPACE_END_OGL