Technique.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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 %Shader %Model 3 flag.
  113. bool isSM3_;
  114. /// Require desktop level hardware flag.
  115. bool isDesktop_;
  116. /// Vertex shader name.
  117. String vertexShaderName_;
  118. /// Pixel shader name.
  119. String pixelShaderName_;
  120. /// Vertex shader defines.
  121. String vertexShaderDefines_;
  122. /// Pixel shader defines.
  123. String pixelShaderDefines_;
  124. /// Vertex shaders.
  125. Vector<SharedPtr<ShaderVariation> > vertexShaders_;
  126. /// Pixel shaders.
  127. Vector<SharedPtr<ShaderVariation> > pixelShaders_;
  128. /// Pass name.
  129. String name_;
  130. };
  131. /// %Material technique. Consists of several passes.
  132. class ATOMIC_API Technique : public Resource
  133. {
  134. OBJECT(Technique);
  135. friend class Renderer;
  136. public:
  137. /// Construct.
  138. Technique(Context* context);
  139. /// Destruct.
  140. ~Technique();
  141. /// Register object factory.
  142. static void RegisterObject(Context* context);
  143. /// Load resource from stream. May be called from a worker thread. Return true if successful.
  144. virtual bool BeginLoad(Deserializer& source);
  145. /// Set whether requires desktop level hardware.
  146. void SetIsDesktop(bool enable);
  147. /// Create a new pass.
  148. Pass* CreatePass(const String& passName);
  149. /// Remove a pass.
  150. void RemovePass(const String& passName);
  151. /// Reset shader pointers in all passes.
  152. void ReleaseShaders();
  153. /// Return whether requires desktop level hardware.
  154. bool IsDesktop() const { return isDesktop_; }
  155. /// Return whether technique is supported by the current hardware.
  156. bool IsSupported() const { return !isDesktop_ || desktopSupport_; }
  157. /// Return whether has a pass.
  158. bool HasPass(unsigned passIndex) const { return passIndex < passes_.Size() && passes_[passIndex].Get() != 0; }
  159. /// 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.
  160. bool HasPass(const String& passName) const;
  161. /// Return a pass, or null if not found.
  162. Pass* GetPass(unsigned passIndex) const { return passIndex < passes_.Size() ? passes_[passIndex].Get() : 0; }
  163. /// 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.
  164. Pass* GetPass(const String& passName) const;
  165. /// Return a pass that is supported for rendering, or null if not found.
  166. Pass* GetSupportedPass(unsigned passIndex) const
  167. {
  168. Pass* pass = passIndex < passes_.Size() ? passes_[passIndex].Get() : 0;
  169. return pass && (!pass->IsDesktop() || desktopSupport_) ? pass : 0;
  170. }
  171. /// Return a supported pass by name. This overload should not be called in time-critical rendering loops; use a pre-acquired pass index instead.
  172. Pass* GetSupportedPass(const String& passName) const;
  173. /// Return number of passes.
  174. unsigned GetNumPasses() const;
  175. /// Return all pass names.
  176. Vector<String> GetPassNames() const;
  177. /// Return all passes.
  178. PODVector<Pass*> GetPasses() const;
  179. /// Return a pass type index by name. Allocate new if not used yet.
  180. static unsigned GetPassIndex(const String& passName);
  181. /// Index for base pass. Initialized once GetPassIndex() has been called for the first time.
  182. static unsigned basePassIndex;
  183. /// Index for alpha pass. Initialized once GetPassIndex() has been called for the first time.
  184. static unsigned alphaPassIndex;
  185. /// Index for prepass material pass. Initialized once GetPassIndex() has been called for the first time.
  186. static unsigned materialPassIndex;
  187. /// Index for deferred G-buffer pass. Initialized once GetPassIndex() has been called for the first time.
  188. static unsigned deferredPassIndex;
  189. /// Index for per-pixel light pass. Initialized once GetPassIndex() has been called for the first time.
  190. static unsigned lightPassIndex;
  191. /// Index for lit base pass. Initialized once GetPassIndex() has been called for the first time.
  192. static unsigned litBasePassIndex;
  193. /// Index for lit alpha pass. Initialized once GetPassIndex() has been called for the first time.
  194. static unsigned litAlphaPassIndex;
  195. /// Index for shadow pass. Initialized once GetPassIndex() has been called for the first time.
  196. static unsigned shadowPassIndex;
  197. private:
  198. /// Require desktop GPU flag.
  199. bool isDesktop_;
  200. /// Cached desktop GPU support flag.
  201. bool desktopSupport_;
  202. /// Passes.
  203. Vector<SharedPtr<Pass> > passes_;
  204. /// Pass index assignments.
  205. static HashMap<String, unsigned> passIndices;
  206. };
  207. }