Quellcode durchsuchen

Merge branch 'master' of github.com:taylor001/crown

Daniele Bartolini vor 10 Jahren
Ursprung
Commit
44137e70b9
4 geänderte Dateien mit 108 neuen und 2 gelöschten Zeilen
  1. 8 0
      src/core/log.h
  2. 1 1
      src/core/profiler.h
  3. 16 1
      src/device.cpp
  4. 83 0
      src/device.h

+ 8 - 0
src/core/log.h

@@ -31,11 +31,19 @@ namespace log_internal
 } // namespace crown
 
 #if CROWN_DEBUG
+	#define CE_LOGIV(msg, va_list) crown::log_internal::logx(crown::LogSeverity::INFO, msg, va_list)
+	#define CE_LOGDV(msg, va_list) crown::log_internal::logx(crown::LogSeverity::DEBUG, msg, va_list)
+	#define CE_LOGEV(msg, va_list) crown::log_internal::logx(crown::LogSeverity::ERROR, msg, va_list)
+	#define CE_LOGWV(msg, va_list) crown::log_internal::logx(crown::LogSeverity::WARN, msg, va_list)
 	#define CE_LOGI(msg, ...) crown::log_internal::logx(crown::LogSeverity::INFO, msg, ##__VA_ARGS__)
 	#define CE_LOGD(msg, ...) crown::log_internal::logx(crown::LogSeverity::DEBUG, msg, ##__VA_ARGS__)
 	#define CE_LOGE(msg, ...) crown::log_internal::logx(crown::LogSeverity::ERROR, msg, ##__VA_ARGS__)
 	#define CE_LOGW(msg, ...) crown::log_internal::logx(crown::LogSeverity::WARN, msg, ##__VA_ARGS__)
 #else
+	#define CE_LOGIV(msg, va_list) ((void)0)
+	#define CE_LOGDV(msg, va_list) ((void)0)
+	#define CE_LOGEV(msg, va_list) ((void)0)
+	#define CE_LOGWV(msg, va_list) ((void)0)
 	#define CE_LOGI(msg, ...) ((void)0)
 	#define CE_LOGD(msg, ...) ((void)0)
 	#define CE_LOGE(msg, ...) ((void)0)

+ 1 - 1
src/core/profiler.h

@@ -82,7 +82,7 @@ namespace profiler_globals
 
 #if CROWN_DEBUG
 	#define ENTER_PROFILE_SCOPE(name) profiler::enter_profile_scope(name)
-	#define LEAVE_PROFILE_SCOPE() profiler::leave_profile_scope(name)
+	#define LEAVE_PROFILE_SCOPE() profiler::leave_profile_scope()
 	#define RECORD_FLOAT(name, value) profiler::record_float(name, value)
 	#define RECORD_VECTOR3(name, value) profiler::record_vector3(name, value)
 	#define ALLOCATE_MEMORY(name, size) profiler::allocate_memory(name, size)

+ 16 - 1
src/device.cpp

@@ -27,6 +27,7 @@
 #include "profiler.h"
 #include "console_server.h"
 #include "input_device.h"
+#include "profiler.h"
 
 #if CROWN_PLATFORM_ANDROID
 	#include "apk_filesystem.h"
@@ -63,6 +64,7 @@ Device::Device(DeviceOptions& opts)
 	, _resource_manager(NULL)
 	, _input_manager(NULL)
 	, _worlds(default_allocator())
+	, _bgfx_allocator(default_allocator())
 {
 }
 
@@ -82,7 +84,13 @@ void Device::init()
 	profiler_globals::init();
 	audio_globals::init();
 	physics_globals::init();
-	bgfx::init();
+
+	bgfx::init(bgfx::RendererType::Count
+		, BGFX_PCI_ID_NONE
+		, 0
+		, &_bgfx_callback
+		, &_bgfx_allocator
+		);
 
 	_resource_loader = CE_NEW(_allocator, ResourceLoader)(*_bundle_filesystem);
 	_resource_manager = CE_NEW(_allocator, ResourceManager)(*_resource_loader);
