| 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 | #pragma once |
| 4 | |
| 5 | #include "BsCorePrerequisites.h" |
| 6 | |
| 7 | namespace bs { namespace ct |
| 8 | { |
| 9 | /** @addtogroup RenderAPI-Internal |
| 10 | * @{ |
| 11 | */ |
| 12 | |
| 13 | /** |
| 14 | * Represents a GPU query that measures execution time of GPU operations. The query will measure any GPU operations |
| 15 | * that take place between its begin() and end() calls. |
| 16 | * |
| 17 | * @note Core thread only. |
| 18 | */ |
| 19 | class BS_CORE_EXPORT TimerQuery |
| 20 | { |
| 21 | public: |
| 22 | virtual ~TimerQuery() = default; |
| 23 | |
| 24 | /** |
| 25 | * Starts the counter. |
| 26 | * |
| 27 | * @param[in] cb Optional command buffer to queue the operation on. If not provided operation |
| 28 | * is executed on the main command buffer. Otherwise it is executed when |
| 29 | * RenderAPI::executeCommands() is called. Buffer must support graphics or compute operations. |
| 30 | * |
| 31 | * @note Place any commands you want to measure after this call. Call "end" when done. |
| 32 | */ |
| 33 | virtual void begin(const SPtr<CommandBuffer>& cb = nullptr) = 0; |
| 34 | |
| 35 | /** |
| 36 | * Stops the counter. |
| 37 | * |
| 38 | * @param[in] cb Command buffer that was provided to the last begin() operation (if any). |
| 39 | */ |
| 40 | virtual void end(const SPtr<CommandBuffer>& cb = nullptr) = 0; |
| 41 | |
| 42 | /** Check if GPU has processed the query. */ |
| 43 | virtual bool isReady() const = 0; |
| 44 | |
| 45 | /** |
| 46 | * Returns the time it took for the query to execute. |
| 47 | * |
| 48 | * @return The time milliseconds. |
| 49 | * |
| 50 | * @note Only valid after isReady() returns true. |
| 51 | */ |
| 52 | virtual float getTimeMs() = 0; |
| 53 | |
| 54 | /** Triggered when GPU processes the query. As a parameter it provides query duration in milliseconds. */ |
| 55 | Event<void(float)> onTriggered; |
| 56 | |
| 57 | /** |
| 58 | * Creates a new query, but does not schedule it on GPU. |
| 59 | * |
| 60 | * @param[in] deviceIdx Index of the GPU device to create the query on. |
| 61 | */ |
| 62 | static SPtr<TimerQuery> create(UINT32 deviceIdx = 0); |
| 63 | |
| 64 | protected: |
| 65 | friend class QueryManager; |
| 66 | |
| 67 | /** Returns true if the has still not been completed by the GPU. */ |
| 68 | bool isActive() const { return mActive; } |
| 69 | void setActive(bool active) { mActive = active; } |
| 70 | |
| 71 | protected: |
| 72 | bool mActive; |
| 73 | }; |
| 74 | |
| 75 | /** @} */ |
| 76 | }} |