Technique.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. //
  2. // Copyright (c) 2008-2014 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #pragma once
  23. #include "GraphicsDefs.h"
  24. #include "HashTable.h"
  25. #include "Resource.h"
  26. namespace Urho3D
  27. {
  28. class ShaderVariation;
  29. /// Lighting mode of a pass.
  30. enum PassLightingMode
  31. {
  32. LIGHTING_UNLIT,
  33. LIGHTING_PERVERTEX,
  34. LIGHTING_PERPIXEL
  35. };
  36. /// %Material rendering pass, which defines shaders and render state.
  37. class URHO3D_API Pass : public RefCounted
  38. {
  39. public:
  40. /// Construct.
  41. Pass(StringHash type);
  42. /// Destruct.
  43. ~Pass();
  44. /// Set blend mode.
  45. void SetBlendMode(BlendMode mode);
  46. /// Set depth compare mode.
  47. void SetDepthTestMode(CompareMode mode);
  48. /// Set pass lighting mode, affects what shader variations will be attempted to be loaded.
  49. void SetLightingMode(PassLightingMode mode);
  50. /// Set depth write on/off.
  51. void SetDepthWrite(bool enable);
  52. /// Set alpha masking hint. Completely opaque draw calls will be performed before alpha masked.
  53. void SetAlphaMask(bool enable);
  54. /// Set whether requires %Shader %Model 3.
  55. void SetIsSM3(bool enable);
  56. /// Set whether requires desktop level hardware.
  57. void SetIsDesktop(bool enable);
  58. /// Set vertex shader name.
  59. void SetVertexShader(const String& name);
  60. /// Set pixel shader name.
  61. void SetPixelShader(const String& name);
  62. /// Set vertex shader defines.
  63. void SetVertexShaderDefines(const String& defines);
  64. /// Set pixel shader defines.
  65. void SetPixelShaderDefines(const String& defines);
  66. /// Reset shader pointers.
  67. void ReleaseShaders();
  68. /// Mark shaders loaded this frame.
  69. void MarkShadersLoaded(unsigned frameNumber);
  70. /// Return pass type.
  71. const StringHash& GetType() const { return type_; }
  72. /// Return blend mode.
  73. BlendMode GetBlendMode() const { return blendMode_; }
  74. /// Return depth compare mode.
  75. CompareMode GetDepthTestMode() const { return depthTestMode_; }
  76. /// Return pass lighting mode.
  77. PassLightingMode GetLightingMode() const { return lightingMode_; }
  78. /// Return last shaders loaded frame number.
  79. unsigned GetShadersLoadedFrameNumber() const { return shadersLoadedFrameNumber_; }
  80. /// Return depth write mode.
  81. bool GetDepthWrite() const { return depthWrite_; }
  82. /// Return alpha masking hint.
  83. bool GetAlphaMask() const { return alphaMask_; }
  84. /// Return whether requires %Shader %Model 3.
  85. bool IsSM3() const { return isSM3_; }
  86. /// Return whether requires desktop level hardware.
  87. bool IsDesktop() const { return isDesktop_; }
  88. /// Return vertex shader name.
  89. const String& GetVertexShader() const { return vertexShaderName_; }
  90. /// Return pixel shader name.
  91. const String& GetPixelShader() const { return pixelShaderName_; }
  92. /// Return vertex shader defines.
  93. const String& GetVertexShaderDefines() const { return vertexShaderDefines_; }
  94. /// Return pixel shader defines.
  95. const String& GetPixelShaderDefines() const { return pixelShaderDefines_; }
  96. /// Return vertex shaders.
  97. Vector<SharedPtr<ShaderVariation> >& GetVertexShaders() { return vertexShaders_; }
  98. /// Return pixel shaders.
  99. Vector<SharedPtr<ShaderVariation> >& GetPixelShaders() { return pixelShaders_; }
  100. private:
  101. /// Pass type.
  102. StringHash type_;
  103. /// Blend mode.
  104. BlendMode blendMode_;
  105. /// Depth compare mode.
  106. CompareMode depthTestMode_;
  107. /// Lighting mode.
  108. PassLightingMode lightingMode_;
  109. /// Last shaders loaded frame number.
  110. unsigned shadersLoadedFrameNumber_;
  111. /// Depth write mode.
  112. bool depthWrite_;
  113. /// Alpha masking hint.
  114. bool alphaMask_;
  115. /// Require %Shader %Model 3 flag.
  116. bool isSM3_;
  117. /// Require desktop level hardware flag.
  118. bool isDesktop_;
  119. /// Vertex shader name.
  120. String vertexShaderName_;
  121. /// Pixel shader name.
  122. String pixelShaderName_;
  123. /// Vertex shader defines.
  124. String vertexShaderDefines_;
  125. /// Pixel shader defines.
  126. String pixelShaderDefines_;
  127. /// Vertex shaders.
  128. Vector<SharedPtr<ShaderVariation> > vertexShaders_;
  129. /// Pixel shaders.
  130. Vector<SharedPtr<ShaderVariation> > pixelShaders_;
  131. };
  132. /// %Material technique. Consists of several passes.
  133. class URHO3D_API Technique : public Resource
  134. {
  135. OBJECT(Technique);
  136. friend class Renderer;
  137. public:
  138. /// Construct.
  139. Technique(Context* context);
  140. /// Destruct.
  141. ~Technique();
  142. /// Register object factory.
  143. static void RegisterObject(Context* context);
  144. /// Load resource from stream. May be called from a worker thread. Return true if successful.
  145. virtual bool BeginLoad(Deserializer& source);
  146. /// Set whether requires %Shader %Model 3.
  147. void SetIsSM3(bool enable);
  148. /// Set whether requires desktop level hardware.
  149. void SetIsDesktop(bool enable);
  150. /// Create a new pass.
  151. Pass* CreatePass(StringHash type);
  152. /// Remove a pass.
  153. void RemovePass(StringHash type);
  154. /// Reset shader pointers in all passes.
  155. void ReleaseShaders();
  156. /// Return whether requires %Shader %Model 3.
  157. bool IsSM3() const { return isSM3_; }
  158. /// Return whether requires desktop level hardware.
  159. bool IsDesktop() const { return isDesktop_; }
  160. /// Return whether technique is supported by the current hardware.
  161. bool IsSupported() const { return (!isSM3_ || sm3Support_) && (!isDesktop_ || desktopSupport_); }
  162. /// Return whether has a pass.
  163. bool HasPass(StringHash type) const { return passes_.Find(type.Value()) != 0; }
  164. /// Return a pass, or null if not found.
  165. Pass* GetPass(StringHash type) const
  166. {
  167. SharedPtr<Pass>* passPtr = passes_.Find(type.Value());
  168. return passPtr ? passPtr->Get() : 0;
  169. }
  170. /// Return a pass that is supported for rendering, or null if not found.
  171. Pass* GetSupportedPass(StringHash type) const
  172. {
  173. SharedPtr<Pass>* passPtr = passes_.Find(type.Value());
  174. Pass* pass = passPtr ? passPtr->Get() : 0;
  175. return pass && (!pass->IsSM3() || sm3Support_) && (!pass->IsDesktop() || desktopSupport_) ? pass : 0;
  176. }
  177. /// Return number of passes.
  178. unsigned GetNumPasses() const { return numPasses_; }
  179. /// Return all the pass types in the hash table. The returned collection is not guaranteed to be in the same order as the hash table insertion order.
  180. Vector<StringHash> GetPassTypes() const;
  181. /// Return all the passes in the hash table. The returned collection is not guaranteed to be in the same order as the hash table insertion order.
  182. PODVector<Pass*> GetPasses() const;
  183. private:
  184. /// Require %Shader %Model 3 flag.
  185. bool isSM3_;
  186. /// Cached %Shader %Model 3 support flag.
  187. bool sm3Support_;
  188. /// Require desktop GPU flag.
  189. bool isDesktop_;
  190. /// Cached desktop GPU support flag.
  191. bool desktopSupport_;
  192. /// Passes.
  193. HashTable<SharedPtr<Pass>, 16> passes_;
  194. /// Number of passes.
  195. unsigned numPasses_;
  196. };
  197. }