Browse Source

Added a simpler way to specify renderer materials

BearishSun 10 năm trước cách đây
mục cha
commit
2116ef0d9f

+ 4 - 0
BansheeEngine/BansheeEngine.vcxproj

@@ -252,6 +252,8 @@
     <ClCompile Include="Source\BsRenderableHandler.cpp" />
     <ClCompile Include="Source\BsRenderable.cpp" />
     <ClCompile Include="Source\BsRenderer.cpp" />
+    <ClCompile Include="Source\BsRendererMaterial.cpp" />
+    <ClCompile Include="Source\BsRendererMaterialManager.cpp" />
     <ClCompile Include="Source\BsRenderQueue.cpp" />
     <ClCompile Include="Source\BsScriptCode.cpp" />
     <ClCompile Include="Source\BsScriptCodeImporter.cpp" />
@@ -286,6 +288,8 @@
     <ClInclude Include="Include\BsRenderable.h" />
     <ClInclude Include="Include\BsRenderableRTTI.h" />
     <ClInclude Include="Include\BsRenderer.h" />
+    <ClInclude Include="Include\BsRendererMaterial.h" />
+    <ClInclude Include="Include\BsRendererMaterialManager.h" />
     <ClInclude Include="Include\BsScriptCode.h" />
     <ClInclude Include="Include\BsScriptCodeImporter.h" />
     <ClInclude Include="Include\BsScriptCodeImportOptions.h" />

+ 12 - 0
BansheeEngine/BansheeEngine.vcxproj.filters

@@ -347,6 +347,12 @@
     <ClInclude Include="Include\BsRenderableHandler.h">
       <Filter>Header Files</Filter>
     </ClInclude>
+    <ClInclude Include="Include\BsRendererMaterial.h">
+      <Filter>Header Files</Filter>
+    </ClInclude>
+    <ClInclude Include="Include\BsRendererMaterialManager.h">
+      <Filter>Header Files</Filter>
+    </ClInclude>
   </ItemGroup>
   <ItemGroup>
     <ClCompile Include="Source\BsGUIElement.cpp">
@@ -592,5 +598,11 @@
     <ClCompile Include="Source\BsRenderableHandler.cpp">
       <Filter>Source Files</Filter>
     </ClCompile>
+    <ClCompile Include="Source\BsRendererMaterialManager.cpp">
+      <Filter>Source Files</Filter>
+    </ClCompile>
+    <ClCompile Include="Source\BsRendererMaterial.cpp">
+      <Filter>Source Files</Filter>
+    </ClCompile>
   </ItemGroup>
 </Project>

+ 7 - 5
BansheeEngine/Include/BsBuiltinResources.h

@@ -121,6 +121,13 @@ namespace BansheeEngine
 		 */
 		HMesh getMesh(BuiltinMesh mesh) const;
 
+		/**
+		 * @brief	Loads a shader at the specified path.
+		 * 
+		 * @param	Path relative to the default shader folder with no file extension.
+		 */
+		static HShader getShader(const Path& path);
+
 		static const Path BuiltinDataFolder;
 		static const Path EngineSkinFolder;
 		static const Path EngineCursorFolder;
@@ -157,11 +164,6 @@ namespace BansheeEngine
 		 */
 		static HTexture getCursorTexture(const WString& name);
 
-		/**
-		 * @brief	Loads a shader with the specified filename
-		 */
-		static HShader getShader(const WString& name);
-
 		HGUISkin mSkin;
 
 		PixelDataPtr mCursorArrow;

+ 76 - 0
BansheeEngine/Include/BsRendererMaterial.h

