RavEngine
Loading...
Searching...
No Matches
Timer.h
1// Redistribution and use in source and binary forms, with or without
2// modification, are permitted provided that the following conditions
3// are met:
4// * Redistributions of source code must retain the above copyright
5// notice, this list of conditions and the following disclaimer.
6// * Redistributions in binary form must reproduce the above copyright
7// notice, this list of conditions and the following disclaimer in the
8// documentation and/or other materials provided with the distribution.
9// * Neither the name of NVIDIA CORPORATION nor the names of its
10// contributors may be used to endorse or promote products derived
11// from this software without specific prior written permission.
12//
13// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
14// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
17// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21// OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24//
25// Copyright (c) 2014-2022 NVIDIA Corporation. All rights reserved.
26
27#pragma once
28
29#include "NvFlowTypes.h"
30
31#if defined(_WIN32)
32#include <Windows.h>
33#else
34#include <time.h>
35#endif
36
38{
39 NvFlowUint64 freq;
40 NvFlowUint64 begin;
41 NvFlowUint64 end;
42 NvFlowUint state;
43 float statTimeAccum;
44 float statTimeCount;
45};
46
47NV_FLOW_INLINE void appTimerInit(AppTimer* ptr)
48{
49 ptr->freq = 1ull;
50 ptr->begin = 0ull;
51 ptr->end = 0ull;
52 ptr->state = 0u;
53 ptr->statTimeAccum = 0.f;
54 ptr->statTimeCount = 0.f;
55}
56
57NV_FLOW_INLINE void appTimerDestroy(AppTimer* ptr)
58{
59 // NOP
60}
61
62NV_FLOW_INLINE void appTimerBegin(AppTimer* ptr)
63{
64 if (ptr->state == 0u)
65 {
66#if defined(_WIN32)
67 LARGE_INTEGER tmpCpuFreq = {};
68 QueryPerformanceFrequency(&tmpCpuFreq);
69 ptr->freq = tmpCpuFreq.QuadPart;
70
71 LARGE_INTEGER tmpCpuTime = {};
72 QueryPerformanceCounter(&tmpCpuTime);
73 ptr->begin = tmpCpuTime.QuadPart;
74#else
75 ptr->freq = 1E9;
76
77 timespec timeValue = {};
78 clock_gettime(CLOCK_MONOTONIC, &timeValue);
79 ptr->begin = 1E9 * NvFlowUint64(timeValue.tv_sec) + NvFlowUint64(timeValue.tv_nsec);
80#endif
81
82 ptr->state = 1u;
83 }
84}
85
86NV_FLOW_INLINE void appTimerEnd(AppTimer* ptr)
87{
88 if (ptr->state == 1u)
89 {
90#if defined(_WIN32)
91 LARGE_INTEGER tmpCpuTime = {};
92 QueryPerformanceCounter(&tmpCpuTime);
93 ptr->end = tmpCpuTime.QuadPart;
94#else
95 timespec timeValue = {};
96 clock_gettime(CLOCK_MONOTONIC, &timeValue);
97 ptr->end = 1E9 * NvFlowUint64(timeValue.tv_sec) + NvFlowUint64(timeValue.tv_nsec);
98#endif
99
100 ptr->state = 0u;
101 }
102}
103
104NV_FLOW_INLINE NvFlowBool32 appTimerGetResults(AppTimer* ptr, float* deltaTime)
105{
106 if (ptr->state == 0u)
107 {
108 *deltaTime = (float)(((double)(ptr->end - ptr->begin) / (double)(ptr->freq)));
109 return NV_FLOW_TRUE;
110 }
111 return NV_FLOW_FALSE;
112}
113
114NV_FLOW_INLINE NvFlowBool32 appTimerUpdateStats(AppTimer* ptr, float deltaTime, float sampleCount, float* pAverageTime)
115{
116 ptr->statTimeAccum += deltaTime;
117 ptr->statTimeCount += 1.f;
118 if (ptr->statTimeCount > sampleCount)
119 {
120 *pAverageTime = ptr->statTimeAccum / ptr->statTimeCount;
121 ptr->statTimeAccum = 0.f;
122 ptr->statTimeCount = 0.f;
123 return NV_FLOW_TRUE;
124 }
125 return NV_FLOW_FALSE;
126}
Definition Timer.h:38