1//************************************ bs::framework - Copyright 2018 Marko Pintera **************************************//
2//*********** Licensed under the MIT license. See LICENSE.md for full terms. This notice is not to be removed. ***********//
3#include "Managers/BsQueryManager.h"
4#include "RenderAPI/BsEventQuery.h"
5#include "RenderAPI/BsTimerQuery.h"
6#include "RenderAPI/BsOcclusionQuery.h"
7#include "Profiling/BsProfilerCPU.h"
8
9namespace bs { namespace ct
10{
11 QueryManager::~QueryManager()
12 {
13 // Trigger all remaining queries, whether they completed or not
14
15 for (auto& query : mEventQueries)
16 {
17 if (query->isActive())
18 query->onTriggered();
19 }
20
21 for (auto& query : mTimerQueries)
22 {
23 if (query->isActive())
24 query->onTriggered(query->getTimeMs());
25 }
26
27 for (auto& query : mOcclusionQueries)
28 {
29 if (query->isActive())
30 query->onComplete(query->getNumSamples());
31 }
32
33 processDeletedQueue();
34 }
35
36 void QueryManager::_update()
37 {
38 for(auto& query : mEventQueries)
39 {
40 if(query->isActive() && query->isReady())
41 {
42 query->onTriggered();
43 query->setActive(false);
44 }
45 }
46
47 for(auto& query : mTimerQueries)
48 {
49 if(query->isActive() && query->isReady())
50 {
51 query->onTriggered(query->getTimeMs());
52 query->setActive(false);
53 }
54 }
55
56 for (auto& query : mOcclusionQueries)
57 {
58 if (query->isActive() && query->isReady())
59 {
60 query->onComplete(query->getNumSamples());
61 query->setActive(false);
62 }
63 }
64
65 processDeletedQueue();
66 }
67
68 void QueryManager::deleteEventQuery(EventQuery* query)
69 {
70 instance().mDeletedEventQueries.push_back(query);
71 }
72
73 void QueryManager::deleteTimerQuery(TimerQuery* query)
74 {
75 instance().mDeletedTimerQueries.push_back(query);
76 }
77
78 void QueryManager::deleteOcclusionQuery(OcclusionQuery* query)
79 {
80 instance().mDeletedOcclusionQueries.push_back(query);
81 }
82
83 void QueryManager::processDeletedQueue()
84 {
85 for (auto& query : mDeletedEventQueries)
86 {
87 auto iterFind = std::find(mEventQueries.begin(), mEventQueries.end(), query);
88
89 if (iterFind != mEventQueries.end())
90 mEventQueries.erase(iterFind);
91
92 bs_delete(query);
93 }
94
95 mDeletedEventQueries.clear();
96
97 for (auto& query : mDeletedTimerQueries)
98 {
99 auto iterFind = std::find(mTimerQueries.begin(), mTimerQueries.end(), query);
100
101 if (iterFind != mTimerQueries.end())
102 mTimerQueries.erase(iterFind);
103
104 bs_delete(query);
105 }
106
107 mDeletedTimerQueries.clear();
108
109 for (auto& query : mDeletedOcclusionQueries)
110 {
111 auto iterFind = std::find(mOcclusionQueries.begin(), mOcclusionQueries.end(), query);
112
113 if (iterFind != mOcclusionQueries.end())
114 mOcclusionQueries.erase(iterFind);
115
116 bs_delete(query);
117 }
118
119 mDeletedOcclusionQueries.clear();
120 }
121}}