Technique.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. //
  2. // Copyright (c) 2008-2017 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 "../Graphics/GraphicsDefs.h"
  24. #include "../Resource/Resource.h"
  25. namespace Urho3D
  26. {
  27. class ShaderVariation;
  28. /// Lighting mode of a pass.
  29. enum PassLightingMode
  30. {
  31. LIGHTING_UNLIT = 0,
  32. LIGHTING_PERVERTEX,
  33. LIGHTING_PERPIXEL
  34. };
  35. /// %Material rendering pass, which defines shaders and render state.
  36. class URHO3D_API Pass : public RefCounted
  37. {
  38. public:
  39. /// Construct.
  40. Pass(const String& passName);
  41. /// Destruct.
  42. ~Pass();
  43. /// Set blend mode.
  44. void SetBlendMode(BlendMode mode);
  45. /// Set culling mode override. By default culling mode is read from the material instead. Set the illegal culling mode MAX_CULLMODES to disable override again.
  46. void SetCullMode(CullMode mode);
  47. /// Set depth compare mode.
  48. void SetDepthTestMode(CompareMode mode);
  49. /// Set pass lighting mode, affects what shader variations will be attempted to be loaded.
  50. void SetLightingMode(PassLightingMode mode);
  51. /// Set depth write on/off.
  52. void SetDepthWrite(bool enable);
  53. /// Set alpha-to-coverage on/off.
  54. void SetAlphaToCoverage(bool enable);
  55. /// Set whether requires desktop level hardware.
  56. void SetIsDesktop(bool enable);
  57. /// Set vertex shader name.
  58. void SetVertexShader(const String& name);
  59. /// Set pixel shader name.
  60. void SetPixelShader(const String& name);
  61. /// Set vertex shader defines. Separate multiple defines with spaces.
  62. void SetVertexShaderDefines(const String& defines);
  63. /// Set pixel shader defines. Separate multiple defines with spaces.
  64. void SetPixelShaderDefines(const String& defines);
  65. /// Set vertex shader define excludes. Use to mark defines that the shader code will not recognize, to prevent compiling redundant shader variations.
  66. void SetVertexShaderDefineExcludes(const String& excludes);
  67. /// Set pixel shader define excludes. Use to mark defines that the shader code will not recognize, to prevent compiling redundant shader variations.
  68. void SetPixelShaderDefineExcludes(const String& excludes);
  69. /// Reset shader pointers.
  70. void ReleaseShaders();
  71. /// Mark shaders loaded this frame.
  72. void MarkShadersLoaded(unsigned frameNumber);
  73. /// Return pass name.
  74. const String& GetName() const { return name_; }
  75. /// Return pass index. This is used for optimal render-time pass queries that avoid map lookups.
  76. unsigned GetIndex() const { return index_; }
  77. /// Return blend mode.
  78. BlendMode GetBlendMode() const { return blendMode_; }
  79. /// Return culling mode override. If pass is not overriding culling mode (default), the illegal mode MAX_CULLMODES is returned.
  80. CullMode GetCullMode() const { return cullMode_; }
  81. /// Return depth compare mode.
  82. CompareMode GetDepthTestMode() const { return depthTestMode_; }
  83. /// Return pass lighting mode.
  84. PassLightingMode GetLightingMode() const { return lightingMode_; }
  85. /// Return last shaders loaded frame number.
  86. unsigned GetShadersLoadedFrameNumber() const { return shadersLoadedFrameNumber_; }
  87. /// Return depth write mode.
  88. bool GetDepthWrite() const { return depthWrite_; }
  89. /// Return alpha-to-coverage mode.
  90. bool GetAlphaToCoverage() const { return alphaToCoverage_; }
  91. /// Return whether requires desktop level hardware.
  92. bool IsDesktop() const { return isDesktop_; }
  93. /// Return vertex shader name.
  94. const String& GetVertexShader() const { return vertexShaderName_; }
  95. /// Return pixel shader name.
  96. const String& GetPixelShader() const { return pixelShaderName_; }
  97. /// Return vertex shader defines.
  98. const String& GetVertexShaderDefines() const { return vertexShaderDefines_; }
  99. /// Return pixel shader defines.
  100. const String& GetPixelShaderDefines() const { return pixelShaderDefines_; }
  101. /// Return vertex shader define excludes.
  102. const String& GetVertexShaderDefineExcludes() const { return vertexShaderDefineExcludes_; }
  103. /// Return pixel shader define excludes.
  104. const String& GetPixelShaderDefineExcludes() const { return pixelShaderDefineExcludes_; }
  105. /// Return vertex shaders.
  106. Vector<SharedPtr<ShaderVariation> >& GetVertexShaders() { return vertexShaders_; }
  107. /// Return pixel shaders.
  108. Vector<SharedPtr<ShaderVariation> >& GetPixelShaders() { return pixelShaders_; }
  109. /// Return vertex shaders with extra defines from the renderpath.
  110. Vector<SharedPtr<ShaderVariation> >& GetVertexShaders(const StringHash& extraDefinesHash);
  111. /// Return pixel shaders with extra defines from the renderpath.
  112. Vector<SharedPtr<ShaderVariation> >& GetPixelShaders(const StringHash& extraDefinesHash);
  113. /// Return the effective vertex shader defines, accounting for excludes. Called internally by Renderer.
  114. String GetEffectiveVertexShaderDefines() const;
  115. /// Return the effective pixel shader defines, accounting for excludes. Called internally by Renderer.
  116. String GetEffectivePixelShaderDefines() const;
  117. private:
  118. /// Pass index.
  119. unsigned index_;
  120. /// Blend mode.
  121. BlendMode blendMode_;
  122. /// Culling mode.
  123. CullMode cullMode_;
  124. /// Depth compare mode.
  125. CompareMode depthTestMode_;
  126. /// Lighting mode.
  127. PassLightingMode lightingMode_;
  128. /// Last shaders loaded frame number.
  129. unsigned shadersLoadedFrameNumber_;
  130. /// Depth write mode.
  131. bool depthWrite_;
  132. /// Alpha-to-coverage mode.
  133. bool alphaToCoverage_;
  134. /// Require desktop level hardware flag.
  135. bool isDesktop_;
  136. /// Vertex shader name.
  137. String vertexShaderName_;
  138. /// Pixel shader name.
  139. String pixelShaderName_;
  140. /// Vertex shader defines.
  141. String vertexShaderDefines_;
  142. /// Pixel shader defines.
  143. String pixelShaderDefines_;
  144. /// Vertex shader define excludes.
  145. String vertexShaderDefineExcludes_;
  146. /// Pixel shader define excludes.
  147. String pixelShaderDefineExcludes_;
  148. /// Vertex shaders.
  149. Vector<SharedPtr<ShaderVariation> > vertexShaders_;
  150. /// Pixel shaders.
  151. Vector<SharedPtr<ShaderVariation> > pixelShaders_;
  152. /// Vertex shaders with extra defines from the renderpath.
  153. HashMap<StringHash, Vector<SharedPtr<ShaderVariation> > > extraVertexShaders_;
  154. /// Pixel shaders with extra defines from the renderpath.
  155. HashMap<StringHash, Vector<SharedPtr<ShaderVariation> > > extraPixelShaders_;
  156. /// Pass name.
  157. String name_;
  158. };
  159. /// %Material technique. Consists of several passes.
  160. class URHO3D_API Technique : public Resource
  161. {
  162. URHO3D_OBJECT(Technique, Resource);
  163. friend class Renderer;
  164. public:
  165. /// Construct.
  166. Technique(Context* context);
  167. /// Destruct.
  168. ~Technique();
  169. /// Register object factory.
  170. static void RegisterObject(Context* context);
  171. /// Load resource from stream. May be called from a worker thread. Return true if successful.
  172. virtual bool BeginLoad(Deserializer& source);
  173. /// Set whether requires desktop level hardware.
  174. void SetIsDesktop(bool enable);
  175. /// Create a new pass.
  176. Pass* CreatePass(const String& passName);
  177. /// Remove a pass.
  178. void RemovePass(const String& passName);
  179. /// Reset shader pointers in all passes.
  180. void ReleaseShaders();
  181. /// Clone the technique. Passes will be deep copied to allow independent modification.
  182. SharedPtr<Technique> Clone(const String& cloneName = String::EMPTY) const;
  183. /// Return whether requires desktop level hardware.
  184. bool IsDesktop() const { return isDesktop_; }
  185. /// Return whether technique is supported by the current hardware.
  186. bool IsSupported() const { return !isDesktop_ || desktopSupport_; }
  187. /// Return whether has a pass.
  188. bool HasPass(unsigned passIndex) const { return passIndex < passes_.Size() && passes_[passIndex].Get() != 0; }
  189. /// Return whether has a pass by name. This overload should not be called in time-critical rendering loops; use a pre-acquired pass index instead.
  190. bool HasPass(const String& passName) const;
  191. /// Return a pass, or null if not found.
  192. Pass* GetPass(unsigned passIndex) const { return passIndex < passes_.Size() ? passes_[passIndex].Get() : 0; }
  193. /// Return a pass by name, or null if not found. This overload should not be called in time-critical rendering loops; use a pre-acquired pass index instead.
  194. Pass* GetPass(const String& passName) const;
  195. /// Return a pass that is supported for rendering, or null if not found.
  196. Pass* GetSupportedPass(unsigned passIndex) const
  197. {
  198. Pass* pass = passIndex < passes_.Size() ? passes_[passIndex].Get() : 0;
  199. return pass && (!pass->IsDesktop() || desktopSupport_) ? pass : 0;
  200. }
  201. /// Return a supported pass by name. This overload should not be called in time-critical rendering loops; use a pre-acquired pass index instead.
  202. Pass* GetSupportedPass(const String& passName) const;
  203. /// Return number of passes.
  204. unsigned GetNumPasses() const;
  205. /// Return all pass names.
  206. Vector<String> GetPassNames() const;
  207. /// Return all passes.
  208. PODVector<Pass*> GetPasses() const;
  209. /// Return a clone with added shader compilation defines. Called internally by Material.
  210. SharedPtr<Technique> CloneWithDefines(const String& vsDefines, const String& psDefines);
  211. /// Return a pass type index by name. Allocate new if not used yet.
  212. static unsigned GetPassIndex(const String& passName);
  213. /// Index for base pass. Initialized once GetPassIndex() has been called for the first time.
  214. static unsigned basePassIndex;
  215. /// Index for alpha pass. Initialized once GetPassIndex() has been called for the first time.
  216. static unsigned alphaPassIndex;
  217. /// Index for prepass material pass. Initialized once GetPassIndex() has been called for the first time.
  218. static unsigned materialPassIndex;
  219. /// Index for deferred G-buffer pass. Initialized once GetPassIndex() has been called for the first time.
  220. static unsigned deferredPassIndex;
  221. /// Index for per-pixel light pass. Initialized once GetPassIndex() has been called for the first time.
  222. static unsigned lightPassIndex;
  223. /// Index for lit base pass. Initialized once GetPassIndex() has been called for the first time.
  224. static unsigned litBasePassIndex;
  225. /// Index for lit alpha pass. Initialized once GetPassIndex() has been called for the first time.
  226. static unsigned litAlphaPassIndex;
  227. /// Index for shadow pass. Initialized once GetPassIndex() has been called for the first time.
  228. static unsigned shadowPassIndex;
  229. private:
  230. /// Require desktop GPU flag.
  231. bool isDesktop_;
  232. /// Cached desktop GPU support flag.
  233. bool desktopSupport_;
  234. /// Passes.
  235. Vector<SharedPtr<Pass> > passes_;
  236. /// Cached clones with added shader compilation defines.
  237. HashMap<Pair<StringHash, StringHash>, SharedPtr<Technique> > cloneTechniques_;
  238. /// Pass index assignments.
  239. static HashMap<String, unsigned> passIndices;
  240. };
  241. }