Technique.h 9.2 KB

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