@@ -206,6 +214,9 @@ void Device::update()
 		profiler_globals::clear();
 		console_server_globals::update();
 
+		RECORD_FLOAT("device.dt", _last_delta_time);
+		RECORD_FLOAT("device.fps", 1.0f/_last_delta_time);
+
 		if (!_is_paused)
 		{
 			_resource_manager->complete_requests();
@@ -215,6 +226,10 @@ void Device::update()
 
 		_input_manager->update();
 
+		const bgfx::Stats* stats = bgfx::getStats();
+		RECORD_FLOAT("bgfx.gpu_time", double(stats->gpuTimeEnd - stats->gpuTimeBegin)*1000.0/stats->gpuTimerFreq);
+		RECORD_FLOAT("bgfx.cpu_time", double(stats->cpuTimeEnd - stats->cpuTimeBegin)*1000.0/stats->cpuTimerFreq);
+
 		bgfx::frame();
 		profiler_globals::flush();
 

+ 83 - 0
src/device.h

@@ -17,6 +17,11 @@
 #include "device_options.h"
 #include "os_event_queue.h"
 #include "string_id.h"
+#include "allocator.h"
+#include "log.h"
+#include "proxy_allocator.h"
+#include <bx/allocator.h>
+#include <bgfx/bgfx.h>
 
 namespace crown
 {
@@ -145,6 +150,84 @@ private:
 
 	Array<World*> _worlds;
 
+	struct BgfxCallback : public bgfx::CallbackI
+	{
+		virtual void fatal(bgfx::Fatal::Enum _code, const char* _str)
+		{
+			CE_ASSERT(false, "Fatal error: 0x%08x: %s", _code, _str);
+		}
+
+		virtual void traceVargs(const char* /*_filePath*/, uint16_t /*_line*/, const char* _format, va_list _argList)
+		{
+			char buf[2048];
+			strncpy(buf, _format, sizeof(buf));
+			buf[strlen(buf)-1] = '\0'; // Remove trailing newline
+			CE_LOGDV(buf, _argList);
+		}
+
+		virtual uint32_t cacheReadSize(uint64_t /*_id*/)
+		{
+			return 0;
+		}
+
+		virtual bool cacheRead(uint64_t /*_id*/, void* /*_data*/, uint32_t /*_size*/)
+		{
+			return false;
+		}
+
+		virtual void cacheWrite(uint64_t /*_id*/, const void* /*_data*/, uint32_t /*_size*/)
+		{
+		}
+
+		virtual void screenShot(const char* /*_filePath*/, uint32_t /*_width*/, uint32_t /*_height*/, uint32_t /*_pitch*/, const void* /*_data*/, uint32_t /*_size*/, bool /*_yflip*/)
+		{
+		}
+
+		virtual void captureBegin(uint32_t /*_width*/, uint32_t /*_height*/, uint32_t /*_pitch*/, bgfx::TextureFormat::Enum /*_format*/, bool /*_yflip*/)
+		{
+		}
+
+		virtual void captureEnd()
+		{
+		}
+
+		virtual void captureFrame(const void* /*_data*/, uint32_t /*_size*/)
+		{
+		}
+	};
+
+	struct BgfxAllocator : public bx::AllocatorI
+	{
+		BgfxAllocator(Allocator& a)
+			: _allocator("bgfx", a)
+		{
+		}
+
+		virtual void* realloc(void* _ptr, size_t _size, size_t _align, const char* /*_file*/, uint32_t /*_line*/)
+		{
+			if (!_ptr)
+				return _allocator.allocate((uint32_t)_size, (uint32_t)_align == 0 ? 1 : _align);
+
+			if (_size == 0)
+			{
+				_allocator.deallocate(_ptr);
+				return NULL;
+			}
+
+			// Realloc
+			void* p = _allocator.allocate((uint32_t)_size, (uint32_t)_align == 0 ? 1 : _align);
+			_allocator.deallocate(_ptr);
+			return p;
+		}
+
+	private:
+
+		ProxyAllocator _allocator;
+	};
+
+	BgfxCallback _bgfx_callback;
+	BgfxAllocator _bgfx_allocator;
+
 private:
 
 	// Disable copying