Browse Source

Performance counters (for making optimizations) & file I/O

Panagiotis Christopoulos Charitos 12 years ago
parent
commit
a76f96b183

+ 39 - 0
include/anki/core/Counters.h

@@ -0,0 +1,39 @@
+#include "anki/util/StdTypes.h"
+#include "anki/util/Singleton.h"
+#include "anki/util/Filesystem.h"
+
+namespace anki {
+
+/// Enumeration of counters
+enum Counter
+{
+	C_RENDERER_MS_TIME,
+	C_RENDERER_IS_TIME,
+	C_RENDERER_PPS_TIME,
+	C_RENDERER_DRAWCALLS_COUNT,
+	C_SCENE_UPDATE_TIME,
+	C_SWAP_BUFFERS_TIME,
+
+	C_COUNT
+};
+
+/// XXX
+class CountersManager
+{
+public:
+	void increaseCounter(Counter counter, F64 val);
+	void increaseCounter(Counter counter, U64 val);
+
+	/// Write the counters of the frame
+	void resolveFrame();
+
+private:
+	File perframeFile;
+};
+
+/// The singleton of the counters manager
+typedef Singleton<CountersManager> CountersManagerSingleton;
+
+#define ANKI_COUNTER_INC(counter, val)
+
+} // end namespace anki

+ 0 - 19
include/anki/core/Instrumentation.h

@@ -1,19 +0,0 @@
-#include "anki/util/StdTypes.h"
-
-namespace anki {
-
-enum InstrumentationCounter
-{
-	IC_RENDERER_MS_TIME,
-	IC_RENDERER_IS_TIME,
-	IC_RENDERER_PPS_TIME,
-	IC_RENDERER_DRAWCALLS_COUNT,
-	IC_SCENE_UPDATE_TIME,
-	IC_SWAP_BUFFERS_TIME,
-
-	IC_COUNT
-};
-
-#define ANKI_INSTR_UPDATE_COUNTER(counter, val)
-
-} // end namespace anki

+ 29 - 14
include/anki/util/Filesystem.h

