Marko Pintera 11 лет назад
Родитель
Сommit
91ba10d45c

+ 10 - 0
BansheeCore/Include/BsGpuParams.h

@@ -550,6 +550,11 @@ namespace BansheeEngine
 		 * @copydoc	GpuParamsBase::GpuParamsBase
 		 */
 		GpuParamsCore(const GpuParamDescPtr& paramDesc, bool transposeMatrices);
+
+		/**
+		 * @copydoc	CoreObjectCore::syncToCore
+		 */
+		void syncToCore(const CoreSyncData& data);
 	};
 
 	/**
@@ -595,5 +600,10 @@ namespace BansheeEngine
 		 * @copydoc	CoreObject::createCore
 		 */
 		SPtr<CoreObjectCore> createCore() const;
+
+		/**
+		 * @copydoc	CoreObject::syncToCore
+		 */
+		CoreSyncData syncToCore(FrameAlloc* allocator);
 	};
 }

+ 5 - 4
BansheeCore/Source/BsCoreApplication.cpp

@@ -122,12 +122,10 @@ namespace BansheeEngine
 		ProfilerGPU::shutDown();
 
 		shutdownPlugin(mSceneManagerPlugin);
-		unloadPlugin(mSceneManagerPlugin);
-		
+
 		RendererManager::shutDown();
 		shutdownPlugin(mRendererPlugin);
-		unloadPlugin(mRendererPlugin);
-
+		
 		Input::shutDown();
 
 		Resources::shutDown();
@@ -138,6 +136,9 @@ namespace BansheeEngine
 		gCoreThread().update();
 		gCoreThread().submitAccessors(true);
 
+		unloadPlugin(mSceneManagerPlugin);
+		unloadPlugin(mRendererPlugin);
+
 		RenderSystemManager::shutDown();
 		GpuProgramCoreManager::shutDown();
 		GpuProgramManager::shutDown();

+ 104 - 0
BansheeCore/Source/BsGpuParams.cpp

@@ -2,6 +2,8 @@
 #include "BsGpuParamDesc.h"
 #include "BsGpuParamBlockBuffer.h"
 #include "BsVector2.h"
+#include "BsTexture.h"
+#include "BsSamplerState.h"
 #include "BsFrameAlloc.h"
 #include "BsDebug.h"
 #include "BsException.h"
@@ -149,6 +151,52 @@ UINT32 GpuParamsBase::getDataParamSize(const String& name) const
 		}
 	}
 
+	void GpuParamsCore::syncToCore(const CoreSyncData& data)
+	{
+		UINT32 textureInfoSize = mNumTextures * sizeof(BoundTextureInfo);
+		UINT32 paramBufferSize = mNumParamBlocks * sizeof(SPtr<GpuParamBlockBufferCore>);
+		UINT32 textureArraySize = mNumTextures * sizeof(SPtr<TextureCore>);
+		UINT32 samplerArraySize = mNumSamplerStates * sizeof(SPtr<SamplerStateCore>);
+
+		UINT32 totalSize = textureInfoSize + paramBufferSize + textureArraySize + samplerArraySize;
+
+		UINT32 textureInfoOffset = 0;
+		UINT32 paramBufferOffset = textureInfoOffset + textureInfoSize;
+		UINT32 textureArrayOffset = paramBufferOffset + paramBufferSize;
+		UINT32 samplerArrayOffset = textureArrayOffset + textureArraySize;
+
+		assert(data.getBufferSize() == totalSize);
+
+		UINT8* dataPtr = data.getBuffer();
+
+		BoundTextureInfo* textureInfos = (BoundTextureInfo*)(dataPtr + textureInfoOffset);
+		SPtr<GpuParamBlockBufferCore>* paramBuffers = (SPtr<GpuParamBlockBufferCore>*)(dataPtr + paramBufferOffset);
+		SPtr<TextureCore>* textures = (SPtr<TextureCore>*)(dataPtr + textureArrayOffset);
+		SPtr<SamplerStateCore>* samplers = (SPtr<SamplerStateCore>*)(dataPtr + samplerArrayOffset);
+
+		// Copy & destruct
+		for (UINT32 i = 0; i < mNumParamBlocks; i++)
+		{
+			mParamBlockBuffers[i] = paramBuffers[i];
+			paramBuffers[i].~SPtr<GpuParamBlockBufferCore>();
+		}
+
+		for (UINT32 i = 0; i < mNumTextures; i++)
+		{
+			mTextureInfo[i] = textureInfos[i];
+			textureInfos[i].~BoundTextureInfo();
+
+			mTextures[i] = textures[i];
+			textures[i].~SPtr<TextureCore>();
+		}
+
+		for (UINT32 i = 0; i < mNumSamplerStates; i++)
+		{
+			mSamplerStates[i] = samplers[i];
+			samplers[i].~SPtr<SamplerStateCore>();
+		}
+	}
+
 	SPtr<GpuParamsCore> GpuParamsCore::create(const GpuParamDescPtr& paramDesc, bool transposeMatrices)
 	{
 		GpuParamsCore* params = new (bs_alloc<GpuParamsCore>()) GpuParamsCore(paramDesc, transposeMatrices);
@@ -193,4 +241,60 @@ UINT32 GpuParamsBase::getDataParamSize(const String& name) const
 		
 		return paramsPtr;
 	}