@@ -0,0 +1,76 @@
+#pragma once
+
+#include "BsPrerequisites.h"
+
+#define RMAT_DEF(path) virtual Path getShaderPath() override const { return path; }
+
+namespace BansheeEngine
+{
+	class RendererMaterialManager;
+
+	/**
+	 * @brief	Base class for all RendererMaterial instances, containing common data and methods.
+	 */
+	class BS_EXPORT RendererMaterialBase
+	{
+	public:
+		virtual ~RendererMaterialBase() { }
+
+		/**
+		 * @brief	Returns path relative to the default shader folder where the material shader is located.
+		 */
+		virtual Path getShaderPath() const { return Path::BLANK; }
+
+	private:
+		/**
+		 * @brief	Initializes the internal material. Should be called by the renderer material manager before
+		 * 			material may be used.
+		 */
+		void _initialize(const SPtr<ShaderCore>& shader);
+
+	protected:
+		friend class RendererMaterialManager;
+
+		/**
+		 * @brief	Allows derived classes to initialize their data.
+		 */
+		virtual void initialize() = 0;
+
+		SPtr<MaterialCore> mMaterial;
+	};
+
+	/**
+	 * @brief	Wrapper class around Material that allows a simple way to load 
+	 * 			and set up materials used by the renderer.
+	 */
+	template<class T>
+	class BS_EXPORT RendererMaterial
+	{
+	private:
+		/**
+		 * @brief	Helper class that allows renderer materials be registered on program/library load.
+		 */
+		struct InitOnStart
+		{
+		public:
+			InitOnStart()
+			{
+				instance = new T();
+
+				RendererMaterialManager::_registerMaterial(instance);
+			}
+		};
+
+	public:
+		virtual ~RendererMaterial() { }
+
+	private:
+		friend class RendererMaterialManager;
+
+		static T* instance;
+		volatile InitOnStart mInit;
+	};
+
+	template<class T>
+	T* RendererMaterial<T>::instance = nullptr;
+}

+ 34 - 0
BansheeEngine/Include/BsRendererMaterialManager.h

@@ -0,0 +1,34 @@
+#pragma once
+
+#include "BsCorePrerequisites.h"
+#include "BsModule.h"
+#include "BsRendererMaterial.h"
+
+namespace BansheeEngine
+{
+	/**
+	 * @brief	Initializes and handles all renderer materials.
+	 */
+	class BS_EXPORT RendererMaterialManager : public Module<RendererMaterialManager>
+	{
+	public:
+		RendererMaterialManager();
+
+	private:
+		template<class T>
+		friend class RendererMaterial;
+
+		/**
+		 * @brief	Registers a new material that should be initialized on module start-up.
+		 */
+		static void _registerMaterial(RendererMaterialBase* material);
+
+		/**
+		 * @brief	Initializes all materials on the core thread.
+		 */
+		static void initOnCore(const Vector<SPtr<ShaderCore>>& shaders);
+
+		static Vector<RendererMaterialBase*> mMaterialsToInit;
+		static Mutex mMutex;
+	};
+}

+ 3 - 0
BansheeEngine/Source/BsApplication.cpp

@@ -15,6 +15,7 @@
 #include "BsPlainTextImporter.h"
 #include "BsImporter.h"
 #include "BsShortcutManager.h"
+#include "BsRendererMaterialManager.h"
 
 namespace BansheeEngine
 {
@@ -55,6 +56,7 @@ namespace BansheeEngine
 		GUIManager::shutDown();
 		GUIMaterialManager::shutDown();
 		BuiltinResources::shutDown();
+		RendererMaterialManager::shutDown();
 		VirtualInput::shutDown();
 	}
 
@@ -67,6 +69,7 @@ namespace BansheeEngine
 
 		VirtualInput::startUp();
 		BuiltinResources::startUp();
+		RendererMaterialManager::startUp();
 		GUIManager::startUp();
 		GUIMaterialManager::startUp();
 		ShortcutManager::startUp();

+ 3 - 2
BansheeEngine/Source/BsBuiltinResources.cpp

@@ -801,10 +801,11 @@ namespace BansheeEngine
 		return Resources::instance().load<SpriteTexture>(texturePath);
 	}
 
-	HShader BuiltinResources::getShader(const WString& name)
+	HShader BuiltinResources::getShader(const Path& path)
 	{
 		Path programPath = EngineShaderFolder;
-		programPath.append(name + L".asset");
+		programPath.append(path);
+		programPath.setExtension(programPath.getExtension() + ".asset");
 
 		return gResources().load<Shader>(programPath);
 	}

