From 9277898aedb9c2b0c54cfaaaee768def3bc1333c Mon Sep 17 00:00:00 2001
From: D-AIRY <admin@ds-servers.com>
Date: Mon, 7 Dec 2020 23:40:48 +0300
Subject: [PATCH] Move sound update to worker thread

---
 source/core/Core.cpp           |  5 +++
 source/core/Core.h             |  1 +
 source/game/GameData.cpp       |  1 -
 source/xEngine/Engine.cpp      |  2 --
 source/xSound/SoundLayer.cpp   | 12 +++++---
 source/xSound/SoundSystem.cpp  |  2 +-
 source/xSound/SoundSystem.h    |  2 +-
 source/xSound/plugin_main.cpp  | 56 +++++++++++++++++++++++++++++-----
 source/xcommon/IXCore.h        |  3 ++
 source/xcommon/IXSoundSystem.h |  2 --
 10 files changed, 66 insertions(+), 20 deletions(-)

diff --git a/source/core/Core.cpp b/source/core/Core.cpp
index f47153f65..ff4e80198 100644
--- a/source/core/Core.cpp
+++ b/source/core/Core.cpp
@@ -382,6 +382,11 @@ void XMETHODCALLTYPE CCore::waitForLoop(ID id)
 	g_pTaskManager->waitFor(id);
 }
 
+void XMETHODCALLTYPE CCore::addTask(ITask *pTask)
+{
+	g_pTaskManager->addTask(pTask);
+}
+
 IXConsole* XMETHODCALLTYPE CCore::getConsole()
 {
 	return(m_pConsole);
diff --git a/source/core/Core.h b/source/core/Core.h
index aea449140..e4490ca90 100644
--- a/source/core/Core.h
+++ b/source/core/Core.h
@@ -55,6 +55,7 @@ public:
 
 	ID XMETHODCALLTYPE forLoop(int iStart, int iEnd, const IParallelForBody *pBody, int iMaxChunkSize = 0) override;
 	void XMETHODCALLTYPE waitForLoop(ID id) override;
+	void XMETHODCALLTYPE addTask(ITask *pTask) override;
 	
 	IBaseEventChannel* getEventChannelInternal(const XGUID &guid) override;
 protected:
diff --git a/source/game/GameData.cpp b/source/game/GameData.cpp
index 75df4dc37..3d9cf94a9 100644
--- a/source/game/GameData.cpp
+++ b/source/game/GameData.cpp
@@ -369,7 +369,6 @@ GameData::GameData(HWND hWnd, bool isGame):
 		m_pSoundPlayer = m_pGuiLayer->newSoundPlayer("sounds/dip.wav", SOUND_SPACE_2D);
 		m_pSoundPlayer->setLoop(SOUND_LOOP_SIMPLE);
 	}
-	pSound->update(float3(), float3(), float3());
 
 	loadFoostepsSounds();
 	isGame = true;
