1// Copyright 2016 The SwiftShader Authors. All Rights Reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//    http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#include "Config.hpp"
16
17#include "Common/Thread.hpp"
18#include "Common/Timer.hpp"
19
20namespace sw
21{
22	Profiler profiler;
23
24	Profiler::Profiler()
25	{
26		reset();
27	}
28
29	void Profiler::reset()
30	{
31		framesSec = 0;
32		framesTotal = 0;
33		FPS = 0;
34
35		#if PERF_PROFILE
36			for(int i = 0; i < PERF_TIMERS; i++)
37			{
38				cycles[i] = 0;
39			}
40
41			ropOperations = 0;
42			ropOperationsTotal = 0;
43			ropOperationsFrame = 0;
44
45			texOperations = 0;
46			texOperationsTotal = 0;
47			texOperationsFrame = 0;
48
49			compressedTex = 0;
50			compressedTexTotal = 0;
51			compressedTexFrame = 0;
52		#endif
53	};
54
55	void Profiler::nextFrame()
56	{
57		#if PERF_PROFILE
58			ropOperationsFrame = sw::atomicExchange(&ropOperations, 0);
59			texOperationsFrame = sw::atomicExchange(&texOperations, 0);
60			compressedTexFrame = sw::atomicExchange(&compressedTex, 0);
61
62			ropOperationsTotal += ropOperationsFrame;
63			texOperationsTotal += texOperationsFrame;
64			compressedTexTotal += compressedTexFrame;
65		#endif
66
67		static double fpsTime = sw::Timer::seconds();
68
69		double time = sw::Timer::seconds();
70		double delta = time - fpsTime;
71		framesSec++;
72
73		if(delta > 1.0)
74		{
75			FPS = framesSec / delta;
76
77			fpsTime = time;
78			framesTotal += framesSec;
79			framesSec = 0;
80		}
81	}
82}