+
+	CoreSyncData GpuParams::syncToCore(FrameAlloc* allocator)
+	{
+		UINT32 textureInfoSize = mNumTextures * sizeof(BoundTextureInfo);
+		UINT32 paramBufferSize = mNumParamBlocks * sizeof(SPtr<GpuParamBlockBufferCore>);
+		UINT32 textureArraySize = mNumTextures * sizeof(SPtr<TextureCore>);
+		UINT32 samplerArraySize = mNumSamplerStates * sizeof(SPtr<SamplerStateCore>);
+
+		UINT32 totalSize = textureInfoSize + paramBufferSize + textureArraySize + samplerArraySize;
+
+		UINT32 textureInfoOffset = 0;
+		UINT32 paramBufferOffset = textureInfoOffset + textureInfoSize;
+		UINT32 textureArrayOffset = paramBufferOffset + paramBufferSize;
+		UINT32 samplerArrayOffset = textureArrayOffset + textureArraySize;
+
+		UINT8* data = allocator->alloc(totalSize);
+
+		BoundTextureInfo* textureInfos = (BoundTextureInfo*)(data + textureInfoOffset);
+		SPtr<GpuParamBlockBufferCore>* paramBuffers = (SPtr<GpuParamBlockBufferCore>*)(data + paramBufferOffset);
+		SPtr<TextureCore>* textures = (SPtr<TextureCore>*)(data + textureArrayOffset);
+		SPtr<SamplerStateCore>* samplers = (SPtr<SamplerStateCore>*)(data + samplerArrayOffset);
+
+		// Construct & copy
+		for (UINT32 i = 0; i < mNumParamBlocks; i++)
+		{
+			new (&paramBuffers[i]) SPtr<GpuParamBlockBufferCore>();
+
+			if (mParamBlockBuffers[i] != nullptr)
+				paramBuffers[i] = mParamBlockBuffers[i]->getCore();
+		}
+
+		for (UINT32 i = 0; i < mNumTextures; i++)
+		{
+			new (&textureInfos[i]) BoundTextureInfo();
+			textureInfos[i] = mTextureInfo[i];
+
+			new (&textures[i]) SPtr<TextureCore>();
+
+			if (mTextures[i].isLoaded())
+				textures[i] = mTextures[i]->getCore();
+			else
+				textures[i] = nullptr;
+		}
+
+		for (UINT32 i = 0; i < mNumSamplerStates; i++)
+		{
+			new (&samplers[i]) SPtr<SamplerStateCore>();
+
+			if (mSamplerStates[i].isLoaded())
+				samplers[i] = mSamplerStates[i]->getCore();
+			else
+				samplers[i] = nullptr;
+		}
+
+		return CoreSyncData(data, totalSize);
+	}
 }

+ 10 - 0
BansheeRenderer/Include/BsBansheeRenderer.h

@@ -153,6 +153,16 @@ namespace BansheeEngine
 		 */
 		void cameraRemoved(const CameraHandlerPtr& camera);
 
+		/**
+		 * @brief	Creates a controller used for rendering.
+		 */
+		void createController();
+
+		/**
+		 * @brief	Destroys a controller used for rendering.
+		 */
+		static void destroyController(LitTexRenderableController* controller);
+
 		Vector<RenderableProxyPtr> mDeletedRenderableProxies;
 		Vector<CameraProxyPtr> mDeletedCameraProxies;
 

+ 13 - 3
BansheeRenderer/Source/BsBansheeRenderer.cpp

@@ -55,15 +55,25 @@ namespace BansheeEngine
 	{
 		Renderer::_onActivated();
 
-		mLitTexHandler = bs_new<LitTexRenderableController>();
+		gCoreAccessor().queueCommand(std::bind(&BansheeRenderer::createController, this));
 	}
 
 	void BansheeRenderer::_onDeactivated()
 	{
 		Renderer::_onDeactivated();
 
-		if (mLitTexHandler != nullptr)
-			bs_delete(mLitTexHandler);
+		gCoreAccessor().queueCommand(std::bind(&destroyController, mLitTexHandler));
+	}
+
+	void BansheeRenderer::createController()
+	{
+		mLitTexHandler = bs_new<LitTexRenderableController>();
+	}
+
+	void BansheeRenderer::destroyController(LitTexRenderableController* controller)
+	{
+		if (controller != nullptr)
+			bs_delete(controller);
 	}
 
 	void BansheeRenderer::addRenderableProxy(RenderableProxyPtr proxy)

+ 9 - 1
BansheeSceneManager/Source/BsBansheeManagerPlugin.cpp

@@ -22,11 +22,19 @@ namespace BansheeEngine
 		return nullptr;
 	}
 
+	/**
+	 * @brief	Called by the engine when the plugin should be shut down and all of its resources should be released.
+	 */
+	extern "C" BS_SM_EXPORT void shutdownPlugin()
+	{
+		SceneManager::shutDown();
+	}
+
 	/**
 	 * @brief	Exit point of the plugin. Called by the engine just before the plugin is unloaded.
 	 */
 	extern "C" BS_SM_EXPORT void unloadPlugin()
 	{
-		SceneManager::shutDown();
+
 	}
 }

+ 0 - 10
TODO.txt

@@ -12,13 +12,6 @@ Also:
 
 GpuParams refactor:
 
-Missing:
- GpuParams::createCore
- GpuParams::getCore
-
- GpuParam versions for Core objects
- Sync methods
-
 I need to ensure renderer buffers get set properly after GpuParams are synced
  - This used to up in updateMaterialProxy along with updating dirty params but since syncing
    is now internal to GpuParams I need to figure out a better way of doing it.
@@ -26,9 +19,6 @@ I need to ensure renderer buffers get set properly after GpuParams are synced
     - Likely just an extra field on GPuParamsCore
 
 
-- Other:
- - PerObjectData should store core version of GpuParamBlockBuffer
-
 Should I force CoreObject sync whenever submitCoreAccessors is called?
  - This way I ensure the data is up to date. e.g. if user updates GpuParams and immediately performs
    some rendering use CoreAccessor that update will not register.