diff --git a/source/xEngine/Engine.cpp b/source/xEngine/Engine.cpp
index f12f97802..2673e4639 100644
--- a/source/xEngine/Engine.cpp
+++ b/source/xEngine/Engine.cpp
@@ -181,10 +181,8 @@ bool XMETHODCALLTYPE CEngine::initGraphics(XWINDOW_OS_HANDLE hWindow, IXEngineCa
 	pMasterLayer->play(true);
 	IXSoundLayer *pGameLayer = pMasterLayer->newSoundLayer("xGame");
 	IXSoundLayer *pGuiLayer = pMasterLayer->newSoundLayer("xGUI");
-	pSound->update(float3(), float3(), float3());
 	pGameLayer->play(false);
 	pGuiLayer->play(false);
-	pSound->update(float3(), float3(), float3());
 	/*IXSoundPlayer *pPlayer = pMasterLayer->newSoundPlayer("sounds/guitar_10.ogg", SOUND_DTYPE_3D);
 	pPlayer->setWorldPos(float3(-11.084, 0.435, -18.707));
 	pPlayer->setLoop(SOUND_LOOP_SIMPLE);
diff --git a/source/xSound/SoundLayer.cpp b/source/xSound/SoundLayer.cpp
index c21e89c02..d09981389 100644
--- a/source/xSound/SoundLayer.cpp
+++ b/source/xSound/SoundLayer.cpp
@@ -211,11 +211,13 @@ IXSoundLayer* XMETHODCALLTYPE CSoundLayer::newSoundLayer(const char *szName, con
 	CSoundLayer *pLayer = new CSoundLayer();
 	pLayer->init(m_pSoundSystem, this, pDesc, szName);
 
-	SndQueueMsg oMsg;
-	oMsg.type = SND_QUEUE_MSG_TYPE_LAYER_NEW;
-	oMsg.pLayer = pLayer;
-	oMsg.pOwner = this;
-	m_pSoundSystem->addMessage(oMsg);
+	addLayer(pLayer);
+
+//	SndQueueMsg oMsg;
+//	oMsg.type = SND_QUEUE_MSG_TYPE_LAYER_NEW;
+//	oMsg.pLayer = pLayer;
+//	oMsg.pOwner = this;
+//	m_pSoundSystem->addMessage(oMsg);
 
 	return pLayer;
 }
diff --git a/source/xSound/SoundSystem.cpp b/source/xSound/SoundSystem.cpp
index 9ddceb66f..e50ed965b 100644
--- a/source/xSound/SoundSystem.cpp
+++ b/source/xSound/SoundSystem.cpp
@@ -48,7 +48,7 @@ void CSoundSystem::addMessage(SndQueueMsg &oMsg)
 	m_queue.push(oMsg);
 }
 
-void XMETHODCALLTYPE CSoundSystem::update(const float3 &vListenerPos, const float3 &vListenerDir, const float3 &vListenerUp)
+void CSoundSystem::update(const float3 &vListenerPos, const float3 &vListenerDir, const float3 &vListenerUp)
 {
 	if(m_pMasterLayer)
 		m_pMasterLayer->update(vListenerPos, vListenerDir, vListenerUp);
diff --git a/source/xSound/SoundSystem.h b/source/xSound/SoundSystem.h
index 024533a24..7ec85c890 100644
--- a/source/xSound/SoundSystem.h
+++ b/source/xSound/SoundSystem.h
@@ -52,7 +52,7 @@ public:
 	virtual IXSoundLayer* XMETHODCALLTYPE createMasterLayer(const AudioRawDesc *pDesc, const char *szName) override;
 	virtual IXSoundLayer* XMETHODCALLTYPE findLayer(const char *szName) override;
 
-	virtual void XMETHODCALLTYPE update(const float3 &vListenerPos, const float3 &vListenerDir, const float3 &vListenerUp) override;
+	virtual void update(const float3 &vListenerPos, const float3 &vListenerDir, const float3 &vListenerUp);
 
 	//########################################################################
 
diff --git a/source/xSound/plugin_main.cpp b/source/xSound/plugin_main.cpp
index 979f2955f..94f754071 100644
--- a/source/xSound/plugin_main.cpp
+++ b/source/xSound/plugin_main.cpp
@@ -4,6 +4,7 @@
 #include "SoundSystem.h"
 #include <Audio.h>
 #include <gcore/sxgcore.h>
+#include <core/ITask.h>
 
 #if defined(_DEBUG)
 #pragma comment(lib, "mital_d.lib")
@@ -51,6 +52,43 @@ protected:
 
 //##########################################################################
 
+class CUpdateTask: public ITaskImpl<ITask>
+{
+public:
+	CUpdateTask(CSoundSystem *pSoundSystem, CObserverChangedEventListener *pObserverListener):
+		m_pSoundSystem(pSoundSystem),
+		m_pObserverListener(pObserverListener),
+		ITaskImpl(CORE_TASK_FLAG_BACKGROUND_REPEATING)
+	{
+	}
+
+	~CUpdateTask()
+	{
+		mem_release(m_pSoundSystem);
+	}
+
+	void run() override
+	{
+		float3 vPos, vLook, vUp;
+
+		if(m_pObserverListener)
+			m_pObserverListener->getObserverParam(&vPos, &vLook, &vUp);
+
+		if(m_pSoundSystem)
+			m_pSoundSystem->update(vPos, vLook, vUp);
+	}
+
+	uint64_t getRepeatInterval() override
+	{
+		return(20000); // 20ms
+	}
+
+protected:
+	CSoundSystem *m_pSoundSystem = NULL;
+	CObserverChangedEventListener *m_pObserverListener = NULL;
+};
+
+#if 0
 class CUpdatableSoundSystem: public IXUnknownImplementation<IXUpdatable>
 {
 public:
@@ -113,6 +151,7 @@ protected:
 	CObserverChangedEventListener *m_pObserverListener = NULL;
 	IXSoundEmitter *m_pEmitter = NULL;
 };
+#endif
 
 //##########################################################################
 
@@ -125,7 +164,8 @@ public:
 		m_pCore = pCore;
 		m_pSoundSystem = new CSoundSystem(m_pCore);
 		m_pObserverListener = new CObserverChangedEventListener(m_pCore);
-		m_pUpdatableSoundSystem = new CUpdatableSoundSystem(m_pSoundSystem, m_pObserverListener);
+		m_pCore->addTask(new CUpdateTask(m_pSoundSystem, m_pObserverListener));
+		//m_pUpdatableSoundSystem = new CUpdatableSoundSystem(m_pSoundSystem, m_pObserverListener);
 	}
 
 	void XMETHODCALLTYPE shutdown() override
@@ -134,7 +174,7 @@ public:
 
 	UINT XMETHODCALLTYPE getInterfaceCount() override
 	{
-		return(2);
+		return(1);
 	}
 	const XGUID* XMETHODCALLTYPE getInterfaceGUID(UINT id) override
 	{
@@ -144,9 +184,9 @@ public:
 		case 0:
 			s_guid = IXSOUNDSYSTEM_GUID;
 			break;
-		case 1:
-			s_guid = IXUPDATABLE_GUID;
-			break;
+		//case 1:
+		//	s_guid = IXUPDATABLE_GUID;
+		//	break;
 		default:
 			return(NULL);
 		}
@@ -156,8 +196,8 @@ public:
 	{
 		if(guid == IXSOUNDSYSTEM_GUID)
 			return(m_pSoundSystem);
-		if(guid == IXUPDATABLE_GUID)
-			return(m_pUpdatableSoundSystem);
+		//if(guid == IXUPDATABLE_GUID)
+		//	return(m_pUpdatableSoundSystem);
 
 		return(NULL);
 	}
@@ -165,7 +205,7 @@ public:
 protected:
 	IXCore *m_pCore;
 	CSoundSystem *m_pSoundSystem = NULL;
-	CUpdatableSoundSystem *m_pUpdatableSoundSystem = NULL;
+	//CUpdatableSoundSystem *m_pUpdatableSoundSystem = NULL;
 	CObserverChangedEventListener *m_pObserverListener = NULL;
 };
 
diff --git a/source/xcommon/IXCore.h b/source/xcommon/IXCore.h
index ac558821f..a5d6d59ad 100644
--- a/source/xcommon/IXCore.h
+++ b/source/xcommon/IXCore.h
@@ -16,6 +16,7 @@
 
 class IPluginManager;
 class IXResourceManager;
+class ITask;
 
 class IXCore: public IXUnknown
 {
@@ -47,6 +48,8 @@ public:
 	virtual ID XMETHODCALLTYPE forLoop(int iStart, int iEnd, const IParallelForBody *pBody, int iMaxChunkSize = 0) = 0;
 	virtual void XMETHODCALLTYPE waitForLoop(ID id) = 0;
 
+	virtual void XMETHODCALLTYPE addTask(ITask *pTask) = 0;
+
 	template<typename T> IEventChannel<T> *getEventChannel(const XGUID &guid)
 	{
 		return((IEventChannel<T>*)getEventChannelInternal(guid));
diff --git a/source/xcommon/IXSoundSystem.h b/source/xcommon/IXSoundSystem.h
index 2bc35b5b2..f33663b53 100644
--- a/source/xcommon/IXSoundSystem.h
+++ b/source/xcommon/IXSoundSystem.h
@@ -120,8 +120,6 @@ public:
 
 	virtual IXSoundLayer* XMETHODCALLTYPE createMasterLayer(const AudioRawDesc *pDesc, const char *szName) = 0;
 	virtual IXSoundLayer* XMETHODCALLTYPE findLayer(const char *szName) = 0;
-
-	virtual void XMETHODCALLTYPE update(const float3 &vListenerPos, const float3 &vListenerDir, const float3 &vListenerUp) = 0;
 };
 
 #endif
-- 
GitLab