summaryrefslogtreecommitdiffstats
path: root/Graphics/GraphicsEngine/include/QueryBase.hpp
blob: d208736d132653b314a7fec048bfe23371990f3f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/*
 *  Copyright 2019-2021 Diligent Graphics LLC
 *  Copyright 2015-2019 Egor Yusov
 *  
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *  
 *      http://www.apache.org/licenses/LICENSE-2.0
 *  
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 *  In no event and under no legal theory, whether in tort (including negligence), 
 *  contract, or otherwise, unless required by applicable law (such as deliberate 
 *  and grossly negligent acts) or agreed to in writing, shall any Contributor be
 *  liable for any damages, including any direct, indirect, special, incidental, 
 *  or consequential damages of any character arising as a result of this License or 
 *  out of the use or inability to use the software (including but not limited to damages 
 *  for loss of goodwill, work stoppage, computer failure or malfunction, or any and 
 *  all other commercial damages or losses), even if such Contributor has been advised 
 *  of the possibility of such damages.
 */

#pragma once

/// \file
/// Implementation of Diligent::QueryBase template class

#include "Query.h"
#include "DeviceObjectBase.hpp"
#include "GraphicsTypes.h"
#include "RefCntAutoPtr.hpp"

namespace Diligent
{

/// Template class implementing base functionality of the query object

/// \tparam EngineImplTraits - Engine implementation type traits.
template <typename EngineImplTraits>
class QueryBase : public DeviceObjectBase<typename EngineImplTraits::QueryInterface, typename EngineImplTraits::RenderDeviceImplType, QueryDesc>
{
public:
    // Base interface this class inherits (IQueryD3D12, IQueryVk, etc.)
    using BaseInterface = typename EngineImplTraits::QueryInterface;

    // Render device implementation type (RenderDeviceD3D12Impl, RenderDeviceVkImpl, etc.).
    using RenderDeviceImplType = typename EngineImplTraits::RenderDeviceImplType;

    enum class QueryState
    {
        Inactive,
        Querying,
        Ended
    };

    using TDeviceObjectBase = DeviceObjectBase<BaseInterface, RenderDeviceImplType, QueryDesc>;

    /// \param pRefCounters      - Reference counters object that controls the lifetime of this command list.
    /// \param pDevice           - Pointer to the device.
    /// \param Desc              - Query description
    /// \param bIsDeviceInternal - Flag indicating if the Query is an internal device object and
    ///							   must not keep a strong reference to the device.
    QueryBase(IReferenceCounters*   pRefCounters,
              RenderDeviceImplType* pDevice,
              const QueryDesc&      Desc,
              bool                  bIsDeviceInternal = false) :
        TDeviceObjectBase{pRefCounters, pDevice, Desc, bIsDeviceInternal}
    {
        const auto& deviceFeatures = pDevice->GetDeviceCaps().Features;
        static_assert(QUERY_TYPE_NUM_TYPES == 6, "Not all QUERY_TYPE enum values are handled below");
        switch (Desc.Type)
        {
            case QUERY_TYPE_OCCLUSION:
                if (!deviceFeatures.OcclusionQueries)
                    LOG_ERROR_AND_THROW("Occlusion queries are not supported by this device");
                break;

            case QUERY_TYPE_BINARY_OCCLUSION:
                if (!deviceFeatures.BinaryOcclusionQueries)
                    LOG_ERROR_AND_THROW("Binary occlusion queries are not supported by this device");
                break;

            case QUERY_TYPE_TIMESTAMP:
                if (!deviceFeatures.TimestampQueries)
                    LOG_ERROR_AND_THROW("Timestamp queries are not supported by this device");
                break;

            case QUERY_TYPE_PIPELINE_STATISTICS:
                if (!deviceFeatures.PipelineStatisticsQueries)
                    LOG_ERROR_AND_THROW("Pipeline statistics queries are not supported by this device");
                break;

            case QUERY_TYPE_DURATION:
                if (!deviceFeatures.DurationQueries)
                    LOG_ERROR_AND_THROW("Duration queries are not supported by this device");
                break;

            default:
                UNEXPECTED("Unexpected device type");
        }
    }

    ~QueryBase()
    {
        if (m_State == QueryState::Querying)
        {
            LOG_ERROR_MESSAGE("Destroying query '", this->m_Desc.Name,
                              "' that is in querying state. End the query before releasing it.");
        }
    }

    IMPLEMENT_QUERY_INTERFACE_IN_PLACE(IID_Query, TDeviceObjectBase)

    virtual void DILIGENT_CALL_TYPE Invalidate() override
    {
        m_State = QueryState::Inactive;
    }

