Technique.h 13 KB

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