Technique.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. //
  2. // Copyright (c) 2008-2014 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 "GraphicsDefs.h"
  24. #include "HashTable.h"
  25. #include "Resource.h"
  26. namespace Urho3D
  27. {
  28. class ShaderVariation;
  29. /// Lighting mode of a pass.
  30. enum PassLightingMode
  31. {
  32. LIGHTING_UNLIT,
  33. LIGHTING_PERVERTEX,
  34. LIGHTING_PERPIXEL
  35. };
  36. /// %Material rendering pass, which defines shaders and render state.
  37. class URHO3D_API Pass : public RefCounted
  38. {
  39. public:
  40. /// Construct.
  41. Pass(StringHash type);
  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 %Shader %Model 3.
  55. void SetIsSM3(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 type.
  69. const StringHash& GetType() const { return type_; }
  70. /// Return blend mode.
  71. BlendMode GetBlendMode() const { return blendMode_; }
  72. /// Return depth compare mode.
  73. CompareMode GetDepthTestMode() const { return depthTestMode_; }
  74. /// Return pass lighting mode.
  75. PassLightingMode GetLightingMode() const { return lightingMode_; }
  76. /// Return last shaders loaded frame number.
  77. unsigned GetShadersLoadedFrameNumber() const { return shadersLoadedFrameNumber_; }
  78. /// Return depth write mode.
  79. bool GetDepthWrite() const { return depthWrite_; }
  80. /// Return alpha masking hint.
  81. bool GetAlphaMask() const { return alphaMask_; }
  82. /// Return whether requires %Shader %Model 3.
  83. bool IsSM3() const { return isSM3_; }
  84. /// Return vertex shader name.
  85. const String& GetVertexShader() const { return vertexShaderName_; }
  86. /// Return pixel shader name.
  87. const String& GetPixelShader() const { return pixelShaderName_; }
  88. /// Return vertex shader defines.
  89. const String& GetVertexShaderDefines() const { return vertexShaderDefines_; }
  90. /// Return pixel shader defines.
  91. const String& GetPixelShaderDefines() const { return pixelShaderDefines_; }
  92. /// Return vertex shaders.
  93. Vector<SharedPtr<ShaderVariation> >& GetVertexShaders() { return vertexShaders_; }
  94. /// Return pixel shaders.
  95. Vector<SharedPtr<ShaderVariation> >& GetPixelShaders() { return pixelShaders_; }
  96. private:
  97. /// Pass type.
  98. StringHash type_;
  99. /// Blend mode.
  100. BlendMode blendMode_;
  101. /// Depth compare mode.
  102. CompareMode depthTestMode_;
  103. /// Lighting mode.
  104. PassLightingMode lightingMode_;
  105. /// Last shaders loaded frame number.
  106. unsigned shadersLoadedFrameNumber_;
  107. /// Depth write mode.
  108. bool depthWrite_;
  109. /// Alpha masking hint.
  110. bool alphaMask_;
  111. /// Require %Shader %Model 3 flag.
  112. bool isSM3_;
  113. /// Vertex shader name.
  114. String vertexShaderName_;
  115. /// Pixel shader name.
  116. String pixelShaderName_;
  117. /// Vertex shader defines.
  118. String vertexShaderDefines_;
  119. /// Pixel shader defines.
  120. String pixelShaderDefines_;
  121. /// Vertex shaders.
  122. Vector<SharedPtr<ShaderVariation> > vertexShaders_;
  123. /// Pixel shaders.
  124. Vector<SharedPtr<ShaderVariation> > pixelShaders_;
  125. };
  126. /// %Material technique. Consists of several passes.
  127. class URHO3D_API Technique : public Resource
  128. {
  129. OBJECT(Technique);
  130. friend class Renderer;
  131. public:
  132. /// Construct.
  133. Technique(Context* context);
  134. /// Destruct.
  135. ~Technique();
  136. /// Register object factory.
  137. static void RegisterObject(Context* context);
  138. /// Load resource. Return true if successful.
  139. virtual bool Load(Deserializer& source);
  140. /// Set whether requires %Shader %Model 3.
  141. void SetIsSM3(bool enable);
  142. /// Create a new pass.
  143. Pass* CreatePass(StringHash type);
  144. /// Remove a pass.
  145. void RemovePass(StringHash type);
  146. /// Reset shader pointers in all passes.
  147. void ReleaseShaders();
  148. /// Return whether requires %Shader %Model 3.
  149. bool IsSM3() const { return isSM3_; }
  150. /// Return whether has a pass.
  151. bool HasPass(StringHash type) const { return passes_.Find(type.Value()) != 0; }
  152. /// Return a pass, or null if not found.
  153. Pass* GetPass(StringHash type) const
  154. {
  155. SharedPtr<Pass>* passPtr = passes_.Find(type.Value());
  156. return passPtr ? passPtr->Get() : 0;
  157. }
  158. /// Return a pass that is supported for rendering, or null if not found.
  159. Pass* GetSupportedPass(StringHash type) const
  160. {
  161. SharedPtr<Pass>* passPtr = passes_.Find(type.Value());
  162. Pass* pass = passPtr ? passPtr->Get() : 0;
  163. return pass && (!pass->IsSM3() || sm3Support_) ? pass : 0;
  164. }
  165. private:
  166. /// Require %Shader %Model 3 flag.
  167. bool isSM3_;
  168. /// Cached %Shader %Model 3 support flag.
  169. bool sm3Support_;
  170. /// Passes.
  171. HashTable<SharedPtr<Pass>, 16> passes_;
  172. };
  173. }