Technique.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. //
  2. // Copyright (c) 2008-2016 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,
  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 masking hint. Completely opaque draw calls will be performed before alpha masked.
  55. void SetAlphaMask(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 name.
  71. const String& GetName() const { return name_; }
  72. /// Return pass index. This is used for optimal render-time pass queries that avoid map lookups.
  73. unsigned GetIndex() const { return index_; }
  74. /// Return blend mode.
  75. BlendMode GetBlendMode() const { return blendMode_; }
  76. /// Return culling mode override. If pass is not overriding culling mode (default), the illegal mode MAX_CULLMODES is returned.
  77. CullMode GetCullMode() const { return cullMode_; }
  78. /// Return depth compare mode.
  79. CompareMode GetDepthTestMode() const { return depthTestMode_; }
  80. /// Return pass lighting mode.
  81. PassLightingMode GetLightingMode() const { return lightingMode_; }
  82. /// Return last shaders loaded frame number.
  83. unsigned GetShadersLoadedFrameNumber() const { return shadersLoadedFrameNumber_; }
  84. /// Return depth write mode.
  85. bool GetDepthWrite() const { return depthWrite_; }
  86. /// Return alpha masking hint.
  87. bool GetAlphaMask() const { return alphaMask_; }
  88. /// Return whether requires desktop level hardware.
  89. bool IsDesktop() const { return isDesktop_; }
  90. /// Return vertex shader name.
  91. const String& GetVertexShader() const { return vertexShaderName_; }
  92. /// Return pixel shader name.
  93. const String& GetPixelShader() const { return pixelShaderName_; }
  94. /// Return vertex shader defines.
  95. const String& GetVertexShaderDefines() const { return vertexShaderDefines_; }
  96. /// Return pixel shader defines.
  97. const String& GetPixelShaderDefines() const { return pixelShaderDefines_; }
  98. /// Return vertex shaders.
  99. Vector<SharedPtr<ShaderVariation> >& GetVertexShaders() { return vertexShaders_; }
  100. /// Return pixel shaders.
  101. Vector<SharedPtr<ShaderVariation> >& GetPixelShaders() { return pixelShaders_; }
  102. private:
  103. /// Pass index.
  104. unsigned index_;
  105. /// Blend mode.
  106. BlendMode blendMode_;
  107. /// Culling mode.
  108. CullMode cullMode_;
  109. /// Depth compare mode.
  110. CompareMode depthTestMode_;
  111. /// Lighting mode.
  112. PassLightingMode lightingMode_;
  113. /// Last shaders loaded frame number.
  114. unsigned shadersLoadedFrameNumber_;
  115. /// Depth write mode.
  116. bool depthWrite_;
  117. /// Alpha masking hint.
  118. bool alphaMask_;
  119. /// Require desktop level hardware flag.
  120. bool isDesktop_;
  121. /// Vertex shader name.
  122. String vertexShaderName_;
  123. /// Pixel shader name.
  124. String pixelShaderName_;
  125. /// Vertex shader defines.
  126. String vertexShaderDefines_;
  127. /// Pixel shader defines.
  128. String pixelShaderDefines_;
  129. /// Vertex shaders.
  130. Vector<SharedPtr<ShaderVariation> > vertexShaders_;
  131. /// Pixel shaders.
  132. Vector<SharedPtr<ShaderVariation> > pixelShaders_;
  133. /// Pass name.
  134. String name_;
  135. };
  136. /// %Material technique. Consists of several passes.
  137. class ATOMIC_API Technique : public Resource
  138. {
  139. ATOMIC_OBJECT(Technique, Resource);
  140. friend class Renderer;
  141. public:
  142. /// Construct.
  143. Technique(Context* context);
  144. /// Destruct.
  145. ~Technique();
  146. /// Register object factory.
  147. static void RegisterObject(Context* context);
  148. /// Load resource from stream. May be called from a worker thread. Return true if successful.
  149. virtual bool BeginLoad(Deserializer& source);
  150. /// Set whether requires desktop level hardware.
  151. void SetIsDesktop(bool enable);
  152. /// Create a new pass.
  153. Pass* CreatePass(const String& passName);
  154. /// Remove a pass.
  155. void RemovePass(const String& passName);
  156. /// Reset shader pointers in all passes.
  157. void ReleaseShaders();
  158. /// Clone the technique. Passes will be deep copied to allow independent modification.
  159. SharedPtr<Technique> Clone(const String& cloneName = String::EMPTY) const;
  160. /// Return whether requires desktop level hardware.
  161. bool IsDesktop() const { return isDesktop_; }
  162. /// Return whether technique is supported by the current hardware.
  163. bool IsSupported() const { return !isDesktop_ || desktopSupport_; }
  164. /// Return whether has a pass.
  165. bool HasPass(unsigned passIndex) const { return passIndex < passes_.Size() && passes_[passIndex].Get() != 0; }
  166. /// 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.
  167. bool HasPass(const String& passName) const;
  168. /// Return a pass, or null if not found.
  169. Pass* GetPass(unsigned passIndex) const { return passIndex < passes_.Size() ? passes_[passIndex].Get() : 0; }
  170. /// 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.
  171. Pass* GetPass(const String& passName) const;
  172. /// Return a pass that is supported for rendering, or null if not found.
  173. Pass* GetSupportedPass(unsigned passIndex) const
  174. {
  175. Pass* pass = passIndex < passes_.Size() ? passes_[passIndex].Get() : 0;
  176. return pass && (!pass->IsDesktop() || desktopSupport_) ? pass : 0;
  177. }
  178. /// Return a supported pass by name. This overload should not be called in time-critical rendering loops; use a pre-acquired pass index instead.
  179. Pass* GetSupportedPass(const String& passName) const;
  180. /// Return number of passes.
  181. unsigned GetNumPasses() const;
  182. /// Return all pass names.
  183. Vector<String> GetPassNames() const;
  184. /// Return all passes.
  185. PODVector<Pass*> GetPasses() const;
  186. /// Return a pass type index by name. Allocate new if not used yet.
  187. static unsigned GetPassIndex(const String& passName);
  188. /// Index for base pass. Initialized once GetPassIndex() has been called for the first time.
  189. static unsigned basePassIndex;
  190. /// Index for alpha pass. Initialized once GetPassIndex() has been called for the first time.
  191. static unsigned alphaPassIndex;
  192. /// Index for prepass material pass. Initialized once GetPassIndex() has been called for the first time.
  193. static unsigned materialPassIndex;
  194. /// Index for deferred G-buffer pass. Initialized once GetPassIndex() has been called for the first time.
  195. static unsigned deferredPassIndex;
  196. /// Index for per-pixel light pass. Initialized once GetPassIndex() has been called for the first time.
  197. static unsigned lightPassIndex;
  198. /// Index for lit base pass. Initialized once GetPassIndex() has been called for the first time.
  199. static unsigned litBasePassIndex;
  200. /// Index for lit alpha pass. Initialized once GetPassIndex() has been called for the first time.
  201. static unsigned litAlphaPassIndex;
  202. /// Index for shadow pass. Initialized once GetPassIndex() has been called for the first time.
  203. static unsigned shadowPassIndex;
  204. private:
  205. /// Require desktop GPU flag.
  206. bool isDesktop_;
  207. /// Cached desktop GPU support flag.
  208. bool desktopSupport_;
  209. /// Passes.
  210. Vector<SharedPtr<Pass> > passes_;
  211. /// Pass index assignments.
  212. static HashMap<String, unsigned> passIndices;
  213. };
  214. }