+ 12 - 0
BansheeEngine/Source/BsRendererMaterial.cpp

@@ -0,0 +1,12 @@
+#include "BsRendererMaterial.h"
+#include "BsMaterial.h"
+
+namespace BansheeEngine
+{
+	void RendererMaterialBase::_initialize(const SPtr<ShaderCore>& shader)
+	{
+		mMaterial = MaterialCore::create(shader);
+
+		initialize();
+	}
+}

+ 44 - 0
BansheeEngine/Source/BsRendererMaterialManager.cpp

@@ -0,0 +1,44 @@
+#include "BsRendererMaterialManager.h"
+#include "BsBuiltinResources.h"
+#include "BsCoreThread.h"
+#include "BsShader.h"
+
+namespace BansheeEngine
+{
+	Vector<RendererMaterialBase*> RendererMaterialManager::mMaterialsToInit;
+	Mutex RendererMaterialManager::mMutex;
+
+	RendererMaterialManager::RendererMaterialManager()
+	{
+		BuiltinResources& br = BuiltinResources::instance();
+
+		Vector<SPtr<ShaderCore>> shaders;
+		for (auto& material : mMaterialsToInit)
+		{
+			HShader shader = br.getShader(material->getShaderPath());
+			if (shader.isLoaded())
+				shaders.push_back(shader->getCore());
+			else
+				shaders.push_back(nullptr);
+		}
+
+		gCoreAccessor().queueCommand(std::bind(&RendererMaterialManager::initOnCore, shaders));
+	}
+
+	void RendererMaterialManager::_registerMaterial(RendererMaterialBase* data)
+	{
+		Lock<> lock(mMutex);
+
+		mMaterialsToInit.push_back(data);
+	}
+
+	void RendererMaterialManager::initOnCore(const Vector<SPtr<ShaderCore>>& shaders)
+	{
+		Lock<> lock(mMutex);
+
+		for (UINT32 i = 0; i < mMaterialsToInit.size(); i++)
+		{
+			mMaterialsToInit[i]->_initialize(shaders[i]);
+		}
+	}
+}

+ 8 - 33
BansheeUtility/Include/BsThreadDefines.h

@@ -2,8 +2,6 @@
 
 #define BS_AUTO_MUTEX_NAME mutex
 
-#if BS_THREAD_SUPPORT
-
 #include <thread>
 #include <chrono>
 #include <mutex>
@@ -51,36 +49,13 @@
 // Utility
 #define BS_THREAD_SLEEP(ms) std::this_thread::sleep_for(std::chrono::milliseconds(ms));
 
-#else
-
-#define BS_AUTO_MUTEX
-#define BS_LOCK_AUTO_MUTEX
-#define BS_MUTEX(name)
-#define BS_STATIC_MUTEX(name)
-#define BS_STATIC_MUTEX_INSTANCE(name)
-#define BS_STATIC_MUTEX_CLASS_INSTANCE(name, classTypeName)
-#define BS_LOCK_MUTEX(name)
-#define BS_LOCK_MUTEX_NAMED(mutexName, lockName)
-#define BS_LOCK_TYPE UINT32
-#define BS_AUTO_SHARED_MUTEX
-#define BS_LOCK_AUTO_SHARED_MUTEX
-#define BS_COPY_AUTO_SHARED_MUTEX(from)
-#define BS_SET_AUTO_SHARED_MUTEX_NULL
-#define BS_MUTEX_CONDITIONAL(name) if(true)
-#define BS_RW_MUTEX(name)
-#define BS_LOCK_RW_MUTEX_READ(name)
-#define BS_LOCK_RW_MUTEX_WRITE(name)
-#define BS_THREAD_SYNCHRONISER(sync) 
-#define BS_STATIC_THREAD_SYNCHRONISER(sync)
-#define BS_STATIC_THREAD_SYNCHRONISER_CLASS_INSTANCE(sync, classTypeName)
-#define BS_THREAD_WAIT(sync, lock) 
-#define BS_THREAD_NOTIFY_ONE(sync) 
-#define BS_THREAD_NOTIFY_ALL(sync) 
-#define BS_THREAD_JOIN(thread)
-#define BS_THREAD_SLEEP(ms)
-#define BS_THREAD_ID_TYPE UINT32
-#define BS_THREAD_WORKER_INHERIT
-#define BS_DEFER_LOCK
+using Mutex = std::mutex;
+using RecursiveMutex = std::recursive_mutex;
+using Signal = std::condition_variable;
+using Thread = std::thread;
 
