Technique.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. //
  2. // Copyright (c) 2008-2015 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. 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 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 desktop level hardware.
  55. void SetIsDesktop(bool enable);
  56. /// Set vertex shader name.
  57. void SetVertexShader(const String& name);
  58. /// Set pixel shader name.
  59. void SetPixelShader(const String& name);
  60. /// Set vertex shader defines.
  61. void SetVertexShaderDefines(const String& defines);
  62. /// Set pixel shader defines.
  63. void SetPixelShaderDefines(const String& defines);
  64. /// Reset shader pointers.
  65. void ReleaseShaders();
  66. /// Mark shaders loaded this frame.
  67. void MarkShadersLoaded(unsigned frameNumber);
  68. /// Return pass name.
  69. const String& GetName() const { return name_; }
  70. /// Return pass index. This is used for optimal render-time pass queries that avoid map lookups.
  71. unsigned GetIndex() const { return index_; }
  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 desktop level hardware.
  85. bool IsDesktop() const { return isDesktop_; }
  86. /// Return vertex shader name.
  87. const String& GetVertexShader() const { return vertexShaderName_; }
  88. /// Return pixel shader name.
  89. const String& GetPixelShader() const { return pixelShaderName_; }
  90. /// Return vertex shader defines.
  91. const String& GetVertexShaderDefines() const { return vertexShaderDefines_; }
  92. /// Return pixel shader defines.
  93. const String& GetPixelShaderDefines() const { return pixelShaderDefines_; }
  94. /// Return vertex shaders.
  95. Vector<SharedPtr<ShaderVariation> >& GetVertexShaders() { return vertexShaders_; }
  96. /// Return pixel shaders.
  97. Vector<SharedPtr<ShaderVariation> >& GetPixelShaders() { return pixelShaders_; }
  98. private:
  99. /// Pass index.
  100. unsigned index_;
  101. /// Blend mode.
  102. BlendMode blendMode_;
  103. /// Depth compare mode.
  104. CompareMode depthTestMode_;
  105. /// Lighting mode.
  106. PassLightingMode lightingMode_;
  107. /// Last shaders loaded frame number.
  108. unsigned shadersLoadedFrameNumber_;
  109. /// Depth write mode.
  110. bool depthWrite_;
  111. /// Alpha masking hint.
  112. bool alphaMask_;
  113. /// Require desktop level hardware flag.
  114. bool isDesktop_;
  115. /// Vertex shader name.
  116. String vertexShaderName_;
  117. /// Pixel shader name.
  118. String pixelShaderName_;
  119. /// Vertex shader defines.
  120. String vertexShaderDefines_;
  121. /// Pixel shader defines.
  122. String pixelShaderDefines_;
  123. /// Vertex shaders.
  124. Vector<SharedPtr<ShaderVariation> > vertexShaders_;
  125. /// Pixel shaders.
  126. Vector<SharedPtr<ShaderVariation> > pixelShaders_;
  127. /// Pass name.
  128. String name_;
  129. };
  130. /// %Material technique. Consists of several passes.
  131. class ATOMIC_API Technique : public Resource
  132. {
  133. OBJECT(Technique);
  134. friend class Renderer;
  135. public:
  136. /// Construct.
  137. Technique(Context* context);
  138. /// Destruct.
  139. ~Technique();
  140. /// Register object factory.
  141. static void RegisterObject(Context* context);
  142. /// Load resource from stream. May be called from a worker thread. Return true if successful.
  143. virtual bool BeginLoad(Deserializer& source);
  144. /// Set whether requires desktop level hardware.
  145. void SetIsDesktop(bool enable);
  146. /// Create a new pass.
  147. Pass* CreatePass(const String& passName);
  148. /// Remove a pass.
  149. void RemovePass(const String& passName);
  150. /// Reset shader pointers in all passes.
  151. void ReleaseShaders();
  152. /// Return whether requires desktop level hardware.
  153. bool IsDesktop() const { return isDesktop_; }
  154. /// Return whether technique is supported by the current hardware.
  155. bool IsSupported() const { return !isDesktop_ || desktopSupport_; }
  156. /// Return whether has a pass.
  157. bool HasPass(unsigned passIndex) const { return passIndex < passes_.Size() && passes_[passIndex].Get() != 0; }
  158. /// 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.
  159. bool HasPass(const String& passName) const;
  160. /// Return a pass, or null if not found.
  161. Pass* GetPass(unsigned passIndex) const { return passIndex < passes_.Size() ? passes_[passIndex].Get() : 0; }
  162. /// 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.
  163. Pass* GetPass(const String& passName) const;
  164. /// Return a pass that is supported for rendering, or null if not found.
  165. Pass* GetSupportedPass(unsigned passIndex) const
  166. {
  167. Pass* pass = passIndex < passes_.Size() ? passes_[passIndex].Get() : 0;
  168. return pass && (!pass->IsDesktop() || desktopSupport_) ? pass : 0;
  169. }
  170. /// Return a supported pass by name. This overload should not be called in time-critical rendering loops; use a pre-acquired pass index instead.
  171. Pass* GetSupportedPass(const String& passName) const;
  172. /// Return number of passes.
  173. unsigned GetNumPasses() const;
  174. /// Return all pass names.
  175. Vector<String> GetPassNames() const;
  176. /// Return all passes.
  177. PODVector<Pass*> GetPasses() const;
  178. /// Return a pass type index by name. Allocate new if not used yet.
  179. static unsigned GetPassIndex(const String& passName);
  180. /// Index for base pass. Initialized once GetPassIndex() has been called for the first time.
  181. static unsigned basePassIndex;
  182. /// Index for alpha pass. Initialized once GetPassIndex() has been called for the first time.
  183. static unsigned alphaPassIndex;
  184. /// Index for prepass material pass. Initialized once GetPassIndex() has been called for the first time.
  185. static unsigned materialPassIndex;
  186. /// Index for deferred G-buffer pass. Initialized once GetPassIndex() has been called for the first time.
  187. static unsigned deferredPassIndex;
  188. /// Index for per-pixel light pass. Initialized once GetPassIndex() has been called for the first time.
  189. static unsigned lightPassIndex;
  190. /// Index for lit base pass. Initialized once GetPassIndex() has been called for the first time.
  191. static unsigned litBasePassIndex;
  192. /// Index for lit alpha pass. Initialized once GetPassIndex() has been called for the first time.
  193. static unsigned litAlphaPassIndex;
  194. /// Index for shadow pass. Initialized once GetPassIndex() has been called for the first time.
  195. static unsigned shadowPassIndex;
  196. private:
  197. /// Require desktop GPU flag.
  198. bool isDesktop_;
  199. /// Cached desktop GPU support flag.
  200. bool desktopSupport_;
  201. /// Passes.
  202. Vector<SharedPtr<Pass> > passes_;
  203. /// Pass index assignments.
  204. static HashMap<String, unsigned> passIndices;
  205. };
  206. }