1 #ifndef SH_PLATFORM_H
2 #define SH_PLATFORM_H
3 
4 #include <string>
5 
6 #include <boost/shared_ptr.hpp>
7 
8 #include "Language.hpp"
9 #include "PropertyBase.hpp"
10 
11 namespace sh
12 {
13 	class Factory;
14 	class MaterialInstance;
15 
16 	enum GpuProgramType
17 	{
18 		GPT_Vertex,
19 		GPT_Fragment
20 		// GPT_Geometry
21 	};
22 
23 	// These classes are supposed to be filled by the platform implementation
24 	class GpuProgram
25 	{
26 	public:
~GpuProgram()27         virtual ~GpuProgram() {}
28 		virtual bool getSupported () = 0; ///< @return true if the compilation was successful
29 
30 		/// @param name name of the uniform in the shader
31 		/// @param autoConstantName name of the auto constant (for example world_viewproj_matrix)
32 		/// @param extraInfo if any extra info is needed (e.g. light index), put it here
33 		virtual void setAutoConstant (const std::string& name, const std::string& autoConstantName, const std::string& extraInfo = "") = 0;
34 	};
35 
36 	class TextureUnitState : public PropertySet
37 	{
38 	public:
39         virtual ~TextureUnitState();
40 		virtual void setTextureName (const std::string& textureName) = 0;
41 
42 	protected:
43 		virtual bool setPropertyOverride (const std::string& name, PropertyValuePtr& value, PropertySetGet *context);
44 	};
45 
46 	class Pass : public PropertySet
47 	{
48 	public:
49 		virtual boost::shared_ptr<TextureUnitState> createTextureUnitState (const std::string& name) = 0;
50 		virtual void assignProgram (GpuProgramType type, const std::string& name) = 0;
51 
52 		/// @param type gpu program type
53 		/// @param name name of the uniform in the shader
54 		/// @param vt type of value, e.g. vector4
55 		/// @param value value to set
56 		/// @param context used for retrieving linked values
57 		virtual void setGpuConstant (int type, const std::string& name, ValueType vt, PropertyValuePtr value, PropertySetGet* context) = 0;
58 
59 		virtual void setTextureUnitIndex (int programType, const std::string& name, int index) = 0;
60 
61 		virtual void addSharedParameter (int type, const std::string& name) = 0;
62 	};
63 
64 	class Material : public PropertySet
65 	{
66 	public:
67 		virtual boost::shared_ptr<Pass> createPass (const std::string& configuration, unsigned short lodIndex) = 0;
68 		virtual bool createConfiguration (const std::string& name, unsigned short lodIndex) = 0; ///< @return false if already exists
69 		virtual void removeAll () = 0; ///< remove all configurations
70 
71 		virtual bool isUnreferenced() = 0;
72 		virtual void unreferenceTextures() = 0;
73 		virtual void ensureLoaded() = 0;
74 
75 		virtual void setLodLevels (const std::string& lodLevels) = 0;
76 
77 		virtual void setShadowCasterMaterial (const std::string& name) = 0;
78 	};
79 
80 	class Platform
81 	{
82 	public:
83 		Platform (const std::string& basePath);
84 		virtual ~Platform ();
85 
86 		/// set the folder to use for shader caching
87 		void setCacheFolder (const std::string& folder);
88 
89 	private:
90 		virtual boost::shared_ptr<Material> createMaterial (const std::string& name) = 0;
91 
92 		virtual boost::shared_ptr<GpuProgram> createGpuProgram (
93 			GpuProgramType type,
94 			const std::string& compileArguments,
95 			const std::string& name, const std::string& profile,
96 			const std::string& source, Language lang) = 0;
97 
98 		virtual void destroyGpuProgram (const std::string& name) = 0;
99 
100 		virtual void setSharedParameter (const std::string& name, PropertyValuePtr value) = 0;
101 
102 		virtual bool isProfileSupported (const std::string& profile) = 0;
103 
104 		virtual void serializeShaders (const std::string& file);
105 		virtual void deserializeShaders (const std::string& file);
106 
107 		std::string getCacheFolder () const;
108 
109 		friend class Factory;
110 		friend class MaterialInstance;
111 		friend class ShaderInstance;
112 		friend class ShaderSet;
113 
114 	protected:
115 		/**
116 		 * this will be \a true if the platform supports serialization (writing shader microcode
117 		 * to disk) and deserialization (create gpu program from saved microcode)
118 		 */
119 		virtual bool supportsShaderSerialization ();
120 
121 		/**
122 		 * this will be \a true if the platform supports a listener that notifies the system
123 		 * whenever a material is requested for rendering. if this is supported, shaders can be
124 		 * compiled on-demand when needed (and not earlier)
125 		 * @todo the Factory is not designed yet to handle the case where this method returns false
126 		 */
127 		virtual bool supportsMaterialQueuedListener ();
128 
129 		/**
130 		 * fire event: material requested for rendering
131 		 * @param name material name
132 		 * @param configuration requested configuration
133 		 */
134 		MaterialInstance* fireMaterialRequested (const std::string& name, const std::string& configuration, unsigned short lodIndex);
135 
136 		std::string mCacheFolder;
137 		Factory* mFactory;
138 
139 	private:
140 		void setFactory (Factory* factory);
141 
142 		std::string mBasePath;
143 		std::string getBasePath();
144 	};
145 }
146 
147 #endif
148