Technique.h 12 KB

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