blob: 10edda5c576f53454d7dc7383090bcea2090bd85 [file] [log] [blame]
daniel@transgaming.com86bdb822012-01-20 18:24:39 +00001//
2// Copyright (c) 2012 The ANGLE Project Authors. All rights reserved.
3// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
7// Query.cpp: Implements the gl::Query class
8
9#include "libGLESv2/Query.h"
10
11#include "libGLESv2/main.h"
12
13namespace gl
14{
15
16Query::Query(GLuint id, GLenum type) : RefCountObject(id)
17{
18 mQuery = NULL;
19 mStatus = GL_FALSE;
20 mResult = GL_FALSE;
21 mType = type;
22}
23
24Query::~Query()
25{
26 if (mQuery != NULL)
27 {
28 mQuery->Release();
29 mQuery = NULL;
30 }
31}
32
33void Query::begin()
34{
35 if (mQuery == NULL)
36 {
37 if (FAILED(getDevice()->CreateQuery(D3DQUERYTYPE_OCCLUSION, &mQuery)))
38 {
39 return error(GL_OUT_OF_MEMORY);
40 }
41 }
42
43 HRESULT result = mQuery->Issue(D3DISSUE_BEGIN);
44 ASSERT(SUCCEEDED(result));
45}
46
47void Query::end()
48{
49 if (mQuery == NULL)
50 {
51 return error(GL_INVALID_OPERATION);
52 }
53
54 HRESULT result = mQuery->Issue(D3DISSUE_END);
55 ASSERT(SUCCEEDED(result));
56
57 mStatus = GL_FALSE;
58 mResult = GL_FALSE;
59}
60
61GLuint Query::getResult()
62{
63 if (mQuery != NULL)
64 {
65 while (!testQuery())
66 {
67 Sleep(0);
68 // explicitly check for device loss
69 // some drivers seem to return S_FALSE even if the device is lost
70 // instead of D3DERR_DEVICELOST like they should
71 if (gl::getDisplay()->testDeviceLost())
72 {
73 gl::getDisplay()->notifyDeviceLost();
74 return error(GL_OUT_OF_MEMORY, 0);
75 }
76 }
77 }
78
79 return (GLuint)mResult;
80}
81
82GLboolean Query::isResultAvailable()
83{
84 if (mQuery != NULL)
85 {
86 testQuery();
87 }
88
89 return mStatus;
90}
91
92GLenum Query::getType() const
93{
94 return mType;
95}
96
97GLboolean Query::testQuery()
98{
99 if (mQuery != NULL && mStatus != GL_TRUE)
100 {
101 DWORD numPixels = 0;
102
103 HRESULT hres = mQuery->GetData(&numPixels, sizeof(DWORD), D3DGETDATA_FLUSH);
104 if (hres == S_OK)
105 {
106 mStatus = GL_TRUE;
107
108 switch (mType)
109 {
110 case GL_ANY_SAMPLES_PASSED_EXT:
111 case GL_ANY_SAMPLES_PASSED_CONSERVATIVE_EXT:
112 mResult = (numPixels > 0) ? GL_TRUE : GL_FALSE;
113 break;
114 default:
115 ASSERT(false);
116 }
117 }
118 else if (checkDeviceLost(hres))
119 {
120 return error(GL_OUT_OF_MEMORY, GL_TRUE);
121 }
122
123 return mStatus;
124 }
125
126 return GL_TRUE; // prevent blocking when query is null
127}
128}