    bool OnBeginQuery(struct IDeviceContext* pContext)
    {
        if (this->m_Desc.Type == QUERY_TYPE_TIMESTAMP)
        {
            LOG_ERROR_MESSAGE("BeginQuery cannot be called on timestamp query '", this->m_Desc.Name,
                              "'. Call EndQuery to set the timestamp.");
            return false;
        }

        if (m_State == QueryState::Querying)
        {
            LOG_ERROR_MESSAGE("Attempting to begin query '", this->m_Desc.Name,
                              "' twice. A query must be ended before it can be begun again.");
            return false;
        }

        m_pContext = pContext;
        m_State    = QueryState::Querying;
        return true;
    }

    bool OnEndQuery(IDeviceContext* pContext)
    {
        if (this->m_Desc.Type != QUERY_TYPE_TIMESTAMP)
        {
            if (m_State != QueryState::Querying)
            {
                LOG_ERROR_MESSAGE("Attempting to end query '", this->m_Desc.Name, "' that has not been begun.");
                return false;
            }
        }

        if (m_pContext == nullptr)
        {
            if (this->m_Desc.Type != QUERY_TYPE_TIMESTAMP)
            {
                LOG_ERROR_MESSAGE("Ending query '", this->m_Desc.Name, "' that has not been begun.");
                return false;
            }

            m_pContext = pContext;
        }
        else if (m_pContext != pContext)
        {
            LOG_ERROR_MESSAGE("Query '", this->m_Desc.Name, "' has been begun by another context.");
            return false;
        }

        m_State = QueryState::Ended;
        return true;
    }

    QueryState GetState() const
    {
        return m_State;
    }

    bool CheckQueryDataPtr(void* pData, Uint32 DataSize)
    {
        if (m_State != QueryState::Ended)
        {
            LOG_ERROR_MESSAGE("Attempting to get data of query '", this->m_Desc.Name, "' that has not been ended.");
            return false;
        }

        if (pData != nullptr)
        {
            if (*reinterpret_cast<QUERY_TYPE*>(pData) != this->m_Desc.Type)
            {
                LOG_ERROR_MESSAGE("Incorrect query data structure type.");
                return false;
            }

            static_assert(QUERY_TYPE_NUM_TYPES == 6, "Not all QUERY_TYPE enum values are handled below.");
            switch (this->m_Desc.Type)
            {
                case QUERY_TYPE_UNDEFINED:
                    UNEXPECTED("Undefined query type is unexpected.");
                    return false;

                case QUERY_TYPE_OCCLUSION:
                    if (DataSize != sizeof(QueryDataOcclusion))
                    {
                        LOG_ERROR_MESSAGE("The size of query data (", DataSize, ") is incorrect: ", sizeof(QueryDataOcclusion), " (aka sizeof(QueryDataOcclusion)) is expected.");
                        return false;
                    }
                    break;

                case QUERY_TYPE_BINARY_OCCLUSION:
                    if (DataSize != sizeof(QueryDataBinaryOcclusion))
                    {
                        LOG_ERROR_MESSAGE("The size of query data (", DataSize, ") is incorrect: ", sizeof(QueryDataBinaryOcclusion), " (aka sizeof(QueryDataBinaryOcclusion)) is expected.");
                        return false;
                    }
                    break;

                case QUERY_TYPE_TIMESTAMP:
                    if (DataSize != sizeof(QueryDataTimestamp))
                    {
                        LOG_ERROR_MESSAGE("The size of query data (", DataSize, ") is incorrect: ", sizeof(QueryDataTimestamp), " (aka sizeof(QueryDataTimestamp)) is expected.");
                        return false;
                    }
                    break;

                case QUERY_TYPE_PIPELINE_STATISTICS:
                    if (DataSize != sizeof(QueryDataPipelineStatistics))
                    {
                        LOG_ERROR_MESSAGE("The size of query data (", DataSize, ") is incorrect: ", sizeof(QueryDataPipelineStatistics), " (aka sizeof(QueryDataPipelineStatistics)) is expected.");
                        return false;
                    }
                    break;

                case QUERY_TYPE_DURATION:
                    if (DataSize != sizeof(QueryDataDuration))
                    {
                        LOG_ERROR_MESSAGE("The size of query data (", DataSize, ") is incorrect: ", sizeof(QueryDataDuration), " (aka sizeof(QueryDataDuration)) is expected.");
                        return false;
                    }
                    break;

                default:
                    UNEXPECTED("Unexpected query type.");
                    return false;
            }
        }

        return true;
    }

protected:
    RefCntAutoPtr<IDeviceContext> m_pContext;

    QueryState m_State = QueryState::Inactive;
};

} // namespace Diligent