ソースを参照

Implement bgfx::AllocatorI and bgfx::CallbackI

Daniele Bartolini 10 年 前
コミット
f3cd5f83fa
3 ファイル変更102 行追加1 行削除
  1. 8 0
      src/core/log.h
  2. 11 1
      src/device.cpp
  3. 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)

+ 11 - 1
src/device.cpp

@@ -28,6 +28,9 @@
 #include "console_server.h"
 #include "input_device.h"
 
+#include "profiler.h"
+#include <stdio.h>
+
 #if CROWN_PLATFORM_ANDROID
 	#include "apk_filesystem.h"
 #endif // CROWN_PLATFORM_ANDROID
@@ -63,6 +66,7 @@ Device::Device(DeviceOptions& opts)
 	, _resource_manager(NULL)
 	, _input_manager(NULL)
 	, _worlds(default_allocator())
+	, _bgfx_allocator(default_allocator())
 {
 }
 
@@ -82,7 +86,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);

+ 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