-#endif
+template <typename T = Mutex>
+using Lock = std::unique_lock<T>;
 
+template <typename T = RecursiveMutex>
+using RecursiveLock = std::unique_lock<T>;

+ 4 - 7
README.md

@@ -8,13 +8,11 @@ On top of the powerful and flexible C++ core lies a fully featured editor and a
 
 ## Download/Install
 
-To get Banshee to run you will need to check out the source code either from GitHub or download the source code package below. Additionally you will need dependencies only available for download below. Dependencies should be extracted in the same folder as the root folder of the project (they share folder structure so it should be easy to see what goes where).
+To get Banshee to run you will need to check out the source code from GitHub and additionally you will need dependencies available for download below. Dependencies should be extracted in the same folder as the root folder of the project (they share folder structure so it should be easy to see what goes where).
 
 To compile Banshee you will need Visual Studio 2013 (Express version will work, but earlier Visual Studio versions will not). Other Windows compilers might work but have not been tested. Support for more platforms and compilers will become available with time.
 
-[Download dependencies] (http://nolinkyet)
-
-[Download source + dependencies] (http://nolinkyet)
+[Download dependencies] (http://bearishsun.thalassa.feralhosting.com/BansheeDependencies.rar)
 
 To compile DirectX render systems you will also need a separately installed DirectX SDK. Check "Dependencies.txt" for all information regarding used dependencies.
 
@@ -118,7 +116,6 @@ To compile DirectX render systems you will also need a separately installed Dire
  * Video system integration
  * Networking system integration
  * Animation
- * GUI animation
  * Mac & Linux support
 
 ## Development state
@@ -182,6 +179,6 @@ Banshee is offered completely free for personal or commercial use under the Gene
 
 # Author
 
-Banshee is developed by Marko Pintera. I built the entire project from in my free time out of personal interest in game engine development, never having had the chance to do it professionally. As time went by it evolved into something more and it's now hard to believe how far the project has progressed since I started. With time (especially if community joins in) I hope we can build something that can rival true AAA projects.
+Banshee is developed by Marko Pintera. I've been a professional game developer for the last five years working on various mid-sized titles. My interests lie in engine and graphics development which I spend most of my free time on.
 
-I'd love to hear input from other developers, especially if it's positive! Contact me at [email protected] (antispam: flip gmail/com). 
+Contact me at [email protected] (antispam: flip gmail/com). 

+ 1 - 1
SBansheeEngine/Source/BsScriptFontBitmap.cpp

@@ -84,7 +84,7 @@ namespace BansheeEngine
 	{
 		const Vector<HTexture>& texturePages = instance->mBitmap->texturePages;
 
-		UINT32 numPages = texturePages.size();
+		UINT32 numPages = (UINT32)texturePages.size();
 		ScriptArray output = ScriptArray::create<ScriptTexture2D>(numPages);
 		for (UINT32 i = 0; i < numPages; i++)
 		{

+ 1 - 1
TODO.txt

@@ -82,7 +82,7 @@ Stage 2 polish:
  - Splash screen
  - Settings/Preferences window (+ menu entry)
  - Console window
- - About box - license info and other general info (+ menu entry)
+ - About box - license info, version info and other general info (+ menu entry)
 
 Optional:
  - When starting drag from hierarchy tree view it tends to select another object (can't repro)