2
0

Technique.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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 vertex shader name.
  55. void SetVertexShader(const String& name);
  56. /// Set pixel shader name.
  57. void SetPixelShader(const String& name);
  58. /// Set vertex shader defines.
  59. void SetVertexShaderDefines(const String& defines);
  60. /// Set pixel shader defines.
  61. void SetPixelShaderDefines(const String& defines);
  62. /// Reset shader pointers.
  63. void ReleaseShaders();
  64. /// Mark shaders loaded this frame.
  65. void MarkShadersLoaded(unsigned frameNumber);
  66. /// Return pass type.
  67. const StringHash& GetType() const { return type_; }
  68. /// Return blend mode.
  69. BlendMode GetBlendMode() const { return blendMode_; }
  70. /// Return depth compare mode.
  71. CompareMode GetDepthTestMode() const { return depthTestMode_; }
  72. /// Return pass lighting mode.
  73. PassLightingMode GetLightingMode() const { return lightingMode_; }
  74. /// Return last shaders loaded frame number.
  75. unsigned GetShadersLoadedFrameNumber() const { return shadersLoadedFrameNumber_; }
  76. /// Return depth write mode.
  77. bool GetDepthWrite() const { return depthWrite_; }
  78. /// Return alpha masking hint.
  79. bool GetAlphaMask() const { return alphaMask_; }
  80. /// Return vertex shader name.
  81. const String& GetVertexShader() const { return vertexShaderName_; }
  82. /// Return pixel shader name.
  83. const String& GetPixelShader() const { return pixelShaderName_; }
  84. /// Return vertex shader defines.
  85. const String& GetVertexShaderDefines() const { return vertexShaderDefines_; }
  86. /// Return pixel shader defines.
  87. const String& GetPixelShaderDefines() const { return pixelShaderDefines_; }
  88. /// Return vertex shaders.
  89. Vector<SharedPtr<ShaderVariation> >& GetVertexShaders() { return vertexShaders_; }
  90. /// Return pixel shaders.
  91. Vector<SharedPtr<ShaderVariation> >& GetPixelShaders() { return pixelShaders_; }
  92. private:
  93. /// Pass type.
  94. StringHash type_;
  95. /// Blend mode.
  96. BlendMode blendMode_;
  97. /// Depth compare mode.
  98. CompareMode depthTestMode_;
  99. /// Lighting mode.
  100. PassLightingMode lightingMode_;
  101. /// Last shaders loaded frame number.
  102. unsigned shadersLoadedFrameNumber_;
  103. /// Depth write mode.
  104. bool depthWrite_;
  105. /// Alpha masking hint.
  106. bool alphaMask_;
  107. /// Vertex shader name.
  108. String vertexShaderName_;
  109. /// Pixel shader name.
  110. String pixelShaderName_;
  111. /// Vertex shader defines.
  112. String vertexShaderDefines_;
  113. /// Pixel shader defines.
  114. String pixelShaderDefines_;
  115. /// Vertex shaders.
  116. Vector<SharedPtr<ShaderVariation> > vertexShaders_;
  117. /// Pixel shaders.
  118. Vector<SharedPtr<ShaderVariation> > pixelShaders_;
  119. };
  120. /// %Material technique. Consists of several passes.
  121. class URHO3D_API Technique : public Resource
  122. {
  123. OBJECT(Technique);
  124. friend class Renderer;
  125. public:
  126. /// Construct.
  127. Technique(Context* context);
  128. /// Destruct.
  129. ~Technique();
  130. /// Register object factory.
  131. static void RegisterObject(Context* context);
  132. /// Load resource. Return true if successful.
  133. virtual bool Load(Deserializer& source);
  134. /// Set whether requires %Shader %Model 3.
  135. void SetIsSM3(bool enable);
  136. /// Create a new pass.
  137. Pass* CreatePass(StringHash type);
  138. /// Remove a pass.
  139. void RemovePass(StringHash type);
  140. /// Reset shader pointers in all passes.
  141. void ReleaseShaders();
  142. /// Return whether requires %Shader %Model 3.
  143. bool IsSM3() const { return isSM3_; }
  144. /// Return whether has a pass.
  145. bool HasPass(StringHash type) const { return passes_.Find(type.Value()) != 0; }
  146. /// Return a pass, or null if not found.
  147. Pass* GetPass(StringHash type) const
  148. {
  149. SharedPtr<Pass>* passPtr = passes_.Find(type.Value());
  150. return passPtr ? passPtr->Get() : 0;
  151. }
  152. private:
  153. /// Require %Shader %Model 3 flag.
  154. bool isSM3_;
  155. /// Passes.
  156. HashTable<SharedPtr<Pass>, 16> passes_;
  157. };
  158. }