Browse Source

Modified SceneGrid rendering so it doesn't use the sim thread render callback

BearishSun 10 years ago
parent
commit
520a45814e

+ 1 - 1
BansheeCore/Include/BsCoreRenderer.h

@@ -113,7 +113,7 @@ namespace BansheeEngine
 		 * @param	camera			Camera for which to trigger the callback.
 		 * @param	camera			Camera for which to trigger the callback.
 		 * @param	index			Index that determines the order of rendering when there are multiple registered callbacks.
 		 * @param	index			Index that determines the order of rendering when there are multiple registered callbacks.
 		 *							This must be unique. Lower indices get rendered sooner. Indices below 0 get rendered before the
 		 *							This must be unique. Lower indices get rendered sooner. Indices below 0 get rendered before the
-		 *							main viewport elements, while indices equal to greater to zero, after.
+		 *							main viewport elements, while indices equal or greater to zero, after.
 		 * @param	callback		Callback to trigger when the specified camera is being rendered.
 		 * @param	callback		Callback to trigger when the specified camera is being rendered.
 		 *
 		 *
 		 * @note	Core thread.
 		 * @note	Core thread.

+ 72 - 33
BansheeEditor/Include/BsSceneGrid.h

@@ -7,13 +7,16 @@
 
 
 namespace BansheeEngine
 namespace BansheeEngine
 {
 {
+	class SceneGridCore;
+
 	/**
 	/**
 	 * @brief	Handles rendering of the grid in the scene view.
 	 * @brief	Handles rendering of the grid in the scene view.
 	 */
 	 */
 	class SceneGrid
 	class SceneGrid
 	{
 	{
 	public:
 	public:
-		SceneGrid();
+		SceneGrid(const CameraHandlerPtr& camera);
+		~SceneGrid();
 
 
 		/**
 		/**
 		 * @brief	Sets the grid origin in world coordinates.
 		 * @brief	Sets the grid origin in world coordinates.
@@ -36,16 +39,6 @@ namespace BansheeEngine
 		 */
 		 */
 		void setSettings(const EditorSettingsPtr& settings);
 		void setSettings(const EditorSettingsPtr& settings);
 
 
-		/**
-		 * @brief	Triggered by the renderer when rendering the specified camera.
-		 *
-		 * @param	camera		Camera about to be rendered.
-		 * @param	drawList	Draw list we can use to queue our render commands in.
-		 *
-		 * @note	Internal method.
-		 */
-		void _render(const CameraHandlerPtr& camera, DrawList& drawList);
-
 		/**
 		/**
 		 * @brief	Called once per frame.
 		 * @brief	Called once per frame.
 		 *
 		 *
@@ -53,45 +46,91 @@ namespace BansheeEngine
 		 */
 		 */
 		void update();
 		void update();
 	private:
 	private:
+		/**
+		 * @brief	Updates internal grid parameters from the attached settings object.
+		 */
+		void updateFromEditorSettings();
+
 		/**
 		/**
 		 * @brief	Rebuilds the scene grid mesh. Call this whenever grid parameters change.
 		 * @brief	Rebuilds the scene grid mesh. Call this whenever grid parameters change.
 		 */
 		 */
 		void updateGridMesh();
 		void updateGridMesh();
 
 
 		/**
 		/**
-		 * @brief	Updates internal grid parameters from the attached settings object.
+		 * @brief	Initializes the core thread portion of the scene grid renderer.
+		 *
+		 * @param	material	Material used for drawing the grid.
+		 * @param	camera		Camera to render the scene grid to.
 		 */
 		 */
-		void updateFromEditorSettings();
+		void initializeCore(const SPtr<CameraHandlerCore>& camera, const SPtr<MaterialCore>& material);
 
 
-		HMesh mGridMesh;
-		HMaterial mGridMaterial;
-		MaterialParamMat4 mViewProjParam;
-		MaterialParamVec4 mWorldCameraPosParam;
-		MaterialParamColor mGridColorParam;
-		MaterialParamFloat mGridSpacingParam;
-		MaterialParamFloat mGridBorderWidthParam;
-		MaterialParamFloat mGridFadeOutStartParam;
-		MaterialParamFloat mGridFadeOutEndParam;
-		VertexDataDescPtr mVertexDesc;
+		/**
+		 * @brief	Destroys the core thread portion of the draw manager.
+		 */
+		void destroyCore(SceneGridCore* core);
+
+		Vector3 mOrigin;
+		float mSpacing = 1.0f;
+		UINT32 mSize = 256;
+		bool mCoreDirty;
 
 
 		EditorSettingsPtr mSettings;
 		EditorSettingsPtr mSettings;
 		UINT32 mSettingsHash = 0;
 		UINT32 mSettingsHash = 0;
 
 
-		Vector3 mOrigin;
+		HMesh mGridMesh;
+		VertexDataDescPtr mVertexDesc;
+		std::atomic<SceneGridCore*> mCore;
+	};
+
+	/**
+	 * @brief	Core thread portion of the scene grid. Handles interaction with the renderer.
+	 */
+	class SceneGridCore
+	{
+	public:
+		SceneGridCore() { }
+		~SceneGridCore();
+
+	private:
+		friend class SceneGrid;
+
+		/**
+		 * @brief	Initializes the object. Must be called right after construction and before any use.
+		 *
+		 * @param	material	Material used for drawing the grid.
+		 * @param	camera		Camera to render the scene grid to.
+		 */
+		void initialize(const SPtr<CameraHandlerCore>& camera, const SPtr<MaterialCore>& material);
+
+		/**
+		 * @brief	Updates the grid mesh to render.
+		 * 			
+		 * @param	mesh		Grid mesh to render.
+		 * @param	scpacing	Spacing between the grid lines.
+		 */
+		void updateData(const SPtr<MeshCore>& mesh, float spacing);
+
+		/**
+		 * @brief	Callback triggered by the renderer, actually draws the grid mesh.
+		 */
+		void render();
+
+		SPtr<CameraHandlerCore> mCamera;
+		SPtr<MeshCore> mGridMesh;
+		SPtr<MaterialCore> mGridMaterial;
 		float mSpacing = 1.0f;
 		float mSpacing = 1.0f;
-		UINT32 mSize = 256;
-		UINT32 mMajorAxisSpacing = 10;
-		UINT32 mAxisMarkerSpacing = 25;
+
+		MaterialParamMat4Core mViewProjParam;
+		MaterialParamVec4Core mWorldCameraPosParam;
+		MaterialParamColorCore mGridColorParam;
+		MaterialParamFloatCore mGridSpacingParam;
+		MaterialParamFloatCore mGridBorderWidthParam;
+		MaterialParamFloatCore mGridFadeOutStartParam;
+		MaterialParamFloatCore mGridFadeOutEndParam;
 
 
 		static const Color GRID_LINE_COLOR;
 		static const Color GRID_LINE_COLOR;
 		static const float LINE_WIDTH;
 		static const float LINE_WIDTH;
 		static const float LINE_BORDER_WIDTH;
 		static const float LINE_BORDER_WIDTH;
-		static const float MAJOR_AXIS_WIDTH;
-		static const float MAJOR_AXIS_BORDER_WIDTH;
-		static const float AXIS_MARKER_WIDTH;
-		static const float AXIS_MARKER_BORDER_WIDTH;
-		static const Color AXIS_X_MARKER_COLOR;
-		static const Color AXIS_Z_MARKER_COLOR;
 		static const float FADE_OUT_START;
 		static const float FADE_OUT_START;
 		static const float FADE_OUT_END;
 		static const float FADE_OUT_END;
 	};
 	};

+ 0 - 9
BansheeEditor/Include/BsSceneViewHandler.h

@@ -61,14 +61,6 @@ namespace BansheeEngine
 		void pickObject(const Vector2I& position, bool additive);
 		void pickObject(const Vector2I& position, bool additive);
 
 
 	protected:
 	protected:
-		/**
-		 * @brief	Triggered by the Renderer when rendering the specified viewport.
-		 *
-		 * @param	viewport	Viewport about to be rendered.
-		 * @param	drawList	Draw list we can use to queue our render commands in.
-		 */
-		void render(const Viewport* viewport, DrawList& drawList);
-
 		/**
 		/**
 		 * @brief	Checks is the pointer currently within the provided window, and if it is not
 		 * @brief	Checks is the pointer currently within the provided window, and if it is not
 		 *			the cursor is wrapped in such a way so that it is returned to within the window bounds.
 		 *			the cursor is wrapped in such a way so that it is returned to within the window bounds.
@@ -83,7 +75,6 @@ namespace BansheeEngine
 		SPtr<CameraHandler> mCamera;
 		SPtr<CameraHandler> mCamera;
 		SceneGrid* mSceneGrid;
 		SceneGrid* mSceneGrid;
 		SelectionRenderer* mSelectionRenderer;
 		SelectionRenderer* mSelectionRenderer;
-		HEvent mRenderCallback;
 
 
 		Vector2I mMouseDeltaCompensate;
 		Vector2I mMouseDeltaCompensate;
 	};
 	};

+ 93 - 41
BansheeEditor/Source/BsSceneGrid.cpp

@@ -8,40 +8,54 @@
 #include "BsBuiltinEditorResources.h"
 #include "BsBuiltinEditorResources.h"
 #include "BsCamera.h"
 #include "BsCamera.h"
 #include "BsRect3.h"
 #include "BsRect3.h"
+#include "BsCoreThread.h"
 #include "BsEditorSettings.h"
 #include "BsEditorSettings.h"
+#include "BsRendererManager.h"
+#include "BsRenderer.h"
 
 
 namespace BansheeEngine
 namespace BansheeEngine
 {
 {
-	const Color SceneGrid::GRID_LINE_COLOR = Color(0.5f, 0.5f, 0.5f);
-	const float SceneGrid::LINE_WIDTH = 0.025f;
-	const float SceneGrid::LINE_BORDER_WIDTH = 0.00075f;
-	const float SceneGrid::MAJOR_AXIS_WIDTH = 0.075f;
-	const float SceneGrid::MAJOR_AXIS_BORDER_WIDTH = 0.015f;
-	const float SceneGrid::AXIS_MARKER_WIDTH = 0.1f;
-	const float SceneGrid::AXIS_MARKER_BORDER_WIDTH = 0.02f;
-	const Color SceneGrid::AXIS_X_MARKER_COLOR = Color::Red;
-	const Color SceneGrid::AXIS_Z_MARKER_COLOR = Color::Blue;
-	const float SceneGrid::FADE_OUT_START = 5.0f;
-	const float SceneGrid::FADE_OUT_END = 40.0f;
-
-	SceneGrid::SceneGrid()
+	const Color SceneGridCore::GRID_LINE_COLOR = Color(0.5f, 0.5f, 0.5f);
+	const float SceneGridCore::LINE_WIDTH = 0.025f;
+	const float SceneGridCore::LINE_BORDER_WIDTH = 0.00075f;
+	const float SceneGridCore::FADE_OUT_START = 5.0f;
+	const float SceneGridCore::FADE_OUT_END = 40.0f;
+
+	SceneGrid::SceneGrid(const CameraHandlerPtr& camera)
+		:mCoreDirty(true), mCore(nullptr)
 	{
 	{
 		mVertexDesc = bs_shared_ptr_new<VertexDataDesc>();
 		mVertexDesc = bs_shared_ptr_new<VertexDataDesc>();
 		mVertexDesc->addVertElem(VET_FLOAT3, VES_POSITION);
 		mVertexDesc->addVertElem(VET_FLOAT3, VES_POSITION);
 		mVertexDesc->addVertElem(VET_FLOAT3, VES_NORMAL);
 		mVertexDesc->addVertElem(VET_FLOAT3, VES_NORMAL);
 
 
-		mGridMaterial = BuiltinEditorResources::instance().createSceneGridMaterial();
-		mViewProjParam = mGridMaterial->getParamMat4("matViewProj");
-		mWorldCameraPosParam = mGridMaterial->getParamVec4("worldCameraPos");
-		mGridColorParam = mGridMaterial->getParamColor("gridColor");
-		mGridSpacingParam = mGridMaterial->getParamFloat("gridSpacing");
-		mGridBorderWidthParam = mGridMaterial->getParamFloat("gridBorderWidth");
-		mGridFadeOutStartParam = mGridMaterial->getParamFloat("gridFadeOutStart");
-		mGridFadeOutEndParam = mGridMaterial->getParamFloat("gridFadeOutEnd");
+		mCore.store(bs_new<SceneGridCore>(), std::memory_order_release);
+
+		HMaterial gridMaterial = BuiltinEditorResources::instance().createSceneGridMaterial();
+		SPtr<MaterialCore> materialCore = gridMaterial->getCore();
+		gCoreAccessor().queueCommand(std::bind(&SceneGrid::initializeCore, this, camera->getCore(), materialCore));
 
 
 		updateGridMesh();
 		updateGridMesh();
 	}
 	}
 
 
+	SceneGrid::~SceneGrid()
+	{
+		gCoreAccessor().queueCommand(std::bind(&SceneGrid::destroyCore, this, mCore.load(std::memory_order_relaxed)));
+	}
+
+	void SceneGrid::initializeCore(const SPtr<CameraHandlerCore>& camera, const SPtr<MaterialCore>& material)
+	{
+		THROW_IF_NOT_CORE_THREAD;
+
+		mCore.load()->initialize(camera, material);
+	}
+
+	void SceneGrid::destroyCore(SceneGridCore* core)
+	{
+		THROW_IF_NOT_CORE_THREAD;
+
+		bs_delete(core);
+	}
+
 	void SceneGrid::setOrigin(const Vector3& origin)
 	void SceneGrid::setOrigin(const Vector3& origin)
 	{
 	{
 		if (mOrigin != origin)
 		if (mOrigin != origin)
@@ -65,7 +79,7 @@ namespace BansheeEngine
 		if (mSpacing != spacing)
 		if (mSpacing != spacing)
 		{
 		{
 			mSpacing = spacing;
 			mSpacing = spacing;
-			updateGridMesh();
+			mCoreDirty = true;
 		}
 		}
 	}
 	}
 
 
@@ -79,27 +93,14 @@ namespace BansheeEngine
 	{
 	{
 		if (mSettings != nullptr && mSettingsHash != mSettings->getHash())
 		if (mSettings != nullptr && mSettingsHash != mSettings->getHash())
 			updateFromEditorSettings();
 			updateFromEditorSettings();
-	}
-
-	void SceneGrid::_render(const CameraHandlerPtr& camera, DrawList& drawList)
-	{
-		MaterialPtr mat = mGridMaterial.getInternalPtr();
-		MeshPtr mesh = mGridMesh.getInternalPtr();
-
-		Matrix4 projMatrix = camera->getProjectionMatrixRS();
-		Matrix4 viewMatrix = camera->getViewMatrix();
-
-		Matrix4 viewProjMatrix = projMatrix * viewMatrix;
-		mViewProjParam.set(viewProjMatrix);
 
 
-		mWorldCameraPosParam.set(Vector4(camera->getPosition(), 1.0f));
-		mGridColorParam.set(GRID_LINE_COLOR);
-		mGridSpacingParam.set(mSpacing);
-		mGridBorderWidthParam.set(LINE_BORDER_WIDTH);
-		mGridFadeOutStartParam.set(FADE_OUT_START);
-		mGridFadeOutEndParam.set(FADE_OUT_END);
+		if (mCoreDirty)
+		{
+			SceneGridCore* core = mCore.load(std::memory_order_relaxed);
+			gCoreAccessor().queueCommand(std::bind(&SceneGridCore::updateData, core, mGridMesh->getCore(), mSpacing));
 
 
-		drawList.add(mat, mesh, 0, Vector3::ZERO);
+			mCoreDirty = false;
+		}
 	}
 	}
 
 
 	void SceneGrid::updateFromEditorSettings()
 	void SceneGrid::updateFromEditorSettings()
@@ -125,5 +126,56 @@ namespace BansheeEngine
 
 
 		ShapeMeshes3D::solidQuad(quad, meshData, 0, 0);
 		ShapeMeshes3D::solidQuad(quad, meshData, 0, 0);
 		mGridMesh = Mesh::create(meshData);
 		mGridMesh = Mesh::create(meshData);
+		mCoreDirty = true;
+	}
+
+	SceneGridCore::~SceneGridCore()
+	{
+		CoreRendererPtr activeRenderer = RendererManager::instance().getActive();
+		activeRenderer->_unregisterRenderCallback(mCamera.get(), -20);
+	}
+
+	void SceneGridCore::initialize(const SPtr<CameraHandlerCore>& camera, const SPtr<MaterialCore>& material)
+	{
+		mCamera = camera;
+		mGridMaterial = material;
+
+		mViewProjParam = mGridMaterial->getParamMat4("matViewProj");
+		mWorldCameraPosParam = mGridMaterial->getParamVec4("worldCameraPos");
+		mGridColorParam = mGridMaterial->getParamColor("gridColor");
+		mGridSpacingParam = mGridMaterial->getParamFloat("gridSpacing");
+		mGridBorderWidthParam = mGridMaterial->getParamFloat("gridBorderWidth");
+		mGridFadeOutStartParam = mGridMaterial->getParamFloat("gridFadeOutStart");
+		mGridFadeOutEndParam = mGridMaterial->getParamFloat("gridFadeOutEnd");
+
+		CoreRendererPtr activeRenderer = RendererManager::instance().getActive();
+		activeRenderer->_registerRenderCallback(camera.get(), -20, std::bind(&SceneGridCore::render, this));			
+	}
+
+	void SceneGridCore::updateData(const SPtr<MeshCore>& mesh, float spacing)
+	{
+		mGridMesh = mesh;
+		mSpacing = spacing;
+	}
+
+	void SceneGridCore::render()
+	{
+		THROW_IF_NOT_CORE_THREAD;
+
+		Matrix4 projMatrix = mCamera->getProjectionMatrixRS();
+		Matrix4 viewMatrix = mCamera->getViewMatrix();
+
+		Matrix4 viewProjMatrix = projMatrix * viewMatrix;
+		mViewProjParam.set(viewProjMatrix);
+
+		mWorldCameraPosParam.set(Vector4(mCamera->getPosition(), 1.0f));
+		mGridColorParam.set(GRID_LINE_COLOR);
+		mGridSpacingParam.set(mSpacing);
+		mGridBorderWidthParam.set(LINE_BORDER_WIDTH);
+		mGridFadeOutStartParam.set(FADE_OUT_START);
+		mGridFadeOutEndParam.set(FADE_OUT_END);
+
+		CoreRenderer::setPass(mGridMaterial, 0);
+		CoreRenderer::draw(mGridMesh, mGridMesh->getProperties().getSubMesh(0));
 	}
 	}
 }
 }

+ 1 - 14
BansheeEditor/Source/BsSceneViewHandler.cpp

@@ -23,9 +23,8 @@ namespace BansheeEngine
 	SceneViewHandler::SceneViewHandler(EditorWidgetBase* parentWidget, const SPtr<CameraHandler>& camera)
 	SceneViewHandler::SceneViewHandler(EditorWidgetBase* parentWidget, const SPtr<CameraHandler>& camera)
 		:mCamera(camera), mSceneGrid(nullptr), mParentWidget(parentWidget)
 		:mCamera(camera), mSceneGrid(nullptr), mParentWidget(parentWidget)
 	{
 	{
-		mRenderCallback = RendererManager::instance().getActive()->onRenderViewport.connect(std::bind(&SceneViewHandler::render, this, _1, _2));
 		mSelectionRenderer = bs_new<SelectionRenderer>();
 		mSelectionRenderer = bs_new<SelectionRenderer>();
-		mSceneGrid = bs_new<SceneGrid>();
+		mSceneGrid = bs_new<SceneGrid>(mCamera);
 		mSceneGrid->setSettings(gEditorApplication().getEditorSettings());
 		mSceneGrid->setSettings(gEditorApplication().getEditorSettings());
 		HandleManager::instance().setSettings(gEditorApplication().getEditorSettings());
 		HandleManager::instance().setSettings(gEditorApplication().getEditorSettings());
 	}
 	}
@@ -34,7 +33,6 @@ namespace BansheeEngine
 	{
 	{
 		bs_delete(mSceneGrid);
 		bs_delete(mSceneGrid);
 		bs_delete(mSelectionRenderer);
 		bs_delete(mSelectionRenderer);
-		mRenderCallback.disconnect();
 
 
 		if (GizmoManager::isStarted()) // If not active, we don't care
 		if (GizmoManager::isStarted()) // If not active, we don't care
 			GizmoManager::instance().clearRenderData();
 			GizmoManager::instance().clearRenderData();
@@ -110,17 +108,6 @@ namespace BansheeEngine
 			Selection::instance().clearSceneSelection();
 			Selection::instance().clearSceneSelection();
 	}
 	}
 
 
-	void SceneViewHandler::render(const Viewport* viewport, DrawList& drawList)
-	{
-		if (mCamera == nullptr)
-			return;
-
-		if (mCamera->getViewport().get() != viewport)
-			return;
-
-		mSceneGrid->_render(mCamera, drawList);
-	}
-
 	Vector2I SceneViewHandler::wrapCursorToWindow()
 	Vector2I SceneViewHandler::wrapCursorToWindow()
 	{
 	{
 		if (mParentWidget == nullptr)
 		if (mParentWidget == nullptr)