@@ -11,33 +11,48 @@ namespace anki {
 /// @addtogroup filesystem
 /// @addtogroup filesystem
 /// @{
 /// @{
 
 
-/// XXX
+/// An abstraction over typical files and files in ziped archives.
 class File
 class File
 {
 {
+private:
+	/// Internal filetype
+	enum FileType
+	{
+		FT_NONE,
+		FT_C, ///< C file
+		FT_ZIP ///< Ziped file
+	};
+
 public:
 public:
-	enum
+	/// Open mode
+	enum OpenFlag
 	{
 	{
-		READ,
-		WRITE,
-		APPEND,
-		BINARY
+		OF_READ = 1 << 0,
+		OF_WRITE = 1 << 1,
+		OF_APPEND = 1 << 2,
+		OF_BINARY = 1 << 3
 	};
 	};
 
 
-	void open(const char* filename, U8 openFlags);
+	/// Default constructor
+	File()
+		: openFlags(0), file(nullptr), fileType(FT_NONE)
+	{}
+
+	/// Closes the file if it's open
+	~File();
 
 
+	/// Open a file
+	void open(const char* filename, U8 openMask);
+
+	/// Read data from the file
 	PtrSize read(void* buff, PtrSize size);
 	PtrSize read(void* buff, PtrSize size);
 
 
+	/// Write data to the file
 	PtrSize write(void* buff, PtrSize size);
 	PtrSize write(void* buff, PtrSize size);
 
 
 public:
 public:
-	enum
-	{
-		C_FILE,
-		ZIP_FILE
-	};
-
 	U8 openFlags; ///< Mainly for assertions
 	U8 openFlags; ///< Mainly for assertions
-	void* file = nullptr; ///< A native type
+	void* file; ///< A native type
 	U8 fileType;
 	U8 fileType;
 };
 };
 
 

+ 30 - 0
src/core/Counters.cpp

@@ -0,0 +1,30 @@
+#include "anki/core/Counters.h"
+
+namespace anki {
+
+//==============================================================================
+
+enum CounterFlag
+{
+	CF_PER_FRAME = 1 << 0,
+	CF_GLOBAL = 1 << 1,
+	CF_F64 = 1 << 2,
+	CF_U64 = 1 << 3
+};
+
+struct CounterInfo
+{
+	const char* name;
+	U32 flags;
+};
+
+static const CounterInfo counters[C_COUNT - 1] = {
+	{"C_RENDERER_MS_TIME", CF_PER_FRAME | CF_F64},
+	{"C_RENDERER_IS_TIME", CF_PER_FRAME | CF_F64},
+	{"C_RENDERER_PPS_TIME", CF_PER_FRAME | CF_F64},
+	{"C_RENDERER_DRAWCALLS_COUNT", CF_GLOBAL | CF_U64},
+	{"C_SCENE_UPDATE_TIME", CF_PER_FRAME | CF_F64},
+	{"C_SWAP_BUFFERS_TIME", CF_PER_FRAME | CF_F64}
+};
+
+} // end namespace anki

+ 54 - 6
src/util/Filesystem.cpp

@@ -10,20 +10,39 @@ namespace anki {
 // File                                                                        =
 // File                                                                        =
 //==============================================================================
 //==============================================================================
 
 
+//==============================================================================
+File::~File()
+{
+	if(file)
+	{
+		switch(fileType)
+		{
+		case FT_C:
+			fclose((FILE*)file);
+			break;
+		case FT_ZIP:
+			ANKI_ASSERT(0 && "Not implemented");
+			break;
+		default:
+			ANKI_ASSERT(0);
+		}
+	}
+}
+
 //==============================================================================
 //==============================================================================
 void File::open(const char* filename, U8 flags)
 void File::open(const char* filename, U8 flags)
 {
 {
 	const char* openMode;
 	const char* openMode;
 
 
-	if(flags & READ)
+	if(flags & OF_READ)
 	{
 	{
 		openMode = "r";
 		openMode = "r";
 	}
 	}
-	else if(flags & WRITE)
+	else if(flags & OF_WRITE)
 	{
 	{
 		openMode = "w";
 		openMode = "w";
 	}
 	}
-	else if(flags & APPEND)
+	else if(flags & OF_APPEND)
 	{
 	{
 		openMode = "a+";
 		openMode = "a+";
 	}
 	}
@@ -40,7 +59,7 @@ void File::open(const char* filename, U8 flags)
 	{
 	{
 		throw ANKI_EXCEPTION("Failed to open file");
 		throw ANKI_EXCEPTION("Failed to open file");
 	}
 	}
-	fileType = C_FILE;
+	fileType = FT_C;
 }
 }
 
 
 //==============================================================================
 //==============================================================================
@@ -48,20 +67,49 @@ PtrSize File::read(void* buff, PtrSize size)
 {
 {
 	ANKI_ASSERT(file);
 	ANKI_ASSERT(file);
 	ANKI_ASSERT(buff);
 	ANKI_ASSERT(buff);
+	ANKI_ASSERT(size > 0);
+
 	PtrSize readSize = 0;
 	PtrSize readSize = 0;
 
 
 	switch(fileType)
 	switch(fileType)
 	{
 	{
-	case C_FILE:
+	case FT_C:
 		readSize = fread(buff, 1, size, (FILE*)file);
 		readSize = fread(buff, 1, size, (FILE*)file);
 		break;
 		break;
-	case ZIP_FILE:
+	case FT_ZIP:
+		ANKI_ASSERT(0 && "Not implemented");
 		break;
 		break;
+	default:
+		ANKI_ASSERT(0);
 	}
 	}
 
 
 	return readSize;
 	return readSize;
 }
 }
 
 
+//==============================================================================
+PtrSize File::write(void* buff, PtrSize size)
+{
+	ANKI_ASSERT(file);
+	ANKI_ASSERT(buff);
+	ANKI_ASSERT(size > 0);
+
+	PtrSize writeSize = 0;
+
+	switch(fileType)
+	{
+	case FT_C:
+		writeSize = fwrite(buff, 1, size, (FILE*)file);
+		break;
+	case FT_ZIP:
+		ANKI_ASSERT(0 && "Not implemented");
+		break;
+	default:
+		ANKI_ASSERT(0);
+	}
+
+	return writeSize;
+}
+
 //==============================================================================
 //==============================================================================
 // Functions                                                                   =
 // Functions                                                                   =
 //==============================================================================
 //==============================================================================